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