summaryrefslogtreecommitdiffstats
path: root/src/livecode/script.cpp
blob: d4761847acc1f7e2b7164a2d53382f9c357d1d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 :