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 drawmenu(void);
38 static void drawmenuh(void);
39 static void drawmenuv(void);
40 static Bool
grabkeyboard(void);
41 static void kpress(XKeyEvent
* e
);
42 static void match(char *pattern
);
43 static void readstdin(void);
44 static void run(void);
45 static void setup(Bool topbar
);
51 static char *maxname
= NULL
;
52 static char *prompt
= NULL
;
53 static char text
[4096];
55 static int promptw
= 0;
57 static unsigned int lines
= 0;
58 static unsigned int numlockmask
= 0;
59 static Bool running
= True
;
60 static Item
*allitems
= NULL
; /* first of all items */
61 static Item
*item
= NULL
; /* first of pattern matching items */
62 static Item
*sel
= NULL
;
63 static Item
*next
= NULL
;
64 static Item
*prev
= NULL
;
65 static Item
*curr
= NULL
;
67 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
68 static char *(*fstrstr
)(const char *, const char *) = strstr
;
69 static void (*calcoffsets
)(void) = calcoffsetsh
;
78 appenditem(Item
*i
, Item
**list
, Item
**last
) {
94 w
= promptw
+ cmdw
+ 2 * spaceitem
;
95 for(next
= curr
; next
&& w
< mw
; next
=next
->right
)
96 w
+= MIN(textw(next
->text
), mw
/ 3);
97 w
= promptw
+ cmdw
+ 2 * spaceitem
;
98 for(prev
= curr
; prev
&& prev
->left
&& w
< mw
; prev
=prev
->left
)
99 w
+= MIN(textw(prev
->left
->text
), mw
/ 3);
108 h
= (dc
.font
.height
+ 2) * lines
;
109 for(next
= curr
; next
&& h
> 0; next
= next
->right
)
110 h
-= dc
.font
.height
+ 2;
111 h
= (dc
.font
.height
+ 2) * lines
;
112 for(prev
= curr
; prev
&& prev
->left
&& h
> 0; prev
= prev
->left
)
113 h
-= dc
.font
.height
+ 2;
117 cistrstr(const char *s
, const char *sub
) {
123 if((c
= tolower(*sub
++)) != '\0') {
127 if((csub
= *s
++) == '\0')
130 while(tolower(csub
) != c
);
132 while(strncasecmp(s
, sub
, len
) != 0);
143 itm
= allitems
->next
;
144 free(allitems
->text
);
149 XDestroyWindow(dpy
, win
);
150 XUngrabKeyboard(dpy
, CurrentTime
);
159 drawtext(NULL
, dc
.norm
);
163 drawtext(prompt
, dc
.sel
);
168 if(cmdw
&& item
&& lines
== 0)
170 drawtext(*text
? text
: NULL
, dc
.norm
);
177 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
187 drawtext(curr
->left
? "<" : NULL
, dc
.norm
);
189 for(i
= curr
; i
!= next
; i
=i
->right
) {
190 dc
.w
= MIN(textw(i
->text
), mw
/ 3);
191 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
196 drawtext(next
? ">" : NULL
, dc
.norm
);
204 dc
.h
= dc
.font
.height
+ 2;
206 for(i
= curr
; i
!= next
; i
=i
->right
) {
207 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
211 drawtext(NULL
, dc
.norm
);
218 for(len
= 1000; len
; len
--) {
219 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
228 kpress(XKeyEvent
* e
) {
229 char buf
[sizeof text
];
235 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
236 if(ksym
== XK_KP_Enter
)
238 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
239 ksym
= (ksym
- XK_KP_0
) + XK_0
;
240 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
241 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
242 || IsPrivateKeypadKey(ksym
))
244 /* first check if a control mask is omitted */
245 if(e
->state
& ControlMask
) {
246 switch(tolower(ksym
)) {
287 while(i
-- > 0 && text
[i
] == ' ');
288 while(i
-- > 0 && text
[i
] != ' ');
293 execlp("dinput", "dinput", text
, NULL
); /* todo: argv */
294 eprint("dmenu: cannot exec dinput:");
300 num
= MIN(num
, sizeof text
);
301 if(num
&& !iscntrl((int) buf
[0])) {
302 memcpy(text
+ len
, buf
, num
+ 1);
310 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
320 while(sel
&& sel
->right
)
333 if(!sel
|| !sel
->left
)
336 if(sel
->right
== curr
) {
354 if((e
->state
& ShiftMask
) || !sel
)
355 fprintf(stdout
, "%s", text
);
357 fprintf(stdout
, "%s", sel
->text
);
363 if(!sel
|| !sel
->right
)
374 strncpy(text
, sel
->text
, sizeof text
);
382 match(char *pattern
) {
384 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
388 plen
= strlen(pattern
);
389 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
390 for(i
= allitems
; i
; i
= i
->next
)
391 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
392 appenditem(i
, &lexact
, &exactend
);
393 else if(!fstrncmp(pattern
, i
->text
, plen
))
394 appenditem(i
, &lprefix
, &prefixend
);
395 else if(fstrstr(i
->text
, pattern
))
396 appenditem(i
, &lsubstr
, &substrend
);
403 itemend
->right
= lprefix
;
404 lprefix
->left
= itemend
;
412 itemend
->right
= lsubstr
;
413 lsubstr
->left
= itemend
;
418 curr
= prev
= next
= sel
= item
;
424 char *p
, buf
[sizeof text
];
425 unsigned int len
= 0, max
= 0;
429 while(fgets(buf
, sizeof buf
, stdin
)) {
431 if(buf
[len
-1] == '\n')
433 if(!(p
= strdup(buf
)))
434 eprint("dmenu: cannot strdup %u bytes\n", len
);
435 if((max
= MAX(max
, len
)) == len
)
437 if(!(new = malloc(sizeof *new)))
438 eprint("dmenu: cannot malloc %u bytes\n", sizeof *new);
439 new->next
= new->left
= new->right
= NULL
;
453 /* main event loop */
454 while(running
&& !XNextEvent(dpy
, &ev
))
460 if(ev
.xexpose
.count
== 0)
463 case VisibilityNotify
:
464 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
465 XRaiseWindow(dpy
, win
);
475 XineramaScreenInfo
*info
= NULL
;
477 XModifierKeymap
*modmap
;
478 XSetWindowAttributes wa
;
479 XWindowAttributes pwa
;
481 /* init modifier map */
482 modmap
= XGetModifierMapping(dpy
);
483 for(i
= 0; i
< 8; i
++)
484 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
485 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
486 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
487 numlockmask
= (1 << i
);
489 XFreeModifiermap(modmap
);
494 wa
.override_redirect
= True
;
495 wa
.background_pixmap
= ParentRelative
;
496 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
498 /* menu window geometry */
499 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
501 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
507 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
508 for(i
= 0; i
< n
; i
++)
509 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
513 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
520 XGetWindowAttributes(dpy
, parent
, &pwa
);
522 y
= topbar
? 0 : pwa
.height
- mh
;
526 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
527 DefaultDepth(dpy
, screen
), CopyFromParent
,
528 DefaultVisual(dpy
, screen
),
529 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
533 cmdw
= MIN(textw(maxname
), mw
/ 3);
535 promptw
= MIN(textw(prompt
), mw
/ 5);
538 XMapRaised(dpy
, win
);
542 main(int argc
, char *argv
[]) {
546 /* command line args */
547 for(i
= 1; i
< argc
; i
++)
548 if(!strcmp(argv
[i
], "-i")) {
549 fstrncmp
= strncasecmp
;
552 else if(!strcmp(argv
[i
], "-b"))
554 else if(!strcmp(argv
[i
], "-e")) {
555 if(++i
< argc
) parent
= atoi(argv
[i
]);
557 else if(!strcmp(argv
[i
], "-l")) {
558 if(++i
< argc
) lines
= atoi(argv
[i
]);
560 calcoffsets
= calcoffsetsv
;
562 else if(!strcmp(argv
[i
], "-fn")) {
563 if(++i
< argc
) font
= argv
[i
];
565 else if(!strcmp(argv
[i
], "-nb")) {
566 if(++i
< argc
) normbgcolor
= argv
[i
];
568 else if(!strcmp(argv
[i
], "-nf")) {
569 if(++i
< argc
) normfgcolor
= argv
[i
];
571 else if(!strcmp(argv
[i
], "-p")) {
572 if(++i
< argc
) prompt
= argv
[i
];
574 else if(!strcmp(argv
[i
], "-sb")) {
575 if(++i
< argc
) selbgcolor
= argv
[i
];
577 else if(!strcmp(argv
[i
], "-sf")) {
578 if(++i
< argc
) selfgcolor
= argv
[i
];
580 else if(!strcmp(argv
[i
], "-v"))
581 eprint("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
583 eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
584 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
585 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
586 fprintf(stderr
, "dmenu: warning: no locale support\n");
587 if(!(dpy
= XOpenDisplay(NULL
)))
588 eprint("dmenu: cannot open display\n");
589 screen
= DefaultScreen(dpy
);
591 parent
= RootWindow(dpy
, screen
);
594 running
= grabkeyboard();