Xinqi Bao's Git
3203014d8db280d1fd5241cedaca8d243f41559f
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
) {
213 fprintf(stdout
, "%s", text
);
216 fprintf(stdout
, "%s", sel
->text
);
218 fprintf(stdout
, "%s", text
);
232 } while(i
&& nitem
&& prev_nitem
== nitem
);
237 if(num
&& !iscntrl((int) buf
[0])) {
240 strncat(text
, buf
, sizeof(text
));
242 strncpy(text
, buf
, sizeof(text
));
251 static char *maxname
= NULL
;
253 unsigned int len
= 0, max
= 0;
257 while(fgets(buf
, sizeof(buf
), stdin
)) {
259 if (buf
[len
- 1] == '\n')
267 new = emalloc(sizeof(Item
));
268 new->next
= new->left
= new->right
= NULL
;
287 main(int argc
, char *argv
[]) {
290 struct timeval timeout
;
293 XSetWindowAttributes wa
;
295 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
296 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
300 eprint("usage: dmenu [-v]\n");
302 dpy
= XOpenDisplay(0);
304 eprint("dmenu: cannot open display\n");
305 screen
= DefaultScreen(dpy
);
306 root
= RootWindow(dpy
, screen
);
308 /* Note, the select() construction allows to grab all keypresses as
309 * early as possible, to not loose them. But if there is no standard
310 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
311 * seconds. This is convenience behavior for rapid typers.
313 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
314 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
318 timeout
.tv_sec
= STDIN_TIMEOUT
;
320 FD_SET(STDIN_FILENO
, &rd
);
321 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
322 goto UninitializedEnd
;
323 maxname
= readstdin();
326 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
327 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
328 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
329 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
332 wa
.override_redirect
= 1;
333 wa
.background_pixmap
= ParentRelative
;
334 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
337 mw
= DisplayWidth(dpy
, screen
);
338 mh
= dc
.font
.height
+ 2;
340 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
341 DefaultDepth(dpy
, screen
), CopyFromParent
,
342 DefaultVisual(dpy
, screen
),
343 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
344 XDefineCursor(dpy
, win
, XCreateFontCursor(dpy
, XC_xterm
));
347 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
348 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
349 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
352 cmdw
= textw(maxname
);
358 XMapRaised(dpy
, win
);
362 /* main event loop */
363 while(running
&& !XNextEvent(dpy
, &ev
)) {
365 default: /* ignore all crap */
371 if(ev
.xexpose
.count
== 0)
379 free(allitems
->text
);
384 XFreeFontSet(dpy
, dc
.font
.set
);
386 XFreeFont(dpy
, dc
.font
.xfont
);
387 XFreePixmap(dpy
, dc
.drawable
);
389 XDestroyWindow(dpy
, win
);
391 XUngrabKeyboard(dpy
, CurrentTime
);