1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details.
12 #include <sys/select.h>
14 #include <X11/Xutil.h>
15 #include <X11/keysym.h>
17 typedef struct Item Item;
19 Item *next; /* traverses all items */
20 Item *left, *right; /* traverses items matching current search pattern */
26 static char text[4096];
27 static int mx, my, mw, mh;
30 static unsigned int cmdw = 0;
31 static Bool running = True;
32 static Item *allitems = NULL; /* first of all items */
33 static Item *item = NULL; /* first of pattern matching items */
34 static Item *sel = NULL;
35 static Item *next = NULL;
36 static Item *prev = NULL;
37 static Item *curr = NULL;
48 for(next = curr; next; next=next->right) {
49 tw = textw(next->text);
57 for(prev = curr; prev && prev->left; prev=prev->left) {
58 tw = textw(prev->left->text);
75 drawtext(NULL, dc.norm);
79 drawtext(text[0] ? text : NULL, dc.norm);
83 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
85 /* determine maximum items */
86 for(i = curr; i != next; i=i->right) {
87 dc.w = textw(i->text);
90 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
95 drawtext(next ? ">" : NULL, dc.norm);
97 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
102 match(char *pattern) {
108 plen = strlen(pattern);
111 for(i = allitems; i; i=i->next)
112 if(!plen || !strncmp(pattern, i->text, plen)) {
122 for(i = allitems; i; i=i->next)
123 if(plen && strncmp(pattern, i->text, plen)
124 && strstr(i->text, pattern)) {
134 curr = prev = next = sel = item;
139 kpress(XKeyEvent * e) {
147 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
148 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
149 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
150 || IsPrivateKeypadKey(ksym))
152 /* first check if a control mask is omitted */
153 if(e->state & ControlMask) {
155 default: /* ignore other control sequences */
173 if(!(sel && sel->left))
176 if(sel->right == curr) {
184 strncpy(text, sel->text, sizeof text);
188 if(!(sel && sel->right))
197 if((e->state & ShiftMask) && text)
198 fprintf(stdout, "%s", text);
200 fprintf(stdout, "%s", sel->text);
202 fprintf(stdout, "%s", text);
216 } while(i && nitem && prev_nitem == nitem);
221 if(num && !iscntrl((int) buf[0])) {
224 strncat(text, buf, sizeof text);
226 strncpy(text, buf, sizeof text);
235 static char *maxname = NULL;
237 unsigned int len = 0, max = 0;
241 while(fgets(buf, sizeof buf, stdin)) {
243 if (buf[len - 1] == '\n')
250 new = emalloc(sizeof(Item));
251 new->next = new->left = new->right = NULL;
270 main(int argc, char *argv[]) {
273 char *normbg = NORMBGCOLOR;
274 char *normfg = NORMFGCOLOR;
275 char *selbg = SELBGCOLOR;
276 char *selfg = SELFGCOLOR;
279 struct timeval timeout;
282 XSetWindowAttributes wa;
286 /* command line args */
287 for(i = 1; i < argc; i++)
288 if(!strncmp(argv[i], "-font", 6)) {
289 if(++i < argc) font = argv[i];
291 else if(!strncmp(argv[i], "-normbg", 8)) {
292 if(++i < argc) normbg = argv[i];
294 else if(!strncmp(argv[i], "-normfg", 8)) {
295 if(++i < argc) normfg = argv[i];
297 else if(!strncmp(argv[i], "-selbg", 7)) {
298 if(++i < argc) selbg = argv[i];
300 else if(!strncmp(argv[i], "-selfg", 7)) {
301 if(++i < argc) selfg = argv[i];
303 else if(!strncmp(argv[i], "-t", 3)) {
304 if(++i < argc) timeout.tv_sec = atoi(argv[i]);
306 else if(!strncmp(argv[i], "-v", 3)) {
307 fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
311 eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout);
312 dpy = XOpenDisplay(0);
314 eprint("dmenu: cannot open display\n");
315 screen = DefaultScreen(dpy);
316 root = RootWindow(dpy, screen);
318 /* Note, the select() construction allows to grab all keypresses as
319 * early as possible, to not loose them. But if there is no standard
320 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
321 * seconds. This is convenience behavior for rapid typers.
323 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
324 GrabModeAsync, CurrentTime) != GrabSuccess)
327 FD_SET(STDIN_FILENO, &rd);
328 if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
329 goto UninitializedEnd;
330 maxname = readstdin();
332 dc.norm[ColBG] = getcolor(normbg);
333 dc.norm[ColFG] = getcolor(normfg);
334 dc.sel[ColBG] = getcolor(selbg);
335 dc.sel[ColFG] = getcolor(selfg);
338 wa.override_redirect = 1;
339 wa.background_pixmap = ParentRelative;
340 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
342 mw = DisplayWidth(dpy, screen);
343 mh = dc.font.height + 2;
344 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
345 DefaultDepth(dpy, screen), CopyFromParent,
346 DefaultVisual(dpy, screen),
347 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
349 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
350 dc.gc = XCreateGC(dpy, root, 0, 0);
351 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
353 cmdw = textw(maxname);
358 XMapRaised(dpy, win);
362 /* main event loop */
363 while(running && !XNextEvent(dpy, &ev))
365 default: /* ignore all crap */
371 if(ev.xexpose.count == 0)
378 itm = allitems->next;
379 free(allitems->text);
384 XFreeFontSet(dpy, dc.font.set);
386 XFreeFont(dpy, dc.font.xfont);
387 XFreePixmap(dpy, dc.drawable);
389 XDestroyWindow(dpy, win);
391 XUngrabKeyboard(dpy, CurrentTime);