Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #include <X11/Xutil.h>
14 #include <X11/extensions/Xinerama.h>
18 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
19 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
21 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
24 typedef struct Item Item
;
27 Item
*next
; /* traverses all items */
28 Item
*left
, *right
; /* traverses items matching current search pattern */
31 /* forward declarations */
32 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
33 static void calcoffsetsh(void);
34 static void calcoffsetsv(void);
35 static char *cistrstr(const char *s
, const char *sub
);
36 static void cleanup(void);
37 static void dinput(void);
38 static void drawmenu(void);
39 static void drawmenuh(void);
40 static void drawmenuv(void);
41 static Bool
grabkeyboard(void);
42 static void kpress(XKeyEvent
* e
);
43 static void match(char *pattern
);
44 static void readstdin(void);
45 static void run(void);
46 static void setup(Bool topbar
);
52 static char *maxname
= NULL
;
53 static char *prompt
= NULL
;
54 static char text
[4096];
56 static int promptw
= 0;
59 static unsigned int lines
= 0;
60 static unsigned int numlockmask
= 0;
61 static unsigned int mw
, mh
;
62 static Bool running
= True
;
65 static Item
*allitems
= NULL
; /* first of all items */
66 static Item
*item
= NULL
; /* first of pattern matching items */
67 static Item
*sel
= NULL
;
68 static Item
*next
= NULL
;
69 static Item
*prev
= NULL
;
70 static Item
*curr
= NULL
;
71 static Window win
, parent
;
72 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
73 static char *(*fstrstr
)(const char *, const char *) = strstr
;
74 static void (*calcoffsets
)(void) = calcoffsetsh
;
77 appenditem(Item
*i
, Item
**list
, Item
**last
) {
91 w
= promptw
+ cmdw
+ (2 * spaceitem
);
92 for(next
= curr
; next
; next
= next
->right
)
93 if((w
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
95 w
= promptw
+ cmdw
+ (2 * spaceitem
);
96 for(prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
97 if((w
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
106 for(i
= 0; i
< lines
&& next
; i
++)
108 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
113 cistrstr(const char *s
, const char *sub
) {
119 if((c
= tolower(*sub
++)) != '\0') {
123 if((csub
= *s
++) == '\0')
126 while(tolower(csub
) != c
);
128 while(strncasecmp(s
, sub
, len
) != 0);
139 itm
= allitems
->next
;
140 free(allitems
->text
);
145 XDestroyWindow(dpy
, win
);
146 XUngrabKeyboard(dpy
, CurrentTime
);
152 execlp("dinput", "dinput", text
, NULL
); /* todo: argv */
153 eprint("cannot exec dinput\n");
162 drawtext(&dc
, NULL
, dc
.norm
);
166 drawtext(&dc
, prompt
, dc
.sel
);
171 if(cmdw
&& item
&& lines
== 0)
173 drawtext(&dc
, *text
? text
: NULL
, dc
.norm
);
180 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
190 drawtext(&dc
, curr
->left
? "<" : NULL
, dc
.norm
);
192 for(i
= curr
; i
!= next
; i
=i
->right
) {
193 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
194 drawtext(&dc
, i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
199 drawtext(&dc
, next
? ">" : NULL
, dc
.norm
);
207 dc
.h
= dc
.font
.height
+ 2;
209 for(i
= curr
; i
!= next
; i
=i
->right
) {
210 drawtext(&dc
, i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
214 drawtext(&dc
, NULL
, dc
.norm
);
221 for(len
= 1000; len
; len
--) {
222 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
231 kpress(XKeyEvent
* e
) {
232 char buf
[sizeof text
];
238 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
239 if(ksym
== XK_KP_Enter
)
241 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
242 ksym
= (ksym
- XK_KP_0
) + XK_0
;
243 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
244 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
245 || IsPrivateKeypadKey(ksym
))
247 /* first check if a control mask is omitted */
248 if(e
->state
& ControlMask
) {
249 switch(tolower(ksym
)) {
290 while(i
-- > 0 && text
[i
] == ' ');
291 while(i
-- > 0 && text
[i
] != ' ');
302 num
= MIN(num
, sizeof text
);
303 if(num
&& !iscntrl((int) buf
[0])) {
304 memcpy(text
+ len
, buf
, num
+ 1);
312 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
322 while(sel
&& sel
->right
)
335 if(!sel
|| !sel
->left
)
338 if(sel
->right
== curr
) {
356 if((e
->state
& ShiftMask
) || !sel
)
357 fprintf(stdout
, "%s", text
);
359 fprintf(stdout
, "%s", sel
->text
);
365 if(!sel
|| !sel
->right
)
375 strncpy(text
, sel
->text
, sizeof text
);
383 match(char *pattern
) {
385 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
389 plen
= strlen(pattern
);
390 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
391 for(i
= allitems
; i
; i
= i
->next
)
392 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
393 appenditem(i
, &lexact
, &exactend
);
394 else if(!fstrncmp(pattern
, i
->text
, plen
))
395 appenditem(i
, &lprefix
, &prefixend
);
396 else if(fstrstr(i
->text
, pattern
))
397 appenditem(i
, &lsubstr
, &substrend
);
404 itemend
->right
= lprefix
;
405 lprefix
->left
= itemend
;
413 itemend
->right
= lsubstr
;
414 lsubstr
->left
= itemend
;
419 curr
= prev
= next
= sel
= item
;
425 char *p
, buf
[sizeof text
];
426 unsigned int len
= 0, max
= 0;
430 while(fgets(buf
, sizeof buf
, stdin
)) {
432 if(buf
[len
-1] == '\n')
434 if(!(p
= strdup(buf
)))
435 eprint("cannot strdup %u bytes\n", len
);
436 if((max
= MAX(max
, len
)) == len
)
438 if(!(new = malloc(sizeof *new)))
439 eprint("cannot malloc %u bytes\n", sizeof *new);
440 new->next
= new->left
= new->right
= NULL
;
454 /* main event loop */
455 while(running
&& !XNextEvent(dpy
, &ev
))
461 if(ev
.xexpose
.count
== 0)
464 case VisibilityNotify
:
465 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
466 XRaiseWindow(dpy
, win
);
476 XineramaScreenInfo
*info
= NULL
;
478 XModifierKeymap
*modmap
;
479 XSetWindowAttributes wa
;
480 XWindowAttributes pwa
;
482 /* init modifier map */
483 modmap
= XGetModifierMapping(dpy
);
484 for(i
= 0; i
< 8; i
++)
485 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
486 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
487 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
488 numlockmask
= (1 << i
);
490 XFreeModifiermap(modmap
);
493 dc
.norm
[ColBG
] = getcolor(&dc
, normbgcolor
);
494 dc
.norm
[ColFG
] = getcolor(&dc
, normfgcolor
);
495 dc
.sel
[ColBG
] = getcolor(&dc
, selbgcolor
);
496 dc
.sel
[ColFG
] = getcolor(&dc
, selfgcolor
);
500 wa
.override_redirect
= True
;
501 wa
.background_pixmap
= ParentRelative
;
502 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
504 /* menu window geometry */
505 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
507 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
513 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
514 for(i
= 0; i
< n
; i
++)
515 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
519 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
526 XGetWindowAttributes(dpy
, parent
, &pwa
);
528 y
= topbar
? 0 : pwa
.height
- mh
;
532 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
533 DefaultDepth(dpy
, screen
), CopyFromParent
,
534 DefaultVisual(dpy
, screen
),
535 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
539 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);
541 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
544 XMapRaised(dpy
, win
);
548 main(int argc
, char *argv
[]) {
552 /* command line args */
554 for(i
= 1; i
< argc
; i
++)
555 if(!strcmp(argv
[i
], "-i")) {
556 fstrncmp
= strncasecmp
;
559 else if(!strcmp(argv
[i
], "-b"))
561 else if(!strcmp(argv
[i
], "-e")) {
562 if(++i
< argc
) parent
= atoi(argv
[i
]);
564 else if(!strcmp(argv
[i
], "-l")) {
565 if(++i
< argc
) lines
= atoi(argv
[i
]);
567 calcoffsets
= calcoffsetsv
;
569 else if(!strcmp(argv
[i
], "-fn")) {
570 if(++i
< argc
) font
= argv
[i
];
572 else if(!strcmp(argv
[i
], "-nb")) {
573 if(++i
< argc
) normbgcolor
= argv
[i
];
575 else if(!strcmp(argv
[i
], "-nf")) {
576 if(++i
< argc
) normfgcolor
= argv
[i
];
578 else if(!strcmp(argv
[i
], "-p")) {
579 if(++i
< argc
) prompt
= argv
[i
];
581 else if(!strcmp(argv
[i
], "-sb")) {
582 if(++i
< argc
) selbgcolor
= argv
[i
];
584 else if(!strcmp(argv
[i
], "-sf")) {
585 if(++i
< argc
) selfgcolor
= argv
[i
];
587 else if(!strcmp(argv
[i
], "-v"))
588 eprint("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
590 eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
591 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
592 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
593 fprintf(stderr
, "dmenu: warning: no locale support\n");
594 if(!(dpy
= XOpenDisplay(NULL
)))
595 eprint("cannot open display\n");
596 screen
= DefaultScreen(dpy
);
598 parent
= RootWindow(dpy
, screen
);
601 running
= grabkeyboard();