i3lock-color

The world's most popular non-default computer lockscreen.
Index Commits Files Refs README LICENSE
commit aeb9982ded73b55d317f0323a138bffea4cda3eb
parent c91afea4f67b0f7972132103697164dc3bb7a755
Author: Jesse R Codling <jclds139@gmail.com>
Date:   Fri, 26 Aug 2022 02:06:15 -0400

Shell command options for media keys (#256)

* Shell command options for media keys

* Use cmd- prefix for custom shell commands

* hotkeys: renumber identifiers for custom commands

Adjusted to use 610-660 instead of 650-699

* Remove unnecessary `strdup(optarg)`

Per discussion in #256

* Fix PR feedback

Co-authored-by: Raymond Li <hi@raymond.li>
Diffstat:
Mi3lock-bash | 15++++++++++++++-
Mi3lock-zsh | 14++++++++++++++
Mi3lock.1 | 37+++++++++++++++++++++++++++++++++++++
Mi3lock.c | 168+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 233 insertions(+), 1 deletion(-)
diff --git a/i3lock-bash b/i3lock-bash
@@ -23,7 +23,6 @@ _i3lock() {
   "--debug"
 
   # i3lock-color OPTIONS
-  
   "--screen -S"
   "--blur -B"
   "--clock --force-clocl -k"
@@ -81,6 +80,20 @@ _i3lock() {
   "--pass-screen-keys"
   "--pass-power-keys"
   "--pass-volume-keys"
+  "--custom-key-commands"
+  "--cmd-brightness-up"
+  "--cmd-brightness-down"
+  "--cmd-media-play"
+  "--cmd-media-pause"
+  "--cmd-media-stop"
+  "--cmd-media-next"
+  "--cmd-media-prev"
+  "--cmd-audio-mute"
+  "--cmd-volume-up"
+  "--cmd-volume-down"
+  "--cmd-power-down"
+  "--cmd-power-off"
+  "--cmd-power-sleep"
   # Bar mode
   "--bar-indicator"
   "--bar-direction"
diff --git a/i3lock-zsh b/i3lock-zsh
@@ -81,6 +81,20 @@ _i3lock() {
     "--pass-screen-keys[Allow screen keys to be used while the screen is locked]"
     "--pass-power-keys[Allow power keys to be used while the screen is locked]"
     "--pass-volume-keys[Allow volume keys to be used while the screen is locked]"
+    "--custom-key-commands[Enable shell commands for media keys]"
+    "--cmd-brightness-up[Command for XF86MonBrightnessUp]"
+    "--cmd-brightness-down[Command for XF86MonBrightnessDown]"
+    "--cmd-media-play[Command for XF86AudioPlay]"
+    "--cmd-media-pause[Command for XF86AudioPause]"
+    "--cmd-media-stop[Command for XF86AudioStop]"
+    "--cmd-media-next[Command for XF86AudioNext]"
+    "--cmd-media-prev[Command for XF86AudioPrev]"
+    "--cmd-audio-mute[Command for XF86AudioMute]"
+    "--cmd-volume-up[Command for XF86AudioRaiseVolume]"
+    "--cmd-volume-down[Command for XF86AudioLowerVolume]"
+    "--cmd-power-down[Command for XF86PowerDown] "
+    "--cmd-power-off[Command for XF86PowerOff]"
+    "--cmd-power-sleep[Command for XF86Sleep]"
     # Bar mode
     "--bar-indicator[Replaces the usual ring indicator with a bar indicator]"
     "--bar-direction[Sets the direction the bars grow in]:direction:((0\:'default' 1\:'reverse' 2\:'both'))"
diff --git a/i3lock.1 b/i3lock.1
@@ -392,6 +392,43 @@ power - XF86PowerDown, XF86PowerOff, XF86Sleep
 volume - XF86AudioMute, XF86AudioLowerVolume, XF86AudioRaiseVolume
 .RE
 
+
+.TP
+.B \-\-custom\-key\-commands
+Enables custom shell commands for media, screen, power and volume keys. An alternative to \-\-pass\-media\0keys that will work in any environment.
+
+.TP
+.B \-\-cmd\-**
+Shell command to run when the corresponding key is pressed. Requires \-\-custom\-key\-commands
+.RS
+.IP \[bu] 2
+brightness\-up - XF86MonBrightnessUp
+.IP \[bu]
+brightness\-down - XF86MonBrightnessDown
+.IP \[bu]
+media\-play - XF86AudioPlay
+.IP \[bu]
+media\-pause - XF86AudioPause
+.IP \[bu]
+media\-stop - XF86AudioStop
+.IP \[bu]
+media\-next - XF86AudioNext
+.IP \[bu]
+media\-prev - XF86AudioPrev
+.IP \[bu]
+audio\-mute - XF86AudioMute
+.IP \[bu]
+volume\-up - XF86AudioRaiseVolume
+.IP \[bu]
+volume\-down - XF86AudioLowerVolume
+.IP \[bu]
+power\-down - XF86PowerDown
+.IP \[bu]
+power\-off - XF86PowerOff
+.IP \[bu]
+power\-sleep - XF86Sleep
+.RE
+
 .TP
 .B \-\-bar\-indicator
 Replaces the usual ring indicator with a bar indicator. Comes with perks.
diff --git a/i3lock.c b/i3lock.c
@@ -263,6 +263,24 @@ bool pass_screen_keys = false;
 bool pass_power_keys = false;
 bool pass_volume_keys = false;
 
+bool hotkeys = false;
+char* cmd_brightness_up = NULL;
+char* cmd_brightness_down = NULL;
+
+char* cmd_media_play = NULL;
+char* cmd_media_pause = NULL;
+char* cmd_media_stop = NULL;
+char* cmd_media_next = NULL;
+char* cmd_media_prev = NULL;
+
+char* cmd_audio_mute = NULL;
+char* cmd_volume_up = NULL;
+char* cmd_volume_down = NULL;
+
+char* cmd_power_down = NULL;
+char* cmd_power_off = NULL;
+char* cmd_power_sleep = NULL;
+
 // for the rendering thread, so we can clean it up
 pthread_t draw_thread;
 // main thread still sometimes calls redraw()
@@ -703,6 +721,90 @@ static void handle_key_press(xcb_key_press_event_t *event) {
     n = xkb_keysym_to_utf8(ksym, buffer, sizeof(buffer));
 #endif
 
+    //custom key commands
+    if (hotkeys) {
+        switch(ksym) {
+            case XKB_KEY_XF86MonBrightnessUp:
+                if (cmd_brightness_up) {
+                    system(cmd_brightness_up);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86MonBrightnessDown:
+                if (cmd_brightness_down) {
+                    system(cmd_brightness_down);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioPlay:
+                if (cmd_media_play) {
+                    system(cmd_media_play);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioPause:
+                if (cmd_media_pause) {
+                    system(cmd_media_pause);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioStop:
+                if (cmd_media_stop) {
+                    system(cmd_media_stop);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioPrev:
+                if (cmd_media_prev) {
+                    system(cmd_media_prev);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioNext:
+                if (cmd_media_next) {
+                    system(cmd_media_next);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioMute:
+                if (cmd_audio_mute) {
+                    system(cmd_audio_mute);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioLowerVolume:
+                if (cmd_volume_down) {
+                    system(cmd_volume_down);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86AudioRaiseVolume:
+                if (cmd_volume_up) {
+                    system(cmd_volume_up);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86PowerDown:
+                if (cmd_power_down) {
+                    system(cmd_power_down);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86PowerOff:
+                if (cmd_power_off) {
+                    system(cmd_power_off);
+                    return;
+                }
+                break;
+            case XKB_KEY_XF86Sleep:
+                if (cmd_power_sleep) {
+                    system(cmd_power_sleep);
+                    return;
+                }
+                break;
+        }
+    }
+
     // media keys
     if (pass_media_keys) {
         switch(ksym) {
@@ -1558,6 +1660,25 @@ int main(int argc, char *argv[]) {
         {"pass-power-keys", no_argument, NULL, 603},
         {"pass-volume-keys", no_argument, NULL, 604},
 
+        // custom commands for pass keys
+        {"custom-key-commands", no_argument, NULL, 610},
+        {"cmd-brightness-up", required_argument, NULL, 620},
+        {"cmd-brightness-down", required_argument, NULL, 621},
+
+        {"cmd-media-play", required_argument, NULL, 630},
+        {"cmd-media-pause", required_argument, NULL, 631},
+        {"cmd-media-stop", required_argument, NULL, 632},
+        {"cmd-media-next", required_argument, NULL, 633},
+        {"cmd-media-prev", required_argument, NULL, 634},
+
+        {"cmd-audio-mute", required_argument, NULL, 640},
+        {"cmd-volume-up", required_argument, NULL, 641},
+        {"cmd-volume-down", required_argument, NULL, 642},
+
+        {"cmd-power-down", required_argument, NULL, 650},
+        {"cmd-power-off", required_argument, NULL, 651},
+        {"cmd-power-sleep", required_argument, NULL, 652},
+
         // bar indicator stuff
         {"bar-indicator", no_argument, NULL, 700},
         {"bar-direction", required_argument, NULL, 701},
@@ -2119,6 +2240,53 @@ int main(int argc, char *argv[]) {
                 pass_volume_keys = true;
                 break;
 
+            //custom key commands
+            case 610:
+                hotkeys = true;
+                break;
+            case 620:
+                cmd_brightness_up = optarg;
+                break;
+            case 621:
+                cmd_brightness_down = optarg;
+                break;
+
+            case 630:
+                cmd_media_play = optarg;
+                break;
+            case 631:
+                cmd_media_pause = optarg;
+                break;
+            case 632:
+                cmd_media_stop = optarg;
+                break;
+            case 633:
+                cmd_media_next = optarg;
+                break;
+            case 634:
+                cmd_media_prev = optarg;
+                break;
+
+            case 640:
+                cmd_audio_mute = optarg;
+                break;
+            case 641:
+                cmd_volume_up = optarg;
+                break;
+            case 642:
+                cmd_volume_down = optarg;
+                break;
+
+            case 650:
+                cmd_power_down = optarg;
+                break;
+            case 651:
+                cmd_power_off = optarg;
+                break;
+            case 652:
+                cmd_power_sleep = optarg;
+                break;
+
             // Bar indicator
             case 700:
                 bar_enabled = true;