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