Xinqi Bao's Git
b6f1ef2b3aaa5efdde316520117515aa705b9147
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
[]) {
70 for(i
= 1; i
< argc
; i
++)
72 if(!strcmp(argv
[i
], "-v")) {
73 fputs("dmenu-"VERSION
", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout
);
76 else if(!strcmp(argv
[i
], "-b"))
78 else if(!strcmp(argv
[i
], "-f"))
80 else if(!strcmp(argv
[i
], "-i"))
81 fstrncmp
= strncasecmp
;
85 else if(!strcmp(argv
[i
], "-l"))
86 lines
= atoi(argv
[++i
]);
87 else if(!strcmp(argv
[i
], "-m"))
88 monitor
= atoi(argv
[++i
]);
89 else if(!strcmp(argv
[i
], "-p"))
91 else if(!strcmp(argv
[i
], "-fn"))
93 else if(!strcmp(argv
[i
], "-nb"))
94 normbgcolor
= argv
[++i
];
95 else if(!strcmp(argv
[i
], "-nf"))
96 normfgcolor
= argv
[++i
];
97 else if(!strcmp(argv
[i
], "-sb"))
98 selbgcolor
= argv
[++i
];
99 else if(!strcmp(argv
[i
], "-sf"))
100 selfgcolor
= argv
[++i
];
120 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
121 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);
126 appenditem(Item
*item
, Item
**list
, Item
**last
) {
130 (*last
)->right
= item
;
143 n
= mw
- (promptw
+ inputw
+ textw(dc
, "<") + textw(dc
, ">"));
145 for(i
= 0, next
= curr
; next
; next
= next
->right
)
146 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, next
->text
), n
)) > n
)
148 for(i
= 0, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
149 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, prev
->left
->text
), n
)) > n
)
161 drawrect(dc
, 0, 0, mw
, mh
, True
, BG(dc
, normcol
));
165 drawtext(dc
, prompt
, selcol
);
168 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
169 drawtext(dc
, text
, normcol
);
170 if((curpos
= textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
171 drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, True
, FG(dc
, normcol
));
175 for(item
= curr
; item
!= next
; item
= item
->right
) {
177 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
182 dc
->w
= textw(dc
, "<");
184 drawtext(dc
, "<", normcol
);
185 for(item
= curr
; item
!= next
; item
= item
->right
) {
187 dc
->w
= MIN(textw(dc
, item
->text
), mw
- dc
->x
- textw(dc
, ">"));
188 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
190 dc
->w
= textw(dc
, ">");
193 drawtext(dc
, ">", normcol
);
195 mapdc(dc
, win
, mw
, mh
);
199 fstrstr(const char *s
, const char *sub
) {
202 for(len
= strlen(sub
); *s
; s
++)
203 if(!fstrncmp(s
, sub
, len
))
212 for(i
= 0; i
< 1000; i
++) {
213 if(!XGrabKeyboard(dc
->dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
))
217 eprintf("cannot grab keyboard\n");
221 insert(const char *s
, ssize_t n
) {
222 if(strlen(text
) + n
> sizeof text
- 1)
224 memmove(text
+ cursor
+ n
, text
+ cursor
, sizeof text
- cursor
- MAX(n
, 0));
226 memcpy(text
+ cursor
, s
, n
);
232 keypress(XKeyEvent
*ev
) {
238 XLookupString(ev
, buf
, sizeof buf
, &ksym
, NULL
);
239 if(ev
->state
& ControlMask
)
240 switch(tolower(ksym
)) {
270 case XK_k
: /* delete right */
280 case XK_u
: /* delete left */
281 insert(NULL
, 0 - cursor
);
283 case XK_w
: /* delete word */
284 while(cursor
> 0 && text
[nextrune(-1)] == ' ')
285 insert(NULL
, nextrune(-1) - cursor
);
286 while(cursor
> 0 && text
[nextrune(-1)] != ' ')
287 insert(NULL
, nextrune(-1) - cursor
);
289 case XK_y
: /* paste selection */
290 XConvertSelection(dc
->dpy
, XA_PRIMARY
, utf8
, utf8
, win
, CurrentTime
);
296 insert(buf
, strlen(buf
));
301 cursor
= nextrune(+1);
304 insert(NULL
, nextrune(-1) - cursor
);
316 while(next
&& (curr
= curr
->right
))
328 sel
= curr
= matches
;
332 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
333 cursor
= nextrune(-1);
339 if(sel
&& sel
->left
&& (sel
= sel
->left
)->right
== curr
) {
358 fputs((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
, stdout
);
362 cursor
= nextrune(+1);
368 if(sel
&& sel
->right
&& (sel
= sel
->right
) == next
) {
376 strncpy(text
, sel
->text
, sizeof text
);
377 cursor
= strlen(text
);
387 Item
*item
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
390 matches
= lexact
= lprefix
= lsubstr
= matchend
= exactend
= prefixend
= substrend
= NULL
;
391 for(item
= items
; item
&& item
->text
; item
++)
392 if(!fstrncmp(text
, item
->text
, len
+ 1))
393 appenditem(item
, &lexact
, &exactend
);
394 else if(!fstrncmp(text
, item
->text
, len
))
395 appenditem(item
, &lprefix
, &prefixend
);
396 else if(fstrstr(item
->text
, text
))
397 appenditem(item
, &lsubstr
, &substrend
);
405 matchend
->right
= lprefix
;
406 lprefix
->left
= matchend
;
410 matchend
= prefixend
;
414 matchend
->right
= lsubstr
;
415 lsubstr
->left
= matchend
;
419 matchend
= substrend
;
421 curr
= sel
= matches
;
430 for(n
= cursor
+ incr
; n
>= 0 && n
< len
&& (text
[n
] & 0xc0) == 0x80; n
+= incr
);
441 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, (sizeof text
/ 4) + 1, False
,
442 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
443 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: strlen(p
));
450 char buf
[sizeof text
], *p
;
453 for(i
= 0; fgets(buf
, sizeof buf
, stdin
); items
[++i
].text
= NULL
) {
454 if(i
+1 == size
/ sizeof *items
|| !items
)
455 if(!(items
= realloc(items
, (size
+= BUFSIZ
))))
456 eprintf("cannot realloc %u bytes:", size
);
457 if((p
= strchr(buf
, '\n')))
459 if(!(items
[i
].text
= strdup(buf
)))
460 eprintf("cannot strdup %u bytes:", strlen(buf
)+1);
461 inputw
= MAX(inputw
, textw(dc
, items
[i
].text
));
469 while(!XNextEvent(dc
->dpy
, &ev
))
472 if(ev
.xexpose
.count
== 0)
478 case SelectionNotify
:
479 if(ev
.xselection
.property
== utf8
)
482 case VisibilityNotify
:
483 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
484 XRaiseWindow(dc
->dpy
, win
);
492 XSetWindowAttributes wa
;
495 XineramaScreenInfo
*info
;
498 screen
= DefaultScreen(dc
->dpy
);
499 root
= RootWindow(dc
->dpy
, screen
);
500 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
502 normcol
[ColBG
] = getcolor(dc
, normbgcolor
);
503 normcol
[ColFG
] = getcolor(dc
, normfgcolor
);
504 selcol
[ColBG
] = getcolor(dc
, selbgcolor
);
505 selcol
[ColFG
] = getcolor(dc
, selfgcolor
);
508 bh
= dc
->font
.height
+ 2;
509 lines
= MAX(lines
, 0);
510 mh
= (lines
+ 1) * bh
;
512 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
517 XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
);
518 for(i
= 0; i
< n
-1; i
++)
519 if((monitor
== info
[i
].screen_number
)
520 || (monitor
< 0 && INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
)))
523 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
531 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
532 mw
= DisplayWidth(dc
->dpy
, screen
);
535 wa
.override_redirect
= True
;
536 wa
.background_pixmap
= ParentRelative
;
537 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
538 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
539 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
540 DefaultVisual(dc
->dpy
, screen
),
541 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
544 resizedc(dc
, mw
, mh
);
545 inputw
= MIN(inputw
, mw
/3);
546 promptw
= prompt
? textw(dc
, prompt
) : 0;
547 XMapRaised(dc
->dpy
, win
);