Xinqi Bao's Git
27b28afa7f5a12081ef7153b62837b59b7fd0aa7
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
4 * See LICENSE file for license details.
14 #include <sys/select.h>
16 #include <X11/cursorfont.h>
17 #include <X11/Xutil.h>
18 #include <X11/keysym.h>
20 typedef struct Item Item
;
22 Item
*next
; /* traverses all items */
23 Item
*left
, *right
; /* traverses items matching current search pattern */
29 static char text
[4096];
30 static int mx
, my
, mw
, mh
;
33 static unsigned int cmdw
= 0;
34 static Bool running
= True
;
35 static Item
*allitems
= NULL
; /* first of all items */
36 static Item
*item
= NULL
; /* first of pattern matching items */
37 static Item
*sel
= NULL
;
38 static Item
*next
= NULL
;
39 static Item
*prev
= NULL
;
40 static Item
*curr
= NULL
;
52 for(next
= curr
; next
; next
=next
->right
) {
53 tw
= textw(next
->text
);
62 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
63 tw
= textw(prev
->left
->text
);
80 drawtext(NULL
, dc
.norm
);
85 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
90 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
93 /* determine maximum items */
94 for(i
= curr
; i
!= next
; i
=i
->right
) {
95 dc
.w
= textw(i
->text
);
98 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
104 drawtext(next
? ">" : NULL
, dc
.norm
);
106 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
111 match(char *pattern
) {
118 plen
= strlen(pattern
);
122 for(i
= allitems
; i
; i
=i
->next
)
123 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
133 for(i
= allitems
; i
; i
=i
->next
)
134 if(plen
&& strncmp(pattern
, i
->text
, plen
)
135 && strstr(i
->text
, pattern
)) {
146 curr
= prev
= next
= sel
= item
;
151 kpress(XKeyEvent
* e
) {
159 num
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, 0);
161 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
162 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
163 || IsPrivateKeypadKey(ksym
))
166 /* first check if a control mask is omitted */
167 if(e
->state
& ControlMask
) {
169 default: /* ignore other control sequences */
187 if(!(sel
&& sel
->left
))
190 if(sel
->right
== curr
) {
198 strncpy(text
, sel
->text
, sizeof(text
));
202 if(!(sel
&& sel
->right
))
211 if((e
->state
& ShiftMask
) && text
)
212 fprintf(stdout
, "%s", text
);
214 fprintf(stdout
, "%s", sel
->text
);
216 fprintf(stdout
, "%s", text
);
230 } while(i
&& nitem
&& prev_nitem
== nitem
);
235 if(num
&& !iscntrl((int) buf
[0])) {
238 strncat(text
, buf
, sizeof(text
));
240 strncpy(text
, buf
, sizeof(text
));
249 static char *maxname
= NULL
;
251 unsigned int len
= 0, max
= 0;
255 while(fgets(buf
, sizeof(buf
), stdin
)) {
257 if (buf
[len
- 1] == '\n')
265 new = emalloc(sizeof(Item
));
266 new->next
= new->left
= new->right
= NULL
;
285 main(int argc
, char *argv
[]) {
288 char *normbg
= NORMBGCOLOR
;
289 char *normfg
= NORMFGCOLOR
;
290 char *selbg
= SELBGCOLOR
;
291 char *selfg
= SELFGCOLOR
;
294 struct timeval timeout
;
297 XSetWindowAttributes wa
;
301 /* command line args */
302 for(i
= 1; i
< argc
; i
++)
303 if(!strncmp(argv
[i
], "-font", 6))
305 else if(!strncmp(argv
[i
], "-normbg", 8))
307 else if(!strncmp(argv
[i
], "-normfg", 8))
309 else if(!strncmp(argv
[i
], "-selbg", 7))
311 else if(!strncmp(argv
[i
], "-selfg", 7))
313 else if(!strncmp(argv
[i
], "-t", 3))
314 timeout
.tv_sec
= atoi(argv
[++i
]);
315 else if(!strncmp(argv
[i
], "-v", 3)) {
316 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
320 eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout
);
322 dpy
= XOpenDisplay(0);
324 eprint("dmenu: cannot open display\n");
325 screen
= DefaultScreen(dpy
);
326 root
= RootWindow(dpy
, screen
);
328 /* Note, the select() construction allows to grab all keypresses as
329 * early as possible, to not loose them. But if there is no standard
330 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
331 * seconds. This is convenience behavior for rapid typers.
333 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
334 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
338 FD_SET(STDIN_FILENO
, &rd
);
339 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
340 goto UninitializedEnd
;
341 maxname
= readstdin();
344 dc
.sel
[ColBG
] = getcolor(selbg
);
345 dc
.sel
[ColFG
] = getcolor(selfg
);
346 dc
.norm
[ColBG
] = getcolor(normbg
);
347 dc
.norm
[ColFG
] = getcolor(normfg
);
350 wa
.override_redirect
= 1;
351 wa
.background_pixmap
= ParentRelative
;
352 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
355 mw
= DisplayWidth(dpy
, screen
);
356 mh
= dc
.font
.height
+ 2;
358 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
359 DefaultDepth(dpy
, screen
), CopyFromParent
,
360 DefaultVisual(dpy
, screen
),
361 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
362 XDefineCursor(dpy
, win
, XCreateFontCursor(dpy
, XC_xterm
));
365 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
366 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
367 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
370 cmdw
= textw(maxname
);
376 XMapRaised(dpy
, win
);
380 /* main event loop */
381 while(running
&& !XNextEvent(dpy
, &ev
)) {
383 default: /* ignore all crap */
389 if(ev
.xexpose
.count
== 0)
396 itm
= allitems
->next
;
397 free(allitems
->text
);
402 XFreeFontSet(dpy
, dc
.font
.set
);
404 XFreeFont(dpy
, dc
.font
.xfont
);
405 XFreePixmap(dpy
, dc
.drawable
);
407 XDestroyWindow(dpy
, win
);
409 XUngrabKeyboard(dpy
, CurrentTime
);