Xinqi Bao's Git
895bdc503b18b9761ed86823178e9c8faececae7
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))
22 enum { ColFG
, ColBG
, ColLast
};
27 unsigned long norm
[ColLast
];
28 unsigned long sel
[ColLast
];
38 } DC
; /* draw context */
40 typedef struct Item Item
;
43 Item
*next
; /* traverses all items */
44 Item
*left
, *right
; /* traverses items matching current search pattern */
47 /* forward declarations */
48 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
49 static void calcoffsets(void);
50 static char *cistrstr(const char *s
, const char *sub
);
51 static void cleanup(void);
52 static void drawmenu(void);
53 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
54 static void eprint(const char *errstr
, ...);
55 static unsigned long getcolor(const char *colstr
);
56 static Bool
grabkeyboard(void);
57 static void initfont(const char *fontstr
);
58 static void kpress(XKeyEvent
* e
);
59 static void match(char *pattern
);
60 static void readstdin(void);
61 static void run(void);
62 static void setup(Bool topbar
);
63 static int textnw(const char *text
, unsigned int len
);
64 static int textw(const char *text
);
69 static char *maxname
= NULL
;
70 static char *prompt
= NULL
;
71 static char text
[4096];
73 static int promptw
= 0;
76 static unsigned int mw
, mh
;
77 static unsigned int numlockmask
= 0;
78 static Bool running
= True
;
81 static Item
*allitems
= NULL
; /* first of all items */
82 static Item
*item
= NULL
; /* first of pattern matching items */
83 static Item
*sel
= NULL
;
84 static Item
*next
= NULL
;
85 static Item
*prev
= NULL
;
86 static Item
*curr
= NULL
;
87 static Window root
, win
;
88 static int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
89 static char *(*fstrstr
)(const char *, const char *) = strstr
;
92 appenditem(Item
*i
, Item
**list
, Item
**last
) {
109 w
= promptw
+ cmdw
+ 2 * spaceitem
;
110 for(next
= curr
; next
; next
=next
->right
) {
111 tw
= textw(next
->text
);
118 w
= promptw
+ cmdw
+ 2 * spaceitem
;
119 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
120 tw
= textw(prev
->left
->text
);
130 cistrstr(const char *s
, const char *sub
) {
136 if((c
= *sub
++) != 0) {
141 if((csub
= *s
++) == 0)
144 while(tolower(csub
) != c
);
146 while(strncasecmp(s
, sub
, len
) != 0);
157 itm
= allitems
->next
;
158 free(allitems
->text
);
163 XFreeFontSet(dpy
, dc
.font
.set
);
165 XFreeFont(dpy
, dc
.font
.xfont
);
166 XFreePixmap(dpy
, dc
.drawable
);
168 XDestroyWindow(dpy
, win
);
169 XUngrabKeyboard(dpy
, CurrentTime
);
180 drawtext(NULL
, dc
.norm
);
184 drawtext(prompt
, dc
.sel
);
191 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
195 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
197 /* determine maximum items */
198 for(i
= curr
; i
!= next
; i
=i
->right
) {
199 dc
.w
= textw(i
->text
);
202 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
205 dc
.x
= mw
- spaceitem
;
207 drawtext(next
? ">" : NULL
, dc
.norm
);
209 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
214 drawtext(const char *text
, unsigned long col
[ColLast
]) {
216 static char buf
[256];
217 unsigned int 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 olen
= len
= strlen(text
);
226 if(len
>= sizeof buf
)
227 len
= sizeof buf
- 1;
228 memcpy(buf
, text
, len
);
230 h
= dc
.font
.ascent
+ dc
.font
.descent
;
231 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
233 /* shorten text if necessary */
234 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
245 return; /* too long */
246 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
248 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
250 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
254 eprint(const char *errstr
, ...) {
257 va_start(ap
, errstr
);
258 vfprintf(stderr
, errstr
, ap
);
264 getcolor(const char *colstr
) {
265 Colormap cmap
= DefaultColormap(dpy
, screen
);
268 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
269 eprint("error, cannot allocate color '%s'\n", colstr
);
277 for(len
= 1000; len
; len
--) {
278 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
287 initfont(const char *fontstr
) {
288 char *def
, **missing
;
291 if(!fontstr
|| fontstr
[0] == '\0')
292 eprint("error, cannot load font: '%s'\n", fontstr
);
295 XFreeFontSet(dpy
, dc
.font
.set
);
296 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
298 XFreeStringList(missing
);
300 XFontSetExtents
*font_extents
;
301 XFontStruct
**xfonts
;
303 dc
.font
.ascent
= dc
.font
.descent
= 0;
304 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
305 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
306 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
307 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
308 dc
.font
.ascent
= (*xfonts
)->ascent
;
309 if(dc
.font
.descent
< (*xfonts
)->descent
)
310 dc
.font
.descent
= (*xfonts
)->descent
;
316 XFreeFont(dpy
, dc
.font
.xfont
);
317 dc
.font
.xfont
= NULL
;
318 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
319 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
320 eprint("error, cannot load font: '%s'\n", fontstr
);
321 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
322 dc
.font
.descent
= dc
.font
.xfont
->descent
;
324 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
328 kpress(XKeyEvent
* e
) {
336 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, 0);
337 if(IsKeypadKey(ksym
)) {
338 if(ksym
== XK_KP_Enter
)
340 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
341 ksym
= (ksym
- XK_KP_0
) + XK_0
;
343 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
344 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
345 || IsPrivateKeypadKey(ksym
))
347 /* first check if a control mask is omitted */
348 if(e
->state
& ControlMask
) {
350 default: /* ignore other control sequences */
377 while(i
>= 0 && text
[i
] == ' ')
379 while(i
>= 0 && text
[i
] != ' ')
387 if(CLEANMASK(e
->state
) & Mod1Mask
) {
412 if(num
&& !iscntrl((int) buf
[0])) {
415 strncat(text
, buf
, sizeof text
);
417 strncpy(text
, buf
, sizeof text
);
434 while(sel
&& sel
->right
)
448 if(!(sel
&& sel
->left
))
451 if(sel
->right
== curr
) {
469 if((e
->state
& ShiftMask
) && *text
)
470 fprintf(stdout
, "%s", text
);
472 fprintf(stdout
, "%s", sel
->text
);
474 fprintf(stdout
, "%s", text
);
479 if(!(sel
&& sel
->right
))
490 strncpy(text
, sel
->text
, sizeof text
);
498 match(char *pattern
) {
500 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
504 plen
= strlen(pattern
);
505 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
506 for(i
= allitems
; i
; i
= i
->next
)
507 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
508 appenditem(i
, &lexact
, &exactend
);
509 else if(!fstrncmp(pattern
, i
->text
, plen
))
510 appenditem(i
, &lprefix
, &prefixend
);
511 else if(fstrstr(i
->text
, pattern
))
512 appenditem(i
, &lsubstr
, &substrend
);
519 itemend
->right
= lprefix
;
520 lprefix
->left
= itemend
;
528 itemend
->right
= lsubstr
;
529 lsubstr
->left
= itemend
;
534 curr
= prev
= next
= sel
= item
;
541 unsigned int len
= 0, max
= 0;
545 while(fgets(buf
, sizeof buf
, stdin
)) {
547 if (buf
[len
- 1] == '\n')
549 if(!(p
= strdup(buf
)))
550 eprint("fatal: could not strdup() %u bytes\n", strlen(buf
));
555 if((new = (Item
*)malloc(sizeof(Item
))) == NULL
)
556 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item
));
557 new->next
= new->left
= new->right
= NULL
;
571 /* main event loop */
572 while(running
&& !XNextEvent(dpy
, &ev
))
574 default: /* ignore all crap */
580 if(ev
.xexpose
.count
== 0)
591 XineramaScreenInfo
*info
= NULL
;
593 XModifierKeymap
*modmap
;
594 XSetWindowAttributes wa
;
596 /* init modifier map */
597 modmap
= XGetModifierMapping(dpy
);
598 for(i
= 0; i
< 8; i
++)
599 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
600 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
601 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
602 numlockmask
= (1 << i
);
604 XFreeModifiermap(modmap
);
607 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
608 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
609 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
610 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
614 wa
.override_redirect
= 1;
615 wa
.background_pixmap
= ParentRelative
;
616 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
618 /* menu window geometry */
619 mh
= dc
.font
.height
+ 2;
621 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
627 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
628 for(i
= 0; i
< n
; i
++)
629 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
633 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
641 y
= topbar
? 0 : DisplayHeight(dpy
, screen
) - mh
;
642 mw
= DisplayWidth(dpy
, screen
);
645 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
646 DefaultDepth(dpy
, screen
), CopyFromParent
,
647 DefaultVisual(dpy
, screen
),
648 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
651 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
652 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
653 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
655 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
657 cmdw
= textw(maxname
);
661 promptw
= textw(prompt
);
666 XMapRaised(dpy
, win
);
670 textnw(const char *text
, unsigned int len
) {
674 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
677 return XTextWidth(dc
.font
.xfont
, text
, len
);
681 textw(const char *text
) {
682 return textnw(text
, strlen(text
)) + dc
.font
.height
;
686 main(int argc
, char *argv
[]) {
690 /* command line args */
691 for(i
= 1; i
< argc
; i
++)
692 if(!strcmp(argv
[i
], "-i")) {
693 fstrncmp
= strncasecmp
;
696 else if(!strcmp(argv
[i
], "-b"))
698 else if(!strcmp(argv
[i
], "-fn")) {
699 if(++i
< argc
) font
= argv
[i
];
701 else if(!strcmp(argv
[i
], "-nb")) {
702 if(++i
< argc
) normbgcolor
= argv
[i
];
704 else if(!strcmp(argv
[i
], "-nf")) {
705 if(++i
< argc
) normfgcolor
= argv
[i
];
707 else if(!strcmp(argv
[i
], "-p")) {
708 if(++i
< argc
) prompt
= argv
[i
];
710 else if(!strcmp(argv
[i
], "-sb")) {
711 if(++i
< argc
) selbgcolor
= argv
[i
];
713 else if(!strcmp(argv
[i
], "-sf")) {
714 if(++i
< argc
) selfgcolor
= argv
[i
];
716 else if(!strcmp(argv
[i
], "-v"))
717 eprint("dmenu-"VERSION
", © 2006-2008 dmenu engineers, see LICENSE for details\n");
719 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
720 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
721 if(!XSupportsLocale())
722 fprintf(stderr
, "warning: no locale support\n");
723 if(!(dpy
= XOpenDisplay(0)))
724 eprint("dmenu: cannot open display\n");
725 screen
= DefaultScreen(dpy
);
726 root
= RootWindow(dpy
, screen
);
728 if(isatty(STDIN_FILENO
)) {
730 running
= grabkeyboard();
732 else { /* prevent keypress loss */
733 running
= grabkeyboard();