Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
13 typedef struct Item Item
;
16 Item
*next
; /* traverses all items */
17 Item
*left
, *right
; /* traverses items matching current search pattern */
20 /* forward declarations */
21 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
22 static void calcoffsetsh(void);
23 static void calcoffsetsv(void);
24 static char *cistrstr(const char *s
, const char *sub
);
25 static void cleanup(void);
26 static void dinput(void);
27 static void drawitem(const char *s
, unsigned long col
[ColLast
]);
28 static void drawmenuh(void);
29 static void drawmenuv(void);
30 static void match(void);
31 static void readstdin(void);
34 static char **argp
= NULL
;
35 static char *maxname
= NULL
;
36 static unsigned int cmdw
= 0;
37 static unsigned int lines
= 0;
38 static Item
*allitems
= NULL
; /* first of all items */
39 static Item
*item
= NULL
; /* first of pattern matching items */
40 static Item
*sel
= NULL
;
41 static Item
*next
= NULL
;
42 static Item
*prev
= NULL
;
43 static Item
*curr
= NULL
;
44 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
45 static char *(*fstrstr
)(const char *, const char *) = strstr
;
46 static void (*calcoffsets
)(void) = calcoffsetsh
;
49 appenditem(Item
*i
, Item
**list
, Item
**last
) {
63 w
= promptw
+ cmdw
+ textw(&dc
, "<") + textw(&dc
, ">");
64 for(x
= w
, next
= curr
; next
; next
= next
->right
)
65 if((x
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
67 for(x
= w
, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
68 if((x
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
77 for(i
= 0; i
< lines
&& next
; i
++)
79 mh
= (dc
.font
.height
+ 2) * (i
+ 1);
80 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
85 cistrstr(const char *s
, const char *sub
) {
91 if((c
= tolower(*sub
++)) != '\0') {
95 if((csub
= *s
++) == '\0')
98 while(tolower(csub
) != c
);
100 while(strncasecmp(s
, sub
, len
) != 0);
111 itm
= allitems
->next
;
112 free(allitems
->text
);
117 XDestroyWindow(dpy
, win
);
118 XUngrabKeyboard(dpy
, CurrentTime
);
127 execvp("dinput", argp
);
128 eprint("cannot exec dinput\n");
137 drawbox(&dc
, normcol
);
138 dc
.h
= dc
.font
.height
+ 2;
139 dc
.y
= topbar
? 0 : mh
- dc
.h
;
143 drawbox(&dc
, selcol
);
144 drawtext(&dc
, prompt
, selcol
);
149 if(cmdw
&& item
&& lines
== 0)
151 drawtext(&dc
, text
, normcol
);
156 commitdraw(&dc
, win
);
160 drawitem(const char *s
, unsigned long col
[ColLast
]) {
162 drawtext(&dc
, s
, col
);
170 dc
.w
= textw(&dc
, "<");
171 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
);
173 for(i
= curr
; i
!= next
; i
= i
->right
) {
174 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
175 drawitem(i
->text
, (sel
== i
) ? selcol
: normcol
);
178 dc
.w
= textw(&dc
, ">");
180 drawtext(&dc
, next
? ">" : NULL
, normcol
);
186 XWindowAttributes wa
;
188 dc
.y
= topbar
? dc
.h
: 0;
190 for(i
= curr
; i
!= next
; i
= i
->right
) {
191 drawitem(i
->text
, (sel
== i
) ? selcol
: normcol
);
194 if(!XGetWindowAttributes(dpy
, win
, &wa
))
195 eprint("cannot get window attributes");
196 XMoveResizeWindow(dpy
, win
, wa
.x
, wa
.y
+ (topbar
? 0 : wa
.height
- mh
), mw
, mh
);
200 kpress(XKeyEvent
*e
) {
201 char buf
[sizeof text
];
207 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
208 if(ksym
== XK_KP_Enter
)
210 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
211 ksym
= (ksym
- XK_KP_0
) + XK_0
;
212 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
213 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
214 || IsPrivateKeypadKey(ksym
))
216 /* first check if a control mask is omitted */
217 if(e
->state
& ControlMask
) {
218 switch(tolower(ksym
)) {
260 while(i
-- > 0 && text
[i
] == ' ');
261 while(i
-- > 0 && text
[i
] != ' ');
269 num
= MIN(num
, sizeof text
);
270 if(num
&& !iscntrl((int) buf
[0])) {
271 memcpy(text
+ len
, buf
, num
+ 1);
279 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
289 while(sel
&& sel
->right
)
300 if(!sel
|| !sel
->left
)
303 if(sel
->right
== curr
) {
321 if(e
->state
& ShiftMask
)
323 fprintf(stdout
, "%s", sel
? sel
->text
: text
);
328 if(!sel
|| !sel
->right
)
338 strncpy(text
, sel
->text
, sizeof text
);
348 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
351 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
352 for(i
= allitems
; i
; i
= i
->next
)
353 if(!fstrncmp(text
, i
->text
, len
+ 1))
354 appenditem(i
, &lexact
, &exactend
);
355 else if(!fstrncmp(text
, i
->text
, len
))
356 appenditem(i
, &lprefix
, &prefixend
);
357 else if(fstrstr(i
->text
, text
))
358 appenditem(i
, &lsubstr
, &substrend
);
365 itemend
->right
= lprefix
;
366 lprefix
->left
= itemend
;
374 itemend
->right
= lsubstr
;
375 lsubstr
->left
= itemend
;
380 curr
= prev
= next
= sel
= item
;
386 char *p
, buf
[sizeof text
];
387 unsigned int len
= 0, max
= 0;
391 while(fgets(buf
, sizeof buf
, stdin
)) {
393 if(buf
[len
-1] == '\n')
395 if(!(p
= strdup(buf
)))
396 eprint("cannot strdup %u bytes\n", len
);
397 if((max
= MAX(max
, len
)) == len
)
399 if(!(new = malloc(sizeof *new)))
400 eprint("cannot malloc %u bytes\n", sizeof *new);
401 new->next
= new->left
= new->right
= NULL
;
412 main(int argc
, char *argv
[]) {
415 /* command line args */
417 for(i
= 1; i
< argc
; i
++)
418 if(!strcmp(argv
[i
], "-i")) {
419 fstrncmp
= strncasecmp
;
422 else if(!strcmp(argv
[i
], "-b"))
424 else if(!strcmp(argv
[i
], "-l")) {
425 if(++i
< argc
) lines
= atoi(argv
[i
]);
427 calcoffsets
= calcoffsetsv
;
429 else if(!strcmp(argv
[i
], "-fn")) {
430 if(++i
< argc
) font
= argv
[i
];
432 else if(!strcmp(argv
[i
], "-nb")) {
433 if(++i
< argc
) normbgcolor
= argv
[i
];
435 else if(!strcmp(argv
[i
], "-nf")) {
436 if(++i
< argc
) normfgcolor
= argv
[i
];
438 else if(!strcmp(argv
[i
], "-p")) {
439 if(++i
< argc
) prompt
= argv
[i
];
441 else if(!strcmp(argv
[i
], "-sb")) {
442 if(++i
< argc
) selbgcolor
= argv
[i
];
444 else if(!strcmp(argv
[i
], "-sf")) {
445 if(++i
< argc
) selfgcolor
= argv
[i
];
447 else if(!strcmp(argv
[i
], "-v")) {
448 printf("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
452 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
453 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr
);
456 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
457 fprintf(stderr
, "dmenu: warning: no locale support\n");
458 if(!(dpy
= XOpenDisplay(NULL
)))
459 eprint("cannot open display\n");
460 if(atexit(&cleanup
) != 0)
461 eprint("cannot register cleanup\n");
462 screen
= DefaultScreen(dpy
);
463 root
= RootWindow(dpy
, screen
);
464 if(!(argp
= malloc(sizeof *argp
* (argc
+2))))
465 eprint("cannot malloc %u bytes\n", sizeof *argp
* (argc
+2));
466 memcpy(argp
+ 2, argv
+ 1, sizeof *argp
* argc
);
472 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);