Xinqi Bao's Git
5f168941881c50b8642c7430e17f5cfdc61768f7
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))
21 #define MAX(a, b) ((a) > (b) ? (a) : (b))
24 enum { ColFG
, ColBG
, ColLast
};
29 unsigned long norm
[ColLast
];
30 unsigned long sel
[ColLast
];
40 } DC
; /* draw context */
42 typedef struct Item Item
;
45 Item
*next
; /* traverses all items */
46 Item
*left
, *right
; /* traverses items matching current search pattern */
49 /* forward declarations */
50 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
51 static void calcoffsetsh(void);
52 static void calcoffsetsv(void);
53 static char *cistrstr(const char *s
, const char *sub
);
54 static void cleanup(void);
55 static void drawmenu(void);
56 static void drawmenuh(void);
57 static void drawmenuv(void);
58 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
59 static void eprint(const char *errstr
, ...);
60 static unsigned long getcolor(const char *colstr
);
61 static Bool
grabkeyboard(void);
62 static void initfont(const char *fontstr
);
63 static void kpress(XKeyEvent
* e
);
64 static void match(char *pattern
);
65 static void readstdin(void);
66 static void run(void);
67 static void setup(Bool topbar
);
68 static int textnw(const char *text
, unsigned int len
);
69 static int textw(const char *text
);
74 static char *maxname
= NULL
;
75 static char *prompt
= NULL
;
76 static char text
[4096];
78 static int promptw
= 0;
80 static int cursor
= 0;
82 static unsigned int mw
, mh
;
83 static unsigned int numlockmask
= 0;
84 static Bool running
= True
;
87 static Item
*allitems
= NULL
; /* first of all items */
88 static Item
*item
= NULL
; /* first of pattern matching items */
89 static Item
*sel
= NULL
;
90 static Item
*next
= NULL
;
91 static Item
*prev
= NULL
;
92 static Item
*curr
= NULL
;
93 static Window root
, win
;
94 static int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
95 static char *(*fstrstr
)(const char *, const char *) = strstr
;
96 static Bool vlist
= False
;
97 static unsigned int lines
= 5;
98 static void (*calcoffsets
)(void) = calcoffsetsh
;
101 appenditem(Item
*i
, Item
**list
, Item
**last
) {
118 w
= promptw
+ cmdw
+ 2 * spaceitem
;
119 for(next
= curr
; next
; next
=next
->right
) {
120 tw
= textw(next
->text
);
127 w
= promptw
+ cmdw
+ 2 * spaceitem
;
128 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
129 tw
= textw(prev
->left
->text
);
140 static unsigned int w
;
144 w
= (dc
.font
.height
+ 2) * (lines
+ 1);
145 for(next
= curr
; next
; next
=next
->right
) {
146 w
-= dc
.font
.height
+ 2;
150 w
= (dc
.font
.height
+ 2) * (lines
+ 1);
151 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
152 w
-= dc
.font
.height
+ 2;
159 cistrstr(const char *s
, const char *sub
) {
165 if((c
= *sub
++) != 0) {
170 if((csub
= *s
++) == 0)
173 while(tolower(csub
) != c
);
175 while(strncasecmp(s
, sub
, len
) != 0);
186 itm
= allitems
->next
;
187 free(allitems
->text
);
192 XFreeFontSet(dpy
, dc
.font
.set
);
194 XFreeFont(dpy
, dc
.font
.xfont
);
195 XFreePixmap(dpy
, dc
.drawable
);
197 XDestroyWindow(dpy
, win
);
198 XUngrabKeyboard(dpy
, CurrentTime
);
203 XRectangle r
= { dc
.x
, dc
.y
+ 2, 1, dc
.font
.height
- 2 };
205 r
.x
+= textnw(text
, cursor
) + dc
.font
.height
/ 2;
207 XSetForeground(dpy
, dc
.gc
, dc
.norm
[ColFG
]);
208 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
217 drawtext(NULL
, dc
.norm
);
221 drawtext(prompt
, dc
.sel
);
228 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
237 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
246 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
248 /* determine maximum items */
249 for(i
= curr
; i
!= next
; i
=i
->right
) {
250 dc
.w
= textw(i
->text
);
253 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
256 dc
.x
= mw
- spaceitem
;
258 drawtext(next
? ">" : NULL
, dc
.norm
);
267 dc
.y
+= dc
.font
.height
+ 2;
268 /* determine maximum items */
269 for(i
= curr
; i
!= next
; i
=i
->right
) {
270 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
271 dc
.y
+= dc
.font
.height
+ 2;
273 drawtext(NULL
, dc
.norm
);
277 drawtext(const char *text
, unsigned long col
[ColLast
]) {
279 int i
, x
, y
, h
, len
, olen
;
280 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
282 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
283 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
288 y
= dc
.y
+ ((h
+2) / 2) - (h
/ 2) + dc
.font
.ascent
;
290 /* shorten text if necessary */
291 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
294 memcpy(buf
, text
, len
);
296 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
297 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
299 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
301 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
305 eprint(const char *errstr
, ...) {
308 va_start(ap
, errstr
);
309 vfprintf(stderr
, errstr
, ap
);
315 getcolor(const char *colstr
) {
316 Colormap cmap
= DefaultColormap(dpy
, screen
);
319 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
320 eprint("error, cannot allocate color '%s'\n", colstr
);
328 for(len
= 1000; len
; len
--) {
329 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
338 initfont(const char *fontstr
) {
339 char *def
, **missing
;
342 if(!fontstr
|| fontstr
[0] == '\0')
343 eprint("error, cannot load font: '%s'\n", fontstr
);
345 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
347 XFreeStringList(missing
);
349 XFontSetExtents
*font_extents
;
350 XFontStruct
**xfonts
;
352 dc
.font
.ascent
= dc
.font
.descent
= 0;
353 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
354 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
355 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
356 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
357 dc
.font
.ascent
= (*xfonts
)->ascent
;
358 if(dc
.font
.descent
< (*xfonts
)->descent
)
359 dc
.font
.descent
= (*xfonts
)->descent
;
364 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
365 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
366 eprint("error, cannot load font: '%s'\n", fontstr
);
367 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
368 dc
.font
.descent
= dc
.font
.xfont
->descent
;
370 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
374 kpress(XKeyEvent
* e
) {
375 char buf
[sizeof text
];
382 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
383 if(IsKeypadKey(ksym
)) {
384 if(ksym
== XK_KP_Enter
)
386 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
387 ksym
= (ksym
- XK_KP_0
) + XK_0
;
389 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
390 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
391 || IsPrivateKeypadKey(ksym
))
393 /* first check if a control mask is omitted */
394 if(e
->state
& ControlMask
) {
396 default: /* ignore other control sequences */
423 while(i
>= 0 && text
[i
] == ' ')
425 while(i
>= 0 && text
[i
] != ' ')
433 if(CLEANMASK(e
->state
) & Mod1Mask
) {
458 if(!(fp
= (FILE*)popen("sselp", "r")))
459 eprint("dmenu: Could not popen sselp\n");
460 c
= fgets(buf
, sizeof buf
, fp
);
466 if(num
&& buf
[num
-1] == '\n')
473 num
= MIN(num
, sizeof text
- cursor
);
474 if(num
&& !iscntrl((int) buf
[0])) {
475 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
476 memmove(text
+ cursor
, buf
, num
);
483 memmove(text
+ cursor
+ -1, text
+ cursor
, sizeof text
- cursor
);
489 memmove(text
+ cursor
, text
+ cursor
+ 1, sizeof text
- cursor
);
499 while(sel
&& sel
->right
)
514 if(sel
&& sel
->left
){
516 if(sel
->right
== curr
) {
539 if((e
->state
& ShiftMask
) && *text
)
540 fprintf(stdout
, "%s", text
);
542 fprintf(stdout
, "%s", sel
->text
);
544 fprintf(stdout
, "%s", text
);
552 else if(sel
&& sel
->right
) {
565 strncpy(text
, sel
->text
, sizeof text
);
566 cursor
= strlen(text
);
571 cursor
= MIN(cursor
, len
);
572 cursor
= MAX(cursor
, 0);
577 match(char *pattern
) {
579 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
583 plen
= strlen(pattern
);
584 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
585 for(i
= allitems
; i
; i
= i
->next
)
586 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
587 appenditem(i
, &lexact
, &exactend
);
588 else if(!fstrncmp(pattern
, i
->text
, plen
))
589 appenditem(i
, &lprefix
, &prefixend
);
590 else if(fstrstr(i
->text
, pattern
))
591 appenditem(i
, &lsubstr
, &substrend
);
598 itemend
->right
= lprefix
;
599 lprefix
->left
= itemend
;
607 itemend
->right
= lsubstr
;
608 lsubstr
->left
= itemend
;
613 curr
= prev
= next
= sel
= item
;
620 unsigned int len
= 0, max
= 0;
624 while(fgets(buf
, sizeof buf
, stdin
)) {
626 if (buf
[len
- 1] == '\n')
628 if(!(p
= strdup(buf
)))
629 eprint("fatal: could not strdup() %u bytes\n", strlen(buf
));
634 if(!(new = (Item
*)malloc(sizeof(Item
))))
635 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item
));
636 new->next
= new->left
= new->right
= NULL
;
650 /* main event loop */
651 while(running
&& !XNextEvent(dpy
, &ev
))
653 default: /* ignore all crap */
659 if(ev
.xexpose
.count
== 0)
670 XineramaScreenInfo
*info
= NULL
;
672 XModifierKeymap
*modmap
;
673 XSetWindowAttributes wa
;
675 /* init modifier map */
676 modmap
= XGetModifierMapping(dpy
);
677 for(i
= 0; i
< 8; i
++)
678 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
679 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
680 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
681 numlockmask
= (1 << i
);
683 XFreeModifiermap(modmap
);
686 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
687 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
688 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
689 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
693 wa
.override_redirect
= True
;
694 wa
.background_pixmap
= ParentRelative
;
695 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
697 /* menu window geometry */
698 mh
= dc
.font
.height
+ 2;
699 mh
= vlist
? mh
* (lines
+1) : mh
;
701 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
707 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
708 for(i
= 0; i
< n
; i
++)
709 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
713 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
721 y
= topbar
? 0 : DisplayHeight(dpy
, screen
) - mh
;
722 mw
= DisplayWidth(dpy
, screen
);
725 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
726 DefaultDepth(dpy
, screen
), CopyFromParent
,
727 DefaultVisual(dpy
, screen
),
728 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
731 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
732 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
733 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
735 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
737 cmdw
= textw(maxname
);
741 promptw
= textw(prompt
);
746 XMapRaised(dpy
, win
);
750 textnw(const char *text
, unsigned int len
) {
754 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
757 return XTextWidth(dc
.font
.xfont
, text
, len
);
761 textw(const char *text
) {
762 return textnw(text
, strlen(text
)) + dc
.font
.height
;
766 main(int argc
, char *argv
[]) {
770 /* command line args */
771 for(i
= 1; i
< argc
; i
++)
772 if(!strcmp(argv
[i
], "-i")) {
773 fstrncmp
= strncasecmp
;
776 else if(!strcmp(argv
[i
], "-b"))
778 else if(!strcmp(argv
[i
], "-l")) {
780 calcoffsets
= calcoffsetsv
;
781 if(++i
< argc
) lines
= atoi(argv
[i
]);
783 else if(!strcmp(argv
[i
], "-fn")) {
784 if(++i
< argc
) font
= argv
[i
];
786 else if(!strcmp(argv
[i
], "-nb")) {
787 if(++i
< argc
) normbgcolor
= argv
[i
];
789 else if(!strcmp(argv
[i
], "-nf")) {
790 if(++i
< argc
) normfgcolor
= argv
[i
];
792 else if(!strcmp(argv
[i
], "-p")) {
793 if(++i
< argc
) prompt
= argv
[i
];
795 else if(!strcmp(argv
[i
], "-sb")) {
796 if(++i
< argc
) selbgcolor
= argv
[i
];
798 else if(!strcmp(argv
[i
], "-sf")) {
799 if(++i
< argc
) selfgcolor
= argv
[i
];
801 else if(!strcmp(argv
[i
], "-v"))
802 eprint("dmenu-"VERSION
", © 2006-2009 dmenu engineers, see LICENSE for details\n");
804 eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
805 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
806 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
807 fprintf(stderr
, "warning: no locale support\n");
808 if(!(dpy
= XOpenDisplay(NULL
)))
809 eprint("dmenu: cannot open display\n");
810 screen
= DefaultScreen(dpy
);
811 root
= RootWindow(dpy
, screen
);
813 if(isatty(STDIN_FILENO
)) {
815 running
= grabkeyboard();
817 else { /* prevent keypress loss */
818 running
= grabkeyboard();