Xinqi Bao's Git
680b4e705767e2834f0a17ca974b4f2f9e4375d8
1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
17 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
20 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
22 typedef struct Item Item
;
25 Item
*next
; /* traverses all items */
26 Item
*left
, *right
; /* traverses items matching current search pattern */
29 /* forward declarations */
30 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
31 static void calcoffsetsh(void);
32 static void calcoffsetsv(void);
33 static char *cistrstr(const char *s
, const char *sub
);
34 static void cleanup(void);
35 static void dinput(void);
36 static void drawmenu(void);
37 static void drawmenuh(void);
38 static void drawmenuv(void);
39 static Bool
grabkeyboard(void);
40 static void kpress(XKeyEvent
*e
);
41 static void match(char *pattern
);
42 static void readstdin(void);
43 static void run(void);
44 static void setup(void);
49 static char **argp
= NULL
;
50 static char *maxname
= NULL
;
51 static char *prompt
= NULL
;
52 static char text
[4096];
54 static int promptw
= 0;
57 static unsigned int lines
= 0;
58 static unsigned int numlockmask
= 0;
59 static unsigned int mw
, mh
;
60 static unsigned long normcol
[ColLast
];
61 static unsigned long selcol
[ColLast
];
62 static Bool running
= True
;
63 static Bool topbar
= True
;
66 static Item
*allitems
= NULL
; /* first of all items */
67 static Item
*item
= NULL
; /* first of pattern matching items */
68 static Item
*sel
= NULL
;
69 static Item
*next
= NULL
;
70 static Item
*prev
= NULL
;
71 static Item
*curr
= NULL
;
72 static Window win
, root
;
73 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
74 static char *(*fstrstr
)(const char *, const char *) = strstr
;
75 static void (*calcoffsets
)(void) = calcoffsetsh
;
78 appenditem(Item
*i
, Item
**list
, Item
**last
) {
92 w
= promptw
+ cmdw
+ (2 * spaceitem
);
93 for(next
= curr
; next
; next
= next
->right
)
94 if((w
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
96 w
= promptw
+ cmdw
+ (2 * spaceitem
);
97 for(prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
98 if((w
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
107 for(i
= 0; i
< lines
&& next
; i
++)
109 mh
= (dc
.font
.height
+ 2) * (i
+ 1);
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
);
156 execvp("dinput", argp
);
157 eprint("cannot exec dinput\n");
166 drawtext(&dc
, NULL
, normcol
, False
);
167 dc
.h
= dc
.font
.height
+ 2;
168 dc
.y
= topbar
? 0 : mh
- dc
.h
;
172 drawtext(&dc
, prompt
, selcol
, False
);
177 if(cmdw
&& item
&& lines
== 0)
179 drawtext(&dc
, *text
? text
: NULL
, normcol
, False
);
184 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
194 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
, False
);
196 for(i
= curr
; i
!= next
; i
= i
->right
) {
197 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
198 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
, False
);
203 drawtext(&dc
, next
? ">" : NULL
, normcol
, False
);
209 XWindowAttributes wa
;
211 dc
.y
= topbar
? dc
.h
: 0;
213 for(i
= curr
; i
!= next
; i
= i
->right
) {
214 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
, False
);
217 if(!XGetWindowAttributes(dpy
, win
, &wa
))
218 eprint("cannot get window attributes");
219 XMoveResizeWindow(dpy
, win
, wa
.x
, wa
.y
+ (topbar
? 0 : wa
.height
- mh
), mw
, mh
);
226 for(len
= 1000; len
; len
--) {
227 if(XGrabKeyboard(dpy
, root
, 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
] != ' ');
305 num
= MIN(num
, sizeof text
);
306 if(num
&& !iscntrl((int) buf
[0])) {
307 memcpy(text
+ len
, buf
, num
+ 1);
315 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
325 while(sel
&& sel
->right
)
338 if(!sel
|| !sel
->left
)
341 if(sel
->right
== curr
) {
359 if(e
->state
& ShiftMask
)
361 fprintf(stdout
, "%s", sel
? sel
->text
: 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
;
483 /* init modifier map */
484 modmap
= XGetModifierMapping(dpy
);
485 for(i
= 0; i
< 8; i
++)
486 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
487 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
488 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
489 numlockmask
= (1 << i
);
491 XFreeModifiermap(modmap
);
494 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
495 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
496 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
497 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
501 wa
.override_redirect
= True
;
502 wa
.background_pixmap
= ParentRelative
;
503 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
505 /* menu window geometry */
506 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
508 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
514 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
515 for(i
= 0; i
< n
; i
++)
516 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
520 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
528 y
= topbar
? 0 : mh
- DisplayHeight(dpy
, screen
);
529 mw
= DisplayWidth(dpy
, screen
);
532 win
= XCreateWindow(dpy
, root
, 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
[]) {
551 /* command line args */
553 for(i
= 1; i
< argc
; i
++)
554 if(!strcmp(argv
[i
], "-i")) {
555 fstrncmp
= strncasecmp
;
558 else if(!strcmp(argv
[i
], "-b"))
560 else if(!strcmp(argv
[i
], "-l")) {
561 if(++i
< argc
) lines
= atoi(argv
[i
]);
563 calcoffsets
= calcoffsetsv
;
565 else if(!strcmp(argv
[i
], "-fn")) {
566 if(++i
< argc
) font
= argv
[i
];
568 else if(!strcmp(argv
[i
], "-nb")) {
569 if(++i
< argc
) normbgcolor
= argv
[i
];
571 else if(!strcmp(argv
[i
], "-nf")) {
572 if(++i
< argc
) normfgcolor
= argv
[i
];
574 else if(!strcmp(argv
[i
], "-p")) {
575 if(++i
< argc
) prompt
= argv
[i
];
577 else if(!strcmp(argv
[i
], "-sb")) {
578 if(++i
< argc
) selbgcolor
= argv
[i
];
580 else if(!strcmp(argv
[i
], "-sf")) {
581 if(++i
< argc
) selfgcolor
= argv
[i
];
583 else if(!strcmp(argv
[i
], "-v")) {
584 printf("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
588 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
589 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr
);
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
);
597 root
= RootWindow(dpy
, screen
);
598 if(!(argp
= malloc(sizeof *argp
* (argc
+2))))
599 eprint("cannot malloc %u bytes\n", sizeof *argp
* (argc
+2));
600 memcpy(argp
+ 2, argv
+ 1, sizeof *argp
* argc
);
603 running
= grabkeyboard();