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.

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