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