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 unsigned long normcol
[ColLast
];
63 static unsigned long selcol
[ColLast
];
64 static Bool running
= True
;
67 static Item
*allitems
= NULL
; /* first of all items */
68 static Item
*item
= NULL
; /* first of pattern matching items */
69 static Item
*sel
= NULL
;
70 static Item
*next
= NULL
;
71 static Item
*prev
= NULL
;
72 static Item
*curr
= NULL
;
73 static Window win
, parent
;
74 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
75 static char *(*fstrstr
)(const char *, const char *) = strstr
;
76 static void (*calcoffsets
)(void) = calcoffsetsh
;
79 appenditem(Item
*i
, Item
**list
, Item
**last
) {
93 w
= promptw
+ cmdw
+ (2 * spaceitem
);
94 for(next
= curr
; next
; next
= next
->right
)
95 if((w
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
97 w
= promptw
+ cmdw
+ (2 * spaceitem
);
98 for(prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
99 if((w
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
108 for(i
= 0; i
< lines
&& next
; i
++)
110 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
115 cistrstr(const char *s
, const char *sub
) {
121 if((c
= tolower(*sub
++)) != '\0') {
125 if((csub
= *s
++) == '\0')
128 while(tolower(csub
) != c
);
130 while(strncasecmp(s
, sub
, len
) != 0);
141 itm
= allitems
->next
;
142 free(allitems
->text
);
147 XDestroyWindow(dpy
, win
);
148 XUngrabKeyboard(dpy
, CurrentTime
);
154 execlp("dinput", "dinput", text
, NULL
); /* todo: argv */
155 eprint("cannot exec dinput\n");
164 drawtext(&dc
, NULL
, normcol
);
168 drawtext(&dc
, prompt
, selcol
);
173 if(cmdw
&& item
&& lines
== 0)
175 drawtext(&dc
, *text
? text
: NULL
, normcol
);
182 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
192 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
);
194 for(i
= curr
; i
!= next
; i
=i
->right
) {
195 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
196 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
);
201 drawtext(&dc
, next
? ">" : NULL
, normcol
);
209 dc
.h
= dc
.font
.height
+ 2;
211 for(i
= curr
; i
!= next
; i
=i
->right
) {
212 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
);
216 drawtext(&dc
, NULL
, normcol
);
223 for(len
= 1000; len
; len
--) {
224 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
233 kpress(XKeyEvent
* e
) {
234 char buf
[sizeof text
];
240 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
241 if(ksym
== XK_KP_Enter
)
243 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
244 ksym
= (ksym
- XK_KP_0
) + XK_0
;
245 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
246 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
247 || IsPrivateKeypadKey(ksym
))
249 /* first check if a control mask is omitted */
250 if(e
->state
& ControlMask
) {
251 switch(tolower(ksym
)) {
292 while(i
-- > 0 && text
[i
] == ' ');
293 while(i
-- > 0 && text
[i
] != ' ');
304 num
= MIN(num
, sizeof text
);
305 if(num
&& !iscntrl((int) buf
[0])) {
306 memcpy(text
+ len
, buf
, num
+ 1);
314 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
324 while(sel
&& sel
->right
)
337 if(!sel
|| !sel
->left
)
340 if(sel
->right
== curr
) {
358 if((e
->state
& ShiftMask
) || !sel
)
359 fprintf(stdout
, "%s", text
);
361 fprintf(stdout
, "%s", sel
->text
);
367 if(!sel
|| !sel
->right
)
377 strncpy(text
, sel
->text
, sizeof text
);
385 match(char *pattern
) {
387 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
391 plen
= strlen(pattern
);
392 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
393 for(i
= allitems
; i
; i
= i
->next
)
394 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
395 appenditem(i
, &lexact
, &exactend
);
396 else if(!fstrncmp(pattern
, i
->text
, plen
))
397 appenditem(i
, &lprefix
, &prefixend
);
398 else if(fstrstr(i
->text
, pattern
))
399 appenditem(i
, &lsubstr
, &substrend
);
406 itemend
->right
= lprefix
;
407 lprefix
->left
= itemend
;
415 itemend
->right
= lsubstr
;
416 lsubstr
->left
= itemend
;
421 curr
= prev
= next
= sel
= item
;
427 char *p
, buf
[sizeof text
];
428 unsigned int len
= 0, max
= 0;
432 while(fgets(buf
, sizeof buf
, stdin
)) {
434 if(buf
[len
-1] == '\n')
436 if(!(p
= strdup(buf
)))
437 eprint("cannot strdup %u bytes\n", len
);
438 if((max
= MAX(max
, len
)) == len
)
440 if(!(new = malloc(sizeof *new)))
441 eprint("cannot malloc %u bytes\n", sizeof *new);
442 new->next
= new->left
= new->right
= NULL
;
456 /* main event loop */
457 while(running
&& !XNextEvent(dpy
, &ev
))
463 if(ev
.xexpose
.count
== 0)
466 case VisibilityNotify
:
467 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
468 XRaiseWindow(dpy
, win
);
478 XineramaScreenInfo
*info
= NULL
;
480 XModifierKeymap
*modmap
;
481 XSetWindowAttributes wa
;
482 XWindowAttributes pwa
;
484 /* init modifier map */
485 modmap
= XGetModifierMapping(dpy
);
486 for(i
= 0; i
< 8; i
++)
487 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
488 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
489 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
490 numlockmask
= (1 << i
);
492 XFreeModifiermap(modmap
);
495 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
496 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
497 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
498 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
502 wa
.override_redirect
= True
;
503 wa
.background_pixmap
= ParentRelative
;
504 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
506 /* menu window geometry */
507 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
509 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
515 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
516 for(i
= 0; i
< n
; i
++)
517 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
521 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
528 XGetWindowAttributes(dpy
, parent
, &pwa
);
530 y
= topbar
? 0 : pwa
.height
- mh
;
534 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
535 DefaultDepth(dpy
, screen
), CopyFromParent
,
536 DefaultVisual(dpy
, screen
),
537 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
541 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);
543 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
546 XMapRaised(dpy
, win
);
550 main(int argc
, char *argv
[]) {
554 /* command line args */
556 for(i
= 1; i
< argc
; i
++)
557 if(!strcmp(argv
[i
], "-i")) {
558 fstrncmp
= strncasecmp
;
561 else if(!strcmp(argv
[i
], "-b"))
563 else if(!strcmp(argv
[i
], "-e")) {
564 if(++i
< argc
) parent
= atoi(argv
[i
]);
566 else if(!strcmp(argv
[i
], "-l")) {
567 if(++i
< argc
) lines
= atoi(argv
[i
]);
569 calcoffsets
= calcoffsetsv
;
571 else if(!strcmp(argv
[i
], "-fn")) {
572 if(++i
< argc
) font
= argv
[i
];
574 else if(!strcmp(argv
[i
], "-nb")) {
575 if(++i
< argc
) normbgcolor
= argv
[i
];
577 else if(!strcmp(argv
[i
], "-nf")) {
578 if(++i
< argc
) normfgcolor
= argv
[i
];
580 else if(!strcmp(argv
[i
], "-p")) {
581 if(++i
< argc
) prompt
= argv
[i
];
583 else if(!strcmp(argv
[i
], "-sb")) {
584 if(++i
< argc
) selbgcolor
= argv
[i
];
586 else if(!strcmp(argv
[i
], "-sf")) {
587 if(++i
< argc
) selfgcolor
= argv
[i
];
589 else if(!strcmp(argv
[i
], "-v"))
590 eprint("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
592 eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
593 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
594 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
595 fprintf(stderr
, "dmenu: warning: no locale support\n");
596 if(!(dpy
= XOpenDisplay(NULL
)))
597 eprint("cannot open display\n");
598 screen
= DefaultScreen(dpy
);
600 parent
= RootWindow(dpy
, screen
);
603 running
= grabkeyboard();