My dmenu build
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dinput.c 8.4 KiB

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