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 drawmenuh(void);
28 static void drawmenuv(void);
29 static void match(void);
30 static void readstdin(void);
33 static char **argp
= NULL
;
34 static char *maxname
= NULL
;
35 static unsigned int cmdw
= 0;
36 static unsigned int lines
= 0;
37 static Item
*allitems
= NULL
; /* first of all items */
38 static Item
*item
= NULL
; /* first of pattern matching items */
39 static Item
*sel
= NULL
;
40 static Item
*next
= NULL
;
41 static Item
*prev
= NULL
;
42 static Item
*curr
= NULL
;
43 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
44 static char *(*fstrstr
)(const char *, const char *) = strstr
;
45 static void (*calcoffsets
)(void) = calcoffsetsh
;
48 appenditem(Item
*i
, Item
**list
, Item
**last
) {
62 w
= promptw
+ cmdw
+ textw(&dc
, "<") + textw(&dc
, ">");
63 for(x
= w
, next
= curr
; next
; next
= next
->right
)
64 if((x
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
66 for(x
= w
, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
67 if((x
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
76 for(i
= 0; i
< lines
&& next
; i
++)
78 mh
= (dc
.font
.height
+ 2) * (i
+ 1);
79 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
84 cistrstr(const char *s
, const char *sub
) {
90 if((c
= tolower(*sub
++)) != '\0') {
94 if((csub
= *s
++) == '\0')
97 while(tolower(csub
) != c
);
99 while(strncasecmp(s
, sub
, len
) != 0);
110 itm
= allitems
->next
;
111 free(allitems
->text
);
116 XDestroyWindow(dpy
, win
);
117 XUngrabKeyboard(dpy
, CurrentTime
);
126 execvp("dinput", argp
);
127 eprint("cannot exec dinput\n");
136 drawbox(&dc
, normcol
);
137 dc
.h
= dc
.font
.height
+ 2;
138 dc
.y
= topbar
? 0 : mh
- dc
.h
;
142 drawtext(&dc
, prompt
, selcol
);
147 if(cmdw
&& item
&& lines
== 0)
149 drawtext(&dc
, text
, normcol
);
154 commitdraw(&dc
, win
);
163 dc
.w
= textw(&dc
, "<");
164 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
);
166 for(i
= curr
; i
!= next
; i
= i
->right
) {
167 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
168 col
= (sel
== i
) ? selcol
: normcol
;
170 drawtext(&dc
, i
->text
, col
);
173 dc
.w
= textw(&dc
, ">");
175 drawtext(&dc
, next
? ">" : NULL
, normcol
);
181 XWindowAttributes wa
;
183 dc
.y
= topbar
? dc
.h
: 0;
185 for(i
= curr
; i
!= next
; i
= i
->right
) {
186 drawtext(&dc
, i
->text
, (sel
== i
) ? selcol
: normcol
);
189 if(!XGetWindowAttributes(dpy
, win
, &wa
))
190 eprint("cannot get window attributes");
191 XMoveResizeWindow(dpy
, win
, wa
.x
, wa
.y
+ (topbar
? 0 : wa
.height
- mh
), mw
, mh
);
195 kpress(XKeyEvent
*e
) {
196 char buf
[sizeof text
];
202 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
203 if(ksym
== XK_KP_Enter
)
205 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
206 ksym
= (ksym
- XK_KP_0
) + XK_0
;
207 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
208 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
209 || IsPrivateKeypadKey(ksym
))
211 /* first check if a control mask is omitted */
212 if(e
->state
& ControlMask
) {
213 switch(tolower(ksym
)) {
255 while(i
-- > 0 && text
[i
] == ' ');
256 while(i
-- > 0 && text
[i
] != ' ');
264 num
= MIN(num
, sizeof text
);
265 if(num
&& !iscntrl((int) buf
[0])) {
266 memcpy(text
+ len
, buf
, num
+ 1);
274 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
284 while(sel
&& sel
->right
)
295 if(!sel
|| !sel
->left
)
298 if(sel
->right
== curr
) {
316 if(e
->state
& ShiftMask
)
318 fprintf(stdout
, "%s", sel
? sel
->text
: text
);
323 if(!sel
|| !sel
->right
)
333 strncpy(text
, sel
->text
, sizeof text
);
343 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
346 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
347 for(i
= allitems
; i
; i
= i
->next
)
348 if(!fstrncmp(text
, i
->text
, len
+ 1))
349 appenditem(i
, &lexact
, &exactend
);
350 else if(!fstrncmp(text
, i
->text
, len
))
351 appenditem(i
, &lprefix
, &prefixend
);
352 else if(fstrstr(i
->text
, text
))
353 appenditem(i
, &lsubstr
, &substrend
);
360 itemend
->right
= lprefix
;
361 lprefix
->left
= itemend
;
369 itemend
->right
= lsubstr
;
370 lsubstr
->left
= itemend
;
375 curr
= prev
= next
= sel
= item
;
381 char *p
, buf
[sizeof text
];
382 unsigned int len
= 0, max
= 0;
386 while(fgets(buf
, sizeof buf
, stdin
)) {
388 if(buf
[len
-1] == '\n')
390 if(!(p
= strdup(buf
)))
391 eprint("cannot strdup %u bytes\n", len
);
392 if((max
= MAX(max
, len
)) == len
)
394 if(!(new = malloc(sizeof *new)))
395 eprint("cannot malloc %u bytes\n", sizeof *new);
396 new->next
= new->left
= new->right
= NULL
;
407 main(int argc
, char *argv
[]) {
410 /* command line args */
412 for(i
= 1; i
< argc
; i
++)
413 if(!strcmp(argv
[i
], "-i")) {
414 fstrncmp
= strncasecmp
;
417 else if(!strcmp(argv
[i
], "-b"))
419 else if(!strcmp(argv
[i
], "-l")) {
420 if(++i
< argc
) lines
= atoi(argv
[i
]);
422 calcoffsets
= calcoffsetsv
;
424 else if(!strcmp(argv
[i
], "-fn")) {
425 if(++i
< argc
) font
= argv
[i
];
427 else if(!strcmp(argv
[i
], "-nb")) {
428 if(++i
< argc
) normbgcolor
= argv
[i
];
430 else if(!strcmp(argv
[i
], "-nf")) {
431 if(++i
< argc
) normfgcolor
= argv
[i
];
433 else if(!strcmp(argv
[i
], "-p")) {
434 if(++i
< argc
) prompt
= argv
[i
];
436 else if(!strcmp(argv
[i
], "-sb")) {
437 if(++i
< argc
) selbgcolor
= argv
[i
];
439 else if(!strcmp(argv
[i
], "-sf")) {
440 if(++i
< argc
) selfgcolor
= argv
[i
];
442 else if(!strcmp(argv
[i
], "-v")) {
443 printf("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
447 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
448 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr
);
451 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
452 fprintf(stderr
, "dmenu: warning: no locale support\n");
453 if(!(dpy
= XOpenDisplay(NULL
)))
454 eprint("cannot open display\n");
455 if(atexit(&cleanup
) != 0)
456 eprint("cannot register cleanup\n");
457 screen
= DefaultScreen(dpy
);
458 root
= RootWindow(dpy
, screen
);
459 if(!(argp
= malloc(sizeof *argp
* (argc
+2))))
460 eprint("cannot malloc %u bytes\n", sizeof *argp
* (argc
+2));
461 memcpy(argp
+ 2, argv
+ 1, sizeof *argp
* argc
);
467 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);