Xinqi Bao's Git
fa03ec8775ab6ea6d41cb898c83fcdb081041acb
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);
115 for(len
= 1000; len
; len
--) {
116 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
125 initcolor(const char *colstr
) {
126 Colormap cmap
= DefaultColormap(dpy
, screen
);
129 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
130 eprint("error, cannot allocate color '%s'\n", colstr
);
135 initfont(const char *fontstr
) {
136 char *def
, **missing
;
141 XFreeFontSet(dpy
, dc
.font
.set
);
142 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
144 XFreeStringList(missing
);
146 XFontSetExtents
*font_extents
;
147 XFontStruct
**xfonts
;
149 dc
.font
.ascent
= dc
.font
.descent
= 0;
150 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
151 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
152 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
153 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
154 dc
.font
.ascent
= (*xfonts
)->ascent
;
155 if(dc
.font
.descent
< (*xfonts
)->descent
)
156 dc
.font
.descent
= (*xfonts
)->descent
;
162 XFreeFont(dpy
, dc
.font
.xfont
);
163 dc
.font
.xfont
= NULL
;
164 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
165 eprint("error, cannot load font: '%s'\n", fontstr
);
166 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
167 dc
.font
.descent
= dc
.font
.xfont
->descent
;
169 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
173 match(char *pattern
) {
179 plen
= strlen(pattern
);
182 for(i
= allitems
; i
; i
=i
->next
)
183 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
193 for(i
= allitems
; i
; i
=i
->next
)
194 if(plen
&& strncmp(pattern
, i
->text
, plen
)
195 && strstr(i
->text
, pattern
)) {
205 curr
= prev
= next
= sel
= item
;
210 kpress(XKeyEvent
* e
) {
218 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
219 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
220 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
221 || IsPrivateKeypadKey(ksym
))
223 /* first check if a control mask is omitted */
224 if(e
->state
& ControlMask
) {
226 default: /* ignore other control sequences */
253 while(i
>= 0 && text
[i
] == ' ')
255 while(i
>= 0 && text
[i
] != ' ')
263 if(CLEANMASK(e
->state
) & Mod1Mask
) {
288 if(num
&& !iscntrl((int) buf
[0])) {
291 strncat(text
, buf
, sizeof text
);
293 strncpy(text
, buf
, sizeof text
);
310 while(sel
&& sel
->right
)
324 if(!(sel
&& sel
->left
))
327 if(sel
->right
== curr
) {
345 if((e
->state
& ShiftMask
) && text
)
346 fprintf(stdout
, "%s", text
);
348 fprintf(stdout
, "%s", sel
->text
);
350 fprintf(stdout
, "%s", text
);
355 if(!(sel
&& sel
->right
))
366 strncpy(text
, sel
->text
, sizeof text
);
375 static char *maxname
= NULL
;
377 unsigned int len
= 0, max
= 0;
381 while(fgets(buf
, sizeof buf
, stdin
)) {
383 if (buf
[len
- 1] == '\n')
390 new = emalloc(sizeof(Item
));
391 new->next
= new->left
= new->right
= NULL
;
405 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
406 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
416 main(int argc
, char *argv
[]) {
420 char *normbg
= NORMBGCOLOR
;
421 char *normfg
= NORMFGCOLOR
;
422 char *selbg
= SELBGCOLOR
;
423 char *selfg
= SELFGCOLOR
;
427 XModifierKeymap
*modmap
;
428 XSetWindowAttributes wa
;
430 /* command line args */
431 for(i
= 1; i
< argc
; i
++)
432 if(!strncmp(argv
[i
], "-b", 3)) {
435 else if(!strncmp(argv
[i
], "-fn", 4)) {
436 if(++i
< argc
) font
= argv
[i
];
438 else if(!strncmp(argv
[i
], "-nb", 4)) {
439 if(++i
< argc
) normbg
= argv
[i
];
441 else if(!strncmp(argv
[i
], "-nf", 4)) {
442 if(++i
< argc
) normfg
= argv
[i
];
444 else if(!strncmp(argv
[i
], "-p", 3)) {
445 if(++i
< argc
) prompt
= argv
[i
];
447 else if(!strncmp(argv
[i
], "-sb", 4)) {
448 if(++i
< argc
) selbg
= argv
[i
];
450 else if(!strncmp(argv
[i
], "-sf", 4)) {
451 if(++i
< argc
) selfg
= argv
[i
];
453 else if(!strncmp(argv
[i
], "-v", 3))
454 eprint("dmenu-"VERSION
", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
457 setlocale(LC_CTYPE
, "");
458 dpy
= XOpenDisplay(0);
460 eprint("dmenu: cannot open display\n");
461 screen
= DefaultScreen(dpy
);
462 root
= RootWindow(dpy
, screen
);
463 if(isatty(STDIN_FILENO
)) {
464 maxname
= readstdin();
465 running
= grabkeyboard();
467 else { /* prevent keypress loss */
468 running
= grabkeyboard();
469 maxname
= readstdin();
471 /* init modifier map */
472 modmap
= XGetModifierMapping(dpy
);
473 for (i
= 0; i
< 8; i
++) {
474 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
475 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
476 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
477 numlockmask
= (1 << i
);
480 XFreeModifiermap(modmap
);
482 dc
.norm
[ColBG
] = initcolor(normbg
);
483 dc
.norm
[ColFG
] = initcolor(normfg
);
484 dc
.sel
[ColBG
] = initcolor(selbg
);
485 dc
.sel
[ColFG
] = initcolor(selfg
);
488 wa
.override_redirect
= 1;
489 wa
.background_pixmap
= ParentRelative
;
490 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
491 mw
= DisplayWidth(dpy
, screen
);
492 mh
= dc
.font
.height
+ 2;
493 win
= XCreateWindow(dpy
, root
, 0,
494 bottom
? DisplayHeight(dpy
, screen
) - mh
: 0, mw
, mh
, 0,
495 DefaultDepth(dpy
, screen
), CopyFromParent
,
496 DefaultVisual(dpy
, screen
),
497 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
499 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
500 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
501 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
503 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
505 cmdw
= textw(maxname
);
509 promptw
= textw(prompt
);
514 XMapRaised(dpy
, win
);
518 /* main event loop */
519 while(running
&& !XNextEvent(dpy
, &ev
))
521 default: /* ignore all crap */
527 if(ev
.xexpose
.count
== 0)
534 itm
= allitems
->next
;
535 free(allitems
->text
);
540 XFreeFontSet(dpy
, dc
.font
.set
);
542 XFreeFont(dpy
, dc
.font
.xfont
);
543 XFreePixmap(dpy
, dc
.drawable
);
545 XDestroyWindow(dpy
, win
);
546 XUngrabKeyboard(dpy
, CurrentTime
);