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.

476 lines
9.8 KiB

18 years ago
18 years ago
16 years ago
18 years ago
16 years ago
18 years ago
18 years ago
18 years ago
16 years ago
16 years ago
14 years ago
16 years ago
17 years ago
16 years ago
14 years ago
16 years ago
14 years ago
16 years ago
17 years ago
17 years ago
18 years ago
14 years ago
14 years ago
18 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
17 years ago
14 years ago
18 years ago
14 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
18 years ago
14 years ago
14 years ago
17 years ago
18 years ago
14 years ago
17 years ago
14 years ago
18 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. #include "dmenu.h"
  12. typedef struct Item Item;
  13. struct Item {
  14. char *text;
  15. Item *next; /* traverses all items */
  16. Item *left, *right; /* traverses items matching current search pattern */
  17. };
  18. /* forward declarations */
  19. static void appenditem(Item *i, Item **list, Item **last);
  20. static void calcoffsetsh(void);
  21. static void calcoffsetsv(void);
  22. static char *cistrstr(const char *s, const char *sub);
  23. static void cleanup(void);
  24. static void dinput(void);
  25. static void drawitem(const char *s, unsigned long col[ColLast]);
  26. static void drawmenuh(void);
  27. static void drawmenuv(void);
  28. static void match(void);
  29. static void readstdin(void);
  30. /* variables */
  31. static char **argp = NULL;
  32. static char *maxname = NULL;
  33. static unsigned int cmdw = 0;
  34. static unsigned int lines = 0;
  35. static Item *allitems = NULL; /* first of all items */
  36. static Item *item = NULL; /* first of pattern matching items */
  37. static Item *sel = NULL;
  38. static Item *next = NULL;
  39. static Item *prev = NULL;
  40. static Item *curr = NULL;
  41. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  42. static char *(*fstrstr)(const char *, const char *) = strstr;
  43. static void (*calcoffsets)(void) = calcoffsetsh;
  44. void
  45. appenditem(Item *i, Item **list, Item **last) {
  46. if(!(*last))
  47. *list = i;
  48. else
  49. (*last)->right = i;
  50. i->left = *last;
  51. i->right = NULL;
  52. *last = i;
  53. }
  54. void
  55. calcoffsetsh(void) {
  56. unsigned int w, x;
  57. w = promptw + cmdw + textw(&dc, "<") + textw(&dc, ">");
  58. for(x = w, next = curr; next; next = next->right)
  59. if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
  60. break;
  61. for(x = w, prev = curr; prev && prev->left; prev = prev->left)
  62. if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
  63. break;
  64. }
  65. void
  66. calcoffsetsv(void) {
  67. unsigned int i;
  68. next = prev = curr;
  69. for(i = 0; i < lines && next; i++)
  70. next = next->right;
  71. mh = (dc.font.height + 2) * (i + 1);
  72. for(i = 0; i < lines && prev && prev->left; i++)
  73. prev = prev->left;
  74. }
  75. char *
  76. cistrstr(const char *s, const char *sub) {
  77. int c, csub;
  78. unsigned int len;
  79. if(!sub)
  80. return (char *)s;
  81. if((c = tolower(*sub++)) != '\0') {
  82. len = strlen(sub);
  83. do {
  84. do {
  85. if((csub = *s++) == '\0')
  86. return NULL;
  87. }
  88. while(tolower(csub) != c);
  89. }
  90. while(strncasecmp(s, sub, len) != 0);
  91. s--;
  92. }
  93. return (char *)s;
  94. }
  95. void
  96. cleanup(void) {
  97. Item *itm;
  98. while(allitems) {
  99. itm = allitems->next;
  100. free(allitems->text);
  101. free(allitems);
  102. allitems = itm;
  103. }
  104. cleanupdraw(&dc);
  105. XDestroyWindow(dpy, win);
  106. XUngrabKeyboard(dpy, CurrentTime);
  107. XCloseDisplay(dpy);
  108. }
  109. void
  110. dinput(void) {
  111. cleanup();
  112. argp[0] = "dinput";
  113. argp[1] = text;
  114. execvp("dinput", argp);
  115. eprint("cannot exec dinput\n");
  116. }
  117. void
  118. drawbar(void) {
  119. dc.x = 0;
  120. dc.y = 0;
  121. dc.w = mw;
  122. dc.h = mh;
  123. drawbox(&dc, normcol);
  124. dc.h = dc.font.height + 2;
  125. dc.y = topbar ? 0 : mh - dc.h;
  126. /* print prompt? */
  127. if(prompt) {
  128. dc.w = promptw;
  129. drawbox(&dc, selcol);
  130. drawtext(&dc, prompt, selcol);
  131. dc.x += dc.w;
  132. }
  133. dc.w = mw - dc.x;
  134. /* print command */
  135. if(cmdw && item && lines == 0)
  136. dc.w = cmdw;
  137. drawtext(&dc, text, normcol);
  138. if(lines > 0)
  139. drawmenuv();
  140. else if(curr)
  141. drawmenuh();
  142. commitdraw(&dc, win);
  143. }
  144. void
  145. drawitem(const char *s, unsigned long col[ColLast]) {
  146. drawbox(&dc, col);
  147. drawtext(&dc, s, col);
  148. }
  149. void
  150. drawmenuh(void) {
  151. Item *i;
  152. dc.x += cmdw;
  153. dc.w = textw(&dc, "<");
  154. drawtext(&dc, curr->left ? "<" : NULL, normcol);
  155. dc.x += dc.w;
  156. for(i = curr; i != next; i = i->right) {
  157. dc.w = MIN(textw(&dc, i->text), mw / 3);
  158. drawitem(i->text, (sel == i) ? selcol : normcol);
  159. dc.x += dc.w;
  160. }
  161. dc.w = textw(&dc, ">");
  162. dc.x = mw - dc.w;
  163. drawtext(&dc, next ? ">" : NULL, normcol);
  164. }
  165. void
  166. drawmenuv(void) {
  167. Item *i;
  168. XWindowAttributes wa;
  169. dc.y = topbar ? dc.h : 0;
  170. dc.w = mw - dc.x;
  171. for(i = curr; i != next; i = i->right) {
  172. drawitem(i->text, (sel == i) ? selcol : normcol);
  173. dc.y += dc.h;
  174. }
  175. if(!XGetWindowAttributes(dpy, win, &wa))
  176. eprint("cannot get window attributes");
  177. XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
  178. }
  179. void
  180. kpress(XKeyEvent *e) {
  181. char buf[sizeof text];
  182. int num;
  183. unsigned int i, len;
  184. KeySym ksym;
  185. len = strlen(text);
  186. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  187. if(ksym == XK_KP_Enter)
  188. ksym = XK_Return;
  189. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  190. ksym = (ksym - XK_KP_0) + XK_0;
  191. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  192. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  193. || IsPrivateKeypadKey(ksym))
  194. return;
  195. /* first check if a control mask is omitted */
  196. if(e->state & ControlMask) {
  197. switch(tolower(ksym)) {
  198. default:
  199. return;
  200. case XK_a:
  201. ksym = XK_Home;
  202. break;
  203. case XK_b:
  204. ksym = XK_Left;
  205. break;
  206. case XK_c:
  207. ksym = XK_Escape;
  208. break;
  209. case XK_e:
  210. ksym = XK_End;
  211. break;
  212. case XK_f:
  213. ksym = XK_Right;
  214. break;
  215. case XK_h:
  216. ksym = XK_BackSpace;
  217. break;
  218. case XK_i:
  219. ksym = XK_Tab;
  220. break;
  221. case XK_j:
  222. case XK_m:
  223. ksym = XK_Return;
  224. break;
  225. case XK_n:
  226. ksym = XK_Down;
  227. break;
  228. case XK_p:
  229. ksym = XK_Up;
  230. break;
  231. case XK_u:
  232. text[0] = '\0';
  233. match();
  234. break;
  235. case XK_w:
  236. if(len == 0)
  237. return;
  238. i = len;
  239. while(i-- > 0 && text[i] == ' ');
  240. while(i-- > 0 && text[i] != ' ');
  241. text[++i] = '\0';
  242. match();
  243. break;
  244. }
  245. }
  246. switch(ksym) {
  247. default:
  248. num = MIN(num, sizeof text);
  249. if(num && !iscntrl((int) buf[0])) {
  250. memcpy(text + len, buf, num + 1);
  251. len += num;
  252. match();
  253. }
  254. break;
  255. case XK_BackSpace:
  256. if(len == 0)
  257. return;
  258. for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
  259. len -= i;
  260. text[len] = '\0';
  261. match();
  262. break;
  263. case XK_End:
  264. while(next) {
  265. sel = curr = next;
  266. calcoffsets();
  267. }
  268. while(sel && sel->right)
  269. sel = sel->right;
  270. break;
  271. case XK_Escape:
  272. exit(EXIT_FAILURE);
  273. case XK_Home:
  274. sel = curr = item;
  275. calcoffsets();
  276. break;
  277. case XK_Left:
  278. case XK_Up:
  279. if(!sel || !sel->left)
  280. return;
  281. sel = sel->left;
  282. if(sel->right == curr) {
  283. curr = prev;
  284. calcoffsets();
  285. }
  286. break;
  287. case XK_Next:
  288. if(!next)
  289. return;
  290. sel = curr = next;
  291. calcoffsets();
  292. break;
  293. case XK_Prior:
  294. if(!prev)
  295. return;
  296. sel = curr = prev;
  297. calcoffsets();
  298. break;
  299. case XK_Return:
  300. if(e->state & ShiftMask)
  301. dinput();
  302. fprintf(stdout, "%s", sel ? sel->text : text);
  303. fflush(stdout);
  304. exit(EXIT_SUCCESS);
  305. case XK_Right:
  306. case XK_Down:
  307. if(!sel || !sel->right)
  308. return;
  309. sel = sel->right;
  310. if(sel == next) {
  311. curr = next;
  312. calcoffsets();
  313. }
  314. break;
  315. case XK_Tab:
  316. if(sel)
  317. strncpy(text, sel->text, sizeof text);
  318. dinput();
  319. break;
  320. }
  321. drawbar();
  322. }
  323. void
  324. match(void) {
  325. unsigned int len;
  326. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  327. len = strlen(text);
  328. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  329. for(i = allitems; i; i = i->next)
  330. if(!fstrncmp(text, i->text, len + 1))
  331. appenditem(i, &lexact, &exactend);
  332. else if(!fstrncmp(text, i->text, len))
  333. appenditem(i, &lprefix, &prefixend);
  334. else if(fstrstr(i->text, text))
  335. appenditem(i, &lsubstr, &substrend);
  336. if(lexact) {
  337. item = lexact;
  338. itemend = exactend;
  339. }
  340. if(lprefix) {
  341. if(itemend) {
  342. itemend->right = lprefix;
  343. lprefix->left = itemend;
  344. }
  345. else
  346. item = lprefix;
  347. itemend = prefixend;
  348. }
  349. if(lsubstr) {
  350. if(itemend) {
  351. itemend->right = lsubstr;
  352. lsubstr->left = itemend;
  353. }
  354. else
  355. item = lsubstr;
  356. }
  357. curr = prev = next = sel = item;
  358. calcoffsets();
  359. }
  360. void
  361. readstdin(void) {
  362. char *p, buf[sizeof text];
  363. unsigned int len = 0, max = 0;
  364. Item *i, *new;
  365. i = NULL;
  366. while(fgets(buf, sizeof buf, stdin)) {
  367. len = strlen(buf);
  368. if(buf[len-1] == '\n')
  369. buf[--len] = '\0';
  370. if(!(p = strdup(buf)))
  371. eprint("cannot strdup %u bytes\n", len);
  372. if((max = MAX(max, len)) == len)
  373. maxname = p;
  374. if(!(new = malloc(sizeof *new)))
  375. eprint("cannot malloc %u bytes\n", sizeof *new);
  376. new->next = new->left = new->right = NULL;
  377. new->text = p;
  378. if(!i)
  379. allitems = new;
  380. else
  381. i->next = new;
  382. i = new;
  383. }
  384. }
  385. int
  386. main(int argc, char *argv[]) {
  387. unsigned int i;
  388. /* command line args */
  389. progname = "dmenu";
  390. for(i = 1; i < argc; i++)
  391. if(!strcmp(argv[i], "-i")) {
  392. fstrncmp = strncasecmp;
  393. fstrstr = cistrstr;
  394. }
  395. else if(!strcmp(argv[i], "-b"))
  396. topbar = False;
  397. else if(!strcmp(argv[i], "-l")) {
  398. if(++i < argc) lines = atoi(argv[i]);
  399. if(lines > 0)
  400. calcoffsets = calcoffsetsv;
  401. }
  402. else if(!strcmp(argv[i], "-fn")) {
  403. if(++i < argc) font = argv[i];
  404. }
  405. else if(!strcmp(argv[i], "-nb")) {
  406. if(++i < argc) normbgcolor = argv[i];
  407. }
  408. else if(!strcmp(argv[i], "-nf")) {
  409. if(++i < argc) normfgcolor = argv[i];
  410. }
  411. else if(!strcmp(argv[i], "-p")) {
  412. if(++i < argc) prompt = argv[i];
  413. }
  414. else if(!strcmp(argv[i], "-sb")) {
  415. if(++i < argc) selbgcolor = argv[i];
  416. }
  417. else if(!strcmp(argv[i], "-sf")) {
  418. if(++i < argc) selfgcolor = argv[i];
  419. }
  420. else if(!strcmp(argv[i], "-v")) {
  421. printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  422. exit(EXIT_SUCCESS);
  423. }
  424. else {
  425. fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  426. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
  427. exit(EXIT_FAILURE);
  428. }
  429. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  430. fprintf(stderr, "dmenu: warning: no locale support\n");
  431. if(!(dpy = XOpenDisplay(NULL)))
  432. eprint("cannot open display\n");
  433. if(atexit(&cleanup) != 0)
  434. eprint("cannot register cleanup\n");
  435. screen = DefaultScreen(dpy);
  436. root = RootWindow(dpy, screen);
  437. if(!(argp = malloc(sizeof *argp * (argc+2))))
  438. eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
  439. memcpy(argp + 2, argv + 1, sizeof *argp * argc);
  440. readstdin();
  441. grabkeyboard();
  442. setup(lines);
  443. if(maxname)
  444. cmdw = MIN(textw(&dc, maxname), mw / 3);
  445. match();
  446. run();
  447. return 0;
  448. }