summaryrefslogtreecommitdiff
path: root/lib/astal/io/variable.vala
blob: 312a27afc9a0d7970e559ad0af344a6649548f9c (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Base class for [[email protected]] mainly meant to be used
 * in higher level language bindings such as Lua and Gjs.
 */
public class AstalIO.VariableBase : Object {
    public signal void changed ();
    public signal void dropped ();
    public signal void error (string err);

    // lua-lgi crashes when using its emitting mechanism
    public void emit_changed() { changed(); }
    public void emit_dropped() { dropped(); }
    public void emit_error(string err) { this.error(err); }

    ~VariableBase() {
        dropped();
    }
}

public class AstalIO.Variable : VariableBase {
    public Value value { owned get; set; }

    private uint poll_id = 0;
    private Process? watch_proc;

    private uint poll_interval { get; set; default = 1000; }
    private string[] poll_exec { get; set; }
    private Closure? poll_transform { get; set; }
    private Closure? poll_fn { get; set; }

    private Closure? watch_transform { get; set; }
    private string[] watch_exec { get; set; }

    public Variable(Value init) {
        Object(value: init);
    }

    public Variable poll(
        uint interval,
        string exec,
        Closure? transform
    ) throws Error {
        string[] argv;
        Shell.parse_argv(exec, out argv);
        return pollv(interval, argv, transform);
    }

    public Variable pollv(
        uint interval,
        string[] execv,
        Closure? transform
    ) throws Error {
        if (is_polling())
            stop_poll();

        poll_interval = interval;
        poll_exec = execv;
        poll_transform = transform;
        poll_fn = null;
        start_poll();
        return this;
    }

    public Variable pollfn(
        uint interval,
        Closure fn
    ) throws Error {
        if (is_polling())
            stop_poll();

        poll_interval = interval;
        poll_fn = fn;
        poll_exec = null;
        start_poll();
        return this;
    }

    public Variable watch(
        string exec,
        Closure? transform
    ) throws Error {
        string[] argv;
        Shell.parse_argv(exec, out argv);
        return watchv(argv, transform);
    }

    public Variable watchv(
        string[] execv,
        Closure? transform
    ) throws Error {
        if (is_watching())
            stop_watch();

        watch_exec = execv;
        watch_transform = transform;
        start_watch();
        return this;
    }

    construct {
        notify["value"].connect(() => changed());
        dropped.connect(() => {
            if (is_polling())
                stop_poll();

            if (is_watching())
                stop_watch();
        });
    }

    private void set_closure(string val, Closure? transform) {
        if (transform != null) {
            var str = Value(typeof(string));
            str.set_string(val);

            var ret_val = Value(this.value.type());
            transform.invoke(ref ret_val, { str, this.value });
            this.value = ret_val;
        }
        else {
            if (this.value.type() == Type.STRING && this.value.get_string() == val)
                return;

            var str = Value(typeof(string));
            str.set_string(val);
            this.value = str;
        }
    }

    private void set_fn() {
        var ret_val = Value(this.value.type());
        poll_fn.invoke(ref ret_val, { this.value });
        this.value = ret_val;
    }

    public void start_poll() throws Error {
        return_if_fail(poll_id == 0);

        if (poll_fn != null) {
            set_fn();
            poll_id = Timeout.add(poll_interval, () => {
                set_fn();
                return Source.CONTINUE;
            }, Priority.DEFAULT);
        }
        if (poll_exec != null) {
            Process.exec_asyncv.begin(poll_exec, (_, res) => {
                try {
                    var str = Process.exec_asyncv.end(res);
                    set_closure(str, poll_transform);
                } catch (Error err) {
                    this.error(err.message);
                }
            });
            poll_id = Timeout.add(poll_interval, () => {
                Process.exec_asyncv.begin(poll_exec, (_, res) => {
                    try {
                        var str = Process.exec_asyncv.end(res);
                        set_closure(str, poll_transform);
                    } catch (Error err) {
                        this.error(err.message);
                        Source.remove(poll_id);
                        poll_id = 0;
                    }
                });
                return Source.CONTINUE;
            }, Priority.DEFAULT);
        }
    }

    public void start_watch() throws Error {
        return_if_fail(watch_proc == null);
        return_if_fail(watch_exec != null);

        watch_proc = new Process.subprocessv(watch_exec);
        watch_proc.stdout.connect((str) => set_closure(str, watch_transform));
        watch_proc.stderr.connect((str) => this.error(str));
    }

    public void stop_poll() {
        return_if_fail(poll_id != 0);
        Source.remove(poll_id);
        poll_id = 0;
    }

    public void stop_watch() {
        return_if_fail(watch_proc != null);
        watch_proc.kill();
        watch_proc = null;
    }

    public bool is_polling() { return poll_id > 0; }
    public bool is_watching() { return watch_proc != null; }

    ~Variable() {
        dropped();
    }
}