Xinqi Bao's Git

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