My dmenu build
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.

dmenu.c 10 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. const char *p;
  147. unsigned int w = textnw(&dc, text, strlen(text));
  148. drawbox(&dc, col);
  149. drawtext(&dc, s, col);
  150. for(p = fstrstr(s, text); *text && (p = fstrstr(p, text)); p++)
  151. drawline(&dc, textnw(&dc, s, p-s) + dc.h/2 - 1, dc.h-2, w, 1, col);
  152. }
  153. void
  154. drawmenuh(void) {
  155. Item *i;
  156. dc.x += cmdw;
  157. dc.w = textw(&dc, "<");
  158. drawtext(&dc, curr->left ? "<" : NULL, normcol);
  159. dc.x += dc.w;
  160. for(i = curr; i != next; i = i->right) {
  161. dc.w = MIN(textw(&dc, i->text), mw / 3);
  162. drawitem(i->text, (sel == i) ? selcol : normcol);
  163. dc.x += dc.w;
  164. }
  165. dc.w = textw(&dc, ">");
  166. dc.x = mw - dc.w;
  167. drawtext(&dc, next ? ">" : NULL, normcol);
  168. }
  169. void
  170. drawmenuv(void) {
  171. Item *i;
  172. XWindowAttributes wa;
  173. dc.y = topbar ? dc.h : 0;
  174. dc.w = mw - dc.x;
  175. for(i = curr; i != next; i = i->right) {
  176. drawitem(i->text, (sel == i) ? selcol : normcol);
  177. dc.y += dc.h;
  178. }
  179. if(!XGetWindowAttributes(dpy, win, &wa))
  180. eprint("cannot get window attributes");
  181. XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
  182. }
  183. void
  184. kpress(XKeyEvent *e) {
  185. char buf[sizeof text];
  186. int num;
  187. unsigned int i, len;
  188. KeySym ksym;
  189. len = strlen(text);
  190. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  191. if(ksym == XK_KP_Enter)
  192. ksym = XK_Return;
  193. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  194. ksym = (ksym - XK_KP_0) + XK_0;
  195. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  196. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  197. || IsPrivateKeypadKey(ksym))
  198. return;
  199. /* first check if a control mask is omitted */
  200. if(e->state & ControlMask) {
  201. switch(tolower(ksym)) {
  202. default:
  203. return;
  204. case XK_a:
  205. ksym = XK_Home;
  206. break;
  207. case XK_b:
  208. ksym = XK_Left;
  209. break;
  210. case XK_c:
  211. ksym = XK_Escape;
  212. break;
  213. case XK_e:
  214. ksym = XK_End;
  215. break;
  216. case XK_f:
  217. ksym = XK_Right;
  218. break;
  219. case XK_h:
  220. ksym = XK_BackSpace;
  221. break;
  222. case XK_i:
  223. ksym = XK_Tab;
  224. break;
  225. case XK_j:
  226. case XK_m:
  227. ksym = XK_Return;
  228. break;
  229. case XK_n:
  230. ksym = XK_Down;
  231. break;
  232. case XK_p:
  233. ksym = XK_Up;
  234. break;
  235. case XK_u:
  236. text[0] = '\0';
  237. match();
  238. break;
  239. case XK_w:
  240. if(len == 0)
  241. return;
  242. i = len;
  243. while(i-- > 0 && text[i] == ' ');
  244. while(i-- > 0 && text[i] != ' ');
  245. text[++i] = '\0';
  246. match();
  247. break;
  248. }
  249. }
  250. switch(ksym) {
  251. default:
  252. num = MIN(num, sizeof text);
  253. if(num && !iscntrl((int) buf[0])) {
  254. memcpy(text + len, buf, num + 1);
  255. len += num;
  256. match();
  257. }
  258. break;
  259. case XK_BackSpace:
  260. if(len == 0)
  261. return;
  262. for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
  263. len -= i;
  264. text[len] = '\0';
  265. match();
  266. break;
  267. case XK_End:
  268. while(next) {
  269. sel = curr = next;
  270. calcoffsets();
  271. }
  272. while(sel && sel->right)
  273. sel = sel->right;
  274. break;
  275. case XK_Escape:
  276. exit(EXIT_FAILURE);
  277. case XK_Home:
  278. sel = curr = item;
  279. calcoffsets();
  280. break;
  281. case XK_Left:
  282. case XK_Up:
  283. if(!sel || !sel->left)
  284. return;
  285. sel = sel->left;
  286. if(sel->right == curr) {
  287. curr = prev;
  288. calcoffsets();
  289. }
  290. break;
  291. case XK_Next:
  292. if(!next)
  293. return;
  294. sel = curr = next;
  295. calcoffsets();
  296. break;
  297. case XK_Prior:
  298. if(!prev)
  299. return;
  300. sel = curr = prev;
  301. calcoffsets();
  302. break;
  303. case XK_Return:
  304. if(e->state & ShiftMask)
  305. dinput();
  306. fprintf(stdout, "%s", sel ? sel->text : text);
  307. fflush(stdout);
  308. exit(EXIT_SUCCESS);
  309. case XK_Right:
  310. case XK_Down:
  311. if(!sel || !sel->right)
  312. return;
  313. sel = sel->right;
  314. if(sel == next) {
  315. curr = next;
  316. calcoffsets();
  317. }
  318. break;
  319. case XK_Tab:
  320. if(sel)
  321. strncpy(text, sel->text, sizeof text);
  322. dinput();
  323. break;
  324. }
  325. drawbar();
  326. }
  327. void
  328. match(void) {
  329. unsigned int len;
  330. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  331. len = strlen(text);
  332. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  333. for(i = allitems; i; i = i->next)
  334. if(!fstrncmp(text, i->text, len + 1))
  335. appenditem(i, &lexact, &exactend);
  336. else if(!fstrncmp(text, i->text, len))
  337. appenditem(i, &lprefix, &prefixend);
  338. else if(fstrstr(i->text, text))
  339. appenditem(i, &lsubstr, &substrend);
  340. if(lexact) {
  341. item = lexact;
  342. itemend = exactend;
  343. }
  344. if(lprefix) {
  345. if(itemend) {
  346. itemend->right = lprefix;
  347. lprefix->left = itemend;
  348. }
  349. else
  350. item = lprefix;
  351. itemend = prefixend;
  352. }
  353. if(lsubstr) {
  354. if(itemend) {
  355. itemend->right = lsubstr;
  356. lsubstr->left = itemend;
  357. }
  358. else
  359. item = lsubstr;
  360. }
  361. curr = prev = next = sel = item;
  362. calcoffsets();
  363. }
  364. void
  365. readstdin(void) {
  366. char *p, buf[sizeof text];
  367. unsigned int len = 0, max = 0;
  368. Item *i, *new;
  369. i = NULL;
  370. while(fgets(buf, sizeof buf, stdin)) {
  371. len = strlen(buf);
  372. if(buf[len-1] == '\n')
  373. buf[--len] = '\0';
  374. if(!(p = strdup(buf)))
  375. eprint("cannot strdup %u bytes\n", len);
  376. if((max = MAX(max, len)) == len)
  377. maxname = p;
  378. if(!(new = malloc(sizeof *new)))
  379. eprint("cannot malloc %u bytes\n", sizeof *new);
  380. new->next = new->left = new->right = NULL;
  381. new->text = p;
  382. if(!i)
  383. allitems = new;
  384. else
  385. i->next = new;
  386. i = new;
  387. }
  388. }
  389. int
  390. main(int argc, char *argv[]) {
  391. unsigned int i;
  392. /* command line args */
  393. progname = "dmenu";
  394. for(i = 1; i < argc; i++)
  395. if(!strcmp(argv[i], "-i")) {
  396. fstrncmp = strncasecmp;
  397. fstrstr = cistrstr;
  398. }
  399. else if(!strcmp(argv[i], "-b"))
  400. topbar = False;
  401. else if(!strcmp(argv[i], "-l")) {
  402. if(++i < argc) lines = atoi(argv[i]);
  403. if(lines > 0)
  404. calcoffsets = calcoffsetsv;
  405. }
  406. else if(!strcmp(argv[i], "-fn")) {
  407. if(++i < argc) font = argv[i];
  408. }
  409. else if(!strcmp(argv[i], "-nb")) {
  410. if(++i < argc) normbgcolor = argv[i];
  411. }
  412. else if(!strcmp(argv[i], "-nf")) {
  413. if(++i < argc) normfgcolor = argv[i];
  414. }
  415. else if(!strcmp(argv[i], "-p")) {
  416. if(++i < argc) prompt = argv[i];
  417. }
  418. else if(!strcmp(argv[i], "-sb")) {
  419. if(++i < argc) selbgcolor = argv[i];
  420. }
  421. else if(!strcmp(argv[i], "-sf")) {
  422. if(++i < argc) selfgcolor = argv[i];
  423. }
  424. else if(!strcmp(argv[i], "-v")) {
  425. printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  426. exit(EXIT_SUCCESS);
  427. }
  428. else {
  429. fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  430. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
  431. exit(EXIT_FAILURE);
  432. }
  433. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  434. fprintf(stderr, "dmenu: warning: no locale support\n");
  435. if(!(dpy = XOpenDisplay(NULL)))
  436. eprint("cannot open display\n");
  437. if(atexit(&cleanup) != 0)
  438. eprint("cannot register cleanup\n");
  439. screen = DefaultScreen(dpy);
  440. root = RootWindow(dpy, screen);
  441. if(!(argp = malloc(sizeof *argp * (argc+2))))
  442. eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
  443. memcpy(argp + 2, argv + 1, sizeof *argp * argc);
  444. readstdin();
  445. grabkeyboard();
  446. setup(lines);
  447. if(maxname)
  448. cmdw = MIN(textw(&dc, maxname), mw / 3);
  449. match();
  450. run();
  451. return 0;
  452. }