Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
14 typedef struct Item Item
;
16 Item
*next
; /* traverses all items */
17 Item
*left
, *right
; /* traverses items matching current search pattern */
23 static char text
[4096];
24 static char *prompt
= NULL
;
28 static unsigned int cmdw
= 0;
29 static unsigned int promptw
= 0;
30 static unsigned int numlockmask
= 0;
31 static Bool running
= True
;
32 static Item
*allitems
= NULL
; /* first of all items */
33 static Item
*item
= NULL
; /* first of pattern matching items */
34 static Item
*sel
= NULL
;
35 static Item
*next
= NULL
;
36 static Item
*prev
= NULL
;
37 static Item
*curr
= NULL
;
47 w
= promptw
+ cmdw
+ 2 * SPACE
;
48 for(next
= curr
; next
; next
=next
->right
) {
49 tw
= textw(next
->text
);
56 w
= promptw
+ cmdw
+ 2 * SPACE
;
57 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
58 tw
= textw(prev
->left
->text
);
75 drawtext(NULL
, dc
.norm
);
79 drawtext(prompt
, dc
.sel
);
86 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
90 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
92 /* determine maximum items */
93 for(i
= curr
; i
!= next
; i
=i
->right
) {
94 dc
.w
= textw(i
->text
);
97 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
102 drawtext(next
? ">" : NULL
, dc
.norm
);
104 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
112 for(len
= 1000; len
; len
--) {
113 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
122 initcolor(const char *colstr
) {
123 Colormap cmap
= DefaultColormap(dpy
, screen
);
126 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
127 eprint("error, cannot allocate color '%s'\n", colstr
);
132 initfont(const char *fontstr
) {
133 char *def
, **missing
;
136 if(!fontstr
|| fontstr
[0] == '\0')
137 eprint("error, cannot load font: '%s'\n", fontstr
);
140 XFreeFontSet(dpy
, dc
.font
.set
);
141 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
143 XFreeStringList(missing
);
145 XFontSetExtents
*font_extents
;
146 XFontStruct
**xfonts
;
148 dc
.font
.ascent
= dc
.font
.descent
= 0;
149 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
150 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
151 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
152 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
153 dc
.font
.ascent
= (*xfonts
)->ascent
;
154 if(dc
.font
.descent
< (*xfonts
)->descent
)
155 dc
.font
.descent
= (*xfonts
)->descent
;
161 XFreeFont(dpy
, dc
.font
.xfont
);
162 dc
.font
.xfont
= NULL
;
163 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))) {
164 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
165 eprint("error, cannot load font: '%s'\n", fontstr
);
167 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
168 dc
.font
.descent
= dc
.font
.xfont
->descent
;
170 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
174 strido(const char *text
, const char *pattern
) {
175 for(; *text
&& *pattern
; text
++)
176 if (*text
== *pattern
)
182 match(char *pattern
) {
188 plen
= strlen(pattern
);
191 for(i
= allitems
; i
; i
=i
->next
)
192 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
202 for(i
= allitems
; i
; i
=i
->next
)
203 if(plen
&& strncmp(pattern
, i
->text
, plen
)
204 && strstr(i
->text
, pattern
)) {
214 for(i
= allitems
; i
; i
=i
->next
)
215 if(plen
&& strncmp(pattern
, i
->text
, plen
)
216 && !strstr(i
->text
, pattern
)
217 && strido(i
->text
,pattern
)) {
227 curr
= prev
= next
= sel
= item
;
232 kpress(XKeyEvent
* e
) {
240 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
241 if(IsKeypadKey(ksym
)) {
242 if(ksym
== XK_KP_Enter
) {
244 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
245 ksym
= (ksym
- XK_KP_0
) + XK_0
;
248 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
249 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
250 || IsPrivateKeypadKey(ksym
))
252 /* first check if a control mask is omitted */
253 if(e
->state
& ControlMask
) {
255 default: /* ignore other control sequences */
282 while(i
>= 0 && text
[i
] == ' ')
284 while(i
>= 0 && text
[i
] != ' ')
292 if(CLEANMASK(e
->state
) & Mod1Mask
) {
317 if(num
&& !iscntrl((int) buf
[0])) {
320 strncat(text
, buf
, sizeof text
);
322 strncpy(text
, buf
, sizeof text
);
339 while(sel
&& sel
->right
)
353 if(!(sel
&& sel
->left
))
356 if(sel
->right
== curr
) {
374 if((e
->state
& ShiftMask
) && text
)
375 fprintf(stdout
, "%s", text
);
377 fprintf(stdout
, "%s", sel
->text
);
379 fprintf(stdout
, "%s", text
);
384 if(!(sel
&& sel
->right
))
395 strncpy(text
, sel
->text
, sizeof text
);
404 static char *maxname
= NULL
;
406 unsigned int len
= 0, max
= 0;
410 while(fgets(buf
, sizeof buf
, stdin
)) {
412 if (buf
[len
- 1] == '\n')
419 new = emalloc(sizeof(Item
));
420 new->next
= new->left
= new->right
= NULL
;
434 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
435 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
445 main(int argc
, char *argv
[]) {
449 char *normbg
= NORMBGCOLOR
;
450 char *normfg
= NORMFGCOLOR
;
451 char *selbg
= SELBGCOLOR
;
452 char *selfg
= SELFGCOLOR
;
456 XModifierKeymap
*modmap
;
457 XSetWindowAttributes wa
;
459 /* command line args */
460 for(i
= 1; i
< argc
; i
++)
461 if(!strcmp(argv
[i
], "-b")) {
464 else if(!strcmp(argv
[i
], "-fn")) {
465 if(++i
< argc
) font
= argv
[i
];
467 else if(!strcmp(argv
[i
], "-nb")) {
468 if(++i
< argc
) normbg
= argv
[i
];
470 else if(!strcmp(argv
[i
], "-nf")) {
471 if(++i
< argc
) normfg
= argv
[i
];
473 else if(!strcmp(argv
[i
], "-p")) {
474 if(++i
< argc
) prompt
= argv
[i
];
476 else if(!strcmp(argv
[i
], "-sb")) {
477 if(++i
< argc
) selbg
= argv
[i
];
479 else if(!strcmp(argv
[i
], "-sf")) {
480 if(++i
< argc
) selfg
= argv
[i
];
482 else if(!strcmp(argv
[i
], "-v"))
483 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
486 setlocale(LC_CTYPE
, "");
487 dpy
= XOpenDisplay(0);
489 eprint("dmenu: cannot open display\n");
490 screen
= DefaultScreen(dpy
);
491 root
= RootWindow(dpy
, screen
);
492 if(isatty(STDIN_FILENO
)) {
493 maxname
= readstdin();
494 running
= grabkeyboard();
496 else { /* prevent keypress loss */
497 running
= grabkeyboard();
498 maxname
= readstdin();
500 /* init modifier map */
501 modmap
= XGetModifierMapping(dpy
);
502 for (i
= 0; i
< 8; i
++) {
503 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
504 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
505 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
506 numlockmask
= (1 << i
);
509 XFreeModifiermap(modmap
);
511 dc
.norm
[ColBG
] = initcolor(normbg
);
512 dc
.norm
[ColFG
] = initcolor(normfg
);
513 dc
.sel
[ColBG
] = initcolor(selbg
);
514 dc
.sel
[ColFG
] = initcolor(selfg
);
517 wa
.override_redirect
= 1;
518 wa
.background_pixmap
= ParentRelative
;
519 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
520 mw
= DisplayWidth(dpy
, screen
);
521 mh
= dc
.font
.height
+ 2;
522 win
= XCreateWindow(dpy
, root
, 0,
523 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
524 DefaultDepth(dpy
, screen
), CopyFromParent
,
525 DefaultVisual(dpy
, screen
),
526 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
528 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
529 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
530 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
532 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
534 cmdw
= textw(maxname
);
538 promptw
= textw(prompt
);
543 XMapRaised(dpy
, win
);
547 /* main event loop */
548 while(running
&& !XNextEvent(dpy
, &ev
))
550 default: /* ignore all crap */
556 if(ev
.xexpose
.count
== 0)
563 itm
= allitems
->next
;
564 free(allitems
->text
);
569 XFreeFontSet(dpy
, dc
.font
.set
);
571 XFreeFont(dpy
, dc
.font
.xfont
);
572 XFreePixmap(dpy
, dc
.drawable
);
574 XDestroyWindow(dpy
, win
);
575 XUngrabKeyboard(dpy
, CurrentTime
);