move script to its own file
s-ol
3 years ago
2 | 2 | set(livecode_SRC |
3 | 3 | context.cpp |
4 | 4 | input.cpp |
5 | script.cpp | |
5 | 6 | api/context.cpp |
6 | 7 | api/input.cpp |
7 | 8 | api/point.cpp |
17 | 17 | #include "display/sp-canvas-item.h" |
18 | 18 | |
19 | 19 | #include "livecode/context.h" |
20 | #include "livecode/script.h" | |
20 | 21 | #include "livecode/api/api.h" |
21 | 22 | |
22 | 23 | namespace Inkscape { |
23 | 24 | namespace Livecode { |
24 | ||
25 | Script::Script(JanetTable *env, std::string const &path) | |
26 | : env(env) | |
27 | , function(nullptr) | |
28 | , file(Gio::File::create_for_path(path)) | |
29 | , monitor(file->monitor_file()) | |
30 | { | |
31 | monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed)); | |
32 | reload(); | |
33 | } | |
34 | ||
35 | void Script::frame() { | |
36 | if (function) { | |
37 | Janet result; | |
38 | janet_pcall(function, 0, NULL, &result, NULL); | |
39 | } | |
40 | } | |
41 | ||
42 | void Script::commit() { | |
43 | } | |
44 | ||
45 | void Script::reload() { | |
46 | try { | |
47 | uint8_t *data; | |
48 | size_t length; | |
49 | file->load_contents((char *&)data, (gsize &)length); | |
50 | ||
51 | Janet result; | |
52 | janet_dobytes(env, data, length, file->get_path().c_str(), &result); | |
53 | if (janet_checktype(result, JANET_FUNCTION)) { | |
54 | function = janet_unwrap_function(result); | |
55 | } else { | |
56 | g_message("Janet script didn't return a function"); | |
57 | } | |
58 | } catch (...) { | |
59 | g_message("error loading file"); | |
60 | } | |
61 | } | |
62 | ||
63 | void Script::file_changed(const Glib::RefPtr<Gio::File>& file, | |
64 | const Glib::RefPtr<Gio::File>& other_file, | |
65 | Gio::FileMonitorEvent event) | |
66 | { | |
67 | reload(); | |
68 | } | |
69 | 25 | |
70 | 26 | Context::Context(SPDesktop *desktop) |
71 | 27 | : desktop(desktop) |
0 | 0 | // SPDX-License-Identifier: GPL-2.0-or-later |
1 | #ifndef INK_LIVECODE_API_H | |
2 | #define INK_LIVECODE_API_H | |
1 | #ifndef INK_LIVECODE_CONTEXT_H | |
2 | #define INK_LIVECODE_CONTEXT_H | |
3 | 3 | |
4 | 4 | /* |
5 | 5 | * Context for the livecoding script language |
15 | 15 | #include <2geom/point.h> |
16 | 16 | #include <2geom/rect.h> |
17 | 17 | #include <gdk/gdk.h> |
18 | #include <giomm/file.h> | |
19 | #include <giomm/filemonitor.h> | |
20 | 18 | #include <janet.h> |
21 | 19 | |
22 | 20 | #include "xml/repr.h" |
23 | 21 | #include "desktop.h" |
24 | 22 | #include "livecode/input.h" |
25 | ||
26 | 23 | |
27 | 24 | class SPDocument; |
28 | 25 | class SPItem; |
31 | 28 | namespace Livecode { |
32 | 29 | |
33 | 30 | class Mouse; |
34 | ||
35 | class Script { | |
36 | public: | |
37 | Script(JanetTable *env, std::string const &path); | |
38 | ||
39 | void frame(); | |
40 | void commit(); | |
41 | ||
42 | private: | |
43 | JanetTable *env; | |
44 | JanetFunction *function; | |
45 | Glib::RefPtr<Gio::File> file; | |
46 | Glib::RefPtr<Gio::FileMonitor> monitor; | |
47 | ||
48 | void reload(); | |
49 | void file_changed(const Glib::RefPtr<Gio::File>& file, | |
50 | const Glib::RefPtr<Gio::File>& other_file, | |
51 | Gio::FileMonitorEvent event); | |
52 | }; | |
31 | class Script; | |
53 | 32 | |
54 | 33 | class Context { |
55 | 34 | public: |
114 | 93 | } |
115 | 94 | } |
116 | 95 | |
117 | #endif // INK_LIVECODE_API_H | |
96 | #endif // INK_LIVECODE_CONTEXT_H | |
118 | 97 | |
119 | 98 | /* |
120 | 99 | Local Variables: |
0 | // SPDX-License-Identifier: GPL-2.0-or-later | |
1 | /* | |
2 | * A Janet script for the livecoding experience | |
3 | * | |
4 | * Authors: | |
5 | * Sol Bekic <s+inkscape@s-ol.nu> | |
6 | * Copyright (C) 2019 Authors | |
7 | * | |
8 | * Released under GNU GPL v2+, read the file 'COPYING' for more information. | |
9 | */ | |
10 | ||
11 | #include "livecode/script.h" | |
12 | ||
13 | namespace Inkscape { | |
14 | namespace Livecode { | |
15 | ||
16 | Script::Script(JanetTable *env, std::string const &path) | |
17 | : env(env) | |
18 | , function(nullptr) | |
19 | , file(Gio::File::create_for_path(path)) | |
20 | , monitor(file->monitor_file()) | |
21 | { | |
22 | monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed)); | |
23 | reload(); | |
24 | } | |
25 | ||
26 | void Script::frame() { | |
27 | if (function) { | |
28 | Janet result; | |
29 | janet_pcall(function, 0, NULL, &result, NULL); | |
30 | } | |
31 | } | |
32 | ||
33 | void Script::commit() { | |
34 | } | |
35 | ||
36 | void Script::reload() { | |
37 | try { | |
38 | uint8_t *data; | |
39 | size_t length; | |
40 | file->load_contents((char *&)data, (gsize &)length); | |
41 | ||
42 | Janet result; | |
43 | janet_dobytes(env, data, length, file->get_path().c_str(), &result); | |
44 | if (janet_checktype(result, JANET_FUNCTION)) { | |
45 | function = janet_unwrap_function(result); | |
46 | } else { | |
47 | g_message("Janet script didn't return a function"); | |
48 | } | |
49 | } catch (...) { | |
50 | g_message("error loading file"); | |
51 | } | |
52 | } | |
53 | ||
54 | void Script::file_changed(const Glib::RefPtr<Gio::File>& file, | |
55 | const Glib::RefPtr<Gio::File>& other_file, | |
56 | Gio::FileMonitorEvent event) | |
57 | { | |
58 | reload(); | |
59 | } | |
60 | ||
61 | } | |
62 | } | |
63 | ||
64 | /* | |
65 | Local Variables: | |
66 | mode:c++ | |
67 | c-file-style:"stroustrup" | |
68 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | |
69 | indent-tabs-mode:nil | |
70 | fill-column:99 | |
71 | End: | |
72 | */ | |
73 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : |
0 | // SPDX-License-Identifier: GPL-2.0-or-later | |
1 | #ifndef INK_LIVECODE_SCRIPT_H | |
2 | #define INK_LIVECODE_SCRIPT_H | |
3 | ||
4 | /* | |
5 | * A Janet script for the livecoding experience | |
6 | * | |
7 | * Authors: | |
8 | * Sol Bekic <s+inkscape@s-ol.nu> | |
9 | * Copyright (C) 2019 Authors | |
10 | * | |
11 | * Released under GNU GPL v2+, read the file 'COPYING' for more information. | |
12 | */ | |
13 | ||
14 | #include <string> | |
15 | #include <giomm/file.h> | |
16 | #include <giomm/filemonitor.h> | |
17 | #include <janet.h> | |
18 | ||
19 | namespace Inkscape { | |
20 | namespace Livecode { | |
21 | ||
22 | class Script { | |
23 | public: | |
24 | Script(JanetTable *env, std::string const &path); | |
25 | ||
26 | void frame(); | |
27 | void commit(); | |
28 | ||
29 | private: | |
30 | JanetTable *env; | |
31 | JanetFunction *function; | |
32 | Glib::RefPtr<Gio::File> file; | |
33 | Glib::RefPtr<Gio::FileMonitor> monitor; | |
34 | ||
35 | void reload(); | |
36 | void file_changed(const Glib::RefPtr<Gio::File>& file, | |
37 | const Glib::RefPtr<Gio::File>& other_file, | |
38 | Gio::FileMonitorEvent event); | |
39 | }; | |
40 | ||
41 | } | |
42 | } | |
43 | ||
44 | #endif // INK_LIVECODE_SCRIPT_H | |
45 | ||
46 | /* | |
47 | Local Variables: | |
48 | mode:c++ | |
49 | c-file-style:"stroustrup" | |
50 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | |
51 | indent-tabs-mode:nil | |
52 | fill-column:99 | |
53 | End: | |
54 | */ | |
55 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : |