Debian Codegraph @

lib/renderers/x11/x11.c

raw
1 #include "internal.h"
2 #include "x11.h"
3 #include "xkb_unicode.h"
4
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <X11/Xutil.h>
8
9 static bool
10 render(struct bm_menu *menu)
11 {
12 struct x11 *x11 = menu->renderer->internal;
13
14 bm_x11_window_render(&x11->window, menu);
15 XFlush(x11->display);
16
17 XEvent ev;
18 if (XNextEvent(x11->display, &ev) || XFilterEvent(&ev, x11->window.drawable))
19 return true;
20
21 switch (ev.type) {
22 case KeyPress:
23 bm_x11_window_key_press(&x11->window, &ev.xkey);
24 break;
25 case SelectionNotify:
26 // paste here
27 break;
28 case VisibilityNotify:
29 if (ev.xvisibility.state != VisibilityUnobscured) {
30 XRaiseWindow(x11->display, x11->window.drawable);
31 XFlush(x11->display);
32 }
33 break;
34 }
35
36 return true;
37 }
38
39 static enum bm_key
40 poll_key(const struct bm_menu *menu, unsigned int *unicode)
41 {
42 struct x11 *x11 = menu->renderer->internal;
43 assert(x11 && unicode);
44
45 if (x11->window.keysym == NoSymbol)
46 return BM_KEY_UNICODE;
47
48 KeySym sym = x11->window.keysym;
49 uint32_t mods = x11->window.mods;
50 *unicode = bm_x11_key_sym2unicode(sym);
51
52 x11->window.keysym = NoSymbol;
53
54 switch (sym) {
55 case XK_Up:
56 return BM_KEY_UP;
57
58 case XK_Down:
59 return BM_KEY_DOWN;
60
61 case XK_Left:
62 return (mods & MOD_SHIFT ? BM_KEY_UP : BM_KEY_LEFT);
63
64 case XK_Right:
65 return (mods & MOD_SHIFT ? BM_KEY_DOWN : BM_KEY_RIGHT);
66
67 case XK_Home:
68 return BM_KEY_HOME;
69
70 case XK_End:
71 return BM_KEY_END;
72
73 case XK_Page_Up:
74 return (mods & MOD_SHIFT ? BM_KEY_SHIFT_PAGE_UP : BM_KEY_PAGE_UP);
75
76 case XK_less:
77 return (mods & MOD_ALT ? BM_KEY_SHIFT_PAGE_UP : BM_KEY_UNICODE);
78
79 case XK_Page_Down:
80 return (mods & MOD_SHIFT ? BM_KEY_SHIFT_PAGE_DOWN : BM_KEY_PAGE_DOWN);
81
82 case XK_greater:
83 return (mods & MOD_ALT ? BM_KEY_SHIFT_PAGE_DOWN : BM_KEY_UNICODE);
84
85 case XK_v:
86 return (mods & MOD_CTRL ? BM_KEY_PAGE_DOWN : (mods & MOD_ALT ? BM_KEY_PAGE_UP : BM_KEY_UNICODE));
87
88 case XK_BackSpace:
89 return BM_KEY_BACKSPACE;
90
91 case XK_Delete:
92 return (mods & MOD_SHIFT ? BM_KEY_LINE_DELETE_LEFT : BM_KEY_DELETE);
93
94 case XK_Tab:
95 return (mods & MOD_SHIFT ? BM_KEY_SHIFT_TAB : BM_KEY_TAB);
96
97 case XK_ISO_Left_Tab:
98 return BM_KEY_SHIFT_TAB;
99
100 case XK_Insert:
101 return BM_KEY_SHIFT_RETURN;
102
103 case XK_KP_Enter:
104 case XK_Return:
105 return (mods & MOD_CTRL ? BM_KEY_CONTROL_RETURN : (mods & MOD_SHIFT ? BM_KEY_SHIFT_RETURN : BM_KEY_RETURN));
106
107 case XK_g:
108 if (!(mods & MOD_CTRL)) return BM_KEY_UNICODE;
109 // fall through
110 case XK_c:
111 if (!(mods & MOD_CTRL)) return BM_KEY_UNICODE;
112 // fall through
113 case XK_bracketleft:
114 if (!(mods & MOD_CTRL)) return BM_KEY_UNICODE;
115 // fall through
116 case XK_Escape:
117 return BM_KEY_ESCAPE;
118
119 case XK_p:
120 return (mods & MOD_CTRL ? BM_KEY_UP : BM_KEY_UNICODE);
121
122 case XK_n:
123 return (mods & MOD_CTRL ? BM_KEY_DOWN : BM_KEY_UNICODE);
124
125 case XK_l:
126 return (mods & MOD_CTRL ? BM_KEY_LEFT : (mods & MOD_ALT ? BM_KEY_DOWN : BM_KEY_UNICODE));
127
128 case XK_b:
129 return (mods & MOD_CTRL ? BM_KEY_LEFT : BM_KEY_UNICODE);
130
131 case XK_f:
132 return (mods & MOD_CTRL ? BM_KEY_RIGHT : BM_KEY_UNICODE);
133
134 case XK_a:
135 return (mods & MOD_CTRL ? BM_KEY_HOME : BM_KEY_UNICODE);
136
137 case XK_e:
138 return (mods & MOD_CTRL ? BM_KEY_END : BM_KEY_UNICODE);
139
140 case XK_h:
141 return (mods & MOD_CTRL ? BM_KEY_BACKSPACE : (mods & MOD_ALT ? BM_KEY_UP : BM_KEY_UNICODE));
142
143 case XK_u:
144 return (mods & MOD_CTRL ? BM_KEY_LINE_DELETE_LEFT : (mods & MOD_ALT ? BM_KEY_PAGE_UP : BM_KEY_UNICODE));
145
146 case XK_k:
147 return (mods & MOD_CTRL ? BM_KEY_LINE_DELETE_RIGHT : (mods & MOD_ALT ? BM_KEY_UP : BM_KEY_UNICODE));
148
149 case XK_w:
150 return (mods & MOD_CTRL ? BM_KEY_WORD_DELETE : BM_KEY_UNICODE);
151
152 case XK_y:
153 return (mods & MOD_CTRL ? BM_KEY_PASTE_PRIMARY : BM_KEY_UNICODE);
154
155 case XK_Y:
156 return (mods & MOD_CTRL ? BM_KEY_PASTE_CLIPBOARD : BM_KEY_UNICODE);
157
158 case XK_j:
159 return (mods & MOD_ALT ? BM_KEY_DOWN : BM_KEY_UNICODE);
160
161 case XK_d:
162 return (mods & MOD_ALT ? BM_KEY_PAGE_DOWN : BM_KEY_UNICODE);
163
164 case XK_m:
165 return (mods & MOD_CTRL ? BM_KEY_RETURN : BM_KEY_UNICODE);
166
167 case XK_1:
168 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_1;
169 break;
170 case XK_2:
171 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_2;
172 break;
173 case XK_3:
174 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_3;
175 break;
176 case XK_4:
177 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_4;
178 break;
179 case XK_5:
180 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_5;
181 break;
182 case XK_6:
183 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_6;
184 break;
185 case XK_7:
186 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_7;
187 break;
188 case XK_8:
189 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_8;
190 break;
191 case XK_9:
192 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_9;
193 break;
194 case XK_0:
195 if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_10;
196 break;
197
198 default: break;
199 }
200
201 return BM_KEY_UNICODE;
202 }
203
204 static uint32_t
205 get_displayed_count(const struct bm_menu *menu)
206 {
207 struct x11 *x11 = menu->renderer->internal;
208 assert(x11);
209 return x11->window.displayed;
210 }
211
212 static void
213 set_align(const struct bm_menu *menu, enum bm_align align)
214 {
215 struct x11 *x11 = menu->renderer->internal;
216 assert(x11);
217 bm_x11_window_set_align(&x11->window, align);
218 }
219
220 static void
221 set_y_offset(const struct bm_menu *menu, int32_t y_offset)
222 {
223 struct x11 *x11 = menu->renderer->internal;
224 assert(x11);
225 bm_x11_window_set_y_offset(&x11->window, y_offset);
226 }
227
228 static void
229 set_width(const struct bm_menu *menu, uint32_t margin, float factor)
230 {
231 struct x11 *x11 = menu->renderer->internal;
232 assert(x11);
233 bm_x11_window_set_width(&x11->window, margin, factor);
234 }
235
236 static void
237 set_monitor(const struct bm_menu *menu, int32_t monitor)
238 {
239 struct x11 *x11 = menu->renderer->internal;
240 assert(x11);
241 bm_x11_window_set_monitor(&x11->window, monitor);
242 }
243
244 static void
245 grab_keyboard(const struct bm_menu *menu, bool grab)
246 {
247 struct x11 *x11 = menu->renderer->internal;
248 assert(x11);
249
250 if (grab) {
251 for (uint32_t i = 0; i < 1000; ++i) {
252 if (XGrabKeyboard(x11->display, DefaultRootWindow(x11->display), True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
253 return;
254 usleep(1000);
255 }
256
257 fprintf(stderr, "x11: cannot grab keyboard\n");
258 } else {
259 XUngrabKeyboard(x11->display, CurrentTime);
260 }
261 }
262
263 static void
264 destructor(struct bm_menu *menu)
265 {
266 struct x11 *x11 = menu->renderer->internal;
267
268 if (!x11)
269 return;
270
271 bm_x11_window_destroy(&x11->window);
272
273 if (x11->display)
274 XCloseDisplay(x11->display);
275
276 free(x11);
277 menu->renderer->internal = NULL;
278 }
279
280 static bool
281 constructor(struct bm_menu *menu)
282 {
283 if (!getenv("BEMENU_BACKEND") && (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET")))
284 return false;
285
286 struct x11 *x11;
287 if (!(menu->renderer->internal = x11 = calloc(1, sizeof(struct x11))))
288 goto fail;
289
290 if (!(x11->display = XOpenDisplay(NULL)))
291 goto fail;
292
293 if (!bm_x11_window_create(&x11->window, x11->display))
294 goto fail;
295
296 XSetClassHint(x11->window.display, x11->window.drawable, (XClassHint[]){{.res_name = (menu->title ? menu->title : "bemenu"), .res_class = "bemenu"}});
297
298 x11->window.align = menu->align;
299 bm_x11_window_set_monitor(&x11->window, menu->monitor);
300
301 x11->window.notify.render = bm_cairo_paint;
302 return true;
303
304 fail:
305 destructor(menu);
306 return false;
307 }
308
309 BM_PUBLIC extern const char*
310 register_renderer(struct render_api *api)
311 {
312 api->constructor = constructor;
313 api->destructor = destructor;
314 api->get_displayed_count = get_displayed_count;
315 api->poll_key = poll_key;
316 api->render = render;
317 api->set_align = set_align;
318 api->set_y_offset = set_y_offset;
319 api->set_width = set_width;
320 api->set_monitor = set_monitor;
321 api->grab_keyboard = grab_keyboard;
322 api->priorty = BM_PRIO_GUI;
323 api->version = BM_PLUGIN_VERSION;
324 return "x11";
325 }
326
327 /* vim: set ts=8 sw=4 tw=0 :*/