events.ncd 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 for accessing configuration from event providers.
  18. provide("events_config");
  19. # Create process manager.
  20. process_manager() manager;
  21. # Wait for input device events.
  22. sys.watch_input("event") watcher;
  23. # Determine if the device is of interest.
  24. # Volume and power keys are reported by "key" devices.
  25. strcmp(watcher.device_type, "key") interest;
  26. # Determine dispatch location.
  27. strcmp(watcher.event_type, "added") added;
  28. and(added, interest) dispatch_added;
  29. strcmp(watcher.event_type, "removed") removed;
  30. and(removed, interest) dispatch_removed;
  31. # Dispatch event.
  32. provide("events_watcher_event");
  33. # If event was not recognized, finish it here.
  34. ifnot(dispatch_added);
  35. ifnot(dispatch_removed);
  36. watcher->nextevent();
  37. }
  38. process events_watcher_event_added {
  39. # Wait for event.
  40. depend("events_watcher_event") evdep;
  41. if(evdep.dispatch_added);
  42. # Start event handling process for this device.
  43. list(evdep.watcher.devname) args;
  44. evdep.manager->start(evdep.watcher.devname, "events_input_device", args);
  45. # Finish event.
  46. evdep.watcher->nextevent();
  47. }
  48. process events_watcher_event_removed {
  49. # Wait for event.
  50. depend("events_watcher_event") evdep;
  51. if(evdep.dispatch_removed);
  52. # Stop event handling process for this device.
  53. evdep.manager->stop(evdep.watcher.devname);
  54. # Finish event.
  55. evdep.watcher->nextevent();
  56. }
  57. template events_input_device {
  58. # Alias arguments.
  59. var(_arg0) dev;
  60. # Dependency for accessing configuration.
  61. depend("events_config") config;
  62. # Wait for input events.
  63. sys.evdev(dev) evdev;
  64. # Query event details.
  65. strcmp(evdev.code, "KEY_MUTE") is_mute;
  66. strcmp(evdev.code, "KEY_VOLUMEUP") is_vup;
  67. strcmp(evdev.code, "KEY_VOLUMEDOWN") is_vdown;
  68. strcmp(evdev.code, "KEY_POWER") is_power;
  69. strcmp(evdev.value, "1") is_pressed;
  70. # Compute where to dispatch the event.
  71. and(is_mute, is_pressed) dispatch_mute;
  72. and(is_vup, is_pressed) dispatch_vup;
  73. and(is_vdown, is_pressed) dispatch_vdown;
  74. and(is_power, is_pressed) dispatch_power;
  75. # Dispatch event.
  76. provide_event("events_input_event");
  77. # Handle unhandled event.
  78. ifnot(dispatch_mute);
  79. ifnot(dispatch_vup);
  80. ifnot(dispatch_vdown);
  81. ifnot(dispatch_power);
  82. # Finish unhandled event.
  83. evdev->nextevent();
  84. }
  85. process events_input_event_mute {
  86. # Wait for event.
  87. depend("events_input_event") dep;
  88. if(dep.dispatch_mute);
  89. # Process event.
  90. list(dep.config.volume_script, "mute") cmd;
  91. runonce(cmd);
  92. # Finish event.
  93. dep.evdev->nextevent();
  94. }
  95. process events_input_event_vup {
  96. # Wait for event.
  97. depend("events_input_event") dep;
  98. if(dep.dispatch_vup);
  99. # Process event.
  100. list(dep.config.volume_script, "up") cmd;
  101. runonce(cmd);
  102. # Finish event.
  103. dep.evdev->nextevent();
  104. }
  105. process events_input_event_vdown {
  106. # Wait for event.
  107. depend("events_input_event") dep;
  108. if(dep.dispatch_vdown);
  109. # Process event.
  110. list(dep.config.volume_script, "down") cmd;
  111. runonce(cmd);
  112. # Finish event.
  113. dep.evdev->nextevent();
  114. }
  115. process events_input_event_power {
  116. # Wait for event.
  117. depend("events_input_event") dep;
  118. if(dep.dispatch_power);
  119. # Process event.
  120. runonce(dep.config.suspend_cmd);
  121. # Finish event.
  122. dep.evdev->nextevent();
  123. }