1 /* See LICENSE file for copyright and license details. */
7 #include <X11/keysym.h>
12 /* forward declarations */
13 static void cleanup(void);
16 static size_t cursor = 0;
21 XDestroyWindow(dpy, win);
22 XUngrabKeyboard(dpy, CurrentTime);
33 drawbox(&dc, normcol);
37 drawtext(&dc, prompt, selcol);
41 drawtext(&dc, text, normcol);
42 drawcursor(&dc, text, cursor, normcol);
47 kpress(XKeyEvent *e) {
48 char buf[sizeof text];
54 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
55 if(ksym == XK_KP_Enter)
57 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
58 ksym = (ksym - XK_KP_0) + XK_0;
59 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
60 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
61 || IsPrivateKeypadKey(ksym))
63 /* first check if a control mask is omitted */
64 if(e->state & ControlMask) {
65 switch(tolower(ksym)) {
94 memmove(text, text + cursor, sizeof text - cursor + 1);
100 while(i-- > 0 && text[i] == ' ');
101 while(i-- > 0 && text[i] != ' ');
102 memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
110 if(!(fp = popen("sselp", "r")))
111 eprint("cannot popen sselp\n");
112 s = fgets(buf, sizeof buf, fp);
118 if(num && buf[num-1] == '\n')
125 num = MIN(num, sizeof text - cursor);
126 if(num && !iscntrl((int) buf[0])) {
127 memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
128 memcpy(text + cursor, buf, num);
135 for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
136 memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
142 for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
143 memmove(text + cursor, text + cursor + i, sizeof text - cursor);
156 while(cursor-- > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
159 fprintf(stdout, "%s", text);
165 while(cursor++ < len && !IS_UTF8_1ST_CHAR(text[cursor]));
172 main(int argc, char *argv[]) {
175 /* command line args */
177 for(i = 1; i < argc; i++)
178 if(!strcmp(argv[i], "-i"))
180 else if(!strcmp(argv[i], "-b"))
182 else if(!strcmp(argv[i], "-l"))
183 i++; /* ignore flag */
184 else if(!strcmp(argv[i], "-fn")) {
185 if(++i < argc) font = argv[i];
187 else if(!strcmp(argv[i], "-nb")) {
188 if(++i < argc) normbgcolor = argv[i];
190 else if(!strcmp(argv[i], "-nf")) {
191 if(++i < argc) normfgcolor = argv[i];
193 else if(!strcmp(argv[i], "-p")) {
194 if(++i < argc) prompt = argv[i];
196 else if(!strcmp(argv[i], "-sb")) {
197 if(++i < argc) selbgcolor = argv[i];
199 else if(!strcmp(argv[i], "-sf")) {
200 if(++i < argc) selfgcolor = argv[i];
202 else if(!strcmp(argv[i], "-v")) {
203 printf("dinput-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
207 strncpy(text, argv[i], sizeof text);
208 cursor = strlen(text);
211 fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
212 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr);
215 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
216 fprintf(stderr, "dinput: warning: no locale support\n");
217 if(!(dpy = XOpenDisplay(NULL)))
218 eprint("cannot open display\n");
219 if(atexit(&cleanup) != 0)
220 eprint("cannot register cleanup\n");
221 screen = DefaultScreen(dpy);
222 root = RootWindow(dpy, screen);