tabbed.c.orig (29696B)
1 /* 2 * See LICENSE file for copyright and license details. 3 */ 4 5 #include <sys/wait.h> 6 #include <locale.h> 7 #include <signal.h> 8 #include <stdarg.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <unistd.h> 13 #include <X11/Xatom.h> 14 #include <X11/Xlib.h> 15 #include <X11/Xproto.h> 16 #include <X11/Xutil.h> 17 #include <X11/XKBlib.h> 18 #include <X11/Xft/Xft.h> 19 20 #include "arg.h" 21 22 /* XEMBED messages */ 23 #define XEMBED_EMBEDDED_NOTIFY 0 24 #define XEMBED_WINDOW_ACTIVATE 1 25 #define XEMBED_WINDOW_DEACTIVATE 2 26 #define XEMBED_REQUEST_FOCUS 3 27 #define XEMBED_FOCUS_IN 4 28 #define XEMBED_FOCUS_OUT 5 29 #define XEMBED_FOCUS_NEXT 6 30 #define XEMBED_FOCUS_PREV 7 31 /* 8-9 were used for XEMBED_GRAB_KEY/XEMBED_UNGRAB_KEY */ 32 #define XEMBED_MODALITY_ON 10 33 #define XEMBED_MODALITY_OFF 11 34 #define XEMBED_REGISTER_ACCELERATOR 12 35 #define XEMBED_UNREGISTER_ACCELERATOR 13 36 #define XEMBED_ACTIVATE_ACCELERATOR 14 37 38 /* Details for XEMBED_FOCUS_IN: */ 39 #define XEMBED_FOCUS_CURRENT 0 40 #define XEMBED_FOCUS_FIRST 1 41 #define XEMBED_FOCUS_LAST 2 42 43 /* Macros */ 44 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 45 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 46 #define LENGTH(x) (sizeof((x)) / sizeof(*(x))) 47 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) 48 #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height) 49 50 enum { ColFG, ColBG, ColLast }; /* color */ 51 enum { WMProtocols, WMDelete, WMName, WMState, WMFullscreen, 52 XEmbed, WMSelectTab, WMLast }; /* default atoms */ 53 54 typedef union { 55 int i; 56 const void *v; 57 } Arg; 58 59 typedef struct { 60 unsigned int mod; 61 KeySym keysym; 62 void (*func)(const Arg *); 63 const Arg arg; 64 } Key; 65 66 typedef struct { 67 int x, y, w, h; 68 XftColor norm[ColLast]; 69 XftColor sel[ColLast]; 70 XftColor urg[ColLast]; 71 Drawable drawable; 72 GC gc; 73 struct { 74 int ascent; 75 int descent; 76 int height; 77 XftFont *xfont; 78 } font; 79 } DC; /* draw context */ 80 81 typedef struct { 82 char name[256]; 83 Window win; 84 int tabx; 85 Bool urgent; 86 Bool closed; 87 } Client; 88 89 /* function declarations */ 90 static void buttonpress(const XEvent *e); 91 static void cleanup(void); 92 static void clientmessage(const XEvent *e); 93 static void configurenotify(const XEvent *e); 94 static void configurerequest(const XEvent *e); 95 static void createnotify(const XEvent *e); 96 static void destroynotify(const XEvent *e); 97 static void die(const char *errstr, ...); 98 static void drawbar(void); 99 static void drawtext(const char *text, XftColor col[ColLast]); 100 static void *ecalloc(size_t n, size_t size); 101 static void *erealloc(void *o, size_t size); 102 static void expose(const XEvent *e); 103 static void focus(int c); 104 static void focusin(const XEvent *e); 105 static void focusonce(const Arg *arg); 106 static void focusurgent(const Arg *arg); 107 static void fullscreen(const Arg *arg); 108 static char *getatom(int a); 109 static int getclient(Window w); 110 static XftColor getcolor(const char *colstr); 111 static int getfirsttab(void); 112 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); 113 static void initfont(const char *fontstr); 114 static Bool isprotodel(int c); 115 static void keypress(const XEvent *e); 116 static void killclient(const Arg *arg); 117 static void manage(Window win); 118 static void maprequest(const XEvent *e); 119 static void move(const Arg *arg); 120 static void movetab(const Arg *arg); 121 static void propertynotify(const XEvent *e); 122 static void resize(int c, int w, int h); 123 static void rotate(const Arg *arg); 124 static void run(void); 125 static void sendxembed(int c, long msg, long detail, long d1, long d2); 126 static void setcmd(int argc, char *argv[], int); 127 static void setup(void); 128 static void sigchld(int unused); 129 static void spawn(const Arg *arg); 130 static int textnw(const char *text, unsigned int len); 131 static void toggle(const Arg *arg); 132 static void unmanage(int c); 133 static void unmapnotify(const XEvent *e); 134 static void updatenumlockmask(void); 135 static void updatetitle(int c); 136 static int xerror(Display *dpy, XErrorEvent *ee); 137 static void xsettitle(Window w, const char *str); 138 139 /* variables */ 140 static int screen; 141 static void (*handler[LASTEvent]) (const XEvent *) = { 142 [ButtonPress] = buttonpress, 143 [ClientMessage] = clientmessage, 144 [ConfigureNotify] = configurenotify, 145 [ConfigureRequest] = configurerequest, 146 [CreateNotify] = createnotify, 147 [UnmapNotify] = unmapnotify, 148 [DestroyNotify] = destroynotify, 149 [Expose] = expose, 150 [FocusIn] = focusin, 151 [KeyPress] = keypress, 152 [MapRequest] = maprequest, 153 [PropertyNotify] = propertynotify, 154 }; 155 static int bh, obh, wx, wy, ww, wh; 156 static unsigned int numlockmask; 157 static Bool running = True, nextfocus, doinitspawn = True, 158 fillagain = False, closelastclient = False, 159 killclientsfirst = False; 160 static Display *dpy; 161 static DC dc; 162 static Atom wmatom[WMLast]; 163 static Window root, win; 164 static Client **clients; 165 static int nclients, sel = -1, lastsel = -1; 166 static int (*xerrorxlib)(Display *, XErrorEvent *); 167 static int cmd_append_pos; 168 static char winid[64]; 169 static char **cmd; 170 static char *wmname = "tabbed"; 171 static const char *geometry; 172 173 char *argv0; 174 175 /* configuration, allows nested code to access above variables */ 176 #include "config.h" 177 178 void 179 buttonpress(const XEvent *e) 180 { 181 const XButtonPressedEvent *ev = &e->xbutton; 182 int i, fc; 183 Arg arg; 184 185 if (ev->y < 0 || ev->y > bh) 186 return; 187 188 if (((fc = getfirsttab()) > 0 && ev->x < TEXTW(before)) || ev->x < 0) 189 return; 190 191 for (i = fc; i < nclients; i++) { 192 if (clients[i]->tabx > ev->x) { 193 switch (ev->button) { 194 case Button1: 195 focus(i); 196 break; 197 case Button2: 198 focus(i); 199 killclient(NULL); 200 break; 201 case Button4: /* FALLTHROUGH */ 202 case Button5: 203 arg.i = ev->button == Button4 ? -1 : 1; 204 rotate(&arg); 205 break; 206 } 207 break; 208 } 209 } 210 } 211 212 void 213 cleanup(void) 214 { 215 int i; 216 217 for (i = 0; i < nclients; i++) { 218 focus(i); 219 killclient(NULL); 220 XReparentWindow(dpy, clients[i]->win, root, 0, 0); 221 unmanage(i); 222 } 223 free(clients); 224 clients = NULL; 225 226 XFreePixmap(dpy, dc.drawable); 227 XFreeGC(dpy, dc.gc); 228 XDestroyWindow(dpy, win); 229 XSync(dpy, False); 230 free(cmd); 231 } 232 233 void 234 clientmessage(const XEvent *e) 235 { 236 const XClientMessageEvent *ev = &e->xclient; 237 238 if (ev->message_type == wmatom[WMProtocols] && 239 ev->data.l[0] == wmatom[WMDelete]) { 240 if (nclients > 1 && killclientsfirst) { 241 killclient(0); 242 return; 243 } 244 running = False; 245 } 246 } 247 248 void 249 configurenotify(const XEvent *e) 250 { 251 const XConfigureEvent *ev = &e->xconfigure; 252 253 if (ev->window == win && (ev->width != ww || ev->height != wh)) { 254 ww = ev->width; 255 wh = ev->height; 256 XFreePixmap(dpy, dc.drawable); 257 dc.drawable = XCreatePixmap(dpy, root, ww, wh, 258 DefaultDepth(dpy, screen)); 259 260 if (!obh && (wh <= bh)) { 261 obh = bh; 262 bh = 0; 263 } else if (!bh && (wh > obh)) { 264 bh = obh; 265 obh = 0; 266 } 267 268 if (sel > -1) 269 resize(sel, ww, wh - bh); 270 XSync(dpy, False); 271 } 272 } 273 274 void 275 configurerequest(const XEvent *e) 276 { 277 const XConfigureRequestEvent *ev = &e->xconfigurerequest; 278 XWindowChanges wc; 279 int c; 280 281 if ((c = getclient(ev->window)) > -1) { 282 wc.x = 0; 283 wc.y = bh; 284 wc.width = ww; 285 wc.height = wh - bh; 286 wc.border_width = 0; 287 wc.sibling = ev->above; 288 wc.stack_mode = ev->detail; 289 XConfigureWindow(dpy, clients[c]->win, ev->value_mask, &wc); 290 } 291 } 292 293 void 294 createnotify(const XEvent *e) 295 { 296 const XCreateWindowEvent *ev = &e->xcreatewindow; 297 298 if (ev->window != win && getclient(ev->window) < 0) 299 manage(ev->window); 300 } 301 302 void 303 destroynotify(const XEvent *e) 304 { 305 const XDestroyWindowEvent *ev = &e->xdestroywindow; 306 int c; 307 308 if ((c = getclient(ev->window)) > -1) 309 unmanage(c); 310 } 311 312 void 313 die(const char *errstr, ...) 314 { 315 va_list ap; 316 317 va_start(ap, errstr); 318 vfprintf(stderr, errstr, ap); 319 va_end(ap); 320 exit(EXIT_FAILURE); 321 } 322 323 void 324 drawbar(void) 325 { 326 XftColor *col; 327 int c, cc, fc, width; 328 char *name = NULL; 329 330 if (nclients == 0) { 331 dc.x = 0; 332 dc.w = ww; 333 XFetchName(dpy, win, &name); 334 drawtext(name ? name : "", dc.norm); 335 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0); 336 XSync(dpy, False); 337 338 return; 339 } 340 341 width = ww; 342 cc = ww / tabwidth; 343 if (nclients > cc) 344 cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth; 345 346 if ((fc = getfirsttab()) + cc < nclients) { 347 dc.w = TEXTW(after); 348 dc.x = width - dc.w; 349 drawtext(after, dc.sel); 350 width -= dc.w; 351 } 352 dc.x = 0; 353 354 if (fc > 0) { 355 dc.w = TEXTW(before); 356 drawtext(before, dc.sel); 357 dc.x += dc.w; 358 width -= dc.w; 359 } 360 361 cc = MIN(cc, nclients); 362 for (c = fc; c < fc + cc; c++) { 363 dc.w = width / cc; 364 if (c == sel) { 365 col = dc.sel; 366 dc.w += width % cc; 367 } else { 368 col = clients[c]->urgent ? dc.urg : dc.norm; 369 } 370 drawtext(clients[c]->name, col); 371 dc.x += dc.w; 372 clients[c]->tabx = dc.x; 373 } 374 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0); 375 XSync(dpy, False); 376 } 377 378 void 379 drawtext(const char *text, XftColor col[ColLast]) 380 { 381 int i, j, x, y, h, len, olen; 382 char buf[256]; 383 XftDraw *d; 384 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; 385 386 XSetForeground(dpy, dc.gc, col[ColBG].pixel); 387 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); 388 if (!text) 389 return; 390 391 olen = strlen(text); 392 h = dc.font.ascent + dc.font.descent; 393 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; 394 x = dc.x + (h / 2); 395 396 /* shorten text if necessary */ 397 for (len = MIN(olen, sizeof(buf)); 398 len && textnw(text, len) > dc.w - h; len--); 399 400 if (!len) 401 return; 402 403 memcpy(buf, text, len); 404 if (len < olen) { 405 for (i = len, j = strlen(titletrim); j && i; 406 buf[--i] = titletrim[--j]) 407 ; 408 } 409 410 d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen)); 411 XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len); 412 XftDrawDestroy(d); 413 } 414 415 void * 416 ecalloc(size_t n, size_t size) 417 { 418 void *p; 419 420 if (!(p = calloc(n, size))) 421 die("%s: cannot calloc\n", argv0); 422 return p; 423 } 424 425 void * 426 erealloc(void *o, size_t size) 427 { 428 void *p; 429 430 if (!(p = realloc(o, size))) 431 die("%s: cannot realloc\n", argv0); 432 return p; 433 } 434 435 void 436 expose(const XEvent *e) 437 { 438 const XExposeEvent *ev = &e->xexpose; 439 440 if (ev->count == 0 && win == ev->window) 441 drawbar(); 442 } 443 444 void 445 focus(int c) 446 { 447 char buf[BUFSIZ] = "tabbed-"VERSION" ::"; 448 size_t i, n; 449 XWMHints* wmh; 450 451 /* If c, sel and clients are -1, raise tabbed-win itself */ 452 if (nclients == 0) { 453 cmd[cmd_append_pos] = NULL; 454 for(i = 0, n = strlen(buf); cmd[i] && n < sizeof(buf); i++) 455 n += snprintf(&buf[n], sizeof(buf) - n, " %s", cmd[i]); 456 457 xsettitle(win, buf); 458 XRaiseWindow(dpy, win); 459 460 return; 461 } 462 463 if (c < 0 || c >= nclients) 464 return; 465 466 resize(c, ww, wh - bh); 467 XRaiseWindow(dpy, clients[c]->win); 468 XSetInputFocus(dpy, clients[c]->win, RevertToParent, CurrentTime); 469 sendxembed(c, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0); 470 sendxembed(c, XEMBED_WINDOW_ACTIVATE, 0, 0, 0); 471 xsettitle(win, clients[c]->name); 472 473 if (sel != c) { 474 lastsel = sel; 475 sel = c; 476 } 477 478 if (clients[c]->urgent && (wmh = XGetWMHints(dpy, clients[c]->win))) { 479 wmh->flags &= ~XUrgencyHint; 480 XSetWMHints(dpy, clients[c]->win, wmh); 481 clients[c]->urgent = False; 482 XFree(wmh); 483 } 484 485 drawbar(); 486 XSync(dpy, False); 487 } 488 489 void 490 focusin(const XEvent *e) 491 { 492 const XFocusChangeEvent *ev = &e->xfocus; 493 int dummy; 494 Window focused; 495 496 if (ev->mode != NotifyUngrab) { 497 XGetInputFocus(dpy, &focused, &dummy); 498 if (focused == win) 499 focus(sel); 500 } 501 } 502 503 void 504 focusonce(const Arg *arg) 505 { 506 nextfocus = True; 507 } 508 509 void 510 focusurgent(const Arg *arg) 511 { 512 int c; 513 514 if (sel < 0) 515 return; 516 517 for (c = (sel + 1) % nclients; c != sel; c = (c + 1) % nclients) { 518 if (clients[c]->urgent) { 519 focus(c); 520 return; 521 } 522 } 523 } 524 525 void 526 fullscreen(const Arg *arg) 527 { 528 XEvent e; 529 530 e.type = ClientMessage; 531 e.xclient.window = win; 532 e.xclient.message_type = wmatom[WMState]; 533 e.xclient.format = 32; 534 e.xclient.data.l[0] = 2; 535 e.xclient.data.l[1] = wmatom[WMFullscreen]; 536 e.xclient.data.l[2] = 0; 537 XSendEvent(dpy, root, False, SubstructureNotifyMask, &e); 538 } 539 540 char * 541 getatom(int a) 542 { 543 static char buf[BUFSIZ]; 544 Atom adummy; 545 int idummy; 546 unsigned long ldummy; 547 unsigned char *p = NULL; 548 549 XGetWindowProperty(dpy, win, wmatom[a], 0L, BUFSIZ, False, XA_STRING, 550 &adummy, &idummy, &ldummy, &ldummy, &p); 551 if (p) 552 strncpy(buf, (char *)p, LENGTH(buf)-1); 553 else 554 buf[0] = '\0'; 555 XFree(p); 556 557 return buf; 558 } 559 560 int 561 getclient(Window w) 562 { 563 int i; 564 565 for (i = 0; i < nclients; i++) { 566 if (clients[i]->win == w) 567 return i; 568 } 569 570 return -1; 571 } 572 573 XftColor 574 getcolor(const char *colstr) 575 { 576 XftColor color; 577 578 if (!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color)) 579 die("%s: cannot allocate color '%s'\n", argv0, colstr); 580 581 return color; 582 } 583 584 int 585 getfirsttab(void) 586 { 587 int cc, ret; 588 589 if (sel < 0) 590 return 0; 591 592 cc = ww / tabwidth; 593 if (nclients > cc) 594 cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth; 595 596 ret = sel - cc / 2 + (cc + 1) % 2; 597 return ret < 0 ? 0 : 598 ret + cc > nclients ? MAX(0, nclients - cc) : 599 ret; 600 } 601 602 Bool 603 gettextprop(Window w, Atom atom, char *text, unsigned int size) 604 { 605 char **list = NULL; 606 int n; 607 XTextProperty name; 608 609 if (!text || size == 0) 610 return False; 611 612 text[0] = '\0'; 613 XGetTextProperty(dpy, w, &name, atom); 614 if (!name.nitems) 615 return False; 616 617 if (name.encoding == XA_STRING) { 618 strncpy(text, (char *)name.value, size - 1); 619 } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success 620 && n > 0 && *list) { 621 strncpy(text, *list, size - 1); 622 XFreeStringList(list); 623 } 624 text[size - 1] = '\0'; 625 XFree(name.value); 626 627 return True; 628 } 629 630 void 631 initfont(const char *fontstr) 632 { 633 if (!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr)) 634 && !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed"))) 635 die("error, cannot load font: '%s'\n", fontstr); 636 637 dc.font.ascent = dc.font.xfont->ascent; 638 dc.font.descent = dc.font.xfont->descent; 639 dc.font.height = dc.font.ascent + dc.font.descent; 640 } 641 642 Bool 643 isprotodel(int c) 644 { 645 int i, n; 646 Atom *protocols; 647 Bool ret = False; 648 649 if (XGetWMProtocols(dpy, clients[c]->win, &protocols, &n)) { 650 for (i = 0; !ret && i < n; i++) { 651 if (protocols[i] == wmatom[WMDelete]) 652 ret = True; 653 } 654 XFree(protocols); 655 } 656 657 return ret; 658 } 659 660 void 661 keypress(const XEvent *e) 662 { 663 const XKeyEvent *ev = &e->xkey; 664 unsigned int i; 665 KeySym keysym; 666 667 keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0); 668 for (i = 0; i < LENGTH(keys); i++) { 669 if (keysym == keys[i].keysym && 670 CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) && 671 keys[i].func) 672 keys[i].func(&(keys[i].arg)); 673 } 674 } 675 676 void 677 killclient(const Arg *arg) 678 { 679 XEvent ev; 680 681 if (sel < 0) 682 return; 683 684 if (isprotodel(sel) && !clients[sel]->closed) { 685 ev.type = ClientMessage; 686 ev.xclient.window = clients[sel]->win; 687 ev.xclient.message_type = wmatom[WMProtocols]; 688 ev.xclient.format = 32; 689 ev.xclient.data.l[0] = wmatom[WMDelete]; 690 ev.xclient.data.l[1] = CurrentTime; 691 XSendEvent(dpy, clients[sel]->win, False, NoEventMask, &ev); 692 clients[sel]->closed = True; 693 } else { 694 XKillClient(dpy, clients[sel]->win); 695 } 696 } 697 698 void 699 manage(Window w) 700 { 701 updatenumlockmask(); 702 { 703 int i, j, nextpos; 704 unsigned int modifiers[] = { 0, LockMask, numlockmask, 705 numlockmask | LockMask }; 706 KeyCode code; 707 Client *c; 708 XEvent e; 709 710 XWithdrawWindow(dpy, w, 0); 711 XReparentWindow(dpy, w, win, 0, bh); 712 XSelectInput(dpy, w, PropertyChangeMask | 713 StructureNotifyMask | EnterWindowMask); 714 XSync(dpy, False); 715 716 for (i = 0; i < LENGTH(keys); i++) { 717 if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) { 718 for (j = 0; j < LENGTH(modifiers); j++) { 719 XGrabKey(dpy, code, keys[i].mod | 720 modifiers[j], w, True, 721 GrabModeAsync, GrabModeAsync); 722 } 723 } 724 } 725 726 c = ecalloc(1, sizeof *c); 727 c->win = w; 728 729 nclients++; 730 clients = erealloc(clients, sizeof(Client *) * nclients); 731 732 if(npisrelative) { 733 nextpos = sel + newposition; 734 } else { 735 if (newposition < 0) 736 nextpos = nclients - newposition; 737 else 738 nextpos = newposition; 739 } 740 if (nextpos >= nclients) 741 nextpos = nclients - 1; 742 if (nextpos < 0) 743 nextpos = 0; 744 745 if (nclients > 1 && nextpos < nclients - 1) 746 memmove(&clients[nextpos + 1], &clients[nextpos], 747 sizeof(Client *) * (nclients - nextpos - 1)); 748 749 clients[nextpos] = c; 750 updatetitle(nextpos); 751 752 XLowerWindow(dpy, w); 753 XMapWindow(dpy, w); 754 755 e.xclient.window = w; 756 e.xclient.type = ClientMessage; 757 e.xclient.message_type = wmatom[XEmbed]; 758 e.xclient.format = 32; 759 e.xclient.data.l[0] = CurrentTime; 760 e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY; 761 e.xclient.data.l[2] = 0; 762 e.xclient.data.l[3] = win; 763 e.xclient.data.l[4] = 0; 764 XSendEvent(dpy, root, False, NoEventMask, &e); 765 766 XSync(dpy, False); 767 768 /* Adjust sel before focus does set it to lastsel. */ 769 if (sel >= nextpos) 770 sel++; 771 focus(nextfocus ? nextpos : 772 sel < 0 ? 0 : 773 sel); 774 nextfocus = foreground; 775 } 776 } 777 778 void 779 maprequest(const XEvent *e) 780 { 781 const XMapRequestEvent *ev = &e->xmaprequest; 782 783 if (getclient(ev->window) < 0) 784 manage(ev->window); 785 } 786 787 void 788 move(const Arg *arg) 789 { 790 if (arg->i >= 0 && arg->i < nclients) 791 focus(arg->i); 792 } 793 794 void 795 movetab(const Arg *arg) 796 { 797 int c; 798 Client *new; 799 800 if (sel < 0) 801 return; 802 803 c = (sel + arg->i) % nclients; 804 if (c < 0) 805 c += nclients; 806 807 if (c == sel) 808 return; 809 810 new = clients[sel]; 811 if (sel < c) 812 memmove(&clients[sel], &clients[sel+1], 813 sizeof(Client *) * (c - sel)); 814 else 815 memmove(&clients[c+1], &clients[c], 816 sizeof(Client *) * (sel - c)); 817 clients[c] = new; 818 sel = c; 819 820 drawbar(); 821 } 822 823 void 824 propertynotify(const XEvent *e) 825 { 826 const XPropertyEvent *ev = &e->xproperty; 827 XWMHints *wmh; 828 int c; 829 char* selection = NULL; 830 Arg arg; 831 832 if (ev->state == PropertyNewValue && ev->atom == wmatom[WMSelectTab]) { 833 selection = getatom(WMSelectTab); 834 if (!strncmp(selection, "0x", 2)) { 835 arg.i = getclient(strtoul(selection, NULL, 0)); 836 move(&arg); 837 } else { 838 cmd[cmd_append_pos] = selection; 839 arg.v = cmd; 840 spawn(&arg); 841 } 842 } else if (ev->state == PropertyNewValue && ev->atom == XA_WM_HINTS && 843 (c = getclient(ev->window)) > -1 && 844 (wmh = XGetWMHints(dpy, clients[c]->win))) { 845 if (wmh->flags & XUrgencyHint) { 846 XFree(wmh); 847 wmh = XGetWMHints(dpy, win); 848 if (c != sel) { 849 if (urgentswitch && wmh && 850 !(wmh->flags & XUrgencyHint)) { 851 /* only switch, if tabbed was focused 852 * since last urgency hint if WMHints 853 * could not be received, 854 * default to no switch */ 855 focus(c); 856 } else { 857 /* if no switch should be performed, 858 * mark tab as urgent */ 859 clients[c]->urgent = True; 860 drawbar(); 861 } 862 } 863 if (wmh && !(wmh->flags & XUrgencyHint)) { 864 /* update tabbed urgency hint 865 * if not set already */ 866 wmh->flags |= XUrgencyHint; 867 XSetWMHints(dpy, win, wmh); 868 } 869 } 870 XFree(wmh); 871 } else if (ev->state != PropertyDelete && ev->atom == XA_WM_NAME && 872 (c = getclient(ev->window)) > -1) { 873 updatetitle(c); 874 } 875 } 876 877 void 878 resize(int c, int w, int h) 879 { 880 XConfigureEvent ce; 881 XWindowChanges wc; 882 883 ce.x = 0; 884 ce.y = wc.y = bh; 885 ce.width = wc.width = w; 886 ce.height = wc.height = h; 887 ce.type = ConfigureNotify; 888 ce.display = dpy; 889 ce.event = clients[c]->win; 890 ce.window = clients[c]->win; 891 ce.above = None; 892 ce.override_redirect = False; 893 ce.border_width = 0; 894 895 XConfigureWindow(dpy, clients[c]->win, CWY | CWWidth | CWHeight, &wc); 896 XSendEvent(dpy, clients[c]->win, False, StructureNotifyMask, 897 (XEvent *)&ce); 898 } 899 900 void 901 rotate(const Arg *arg) 902 { 903 int nsel = -1; 904 905 if (sel < 0) 906 return; 907 908 if (arg->i == 0) { 909 if (lastsel > -1) 910 focus(lastsel); 911 } else if (sel > -1) { 912 /* Rotating in an arg->i step around the clients. */ 913 nsel = sel + arg->i; 914 while (nsel >= nclients) 915 nsel -= nclients; 916 while (nsel < 0) 917 nsel += nclients; 918 focus(nsel); 919 } 920 } 921 922 void 923 run(void) 924 { 925 XEvent ev; 926 927 /* main event loop */ 928 XSync(dpy, False); 929 drawbar(); 930 if (doinitspawn == True) 931 spawn(NULL); 932 933 while (running) { 934 XNextEvent(dpy, &ev); 935 if (handler[ev.type]) 936 (handler[ev.type])(&ev); /* call handler */ 937 } 938 } 939 940 void 941 sendxembed(int c, long msg, long detail, long d1, long d2) 942 { 943 XEvent e = { 0 }; 944 945 e.xclient.window = clients[c]->win; 946 e.xclient.type = ClientMessage; 947 e.xclient.message_type = wmatom[XEmbed]; 948 e.xclient.format = 32; 949 e.xclient.data.l[0] = CurrentTime; 950 e.xclient.data.l[1] = msg; 951 e.xclient.data.l[2] = detail; 952 e.xclient.data.l[3] = d1; 953 e.xclient.data.l[4] = d2; 954 XSendEvent(dpy, clients[c]->win, False, NoEventMask, &e); 955 } 956 957 void 958 setcmd(int argc, char *argv[], int replace) 959 { 960 int i; 961 962 cmd = ecalloc(argc + 3, sizeof(*cmd)); 963 if (argc == 0) 964 return; 965 for (i = 0; i < argc; i++) 966 cmd[i] = argv[i]; 967 cmd[replace > 0 ? replace : argc] = winid; 968 cmd_append_pos = argc + !replace; 969 cmd[cmd_append_pos] = cmd[cmd_append_pos + 1] = NULL; 970 } 971 972 void 973 setup(void) 974 { 975 int bitm, tx, ty, tw, th, dh, dw, isfixed; 976 XWMHints *wmh; 977 XClassHint class_hint; 978 XSizeHints *size_hint; 979 980 /* clean up any zombies immediately */ 981 sigchld(0); 982 983 /* init screen */ 984 screen = DefaultScreen(dpy); 985 root = RootWindow(dpy, screen); 986 initfont(font); 987 bh = dc.h = dc.font.height + 2; 988 989 /* init atoms */ 990 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 991 wmatom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", 992 False); 993 wmatom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 994 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 995 wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False); 996 wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 997 wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False); 998 999 /* init appearance */ 1000 wx = 0; 1001 wy = 0; 1002 ww = 800; 1003 wh = 600; 1004 isfixed = 0; 1005 1006 if (geometry) { 1007 tx = ty = tw = th = 0; 1008 bitm = XParseGeometry(geometry, &tx, &ty, (unsigned *)&tw, 1009 (unsigned *)&th); 1010 if (bitm & XValue) 1011 wx = tx; 1012 if (bitm & YValue) 1013 wy = ty; 1014 if (bitm & WidthValue) 1015 ww = tw; 1016 if (bitm & HeightValue) 1017 wh = th; 1018 if (bitm & XNegative && wx == 0) 1019 wx = -1; 1020 if (bitm & YNegative && wy == 0) 1021 wy = -1; 1022 if (bitm & (HeightValue | WidthValue)) 1023 isfixed = 1; 1024 1025 dw = DisplayWidth(dpy, screen); 1026 dh = DisplayHeight(dpy, screen); 1027 if (wx < 0) 1028 wx = dw + wx - ww - 1; 1029 if (wy < 0) 1030 wy = dh + wy - wh - 1; 1031 } 1032 1033 dc.norm[ColBG] = getcolor(normbgcolor); 1034 dc.norm[ColFG] = getcolor(normfgcolor); 1035 dc.sel[ColBG] = getcolor(selbgcolor); 1036 dc.sel[ColFG] = getcolor(selfgcolor); 1037 dc.urg[ColBG] = getcolor(urgbgcolor); 1038 dc.urg[ColFG] = getcolor(urgfgcolor); 1039 dc.drawable = XCreatePixmap(dpy, root, ww, wh, 1040 DefaultDepth(dpy, screen)); 1041 dc.gc = XCreateGC(dpy, root, 0, 0); 1042 1043 win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0, 1044 dc.norm[ColFG].pixel, dc.norm[ColBG].pixel); 1045 XMapRaised(dpy, win); 1046 XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask | 1047 ButtonPressMask | ExposureMask | KeyPressMask | 1048 PropertyChangeMask | StructureNotifyMask | 1049 SubstructureRedirectMask); 1050 xerrorxlib = XSetErrorHandler(xerror); 1051 1052 class_hint.res_name = wmname; 1053 class_hint.res_class = "tabbed"; 1054 XSetClassHint(dpy, win, &class_hint); 1055 1056 size_hint = XAllocSizeHints(); 1057 if (!isfixed) { 1058 size_hint->flags = PSize | PMinSize; 1059 size_hint->height = wh; 1060 size_hint->width = ww; 1061 size_hint->min_height = bh + 1; 1062 } else { 1063 size_hint->flags = PMaxSize | PMinSize; 1064 size_hint->min_width = size_hint->max_width = ww; 1065 size_hint->min_height = size_hint->max_height = wh; 1066 } 1067 wmh = XAllocWMHints(); 1068 XSetWMProperties(dpy, win, NULL, NULL, NULL, 0, size_hint, wmh, NULL); 1069 XFree(size_hint); 1070 XFree(wmh); 1071 1072 XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1); 1073 1074 snprintf(winid, sizeof(winid), "%lu", win); 1075 setenv("XEMBED", winid, 1); 1076 1077 nextfocus = foreground; 1078 focus(-1); 1079 } 1080 1081 void 1082 sigchld(int unused) 1083 { 1084 if (signal(SIGCHLD, sigchld) == SIG_ERR) 1085 die("%s: cannot install SIGCHLD handler", argv0); 1086 1087 while (0 < waitpid(-1, NULL, WNOHANG)); 1088 } 1089 1090 void 1091 spawn(const Arg *arg) 1092 { 1093 if (fork() == 0) { 1094 if(dpy) 1095 close(ConnectionNumber(dpy)); 1096 1097 setsid(); 1098 if (arg && arg->v) { 1099 execvp(((char **)arg->v)[0], (char **)arg->v); 1100 fprintf(stderr, "%s: execvp %s", argv0, 1101 ((char **)arg->v)[0]); 1102 } else { 1103 cmd[cmd_append_pos] = NULL; 1104 execvp(cmd[0], cmd); 1105 fprintf(stderr, "%s: execvp %s", argv0, cmd[0]); 1106 } 1107 perror(" failed"); 1108 exit(0); 1109 } 1110 } 1111 1112 int 1113 textnw(const char *text, unsigned int len) 1114 { 1115 XGlyphInfo ext; 1116 XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext); 1117 return ext.xOff; 1118 } 1119 1120 void 1121 toggle(const Arg *arg) 1122 { 1123 *(Bool*) arg->v = !*(Bool*) arg->v; 1124 } 1125 1126 void 1127 unmanage(int c) 1128 { 1129 if (c < 0 || c >= nclients) { 1130 drawbar(); 1131 XSync(dpy, False); 1132 return; 1133 } 1134 1135 if (!nclients) 1136 return; 1137 1138 if (c == 0) { 1139 /* First client. */ 1140 nclients--; 1141 free(clients[0]); 1142 memmove(&clients[0], &clients[1], sizeof(Client *) * nclients); 1143 } else if (c == nclients - 1) { 1144 /* Last client. */ 1145 nclients--; 1146 free(clients[c]); 1147 clients = erealloc(clients, sizeof(Client *) * nclients); 1148 } else { 1149 /* Somewhere inbetween. */ 1150 free(clients[c]); 1151 memmove(&clients[c], &clients[c+1], 1152 sizeof(Client *) * (nclients - (c + 1))); 1153 nclients--; 1154 } 1155 1156 if (nclients <= 0) { 1157 lastsel = sel = -1; 1158 1159 if (closelastclient) 1160 running = False; 1161 else if (fillagain && running) 1162 spawn(NULL); 1163 } else { 1164 if (lastsel >= nclients) 1165 lastsel = nclients - 1; 1166 else if (lastsel > c) 1167 lastsel--; 1168 1169 if (c == sel && lastsel >= 0) { 1170 focus(lastsel); 1171 } else { 1172 if (sel > c) 1173 sel--; 1174 if (sel >= nclients) 1175 sel = nclients - 1; 1176 1177 focus(sel); 1178 } 1179 } 1180 1181 drawbar(); 1182 XSync(dpy, False); 1183 } 1184 1185 void 1186 unmapnotify(const XEvent *e) 1187 { 1188 const XUnmapEvent *ev = &e->xunmap; 1189 int c; 1190 1191 if ((c = getclient(ev->window)) > -1) 1192 unmanage(c); 1193 } 1194 1195 void 1196 updatenumlockmask(void) 1197 { 1198 unsigned int i, j; 1199 XModifierKeymap *modmap; 1200 1201 numlockmask = 0; 1202 modmap = XGetModifierMapping(dpy); 1203 for (i = 0; i < 8; i++) { 1204 for (j = 0; j < modmap->max_keypermod; j++) { 1205 if (modmap->modifiermap[i * modmap->max_keypermod + j] 1206 == XKeysymToKeycode(dpy, XK_Num_Lock)) 1207 numlockmask = (1 << i); 1208 } 1209 } 1210 XFreeModifiermap(modmap); 1211 } 1212 1213 void 1214 updatetitle(int c) 1215 { 1216 if (!gettextprop(clients[c]->win, wmatom[WMName], clients[c]->name, 1217 sizeof(clients[c]->name))) 1218 gettextprop(clients[c]->win, XA_WM_NAME, clients[c]->name, 1219 sizeof(clients[c]->name)); 1220 if (sel == c) 1221 xsettitle(win, clients[c]->name); 1222 drawbar(); 1223 } 1224 1225 /* There's no way to check accesses to destroyed windows, thus those cases are 1226 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs 1227 * default error handler, which may call exit. */ 1228 int 1229 xerror(Display *dpy, XErrorEvent *ee) 1230 { 1231 if (ee->error_code == BadWindow 1232 || (ee->request_code == X_SetInputFocus && 1233 ee->error_code == BadMatch) 1234 || (ee->request_code == X_PolyText8 && 1235 ee->error_code == BadDrawable) 1236 || (ee->request_code == X_PolyFillRectangle && 1237 ee->error_code == BadDrawable) 1238 || (ee->request_code == X_PolySegment && 1239 ee->error_code == BadDrawable) 1240 || (ee->request_code == X_ConfigureWindow && 1241 ee->error_code == BadMatch) 1242 || (ee->request_code == X_GrabButton && 1243 ee->error_code == BadAccess) 1244 || (ee->request_code == X_GrabKey && 1245 ee->error_code == BadAccess) 1246 || (ee->request_code == X_CopyArea && 1247 ee->error_code == BadDrawable)) 1248 return 0; 1249 1250 fprintf(stderr, "%s: fatal error: request code=%d, error code=%d\n", 1251 argv0, ee->request_code, ee->error_code); 1252 return xerrorxlib(dpy, ee); /* may call exit */ 1253 } 1254 1255 void 1256 xsettitle(Window w, const char *str) 1257 { 1258 XTextProperty xtp; 1259 1260 if (XmbTextListToTextProperty(dpy, (char **)&str, 1, 1261 XCompoundTextStyle, &xtp) == Success) { 1262 XSetTextProperty(dpy, w, &xtp, wmatom[WMName]); 1263 XSetTextProperty(dpy, w, &xtp, XA_WM_NAME); 1264 XFree(xtp.value); 1265 } 1266 } 1267 1268 void 1269 usage(void) 1270 { 1271 die("usage: %s [-dfksv] [-g geometry] [-n name] [-p [s+/-]pos]\n" 1272 " [-r narg] [-o color] [-O color] [-t color] [-T color]\n" 1273 " [-u color] [-U color] command...\n", argv0); 1274 } 1275 1276 int 1277 main(int argc, char *argv[]) 1278 { 1279 Bool detach = False; 1280 int replace = 0; 1281 char *pstr; 1282 1283 ARGBEGIN { 1284 case 'c': 1285 closelastclient = True; 1286 fillagain = False; 1287 break; 1288 case 'd': 1289 detach = True; 1290 break; 1291 case 'f': 1292 fillagain = True; 1293 break; 1294 case 'g': 1295 geometry = EARGF(usage()); 1296 break; 1297 case 'k': 1298 killclientsfirst = True; 1299 break; 1300 case 'n': 1301 wmname = EARGF(usage()); 1302 break; 1303 case 'O': 1304 normfgcolor = EARGF(usage()); 1305 break; 1306 case 'o': 1307 normbgcolor = EARGF(usage()); 1308 break; 1309 case 'p': 1310 pstr = EARGF(usage()); 1311 if (pstr[0] == 's') { 1312 npisrelative = True; 1313 newposition = atoi(&pstr[1]); 1314 } else { 1315 newposition = atoi(pstr); 1316 } 1317 break; 1318 case 'r': 1319 replace = atoi(EARGF(usage())); 1320 break; 1321 case 's': 1322 doinitspawn = False; 1323 break; 1324 case 'T': 1325 selfgcolor = EARGF(usage()); 1326 break; 1327 case 't': 1328 selbgcolor = EARGF(usage()); 1329 break; 1330 case 'U': 1331 urgfgcolor = EARGF(usage()); 1332 break; 1333 case 'u': 1334 urgbgcolor = EARGF(usage()); 1335 break; 1336 case 'v': 1337 die("tabbed-"VERSION", © 2009-2016 tabbed engineers, " 1338 "see LICENSE for details.\n"); 1339 break; 1340 default: 1341 usage(); 1342 break; 1343 } ARGEND; 1344 1345 if (argc < 1) { 1346 doinitspawn = False; 1347 fillagain = False; 1348 } 1349 1350 setcmd(argc, argv, replace); 1351 1352 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 1353 fprintf(stderr, "%s: no locale support\n", argv0); 1354 if (!(dpy = XOpenDisplay(NULL))) 1355 die("%s: cannot open display\n", argv0); 1356 1357 setup(); 1358 printf("0x%lx\n", win); 1359 fflush(NULL); 1360 1361 if (detach) { 1362 if (fork() == 0) { 1363 fclose(stdout); 1364 } else { 1365 if (dpy) 1366 close(ConnectionNumber(dpy)); 1367 return EXIT_SUCCESS; 1368 } 1369 } 1370 1371 run(); 1372 cleanup(); 1373 XCloseDisplay(dpy); 1374 1375 return EXIT_SUCCESS; 1376 }