Xinqi Bao's Git

removed -e flag (too buggy), cleaned up
[dmenu.git] / dinput.c
1 /* See LICENSE file for copyright and license details. */
2 #include <ctype.h>
3 #include <locale.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <X11/keysym.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #ifdef XINERAMA
12 #include <X11/extensions/Xinerama.h>
13 #endif
14 #include <draw.h>
15
16 /* macros */
17 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
20 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
21
22 /* forward declarations */
23 static void cleanup(void);
24 static void drawcursor(void);
25 static void drawinput(void);
26 static Bool grabkeyboard(void);
27 static void kpress(XKeyEvent *e);
28 static void run(void);
29 static void setup(Bool topbar);
30
31 #include "config.h"
32
33 /* variables */
34 static char *prompt = NULL;
35 static char text[4096];
36 static int promptw = 0;
37 static int ret = 0;
38 static int screen;
39 static unsigned int cursor = 0;
40 static unsigned int numlockmask = 0;
41 static unsigned int mw, mh;
42 static unsigned long normcol[ColLast];
43 static unsigned long selcol[ColLast];
44 static Bool running = True;
45 static DC dc;
46 static Display *dpy;
47 static Window win, root;
48
49 void
50 cleanup(void) {
51 cleanupdraw(&dc);
52 XDestroyWindow(dpy, win);
53 XUngrabKeyboard(dpy, CurrentTime);
54 }
55
56 void
57 drawcursor(void) {
58 XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 };
59
60 r.x += textnw(&dc, text, cursor) + dc.font.height / 2;
61
62 XSetForeground(dpy, dc.gc, normcol[ColFG]);
63 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
64 }
65
66 void
67 drawinput(void)
68 {
69 dc.x = 0;
70 dc.y = 0;
71 dc.w = mw;
72 dc.h = mh;
73 drawtext(&dc, NULL, normcol, False);
74 /* print prompt? */
75 if(prompt) {
76 dc.w = promptw;
77 drawtext(&dc, prompt, selcol, False);
78 dc.x += dc.w;
79 }
80 dc.w = mw - dc.x;
81 drawtext(&dc, *text ? text : NULL, normcol, False);
82 drawcursor();
83 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
84 XFlush(dpy);
85 }
86
87 Bool
88 grabkeyboard(void) {
89 unsigned int len;
90
91 for(len = 1000; len; len--) {
92 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
93 == GrabSuccess)
94 break;
95 usleep(1000);
96 }
97 return len > 0;
98 }
99
100 void
101 kpress(XKeyEvent *e) {
102 char buf[sizeof text];
103 int num;
104 unsigned int i, len;
105 KeySym ksym;
106
107 len = strlen(text);
108 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
109 if(ksym == XK_KP_Enter)
110 ksym = XK_Return;
111 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
112 ksym = (ksym - XK_KP_0) + XK_0;
113 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
114 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
115 || IsPrivateKeypadKey(ksym))
116 return;
117 /* first check if a control mask is omitted */
118 if(e->state & ControlMask) {
119 switch(tolower(ksym)) {
120 default:
121 return;
122 case XK_a:
123 ksym = XK_Home;
124 break;
125 case XK_b:
126 ksym = XK_Left;
127 break;
128 case XK_c:
129 ksym = XK_Escape;
130 break;
131 case XK_e:
132 ksym = XK_End;
133 break;
134 case XK_f:
135 ksym = XK_Right;
136 break;
137 case XK_h:
138 ksym = XK_BackSpace;
139 break;
140 case XK_j:
141 case XK_m:
142 ksym = XK_Return;
143 break;
144 case XK_k:
145 text[cursor] = '\0';
146 break;
147 case XK_u:
148 memmove(text, text + cursor, sizeof text - cursor + 1);
149 cursor = 0;
150 break;
151 case XK_w:
152 if(cursor > 0) {
153 i = cursor;
154 while(i-- > 0 && text[i] == ' ');
155 while(i-- > 0 && text[i] != ' ');
156 memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
157 cursor = i + 1;
158 }
159 break;
160 case XK_y:
161 {
162 FILE *fp;
163 char *s;
164 if(!(fp = popen("sselp", "r")))
165 eprint("cannot popen sselp\n");
166 s = fgets(buf, sizeof buf, fp);
167 pclose(fp);
168 if(s == NULL)
169 return;
170 }
171 num = strlen(buf);
172 if(num && buf[num-1] == '\n')
173 buf[--num] = '\0';
174 break;
175 }
176 }
177 switch(ksym) {
178 default:
179 num = MIN(num, sizeof text - cursor);
180 if(num && !iscntrl((int) buf[0])) {
181 memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
182 memcpy(text + cursor, buf, num);
183 cursor += num;
184 }
185 break;
186 case XK_BackSpace:
187 if(cursor == 0)
188 return;
189 for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
190 memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
191 cursor -= i;
192 break;
193 case XK_Delete:
194 if(cursor == len)
195 return;
196 for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
197 memmove(text + cursor, text + cursor + i, sizeof text - cursor);
198 break;
199 case XK_End:
200 cursor = len;
201 break;
202 case XK_Escape:
203 ret = 1;
204 running = False;
205 return;
206 case XK_Home:
207 cursor = 0;
208 break;
209 case XK_Left:
210 if(cursor == 0)
211 return;
212 while(cursor-- > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
213 break;
214 case XK_Return:
215 fprintf(stdout, "%s", text);
216 fflush(stdout);
217 running = False;
218 return;
219 case XK_Right:
220 if(cursor == len)
221 return;
222 while(cursor++ < len && !IS_UTF8_1ST_CHAR(text[cursor]));
223 break;
224 }
225 drawinput();
226 }
227
228 void
229 run(void) {
230 XEvent ev;
231
232 /* main event loop */
233 while(running && !XNextEvent(dpy, &ev))
234 switch(ev.type) {
235 case KeyPress:
236 kpress(&ev.xkey);
237 break;
238 case Expose:
239 if(ev.xexpose.count == 0)
240 drawinput();
241 break;
242 case VisibilityNotify:
243 if (ev.xvisibility.state != VisibilityUnobscured)
244 XRaiseWindow(dpy, win);
245 break;
246 }
247 }
248
249 void
250 setup(Bool topbar) {
251 int i, j, x, y;
252 #if XINERAMA
253 int n;
254 XineramaScreenInfo *info = NULL;
255 #endif
256 XModifierKeymap *modmap;
257 XSetWindowAttributes wa;
258
259 /* init modifier map */
260 modmap = XGetModifierMapping(dpy);
261 for(i = 0; i < 8; i++)
262 for(j = 0; j < modmap->max_keypermod; j++) {
263 if(modmap->modifiermap[i * modmap->max_keypermod + j]
264 == XKeysymToKeycode(dpy, XK_Num_Lock))
265 numlockmask = (1 << i);
266 }
267 XFreeModifiermap(modmap);
268
269 dc.dpy = dpy;
270 normcol[ColBG] = getcolor(&dc, normbgcolor);
271 normcol[ColFG] = getcolor(&dc, normfgcolor);
272 selcol[ColBG] = getcolor(&dc, selbgcolor);
273 selcol[ColFG] = getcolor(&dc, selfgcolor);
274 initfont(&dc, font);
275
276 /* input window */
277 wa.override_redirect = True;
278 wa.background_pixmap = ParentRelative;
279 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
280
281 /* input window geometry */
282 mh = dc.font.height + 2;
283 #if XINERAMA
284 if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
285 i = 0;
286 if(n > 1) {
287 int di;
288 unsigned int dui;
289 Window dummy;
290 if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
291 for(i = 0; i < n; i++)
292 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
293 break;
294 }
295 x = info[i].x_org;
296 y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
297 mw = info[i].width;
298 XFree(info);
299 }
300 else
301 #endif
302 {
303 x = 0;
304 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
305 mw = DisplayWidth(dpy, screen);
306 }
307
308 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
309 DefaultDepth(dpy, screen), CopyFromParent,
310 DefaultVisual(dpy, screen),
311 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
312
313 setupdraw(&dc, win);
314 if(prompt)
315 promptw = MIN(textw(&dc, prompt), mw / 5);
316 cursor = strlen(text);
317 XMapRaised(dpy, win);
318 }
319
320 int
321 main(int argc, char *argv[]) {
322 unsigned int i;
323 Bool topbar = True;
324
325 /* command line args */
326 progname = "dinput";
327 for(i = 1; i < argc; i++)
328 if(!strcmp(argv[i], "-i"))
329 ; /* ignore flag */
330 else if(!strcmp(argv[i], "-b"))
331 topbar = False;
332 else if(!strcmp(argv[i], "-l"))
333 i++; /* ignore flag */
334 else if(!strcmp(argv[i], "-fn")) {
335 if(++i < argc) font = argv[i];
336 }
337 else if(!strcmp(argv[i], "-nb")) {
338 if(++i < argc) normbgcolor = argv[i];
339 }
340 else if(!strcmp(argv[i], "-nf")) {
341 if(++i < argc) normfgcolor = argv[i];
342 }
343 else if(!strcmp(argv[i], "-p")) {
344 if(++i < argc) prompt = argv[i];
345 }
346 else if(!strcmp(argv[i], "-sb")) {
347 if(++i < argc) selbgcolor = argv[i];
348 }
349 else if(!strcmp(argv[i], "-sf")) {
350 if(++i < argc) selfgcolor = argv[i];
351 }
352 else if(!strcmp(argv[i], "-v")) {
353 printf("dinput-"VERSION", © 2006-2010 dinput engineers, see LICENSE for details\n");
354 exit(EXIT_SUCCESS);
355 }
356 else if(!*text)
357 strncpy(text, argv[i], sizeof text);
358 else {
359 fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
360 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr);
361 exit(EXIT_FAILURE);
362 }
363 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
364 fprintf(stderr, "dinput: warning: no locale support\n");
365 if(!(dpy = XOpenDisplay(NULL)))
366 eprint("cannot open display\n");
367 screen = DefaultScreen(dpy);
368 root = RootWindow(dpy, screen);
369
370 running = grabkeyboard();
371 setup(topbar);
372 drawinput();
373 XSync(dpy, False);
374 run();
375 cleanup();
376 XCloseDisplay(dpy);
377 return ret;
378 }