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 */
187 if(!(sel && sel->left))
190 if(sel->right == curr) {
198 strncpy(text, sel->text, sizeof(text));
202 if(!(sel && sel->right))
211 if(e->state & ShiftMask) {
213 fprintf(stdout, "%s", text);
216 fprintf(stdout, "%s", sel->text);
218 fprintf(stdout, "%s", text);
232 } while(i && nitem && prev_nitem == nitem);
237 if(num && !iscntrl((int) buf[0])) {
240 strncat(text, buf, sizeof(text));
242 strncpy(text, buf, sizeof(text));
252 static char *maxname = NULL;
254 unsigned int len = 0, max = 0;
258 while(fgets(buf, sizeof(buf), stdin)) {
260 if (buf[len - 1] == '\n')
268 new = emalloc(sizeof(Item));
269 new->next = new->left = new->right = NULL;
288 main(int argc, char *argv[])
292 XSetWindowAttributes wa;
294 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
295 fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
299 eprint("usage: dmenu [-v]\n");
301 dpy = XOpenDisplay(0);
303 eprint("dmenu: cannot open display\n");
304 screen = DefaultScreen(dpy);
305 root = RootWindow(dpy, screen);
307 maxname = readstdin();
309 /* grab as early as possible, but after reading all items!!! */
310 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
311 GrabModeAsync, CurrentTime) != GrabSuccess)
315 dc.bg = getcolor(BGCOLOR);
316 dc.fg = getcolor(FGCOLOR);
317 dc.border = getcolor(BORDERCOLOR);
320 wa.override_redirect = 1;
321 wa.background_pixmap = ParentRelative;
322 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
325 mw = DisplayWidth(dpy, screen);
326 mh = dc.font.height + 4;
328 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
329 DefaultDepth(dpy, screen), CopyFromParent,
330 DefaultVisual(dpy, screen),
331 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
332 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
335 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
336 dc.gc = XCreateGC(dpy, root, 0, 0);
339 cmdw = textw(maxname);
345 XMapRaised(dpy, win);
349 /* main event loop */
350 while(!done && !XNextEvent(dpy, &ev)) {
356 if(ev.xexpose.count == 0)
364 XUngrabKeyboard(dpy, CurrentTime);
365 XFreePixmap(dpy, dc.drawable);
367 XDestroyWindow(dpy, win);