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