Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
14 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
17 enum { ColFG
, ColBG
, ColLast
};
22 unsigned long norm
[ColLast
];
23 unsigned long sel
[ColLast
];
33 } DC
; /* draw context */
35 typedef struct Item Item
;
37 Item
*next
; /* traverses all items */
38 Item
*left
, *right
; /* traverses items matching current search pattern */
42 /* forward declarations */
43 static void *emalloc(unsigned int size
);
44 static void eprint(const char *errstr
, ...);
45 static char *estrdup(const char *str
);
46 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
47 static unsigned int textw(const char *text
);
48 static unsigned int textnw(const char *text
, unsigned int len
);
49 static void calcoffsets(void);
50 static void drawmenu(void);
51 static Bool
grabkeyboard(void);
52 static unsigned long getcolor(const char *colstr
);
53 static void initfont(const char *fontstr
);
54 static int strido(const char *text
, const char *pattern
);
55 static void match(char *pattern
);
56 static void kpress(XKeyEvent
* e
);
57 static char *readstdin(void);
58 static void usage(void);
65 static char text
[4096];
66 static char *prompt
= NULL
;
70 static unsigned int cmdw
= 0;
71 static unsigned int promptw
= 0;
72 static unsigned int numlockmask
= 0;
73 static Bool running
= True
;
74 static Item
*allitems
= NULL
; /* first of all items */
75 static Item
*item
= NULL
; /* first of pattern matching items */
76 static Item
*sel
= NULL
;
77 static Item
*next
= NULL
;
78 static Item
*prev
= NULL
;
79 static Item
*curr
= NULL
;
86 emalloc(unsigned int size
) {
87 void *res
= malloc(size
);
90 eprint("fatal: could not malloc() %u bytes\n", size
);
95 eprint(const char *errstr
, ...) {
99 vfprintf(stderr
, errstr
, ap
);
105 estrdup(const char *str
) {
106 void *res
= strdup(str
);
109 eprint("fatal: could not malloc() %u bytes\n", strlen(str
));
115 drawtext(const char *text
, unsigned long col
[ColLast
]) {
117 static char buf
[256];
118 unsigned int len
, olen
;
119 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
121 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
122 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
126 olen
= len
= strlen(text
);
127 if(len
>= sizeof buf
)
128 len
= sizeof buf
- 1;
129 memcpy(buf
, text
, len
);
131 h
= dc
.font
.ascent
+ dc
.font
.descent
;
132 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
134 /* shorten text if necessary */
135 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
146 return; /* too long */
147 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
149 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
151 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
155 textw(const char *text
) {
156 return textnw(text
, strlen(text
)) + dc
.font
.height
;
160 textnw(const char *text
, unsigned int len
) {
164 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
167 return XTextWidth(dc
.font
.xfont
, text
, len
);
176 w
= promptw
+ cmdw
+ 2 * SPACE
;
177 for(next
= curr
; next
; next
=next
->right
) {
178 tw
= textw(next
->text
);
185 w
= promptw
+ cmdw
+ 2 * SPACE
;
186 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
187 tw
= textw(prev
->left
->text
);
204 drawtext(NULL
, dc
.norm
);
208 drawtext(prompt
, dc
.sel
);
215 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
219 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
221 /* determine maximum items */
222 for(i
= curr
; i
!= next
; i
=i
->right
) {
223 dc
.w
= textw(i
->text
);
226 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
231 drawtext(next
? ">" : NULL
, dc
.norm
);
233 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
241 for(len
= 1000; len
; len
--) {
242 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
251 getcolor(const char *colstr
) {
252 Colormap cmap
= DefaultColormap(dpy
, screen
);
255 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
256 eprint("error, cannot allocate color '%s'\n", colstr
);
261 initfont(const char *fontstr
) {
262 char *def
, **missing
;
265 if(!fontstr
|| fontstr
[0] == '\0')
266 eprint("error, cannot load font: '%s'\n", fontstr
);
269 XFreeFontSet(dpy
, dc
.font
.set
);
270 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
272 XFreeStringList(missing
);
274 XFontSetExtents
*font_extents
;
275 XFontStruct
**xfonts
;
277 dc
.font
.ascent
= dc
.font
.descent
= 0;
278 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
279 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
280 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
281 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
282 dc
.font
.ascent
= (*xfonts
)->ascent
;
283 if(dc
.font
.descent
< (*xfonts
)->descent
)
284 dc
.font
.descent
= (*xfonts
)->descent
;
290 XFreeFont(dpy
, dc
.font
.xfont
);
291 dc
.font
.xfont
= NULL
;
292 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))) {
293 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
294 eprint("error, cannot load font: '%s'\n", fontstr
);
296 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
297 dc
.font
.descent
= dc
.font
.xfont
->descent
;
299 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
303 strido(const char *text
, const char *pattern
) {
304 for(; *text
&& *pattern
; text
++)
305 if (*text
== *pattern
)
311 match(char *pattern
) {
317 plen
= strlen(pattern
);
320 for(i
= allitems
; i
; i
=i
->next
)
321 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
331 for(i
= allitems
; i
; i
=i
->next
)
332 if(plen
&& strncmp(pattern
, i
->text
, plen
)
333 && strstr(i
->text
, pattern
)) {
343 for(i
= allitems
; i
; i
=i
->next
)
344 if(plen
&& strncmp(pattern
, i
->text
, plen
)
345 && !strstr(i
->text
, pattern
)
346 && strido(i
->text
,pattern
)) {
356 curr
= prev
= next
= sel
= item
;
361 kpress(XKeyEvent
* e
) {
369 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
370 if(IsKeypadKey(ksym
)) {
371 if(ksym
== XK_KP_Enter
) {
373 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
374 ksym
= (ksym
- XK_KP_0
) + XK_0
;
377 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
378 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
379 || IsPrivateKeypadKey(ksym
))
381 /* first check if a control mask is omitted */
382 if(e
->state
& ControlMask
) {
384 default: /* ignore other control sequences */
411 while(i
>= 0 && text
[i
] == ' ')
413 while(i
>= 0 && text
[i
] != ' ')
421 if(CLEANMASK(e
->state
) & Mod1Mask
) {
446 if(num
&& !iscntrl((int) buf
[0])) {
449 strncat(text
, buf
, sizeof text
);
451 strncpy(text
, buf
, sizeof text
);
468 while(sel
&& sel
->right
)
482 if(!(sel
&& sel
->left
))
485 if(sel
->right
== curr
) {
503 if((e
->state
& ShiftMask
) && text
)
504 fprintf(stdout
, "%s", text
);
506 fprintf(stdout
, "%s", sel
->text
);
508 fprintf(stdout
, "%s", text
);
513 if(!(sel
&& sel
->right
))
524 strncpy(text
, sel
->text
, sizeof text
);
533 static char *maxname
= NULL
;
535 unsigned int len
= 0, max
= 0;
539 while(fgets(buf
, sizeof buf
, stdin
)) {
541 if (buf
[len
- 1] == '\n')
548 new = emalloc(sizeof(Item
));
549 new->next
= new->left
= new->right
= NULL
;
563 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
564 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
568 main(int argc
, char *argv
[]) {
572 char *normbg
= NORMBGCOLOR
;
573 char *normfg
= NORMFGCOLOR
;
574 char *selbg
= SELBGCOLOR
;
575 char *selfg
= SELFGCOLOR
;
579 XModifierKeymap
*modmap
;
580 XSetWindowAttributes wa
;
582 /* command line args */
583 for(i
= 1; i
< argc
; i
++)
584 if(!strcmp(argv
[i
], "-b")) {
587 else if(!strcmp(argv
[i
], "-fn")) {
588 if(++i
< argc
) font
= argv
[i
];
590 else if(!strcmp(argv
[i
], "-nb")) {
591 if(++i
< argc
) normbg
= argv
[i
];
593 else if(!strcmp(argv
[i
], "-nf")) {
594 if(++i
< argc
) normfg
= argv
[i
];
596 else if(!strcmp(argv
[i
], "-p")) {
597 if(++i
< argc
) prompt
= argv
[i
];
599 else if(!strcmp(argv
[i
], "-sb")) {
600 if(++i
< argc
) selbg
= argv
[i
];
602 else if(!strcmp(argv
[i
], "-sf")) {
603 if(++i
< argc
) selfg
= argv
[i
];
605 else if(!strcmp(argv
[i
], "-v"))
606 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
609 setlocale(LC_CTYPE
, "");
610 dpy
= XOpenDisplay(0);
612 eprint("dmenu: cannot open display\n");
613 screen
= DefaultScreen(dpy
);
614 root
= RootWindow(dpy
, screen
);
615 if(isatty(STDIN_FILENO
)) {
616 maxname
= readstdin();
617 running
= grabkeyboard();
619 else { /* prevent keypress loss */
620 running
= grabkeyboard();
621 maxname
= readstdin();
623 /* init modifier map */
624 modmap
= XGetModifierMapping(dpy
);
625 for (i
= 0; i
< 8; i
++) {
626 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
627 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
628 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
629 numlockmask
= (1 << i
);
632 XFreeModifiermap(modmap
);
634 dc
.norm
[ColBG
] = getcolor(normbg
);
635 dc
.norm
[ColFG
] = getcolor(normfg
);
636 dc
.sel
[ColBG
] = getcolor(selbg
);
637 dc
.sel
[ColFG
] = getcolor(selfg
);
640 wa
.override_redirect
= 1;
641 wa
.background_pixmap
= ParentRelative
;
642 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
643 mw
= DisplayWidth(dpy
, screen
);
644 mh
= dc
.font
.height
+ 2;
645 win
= XCreateWindow(dpy
, root
, 0,
646 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
647 DefaultDepth(dpy
, screen
), CopyFromParent
,
648 DefaultVisual(dpy
, screen
),
649 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
651 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
652 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
653 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
655 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
657 cmdw
= textw(maxname
);
661 promptw
= textw(prompt
);
666 XMapRaised(dpy
, win
);
670 /* main event loop */
671 while(running
&& !XNextEvent(dpy
, &ev
))
673 default: /* ignore all crap */
679 if(ev
.xexpose
.count
== 0)
686 itm
= allitems
->next
;
687 free(allitems
->text
);
692 XFreeFontSet(dpy
, dc
.font
.set
);
694 XFreeFont(dpy
, dc
.font
.xfont
);
695 XFreePixmap(dpy
, dc
.drawable
);
697 XDestroyWindow(dpy
, win
);
698 XUngrabKeyboard(dpy
, CurrentTime
);