Xinqi Bao's Git
fa37f49e7ac5f914e8b8c858365a6fd6635fc6ac
1 /* See LICENSE file for copyright and license details. */
11 #include <X11/extensions/Xinerama.h>
15 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
16 #define MIN(a,b) ((a) < (b) ? (a) : (b))
17 #define MAX(a,b) ((a) > (b) ? (a) : (b))
18 #define UTF8_CODEPOINT(c) (((c) & 0xc0) != 0x80)
20 typedef struct Item Item
;
23 Item
*next
; /* traverses all items */
24 Item
*left
, *right
; /* traverses matching items */
27 static void appenditem(Item
*item
, Item
**list
, Item
**last
);
28 static void calcoffsets(void);
29 static char *cistrstr(const char *s
, const char *sub
);
30 static void drawmenu(void);
31 static void grabkeyboard(void);
32 static void insert(const char *s
, ssize_t n
);
33 static void keypress(XKeyEvent
*ev
);
34 static void match(void);
35 static void paste(void);
36 static void readstdin(void);
37 static void run(void);
38 static void setup(void);
39 static void usage(void);
41 static char text
[4096];
42 static size_t cursor
= 0;
43 static const char *prompt
= NULL
;
44 static const char *normbgcolor
= "#cccccc";
45 static const char *normfgcolor
= "#000000";
46 static const char *selbgcolor
= "#0066ff";
47 static const char *selfgcolor
= "#ffffff";
48 static unsigned int inputw
= 0;
49 static unsigned int lines
= 0;
50 static unsigned int mw
, mh
;
51 static unsigned int promptw
;
52 static unsigned long normcol
[ColLast
];
53 static unsigned long selcol
[ColLast
];
55 static Bool topbar
= True
;
57 static Item
*allitems
, *matches
;
58 static Item
*curr
, *prev
, *next
, *sel
;
59 static Window root
, win
;
61 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
62 static char *(*fstrstr
)(const char *, const char *) = strstr
;
65 appenditem(Item
*item
, Item
**list
, Item
**last
) {
69 (*last
)->right
= item
;
79 h
= dc
->font
.height
+2;
83 n
= mw
- (promptw
+ inputw
+ textw(dc
, "<") + textw(dc
, ">"));
86 for(i
= 0; next
; next
= next
->right
)
87 if((i
+= (lines
> 0) ? h
: MIN(textw(dc
, next
->text
), mw
/3)) > n
)
89 for(i
= 0; prev
&& prev
->left
; prev
= prev
->left
)
90 if((i
+= (lines
> 0) ? h
: MIN(textw(dc
, prev
->left
->text
), mw
/3)) > n
)
95 cistrstr(const char *s
, const char *sub
) {
98 for(len
= strlen(sub
); *s
; s
++)
99 if(!strncasecmp(s
, sub
, len
))
111 drawrect(dc
, 0, 0, mw
, mh
, BG(dc
, normcol
));
112 dc
->h
= dc
->font
.height
+ 2;
113 dc
->y
= topbar
? 0 : mh
- dc
->h
;
117 drawtext(dc
, prompt
, selcol
);
120 dc
->w
= (lines
> 0 || !matches
) ? mw
- dc
->x
: inputw
;
121 drawtext(dc
, text
, normcol
);
122 if((curpos
= textnw(dc
, text
, cursor
) + dc
->h
/2 - 2) < dc
->w
)
123 drawrect(dc
, curpos
, 2, 1, dc
->h
- 4, FG(dc
, normcol
));
126 dc
->y
= topbar
? dc
->h
: 0;
128 for(item
= curr
; item
!= next
; item
= item
->right
) {
129 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
135 dc
->w
= textw(dc
, "<");
137 drawtext(dc
, "<", normcol
);
138 for(item
= curr
; item
!= next
; item
= item
->right
) {
140 dc
->w
= MIN(textw(dc
, item
->text
), mw
/3);
141 drawtext(dc
, item
->text
, (item
== sel
) ? selcol
: normcol
);
143 dc
->w
= textw(dc
, ">");
146 drawtext(dc
, ">", normcol
);
155 for(i
= 0; i
< 1000; i
++) {
156 if(!XGrabKeyboard(dc
->dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
))
160 eprintf("cannot grab keyboard\n");
164 insert(const char *s
, ssize_t n
) {
165 memmove(text
+ cursor
+ n
, text
+ cursor
, sizeof text
- cursor
- n
);
167 memcpy(text
+ cursor
, s
, n
);
173 keypress(XKeyEvent
*ev
) {
180 XLookupString(ev
, buf
, sizeof buf
, &ksym
, NULL
);
181 if(ev
->state
& ControlMask
) {
182 switch(tolower(ksym
)) {
212 case XK_k
: /* delete right */
222 case XK_u
: /* delete left */
223 insert(NULL
, -cursor
);
225 case XK_w
: /* delete word */
229 while(cursor
- n
++ > 0 && text
[cursor
- n
] == ' ');
230 while(cursor
- n
++ > 0 && text
[cursor
- n
] != ' ');
233 case XK_y
: /* paste selection */
234 XConvertSelection(dc
->dpy
, XA_PRIMARY
, utf8
, None
, win
, CurrentTime
);
240 if(!iscntrl((int)*buf
))
241 insert(buf
, MIN(strlen(buf
), sizeof text
- cursor
));
246 for(n
= 1; cursor
- n
> 0 && !UTF8_CODEPOINT(text
[cursor
- n
]); n
++);
252 for(n
= 1; cursor
+ n
< len
&& !UTF8_CODEPOINT(text
[cursor
+ n
]); n
++);
265 while(sel
&& sel
->right
)
275 sel
= curr
= matches
;
279 if(cursor
> 0 && (!sel
|| !sel
->left
|| lines
> 0)) {
280 while(cursor
-- > 0 && !UTF8_CODEPOINT(text
[cursor
]));
286 if(!sel
|| !sel
->left
)
288 if((sel
= sel
->left
)->right
== curr
) {
307 fputs((sel
&& !(ev
->state
& ShiftMask
)) ? sel
->text
: text
, stdout
);
312 while(cursor
++ < len
&& !UTF8_CODEPOINT(text
[cursor
]));
318 if(!sel
|| !sel
->right
)
320 if((sel
= sel
->right
) == next
) {
328 strncpy(text
, sel
->text
, sizeof text
);
329 cursor
= strlen(text
);
339 Item
*item
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
342 matches
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
343 for(item
= allitems
; item
; item
= item
->next
)
344 if(!fstrncmp(text
, item
->text
, len
+ 1))
345 appenditem(item
, &lexact
, &exactend
);
346 else if(!fstrncmp(text
, item
->text
, len
))
347 appenditem(item
, &lprefix
, &prefixend
);
348 else if(fstrstr(item
->text
, text
))
349 appenditem(item
, &lsubstr
, &substrend
);
356 itemend
->right
= lprefix
;
357 lprefix
->left
= itemend
;
365 itemend
->right
= lsubstr
;
366 lsubstr
->left
= itemend
;
371 curr
= prev
= next
= sel
= matches
;
382 XGetWindowProperty(dc
->dpy
, win
, utf8
, 0, sizeof text
- cursor
, True
,
383 utf8
, &da
, &di
, &dl
, &dl
, (unsigned char **)&p
);
384 insert(p
, (q
= strchr(p
, '\n')) ? q
-p
: strlen(p
));
391 char buf
[sizeof text
], *p
;
395 for(item
= NULL
; fgets(buf
, sizeof buf
, stdin
); item
= new) {
396 if((p
= strchr(buf
, '\n')))
398 if(!(new = malloc(sizeof *new)))
399 eprintf("cannot malloc %u bytes\n", sizeof *new);
400 if(!(new->text
= strdup(buf
)))
401 eprintf("cannot strdup %u bytes\n", strlen(buf
)+1);
402 inputw
= MAX(inputw
, textw(dc
, new->text
));
403 new->next
= new->left
= new->right
= NULL
;
415 while(!XNextEvent(dc
->dpy
, &ev
))
418 if(ev
.xexpose
.count
== 0)
424 case SelectionNotify
:
425 if(ev
.xselection
.property
== utf8
)
428 case VisibilityNotify
:
429 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
430 XRaiseWindow(dc
->dpy
, win
);
438 XSetWindowAttributes wa
;
441 XineramaScreenInfo
*info
;
444 screen
= DefaultScreen(dc
->dpy
);
445 root
= RootWindow(dc
->dpy
, screen
);
446 utf8
= XInternAtom(dc
->dpy
, "UTF8_STRING", False
);
448 normcol
[ColBG
] = getcolor(dc
, normbgcolor
);
449 normcol
[ColFG
] = getcolor(dc
, normfgcolor
);
450 selcol
[ColBG
] = getcolor(dc
, selbgcolor
);
451 selcol
[ColFG
] = getcolor(dc
, selfgcolor
);
454 mh
= (dc
->font
.height
+ 2) * (lines
+ 1);
456 if((info
= XineramaQueryScreens(dc
->dpy
, &n
))) {
461 XQueryPointer(dc
->dpy
, root
, &dw
, &dw
, &x
, &y
, &di
, &di
, &du
);
462 for(i
= 0; i
< n
; i
++)
463 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
466 y
= info
[i
].y_org
+ (topbar
? 0 : info
[i
].height
- mh
);
474 y
= topbar
? 0 : DisplayHeight(dc
->dpy
, screen
) - mh
;
475 mw
= DisplayWidth(dc
->dpy
, screen
);
478 wa
.override_redirect
= True
;
479 wa
.background_pixmap
= ParentRelative
;
480 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
481 win
= XCreateWindow(dc
->dpy
, root
, x
, y
, mw
, mh
, 0,
482 DefaultDepth(dc
->dpy
, screen
), CopyFromParent
,
483 DefaultVisual(dc
->dpy
, screen
),
484 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
487 setcanvas(dc
, mw
, mh
);
488 inputw
= MIN(inputw
, mw
/3);
489 promptw
= prompt
? MIN(textw(dc
, prompt
), mw
/5) : 0;
490 XMapRaised(dc
->dpy
, win
);
497 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
498 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr
);
503 main(int argc
, char *argv
[]) {
509 for(i
= 1; i
< argc
; i
++)
511 if(!strcmp(argv
[i
], "-v")) {
512 fputs("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout
);
515 else if(!strcmp(argv
[i
], "-b"))
517 else if(!strcmp(argv
[i
], "-i")) {
518 fstrncmp
= strncasecmp
;
524 else if(!strcmp(argv
[i
], "-l"))
525 lines
= atoi(argv
[++i
]);
526 else if(!strcmp(argv
[i
], "-p"))
528 else if(!strcmp(argv
[i
], "-fn"))
529 initfont(dc
, argv
[++i
]);
530 else if(!strcmp(argv
[i
], "-nb"))
531 normbgcolor
= argv
[++i
];
532 else if(!strcmp(argv
[i
], "-nf"))
533 normfgcolor
= argv
[++i
];
534 else if(!strcmp(argv
[i
], "-sb"))
535 selbgcolor
= argv
[++i
];
536 else if(!strcmp(argv
[i
], "-sf"))
537 selfgcolor
= argv
[++i
];
545 return EXIT_FAILURE
; /* should not reach */