dmenu for lunch applications in dwm
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.

379 lines
8.6 KiB

14 years ago
14 years ago
  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 <strings.h>
  9. #include <unistd.h>
  10. #include <X11/keysym.h>
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13. #ifdef XINERAMA
  14. #include <X11/extensions/Xinerama.h>
  15. #endif
  16. /* macros */
  17. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  18. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  19. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  20. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  21. #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(Bool topbar);
  30. #include "config.h"
  31. #include "draw.h"
  32. /* variables */
  33. static char *prompt = NULL;
  34. static char text[4096];
  35. static int promptw = 0;
  36. static int ret = 0;
  37. static int screen;
  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 running = True;
  44. static DC dc;
  45. static Display *dpy;
  46. static Window win, parent;
  47. void
  48. cleanup(void) {
  49. cleanupdraw(&dc);
  50. XDestroyWindow(dpy, win);
  51. XUngrabKeyboard(dpy, CurrentTime);
  52. }
  53. void
  54. drawcursor(void) {
  55. XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 };
  56. r.x += textnw(&dc, text, cursor) + dc.font.height / 2;
  57. XSetForeground(dpy, dc.gc, normcol[ColFG]);
  58. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  59. }
  60. void
  61. drawinput(void)
  62. {
  63. dc.x = 0;
  64. dc.y = 0;
  65. dc.w = mw;
  66. dc.h = mh;
  67. drawtext(&dc, NULL, normcol);
  68. /* print prompt? */
  69. if(prompt) {
  70. dc.w = promptw;
  71. drawtext(&dc, prompt, selcol);
  72. dc.x += dc.w;
  73. }
  74. dc.w = mw - dc.x;
  75. drawtext(&dc, *text ? text : NULL, normcol);
  76. drawcursor();
  77. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  78. XFlush(dpy);
  79. }
  80. Bool
  81. grabkeyboard(void) {
  82. unsigned int len;
  83. for(len = 1000; len; len--) {
  84. if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  85. == GrabSuccess)
  86. break;
  87. usleep(1000);
  88. }
  89. return len > 0;
  90. }
  91. void
  92. kpress(XKeyEvent * e) {
  93. char buf[sizeof text];
  94. int num;
  95. unsigned int i, len;
  96. KeySym ksym;
  97. len = strlen(text);
  98. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  99. if(ksym == XK_KP_Enter)
  100. ksym = XK_Return;
  101. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  102. ksym = (ksym - XK_KP_0) + XK_0;
  103. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  104. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  105. || IsPrivateKeypadKey(ksym))
  106. return;
  107. /* first check if a control mask is omitted */
  108. if(e->state & ControlMask) {
  109. switch(tolower(ksym)) {
  110. default:
  111. return;
  112. case XK_a:
  113. ksym = XK_Home;
  114. break;
  115. case XK_b:
  116. ksym = XK_Left;
  117. break;
  118. case XK_c:
  119. ksym = XK_Escape;
  120. break;
  121. case XK_e:
  122. ksym = XK_End;
  123. break;
  124. case XK_f:
  125. ksym = XK_Right;
  126. break;
  127. case XK_h:
  128. ksym = XK_BackSpace;
  129. break;
  130. case XK_j:
  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. fprintf(stderr, "dc.font.xfont: %u\n", (size_t)dc.font.xfont);
  261. /* input window */
  262. wa.override_redirect = True;
  263. wa.background_pixmap = ParentRelative;
  264. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
  265. /* input window geometry */
  266. mh = dc.font.height + 2;
  267. #if XINERAMA
  268. if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  269. i = 0;
  270. if(n > 1) {
  271. int di;
  272. unsigned int dui;
  273. Window dummy;
  274. if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
  275. for(i = 0; i < n; i++)
  276. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  277. break;
  278. }
  279. x = info[i].x_org;
  280. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  281. mw = info[i].width;
  282. XFree(info);
  283. }
  284. else
  285. #endif
  286. {
  287. XGetWindowAttributes(dpy, parent, &pwa);
  288. x = 0;
  289. y = topbar ? 0 : pwa.height - mh;
  290. mw = pwa.width;
  291. }
  292. win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
  293. DefaultDepth(dpy, screen), CopyFromParent,
  294. DefaultVisual(dpy, screen),
  295. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  296. setupdraw(&dc, win);
  297. if(prompt)
  298. promptw = MIN(textw(&dc, prompt), mw / 5);
  299. cursor = strlen(text);
  300. XMapRaised(dpy, win);
  301. }
  302. int
  303. main(int argc, char *argv[]) {
  304. unsigned int i;
  305. Bool topbar = True;
  306. /* command line args */
  307. progname = argv[0];
  308. for(i = 1; i < argc; i++)
  309. if(!strcmp(argv[i], "-b"))
  310. topbar = False;
  311. else if(!strcmp(argv[i], "-e")) {
  312. if(++i < argc) parent = atoi(argv[i]);
  313. }
  314. else if(!strcmp(argv[i], "-fn")) {
  315. if(++i < argc) font = argv[i];
  316. }
  317. else if(!strcmp(argv[i], "-nb")) {
  318. if(++i < argc) normbgcolor = argv[i];
  319. }
  320. else if(!strcmp(argv[i], "-nf")) {
  321. if(++i < argc) normfgcolor = argv[i];
  322. }
  323. else if(!strcmp(argv[i], "-p")) {
  324. if(++i < argc) prompt = argv[i];
  325. }
  326. else if(!strcmp(argv[i], "-sb")) {
  327. if(++i < argc) selbgcolor = argv[i];
  328. }
  329. else if(!strcmp(argv[i], "-sf")) {
  330. if(++i < argc) selfgcolor = argv[i];
  331. }
  332. else if(!strcmp(argv[i], "-v"))
  333. eprint("dinput-"VERSION", © 2006-2010 dinput engineers, see LICENSE for details\n");
  334. else if(!*text)
  335. strncpy(text, argv[i], sizeof text);
  336. else
  337. eprint("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  338. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n");
  339. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  340. fprintf(stderr, "dinput: warning: no locale support\n");
  341. if(!(dpy = XOpenDisplay(NULL)))
  342. eprint("cannot open display\n");
  343. screen = DefaultScreen(dpy);
  344. if(!parent)
  345. parent = RootWindow(dpy, screen);
  346. running = grabkeyboard();
  347. setup(topbar);
  348. drawinput();
  349. XSync(dpy, False);
  350. run();
  351. cleanup();
  352. XCloseDisplay(dpy);
  353. return ret;
  354. }