volumekeys.ncd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #
  2. # NCD volume key handling program.
  3. #
  4. # This program responds to volume key presses by synchronously calling an external
  5. # script for muting and adjusting volume.
  6. #
  7. # It uses process_manager() and sys.watch_directory() to dynamically create and remove
  8. # processes that deal with specific input devices. The individual input device processes
  9. # then use sys.evdev() to handle input events from specific devices.
  10. #
  11. process volumekeys_main {
  12. # Directory to watch for input devices.
  13. var("/dev/input/by-path") watch_dir;
  14. # Regular expression for device names to work with.
  15. var("-event$") watch_regex;
  16. # Volume control script, called with argument "up", "down" or "mute".
  17. var("/usr/local/bin/volumekey") volume_script;
  18. # Provide for accessing configuration from event providers.
  19. provide("volumekeys_config");
  20. # Create process manager.
  21. process_manager() manager;
  22. # Wait for directory events.
  23. sys.watch_directory(watch_dir) watcher;
  24. # Determine if the device is of interest.
  25. regex_match(watcher.filename, watch_regex) match;
  26. # Determine dispatch location.
  27. strcmp(watcher.event_type, "added") added;
  28. and(added, match.succeeded) dispatch_added;
  29. strcmp(watcher.event_type, "removed") removed;
  30. and(removed, match.succeeded) dispatch_removed;
  31. # Dispatch event.
  32. provide("volumekeys_directory_event");
  33. # If event was not recognized, finish it here.
  34. ifnot(dispatch_added);
  35. ifnot(dispatch_removed);
  36. watcher->nextevent();
  37. }
  38. process volumekeys_directory_event_added {
  39. # Wait for event.
  40. depend("volumekeys_directory_event") evdep;
  41. if(evdep.dispatch_added);
  42. # Start event handling process for this device.
  43. list(evdep.watcher.filepath) args;
  44. evdep.manager->start(evdep.watcher.filename, "volumekeys_input_device", args);
  45. # Finish event.
  46. evdep.watcher->nextevent();
  47. }
  48. process volumekeys_directory_event_removed {
  49. # Wait for event.
  50. depend("volumekeys_directory_event") evdep;
  51. if(evdep.dispatch_removed);
  52. # Stop event handling process for this device.
  53. evdep.manager->stop(evdep.watcher.filename);
  54. # Finish event.
  55. evdep.watcher->nextevent();
  56. }
  57. template volumekeys_input_device {
  58. # Alias arguments.
  59. var(_arg0) dev;
  60. # Dependency for accessing configuration.
  61. depend("volumekeys_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.value, "1") is_pressed;
  69. # Compute where to dispatch the event.
  70. and(is_mute, is_pressed) dispatch_mute;
  71. and(is_vup, is_pressed) dispatch_vup;
  72. and(is_vdown, is_pressed) dispatch_vdown;
  73. # Dispatch event.
  74. provide_event("volumekeys_input_event");
  75. # If event was not recognized, finish it here.
  76. ifnot(dispatch_mute);
  77. ifnot(dispatch_vup);
  78. ifnot(dispatch_vdown);
  79. evdev->nextevent();
  80. }
  81. process volumekeys_event_mute {
  82. # Wait for event.
  83. depend("volumekeys_input_event") dep;
  84. if(dep.dispatch_mute);
  85. # Process event.
  86. list(dep.config.volume_script, "mute") do;
  87. list("/bin/true") undo;
  88. run(do, undo);
  89. # Finish event.
  90. dep.evdev->nextevent();
  91. }
  92. process volumekeys_event_vup {
  93. # Wait for event.
  94. depend("volumekeys_input_event") dep;
  95. if(dep.dispatch_vup);
  96. # Process event.
  97. list(dep.config.volume_script, "up") do;
  98. list("/bin/true") undo;
  99. run(do, undo);
  100. # Finish event.
  101. dep.evdev->nextevent();
  102. }
  103. process volumekeys_event_vdown {
  104. # Wait for event.
  105. depend("volumekeys_input_event") dep;
  106. if(dep.dispatch_vdown);
  107. # Process event.
  108. list(dep.config.volume_script, "down") do;
  109. list("/bin/true") undo;
  110. run(do, undo);
  111. # Finish event.
  112. dep.evdev->nextevent();
  113. }