Xinqi Bao's Git
4bbc7bc891a27cd7b91cb9e983b969f797190a50
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
);
38 drawtext(&dc
, prompt
, selcol
);
42 drawtext(&dc
, text
, normcol
);
43 drawline(&dc
, textnw(&dc
, text
, cursor
) + dc
.font
.height
/2, 2, 1,
44 dc
.font
.height
-2, normcol
);
49 kpress(XKeyEvent
*e
) {
50 char buf
[sizeof text
];
56 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
57 if(ksym
== XK_KP_Enter
)
59 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
60 ksym
= (ksym
- XK_KP_0
) + XK_0
;
61 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
62 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
63 || IsPrivateKeypadKey(ksym
))
65 /* first check if a control mask is omitted */
66 if(e
->state
& ControlMask
) {
67 switch(tolower(ksym
)) {
96 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
102 while(i
-- > 0 && text
[i
] == ' ');
103 while(i
-- > 0 && text
[i
] != ' ');
104 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
112 if(!(fp
= popen("sselp", "r")))
113 eprint("cannot popen sselp\n");
114 s
= fgets(buf
, sizeof buf
, fp
);
120 if(num
&& buf
[num
-1] == '\n')
127 num
= MIN(num
, sizeof text
- cursor
);
128 if(num
&& !iscntrl((int) buf
[0])) {
129 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
130 memcpy(text
+ cursor
, buf
, num
);
137 for(i
= 1; cursor
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[cursor
- i
]); i
++);
138 memmove(text
+ cursor
- i
, text
+ cursor
, sizeof text
- cursor
+ i
);
144 for(i
= 1; cursor
+ i
< len
&& !IS_UTF8_1ST_CHAR(text
[cursor
+ i
]); i
++);
145 memmove(text
+ cursor
, text
+ cursor
+ i
, sizeof text
- cursor
);
158 while(cursor
-- > 0 && !IS_UTF8_1ST_CHAR(text
[cursor
]));
161 fprintf(stdout
, "%s", text
);
167 while(cursor
++ < len
&& !IS_UTF8_1ST_CHAR(text
[cursor
]));
174 main(int argc
, char *argv
[]) {
177 /* command line args */
179 for(i
= 1; i
< argc
; i
++)
180 if(!strcmp(argv
[i
], "-i"))
182 else if(!strcmp(argv
[i
], "-b"))
184 else if(!strcmp(argv
[i
], "-l"))
185 i
++; /* ignore flag */
186 else if(!strcmp(argv
[i
], "-fn")) {
187 if(++i
< argc
) font
= argv
[i
];
189 else if(!strcmp(argv
[i
], "-nb")) {
190 if(++i
< argc
) normbgcolor
= argv
[i
];
192 else if(!strcmp(argv
[i
], "-nf")) {
193 if(++i
< argc
) normfgcolor
= argv
[i
];
195 else if(!strcmp(argv
[i
], "-p")) {
196 if(++i
< argc
) prompt
= argv
[i
];
198 else if(!strcmp(argv
[i
], "-sb")) {
199 if(++i
< argc
) selbgcolor
= argv
[i
];
201 else if(!strcmp(argv
[i
], "-sf")) {
202 if(++i
< argc
) selfgcolor
= argv
[i
];
204 else if(!strcmp(argv
[i
], "-v")) {
205 printf("dinput-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
209 strncpy(text
, argv
[i
], sizeof text
);
210 cursor
= strlen(text
);
213 fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
214 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr
);
217 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
218 fprintf(stderr
, "dinput: warning: no locale support\n");
219 if(!(dpy
= XOpenDisplay(NULL
)))
220 eprint("cannot open display\n");
221 if(atexit(&cleanup
) != 0)
222 eprint("cannot register cleanup\n");
223 screen
= DefaultScreen(dpy
);
224 root
= RootWindow(dpy
, screen
);