tabbed

tab interface for application supporting Xembed
git clone git://git.suckless.org/tabbed
Log | Files | Refs | README | LICENSE

tabbed.c (29931B)


      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, 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 
    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 	struct sigaction sa;
    980 
    981 	/* do not transform children into zombies when they terminate */
    982 	sigemptyset(&sa.sa_mask);
    983 	sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
    984 	sa.sa_handler = SIG_IGN;
    985 	sigaction(SIGCHLD, &sa, NULL);
    986 
    987 	/* clean up any zombies that might have been inherited */
    988 	while (waitpid(-1, NULL, WNOHANG) > 0);
    989 
    990 	/* init screen */
    991 	screen = DefaultScreen(dpy);
    992 	root = RootWindow(dpy, screen);
    993 	initfont(font);
    994 	bh = dc.h = dc.font.height + 2;
    995 
    996 	/* init atoms */
    997 	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    998 	wmatom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN",
    999 	                                   False);
   1000 	wmatom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
   1001 	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
   1002 	wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False);
   1003 	wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
   1004 	wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False);
   1005 
   1006 	/* init appearance */
   1007 	wx = 0;
   1008 	wy = 0;
   1009 	ww = 800;
   1010 	wh = 600;
   1011 	isfixed = 0;
   1012 
   1013 	if (geometry) {
   1014 		tx = ty = tw = th = 0;
   1015 		bitm = XParseGeometry(geometry, &tx, &ty, (unsigned *)&tw,
   1016 		                      (unsigned *)&th);
   1017 		if (bitm & XValue)
   1018 			wx = tx;
   1019 		if (bitm & YValue)
   1020 			wy = ty;
   1021 		if (bitm & WidthValue)
   1022 			ww = tw;
   1023 		if (bitm & HeightValue)
   1024 			wh = th;
   1025 		if (bitm & XNegative && wx == 0)
   1026 			wx = -1;
   1027 		if (bitm & YNegative && wy == 0)
   1028 			wy = -1;
   1029 		if (bitm & (HeightValue | WidthValue))
   1030 			isfixed = 1;
   1031 
   1032 		dw = DisplayWidth(dpy, screen);
   1033 		dh = DisplayHeight(dpy, screen);
   1034 		if (wx < 0)
   1035 			wx = dw + wx - ww - 1;
   1036 		if (wy < 0)
   1037 			wy = dh + wy - wh - 1;
   1038 	}
   1039 
   1040 	dc.norm[ColBG] = getcolor(normbgcolor);
   1041 	dc.norm[ColFG] = getcolor(normfgcolor);
   1042 	dc.sel[ColBG] = getcolor(selbgcolor);
   1043 	dc.sel[ColFG] = getcolor(selfgcolor);
   1044 	dc.urg[ColBG] = getcolor(urgbgcolor);
   1045 	dc.urg[ColFG] = getcolor(urgfgcolor);
   1046 	dc.drawable = XCreatePixmap(dpy, root, ww, wh,
   1047 	                            DefaultDepth(dpy, screen));
   1048 	dc.gc = XCreateGC(dpy, root, 0, 0);
   1049 
   1050 	win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0,
   1051 	                          dc.norm[ColFG].pixel, dc.norm[ColBG].pixel);
   1052 	XMapRaised(dpy, win);
   1053 	XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask |
   1054 	             ButtonPressMask | ExposureMask | KeyPressMask |
   1055 	             PropertyChangeMask | StructureNotifyMask |
   1056 	             SubstructureRedirectMask);
   1057 	xerrorxlib = XSetErrorHandler(xerror);
   1058 
   1059 	class_hint.res_name = wmname;
   1060 	class_hint.res_class = "tabbed";
   1061 	XSetClassHint(dpy, win, &class_hint);
   1062 
   1063 	size_hint = XAllocSizeHints();
   1064 	if (!isfixed) {
   1065 		size_hint->flags = PSize | PMinSize;
   1066 		size_hint->height = wh;
   1067 		size_hint->width = ww;
   1068 		size_hint->min_height = bh + 1;
   1069 	} else {
   1070 		size_hint->flags = PMaxSize | PMinSize;
   1071 		size_hint->min_width = size_hint->max_width = ww;
   1072 		size_hint->min_height = size_hint->max_height = wh;
   1073 	}
   1074 	wmh = XAllocWMHints();
   1075 	XSetWMProperties(dpy, win, NULL, NULL, NULL, 0, size_hint, wmh, NULL);
   1076 	XFree(size_hint);
   1077 	XFree(wmh);
   1078 
   1079 	XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1);
   1080 
   1081 	snprintf(winid, sizeof(winid), "%lu", win);
   1082 	setenv("XEMBED", winid, 1);
   1083 
   1084 	nextfocus = foreground;
   1085 	focus(-1);
   1086 }
   1087 
   1088 void
   1089 spawn(const Arg *arg)
   1090 {
   1091 	struct sigaction sa;
   1092 
   1093 	if (fork() == 0) {
   1094 		if(dpy)
   1095 			close(ConnectionNumber(dpy));
   1096 
   1097 		setsid();
   1098 
   1099 		sigemptyset(&sa.sa_mask);
   1100 		sa.sa_flags = 0;
   1101 		sa.sa_handler = SIG_DFL;
   1102 		sigaction(SIGCHLD, &sa, NULL);
   1103 
   1104 		if (arg && arg->v) {
   1105 			execvp(((char **)arg->v)[0], (char **)arg->v);
   1106 			fprintf(stderr, "%s: execvp %s", argv0,
   1107 			        ((char **)arg->v)[0]);
   1108 		} else {
   1109 			cmd[cmd_append_pos] = NULL;
   1110 			execvp(cmd[0], cmd);
   1111 			fprintf(stderr, "%s: execvp %s", argv0, cmd[0]);
   1112 		}
   1113 		perror(" failed");
   1114 		exit(0);
   1115 	}
   1116 }
   1117 
   1118 int
   1119 textnw(const char *text, unsigned int len)
   1120 {
   1121 	XGlyphInfo ext;
   1122 	XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
   1123 	return ext.xOff;
   1124 }
   1125 
   1126 void
   1127 toggle(const Arg *arg)
   1128 {
   1129     *(Bool*) arg->v = !*(Bool*) arg->v;
   1130 }
   1131 
   1132 void
   1133 unmanage(int c)
   1134 {
   1135 	if (c < 0 || c >= nclients) {
   1136 		drawbar();
   1137 		XSync(dpy, False);
   1138 		return;
   1139 	}
   1140 
   1141 	if (!nclients)
   1142 		return;
   1143 
   1144 	if (c == 0) {
   1145 		/* First client. */
   1146 		nclients--;
   1147 		free(clients[0]);
   1148 		memmove(&clients[0], &clients[1], sizeof(Client *) * nclients);
   1149 	} else if (c == nclients - 1) {
   1150 		/* Last client. */
   1151 		nclients--;
   1152 		free(clients[c]);
   1153 		clients = erealloc(clients, sizeof(Client *) * nclients);
   1154 	} else {
   1155 		/* Somewhere inbetween. */
   1156 		free(clients[c]);
   1157 		memmove(&clients[c], &clients[c+1],
   1158 		        sizeof(Client *) * (nclients - (c + 1)));
   1159 		nclients--;
   1160 	}
   1161 
   1162 	if (nclients <= 0) {
   1163 		lastsel = sel = -1;
   1164 
   1165 		if (closelastclient)
   1166 			running = False;
   1167 		else if (fillagain && running)
   1168 			spawn(NULL);
   1169 	} else {
   1170 		if (lastsel >= nclients)
   1171 			lastsel = nclients - 1;
   1172 		else if (lastsel > c)
   1173 			lastsel--;
   1174 
   1175 		if (c == sel && lastsel >= 0) {
   1176 			focus(lastsel);
   1177 		} else {
   1178 			if (sel > c)
   1179 				sel--;
   1180 			if (sel >= nclients)
   1181 				sel = nclients - 1;
   1182 
   1183 			focus(sel);
   1184 		}
   1185 	}
   1186 
   1187 	drawbar();
   1188 	XSync(dpy, False);
   1189 }
   1190 
   1191 void
   1192 unmapnotify(const XEvent *e)
   1193 {
   1194 	const XUnmapEvent *ev = &e->xunmap;
   1195 	int c;
   1196 
   1197 	if ((c = getclient(ev->window)) > -1)
   1198 		unmanage(c);
   1199 }
   1200 
   1201 void
   1202 updatenumlockmask(void)
   1203 {
   1204 	unsigned int i, j;
   1205 	XModifierKeymap *modmap;
   1206 
   1207 	numlockmask = 0;
   1208 	modmap = XGetModifierMapping(dpy);
   1209 	for (i = 0; i < 8; i++) {
   1210 		for (j = 0; j < modmap->max_keypermod; j++) {
   1211 			if (modmap->modifiermap[i * modmap->max_keypermod + j]
   1212 			    == XKeysymToKeycode(dpy, XK_Num_Lock))
   1213 				numlockmask = (1 << i);
   1214 		}
   1215 	}
   1216 	XFreeModifiermap(modmap);
   1217 }
   1218 
   1219 void
   1220 updatetitle(int c)
   1221 {
   1222 	if (!gettextprop(clients[c]->win, wmatom[WMName], clients[c]->name,
   1223 	    sizeof(clients[c]->name)))
   1224 		gettextprop(clients[c]->win, XA_WM_NAME, clients[c]->name,
   1225 		            sizeof(clients[c]->name));
   1226 	if (sel == c)
   1227 		xsettitle(win, clients[c]->name);
   1228 	drawbar();
   1229 }
   1230 
   1231 /* There's no way to check accesses to destroyed windows, thus those cases are
   1232  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
   1233  * default error handler, which may call exit.  */
   1234 int
   1235 xerror(Display *dpy, XErrorEvent *ee)
   1236 {
   1237 	if (ee->error_code == BadWindow
   1238 	    || (ee->request_code == X_SetInputFocus &&
   1239 	        ee->error_code == BadMatch)
   1240 	    || (ee->request_code == X_PolyText8 &&
   1241 	        ee->error_code == BadDrawable)
   1242 	    || (ee->request_code == X_PolyFillRectangle &&
   1243 	        ee->error_code == BadDrawable)
   1244 	    || (ee->request_code == X_PolySegment &&
   1245 	        ee->error_code == BadDrawable)
   1246 	    || (ee->request_code == X_ConfigureWindow &&
   1247 	        ee->error_code == BadMatch)
   1248 	    || (ee->request_code == X_GrabButton &&
   1249 	        ee->error_code == BadAccess)
   1250 	    || (ee->request_code == X_GrabKey &&
   1251 	        ee->error_code == BadAccess)
   1252 	    || (ee->request_code == X_CopyArea &&
   1253 	        ee->error_code == BadDrawable))
   1254 		return 0;
   1255 
   1256 	fprintf(stderr, "%s: fatal error: request code=%d, error code=%d\n",
   1257 	        argv0, ee->request_code, ee->error_code);
   1258 	return xerrorxlib(dpy, ee); /* may call exit */
   1259 }
   1260 
   1261 void
   1262 xsettitle(Window w, const char *str)
   1263 {
   1264 	XTextProperty xtp;
   1265 
   1266 	if (XmbTextListToTextProperty(dpy, (char **)&str, 1,
   1267 	    XCompoundTextStyle, &xtp) == Success) {
   1268 		XSetTextProperty(dpy, w, &xtp, wmatom[WMName]);
   1269 		XSetTextProperty(dpy, w, &xtp, XA_WM_NAME);
   1270 		XFree(xtp.value);
   1271 	}
   1272 }
   1273 
   1274 void
   1275 usage(void)
   1276 {
   1277 	die("usage: %s [-dfksv] [-g geometry] [-n name] [-p [s+/-]pos]\n"
   1278 	    "       [-r narg] [-o color] [-O color] [-t color] [-T color]\n"
   1279 	    "       [-u color] [-U color] command...\n", argv0);
   1280 }
   1281 
   1282 int
   1283 main(int argc, char *argv[])
   1284 {
   1285 	Bool detach = False;
   1286 	int replace = 0;
   1287 	char *pstr;
   1288 
   1289 	ARGBEGIN {
   1290 	case 'c':
   1291 		closelastclient = True;
   1292 		fillagain = False;
   1293 		break;
   1294 	case 'd':
   1295 		detach = True;
   1296 		break;
   1297 	case 'f':
   1298 		fillagain = True;
   1299 		break;
   1300 	case 'g':
   1301 		geometry = EARGF(usage());
   1302 		break;
   1303 	case 'k':
   1304 		killclientsfirst = True;
   1305 		break;
   1306 	case 'n':
   1307 		wmname = EARGF(usage());
   1308 		break;
   1309 	case 'O':
   1310 		normfgcolor = EARGF(usage());
   1311 		break;
   1312 	case 'o':
   1313 		normbgcolor = EARGF(usage());
   1314 		break;
   1315 	case 'p':
   1316 		pstr = EARGF(usage());
   1317 		if (pstr[0] == 's') {
   1318 			npisrelative = True;
   1319 			newposition = atoi(&pstr[1]);
   1320 		} else {
   1321 			newposition = atoi(pstr);
   1322 		}
   1323 		break;
   1324 	case 'r':
   1325 		replace = atoi(EARGF(usage()));
   1326 		break;
   1327 	case 's':
   1328 		doinitspawn = False;
   1329 		break;
   1330 	case 'T':
   1331 		selfgcolor = EARGF(usage());
   1332 		break;
   1333 	case 't':
   1334 		selbgcolor = EARGF(usage());
   1335 		break;
   1336 	case 'U':
   1337 		urgfgcolor = EARGF(usage());
   1338 		break;
   1339 	case 'u':
   1340 		urgbgcolor = EARGF(usage());
   1341 		break;
   1342 	case 'v':
   1343 		die("tabbed-"VERSION", © 2009-2016 tabbed engineers, "
   1344 		    "see LICENSE for details.\n");
   1345 		break;
   1346 	default:
   1347 		usage();
   1348 		break;
   1349 	} ARGEND;
   1350 
   1351 	if (argc < 1) {
   1352 		doinitspawn = False;
   1353 		fillagain = False;
   1354 	}
   1355 
   1356 	setcmd(argc, argv, replace);
   1357 
   1358 	if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
   1359 		fprintf(stderr, "%s: no locale support\n", argv0);
   1360 	if (!(dpy = XOpenDisplay(NULL)))
   1361 		die("%s: cannot open display\n", argv0);
   1362 
   1363 	setup();
   1364 	printf("0x%lx\n", win);
   1365 	fflush(NULL);
   1366 
   1367 	if (detach) {
   1368 		if (fork() == 0) {
   1369 			fclose(stdout);
   1370 		} else {
   1371 			if (dpy)
   1372 				close(ConnectionNumber(dpy));
   1373 			return EXIT_SUCCESS;
   1374 		}
   1375 	}
   1376 
   1377 	run();
   1378 	cleanup();
   1379 	XCloseDisplay(dpy);
   1380 
   1381 	return EXIT_SUCCESS;
   1382 }