Debian Codegraph @

Nasal/prop_key_handler.nas

raw
1 # Property Key Handler
2 # ------------------------------------------------------------------
3 # This is an extension mainly targeted at developers. It implements
4 # some useful tools for dealing with internal properties if enabled
5 # (Menu->Debug->Configure Development Extensions). To use this feature,
6 # press the '/'-key, then type a property path (using the <TAB> key to
7 # complete property path elements if possible), or a search string ...
8 #
9 #
10 # Commands:
11 #
12 # <property>=<value><CR> -> set property to value
13 # <property><CR> -> print property and value to screen and terminal
14 # <property>* -> print property and all children to terminal
15 # <property>! -> add property to display list (reset list with /!)
16 # <property>: -> open property browser in this property's directory
17 # <string>? -> print all properties whose path or value contains this string
18 # <string># -> add listener for property and all child properties (cancel all listeners with /#)
19 #
20 #
21 # Keys:
22 #
23 # <CR> ... carriage return or enter, to confirm some operations
24 # <TAB> ... complete property path element (if possible), or
25 # cycle through available elements
26 # <Shift-TAB> ... like <TAB> but cycles backwards
27 # <CurUp>/<CurDown> ... switch back/forth in the history
28 # <Escape> ... cancel the operation
29 # <Shift-Backspace> ... remove last whole path element
30 #
31 #
32 # Colors:
33 #
34 # white ... syntactically correct path to not yet existing property
35 # green ... path to existing property
36 # red ... broken path syntax (e.g. "/foo*bar" ... '*' not allowed)
37 # yellow ... while typing in value for a valid property path
38 # magenta ... while typing search string (except when first character is '/')
39 #
40 #
41 # For example, to open the property browser in /position/, type '/p<TAB>:'.
42
43
44 var listener = nil;
45 var input = nil; # what the user typed (doesn't contain unconfirmed autocompleted parts)
46 var text = nil; # what is shown in the popup
47 var state = nil;
48 var listeners = [];
49 var listeners_children = {};
50
51 var completion = [];
52 var completion_pos = -1;
53 var history = [];
54 var history_pos = -1;
55
56
57 var start = func {
58 state = parse_input(text = "");
59 handle_key(`/`, 0);
60
61 listener = setlistener("/devices/status/keyboard/event", func(event) {
62 if (!event.getNode("pressed").getValue())
63 return;
64 var key = event.getNode("key");
65 var shift = event.getNode("modifier/shift").getValue();
66 if (handle_key(key.getValue(), shift))
67 key.setValue(-1); # drop key event
68 });
69 }
70
71
72 var stop = func(save_history = 0) {
73 removelistener(listener);
74 if (save_history and (!size(history) or !streq(history[-1], text)))
75 append(history, text);
76
77 history_pos = size(history);
78 gui.popdown();
79 }
80
81
82 var handle_key = func(key, shift) {
83 if (key == 357) { # up
84 set_history(-1);
85
86 } elsif (key == 359) { # down
87 set_history(1);
88
89 } elsif (key == `\n` or key == `\r`) {
90 if (state.error)
91 return 1;
92 if (state.value != nil)
93 setprop(state.path, state.value);
94
95 var n = props.globals.getNode(state.path);
96 var s = state.path;
97 if (n != nil) {
98 print_prop(n);
99 var v = n.getValue();
100 s ~= " = " ~ (v == nil ? "<nil>" : v);
101 } else {
102 s ~= " does not exist";
103 }
104 screen.log.write(s, 1, 1, 1);
105 stop(1);
106 return 1;
107
108 } elsif (key == 27) { # escape -> cancel
109 stop(0);
110 return 1;
111
112 } elsif (key == `\t`) { # tab
113 if (size(text) and text[0] == `/`) {
114 text = complete(input, shift ? -1 : 1);
115 build_completion(input);
116 var n = call(func { props.globals.getNode(text) }, [], var err = []);
117 if (!size(err) and n != nil and n.getAttribute("children") and size(completion) == 1)
118 handle_key(`/`, 0);
119 }
120
121 } elsif (key == 8) { # backspace
122 if (shift) { # + shift: remove one path element
123 input = text = state.parent.getPath();
124 if (text == "")
125 handle_key(`/`, 0);
126 } else {
127 input = text = substr(text, 0, size(text) - 1);
128 if (text == "")
129 stop(); # nothing in our field? close the dialog
130 }
131 completion_pos = -1;
132
133 } elsif (!string.isprint(key)) {
134 return 0; # pass other funny events
135
136 } elsif (key == `?` and state.value == nil) {
137 text = substr(text, 1);
138 print("\n-- property search: '", text, "' ----------------------------------");
139 search(props.globals, text);
140 print("-- done --\n");
141 stop(0);
142 return 1;
143
144 } elsif (key == `!` and state.node != nil and state.value == nil) {
145 if (!state.node.getPath()) {
146 screen.property_display.reset();
147 stop(0);
148 } else {
149 screen.property_display.add(state.node);
150 stop(1);
151 }
152 return 1;
153
154 } elsif (key == `*` and state.node != nil and state.value == nil) {
155 debug.tree(state.node);
156 stop(1);
157 return 1;
158
159 } elsif (key == `#`) {
160 # Add a listener.
161 #printf("received '#'. state.path=%s", view.str(state.path));
162 if (state.path == '/') {
163 foreach( var l; listeners) {
164 removelistener(l);
165 }
166 listeners = [];
167 listeners_children = {};
168 var text = sprintf("number of listeners=%s", size(listeners));
169 print(text);
170 screen.log.write(text, 1, 1, 1);
171 }
172 else {
173 var path = state.path;
174 node = props.globals.getNode(path);
175 if (node == nil) {
176 var text = "cannot find node for path=" ~ path;
177 print(text);
178 screen.log.write(text, 1, 1, 1);
179 }
180 else {
181 var listener = func(n) {
182 path = n.getPath();
183 value = n.getValue();
184 value_prev = listeners_children[path];
185
186 # We store old values in listeners_children[], so
187 # that we avoid printing lots of diagnostics for
188 # writes that don't change the value.
189 #
190 # setlistener() only stores the main node's value,
191 # so we have to do this by hand for child nodes.
192 #
193 # [In any case, the setlistener() API isn't able to
194 # support both listening to child nodes and avoid
195 # reporting non-changing write accesses.]
196 #
197 listeners_children[path] = value;
198 if (value != value_prev) {
199 if (value == nil) value = "<nil>";
200 var text = path ~ "=" ~ value;
201 print(text);
202 screen.log.write(text, 1, 1, 1);
203 }
204 }
205 var l = setlistener(node, listener, 0, 2);
206 append(listeners, l);
207 var text = sprintf("number of listeners=%s", size(listeners));
208 print(text);
209 screen.log.write(text, 1, 1, 1);
210 }
211 }
212 stop(1);
213 return 1;
214
215 } elsif (key == `:` and state.node != nil and state.value == nil) {
216 var n = state.node.getAttribute("children") ? state.node : state.parent;
217 gui.property_browser(n);
218 stop(1);
219 return 1;
220
221 } else {
222 text ~= chr(key);
223 input = text;
224 completion_pos = -1;
225 history_pos = size(history);
226 }
227
228 state = parse_input(text);
229 build_completion(input);
230
231 var color = nil;
232 if (size(text) and text[0] != `/`) # search mode (magenta)
233 color = set_color(1, 0.4, 0.9);
234 elsif (state.error) # error mode (red)
235 color = set_color(1, 0.4, 0.4);
236 elsif (state.value != nil) # value edit mode (yellow)
237 color = set_color(1, 0.8, 0);
238 elsif (state.node != nil) # existing node (green)
239 color = set_color(0.7, 1, 0.7);
240
241 gui.popupTip(text, 1000000, color);
242 return 1; # discard key event
243 }
244
245
246 var parse_input = func(expr) {
247 var path = expr;
248 var value = nil;
249
250 if ((var pos = find("=", expr)) >= 0) {
251 path = substr(expr, 0, pos);
252 value = substr(expr, pos + 1);
253 }
254
255 # split argument in parent and name
256 var last = 0;
257 while ((var pos = find("/", path, last + 1)) > 0)
258 last = pos;
259 var parent = substr(path, 0, last); # without trailing /
260 var raw_name = substr(path, last + 1);
261 var name = raw_name;
262 if ((var pos = find("[", name)) >= 0)
263 name = substr(name, 0, pos);
264 var node = nil;
265
266 # run dangerous operations in cage (the paths might be invalid)
267 call(func {
268 parent = props.globals.getNode(parent);
269 node = props.globals.getNode(path);
270 }, [], var error = []);
271
272 return {
273 error: size(error),
274 path: path,
275 value: value,
276 raw_name: raw_name, # "binding[1"
277 name: name, # "binding"
278 parent: parent,
279 node: node,
280 };
281 }
282
283
284 var build_completion = func(in) {
285 completion = [];
286 var s = parse_input(in);
287 if (s.error or s.parent == nil)
288 return;
289
290 foreach (var c; s.parent.getChildren()) {
291 var index = c.getIndex();
292 var name = c.getName();
293 var fullname = name;
294 if (index > 0)
295 fullname ~= "[" ~ index ~ "]";
296 if (substr(fullname, 0, size(s.raw_name)) == s.raw_name)
297 append(completion, [fullname, name, index]);
298 }
299 completion = sort(completion, func(a, b) cmp(a[1], b[1]) or a[2] - b[2]);
300 #print(debug.string([completion_pos, completion]), "\n");
301 }
302
303
304 var complete = func(in, step) {
305 if (state.parent == nil or state.value != nil)
306 return in; # can't complete broken path or assignment
307
308 completion_pos += step;
309 if (completion_pos < 0)
310 completion_pos = size(completion) - 1;
311 elsif (completion_pos >= size(completion))
312 completion_pos = 0;
313
314 if (completion_pos < size(completion))
315 in = state.parent.getPath() ~ "/" ~ completion[completion_pos][0];
316
317 return in;
318 }
319
320
321 var set_history = func(step) {
322 history_pos += step;
323 if (history_pos < 0) {
324 history_pos = 0;
325 } elsif (history_pos >= size(history)) {
326 history_pos = size(history);
327 text = "";
328 } else {
329 text = history[history_pos];
330 }
331 input = text;
332 }
333
334
335 var set_color = func(r, g, b) {
336 return { text: { color: { red: r, green: g, blue: b } }};
337 }
338
339
340 var print_prop = func(n) {
341 print(n.getPath(), " = ", debug.string(n.getValue()), " ", debug.attributes(n));
342 }
343
344
345 var search = func(n, s) {
346 if (find(s, n.getPath()) >= 0)
347 print_prop(n);
348 elsif (n.getType() != "NONE") {
349 var f = call(func { return find(s, "" ~ n.getValue());}, nil, var err=[]);
350 if (size(err)) {
351 # This can happen if value is '-nan'.
352 printf("got error from n.getValue() for n.getPath()=%s", n.getPath());
353 }
354 else if (f >= 0) {
355 print_prop(n);
356 }
357 }
358 foreach (var c; n.getChildren())
359 search(c, s);
360 }
361
362
363 _setlistener("/sim/signals/nasal-dir-initialized", func {
364 foreach (var p; props.globals.getNode("/sim/gui/prop-key-handler/history", 1).getChildren("entry"))
365 append(history, p.getValue());
366 var max = props.globals.initNode("/sim/gui/prop-key-handler/history-max-size", 30).getValue();
367 if (size(history) > max)
368 history = subvec(history, size(history) - max);
369 });
370
371
372 _setlistener("/sim/signals/exit", func {
373 var max = props.globals.initNode("/sim/gui/prop-key-handler/history-max-size", 30).getValue();
374 if (size(history) > max)
375 history = subvec(history, size(history) - max);
376 forindex (var i; history) {
377 var p = props.globals.getNode("/sim/gui/prop-key-handler/history", 1).getChild("entry", i, 1);
378 p.setValue(history[i]);
379 p.setAttribute("userarchive", 1);
380 }
381 });
382
383