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);
112 initcolor(const char *colstr
) {
113 Colormap cmap
= DefaultColormap(dpy
, screen
);
116 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
117 eprint("error, cannot allocate color '%s'\n", colstr
);
122 initfont(const char *fontstr
) {
123 char *def
, **missing
;
128 XFreeFontSet(dpy
, dc
.font
.set
);
129 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
131 XFreeStringList(missing
);
133 XFontSetExtents
*font_extents
;
134 XFontStruct
**xfonts
;
136 dc
.font
.ascent
= dc
.font
.descent
= 0;
137 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
138 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
139 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
140 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
141 dc
.font
.ascent
= (*xfonts
)->ascent
;
142 if(dc
.font
.descent
< (*xfonts
)->descent
)
143 dc
.font
.descent
= (*xfonts
)->descent
;
149 XFreeFont(dpy
, dc
.font
.xfont
);
150 dc
.font
.xfont
= NULL
;
151 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
152 eprint("error, cannot load font: '%s'\n", fontstr
);
153 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
154 dc
.font
.descent
= dc
.font
.xfont
->descent
;
156 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
160 match(char *pattern
) {
166 plen
= strlen(pattern
);
169 for(i
= allitems
; i
; i
=i
->next
)
170 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
180 for(i
= allitems
; i
; i
=i
->next
)
181 if(plen
&& strncmp(pattern
, i
->text
, plen
)
182 && strstr(i
->text
, pattern
)) {
192 curr
= prev
= next
= sel
= item
;
197 kpress(XKeyEvent
* e
) {
199 int i
, num
, prev_nitem
;
205 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
206 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
207 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
208 || IsPrivateKeypadKey(ksym
))
210 /* first check if a control mask is omitted */
211 if(e
->state
& ControlMask
) {
213 default: /* ignore other control sequences */
240 while(i
>= 0 && text
[i
] == ' ')
242 while(i
>= 0 && text
[i
] != ' ')
250 if(CLEANMASK(e
->state
) & Mod1Mask
) {
275 if(num
&& !iscntrl((int) buf
[0])) {
278 strncat(text
, buf
, sizeof text
);
280 strncpy(text
, buf
, sizeof text
);
290 } while(i
&& nitem
&& prev_nitem
== nitem
);
301 while(sel
&& sel
->right
)
315 if(!(sel
&& sel
->left
))
318 if(sel
->right
== curr
) {
336 if((e
->state
& ShiftMask
) && text
)
337 fprintf(stdout
, "%s", text
);
339 fprintf(stdout
, "%s", sel
->text
);
341 fprintf(stdout
, "%s", text
);
346 if(!(sel
&& sel
->right
))
357 strncpy(text
, sel
->text
, sizeof text
);
366 static char *maxname
= NULL
;
368 unsigned int len
= 0, max
= 0;
372 while(fgets(buf
, sizeof buf
, stdin
)) {
374 if (buf
[len
- 1] == '\n')
381 new = emalloc(sizeof(Item
));
382 new->next
= new->left
= new->right
= NULL
;
396 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
397 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
407 main(int argc
, char *argv
[]) {
411 char *normbg
= NORMBGCOLOR
;
412 char *normfg
= NORMFGCOLOR
;
413 char *selbg
= SELBGCOLOR
;
414 char *selfg
= SELFGCOLOR
;
418 XModifierKeymap
*modmap
;
419 XSetWindowAttributes wa
;
421 if(isatty(STDIN_FILENO
)) {
422 fputs("error: dmenu can't run in an interactive shell\n", stdout
);
425 /* command line args */
426 for(i
= 1; i
< argc
; i
++)
427 if(!strncmp(argv
[i
], "-b", 3)) {
430 else if(!strncmp(argv
[i
], "-fn", 4)) {
431 if(++i
< argc
) font
= argv
[i
];
433 else if(!strncmp(argv
[i
], "-nb", 4)) {
434 if(++i
< argc
) normbg
= argv
[i
];
436 else if(!strncmp(argv
[i
], "-nf", 4)) {
437 if(++i
< argc
) normfg
= argv
[i
];
439 else if(!strncmp(argv
[i
], "-p", 3)) {
440 if(++i
< argc
) prompt
= argv
[i
];
442 else if(!strncmp(argv
[i
], "-sb", 4)) {
443 if(++i
< argc
) selbg
= argv
[i
];
445 else if(!strncmp(argv
[i
], "-sf", 4)) {
446 if(++i
< argc
) selfg
= argv
[i
];
448 else if(!strncmp(argv
[i
], "-v", 3))
449 eprint("dmenu-"VERSION
", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
452 setlocale(LC_CTYPE
, "");
453 dpy
= XOpenDisplay(0);
455 eprint("dmenu: cannot open display\n");
456 screen
= DefaultScreen(dpy
);
457 root
= RootWindow(dpy
, screen
);
458 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
459 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
461 maxname
= readstdin();
462 /* init modifier map */
463 modmap
= XGetModifierMapping(dpy
);
464 for (i
= 0; i
< 8; i
++) {
465 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
466 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
467 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
468 numlockmask
= (1 << i
);
471 XFreeModifiermap(modmap
);
473 dc
.norm
[ColBG
] = initcolor(normbg
);
474 dc
.norm
[ColFG
] = initcolor(normfg
);
475 dc
.sel
[ColBG
] = initcolor(selbg
);
476 dc
.sel
[ColFG
] = initcolor(selfg
);
479 wa
.override_redirect
= 1;
480 wa
.background_pixmap
= ParentRelative
;
481 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
482 mw
= DisplayWidth(dpy
, screen
);
483 mh
= dc
.font
.height
+ 2;
484 win
= XCreateWindow(dpy
, root
, 0,
485 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
486 DefaultDepth(dpy
, screen
), CopyFromParent
,
487 DefaultVisual(dpy
, screen
),
488 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
490 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
491 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
492 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
494 cmdw
= textw(maxname
);
498 promptw
= textw(prompt
);
503 XMapRaised(dpy
, win
);
507 /* main event loop */
508 while(running
&& !XNextEvent(dpy
, &ev
))
510 default: /* ignore all crap */
516 if(ev
.xexpose
.count
== 0)
523 itm
= allitems
->next
;
524 free(allitems
->text
);
529 XFreeFontSet(dpy
, dc
.font
.set
);
531 XFreeFont(dpy
, dc
.font
.xfont
);
532 XFreePixmap(dpy
, dc
.drawable
);
534 XDestroyWindow(dpy
, win
);
535 XUngrabKeyboard(dpy
, CurrentTime
);