events.ncd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. process events_main {
  13. # Volume control script, called with argument "up", "down" or "mute".
  14. var("/usr/local/bin/volumekey") volume_script;
  15. # Suspend command.
  16. list("/usr/sbin/pm-suspend") suspend_cmd;
  17. provide("GLOBAL");
  18. }
  19. process events_watcher {
  20. depend("GLOBAL");
  21. # Create process manager.
  22. process_manager() manager;
  23. # Wait for input device events.
  24. sys.watch_input("event") watcher;
  25. # Dispatch.
  26. concat("events_watcher_", watcher.event_type) func;
  27. call(func, {});
  28. # Next event.
  29. watcher->nextevent();
  30. }
  31. template events_watcher_added {
  32. # Start event handling process for this device.
  33. _caller.manager->start(_caller.watcher.devname, "events_input_device", {_caller.watcher.devname});
  34. }
  35. template events_watcher_removed {
  36. # Stop event handling process for this device.
  37. _caller.manager->stop(_caller.watcher.devname);
  38. }
  39. template events_input_device {
  40. # Alias arguments.
  41. var(_arg0) dev;
  42. # Get global.
  43. depend("GLOBAL") gdep;
  44. # Wait for input events.
  45. sys.evdev(dev) evdev;
  46. # Query event details.
  47. strcmp(evdev.code, "KEY_MUTE") is_mute;
  48. strcmp(evdev.code, "KEY_VOLUMEUP") is_vup;
  49. strcmp(evdev.code, "KEY_VOLUMEDOWN") is_vdown;
  50. strcmp(evdev.code, "KEY_POWER") is_power;
  51. strcmp(evdev.value, "1") is_pressed;
  52. # Compute where to dispatch the event.
  53. and(is_mute, is_pressed) dispatch_mute;
  54. and(is_vup, is_pressed) dispatch_vup;
  55. and(is_vdown, is_pressed) dispatch_vdown;
  56. and(is_power, is_pressed) dispatch_power;
  57. # Dispatch event.
  58. choose({
  59. {dispatch_mute, "events_input_event_mute"},
  60. {dispatch_vup, "events_input_event_vup"},
  61. {dispatch_vdown, "events_input_event_vdown"},
  62. {dispatch_power, "events_input_event_power"}},
  63. "<none>"
  64. ) func;
  65. call(func, {});
  66. # Next event.
  67. evdev->nextevent();
  68. }
  69. template events_input_event_mute {
  70. runonce({_caller.gdep.volume_script, "mute"});
  71. }
  72. template events_input_event_vup {
  73. runonce({_caller.gdep.volume_script, "up"});
  74. }
  75. template events_input_event_vdown {
  76. runonce({_caller.gdep.volume_script, "down"});
  77. }
  78. template events_input_event_power {
  79. runonce(_caller.gdep.suspend_cmd);
  80. }