Xinqi Bao's Git
1762dba03ff953e2795dab10ca81f1b169b0a8d8
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 drawcursor(void);
56 static void drawmenu(void);
57 static void drawmenuh(void);
58 static void drawmenuv(void);
59 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
60 static void eprint(const char *errstr
, ...);
61 static unsigned long getcolor(const char *colstr
);
62 static Bool
grabkeyboard(void);
63 static void initfont(const char *fontstr
);
64 static void kpress(XKeyEvent
* e
);
65 static void match(char *pattern
);
66 static void readstdin(void);
67 static void run(void);
68 static void setup(Bool topbar
);
69 static int textnw(const char *text
, unsigned int len
);
70 static int textw(const char *text
);
75 static char *maxname
= NULL
;
76 static char *prompt
= NULL
;
77 static char text
[4096];
79 static int promptw
= 0;
81 static int cursor
= 0;
83 static unsigned int mw
, mh
;
84 static unsigned int numlockmask
= 0;
85 static Bool running
= True
;
88 static Item
*allitems
= NULL
; /* first of all items */
89 static Item
*item
= NULL
; /* first of pattern matching items */
90 static Item
*sel
= NULL
;
91 static Item
*next
= NULL
;
92 static Item
*prev
= NULL
;
93 static Item
*curr
= NULL
;
94 static Window parent
, win
;
95 static int (*fstrncmp
)(const char *, const char *, size_t n
) = strncmp
;
96 static char *(*fstrstr
)(const char *, const char *) = strstr
;
97 static unsigned int lines
= 0;
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
= MIN(textw(next
->text
), mw
/ 3);
125 w
= promptw
+ cmdw
+ 2 * spaceitem
;
126 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
127 tw
= MIN(textw(prev
->left
->text
), mw
/ 3);
136 static unsigned int h
;
140 h
= (dc
.font
.height
+ 2) * (lines
+ 1);
141 for(next
= curr
; next
; next
=next
->right
) {
142 h
-= dc
.font
.height
+ 2;
146 h
= (dc
.font
.height
+ 2) * (lines
+ 1);
147 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
148 h
-= dc
.font
.height
+ 2;
155 cistrstr(const char *s
, const char *sub
) {
161 if((c
= *sub
++) != '\0') {
166 if((csub
= *s
++) == '\0')
169 while(tolower(csub
) != c
);
171 while(strncasecmp(s
, sub
, len
) != 0);
182 itm
= allitems
->next
;
183 free(allitems
->text
);
188 XFreeFontSet(dpy
, dc
.font
.set
);
190 XFreeFont(dpy
, dc
.font
.xfont
);
191 XFreePixmap(dpy
, dc
.drawable
);
193 XDestroyWindow(dpy
, win
);
194 XUngrabKeyboard(dpy
, CurrentTime
);
199 XRectangle r
= { dc
.x
, dc
.y
+ 2, 1, dc
.font
.height
- 2 };
201 r
.x
+= textnw(text
, cursor
) + dc
.font
.height
/ 2;
203 XSetForeground(dpy
, dc
.gc
, dc
.norm
[ColFG
]);
204 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
213 drawtext(NULL
, dc
.norm
);
217 drawtext(prompt
, dc
.sel
);
222 if(cmdw
&& item
&& lines
== 0)
224 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
232 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
242 drawtext(curr
->left
? "<" : NULL
, dc
.norm
);
244 for(i
= curr
; i
!= next
; i
=i
->right
) {
245 dc
.w
= MIN(textw(i
->text
), mw
/ 3);
246 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
251 drawtext(next
? ">" : NULL
, dc
.norm
);
260 dc
.y
+= dc
.font
.height
+ 2;
261 for(i
= curr
; i
!= next
; i
=i
->right
) {
262 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
263 dc
.y
+= dc
.font
.height
+ 2;
265 drawtext(NULL
, dc
.norm
);
269 drawtext(const char *text
, unsigned long col
[ColLast
]) {
271 int i
, x
, y
, h
, len
, olen
;
272 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
274 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
275 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
280 y
= dc
.y
+ ((h
+2) / 2) - (h
/ 2) + dc
.font
.ascent
;
282 /* shorten text if necessary */
283 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
286 memcpy(buf
, text
, len
);
288 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
289 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
291 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
293 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
297 eprint(const char *errstr
, ...) {
300 va_start(ap
, errstr
);
301 vfprintf(stderr
, errstr
, ap
);
307 getcolor(const char *colstr
) {
308 Colormap cmap
= DefaultColormap(dpy
, screen
);
311 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
312 eprint("error, cannot allocate color '%s'\n", colstr
);
320 for(len
= 1000; len
; len
--) {
321 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
330 initfont(const char *fontstr
) {
331 char *def
, **missing
;
334 if(!fontstr
|| fontstr
[0] == '\0')
335 eprint("error, cannot load font: '%s'\n", fontstr
);
337 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
339 XFreeStringList(missing
);
341 XFontStruct
**xfonts
;
343 dc
.font
.ascent
= dc
.font
.descent
= 0;
344 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
345 for(i
= 0; i
< n
; i
++) {
346 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
347 dc
.font
.descent
= MAX(dc
.font
.descent
, (*xfonts
)->descent
);
352 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
353 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
354 eprint("error, cannot load font: '%s'\n", fontstr
);
355 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
356 dc
.font
.descent
= dc
.font
.xfont
->descent
;
358 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
362 kpress(XKeyEvent
* e
) {
363 char buf
[sizeof text
];
370 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
371 if(IsKeypadKey(ksym
)) {
372 if(ksym
== XK_KP_Enter
)
374 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
375 ksym
= (ksym
- XK_KP_0
) + XK_0
;
377 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
378 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
379 || IsPrivateKeypadKey(ksym
))
381 /* first check if a control mask is omitted */
382 if(e
->state
& ControlMask
) {
384 default: /* ignore other control sequences */
412 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
420 while(i
-- > 0 && text
[i
] == ' ');
421 while(i
-- > 0 && text
[i
] != ' ');
422 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
429 if(CLEANMASK(e
->state
) & Mod1Mask
) {
454 if(!(fp
= (FILE*)popen("sselp", "r")))
455 eprint("dmenu: Could not popen sselp\n");
456 s
= fgets(buf
, sizeof buf
, fp
);
462 if(num
&& buf
[num
-1] == '\n')
469 num
= MIN(num
, sizeof text
- cursor
);
470 if(num
&& !iscntrl((int) buf
[0])) {
471 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
472 memcpy(text
+ cursor
, buf
, num
);
479 memmove(text
+ cursor
- 1, text
+ cursor
, sizeof text
- cursor
+ 1);
485 memmove(text
+ cursor
, text
+ cursor
+ 1, sizeof text
- cursor
);
497 while(sel
&& sel
->right
)
514 if(sel
&& sel
->left
){
516 if(sel
->right
== curr
) {
539 if((e
->state
& ShiftMask
) || !sel
)
540 fprintf(stdout
, "%s", text
);
542 fprintf(stdout
, "%s", sel
->text
);
550 else if(sel
&& sel
->right
) {
563 strncpy(text
, sel
->text
, sizeof text
);
564 cursor
= strlen(text
);
572 match(char *pattern
) {
574 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
578 plen
= strlen(pattern
);
579 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
580 for(i
= allitems
; i
; i
= i
->next
)
581 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
582 appenditem(i
, &lexact
, &exactend
);
583 else if(!fstrncmp(pattern
, i
->text
, plen
))
584 appenditem(i
, &lprefix
, &prefixend
);
585 else if(fstrstr(i
->text
, pattern
))
586 appenditem(i
, &lsubstr
, &substrend
);
593 itemend
->right
= lprefix
;
594 lprefix
->left
= itemend
;
602 itemend
->right
= lsubstr
;
603 lsubstr
->left
= itemend
;
608 curr
= prev
= next
= sel
= item
;
614 char *p
, buf
[sizeof text
];
615 unsigned int len
= 0, max
= 0;
619 while(fgets(buf
, sizeof buf
, stdin
)) {
621 if(buf
[len
-1] == '\n')
623 if(!(p
= strdup(buf
)))
624 eprint("fatal: could not strdup() %u bytes\n", len
);
625 if(max
< len
|| !maxname
) {
629 if(!(new = (Item
*)malloc(sizeof(Item
))))
630 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item
));
631 new->next
= new->left
= new->right
= NULL
;
645 /* main event loop */
646 while(running
&& !XNextEvent(dpy
, &ev
))
648 default: /* ignore all crap */
654 if(ev
.xexpose
.count
== 0)
657 case VisibilityNotify
:
658 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
659 XRaiseWindow(dpy
, win
);
669 XineramaScreenInfo
*info
= NULL
;
671 XModifierKeymap
*modmap
;
672 XSetWindowAttributes wa
;
673 XWindowAttributes pwa
;
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
| VisibilityChangeMask
;
697 /* menu window geometry */
698 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
700 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
706 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
707 for(i
= 0; i
< n
; i
++)
708 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
712 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
719 XGetWindowAttributes(dpy
, parent
, &pwa
);
721 y
= topbar
? 0 : pwa
.height
- mh
;
725 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
726 DefaultDepth(dpy
, screen
), CopyFromParent
,
727 DefaultVisual(dpy
, screen
),
728 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
731 dc
.drawable
= XCreatePixmap(dpy
, parent
, mw
, mh
, DefaultDepth(dpy
, screen
));
732 dc
.gc
= XCreateGC(dpy
, parent
, 0, NULL
);
733 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
735 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
737 cmdw
= MIN(textw(maxname
), mw
/ 3);
739 promptw
= MIN(textw(prompt
), mw
/ 5);
742 XMapRaised(dpy
, win
);
746 textnw(const char *text
, unsigned int len
) {
750 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
753 return XTextWidth(dc
.font
.xfont
, text
, len
);
757 textw(const char *text
) {
758 return textnw(text
, strlen(text
)) + dc
.font
.height
;
762 main(int argc
, char *argv
[]) {
766 /* command line args */
767 for(i
= 1; i
< argc
; i
++)
768 if(!strcmp(argv
[i
], "-i")) {
769 fstrncmp
= strncasecmp
;
772 else if(!strcmp(argv
[i
], "-b"))
774 else if(!strcmp(argv
[i
], "-e")) {
775 if(++i
< argc
) parent
= atoi(argv
[i
]);
777 else if(!strcmp(argv
[i
], "-l")) {
778 calcoffsets
= calcoffsetsv
;
779 if(++i
< argc
) lines
= atoi(argv
[i
]);
781 else if(!strcmp(argv
[i
], "-fn")) {
782 if(++i
< argc
) font
= argv
[i
];
784 else if(!strcmp(argv
[i
], "-nb")) {
785 if(++i
< argc
) normbgcolor
= argv
[i
];
787 else if(!strcmp(argv
[i
], "-nf")) {
788 if(++i
< argc
) normfgcolor
= argv
[i
];
790 else if(!strcmp(argv
[i
], "-p")) {
791 if(++i
< argc
) prompt
= argv
[i
];
793 else if(!strcmp(argv
[i
], "-sb")) {
794 if(++i
< argc
) selbgcolor
= argv
[i
];
796 else if(!strcmp(argv
[i
], "-sf")) {
797 if(++i
< argc
) selfgcolor
= argv
[i
];
799 else if(!strcmp(argv
[i
], "-v"))
800 eprint("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
802 eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
803 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
804 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
805 fprintf(stderr
, "warning: no locale support\n");
806 if(!(dpy
= XOpenDisplay(NULL
)))
807 eprint("dmenu: cannot open display\n");
808 screen
= DefaultScreen(dpy
);
810 parent
= RootWindow(dpy
, screen
);
813 running
= grabkeyboard();