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( !naIsString(args[i]) )
38 naRuntimeError(c, "clipboard: invalid arg (not a string)");
40 std::string type_str( naStr_data(args[i]) );
41 boost::to_upper(type_str);
43 if( type_str == "CLIPBOARD" )
44 return NasalClipboard::CLIPBOARD;
45 else if( type_str == "PRIMARY" || type_str == "SELECTION" )
46 return NasalClipboard::PRIMARY;
48 naRuntimeError(c, "clipboard: unknown clipboard type");
51 return NasalClipboard::CLIPBOARD;
54 //------------------------------------------------------------------------------
55 static naRef f_setClipboardText(naContext c, naRef me, int argc, naRef* args)
57 if( argc < 1 || argc > 2 )
58 naRuntimeError( c, "clipboard.setText() expects 1 or 2 arguments: "
59 "text, [, type = \"CLIPBOARD\"]" );
61 if( !naIsString(args[0]) )
62 naRuntimeError(c, "clipboard.setText() invalid arg (arg 0 not a string)");
67 NasalClipboard::getInstance()->setText( naStr_data(args[0]),
68 parseType(c, argc, args, 1) )
72 //------------------------------------------------------------------------------
73 static naRef f_getClipboardText(naContext c, naRef me, int argc, naRef* args)
76 naRuntimeError(c, "clipboard.getText() accepts max 1 arg: "
77 "[type = \"CLIPBOARD\"]" );
79 const std::string& text =
80 NasalClipboard::getInstance()->getText(parseType(c, argc, args, 0));
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());
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
96 //------------------------------------------------------------------------------
97 NasalClipboard::Ptr NasalClipboard::_clipboard;
98 naRef NasalClipboard::_clipboard_hash;
100 //------------------------------------------------------------------------------
101 NasalClipboard::~NasalClipboard()
106 //------------------------------------------------------------------------------
107 void NasalClipboard::init(FGNasalSys *nasal)
109 _clipboard = create();
110 _clipboard_hash = naNewHash(nasal->context());
112 nasal->globalsSet("clipboard", _clipboard_hash);
114 for(size_t i=0;funcs[i].name;i++)
120 naNewFunc(nasal->context(), naNewCCode(nasal->context(), funcs[i].func))
123 SG_LOG(SG_NASAL, SG_DEBUG, "Adding clipboard function: " << funcs[i].name );
127 //------------------------------------------------------------------------------
128 NasalClipboard::Ptr NasalClipboard::getInstance()