dwm-meillo

annotate menu.c @ 25:e238dc4844d7

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