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.
 
 
 
 
 
 

402 lines
7.6 KiB

  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
  4. * See LICENSE file for license details.
  5. */
  6. #include "dmenu.h"
  7. #include <ctype.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/select.h>
  13. #include <sys/time.h>
  14. #include <X11/cursorfont.h>
  15. #include <X11/Xutil.h>
  16. #include <X11/keysym.h>
  17. typedef struct Item Item;
  18. struct Item {
  19. Item *next; /* traverses all items */
  20. Item *left, *right; /* traverses items matching current search pattern */
  21. char *text;
  22. };
  23. /* static */
  24. static char text[4096];
  25. static int mx, my, mw, mh;
  26. static int ret = 0;
  27. static int nitem = 0;
  28. static unsigned int cmdw = 0;
  29. static Bool running = True;
  30. static Item *allitems = NULL; /* first of all items */
  31. static Item *item = NULL; /* first of pattern matching items */
  32. static Item *sel = NULL;
  33. static Item *next = NULL;
  34. static Item *prev = NULL;
  35. static Item *curr = NULL;
  36. static Window root;
  37. static Window win;
  38. static void
  39. calcoffsets()
  40. {
  41. unsigned int tw, w;
  42. if(!curr)
  43. return;
  44. w = cmdw + 2 * SPACE;
  45. for(next = curr; next; next=next->right) {
  46. tw = textw(next->text);
  47. if(tw > mw / 3)
  48. tw = mw / 3;
  49. w += tw;
  50. if(w > mw)
  51. break;
  52. }
  53. w = cmdw + 2 * SPACE;
  54. for(prev = curr; prev && prev->left; prev=prev->left) {
  55. tw = textw(prev->left->text);
  56. if(tw > mw / 3)
  57. tw = mw / 3;
  58. w += tw;
  59. if(w > mw)
  60. break;
  61. }
  62. }
  63. static void
  64. drawmenu()
  65. {
  66. Item *i;
  67. dc.x = 0;
  68. dc.y = 0;
  69. dc.w = mw;
  70. dc.h = mh;
  71. drawtext(NULL, dc.norm);
  72. /* print command */
  73. if(cmdw && item)
  74. dc.w = cmdw;
  75. drawtext(text[0] ? text : NULL, dc.norm);
  76. dc.x += cmdw;
  77. if(curr) {
  78. dc.w = SPACE;
  79. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  80. dc.x += dc.w;
  81. /* determine maximum items */
  82. for(i = curr; i != next; i=i->right) {
  83. dc.w = textw(i->text);
  84. if(dc.w > mw / 3)
  85. dc.w = mw / 3;
  86. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  87. dc.x += dc.w;
  88. }
  89. dc.x = mw - SPACE;
  90. dc.w = SPACE;
  91. drawtext(next ? ">" : NULL, dc.norm);
  92. }
  93. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  94. XFlush(dpy);
  95. }
  96. static void
  97. match(char *pattern)
  98. {
  99. unsigned int plen;
  100. Item *i, *j;
  101. if(!pattern)
  102. return;
  103. plen = strlen(pattern);
  104. item = j = NULL;
  105. nitem = 0;
  106. for(i = allitems; i; i=i->next)
  107. if(!plen || !strncmp(pattern, i->text, plen)) {
  108. if(!j)
  109. item = i;
  110. else
  111. j->right = i;
  112. i->left = j;
  113. i->right = NULL;
  114. j = i;
  115. nitem++;
  116. }
  117. for(i = allitems; i; i=i->next)
  118. if(plen && strncmp(pattern, i->text, plen)
  119. && strstr(i->text, pattern)) {
  120. if(!j)
  121. item = i;
  122. else
  123. j->right = i;
  124. i->left = j;
  125. i->right = NULL;
  126. j = i;
  127. nitem++;
  128. }
  129. curr = prev = next = sel = item;
  130. calcoffsets();
  131. }
  132. static void
  133. kpress(XKeyEvent * e)
  134. {
  135. char buf[32];
  136. int num, prev_nitem;
  137. unsigned int i, len;
  138. KeySym ksym;
  139. len = strlen(text);
  140. buf[0] = 0;
  141. num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
  142. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  143. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  144. || IsPrivateKeypadKey(ksym))
  145. return;
  146. /* first check if a control mask is omitted */
  147. if(e->state & ControlMask) {
  148. switch (ksym) {
  149. default: /* ignore other control sequences */
  150. return;
  151. break;
  152. case XK_h:
  153. case XK_H:
  154. ksym = XK_BackSpace;
  155. break;
  156. case XK_u:
  157. case XK_U:
  158. text[0] = 0;
  159. match(text);
  160. drawmenu();
  161. return;
  162. break;
  163. }
  164. }
  165. switch(ksym) {
  166. case XK_Left:
  167. if(!(sel && sel->left))
  168. return;
  169. sel=sel->left;
  170. if(sel->right == curr) {
  171. curr = prev;
  172. calcoffsets();
  173. }
  174. break;
  175. case XK_Tab:
  176. if(!sel)
  177. return;
  178. strncpy(text, sel->text, sizeof(text));
  179. match(text);
  180. break;
  181. case XK_Right:
  182. if(!(sel && sel->right))
  183. return;
  184. sel=sel->right;
  185. if(sel == next) {
  186. curr = next;
  187. calcoffsets();
  188. }
  189. break;
  190. case XK_Return:
  191. if(e->state & ShiftMask) {
  192. if(text)
  193. fprintf(stdout, "%s", text);
  194. }
  195. else if(sel)
  196. fprintf(stdout, "%s", sel->text);
  197. else if(text)
  198. fprintf(stdout, "%s", text);
  199. fflush(stdout);
  200. running = False;
  201. break;
  202. case XK_Escape:
  203. ret = 1;
  204. running = False;
  205. break;
  206. case XK_BackSpace:
  207. if((i = len)) {
  208. prev_nitem = nitem;
  209. do {
  210. text[--i] = 0;
  211. match(text);
  212. } while(i && nitem && prev_nitem == nitem);
  213. match(text);
  214. }
  215. break;
  216. default:
  217. if(num && !iscntrl((int) buf[0])) {
  218. buf[num] = 0;
  219. if(len > 0)
  220. strncat(text, buf, sizeof(text));
  221. else
  222. strncpy(text, buf, sizeof(text));
  223. match(text);
  224. }
  225. }
  226. drawmenu();
  227. }
  228. static char *
  229. readstdin()
  230. {
  231. static char *maxname = NULL;
  232. char *p, buf[1024];
  233. unsigned int len = 0, max = 0;
  234. Item *i, *new;
  235. i = 0;
  236. while(fgets(buf, sizeof(buf), stdin)) {
  237. len = strlen(buf);
  238. if (buf[len - 1] == '\n')
  239. buf[len - 1] = 0;
  240. p = estrdup(buf);
  241. if(max < len) {
  242. maxname = p;
  243. max = len;
  244. }
  245. new = emalloc(sizeof(Item));
  246. new->next = new->left = new->right = NULL;
  247. new->text = p;
  248. if(!i)
  249. allitems = new;
  250. else
  251. i->next = new;
  252. i = new;
  253. }
  254. return maxname;
  255. }
  256. /* extern */
  257. int screen;
  258. Display *dpy;
  259. DC dc = {0};
  260. int
  261. main(int argc, char *argv[])
  262. {
  263. char *maxname;
  264. fd_set rd;
  265. struct timeval timeout;
  266. Item *i;
  267. XEvent ev;
  268. XSetWindowAttributes wa;
  269. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  270. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  271. exit(EXIT_SUCCESS);
  272. }
  273. else if(argc != 1)
  274. eprint("usage: dmenu [-v]\n");
  275. dpy = XOpenDisplay(0);
  276. if(!dpy)
  277. eprint("dmenu: cannot open display\n");
  278. screen = DefaultScreen(dpy);
  279. root = RootWindow(dpy, screen);
  280. /* Note, the select() construction allows to grab all keypresses as
  281. * early as possible, to not loose them. But if there is no standard
  282. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  283. * seconds. This is convenience behavior for rapid typers.
  284. */
  285. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  286. GrabModeAsync, CurrentTime) != GrabSuccess)
  287. usleep(1000);
  288. timeout.tv_usec = 0;
  289. timeout.tv_sec = STDIN_TIMEOUT;
  290. FD_ZERO(&rd);
  291. FD_SET(STDIN_FILENO, &rd);
  292. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  293. goto UninitializedEnd;
  294. maxname = readstdin();
  295. /* style */
  296. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  297. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  298. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  299. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  300. setfont(FONT);
  301. wa.override_redirect = 1;
  302. wa.background_pixmap = ParentRelative;
  303. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  304. mx = my = 0;
  305. mw = DisplayWidth(dpy, screen);
  306. mh = dc.font.height + 2;
  307. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  308. DefaultDepth(dpy, screen), CopyFromParent,
  309. DefaultVisual(dpy, screen),
  310. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  311. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  312. /* pixmap */
  313. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  314. dc.gc = XCreateGC(dpy, root, 0, 0);
  315. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  316. if(maxname)
  317. cmdw = textw(maxname);
  318. if(cmdw > mw / 3)
  319. cmdw = mw / 3;
  320. text[0] = 0;
  321. match(text);
  322. XMapRaised(dpy, win);
  323. drawmenu();
  324. XSync(dpy, False);
  325. /* main event loop */
  326. while(running && !XNextEvent(dpy, &ev)) {
  327. switch (ev.type) {
  328. default: /* ignore all crap */
  329. break;
  330. case KeyPress:
  331. kpress(&ev.xkey);
  332. break;
  333. case Expose:
  334. if(ev.xexpose.count == 0)
  335. drawmenu();
  336. break;
  337. }
  338. }
  339. while(allitems) {
  340. i = allitems->next;
  341. free(allitems->text);
  342. free(allitems);
  343. allitems = i;
  344. }
  345. if(dc.font.set)
  346. XFreeFontSet(dpy, dc.font.set);
  347. else
  348. XFreeFont(dpy, dc.font.xfont);
  349. XFreePixmap(dpy, dc.drawable);
  350. XFreeGC(dpy, dc.gc);
  351. XDestroyWindow(dpy, win);
  352. UninitializedEnd:
  353. XUngrabKeyboard(dpy, CurrentTime);
  354. XCloseDisplay(dpy);
  355. return ret;
  356. }