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
) {
206 int i
, num
, prev_nitem
;
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
);
297 } while(i
&& nitem
&& prev_nitem
== nitem
);
308 while(sel
&& sel
->right
)
322 if(!(sel
&& sel
->left
))
325 if(sel
->right
== curr
) {
343 if((e
->state
& ShiftMask
) && text
)
344 fprintf(stdout
, "%s", text
);
346 fprintf(stdout
, "%s", sel
->text
);
348 fprintf(stdout
, "%s", text
);
353 if(!(sel
&& sel
->right
))
364 strncpy(text
, sel
->text
, sizeof text
);
373 static char *maxname
= NULL
;
375 unsigned int len
= 0, max
= 0;
379 while(fgets(buf
, sizeof buf
, stdin
)) {
381 if (buf
[len
- 1] == '\n')
388 new = emalloc(sizeof(Item
));
389 new->next
= new->left
= new->right
= NULL
;
403 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
404 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
414 main(int argc
, char *argv
[]) {
418 char *normbg
= NORMBGCOLOR
;
419 char *normfg
= NORMFGCOLOR
;
420 char *selbg
= SELBGCOLOR
;
421 char *selfg
= SELFGCOLOR
;
425 XModifierKeymap
*modmap
;
426 XSetWindowAttributes wa
;
428 /* command line args */
429 for(i
= 1; i
< argc
; i
++)
430 if(!strncmp(argv
[i
], "-b", 3)) {
433 else if(!strncmp(argv
[i
], "-fn", 4)) {
434 if(++i
< argc
) font
= argv
[i
];
436 else if(!strncmp(argv
[i
], "-nb", 4)) {
437 if(++i
< argc
) normbg
= argv
[i
];
439 else if(!strncmp(argv
[i
], "-nf", 4)) {
440 if(++i
< argc
) normfg
= argv
[i
];
442 else if(!strncmp(argv
[i
], "-p", 3)) {
443 if(++i
< argc
) prompt
= argv
[i
];
445 else if(!strncmp(argv
[i
], "-sb", 4)) {
446 if(++i
< argc
) selbg
= argv
[i
];
448 else if(!strncmp(argv
[i
], "-sf", 4)) {
449 if(++i
< argc
) selfg
= argv
[i
];
451 else if(!strncmp(argv
[i
], "-v", 3))
452 eprint("dmenu-"VERSION
", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
455 setlocale(LC_CTYPE
, "");
456 dpy
= XOpenDisplay(0);
458 eprint("dmenu: cannot open display\n");
459 screen
= DefaultScreen(dpy
);
460 root
= RootWindow(dpy
, screen
);
461 if(isatty(STDIN_FILENO
)) {
462 maxname
= readstdin();
465 else { /* prevent keypress loss */
467 maxname
= readstdin();
469 /* init modifier map */
470 modmap
= XGetModifierMapping(dpy
);
471 for (i
= 0; i
< 8; i
++) {
472 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
473 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
474 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
475 numlockmask
= (1 << i
);
478 XFreeModifiermap(modmap
);
480 dc
.norm
[ColBG
] = initcolor(normbg
);
481 dc
.norm
[ColFG
] = initcolor(normfg
);
482 dc
.sel
[ColBG
] = initcolor(selbg
);
483 dc
.sel
[ColFG
] = initcolor(selfg
);
486 wa
.override_redirect
= 1;
487 wa
.background_pixmap
= ParentRelative
;
488 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
489 mw
= DisplayWidth(dpy
, screen
);
490 mh
= dc
.font
.height
+ 2;
491 win
= XCreateWindow(dpy
, root
, 0,
492 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
493 DefaultDepth(dpy
, screen
), CopyFromParent
,
494 DefaultVisual(dpy
, screen
),
495 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
497 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
498 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
499 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
501 cmdw
= textw(maxname
);
505 promptw
= textw(prompt
);
510 XMapRaised(dpy
, win
);
514 /* main event loop */
515 while(running
&& !XNextEvent(dpy
, &ev
))
517 default: /* ignore all crap */
523 if(ev
.xexpose
.count
== 0)
530 itm
= allitems
->next
;
531 free(allitems
->text
);
536 XFreeFontSet(dpy
, dc
.font
.set
);
538 XFreeFont(dpy
, dc
.font
.xfont
);
539 XFreePixmap(dpy
, dc
.drawable
);
541 XDestroyWindow(dpy
, win
);
542 XUngrabKeyboard(dpy
, CurrentTime
);