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.

386 lines
8.7 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
  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, normcol, False);
  71. dc.x += dc.w;
  72. }
  73. dc.w = mw - dc.x;
  74. drawtext(&dc, *text ? text : NULL, selcol, 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], "-i"))
  309. ; /* ignore flag */
  310. else if(!strcmp(argv[i], "-b"))
  311. topbar = False;
  312. else if(!strcmp(argv[i], "-e")) {
  313. if(++i < argc) parent = atoi(argv[i]);
  314. }
  315. else if(!strcmp(argv[i], "-l"))
  316. i++; /* ignore flag */
  317. else if(!strcmp(argv[i], "-fn")) {
  318. if(++i < argc) font = argv[i];
  319. }
  320. else if(!strcmp(argv[i], "-nb")) {
  321. if(++i < argc) normbgcolor = argv[i];
  322. }
  323. else if(!strcmp(argv[i], "-nf")) {
  324. if(++i < argc) normfgcolor = argv[i];
  325. }
  326. else if(!strcmp(argv[i], "-p")) {
  327. if(++i < argc) prompt = argv[i];
  328. }
  329. else if(!strcmp(argv[i], "-sb")) {
  330. if(++i < argc) selbgcolor = argv[i];
  331. }
  332. else if(!strcmp(argv[i], "-sf")) {
  333. if(++i < argc) selfgcolor = argv[i];
  334. }
  335. else if(!strcmp(argv[i], "-v")) {
  336. printf("dinput-"VERSION", © 2006-2010 dinput engineers, see LICENSE for details\n");
  337. exit(EXIT_SUCCESS);
  338. }
  339. else if(!*text)
  340. strncpy(text, argv[i], sizeof text);
  341. else {
  342. fputs("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  343. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr);
  344. exit(EXIT_FAILURE);
  345. }
  346. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  347. fprintf(stderr, "dinput: warning: no locale support\n");
  348. if(!(dpy = XOpenDisplay(NULL)))
  349. eprint("cannot open display\n");
  350. screen = DefaultScreen(dpy);
  351. if(!parent)
  352. parent = RootWindow(dpy, screen);
  353. running = grabkeyboard();
  354. setup(topbar);
  355. drawinput();
  356. XSync(dpy, False);
  357. run();
  358. cleanup();
  359. XCloseDisplay(dpy);
  360. return ret;
  361. }