aewl

annotate menu.c @ 30:2e0fb4130bfb

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