dwm-meillo

annotate menu.c @ 31:386649deb651

before leaning things up
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 01:04:38 +0200
parents e8f627998d6f
children
rev   line source
garbeam@1 1 /*
garbeam@1 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@1 3 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
garbeam@1 4 * See LICENSE file for license details.
garbeam@1 5 */
garbeam@1 6
garbeam@3 7 #include "config.h"
garbeam@3 8 #include "draw.h"
garbeam@3 9 #include "util.h"
garbeam@3 10
garbeam@1 11 #include <ctype.h>
garbeam@1 12 #include <stdlib.h>
garbeam@1 13 #include <stdio.h>
garbeam@1 14 #include <string.h>
garbeam@1 15 #include <unistd.h>
garbeam@1 16 #include <X11/cursorfont.h>
garbeam@1 17 #include <X11/Xutil.h>
garbeam@1 18 #include <X11/keysym.h>
garbeam@1 19
garbeam@1 20 typedef struct Item Item;
garbeam@1 21
garbeam@1 22 struct Item {
garbeam@1 23 Item *next; /* traverses all items */
garbeam@1 24 Item *left, *right; /* traverses items matching current search pattern */
garbeam@1 25 char *text;
garbeam@1 26 };
garbeam@1 27
garbeam@3 28 static Display *dpy;
garbeam@3 29 static Window root;
garbeam@3 30 static Window win;
garbeam@1 31 static Bool done = False;
garbeam@3 32
garbeam@7 33 static Item *allitem = NULL; /* first of all items */
garbeam@7 34 static Item *item = NULL; /* first of pattern matching items */
garbeam@7 35 static Item *sel = NULL;
garbeam@7 36 static Item *nextoff = NULL;
garbeam@7 37 static Item *prevoff = NULL;
garbeam@7 38 static Item *curroff = NULL;
garbeam@3 39
garbeam@26 40 static int screen, mx, my, mw, mh;
garbeam@7 41 static char *title = NULL;
garbeam@3 42 static char text[4096];
garbeam@1 43 static int ret = 0;
garbeam@1 44 static int nitem = 0;
garbeam@1 45 static unsigned int cmdw = 0;
garbeam@26 46 static unsigned int tw = 0;
garbeam@26 47 static unsigned int cw = 0;
garbeam@1 48 static const int seek = 30; /* 30px */
garbeam@1 49
garbeam@3 50 static Brush brush = {0};
garbeam@3 51
garbeam@5 52 static void draw_menu();
garbeam@3 53 static void kpress(XKeyEvent * e);
garbeam@1 54
garbeam@3 55 static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@1 56
garbeam@1 57 static void
garbeam@31 58 usage() { error("usage: gridmenu [-v] [-t <title>]\n"); }
garbeam@1 59
garbeam@1 60 static void
garbeam@1 61 update_offsets()
garbeam@1 62 {
garbeam@1 63 unsigned int tw, w = cmdw + 2 * seek;
garbeam@1 64
garbeam@1 65 if(!curroff)
garbeam@1 66 return;
garbeam@1 67
garbeam@1 68 for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
garbeam@26 69 tw = textw(&brush.font, nextoff->text);
garbeam@26 70 if(tw > mw / 3)
garbeam@26 71 tw = mw / 3;
garbeam@3 72 w += tw + brush.font.height;
garbeam@26 73 if(w > mw)
garbeam@1 74 break;
garbeam@1 75 }
garbeam@1 76
garbeam@1 77 w = cmdw + 2 * seek;
garbeam@1 78 for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
garbeam@26 79 tw = textw(&brush.font, prevoff->left->text);
garbeam@26 80 if(tw > mw / 3)
garbeam@26 81 tw = mw / 3;
garbeam@3 82 w += tw + brush.font.height;
garbeam@26 83 if(w > mw)
garbeam@1 84 break;
garbeam@1 85 }
garbeam@1 86 }
garbeam@1 87
garbeam@1 88 static void
garbeam@1 89 update_items(char *pattern)
garbeam@1 90 {
garbeam@1 91 unsigned int plen = strlen(pattern);
garbeam@1 92 Item *i, *j;
garbeam@1 93
garbeam@1 94 if(!pattern)
garbeam@1 95 return;
garbeam@1 96
garbeam@1 97 if(!title || *pattern)
garbeam@26 98 cmdw = cw;
garbeam@1 99 else
garbeam@26 100 cmdw = tw;
garbeam@1 101
garbeam@7 102 item = j = NULL;
garbeam@1 103 nitem = 0;
garbeam@1 104
garbeam@1 105 for(i = allitem; i; i=i->next)
garbeam@1 106 if(!plen || !strncmp(pattern, i->text, plen)) {
garbeam@1 107 if(!j)
garbeam@1 108 item = i;
garbeam@1 109 else
garbeam@1 110 j->right = i;
garbeam@1 111 i->left = j;
garbeam@7 112 i->right = NULL;
garbeam@1 113 j = i;
garbeam@1 114 nitem++;
garbeam@1 115 }
garbeam@1 116 for(i = allitem; i; i=i->next)
garbeam@1 117 if(plen && strncmp(pattern, i->text, plen)
garbeam@1 118 && strstr(i->text, pattern)) {
garbeam@1 119 if(!j)
garbeam@1 120 item = i;
garbeam@1 121 else
garbeam@1 122 j->right = i;
garbeam@1 123 i->left = j;
garbeam@7 124 i->right = NULL;
garbeam@1 125 j = i;
garbeam@1 126 nitem++;
garbeam@1 127 }
garbeam@1 128
garbeam@1 129 curroff = prevoff = nextoff = sel = item;
garbeam@1 130
garbeam@1 131 update_offsets();
garbeam@1 132 }
garbeam@1 133
garbeam@1 134 /* creates brush structs for brush mode drawing */
garbeam@1 135 static void
garbeam@1 136 draw_menu()
garbeam@1 137 {
garbeam@1 138 Item *i;
garbeam@1 139
garbeam@26 140 brush.x = 0;
garbeam@26 141 brush.y = 0;
garbeam@26 142 brush.w = mw;
garbeam@26 143 brush.h = mh;
garbeam@3 144 draw(dpy, &brush, False, 0);
garbeam@1 145
garbeam@1 146 /* print command */
garbeam@1 147 if(!title || text[0]) {
garbeam@26 148 cmdw = cw;
garbeam@1 149 if(cmdw && item)
garbeam@26 150 brush.w = cmdw;
garbeam@3 151 draw(dpy, &brush, False, text);
garbeam@1 152 }
garbeam@1 153 else {
garbeam@26 154 cmdw = tw;
garbeam@26 155 brush.w = cmdw;
garbeam@3 156 draw(dpy, &brush, False, title);
garbeam@1 157 }
garbeam@26 158 brush.x += brush.w;
garbeam@1 159
garbeam@1 160 if(curroff) {
garbeam@26 161 brush.w = seek;
garbeam@3 162 draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
garbeam@26 163 brush.x += brush.w;
garbeam@1 164
garbeam@1 165 /* determine maximum items */
garbeam@1 166 for(i = curroff; i != nextoff; i=i->right) {
garbeam@1 167 brush.border = False;
garbeam@26 168 brush.w = textw(&brush.font, i->text);
garbeam@26 169 if(brush.w > mw / 3)
garbeam@26 170 brush.w = mw / 3;
garbeam@26 171 brush.w += brush.font.height;
garbeam@1 172 if(sel == i) {
garbeam@3 173 swap((void **)&brush.fg, (void **)&brush.bg);
garbeam@3 174 draw(dpy, &brush, True, i->text);
garbeam@3 175 swap((void **)&brush.fg, (void **)&brush.bg);
garbeam@1 176 }
garbeam@3 177 else
garbeam@3 178 draw(dpy, &brush, False, i->text);
garbeam@26 179 brush.x += brush.w;
garbeam@1 180 }
garbeam@1 181
garbeam@26 182 brush.x = mw - seek;
garbeam@26 183 brush.w = seek;
garbeam@3 184 draw(dpy, &brush, False, nextoff ? ">" : 0);
garbeam@1 185 }
garbeam@26 186 XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
garbeam@3 187 XFlush(dpy);
garbeam@1 188 }
garbeam@1 189
garbeam@1 190 static void
garbeam@3 191 kpress(XKeyEvent * e)
garbeam@1 192 {
garbeam@1 193 KeySym ksym;
garbeam@1 194 char buf[32];
garbeam@1 195 int num, prev_nitem;
garbeam@1 196 unsigned int i, len = strlen(text);
garbeam@1 197
garbeam@1 198 buf[0] = 0;
garbeam@1 199 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
garbeam@1 200
garbeam@1 201 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
garbeam@1 202 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
garbeam@1 203 || IsPrivateKeypadKey(ksym))
garbeam@1 204 return;
garbeam@1 205
garbeam@1 206 /* first check if a control mask is omitted */
garbeam@1 207 if(e->state & ControlMask) {
garbeam@1 208 switch (ksym) {
garbeam@31 209 default: /* ignore other control sequences */
garbeam@31 210 return;
garbeam@31 211 break;
garbeam@1 212 case XK_h:
garbeam@1 213 ksym = XK_BackSpace;
garbeam@1 214 break;
garbeam@1 215 case XK_U:
garbeam@1 216 case XK_u:
garbeam@1 217 text[0] = 0;
garbeam@1 218 update_items(text);
garbeam@1 219 draw_menu();
garbeam@1 220 return;
garbeam@1 221 break;
garbeam@1 222 case XK_bracketleft:
garbeam@1 223 ksym = XK_Escape;
garbeam@1 224 break;
garbeam@1 225 }
garbeam@1 226 }
garbeam@31 227 switch(ksym) {
garbeam@1 228 case XK_Left:
garbeam@1 229 if(!(sel && sel->left))
garbeam@1 230 return;
garbeam@1 231 sel=sel->left;
garbeam@1 232 if(sel->right == curroff) {
garbeam@1 233 curroff = prevoff;
garbeam@1 234 update_offsets();
garbeam@1 235 }
garbeam@1 236 break;
garbeam@1 237 case XK_Tab:
garbeam@1 238 if(!sel)
garbeam@1 239 return;
garbeam@3 240 strncpy(text, sel->text, sizeof(text));
garbeam@1 241 update_items(text);
garbeam@1 242 break;
garbeam@1 243 case XK_Right:
garbeam@1 244 if(!(sel && sel->right))
garbeam@1 245 return;
garbeam@1 246 sel=sel->right;
garbeam@1 247 if(sel == nextoff) {
garbeam@1 248 curroff = nextoff;
garbeam@1 249 update_offsets();
garbeam@1 250 }
garbeam@1 251 break;
garbeam@1 252 case XK_Return:
garbeam@1 253 if(e->state & ShiftMask) {
garbeam@1 254 if(text)
garbeam@1 255 fprintf(stdout, "%s", text);
garbeam@1 256 }
garbeam@1 257 else if(sel)
garbeam@1 258 fprintf(stdout, "%s", sel->text);
garbeam@1 259 else if(text)
garbeam@1 260 fprintf(stdout, "%s", text);
garbeam@1 261 fflush(stdout);
garbeam@1 262 done = True;
garbeam@1 263 break;
garbeam@1 264 case XK_Escape:
garbeam@1 265 ret = 1;
garbeam@1 266 done = True;
garbeam@1 267 break;
garbeam@1 268 case XK_BackSpace:
garbeam@1 269 if((i = len)) {
garbeam@1 270 prev_nitem = nitem;
garbeam@1 271 do {
garbeam@1 272 text[--i] = 0;
garbeam@1 273 update_items(text);
garbeam@1 274 } while(i && nitem && prev_nitem == nitem);
garbeam@1 275 update_items(text);
garbeam@1 276 }
garbeam@1 277 break;
garbeam@1 278 default:
garbeam@13 279 if(num && !iscntrl((int) buf[0])) {
garbeam@1 280 buf[num] = 0;
garbeam@1 281 if(len > 0)
garbeam@3 282 strncat(text, buf, sizeof(text));
garbeam@1 283 else
garbeam@3 284 strncpy(text, buf, sizeof(text));
garbeam@1 285 update_items(text);
garbeam@1 286 }
garbeam@1 287 }
garbeam@1 288 draw_menu();
garbeam@1 289 }
garbeam@1 290
garbeam@1 291 static char *
garbeam@1 292 read_allitems()
garbeam@1 293 {
garbeam@7 294 static char *maxname = NULL;
garbeam@1 295 char *p, buf[1024];
garbeam@1 296 unsigned int len = 0, max = 0;
garbeam@1 297 Item *i, *new;
garbeam@1 298
garbeam@3 299 i = 0;
garbeam@1 300 while(fgets(buf, sizeof(buf), stdin)) {
garbeam@1 301 len = strlen(buf);
garbeam@1 302 if (buf[len - 1] == '\n')
garbeam@1 303 buf[len - 1] = 0;
garbeam@3 304 p = estrdup(buf);
garbeam@1 305 if(max < len) {
garbeam@1 306 maxname = p;
garbeam@1 307 max = len;
garbeam@1 308 }
garbeam@1 309
garbeam@3 310 new = emalloc(sizeof(Item));
garbeam@7 311 new->next = new->left = new->right = NULL;
garbeam@1 312 new->text = p;
garbeam@1 313 if(!i)
garbeam@1 314 allitem = new;
garbeam@1 315 else
garbeam@1 316 i->next = new;
garbeam@1 317 i = new;
garbeam@1 318 }
garbeam@1 319
garbeam@1 320 return maxname;
garbeam@1 321 }
garbeam@1 322
garbeam@1 323 int
garbeam@1 324 main(int argc, char *argv[])
garbeam@1 325 {
garbeam@1 326 int i;
garbeam@1 327 XSetWindowAttributes wa;
garbeam@3 328 char *maxname;
garbeam@1 329 XEvent ev;
garbeam@1 330
garbeam@1 331 /* command line args */
garbeam@1 332 for(i = 1; i < argc; i++) {
garbeam@1 333 if (argv[i][0] == '-')
garbeam@1 334 switch (argv[i][1]) {
garbeam@1 335 case 'v':
garbeam@1 336 fprintf(stdout, "%s", version);
garbeam@1 337 exit(0);
garbeam@1 338 break;
garbeam@1 339 case 't':
garbeam@1 340 if(++i < argc)
garbeam@1 341 title = argv[i];
garbeam@1 342 else
garbeam@1 343 usage();
garbeam@1 344 break;
garbeam@1 345 default:
garbeam@1 346 usage();
garbeam@1 347 break;
garbeam@1 348 }
garbeam@1 349 else
garbeam@1 350 usage();
garbeam@1 351 }
garbeam@1 352
garbeam@3 353 dpy = XOpenDisplay(0);
garbeam@3 354 if(!dpy)
garbeam@3 355 error("gridmenu: cannot open dpy\n");
garbeam@3 356 screen = DefaultScreen(dpy);
garbeam@3 357 root = RootWindow(dpy, screen);
garbeam@1 358
garbeam@1 359 maxname = read_allitems();
garbeam@1 360
garbeam@1 361 /* grab as early as possible, but after reading all items!!! */
garbeam@3 362 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
garbeam@1 363 GrabModeAsync, CurrentTime) != GrabSuccess)
garbeam@1 364 usleep(1000);
garbeam@1 365
garbeam@3 366 /* style */
garbeam@3 367 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
garbeam@3 368 loadfont(dpy, &brush.font, FONT);
garbeam@1 369
garbeam@1 370 wa.override_redirect = 1;
garbeam@1 371 wa.background_pixmap = ParentRelative;
garbeam@5 372 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
garbeam@1 373
garbeam@26 374 mx = my = 0;
garbeam@26 375 mw = DisplayWidth(dpy, screen);
garbeam@26 376 mh = texth(&brush.font);
garbeam@1 377
garbeam@26 378 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
garbeam@26 379 DefaultDepth(dpy, screen), CopyFromParent,
garbeam@26 380 DefaultVisual(dpy, screen),
garbeam@1 381 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
garbeam@3 382 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
garbeam@3 383 XFlush(dpy);
garbeam@1 384
garbeam@1 385 /* pixmap */
garbeam@5 386 brush.gc = XCreateGC(dpy, root, 0, 0);
garbeam@26 387 brush.drawable = XCreatePixmap(dpy, win, mw, mh,
garbeam@3 388 DefaultDepth(dpy, screen));
garbeam@3 389 XFlush(dpy);
garbeam@1 390
garbeam@1 391 if(maxname)
garbeam@26 392 cw = textw(&brush.font, maxname) + brush.font.height;
garbeam@26 393 if(cw > mw / 3)
garbeam@26 394 cw = mw / 3;
garbeam@1 395
garbeam@1 396 if(title) {
garbeam@26 397 tw = textw(&brush.font, title) + brush.font.height;
garbeam@26 398 if(tw > mw / 3)
garbeam@26 399 tw = mw / 3;
garbeam@1 400 }
garbeam@1 401
garbeam@26 402 cmdw = title ? tw : cw;
garbeam@1 403
garbeam@1 404 text[0] = 0;
garbeam@1 405 update_items(text);
garbeam@3 406 XMapRaised(dpy, win);
garbeam@1 407 draw_menu();
garbeam@3 408 XFlush(dpy);
garbeam@1 409
garbeam@1 410 /* main event loop */
garbeam@31 411 while(!done && !XNextEvent(dpy, &ev)) {
garbeam@1 412 switch (ev.type) {
garbeam@31 413 case KeyPress:
garbeam@31 414 kpress(&ev.xkey);
garbeam@31 415 break;
garbeam@31 416 case Expose:
garbeam@31 417 if(ev.xexpose.count == 0)
garbeam@31 418 draw_menu();
garbeam@31 419 break;
garbeam@31 420 default:
garbeam@31 421 break;
garbeam@1 422 }
garbeam@1 423 }
garbeam@1 424
garbeam@3 425 XUngrabKeyboard(dpy, CurrentTime);
garbeam@3 426 XFreePixmap(dpy, brush.drawable);
garbeam@3 427 XFreeGC(dpy, brush.gc);
garbeam@3 428 XDestroyWindow(dpy, win);
garbeam@3 429 XCloseDisplay(dpy);
garbeam@1 430
garbeam@1 431 return ret;
garbeam@1 432 }