2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
4 * See LICENSE file for license details.
14 #include <X11/cursorfont.h>
15 #include <X11/Xutil.h>
16 #include <X11/keysym.h>
18 typedef struct Item Item;
21 Item *next; /* traverses all items */
22 Item *left, *right; /* traverses items matching current search pattern */
29 static Bool done = False;
31 static Item *allitem = NULL; /* first of all items */
32 static Item *item = NULL; /* first of pattern matching items */
33 static Item *sel = NULL;
34 static Item *nextoff = NULL;
35 static Item *prevoff = NULL;
36 static Item *curroff = NULL;
38 static int screen, mx, my, mw, mh;
39 static char *title = NULL;
40 static char text[4096];
43 static unsigned int cmdw = 0;
44 static unsigned int tw = 0;
45 static unsigned int cw = 0;
46 static const int seek = 30; /* 30px */
48 static Brush brush = {0};
50 static void draw_menu();
51 static void kpress(XKeyEvent * e);
53 static char version[] = "dmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
58 unsigned int tw, w = cmdw + 2 * seek;
63 for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
64 tw = textw(&brush.font, nextoff->text);
67 w += tw + brush.font.height;
73 for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
74 tw = textw(&brush.font, prevoff->left->text);
77 w += tw + brush.font.height;
84 update_items(char *pattern)
86 unsigned int plen = strlen(pattern);
92 if(!title || *pattern)
100 for(i = allitem; i; i=i->next)
101 if(!plen || !strncmp(pattern, i->text, plen)) {
111 for(i = allitem; i; i=i->next)
112 if(plen && strncmp(pattern, i->text, plen)
113 && strstr(i->text, pattern)) {
124 curroff = prevoff = nextoff = sel = item;
129 /* creates brush structs for brush mode drawing */
139 draw(dpy, &brush, False, 0);
142 if(!title || text[0]) {
146 draw(dpy, &brush, False, text);
151 draw(dpy, &brush, False, title);
157 draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
160 /* determine maximum items */
161 for(i = curroff; i != nextoff; i=i->right) {
162 brush.border = False;
163 brush.w = textw(&brush.font, i->text);
166 brush.w += brush.font.height;
168 swap((void **)&brush.fg, (void **)&brush.bg);
169 draw(dpy, &brush, True, i->text);
170 swap((void **)&brush.fg, (void **)&brush.bg);
173 draw(dpy, &brush, False, i->text);
179 draw(dpy, &brush, False, nextoff ? ">" : 0);
181 XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
186 kpress(XKeyEvent * e)
191 unsigned int i, len = strlen(text);
194 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
196 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
197 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
198 || IsPrivateKeypadKey(ksym))
201 /* first check if a control mask is omitted */
202 if(e->state & ControlMask) {
204 default: /* ignore other control sequences */
224 if(!(sel && sel->left))
227 if(sel->right == curroff) {
235 strncpy(text, sel->text, sizeof(text));
239 if(!(sel && sel->right))
248 if(e->state & ShiftMask) {
250 fprintf(stdout, "%s", text);
253 fprintf(stdout, "%s", sel->text);
255 fprintf(stdout, "%s", text);
269 } while(i && nitem && prev_nitem == nitem);
274 if(num && !iscntrl((int) buf[0])) {
277 strncat(text, buf, sizeof(text));
279 strncpy(text, buf, sizeof(text));
289 static char *maxname = NULL;
291 unsigned int len = 0, max = 0;
295 while(fgets(buf, sizeof(buf), stdin)) {
297 if (buf[len - 1] == '\n')
305 new = emalloc(sizeof(Item));
306 new->next = new->left = new->right = NULL;
319 main(int argc, char *argv[])
322 XSetWindowAttributes wa;
326 /* command line args */
327 for(i = 1; i < argc; i++) {
328 if (argv[i][0] == '-')
329 switch (argv[i][1]) {
331 fprintf(stdout, "%s", version);
340 eprint("usage: dmenu [-v] [-t <title>]\n");
344 eprint("usage: dmenu [-v] [-t <title>]\n");
347 dpy = XOpenDisplay(0);
349 eprint("dmenu: cannot open dpy\n");
350 screen = DefaultScreen(dpy);
351 root = RootWindow(dpy, screen);
353 maxname = read_allitems();
355 /* grab as early as possible, but after reading all items!!! */
356 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
357 GrabModeAsync, CurrentTime) != GrabSuccess)
361 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
362 loadfont(dpy, &brush.font, FONT);
364 wa.override_redirect = 1;
365 wa.background_pixmap = ParentRelative;
366 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
369 mw = DisplayWidth(dpy, screen);
370 mh = texth(&brush.font);
372 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
373 DefaultDepth(dpy, screen), CopyFromParent,
374 DefaultVisual(dpy, screen),
375 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
376 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
380 brush.gc = XCreateGC(dpy, root, 0, 0);
381 brush.drawable = XCreatePixmap(dpy, win, mw, mh,
382 DefaultDepth(dpy, screen));
386 cw = textw(&brush.font, maxname) + brush.font.height;
391 tw = textw(&brush.font, title) + brush.font.height;
396 cmdw = title ? tw : cw;
400 XMapRaised(dpy, win);
404 /* main event loop */
405 while(!done && !XNextEvent(dpy, &ev)) {
411 if(ev.xexpose.count == 0)
419 XUngrabKeyboard(dpy, CurrentTime);
420 XFreePixmap(dpy, brush.drawable);
421 XFreeGC(dpy, brush.gc);
422 XDestroyWindow(dpy, win);