Xinqi Bao's Git
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 void 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;
56 static unsigned int lines
= 0;
57 static unsigned int numlockmask
= 0;
58 static unsigned int mw
, mh
;
59 static unsigned long normcol
[ColLast
];
60 static unsigned long selcol
[ColLast
];
61 static Bool topbar
= True
;
64 static Item
*allitems
= NULL
; /* first of all items */
65 static Item
*item
= NULL
; /* first of pattern matching items */
66 static Item
*sel
= NULL
;
67 static Item
*next
= NULL
;
68 static Item
*prev
= NULL
;
69 static Item
*curr
= NULL
;
70 static Window win
, root
;
71 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
72 static char *(*fstrstr
)(const char *, const char *) = strstr
;
73 static void (*calcoffsets
)(void) = calcoffsetsh
;
76 appenditem(Item
*i
, Item
**list
, Item
**last
) {
90 x
= promptw
+ cmdw
+ (2 * spaceitem
);
91 for(next
= curr
; next
; next
= next
->right
)
92 if((x
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
94 x
= promptw
+ cmdw
+ (2 * spaceitem
);
95 for(prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
96 if((x
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
105 for(i
= 0; i
< lines
&& next
; i
++)
107 mh
= (dc
.font
.height
+ 2) * (i
+ 1);
108 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
113 cistrstr(const char *s
, const char *sub
) {
119 if((c
= tolower(*sub
++)) != '\0') {
123 if((csub
= *s
++) == '\0')
126 while(tolower(csub
) != c
);
128 while(strncasecmp(s
, sub
, len
) != 0);
139 itm
= allitems
->next
;
140 free(allitems
->text
);
145 XDestroyWindow(dpy
, win
);
146 XUngrabKeyboard(dpy
, CurrentTime
);
155 execvp("dinput", argp
);
156 eprint("cannot exec dinput\n");
165 drawtext(&dc
, NULL
, normcol
, False
);
166 dc
.h
= dc
.font
.height
+ 2;
167 dc
.y
= topbar
? 0 : mh
- dc
.h
;
171 drawtext(&dc
, prompt
, selcol
, False
);
176 if(cmdw
&& item
&& lines
== 0)
178 drawtext(&dc
, *text
? text
: NULL
, normcol
, False
);
183 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
192 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
, False
);
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
, False
);
201 drawtext(&dc
, next
? ">" : NULL
, normcol
, False
);
207 XWindowAttributes wa
;
209 dc
.y
= topbar
? dc
.h
: 0;
211 for(i
= curr
; i
!= next
; i
= i
->right
) {
212 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
, False
);
215 if(!XGetWindowAttributes(dpy
, win
, &wa
))
216 eprint("cannot get window attributes");
217 XMoveResizeWindow(dpy
, win
, wa
.x
, wa
.y
+ (topbar
? 0 : wa
.height
- mh
), mw
, mh
);
224 for(len
= 1000; len
; len
--) {
225 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
235 kpress(XKeyEvent
*e
) {
236 char buf
[sizeof text
];
242 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
243 if(ksym
== XK_KP_Enter
)
245 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
246 ksym
= (ksym
- XK_KP_0
) + XK_0
;
247 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
248 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
249 || IsPrivateKeypadKey(ksym
))
251 /* first check if a control mask is omitted */
252 if(e
->state
& ControlMask
) {
253 switch(tolower(ksym
)) {
295 while(i
-- > 0 && text
[i
] == ' ');
296 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
)
335 if(!sel
|| !sel
->left
)
338 if(sel
->right
== curr
) {
356 if(e
->state
& ShiftMask
)
358 fprintf(stdout
, "%s", sel
? sel
->text
: text
);
363 if(!sel
|| !sel
->right
)
373 strncpy(text
, sel
->text
, sizeof text
);
381 match(char *pattern
) {
383 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
387 plen
= strlen(pattern
);
388 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
389 for(i
= allitems
; i
; i
= i
->next
)
390 if(!fstrncmp(pattern
, i
->text
, plen
+ 1))
391 appenditem(i
, &lexact
, &exactend
);
392 else if(!fstrncmp(pattern
, i
->text
, plen
))
393 appenditem(i
, &lprefix
, &prefixend
);
394 else if(fstrstr(i
->text
, pattern
))
395 appenditem(i
, &lsubstr
, &substrend
);
402 itemend
->right
= lprefix
;
403 lprefix
->left
= itemend
;
411 itemend
->right
= lsubstr
;
412 lsubstr
->left
= itemend
;
417 curr
= prev
= next
= sel
= item
;
423 char *p
, buf
[sizeof text
];
424 unsigned int len
= 0, max
= 0;
428 while(fgets(buf
, sizeof buf
, stdin
)) {
430 if(buf
[len
-1] == '\n')
432 if(!(p
= strdup(buf
)))
433 eprint("cannot strdup %u bytes\n", len
);
434 if((max
= MAX(max
, len
)) == len
)
436 if(!(new = malloc(sizeof *new)))
437 eprint("cannot malloc %u bytes\n", sizeof *new);
438 new->next
= new->left
= new->right
= NULL
;
452 /* main event loop */
454 while(!XNextEvent(dpy
, &ev
))
460 if(ev
.xexpose
.count
== 0)
463 case VisibilityNotify
:
464 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
465 XRaiseWindow(dpy
, win
);
476 XineramaScreenInfo
*info
= NULL
;
478 XModifierKeymap
*modmap
;
479 XSetWindowAttributes wa
;
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
);
492 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
493 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
494 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
495 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
499 wa
.override_redirect
= True
;
500 wa
.background_pixmap
= ParentRelative
;
501 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
503 /* menu window geometry */
504 mh
= (dc
.font
.height
+ 2) * (lines
+ 1);
506 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
512 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
513 for(i
= 0; i
< n
; i
++)
514 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
518 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
526 y
= topbar
? 0 : mh
- DisplayHeight(dpy
, screen
);
527 mw
= DisplayWidth(dpy
, screen
);
530 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
531 DefaultDepth(dpy
, screen
), CopyFromParent
,
532 DefaultVisual(dpy
, screen
),
533 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
537 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);
539 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
542 XMapRaised(dpy
, win
);
546 main(int argc
, char *argv
[]) {
549 /* command line args */
551 for(i
= 1; i
< argc
; i
++)
552 if(!strcmp(argv
[i
], "-i")) {
553 fstrncmp
= strncasecmp
;
556 else if(!strcmp(argv
[i
], "-b"))
558 else if(!strcmp(argv
[i
], "-l")) {
559 if(++i
< argc
) lines
= atoi(argv
[i
]);
561 calcoffsets
= calcoffsetsv
;
563 else if(!strcmp(argv
[i
], "-fn")) {
564 if(++i
< argc
) font
= argv
[i
];
566 else if(!strcmp(argv
[i
], "-nb")) {
567 if(++i
< argc
) normbgcolor
= argv
[i
];
569 else if(!strcmp(argv
[i
], "-nf")) {
570 if(++i
< argc
) normfgcolor
= argv
[i
];
572 else if(!strcmp(argv
[i
], "-p")) {
573 if(++i
< argc
) prompt
= argv
[i
];
575 else if(!strcmp(argv
[i
], "-sb")) {
576 if(++i
< argc
) selbgcolor
= argv
[i
];
578 else if(!strcmp(argv
[i
], "-sf")) {
579 if(++i
< argc
) selfgcolor
= argv
[i
];
581 else if(!strcmp(argv
[i
], "-v")) {
582 printf("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
586 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
587 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr
);
590 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
591 fprintf(stderr
, "dmenu: warning: no locale support\n");
592 if(!(dpy
= XOpenDisplay(NULL
)))
593 eprint("cannot open display\n");
594 if(atexit(&cleanup
) != 0)
595 eprint("cannot register cleanup\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
);