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.

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