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 13 KiB

18 years ago
18 years ago
16 years ago
18 years ago
16 years ago
18 years ago
16 years ago
18 years ago
18 years ago
18 years ago
18 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
14 years ago
18 years ago
14 years ago
17 years ago
16 years ago
14 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
18 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
17 years ago
17 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
18 years ago
18 years ago
18 years ago
18 years ago
18 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
14 years ago
17 years ago
17 years ago
17 years ago
14 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
14 years ago
14 years ago
18 years ago
17 years ago
18 years ago
17 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
17 years ago
18 years ago
17 years ago
14 years ago
17 years ago
18 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. typedef struct Item Item;
  23. struct Item {
  24. char *text;
  25. Item *next; /* traverses all items */
  26. Item *left, *right; /* traverses items matching current search pattern */
  27. };
  28. /* forward declarations */
  29. static void appenditem(Item *i, Item **list, Item **last);
  30. static void calcoffsetsh(void);
  31. static void calcoffsetsv(void);
  32. static char *cistrstr(const char *s, const char *sub);
  33. static void cleanup(void);
  34. static void dinput(void);
  35. static void drawmenu(void);
  36. static void drawmenuh(void);
  37. static void drawmenuv(void);
  38. static Bool grabkeyboard(void);
  39. static void kpress(XKeyEvent *e);
  40. static void match(char *pattern);
  41. static void readstdin(void);
  42. static void run(void);
  43. static void setup(void);
  44. #include "config.h"
  45. #include "draw.h"
  46. /* variables */
  47. static char **argp = NULL;
  48. static char *maxname = NULL;
  49. static char *prompt = NULL;
  50. static char text[4096];
  51. static int cmdw = 0;
  52. static int promptw = 0;
  53. static int ret = 0;
  54. static int screen;
  55. static unsigned int lines = 0;
  56. static unsigned int numlockmask = 0;
  57. static unsigned int mw, mh;
  58. static unsigned long normcol[ColLast];
  59. static unsigned long selcol[ColLast];
  60. static Bool running = True;
  61. static Bool topbar = True;
  62. static DC dc;
  63. static Display *dpy;
  64. static Item *allitems = NULL; /* first of all items */
  65. static Item *item = NULL; /* first of pattern matching items */
  66. static Item *sel = NULL;
  67. static Item *next = NULL;
  68. static Item *prev = NULL;
  69. static Item *curr = NULL;
  70. static Window win, parent;
  71. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  72. static char *(*fstrstr)(const char *, const char *) = strstr;
  73. static void (*calcoffsets)(void) = calcoffsetsh;
  74. void
  75. appenditem(Item *i, Item **list, Item **last) {
  76. if(!(*last))
  77. *list = i;
  78. else
  79. (*last)->right = i;
  80. i->left = *last;
  81. i->right = NULL;
  82. *last = i;
  83. }
  84. void
  85. calcoffsetsh(void) {
  86. unsigned int w;
  87. w = promptw + cmdw + (2 * spaceitem);
  88. for(next = curr; next; next = next->right)
  89. if((w += MIN(textw(&dc, next->text), mw / 3)) > mw)
  90. break;
  91. w = promptw + cmdw + (2 * spaceitem);
  92. for(prev = curr; prev && prev->left; prev = prev->left)
  93. if((w += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
  94. break;
  95. }
  96. void
  97. calcoffsetsv(void) {
  98. unsigned int i;
  99. next = prev = curr;
  100. for(i = 0; i < lines && next; i++)
  101. next = next->right;
  102. for(i = 0; i < lines && prev && prev->left; i++)
  103. prev = prev->left;
  104. }
  105. char *
  106. cistrstr(const char *s, const char *sub) {
  107. int c, csub;
  108. unsigned int len;
  109. if(!sub)
  110. return (char *)s;
  111. if((c = tolower(*sub++)) != '\0') {
  112. len = strlen(sub);
  113. do {
  114. do {
  115. if((csub = *s++) == '\0')
  116. return NULL;
  117. }
  118. while(tolower(csub) != c);
  119. }
  120. while(strncasecmp(s, sub, len) != 0);
  121. s--;
  122. }
  123. return (char *)s;
  124. }
  125. void
  126. cleanup(void) {
  127. Item *itm;
  128. while(allitems) {
  129. itm = allitems->next;
  130. free(allitems->text);
  131. free(allitems);
  132. allitems = itm;
  133. }
  134. cleanupdraw(&dc);
  135. XDestroyWindow(dpy, win);
  136. XUngrabKeyboard(dpy, CurrentTime);
  137. }
  138. void
  139. dinput(void) {
  140. cleanup();
  141. argp[0] = "dinput";
  142. argp[1] = text;
  143. execvp("dinput", argp);
  144. eprint("cannot exec dinput\n");
  145. }
  146. void
  147. drawmenu(void) {
  148. dc.x = 0;
  149. dc.y = 0;
  150. dc.w = mw;
  151. dc.h = mh;
  152. drawtext(&dc, NULL, normcol, False);
  153. /* print prompt? */
  154. if(prompt) {
  155. dc.w = promptw;
  156. drawtext(&dc, prompt, selcol, False);
  157. dc.x += dc.w;
  158. }
  159. dc.w = mw - dc.x;
  160. /* print command */
  161. if(cmdw && item && lines == 0)
  162. dc.w = cmdw;
  163. drawtext(&dc, *text ? text : NULL, normcol, False);
  164. if(curr) {
  165. if(lines > 0)
  166. drawmenuv();
  167. else
  168. drawmenuh();
  169. }
  170. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  171. XFlush(dpy);
  172. }
  173. void
  174. drawmenuh(void) {
  175. Item *i;
  176. dc.x += cmdw;
  177. dc.w = spaceitem;
  178. drawtext(&dc, curr->left ? "<" : NULL, normcol, False);
  179. dc.x += dc.w;
  180. for(i = curr; i != next; i = i->right) {
  181. dc.w = MIN(textw(&dc, i->text), mw / 3);
  182. drawtext(&dc, i->text, (sel == i) ? selcol : normcol, False);
  183. dc.x += dc.w;
  184. }
  185. dc.w = spaceitem;
  186. dc.x = mw - dc.w;
  187. drawtext(&dc, next ? ">" : NULL, normcol, False);
  188. }
  189. void
  190. drawmenuv(void) {
  191. Item *i;
  192. dc.w = mw - dc.x;
  193. dc.h = dc.font.height + 2;
  194. dc.y = dc.h;
  195. for(i = curr; i != next; i = i->right) {
  196. drawtext(&dc, i->text, (sel == i) ? selcol : normcol, False);
  197. dc.y += dc.h;
  198. }
  199. dc.h = mh - dc.y;
  200. drawtext(&dc, NULL, normcol, False);
  201. }
  202. Bool
  203. grabkeyboard(void) {
  204. unsigned int len;
  205. for(len = 1000; len; len--) {
  206. if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  207. == GrabSuccess)
  208. break;
  209. usleep(1000);
  210. }
  211. return len > 0;
  212. }
  213. void
  214. kpress(XKeyEvent *e) {
  215. char buf[sizeof text];
  216. int num;
  217. unsigned int i, len;
  218. KeySym ksym;
  219. len = strlen(text);
  220. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  221. if(ksym == XK_KP_Enter)
  222. ksym = XK_Return;
  223. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  224. ksym = (ksym - XK_KP_0) + XK_0;
  225. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  226. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  227. || IsPrivateKeypadKey(ksym))
  228. return;
  229. /* first check if a control mask is omitted */
  230. if(e->state & ControlMask) {
  231. switch(tolower(ksym)) {
  232. default:
  233. return;
  234. case XK_a:
  235. ksym = XK_Home;
  236. break;
  237. case XK_b:
  238. ksym = XK_Left;
  239. break;
  240. case XK_c:
  241. ksym = XK_Escape;
  242. break;
  243. case XK_e:
  244. ksym = XK_End;
  245. break;
  246. case XK_f:
  247. ksym = XK_Right;
  248. break;
  249. case XK_h:
  250. ksym = XK_BackSpace;
  251. break;
  252. case XK_i:
  253. ksym = XK_Tab;
  254. break;
  255. case XK_j:
  256. case XK_m:
  257. ksym = XK_Return;
  258. break;
  259. case XK_n:
  260. ksym = XK_Down;
  261. break;
  262. case XK_p:
  263. ksym = XK_Up;
  264. break;
  265. case XK_u:
  266. text[0] = '\0';
  267. match(text);
  268. break;
  269. case XK_w:
  270. if(len == 0)
  271. return;
  272. i = len;
  273. while(i-- > 0 && text[i] == ' ');
  274. while(i-- > 0 && text[i] != ' ');
  275. text[++i] = '\0';
  276. match(text);
  277. break;
  278. case XK_x:
  279. dinput();
  280. break;
  281. }
  282. }
  283. switch(ksym) {
  284. default:
  285. num = MIN(num, sizeof text);
  286. if(num && !iscntrl((int) buf[0])) {
  287. memcpy(text + len, buf, num + 1);
  288. len += num;
  289. match(text);
  290. }
  291. break;
  292. case XK_BackSpace:
  293. if(len == 0)
  294. return;
  295. for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
  296. len -= i;
  297. text[len] = '\0';
  298. match(text);
  299. break;
  300. case XK_End:
  301. while(next) {
  302. sel = curr = next;
  303. calcoffsets();
  304. }
  305. while(sel && sel->right)
  306. sel = sel->right;
  307. break;
  308. case XK_Escape:
  309. ret = 1;
  310. running = False;
  311. return;
  312. case XK_Home:
  313. sel = curr = item;
  314. calcoffsets();
  315. break;
  316. case XK_Left:
  317. case XK_Up:
  318. if(!sel || !sel->left)
  319. return;
  320. sel = sel->left;
  321. if(sel->right == curr) {
  322. curr = prev;
  323. calcoffsets();
  324. }
  325. break;
  326. case XK_Next:
  327. if(!next)
  328. return;
  329. sel = curr = next;
  330. calcoffsets();
  331. break;
  332. case XK_Prior:
  333. if(!prev)
  334. return;
  335. sel = curr = prev;
  336. calcoffsets();
  337. break;
  338. case XK_Return:
  339. if((e->state & ShiftMask) || !sel)
  340. fprintf(stdout, "%s", text);
  341. else
  342. fprintf(stdout, "%s", sel->text);
  343. fflush(stdout);
  344. running = False;
  345. return;
  346. case XK_Right:
  347. case XK_Down:
  348. if(!sel || !sel->right)
  349. return;
  350. sel = sel->right;
  351. if(sel == next) {
  352. curr = next;
  353. calcoffsets();
  354. }
  355. break;
  356. case XK_Tab:
  357. if(sel)
  358. strncpy(text, sel->text, sizeof text);
  359. dinput();
  360. break;
  361. }
  362. drawmenu();
  363. }
  364. void
  365. match(char *pattern) {
  366. unsigned int plen;
  367. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  368. if(!pattern)
  369. return;
  370. plen = strlen(pattern);
  371. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  372. for(i = allitems; i; i = i->next)
  373. if(!fstrncmp(pattern, i->text, plen + 1))
  374. appenditem(i, &lexact, &exactend);
  375. else if(!fstrncmp(pattern, i->text, plen))
  376. appenditem(i, &lprefix, &prefixend);
  377. else if(fstrstr(i->text, pattern))
  378. appenditem(i, &lsubstr, &substrend);
  379. if(lexact) {
  380. item = lexact;
  381. itemend = exactend;
  382. }
  383. if(lprefix) {
  384. if(itemend) {
  385. itemend->right = lprefix;
  386. lprefix->left = itemend;
  387. }
  388. else
  389. item = lprefix;
  390. itemend = prefixend;
  391. }
  392. if(lsubstr) {
  393. if(itemend) {
  394. itemend->right = lsubstr;
  395. lsubstr->left = itemend;
  396. }
  397. else
  398. item = lsubstr;
  399. }
  400. curr = prev = next = sel = item;
  401. calcoffsets();
  402. }
  403. void
  404. readstdin(void) {
  405. char *p, buf[sizeof text];
  406. unsigned int len = 0, max = 0;
  407. Item *i, *new;
  408. i = NULL;
  409. while(fgets(buf, sizeof buf, stdin)) {
  410. len = strlen(buf);
  411. if(buf[len-1] == '\n')
  412. buf[--len] = '\0';
  413. if(!(p = strdup(buf)))
  414. eprint("cannot strdup %u bytes\n", len);
  415. if((max = MAX(max, len)) == len)
  416. maxname = p;
  417. if(!(new = malloc(sizeof *new)))
  418. eprint("cannot malloc %u bytes\n", sizeof *new);
  419. new->next = new->left = new->right = NULL;
  420. new->text = p;
  421. if(!i)
  422. allitems = new;
  423. else
  424. i->next = new;
  425. i = new;
  426. }
  427. }
  428. void
  429. run(void) {
  430. XEvent ev;
  431. /* main event loop */
  432. while(running && !XNextEvent(dpy, &ev))
  433. switch(ev.type) {
  434. case KeyPress:
  435. kpress(&ev.xkey);
  436. break;
  437. case Expose:
  438. if(ev.xexpose.count == 0)
  439. drawmenu();
  440. break;
  441. case VisibilityNotify:
  442. if (ev.xvisibility.state != VisibilityUnobscured)
  443. XRaiseWindow(dpy, win);
  444. break;
  445. }
  446. }
  447. void
  448. setup(void) {
  449. int i, j, x, y;
  450. #if XINERAMA
  451. int n;
  452. XineramaScreenInfo *info = NULL;
  453. #endif
  454. XModifierKeymap *modmap;
  455. XSetWindowAttributes wa;
  456. XWindowAttributes pwa;
  457. /* init modifier map */
  458. modmap = XGetModifierMapping(dpy);
  459. for(i = 0; i < 8; i++)
  460. for(j = 0; j < modmap->max_keypermod; j++) {
  461. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  462. == XKeysymToKeycode(dpy, XK_Num_Lock))
  463. numlockmask = (1 << i);
  464. }
  465. XFreeModifiermap(modmap);
  466. dc.dpy = dpy;
  467. normcol[ColBG] = getcolor(&dc, normbgcolor);
  468. normcol[ColFG] = getcolor(&dc, normfgcolor);
  469. selcol[ColBG] = getcolor(&dc, selbgcolor);
  470. selcol[ColFG] = getcolor(&dc, selfgcolor);
  471. initfont(&dc, font);
  472. /* menu window */
  473. wa.override_redirect = True;
  474. wa.background_pixmap = ParentRelative;
  475. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  476. /* menu window geometry */
  477. mh = (dc.font.height + 2) * (lines + 1);
  478. #if XINERAMA
  479. if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  480. i = 0;
  481. if(n > 1) {
  482. int di;
  483. unsigned int dui;
  484. Window dummy;
  485. if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
  486. for(i = 0; i < n; i++)
  487. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  488. break;
  489. }
  490. x = info[i].x_org;
  491. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  492. mw = info[i].width;
  493. XFree(info);
  494. }
  495. else
  496. #endif
  497. {
  498. if(!XGetWindowAttributes(dpy, parent, &pwa))
  499. eprint("cannot get window attributes");
  500. x = 0;
  501. y = topbar ? 0 : pwa.height - mh;
  502. mw = pwa.width;
  503. }
  504. win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
  505. DefaultDepth(dpy, screen), CopyFromParent,
  506. DefaultVisual(dpy, screen),
  507. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  508. setupdraw(&dc, win);
  509. if(maxname)
  510. cmdw = MIN(textw(&dc, maxname), mw / 3);
  511. if(prompt)
  512. promptw = MIN(textw(&dc, prompt), mw / 5);
  513. text[0] = '\0';
  514. match(text);
  515. XMapRaised(dpy, win);
  516. }
  517. int
  518. main(int argc, char *argv[]) {
  519. unsigned int i;
  520. /* command line args */
  521. progname = argv[0];
  522. for(i = 1; i < argc; i++)
  523. if(!strcmp(argv[i], "-i")) {
  524. fstrncmp = strncasecmp;
  525. fstrstr = cistrstr;
  526. }
  527. else if(!strcmp(argv[i], "-b"))
  528. topbar = False;
  529. else if(!strcmp(argv[i], "-e")) {
  530. if(++i < argc) parent = atoi(argv[i]);
  531. }
  532. else if(!strcmp(argv[i], "-l")) {
  533. if(++i < argc) lines = atoi(argv[i]);
  534. if(lines > 0)
  535. calcoffsets = calcoffsetsv;
  536. }
  537. else if(!strcmp(argv[i], "-fn")) {
  538. if(++i < argc) font = argv[i];
  539. }
  540. else if(!strcmp(argv[i], "-nb")) {
  541. if(++i < argc) normbgcolor = argv[i];
  542. }
  543. else if(!strcmp(argv[i], "-nf")) {
  544. if(++i < argc) normfgcolor = argv[i];
  545. }
  546. else if(!strcmp(argv[i], "-p")) {
  547. if(++i < argc) prompt = argv[i];
  548. }
  549. else if(!strcmp(argv[i], "-sb")) {
  550. if(++i < argc) selbgcolor = argv[i];
  551. }
  552. else if(!strcmp(argv[i], "-sf")) {
  553. if(++i < argc) selfgcolor = argv[i];
  554. }
  555. else if(!strcmp(argv[i], "-v")) {
  556. printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  557. exit(EXIT_SUCCESS);
  558. }
  559. else {
  560. fputs("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  561. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
  562. exit(EXIT_FAILURE);
  563. }
  564. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  565. fprintf(stderr, "dmenu: warning: no locale support\n");
  566. if(!(dpy = XOpenDisplay(NULL)))
  567. eprint("cannot open display\n");
  568. screen = DefaultScreen(dpy);
  569. if(!parent)
  570. parent = RootWindow(dpy, screen);
  571. if(!(argp = malloc(sizeof *argp * (argc+2))))
  572. eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
  573. memcpy(argp + 2, argv + 1, sizeof *argp * argc);
  574. readstdin();
  575. running = grabkeyboard();
  576. setup();
  577. drawmenu();
  578. XSync(dpy, False);
  579. run();
  580. cleanup();
  581. XCloseDisplay(dpy);
  582. return ret;
  583. }