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.

381 lines
8.4 KiB

  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 void eprint(const char *errstr, ...);
  27. static Bool grabkeyboard(void);
  28. static void kpress(XKeyEvent * e);
  29. static void run(void);
  30. static void setup(Bool topbar);
  31. #include "config.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 mw, mh;
  39. static unsigned int cursor = 0;
  40. static unsigned int numlockmask = 0;
  41. static Bool running = True;
  42. static Display *dpy;
  43. static Window parent, win;
  44. #include "draw.c"
  45. void
  46. cleanup(void) {
  47. dccleanup();
  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(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(NULL, dc.norm);
  66. /* print prompt? */
  67. if(prompt) {
  68. dc.w = promptw;
  69. drawtext(prompt, dc.sel);
  70. dc.x += dc.w;
  71. }
  72. dc.w = mw - dc.x;
  73. drawtext(*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. void
  79. eprint(const char *errstr, ...) {
  80. va_list ap;
  81. va_start(ap, errstr);
  82. vfprintf(stderr, errstr, ap);
  83. va_end(ap);
  84. exit(EXIT_FAILURE);
  85. }
  86. Bool
  87. grabkeyboard(void) {
  88. unsigned int len;
  89. for(len = 1000; len; len--) {
  90. if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  91. == GrabSuccess)
  92. break;
  93. usleep(1000);
  94. }
  95. return len > 0;
  96. }
  97. void
  98. kpress(XKeyEvent * e) {
  99. char buf[sizeof text];
  100. int num;
  101. unsigned int i, len;
  102. KeySym ksym;
  103. len = strlen(text);
  104. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  105. if(ksym == XK_KP_Enter)
  106. ksym = XK_Return;
  107. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  108. ksym = (ksym - XK_KP_0) + XK_0;
  109. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  110. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  111. || IsPrivateKeypadKey(ksym))
  112. return;
  113. /* first check if a control mask is omitted */
  114. if(e->state & ControlMask) {
  115. switch(tolower(ksym)) {
  116. default:
  117. return;
  118. case XK_a:
  119. ksym = XK_Home;
  120. break;
  121. case XK_b:
  122. ksym = XK_Left;
  123. break;
  124. case XK_c:
  125. ksym = XK_Escape;
  126. break;
  127. case XK_e:
  128. ksym = XK_End;
  129. break;
  130. case XK_f:
  131. ksym = XK_Right;
  132. break;
  133. case XK_h:
  134. ksym = XK_BackSpace;
  135. break;
  136. case XK_j:
  137. ksym = XK_Return;
  138. break;
  139. case XK_k:
  140. text[cursor] = '\0';
  141. break;
  142. case XK_u:
  143. memmove(text, text + cursor, sizeof text - cursor + 1);
  144. cursor = 0;
  145. break;
  146. case XK_w:
  147. if(cursor > 0) {
  148. i = cursor;
  149. while(i-- > 0 && text[i] == ' ');
  150. while(i-- > 0 && text[i] != ' ');
  151. memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
  152. cursor = i + 1;
  153. }
  154. break;
  155. case XK_y:
  156. {
  157. FILE *fp;
  158. char *s;
  159. if(!(fp = popen("sselp", "r")))
  160. eprint("dinput: cannot popen sselp\n");
  161. s = fgets(buf, sizeof buf, fp);
  162. pclose(fp);
  163. if(s == NULL)
  164. return;
  165. }
  166. num = strlen(buf);
  167. if(num && buf[num-1] == '\n')
  168. buf[--num] = '\0';
  169. break;
  170. }
  171. }
  172. switch(ksym) {
  173. default:
  174. num = MIN(num, sizeof text - cursor);
  175. if(num && !iscntrl((int) buf[0])) {
  176. memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
  177. memcpy(text + cursor, buf, num);
  178. cursor += num;
  179. }
  180. break;
  181. case XK_BackSpace:
  182. if(cursor == 0)
  183. return;
  184. for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
  185. memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
  186. cursor -= i;
  187. break;
  188. case XK_Delete:
  189. if(cursor == len)
  190. return;
  191. for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
  192. memmove(text + cursor, text + cursor + i, sizeof text - cursor);
  193. break;
  194. case XK_End:
  195. cursor = len;
  196. break;
  197. case XK_Escape:
  198. ret = 1;
  199. running = False;
  200. return;
  201. case XK_Home:
  202. cursor = 0;
  203. break;
  204. case XK_Left:
  205. if(cursor == 0)
  206. return;
  207. while(cursor-- > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
  208. break;
  209. case XK_Return:
  210. fprintf(stdout, "%s", text);
  211. fflush(stdout);
  212. running = False;
  213. return;
  214. case XK_Right:
  215. if(cursor == len)
  216. return;
  217. while(cursor++ < len && !IS_UTF8_1ST_CHAR(text[cursor]));
  218. break;
  219. }
  220. drawinput();
  221. }
  222. void
  223. run(void) {
  224. XEvent ev;
  225. /* main event loop */
  226. while(running && !XNextEvent(dpy, &ev))
  227. switch (ev.type) {
  228. case KeyPress:
  229. kpress(&ev.xkey);
  230. break;
  231. case Expose:
  232. if(ev.xexpose.count == 0)
  233. drawinput();
  234. break;
  235. case VisibilityNotify:
  236. if (ev.xvisibility.state != VisibilityUnobscured)
  237. XRaiseWindow(dpy, win);
  238. break;
  239. }
  240. }
  241. void
  242. setup(Bool topbar) {
  243. int i, j, x, y;
  244. #if XINERAMA
  245. int n;
  246. XineramaScreenInfo *info = NULL;
  247. #endif
  248. XModifierKeymap *modmap;
  249. XSetWindowAttributes wa;
  250. XWindowAttributes pwa;
  251. /* init modifier map */
  252. modmap = XGetModifierMapping(dpy);
  253. for(i = 0; i < 8; i++)
  254. for(j = 0; j < modmap->max_keypermod; j++) {
  255. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  256. == XKeysymToKeycode(dpy, XK_Num_Lock))
  257. numlockmask = (1 << i);
  258. }
  259. XFreeModifiermap(modmap);
  260. initfont(font);
  261. /* menu window */
  262. wa.override_redirect = True;
  263. wa.background_pixmap = ParentRelative;
  264. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
  265. /* menu 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. dcsetup();
  297. if(prompt)
  298. promptw = MIN(textw(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. 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("dinput: 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. }