removed useless newlines
5fd7af18
4 file(s) · +8 −37
| 1 | - | /* |
|
| 2 | - | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 1 | + | /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 3 | 2 | * See LICENSE file for license details. |
|
| 4 | 3 | */ |
|
| 5 | 4 |
| 1 | - | /* |
|
| 2 | - | * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 1 | + | /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 3 | 2 | * See LICENSE file for license details. |
|
| 4 | 3 | */ |
|
| 5 | 4 | #include "dmenu.h" |
|
| 32 | 31 | ||
| 33 | 32 | XSetForeground(dpy, dc.gc, col[ColBG]); |
|
| 34 | 33 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 35 | - | ||
| 36 | 34 | if(!text) |
|
| 37 | 35 | return; |
|
| 38 | - | ||
| 39 | 36 | w = 0; |
|
| 40 | 37 | olen = len = strlen(text); |
|
| 41 | 38 | if(len >= sizeof(buf)) |
|
| 42 | 39 | len = sizeof(buf) - 1; |
|
| 43 | 40 | memcpy(buf, text, len); |
|
| 44 | 41 | buf[len] = 0; |
|
| 45 | - | ||
| 46 | 42 | h = dc.font.ascent + dc.font.descent; |
|
| 47 | 43 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
|
| 48 | 44 | x = dc.x + (h / 2); |
|
| 49 | - | ||
| 50 | 45 | /* shorten text if necessary */ |
|
| 51 | 46 | while(len && (w = textnw(buf, len)) > dc.w - h) |
|
| 52 | 47 | buf[--len] = 0; |
|
| 58 | 53 | if(len > 3) |
|
| 59 | 54 | buf[len - 3] = '.'; |
|
| 60 | 55 | } |
|
| 61 | - | ||
| 62 | 56 | if(w > dc.w) |
|
| 63 | 57 | return; /* too long */ |
|
| 64 | - | ||
| 65 | 58 | gcv.foreground = col[ColFG]; |
|
| 66 | 59 | if(dc.font.set) { |
|
| 67 | 60 | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 106 | 99 | XFontSetExtents *font_extents; |
|
| 107 | 100 | XFontStruct **xfonts; |
|
| 108 | 101 | char **font_names; |
|
| 109 | - | ||
| 110 | 102 | dc.font.ascent = dc.font.descent = 0; |
|
| 111 | 103 | font_extents = XExtentsOfFontSet(dc.font.set); |
|
| 112 | 104 | n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); |
|
| 1 | - | /* |
|
| 2 | - | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 1 | + | /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 3 | 2 | * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com> |
|
| 4 | 3 | * See LICENSE file for license details. |
|
| 5 | 4 | */ |
|
| 47 | 46 | ||
| 48 | 47 | if(!curr) |
|
| 49 | 48 | return; |
|
| 50 | - | ||
| 51 | 49 | w = cmdw + 2 * SPACE; |
|
| 52 | 50 | for(next = curr; next; next=next->right) { |
|
| 53 | 51 | tw = textw(next->text); |
|
| 57 | 55 | if(w > mw) |
|
| 58 | 56 | break; |
|
| 59 | 57 | } |
|
| 60 | - | ||
| 61 | 58 | w = cmdw + 2 * SPACE; |
|
| 62 | 59 | for(prev = curr; prev && prev->left; prev=prev->left) { |
|
| 63 | 60 | tw = textw(prev->left->text); |
|
| 78 | 75 | dc.w = mw; |
|
| 79 | 76 | dc.h = mh; |
|
| 80 | 77 | drawtext(NULL, dc.norm); |
|
| 81 | - | ||
| 82 | 78 | /* print command */ |
|
| 83 | 79 | if(cmdw && item) |
|
| 84 | 80 | dc.w = cmdw; |
|
| 85 | 81 | drawtext(text[0] ? text : NULL, dc.norm); |
|
| 86 | 82 | dc.x += cmdw; |
|
| 87 | - | ||
| 88 | 83 | if(curr) { |
|
| 89 | 84 | dc.w = SPACE; |
|
| 90 | 85 | drawtext((curr && curr->left) ? "<" : NULL, dc.norm); |
|
| 91 | 86 | dc.x += dc.w; |
|
| 92 | - | ||
| 93 | 87 | /* determine maximum items */ |
|
| 94 | 88 | for(i = curr; i != next; i=i->right) { |
|
| 95 | 89 | dc.w = textw(i->text); |
|
| 98 | 92 | drawtext(i->text, (sel == i) ? dc.sel : dc.norm); |
|
| 99 | 93 | dc.x += dc.w; |
|
| 100 | 94 | } |
|
| 101 | - | ||
| 102 | 95 | dc.x = mw - SPACE; |
|
| 103 | 96 | dc.w = SPACE; |
|
| 104 | 97 | drawtext(next ? ">" : NULL, dc.norm); |
|
| 114 | 107 | ||
| 115 | 108 | if(!pattern) |
|
| 116 | 109 | return; |
|
| 117 | - | ||
| 118 | 110 | plen = strlen(pattern); |
|
| 119 | 111 | item = j = NULL; |
|
| 120 | 112 | nitem = 0; |
|
| 121 | - | ||
| 122 | 113 | for(i = allitems; i; i=i->next) |
|
| 123 | 114 | if(!plen || !strncmp(pattern, i->text, plen)) { |
|
| 124 | 115 | if(!j) |
|
| 142 | 133 | j = i; |
|
| 143 | 134 | nitem++; |
|
| 144 | 135 | } |
|
| 145 | - | ||
| 146 | 136 | curr = prev = next = sel = item; |
|
| 147 | 137 | calcoffsets(); |
|
| 148 | 138 | } |
|
| 157 | 147 | len = strlen(text); |
|
| 158 | 148 | buf[0] = 0; |
|
| 159 | 149 | num = XLookupString(e, buf, sizeof(buf), &ksym, 0); |
|
| 160 | - | ||
| 161 | 150 | if(IsFunctionKey(ksym) || IsKeypadKey(ksym) |
|
| 162 | 151 | || IsMiscFunctionKey(ksym) || IsPFKey(ksym) |
|
| 163 | 152 | || IsPrivateKeypadKey(ksym)) |
|
| 164 | 153 | return; |
|
| 165 | - | ||
| 166 | 154 | /* first check if a control mask is omitted */ |
|
| 167 | 155 | if(e->state & ControlMask) { |
|
| 168 | 156 | switch (ksym) { |
|
| 261 | 249 | maxname = p; |
|
| 262 | 250 | max = len; |
|
| 263 | 251 | } |
|
| 264 | - | ||
| 265 | 252 | new = emalloc(sizeof(Item)); |
|
| 266 | 253 | new->next = new->left = new->right = NULL; |
|
| 267 | 254 | new->text = p; |
|
| 318 | 305 | } |
|
| 319 | 306 | else |
|
| 320 | 307 | eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout); |
|
| 321 | - | ||
| 322 | 308 | dpy = XOpenDisplay(0); |
|
| 323 | 309 | if(!dpy) |
|
| 324 | 310 | eprint("dmenu: cannot open display\n"); |
|
| 333 | 319 | while(XGrabKeyboard(dpy, root, True, GrabModeAsync, |
|
| 334 | 320 | GrabModeAsync, CurrentTime) != GrabSuccess) |
|
| 335 | 321 | usleep(1000); |
|
| 336 | - | ||
| 337 | 322 | FD_ZERO(&rd); |
|
| 338 | 323 | FD_SET(STDIN_FILENO, &rd); |
|
| 339 | 324 | if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1) |
|
| 340 | 325 | goto UninitializedEnd; |
|
| 341 | 326 | maxname = readstdin(); |
|
| 342 | - | ||
| 343 | 327 | /* style */ |
|
| 344 | 328 | dc.norm[ColBG] = getcolor(normbg); |
|
| 345 | 329 | dc.norm[ColFG] = getcolor(normfg); |
|
| 346 | 330 | dc.sel[ColBG] = getcolor(selbg); |
|
| 347 | 331 | dc.sel[ColFG] = getcolor(selfg); |
|
| 348 | 332 | setfont(font); |
|
| 349 | - | ||
| 333 | + | /* menu window */ |
|
| 350 | 334 | wa.override_redirect = 1; |
|
| 351 | 335 | wa.background_pixmap = ParentRelative; |
|
| 352 | 336 | wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask; |
|
| 353 | - | ||
| 354 | 337 | mx = my = 0; |
|
| 355 | 338 | mw = DisplayWidth(dpy, screen); |
|
| 356 | 339 | mh = dc.font.height + 2; |
|
| 357 | - | ||
| 358 | 340 | win = XCreateWindow(dpy, root, mx, my, mw, mh, 0, |
|
| 359 | 341 | DefaultDepth(dpy, screen), CopyFromParent, |
|
| 360 | 342 | DefaultVisual(dpy, screen), |
|
| 361 | 343 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
|
| 362 | 344 | XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); |
|
| 363 | - | ||
| 364 | 345 | /* pixmap */ |
|
| 365 | 346 | dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen)); |
|
| 366 | 347 | dc.gc = XCreateGC(dpy, root, 0, 0); |
|
| 367 | 348 | XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); |
|
| 368 | - | ||
| 369 | 349 | if(maxname) |
|
| 370 | 350 | cmdw = textw(maxname); |
|
| 371 | 351 | if(cmdw > mw / 3) |
|
| 372 | 352 | cmdw = mw / 3; |
|
| 373 | - | ||
| 374 | 353 | text[0] = 0; |
|
| 375 | 354 | match(text); |
|
| 376 | 355 | XMapRaised(dpy, win); |
|
| 392 | 371 | } |
|
| 393 | 372 | } |
|
| 394 | 373 | ||
| 374 | + | /* cleanup */ |
|
| 395 | 375 | while(allitems) { |
|
| 396 | 376 | itm = allitems->next; |
|
| 397 | 377 | free(allitems->text); |
|
| 408 | 388 | UninitializedEnd: |
|
| 409 | 389 | XUngrabKeyboard(dpy, CurrentTime); |
|
| 410 | 390 | XCloseDisplay(dpy); |
|
| 411 | - | ||
| 412 | 391 | return ret; |
|
| 413 | 392 | } |
|
| 1 | - | /* |
|
| 2 | - | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 1 | + | /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 3 | 2 | * See LICENSE file for license details. |
|
| 4 | 3 | */ |
|
| 5 | 4 | #include "dmenu.h" |
|
| 22 | 21 | void * |
|
| 23 | 22 | emalloc(unsigned int size) { |
|
| 24 | 23 | void *res = malloc(size); |
|
| 24 | + | ||
| 25 | 25 | if(!res) |
|
| 26 | 26 | badmalloc(size); |
|
| 27 | 27 | return res; |
|
| 40 | 40 | char * |
|
| 41 | 41 | estrdup(const char *str) { |
|
| 42 | 42 | void *res = strdup(str); |
|
| 43 | + | ||
| 43 | 44 | if(!res) |
|
| 44 | 45 | badmalloc(strlen(str)); |
|
| 45 | 46 | return res; |
|