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.

806 lines
18 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
16 years ago
16 years ago
18 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 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
17 years ago
14 years ago
14 years ago
14 years ago
14 years ago
17 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
14 years ago
17 years ago
18 years ago
16 years ago
16 years ago
16 years ago
16 years ago
18 years ago
16 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
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
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
18 years ago
17 years ago
18 years ago
18 years ago
17 years ago
18 years ago
18 years ago
17 years ago
18 years ago
16 years ago
17 years ago
16 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
  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. /* enums */
  23. enum { ColFG, ColBG, ColLast };
  24. /* typedefs */
  25. typedef struct {
  26. int x, y, w, h;
  27. unsigned long norm[ColLast];
  28. unsigned long sel[ColLast];
  29. Drawable drawable;
  30. GC gc;
  31. struct {
  32. XFontStruct *xfont;
  33. XFontSet set;
  34. int ascent;
  35. int descent;
  36. int height;
  37. } font;
  38. } DC; /* draw context */
  39. typedef struct Item Item;
  40. struct Item {
  41. char *text;
  42. Item *next; /* traverses all items */
  43. Item *left, *right; /* traverses items matching current search pattern */
  44. };
  45. /* forward declarations */
  46. static void appenditem(Item *i, Item **list, Item **last);
  47. static void calcoffsetsh(void);
  48. static void calcoffsetsv(void);
  49. static char *cistrstr(const char *s, const char *sub);
  50. static void cleanup(void);
  51. static void drawcursor(void);
  52. static void drawmenu(void);
  53. static void drawmenuh(void);
  54. static void drawmenuv(void);
  55. static void drawtext(const char *text, unsigned long col[ColLast]);
  56. static void eprint(const char *errstr, ...);
  57. static unsigned long getcolor(const char *colstr);
  58. static Bool grabkeyboard(void);
  59. static void initfont(const char *fontstr);
  60. static void kpress(XKeyEvent * e);
  61. static void match(char *pattern);
  62. static void readstdin(void);
  63. static void run(void);
  64. static void setup(Bool topbar);
  65. static int textnw(const char *text, unsigned int len);
  66. static int textw(const char *text);
  67. #include "config.h"
  68. /* variables */
  69. static char *maxname = NULL;
  70. static char *prompt = NULL;
  71. static char text[4096];
  72. static int cmdw = 0;
  73. static int promptw = 0;
  74. static int ret = 0;
  75. static int cursor = 0;
  76. static int screen;
  77. static unsigned int mw, mh;
  78. static unsigned int numlockmask = 0;
  79. static Bool running = True;
  80. static Display *dpy;
  81. static DC dc;
  82. static Item *allitems = NULL; /* first of all items */
  83. static Item *item = NULL; /* first of pattern matching items */
  84. static Item *sel = NULL;
  85. static Item *next = NULL;
  86. static Item *prev = NULL;
  87. static Item *curr = NULL;
  88. static Window parent, win;
  89. static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
  90. static char *(*fstrstr)(const char *, const char *) = strstr;
  91. static unsigned int lines = 0;
  92. static void (*calcoffsets)(void) = calcoffsetsh;
  93. void
  94. appenditem(Item *i, Item **list, Item **last) {
  95. if(!(*last))
  96. *list = i;
  97. else
  98. (*last)->right = i;
  99. i->left = *last;
  100. i->right = NULL;
  101. *last = i;
  102. }
  103. void
  104. calcoffsetsh(void) {
  105. unsigned int w;
  106. if(!curr)
  107. return;
  108. w = promptw + cmdw + 2 * spaceitem;
  109. for(next = curr; next && w < mw; next=next->right)
  110. w += MIN(textw(next->text), mw / 3);
  111. w = promptw + cmdw + 2 * spaceitem;
  112. for(prev = curr; prev && prev->left && w < mw; prev=prev->left)
  113. w += MIN(textw(prev->left->text), mw / 3);
  114. }
  115. void
  116. calcoffsetsv(void) {
  117. unsigned int h;
  118. if(!curr)
  119. return;
  120. h = (dc.font.height + 2) * lines;
  121. for(next = curr; next && h > 0; next = next->right)
  122. h -= dc.font.height + 2;
  123. h = (dc.font.height + 2) * lines;
  124. for(prev = curr; prev && prev->left && h > 0; prev = prev->left)
  125. h -= dc.font.height + 2;
  126. }
  127. char *
  128. cistrstr(const char *s, const char *sub) {
  129. int c, csub;
  130. unsigned int len;
  131. if(!sub)
  132. return (char *)s;
  133. if((c = *sub++) != '\0') {
  134. c = tolower(c);
  135. len = strlen(sub);
  136. do {
  137. do {
  138. if((csub = *s++) == '\0')
  139. return NULL;
  140. }
  141. while(tolower(csub) != c);
  142. }
  143. while(strncasecmp(s, sub, len) != 0);
  144. s--;
  145. }
  146. return (char *)s;
  147. }
  148. void
  149. cleanup(void) {
  150. if(dc.font.set)
  151. XFreeFontSet(dpy, dc.font.set);
  152. else
  153. XFreeFont(dpy, dc.font.xfont);
  154. XFreePixmap(dpy, dc.drawable);
  155. XFreeGC(dpy, dc.gc);
  156. XDestroyWindow(dpy, win);
  157. XUngrabKeyboard(dpy, CurrentTime);
  158. }
  159. void
  160. drawcursor(void) {
  161. XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 };
  162. r.x += textnw(text, cursor) + dc.font.height / 2;
  163. XSetForeground(dpy, dc.gc, dc.norm[ColFG]);
  164. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  165. }
  166. void
  167. drawmenu(void) {
  168. dc.x = 0;
  169. dc.y = 0;
  170. dc.w = mw;
  171. dc.h = mh;
  172. drawtext(NULL, dc.norm);
  173. /* print prompt? */
  174. if(prompt) {
  175. dc.w = promptw;
  176. drawtext(prompt, dc.sel);
  177. dc.x += dc.w;
  178. }
  179. dc.w = mw - dc.x;
  180. /* print command */
  181. if(cmdw && item && lines == 0)
  182. dc.w = cmdw;
  183. drawtext(text[0] ? text : NULL, dc.norm);
  184. drawcursor();
  185. if(curr) {
  186. if(lines > 0)
  187. drawmenuv();
  188. else
  189. drawmenuh();
  190. }
  191. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  192. XFlush(dpy);
  193. }
  194. void
  195. drawmenuh(void) {
  196. Item *i;
  197. dc.x += cmdw;
  198. dc.w = spaceitem;
  199. drawtext(curr->left ? "<" : NULL, dc.norm);
  200. dc.x += dc.w;
  201. for(i = curr; i != next; i=i->right) {
  202. dc.w = MIN(textw(i->text), mw / 3);
  203. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  204. dc.x += dc.w;
  205. }
  206. dc.w = spaceitem;
  207. dc.x = mw - dc.w;
  208. drawtext(next ? ">" : NULL, dc.norm);
  209. }
  210. void
  211. drawmenuv(void) {
  212. Item *i;
  213. dc.w = mw - dc.x;
  214. dc.h = dc.font.height + 2;
  215. dc.y = dc.h;
  216. for(i = curr; i != next; i=i->right) {
  217. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  218. dc.y += dc.h;
  219. }
  220. dc.h = mh - dc.y;
  221. drawtext(NULL, dc.norm);
  222. }
  223. void
  224. drawtext(const char *text, unsigned long col[ColLast]) {
  225. char buf[256];
  226. int i, x, y, h, len, olen;
  227. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  228. XSetForeground(dpy, dc.gc, col[ColBG]);
  229. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  230. if(!text)
  231. return;
  232. olen = strlen(text);
  233. h = dc.font.height;
  234. y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
  235. x = dc.x + (h / 2);
  236. /* shorten text if necessary */
  237. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  238. if(!len)
  239. return;
  240. memcpy(buf, text, len);
  241. if(len < olen)
  242. for(i = len; i && i > len - 3; buf[--i] = '.');
  243. XSetForeground(dpy, dc.gc, col[ColFG]);
  244. if(dc.font.set)
  245. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  246. else
  247. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  248. }
  249. void
  250. eprint(const char *errstr, ...) {
  251. va_list ap;
  252. va_start(ap, errstr);
  253. vfprintf(stderr, errstr, ap);
  254. va_end(ap);
  255. exit(EXIT_FAILURE);
  256. }
  257. unsigned long
  258. getcolor(const char *colstr) {
  259. Colormap cmap = DefaultColormap(dpy, screen);
  260. XColor color;
  261. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  262. eprint("dmenu: cannot allocate color '%s'\n", colstr);
  263. return color.pixel;
  264. }
  265. Bool
  266. grabkeyboard(void) {
  267. unsigned int len;
  268. for(len = 1000; len; len--) {
  269. if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  270. == GrabSuccess)
  271. break;
  272. usleep(1000);
  273. }
  274. return len > 0;
  275. }
  276. void
  277. initfont(const char *fontstr) {
  278. char *def, **missing = NULL;
  279. int i, n;
  280. if(!fontstr || fontstr[0] == '\0')
  281. eprint("dmenu: cannot load font: '%s'\n", fontstr);
  282. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  283. if(missing)
  284. XFreeStringList(missing);
  285. if(dc.font.set) {
  286. XFontStruct **xfonts;
  287. char **font_names;
  288. dc.font.ascent = dc.font.descent = 0;
  289. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  290. for(i = 0; i < n; i++) {
  291. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  292. dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
  293. xfonts++;
  294. }
  295. }
  296. else {
  297. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  298. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  299. eprint("dmenu: cannot load font: '%s'\n", fontstr);
  300. dc.font.ascent = dc.font.xfont->ascent;
  301. dc.font.descent = dc.font.xfont->descent;
  302. }
  303. dc.font.height = dc.font.ascent + dc.font.descent;
  304. }
  305. void
  306. kpress(XKeyEvent * e) {
  307. char buf[sizeof text];
  308. int i, num;
  309. unsigned int len;
  310. KeySym ksym;
  311. len = strlen(text);
  312. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  313. if(IsKeypadKey(ksym)) {
  314. if(ksym == XK_KP_Enter)
  315. ksym = XK_Return;
  316. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  317. ksym = (ksym - XK_KP_0) + XK_0;
  318. }
  319. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  320. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  321. || IsPrivateKeypadKey(ksym))
  322. return;
  323. /* first check if a control mask is omitted */
  324. if(e->state & ControlMask) {
  325. switch (ksym) {
  326. default: /* ignore other control sequences */
  327. return;
  328. case XK_a:
  329. case XK_A:
  330. ksym = XK_Home;
  331. break;
  332. case XK_c:
  333. case XK_C:
  334. ksym = XK_Escape;
  335. break;
  336. case XK_e:
  337. case XK_E:
  338. ksym = XK_End;
  339. break;
  340. case XK_h:
  341. case XK_H:
  342. ksym = XK_BackSpace;
  343. break;
  344. case XK_i:
  345. case XK_I:
  346. ksym = XK_Tab;
  347. break;
  348. case XK_j:
  349. case XK_J:
  350. ksym = XK_Return;
  351. break;
  352. case XK_u:
  353. case XK_U:
  354. memmove(text, text + cursor, sizeof text - cursor + 1);
  355. cursor = 0;
  356. match(text);
  357. break;
  358. case XK_w:
  359. case XK_W:
  360. if(cursor > 0) {
  361. i = cursor;
  362. while(i-- > 0 && text[i] == ' ');
  363. while(i-- > 0 && text[i] != ' ');
  364. memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
  365. cursor = i + 1;
  366. match(text);
  367. }
  368. break;
  369. }
  370. }
  371. if(CLEANMASK(e->state) & Mod1Mask) {
  372. switch(ksym) {
  373. default: return;
  374. case XK_h:
  375. ksym = XK_Left;
  376. break;
  377. case XK_l:
  378. ksym = XK_Right;
  379. break;
  380. case XK_j:
  381. ksym = XK_Next;
  382. break;
  383. case XK_k:
  384. ksym = XK_Prior;
  385. break;
  386. case XK_g:
  387. ksym = XK_Home;
  388. break;
  389. case XK_G:
  390. ksym = XK_End;
  391. break;
  392. case XK_p:
  393. {
  394. FILE *fp;
  395. char *s;
  396. if(!(fp = popen("sselp", "r")))
  397. eprint("dmenu: cannot popen sselp\n");
  398. s = fgets(buf, sizeof buf, fp);
  399. pclose(fp);
  400. if(s == NULL)
  401. return;
  402. }
  403. num = strlen(buf);
  404. if(num && buf[num-1] == '\n')
  405. buf[--num] = '\0';
  406. break;
  407. }
  408. }
  409. switch(ksym) {
  410. default:
  411. num = MIN(num, sizeof text - cursor);
  412. if(num && !iscntrl((int) buf[0])) {
  413. memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
  414. memcpy(text + cursor, buf, num);
  415. cursor += num;
  416. match(text);
  417. }
  418. break;
  419. case XK_BackSpace:
  420. if(cursor > 0) {
  421. for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
  422. memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
  423. cursor -= i;
  424. match(text);
  425. }
  426. break;
  427. case XK_Delete:
  428. for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
  429. memmove(text + cursor, text + cursor + i, sizeof text - cursor);
  430. match(text);
  431. break;
  432. case XK_End:
  433. if(cursor < len) {
  434. cursor = len;
  435. break;
  436. }
  437. while(next) {
  438. sel = curr = next;
  439. calcoffsets();
  440. }
  441. while(sel && sel->right)
  442. sel = sel->right;
  443. break;
  444. case XK_Escape:
  445. ret = 1;
  446. running = False;
  447. break;
  448. case XK_Home:
  449. if(sel == item) {
  450. cursor = 0;
  451. break;
  452. }
  453. sel = curr = item;
  454. calcoffsets();
  455. break;
  456. case XK_Left:
  457. case XK_Up:
  458. if(sel && sel->left){
  459. sel=sel->left;
  460. if(sel->right == curr) {
  461. curr = prev;
  462. calcoffsets();
  463. }
  464. }
  465. else if(cursor > 0) {
  466. do {
  467. cursor--;
  468. } while(cursor > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
  469. } else
  470. return;
  471. break;
  472. case XK_Next:
  473. if(!next)
  474. return;
  475. sel = curr = next;
  476. calcoffsets();
  477. break;
  478. case XK_Prior:
  479. if(!prev)
  480. return;
  481. sel = curr = prev;
  482. calcoffsets();
  483. break;
  484. case XK_Return:
  485. if((e->state & ShiftMask) || !sel)
  486. fprintf(stdout, "%s", text);
  487. else
  488. fprintf(stdout, "%s", sel->text);
  489. fflush(stdout);
  490. running = False;
  491. break;
  492. case XK_Right:
  493. case XK_Down:
  494. if(cursor < len) {
  495. do {
  496. cursor++;
  497. } while(cursor < len && !IS_UTF8_1ST_CHAR(text[cursor]));
  498. } else if(sel && sel->right) {
  499. sel=sel->right;
  500. if(sel == next) {
  501. curr = next;
  502. calcoffsets();
  503. }
  504. }
  505. else
  506. return;
  507. break;
  508. case XK_Tab:
  509. if(!sel)
  510. return;
  511. strncpy(text, sel->text, sizeof text);
  512. cursor = strlen(text);
  513. match(text);
  514. break;
  515. }
  516. drawmenu();
  517. }
  518. void
  519. match(char *pattern) {
  520. unsigned int plen;
  521. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  522. if(!pattern)
  523. return;
  524. plen = strlen(pattern);
  525. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  526. for(i = allitems; i; i = i->next)
  527. if(!fstrncmp(pattern, i->text, plen + 1))
  528. appenditem(i, &lexact, &exactend);
  529. else if(!fstrncmp(pattern, i->text, plen))
  530. appenditem(i, &lprefix, &prefixend);
  531. else if(fstrstr(i->text, pattern))
  532. appenditem(i, &lsubstr, &substrend);
  533. if(lexact) {
  534. item = lexact;
  535. itemend = exactend;
  536. }
  537. if(lprefix) {
  538. if(itemend) {
  539. itemend->right = lprefix;
  540. lprefix->left = itemend;
  541. }
  542. else
  543. item = lprefix;
  544. itemend = prefixend;
  545. }
  546. if(lsubstr) {
  547. if(itemend) {
  548. itemend->right = lsubstr;
  549. lsubstr->left = itemend;
  550. }
  551. else
  552. item = lsubstr;
  553. }
  554. curr = prev = next = sel = item;
  555. calcoffsets();
  556. }
  557. void
  558. readstdin(void) {
  559. char *p, buf[sizeof text];
  560. unsigned int len = 0, max = 0;
  561. Item *i, *new;
  562. i = NULL;
  563. while(fgets(buf, sizeof buf, stdin)) {
  564. len = strlen(buf);
  565. if(buf[len-1] == '\n')
  566. buf[--len] = '\0';
  567. if(!(p = strdup(buf)))
  568. eprint("dmenu: cannot strdup %u bytes\n", len);
  569. if(max < len || !maxname) {
  570. maxname = p;
  571. max = len;
  572. }
  573. if(!(new = malloc(sizeof *new)))
  574. eprint("dmenu: cannot malloc %u bytes\n", sizeof *new);
  575. new->next = new->left = new->right = NULL;
  576. new->text = p;
  577. if(!i)
  578. allitems = new;
  579. else
  580. i->next = new;
  581. i = new;
  582. }
  583. }
  584. void
  585. run(void) {
  586. XEvent ev;
  587. /* main event loop */
  588. while(running && !XNextEvent(dpy, &ev))
  589. switch (ev.type) {
  590. default: /* ignore all crap */
  591. break;
  592. case KeyPress:
  593. kpress(&ev.xkey);
  594. break;
  595. case Expose:
  596. if(ev.xexpose.count == 0)
  597. drawmenu();
  598. break;
  599. case VisibilityNotify:
  600. if (ev.xvisibility.state != VisibilityUnobscured)
  601. XRaiseWindow(dpy, win);
  602. break;
  603. }
  604. }
  605. void
  606. setup(Bool topbar) {
  607. int i, j, x, y;
  608. #if XINERAMA
  609. int n;
  610. XineramaScreenInfo *info = NULL;
  611. #endif
  612. XModifierKeymap *modmap;
  613. XSetWindowAttributes wa;
  614. XWindowAttributes pwa;
  615. /* init modifier map */
  616. modmap = XGetModifierMapping(dpy);
  617. for(i = 0; i < 8; i++)
  618. for(j = 0; j < modmap->max_keypermod; j++) {
  619. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  620. == XKeysymToKeycode(dpy, XK_Num_Lock))
  621. numlockmask = (1 << i);
  622. }
  623. XFreeModifiermap(modmap);
  624. /* style */
  625. dc.norm[ColBG] = getcolor(normbgcolor);
  626. dc.norm[ColFG] = getcolor(normfgcolor);
  627. dc.sel[ColBG] = getcolor(selbgcolor);
  628. dc.sel[ColFG] = getcolor(selfgcolor);
  629. initfont(font);
  630. /* menu window */
  631. wa.override_redirect = True;
  632. wa.background_pixmap = ParentRelative;
  633. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
  634. /* menu window geometry */
  635. mh = (dc.font.height + 2) * (lines + 1);
  636. #if XINERAMA
  637. if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  638. i = 0;
  639. if(n > 1) {
  640. int di;
  641. unsigned int dui;
  642. Window dummy;
  643. if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
  644. for(i = 0; i < n; i++)
  645. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  646. break;
  647. }
  648. x = info[i].x_org;
  649. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  650. mw = info[i].width;
  651. XFree(info);
  652. }
  653. else
  654. #endif
  655. {
  656. XGetWindowAttributes(dpy, parent, &pwa);
  657. x = 0;
  658. y = topbar ? 0 : pwa.height - mh;
  659. mw = pwa.width;
  660. }
  661. win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
  662. DefaultDepth(dpy, screen), CopyFromParent,
  663. DefaultVisual(dpy, screen),
  664. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  665. /* pixmap */
  666. dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
  667. dc.gc = XCreateGC(dpy, parent, 0, NULL);
  668. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  669. if(!dc.font.set)
  670. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  671. if(maxname)
  672. cmdw = MIN(textw(maxname), mw / 3);
  673. if(prompt)
  674. promptw = MIN(textw(prompt), mw / 5);
  675. text[0] = '\0';
  676. match(text);
  677. XMapRaised(dpy, win);
  678. }
  679. int
  680. textnw(const char *text, unsigned int len) {
  681. XRectangle r;
  682. if(dc.font.set) {
  683. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  684. return r.width;
  685. }
  686. return XTextWidth(dc.font.xfont, text, len);
  687. }
  688. int
  689. textw(const char *text) {
  690. return textnw(text, strlen(text)) + dc.font.height;
  691. }
  692. int
  693. main(int argc, char *argv[]) {
  694. unsigned int i;
  695. Bool topbar = True;
  696. /* command line args */
  697. for(i = 1; i < argc; i++)
  698. if(!strcmp(argv[i], "-i")) {
  699. fstrncmp = strncasecmp;
  700. fstrstr = cistrstr;
  701. }
  702. else if(!strcmp(argv[i], "-b"))
  703. topbar = False;
  704. else if(!strcmp(argv[i], "-e")) {
  705. if(++i < argc) parent = atoi(argv[i]);
  706. }
  707. else if(!strcmp(argv[i], "-l")) {
  708. if(++i < argc) lines = atoi(argv[i]);
  709. if(lines > 0)
  710. calcoffsets = calcoffsetsv;
  711. }
  712. else if(!strcmp(argv[i], "-fn")) {
  713. if(++i < argc) font = argv[i];
  714. }
  715. else if(!strcmp(argv[i], "-nb")) {
  716. if(++i < argc) normbgcolor = argv[i];
  717. }
  718. else if(!strcmp(argv[i], "-nf")) {
  719. if(++i < argc) normfgcolor = argv[i];
  720. }
  721. else if(!strcmp(argv[i], "-p")) {
  722. if(++i < argc) prompt = argv[i];
  723. }
  724. else if(!strcmp(argv[i], "-sb")) {
  725. if(++i < argc) selbgcolor = argv[i];
  726. }
  727. else if(!strcmp(argv[i], "-sf")) {
  728. if(++i < argc) selfgcolor = argv[i];
  729. }
  730. else if(!strcmp(argv[i], "-v"))
  731. eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  732. else
  733. eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  734. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  735. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  736. fprintf(stderr, "dmenu: warning: no locale support\n");
  737. if(!(dpy = XOpenDisplay(NULL)))
  738. eprint("dmenu: cannot open display\n");
  739. screen = DefaultScreen(dpy);
  740. if(!parent)
  741. parent = RootWindow(dpy, screen);
  742. readstdin();
  743. running = grabkeyboard();
  744. setup(topbar);
  745. drawmenu();
  746. XSync(dpy, False);
  747. run();
  748. cleanup();
  749. XCloseDisplay(dpy);
  750. return ret;
  751. }