aewl

annotate gridmenu.c @ 2:a79188fe4a40

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