Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
11 #include <X11/Xutil.h>
12 #include <X11/keysym.h>
15 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
18 enum { ColFG
, ColBG
, ColLast
};
23 unsigned long norm
[ColLast
];
24 unsigned long sel
[ColLast
];
34 } DC
; /* draw context */
36 typedef struct Item Item
;
39 Item
*next
; /* traverses all items */
40 Item
*left
, *right
; /* traverses items matching current search pattern */
43 /* forward declarations */
44 void appenditem(Item
*i
, Item
**list
, Item
**last
);
45 void calcoffsets(void);
46 char *cistrstr(const char *s
, const char *sub
);
49 void drawtext(const char *text
, unsigned long col
[ColLast
]);
50 void *emalloc(unsigned int size
);
51 void eprint(const char *errstr
, ...);
52 char *estrdup(const char *str
);
53 unsigned long getcolor(const char *colstr
);
54 Bool
grabkeyboard(void);
55 void initfont(const char *fontstr
);
56 void kpress(XKeyEvent
* e
);
57 void match(char *pattern
);
60 void setup(int x
, int y
, int w
);
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 numlockmask
= 0;
84 Item
*allitems
= NULL
; /* first of all items */
85 Item
*item
= NULL
; /* first of pattern matching items */
91 int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
92 char *(*fstrstr
)(const char *, const char *) = strstr
;
95 appenditem(Item
*i
, Item
**list
, Item
**last
) {
111 w
= promptw
+ cmdw
+ 2 * SPACE
;
112 for(next
= curr
; next
; next
=next
->right
) {
113 tw
= textw(next
->text
);
120 w
= promptw
+ cmdw
+ 2 * SPACE
;
121 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
122 tw
= textw(prev
->left
->text
);
132 cistrstr(const char *s
, const char *sub
) {
138 if((c
= *sub
++) != 0) {
143 if((csub
= *s
++) == 0)
146 while(tolower(csub
) != c
);
148 while(strncasecmp(s
, sub
, len
) != 0);
159 itm
= allitems
->next
;
160 free(allitems
->text
);
165 XFreeFontSet(dpy
, dc
.font
.set
);
167 XFreeFont(dpy
, dc
.font
.xfont
);
168 XFreePixmap(dpy
, dc
.drawable
);
170 XDestroyWindow(dpy
, win
);
171 XUngrabKeyboard(dpy
, CurrentTime
);
182 drawtext(NULL
, dc
.norm
);
186 drawtext(prompt
, dc
.sel
);
193 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
197 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
199 /* determine maximum items */
200 for(i
= curr
; i
!= next
; i
=i
->right
) {
201 dc
.w
= textw(i
->text
);
204 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
209 drawtext(next
? ">" : NULL
, dc
.norm
);
211 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
216 drawtext(const char *text
, unsigned long col
[ColLast
]) {
218 static char buf
[256];
219 unsigned int len
, olen
;
220 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
222 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
223 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
227 olen
= len
= strlen(text
);
228 if(len
>= sizeof buf
)
229 len
= sizeof buf
- 1;
230 memcpy(buf
, text
, len
);
232 h
= dc
.font
.ascent
+ dc
.font
.descent
;
233 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
235 /* shorten text if necessary */
236 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
247 return; /* too long */
248 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
250 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
252 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
256 emalloc(unsigned int size
) {
257 void *res
= malloc(size
);
260 eprint("fatal: could not malloc() %u bytes\n", size
);
265 eprint(const char *errstr
, ...) {
268 va_start(ap
, errstr
);
269 vfprintf(stderr
, errstr
, ap
);
275 estrdup(const char *str
) {
276 void *res
= strdup(str
);
279 eprint("fatal: could not malloc() %u bytes\n", strlen(str
));
284 getcolor(const char *colstr
) {
285 Colormap cmap
= DefaultColormap(dpy
, screen
);
288 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
289 eprint("error, cannot allocate color '%s'\n", colstr
);
297 for(len
= 1000; len
; len
--) {
298 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
307 initfont(const char *fontstr
) {
308 char *def
, **missing
;
311 if(!fontstr
|| fontstr
[0] == '\0')
312 eprint("error, cannot load font: '%s'\n", fontstr
);
315 XFreeFontSet(dpy
, dc
.font
.set
);
316 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
318 XFreeStringList(missing
);
320 XFontSetExtents
*font_extents
;
321 XFontStruct
**xfonts
;
323 dc
.font
.ascent
= dc
.font
.descent
= 0;
324 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
325 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
326 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
327 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
328 dc
.font
.ascent
= (*xfonts
)->ascent
;
329 if(dc
.font
.descent
< (*xfonts
)->descent
)
330 dc
.font
.descent
= (*xfonts
)->descent
;
336 XFreeFont(dpy
, dc
.font
.xfont
);
337 dc
.font
.xfont
= NULL
;
338 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
339 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
340 eprint("error, cannot load font: '%s'\n", fontstr
);
341 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
342 dc
.font
.descent
= dc
.font
.xfont
->descent
;
344 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
348 kpress(XKeyEvent
* e
) {
356 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
357 if(IsKeypadKey(ksym
))
358 if(ksym
== XK_KP_Enter
)
360 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
361 ksym
= (ksym
- XK_KP_0
) + XK_0
;
362 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
363 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
364 || IsPrivateKeypadKey(ksym
))
366 /* first check if a control mask is omitted */
367 if(e
->state
& ControlMask
) {
369 default: /* ignore other control sequences */
396 while(i
>= 0 && text
[i
] == ' ')
398 while(i
>= 0 && text
[i
] != ' ')
406 if(CLEANMASK(e
->state
) & Mod1Mask
) {
431 if(num
&& !iscntrl((int) buf
[0])) {
434 strncat(text
, buf
, sizeof text
);
436 strncpy(text
, buf
, sizeof text
);
453 while(sel
&& sel
->right
)
467 if(!(sel
&& sel
->left
))
470 if(sel
->right
== curr
) {
488 if((e
->state
& ShiftMask
) && text
)
489 fprintf(stdout
, "%s", text
);
491 fprintf(stdout
, "%s", sel
->text
);
493 fprintf(stdout
, "%s", text
);
498 if(!(sel
&& sel
->right
))
509 strncpy(text
, sel
->text
, sizeof text
);
517 match(char *pattern
) {
519 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
523 plen
= strlen(pattern
);
524 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
525 for(i
= allitems
; i
; i
= i
->next
)
526 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
527 appenditem(i
, &lexact
, &exactend
);
528 else if(!fstrncmp(pattern
, i
->text
, plen
))
529 appenditem(i
, &lprefix
, &prefixend
);
530 else if(fstrstr(i
->text
, pattern
))
531 appenditem(i
, &lsubstr
, &substrend
);
538 itemend
->right
= lprefix
;
539 lprefix
->left
= itemend
;
547 itemend
->right
= lsubstr
;
548 lsubstr
->left
= itemend
;
553 curr
= prev
= next
= sel
= item
;
560 unsigned int len
= 0, max
= 0;
564 while(fgets(buf
, sizeof buf
, stdin
)) {
566 if (buf
[len
- 1] == '\n')
573 new = emalloc(sizeof(Item
));
574 new->next
= new->left
= new->right
= NULL
;
588 /* main event loop */
589 while(running
&& !XNextEvent(dpy
, &ev
))
591 default: /* ignore all crap */
597 if(ev
.xexpose
.count
== 0)
604 setup(int x
, int y
, int w
) {
606 XModifierKeymap
*modmap
;
607 XSetWindowAttributes wa
;
609 /* init modifier map */
610 modmap
= XGetModifierMapping(dpy
);
611 for(i
= 0; i
< 8; i
++)
612 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
613 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
614 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
615 numlockmask
= (1 << i
);
617 XFreeModifiermap(modmap
);
620 dc
.norm
[ColBG
] = getcolor(normbg
);
621 dc
.norm
[ColFG
] = getcolor(normfg
);
622 dc
.sel
[ColBG
] = getcolor(selbg
);
623 dc
.sel
[ColFG
] = getcolor(selfg
);
627 wa
.override_redirect
= 1;
628 wa
.background_pixmap
= ParentRelative
;
629 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
630 mw
= w
? w
: DisplayWidth(dpy
, screen
);
631 mh
= dc
.font
.height
+ 2;
634 y
= DisplayHeight(dpy
, screen
) - mh
;
638 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
639 DefaultDepth(dpy
, screen
), CopyFromParent
,
640 DefaultVisual(dpy
, screen
),
641 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
644 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
645 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
646 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
648 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
650 cmdw
= textw(maxname
);
654 promptw
= textw(prompt
);
659 XMapRaised(dpy
, win
);
663 textnw(const char *text
, unsigned int len
) {
667 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
670 return XTextWidth(dc
.font
.xfont
, text
, len
);
674 textw(const char *text
) {
675 return textnw(text
, strlen(text
)) + dc
.font
.height
;
679 main(int argc
, char *argv
[]) {
680 int x
= 0, y
= 0, w
= 0;
683 /* command line args */
684 for(i
= 1; i
< argc
; i
++)
685 if(!strcmp(argv
[i
], "-i")) {
686 fstrncmp
= strncasecmp
;
689 else if(!strcmp(argv
[i
], "-fn")) {
690 if(++i
< argc
) font
= argv
[i
];
692 else if(!strcmp(argv
[i
], "-nb")) {
693 if(++i
< argc
) normbg
= argv
[i
];
695 else if(!strcmp(argv
[i
], "-nf")) {
696 if(++i
< argc
) normfg
= argv
[i
];
698 else if(!strcmp(argv
[i
], "-p")) {
699 if(++i
< argc
) prompt
= argv
[i
];
701 else if(!strcmp(argv
[i
], "-sb")) {
702 if(++i
< argc
) selbg
= argv
[i
];
704 else if(!strcmp(argv
[i
], "-sf")) {
705 if(++i
< argc
) selfg
= argv
[i
];
707 else if(!strcmp(argv
[i
], "-x")) {
708 if(++i
< argc
) x
= atoi(argv
[i
]);
710 else if(!strcmp(argv
[i
], "-y")) {
712 if(!strcmp(argv
[i
], "-0"))
717 else if(!strcmp(argv
[i
], "-w")) {
718 if(++i
< argc
) w
= atoi(argv
[i
]);
720 else if(!strcmp(argv
[i
], "-v"))
721 eprint("dmenu-"VERSION
", © 2006-2008 dmenu engineers, see LICENSE for details\n");
723 eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
724 " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
725 " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
726 setlocale(LC_CTYPE
, "");
727 dpy
= XOpenDisplay(0);
729 eprint("dmenu: cannot open display\n");
730 screen
= DefaultScreen(dpy
);
731 root
= RootWindow(dpy
, screen
);
733 if(isatty(STDIN_FILENO
)) {
735 running
= grabkeyboard();
737 else { /* prevent keypress loss */
738 running
= grabkeyboard();