Xinqi Bao's Git
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.
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 */
181 if(e
->state
& Mod1Mask
) {
206 if(num
&& !iscntrl((int) buf
[0])) {
209 strncat(text
, buf
, sizeof text
);
211 strncpy(text
, buf
, sizeof text
);
221 } while(i
&& nitem
&& prev_nitem
== nitem
);
232 while(sel
&& sel
->right
)
246 if(!(sel
&& sel
->left
))
249 if(sel
->right
== curr
) {
267 if((e
->state
& ShiftMask
) && text
)
268 fprintf(stdout
, "%s", text
);
270 fprintf(stdout
, "%s", sel
->text
);
272 fprintf(stdout
, "%s", text
);
277 if(!(sel
&& sel
->right
))
288 strncpy(text
, sel
->text
, sizeof text
);
297 static char *maxname
= NULL
;
299 unsigned int len
= 0, max
= 0;
303 while(fgets(buf
, sizeof buf
, stdin
)) {
305 if (buf
[len
- 1] == '\n')
312 new = emalloc(sizeof(Item
));
313 new->next
= new->left
= new->right
= NULL
;
332 main(int argc
, char *argv
[]) {
336 char *normbg
= NORMBGCOLOR
;
337 char *normfg
= NORMFGCOLOR
;
338 char *selbg
= SELBGCOLOR
;
339 char *selfg
= SELFGCOLOR
;
342 struct timeval timeout
;
345 XSetWindowAttributes wa
;
349 /* command line args */
350 for(i
= 1; i
< argc
; i
++)
351 if(!strncmp(argv
[i
], "-bottom", 8)) {
354 else if(!strncmp(argv
[i
], "-font", 6)) {
355 if(++i
< argc
) font
= argv
[i
];
357 else if(!strncmp(argv
[i
], "-normbg", 8)) {
358 if(++i
< argc
) normbg
= argv
[i
];
360 else if(!strncmp(argv
[i
], "-normfg", 8)) {
361 if(++i
< argc
) normfg
= argv
[i
];
363 else if(!strncmp(argv
[i
], "-selbg", 7)) {
364 if(++i
< argc
) selbg
= argv
[i
];
366 else if(!strncmp(argv
[i
], "-selfg", 7)) {
367 if(++i
< argc
) selfg
= argv
[i
];
369 else if(!strncmp(argv
[i
], "-p", 3)) {
370 if(++i
< argc
) prompt
= argv
[i
];
372 else if(!strncmp(argv
[i
], "-t", 3)) {
373 if(++i
< argc
) timeout
.tv_sec
= atoi(argv
[i
]);
375 else if(!strncmp(argv
[i
], "-v", 3)) {
376 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
380 eprint("usage: dmenu [-bottom] [-font <name>] [-{norm,sel}{bg,fg} <color>]\n"
381 " [-p <prompt>] [-t <seconds>] [-v]\n", stdout
);
382 setlocale(LC_CTYPE
, "");
383 dpy
= XOpenDisplay(0);
385 eprint("dmenu: cannot open display\n");
386 screen
= DefaultScreen(dpy
);
387 root
= RootWindow(dpy
, screen
);
389 /* Note, the select() construction allows to grab all keypresses as
390 * early as possible, to not loose them. But if there is no standard
391 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
392 * seconds. This is convenience behavior for rapid typers.
394 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
395 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
398 FD_SET(STDIN_FILENO
, &rd
);
399 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
400 goto UninitializedEnd
;
401 maxname
= readstdin();
403 dc
.norm
[ColBG
] = getcolor(normbg
);
404 dc
.norm
[ColFG
] = getcolor(normfg
);
405 dc
.sel
[ColBG
] = getcolor(selbg
);
406 dc
.sel
[ColFG
] = getcolor(selfg
);
409 wa
.override_redirect
= 1;
410 wa
.background_pixmap
= ParentRelative
;
411 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
413 mw
= DisplayWidth(dpy
, screen
);
415 mh
= dc
.font
.ascent
+ dc
.font
.descent
+ 3; // match wmii
416 my
= DisplayHeight(dpy
, screen
) - mh
;
419 mh
= dc
.font
.height
+ 2;
420 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
421 DefaultDepth(dpy
, screen
), CopyFromParent
,
422 DefaultVisual(dpy
, screen
),
423 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
425 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
426 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
427 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
429 cmdw
= textw(maxname
);
433 promptw
= textw(prompt
);
438 XMapRaised(dpy
, win
);
442 /* main event loop */
443 while(running
&& !XNextEvent(dpy
, &ev
))
445 default: /* ignore all crap */
451 if(ev
.xexpose
.count
== 0)
458 itm
= allitems
->next
;
459 free(allitems
->text
);
464 XFreeFontSet(dpy
, dc
.font
.set
);
466 XFreeFont(dpy
, dc
.font
.xfont
);
467 XFreePixmap(dpy
, dc
.drawable
);
469 XDestroyWindow(dpy
, win
);
471 XUngrabKeyboard(dpy
, CurrentTime
);