diff options
Diffstat (limited to 'src/livecode')
| -rw-r--r-- | src/livecode/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/livecode/context.cpp | 46 | ||||
| -rw-r--r-- | src/livecode/context.h | 29 | ||||
| -rw-r--r-- | src/livecode/script.cpp | 74 | ||||
| -rw-r--r-- | src/livecode/script.h | 56 |
5 files changed, 136 insertions, 70 deletions
diff --git a/src/livecode/CMakeLists.txt b/src/livecode/CMakeLists.txt index 96e1574fc..cd099dac7 100644 --- a/src/livecode/CMakeLists.txt +++ b/src/livecode/CMakeLists.txt @@ -3,6 +3,7 @@ set(livecode_SRC context.cpp input.cpp + script.cpp api/context.cpp api/input.cpp api/point.cpp diff --git a/src/livecode/context.cpp b/src/livecode/context.cpp index a22191400..b63641411 100644 --- a/src/livecode/context.cpp +++ b/src/livecode/context.cpp @@ -18,56 +18,12 @@ #include "display/sp-canvas-item.h" #include "livecode/context.h" +#include "livecode/script.h" #include "livecode/api/api.h" namespace Inkscape { namespace Livecode { -Script::Script(JanetTable *env, std::string const &path) - : env(env) - , function(nullptr) - , file(Gio::File::create_for_path(path)) - , monitor(file->monitor_file()) -{ - monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed)); - reload(); -} - -void Script::frame() { - if (function) { - Janet result; - janet_pcall(function, 0, NULL, &result, NULL); - } -} - -void Script::commit() { -} - -void Script::reload() { - try { - uint8_t *data; - size_t length; - file->load_contents((char *&)data, (gsize &)length); - - Janet result; - janet_dobytes(env, data, length, file->get_path().c_str(), &result); - if (janet_checktype(result, JANET_FUNCTION)) { - function = janet_unwrap_function(result); - } else { - g_message("Janet script didn't return a function"); - } - } catch (...) { - g_message("error loading file"); - } -} - -void Script::file_changed(const Glib::RefPtr<Gio::File>& file, - const Glib::RefPtr<Gio::File>& other_file, - Gio::FileMonitorEvent event) -{ - reload(); -} - Context::Context(SPDesktop *desktop) : desktop(desktop) , _mouse(*this) diff --git a/src/livecode/context.h b/src/livecode/context.h index 4ebb73609..520074968 100644 --- a/src/livecode/context.h +++ b/src/livecode/context.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later -#ifndef INK_LIVECODE_API_H -#define INK_LIVECODE_API_H +#ifndef INK_LIVECODE_CONTEXT_H +#define INK_LIVECODE_CONTEXT_H /* * Context for the livecoding script language @@ -16,15 +16,12 @@ #include <2geom/point.h> #include <2geom/rect.h> #include <gdk/gdk.h> -#include <giomm/file.h> -#include <giomm/filemonitor.h> #include <janet.h> #include "xml/repr.h" #include "desktop.h" #include "livecode/input.h" - class SPDocument; class SPItem; @@ -32,25 +29,7 @@ namespace Inkscape { namespace Livecode { class Mouse; - -class Script { -public: - Script(JanetTable *env, std::string const &path); - - void frame(); - void commit(); - -private: - JanetTable *env; - JanetFunction *function; - Glib::RefPtr<Gio::File> file; - Glib::RefPtr<Gio::FileMonitor> monitor; - - void reload(); - void file_changed(const Glib::RefPtr<Gio::File>& file, - const Glib::RefPtr<Gio::File>& other_file, - Gio::FileMonitorEvent event); -}; +class Script; class Context { public: @@ -115,7 +94,7 @@ private: } } -#endif // INK_LIVECODE_API_H +#endif // INK_LIVECODE_CONTEXT_H /* Local Variables: diff --git a/src/livecode/script.cpp b/src/livecode/script.cpp new file mode 100644 index 000000000..d4761847a --- /dev/null +++ b/src/livecode/script.cpp @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * A Janet script for the livecoding experience + * + * Authors: + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "livecode/script.h" + +namespace Inkscape { +namespace Livecode { + +Script::Script(JanetTable *env, std::string const &path) + : env(env) + , function(nullptr) + , file(Gio::File::create_for_path(path)) + , monitor(file->monitor_file()) +{ + monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed)); + reload(); +} + +void Script::frame() { + if (function) { + Janet result; + janet_pcall(function, 0, NULL, &result, NULL); + } +} + +void Script::commit() { +} + +void Script::reload() { + try { + uint8_t *data; + size_t length; + file->load_contents((char *&)data, (gsize &)length); + + Janet result; + janet_dobytes(env, data, length, file->get_path().c_str(), &result); + if (janet_checktype(result, JANET_FUNCTION)) { + function = janet_unwrap_function(result); + } else { + g_message("Janet script didn't return a function"); + } + } catch (...) { + g_message("error loading file"); + } +} + +void Script::file_changed(const Glib::RefPtr<Gio::File>& file, + const Glib::RefPtr<Gio::File>& other_file, + Gio::FileMonitorEvent event) +{ + reload(); +} + +} +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/livecode/script.h b/src/livecode/script.h new file mode 100644 index 000000000..9891d5c21 --- /dev/null +++ b/src/livecode/script.h @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#ifndef INK_LIVECODE_SCRIPT_H +#define INK_LIVECODE_SCRIPT_H + +/* + * A Janet script for the livecoding experience + * + * Authors: + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include <string> +#include <giomm/file.h> +#include <giomm/filemonitor.h> +#include <janet.h> + +namespace Inkscape { +namespace Livecode { + +class Script { +public: + Script(JanetTable *env, std::string const &path); + + void frame(); + void commit(); + +private: + JanetTable *env; + JanetFunction *function; + Glib::RefPtr<Gio::File> file; + Glib::RefPtr<Gio::FileMonitor> monitor; + + void reload(); + void file_changed(const Glib::RefPtr<Gio::File>& file, + const Glib::RefPtr<Gio::File>& other_file, + Gio::FileMonitorEvent event); +}; + +} +} + +#endif // INK_LIVECODE_SCRIPT_H + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : |
