Xinqi Bao's Git
40b220cbd842534f7e85bda034443563c2f0106a
1 /* See LICENSE file for copyright and license details. */
11 #include <X11/extensions/Xinerama.h>
15 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
16 #define MIN(a,b) ((a) < (b) ? (a) : (b))
17 #define MAX(a,b) ((a) > (b) ? (a) : (b))
19 typedef struct Item Item
;
25 static void appenditem(Item
*item
, Item
**list
, Item
**last
);
26 static void calcoffsets(void);
27 static void drawmenu(void);
28 static char *fstrstr(const char *s
, const char *sub
);
29 static void grabkeyboard(void);
30 static void insert(const char *s
, ssize_t n
);
31 static void keypress(XKeyEvent
*ev
);
32 static void match(void);
33 static size_t nextrune(int incr
);
34 static void paste(void);
35 static void readstdin(void);
36 static void run(void);
37 static void setup(void);
39 static char text
[BUFSIZ
] = "";
40 static int bh
, mw
, mh
;
41 static int inputw
= 0;
43 static int monitor
= -1;
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 long normcol
[ColLast
];
53 static unsigned long selcol
[ColLast
];
55 static Bool topbar
= True
;
57 static Item
*items
= NULL
;
58 static Item
*matches
, *matchend
;
59 static Item
*prev
, *curr
, *next
, *sel
;
60 static Window root
, win
;
62 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
65 main(int argc
, char *argv
[]) {
69 for(i
= 1; i
< argc
; i
++)
71 if(!strcmp(argv
[i
], "-v")) {
72 fputs("dmenu-"VERSION
", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout
);
75 else if(!strcmp(argv
[i
], "-b"))
77 else if(!strcmp(argv
[i
], "-f"))
79 else if(!strcmp(argv
[i
], "-i"))
80 fstrncmp
= strncasecmp
;
84 else if(!strcmp(argv
[i
], "-l"))
85 lines
= atoi(argv
[++i
]);
86 else if(!strcmp(argv
[i
], "-m"))
87 monitor
= atoi(argv
[++i
]);
88 else if(!strcmp(argv
[i
], "-p"))
90 else if(!strcmp(argv
[i
], "-fn"))
92 else if(!strcmp(argv
[i
], "-nb"))
93 normbgcolor
= argv
[++i
];
94 else if(!strcmp(argv
[i
], "-nf"))
95 normfgcolor
= argv
[++i
];
96 else if(!strcmp(argv
[i
], "-sb"))
97 selbgcolor
= argv
[++i
];
98 else if(!strcmp(argv
[i
], "-sf"))
99 selfgcolor
= argv
[++i
];
119 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
120 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);
125 appenditem(Item
*item
, Item
**list
, Item
**last
) {
129 (*last
)->right
= item
;
142 n
= mw
- (promptw
+ inputw
+ textw(dc
, "<") + textw(dc
, ">"));
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
)
160 drawrect(dc
, 0, 0, mw
, mh
, True
, BG(dc
, normcol
));
164 drawtext(dc
, prompt
, selcol
);
167 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
168 drawtext(dc
, text
, normcol
);
169 if((curpos
= textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
170 drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, True
, FG(dc
, normcol
));
174 for(item
= curr
; item
!= next
; item
= item
->right
) {
176 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
181 dc
->w
= textw(dc
, "<");
183 drawtext(dc
, "<", normcol
);
184 for(item
= curr
; item
!= next
; item
= item
->right
) {
186 dc
->w
= MIN(textw(dc
, item
->text
), mw
- dc
->x
- textw(dc
, ">"));
187 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
189 dc
->w
= textw(dc
, ">");
192 drawtext(dc
, ">", normcol
);
194 mapdc(dc
, win
, mw
, mh
);
198 fstrstr(const char *s
, const char *sub
) {
201 for(len
= strlen(sub
); *s
; s
++)
202 if(!fstrncmp(s
, sub
, len
))
211 for(i
= 0; i
< 1000; i
++) {
212 if(!XGrabKeyboard(dc
->dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
))
216 eprintf("cannot grab keyboard\n");
220 insert(const char *s
, ssize_t n
) {
221 if(strlen(text
) + n
> sizeof text
- 1)
223 memmove(text
+ cursor
+ n
, text
+ cursor
, sizeof text
- cursor
- MAX(n
, 0));
225 memcpy(text
+ cursor
, s
, n
);
231 keypress(XKeyEvent
*ev
) {
237 XLookupString(ev
, buf
, sizeof buf
, &ksym
, NULL
);
238 if(ev
->state
& ControlMask
)
239 switch(tolower(ksym
)) {
269 case XK_k
: /* delete right */
279 case XK_u
: /* delete left */
280 insert(NULL
, 0 - cursor
);
282 case XK_w
: /* delete word */
283 while(cursor
> 0 && text
[nextrune(-1)] == ' ')
284 insert(NULL
, nextrune(-1) - cursor
);
285 while(cursor
> 0 && text
[nextrune(-1)] != ' ')
286 insert(NULL
, nextrune(-1) - cursor
);
288 case XK_y
: /* paste selection */
289 XConvertSelection(dc
->dpy
, XA_PRIMARY
, utf8
, utf8
, win
, CurrentTime
);
295 insert(buf
, strlen(buf
));
300 cursor
= nextrune(+1);
303 insert(NULL
, nextrune(-1) - cursor
);
315 while(next
&& (curr
= curr
->right
))
327 sel
= curr
= matches
;
331 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
332 cursor
= nextrune(-1);
338 if(sel
&& sel
->left
&& (sel
= sel
->left
)->right
== curr
) {
357 fputs((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
, stdout
);
361 cursor
= nextrune(+1);
367 if(sel
&& sel
->right
&& (sel
= sel
->right
) == next
) {
375 strncpy(text
, sel
->text
, sizeof text
);
376 cursor
= strlen(text
);
385 size_t len
= strlen(text
);
386 Item
*item
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
388 matches
= lexact
= lprefix
= lsubstr
= matchend
= exactend
= prefixend
= substrend
= NULL
;
389 for(item
= items
; item
&& item
->text
; item
++)
390 if(!fstrncmp(text
, item
->text
, len
+ 1))
391 appenditem(item
, &lexact
, &exactend
);
392 else if(!fstrncmp(text
, item
->text
, len
))
393 appenditem(item
, &lprefix
, &prefixend
);
394 else if(fstrstr(item
->text
, text
))
395 appenditem(item
, &lsubstr
, &substrend
);
403 matchend
->right
= lprefix
;
404 lprefix
->left
= matchend
;
408 matchend
= prefixend
;
412 matchend
->right
= lsubstr
;
413 lsubstr
->left
= matchend
;
417 matchend
= substrend
;
419 curr
= sel
= matches
;
425 size_t n
, len
= strlen(text
);
427 for(n
= cursor
+ incr
; n
>= 0 && n
< len
&& (text
[n
] & 0xc0) == 0x80; n
+= incr
);
438 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, (sizeof text
/ 4) + 1, False
,
439 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
440 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: strlen(p
));
447 char buf
[sizeof text
], *p
;
450 for(i
= 0; fgets(buf
, sizeof buf
, stdin
); items
[++i
].text
= NULL
) {
451 if(i
+1 == size
/ sizeof *items
|| !items
)
452 if(!(items
= realloc(items
, (size
+= BUFSIZ
))))
453 eprintf("cannot realloc %u bytes:", size
);
454 if((p
= strchr(buf
, '\n')))
456 if(!(items
[i
].text
= strdup(buf
)))
457 eprintf("cannot strdup %u bytes:", strlen(buf
)+1);
458 inputw
= MAX(inputw
, textw(dc
, items
[i
].text
));
466 while(!XNextEvent(dc
->dpy
, &ev
))
469 if(ev
.xexpose
.count
== 0)
475 case SelectionNotify
:
476 if(ev
.xselection
.property
== utf8
)
479 case VisibilityNotify
:
480 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
481 XRaiseWindow(dc
->dpy
, win
);
489 XSetWindowAttributes wa
;
492 XineramaScreenInfo
*info
;
495 screen
= DefaultScreen(dc
->dpy
);
496 root
= RootWindow(dc
->dpy
, screen
);
497 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
499 normcol
[ColBG
] = getcolor(dc
, normbgcolor
);
500 normcol
[ColFG
] = getcolor(dc
, normfgcolor
);
501 selcol
[ColBG
] = getcolor(dc
, selbgcolor
);
502 selcol
[ColFG
] = getcolor(dc
, selfgcolor
);
505 bh
= dc
->font
.height
+ 2;
506 lines
= MAX(lines
, 0);
507 mh
= (lines
+ 1) * bh
;
509 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
514 XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
);
515 for(i
= 0; i
< n
-1; i
++)
516 if((monitor
== info
[i
].screen_number
)
517 || (monitor
< 0 && INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
)))
520 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
528 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
529 mw
= DisplayWidth(dc
->dpy
, screen
);
532 wa
.override_redirect
= True
;
533 wa
.background_pixmap
= ParentRelative
;
534 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
535 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
536 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
537 DefaultVisual(dc
->dpy
, screen
),
538 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
541 resizedc(dc
, mw
, mh
);
542 inputw
= MIN(inputw
, mw
/3);
543 promptw
= prompt
? textw(dc
, prompt
) : 0;
544 XMapRaised(dc
->dpy
, win
);