1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
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))
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(void);
34 static char *prompt = NULL;
35 static char text[4096];
36 static int promptw = 0;
38 static unsigned int cursor = 0;
39 static unsigned int numlockmask = 0;
40 static unsigned int mw, mh;
41 static unsigned long normcol[ColLast];
42 static unsigned long selcol[ColLast];
43 static Bool topbar = True;
46 static Window win, root;
51 XDestroyWindow(dpy, win);
52 XUngrabKeyboard(dpy, CurrentTime);
58 XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 };
60 r.x += textnw(&dc, text, cursor) + dc.font.height / 2;
62 XSetForeground(dpy, dc.gc, normcol[ColFG]);
63 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
73 drawtext(&dc, NULL, normcol, False);
77 drawtext(&dc, prompt, selcol, False);
81 drawtext(&dc, *text ? text : NULL, normcol, False);
83 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
90 for(len = 1000; len; len--) {
91 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
100 kpress(XKeyEvent *e) {
101 char buf[sizeof text];
107 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
108 if(ksym == XK_KP_Enter)
110 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
111 ksym = (ksym - XK_KP_0) + XK_0;
112 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
113 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
114 || IsPrivateKeypadKey(ksym))
116 /* first check if a control mask is omitted */
117 if(e->state & ControlMask) {
118 switch(tolower(ksym)) {
147 memmove(text, text + cursor, sizeof text - cursor + 1);
153 while(i-- > 0 && text[i] == ' ');
154 while(i-- > 0 && text[i] != ' ');
155 memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
163 if(!(fp = popen("sselp", "r")))
164 eprint("cannot popen sselp\n");
165 s = fgets(buf, sizeof buf, fp);
171 if(num && buf[num-1] == '\n')
178 num = MIN(num, sizeof text - cursor);
179 if(num && !iscntrl((int) buf[0])) {
180 memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
181 memcpy(text + cursor, buf, num);
188 for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
189 memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
195 for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
196 memmove(text + cursor, text + cursor + i, sizeof text - cursor);
209 while(cursor-- > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
212 fprintf(stdout, "%s", text);
218 while(cursor++ < len && !IS_UTF8_1ST_CHAR(text[cursor]));
228 /* main event loop */
230 while(!XNextEvent(dpy, &ev))
236 if(ev.xexpose.count == 0)
239 case VisibilityNotify:
240 if(ev.xvisibility.state != VisibilityUnobscured)
241 XRaiseWindow(dpy, win);
252 XineramaScreenInfo *info = NULL;
254 XModifierKeymap *modmap;
255 XSetWindowAttributes wa;
257 /* init modifier map */
258 modmap = XGetModifierMapping(dpy);
259 for(i = 0; i < 8; i++)
260 for(j = 0; j < modmap->max_keypermod; j++) {
261 if(modmap->modifiermap[i * modmap->max_keypermod + j]
262 == XKeysymToKeycode(dpy, XK_Num_Lock))
263 numlockmask = (1 << i);
265 XFreeModifiermap(modmap);
268 normcol[ColBG] = getcolor(&dc, normbgcolor);
269 normcol[ColFG] = getcolor(&dc, normfgcolor);
270 selcol[ColBG] = getcolor(&dc, selbgcolor);
271 selcol[ColFG] = getcolor(&dc, selfgcolor);
275 wa.override_redirect = True;
276 wa.background_pixmap = ParentRelative;
277 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
279 /* input window geometry */
280 mh = dc.font.height + 2;
282 if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
288 if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
289 for(i = 0; i < n; i++)
290 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
294 y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
302 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
303 mw = DisplayWidth(dpy, screen);
306 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
307 DefaultDepth(dpy, screen), CopyFromParent,
308 DefaultVisual(dpy, screen),
309 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
313 promptw = MIN(textw(&dc, prompt), mw / 5);
314 cursor = strlen(text);
315 XMapRaised(dpy, win);
319 main(int argc, char *argv[]) {
322 /* command line args */
324 for(i = 1; i < argc; i++)
325 if(!strcmp(argv[i], "-i"))
327 else if(!strcmp(argv[i], "-b"))
329 else if(!strcmp(argv[i], "-l"))
330 i++; /* ignore flag */
331 else if(!strcmp(argv[i], "-fn")) {
332 if(++i < argc) font = argv[i];
334 else if(!strcmp(argv[i], "-nb")) {
335 if(++i < argc) normbgcolor = argv[i];
337 else if(!strcmp(argv[i], "-nf")) {
338 if(++i < argc) normfgcolor = argv[i];
340 else if(!strcmp(argv[i], "-p")) {
341 if(++i < argc) prompt = argv[i];
343 else if(!strcmp(argv[i], "-sb")) {
344 if(++i < argc) selbgcolor = argv[i];
346 else if(!strcmp(argv[i], "-sf")) {
347 if(++i < argc) selfgcolor = argv[i];
349 else if(!strcmp(argv[i], "-v")) {
350 printf("dinput-"VERSION", © 2006-2010 dinput engineers, see LICENSE for details\n");
354 strncpy(text, argv[i], sizeof text);
356 fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
357 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr);
360 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
361 fprintf(stderr, "dinput: warning: no locale support\n");
362 if(!(dpy = XOpenDisplay(NULL)))
363 eprint("cannot open display\n");
364 if(atexit(&cleanup) != 0)
365 eprint("cannot register cleanup\n");
366 screen = DefaultScreen(dpy);
367 root = RootWindow(dpy, screen);