Xinqi Bao's Git
be9b41b5e0c8615d69941f28e45cb86083f879ee
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 drawmenuh(void);
56 static void drawmenuv(void);
57 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
58 static void eprint(const char *errstr
, ...);
59 static unsigned long getcolor(const char *colstr
);
60 static Bool
grabkeyboard(void);
61 static void initfont(const char *fontstr
);
62 static void kpress(XKeyEvent
* e
);
63 static void match(char *pattern
);
64 static void readstdin(void);
65 static void run(void);
66 static void setup(Bool topbar
);
67 static int textnw(const char *text
, unsigned int len
);
68 static int textw(const char *text
);
73 static char *maxname
= NULL
;
74 static char *prompt
= NULL
;
75 static char text
[4096];
77 static int promptw
= 0;
79 static int cursor
= 0;
81 static unsigned int mw
, mh
;
82 static unsigned int numlockmask
= 0;
83 static Bool running
= True
;
86 static Item
*allitems
= NULL
; /* first of all items */
87 static Item
*item
= NULL
; /* first of pattern matching items */
88 static Item
*sel
= NULL
;
89 static Item
*next
= NULL
;
90 static Item
*prev
= NULL
;
91 static Item
*curr
= NULL
;
92 static Window root
, win
;
93 static int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
94 static char *(*fstrstr
)(const char *, const char *) = strstr
;
95 static Bool vlist
= False
;
96 static unsigned int lines
= 5;
97 static void (*calcoffsets
)(void) = calcoffsetsh
;
98 static void (*drawmenu
)(void) = drawmenuh
;
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
.h
- 4 };
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);
219 drawtext(NULL
, dc
.norm
);
223 drawtext(prompt
, dc
.sel
);
230 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
235 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
237 /* determine maximum items */
238 for(i
= curr
; i
!= next
; i
=i
->right
) {
239 dc
.w
= textw(i
->text
);
242 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
245 dc
.x
= mw
- spaceitem
;
247 drawtext(next
? ">" : NULL
, dc
.norm
);
249 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
261 drawtext(NULL
, dc
.norm
);
265 drawtext(prompt
, dc
.sel
);
270 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
274 dc
.y
+= dc
.font
.height
+ 2;
275 /* determine maximum items */
276 for(i
= curr
; i
!= next
; i
=i
->right
) {
277 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
278 dc
.y
+= dc
.font
.height
+ 2;
280 drawtext(NULL
, dc
.norm
);
282 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
287 drawtext(const char *text
, unsigned long col
[ColLast
]) {
289 int i
, x
, y
, h
, len
, olen
;
290 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
292 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
293 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
298 y
= dc
.y
+ ((h
+2) / 2) - (h
/ 2) + dc
.font
.ascent
;
300 /* shorten text if necessary */
301 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
304 memcpy(buf
, text
, len
);
306 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
307 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
309 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
311 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
315 eprint(const char *errstr
, ...) {
318 va_start(ap
, errstr
);
319 vfprintf(stderr
, errstr
, ap
);
325 getcolor(const char *colstr
) {
326 Colormap cmap
= DefaultColormap(dpy
, screen
);
329 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
330 eprint("error, cannot allocate color '%s'\n", colstr
);
338 for(len
= 1000; len
; len
--) {
339 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
348 initfont(const char *fontstr
) {
349 char *def
, **missing
;
352 if(!fontstr
|| fontstr
[0] == '\0')
353 eprint("error, cannot load font: '%s'\n", fontstr
);
355 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
357 XFreeStringList(missing
);
359 XFontSetExtents
*font_extents
;
360 XFontStruct
**xfonts
;
362 dc
.font
.ascent
= dc
.font
.descent
= 0;
363 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
364 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
365 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
366 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
367 dc
.font
.ascent
= (*xfonts
)->ascent
;
368 if(dc
.font
.descent
< (*xfonts
)->descent
)
369 dc
.font
.descent
= (*xfonts
)->descent
;
374 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
375 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
376 eprint("error, cannot load font: '%s'\n", fontstr
);
377 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
378 dc
.font
.descent
= dc
.font
.xfont
->descent
;
380 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
384 kpress(XKeyEvent
* e
) {
392 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
393 if(IsKeypadKey(ksym
)) {
394 if(ksym
== XK_KP_Enter
)
396 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
397 ksym
= (ksym
- XK_KP_0
) + XK_0
;
399 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
400 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
401 || IsPrivateKeypadKey(ksym
))
403 /* first check if a control mask is omitted */
404 if(e
->state
& ControlMask
) {
406 default: /* ignore other control sequences */
433 while(i
>= 0 && text
[i
] == ' ')
435 while(i
>= 0 && text
[i
] != ' ')
443 if(CLEANMASK(e
->state
) & Mod1Mask
) {
468 if(!(fp
= (FILE*)popen("sselp", "r")))
469 fprintf(stderr
, "dmenu: Could not popen sselp\n");
470 c
= fgets(text
+ len
, sizeof(text
) - len
, fp
);
476 if(len
&& text
[len
-1] == '\n')
485 if(num
&& !iscntrl((int) buf
[0])) {
487 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
);
488 strncpy(text
+ cursor
, buf
, sizeof text
- cursor
);
495 memmove(text
+ cursor
+ -1, text
+ cursor
, sizeof text
- cursor
);
507 while(sel
&& sel
->right
)
522 if(sel
&& sel
->left
){
524 if(sel
->right
== curr
) {
547 if((e
->state
& ShiftMask
) && *text
)
548 fprintf(stdout
, "%s", text
);
550 fprintf(stdout
, "%s", sel
->text
);
552 fprintf(stdout
, "%s", text
);
560 else if(sel
&& sel
->right
) {
573 strncpy(text
, sel
->text
, sizeof text
);
574 cursor
= strlen(text
);
579 cursor
= MIN(cursor
, len
);
580 cursor
= MAX(cursor
, 0);
585 match(char *pattern
) {
587 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
591 plen
= strlen(pattern
);
592 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
593 for(i
= allitems
; i
; i
= i
->next
)
594 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
595 appenditem(i
, &lexact
, &exactend
);
596 else if(!fstrncmp(pattern
, i
->text
, plen
))
597 appenditem(i
, &lprefix
, &prefixend
);
598 else if(fstrstr(i
->text
, pattern
))
599 appenditem(i
, &lsubstr
, &substrend
);
606 itemend
->right
= lprefix
;
607 lprefix
->left
= itemend
;
615 itemend
->right
= lsubstr
;
616 lsubstr
->left
= itemend
;
621 curr
= prev
= next
= sel
= item
;
628 unsigned int len
= 0, max
= 0;
632 while(fgets(buf
, sizeof buf
, stdin
)) {
634 if (buf
[len
- 1] == '\n')
636 if(!(p
= strdup(buf
)))
637 eprint("fatal: could not strdup() %u bytes\n", strlen(buf
));
642 if(!(new = (Item
*)malloc(sizeof(Item
))))
643 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item
));
644 new->next
= new->left
= new->right
= NULL
;
658 /* main event loop */
659 while(running
&& !XNextEvent(dpy
, &ev
))
661 default: /* ignore all crap */
667 if(ev
.xexpose
.count
== 0)
678 XineramaScreenInfo
*info
= NULL
;
680 XModifierKeymap
*modmap
;
681 XSetWindowAttributes wa
;
683 /* init modifier map */
684 modmap
= XGetModifierMapping(dpy
);
685 for(i
= 0; i
< 8; i
++)
686 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
687 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
688 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
689 numlockmask
= (1 << i
);
691 XFreeModifiermap(modmap
);
694 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
695 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
696 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
697 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
701 wa
.override_redirect
= True
;
702 wa
.background_pixmap
= ParentRelative
;
703 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
705 /* menu window geometry */
706 mh
= dc
.font
.height
+ 2;
707 mh
= vlist
? mh
* (lines
+1) : mh
;
709 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
715 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
716 for(i
= 0; i
< n
; i
++)
717 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
721 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
729 y
= topbar
? 0 : DisplayHeight(dpy
, screen
) - mh
;
730 mw
= DisplayWidth(dpy
, screen
);
733 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
734 DefaultDepth(dpy
, screen
), CopyFromParent
,
735 DefaultVisual(dpy
, screen
),
736 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
739 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
740 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
741 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
743 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
745 cmdw
= textw(maxname
);
749 promptw
= textw(prompt
);
754 XMapRaised(dpy
, win
);
758 textnw(const char *text
, unsigned int len
) {
762 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
765 return XTextWidth(dc
.font
.xfont
, text
, len
);
769 textw(const char *text
) {
770 return textnw(text
, strlen(text
)) + dc
.font
.height
;
774 main(int argc
, char *argv
[]) {
778 /* command line args */
779 for(i
= 1; i
< argc
; i
++)
780 if(!strcmp(argv
[i
], "-i")) {
781 fstrncmp
= strncasecmp
;
784 else if(!strcmp(argv
[i
], "-b"))
786 else if(!strcmp(argv
[i
], "-l")) {
788 calcoffsets
= calcoffsetsv
;
789 drawmenu
= drawmenuv
;
790 if(++i
< argc
) lines
+= atoi(argv
[i
]);
792 else if(!strcmp(argv
[i
], "-fn")) {
793 if(++i
< argc
) font
= argv
[i
];
795 else if(!strcmp(argv
[i
], "-nb")) {
796 if(++i
< argc
) normbgcolor
= argv
[i
];
798 else if(!strcmp(argv
[i
], "-nf")) {
799 if(++i
< argc
) normfgcolor
= argv
[i
];
801 else if(!strcmp(argv
[i
], "-p")) {
802 if(++i
< argc
) prompt
= argv
[i
];
804 else if(!strcmp(argv
[i
], "-sb")) {
805 if(++i
< argc
) selbgcolor
= argv
[i
];
807 else if(!strcmp(argv
[i
], "-sf")) {
808 if(++i
< argc
) selfgcolor
= argv
[i
];
810 else if(!strcmp(argv
[i
], "-v"))
811 eprint("dmenu-"VERSION
", © 2006-2009 dmenu engineers, see LICENSE for details\n");
813 eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
814 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
815 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
816 fprintf(stderr
, "warning: no locale support\n");
817 if(!(dpy
= XOpenDisplay(NULL
)))
818 eprint("dmenu: cannot open display\n");
819 screen
= DefaultScreen(dpy
);
820 root
= RootWindow(dpy
, screen
);
822 if(isatty(STDIN_FILENO
)) {
824 running
= grabkeyboard();
826 else { /* prevent keypress loss */
827 running
= grabkeyboard();