Xinqi Bao's Git
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details.
12 #include <X11/Xutil.h>
13 #include <X11/keysym.h>
15 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
17 typedef struct Item Item
;
19 Item
*next
; /* traverses all items */
20 Item
*left
, *right
; /* traverses items matching current search pattern */
26 static char text
[4096];
27 static char *prompt
= NULL
;
31 static unsigned int cmdw
= 0;
32 static unsigned int promptw
= 0;
33 static unsigned int numlockmask
= 0;
34 static Bool running
= True
;
35 static Item
*allitems
= NULL
; /* first of all items */
36 static Item
*item
= NULL
; /* first of pattern matching items */
37 static Item
*sel
= NULL
;
38 static Item
*next
= NULL
;
39 static Item
*prev
= NULL
;
40 static Item
*curr
= NULL
;
50 w
= promptw
+ cmdw
+ 2 * SPACE
;
51 for(next
= curr
; next
; next
=next
->right
) {
52 tw
= textw(next
->text
);
59 w
= promptw
+ cmdw
+ 2 * SPACE
;
60 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
61 tw
= textw(prev
->left
->text
);
78 drawtext(NULL
, dc
.norm
);
82 drawtext(prompt
, dc
.sel
);
89 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
93 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
95 /* determine maximum items */
96 for(i
= curr
; i
!= next
; i
=i
->right
) {
97 dc
.w
= textw(i
->text
);
100 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
105 drawtext(next
? ">" : NULL
, dc
.norm
);
107 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
113 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
114 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
119 initcolor(const char *colstr
) {
120 Colormap cmap
= DefaultColormap(dpy
, screen
);
123 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
124 eprint("error, cannot allocate color '%s'\n", colstr
);
129 initfont(const char *fontstr
) {
130 char *def
, **missing
;
135 XFreeFontSet(dpy
, dc
.font
.set
);
136 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
138 XFreeStringList(missing
);
140 XFontSetExtents
*font_extents
;
141 XFontStruct
**xfonts
;
143 dc
.font
.ascent
= dc
.font
.descent
= 0;
144 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
145 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
146 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
147 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
148 dc
.font
.ascent
= (*xfonts
)->ascent
;
149 if(dc
.font
.descent
< (*xfonts
)->descent
)
150 dc
.font
.descent
= (*xfonts
)->descent
;
156 XFreeFont(dpy
, dc
.font
.xfont
);
157 dc
.font
.xfont
= NULL
;
158 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
159 eprint("error, cannot load font: '%s'\n", fontstr
);
160 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
161 dc
.font
.descent
= dc
.font
.xfont
->descent
;
163 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
167 match(char *pattern
) {
173 plen
= strlen(pattern
);
176 for(i
= allitems
; i
; i
=i
->next
)
177 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
187 for(i
= allitems
; i
; i
=i
->next
)
188 if(plen
&& strncmp(pattern
, i
->text
, plen
)
189 && strstr(i
->text
, pattern
)) {
199 curr
= prev
= next
= sel
= item
;
204 kpress(XKeyEvent
* e
) {
212 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
213 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
214 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
215 || IsPrivateKeypadKey(ksym
))
217 /* first check if a control mask is omitted */
218 if(e
->state
& ControlMask
) {
220 default: /* ignore other control sequences */
247 while(i
>= 0 && text
[i
] == ' ')
249 while(i
>= 0 && text
[i
] != ' ')
257 if(CLEANMASK(e
->state
) & Mod1Mask
) {
282 if(num
&& !iscntrl((int) buf
[0])) {
285 strncat(text
, buf
, sizeof text
);
287 strncpy(text
, buf
, sizeof text
);
304 while(sel
&& sel
->right
)
318 if(!(sel
&& sel
->left
))
321 if(sel
->right
== curr
) {
339 if((e
->state
& ShiftMask
) && text
)
340 fprintf(stdout
, "%s", text
);
342 fprintf(stdout
, "%s", sel
->text
);
344 fprintf(stdout
, "%s", text
);
349 if(!(sel
&& sel
->right
))
360 strncpy(text
, sel
->text
, sizeof text
);
369 static char *maxname
= NULL
;
371 unsigned int len
= 0, max
= 0;
375 while(fgets(buf
, sizeof buf
, stdin
)) {
377 if (buf
[len
- 1] == '\n')
384 new = emalloc(sizeof(Item
));
385 new->next
= new->left
= new->right
= NULL
;
399 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
400 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
410 main(int argc
, char *argv
[]) {
414 char *normbg
= NORMBGCOLOR
;
415 char *normfg
= NORMFGCOLOR
;
416 char *selbg
= SELBGCOLOR
;
417 char *selfg
= SELFGCOLOR
;
421 XModifierKeymap
*modmap
;
422 XSetWindowAttributes wa
;
424 /* command line args */
425 for(i
= 1; i
< argc
; i
++)
426 if(!strncmp(argv
[i
], "-b", 3)) {
429 else if(!strncmp(argv
[i
], "-fn", 4)) {
430 if(++i
< argc
) font
= argv
[i
];
432 else if(!strncmp(argv
[i
], "-nb", 4)) {
433 if(++i
< argc
) normbg
= argv
[i
];
435 else if(!strncmp(argv
[i
], "-nf", 4)) {
436 if(++i
< argc
) normfg
= argv
[i
];
438 else if(!strncmp(argv
[i
], "-p", 3)) {
439 if(++i
< argc
) prompt
= argv
[i
];
441 else if(!strncmp(argv
[i
], "-sb", 4)) {
442 if(++i
< argc
) selbg
= argv
[i
];
444 else if(!strncmp(argv
[i
], "-sf", 4)) {
445 if(++i
< argc
) selfg
= argv
[i
];
447 else if(!strncmp(argv
[i
], "-v", 3))
448 eprint("dmenu-"VERSION
", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
451 setlocale(LC_CTYPE
, "");
452 dpy
= XOpenDisplay(0);
454 eprint("dmenu: cannot open display\n");
455 screen
= DefaultScreen(dpy
);
456 root
= RootWindow(dpy
, screen
);
457 if(isatty(STDIN_FILENO
)) {
458 maxname
= readstdin();
461 else { /* prevent keypress loss */
463 maxname
= readstdin();
465 /* init modifier map */
466 modmap
= XGetModifierMapping(dpy
);
467 for (i
= 0; i
< 8; i
++) {
468 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
469 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
470 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
471 numlockmask
= (1 << i
);
474 XFreeModifiermap(modmap
);
476 dc
.norm
[ColBG
] = initcolor(normbg
);
477 dc
.norm
[ColFG
] = initcolor(normfg
);
478 dc
.sel
[ColBG
] = initcolor(selbg
);
479 dc
.sel
[ColFG
] = initcolor(selfg
);
482 wa
.override_redirect
= 1;
483 wa
.background_pixmap
= ParentRelative
;
484 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
485 mw
= DisplayWidth(dpy
, screen
);
486 mh
= dc
.font
.height
+ 2;
487 win
= XCreateWindow(dpy
, root
, 0,
488 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
489 DefaultDepth(dpy
, screen
), CopyFromParent
,
490 DefaultVisual(dpy
, screen
),
491 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
493 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
494 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
495 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
497 cmdw
= textw(maxname
);
501 promptw
= textw(prompt
);
506 XMapRaised(dpy
, win
);
510 /* main event loop */
511 while(running
&& !XNextEvent(dpy
, &ev
))
513 default: /* ignore all crap */
519 if(ev
.xexpose
.count
== 0)
526 itm
= allitems
->next
;
527 free(allitems
->text
);
532 XFreeFontSet(dpy
, dc
.font
.set
);
534 XFreeFont(dpy
, dc
.font
.xfont
);
535 XFreePixmap(dpy
, dc
.drawable
);
537 XDestroyWindow(dpy
, win
);
538 XUngrabKeyboard(dpy
, CurrentTime
);