Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #include <X11/Xutil.h>
14 #include <X11/extensions/Xinerama.h>
18 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
19 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
23 enum { ColFG
, ColBG
, ColLast
};
28 unsigned long norm
[ColLast
];
29 unsigned long sel
[ColLast
];
39 } DC
; /* draw context */
41 typedef struct Item Item
;
44 Item
*next
; /* traverses all items */
45 Item
*left
, *right
; /* traverses items matching current search pattern */
48 /* forward declarations */
49 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
50 static void calcoffsets(void);
51 static char *cistrstr(const char *s
, const char *sub
);
52 static void cleanup(void);
53 static void drawmenu(void);
54 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
55 static void eprint(const char *errstr
, ...);
56 static unsigned long getcolor(const char *colstr
);
57 static Bool
grabkeyboard(void);
58 static void initfont(const char *fontstr
);
59 static void kpress(XKeyEvent
* e
);
60 static void match(char *pattern
);
61 static void readstdin(void);
62 static void run(void);
63 static void setup(Bool topbar
);
64 static int textnw(const char *text
, unsigned int len
);
65 static int textw(const char *text
);
70 static char *maxname
= NULL
;
71 static char *prompt
= NULL
;
72 static char text
[4096];
74 static int promptw
= 0;
77 static unsigned int mw
, mh
;
78 static unsigned int numlockmask
= 0;
79 static Bool running
= True
;
82 static Item
*allitems
= NULL
; /* first of all items */
83 static Item
*item
= NULL
; /* first of pattern matching items */
84 static Item
*sel
= NULL
;
85 static Item
*next
= NULL
;
86 static Item
*prev
= NULL
;
87 static Item
*curr
= NULL
;
88 static Window root
, win
;
89 static int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
90 static char *(*fstrstr
)(const char *, const char *) = strstr
;
93 appenditem(Item
*i
, Item
**list
, Item
**last
) {
110 w
= promptw
+ cmdw
+ 2 * spaceitem
;
111 for(next
= curr
; next
; next
=next
->right
) {
112 tw
= textw(next
->text
);
119 w
= promptw
+ cmdw
+ 2 * spaceitem
;
120 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
121 tw
= textw(prev
->left
->text
);
131 cistrstr(const char *s
, const char *sub
) {
137 if((c
= *sub
++) != 0) {
142 if((csub
= *s
++) == 0)
145 while(tolower(csub
) != c
);
147 while(strncasecmp(s
, sub
, len
) != 0);
158 itm
= allitems
->next
;
159 free(allitems
->text
);
164 XFreeFontSet(dpy
, dc
.font
.set
);
166 XFreeFont(dpy
, dc
.font
.xfont
);
167 XFreePixmap(dpy
, dc
.drawable
);
169 XDestroyWindow(dpy
, win
);
170 XUngrabKeyboard(dpy
, CurrentTime
);
181 drawtext(NULL
, dc
.norm
);
185 drawtext(prompt
, dc
.sel
);
192 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
196 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
198 /* determine maximum items */
199 for(i
= curr
; i
!= next
; i
=i
->right
) {
200 dc
.w
= textw(i
->text
);
203 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
206 dc
.x
= mw
- spaceitem
;
208 drawtext(next
? ">" : NULL
, dc
.norm
);
210 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
215 drawtext(const char *text
, unsigned long col
[ColLast
]) {
217 int i
, x
, y
, h
, len
, olen
;
218 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
220 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
221 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
225 h
= dc
.font
.ascent
+ dc
.font
.descent
;
226 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
228 /* shorten text if necessary */
229 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
232 memcpy(buf
, text
, len
);
234 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
235 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
237 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
239 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
243 eprint(const char *errstr
, ...) {
246 va_start(ap
, errstr
);
247 vfprintf(stderr
, errstr
, ap
);
253 getcolor(const char *colstr
) {
254 Colormap cmap
= DefaultColormap(dpy
, screen
);
257 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
258 eprint("error, cannot allocate color '%s'\n", colstr
);
266 for(len
= 1000; len
; len
--) {
267 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
276 initfont(const char *fontstr
) {
277 char *def
, **missing
;
280 if(!fontstr
|| fontstr
[0] == '\0')
281 eprint("error, cannot load font: '%s'\n", fontstr
);
284 XFreeFontSet(dpy
, dc
.font
.set
);
285 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
287 XFreeStringList(missing
);
289 XFontSetExtents
*font_extents
;
290 XFontStruct
**xfonts
;
292 dc
.font
.ascent
= dc
.font
.descent
= 0;
293 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
294 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
295 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
296 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
297 dc
.font
.ascent
= (*xfonts
)->ascent
;
298 if(dc
.font
.descent
< (*xfonts
)->descent
)
299 dc
.font
.descent
= (*xfonts
)->descent
;
305 XFreeFont(dpy
, dc
.font
.xfont
);
306 dc
.font
.xfont
= NULL
;
307 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
308 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
309 eprint("error, cannot load font: '%s'\n", fontstr
);
310 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
311 dc
.font
.descent
= dc
.font
.xfont
->descent
;
313 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
317 kpress(XKeyEvent
* e
) {
325 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
326 if(IsKeypadKey(ksym
)) {
327 if(ksym
== XK_KP_Enter
)
329 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
330 ksym
= (ksym
- XK_KP_0
) + XK_0
;
332 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
333 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
334 || IsPrivateKeypadKey(ksym
))
336 /* first check if a control mask is omitted */
337 if(e
->state
& ControlMask
) {
339 default: /* ignore other control sequences */
366 while(i
>= 0 && text
[i
] == ' ')
368 while(i
>= 0 && text
[i
] != ' ')
376 if(CLEANMASK(e
->state
) & Mod1Mask
) {
401 if(num
&& !iscntrl((int) buf
[0])) {
404 strncat(text
, buf
, sizeof text
);
406 strncpy(text
, buf
, sizeof text
);
423 while(sel
&& sel
->right
)
437 if(!(sel
&& sel
->left
))
440 if(sel
->right
== curr
) {
458 if((e
->state
& ShiftMask
) && *text
)
459 fprintf(stdout
, "%s", text
);
461 fprintf(stdout
, "%s", sel
->text
);
463 fprintf(stdout
, "%s", text
);
468 if(!(sel
&& sel
->right
))
479 strncpy(text
, sel
->text
, sizeof text
);
487 match(char *pattern
) {
489 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
493 plen
= strlen(pattern
);
494 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
495 for(i
= allitems
; i
; i
= i
->next
)
496 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
497 appenditem(i
, &lexact
, &exactend
);
498 else if(!fstrncmp(pattern
, i
->text
, plen
))
499 appenditem(i
, &lprefix
, &prefixend
);
500 else if(fstrstr(i
->text
, pattern
))
501 appenditem(i
, &lsubstr
, &substrend
);
508 itemend
->right
= lprefix
;
509 lprefix
->left
= itemend
;
517 itemend
->right
= lsubstr
;
518 lsubstr
->left
= itemend
;
523 curr
= prev
= next
= sel
= item
;
530 unsigned int len
= 0, max
= 0;
534 while(fgets(buf
, sizeof buf
, stdin
)) {
536 if (buf
[len
- 1] == '\n')
538 if(!(p
= strdup(buf
)))
539 eprint("fatal: could not strdup() %u bytes\n", strlen(buf
));
544 if((new = (Item
*)malloc(sizeof(Item
))) == NULL
)
545 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item
));
546 new->next
= new->left
= new->right
= NULL
;
560 /* main event loop */
561 while(running
&& !XNextEvent(dpy
, &ev
))
563 default: /* ignore all crap */
569 if(ev
.xexpose
.count
== 0)
580 XineramaScreenInfo
*info
= NULL
;
582 XModifierKeymap
*modmap
;
583 XSetWindowAttributes wa
;
585 /* init modifier map */
586 modmap
= XGetModifierMapping(dpy
);
587 for(i
= 0; i
< 8; i
++)
588 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
589 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
590 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
591 numlockmask
= (1 << i
);
593 XFreeModifiermap(modmap
);
596 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
597 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
598 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
599 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
603 wa
.override_redirect
= 1;
604 wa
.background_pixmap
= ParentRelative
;
605 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
607 /* menu window geometry */
608 mh
= dc
.font
.height
+ 2;
610 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
616 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
617 for(i
= 0; i
< n
; i
++)
618 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
622 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
630 y
= topbar
? 0 : DisplayHeight(dpy
, screen
) - mh
;
631 mw
= DisplayWidth(dpy
, screen
);
634 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
635 DefaultDepth(dpy
, screen
), CopyFromParent
,
636 DefaultVisual(dpy
, screen
),
637 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
640 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
641 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
642 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
644 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
646 cmdw
= textw(maxname
);
650 promptw
= textw(prompt
);
655 XMapRaised(dpy
, win
);
659 textnw(const char *text
, unsigned int len
) {
663 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
666 return XTextWidth(dc
.font
.xfont
, text
, len
);
670 textw(const char *text
) {
671 return textnw(text
, strlen(text
)) + dc
.font
.height
;
675 main(int argc
, char *argv
[]) {
679 /* command line args */
680 for(i
= 1; i
< argc
; i
++)
681 if(!strcmp(argv
[i
], "-i")) {
682 fstrncmp
= strncasecmp
;
685 else if(!strcmp(argv
[i
], "-b"))
687 else if(!strcmp(argv
[i
], "-fn")) {
688 if(++i
< argc
) font
= argv
[i
];
690 else if(!strcmp(argv
[i
], "-nb")) {
691 if(++i
< argc
) normbgcolor
= argv
[i
];
693 else if(!strcmp(argv
[i
], "-nf")) {
694 if(++i
< argc
) normfgcolor
= argv
[i
];
696 else if(!strcmp(argv
[i
], "-p")) {
697 if(++i
< argc
) prompt
= argv
[i
];
699 else if(!strcmp(argv
[i
], "-sb")) {
700 if(++i
< argc
) selbgcolor
= argv
[i
];
702 else if(!strcmp(argv
[i
], "-sf")) {
703 if(++i
< argc
) selfgcolor
= 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] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
709 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
710 if(!XSupportsLocale())
711 fprintf(stderr
, "warning: no locale support\n");
712 if(!(dpy
= XOpenDisplay(0)))
713 eprint("dmenu: cannot open display\n");
714 screen
= DefaultScreen(dpy
);
715 root
= RootWindow(dpy
, screen
);
717 if(isatty(STDIN_FILENO
)) {
719 running
= grabkeyboard();
721 else { /* prevent keypress loss */
722 running
= grabkeyboard();