git.s-ol.nu inkscape / 1820294
move script to its own file s-ol 3 years ago
5 changed file(s) with 136 addition(s) and 70 deletion(s). Raw diff Collapse all Expand all
22 set(livecode_SRC
33 context.cpp
44 input.cpp
5 script.cpp
56 api/context.cpp
67 api/input.cpp
78 api/point.cpp
1717 #include "display/sp-canvas-item.h"
1818
1919 #include "livecode/context.h"
20 #include "livecode/script.h"
2021 #include "livecode/api/api.h"
2122
2223 namespace Inkscape {
2324 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 }
6925
7026 Context::Context(SPDesktop *desktop)
7127 : desktop(desktop)
00 // 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
33
44 /*
55 * Context for the livecoding script language
1515 #include <2geom/point.h>
1616 #include <2geom/rect.h>
1717 #include <gdk/gdk.h>
18 #include <giomm/file.h>
19 #include <giomm/filemonitor.h>
2018 #include <janet.h>
2119
2220 #include "xml/repr.h"
2321 #include "desktop.h"
2422 #include "livecode/input.h"
25
2623
2724 class SPDocument;
2825 class SPItem;
3128 namespace Livecode {
3229
3330 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;
5332
5433 class Context {
5534 public:
11493 }
11594 }
11695
117 #endif // INK_LIVECODE_API_H
96 #endif // INK_LIVECODE_CONTEXT_H
11897
11998 /*
12099 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 :