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.

378 lines
8.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
16 years ago
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <locale.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <X11/keysym.h>
  9. #include <X11/Xlib.h>
  10. #include <X11/Xutil.h>
  11. #ifdef XINERAMA
  12. #include <X11/extensions/Xinerama.h>
  13. #endif
  14. #include <draw.h>
  15. /* macros */
  16. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  17. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  18. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  19. #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
  20. /* forward declarations */
  21. static void cleanup(void);
  22. static void drawcursor(void);
  23. static void drawinput(void);
  24. static Bool grabkeyboard(void);
  25. static void kpress(XKeyEvent *e);
  26. static void run(void);
  27. static void setup(Bool topbar);
  28. #include "config.h"
  29. /* variables */
  30. static char *prompt = NULL;
  31. static char text[4096];
  32. static int promptw = 0;
  33. static int ret = 0;
  34. static int screen;
  35. static unsigned int cursor = 0;
  36. static unsigned int numlockmask = 0;
  37. static unsigned int mw, mh;
  38. static unsigned long normcol[ColLast];
  39. static unsigned long selcol[ColLast];
  40. static Bool running = True;
  41. static DC dc;
  42. static Display *dpy;
  43. static Window win, root;
  44. void
  45. cleanup(void) {
  46. cleanupdraw(&dc);
  47. XDestroyWindow(dpy, win);
  48. XUngrabKeyboard(dpy, CurrentTime);
  49. }
  50. void
  51. drawcursor(void) {
  52. XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 };
  53. r.x += textnw(&dc, text, cursor) + dc.font.height / 2;
  54. XSetForeground(dpy, dc.gc, normcol[ColFG]);
  55. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  56. }
  57. void
  58. drawinput(void)
  59. {
  60. dc.x = 0;
  61. dc.y = 0;
  62. dc.w = mw;
  63. dc.h = mh;
  64. drawtext(&dc, NULL, normcol, False);
  65. /* print prompt? */
  66. if(prompt) {
  67. dc.w = promptw;
  68. drawtext(&dc, prompt, selcol, False);
  69. dc.x += dc.w;
  70. }
  71. dc.w = mw - dc.x;
  72. drawtext(&dc, *text ? text : NULL, normcol, False);
  73. drawcursor();
  74. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  75. XFlush(dpy);
  76. }
  77. Bool
  78. grabkeyboard(void) {
  79. unsigned int len;
  80. for(len = 1000; len; len--) {
  81. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  82. == GrabSuccess)
  83. break;
  84. usleep(1000);
  85. }
  86. return len > 0;
  87. }
  88. void
  89. kpress(XKeyEvent *e) {
  90. char buf[sizeof text];
  91. int num;
  92. unsigned int i, len;
  93. KeySym ksym;
  94. len = strlen(text);
  95. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  96. if(ksym == XK_KP_Enter)
  97. ksym = XK_Return;
  98. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  99. ksym = (ksym - XK_KP_0) + XK_0;
  100. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  101. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  102. || IsPrivateKeypadKey(ksym))
  103. return;
  104. /* first check if a control mask is omitted */
  105. if(e->state & ControlMask) {
  106. switch(tolower(ksym)) {
  107. default:
  108. return;
  109. case XK_a:
  110. ksym = XK_Home;
  111. break;
  112. case XK_b:
  113. ksym = XK_Left;
  114. break;
  115. case XK_c:
  116. ksym = XK_Escape;
  117. break;
  118. case XK_e:
  119. ksym = XK_End;
  120. break;
  121. case XK_f:
  122. ksym = XK_Right;
  123. break;
  124. case XK_h:
  125. ksym = XK_BackSpace;
  126. break;
  127. case XK_j:
  128. case XK_m:
  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. /* init modifier map */
  243. modmap = XGetModifierMapping(dpy);
  244. for(i = 0; i < 8; i++)
  245. for(j = 0; j < modmap->max_keypermod; j++) {
  246. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  247. == XKeysymToKeycode(dpy, XK_Num_Lock))
  248. numlockmask = (1 << i);
  249. }
  250. XFreeModifiermap(modmap);
  251. dc.dpy = dpy;
  252. normcol[ColBG] = getcolor(&dc, normbgcolor);
  253. normcol[ColFG] = getcolor(&dc, normfgcolor);
  254. selcol[ColBG] = getcolor(&dc, selbgcolor);
  255. selcol[ColFG] = getcolor(&dc, selfgcolor);
  256. initfont(&dc, font);
  257. /* input window */
  258. wa.override_redirect = True;
  259. wa.background_pixmap = ParentRelative;
  260. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  261. /* input window geometry */
  262. mh = dc.font.height + 2;
  263. #if XINERAMA
  264. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  265. i = 0;
  266. if(n > 1) {
  267. int di;
  268. unsigned int dui;
  269. Window dummy;
  270. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  271. for(i = 0; i < n; i++)
  272. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  273. break;
  274. }
  275. x = info[i].x_org;
  276. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  277. mw = info[i].width;
  278. XFree(info);
  279. }
  280. else
  281. #endif
  282. {
  283. x = 0;
  284. y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
  285. mw = DisplayWidth(dpy, screen);
  286. }
  287. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  288. DefaultDepth(dpy, screen), CopyFromParent,
  289. DefaultVisual(dpy, screen),
  290. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  291. setupdraw(&dc, win);
  292. if(prompt)
  293. promptw = MIN(textw(&dc, prompt), mw / 5);
  294. cursor = strlen(text);
  295. XMapRaised(dpy, win);
  296. }
  297. int
  298. main(int argc, char *argv[]) {
  299. unsigned int i;
  300. Bool topbar = True;
  301. /* command line args */
  302. progname = "dinput";
  303. for(i = 1; i < argc; i++)
  304. if(!strcmp(argv[i], "-i"))
  305. ; /* ignore flag */
  306. else if(!strcmp(argv[i], "-b"))
  307. topbar = False;
  308. else if(!strcmp(argv[i], "-l"))
  309. i++; /* ignore flag */
  310. else if(!strcmp(argv[i], "-fn")) {
  311. if(++i < argc) font = argv[i];
  312. }
  313. else if(!strcmp(argv[i], "-nb")) {
  314. if(++i < argc) normbgcolor = argv[i];
  315. }
  316. else if(!strcmp(argv[i], "-nf")) {
  317. if(++i < argc) normfgcolor = argv[i];
  318. }
  319. else if(!strcmp(argv[i], "-p")) {
  320. if(++i < argc) prompt = argv[i];
  321. }
  322. else if(!strcmp(argv[i], "-sb")) {
  323. if(++i < argc) selbgcolor = argv[i];
  324. }
  325. else if(!strcmp(argv[i], "-sf")) {
  326. if(++i < argc) selfgcolor = argv[i];
  327. }
  328. else if(!strcmp(argv[i], "-v")) {
  329. printf("dinput-"VERSION", © 2006-2010 dinput engineers, see LICENSE for details\n");
  330. exit(EXIT_SUCCESS);
  331. }
  332. else if(!*text)
  333. strncpy(text, argv[i], sizeof text);
  334. else {
  335. fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  336. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr);
  337. exit(EXIT_FAILURE);
  338. }
  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. root = 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. }