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;
20 Item *next; /* traverses all items */
21 Item *left, *right; /* traverses items matching current search pattern */
27 static char *title, text[4096];
28 static int mx, my, mw, mh;
31 static unsigned int cmdw = 0;
32 static unsigned int tw = 0;
33 static unsigned int cw = 0;
34 static Bool done = False;
35 static Item *allitems = NULL; /* first of all items */
36 static Item *item = NULL; /* first of pattern matching items */
37 static Item *sel = NULL;
38 static Item *next = NULL;
39 static Item *prev = NULL;
40 static Item *curr = NULL;
53 for(next = curr; next; next=next->right) {
54 tw = textw(next->text);
63 for(prev = curr; prev && prev->left; prev=prev->left) {
64 tw = textw(prev->left->text);
82 drawtext(NULL, False, False);
85 if(!title || text[0]) {
89 drawtext(text, False, False);
94 drawtext(title, False, False);
100 drawtext((curr && curr->left) ? "<" : NULL, False, False);
103 /* determine maximum items */
104 for(i = curr; i != next; i=i->right) {
106 dc.w = textw(i->text);
109 drawtext(i->text, sel == i, sel == i);
115 drawtext(next ? ">" : NULL, False, False);
117 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
130 if(!title || *pattern)
135 plen = strlen(pattern);
139 for(i = allitems; i; i=i->next)
140 if(!plen || !strncmp(pattern, i->text, plen)) {
150 for(i = allitems; i; i=i->next)
151 if(plen && strncmp(pattern, i->text, plen)
152 && strstr(i->text, pattern)) {
163 curr = prev = next = sel = item;
168 kpress(XKeyEvent * e)
177 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
179 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
180 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
181 || IsPrivateKeypadKey(ksym))
184 /* first check if a control mask is omitted */
185 if(e->state & ControlMask) {
187 default: /* ignore other control sequences */
207 if(!(sel && sel->left))
210 if(sel->right == curr) {
218 strncpy(text, sel->text, sizeof(text));
222 if(!(sel && sel->right))
231 if(e->state & ShiftMask) {
233 fprintf(stdout, "%s", text);
236 fprintf(stdout, "%s", sel->text);
238 fprintf(stdout, "%s", text);
252 } while(i && nitem && prev_nitem == nitem);
257 if(num && !iscntrl((int) buf[0])) {
260 strncat(text, buf, sizeof(text));
262 strncpy(text, buf, sizeof(text));
272 static char *maxname = NULL;
274 unsigned int len = 0, max = 0;
278 while(fgets(buf, sizeof(buf), stdin)) {
280 if (buf[len - 1] == '\n')
288 new = emalloc(sizeof(Item));
289 new->next = new->left = new->right = NULL;
308 main(int argc, char *argv[])
313 XSetWindowAttributes wa;
315 /* command line args */
316 for(i = 1; i < argc; i++) {
317 if (argv[i][0] == '-')
318 switch (argv[i][1]) {
320 fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
329 eprint("usage: dmenu [-v] [-t <title>]\n");
333 eprint("usage: dmenu [-v] [-t <title>]\n");
336 dpy = XOpenDisplay(0);
338 eprint("dmenu: cannot open dpy\n");
339 screen = DefaultScreen(dpy);
340 root = RootWindow(dpy, screen);
342 maxname = readinput();
344 /* grab as early as possible, but after reading all items!!! */
345 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
346 GrabModeAsync, CurrentTime) != GrabSuccess)
350 dc.bg = getcolor(BGCOLOR);
351 dc.fg = getcolor(FGCOLOR);
352 dc.border = getcolor(BORDERCOLOR);
355 wa.override_redirect = 1;
356 wa.background_pixmap = ParentRelative;
357 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
360 mw = DisplayWidth(dpy, screen);
361 mh = dc.font.height + 4;
363 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
364 DefaultDepth(dpy, screen), CopyFromParent,
365 DefaultVisual(dpy, screen),
366 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
367 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
370 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
371 dc.gc = XCreateGC(dpy, root, 0, 0);
384 cmdw = title ? tw : cw;
388 XMapRaised(dpy, win);
392 /* main event loop */
393 while(!done && !XNextEvent(dpy, &ev)) {
399 if(ev.xexpose.count == 0)
407 XUngrabKeyboard(dpy, CurrentTime);
408 XFreePixmap(dpy, dc.drawable);
410 XDestroyWindow(dpy, win);