Xinqi Bao's Git
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 <X11/cursorfont.h>
15 #include <X11/Xutil.h>
16 #include <X11/keysym.h>
18 typedef struct Item Item
;
20 Item
*next
; /* traverses all items */
21 Item
*left
, *right
; /* traverses items matching current search pattern */
27 static char text
[4096];
28 static int mx
, my
, mw
, mh
;
31 static unsigned int cmdw
= 0;
32 static Bool running
= True
;
33 static Item
*allitems
= NULL
; /* first of all items */
34 static Item
*item
= NULL
; /* first of pattern matching items */
35 static Item
*sel
= NULL
;
36 static Item
*next
= NULL
;
37 static Item
*prev
= NULL
;
38 static Item
*curr
= NULL
;
51 for(next
= curr
; next
; next
=next
->right
) {
52 tw
= textw(next
->text
);
61 for(prev
= curr
; prev
&& prev
->left
; prev
=prev
->left
) {
62 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);
119 plen
= strlen(pattern
);
123 for(i
= allitems
; i
; i
=i
->next
)
124 if(!plen
|| !strncmp(pattern
, i
->text
, plen
)) {
134 for(i
= allitems
; i
; i
=i
->next
)
135 if(plen
&& strncmp(pattern
, i
->text
, plen
)
136 && strstr(i
->text
, pattern
)) {
147 curr
= prev
= next
= sel
= item
;
152 kpress(XKeyEvent
* e
)
161 num
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, 0);
163 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
164 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
165 || IsPrivateKeypadKey(ksym
))
168 /* first check if a control mask is omitted */
169 if(e
->state
& ControlMask
) {
171 default: /* ignore other control sequences */
189 if(!(sel
&& sel
->left
))
192 if(sel
->right
== curr
) {
200 strncpy(text
, sel
->text
, sizeof(text
));
204 if(!(sel
&& sel
->right
))
213 if(e
->state
& ShiftMask
) {
215 fprintf(stdout
, "%s", text
);
218 fprintf(stdout
, "%s", sel
->text
);
220 fprintf(stdout
, "%s", text
);
234 } while(i
&& nitem
&& prev_nitem
== nitem
);
239 if(num
&& !iscntrl((int) buf
[0])) {
242 strncat(text
, buf
, sizeof(text
));
244 strncpy(text
, buf
, sizeof(text
));
254 static char *maxname
= NULL
;
256 unsigned int len
= 0, max
= 0;
260 while(fgets(buf
, sizeof(buf
), stdin
)) {
262 if (buf
[len
- 1] == '\n')
270 new = emalloc(sizeof(Item
));
271 new->next
= new->left
= new->right
= NULL
;
290 main(int argc
, char *argv
[])
295 XSetWindowAttributes wa
;
297 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
298 fputs("dmenu-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
302 eprint("usage: dmenu [-v]\n");
304 dpy
= XOpenDisplay(0);
306 eprint("dmenu: cannot open display\n");
307 screen
= DefaultScreen(dpy
);
308 root
= RootWindow(dpy
, screen
);
310 maxname
= readstdin();
312 /* grab as early as possible, but after reading all items!!! */
313 while(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
,
314 GrabModeAsync
, CurrentTime
) != GrabSuccess
)
318 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
319 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
320 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
321 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
324 wa
.override_redirect
= 1;
325 wa
.background_pixmap
= ParentRelative
;
326 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
;
329 mw
= DisplayWidth(dpy
, screen
);
330 mh
= dc
.font
.height
+ 2;
332 win
= XCreateWindow(dpy
, root
, mx
, my
, mw
, mh
, 0,
333 DefaultDepth(dpy
, screen
), CopyFromParent
,
334 DefaultVisual(dpy
, screen
),
335 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
336 XDefineCursor(dpy
, win
, XCreateFontCursor(dpy
, XC_xterm
));
339 dc
.drawable
= XCreatePixmap(dpy
, root
, mw
, mh
, DefaultDepth(dpy
, screen
));
340 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
341 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
344 cmdw
= textw(maxname
);
350 XMapRaised(dpy
, win
);
354 /* main event loop */
355 while(running
&& !XNextEvent(dpy
, &ev
)) {
361 if(ev
.xexpose
.count
== 0)
369 XUngrabKeyboard(dpy
, CurrentTime
);
372 free(allitems
->text
);
377 XFreeFontSet(dpy
, dc
.font
.set
);
379 XFreeFont(dpy
, dc
.font
.xfont
);
380 XFreePixmap(dpy
, dc
.drawable
);
382 XDestroyWindow(dpy
, win
);