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 */
43 /* forward declarations */
44 Item
*appenditem(Item
*i
, Item
*last
);
45 void calcoffsets(void);
48 void drawtext(const char *text
, unsigned long col
[ColLast
]);
49 void *emalloc(unsigned int size
);
50 void eprint(const char *errstr
, ...);
51 char *estrdup(const char *str
);
52 unsigned long getcolor(const char *colstr
);
53 Bool
grabkeyboard(void);
54 void initfont(const char *fontstr
);
55 void kpress(XKeyEvent
* e
);
56 void match(char *pattern
);
59 void setup(Bool bottom
);
60 int strcaseido(const char *text
, const char *pattern
);
61 unsigned int textnw(const char *text
, unsigned int len
);
62 unsigned int textw(const char *text
);
69 char *normbg
= NORMBGCOLOR
;
70 char *normfg
= NORMFGCOLOR
;
72 char *selbg
= SELBGCOLOR
;
73 char *selfg
= SELFGCOLOR
;
77 unsigned int cmdw
= 0;
79 unsigned int promptw
= 0;
80 unsigned int nitem
= 0;
81 unsigned int numlockmask
= 0;
82 Bool idomatch
= False
;
86 Item
*allitems
= NULL
; /* first of all items */
87 Item
*item
= NULL
; /* first of pattern matching items */
95 appenditem(Item
*i
, Item
*last
) {
114 w
= promptw
+ cmdw
+ 2 * SPACE
;
115 for(next
= curr
; next
; next
=next
->right
) {
116 tw
= textw(next
->text
);
123 w
= promptw
+ cmdw
+ 2 * SPACE
;
124 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
125 tw
= textw(prev
->left
->text
);
139 itm
= allitems
->next
;
140 free(allitems
->text
);
145 XFreeFontSet(dpy
, dc
.font
.set
);
147 XFreeFont(dpy
, dc
.font
.xfont
);
148 XFreePixmap(dpy
, dc
.drawable
);
150 XDestroyWindow(dpy
, win
);
151 XUngrabKeyboard(dpy
, CurrentTime
);
162 drawtext(NULL
, dc
.norm
);
166 drawtext(prompt
, dc
.sel
);
173 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
177 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
179 /* determine maximum items */
180 for(i
= curr
; i
!= next
; i
=i
->right
) {
181 dc
.w
= textw(i
->text
);
184 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
189 drawtext(next
? ">" : NULL
, dc
.norm
);
191 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
196 drawtext(const char *text
, unsigned long col
[ColLast
]) {
198 static char buf
[256];
199 unsigned int len
, olen
;
200 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
202 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
203 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
207 olen
= len
= strlen(text
);
208 if(len
>= sizeof buf
)
209 len
= sizeof buf
- 1;
210 memcpy(buf
, text
, len
);
212 h
= dc
.font
.ascent
+ dc
.font
.descent
;
213 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
215 /* shorten text if necessary */
216 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
227 return; /* too long */
228 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
230 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
232 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
236 emalloc(unsigned int size
) {
237 void *res
= malloc(size
);
240 eprint("fatal: could not malloc() %u bytes\n", size
);
245 eprint(const char *errstr
, ...) {
248 va_start(ap
, errstr
);
249 vfprintf(stderr
, errstr
, ap
);
255 estrdup(const char *str
) {
256 void *res
= strdup(str
);
259 eprint("fatal: could not malloc() %u bytes\n", strlen(str
));
264 getcolor(const char *colstr
) {
265 Colormap cmap
= DefaultColormap(dpy
, screen
);
268 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
269 eprint("error, cannot allocate color '%s'\n", colstr
);
277 for(len
= 1000; len
; len
--) {
278 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
287 initfont(const char *fontstr
) {
288 char *def
, **missing
;
291 if(!fontstr
|| fontstr
[0] == '\0')
292 eprint("error, cannot load font: '%s'\n", fontstr
);
295 XFreeFontSet(dpy
, dc
.font
.set
);
296 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
298 XFreeStringList(missing
);
300 XFontSetExtents
*font_extents
;
301 XFontStruct
**xfonts
;
303 dc
.font
.ascent
= dc
.font
.descent
= 0;
304 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
305 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
306 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
307 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
308 dc
.font
.ascent
= (*xfonts
)->ascent
;
309 if(dc
.font
.descent
< (*xfonts
)->descent
)
310 dc
.font
.descent
= (*xfonts
)->descent
;
316 XFreeFont(dpy
, dc
.font
.xfont
);
317 dc
.font
.xfont
= NULL
;
318 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
319 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
320 eprint("error, cannot load font: '%s'\n", fontstr
);
321 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
322 dc
.font
.descent
= dc
.font
.xfont
->descent
;
324 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
328 kpress(XKeyEvent
* e
) {
336 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
337 if(IsKeypadKey(ksym
)) {
338 if(ksym
== XK_KP_Enter
) {
340 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
341 ksym
= (ksym
- XK_KP_0
) + XK_0
;
344 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
345 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
346 || IsPrivateKeypadKey(ksym
))
348 /* first check if a control mask is omitted */
349 if(e
->state
& ControlMask
) {
351 default: /* ignore other control sequences */
378 while(i
>= 0 && text
[i
] == ' ')
380 while(i
>= 0 && text
[i
] != ' ')
388 if(CLEANMASK(e
->state
) & Mod1Mask
) {
413 if(num
&& !iscntrl((int) buf
[0])) {
416 strncat(text
, buf
, sizeof text
);
418 strncpy(text
, buf
, sizeof text
);
435 while(sel
&& sel
->right
)
449 if(!(sel
&& sel
->left
))
452 if(sel
->right
== curr
) {
470 if((e
->state
& ShiftMask
) && text
)
471 fprintf(stdout
, "%s", text
);
473 fprintf(stdout
, "%s", sel
->text
);
475 fprintf(stdout
, "%s", text
);
480 if(!(sel
&& sel
->right
))
491 strncpy(text
, sel
->text
, sizeof text
);
499 match(char *pattern
) {
505 plen
= strlen(pattern
);
508 for(i
= allitems
; i
; i
=i
->next
)
511 for(i
= allitems
; i
; i
= i
->next
)
512 if(!i
->matched
&& !strncasecmp(pattern
, i
->text
, plen
))
513 j
= appenditem(i
, j
);
515 for (i
= allitems
; i
; i
= i
->next
)
516 if(!i
->matched
&& strcasestr(i
->text
, pattern
))
517 j
= appenditem(i
, j
);
520 for (i
= allitems
; i
; i
= i
->next
)
521 if(!i
->matched
&& strcaseido(i
->text
, pattern
))
522 j
= appenditem(i
, j
);
524 curr
= prev
= next
= sel
= item
;
531 unsigned int len
= 0, max
= 0;
535 while(fgets(buf
, sizeof buf
, stdin
)) {
537 if (buf
[len
- 1] == '\n')
544 new = emalloc(sizeof(Item
));
545 new->next
= new->left
= new->right
= NULL
;
559 /* main event loop */
560 while(running
&& !XNextEvent(dpy
, &ev
))
562 default: /* ignore all crap */
568 if(ev
.xexpose
.count
== 0)
577 XModifierKeymap
*modmap
;
578 XSetWindowAttributes wa
;
580 /* init modifier map */
581 modmap
= XGetModifierMapping(dpy
);
582 for(i
= 0; i
< 8; i
++)
583 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
584 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
585 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
586 numlockmask
= (1 << i
);
588 XFreeModifiermap(modmap
);
591 dc
.norm
[ColBG
] = getcolor(normbg
);
592 dc
.norm
[ColFG
] = getcolor(normfg
);
593 dc
.sel
[ColBG
] = getcolor(selbg
);
594 dc
.sel
[ColFG
] = getcolor(selfg
);
598 wa
.override_redirect
= 1;
599 wa
.background_pixmap
= ParentRelative
;
600 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
601 mw
= DisplayWidth(dpy
, screen
);
602 mh
= dc
.font
.height
+ 2;
603 win
= XCreateWindow(dpy
, root
, 0,
604 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
605 DefaultDepth(dpy
, screen
), CopyFromParent
,
606 DefaultVisual(dpy
, screen
),
607 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
610 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
611 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
612 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
614 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
616 cmdw
= textw(maxname
);
620 promptw
= textw(prompt
);
625 XMapRaised(dpy
, win
);
629 strcaseido(const char *text
, const char *pattern
) {
630 for(; *text
&& *pattern
; text
++)
631 if (tolower(*text
) == tolower(*pattern
))
637 textnw(const char *text
, unsigned int len
) {
641 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
644 return XTextWidth(dc
.font
.xfont
, text
, len
);
648 textw(const char *text
) {
649 return textnw(text
, strlen(text
)) + dc
.font
.height
;
653 main(int argc
, char *argv
[]) {
657 /* command line args */
658 for(i
= 1; i
< argc
; i
++)
659 if(!strcmp(argv
[i
], "-b")) {
662 else if(!strcmp(argv
[i
], "-i"))
664 else if(!strcmp(argv
[i
], "-fn")) {
665 if(++i
< argc
) font
= argv
[i
];
667 else if(!strcmp(argv
[i
], "-nb")) {
668 if(++i
< argc
) normbg
= argv
[i
];
670 else if(!strcmp(argv
[i
], "-nf")) {
671 if(++i
< argc
) normfg
= argv
[i
];
673 else if(!strcmp(argv
[i
], "-p")) {
674 if(++i
< argc
) prompt
= argv
[i
];
676 else if(!strcmp(argv
[i
], "-sb")) {
677 if(++i
< argc
) selbg
= argv
[i
];
679 else if(!strcmp(argv
[i
], "-sf")) {
680 if(++i
< argc
) selfg
= argv
[i
];
682 else if(!strcmp(argv
[i
], "-v"))
683 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, Michał Janeczek\n");
685 eprint("usage: dmenu [-b] [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
686 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
687 setlocale(LC_CTYPE
, "");
688 dpy
= XOpenDisplay(0);
690 eprint("dmenu: cannot open display\n");
691 screen
= DefaultScreen(dpy
);
692 root
= RootWindow(dpy
, screen
);
694 if(isatty(STDIN_FILENO
)) {
696 running
= grabkeyboard();
698 else { /* prevent keypress loss */
699 running
= grabkeyboard();