Introduction
Introduction Statistics Contact Development Disclaimer Help
dwm-keychord-6.2.diff - sites - public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log
Files
Refs
---
dwm-keychord-6.2.diff (9320B)
---
1 From af959703381f2c216624eff7795f59156b05c2a0 Mon Sep 17 00:00:00 2001
2 From: Hai Nguyen <[email protected]>
3 Date: Wed, 19 Jan 2022 04:38:20 -0500
4 Subject: [PATCH] implement keychord using array and pointer instead of h…
5 allocation
6
7 ---
8 config.def.h | 63 +++++++++++++++++++++++----------------------
9 dwm.c | 72 ++++++++++++++++++++++++++++++++++++++++------------
10 2 files changed, 88 insertions(+), 47 deletions(-)
11
12 diff --git a/config.def.h b/config.def.h
13 index a2ac963..7cc8ddd 100644
14 --- a/config.def.h
15 +++ b/config.def.h
16 @@ -46,11 +46,11 @@ static const Layout layouts[] = {
17
18 /* key definitions */
19 #define MODKEY Mod1Mask
20 -#define TAGKEYS(KEY,TAG) \
21 - { MODKEY, KEY, view, {.ui …
22 - { MODKEY|ControlMask, KEY, toggleview, {.ui …
23 - { MODKEY|ShiftMask, KEY, tag, {.ui …
24 - { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui …
25 +#define TAGKEYS(KEY,TAG) …
26 + &((Keychord){1, {{MODKEY, KEY}}, …
27 + &((Keychord){1, {{MODKEY|ControlMask, KEY}}, …
28 + &((Keychord){1, {{MODKEY|ShiftMask, KEY}}, …
29 + &((Keychord){1, {{MODKEY|ControlMask|ShiftMask, KEY}}, …
30
31 /* helper for spawning shell commands in the pre dwm-5.0 fashion */
32 #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL }…
33 @@ -60,31 +60,32 @@ static char dmenumon[2] = "0"; /* component of dmenu…
34 static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", d…
35 static const char *termcmd[] = { "st", NULL };
36
37 -static Key keys[] = {
38 - /* modifier key function argu…
39 - { MODKEY, XK_p, spawn, {.v …
40 - { MODKEY|ShiftMask, XK_Return, spawn, {.v …
41 - { MODKEY, XK_b, togglebar, {0} …
42 - { MODKEY, XK_j, focusstack, {.i …
43 - { MODKEY, XK_k, focusstack, {.i …
44 - { MODKEY, XK_i, incnmaster, {.i …
45 - { MODKEY, XK_d, incnmaster, {.i …
46 - { MODKEY, XK_h, setmfact, {.f …
47 - { MODKEY, XK_l, setmfact, {.f …
48 - { MODKEY, XK_Return, zoom, {0} …
49 - { MODKEY, XK_Tab, view, {0} …
50 - { MODKEY|ShiftMask, XK_c, killclient, {0} …
51 - { MODKEY, XK_t, setlayout, {.v …
52 - { MODKEY, XK_f, setlayout, {.v …
53 - { MODKEY, XK_m, setlayout, {.v …
54 - { MODKEY, XK_space, setlayout, {0} …
55 - { MODKEY|ShiftMask, XK_space, togglefloating, {0} …
56 - { MODKEY, XK_0, view, {.ui…
57 - { MODKEY|ShiftMask, XK_0, tag, {.ui…
58 - { MODKEY, XK_comma, focusmon, {.i …
59 - { MODKEY, XK_period, focusmon, {.i …
60 - { MODKEY|ShiftMask, XK_comma, tagmon, {.i …
61 - { MODKEY|ShiftMask, XK_period, tagmon, {.i …
62 +static Keychord *keychords[] = {
63 + /* Keys function argument */
64 + &((Keychord){1, {{MODKEY, XK_p}}, …
65 + &((Keychord){1, {{MODKEY|ShiftMask, XK_Return}}, …
66 + &((Keychord){2, {{MODKEY, XK_e}, {MODKEY, XK_e}}, …
67 + &((Keychord){1, {{MODKEY, XK_b}}, …
68 + &((Keychord){1, {{MODKEY, XK_j}}, …
69 + &((Keychord){1, {{MODKEY, XK_k}}, …
70 + &((Keychord){1, {{MODKEY, XK_i}}, …
71 + &((Keychord){1, {{MODKEY, XK_d}}, …
72 + &((Keychord){1, {{MODKEY, XK_h}}, …
73 + &((Keychord){1, {{MODKEY, XK_l}}, …
74 + &((Keychord){1, {{MODKEY, XK_Return}}, …
75 + &((Keychord){1, {{MODKEY, XK_Tab}}, …
76 + &((Keychord){1, {{MODKEY|ShiftMask, XK_c}}, …
77 + &((Keychord){1, {{MODKEY, XK_t}}, …
78 + &((Keychord){1, {{MODKEY, XK_f}}, …
79 + &((Keychord){1, {{MODKEY, XK_m}}, …
80 + &((Keychord){1, {{MODKEY, XK_space}}, …
81 + &((Keychord){1, {{MODKEY|ShiftMask, XK_space}}, …
82 + &((Keychord){1, {{MODKEY, XK_0}}, …
83 + &((Keychord){1, {{MODKEY|ShiftMask, XK_0}}, …
84 + &((Keychord){1, {{MODKEY, XK_comma}}, …
85 + &((Keychord){1, {{MODKEY, XK_period}}, …
86 + &((Keychord){1, {{MODKEY|ShiftMask, XK_comma}}, …
87 + &((Keychord){1, {{MODKEY|ShiftMask, XK_period}}, …
88 TAGKEYS( XK_1, 0)
89 TAGKEYS( XK_2, 1)
90 TAGKEYS( XK_3, 2)
91 @@ -94,7 +95,7 @@ static Key keys[] = {
92 TAGKEYS( XK_7, 6)
93 TAGKEYS( XK_8, 7)
94 TAGKEYS( XK_9, 8)
95 - { MODKEY|ShiftMask, XK_q, quit, {0} …
96 + &((Keychord){1, {{MODKEY|ShiftMask, XK_q}}, …
97 };
98
99 /* button definitions */
100 diff --git a/dwm.c b/dwm.c
101 index a96f33c..f9777bd 100644
102 --- a/dwm.c
103 +++ b/dwm.c
104 @@ -102,9 +102,14 @@ struct Client {
105 typedef struct {
106 unsigned int mod;
107 KeySym keysym;
108 +} Key;
109 +
110 +typedef struct {
111 + unsigned int n;
112 + const Key keys[5];
113 void (*func)(const Arg *);
114 const Arg arg;
115 -} Key;
116 +} Keychord;
117
118 typedef struct {
119 const char *symbol;
120 @@ -268,6 +273,7 @@ static Display *dpy;
121 static Drw *drw;
122 static Monitor *mons, *selmon;
123 static Window root, wmcheckwin;
124 +unsigned int currentkey = 0;
125
126 /* configuration, allows nested code to access above variables */
127 #include "config.h"
128 @@ -954,16 +960,17 @@ grabkeys(void)
129 {
130 updatenumlockmask();
131 {
132 - unsigned int i, j;
133 + unsigned int i, k;
134 unsigned int modifiers[] = { 0, LockMask, numlockmask, …
135 KeyCode code;
136 -
137 XUngrabKey(dpy, AnyKey, AnyModifier, root);
138 - for (i = 0; i < LENGTH(keys); i++)
139 - if ((code = XKeysymToKeycode(dpy, keys[i].keysy…
140 - for (j = 0; j < LENGTH(modifiers); j++)
141 - XGrabKey(dpy, code, keys[i].mod…
142 - True, GrabModeAsync, Gr…
143 + for (i = 0; i < LENGTH(keychords); i++)
144 + if ((code = XKeysymToKeycode(dpy, keychords[i]-…
145 + for (k = 0; k < LENGTH(modifiers); k++)
146 + XGrabKey(dpy, code, keychords[i…
147 + True, GrabMode…
148 + if(currentkey > 0)
149 + XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Escape),…
150 }
151 }
152
153 @@ -989,17 +996,50 @@ isuniquegeom(XineramaScreenInfo *unique, size_t n,…
154 void
155 keypress(XEvent *e)
156 {
157 - unsigned int i;
158 + XEvent event = *e;
159 + unsigned int ran = 0;
160 KeySym keysym;
161 XKeyEvent *ev;
162
163 - ev = &e->xkey;
164 - keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
165 - for (i = 0; i < LENGTH(keys); i++)
166 - if (keysym == keys[i].keysym
167 - && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
168 - && keys[i].func)
169 - keys[i].func(&(keys[i].arg));
170 + Keychord *arr1[sizeof(keychords) / sizeof(Keychord*)];
171 + Keychord *arr2[sizeof(keychords) / sizeof(Keychord*)];
172 + memcpy(arr1, keychords, sizeof(keychords));
173 + Keychord **rpointer = arr1;
174 + Keychord **wpointer = arr2;
175 +
176 + size_t r = sizeof(keychords)/ sizeof(Keychord*);
177 +
178 + while(1){
179 + ev = &event.xkey;
180 + keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
181 + size_t w = 0;
182 + for (int i = 0; i < r; i++){
183 + if(keysym == (*(rpointer + i))->keys[currentkey…
184 + && CLEANMASK((*(rpointer + i))->keys[current…
185 + && (*(rpointer + i))->func){
186 + if((*(rpointer + i))->n == currentkey +…
187 + (*(rpointer + i))->func(&((*(rp…
188 + ran = 1;
189 + }else{
190 + *(wpointer + w) = *(rpointer + …
191 + w++;
192 + }
193 + }
194 + }
195 + currentkey++;
196 + if(w == 0 || ran == 1)
197 + break;
198 + grabkeys();
199 + while (running && !XNextEvent(dpy, &event) && !ran)
200 + if(event.type == KeyPress)
201 + break;
202 + r = w;
203 + Keychord **holder = rpointer;
204 + rpointer = wpointer;
205 + wpointer = holder;
206 + }
207 + currentkey = 0;
208 + grabkeys();
209 }
210
211 void
212 --
213 2.34.1
214
You are viewing proxied material from suckless.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.