diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2019-12-02 20:41:05 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2019-12-02 20:41:05 +0000 |
| commit | 1820294e9ea199058ba4d0626b23992e08210b44 (patch) | |
| tree | 8b6574d814b991dbcca3eaa92e557402bfee6b3e /src/livecode/script.cpp | |
| parent | more janet bindings (diff) | |
| download | inkscape-1820294e9ea199058ba4d0626b23992e08210b44.tar.gz inkscape-1820294e9ea199058ba4d0626b23992e08210b44.zip | |
move script to its own file
Diffstat (limited to '')
| -rw-r--r-- | src/livecode/script.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
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 : |
