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 **argp
= NULL
;
53 static char *maxname
= NULL
;
54 static char *prompt
= NULL
;
55 static char text
[4096];
57 static int promptw
= 0;
60 static unsigned int lines
= 0;
61 static unsigned int numlockmask
= 0;
62 static unsigned int mw
, mh
;
63 static unsigned long normcol
[ColLast
];
64 static unsigned long selcol
[ColLast
];
65 static Bool running
= True
;
68 static Item
*allitems
= NULL
; /* first of all items */
69 static Item
*item
= NULL
; /* first of pattern matching items */
70 static Item
*sel
= NULL
;
71 static Item
*next
= NULL
;
72 static Item
*prev
= NULL
;
73 static Item
*curr
= NULL
;
74 static Window win
, parent
;
75 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
76 static char *(*fstrstr
)(const char *, const char *) = strstr
;
77 static void (*calcoffsets
)(void) = calcoffsetsh
;
80 appenditem(Item
*i
, Item
**list
, Item
**last
) {
94 w
= promptw
+ cmdw
+ (2 * spaceitem
);
95 for(next
= curr
; next
; next
= next
->right
)
96 if((w
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
98 w
= promptw
+ cmdw
+ (2 * spaceitem
);
99 for(prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
100 if((w
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
109 for(i
= 0; i
< lines
&& next
; i
++)
111 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
116 cistrstr(const char *s
, const char *sub
) {
122 if((c
= tolower(*sub
++)) != '\0') {
126 if((csub
= *s
++) == '\0')
129 while(tolower(csub
) != c
);
131 while(strncasecmp(s
, sub
, len
) != 0);
142 itm
= allitems
->next
;
143 free(allitems
->text
);
148 XDestroyWindow(dpy
, win
);
149 XUngrabKeyboard(dpy
, CurrentTime
);
157 execvp("dinput", argp
);
158 eprint("cannot exec dinput\n");
167 drawtext(&dc
, NULL
, normcol
, False
);
171 drawtext(&dc
, prompt
, selcol
, False
);
176 if(cmdw
&& item
&& lines
== 0)
178 drawtext(&dc
, *text
? text
: NULL
, normcol
, False
);
185 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
195 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
, False
);
197 for(i
= curr
; i
!= next
; i
= i
->right
) {
198 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
199 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
, False
);
204 drawtext(&dc
, next
? ">" : NULL
, normcol
, False
);
212 dc
.h
= dc
.font
.height
+ 2;
214 for(i
= curr
; i
!= next
; i
= i
->right
) {
215 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
, False
);
219 drawtext(&dc
, NULL
, normcol
, False
);
226 for(len
= 1000; len
; len
--) {
227 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
236 kpress(XKeyEvent
*e
) {
237 char buf
[sizeof text
];
243 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
244 if(ksym
== XK_KP_Enter
)
246 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
247 ksym
= (ksym
- XK_KP_0
) + XK_0
;
248 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
249 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
250 || IsPrivateKeypadKey(ksym
))
252 /* first check if a control mask is omitted */
253 if(e
->state
& ControlMask
) {
254 switch(tolower(ksym
)) {
296 while(i
-- > 0 && text
[i
] == ' ');
297 while(i
-- > 0 && text
[i
] != ' ');
308 num
= MIN(num
, sizeof text
);
309 if(num
&& !iscntrl((int) buf
[0])) {
310 memcpy(text
+ len
, buf
, num
+ 1);
318 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
328 while(sel
&& sel
->right
)
341 if(!sel
|| !sel
->left
)
344 if(sel
->right
== curr
) {
362 if((e
->state
& ShiftMask
) || !sel
)
363 fprintf(stdout
, "%s", text
);
365 fprintf(stdout
, "%s", sel
->text
);
371 if(!sel
|| !sel
->right
)
381 strncpy(text
, sel
->text
, sizeof text
);
389 match(char *pattern
) {
391 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
395 plen
= strlen(pattern
);
396 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
397 for(i
= allitems
; i
; i
= i
->next
)
398 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
399 appenditem(i
, &lexact
, &exactend
);
400 else if(!fstrncmp(pattern
, i
->text
, plen
))
401 appenditem(i
, &lprefix
, &prefixend
);
402 else if(fstrstr(i
->text
, pattern
))
403 appenditem(i
, &lsubstr
, &substrend
);
410 itemend
->right
= lprefix
;
411 lprefix
->left
= itemend
;
419 itemend
->right
= lsubstr
;
420 lsubstr
->left
= itemend
;
425 curr
= prev
= next
= sel
= item
;
431 char *p
, buf
[sizeof text
];
432 unsigned int len
= 0, max
= 0;
436 while(fgets(buf
, sizeof buf
, stdin
)) {
438 if(buf
[len
-1] == '\n')
440 if(!(p
= strdup(buf
)))
441 eprint("cannot strdup %u bytes\n", len
);
442 if((max
= MAX(max
, len
)) == len
)
444 if(!(new = malloc(sizeof *new)))
445 eprint("cannot malloc %u bytes\n", sizeof *new);
446 new->next
= new->left
= new->right
= NULL
;
460 /* main event loop */
461 while(running
&& !XNextEvent(dpy
, &ev
))
467 if(ev
.xexpose
.count
== 0)
470 case VisibilityNotify
:
471 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
472 XRaiseWindow(dpy
, win
);
482 XineramaScreenInfo
*info
= NULL
;
484 XModifierKeymap
*modmap
;
485 XSetWindowAttributes wa
;
486 XWindowAttributes pwa
;
488 /* init modifier map */
489 modmap
= XGetModifierMapping(dpy
);
490 for(i
= 0; i
< 8; i
++)
491 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
492 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
493 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
494 numlockmask
= (1 << i
);
496 XFreeModifiermap(modmap
);
499 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
500 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
501 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
502 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
506 wa
.override_redirect
= True
;
507 wa
.background_pixmap
= ParentRelative
;
508 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
510 /* menu window geometry */
511 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
513 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
519 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
520 for(i
= 0; i
< n
; i
++)
521 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
525 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
532 XGetWindowAttributes(dpy
, parent
, &pwa
);
534 y
= topbar
? 0 : pwa
.height
- mh
;
538 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
539 DefaultDepth(dpy
, screen
), CopyFromParent
,
540 DefaultVisual(dpy
, screen
),
541 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
545 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);
547 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
550 XMapRaised(dpy
, win
);
554 main(int argc
, char *argv
[]) {
558 /* command line args */
560 for(i
= 1; i
< argc
; i
++)
561 if(!strcmp(argv
[i
], "-i")) {
562 fstrncmp
= strncasecmp
;
565 else if(!strcmp(argv
[i
], "-b"))
567 else if(!strcmp(argv
[i
], "-e")) {
568 if(++i
< argc
) parent
= atoi(argv
[i
]);
570 else if(!strcmp(argv
[i
], "-l")) {
571 if(++i
< argc
) lines
= atoi(argv
[i
]);
573 calcoffsets
= calcoffsetsv
;
575 else if(!strcmp(argv
[i
], "-fn")) {
576 if(++i
< argc
) font
= argv
[i
];
578 else if(!strcmp(argv
[i
], "-nb")) {
579 if(++i
< argc
) normbgcolor
= argv
[i
];
581 else if(!strcmp(argv
[i
], "-nf")) {
582 if(++i
< argc
) normfgcolor
= argv
[i
];
584 else if(!strcmp(argv
[i
], "-p")) {
585 if(++i
< argc
) prompt
= argv
[i
];
587 else if(!strcmp(argv
[i
], "-sb")) {
588 if(++i
< argc
) selbgcolor
= argv
[i
];
590 else if(!strcmp(argv
[i
], "-sf")) {
591 if(++i
< argc
) selfgcolor
= argv
[i
];
593 else if(!strcmp(argv
[i
], "-v")) {
594 printf("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
598 fputs("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
599 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr
);
602 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
603 fprintf(stderr
, "dmenu: warning: no locale support\n");
604 if(!(dpy
= XOpenDisplay(NULL
)))
605 eprint("cannot open display\n");
606 screen
= DefaultScreen(dpy
);
608 parent
= RootWindow(dpy
, screen
);
609 if(!(argp
= malloc(sizeof *argp
* (argc
+2))))
610 eprint("cannot malloc %u bytes\n", sizeof *argp
* (argc
+2));
611 memcpy(argp
+ 2, argv
+ 1, sizeof *argp
* argc
);
614 running
= grabkeyboard();