BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | # |
2 | # NCD input event handling example program. |
||
3 | # |
||
4 | # This program responds to volume key presses by synchronously calling an external |
||
5 | # script for muting and adjusting volume, and responds to power button presses by |
||
6 | # suspending using pm-suspend. |
||
7 | # |
||
8 | # It uses process_manager() and sys.watch_input() to dynamically create and remove |
||
9 | # processes that deal with specific input devices. The individual input device processes |
||
10 | # then use sys.evdev() to handle input events from their input device. |
||
11 | # |
||
12 | |||
13 | process events_main { |
||
14 | # Volume control script, called with argument "up", "down" or "mute". |
||
15 | var("/usr/local/bin/volumekey") volume_script; |
||
16 | |||
17 | # Suspend command. |
||
18 | list("/usr/sbin/pm-suspend") suspend_cmd; |
||
19 | |||
20 | provide("GLOBAL"); |
||
21 | } |
||
22 | |||
23 | process events_watcher { |
||
24 | depend("GLOBAL"); |
||
25 | |||
26 | # Create process manager. |
||
27 | process_manager() manager; |
||
28 | |||
29 | # Wait for input device events. |
||
30 | sys.watch_input("event") watcher; |
||
31 | |||
32 | # Dispatch. |
||
33 | concat("events_watcher_", watcher.event_type) func; |
||
34 | call(func, {}); |
||
35 | |||
36 | # Next event. |
||
37 | watcher->nextevent(); |
||
38 | } |
||
39 | |||
40 | template events_watcher_added { |
||
41 | # Start event handling process for this device. |
||
42 | _caller.manager->start(_caller.watcher.devname, "events_input_device", {_caller.watcher.devname}); |
||
43 | } |
||
44 | |||
45 | template events_watcher_removed { |
||
46 | # Stop event handling process for this device. |
||
47 | _caller.manager->stop(_caller.watcher.devname); |
||
48 | } |
||
49 | |||
50 | template events_input_device { |
||
51 | # Alias arguments. |
||
52 | var(_arg0) dev; |
||
53 | |||
54 | # Get global. |
||
55 | depend("GLOBAL") gdep; |
||
56 | |||
57 | # Wait for input events. |
||
58 | sys.evdev(dev) evdev; |
||
59 | |||
60 | # Query event details. |
||
61 | strcmp(evdev.code, "KEY_MUTE") is_mute; |
||
62 | strcmp(evdev.code, "KEY_VOLUMEUP") is_vup; |
||
63 | strcmp(evdev.code, "KEY_VOLUMEDOWN") is_vdown; |
||
64 | strcmp(evdev.code, "KEY_POWER") is_power; |
||
65 | strcmp(evdev.value, "1") is_pressed; |
||
66 | |||
67 | # Compute where to dispatch the event. |
||
68 | and(is_mute, is_pressed) dispatch_mute; |
||
69 | and(is_vup, is_pressed) dispatch_vup; |
||
70 | and(is_vdown, is_pressed) dispatch_vdown; |
||
71 | and(is_power, is_pressed) dispatch_power; |
||
72 | |||
73 | # Dispatch event. |
||
74 | choose({ |
||
75 | {dispatch_mute, "events_input_event_mute"}, |
||
76 | {dispatch_vup, "events_input_event_vup"}, |
||
77 | {dispatch_vdown, "events_input_event_vdown"}, |
||
78 | {dispatch_power, "events_input_event_power"}}, |
||
79 | "<none>" |
||
80 | ) func; |
||
81 | call(func, {}); |
||
82 | |||
83 | # Next event. |
||
84 | evdev->nextevent(); |
||
85 | } |
||
86 | |||
87 | template events_input_event_mute { |
||
88 | runonce({_caller.gdep.volume_script, "mute"}); |
||
89 | } |
||
90 | |||
91 | template events_input_event_vup { |
||
92 | runonce({_caller.gdep.volume_script, "up"}); |
||
93 | } |
||
94 | |||
95 | template events_input_event_vdown { |
||
96 | runonce({_caller.gdep.volume_script, "down"}); |
||
97 | } |
||
98 | |||
99 | template events_input_event_power { |
||
100 | runonce(_caller.gdep.suspend_cmd); |
||
101 | } |