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.

372 lines
8.2 KiB

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 <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 unsigned int cursor = 0;
  38. static unsigned int numlockmask = 0;
  39. static Bool running = True;
  40. static Window win;
  41. Display *dpy;
  42. DC dc;
  43. int screen;
  44. unsigned int mw, mh;
  45. Window parent;
  46. void
  47. cleanup(void) {
  48. drawcleanup();
  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(text, cursor) + dc.font.height / 2;
  56. XSetForeground(dpy, dc.gc, dc.norm[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(NULL, dc.norm);
  67. /* print prompt? */
  68. if(prompt) {
  69. dc.w = promptw;
  70. drawtext(prompt, dc.sel);
  71. dc.x += dc.w;
  72. }
  73. dc.w = mw - dc.x;
  74. drawtext(*text ? text : NULL, dc.norm);
  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. ksym = XK_Return;
  131. break;
  132. case XK_k:
  133. text[cursor] = '\0';
  134. break;
  135. case XK_u:
  136. memmove(text, text + cursor, sizeof text - cursor + 1);
  137. cursor = 0;
  138. break;
  139. case XK_w:
  140. if(cursor > 0) {
  141. i = cursor;
  142. while(i-- > 0 && text[i] == ' ');
  143. while(i-- > 0 && text[i] != ' ');
  144. memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
  145. cursor = i + 1;
  146. }
  147. break;
  148. case XK_y:
  149. {
  150. FILE *fp;
  151. char *s;
  152. if(!(fp = popen("sselp", "r")))
  153. eprint("dinput: cannot popen sselp\n");
  154. s = fgets(buf, sizeof buf, fp);
  155. pclose(fp);
  156. if(s == NULL)
  157. return;
  158. }
  159. num = strlen(buf);
  160. if(num && buf[num-1] == '\n')
  161. buf[--num] = '\0';
  162. break;
  163. }
  164. }
  165. switch(ksym) {
  166. default:
  167. num = MIN(num, sizeof text - cursor);
  168. if(num && !iscntrl((int) buf[0])) {
  169. memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
  170. memcpy(text + cursor, buf, num);
  171. cursor += num;
  172. }
  173. break;
  174. case XK_BackSpace:
  175. if(cursor == 0)
  176. return;
  177. for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
  178. memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
  179. cursor -= i;
  180. break;
  181. case XK_Delete:
  182. if(cursor == len)
  183. return;
  184. for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
  185. memmove(text + cursor, text + cursor + i, sizeof text - cursor);
  186. break;
  187. case XK_End:
  188. cursor = len;
  189. break;
  190. case XK_Escape:
  191. ret = 1;
  192. running = False;
  193. return;
  194. case XK_Home:
  195. cursor = 0;
  196. break;
  197. case XK_Left:
  198. if(cursor == 0)
  199. return;
  200. while(cursor-- > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
  201. break;
  202. case XK_Return:
  203. fprintf(stdout, "%s", text);
  204. fflush(stdout);
  205. running = False;
  206. return;
  207. case XK_Right:
  208. if(cursor == len)
  209. return;
  210. while(cursor++ < len && !IS_UTF8_1ST_CHAR(text[cursor]));
  211. break;
  212. }
  213. drawinput();
  214. }
  215. void
  216. run(void) {
  217. XEvent ev;
  218. /* main event loop */
  219. while(running && !XNextEvent(dpy, &ev))
  220. switch (ev.type) {
  221. case KeyPress:
  222. kpress(&ev.xkey);
  223. break;
  224. case Expose:
  225. if(ev.xexpose.count == 0)
  226. drawinput();
  227. break;
  228. case VisibilityNotify:
  229. if (ev.xvisibility.state != VisibilityUnobscured)
  230. XRaiseWindow(dpy, win);
  231. break;
  232. }
  233. }
  234. void
  235. setup(Bool topbar) {
  236. int i, j, x, y;
  237. #if XINERAMA
  238. int n;
  239. XineramaScreenInfo *info = NULL;
  240. #endif
  241. XModifierKeymap *modmap;
  242. XSetWindowAttributes wa;
  243. XWindowAttributes pwa;
  244. /* init modifier map */
  245. modmap = XGetModifierMapping(dpy);
  246. for(i = 0; i < 8; i++)
  247. for(j = 0; j < modmap->max_keypermod; j++) {
  248. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  249. == XKeysymToKeycode(dpy, XK_Num_Lock))
  250. numlockmask = (1 << i);
  251. }
  252. XFreeModifiermap(modmap);
  253. initfont(font);
  254. /* menu window */
  255. wa.override_redirect = True;
  256. wa.background_pixmap = ParentRelative;
  257. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
  258. /* menu window geometry */
  259. mh = (dc.font.height + 2);
  260. #if XINERAMA
  261. if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  262. i = 0;
  263. if(n > 1) {
  264. int di;
  265. unsigned int dui;
  266. Window dummy;
  267. if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
  268. for(i = 0; i < n; i++)
  269. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  270. break;
  271. }
  272. x = info[i].x_org;
  273. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  274. mw = info[i].width;
  275. XFree(info);
  276. }
  277. else
  278. #endif
  279. {
  280. XGetWindowAttributes(dpy, parent, &pwa);
  281. x = 0;
  282. y = topbar ? 0 : pwa.height - mh;
  283. mw = pwa.width;
  284. }
  285. win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
  286. DefaultDepth(dpy, screen), CopyFromParent,
  287. DefaultVisual(dpy, screen),
  288. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  289. drawsetup();
  290. if(prompt)
  291. promptw = MIN(textw(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. Bool topbar = True;
  299. /* command line args */
  300. for(i = 1; i < argc; i++)
  301. if(!strcmp(argv[i], "-b"))
  302. topbar = False;
  303. else if(!strcmp(argv[i], "-e")) {
  304. if(++i < argc) parent = atoi(argv[i]);
  305. }
  306. else if(!strcmp(argv[i], "-fn")) {
  307. if(++i < argc) font = argv[i];
  308. }
  309. else if(!strcmp(argv[i], "-nb")) {
  310. if(++i < argc) normbgcolor = argv[i];
  311. }
  312. else if(!strcmp(argv[i], "-nf")) {
  313. if(++i < argc) normfgcolor = argv[i];
  314. }
  315. else if(!strcmp(argv[i], "-p")) {
  316. if(++i < argc) prompt = argv[i];
  317. }
  318. else if(!strcmp(argv[i], "-sb")) {
  319. if(++i < argc) selbgcolor = argv[i];
  320. }
  321. else if(!strcmp(argv[i], "-sf")) {
  322. if(++i < argc) selfgcolor = argv[i];
  323. }
  324. else if(!strcmp(argv[i], "-v"))
  325. eprint("dinput-"VERSION", © 2006-2010 dinput engineers, see LICENSE for details\n");
  326. else if(!*text)
  327. strncpy(text, argv[i], sizeof text);
  328. else
  329. eprint("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  330. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n");
  331. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  332. fprintf(stderr, "dinput: warning: no locale support\n");
  333. if(!(dpy = XOpenDisplay(NULL)))
  334. eprint("dinput: cannot open display\n");
  335. screen = DefaultScreen(dpy);
  336. if(!parent)
  337. parent = RootWindow(dpy, screen);
  338. running = grabkeyboard();
  339. setup(topbar);
  340. drawinput();
  341. XSync(dpy, False);
  342. run();
  343. cleanup();
  344. XCloseDisplay(dpy);
  345. return ret;
  346. }