Xinqi Bao's Git
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
;
22 Item
*next
; /* traverses all items */
23 Item
*left
, *right
; /* traverses matching items */
26 static void appenditem(Item
*item
, Item
**list
, Item
**last
);
27 static void calcoffsets(void);
28 static void drawmenu(void);
29 static char *fstrstr(const char *s
, const char *sub
);
30 static void grabkeyboard(void);
31 static void insert(const char *s
, ssize_t n
);
32 static void keypress(XKeyEvent
*ev
);
33 static void match(void);
34 static size_t nextrune(int incr
);
35 static void paste(void);
36 static void readstdin(void);
37 static void run(void);
38 static void setup(void);
39 static void usage(void);
41 static char text
[BUFSIZ
];
42 static int bh
, mw
, mh
;
43 static int inputw
= 0;
46 static size_t cursor
= 0;
47 static const char *font
= NULL
;
48 static const char *prompt
= NULL
;
49 static const char *normbgcolor
= "#cccccc";
50 static const char *normfgcolor
= "#000000";
51 static const char *selbgcolor
= "#0066ff";
52 static const char *selfgcolor
= "#ffffff";
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
, *sel
;
60 static Item
*prev
, *curr
, *next
;
61 static Window root
, win
;
63 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
66 appenditem(Item
*item
, Item
**list
, Item
**last
) {
70 (*last
)->right
= item
;
83 n
= mw
- (promptw
+ inputw
+ dc_textw(dc
, "<") + dc_textw(dc
, ">"));
85 for(i
= 0, next
= curr
; next
; next
= next
->right
)
86 if((i
+= (lines
> 0) ? bh
: MIN(dc_textw(dc
, next
->text
), n
)) > n
)
88 for(i
= 0, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
89 if((i
+= (lines
> 0) ? bh
: MIN(dc_textw(dc
, prev
->left
->text
), n
)) > n
)
101 dc_drawrect(dc
, 0, 0, mw
, mh
, True
, BG(dc
, normcol
));
105 dc_drawtext(dc
, prompt
, selcol
);
108 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
109 dc_drawtext(dc
, text
, normcol
);
110 if((curpos
= dc_textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
111 dc_drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, True
, FG(dc
, normcol
));
115 for(item
= curr
; item
!= next
; item
= item
->right
) {
117 dc_drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
122 dc
->w
= dc_textw(dc
, "<");
124 dc_drawtext(dc
, "<", normcol
);
125 for(item
= curr
; item
!= next
; item
= item
->right
) {
127 dc
->w
= MIN(dc_textw(dc
, item
->text
), mw
- dc
->x
- dc_textw(dc
, ">"));
128 dc_drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
130 dc
->w
= dc_textw(dc
, ">");
133 dc_drawtext(dc
, ">", normcol
);
135 dc_map(dc
, win
, mw
, mh
);
139 fstrstr(const char *s
, const char *sub
) {
142 for(len
= strlen(sub
); *s
; s
++)
143 if(!fstrncmp(s
, sub
, len
))
152 for(i
= 0; i
< 1000; i
++) {
153 if(!XGrabKeyboard(dc
->dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
))
157 eprintf("cannot grab keyboard\n");
161 insert(const char *s
, ssize_t n
) {
162 if(strlen(text
) + n
> sizeof text
- 1)
164 memmove(text
+ cursor
+ n
, text
+ cursor
, sizeof text
- cursor
- MAX(n
, 0));
166 memcpy(text
+ cursor
, s
, n
);
172 keypress(XKeyEvent
*ev
) {
178 XLookupString(ev
, buf
, sizeof buf
, &ksym
, NULL
);
179 if(ev
->state
& ControlMask
) {
180 switch(tolower(ksym
)) {
210 case XK_k
: /* delete right */
220 case XK_u
: /* delete left */
221 insert(NULL
, 0 - cursor
);
223 case XK_w
: /* delete word */
224 while(cursor
> 0 && text
[nextrune(-1)] == ' ')
225 insert(NULL
, nextrune(-1) - cursor
);
226 while(cursor
> 0 && text
[nextrune(-1)] != ' ')
227 insert(NULL
, nextrune(-1) - cursor
);
234 insert(buf
, strlen(buf
));
239 cursor
= nextrune(+1);
242 insert(NULL
, nextrune(-1) - cursor
);
253 while(sel
&& sel
->right
)
263 sel
= curr
= matches
;
266 case XK_Insert
: /* paste selection */
267 if(ev
->state
& ShiftMask
)
268 XConvertSelection(dc
->dpy
, XA_PRIMARY
, utf8
, utf8
, win
, CurrentTime
);
271 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
272 cursor
= nextrune(-1);
278 if(sel
&& sel
->left
&& (sel
= sel
->left
)->right
== curr
) {
297 fputs((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
, stdout
);
302 cursor
= nextrune(+1);
308 if(sel
&& sel
->right
&& (sel
= sel
->right
) == next
) {
316 strncpy(text
, sel
->text
, sizeof text
);
317 cursor
= strlen(text
);
327 Item
*item
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
330 matches
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
331 for(item
= items
; item
; item
= item
->next
)
332 if(!fstrncmp(text
, item
->text
, len
+ 1))
333 appenditem(item
, &lexact
, &exactend
);
334 else if(!fstrncmp(text
, item
->text
, len
))
335 appenditem(item
, &lprefix
, &prefixend
);
336 else if(fstrstr(item
->text
, text
))
337 appenditem(item
, &lsubstr
, &substrend
);
345 itemend
->right
= lprefix
;
346 lprefix
->left
= itemend
;
354 itemend
->right
= lsubstr
;
355 lsubstr
->left
= itemend
;
360 curr
= prev
= next
= sel
= matches
;
369 for(n
= cursor
+ incr
; n
>= 0 && n
< len
&& (text
[n
] & 0xc0) == 0x80; n
+= incr
);
380 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, (sizeof text
/ 4) + 1, False
,
381 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
382 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: strlen(p
));
389 char buf
[sizeof text
], *p
;
392 for(end
= &items
; fgets(buf
, sizeof buf
, stdin
); *end
= item
, end
= &item
->next
) {
393 if((p
= strchr(buf
, '\n')))
395 if(!(item
= malloc(sizeof *item
)))
396 eprintf("cannot malloc %u bytes\n", sizeof *item
);
397 if(!(item
->text
= strdup(buf
)))
398 eprintf("cannot strdup %u bytes\n", strlen(buf
)+1);
399 item
->next
= item
->left
= item
->right
= NULL
;
400 inputw
= MAX(inputw
, dc_textw(dc
, item
->text
));
408 while(!XNextEvent(dc
->dpy
, &ev
))
411 if(ev
.xexpose
.count
== 0)
417 case SelectionNotify
:
418 if(ev
.xselection
.property
== utf8
)
421 case VisibilityNotify
:
422 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
423 XRaiseWindow(dc
->dpy
, win
);
431 XSetWindowAttributes wa
;
434 XineramaScreenInfo
*info
;
437 screen
= DefaultScreen(dc
->dpy
);
438 root
= RootWindow(dc
->dpy
, screen
);
439 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
441 normcol
[ColBG
] = dc_color(dc
, normbgcolor
);
442 normcol
[ColFG
] = dc_color(dc
, normfgcolor
);
443 selcol
[ColBG
] = dc_color(dc
, selbgcolor
);
444 selcol
[ColFG
] = dc_color(dc
, selfgcolor
);
447 bh
= dc
->font
.height
+ 2;
448 lines
= MAX(lines
, 0);
449 mh
= (lines
+ 1) * bh
;
451 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
456 XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
);
457 for(i
= 0; i
< n
; i
++)
458 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
461 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
469 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
470 mw
= DisplayWidth(dc
->dpy
, screen
);
473 wa
.override_redirect
= True
;
474 wa
.background_pixmap
= ParentRelative
;
475 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
476 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
477 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
478 DefaultVisual(dc
->dpy
, screen
),
479 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
482 dc_resize(dc
, mw
, mh
);
483 inputw
= MIN(inputw
, mw
/3);
484 promptw
= prompt
? dc_textw(dc
, prompt
) : 0;
485 XMapRaised(dc
->dpy
, win
);
492 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
493 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);
498 main(int argc
, char *argv
[]) {
502 for(i
= 1; i
< argc
; i
++)
504 if(!strcmp(argv
[i
], "-v")) {
505 fputs("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout
);
508 else if(!strcmp(argv
[i
], "-b"))
510 else if(!strcmp(argv
[i
], "-i"))
511 fstrncmp
= strncasecmp
;
515 else if(!strcmp(argv
[i
], "-l"))
516 lines
= atoi(argv
[++i
]);
517 else if(!strcmp(argv
[i
], "-p"))
519 else if(!strcmp(argv
[i
], "-fn"))
521 else if(!strcmp(argv
[i
], "-nb"))
522 normbgcolor
= argv
[++i
];
523 else if(!strcmp(argv
[i
], "-nf"))
524 normfgcolor
= argv
[++i
];
525 else if(!strcmp(argv
[i
], "-sb"))
526 selbgcolor
= argv
[++i
];
527 else if(!strcmp(argv
[i
], "-sf"))
528 selfgcolor
= argv
[++i
];
538 return EXIT_FAILURE
; /* should not reach */