Xinqi Bao's Git
688fabf26a4aa39d5c912f7276d6e29b6911cb59
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
14 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
17 enum { ColFG
, ColBG
, ColLast
};
22 unsigned long norm
[ColLast
];
23 unsigned long sel
[ColLast
];
33 } DC
; /* draw context */
35 typedef struct Item Item
;
39 Item
*next
; /* traverses all items */
40 Item
*left
, *right
; /* traverses items matching current search pattern */
43 /* forward declarations */
44 Item
*appenditem(Item
*i
, Item
*last
);
45 void calcoffsets(void);
46 char *cistrstr(const char *s
, const char *sub
);
49 void drawtext(const char *text
, unsigned long col
[ColLast
]);
50 void *emalloc(unsigned int size
);
51 void eprint(const char *errstr
, ...);
52 char *estrdup(const char *str
);
53 unsigned long getcolor(const char *colstr
);
54 Bool
grabkeyboard(void);
55 void initfont(const char *fontstr
);
56 void kpress(XKeyEvent
* e
);
57 void match(char *pattern
);
60 void setup(int x
, int y
, int w
);
61 unsigned int textnw(const char *text
, unsigned int len
);
62 unsigned int textw(const char *text
);
69 char *normbg
= NORMBGCOLOR
;
70 char *normfg
= NORMFGCOLOR
;
72 char *selbg
= SELBGCOLOR
;
73 char *selfg
= SELFGCOLOR
;
77 unsigned int cmdw
= 0;
79 unsigned int promptw
= 0;
80 unsigned int nitem
= 0;
81 unsigned int numlockmask
= 0;
85 Item
*allitems
= NULL
; /* first of all items */
86 Item
*item
= NULL
; /* first of pattern matching items */
92 int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
93 char *(*fstrstr
)(const char *, const char *) = strstr
;
96 appenditem(Item
*i
, Item
*last
) {
114 w
= promptw
+ cmdw
+ 2 * SPACE
;
115 for(next
= curr
; next
; next
=next
->right
) {
116 tw
= textw(next
->text
);
123 w
= promptw
+ cmdw
+ 2 * SPACE
;
124 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
125 tw
= textw(prev
->left
->text
);
135 cistrstr(const char *s
, const char *sub
) {
141 if((c
= *sub
++) != 0) {
146 if((csub
= *s
++) == 0)
149 while(tolower(csub
) != c
);
151 while(strncasecmp(s
, sub
, len
) != 0);
162 itm
= allitems
->next
;
163 free(allitems
->text
);
168 XFreeFontSet(dpy
, dc
.font
.set
);
170 XFreeFont(dpy
, dc
.font
.xfont
);
171 XFreePixmap(dpy
, dc
.drawable
);
173 XDestroyWindow(dpy
, win
);
174 XUngrabKeyboard(dpy
, CurrentTime
);
185 drawtext(NULL
, dc
.norm
);
189 drawtext(prompt
, dc
.sel
);
196 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
200 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
202 /* determine maximum items */
203 for(i
= curr
; i
!= next
; i
=i
->right
) {
204 dc
.w
= textw(i
->text
);
207 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
212 drawtext(next
? ">" : NULL
, dc
.norm
);
214 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
219 drawtext(const char *text
, unsigned long col
[ColLast
]) {
221 static char buf
[256];
222 unsigned int len
, olen
;
223 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
225 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
226 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
230 olen
= len
= strlen(text
);
231 if(len
>= sizeof buf
)
232 len
= sizeof buf
- 1;
233 memcpy(buf
, text
, len
);
235 h
= dc
.font
.ascent
+ dc
.font
.descent
;
236 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
238 /* shorten text if necessary */
239 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
250 return; /* too long */
251 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
253 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
255 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
259 emalloc(unsigned int size
) {
260 void *res
= malloc(size
);
263 eprint("fatal: could not malloc() %u bytes\n", size
);
268 eprint(const char *errstr
, ...) {
271 va_start(ap
, errstr
);
272 vfprintf(stderr
, errstr
, ap
);
278 estrdup(const char *str
) {
279 void *res
= strdup(str
);
282 eprint("fatal: could not malloc() %u bytes\n", strlen(str
));
287 getcolor(const char *colstr
) {
288 Colormap cmap
= DefaultColormap(dpy
, screen
);
291 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
292 eprint("error, cannot allocate color '%s'\n", colstr
);
300 for(len
= 1000; len
; len
--) {
301 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
310 initfont(const char *fontstr
) {
311 char *def
, **missing
;
314 if(!fontstr
|| fontstr
[0] == '\0')
315 eprint("error, cannot load font: '%s'\n", fontstr
);
318 XFreeFontSet(dpy
, dc
.font
.set
);
319 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
321 XFreeStringList(missing
);
323 XFontSetExtents
*font_extents
;
324 XFontStruct
**xfonts
;
326 dc
.font
.ascent
= dc
.font
.descent
= 0;
327 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
328 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
329 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
330 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
331 dc
.font
.ascent
= (*xfonts
)->ascent
;
332 if(dc
.font
.descent
< (*xfonts
)->descent
)
333 dc
.font
.descent
= (*xfonts
)->descent
;
339 XFreeFont(dpy
, dc
.font
.xfont
);
340 dc
.font
.xfont
= NULL
;
341 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
342 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
343 eprint("error, cannot load font: '%s'\n", fontstr
);
344 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
345 dc
.font
.descent
= dc
.font
.xfont
->descent
;
347 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
351 kpress(XKeyEvent
* e
) {
359 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
360 if(IsKeypadKey(ksym
)) {
361 if(ksym
== XK_KP_Enter
) {
363 } else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
) {
364 ksym
= (ksym
- XK_KP_0
) + XK_0
;
367 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
368 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
369 || IsPrivateKeypadKey(ksym
))
371 /* first check if a control mask is omitted */
372 if(e
->state
& ControlMask
) {
374 default: /* ignore other control sequences */
401 while(i
>= 0 && text
[i
] == ' ')
403 while(i
>= 0 && text
[i
] != ' ')
411 if(CLEANMASK(e
->state
) & Mod1Mask
) {
436 if(num
&& !iscntrl((int) buf
[0])) {
439 strncat(text
, buf
, sizeof text
);
441 strncpy(text
, buf
, sizeof text
);
458 while(sel
&& sel
->right
)
472 if(!(sel
&& sel
->left
))
475 if(sel
->right
== curr
) {
493 if((e
->state
& ShiftMask
) && text
)
494 fprintf(stdout
, "%s", text
);
496 fprintf(stdout
, "%s", sel
->text
);
498 fprintf(stdout
, "%s", text
);
503 if(!(sel
&& sel
->right
))
514 strncpy(text
, sel
->text
, sizeof text
);
522 match(char *pattern
) {
528 plen
= strlen(pattern
);
531 for(i
= allitems
; i
; i
= i
->next
)
532 if((i
->matched
= !fstrncmp(pattern
, i
->text
, plen
)))
533 j
= appenditem(i
, j
);
534 for(i
= allitems
; i
; i
= i
->next
)
535 if(!i
->matched
&& fstrstr(i
->text
, pattern
))
536 j
= appenditem(i
, j
);
537 curr
= prev
= next
= sel
= item
;
544 unsigned int len
= 0, max
= 0;
548 while(fgets(buf
, sizeof buf
, stdin
)) {
550 if (buf
[len
- 1] == '\n')
557 new = emalloc(sizeof(Item
));
558 new->next
= new->left
= new->right
= NULL
;
572 /* main event loop */
573 while(running
&& !XNextEvent(dpy
, &ev
))
575 default: /* ignore all crap */
581 if(ev
.xexpose
.count
== 0)
588 setup(int x
, int y
, int w
) {
590 XModifierKeymap
*modmap
;
591 XSetWindowAttributes wa
;
593 /* init modifier map */
594 modmap
= XGetModifierMapping(dpy
);
595 for(i
= 0; i
< 8; i
++)
596 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
597 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
598 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
599 numlockmask
= (1 << i
);
601 XFreeModifiermap(modmap
);
604 dc
.norm
[ColBG
] = getcolor(normbg
);
605 dc
.norm
[ColFG
] = getcolor(normfg
);
606 dc
.sel
[ColBG
] = getcolor(selbg
);
607 dc
.sel
[ColFG
] = getcolor(selfg
);
611 wa
.override_redirect
= 1;
612 wa
.background_pixmap
= ParentRelative
;
613 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
614 mw
= w
? w
: DisplayWidth(dpy
, screen
);
615 mh
= dc
.font
.height
+ 2;
617 if(y
== (int)(unsigned int)-1)
618 y
= DisplayHeight(dpy
, screen
) - mh
;
622 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
623 DefaultDepth(dpy
, screen
), CopyFromParent
,
624 DefaultVisual(dpy
, screen
),
625 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
628 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
629 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
630 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
632 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
634 cmdw
= textw(maxname
);
638 promptw
= textw(prompt
);
643 XMapRaised(dpy
, win
);
647 textnw(const char *text
, unsigned int len
) {
651 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
654 return XTextWidth(dc
.font
.xfont
, text
, len
);
658 textw(const char *text
) {
659 return textnw(text
, strlen(text
)) + dc
.font
.height
;
663 main(int argc
, char *argv
[]) {
664 int x
= 0, y
= 0, w
= 0;
667 /* command line args */
668 for(i
= 1; i
< argc
; i
++)
669 if(!strcmp(argv
[i
], "-i")) {
670 fstrncmp
= strncasecmp
;
673 else if(!strcmp(argv
[i
], "-fn")) {
674 if(++i
< argc
) font
= argv
[i
];
676 else if(!strcmp(argv
[i
], "-nb")) {
677 if(++i
< argc
) normbg
= argv
[i
];
679 else if(!strcmp(argv
[i
], "-nf")) {
680 if(++i
< argc
) normfg
= argv
[i
];
682 else if(!strcmp(argv
[i
], "-p")) {
683 if(++i
< argc
) prompt
= argv
[i
];
685 else if(!strcmp(argv
[i
], "-sb")) {
686 if(++i
< argc
) selbg
= argv
[i
];
688 else if(!strcmp(argv
[i
], "-sf")) {
689 if(++i
< argc
) selfg
= argv
[i
];
691 else if(!strcmp(argv
[i
], "-x")) {
692 if(++i
< argc
) x
= atoi(argv
[i
]);
694 else if(!strcmp(argv
[i
], "-y")) {
696 if(!strcmp(argv
[i
], "-0"))
697 y
= (int)(unsigned int)-1;
702 else if(!strcmp(argv
[i
], "-w")) {
703 if(++i
< argc
) w
= atoi(argv
[i
]);
705 else if(!strcmp(argv
[i
], "-v"))
706 eprint("dmenu-"VERSION
", © 2006-2008 dmenu engineers, see LICENSE for details\n");
708 eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
709 " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
710 " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
711 setlocale(LC_CTYPE
, "");
712 dpy
= XOpenDisplay(0);
714 eprint("dmenu: cannot open display\n");
715 screen
= DefaultScreen(dpy
);
716 root
= RootWindow(dpy
, screen
);
718 if(isatty(STDIN_FILENO
)) {
720 running
= grabkeyboard();
722 else { /* prevent keypress loss */
723 running
= grabkeyboard();