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 void eprint(const char *errstr
, ...);
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;
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 unsigned int lines
= 0;
70 static void (*calcoffsets
)(void) = calcoffsetsh
;
73 appenditem(Item
*i
, Item
**list
, Item
**last
) {
89 w
= promptw
+ cmdw
+ 2 * spaceitem
;
90 for(next
= curr
; next
&& w
< mw
; next
=next
->right
)
91 w
+= MIN(textw(next
->text
), mw
/ 3);
92 w
= promptw
+ cmdw
+ 2 * spaceitem
;
93 for(prev
= curr
; prev
&& prev
->left
&& w
< mw
; prev
=prev
->left
)
94 w
+= MIN(textw(prev
->left
->text
), mw
/ 3);
103 h
= (dc
.font
.height
+ 2) * lines
;
104 for(next
= curr
; next
&& h
> 0; next
= next
->right
)
105 h
-= dc
.font
.height
+ 2;
106 h
= (dc
.font
.height
+ 2) * lines
;
107 for(prev
= curr
; prev
&& prev
->left
&& h
> 0; prev
= prev
->left
)
108 h
-= dc
.font
.height
+ 2;
112 cistrstr(const char *s
, const char *sub
) {
118 if((c
= tolower(*sub
++)) != '\0') {
122 if((csub
= *s
++) == '\0')
125 while(tolower(csub
) != c
);
127 while(strncasecmp(s
, sub
, len
) != 0);
136 XDestroyWindow(dpy
, win
);
137 XUngrabKeyboard(dpy
, CurrentTime
);
146 drawtext(NULL
, dc
.norm
);
150 drawtext(prompt
, dc
.sel
);
155 if(cmdw
&& item
&& lines
== 0)
157 drawtext(*text
? text
: NULL
, dc
.norm
);
164 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
174 drawtext(curr
->left
? "<" : NULL
, dc
.norm
);
176 for(i
= curr
; i
!= next
; i
=i
->right
) {
177 dc
.w
= MIN(textw(i
->text
), mw
/ 3);
178 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
183 drawtext(next
? ">" : NULL
, dc
.norm
);
191 dc
.h
= dc
.font
.height
+ 2;
193 for(i
= curr
; i
!= next
; i
=i
->right
) {
194 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
198 drawtext(NULL
, dc
.norm
);
205 for(len
= 1000; len
; len
--) {
206 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
215 kpress(XKeyEvent
* e
) {
216 char buf
[sizeof text
];
222 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
223 if(ksym
== XK_KP_Enter
)
225 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
226 ksym
= (ksym
- XK_KP_0
) + XK_0
;
227 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
228 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
229 || IsPrivateKeypadKey(ksym
))
231 /* first check if a control mask is omitted */
232 if(e
->state
& ControlMask
) {
233 switch(tolower(ksym
)) {
274 while(i
-- > 0 && text
[i
] == ' ');
275 while(i
-- > 0 && text
[i
] != ' ');
280 execlp("dinput", "dinput", text
, NULL
); /* todo: argv */
281 eprint("dmenu: cannot exec dinput:");
287 num
= MIN(num
, sizeof text
);
288 if(num
&& !iscntrl((int) buf
[0])) {
289 memcpy(text
+ len
, buf
, num
+ 1);
297 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
307 while(sel
&& sel
->right
)
320 if(!sel
|| !sel
->left
)
323 if(sel
->right
== curr
) {
341 if((e
->state
& ShiftMask
) || !sel
)
342 fprintf(stdout
, "%s", text
);
344 fprintf(stdout
, "%s", sel
->text
);
350 if(!sel
|| !sel
->right
)
361 strncpy(text
, sel
->text
, sizeof text
);
369 match(char *pattern
) {
371 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
375 plen
= strlen(pattern
);
376 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
377 for(i
= allitems
; i
; i
= i
->next
)
378 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
379 appenditem(i
, &lexact
, &exactend
);
380 else if(!fstrncmp(pattern
, i
->text
, plen
))
381 appenditem(i
, &lprefix
, &prefixend
);
382 else if(fstrstr(i
->text
, pattern
))
383 appenditem(i
, &lsubstr
, &substrend
);
390 itemend
->right
= lprefix
;
391 lprefix
->left
= itemend
;
399 itemend
->right
= lsubstr
;
400 lsubstr
->left
= itemend
;
405 curr
= prev
= next
= sel
= item
;
411 char *p
, buf
[sizeof text
];
412 unsigned int len
= 0, max
= 0;
416 while(fgets(buf
, sizeof buf
, stdin
)) {
418 if(buf
[len
-1] == '\n')
420 if(!(p
= strdup(buf
)))
421 eprint("dmenu: cannot strdup %u bytes\n", len
);
422 if((max
= MAX(max
, len
)) == len
)
424 if(!(new = malloc(sizeof *new)))
425 eprint("dmenu: cannot malloc %u bytes\n", sizeof *new);
426 new->next
= new->left
= new->right
= NULL
;
440 /* main event loop */
441 while(running
&& !XNextEvent(dpy
, &ev
))
447 if(ev
.xexpose
.count
== 0)
450 case VisibilityNotify
:
451 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
452 XRaiseWindow(dpy
, win
);
462 XineramaScreenInfo
*info
= NULL
;
464 XModifierKeymap
*modmap
;
465 XSetWindowAttributes wa
;
466 XWindowAttributes pwa
;
468 /* init modifier map */
469 modmap
= XGetModifierMapping(dpy
);
470 for(i
= 0; i
< 8; i
++)
471 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
472 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
473 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
474 numlockmask
= (1 << i
);
476 XFreeModifiermap(modmap
);
481 wa
.override_redirect
= True
;
482 wa
.background_pixmap
= ParentRelative
;
483 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
485 /* menu window geometry */
486 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
488 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
494 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
495 for(i
= 0; i
< n
; i
++)
496 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
500 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
507 XGetWindowAttributes(dpy
, parent
, &pwa
);
509 y
= topbar
? 0 : pwa
.height
- mh
;
513 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
514 DefaultDepth(dpy
, screen
), CopyFromParent
,
515 DefaultVisual(dpy
, screen
),
516 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
520 cmdw
= MIN(textw(maxname
), mw
/ 3);
522 promptw
= MIN(textw(prompt
), mw
/ 5);
525 XMapRaised(dpy
, win
);
529 main(int argc
, char *argv
[]) {
533 /* command line args */
534 for(i
= 1; i
< argc
; i
++)
535 if(!strcmp(argv
[i
], "-i")) {
536 fstrncmp
= strncasecmp
;
539 else if(!strcmp(argv
[i
], "-b"))
541 else if(!strcmp(argv
[i
], "-e")) {
542 if(++i
< argc
) parent
= atoi(argv
[i
]);
544 else if(!strcmp(argv
[i
], "-l")) {
545 if(++i
< argc
) lines
= atoi(argv
[i
]);
547 calcoffsets
= calcoffsetsv
;
549 else if(!strcmp(argv
[i
], "-fn")) {
550 if(++i
< argc
) font
= argv
[i
];
552 else if(!strcmp(argv
[i
], "-nb")) {
553 if(++i
< argc
) normbgcolor
= argv
[i
];
555 else if(!strcmp(argv
[i
], "-nf")) {
556 if(++i
< argc
) normfgcolor
= argv
[i
];
558 else if(!strcmp(argv
[i
], "-p")) {
559 if(++i
< argc
) prompt
= argv
[i
];
561 else if(!strcmp(argv
[i
], "-sb")) {
562 if(++i
< argc
) selbgcolor
= argv
[i
];
564 else if(!strcmp(argv
[i
], "-sf")) {
565 if(++i
< argc
) selfgcolor
= argv
[i
];
567 else if(!strcmp(argv
[i
], "-v"))
568 eprint("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
570 eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
571 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
572 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
573 fprintf(stderr
, "dmenu: warning: no locale support\n");
574 if(!(dpy
= XOpenDisplay(NULL
)))
575 eprint("dmenu: cannot open display\n");
576 screen
= DefaultScreen(dpy
);
578 parent
= RootWindow(dpy
, screen
);
581 running
= grabkeyboard();