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 text[4096];
28 static int mx, my, mw, mh;
31 static unsigned int cmdw = 0;
32 static Bool done = False;
33 static Item *allitems = NULL; /* first of all items */
34 static Item *item = NULL; /* first of pattern matching items */
35 static Item *sel = NULL;
36 static Item *next = NULL;
37 static Item *prev = NULL;
38 static Item *curr = NULL;
51 for(next = curr; next; next=next->right) {
52 tw = textw(next->text);
61 for(prev = curr; prev && prev->left; prev=prev->left) {
62 tw = textw(prev->left->text);
80 drawtext(NULL, False, False);
85 drawtext(text[0] ? text : NULL, False, False);
90 drawtext((curr && curr->left) ? "<" : NULL, False, False);
93 /* determine maximum items */
94 for(i = curr; i != next; i=i->right) {
95 dc.w = textw(i->text);
98 drawtext(i->text, sel == i, sel == i);
104 drawtext(next ? ">" : NULL, False, False);
106 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
119 plen = strlen(pattern);
123 for(i = allitems; i; i=i->next)
124 if(!plen || !strncmp(pattern, i->text, plen)) {
134 for(i = allitems; i; i=i->next)
135 if(plen && strncmp(pattern, i->text, plen)
136 && strstr(i->text, pattern)) {
147 curr = prev = next = sel = item;
152 kpress(XKeyEvent * e)
161 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
163 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
164 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
165 || IsPrivateKeypadKey(ksym))
168 /* first check if a control mask is omitted */
169 if(e->state & ControlMask) {
171 default: /* ignore other control sequences */
191 if(!(sel && sel->left))
194 if(sel->right == curr) {
202 strncpy(text, sel->text, sizeof(text));
206 if(!(sel && sel->right))
215 if(e->state & ShiftMask) {
217 fprintf(stdout, "%s", text);
220 fprintf(stdout, "%s", sel->text);
222 fprintf(stdout, "%s", text);
236 } while(i && nitem && prev_nitem == nitem);
241 if(num && !iscntrl((int) buf[0])) {
244 strncat(text, buf, sizeof(text));
246 strncpy(text, buf, sizeof(text));
256 static char *maxname = NULL;
258 unsigned int len = 0, max = 0;
262 while(fgets(buf, sizeof(buf), stdin)) {
264 if (buf[len - 1] == '\n')
272 new = emalloc(sizeof(Item));
273 new->next = new->left = new->right = NULL;
292 main(int argc, char *argv[])
296 XSetWindowAttributes wa;
298 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
299 fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
303 eprint("usage: dmenu [-v]\n");
305 dpy = XOpenDisplay(0);
307 eprint("dmenu: cannot open display\n");
308 screen = DefaultScreen(dpy);
309 root = RootWindow(dpy, screen);
311 maxname = readstdin();
313 /* grab as early as possible, but after reading all items!!! */
314 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
315 GrabModeAsync, CurrentTime) != GrabSuccess)
319 dc.bg = getcolor(BGCOLOR);
320 dc.fg = getcolor(FGCOLOR);
321 dc.border = getcolor(BORDERCOLOR);
324 wa.override_redirect = 1;
325 wa.background_pixmap = ParentRelative;
326 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
329 mw = DisplayWidth(dpy, screen);
330 mh = dc.font.height + 4;
332 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
333 DefaultDepth(dpy, screen), CopyFromParent,
334 DefaultVisual(dpy, screen),
335 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
336 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
339 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
340 dc.gc = XCreateGC(dpy, root, 0, 0);
343 cmdw = textw(maxname);
349 XMapRaised(dpy, win);
353 /* main event loop */
354 while(!done && !XNextEvent(dpy, &ev)) {
360 if(ev.xexpose.count == 0)
368 XUngrabKeyboard(dpy, CurrentTime);
369 XFreePixmap(dpy, dc.drawable);
371 XDestroyWindow(dpy, win);