]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalClipboard.cxx
Reset: Nasal can be shutdown.
[flightgear.git] / src / Scripting / NasalClipboard.cxx
1 // X11 implementation of clipboard access for Nasal
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include "NasalClipboard.hxx"
24 #include "NasalSys.hxx"
25
26 #include <boost/algorithm/string/case_conv.hpp>
27 #include <cstddef>
28
29 /*
30  *  Nasal wrappers for setting/getting clipboard text
31  */
32 //------------------------------------------------------------------------------
33 static NasalClipboard::Type parseType(naContext c, int argc, naRef* args, int i)
34 {
35   if( argc > i )
36   {
37     if( naIsNum(args[i]) )
38     {
39       if( static_cast<int>(args[i].num) == NasalClipboard::CLIPBOARD )
40         return NasalClipboard::CLIPBOARD;
41       if( static_cast<int>(args[i].num) == NasalClipboard::PRIMARY )
42         return NasalClipboard::PRIMARY;
43     }
44
45     naRuntimeError
46     (
47       c,
48       "clipboard: invalid arg "
49       "(expected clipboard.CLIPBOARD or clipboard.SELECTION)"
50     );
51   }
52
53   return NasalClipboard::CLIPBOARD;
54 }
55
56 //------------------------------------------------------------------------------
57 static naRef f_setClipboardText(naContext c, naRef me, int argc, naRef* args)
58 {
59   if( argc < 1 || argc > 2 )
60     naRuntimeError( c, "clipboard.setText() expects 1 or 2 arguments: "
61                        "text, [, type = clipboard.CLIPBOARD]" );
62
63   if( !naIsString(args[0]) )
64     naRuntimeError(c, "clipboard.setText() invalid arg (arg 0 not a string)");
65
66   return
67     naNum
68     (
69       NasalClipboard::getInstance()->setText( naStr_data(args[0]),
70                                               parseType(c, argc, args, 1) )
71     );
72 }
73
74 //------------------------------------------------------------------------------
75 static naRef f_getClipboardText(naContext c, naRef me, int argc, naRef* args)
76 {
77   if( argc > 1 )
78     naRuntimeError(c, "clipboard.getText() accepts max 1 arg: "
79                       "[type = clipboard.CLIPBOARD]" );
80
81   const std::string& text =
82     NasalClipboard::getInstance()->getText(parseType(c, argc, args, 0));
83
84   // TODO create some nasal helper functions (eg. stringToNasal)
85   //      some functions are available spread over different files (eg.
86   //      NasalPositioned.cxx)
87   return naStr_fromdata(naNewString(c), text.c_str(), text.length());
88 }
89
90 //------------------------------------------------------------------------------
91 // Table of extension functions
92 static struct {const char* name; naCFunction func; } funcs[] = {
93   { "setText", f_setClipboardText },
94   { "getText", f_getClipboardText }
95 };
96
97 // Table of extension symbols
98 static struct {const char* name; naRef val; } symbols[] = {
99   { "CLIPBOARD", naNum(NasalClipboard::CLIPBOARD) },
100   { "SELECTION", naNum(NasalClipboard::PRIMARY) }
101 };
102
103 //------------------------------------------------------------------------------
104 NasalClipboard::Ptr NasalClipboard::_clipboard;
105 naRef NasalClipboard::_clipboard_hash;
106
107 //------------------------------------------------------------------------------
108 NasalClipboard::~NasalClipboard()
109 {
110
111 }
112
113 //------------------------------------------------------------------------------
114 void NasalClipboard::init(FGNasalSys *nasal)
115 {
116   _clipboard = create();
117   _clipboard_hash = naNewHash(nasal->context());
118
119   nasal->globalsSet("clipboard", _clipboard_hash);
120
121   for( size_t i = 0; i < sizeof(funcs)/sizeof(funcs[0]); ++i )
122   {
123     nasal->hashset
124     (
125       _clipboard_hash,
126       funcs[i].name,
127       naNewFunc(nasal->context(), naNewCCode(nasal->context(), funcs[i].func))
128     );
129
130     SG_LOG(SG_NASAL, SG_DEBUG, "Adding clipboard function: " << funcs[i].name);
131   }
132
133   for( size_t i = 0; i < sizeof(symbols)/sizeof(symbols[0]); ++i )
134   {
135     nasal->hashset(_clipboard_hash, symbols[i].name, symbols[i].val);
136
137     SG_LOG(SG_NASAL, SG_DEBUG, "Adding clipboard symbol: " << symbols[i].name);
138   }
139 }
140
141 //------------------------------------------------------------------------------
142 NasalClipboard::Ptr NasalClipboard::getInstance()
143 {
144   return _clipboard;
145 }