Xinqi Bao's Git
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details. */
11 #include <X11/Xutil.h>
12 #include <X11/keysym.h>
14 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
16 typedef struct Item Item
;
18 Item
*next
; /* traverses all items */
19 Item
*left
, *right
; /* traverses items matching current search pattern */
25 static char text
[4096];
26 static char *prompt
= NULL
;
30 static unsigned int cmdw
= 0;
31 static unsigned int promptw
= 0;
32 static unsigned int numlockmask
= 0;
33 static Bool running
= True
;
34 static Item
*allitems
= NULL
; /* first of all items */
35 static Item
*item
= NULL
; /* first of pattern matching items */
36 static Item
*sel
= NULL
;
37 static Item
*next
= NULL
;
38 static Item
*prev
= NULL
;
39 static Item
*curr
= NULL
;
49 w
= promptw
+ cmdw
+ 2 * SPACE
;
50 for(next
= curr
; next
; next
=next
->right
) {
51 tw
= textw(next
->text
);
58 w
= promptw
+ cmdw
+ 2 * SPACE
;
59 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
60 tw
= textw(prev
->left
->text
);
77 drawtext(NULL
, dc
.norm
);
81 drawtext(prompt
, dc
.sel
);
88 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
92 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
94 /* determine maximum items */
95 for(i
= curr
; i
!= next
; i
=i
->right
) {
96 dc
.w
= textw(i
->text
);
99 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
104 drawtext(next
? ">" : NULL
, dc
.norm
);
106 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
114 for(len
= 1000; len
; len
--) {
115 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
124 initcolor(const char *colstr
) {
125 Colormap cmap
= DefaultColormap(dpy
, screen
);
128 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
129 eprint("error, cannot allocate color '%s'\n", colstr
);
134 initfont(const char *fontstr
) {
135 char *def
, **missing
;
138 if(!fontstr
|| fontstr
[0] == '\0')
139 eprint("error, cannot load font: '%s'\n", fontstr
);
142 XFreeFontSet(dpy
, dc
.font
.set
);
143 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
145 XFreeStringList(missing
);
147 XFontSetExtents
*font_extents
;
148 XFontStruct
**xfonts
;
150 dc
.font
.ascent
= dc
.font
.descent
= 0;
151 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
152 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
153 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
154 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
155 dc
.font
.ascent
= (*xfonts
)->ascent
;
156 if(dc
.font
.descent
< (*xfonts
)->descent
)
157 dc
.font
.descent
= (*xfonts
)->descent
;
163 XFreeFont(dpy
, dc
.font
.xfont
);
164 dc
.font
.xfont
= NULL
;
165 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
166 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 match(char *pattern
) {
180 plen
= strlen(pattern
);
183 for(i
= allitems
; i
; i
=i
->next
)
184 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
194 for(i
= allitems
; i
; i
=i
->next
)
195 if(plen
&& strncmp(pattern
, i
->text
, plen
)
196 && strstr(i
->text
, pattern
)) {
206 curr
= prev
= next
= sel
= item
;
211 kpress(XKeyEvent
* e
) {
219 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
220 if(IsKeypadKey(ksym
)) {
221 if(ksym
== XK_KP_Enter
) {
223 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
224 ksym
= (ksym
- XK_KP_0
) + XK_0
;
227 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
228 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
229 || IsPrivateKeypadKey(ksym
))
231 /* first check if a control mask is omitted */
232 if(e
->state
& ControlMask
) {
234 default: /* ignore other control sequences */
261 while(i
>= 0 && text
[i
] == ' ')
263 while(i
>= 0 && text
[i
] != ' ')
271 if(CLEANMASK(e
->state
) & Mod1Mask
) {
296 if(num
&& !iscntrl((int) buf
[0])) {
299 strncat(text
, buf
, sizeof text
);
301 strncpy(text
, buf
, sizeof text
);
318 while(sel
&& sel
->right
)
332 if(!(sel
&& sel
->left
))
335 if(sel
->right
== curr
) {
353 if((e
->state
& ShiftMask
) && text
)
354 fprintf(stdout
, "%s", text
);
356 fprintf(stdout
, "%s", sel
->text
);
358 fprintf(stdout
, "%s", text
);
363 if(!(sel
&& sel
->right
))
374 strncpy(text
, sel
->text
, sizeof text
);
383 static char *maxname
= NULL
;
385 unsigned int len
= 0, max
= 0;
389 while(fgets(buf
, sizeof buf
, stdin
)) {
391 if (buf
[len
- 1] == '\n')
398 new = emalloc(sizeof(Item
));
399 new->next
= new->left
= new->right
= NULL
;
413 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
414 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
424 main(int argc
, char *argv
[]) {
428 char *normbg
= NORMBGCOLOR
;
429 char *normfg
= NORMFGCOLOR
;
430 char *selbg
= SELBGCOLOR
;
431 char *selfg
= SELFGCOLOR
;
435 XModifierKeymap
*modmap
;
436 XSetWindowAttributes wa
;
438 /* command line args */
439 for(i
= 1; i
< argc
; i
++)
440 if(!strcmp(argv
[i
], "-b")) {
443 else if(!strcmp(argv
[i
], "-fn")) {
444 if(++i
< argc
) font
= argv
[i
];
446 else if(!strcmp(argv
[i
], "-nb")) {
447 if(++i
< argc
) normbg
= argv
[i
];
449 else if(!strcmp(argv
[i
], "-nf")) {
450 if(++i
< argc
) normfg
= argv
[i
];
452 else if(!strcmp(argv
[i
], "-p")) {
453 if(++i
< argc
) prompt
= argv
[i
];
455 else if(!strcmp(argv
[i
], "-sb")) {
456 if(++i
< argc
) selbg
= argv
[i
];
458 else if(!strcmp(argv
[i
], "-sf")) {
459 if(++i
< argc
) selfg
= argv
[i
];
461 else if(!strcmp(argv
[i
], "-v"))
462 eprint("dmenu-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
465 setlocale(LC_CTYPE
, "");
466 dpy
= XOpenDisplay(0);
468 eprint("dmenu: cannot open display\n");
469 screen
= DefaultScreen(dpy
);
470 root
= RootWindow(dpy
, screen
);
471 if(isatty(STDIN_FILENO
)) {
472 maxname
= readstdin();
473 running
= grabkeyboard();
475 else { /* prevent keypress loss */
476 running
= grabkeyboard();
477 maxname
= readstdin();
479 /* init modifier map */
480 modmap
= XGetModifierMapping(dpy
);
481 for (i
= 0; i
< 8; i
++) {
482 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
483 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
484 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
485 numlockmask
= (1 << i
);
488 XFreeModifiermap(modmap
);
490 dc
.norm
[ColBG
] = initcolor(normbg
);
491 dc
.norm
[ColFG
] = initcolor(normfg
);
492 dc
.sel
[ColBG
] = initcolor(selbg
);
493 dc
.sel
[ColFG
] = initcolor(selfg
);
496 wa
.override_redirect
= 1;
497 wa
.background_pixmap
= ParentRelative
;
498 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
499 mw
= DisplayWidth(dpy
, screen
);
500 mh
= dc
.font
.height
+ 2;
501 win
= XCreateWindow(dpy
, root
, 0,
502 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
503 DefaultDepth(dpy
, screen
), CopyFromParent
,
504 DefaultVisual(dpy
, screen
),
505 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
507 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
508 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
509 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
511 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
513 cmdw
= textw(maxname
);
517 promptw
= textw(prompt
);
522 XMapRaised(dpy
, win
);
526 /* main event loop */
527 while(running
&& !XNextEvent(dpy
, &ev
))
529 default: /* ignore all crap */
535 if(ev
.xexpose
.count
== 0)
542 itm
= allitems
->next
;
543 free(allitems
->text
);
548 XFreeFontSet(dpy
, dc
.font
.set
);
550 XFreeFont(dpy
, dc
.font
.xfont
);
551 XFreePixmap(dpy
, dc
.drawable
);
553 XDestroyWindow(dpy
, win
);
554 XUngrabKeyboard(dpy
, CurrentTime
);