Xinqi Bao's Git
a06ae1572672deba09e4a36537369a8e457e4601
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
;
28 static void appenditem(Item
*item
, Item
**list
, Item
**last
);
29 static void calcoffsets(void);
30 static char *cistrstr(const char *s
, const char *sub
);
31 static void drawmenu(void);
32 static void grabkeyboard(void);
33 static void insert(const char *str
, ssize_t n
);
34 static void keypress(XKeyEvent
*ev
);
35 static void match(void);
36 static size_t nextrune(int inc
);
37 static void paste(void);
38 static void readstdin(void);
39 static void run(void);
40 static void setup(void);
41 static void usage(void);
43 static char text
[BUFSIZ
] = "";
44 static int bh
, mw
, mh
;
45 static int inputw
, promptw
;
46 static size_t cursor
= 0;
47 static unsigned long normcol
[ColLast
];
48 static unsigned long selcol
[ColLast
];
49 static unsigned long outcol
[ColLast
];
50 static Atom clip
, utf8
;
52 static Item
*items
= NULL
;
53 static Item
*matches
, *matchend
;
54 static Item
*prev
, *curr
, *next
, *sel
;
60 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
61 static char *(*fstrstr
)(const char *, const char *) = strstr
;
64 main(int argc
, char *argv
[]) {
68 for(i
= 1; i
< argc
; i
++)
69 /* these options take no arguments */
70 if(!strcmp(argv
[i
], "-v")) { /* prints version information */
71 puts("dmenu-"VERSION
", © 2006-2012 dmenu engineers, see LICENSE for details");
74 else if(!strcmp(argv
[i
], "-b")) /* appears at the bottom of the screen */
76 else if(!strcmp(argv
[i
], "-f")) /* grabs keyboard before reading stdin */
78 else if(!strcmp(argv
[i
], "-i")) { /* case-insensitive item matching */
79 fstrncmp
= strncasecmp
;
84 /* these options take one argument */
85 else if(!strcmp(argv
[i
], "-l")) /* number of lines in vertical list */
86 lines
= atoi(argv
[++i
]);
87 else if(!strcmp(argv
[i
], "-p")) /* adds prompt to left of input field */
89 else if(!strcmp(argv
[i
], "-fn")) /* font or font set */
91 else if(!strcmp(argv
[i
], "-nb")) /* normal background color */
92 normbgcolor
= argv
[++i
];
93 else if(!strcmp(argv
[i
], "-nf")) /* normal foreground color */
94 normfgcolor
= argv
[++i
];
95 else if(!strcmp(argv
[i
], "-sb")) /* selected background color */
96 selbgcolor
= argv
[++i
];
97 else if(!strcmp(argv
[i
], "-sf")) /* selected foreground color */
98 selfgcolor
= argv
[++i
];
116 return 1; /* unreachable */
120 appenditem(Item
*item
, Item
**list
, Item
**last
) {
122 (*last
)->right
= item
;
138 n
= mw
- (promptw
+ inputw
+ textw(dc
, "<") + textw(dc
, ">"));
139 /* calculate which items will begin the next page and previous page */
140 for(i
= 0, next
= curr
; next
; next
= next
->right
)
141 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, next
->text
), n
)) > n
)
143 for(i
= 0, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
144 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, prev
->left
->text
), n
)) > n
)
149 cistrstr(const char *s
, const char *sub
) {
152 for(len
= strlen(sub
); *s
; s
++)
153 if(!strncasecmp(s
, sub
, len
))
166 drawrect(dc
, 0, 0, mw
, mh
, True
, BG(dc
, normcol
));
168 if(prompt
&& *prompt
) {
170 drawtext(dc
, prompt
, selcol
);
173 /* draw input field */
174 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
175 drawtext(dc
, text
, normcol
);
176 if((curpos
= textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
177 drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, True
, FG(dc
, normcol
));
180 /* draw vertical list */
182 for(item
= curr
; item
!= next
; item
= item
->right
) {
184 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
:
185 (item
->out
) ? outcol
: normcol
);
189 /* draw horizontal list */
191 dc
->w
= textw(dc
, "<");
193 drawtext(dc
, "<", normcol
);
194 for(item
= curr
; item
!= next
; item
= item
->right
) {
196 dc
->w
= MIN(textw(dc
, item
->text
), mw
- dc
->x
- textw(dc
, ">"));
197 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
:
198 (item
->out
) ? outcol
: normcol
);
200 dc
->w
= textw(dc
, ">");
203 drawtext(dc
, ">", normcol
);
205 mapdc(dc
, win
, mw
, mh
);
212 /* try to grab keyboard, we may have to wait for another process to ungrab */
213 for(i
= 0; i
< 1000; i
++) {
214 if(XGrabKeyboard(dc
->dpy
, DefaultRootWindow(dc
->dpy
), True
,
215 GrabModeAsync
, GrabModeAsync
, CurrentTime
) == GrabSuccess
)
219 eprintf("cannot grab keyboard\n");
223 insert(const char *str
, ssize_t n
) {
224 if(strlen(text
) + n
> sizeof text
- 1)
226 /* move existing text out of the way, insert new text, and update cursor */
227 memmove(&text
[cursor
+ n
], &text
[cursor
], sizeof text
- cursor
- MAX(n
, 0));
229 memcpy(&text
[cursor
], str
, n
);
235 keypress(XKeyEvent
*ev
) {
238 KeySym ksym
= NoSymbol
;
241 len
= XmbLookupString(xic
, ev
, buf
, sizeof buf
, &ksym
, &status
);
242 if(status
== XBufferOverflow
)
244 if(ev
->state
& ControlMask
)
246 case XK_a
: ksym
= XK_Home
; break;
247 case XK_b
: ksym
= XK_Left
; break;
248 case XK_c
: ksym
= XK_Escape
; break;
249 case XK_d
: ksym
= XK_Delete
; break;
250 case XK_e
: ksym
= XK_End
; break;
251 case XK_f
: ksym
= XK_Right
; break;
252 case XK_g
: ksym
= XK_Escape
; break;
253 case XK_h
: ksym
= XK_BackSpace
; break;
254 case XK_i
: ksym
= XK_Tab
; break;
255 case XK_j
: /* fallthrough */
256 case XK_J
: ksym
= XK_Return
; break;
257 case XK_m
: /* fallthrough */
258 case XK_M
: ksym
= XK_Return
; break;
259 case XK_n
: ksym
= XK_Down
; break;
260 case XK_p
: ksym
= XK_Up
; break;
262 case XK_k
: /* delete right */
266 case XK_u
: /* delete left */
267 insert(NULL
, 0 - cursor
);
269 case XK_w
: /* delete word */
270 while(cursor
> 0 && text
[nextrune(-1)] == ' ')
271 insert(NULL
, nextrune(-1) - cursor
);
272 while(cursor
> 0 && text
[nextrune(-1)] != ' ')
273 insert(NULL
, nextrune(-1) - cursor
);
275 case XK_y
: /* paste selection */
276 XConvertSelection(dc
->dpy
, (ev
->state
& ShiftMask
) ? clip
: XA_PRIMARY
,
277 utf8
, utf8
, win
, CurrentTime
);
285 else if(ev
->state
& Mod1Mask
)
287 case XK_g
: ksym
= XK_Home
; break;
288 case XK_G
: ksym
= XK_End
; break;
289 case XK_h
: ksym
= XK_Up
; break;
290 case XK_j
: ksym
= XK_Next
; break;
291 case XK_k
: ksym
= XK_Prior
; break;
292 case XK_l
: ksym
= XK_Down
; break;
302 if(text
[cursor
] == '\0')
304 cursor
= nextrune(+1);
309 insert(NULL
, nextrune(-1) - cursor
);
312 if(text
[cursor
] != '\0') {
313 cursor
= strlen(text
);
317 /* jump to end of list and position items in reverse */
322 while(next
&& (curr
= curr
->right
))
334 sel
= curr
= matches
;
338 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
339 cursor
= nextrune(-1);
346 if(sel
&& sel
->left
&& (sel
= sel
->left
)->right
== curr
) {
365 puts((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
);
366 if(!(ev
->state
& ControlMask
))
371 if(text
[cursor
] != '\0') {
372 cursor
= nextrune(+1);
379 if(sel
&& sel
->right
&& (sel
= sel
->right
) == next
) {
387 strncpy(text
, sel
->text
, sizeof text
- 1);
388 text
[sizeof text
- 1] = '\0';
389 cursor
= strlen(text
);
398 static char **tokv
= NULL
;
401 char buf
[sizeof text
], *s
;
404 Item
*item
, *lprefix
, *lsubstr
, *prefixend
, *substrend
;
407 /* separate input text into tokens to be matched individually */
408 for(s
= strtok(buf
, " "); s
; tokv
[tokc
-1] = s
, s
= strtok(NULL
, " "))
409 if(++tokc
> tokn
&& !(tokv
= realloc(tokv
, ++tokn
* sizeof *tokv
)))
410 eprintf("cannot realloc %u bytes\n", tokn
* sizeof *tokv
);
411 len
= tokc
? strlen(tokv
[0]) : 0;
413 matches
= lprefix
= lsubstr
= matchend
= prefixend
= substrend
= NULL
;
414 for(item
= items
; item
&& item
->text
; item
++) {
415 for(i
= 0; i
< tokc
; i
++)
416 if(!fstrstr(item
->text
, tokv
[i
]))
418 if(i
!= tokc
) /* not all tokens match */
420 /* exact matches go first, then prefixes, then substrings */
421 if(!tokc
|| !fstrncmp(tokv
[0], item
->text
, len
+1))
422 appenditem(item
, &matches
, &matchend
);
423 else if(!fstrncmp(tokv
[0], item
->text
, len
))
424 appenditem(item
, &lprefix
, &prefixend
);
426 appenditem(item
, &lsubstr
, &substrend
);
430 matchend
->right
= lprefix
;
431 lprefix
->left
= matchend
;
435 matchend
= prefixend
;
439 matchend
->right
= lsubstr
;
440 lsubstr
->left
= matchend
;
444 matchend
= substrend
;
446 curr
= sel
= matches
;
454 /* return location of next utf8 rune in the given direction (+1 or -1) */
455 for(n
= cursor
+ inc
; n
+ inc
>= 0 && (text
[n
] & 0xc0) == 0x80; n
+= inc
);
466 /* we have been given the current selection, now insert it into input */
467 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, (sizeof text
/ 4) + 1, False
,
468 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
469 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: (ssize_t
)strlen(p
));
476 char buf
[sizeof text
], *p
, *maxstr
= NULL
;
477 size_t i
, max
= 0, size
= 0;
479 /* read each line from stdin and add it to the item list */
480 for(i
= 0; fgets(buf
, sizeof buf
, stdin
); i
++) {
481 if(i
+1 >= size
/ sizeof *items
)
482 if(!(items
= realloc(items
, (size
+= BUFSIZ
))))
483 eprintf("cannot realloc %u bytes:", size
);
484 if((p
= strchr(buf
, '\n')))
486 if(!(items
[i
].text
= strdup(buf
)))
487 eprintf("cannot strdup %u bytes:", strlen(buf
)+1);
488 items
[i
].out
= False
;
489 if(strlen(items
[i
].text
) > max
)
490 max
= strlen(maxstr
= items
[i
].text
);
493 items
[i
].text
= NULL
;
494 inputw
= maxstr
? textw(dc
, maxstr
) : 0;
495 lines
= MIN(lines
, i
);
502 while(!XNextEvent(dc
->dpy
, &ev
)) {
503 if(XFilterEvent(&ev
, win
))
507 if(ev
.xexpose
.count
== 0)
508 mapdc(dc
, win
, mw
, mh
);
513 case SelectionNotify
:
514 if(ev
.xselection
.property
== utf8
)
517 case VisibilityNotify
:
518 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
519 XRaiseWindow(dc
->dpy
, win
);
527 int x
, y
, screen
= DefaultScreen(dc
->dpy
);
528 Window root
= RootWindow(dc
->dpy
, screen
);
529 XSetWindowAttributes swa
;
533 XineramaScreenInfo
*info
;
536 normcol
[ColBG
] = getcolor(dc
, normbgcolor
);
537 normcol
[ColFG
] = getcolor(dc
, normfgcolor
);
538 selcol
[ColBG
] = getcolor(dc
, selbgcolor
);
539 selcol
[ColFG
] = getcolor(dc
, selfgcolor
);
540 outcol
[ColBG
] = getcolor(dc
, outbgcolor
);
541 outcol
[ColFG
] = getcolor(dc
, outfgcolor
);
543 clip
= XInternAtom(dc
->dpy
, "CLIPBOARD", False
);
544 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
546 /* calculate menu geometry */
547 bh
= dc
->font
.height
+ 2;
548 lines
= MAX(lines
, 0);
549 mh
= (lines
+ 1) * bh
;
551 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
552 int a
, j
, di
, i
= 0, area
= 0;
554 Window w
, pw
, dw
, *dws
;
555 XWindowAttributes wa
;
557 XGetInputFocus(dc
->dpy
, &w
, &di
);
558 if(w
!= root
&& w
!= PointerRoot
&& w
!= None
) {
559 /* find top-level window containing current input focus */
561 if(XQueryTree(dc
->dpy
, (pw
= w
), &dw
, &w
, &dws
, &du
) && dws
)
563 } while(w
!= root
&& w
!= pw
);
564 /* find xinerama screen with which the window intersects most */
565 if(XGetWindowAttributes(dc
->dpy
, pw
, &wa
))
566 for(j
= 0; j
< n
; j
++)
567 if((a
= INTERSECT(wa
.x
, wa
.y
, wa
.width
, wa
.height
, info
[j
])) > area
) {
572 /* no focused window is on screen, so use pointer location instead */
573 if(!area
&& XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
))
574 for(i
= 0; i
< n
; i
++)
575 if(INTERSECT(x
, y
, 1, 1, info
[i
]))
579 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
587 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
588 mw
= DisplayWidth(dc
->dpy
, screen
);
590 promptw
= (prompt
&& *prompt
) ? textw(dc
, prompt
) : 0;
591 inputw
= MIN(inputw
, mw
/3);
594 /* create menu window */
595 swa
.override_redirect
= True
;
596 swa
.background_pixel
= normcol
[ColBG
];
597 swa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
598 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
599 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
600 DefaultVisual(dc
->dpy
, screen
),
601 CWOverrideRedirect
| CWBackPixel
| CWEventMask
, &swa
);
603 /* open input methods */
604 xim
= XOpenIM(dc
->dpy
, NULL
, NULL
, NULL
);
605 xic
= XCreateIC(xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
606 XNClientWindow
, win
, XNFocusWindow
, win
, NULL
);
608 XMapRaised(dc
->dpy
, win
);
609 resizedc(dc
, mw
, mh
);
615 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
616 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);