Xinqi Bao's Git
1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details.
13 #include <sys/select.h>
15 #include <X11/cursorfont.h>
16 #include <X11/Xutil.h>
17 #include <X11/keysym.h>
19 typedef struct Item Item
;
21 Item
*next
; /* traverses all items */
22 Item
*left
, *right
; /* traverses items matching current search pattern */
28 static char text
[4096];
29 static int mx
, my
, mw
, mh
;
32 static unsigned int cmdw
= 0;
33 static Bool running
= True
;
34 static Item
*allitems
= NULL
; /* first of all items */
35 static Item
*item
= NULL
; /* first of pattern matching items */
36 static Item
*sel
= NULL
;
37 static Item
*next
= NULL
;
38 static Item
*prev
= NULL
;
39 static Item
*curr
= NULL
;
50 for(next
= curr
; next
; next
=next
->right
) {
51 tw
= textw(next
->text
);
59 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
60 tw
= textw(prev
->left
->text
);
77 drawtext(NULL
, dc
.norm
);
81 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
85 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
87 /* determine maximum items */
88 for(i
= curr
; i
!= next
; i
=i
->right
) {
89 dc
.w
= textw(i
->text
);
92 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
97 drawtext(next
? ">" : NULL
, dc
.norm
);
99 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
104 match(char *pattern
) {
110 plen
= strlen(pattern
);
113 for(i
= allitems
; i
; i
=i
->next
)
114 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
124 for(i
= allitems
; i
; i
=i
->next
)
125 if(plen
&& strncmp(pattern
, i
->text
, plen
)
126 && strstr(i
->text
, pattern
)) {
136 curr
= prev
= next
= sel
= item
;
141 kpress(XKeyEvent
* e
) {
149 num
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, 0);
150 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
151 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
152 || IsPrivateKeypadKey(ksym
))
154 /* first check if a control mask is omitted */
155 if(e
->state
& ControlMask
) {
157 default: /* ignore other control sequences */
175 if(!(sel
&& sel
->left
))
178 if(sel
->right
== curr
) {
186 strncpy(text
, sel
->text
, sizeof(text
));
190 if(!(sel
&& sel
->right
))
199 if((e
->state
& ShiftMask
) && text
)
200 fprintf(stdout
, "%s", text
);
202 fprintf(stdout
, "%s", sel
->text
);
204 fprintf(stdout
, "%s", text
);
218 } while(i
&& nitem
&& prev_nitem
== nitem
);
223 if(num
&& !iscntrl((int) buf
[0])) {
226 strncat(text
, buf
, sizeof(text
));
228 strncpy(text
, buf
, sizeof(text
));
237 static char *maxname
= NULL
;
239 unsigned int len
= 0, max
= 0;
243 while(fgets(buf
, sizeof(buf
), stdin
)) {
245 if (buf
[len
- 1] == '\n')
252 new = emalloc(sizeof(Item
));
253 new->next
= new->left
= new->right
= NULL
;
272 main(int argc
, char *argv
[]) {
275 char *normbg
= NORMBGCOLOR
;
276 char *normfg
= NORMFGCOLOR
;
277 char *selbg
= SELBGCOLOR
;
278 char *selfg
= SELFGCOLOR
;
281 struct timeval timeout
;
284 XSetWindowAttributes wa
;
288 /* command line args */
289 for(i
= 1; i
< argc
; i
++)
290 if(!strncmp(argv
[i
], "-font", 6))
292 else if(!strncmp(argv
[i
], "-normbg", 8))
294 else if(!strncmp(argv
[i
], "-normfg", 8))
296 else if(!strncmp(argv
[i
], "-selbg", 7))
298 else if(!strncmp(argv
[i
], "-selfg", 7))
300 else if(!strncmp(argv
[i
], "-t", 3))
301 timeout
.tv_sec
= atoi(argv
[++i
]);
302 else if(!strncmp(argv
[i
], "-v", 3)) {
303 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
307 eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout
);
308 dpy
= XOpenDisplay(0);
310 eprint("dmenu: cannot open display\n");
311 screen
= DefaultScreen(dpy
);
312 root
= RootWindow(dpy
, screen
);
314 /* Note, the select() construction allows to grab all keypresses as
315 * early as possible, to not loose them. But if there is no standard
316 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
317 * seconds. This is convenience behavior for rapid typers.
319 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
320 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
323 FD_SET(STDIN_FILENO
, &rd
);
324 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
325 goto UninitializedEnd
;
326 maxname
= readstdin();
328 dc
.norm
[ColBG
] = getcolor(normbg
);
329 dc
.norm
[ColFG
] = getcolor(normfg
);
330 dc
.sel
[ColBG
] = getcolor(selbg
);
331 dc
.sel
[ColFG
] = getcolor(selfg
);
334 wa
.override_redirect
= 1;
335 wa
.background_pixmap
= ParentRelative
;
336 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
338 mw
= DisplayWidth(dpy
, screen
);
339 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
));
346 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
347 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
348 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
350 cmdw
= textw(maxname
);
355 XMapRaised(dpy
, win
);
359 /* main event loop */
360 while(running
&& !XNextEvent(dpy
, &ev
)) {
362 default: /* ignore all crap */
368 if(ev
.xexpose
.count
== 0)
376 itm
= allitems
->next
;
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
);