korgwm/Hotkeys.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« korgwm/EWMH.pm ↑ Summary korgwm/Mouse.pm »
1 #!/usr/bin/perl
2 # made by: KorG
3 # vim: cc=119 et sw=4 ts=4 :
4 package X11::korgwm::Hotkeys;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶use X11::XCB ':all';
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use X11::korgwm::Common;
11 require X11::korgwm::Config;
12 require X11::korgwm::Executor;
13  
14 # <X11/keysymdef.h>
15 my $keys = {
16 "CR" => 0xFF0D, # XK_Return
17 "TAB" => 0xFF09, # XK_Tab
18 "Print" => 0xFF61, # XK_Print
19 (map {; "F$_" => 0xFFBD + $_ } 1..12), # XK_Fx
20 "XF86MonBrightnessUp" => 0x1008FF02,
21 "XF86MonBrightnessDown" => 0x1008FF03,
22 "XF86AudioLowerVolume" => 0x1008FF11,
23 "XF86AudioMute" => 0x1008FF12,
24 "XF86AudioRaiseVolume" => 0x1008FF13,
25 "XF86AudioPlay" => 0x1008FF14,
26 "XF86AudioStop" => 0x1008FF15,
27 "XF86AudioPrev" => 0x1008FF16,
28 "XF86AudioNext" => 0x1008FF17,
29 "XF86WakeUp" => 0x1008FF2B,
30 "XF86AudioPause" => 0x1008FF31,
31 };
32  
33 # xcb modifiers
34 my $modifiers = {
35 "alt" => MOD_MASK_1,
36 "ctrl" => MOD_MASK_CONTROL,
37 "mod" => MOD_MASK_4,
38 "shift" => MOD_MASK_SHIFT,
39 };
40  
41 my $keymap; # a mapping between keycodes (arr index) and char codes from X11
42 my $keycodes; # reverse mapping for us
43 my $hotkeys; # hash with actual functions to run
44  
45 # Flag and result container for grab_key()
46 my $grab_active;
47 my $grab_result;
48  
49 # Switch to a mode to grab a single key
50 sub grab_key($callback = undef) {
51 $grab_result = $callback;
52 $grab_active = 1;
53 $X->grab_keyboard(0, $X->root->id, TIME_CURRENT_TIME, GRAB_MODE_ASYNC, GRAB_MODE_ASYNC);
54 $X->flush();
55 }
56  
57 # Register a hotkey
58 sub hotkey($hotkey, $cmd) {
59 my @keys = split /_/, $hotkey;
60 my $key = pop @keys;
61 $key = $keys->{$key} // ord($key);
62 my $mask = 0;
63 for (@keys) {
64 my $mod = $modifiers->{$_} or croak "Modifier $_ not defined";
65 $mask |= $mod;
66 }
67 $hotkeys->{$key}->{$mask} = X11::korgwm::Executor::parse($cmd);
68 }
69  
70 sub init {
71 # Override xkb rules to support XF86 media keys via xf86-input-evdev
72 qx(setxkbmap -rules evdev) if $cfg->{setxkb_evdev};
73  
74 # Init keymap
75 $keymap = $X->get_keymap();
76  
77 # Prepare reverse mapping
78 for (my $i = $#{ $keymap }; $i > 0; $i--) {
79 my $keycode = $keymap->[$i] or next;
80 push @{ $keycodes->{$keycode->[0]} }, $i;
81 }
82  
83 # Parse hotkeys from config and fill %$hotkeys
84 hotkey($_, $cfg->{hotkeys}->{$_}) for keys %{ $cfg->{hotkeys} };
85  
86 # Register event handler
87 add_event_cb(KEY_PRESS(), sub($evt) {
88 my $key = $keymap->[$evt->{detail}]->[0];
89 my $mask = $evt->{state};
90  
91 # If some routine asks us to grab the key, save it to an object and return
92 if ($grab_active) {
93 $grab_active = undef;
94  
95 my $grab_data = [ $key, $mask ];
96  
97 if (ref(my $callback = $grab_result) eq 'CODE') {
98 $callback->($grab_data);
99 }
100  
101 $grab_result = $grab_data;
102  
103 $X->ungrab_keyboard(TIME_CURRENT_TIME);
104 $X->flush();
105 return;
106 }
107  
108 # Sometimes we get modifiers itself from X11, so ignore them (constants took from <X11/keysymdef.h>)
109 return carp "X11 sent us a modifier key $key mask $mask" if $key >= 0xffe1 and $key <= 0xffee;
110  
111 my $handler = $hotkeys->{$key}->{$mask};
112 unless ($handler) {
113 return carp "X11 sent us XK_Escape, but didn't even think to call us Godfather" if $key == 0xff1b;
114 return carp "Caught unexpected key: $key code: $evt->{detail} mask: $mask";
115 }
116 $handler->();
117 });
118  
119 # We are not interested in KeyRelease events, so ignore it
120 add_event_ignore(KEY_RELEASE());
121  
122 # Grab keys
123 my $root_id = $X->root->id;
124 for my $key (keys %{ $hotkeys }) {
125 for my $mask (keys %{ $hotkeys->{$key} }) {
126 # In case of improper setxkbmap (when "us" is not on the first place) keycodes could be undefined
127 my $keycodes_list = $keycodes->{$key} // croak "Keycode for key '$key' is not defined on X11 server!";
128  
129 $X->grab_key(0, $root_id, $mask, $_, GRAB_MODE_ASYNC, GRAB_MODE_ASYNC) for @{ $keycodes_list };
130 }
131 }
132 $X->flush();
133 }
134  
135 push @X11::korgwm::extensions, \&init;
136  
137 1;
« korgwm/EWMH.pm ↑ Summary korgwm/Mouse.pm »