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