Xinqi Bao's Git
170a3e04c1494a0be2690ae5237e7374e54fa7a0
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 struct timeval timeout
;
291 XSetWindowAttributes wa
;
293 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
294 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
298 eprint("usage: dmenu [-v]\n");
300 dpy
= XOpenDisplay(0);
302 eprint("dmenu: cannot open display\n");
303 screen
= DefaultScreen(dpy
);
304 root
= RootWindow(dpy
, screen
);
306 /* Note, the select() construction allows to grab all keypresses as
307 * early as possible, to not loose them. But if there is no standard
308 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
309 * seconds. This is convenience behavior for rapid typers.
311 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
312 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
316 timeout
.tv_sec
= STDIN_TIMEOUT
;
318 FD_SET(STDIN_FILENO
, &rd
);
319 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
320 goto UninitializedEnd
;
321 maxname
= readstdin();
324 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
325 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
326 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
327 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
330 wa
.override_redirect
= 1;
331 wa
.background_pixmap
= ParentRelative
;
332 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
335 mw
= DisplayWidth(dpy
, screen
);
336 mh
= dc
.font
.height
+ 2;
338 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
339 DefaultDepth(dpy
, screen
), CopyFromParent
,
340 DefaultVisual(dpy
, screen
),
341 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
342 XDefineCursor(dpy
, win
, XCreateFontCursor(dpy
, XC_xterm
));
345 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
346 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
347 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
350 cmdw
= textw(maxname
);
356 XMapRaised(dpy
, win
);
360 /* main event loop */
361 while(running
&& !XNextEvent(dpy
, &ev
)) {
363 default: /* ignore all crap */
369 if(ev
.xexpose
.count
== 0)
377 free(allitems
->text
);
382 XFreeFontSet(dpy
, dc
.font
.set
);
384 XFreeFont(dpy
, dc
.font
.xfont
);
385 XFreePixmap(dpy
, dc
.drawable
);
387 XDestroyWindow(dpy
, win
);
389 XUngrabKeyboard(dpy
, CurrentTime
);