a7732690 |
1 | #include <err.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | #include <unistd.h> |
6 | #include <X11/Xlib.h> |
7 | #include <X11/Xutil.h> |
f644b8bc |
8 | #include <X11/Xresource.h> |
858338d9 |
9 | #include <X11/XKBlib.h> |
dbeb9940 |
10 | #include <X11/Xft/Xft.h> |
858338d9 |
11 | |
d2435fcd |
12 | #define PROGNAME "xmenu" |
858338d9 |
13 | #define ITEMPREV 0 |
14 | #define ITEMNEXT 1 |
a7732690 |
15 | |
16 | /* macros */ |
17 | #define LEN(x) (sizeof (x) / sizeof (x[0])) |
f1583285 |
18 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
d8a7caf2 |
19 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
a7732690 |
20 | |
21 | /* color enum */ |
22 | enum {ColorFG, ColorBG, ColorLast}; |
23 | |
24 | /* draw context structure */ |
25 | struct DC { |
dbeb9940 |
26 | XftColor normal[ColorLast]; |
27 | XftColor selected[ColorLast]; |
fd530f3f |
28 | XftColor border; |
29 | XftColor separator; |
a7732690 |
30 | |
a7732690 |
31 | GC gc; |
dbeb9940 |
32 | XftFont *font; |
a7732690 |
33 | }; |
34 | |
35 | /* menu geometry structure */ |
257e42cc |
36 | struct Geometry { |
37 | int border; /* window border width */ |
38 | int separator; /* menu separator width */ |
39 | int itemw, itemh; /* item width and height */ |
a7732690 |
40 | int cursx, cursy; /* cursor position */ |
41 | int screenw, screenh; /* screen width and height */ |
42 | }; |
43 | |
44 | /* menu item structure */ |
45 | struct Item { |
d8a7caf2 |
46 | char *label; /* string to be drawed on menu */ |
47 | char *output; /* string to be outputed when item is clicked */ |
48 | int y; /* item y position relative to menu */ |
49 | int h; /* item height */ |
50 | size_t labellen; /* strlen(label) */ |
858338d9 |
51 | struct Item *prev; /* previous item */ |
d8a7caf2 |
52 | struct Item *next; /* next item */ |
53 | struct Menu *submenu; /* submenu spawned by clicking on item */ |
a7732690 |
54 | }; |
55 | |
56 | /* menu structure */ |
57 | struct Menu { |
d8a7caf2 |
58 | struct Menu *parent; /* parent menu */ |
59 | struct Item *caller; /* item that spawned the menu */ |
60 | struct Item *list; /* list of items contained by the menu */ |
61 | struct Item *selected; /* item currently selected in the menu */ |
62 | int x, y, w, h; /* menu geometry */ |
63 | unsigned level; /* menu level relative to root */ |
64 | Drawable pixmap; /* pixmap to draw the menu on */ |
dbeb9940 |
65 | XftDraw *draw; |
d8a7caf2 |
66 | Window win; /* menu window to map on the screen */ |
a7732690 |
67 | }; |
68 | |
257e42cc |
69 | /* functions declarations */ |
f644b8bc |
70 | static void getresources(void); |
257e42cc |
71 | static void getcolor(const char *s, XftColor *color); |
a7732690 |
72 | static void setupdc(void); |
beae67a6 |
73 | static void calcgeom(struct Geometry *geom); |
a80fee22 |
74 | static struct Item *allocitem(const char *label, const char *output); |
a7732690 |
75 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
a5ddd2cd |
76 | static struct Menu * buildmenutree(unsigned level, const char *label, const char *output); |
257e42cc |
77 | static struct Menu *parsestdin(void); |
beae67a6 |
78 | static void calcmenu(struct Geometry *geom, struct Menu *menu); |
85003546 |
79 | static void grabpointer(void); |
80 | static void grabkeyboard(void); |
257e42cc |
81 | static struct Menu *getmenu(struct Menu *currmenu, Window win); |
82 | static struct Item *getitem(struct Menu *menu, int y); |
83 | static void mapmenu(struct Menu *currmenu); |
84 | static void drawseparator(struct Menu *menu, struct Item *item); |
85 | static void drawitem(struct Menu *menu, struct Item *item, XftColor *color); |
86 | static void drawmenu(struct Menu *currmenu); |
87 | static struct Item *itemcycle(struct Menu *currmenu, int direction); |
88 | static void run(struct Menu *currmenu); |
ec4ed1ac |
89 | static void freemenu(struct Menu *menu); |
15dfafdf |
90 | static void cleanup(void); |
a7732690 |
91 | static void usage(void); |
92 | |
738ed9d5 |
93 | /* X stuff */ |
a7732690 |
94 | static Display *dpy; |
c6d9174e |
95 | static int screen; |
dbeb9940 |
96 | static Visual *visual; |
a7732690 |
97 | static Window rootwin; |
c6d9174e |
98 | static Colormap colormap; |
a7732690 |
99 | static struct DC dc; |
a7732690 |
100 | |
a7732690 |
101 | #include "config.h" |
102 | |
beae67a6 |
103 | /* xmenu: generate menu from stdin and print selected entry to stdout */ |
a7732690 |
104 | int |
105 | main(int argc, char *argv[]) |
106 | { |
257e42cc |
107 | struct Menu *rootmenu; |
beae67a6 |
108 | struct Geometry geom; |
a7732690 |
109 | int ch; |
110 | |
27443900 |
111 | while ((ch = getopt(argc, argv, "")) != -1) { |
a7732690 |
112 | switch (ch) { |
a7732690 |
113 | default: |
114 | usage(); |
115 | break; |
116 | } |
117 | } |
118 | argc -= optind; |
119 | argv += optind; |
120 | |
c6d9174e |
121 | if (argc != 0) |
122 | usage(); |
123 | |
a7732690 |
124 | /* open connection to server and set X variables */ |
125 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
126 | errx(1, "cannot open display"); |
127 | screen = DefaultScreen(dpy); |
dbeb9940 |
128 | visual = DefaultVisual(dpy, screen); |
a7732690 |
129 | rootwin = RootWindow(dpy, screen); |
130 | colormap = DefaultColormap(dpy, screen); |
21a9aaec |
131 | |
a7732690 |
132 | /* setup */ |
f644b8bc |
133 | getresources(); |
a7732690 |
134 | setupdc(); |
beae67a6 |
135 | calcgeom(&geom); |
a7732690 |
136 | |
137 | /* generate menus and recalculate them */ |
257e42cc |
138 | rootmenu = parsestdin(); |
a7732690 |
139 | if (rootmenu == NULL) |
140 | errx(1, "no menu generated"); |
beae67a6 |
141 | calcmenu(&geom, rootmenu); |
a7732690 |
142 | |
465d07db |
143 | /* grab mouse and keyboard */ |
27443900 |
144 | grabpointer(); |
145 | grabkeyboard(); |
465d07db |
146 | |
a7732690 |
147 | /* run event loop */ |
257e42cc |
148 | run(rootmenu); |
a7732690 |
149 | |
15dfafdf |
150 | /* freeing stuff */ |
151 | freemenu(rootmenu); |
152 | cleanup(); |
153 | |
21a9aaec |
154 | return 0; |
155 | } |
156 | |
f644b8bc |
157 | /* read xrdb for configuration options */ |
158 | static void |
159 | getresources(void) |
160 | { |
161 | char *xrm; |
162 | long n; |
fd530f3f |
163 | char *type; |
164 | XrmDatabase xdb; |
165 | XrmValue xval; |
f644b8bc |
166 | |
167 | XrmInitialize(); |
fd530f3f |
168 | if ((xrm = XResourceManagerString(dpy)) == NULL) |
169 | return; |
170 | |
171 | xdb = XrmGetStringDatabase(xrm); |
172 | |
173 | if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True) |
174 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
175 | border_pixels = n; |
176 | if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True) |
177 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
178 | separator_pixels = n; |
179 | if (XrmGetResource(xdb, "xmenu.padding", "*", &type, &xval) == True) |
180 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
181 | padding_pixels = n; |
182 | if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True) |
183 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
184 | width_pixels = n; |
185 | if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True) |
186 | background_color = strdup(xval.addr); |
187 | if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True) |
188 | foreground_color = strdup(xval.addr); |
189 | if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True) |
190 | selbackground_color = strdup(xval.addr); |
191 | if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True) |
192 | selforeground_color = strdup(xval.addr); |
193 | if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True) |
194 | separator_color = strdup(xval.addr); |
195 | if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True) |
196 | border_color = strdup(xval.addr); |
197 | if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True) |
198 | font = strdup(xval.addr); |
199 | |
200 | XrmDestroyDatabase(xdb); |
f644b8bc |
201 | } |
202 | |
a7732690 |
203 | /* get color from color string */ |
dbeb9940 |
204 | static void |
205 | getcolor(const char *s, XftColor *color) |
a7732690 |
206 | { |
dbeb9940 |
207 | if(!XftColorAllocName(dpy, visual, colormap, s, color)) |
a7732690 |
208 | errx(1, "cannot allocate color: %s", s); |
a7732690 |
209 | } |
210 | |
211 | /* init draw context */ |
212 | static void |
213 | setupdc(void) |
214 | { |
215 | /* get color pixels */ |
fd530f3f |
216 | getcolor(background_color, &dc.normal[ColorBG]); |
217 | getcolor(foreground_color, &dc.normal[ColorFG]); |
218 | getcolor(selbackground_color, &dc.selected[ColorBG]); |
219 | getcolor(selforeground_color, &dc.selected[ColorFG]); |
220 | getcolor(separator_color, &dc.separator); |
221 | getcolor(border_color, &dc.border); |
a7732690 |
222 | |
223 | /* try to get font */ |
dbeb9940 |
224 | if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL) |
a7732690 |
225 | errx(1, "cannot load font"); |
a7732690 |
226 | |
fd530f3f |
227 | /* create common GC */ |
a7732690 |
228 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
a7732690 |
229 | } |
230 | |
257e42cc |
231 | /* calculate menu and screen geometry */ |
a7732690 |
232 | static void |
beae67a6 |
233 | calcgeom(struct Geometry *geom) |
a7732690 |
234 | { |
257e42cc |
235 | Window w1, w2; /* unused variables */ |
236 | int a, b; /* unused variables */ |
237 | unsigned mask; /* unused variable */ |
238 | |
beae67a6 |
239 | XQueryPointer(dpy, rootwin, &w1, &w2, &geom->cursx, &geom->cursy, &a, &b, &mask); |
240 | geom->screenw = DisplayWidth(dpy, screen); |
241 | geom->screenh = DisplayHeight(dpy, screen); |
242 | geom->itemh = dc.font->height + padding_pixels * 2; |
243 | geom->itemw = width_pixels; |
244 | geom->border = border_pixels; |
245 | geom->separator = separator_pixels; |
a7732690 |
246 | } |
247 | |
a7732690 |
248 | /* allocate an item */ |
249 | static struct Item * |
a80fee22 |
250 | allocitem(const char *label, const char *output) |
a7732690 |
251 | { |
252 | struct Item *item; |
253 | |
254 | if ((item = malloc(sizeof *item)) == NULL) |
255 | err(1, "malloc"); |
a5ddd2cd |
256 | if (label == NULL) { |
7fbd1c5e |
257 | item->label = NULL; |
258 | item->output = NULL; |
259 | } else { |
260 | if ((item->label = strdup(label)) == NULL) |
261 | err(1, "strdup"); |
15dfafdf |
262 | if (label == output) { |
263 | item->output = item->label; |
264 | } else { |
265 | if ((item->output = strdup(output)) == NULL) |
266 | err(1, "strdup"); |
267 | } |
7fbd1c5e |
268 | } |
a80fee22 |
269 | item->y = 0; |
beae67a6 |
270 | item->h = 0; |
f1583285 |
271 | if (item->label == NULL) |
272 | item->labellen = 0; |
273 | else |
274 | item->labellen = strlen(item->label); |
a7732690 |
275 | item->next = NULL; |
276 | item->submenu = NULL; |
277 | |
278 | return item; |
279 | } |
280 | |
281 | /* allocate a menu */ |
282 | static struct Menu * |
283 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
284 | { |
285 | XSetWindowAttributes swa; |
286 | struct Menu *menu; |
287 | |
288 | if ((menu = malloc(sizeof *menu)) == NULL) |
289 | err(1, "malloc"); |
290 | menu->parent = parent; |
291 | menu->list = list; |
d888f2ca |
292 | menu->caller = NULL; |
a7732690 |
293 | menu->selected = NULL; |
beae67a6 |
294 | menu->w = 0; /* calculated by calcmenu() */ |
a80fee22 |
295 | menu->h = 0; /* calculated by calcmenu() */ |
296 | menu->x = 0; /* calculated by calcmenu() */ |
297 | menu->y = 0; /* calculated by calcmenu() */ |
a7732690 |
298 | menu->level = level; |
a7732690 |
299 | |
27443900 |
300 | swa.override_redirect = True; |
fd530f3f |
301 | swa.background_pixel = dc.normal[ColorBG].pixel; |
302 | swa.border_pixel = dc.border.pixel; |
303 | swa.save_under = True; /* pop-up windows should save_under*/ |
a7732690 |
304 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
f15fc339 |
305 | | PointerMotionMask | LeaveWindowMask; |
beae67a6 |
306 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0, |
a7732690 |
307 | CopyFromParent, CopyFromParent, CopyFromParent, |
fd530f3f |
308 | CWOverrideRedirect | CWBackPixel | |
309 | CWBorderPixel | CWEventMask | CWSaveUnder, |
a7732690 |
310 | &swa); |
311 | |
312 | return menu; |
313 | } |
314 | |
a5ddd2cd |
315 | /* build the menu tree */ |
316 | static struct Menu * |
317 | buildmenutree(unsigned level, const char *label, const char *output) |
318 | { |
319 | static struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
320 | static struct Menu *rootmenu = NULL; /* menu to be returned */ |
321 | struct Item *curritem = NULL; /* item currently being read */ |
322 | struct Item *item; /* dummy item for loops */ |
323 | struct Menu *menu; /* dummy menu for loops */ |
324 | unsigned i; |
325 | |
326 | /* create the item */ |
327 | curritem = allocitem(label, output); |
328 | |
329 | /* put the item in the menu tree */ |
330 | if (prevmenu == NULL) { /* there is no menu yet */ |
331 | menu = allocmenu(NULL, curritem, level); |
332 | rootmenu = menu; |
333 | prevmenu = menu; |
334 | curritem->prev = NULL; |
335 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu */ |
336 | /* go up the menu tree until find the menu this item continues */ |
337 | for (menu = prevmenu, i = level; |
338 | menu != NULL && i != prevmenu->level; |
339 | menu = menu->parent, i++) |
340 | ; |
341 | if (menu == NULL) |
342 | errx(1, "reached NULL menu"); |
343 | |
344 | /* find last item in the new menu */ |
345 | for (item = menu->list; item->next != NULL; item = item->next) |
346 | ; |
347 | |
348 | prevmenu = menu; |
349 | item->next = curritem; |
350 | curritem->prev = item; |
351 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
352 | /* find last item in the previous menu */ |
353 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
354 | ; |
355 | |
356 | item->next = curritem; |
357 | curritem->prev = item; |
358 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
359 | menu = allocmenu(prevmenu, curritem, level); |
360 | |
361 | /* find last item in the previous menu */ |
362 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
363 | ; |
364 | |
365 | prevmenu = menu; |
366 | menu->caller = item; |
367 | item->submenu = menu; |
368 | curritem->prev = NULL; |
369 | } |
370 | |
371 | return rootmenu; |
372 | } |
373 | |
a7732690 |
374 | /* create menus and items from the stdin */ |
257e42cc |
375 | static struct Menu * |
a7732690 |
376 | parsestdin(void) |
377 | { |
a5ddd2cd |
378 | struct Menu *rootmenu; |
a7732690 |
379 | char *s, buf[BUFSIZ]; |
380 | char *label, *output; |
381 | unsigned level = 0; |
257e42cc |
382 | |
ddb15acf |
383 | rootmenu = NULL; |
384 | |
a7732690 |
385 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
a5ddd2cd |
386 | /* get the indentation level */ |
387 | level = strspn(buf, "\t"); |
a7732690 |
388 | |
a5ddd2cd |
389 | /* get the label */ |
390 | s = level + buf; |
391 | label = strtok(s, "\t\n"); |
a7732690 |
392 | |
a5ddd2cd |
393 | /* get the output */ |
394 | output = strtok(NULL, "\n"); |
395 | if (output == NULL) { |
396 | output = label; |
397 | } else { |
398 | while (*output == '\t') |
399 | output++; |
a7732690 |
400 | } |
a5ddd2cd |
401 | |
402 | rootmenu = buildmenutree(level, label, output); |
a7732690 |
403 | } |
a7732690 |
404 | |
257e42cc |
405 | return rootmenu; |
a7732690 |
406 | } |
407 | |
21a9aaec |
408 | /* recursivelly calculate menu geometry and set window hints */ |
a7732690 |
409 | static void |
beae67a6 |
410 | calcmenu(struct Geometry *geom, struct Menu *menu) |
a7732690 |
411 | { |
d2435fcd |
412 | static XClassHint classh = {PROGNAME, PROGNAME}; |
a7732690 |
413 | XWindowChanges changes; |
09c13122 |
414 | XSizeHints sizeh; |
dbeb9940 |
415 | XGlyphInfo ext; |
a80fee22 |
416 | struct Item *item; |
f1583285 |
417 | int labelwidth; |
ec4ed1ac |
418 | int width, height; |
a7732690 |
419 | |
f1583285 |
420 | /* calculate items positions and menu width and height */ |
beae67a6 |
421 | menu->w = geom->itemw; |
a80fee22 |
422 | for (item = menu->list; item != NULL; item = item->next) { |
423 | item->y = menu->h; |
beae67a6 |
424 | |
7fbd1c5e |
425 | if (item->label == NULL) /* height for separator item */ |
beae67a6 |
426 | item->h = geom->separator; |
a80fee22 |
427 | else |
beae67a6 |
428 | item->h = geom->itemh; |
429 | menu->h += item->h; |
f1583285 |
430 | |
dbeb9940 |
431 | XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label, |
432 | item->labellen, &ext); |
433 | labelwidth = ext.xOff + dc.font->height * 2; |
f1583285 |
434 | menu->w = MAX(menu->w, labelwidth); |
a80fee22 |
435 | } |
a7732690 |
436 | |
437 | /* calculate menu's x and y positions */ |
beae67a6 |
438 | width = menu->w + geom->border * 2; |
439 | height = menu->h + geom->border * 2; |
a7732690 |
440 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
beae67a6 |
441 | if (geom->screenw - geom->cursx >= menu->w) |
442 | menu->x = geom->cursx; |
443 | else if (geom->cursx > width) |
444 | menu->x = geom->cursx - width; |
445 | |
446 | if (geom->screenh - geom->cursy >= height) |
447 | menu->y = geom->cursy; |
448 | else if (geom->screenh > height) |
449 | menu->y = geom->screenh - height; |
a7732690 |
450 | } else { /* else, calculate in respect to parent menu */ |
beae67a6 |
451 | if (geom->screenw - (menu->parent->x + menu->parent->w + geom->border) >= width) |
452 | menu->x = menu->parent->x + menu->parent->w + geom->border; |
453 | else if (menu->parent->x > menu->w + geom->border) |
454 | menu->x = menu->parent->x - menu->w - geom->border; |
a7732690 |
455 | |
beae67a6 |
456 | if (geom->screenh - (menu->caller->y + menu->parent->y) > height) |
8455c369 |
457 | menu->y = menu->caller->y + menu->parent->y; |
beae67a6 |
458 | else if (geom->screenh - menu->parent->y > height) |
a7732690 |
459 | menu->y = menu->parent->y; |
beae67a6 |
460 | else if (geom->screenh > height) |
461 | menu->y = geom->screenh - height; |
a7732690 |
462 | } |
463 | |
464 | /* update menu geometry */ |
beae67a6 |
465 | changes.border_width = geom->border; |
a7732690 |
466 | changes.height = menu->h; |
f1583285 |
467 | changes.width = menu->w; |
a7732690 |
468 | changes.x = menu->x; |
469 | changes.y = menu->y; |
beae67a6 |
470 | XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes); |
a7732690 |
471 | |
d2435fcd |
472 | /* set window manager hints */ |
09c13122 |
473 | sizeh.flags = PMaxSize | PMinSize; |
474 | sizeh.min_width = sizeh.max_width = menu->w; |
475 | sizeh.min_height = sizeh.max_height = menu->h; |
0c1a7864 |
476 | XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh, |
d2435fcd |
477 | NULL, &classh); |
09c13122 |
478 | |
dbeb9940 |
479 | /* create pixmap and XftDraw */ |
f15fc339 |
480 | menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h, |
481 | DefaultDepth(dpy, screen)); |
dbeb9940 |
482 | menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap); |
f15fc339 |
483 | |
a80fee22 |
484 | /* calculate positions of submenus */ |
a7732690 |
485 | for (item = menu->list; item != NULL; item = item->next) { |
486 | if (item->submenu != NULL) |
beae67a6 |
487 | calcmenu(geom, item->submenu); |
a7732690 |
488 | } |
489 | } |
490 | |
85003546 |
491 | /* try to grab pointer, we may have to wait for another process to ungrab */ |
492 | static void |
493 | grabpointer(void) |
494 | { |
495 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; |
496 | int i; |
497 | |
498 | for (i = 0; i < 1000; i++) { |
499 | if (XGrabPointer(dpy, rootwin, True, ButtonPressMask, |
500 | GrabModeAsync, GrabModeAsync, None, |
501 | None, CurrentTime) == GrabSuccess) |
502 | return; |
503 | nanosleep(&ts, NULL); |
504 | } |
505 | errx(1, "cannot grab keyboard"); |
506 | } |
507 | |
508 | /* try to grab keyboard, we may have to wait for another process to ungrab */ |
509 | static void |
510 | grabkeyboard(void) |
511 | { |
512 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; |
513 | int i; |
514 | |
515 | for (i = 0; i < 1000; i++) { |
516 | if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync, |
517 | GrabModeAsync, CurrentTime) == GrabSuccess) |
518 | return; |
519 | nanosleep(&ts, NULL); |
520 | } |
521 | errx(1, "cannot grab keyboard"); |
522 | } |
523 | |
0c1a7864 |
524 | /* get menu of given window */ |
525 | static struct Menu * |
257e42cc |
526 | getmenu(struct Menu *currmenu, Window win) |
a7732690 |
527 | { |
0c1a7864 |
528 | struct Menu *menu; |
a7732690 |
529 | |
0c1a7864 |
530 | for (menu = currmenu; menu != NULL; menu = menu->parent) |
531 | if (menu->win == win) |
532 | return menu; |
a7732690 |
533 | |
0c1a7864 |
534 | return NULL; |
535 | } |
536 | |
537 | /* get item of given menu and position */ |
538 | static struct Item * |
539 | getitem(struct Menu *menu, int y) |
540 | { |
541 | struct Item *item; |
a7732690 |
542 | |
0c1a7864 |
543 | if (menu == NULL) |
544 | return NULL; |
545 | |
546 | for (item = menu->list; item != NULL; item = item->next) |
547 | if (y >= item->y && y <= item->y + item->h) |
548 | return item; |
549 | |
550 | return NULL; |
a7732690 |
551 | } |
552 | |
257e42cc |
553 | /* umap previous menus and map current menu and its parents */ |
a7732690 |
554 | static void |
257e42cc |
555 | mapmenu(struct Menu *currmenu) |
a7732690 |
556 | { |
257e42cc |
557 | static struct Menu *prevmenu = NULL; |
d8a7caf2 |
558 | struct Menu *menu, *menu_; |
d8a7caf2 |
559 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
560 | unsigned minlevel; /* level of the closest to root menu */ |
561 | unsigned maxlevel; /* level of the closest to root menu */ |
a7732690 |
562 | |
257e42cc |
563 | /* do not remap current menu if it wasn't updated*/ |
564 | if (prevmenu == currmenu) |
a7732690 |
565 | return; |
566 | |
257e42cc |
567 | /* if this is the first time mapping, skip calculations */ |
568 | if (prevmenu == NULL) { |
fd530f3f |
569 | XMapWindow(dpy, currmenu->win); |
cdc4d51f |
570 | prevmenu = currmenu; |
571 | return; |
27443900 |
572 | } |
573 | |
d8a7caf2 |
574 | /* find lowest common ancestor menu */ |
257e42cc |
575 | minlevel = MIN(currmenu->level, prevmenu->level); |
576 | maxlevel = MAX(currmenu->level, prevmenu->level); |
577 | if (currmenu->level == maxlevel) { |
27443900 |
578 | menu = currmenu; |
257e42cc |
579 | menu_ = prevmenu; |
580 | } else { |
581 | menu = prevmenu; |
582 | menu_ = currmenu; |
27443900 |
583 | } |
584 | while (menu->level > minlevel) |
585 | menu = menu->parent; |
586 | while (menu != menu_) { |
587 | menu = menu->parent; |
588 | menu_ = menu_->parent; |
d8a7caf2 |
589 | } |
27443900 |
590 | lcamenu = menu; |
d8a7caf2 |
591 | |
873c080c |
592 | /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */ |
257e42cc |
593 | for (menu = prevmenu; menu != lcamenu; menu = menu->parent) { |
8455c369 |
594 | menu->selected = NULL; |
a7732690 |
595 | XUnmapWindow(dpy, menu->win); |
596 | } |
597 | |
873c080c |
598 | /* map menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d8a7caf2 |
599 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
600 | XMapWindow(dpy, menu->win); |
fd530f3f |
601 | } |
257e42cc |
602 | |
257e42cc |
603 | prevmenu = currmenu; |
fd530f3f |
604 | } |
605 | |
606 | /* draw separator item */ |
607 | static void |
608 | drawseparator(struct Menu *menu, struct Item *item) |
609 | { |
6b5123e7 |
610 | int y; |
fd530f3f |
611 | |
6b5123e7 |
612 | y = item->y + item->h/2; |
fd530f3f |
613 | |
614 | XSetForeground(dpy, dc.gc, dc.separator.pixel); |
6b5123e7 |
615 | XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y); |
fd530f3f |
616 | } |
617 | |
618 | /* draw regular item */ |
619 | static void |
620 | drawitem(struct Menu *menu, struct Item *item, XftColor *color) |
621 | { |
622 | int x, y; |
623 | |
cdc4d51f |
624 | x = dc.font->height; |
257e42cc |
625 | y = item->y + item->h/2 + dc.font->ascent/2 - 1; |
fd530f3f |
626 | XSetForeground(dpy, dc.gc, color[ColorFG].pixel); |
627 | XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font, |
628 | x, y, item->label, item->labellen); |
629 | |
630 | /* draw triangle, if item contains a submenu */ |
631 | if (item->submenu != NULL) { |
257e42cc |
632 | x = menu->w - dc.font->height/2 - triangle_width/2; |
633 | y = item->y + item->h/2 - triangle_height/2 - 1; |
fd530f3f |
634 | |
635 | XPoint triangle[] = { |
636 | {x, y}, |
637 | {x + triangle_width, y + triangle_height/2}, |
638 | {x, y + triangle_height}, |
639 | {x, y} |
640 | }; |
641 | |
642 | XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle), |
643 | Convex, CoordModeOrigin); |
d888f2ca |
644 | } |
a7732690 |
645 | } |
646 | |
647 | /* draw items of the current menu and of its ancestors */ |
648 | static void |
257e42cc |
649 | drawmenu(struct Menu *currmenu) |
a7732690 |
650 | { |
651 | struct Menu *menu; |
652 | struct Item *item; |
653 | |
654 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
655 | for (item = menu->list; item != NULL; item = item->next) { |
dbeb9940 |
656 | XftColor *color; |
a7732690 |
657 | |
658 | /* determine item color */ |
fd530f3f |
659 | if (item == menu->selected && item->label != NULL) |
f644b8bc |
660 | color = dc.selected; |
a7732690 |
661 | else |
f644b8bc |
662 | color = dc.normal; |
663 | |
a7732690 |
664 | /* draw item box */ |
dbeb9940 |
665 | XSetForeground(dpy, dc.gc, color[ColorBG].pixel); |
f15fc339 |
666 | XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
f1583285 |
667 | menu->w, item->h); |
7fbd1c5e |
668 | |
fd530f3f |
669 | if (item->label == NULL) /* item is a separator */ |
670 | drawseparator(menu, item); |
671 | else /* item is a regular item */ |
672 | drawitem(menu, item, color); |
a7732690 |
673 | } |
fd530f3f |
674 | |
675 | XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0, |
676 | menu->w, menu->h, 0, 0); |
a7732690 |
677 | } |
678 | } |
679 | |
858338d9 |
680 | /* cycle through the items; non-zero direction is next, zero is prev */ |
681 | static struct Item * |
257e42cc |
682 | itemcycle(struct Menu *currmenu, int direction) |
858338d9 |
683 | { |
684 | struct Item *item; |
685 | struct Item *lastitem; |
686 | |
687 | item = NULL; |
688 | |
689 | if (direction == ITEMNEXT) { |
690 | if (currmenu->selected == NULL) |
691 | item = currmenu->list; |
692 | else if (currmenu->selected->next != NULL) |
693 | item = currmenu->selected->next; |
694 | |
695 | while (item != NULL && item->label == NULL) |
696 | item = item->next; |
697 | |
698 | if (item == NULL) |
699 | item = currmenu->list; |
700 | } else { |
701 | for (lastitem = currmenu->list; |
702 | lastitem != NULL && lastitem->next != NULL; |
703 | lastitem = lastitem->next) |
704 | ; |
705 | |
706 | if (currmenu->selected == NULL) |
707 | item = lastitem; |
708 | else if (currmenu->selected->prev != NULL) |
709 | item = currmenu->selected->prev; |
710 | |
711 | while (item != NULL && item->label == NULL) |
712 | item = item->prev; |
713 | |
714 | if (item == NULL) |
715 | item = lastitem; |
716 | } |
717 | |
718 | return item; |
719 | } |
720 | |
a7732690 |
721 | /* run event loop */ |
722 | static void |
257e42cc |
723 | run(struct Menu *currmenu) |
a7732690 |
724 | { |
725 | struct Menu *menu; |
726 | struct Item *item; |
727 | struct Item *previtem = NULL; |
858338d9 |
728 | KeySym ksym; |
a7732690 |
729 | XEvent ev; |
730 | |
257e42cc |
731 | mapmenu(currmenu); |
fd530f3f |
732 | |
a7732690 |
733 | while (!XNextEvent(dpy, &ev)) { |
734 | switch(ev.type) { |
735 | case Expose: |
858338d9 |
736 | if (ev.xexpose.count == 0) |
257e42cc |
737 | drawmenu(currmenu); |
a7732690 |
738 | break; |
739 | case MotionNotify: |
257e42cc |
740 | menu = getmenu(currmenu, ev.xbutton.window); |
0c1a7864 |
741 | item = getitem(menu, ev.xbutton.y); |
c6d9174e |
742 | if (menu == NULL || item == NULL || previtem == item) |
fd530f3f |
743 | break; |
c6d9174e |
744 | previtem = item; |
745 | menu->selected = item; |
746 | if (item->submenu != NULL) { |
747 | currmenu = item->submenu; |
748 | currmenu->selected = NULL; |
749 | } else { |
750 | currmenu = menu; |
a7732690 |
751 | } |
c6d9174e |
752 | mapmenu(currmenu); |
753 | drawmenu(currmenu); |
a7732690 |
754 | break; |
755 | case ButtonRelease: |
257e42cc |
756 | menu = getmenu(currmenu, ev.xbutton.window); |
0c1a7864 |
757 | item = getitem(menu, ev.xbutton.y); |
fd530f3f |
758 | if (menu == NULL || item == NULL) |
858338d9 |
759 | break; |
fd530f3f |
760 | selectitem: |
761 | if (item->label == NULL) |
762 | break; /* ignore separators */ |
763 | if (item->submenu != NULL) { |
257e42cc |
764 | currmenu = item->submenu; |
a7732690 |
765 | } else { |
fd530f3f |
766 | printf("%s\n", item->output); |
08f16589 |
767 | return; |
a7732690 |
768 | } |
257e42cc |
769 | mapmenu(currmenu); |
fd530f3f |
770 | currmenu->selected = currmenu->list; |
257e42cc |
771 | drawmenu(currmenu); |
fd530f3f |
772 | break; |
858338d9 |
773 | case ButtonPress: |
257e42cc |
774 | menu = getmenu(currmenu, ev.xbutton.window); |
0c1a7864 |
775 | if (menu == NULL) |
858338d9 |
776 | return; |
777 | break; |
778 | case KeyPress: |
779 | ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0); |
780 | |
0c1a7864 |
781 | /* esc closes xmenu when current menu is the root menu */ |
257e42cc |
782 | if (ksym == XK_Escape && currmenu->parent == NULL) |
858338d9 |
783 | return; |
784 | |
785 | /* Shift-Tab = ISO_Left_Tab */ |
786 | if (ksym == XK_Tab && (ev.xkey.state & ShiftMask)) |
787 | ksym = XK_ISO_Left_Tab; |
788 | |
789 | /* cycle through menu */ |
790 | item = NULL; |
791 | if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) { |
257e42cc |
792 | item = itemcycle(currmenu, ITEMPREV); |
858338d9 |
793 | } else if (ksym == XK_Tab || ksym == XK_Down) { |
257e42cc |
794 | item = itemcycle(currmenu, ITEMNEXT); |
858338d9 |
795 | } else if ((ksym == XK_Return || ksym == XK_Right) && |
796 | currmenu->selected != NULL) { |
797 | item = currmenu->selected; |
798 | goto selectitem; |
799 | } else if ((ksym == XK_Escape || ksym == XK_Left) && |
800 | currmenu->parent != NULL) { |
801 | item = currmenu->parent->selected; |
257e42cc |
802 | currmenu = currmenu->parent; |
803 | mapmenu(currmenu); |
858338d9 |
804 | } else |
805 | break; |
806 | currmenu->selected = item; |
257e42cc |
807 | drawmenu(currmenu); |
a7732690 |
808 | break; |
f15fc339 |
809 | case LeaveNotify: |
fd530f3f |
810 | previtem = NULL; |
f15fc339 |
811 | currmenu->selected = NULL; |
257e42cc |
812 | drawmenu(currmenu); |
f15fc339 |
813 | break; |
a7732690 |
814 | } |
815 | } |
816 | } |
817 | |
fd530f3f |
818 | /* recursivelly free pixmaps and destroy windows */ |
f15fc339 |
819 | static void |
ec4ed1ac |
820 | freemenu(struct Menu *menu) |
f15fc339 |
821 | { |
822 | struct Item *item; |
ec4ed1ac |
823 | struct Item *tmp; |
f15fc339 |
824 | |
ec4ed1ac |
825 | item = menu->list; |
826 | while (item != NULL) { |
f15fc339 |
827 | if (item->submenu != NULL) |
ec4ed1ac |
828 | freemenu(item->submenu); |
829 | tmp = item; |
830 | item = item->next; |
15dfafdf |
831 | if (tmp->label != tmp->output) |
832 | free(tmp->label); |
6b5123e7 |
833 | free(tmp->output); |
ec4ed1ac |
834 | free(tmp); |
835 | } |
f15fc339 |
836 | |
837 | XFreePixmap(dpy, menu->pixmap); |
dbeb9940 |
838 | XftDrawDestroy(menu->draw); |
f15fc339 |
839 | XDestroyWindow(dpy, menu->win); |
ec4ed1ac |
840 | free(menu); |
f15fc339 |
841 | } |
842 | |
a7732690 |
843 | /* cleanup and exit */ |
844 | static void |
15dfafdf |
845 | cleanup(void) |
a7732690 |
846 | { |
257e42cc |
847 | XUngrabPointer(dpy, CurrentTime); |
848 | XUngrabKeyboard(dpy, CurrentTime); |
849 | |
dbeb9940 |
850 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); |
851 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); |
852 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); |
853 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]); |
fd530f3f |
854 | XftColorFree(dpy, visual, colormap, &dc.separator); |
855 | XftColorFree(dpy, visual, colormap, &dc.border); |
dbeb9940 |
856 | |
f15fc339 |
857 | XFreeGC(dpy, dc.gc); |
a7732690 |
858 | XCloseDisplay(dpy); |
a7732690 |
859 | } |
860 | |
861 | /* show usage */ |
862 | static void |
863 | usage(void) |
864 | { |
c6d9174e |
865 | (void)fprintf(stderr, "usage: xmenu\n"); |
a7732690 |
866 | exit(1); |
867 | } |