My dmenu build
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dinput.c 8.5 KiB

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