cdeaefaa |
1 | #include <ctype.h> |
a7732690 |
2 | #include <err.h> |
05cfe1a0 |
3 | #include <errno.h> |
a7732690 |
4 | #include <stdio.h> |
5 | #include <stdlib.h> |
6 | #include <string.h> |
05cfe1a0 |
7 | #include <limits.h> |
8902c43b |
8 | #include <time.h> |
a7732690 |
9 | #include <unistd.h> |
10 | #include <X11/Xlib.h> |
8902c43b |
11 | #include <X11/Xatom.h> |
a7732690 |
12 | #include <X11/Xutil.h> |
f644b8bc |
13 | #include <X11/Xresource.h> |
858338d9 |
14 | #include <X11/XKBlib.h> |
dbeb9940 |
15 | #include <X11/Xft/Xft.h> |
237da982 |
16 | #include <X11/extensions/Xinerama.h> |
33376f54 |
17 | #include <Imlib2.h> |
8902c43b |
18 | #include "xmenu.h" |
858338d9 |
19 | |
8902c43b |
20 | /* |
21 | * Function declarations |
22 | */ |
23 | |
05cfe1a0 |
24 | /* argument parser */ |
237da982 |
25 | static void parseposition(char *optarg); |
05cfe1a0 |
26 | |
8902c43b |
27 | /* initializers, and their helper routines */ |
cdeaefaa |
28 | static void parsefonts(const char *s); |
8902c43b |
29 | static void ealloccolor(const char *s, XftColor *color); |
237da982 |
30 | static void initmonitor(void); |
8902c43b |
31 | static void initresources(void); |
32 | static void initdc(void); |
b6cf4847 |
33 | static void initiconsize(void); |
8902c43b |
34 | static void initatoms(void); |
35 | |
36 | /* structure builders, and their helper routines */ |
33376f54 |
37 | static struct Item *allocitem(const char *label, const char *output, char *file); |
a7732690 |
38 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
33376f54 |
39 | static struct Menu *buildmenutree(unsigned level, const char *label, const char *output, char *file); |
257e42cc |
40 | static struct Menu *parsestdin(void); |
8902c43b |
41 | |
3d853664 |
42 | /* text drawer, and its helper routine */ |
cdeaefaa |
43 | static FcChar32 getnextutf8char(const char *s, const char **end_ret); |
cdeaefaa |
44 | static int drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text); |
cdeaefaa |
45 | |
8902c43b |
46 | /* structure setters, and their helper routines */ |
71b4db92 |
47 | static void setupitems(struct Menu *menu); |
8902c43b |
48 | static void setupmenupos(struct Menu *menu); |
49 | static void setupmenu(struct Menu *menu, XClassHint *classh); |
50 | |
51 | /* grabbers */ |
85003546 |
52 | static void grabpointer(void); |
53 | static void grabkeyboard(void); |
8902c43b |
54 | |
3d853664 |
55 | /* item drawer, and its helper routine */ |
56 | static Imlib_Image loadicon(const char *file); |
57 | static void drawitems(struct Menu *menu); |
58 | |
59 | /* menu drawers and mappers */ |
60 | static void drawmenus(struct Menu *currmenu); |
257e42cc |
61 | static void mapmenu(struct Menu *currmenu); |
8902c43b |
62 | |
cdeaefaa |
63 | /* getters */ |
8902c43b |
64 | static struct Menu *getmenu(struct Menu *currmenu, Window win); |
65 | static struct Item *getitem(struct Menu *menu, int y); |
cdeaefaa |
66 | |
67 | /* cycle through items */ |
257e42cc |
68 | static struct Item *itemcycle(struct Menu *currmenu, int direction); |
cdeaefaa |
69 | |
70 | /* main event loop */ |
257e42cc |
71 | static void run(struct Menu *currmenu); |
8902c43b |
72 | |
73 | /* cleaners */ |
74 | static void cleanmenu(struct Menu *menu); |
15dfafdf |
75 | static void cleanup(void); |
8902c43b |
76 | |
77 | /* show usage */ |
a7732690 |
78 | static void usage(void); |
79 | |
8902c43b |
80 | |
81 | /* |
82 | * Variable declarations |
83 | */ |
84 | |
738ed9d5 |
85 | /* X stuff */ |
a7732690 |
86 | static Display *dpy; |
c6d9174e |
87 | static int screen; |
dbeb9940 |
88 | static Visual *visual; |
a7732690 |
89 | static Window rootwin; |
c6d9174e |
90 | static Colormap colormap; |
a7732690 |
91 | static struct DC dc; |
237da982 |
92 | static struct Monitor mon; |
8902c43b |
93 | static Atom utf8string; |
3bec05ea |
94 | static Atom wmdelete; |
8902c43b |
95 | static Atom netatom[NetLast]; |
3bec05ea |
96 | |
97 | /* flags */ |
71b4db92 |
98 | static int iflag = 0; /* whether to disable icons */ |
237da982 |
99 | static int mflag = 0; /* whether the user specified a monitor with -p */ |
100 | static int pflag = 0; /* whether the user specified a position with -p */ |
05cfe1a0 |
101 | static int wflag = 0; /* whether to let the window manager control XMenu */ |
a7732690 |
102 | |
8902c43b |
103 | /* include config variable */ |
a7732690 |
104 | #include "config.h" |
105 | |
8902c43b |
106 | |
107 | /* |
108 | * Function implementations |
109 | */ |
110 | |
beae67a6 |
111 | /* xmenu: generate menu from stdin and print selected entry to stdout */ |
a7732690 |
112 | int |
113 | main(int argc, char *argv[]) |
114 | { |
257e42cc |
115 | struct Menu *rootmenu; |
3bec05ea |
116 | XClassHint classh; |
a7732690 |
117 | int ch; |
118 | |
05cfe1a0 |
119 | while ((ch = getopt(argc, argv, "ip:w")) != -1) { |
a7732690 |
120 | switch (ch) { |
71b4db92 |
121 | case 'i': |
122 | iflag = 1; |
123 | break; |
05cfe1a0 |
124 | case 'p': |
125 | pflag = 1; |
126 | parseposition(optarg); |
05cfe1a0 |
127 | break; |
3bec05ea |
128 | case 'w': |
129 | wflag = 1; |
130 | break; |
a7732690 |
131 | default: |
132 | usage(); |
133 | break; |
134 | } |
135 | } |
136 | argc -= optind; |
137 | argv += optind; |
138 | |
3bec05ea |
139 | if (argc > 1) |
c6d9174e |
140 | usage(); |
141 | |
a7732690 |
142 | /* open connection to server and set X variables */ |
143 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
144 | errx(1, "cannot open display"); |
145 | screen = DefaultScreen(dpy); |
dbeb9940 |
146 | visual = DefaultVisual(dpy, screen); |
a7732690 |
147 | rootwin = RootWindow(dpy, screen); |
148 | colormap = DefaultColormap(dpy, screen); |
21a9aaec |
149 | |
33376f54 |
150 | /* imlib2 stuff */ |
71b4db92 |
151 | if (!iflag) { |
152 | imlib_set_cache_size(2048 * 1024); |
153 | imlib_context_set_dither(1); |
154 | imlib_context_set_display(dpy); |
155 | imlib_context_set_visual(visual); |
156 | imlib_context_set_colormap(colormap); |
157 | } |
33376f54 |
158 | |
8902c43b |
159 | /* initializers */ |
237da982 |
160 | initmonitor(); |
8902c43b |
161 | initresources(); |
162 | initdc(); |
b6cf4847 |
163 | initiconsize(); |
8902c43b |
164 | initatoms(); |
a7732690 |
165 | |
3bec05ea |
166 | /* set window class */ |
167 | classh.res_class = PROGNAME; |
168 | if (argc == 1) |
169 | classh.res_name = *argv; |
170 | else |
171 | classh.res_name = PROGNAME; |
172 | |
f8ffe0b2 |
173 | /* generate menus and set them up */ |
257e42cc |
174 | rootmenu = parsestdin(); |
a7732690 |
175 | if (rootmenu == NULL) |
176 | errx(1, "no menu generated"); |
8902c43b |
177 | setupmenu(rootmenu, &classh); |
a7732690 |
178 | |
465d07db |
179 | /* grab mouse and keyboard */ |
3bec05ea |
180 | if (!wflag) { |
181 | grabpointer(); |
182 | grabkeyboard(); |
183 | } |
465d07db |
184 | |
a7732690 |
185 | /* run event loop */ |
257e42cc |
186 | run(rootmenu); |
a7732690 |
187 | |
15dfafdf |
188 | /* freeing stuff */ |
8902c43b |
189 | cleanmenu(rootmenu); |
15dfafdf |
190 | cleanup(); |
191 | |
21a9aaec |
192 | return 0; |
193 | } |
194 | |
237da982 |
195 | /* parse position string from -p, |
196 | * put results on config.posx, config.posy, and config.monitor */ |
05cfe1a0 |
197 | static void |
237da982 |
198 | parseposition(char *optarg) |
05cfe1a0 |
199 | { |
200 | long n; |
237da982 |
201 | char *s = optarg; |
05cfe1a0 |
202 | char *endp; |
203 | |
204 | n = strtol(s, &endp, 10); |
205 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != 'x') |
206 | goto error; |
207 | config.posx = n; |
208 | s = endp+1; |
209 | n = strtol(s, &endp, 10); |
237da982 |
210 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s) |
05cfe1a0 |
211 | goto error; |
212 | config.posy = n; |
237da982 |
213 | if (*endp == ':') { |
214 | s = endp+1; |
215 | mflag = 1; |
216 | if (strncasecmp(s, "CUR", 3) == 0) { |
217 | config.monitor = -1; |
218 | endp = s+3; |
219 | } else { |
220 | n = strtol(s, &endp, 10); |
221 | if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0') |
222 | goto error; |
223 | config.monitor = n; |
224 | } |
225 | } else if (*endp != '\0') { |
226 | goto error; |
227 | } |
05cfe1a0 |
228 | |
229 | return; |
230 | |
231 | error: |
232 | errx(1, "improper position: %s", optarg); |
233 | } |
234 | |
cdeaefaa |
235 | /* parse color string */ |
236 | static void |
237 | parsefonts(const char *s) |
238 | { |
239 | const char *p; |
240 | char buf[1024]; |
241 | size_t nfont = 0; |
242 | |
243 | dc.nfonts = 1; |
244 | for (p = s; *p; p++) |
245 | if (*p == ',') |
246 | dc.nfonts++; |
247 | |
248 | if ((dc.fonts = calloc(dc.nfonts, sizeof *dc.fonts)) == NULL) |
249 | err(1, "calloc"); |
250 | |
251 | p = s; |
252 | while (*p != '\0') { |
253 | size_t i; |
254 | |
255 | i = 0; |
256 | while (isspace(*p)) |
257 | p++; |
258 | while (*p != '\0' && *p != ',') { |
259 | buf[i++] = *p++; |
260 | } |
261 | if (*p == ',') |
262 | p++; |
263 | buf[i] = '\0'; |
264 | if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL) |
265 | errx(1, "cannot load font"); |
266 | } |
267 | } |
268 | |
8902c43b |
269 | /* get color from color string */ |
270 | static void |
271 | ealloccolor(const char *s, XftColor *color) |
272 | { |
273 | if(!XftColorAllocName(dpy, visual, colormap, s, color)) |
274 | errx(1, "cannot allocate color: %s", s); |
275 | } |
276 | |
237da982 |
277 | /* query monitor information and cursor position */ |
278 | static void |
279 | initmonitor(void) |
280 | { |
281 | XineramaScreenInfo *info = NULL; |
282 | Window dw; /* dummy variable */ |
283 | int di; /* dummy variable */ |
284 | unsigned du; /* dummy variable */ |
285 | int cursx, cursy; /* cursor position */ |
286 | int nmons; |
287 | int i; |
288 | |
289 | XQueryPointer(dpy, rootwin, &dw, &dw, &cursx, &cursy, &di, &di, &du); |
290 | |
291 | mon.x = mon.y = 0; |
292 | mon.w = DisplayWidth(dpy, screen); |
293 | mon.h = DisplayHeight(dpy, screen); |
294 | |
295 | if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) { |
296 | int selmon = 0; |
297 | |
298 | if (!mflag || (mflag && (config.monitor < 0 || config.monitor >= nmons))) { |
299 | for (i = 0; i < nmons; i++) { |
d2304ecf |
300 | if (BETWEEN(cursx, info[i].x_org, info[i].x_org + info[i].width) && |
301 | BETWEEN(cursy, info[i].y_org, info[i].y_org + info[i].height)) { |
237da982 |
302 | selmon = i; |
303 | break; |
304 | } |
305 | } |
306 | } else { |
307 | selmon = config.monitor; |
308 | } |
309 | |
310 | mon.x = info[selmon].x_org; |
311 | mon.y = info[selmon].y_org; |
312 | mon.w = info[selmon].width; |
313 | mon.h = info[selmon].height; |
314 | } |
315 | |
316 | if (!pflag) { |
317 | config.posx = cursx; |
318 | config.posy = cursy; |
319 | } else if (mflag) { |
320 | config.posx += mon.x; |
321 | config.posy += mon.y; |
322 | } |
323 | } |
324 | |
f644b8bc |
325 | /* read xrdb for configuration options */ |
326 | static void |
8902c43b |
327 | initresources(void) |
f644b8bc |
328 | { |
329 | char *xrm; |
330 | long n; |
fd530f3f |
331 | char *type; |
332 | XrmDatabase xdb; |
333 | XrmValue xval; |
f644b8bc |
334 | |
335 | XrmInitialize(); |
fd530f3f |
336 | if ((xrm = XResourceManagerString(dpy)) == NULL) |
337 | return; |
338 | |
339 | xdb = XrmGetStringDatabase(xrm); |
340 | |
341 | if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True) |
342 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
343 | config.border_pixels = n; |
fd530f3f |
344 | if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True) |
345 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
346 | config.separator_pixels = n; |
685ca30d |
347 | if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True) |
fd530f3f |
348 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
349 | config.height_pixels = n; |
fd530f3f |
350 | if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True) |
351 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
8902c43b |
352 | config.width_pixels = n; |
eecf9b25 |
353 | if (XrmGetResource(xdb, "xmenu.gap", "*", &type, &xval) == True) |
354 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
355 | config.gap_pixels = n; |
fd530f3f |
356 | if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True) |
8902c43b |
357 | config.background_color = strdup(xval.addr); |
fd530f3f |
358 | if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True) |
8902c43b |
359 | config.foreground_color = strdup(xval.addr); |
fd530f3f |
360 | if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True) |
8902c43b |
361 | config.selbackground_color = strdup(xval.addr); |
fd530f3f |
362 | if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True) |
8902c43b |
363 | config.selforeground_color = strdup(xval.addr); |
fd530f3f |
364 | if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True) |
8902c43b |
365 | config.separator_color = strdup(xval.addr); |
fd530f3f |
366 | if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True) |
8902c43b |
367 | config.border_color = strdup(xval.addr); |
fd530f3f |
368 | if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True) |
8902c43b |
369 | config.font = strdup(xval.addr); |
fd530f3f |
370 | |
371 | XrmDestroyDatabase(xdb); |
f644b8bc |
372 | } |
373 | |
a7732690 |
374 | /* init draw context */ |
375 | static void |
8902c43b |
376 | initdc(void) |
a7732690 |
377 | { |
378 | /* get color pixels */ |
8902c43b |
379 | ealloccolor(config.background_color, &dc.normal[ColorBG]); |
380 | ealloccolor(config.foreground_color, &dc.normal[ColorFG]); |
381 | ealloccolor(config.selbackground_color, &dc.selected[ColorBG]); |
382 | ealloccolor(config.selforeground_color, &dc.selected[ColorFG]); |
383 | ealloccolor(config.separator_color, &dc.separator); |
384 | ealloccolor(config.border_color, &dc.border); |
a7732690 |
385 | |
cdeaefaa |
386 | /* parse fonts */ |
387 | parsefonts(config.font); |
a7732690 |
388 | |
fd530f3f |
389 | /* create common GC */ |
a7732690 |
390 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
a7732690 |
391 | } |
392 | |
b6cf4847 |
393 | /* calculate icon size */ |
a7732690 |
394 | static void |
b6cf4847 |
395 | initiconsize(void) |
a7732690 |
396 | { |
71b4db92 |
397 | config.iconsize = config.height_pixels - config.iconpadding * 2; |
8902c43b |
398 | } |
399 | |
400 | /* intern atoms */ |
401 | static void |
402 | initatoms(void) |
403 | { |
404 | utf8string = XInternAtom(dpy, "UTF8_STRING", False); |
405 | wmdelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); |
406 | netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); |
407 | netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); |
408 | netatom[NetWMWindowTypePopupMenu] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False); |
a7732690 |
409 | } |
410 | |
a7732690 |
411 | /* allocate an item */ |
412 | static struct Item * |
33376f54 |
413 | allocitem(const char *label, const char *output, char *file) |
a7732690 |
414 | { |
415 | struct Item *item; |
416 | |
417 | if ((item = malloc(sizeof *item)) == NULL) |
418 | err(1, "malloc"); |
a5ddd2cd |
419 | if (label == NULL) { |
7fbd1c5e |
420 | item->label = NULL; |
421 | item->output = NULL; |
422 | } else { |
423 | if ((item->label = strdup(label)) == NULL) |
424 | err(1, "strdup"); |
15dfafdf |
425 | if (label == output) { |
426 | item->output = item->label; |
427 | } else { |
428 | if ((item->output = strdup(output)) == NULL) |
429 | err(1, "strdup"); |
430 | } |
7fbd1c5e |
431 | } |
33376f54 |
432 | if (file == NULL) { |
433 | item->file = NULL; |
434 | } else { |
435 | if ((item->file = strdup(file)) == NULL) |
436 | err(1, "strdup"); |
437 | } |
a80fee22 |
438 | item->y = 0; |
beae67a6 |
439 | item->h = 0; |
a7732690 |
440 | item->next = NULL; |
441 | item->submenu = NULL; |
3bec05ea |
442 | item->icon = NULL; |
a7732690 |
443 | |
444 | return item; |
445 | } |
446 | |
a3a73837 |
447 | /* allocate a menu and create its window */ |
a7732690 |
448 | static struct Menu * |
449 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
450 | { |
451 | XSetWindowAttributes swa; |
452 | struct Menu *menu; |
453 | |
454 | if ((menu = malloc(sizeof *menu)) == NULL) |
455 | err(1, "malloc"); |
456 | menu->parent = parent; |
457 | menu->list = list; |
d888f2ca |
458 | menu->caller = NULL; |
a7732690 |
459 | menu->selected = NULL; |
d2304ecf |
460 | menu->w = 0; /* recalculated by setupmenu() */ |
461 | menu->h = 0; /* recalculated by setupmenu() */ |
462 | menu->x = mon.x; /* recalculated by setupmenu() */ |
463 | menu->y = mon.y; /* recalculated by setupmenu() */ |
a7732690 |
464 | menu->level = level; |
3d853664 |
465 | menu->drawn = 0; |
a2ff706d |
466 | menu->hasicon = 0; |
a7732690 |
467 | |
3bec05ea |
468 | swa.override_redirect = (wflag) ? False : True; |
fd530f3f |
469 | swa.background_pixel = dc.normal[ColorBG].pixel; |
470 | swa.border_pixel = dc.border.pixel; |
471 | swa.save_under = True; /* pop-up windows should save_under*/ |
a7732690 |
472 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
f15fc339 |
473 | | PointerMotionMask | LeaveWindowMask; |
3bec05ea |
474 | if (wflag) |
475 | swa.event_mask |= StructureNotifyMask; |
beae67a6 |
476 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0, |
a7732690 |
477 | CopyFromParent, CopyFromParent, CopyFromParent, |
fd530f3f |
478 | CWOverrideRedirect | CWBackPixel | |
479 | CWBorderPixel | CWEventMask | CWSaveUnder, |
a7732690 |
480 | &swa); |
481 | |
482 | return menu; |
483 | } |
484 | |
a5ddd2cd |
485 | /* build the menu tree */ |
486 | static struct Menu * |
33376f54 |
487 | buildmenutree(unsigned level, const char *label, const char *output, char *file) |
a5ddd2cd |
488 | { |
489 | static struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
490 | static struct Menu *rootmenu = NULL; /* menu to be returned */ |
491 | struct Item *curritem = NULL; /* item currently being read */ |
492 | struct Item *item; /* dummy item for loops */ |
493 | struct Menu *menu; /* dummy menu for loops */ |
494 | unsigned i; |
495 | |
496 | /* create the item */ |
33376f54 |
497 | curritem = allocitem(label, output, file); |
a5ddd2cd |
498 | |
499 | /* put the item in the menu tree */ |
500 | if (prevmenu == NULL) { /* there is no menu yet */ |
685ca30d |
501 | menu = allocmenu(NULL, curritem, level); |
502 | rootmenu = menu; |
503 | prevmenu = menu; |
504 | curritem->prev = NULL; |
a5ddd2cd |
505 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu */ |
506 | /* go up the menu tree until find the menu this item continues */ |
507 | for (menu = prevmenu, i = level; |
508 | menu != NULL && i != prevmenu->level; |
509 | menu = menu->parent, i++) |
510 | ; |
511 | if (menu == NULL) |
512 | errx(1, "reached NULL menu"); |
513 | |
514 | /* find last item in the new menu */ |
515 | for (item = menu->list; item->next != NULL; item = item->next) |
516 | ; |
517 | |
518 | prevmenu = menu; |
519 | item->next = curritem; |
520 | curritem->prev = item; |
521 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
522 | /* find last item in the previous menu */ |
523 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
524 | ; |
525 | |
526 | item->next = curritem; |
527 | curritem->prev = item; |
528 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
529 | menu = allocmenu(prevmenu, curritem, level); |
530 | |
531 | /* find last item in the previous menu */ |
532 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
533 | ; |
534 | |
535 | prevmenu = menu; |
536 | menu->caller = item; |
537 | item->submenu = menu; |
538 | curritem->prev = NULL; |
539 | } |
540 | |
a2ff706d |
541 | if (curritem->file) |
542 | prevmenu->hasicon = 1; |
543 | |
a5ddd2cd |
544 | return rootmenu; |
545 | } |
546 | |
a7732690 |
547 | /* create menus and items from the stdin */ |
257e42cc |
548 | static struct Menu * |
a7732690 |
549 | parsestdin(void) |
550 | { |
a5ddd2cd |
551 | struct Menu *rootmenu; |
a7732690 |
552 | char *s, buf[BUFSIZ]; |
33376f54 |
553 | char *file, *label, *output; |
a7732690 |
554 | unsigned level = 0; |
257e42cc |
555 | |
ddb15acf |
556 | rootmenu = NULL; |
557 | |
a7732690 |
558 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
a5ddd2cd |
559 | /* get the indentation level */ |
560 | level = strspn(buf, "\t"); |
a7732690 |
561 | |
a5ddd2cd |
562 | /* get the label */ |
563 | s = level + buf; |
564 | label = strtok(s, "\t\n"); |
a7732690 |
565 | |
33376f54 |
566 | /* get the filename */ |
567 | file = NULL; |
568 | if (label != NULL && strncmp(label, "IMG:", 4) == 0) { |
569 | file = label + 4; |
570 | label = strtok(NULL, "\t\n"); |
571 | } |
572 | |
a5ddd2cd |
573 | /* get the output */ |
574 | output = strtok(NULL, "\n"); |
575 | if (output == NULL) { |
576 | output = label; |
577 | } else { |
578 | while (*output == '\t') |
579 | output++; |
a7732690 |
580 | } |
a5ddd2cd |
581 | |
33376f54 |
582 | rootmenu = buildmenutree(level, label, output, file); |
a7732690 |
583 | } |
a7732690 |
584 | |
257e42cc |
585 | return rootmenu; |
a7732690 |
586 | } |
587 | |
3d853664 |
588 | /* get next utf8 char from s return its codepoint and set next_ret to pointer to end of character */ |
cdeaefaa |
589 | static FcChar32 |
590 | getnextutf8char(const char *s, const char **next_ret) |
591 | { |
cdeaefaa |
592 | static const unsigned char utfbyte[] = {0x80, 0x00, 0xC0, 0xE0, 0xF0}; |
cdeaefaa |
593 | static const unsigned char utfmask[] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; |
cdeaefaa |
594 | static const FcChar32 utfmin[] = {0, 0x00, 0x80, 0x800, 0x10000}; |
595 | static const FcChar32 utfmax[] = {0, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; |
3d853664 |
596 | /* 0xFFFD is the replacement character, used to represent unknown characters */ |
cdeaefaa |
597 | static const FcChar32 unknown = 0xFFFD; |
598 | FcChar32 ucode; /* FcChar32 type holds 32 bits */ |
599 | size_t usize = 0; /* n' of bytes of the utf8 character */ |
600 | size_t i; |
601 | |
602 | *next_ret = s+1; |
603 | |
604 | /* get code of first byte of utf8 character */ |
605 | for (i = 0; i < sizeof utfmask; i++) { |
606 | if (((unsigned char)*s & utfmask[i]) == utfbyte[i]) { |
607 | usize = i; |
608 | ucode = (unsigned char)*s & ~utfmask[i]; |
609 | break; |
610 | } |
611 | } |
612 | |
613 | /* if first byte is a continuation byte or is not allowed, return unknown */ |
614 | if (i == sizeof utfmask || usize == 0) |
615 | return unknown; |
616 | |
617 | /* check the other usize-1 bytes */ |
618 | s++; |
619 | for (i = 1; i < usize; i++) { |
620 | *next_ret = s+1; |
237da982 |
621 | /* if byte is nul or is not a continuation byte, return unknown */ |
cdeaefaa |
622 | if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0]) |
623 | return unknown; |
624 | /* 6 is the number of relevant bits in the continuation byte */ |
625 | ucode = (ucode << 6) | ((unsigned char)*s & ~utfmask[0]); |
626 | s++; |
627 | } |
628 | |
629 | /* check if ucode is invalid or in utf-16 surrogate halves */ |
630 | if (!BETWEEN(ucode, utfmin[usize], utfmax[usize]) |
631 | || BETWEEN (ucode, 0xD800, 0xDFFF)) |
632 | return unknown; |
633 | |
634 | return ucode; |
635 | } |
636 | |
637 | /* draw text into XftDraw */ |
638 | static int |
639 | drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text) |
640 | { |
641 | const char *s, *nexts; |
642 | FcChar32 ucode; |
643 | XftFont *currfont; |
644 | int textlen = 0; |
645 | |
646 | s = text; |
647 | while (*s) { |
648 | XGlyphInfo ext; |
649 | int charexists; |
650 | size_t len; |
651 | size_t i; |
652 | |
653 | charexists = 0; |
654 | ucode = getnextutf8char(s, &nexts); |
655 | for (i = 0; i < dc.nfonts; i++) { |
656 | charexists = XftCharExists(dpy, dc.fonts[i], ucode); |
657 | if (charexists) |
658 | break; |
659 | } |
660 | if (charexists) |
661 | currfont = dc.fonts[i]; |
662 | |
663 | len = nexts - s; |
664 | |
665 | XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)s, |
666 | len, &ext); |
667 | textlen += ext.xOff; |
668 | |
669 | if (draw) { |
670 | int texty; |
671 | |
64240f40 |
672 | texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent; |
cdeaefaa |
673 | XftDrawStringUtf8(draw, color, currfont, x, texty, |
674 | (XftChar8 *)s, len); |
675 | x += ext.xOff; |
676 | } |
677 | |
678 | s = nexts; |
679 | } |
680 | |
681 | return textlen; |
682 | } |
683 | |
71b4db92 |
684 | /* setup the height, width and icon of the items of a menu */ |
a7732690 |
685 | static void |
71b4db92 |
686 | setupitems(struct Menu *menu) |
a7732690 |
687 | { |
a80fee22 |
688 | struct Item *item; |
a7732690 |
689 | |
8902c43b |
690 | menu->w = config.width_pixels; |
a80fee22 |
691 | for (item = menu->list; item != NULL; item = item->next) { |
15362de4 |
692 | int itemwidth; |
693 | int textwidth; |
694 | |
a80fee22 |
695 | item->y = menu->h; |
beae67a6 |
696 | |
7fbd1c5e |
697 | if (item->label == NULL) /* height for separator item */ |
8902c43b |
698 | item->h = config.separator_pixels; |
a80fee22 |
699 | else |
8902c43b |
700 | item->h = config.height_pixels; |
beae67a6 |
701 | menu->h += item->h; |
f1583285 |
702 | |
15362de4 |
703 | if (item->label) |
d2304ecf |
704 | textwidth = drawtext(NULL, NULL, 0, 0, 0, item->label); |
15362de4 |
705 | else |
706 | textwidth = 0; |
685ca30d |
707 | |
71b4db92 |
708 | /* |
709 | * set menu width |
710 | * |
15362de4 |
711 | * the item width depends on the size of its label (textwidth), |
71b4db92 |
712 | * and it is only used to calculate the width of the menu (which |
713 | * is equal to the width of the largest item). |
714 | * |
715 | * the horizontal padding appears 4 times through the width of a |
15362de4 |
716 | * item: before and after its icon, and before and after its triangle. |
71b4db92 |
717 | * if the iflag is set (icons are disabled) then the horizontal |
15362de4 |
718 | * padding appears 3 times: before the label and around the triangle. |
71b4db92 |
719 | */ |
15362de4 |
720 | itemwidth = textwidth + config.triangle_width + config.horzpadding * 3; |
a2ff706d |
721 | itemwidth += (iflag || !menu->hasicon) ? 0 : config.iconsize + config.horzpadding; |
71b4db92 |
722 | menu->w = MAX(menu->w, itemwidth); |
a80fee22 |
723 | } |
f8ffe0b2 |
724 | } |
725 | |
726 | /* setup the position of a menu */ |
727 | static void |
8902c43b |
728 | setupmenupos(struct Menu *menu) |
f8ffe0b2 |
729 | { |
730 | int width, height; |
a7732690 |
731 | |
8902c43b |
732 | width = menu->w + config.border_pixels * 2; |
733 | height = menu->h + config.border_pixels * 2; |
a7732690 |
734 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
237da982 |
735 | if (pflag || (config.posx > mon.x && mon.x + mon.w - config.posx >= width)) |
05cfe1a0 |
736 | menu->x = config.posx; |
737 | else if (config.posx > width) |
738 | menu->x = config.posx - width; |
8902c43b |
739 | |
237da982 |
740 | if (pflag || (config.posy > mon.y && mon.y + mon.h - config.posy >= height)) |
05cfe1a0 |
741 | menu->y = config.posy; |
b6cf4847 |
742 | else if (mon.y + mon.h > height) |
237da982 |
743 | menu->y = mon.y + mon.h - height; |
a7732690 |
744 | } else { /* else, calculate in respect to parent menu */ |
237da982 |
745 | int parentwidth; |
746 | |
747 | parentwidth = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels; |
748 | |
749 | if (mon.x + mon.w - parentwidth >= width) |
750 | menu->x = parentwidth; |
8902c43b |
751 | else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels) |
752 | menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels; |
a7732690 |
753 | |
8e799bb4 |
754 | if (mon.y + mon.h - (menu->caller->y + menu->parent->y) >= height) |
8455c369 |
755 | menu->y = menu->caller->y + menu->parent->y; |
237da982 |
756 | else if (mon.y + mon.h > height) |
757 | menu->y = mon.y + mon.h - height; |
a7732690 |
758 | } |
f8ffe0b2 |
759 | } |
760 | |
761 | /* recursivelly setup menu configuration and its pixmap */ |
762 | static void |
8902c43b |
763 | setupmenu(struct Menu *menu, XClassHint *classh) |
f8ffe0b2 |
764 | { |
8902c43b |
765 | char *title; |
f8ffe0b2 |
766 | struct Item *item; |
f8ffe0b2 |
767 | XWindowChanges changes; |
768 | XSizeHints sizeh; |
3bec05ea |
769 | XTextProperty wintitle; |
f8ffe0b2 |
770 | |
771 | /* setup size and position of menus */ |
71b4db92 |
772 | setupitems(menu); |
8902c43b |
773 | setupmenupos(menu); |
a7732690 |
774 | |
775 | /* update menu geometry */ |
8902c43b |
776 | changes.border_width = config.border_pixels; |
a7732690 |
777 | changes.height = menu->h; |
f1583285 |
778 | changes.width = menu->w; |
a7732690 |
779 | changes.x = menu->x; |
780 | changes.y = menu->y; |
beae67a6 |
781 | XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes); |
a7732690 |
782 | |
3bec05ea |
783 | /* set window title (used if wflag is on) */ |
784 | if (menu->parent == NULL) { |
8902c43b |
785 | title = classh->res_name; |
3bec05ea |
786 | } else { |
8902c43b |
787 | title = menu->caller->output; |
3bec05ea |
788 | } |
8902c43b |
789 | XStringListToTextProperty(&title, 1, &wintitle); |
3bec05ea |
790 | |
d2435fcd |
791 | /* set window manager hints */ |
c15958bd |
792 | sizeh.flags = USPosition | PMaxSize | PMinSize; |
09c13122 |
793 | sizeh.min_width = sizeh.max_width = menu->w; |
794 | sizeh.min_height = sizeh.max_height = menu->h; |
3bec05ea |
795 | XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh); |
09c13122 |
796 | |
a3a73837 |
797 | /* set WM protocols and ewmh window properties */ |
798 | XSetWMProtocols(dpy, menu->win, &wmdelete, 1); |
8902c43b |
799 | XChangeProperty(dpy, menu->win, netatom[NetWMName], utf8string, 8, |
a3a73837 |
800 | PropModeReplace, (unsigned char *)title, strlen(title)); |
8902c43b |
801 | XChangeProperty(dpy, menu->win, netatom[NetWMWindowType], XA_ATOM, 32, |
802 | PropModeReplace, |
803 | (unsigned char *)&netatom[NetWMWindowTypePopupMenu], 1); |
804 | |
a80fee22 |
805 | /* calculate positions of submenus */ |
a7732690 |
806 | for (item = menu->list; item != NULL; item = item->next) { |
807 | if (item->submenu != NULL) |
8902c43b |
808 | setupmenu(item->submenu, classh); |
a7732690 |
809 | } |
810 | } |
811 | |
85003546 |
812 | /* try to grab pointer, we may have to wait for another process to ungrab */ |
813 | static void |
814 | grabpointer(void) |
815 | { |
816 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; |
817 | int i; |
818 | |
819 | for (i = 0; i < 1000; i++) { |
820 | if (XGrabPointer(dpy, rootwin, True, ButtonPressMask, |
821 | GrabModeAsync, GrabModeAsync, None, |
822 | None, CurrentTime) == GrabSuccess) |
823 | return; |
824 | nanosleep(&ts, NULL); |
825 | } |
826 | errx(1, "cannot grab keyboard"); |
827 | } |
828 | |
829 | /* try to grab keyboard, we may have to wait for another process to ungrab */ |
830 | static void |
831 | grabkeyboard(void) |
832 | { |
833 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; |
834 | int i; |
835 | |
836 | for (i = 0; i < 1000; i++) { |
837 | if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync, |
838 | GrabModeAsync, CurrentTime) == GrabSuccess) |
839 | return; |
840 | nanosleep(&ts, NULL); |
841 | } |
842 | errx(1, "cannot grab keyboard"); |
843 | } |
844 | |
3d853664 |
845 | /* load and scale icon */ |
846 | static Imlib_Image |
847 | loadicon(const char *file) |
848 | { |
849 | Imlib_Image icon; |
850 | int width; |
851 | int height; |
852 | int imgsize; |
853 | |
854 | icon = imlib_load_image(file); |
855 | if (icon == NULL) |
856 | errx(1, "cannot load icon %s", file); |
857 | |
858 | imlib_context_set_image(icon); |
859 | |
860 | width = imlib_image_get_width(); |
861 | height = imlib_image_get_height(); |
862 | imgsize = MIN(width, height); |
863 | |
864 | icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize, |
865 | config.iconsize, |
866 | config.iconsize); |
867 | |
868 | return icon; |
869 | } |
870 | |
871 | /* draw pixmap for the selected and unselected version of each item on menu */ |
872 | static void |
873 | drawitems(struct Menu *menu) |
874 | { |
875 | struct Item *item; |
876 | |
877 | for (item = menu->list; item != NULL; item = item->next) { |
878 | XftDraw *dsel, *dunsel; |
879 | int x, y; |
880 | |
881 | item->unsel = XCreatePixmap(dpy, menu->win, menu->w, item->h, |
882 | DefaultDepth(dpy, screen)); |
883 | |
884 | XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel); |
885 | XFillRectangle(dpy, item->unsel, dc.gc, 0, 0, menu->w, item->h); |
886 | |
887 | if (item->label == NULL) { /* item is separator */ |
888 | y = item->h/2; |
889 | XSetForeground(dpy, dc.gc, dc.separator.pixel); |
890 | XDrawLine(dpy, item->unsel, dc.gc, config.horzpadding, y, |
891 | menu->w - config.horzpadding, y); |
892 | |
893 | item->sel = item->unsel; |
894 | } else { |
895 | |
896 | item->sel = XCreatePixmap(dpy, menu->win, menu->w, item->h, |
897 | DefaultDepth(dpy, screen)); |
898 | XSetForeground(dpy, dc.gc, dc.selected[ColorBG].pixel); |
899 | XFillRectangle(dpy, item->sel, dc.gc, 0, 0, menu->w, item->h); |
900 | |
901 | /* draw text */ |
902 | x = config.horzpadding; |
a2ff706d |
903 | x += (iflag || !menu->hasicon) ? 0 : config.horzpadding + config.iconsize; |
3d853664 |
904 | dsel = XftDrawCreate(dpy, item->sel, visual, colormap); |
905 | dunsel = XftDrawCreate(dpy, item->unsel, visual, colormap); |
906 | XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel); |
907 | drawtext(dsel, &dc.selected[ColorFG], x, 0, item->h, item->label); |
908 | XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel); |
909 | drawtext(dunsel, &dc.normal[ColorFG], x, 0, item->h, item->label); |
910 | XftDrawDestroy(dsel); |
911 | XftDrawDestroy(dunsel); |
912 | |
913 | /* draw triangle */ |
914 | if (item->submenu != NULL) { |
915 | x = menu->w - config.triangle_width - config.horzpadding; |
916 | y = (item->h - config.triangle_height + 1) / 2; |
917 | |
918 | XPoint triangle[] = { |
919 | {x, y}, |
920 | {x + config.triangle_width, y + config.triangle_height/2}, |
921 | {x, y + config.triangle_height}, |
922 | {x, y} |
923 | }; |
924 | |
925 | XSetForeground(dpy, dc.gc, dc.selected[ColorFG].pixel); |
926 | XFillPolygon(dpy, item->sel, dc.gc, triangle, LEN(triangle), |
927 | Convex, CoordModeOrigin); |
928 | XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel); |
929 | XFillPolygon(dpy, item->unsel, dc.gc, triangle, LEN(triangle), |
930 | Convex, CoordModeOrigin); |
931 | } |
932 | |
933 | /* draw icon */ |
934 | if (item->file != NULL && !iflag) { |
935 | item->icon = loadicon(item->file); |
936 | |
3d853664 |
937 | imlib_context_set_image(item->icon); |
237da982 |
938 | imlib_context_set_drawable(item->sel); |
3d853664 |
939 | imlib_render_image_on_drawable(config.horzpadding, config.iconpadding); |
940 | imlib_context_set_drawable(item->unsel); |
941 | imlib_render_image_on_drawable(config.horzpadding, config.iconpadding); |
942 | } |
943 | } |
944 | } |
945 | } |
946 | |
947 | /* copy pixmaps of items of the current menu and of its ancestors into menu window */ |
948 | static void |
949 | drawmenus(struct Menu *currmenu) |
950 | { |
951 | struct Menu *menu; |
952 | struct Item *item; |
953 | |
954 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
955 | if (!menu->drawn) { |
956 | drawitems(menu); |
957 | menu->drawn = 1; |
958 | } |
959 | for (item = menu->list; item != NULL; item = item->next) { |
960 | if (item == menu->selected) |
961 | XCopyArea(dpy, item->sel, menu->win, dc.gc, 0, 0, |
962 | menu->w, item->h, 0, item->y); |
963 | else |
964 | XCopyArea(dpy, item->unsel, menu->win, dc.gc, 0, 0, |
965 | menu->w, item->h, 0, item->y); |
966 | } |
967 | } |
968 | } |
969 | |
257e42cc |
970 | /* umap previous menus and map current menu and its parents */ |
a7732690 |
971 | static void |
257e42cc |
972 | mapmenu(struct Menu *currmenu) |
a7732690 |
973 | { |
257e42cc |
974 | static struct Menu *prevmenu = NULL; |
d8a7caf2 |
975 | struct Menu *menu, *menu_; |
d8a7caf2 |
976 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
977 | unsigned minlevel; /* level of the closest to root menu */ |
978 | unsigned maxlevel; /* level of the closest to root menu */ |
a7732690 |
979 | |
257e42cc |
980 | /* do not remap current menu if it wasn't updated*/ |
981 | if (prevmenu == currmenu) |
a7732690 |
982 | return; |
983 | |
257e42cc |
984 | /* if this is the first time mapping, skip calculations */ |
985 | if (prevmenu == NULL) { |
fd530f3f |
986 | XMapWindow(dpy, currmenu->win); |
cdc4d51f |
987 | prevmenu = currmenu; |
988 | return; |
27443900 |
989 | } |
990 | |
d8a7caf2 |
991 | /* find lowest common ancestor menu */ |
257e42cc |
992 | minlevel = MIN(currmenu->level, prevmenu->level); |
993 | maxlevel = MAX(currmenu->level, prevmenu->level); |
994 | if (currmenu->level == maxlevel) { |
27443900 |
995 | menu = currmenu; |
257e42cc |
996 | menu_ = prevmenu; |
997 | } else { |
998 | menu = prevmenu; |
999 | menu_ = currmenu; |
27443900 |
1000 | } |
1001 | while (menu->level > minlevel) |
1002 | menu = menu->parent; |
1003 | while (menu != menu_) { |
1004 | menu = menu->parent; |
1005 | menu_ = menu_->parent; |
d8a7caf2 |
1006 | } |
27443900 |
1007 | lcamenu = menu; |
d8a7caf2 |
1008 | |
873c080c |
1009 | /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */ |
257e42cc |
1010 | for (menu = prevmenu; menu != lcamenu; menu = menu->parent) { |
8455c369 |
1011 | menu->selected = NULL; |
a7732690 |
1012 | XUnmapWindow(dpy, menu->win); |
1013 | } |
1014 | |
873c080c |
1015 | /* map menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d8a7caf2 |
1016 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
3bec05ea |
1017 | |
1018 | if (wflag) { |
8902c43b |
1019 | setupmenupos(menu); |
3bec05ea |
1020 | XMoveWindow(dpy, menu->win, menu->x, menu->y); |
1021 | } |
1022 | |
a7732690 |
1023 | XMapWindow(dpy, menu->win); |
fd530f3f |
1024 | } |
257e42cc |
1025 | |
257e42cc |
1026 | prevmenu = currmenu; |
fd530f3f |
1027 | } |
1028 | |
8902c43b |
1029 | /* get menu of given window */ |
1030 | static struct Menu * |
1031 | getmenu(struct Menu *currmenu, Window win) |
1032 | { |
1033 | struct Menu *menu; |
1034 | |
1035 | for (menu = currmenu; menu != NULL; menu = menu->parent) |
1036 | if (menu->win == win) |
1037 | return menu; |
1038 | |
1039 | return NULL; |
1040 | } |
1041 | |
1042 | /* get item of given menu and position */ |
1043 | static struct Item * |
1044 | getitem(struct Menu *menu, int y) |
1045 | { |
1046 | struct Item *item; |
1047 | |
1048 | if (menu == NULL) |
1049 | return NULL; |
1050 | |
1051 | for (item = menu->list; item != NULL; item = item->next) |
1052 | if (y >= item->y && y <= item->y + item->h) |
1053 | return item; |
1054 | |
1055 | return NULL; |
1056 | } |
1057 | |
858338d9 |
1058 | /* cycle through the items; non-zero direction is next, zero is prev */ |
1059 | static struct Item * |
257e42cc |
1060 | itemcycle(struct Menu *currmenu, int direction) |
858338d9 |
1061 | { |
1062 | struct Item *item; |
1063 | struct Item *lastitem; |
1064 | |
1065 | item = NULL; |
1066 | |
1067 | if (direction == ITEMNEXT) { |
1068 | if (currmenu->selected == NULL) |
1069 | item = currmenu->list; |
1070 | else if (currmenu->selected->next != NULL) |
1071 | item = currmenu->selected->next; |
1072 | |
1073 | while (item != NULL && item->label == NULL) |
1074 | item = item->next; |
1075 | |
1076 | if (item == NULL) |
1077 | item = currmenu->list; |
1078 | } else { |
1079 | for (lastitem = currmenu->list; |
1080 | lastitem != NULL && lastitem->next != NULL; |
1081 | lastitem = lastitem->next) |
1082 | ; |
1083 | |
1084 | if (currmenu->selected == NULL) |
1085 | item = lastitem; |
1086 | else if (currmenu->selected->prev != NULL) |
1087 | item = currmenu->selected->prev; |
1088 | |
1089 | while (item != NULL && item->label == NULL) |
1090 | item = item->prev; |
1091 | |
1092 | if (item == NULL) |
1093 | item = lastitem; |
1094 | } |
1095 | |
1096 | return item; |
1097 | } |
1098 | |
a7732690 |
1099 | /* run event loop */ |
1100 | static void |
257e42cc |
1101 | run(struct Menu *currmenu) |
a7732690 |
1102 | { |
1103 | struct Menu *menu; |
1104 | struct Item *item; |
1105 | struct Item *previtem = NULL; |
858338d9 |
1106 | KeySym ksym; |
a7732690 |
1107 | XEvent ev; |
1108 | |
257e42cc |
1109 | mapmenu(currmenu); |
fd530f3f |
1110 | |
a7732690 |
1111 | while (!XNextEvent(dpy, &ev)) { |
1112 | switch(ev.type) { |
1113 | case Expose: |
858338d9 |
1114 | if (ev.xexpose.count == 0) |
3d853664 |
1115 | drawmenus(currmenu); |
a7732690 |
1116 | break; |
1117 | case MotionNotify: |
257e42cc |
1118 | menu = getmenu(currmenu, ev.xbutton.window); |
0c1a7864 |
1119 | item = getitem(menu, ev.xbutton.y); |
c6d9174e |
1120 | if (menu == NULL || item == NULL || previtem == item) |
fd530f3f |
1121 | break; |
c6d9174e |
1122 | previtem = item; |
1123 | menu->selected = item; |
1124 | if (item->submenu != NULL) { |
1125 | currmenu = item->submenu; |
1126 | currmenu->selected = NULL; |
1127 | } else { |
1128 | currmenu = menu; |
a7732690 |
1129 | } |
c6d9174e |
1130 | mapmenu(currmenu); |
3d853664 |
1131 | drawmenus(currmenu); |
a7732690 |
1132 | break; |
1133 | case ButtonRelease: |
257e42cc |
1134 | menu = getmenu(currmenu, ev.xbutton.window); |
0c1a7864 |
1135 | item = getitem(menu, ev.xbutton.y); |
fd530f3f |
1136 | if (menu == NULL || item == NULL) |
858338d9 |
1137 | break; |
fd530f3f |
1138 | selectitem: |
1139 | if (item->label == NULL) |
1140 | break; /* ignore separators */ |
1141 | if (item->submenu != NULL) { |
257e42cc |
1142 | currmenu = item->submenu; |
a7732690 |
1143 | } else { |
fd530f3f |
1144 | printf("%s\n", item->output); |
08f16589 |
1145 | return; |
a7732690 |
1146 | } |
257e42cc |
1147 | mapmenu(currmenu); |
fd530f3f |
1148 | currmenu->selected = currmenu->list; |
3d853664 |
1149 | drawmenus(currmenu); |
fd530f3f |
1150 | break; |
858338d9 |
1151 | case ButtonPress: |
257e42cc |
1152 | menu = getmenu(currmenu, ev.xbutton.window); |
0c1a7864 |
1153 | if (menu == NULL) |
858338d9 |
1154 | return; |
1155 | break; |
1156 | case KeyPress: |
1157 | ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0); |
1158 | |
0c1a7864 |
1159 | /* esc closes xmenu when current menu is the root menu */ |
257e42cc |
1160 | if (ksym == XK_Escape && currmenu->parent == NULL) |
858338d9 |
1161 | return; |
1162 | |
1163 | /* Shift-Tab = ISO_Left_Tab */ |
1164 | if (ksym == XK_Tab && (ev.xkey.state & ShiftMask)) |
1165 | ksym = XK_ISO_Left_Tab; |
1166 | |
1167 | /* cycle through menu */ |
1168 | item = NULL; |
1169 | if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) { |
257e42cc |
1170 | item = itemcycle(currmenu, ITEMPREV); |
858338d9 |
1171 | } else if (ksym == XK_Tab || ksym == XK_Down) { |
257e42cc |
1172 | item = itemcycle(currmenu, ITEMNEXT); |
858338d9 |
1173 | } else if ((ksym == XK_Return || ksym == XK_Right) && |
1174 | currmenu->selected != NULL) { |
1175 | item = currmenu->selected; |
1176 | goto selectitem; |
1177 | } else if ((ksym == XK_Escape || ksym == XK_Left) && |
1178 | currmenu->parent != NULL) { |
1179 | item = currmenu->parent->selected; |
257e42cc |
1180 | currmenu = currmenu->parent; |
1181 | mapmenu(currmenu); |
858338d9 |
1182 | } else |
1183 | break; |
1184 | currmenu->selected = item; |
3d853664 |
1185 | drawmenus(currmenu); |
a7732690 |
1186 | break; |
f15fc339 |
1187 | case LeaveNotify: |
fd530f3f |
1188 | previtem = NULL; |
f15fc339 |
1189 | currmenu->selected = NULL; |
3d853664 |
1190 | drawmenus(currmenu); |
f15fc339 |
1191 | break; |
3bec05ea |
1192 | case ConfigureNotify: |
1193 | menu = getmenu(currmenu, ev.xconfigure.window); |
1194 | if (menu == NULL) |
1195 | break; |
1196 | menu->x = ev.xconfigure.x; |
1197 | menu->y = ev.xconfigure.y; |
1198 | break; |
1199 | case ClientMessage: |
8902c43b |
1200 | if ((unsigned long) ev.xclient.data.l[0] != wmdelete) |
1201 | break; |
3bec05ea |
1202 | /* user closed window */ |
1203 | menu = getmenu(currmenu, ev.xclient.window); |
1204 | if (menu->parent == NULL) |
1205 | return; /* closing the root menu closes the program */ |
1206 | currmenu = menu->parent; |
1207 | mapmenu(currmenu); |
1208 | break; |
a7732690 |
1209 | } |
1210 | } |
1211 | } |
1212 | |
fd530f3f |
1213 | /* recursivelly free pixmaps and destroy windows */ |
f15fc339 |
1214 | static void |
8902c43b |
1215 | cleanmenu(struct Menu *menu) |
f15fc339 |
1216 | { |
1217 | struct Item *item; |
ec4ed1ac |
1218 | struct Item *tmp; |
f15fc339 |
1219 | |
ec4ed1ac |
1220 | item = menu->list; |
1221 | while (item != NULL) { |
f15fc339 |
1222 | if (item->submenu != NULL) |
8902c43b |
1223 | cleanmenu(item->submenu); |
ec4ed1ac |
1224 | tmp = item; |
3d853664 |
1225 | if (menu->drawn) { |
1226 | XFreePixmap(dpy, item->unsel); |
1227 | if (tmp->label != NULL) |
1228 | XFreePixmap(dpy, item->sel); |
1229 | } |
15dfafdf |
1230 | if (tmp->label != tmp->output) |
1231 | free(tmp->label); |
6b5123e7 |
1232 | free(tmp->output); |
33376f54 |
1233 | if (tmp->file != NULL) { |
1234 | free(tmp->file); |
3bec05ea |
1235 | if (tmp->icon != NULL) { |
1236 | imlib_context_set_image(tmp->icon); |
33376f54 |
1237 | imlib_free_image(); |
1238 | } |
1239 | } |
1240 | item = item->next; |
ec4ed1ac |
1241 | free(tmp); |
1242 | } |
f15fc339 |
1243 | |
f15fc339 |
1244 | XDestroyWindow(dpy, menu->win); |
ec4ed1ac |
1245 | free(menu); |
f15fc339 |
1246 | } |
1247 | |
8902c43b |
1248 | /* cleanup X and exit */ |
a7732690 |
1249 | static void |
15dfafdf |
1250 | cleanup(void) |
a7732690 |
1251 | { |
7539247b |
1252 | size_t i; |
1253 | |
257e42cc |
1254 | XUngrabPointer(dpy, CurrentTime); |
1255 | XUngrabKeyboard(dpy, CurrentTime); |
1256 | |
dbeb9940 |
1257 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); |
1258 | XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); |
1259 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); |
1260 | XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]); |
fd530f3f |
1261 | XftColorFree(dpy, visual, colormap, &dc.separator); |
1262 | XftColorFree(dpy, visual, colormap, &dc.border); |
dbeb9940 |
1263 | |
7539247b |
1264 | for (i = 0; i < dc.nfonts; i++) |
1265 | XftFontClose(dpy, dc.fonts[i]); |
1266 | |
f15fc339 |
1267 | XFreeGC(dpy, dc.gc); |
a7732690 |
1268 | XCloseDisplay(dpy); |
a7732690 |
1269 | } |
1270 | |
1271 | /* show usage */ |
1272 | static void |
1273 | usage(void) |
1274 | { |
05cfe1a0 |
1275 | (void)fprintf(stderr, "usage: xmenu [-iw] [-p position] [title]\n"); |
a7732690 |
1276 | exit(1); |
1277 | } |