1 // X11 implementation of clipboard access for Nasal
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
23 #include "NasalClipboard.hxx"
24 #include "NasalSys.hxx"
26 #include <boost/algorithm/string/case_conv.hpp>
30 * Nasal wrappers for setting/getting clipboard text
32 //------------------------------------------------------------------------------
33 static NasalClipboard::Type parseType(naContext c, int argc, naRef* args, int i)
37 if( naIsNum(args[i]) )
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;
48 "clipboard: invalid arg "
49 "(expected clipboard.CLIPBOARD or clipboard.SELECTION)"
53 return NasalClipboard::CLIPBOARD;
56 //------------------------------------------------------------------------------
57 static naRef f_setClipboardText(naContext c, naRef me, int argc, naRef* args)
59 if( argc < 1 || argc > 2 )
60 naRuntimeError( c, "clipboard.setText() expects 1 or 2 arguments: "
61 "text, [, type = clipboard.CLIPBOARD]" );
63 if( !naIsString(args[0]) )
64 naRuntimeError(c, "clipboard.setText() invalid arg (arg 0 not a string)");
69 NasalClipboard::getInstance()->setText( naStr_data(args[0]),
70 parseType(c, argc, args, 1) )
74 //------------------------------------------------------------------------------
75 static naRef f_getClipboardText(naContext c, naRef me, int argc, naRef* args)
78 naRuntimeError(c, "clipboard.getText() accepts max 1 arg: "
79 "[type = clipboard.CLIPBOARD]" );
81 const std::string& text =
82 NasalClipboard::getInstance()->getText(parseType(c, argc, args, 0));
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());
90 //------------------------------------------------------------------------------
91 // Table of extension functions
92 static struct {const char* name; naCFunction func; } funcs[] = {
93 { "setText", f_setClipboardText },
94 { "getText", f_getClipboardText }
97 // Table of extension symbols
98 static struct {const char* name; naRef val; } symbols[] = {
99 { "CLIPBOARD", naNum(NasalClipboard::CLIPBOARD) },
100 { "SELECTION", naNum(NasalClipboard::PRIMARY) }
103 //------------------------------------------------------------------------------
104 NasalClipboard::Ptr NasalClipboard::_clipboard;
105 naRef NasalClipboard::_clipboard_hash;
107 //------------------------------------------------------------------------------
108 NasalClipboard::~NasalClipboard()
113 //------------------------------------------------------------------------------
114 void NasalClipboard::init(FGNasalSys *nasal)
116 _clipboard = create();
117 _clipboard_hash = naNewHash(nasal->context());
119 nasal->globalsSet("clipboard", _clipboard_hash);
121 for( size_t i = 0; i < sizeof(funcs)/sizeof(funcs[0]); ++i )
127 naNewFunc(nasal->context(), naNewCCode(nasal->context(), funcs[i].func))
130 SG_LOG(SG_NASAL, SG_DEBUG, "Adding clipboard function: " << funcs[i].name);
133 for( size_t i = 0; i < sizeof(symbols)/sizeof(symbols[0]); ++i )
135 nasal->hashset(_clipboard_hash, symbols[i].name, symbols[i].val);
137 SG_LOG(SG_NASAL, SG_DEBUG, "Adding clipboard symbol: " << symbols[i].name);
141 //------------------------------------------------------------------------------
142 NasalClipboard::Ptr NasalClipboard::getInstance()