| 1 | // X11 implementation of clipboard access for Nasal |
| 2 | // |
| 3 | // Copyright (C) 2012 Thomas Geymayer <[email protected]> |
| 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 | #include <simgear/nasal/cppbind/NasalCallContext.hxx> |
| 26 | |
| 27 | #include <cstddef> |
| 28 | |
| 29 | /* |
| 30 | * Nasal wrappers for setting/getting clipboard text |
| 31 | */ |
| 32 | //------------------------------------------------------------------------------ |
| 33 | static NasalClipboard::Type parseType(const nasal::CallContext& ctx, size_t i) |
| 34 | { |
| 35 | if( ctx.argc > i ) |
| 36 | { |
| 37 | if( ctx.isNumeric(i) ) |
| 38 | { |
| 39 | if( ctx.requireArg<int>(i) == NasalClipboard::CLIPBOARD ) |
| 40 | return NasalClipboard::CLIPBOARD; |
| 41 | if( ctx.requireArg<int>(i) == NasalClipboard::PRIMARY ) |
| 42 | return NasalClipboard::PRIMARY; |
| 43 | } |
| 44 | |
| 45 | ctx.runtimeError("clipboard: invalid arg " |
| 46 | "(expected clipboard.CLIPBOARD or clipboard.SELECTION)"); |
| 47 | } |
| 48 | |
| 49 | return NasalClipboard::CLIPBOARD; |
| 50 | } |
| 51 | |
| 52 | //------------------------------------------------------------------------------ |
| 53 | static naRef f_setClipboardText(const nasal::CallContext& ctx) |
| 54 | { |
| 55 | if( ctx.argc < 1 || ctx.argc > 2 ) |
| 56 | ctx.runtimeError("clipboard.setText() expects 1 or 2 arguments: " |
| 57 | "text, [, type = clipboard.CLIPBOARD]"); |
| 58 | |
| 59 | return |
| 60 | naNum |
| 61 | ( |
| 62 | NasalClipboard::getInstance()->setText( ctx.requireArg<std::string>(0), |
| 63 | parseType(ctx, 1) ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | //------------------------------------------------------------------------------ |
| 68 | static naRef f_getClipboardText(const nasal::CallContext& ctx) |
| 69 | { |
| 70 | if( ctx.argc > 1 ) |
| 71 | ctx.runtimeError("clipboard.getText() accepts max 1 arg: " |
| 72 | "[type = clipboard.CLIPBOARD]"); |
| 73 | |
| 74 | return ctx.to_nasal |
| 75 | ( |
| 76 | NasalClipboard::getInstance()->getText(parseType(ctx, 0)) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | //------------------------------------------------------------------------------ |
| 81 | NasalClipboard::Ptr NasalClipboard::_clipboard; |
| 82 | |
| 83 | //------------------------------------------------------------------------------ |
| 84 | NasalClipboard::~NasalClipboard() |
| 85 | { |
| 86 | |
| 87 | } |
| 88 | |
| 89 | //------------------------------------------------------------------------------ |
| 90 | void NasalClipboard::init(FGNasalSys *nasal) |
| 91 | { |
| 92 | _clipboard = create(); |
| 93 | |
| 94 | nasal::Hash clipboard = nasal->getGlobals().createHash("clipboard"); |
| 95 | |
| 96 | clipboard.set("setText", f_setClipboardText); |
| 97 | clipboard.set("getText", f_getClipboardText); |
| 98 | clipboard.set("CLIPBOARD", NasalClipboard::CLIPBOARD); |
| 99 | clipboard.set("SELECTION", NasalClipboard::PRIMARY); |
| 100 | } |
| 101 | |
| 102 | //------------------------------------------------------------------------------ |
| 103 | NasalClipboard::Ptr NasalClipboard::getInstance() |
| 104 | { |
| 105 | return _clipboard; |
| 106 | } |