Xinqi Bao's Git
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details.
13 #include <sys/select.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 char *prompt
= NULL
;
29 static int mx
, my
, mw
, mh
;
32 static unsigned int cmdw
= 0;
33 static unsigned int promptw
= 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
;
50 w
= promptw
+ cmdw
+ 2 * SPACE
;
51 for(next
= curr
; next
; next
=next
->right
) {
52 tw
= textw(next
->text
);
59 w
= promptw
+ cmdw
+ 2 * SPACE
;
60 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
61 tw
= textw(prev
->left
->text
);
78 drawtext(NULL
, dc
.norm
);
82 drawtext(prompt
, dc
.sel
);
89 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
93 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
95 /* determine maximum items */
96 for(i
= curr
; i
!= next
; i
=i
->right
) {
97 dc
.w
= textw(i
->text
);
100 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
105 drawtext(next
? ">" : NULL
, dc
.norm
);
107 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
112 match(char *pattern
) {
118 plen
= strlen(pattern
);
121 for(i
= allitems
; i
; i
=i
->next
)
122 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
132 for(i
= allitems
; i
; i
=i
->next
)
133 if(plen
&& strncmp(pattern
, i
->text
, plen
)
134 && strstr(i
->text
, pattern
)) {
144 curr
= prev
= next
= sel
= item
;
149 kpress(XKeyEvent
* e
) {
157 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
158 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
159 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
160 || IsPrivateKeypadKey(ksym
))
162 /* first check if a control mask is omitted */
163 if(e
->state
& ControlMask
) {
165 default: /* ignore other control sequences */
182 if(e
->state
& Mod1Mask
) {
207 if(num
&& !iscntrl((int) buf
[0])) {
210 strncat(text
, buf
, sizeof text
);
212 strncpy(text
, buf
, sizeof text
);
222 } while(i
&& nitem
&& prev_nitem
== nitem
);
233 while(sel
&& sel
->right
)
247 if(!(sel
&& sel
->left
))
250 if(sel
->right
== curr
) {
268 if((e
->state
& ShiftMask
) && text
)
269 fprintf(stdout
, "%s", text
);
271 fprintf(stdout
, "%s", sel
->text
);
273 fprintf(stdout
, "%s", text
);
278 if(!(sel
&& sel
->right
))
289 strncpy(text
, sel
->text
, sizeof text
);
298 static char *maxname
= NULL
;
300 unsigned int len
= 0, max
= 0;
304 while(fgets(buf
, sizeof buf
, stdin
)) {
306 if (buf
[len
- 1] == '\n')
313 new = emalloc(sizeof(Item
));
314 new->next
= new->left
= new->right
= NULL
;
333 main(int argc
, char *argv
[]) {
337 char *normbg
= NORMBGCOLOR
;
338 char *normfg
= NORMFGCOLOR
;
339 char *selbg
= SELBGCOLOR
;
340 char *selfg
= SELFGCOLOR
;
343 struct timeval timeout
;
346 XSetWindowAttributes wa
;
350 /* command line args */
351 for(i
= 1; i
< argc
; i
++)
352 if(!strncmp(argv
[i
], "-b", 3)) {
355 else if(!strncmp(argv
[i
], "-fn", 4)) {
356 if(++i
< argc
) font
= argv
[i
];
358 else if(!strncmp(argv
[i
], "-nb", 4)) {
359 if(++i
< argc
) normbg
= argv
[i
];
361 else if(!strncmp(argv
[i
], "-nf", 4)) {
362 if(++i
< argc
) normfg
= argv
[i
];
364 else if(!strncmp(argv
[i
], "-p", 3)) {
365 if(++i
< argc
) prompt
= argv
[i
];
367 else if(!strncmp(argv
[i
], "-sb", 4)) {
368 if(++i
< argc
) selbg
= argv
[i
];
370 else if(!strncmp(argv
[i
], "-sf", 4)) {
371 if(++i
< argc
) selfg
= argv
[i
];
373 else if(!strncmp(argv
[i
], "-t", 3)) {
374 if(++i
< argc
) timeout
.tv_sec
= atoi(argv
[i
]);
376 else if(!strncmp(argv
[i
], "-v", 3)) {
377 fputs("dmenu-"VERSION
", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout
);
381 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>] [-p <prompt>]\n"
382 " [-sb <color>] [-sf <color>] [-t <seconds>] [-v]\n", stdout
);
383 setlocale(LC_CTYPE
, "");
384 dpy
= XOpenDisplay(0);
386 eprint("dmenu: cannot open display\n");
387 screen
= DefaultScreen(dpy
);
388 root
= RootWindow(dpy
, screen
);
390 /* Note, the select() construction allows to grab all keypresses as
391 * early as possible, to not loose them. But if there is no standard
392 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
393 * seconds. This is convenience behavior for rapid typers.
395 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
396 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
399 FD_SET(STDIN_FILENO
, &rd
);
400 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
401 goto UninitializedEnd
;
402 maxname
= readstdin();
404 dc
.norm
[ColBG
] = getcolor(normbg
);
405 dc
.norm
[ColFG
] = getcolor(normfg
);
406 dc
.sel
[ColBG
] = getcolor(selbg
);
407 dc
.sel
[ColFG
] = getcolor(selfg
);
410 wa
.override_redirect
= 1;
411 wa
.background_pixmap
= ParentRelative
;
412 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
414 mw
= DisplayWidth(dpy
, screen
);
415 mh
= dc
.font
.height
+ 2;
417 my
+= DisplayHeight(dpy
, screen
) - mh
;
418 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
419 DefaultDepth(dpy
, screen
), CopyFromParent
,
420 DefaultVisual(dpy
, screen
),
421 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
423 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
424 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
425 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
427 cmdw
= textw(maxname
);
431 promptw
= textw(prompt
);
436 XMapRaised(dpy
, win
);
440 /* main event loop */
441 while(running
&& !XNextEvent(dpy
, &ev
))
443 default: /* ignore all crap */
449 if(ev
.xexpose
.count
== 0)
456 itm
= allitems
->next
;
457 free(allitems
->text
);
462 XFreeFontSet(dpy
, dc
.font
.set
);
464 XFreeFont(dpy
, dc
.font
.xfont
);
465 XFreePixmap(dpy
, dc
.drawable
);
467 XDestroyWindow(dpy
, win
);
469 XUngrabKeyboard(dpy
, CurrentTime
);