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 size_t cursor
= 0;
43 static const char *font
= NULL
;
44 static const char *prompt
= NULL
;
45 static const char *normbgcolor
= "#cccccc";
46 static const char *normfgcolor
= "#000000";
47 static const char *selbgcolor
= "#0066ff";
48 static const char *selfgcolor
= "#ffffff";
49 static unsigned int bh
, mw
, mh
;
50 static unsigned int inputw
= 0;
51 static unsigned int lines
= 0;
52 static unsigned int promptw
;
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
+ textw(dc
, "<") + textw(dc
, ">"));
85 for(i
= 0, next
= curr
; next
; next
= next
->right
)
86 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, next
->text
), mw
/3)) > n
)
88 for(i
= 0, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
89 if((i
+= (lines
> 0) ? bh
: MIN(textw(dc
, prev
->left
->text
), mw
/3)) > n
)
101 drawrect(dc
, 0, 0, mw
, mh
, True
, BG(dc
, normcol
));
105 drawtext(dc
, prompt
, selcol
);
108 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
109 drawtext(dc
, text
, normcol
);
110 if((curpos
= textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
111 drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, True
, FG(dc
, normcol
));
115 for(item
= curr
; item
!= next
; item
= item
->right
) {
117 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
122 dc
->w
= textw(dc
, "<");
124 drawtext(dc
, "<", normcol
);
125 for(item
= curr
; item
!= next
; item
= item
->right
) {
127 dc
->w
= MIN(textw(dc
, item
->text
), mw
/3);
128 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
130 dc
->w
= textw(dc
, ">");
133 drawtext(dc
, ">", normcol
);
135 commitdraw(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
);
229 case XK_y
: /* paste selection */
230 XConvertSelection(dc
->dpy
, XA_PRIMARY
, utf8
, utf8
, win
, CurrentTime
);
237 insert(buf
, strlen(buf
));
242 cursor
= nextrune(+1);
245 insert(NULL
, nextrune(-1) - cursor
);
256 while(sel
&& sel
->right
)
266 sel
= curr
= matches
;
270 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
271 cursor
= nextrune(-1);
277 if(sel
&& sel
->left
&& (sel
= sel
->left
)->right
== curr
) {
296 fputs((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
, stdout
);
301 cursor
= nextrune(+1);
307 if(sel
&& sel
->right
&& (sel
= sel
->right
) == next
) {
315 strncpy(text
, sel
->text
, sizeof text
);
316 cursor
= strlen(text
);
326 Item
*item
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
329 matches
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
330 for(item
= items
; item
; item
= item
->next
)
331 if(!fstrncmp(text
, item
->text
, len
+ 1))
332 appenditem(item
, &lexact
, &exactend
);
333 else if(!fstrncmp(text
, item
->text
, len
))
334 appenditem(item
, &lprefix
, &prefixend
);
335 else if(fstrstr(item
->text
, text
))
336 appenditem(item
, &lsubstr
, &substrend
);
344 itemend
->right
= lprefix
;
345 lprefix
->left
= itemend
;
353 itemend
->right
= lsubstr
;
354 lsubstr
->left
= itemend
;
359 curr
= prev
= next
= sel
= matches
;
368 for(n
= cursor
+ incr
; n
>= 0 && n
< len
&& (text
[n
] & 0xc0) == 0x80; n
+= incr
);
379 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, (sizeof text
/ 4) + 1, False
,
380 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
381 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: strlen(p
));
388 char buf
[sizeof text
], *p
;
391 for(end
= &items
; fgets(buf
, sizeof buf
, stdin
); *end
= item
, end
= &item
->next
) {
392 if((p
= strchr(buf
, '\n')))
394 if(!(item
= malloc(sizeof *item
)))
395 eprintf("cannot malloc %u bytes\n", sizeof *item
);
396 if(!(item
->text
= strdup(buf
)))
397 eprintf("cannot strdup %u bytes\n", strlen(buf
)+1);
398 item
->next
= item
->left
= item
->right
= NULL
;
399 inputw
= MAX(inputw
, textw(dc
, item
->text
));
407 while(!XNextEvent(dc
->dpy
, &ev
))
410 if(ev
.xexpose
.count
== 0)
416 case SelectionNotify
:
417 if(ev
.xselection
.property
== utf8
)
420 case VisibilityNotify
:
421 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
422 XRaiseWindow(dc
->dpy
, win
);
430 XSetWindowAttributes wa
;
433 XineramaScreenInfo
*info
;
436 screen
= DefaultScreen(dc
->dpy
);
437 root
= RootWindow(dc
->dpy
, screen
);
438 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
440 normcol
[ColBG
] = getcolor(dc
, normbgcolor
);
441 normcol
[ColFG
] = getcolor(dc
, normfgcolor
);
442 selcol
[ColBG
] = getcolor(dc
, selbgcolor
);
443 selcol
[ColFG
] = getcolor(dc
, selfgcolor
);
446 bh
= dc
->font
.height
+ 2;
447 mh
= (lines
+ 1) * bh
;
449 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
454 XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
);
455 for(i
= 0; i
< n
; i
++)
456 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
459 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
467 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
468 mw
= DisplayWidth(dc
->dpy
, screen
);
471 wa
.override_redirect
= True
;
472 wa
.background_pixmap
= ParentRelative
;
473 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
474 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
475 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
476 DefaultVisual(dc
->dpy
, screen
),
477 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
480 setcanvas(dc
, mw
, mh
);
481 inputw
= MIN(inputw
, mw
/3);
482 promptw
= prompt
? MIN(textw(dc
, prompt
), mw
/5) : 0;
483 XMapRaised(dc
->dpy
, win
);
490 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
491 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);
496 main(int argc
, char *argv
[]) {
500 for(i
= 1; i
< argc
; i
++)
502 if(!strcmp(argv
[i
], "-v")) {
503 fputs("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout
);
506 else if(!strcmp(argv
[i
], "-b"))
508 else if(!strcmp(argv
[i
], "-i"))
509 fstrncmp
= strncasecmp
;
513 else if(!strcmp(argv
[i
], "-l"))
514 lines
= atoi(argv
[++i
]);
515 else if(!strcmp(argv
[i
], "-p"))
517 else if(!strcmp(argv
[i
], "-fn"))
519 else if(!strcmp(argv
[i
], "-nb"))
520 normbgcolor
= argv
[++i
];
521 else if(!strcmp(argv
[i
], "-nf"))
522 normfgcolor
= argv
[++i
];
523 else if(!strcmp(argv
[i
], "-sb"))
524 selbgcolor
= argv
[++i
];
525 else if(!strcmp(argv
[i
], "-sf"))
526 selfgcolor
= argv
[++i
];
536 return EXIT_FAILURE
; /* should not reach */