Xinqi Bao's Git
3256f9c59e0090302e42187ed98620c3919bfe57
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 char *cistrstr(const char *s
, const char *sub
);
62 unsigned int textnw(const char *text
, unsigned int len
);
63 unsigned int textw(const char *text
);
70 char *normbg
= NORMBGCOLOR
;
71 char *normfg
= NORMFGCOLOR
;
73 char *selbg
= SELBGCOLOR
;
74 char *selfg
= SELFGCOLOR
;
78 unsigned int cmdw
= 0;
80 unsigned int promptw
= 0;
81 unsigned int nitem
= 0;
82 unsigned int numlockmask
= 0;
83 Bool idomatch
= False
;
87 Item
*allitems
= NULL
; /* first of all items */
88 Item
*item
= NULL
; /* first of pattern matching items */
96 appenditem(Item
*i
, Item
*last
) {
115 w
= promptw
+ cmdw
+ 2 * SPACE
;
116 for(next
= curr
; next
; next
=next
->right
) {
117 tw
= textw(next
->text
);
124 w
= promptw
+ cmdw
+ 2 * SPACE
;
125 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
126 tw
= textw(prev
->left
->text
);
140 itm
= allitems
->next
;
141 free(allitems
->text
);
146 XFreeFontSet(dpy
, dc
.font
.set
);
148 XFreeFont(dpy
, dc
.font
.xfont
);
149 XFreePixmap(dpy
, dc
.drawable
);
151 XDestroyWindow(dpy
, win
);
152 XUngrabKeyboard(dpy
, CurrentTime
);
163 drawtext(NULL
, dc
.norm
);
167 drawtext(prompt
, dc
.sel
);
174 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
178 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
180 /* determine maximum items */
181 for(i
= curr
; i
!= next
; i
=i
->right
) {
182 dc
.w
= textw(i
->text
);
185 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
190 drawtext(next
? ">" : NULL
, dc
.norm
);
192 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
197 drawtext(const char *text
, unsigned long col
[ColLast
]) {
199 static char buf
[256];
200 unsigned int len
, olen
;
201 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
203 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
204 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
208 olen
= len
= strlen(text
);
209 if(len
>= sizeof buf
)
210 len
= sizeof buf
- 1;
211 memcpy(buf
, text
, len
);
213 h
= dc
.font
.ascent
+ dc
.font
.descent
;
214 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
216 /* shorten text if necessary */
217 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
228 return; /* too long */
229 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
231 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
233 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
237 emalloc(unsigned int size
) {
238 void *res
= malloc(size
);
241 eprint("fatal: could not malloc() %u bytes\n", size
);
246 eprint(const char *errstr
, ...) {
249 va_start(ap
, errstr
);
250 vfprintf(stderr
, errstr
, ap
);
256 estrdup(const char *str
) {
257 void *res
= strdup(str
);
260 eprint("fatal: could not malloc() %u bytes\n", strlen(str
));
265 getcolor(const char *colstr
) {
266 Colormap cmap
= DefaultColormap(dpy
, screen
);
269 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
270 eprint("error, cannot allocate color '%s'\n", colstr
);
278 for(len
= 1000; len
; len
--) {
279 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
288 initfont(const char *fontstr
) {
289 char *def
, **missing
;
292 if(!fontstr
|| fontstr
[0] == '\0')
293 eprint("error, cannot load font: '%s'\n", fontstr
);
296 XFreeFontSet(dpy
, dc
.font
.set
);
297 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
299 XFreeStringList(missing
);
301 XFontSetExtents
*font_extents
;
302 XFontStruct
**xfonts
;
304 dc
.font
.ascent
= dc
.font
.descent
= 0;
305 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
306 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
307 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
308 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
309 dc
.font
.ascent
= (*xfonts
)->ascent
;
310 if(dc
.font
.descent
< (*xfonts
)->descent
)
311 dc
.font
.descent
= (*xfonts
)->descent
;
317 XFreeFont(dpy
, dc
.font
.xfont
);
318 dc
.font
.xfont
= NULL
;
319 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
320 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
321 eprint("error, cannot load font: '%s'\n", fontstr
);
322 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
323 dc
.font
.descent
= dc
.font
.xfont
->descent
;
325 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
329 kpress(XKeyEvent
* e
) {
337 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
338 if(IsKeypadKey(ksym
)) {
339 if(ksym
== XK_KP_Enter
) {
341 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
342 ksym
= (ksym
- XK_KP_0
) + XK_0
;
345 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
346 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
347 || IsPrivateKeypadKey(ksym
))
349 /* first check if a control mask is omitted */
350 if(e
->state
& ControlMask
) {
352 default: /* ignore other control sequences */
379 while(i
>= 0 && text
[i
] == ' ')
381 while(i
>= 0 && text
[i
] != ' ')
389 if(CLEANMASK(e
->state
) & Mod1Mask
) {
414 if(num
&& !iscntrl((int) buf
[0])) {
417 strncat(text
, buf
, sizeof text
);
419 strncpy(text
, buf
, sizeof text
);
436 while(sel
&& sel
->right
)
450 if(!(sel
&& sel
->left
))
453 if(sel
->right
== curr
) {
471 if((e
->state
& ShiftMask
) && text
)
472 fprintf(stdout
, "%s", text
);
474 fprintf(stdout
, "%s", sel
->text
);
476 fprintf(stdout
, "%s", text
);
481 if(!(sel
&& sel
->right
))
492 strncpy(text
, sel
->text
, sizeof text
);
500 match(char *pattern
) {
506 plen
= strlen(pattern
);
509 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
);
514 for(i
= allitems
; i
; i
= i
->next
)
515 if(!i
->matched
&& cistrstr(i
->text
, pattern
))
516 j
= appenditem(i
, j
);
518 for(i
= allitems
; i
; i
= i
->next
)
519 if(!i
->matched
&& strcaseido(i
->text
, pattern
))
520 j
= appenditem(i
, j
);
521 curr
= prev
= next
= sel
= item
;
528 unsigned int len
= 0, max
= 0;
532 while(fgets(buf
, sizeof buf
, stdin
)) {
534 if (buf
[len
- 1] == '\n')
541 new = emalloc(sizeof(Item
));
542 new->next
= new->left
= new->right
= NULL
;
556 /* main event loop */
557 while(running
&& !XNextEvent(dpy
, &ev
))
559 default: /* ignore all crap */
565 if(ev
.xexpose
.count
== 0)
574 XModifierKeymap
*modmap
;
575 XSetWindowAttributes wa
;
577 /* init modifier map */
578 modmap
= XGetModifierMapping(dpy
);
579 for(i
= 0; i
< 8; i
++)
580 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
581 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
582 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
583 numlockmask
= (1 << i
);
585 XFreeModifiermap(modmap
);
588 dc
.norm
[ColBG
] = getcolor(normbg
);
589 dc
.norm
[ColFG
] = getcolor(normfg
);
590 dc
.sel
[ColBG
] = getcolor(selbg
);
591 dc
.sel
[ColFG
] = getcolor(selfg
);
595 wa
.override_redirect
= 1;
596 wa
.background_pixmap
= ParentRelative
;
597 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
598 mw
= DisplayWidth(dpy
, screen
);
599 mh
= dc
.font
.height
+ 2;
600 win
= XCreateWindow(dpy
, root
, 0,
601 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
602 DefaultDepth(dpy
, screen
), CopyFromParent
,
603 DefaultVisual(dpy
, screen
),
604 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
607 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
608 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
609 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
611 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
613 cmdw
= textw(maxname
);
617 promptw
= textw(prompt
);
622 XMapRaised(dpy
, win
);
626 strcaseido(const char *text
, const char *pattern
) {
627 for(; *text
&& *pattern
; text
++)
628 if(tolower((int)*text
) == tolower((int)*pattern
))
634 cistrstr(const char *s
, const char *sub
) {
640 if((c
= *sub
++) != 0) {
645 if((csub
= *s
++) == 0)
648 while(tolower(csub
) != c
);
650 while(strncasecmp(s
, sub
, len
) != 0);
657 textnw(const char *text
, unsigned int len
) {
661 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
664 return XTextWidth(dc
.font
.xfont
, text
, len
);
668 textw(const char *text
) {
669 return textnw(text
, strlen(text
)) + dc
.font
.height
;
673 main(int argc
, char *argv
[]) {
677 /* command line args */
678 for(i
= 1; i
< argc
; i
++)
679 if(!strcmp(argv
[i
], "-b")) {
682 else if(!strcmp(argv
[i
], "-i"))
684 else if(!strcmp(argv
[i
], "-fn")) {
685 if(++i
< argc
) font
= argv
[i
];
687 else if(!strcmp(argv
[i
], "-nb")) {
688 if(++i
< argc
) normbg
= argv
[i
];
690 else if(!strcmp(argv
[i
], "-nf")) {
691 if(++i
< argc
) normfg
= argv
[i
];
693 else if(!strcmp(argv
[i
], "-p")) {
694 if(++i
< argc
) prompt
= argv
[i
];
696 else if(!strcmp(argv
[i
], "-sb")) {
697 if(++i
< argc
) selbg
= argv
[i
];
699 else if(!strcmp(argv
[i
], "-sf")) {
700 if(++i
< argc
) selfg
= argv
[i
];
702 else if(!strcmp(argv
[i
], "-v"))
703 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, Michał Janeczek\n");
705 eprint("usage: dmenu [-b] [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
706 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
707 setlocale(LC_CTYPE
, "");
708 dpy
= XOpenDisplay(0);
710 eprint("dmenu: cannot open display\n");
711 screen
= DefaultScreen(dpy
);
712 root
= RootWindow(dpy
, screen
);
714 if(isatty(STDIN_FILENO
)) {
716 running
= grabkeyboard();
718 else { /* prevent keypress loss */
719 running
= grabkeyboard();