Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
14 typedef struct Item Item
;
16 Item
*next
; /* traverses all items */
17 Item
*left
, *right
; /* traverses items matching current search pattern */
23 static char text
[4096];
24 static char *prompt
= NULL
;
28 static unsigned int cmdw
= 0;
29 static unsigned int promptw
= 0;
30 static unsigned int numlockmask
= 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
;
47 w
= promptw
+ cmdw
+ 2 * SPACE
;
48 for(next
= curr
; next
; next
=next
->right
) {
49 tw
= textw(next
->text
);
56 w
= promptw
+ cmdw
+ 2 * SPACE
;
57 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
58 tw
= textw(prev
->left
->text
);
75 drawtext(NULL
, dc
.norm
);
79 drawtext(prompt
, dc
.sel
);
86 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
90 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
92 /* determine maximum items */
93 for(i
= curr
; i
!= next
; i
=i
->right
) {
94 dc
.w
= textw(i
->text
);
97 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
102 drawtext(next
? ">" : NULL
, dc
.norm
);
104 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
112 for(len
= 1000; len
; len
--) {
113 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
122 initcolor(const char *colstr
) {
123 Colormap cmap
= DefaultColormap(dpy
, screen
);
126 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
127 eprint("error, cannot allocate color '%s'\n", colstr
);
132 initfont(const char *fontstr
) {
133 char *def
, **missing
;
136 if(!fontstr
|| fontstr
[0] == '\0')
137 eprint("error, cannot load font: '%s'\n", fontstr
);
140 XFreeFontSet(dpy
, dc
.font
.set
);
141 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
143 XFreeStringList(missing
);
145 XFontSetExtents
*font_extents
;
146 XFontStruct
**xfonts
;
148 dc
.font
.ascent
= dc
.font
.descent
= 0;
149 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
150 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
151 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
152 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
153 dc
.font
.ascent
= (*xfonts
)->ascent
;
154 if(dc
.font
.descent
< (*xfonts
)->descent
)
155 dc
.font
.descent
= (*xfonts
)->descent
;
161 XFreeFont(dpy
, dc
.font
.xfont
);
162 dc
.font
.xfont
= NULL
;
163 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
164 eprint("error, cannot load font: '%s'\n", fontstr
);
165 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
166 dc
.font
.descent
= dc
.font
.xfont
->descent
;
168 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
172 match(char *pattern
) {
178 plen
= strlen(pattern
);
181 for(i
= allitems
; i
; i
=i
->next
)
182 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
192 for(i
= allitems
; i
; i
=i
->next
)
193 if(plen
&& strncmp(pattern
, i
->text
, plen
)
194 && strstr(i
->text
, pattern
)) {
204 curr
= prev
= next
= sel
= item
;
209 kpress(XKeyEvent
* e
) {
217 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
218 if(IsKeypadKey(ksym
)) {
219 if(ksym
== XK_KP_Enter
) {
221 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
222 ksym
= (ksym
- XK_KP_0
) + XK_0
;
225 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
226 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
227 || IsPrivateKeypadKey(ksym
))
229 /* first check if a control mask is omitted */
230 if(e
->state
& ControlMask
) {
232 default: /* ignore other control sequences */
259 while(i
>= 0 && text
[i
] == ' ')
261 while(i
>= 0 && text
[i
] != ' ')
269 if(CLEANMASK(e
->state
) & Mod1Mask
) {
294 if(num
&& !iscntrl((int) buf
[0])) {
297 strncat(text
, buf
, sizeof text
);
299 strncpy(text
, buf
, sizeof text
);
316 while(sel
&& sel
->right
)
330 if(!(sel
&& sel
->left
))
333 if(sel
->right
== curr
) {
351 if((e
->state
& ShiftMask
) && text
)
352 fprintf(stdout
, "%s", text
);
354 fprintf(stdout
, "%s", sel
->text
);
356 fprintf(stdout
, "%s", text
);
361 if(!(sel
&& sel
->right
))
372 strncpy(text
, sel
->text
, sizeof text
);
381 static char *maxname
= NULL
;
383 unsigned int len
= 0, max
= 0;
387 while(fgets(buf
, sizeof buf
, stdin
)) {
389 if (buf
[len
- 1] == '\n')
396 new = emalloc(sizeof(Item
));
397 new->next
= new->left
= new->right
= NULL
;
411 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
412 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
422 main(int argc
, char *argv
[]) {
426 char *normbg
= NORMBGCOLOR
;
427 char *normfg
= NORMFGCOLOR
;
428 char *selbg
= SELBGCOLOR
;
429 char *selfg
= SELFGCOLOR
;
433 XModifierKeymap
*modmap
;
434 XSetWindowAttributes wa
;
436 /* command line args */
437 for(i
= 1; i
< argc
; i
++)
438 if(!strcmp(argv
[i
], "-b")) {
441 else if(!strcmp(argv
[i
], "-fn")) {
442 if(++i
< argc
) font
= argv
[i
];
444 else if(!strcmp(argv
[i
], "-nb")) {
445 if(++i
< argc
) normbg
= argv
[i
];
447 else if(!strcmp(argv
[i
], "-nf")) {
448 if(++i
< argc
) normfg
= argv
[i
];
450 else if(!strcmp(argv
[i
], "-p")) {
451 if(++i
< argc
) prompt
= argv
[i
];
453 else if(!strcmp(argv
[i
], "-sb")) {
454 if(++i
< argc
) selbg
= argv
[i
];
456 else if(!strcmp(argv
[i
], "-sf")) {
457 if(++i
< argc
) selfg
= argv
[i
];
459 else if(!strcmp(argv
[i
], "-v"))
460 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
463 setlocale(LC_CTYPE
, "");
464 dpy
= XOpenDisplay(0);
466 eprint("dmenu: cannot open display\n");
467 screen
= DefaultScreen(dpy
);
468 root
= RootWindow(dpy
, screen
);
469 if(isatty(STDIN_FILENO
)) {
470 maxname
= readstdin();
471 running
= grabkeyboard();
473 else { /* prevent keypress loss */
474 running
= grabkeyboard();
475 maxname
= readstdin();
477 /* init modifier map */
478 modmap
= XGetModifierMapping(dpy
);
479 for (i
= 0; i
< 8; i
++) {
480 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
481 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
482 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
483 numlockmask
= (1 << i
);
486 XFreeModifiermap(modmap
);
488 dc
.norm
[ColBG
] = initcolor(normbg
);
489 dc
.norm
[ColFG
] = initcolor(normfg
);
490 dc
.sel
[ColBG
] = initcolor(selbg
);
491 dc
.sel
[ColFG
] = initcolor(selfg
);
494 wa
.override_redirect
= 1;
495 wa
.background_pixmap
= ParentRelative
;
496 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
497 mw
= DisplayWidth(dpy
, screen
);
498 mh
= dc
.font
.height
+ 2;
499 win
= XCreateWindow(dpy
, root
, 0,
500 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
501 DefaultDepth(dpy
, screen
), CopyFromParent
,
502 DefaultVisual(dpy
, screen
),
503 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
505 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
506 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
507 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
509 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
511 cmdw
= textw(maxname
);
515 promptw
= textw(prompt
);
520 XMapRaised(dpy
, win
);
524 /* main event loop */
525 while(running
&& !XNextEvent(dpy
, &ev
))
527 default: /* ignore all crap */
533 if(ev
.xexpose
.count
== 0)
540 itm
= allitems
->next
;
541 free(allitems
->text
);
546 XFreeFontSet(dpy
, dc
.font
.set
);
548 XFreeFont(dpy
, dc
.font
.xfont
);
549 XFreePixmap(dpy
, dc
.drawable
);
551 XDestroyWindow(dpy
, win
);
552 XUngrabKeyboard(dpy
, CurrentTime
);