Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
16 #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
17 * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
18 #define MIN(a,b) ((a) < (b) ? (a) : (b))
19 #define MAX(a,b) ((a) > (b) ? (a) : (b))
21 typedef struct Item Item
;
27 static void appenditem(Item
*item
, Item
**list
, Item
**last
);
28 static void calcoffsets(void);
29 static char *cistrstr(const char *s
, const char *sub
);
30 static void drawmenu(void);
31 static void grabkeyboard(void);
32 static void insert(const char *str
, ssize_t n
);
33 static void keypress(XKeyEvent
*ev
);
34 static void match(void);
35 static size_t nextrune(int inc
);
36 static void paste(void);
37 static void readstdin(void);
38 static void run(void);
39 static void setup(void);
40 static void usage(void);
42 static char text
[BUFSIZ
] = "";
43 static int bh
, mw
, mh
;
44 static int inputw
, promptw
;
45 static size_t cursor
= 0;
46 static const char *font
= NULL
;
47 static const char *prompt
= NULL
;
48 static const char *normbgcolor
= "#cccccc";
49 static const char *normfgcolor
= "#000000";
50 static const char *selbgcolor
= "#0066ff";
51 static const char *selfgcolor
= "#ffffff";
52 static unsigned int lines
= 0;
53 static unsigned long normcol
[ColLast
];
54 static unsigned long selcol
[ColLast
];
56 static Bool topbar
= True
;
58 static Item
*items
= NULL
;
59 static Item
*matches
, *matchend
;
60 static Item
*prev
, *curr
, *next
, *sel
;
64 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
65 static char *(*fstrstr
)(const char *, const char *) = strstr
;
68 main(int argc
, char *argv
[]) {
72 for(i
= 1; i
< argc
; i
++)
73 /* these options take no arguments */
74 if(!strcmp(argv
[i
], "-v")) { /* prints version information */
75 puts("dmenu-"VERSION
", © 2006-2011 dmenu engineers, see LICENSE for details");
78 else if(!strcmp(argv
[i
], "-b")) /* appears at the bottom of the screen */
80 else if(!strcmp(argv
[i
], "-f")) /* grabs keyboard before reading stdin */
82 else if(!strcmp(argv
[i
], "-i")) { /* case-insensitive item matching */
83 fstrncmp
= strncasecmp
;
88 /* these options take one argument */
89 else if(!strcmp(argv
[i
], "-l")) /* number of lines in vertical list */
90 lines
= atoi(argv
[++i
]);
91 else if(!strcmp(argv
[i
], "-p")) /* adds prompt to left of input field */
93 else if(!strcmp(argv
[i
], "-fn")) /* font or font set */
95 else if(!strcmp(argv
[i
], "-nb")) /* normal background color */
96 normbgcolor
= argv
[++i
];
97 else if(!strcmp(argv
[i
], "-nf")) /* normal foreground color */
98 normfgcolor
= argv
[++i
];
99 else if(!strcmp(argv
[i
], "-sb")) /* selected background color */
100 selbgcolor
= argv
[++i
];
101 else if(!strcmp(argv
[i
], "-sf")) /* selected foreground color */
102 selfgcolor
= argv
[++i
];
120 return EXIT_FAILURE
; /* unreachable */
124 appenditem(Item
*item
, Item
**list
, Item
**last
) {
126 (*last
)->right
= item
;
142 n
= mw
- (promptw
+ inputw
+ textw(dc
, "<") + textw(dc
, ">"));
143 /* calculate which items will begin the next page and previous page */
144 for(i
= 0, next
= curr
; next
; next
= next
->right
)
145 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, next
->text
), n
)) > n
)
147 for(i
= 0, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
148 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, prev
->left
->text
), n
)) > n
)
153 cistrstr(const char *s
, const char *sub
) {
156 for(len
= strlen(sub
); *s
; s
++)
157 if(!strncasecmp(s
, sub
, len
))
170 drawrect(dc
, 0, 0, mw
, mh
, True
, BG(dc
, normcol
));
174 drawtext(dc
, prompt
, selcol
);
177 /* draw input field */
178 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
179 drawtext(dc
, text
, normcol
);
180 if((curpos
= textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
181 drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, True
, FG(dc
, normcol
));
184 /* draw vertical list */
186 for(item
= curr
; item
!= next
; item
= item
->right
) {
188 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
192 /* draw horizontal list */
194 dc
->w
= textw(dc
, "<");
196 drawtext(dc
, "<", normcol
);
197 for(item
= curr
; item
!= next
; item
= item
->right
) {
199 dc
->w
= MIN(textw(dc
, item
->text
), mw
- dc
->x
- textw(dc
, ">"));
200 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
202 dc
->w
= textw(dc
, ">");
205 drawtext(dc
, ">", normcol
);
207 mapdc(dc
, win
, mw
, mh
);
214 /* try to grab keyboard, we may have to wait for another process to ungrab */
215 for(i
= 0; i
< 1000; i
++) {
216 if(XGrabKeyboard(dc
->dpy
, DefaultRootWindow(dc
->dpy
), True
,
217 GrabModeAsync
, GrabModeAsync
, CurrentTime
) == GrabSuccess
)
221 eprintf("cannot grab keyboard\n");
225 insert(const char *str
, ssize_t n
) {
226 if(strlen(text
) + n
> sizeof text
- 1)
228 /* move existing text out of the way, insert new text, and update cursor */
229 memmove(&text
[cursor
+ n
], &text
[cursor
], sizeof text
- cursor
- MAX(n
, 0));
231 memcpy(&text
[cursor
], str
, n
);
237 keypress(XKeyEvent
*ev
) {
240 KeySym ksym
= NoSymbol
;
243 len
= XmbLookupString(xic
, ev
, buf
, sizeof buf
, &ksym
, &status
);
244 if(status
== XBufferOverflow
)
246 if(ev
->state
& ControlMask
) {
249 XConvertCase(ksym
, &lower
, &upper
);
251 case XK_a
: ksym
= XK_Home
; break;
252 case XK_b
: ksym
= XK_Left
; break;
253 case XK_c
: ksym
= XK_Escape
; break;
254 case XK_d
: ksym
= XK_Delete
; break;
255 case XK_e
: ksym
= XK_End
; break;
256 case XK_f
: ksym
= XK_Right
; break;
257 case XK_h
: ksym
= XK_BackSpace
; break;
258 case XK_i
: ksym
= XK_Tab
; break;
259 case XK_j
: ksym
= XK_Return
; break;
260 case XK_m
: ksym
= XK_Return
; break;
261 case XK_n
: ksym
= XK_Up
; break;
262 case XK_p
: ksym
= XK_Down
; break;
264 case XK_k
: /* delete right */
268 case XK_u
: /* delete left */
269 insert(NULL
, 0 - cursor
);
271 case XK_w
: /* delete word */
272 while(cursor
> 0 && text
[nextrune(-1)] == ' ')
273 insert(NULL
, nextrune(-1) - cursor
);
274 while(cursor
> 0 && text
[nextrune(-1)] != ' ')
275 insert(NULL
, nextrune(-1) - cursor
);
277 case XK_y
: /* paste selection */
278 XConvertSelection(dc
->dpy
, XA_PRIMARY
, utf8
, utf8
, win
, CurrentTime
);
290 if(text
[cursor
] == '\0')
292 cursor
= nextrune(+1);
297 insert(NULL
, nextrune(-1) - cursor
);
300 if(text
[cursor
] != '\0') {
301 cursor
= strlen(text
);
305 /* jump to end of list and position items in reverse */
310 while(next
&& (curr
= curr
->right
))
322 sel
= curr
= matches
;
326 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
327 cursor
= nextrune(-1);
332 if(sel
&& sel
->left
&& (sel
= sel
->left
)->right
== curr
) {
351 puts((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
);
354 if(text
[cursor
] != '\0') {
355 cursor
= nextrune(+1);
360 if(sel
&& sel
->right
&& (sel
= sel
->right
) == next
) {
368 strncpy(text
, sel
->text
, sizeof text
);
369 cursor
= strlen(text
);
378 static char **tokv
= NULL
;
381 char buf
[sizeof text
], *s
;
384 Item
*item
, *lprefix
, *lsubstr
, *prefixend
, *substrend
;
387 /* separate input text into tokens to be matched individually */
388 for(s
= strtok(buf
, " "); s
; tokv
[tokc
-1] = s
, s
= strtok(NULL
, " "))
389 if(++tokc
> tokn
&& !(tokv
= realloc(tokv
, ++tokn
* sizeof *tokv
)))
390 eprintf("cannot realloc %u bytes\n", tokn
* sizeof *tokv
);
391 len
= tokc
? strlen(tokv
[0]) : 0;
393 matches
= lprefix
= lsubstr
= matchend
= prefixend
= substrend
= NULL
;
394 for(item
= items
; item
&& item
->text
; item
++) {
395 for(i
= 0; i
< tokc
; i
++)
396 if(!fstrstr(item
->text
, tokv
[i
]))
398 if(i
!= tokc
) /* not all tokens match */
400 /* exact matches go first, then prefixes, then substrings */
401 if(!tokc
|| !fstrncmp(tokv
[0], item
->text
, len
+1))
402 appenditem(item
, &matches
, &matchend
);
403 else if(!fstrncmp(tokv
[0], item
->text
, len
))
404 appenditem(item
, &lprefix
, &prefixend
);
406 appenditem(item
, &lsubstr
, &substrend
);
410 matchend
->right
= lprefix
;
411 lprefix
->left
= matchend
;
415 matchend
= prefixend
;
419 matchend
->right
= lsubstr
;
420 lsubstr
->left
= matchend
;
424 matchend
= substrend
;
426 curr
= sel
= matches
;
434 /* return location of next utf8 rune in the given direction (+1 or -1) */
435 for(n
= cursor
+ inc
; n
+ inc
>= 0 && (text
[n
] & 0xc0) == 0x80; n
+= inc
);
446 /* we have been given the current selection, now insert it into input */
447 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, (sizeof text
/ 4) + 1, False
,
448 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
449 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: (ssize_t
)strlen(p
));
456 char buf
[sizeof text
], *p
, *maxstr
= NULL
;
457 size_t i
, max
= 0, size
= 0;
459 /* read each line from stdin and add it to the item list */
460 for(i
= 0; fgets(buf
, sizeof buf
, stdin
); i
++) {
461 if(i
+1 >= size
/ sizeof *items
)
462 if(!(items
= realloc(items
, (size
+= BUFSIZ
))))
463 eprintf("cannot realloc %u bytes:", size
);
464 if((p
= strchr(buf
, '\n')))
466 if(!(items
[i
].text
= strdup(buf
)))
467 eprintf("cannot strdup %u bytes:", strlen(buf
)+1);
468 if(strlen(items
[i
].text
) > max
)
469 max
= strlen(maxstr
= items
[i
].text
);
472 items
[i
].text
= NULL
;
473 inputw
= maxstr
? textw(dc
, maxstr
) : 0;
474 lines
= MIN(lines
, i
);
481 while(!XNextEvent(dc
->dpy
, &ev
)) {
482 if(XFilterEvent(&ev
, win
))
486 if(ev
.xexpose
.count
== 0)
487 mapdc(dc
, win
, mw
, mh
);
492 case SelectionNotify
:
493 if(ev
.xselection
.property
== utf8
)
496 case VisibilityNotify
:
497 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
498 XRaiseWindow(dc
->dpy
, win
);
506 int x
, y
, screen
= DefaultScreen(dc
->dpy
);
507 Window root
= RootWindow(dc
->dpy
, screen
);
508 XSetWindowAttributes swa
;
512 XineramaScreenInfo
*info
;
515 normcol
[ColBG
] = getcolor(dc
, normbgcolor
);
516 normcol
[ColFG
] = getcolor(dc
, normfgcolor
);
517 selcol
[ColBG
] = getcolor(dc
, selbgcolor
);
518 selcol
[ColFG
] = getcolor(dc
, selfgcolor
);
520 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
522 /* calculate menu geometry */
523 bh
= dc
->font
.height
+ 2;
524 lines
= MAX(lines
, 0);
525 mh
= (lines
+ 1) * bh
;
527 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
528 int a
, j
, di
, i
= 0, area
= 0;
530 Window w
, pw
, dw
, *dws
;
531 XWindowAttributes wa
;
533 XGetInputFocus(dc
->dpy
, &w
, &di
);
534 if(w
!= root
&& w
!= PointerRoot
&& w
!= None
) {
535 /* find top-level window containing current input focus */
537 if(XQueryTree(dc
->dpy
, (pw
= w
), &dw
, &w
, &dws
, &du
) && dws
)
539 } while(w
!= root
&& w
!= pw
);
540 /* find xinerama screen with which the window intersects most */
541 if(XGetWindowAttributes(dc
->dpy
, pw
, &wa
))
542 for(j
= 0; j
< n
; j
++)
543 if((a
= INTERSECT(wa
.x
, wa
.y
, wa
.width
, wa
.height
, info
[j
])) > area
) {
548 /* no focused window is on screen, so use pointer location instead */
549 if(!area
&& XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
))
550 for(i
= 0; i
< n
; i
++)
551 if(INTERSECT(x
, y
, 1, 1, info
[i
]))
555 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
563 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
564 mw
= DisplayWidth(dc
->dpy
, screen
);
566 promptw
= prompt
? textw(dc
, prompt
) : 0;
567 inputw
= MIN(inputw
, mw
/3);
570 /* create menu window */
571 swa
.override_redirect
= True
;
572 swa
.background_pixmap
= ParentRelative
;
573 swa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
574 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
575 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
576 DefaultVisual(dc
->dpy
, screen
),
577 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &swa
);
579 /* open input methods */
580 xim
= XOpenIM(dc
->dpy
, NULL
, NULL
, NULL
);
581 xic
= XCreateIC(xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
582 XNClientWindow
, win
, XNFocusWindow
, win
, NULL
);
584 XMapRaised(dc
->dpy
, win
);
585 resizedc(dc
, mw
, mh
);
591 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
592 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);