Xinqi Bao's Git
34df9ed604f8d258f907e772e67d2f56b2532532
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 void calcoffsets(void);
46 void drawtext(const char *text
, unsigned long col
[ColLast
]);
47 void *emalloc(unsigned int size
);
48 void eprint(const char *errstr
, ...);
49 char *estrdup(const char *str
);
50 unsigned long getcolor(const char *colstr
);
51 Bool
grabkeyboard(void);
52 void initfont(const char *fontstr
);
53 void kpress(XKeyEvent
* e
);
54 void match(char *pattern
);
57 void setup(Bool bottom
);
58 int strido(const char *text
, const char *pattern
);
59 unsigned int textnw(const char *text
, unsigned int len
);
60 unsigned int textw(const char *text
);
67 char *normbg
= NORMBGCOLOR
;
68 char *normfg
= NORMFGCOLOR
;
70 char *selbg
= SELBGCOLOR
;
71 char *selfg
= SELFGCOLOR
;
75 unsigned int cmdw
= 0;
77 unsigned int promptw
= 0;
78 unsigned int nitem
= 0;
79 unsigned int numlockmask
= 0;
83 Item
*allitems
= NULL
; /* first of all items */
84 Item
*item
= NULL
; /* first of pattern matching items */
97 w
= promptw
+ cmdw
+ 2 * SPACE
;
98 for(next
= curr
; next
; next
=next
->right
) {
99 tw
= textw(next
->text
);
106 w
= promptw
+ cmdw
+ 2 * SPACE
;
107 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
108 tw
= textw(prev
->left
->text
);
122 itm
= allitems
->next
;
123 free(allitems
->text
);
128 XFreeFontSet(dpy
, dc
.font
.set
);
130 XFreeFont(dpy
, dc
.font
.xfont
);
131 XFreePixmap(dpy
, dc
.drawable
);
133 XDestroyWindow(dpy
, win
);
134 XUngrabKeyboard(dpy
, CurrentTime
);
145 drawtext(NULL
, dc
.norm
);
149 drawtext(prompt
, dc
.sel
);
156 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
160 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
162 /* determine maximum items */
163 for(i
= curr
; i
!= next
; i
=i
->right
) {
164 dc
.w
= textw(i
->text
);
167 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
172 drawtext(next
? ">" : NULL
, dc
.norm
);
174 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
179 drawtext(const char *text
, unsigned long col
[ColLast
]) {
181 static char buf
[256];
182 unsigned int len
, olen
;
183 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
185 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
186 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
190 olen
= len
= strlen(text
);
191 if(len
>= sizeof buf
)
192 len
= sizeof buf
- 1;
193 memcpy(buf
, text
, len
);
195 h
= dc
.font
.ascent
+ dc
.font
.descent
;
196 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
198 /* shorten text if necessary */
199 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
210 return; /* too long */
211 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
213 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
215 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
219 emalloc(unsigned int size
) {
220 void *res
= malloc(size
);
223 eprint("fatal: could not malloc() %u bytes\n", size
);
228 eprint(const char *errstr
, ...) {
231 va_start(ap
, errstr
);
232 vfprintf(stderr
, errstr
, ap
);
238 estrdup(const char *str
) {
239 void *res
= strdup(str
);
242 eprint("fatal: could not malloc() %u bytes\n", strlen(str
));
247 getcolor(const char *colstr
) {
248 Colormap cmap
= DefaultColormap(dpy
, screen
);
251 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
252 eprint("error, cannot allocate color '%s'\n", colstr
);
260 for(len
= 1000; len
; len
--) {
261 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
270 initfont(const char *fontstr
) {
271 char *def
, **missing
;
274 if(!fontstr
|| fontstr
[0] == '\0')
275 eprint("error, cannot load font: '%s'\n", fontstr
);
278 XFreeFontSet(dpy
, dc
.font
.set
);
279 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
281 XFreeStringList(missing
);
283 XFontSetExtents
*font_extents
;
284 XFontStruct
**xfonts
;
286 dc
.font
.ascent
= dc
.font
.descent
= 0;
287 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
288 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
289 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
290 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
291 dc
.font
.ascent
= (*xfonts
)->ascent
;
292 if(dc
.font
.descent
< (*xfonts
)->descent
)
293 dc
.font
.descent
= (*xfonts
)->descent
;
299 XFreeFont(dpy
, dc
.font
.xfont
);
300 dc
.font
.xfont
= NULL
;
301 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))) {
302 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
303 eprint("error, cannot load font: '%s'\n", fontstr
);
305 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
306 dc
.font
.descent
= dc
.font
.xfont
->descent
;
308 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
312 kpress(XKeyEvent
* e
) {
320 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
321 if(IsKeypadKey(ksym
)) {
322 if(ksym
== XK_KP_Enter
) {
324 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
325 ksym
= (ksym
- XK_KP_0
) + XK_0
;
328 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
329 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
330 || IsPrivateKeypadKey(ksym
))
332 /* first check if a control mask is omitted */
333 if(e
->state
& ControlMask
) {
335 default: /* ignore other control sequences */
362 while(i
>= 0 && text
[i
] == ' ')
364 while(i
>= 0 && text
[i
] != ' ')
372 if(CLEANMASK(e
->state
) & Mod1Mask
) {
397 if(num
&& !iscntrl((int) buf
[0])) {
400 strncat(text
, buf
, sizeof text
);
402 strncpy(text
, buf
, sizeof text
);
419 while(sel
&& sel
->right
)
433 if(!(sel
&& sel
->left
))
436 if(sel
->right
== curr
) {
454 if((e
->state
& ShiftMask
) && text
)
455 fprintf(stdout
, "%s", text
);
457 fprintf(stdout
, "%s", sel
->text
);
459 fprintf(stdout
, "%s", text
);
464 if(!(sel
&& sel
->right
))
475 strncpy(text
, sel
->text
, sizeof text
);
483 match(char *pattern
) {
489 plen
= strlen(pattern
);
492 for(i
= allitems
; i
; i
=i
->next
)
493 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
503 for(i
= allitems
; i
; i
=i
->next
)
504 if(plen
&& strncmp(pattern
, i
->text
, plen
)
505 && strstr(i
->text
, pattern
)) {
515 for(i
= allitems
; i
; i
=i
->next
)
516 if(plen
&& strncmp(pattern
, i
->text
, plen
)
517 && !strstr(i
->text
, pattern
)
518 && strido(i
->text
,pattern
)) {
528 curr
= prev
= next
= sel
= item
;
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 /* main event loop */
564 while(running
&& !XNextEvent(dpy
, &ev
))
566 default: /* ignore all crap */
572 if(ev
.xexpose
.count
== 0)
581 XModifierKeymap
*modmap
;
582 XSetWindowAttributes wa
;
584 /* init modifier map */
585 modmap
= XGetModifierMapping(dpy
);
586 for(i
= 0; i
< 8; i
++)
587 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
588 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
589 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
590 numlockmask
= (1 << i
);
592 XFreeModifiermap(modmap
);
595 dc
.norm
[ColBG
] = getcolor(normbg
);
596 dc
.norm
[ColFG
] = getcolor(normfg
);
597 dc
.sel
[ColBG
] = getcolor(selbg
);
598 dc
.sel
[ColFG
] = getcolor(selfg
);
602 wa
.override_redirect
= 1;
603 wa
.background_pixmap
= ParentRelative
;
604 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
605 mw
= DisplayWidth(dpy
, screen
);
606 mh
= dc
.font
.height
+ 2;
607 win
= XCreateWindow(dpy
, root
, 0,
608 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
609 DefaultDepth(dpy
, screen
), CopyFromParent
,
610 DefaultVisual(dpy
, screen
),
611 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
614 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
615 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
616 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
618 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
620 cmdw
= textw(maxname
);
624 promptw
= textw(prompt
);
629 XMapRaised(dpy
, win
);
633 strido(const char *text
, const char *pattern
) {
634 for(; *text
&& *pattern
; text
++)
635 if (*text
== *pattern
)
641 textnw(const char *text
, unsigned int len
) {
645 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
648 return XTextWidth(dc
.font
.xfont
, text
, len
);
652 textw(const char *text
) {
653 return textnw(text
, strlen(text
)) + dc
.font
.height
;
657 main(int argc
, char *argv
[]) {
661 /* command line args */
662 for(i
= 1; i
< argc
; i
++)
663 if(!strcmp(argv
[i
], "-b")) {
666 else if(!strcmp(argv
[i
], "-fn")) {
667 if(++i
< argc
) font
= argv
[i
];
669 else if(!strcmp(argv
[i
], "-nb")) {
670 if(++i
< argc
) normbg
= argv
[i
];
672 else if(!strcmp(argv
[i
], "-nf")) {
673 if(++i
< argc
) normfg
= argv
[i
];
675 else if(!strcmp(argv
[i
], "-p")) {
676 if(++i
< argc
) prompt
= argv
[i
];
678 else if(!strcmp(argv
[i
], "-sb")) {
679 if(++i
< argc
) selbg
= argv
[i
];
681 else if(!strcmp(argv
[i
], "-sf")) {
682 if(++i
< argc
) selfg
= argv
[i
];
684 else if(!strcmp(argv
[i
], "-v"))
685 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
687 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
688 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
689 setlocale(LC_CTYPE
, "");
690 dpy
= XOpenDisplay(0);
692 eprint("dmenu: cannot open display\n");
693 screen
= DefaultScreen(dpy
);
694 root
= RootWindow(dpy
, screen
);
696 if(isatty(STDIN_FILENO
)) {
698 running
= grabkeyboard();
700 else { /* prevent keypress loss */
701 running
= grabkeyboard();