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