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 <sys/select.h>
16 #include <X11/cursorfont.h>
17 #include <X11/Xutil.h>
18 #include <X11/keysym.h>
20 typedef struct Item Item;
22 Item *next; /* traverses all items */
23 Item *left, *right; /* traverses items matching current search pattern */
29 static char text[4096];
30 static int mx, my, mw, mh;
33 static unsigned int cmdw = 0;
34 static Bool running = True;
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;
52 for(next = curr; next; next=next->right) {
53 tw = textw(next->text);
62 for(prev = curr; prev && prev->left; prev=prev->left) {
63 tw = textw(prev->left->text);
80 drawtext(NULL, dc.norm);
85 drawtext(text[0] ? text : NULL, dc.norm);
90 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
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) ? dc.sel : dc.norm);
104 drawtext(next ? ">" : NULL, dc.norm);
106 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
111 match(char *pattern) {
118 plen = strlen(pattern);
122 for(i = allitems; i; i=i->next)
123 if(plen ? !strncmp(pattern, i->text, plen) :
124 strncmp(pattern, i->text, plen) && strstr(i->text, pattern)) {
135 curr = prev = next = sel = item;
140 kpress(XKeyEvent * e) {
148 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
150 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
151 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
152 || IsPrivateKeypadKey(ksym))
155 /* first check if a control mask is omitted */
156 if(e->state & ControlMask) {
158 default: /* ignore other control sequences */
176 if(!(sel && sel->left))
179 if(sel->right == curr) {
187 strncpy(text, sel->text, sizeof(text));
191 if(!(sel && sel->right))
200 if((e->state & ShiftMask) && text)
201 fprintf(stdout, "%s", text);
203 fprintf(stdout, "%s", sel->text);
205 fprintf(stdout, "%s", text);
219 } while(i && nitem && prev_nitem == nitem);
224 if(num && !iscntrl((int) buf[0])) {
227 strncat(text, buf, sizeof(text));
229 strncpy(text, buf, sizeof(text));
238 static char *maxname = NULL;
240 unsigned int len = 0, max = 0;
244 while(fgets(buf, sizeof(buf), stdin)) {
246 if (buf[len - 1] == '\n')
254 new = emalloc(sizeof(Item));
255 new->next = new->left = new->right = NULL;
274 main(int argc, char *argv[]) {
277 struct timeval timeout;
280 XSetWindowAttributes wa;
282 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
283 fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
287 eprint("usage: dmenu [-v]\n");
289 dpy = XOpenDisplay(0);
291 eprint("dmenu: cannot open display\n");
292 screen = DefaultScreen(dpy);
293 root = RootWindow(dpy, screen);
295 /* Note, the select() construction allows to grab all keypresses as
296 * early as possible, to not loose them. But if there is no standard
297 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
298 * seconds. This is convenience behavior for rapid typers.
300 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
301 GrabModeAsync, CurrentTime) != GrabSuccess)
305 timeout.tv_sec = STDIN_TIMEOUT;
307 FD_SET(STDIN_FILENO, &rd);
308 if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
309 goto UninitializedEnd;
310 maxname = readstdin();
313 dc.sel[ColBG] = getcolor(SELBGCOLOR);
314 dc.sel[ColFG] = getcolor(SELFGCOLOR);
315 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
316 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
319 wa.override_redirect = 1;
320 wa.background_pixmap = ParentRelative;
321 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
324 mw = DisplayWidth(dpy, screen);
325 mh = dc.font.height + 2;
327 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
328 DefaultDepth(dpy, screen), CopyFromParent,
329 DefaultVisual(dpy, screen),
330 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
331 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
334 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
335 dc.gc = XCreateGC(dpy, root, 0, 0);
336 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
339 cmdw = textw(maxname);
345 XMapRaised(dpy, win);
349 /* main event loop */
350 while(running && !XNextEvent(dpy, &ev)) {
352 default: /* ignore all crap */
358 if(ev.xexpose.count == 0)
366 free(allitems->text);
371 XFreeFontSet(dpy, dc.font.set);
373 XFreeFont(dpy, dc.font.xfont);
374 XFreePixmap(dpy, dc.drawable);
376 XDestroyWindow(dpy, win);
378 XUngrabKeyboard(dpy, CurrentTime);