Xinqi Bao's Git
03c8b1f68d78cfb733053bc8ee3f2413709d113f
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
;
53 for(next
= curr
; next
; next
=next
->right
) {
54 tw
= textw(next
->text
);
63 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
64 tw
= textw(prev
->left
->text
);
82 drawtext(NULL
, dc
.norm
);
87 drawtext(text
[0] ? text
: NULL
, dc
.norm
);
92 drawtext((curr
&& curr
->left
) ? "<" : NULL
, dc
.norm
);
95 /* determine maximum items */
96 for(i
= curr
; i
!= next
; i
=i
->right
) {
97 dc
.w
= textw(i
->text
);
100 drawtext(i
->text
, (sel
== i
) ? dc
.sel
: dc
.norm
);
106 drawtext(next
? ">" : NULL
, dc
.norm
);
108 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
121 plen
= strlen(pattern
);
125 for(i
= allitems
; i
; i
=i
->next
)
126 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
136 for(i
= allitems
; i
; i
=i
->next
)
137 if(plen
&& strncmp(pattern
, i
->text
, plen
)
138 && strstr(i
->text
, pattern
)) {
149 curr
= prev
= next
= sel
= item
;
154 kpress(XKeyEvent
* e
)
163 num
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, 0);
165 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
166 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
167 || IsPrivateKeypadKey(ksym
))
170 /* first check if a control mask is omitted */
171 if(e
->state
& ControlMask
) {
173 default: /* ignore other control sequences */
191 if(!(sel
&& sel
->left
))
194 if(sel
->right
== curr
) {
202 strncpy(text
, sel
->text
, sizeof(text
));
206 if(!(sel
&& sel
->right
))
215 if(e
->state
& ShiftMask
) {
217 fprintf(stdout
, "%s", text
);
220 fprintf(stdout
, "%s", sel
->text
);
222 fprintf(stdout
, "%s", text
);
236 } while(i
&& nitem
&& prev_nitem
== nitem
);
241 if(num
&& !iscntrl((int) buf
[0])) {
244 strncat(text
, buf
, sizeof(text
));
246 strncpy(text
, buf
, sizeof(text
));
256 static char *maxname
= NULL
;
258 unsigned int len
= 0, max
= 0;
262 while(fgets(buf
, sizeof(buf
), stdin
)) {
264 if (buf
[len
- 1] == '\n')
272 new = emalloc(sizeof(Item
));
273 new->next
= new->left
= new->right
= NULL
;
292 main(int argc
, char *argv
[])
296 struct timeval timeout
;
299 XSetWindowAttributes wa
;
301 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
302 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
306 eprint("usage: dmenu [-v]\n");
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
)
324 timeout
.tv_sec
= STDIN_TIMEOUT
;
326 FD_SET(STDIN_FILENO
, &rd
);
327 if(select(ConnectionNumber(dpy
) + 1, &rd
, NULL
, NULL
, &timeout
) < 1)
328 goto UninitializedEnd
;
329 maxname
= readstdin();
332 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
333 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
334 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
335 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
338 wa
.override_redirect
= 1;
339 wa
.background_pixmap
= ParentRelative
;
340 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
343 mw
= DisplayWidth(dpy
, screen
);
344 mh
= dc
.font
.height
+ 2;
346 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
347 DefaultDepth(dpy
, screen
), CopyFromParent
,
348 DefaultVisual(dpy
, screen
),
349 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
350 XDefineCursor(dpy
, win
, XCreateFontCursor(dpy
, XC_xterm
));
353 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
354 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
355 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
358 cmdw
= textw(maxname
);
364 XMapRaised(dpy
, win
);
368 /* main event loop */
369 while(running
&& !XNextEvent(dpy
, &ev
)) {
371 default: /* ignore all crap */
377 if(ev
.xexpose
.count
== 0)
385 free(allitems
->text
);
390 XFreeFontSet(dpy
, dc
.font
.set
);
392 XFreeFont(dpy
, dc
.font
.xfont
);
393 XFreePixmap(dpy
, dc
.drawable
);
395 XDestroyWindow(dpy
, win
);
397 XUngrabKeyboard(dpy
, CurrentTime
);