]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalClipboard.cxx
db148dce0a9875d24d61a91eba3d4b6a5d4d078a
[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( !naIsString(args[i]) )
38       naRuntimeError(c, "clipboard: invalid arg (not a string)");
39
40     std::string type_str( naStr_data(args[i]) );
41     boost::to_upper(type_str);
42
43     if( type_str == "CLIPBOARD" )
44       return NasalClipboard::CLIPBOARD;
45     else if( type_str == "PRIMARY" || type_str == "SELECTION" )
46       return NasalClipboard::PRIMARY;
47     else
48       naRuntimeError(c, "clipboard: unknown clipboard type");
49   }
50
51   return NasalClipboard::CLIPBOARD;
52 }
53
54 //------------------------------------------------------------------------------
55 static naRef f_setClipboardText(naContext c, naRef me, int argc, naRef* args)
56 {
57   if( argc < 1 || argc > 2 )
58     naRuntimeError( c, "clipboard.setText() expects 1 or 2 arguments: "
59                        "text, [, type = \"CLIPBOARD\"]" );
60
61   if( !naIsString(args[0]) )
62     naRuntimeError(c, "clipboard.setText() invalid arg (arg 0 not a string)");
63
64   return
65     naNum
66     (
67       NasalClipboard::getInstance()->setText( naStr_data(args[0]),
68                                               parseType(c, argc, args, 1) )
69     );
70 }
71
72 //------------------------------------------------------------------------------
73 static naRef f_getClipboardText(naContext c, naRef me, int argc, naRef* args)
74 {
75   if( argc > 1 )
76     naRuntimeError(c, "clipboard.getText() accepts max 1 arg: "
77                       "[type = \"CLIPBOARD\"]" );
78
79   const std::string& text =
80     NasalClipboard::getInstance()->getText(parseType(c, argc, args, 0));
81
82   // TODO create some nasal helper functions (eg. stringToNasal)
83   //      some functions are available spread over different files (eg.
84   //      NasalPositioned.cxx)
85   return naStr_fromdata(naNewString(c), text.c_str(), text.length());
86 }
87
88 //------------------------------------------------------------------------------
89 // Table of extension functions, terminate with 0,0
90 static struct {const char* name; naCFunction func; } funcs[] = {
91   { "setText", f_setClipboardText },
92   { "getText", f_getClipboardText },
93   { 0,0 } // TERMINATION
94 };
95
96 //------------------------------------------------------------------------------
97 NasalClipboard::Ptr NasalClipboard::_clipboard;
98 naRef NasalClipboard::_clipboard_hash;
99
100 //------------------------------------------------------------------------------
101 NasalClipboard::~NasalClipboard()
102 {
103
104 }
105
106 //------------------------------------------------------------------------------
107 void NasalClipboard::init(FGNasalSys *nasal)
108 {
109   _clipboard = create();
110   _clipboard_hash = naNewHash(nasal->context());
111
112   nasal->globalsSet("clipboard", _clipboard_hash);
113
114   for(size_t i=0;funcs[i].name;i++)
115   {
116     nasal->hashset
117     (
118       _clipboard_hash,
119       funcs[i].name,
120       naNewFunc(nasal->context(), naNewCCode(nasal->context(), funcs[i].func))
121     );
122
123     SG_LOG(SG_NASAL, SG_DEBUG, "Adding clipboard function: " << funcs[i].name );
124   }
125 }
126
127 //------------------------------------------------------------------------------
128 NasalClipboard::Ptr NasalClipboard::getInstance()
129 {
130   return _clipboard;
131 }