├── tabbed ├── xembed ├── tabbed.o ├── xembed.o ├── TODO ├── scripts ├── tabbedize └── tabbed-slime ├── tabbed.c.rej ├── patches ├── tabbed-clientnumber-0.6.diff ├── tabbed-keyrelease-20191216-b5f9ec6.diff ├── tabbed-hidetabs-20191216-b5f9ec6.diff ├── alpha.diff ├── tabbed-0.6-xft.diff └── tabbed-icon-20200905-2da4e96.diff ├── config.mk ├── xembed.c ├── xembed.1 ├── arg.h ├── LICENSE ├── Makefile ├── README.md ├── icon.h ├── tabbed.1 ├── config.h ├── config.def.h ├── tabbed.c.orig └── tabbed.c /tabbed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huijunchen9260/tabbed-hjc/HEAD/tabbed -------------------------------------------------------------------------------- /xembed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huijunchen9260/tabbed-hjc/HEAD/xembed -------------------------------------------------------------------------------- /tabbed.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huijunchen9260/tabbed-hjc/HEAD/tabbed.o -------------------------------------------------------------------------------- /xembed.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huijunchen9260/tabbed-hjc/HEAD/xembed.o -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | # TODO 2 | * add some way to detach windows 3 | * add some way to attach windows 4 | -------------------------------------------------------------------------------- /scripts/tabbedize: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Dependency: xwininfo, xdotool 4 | # Work without EWMH/NetWM 5 | 6 | rootid=$(printf '%d' "$(xwininfo -root | grep "Window id" | cut -d ' ' -f 4)") 7 | wid=$(xdotool getwindowfocus) 8 | wname=$(xwininfo -id "$wid" | grep 'Window id:' | cut -d ' ' -f 5-) 9 | [ "$wid" != "$rootid" ] && [ "$wname" != '(has no name)' ] && tabbed -c -d | xargs -I {} xdotool windowreparent "$wid" "{}" 10 | 11 | -------------------------------------------------------------------------------- /tabbed.c.rej: -------------------------------------------------------------------------------- 1 | --- tabbed.c 2 | +++ tabbed.c 3 | @@ -50,7 +51,7 @@ 4 | 5 | enum { ColFG, ColBG, ColLast }; /* color */ 6 | enum { WMProtocols, WMDelete, WMName, WMState, WMFullscreen, 7 | - XEmbed, WMSelectTab, WMLast }; /* default atoms */ 8 | + XEmbed, WMSelectTab, WMIcon, WMLast }; /* default atoms */ 9 | 10 | typedef union { 11 | int i; 12 | @@ -171,6 +173,7 @@ static char winid[64]; 13 | static char **cmd; 14 | static char *wmname = "tabbed"; 15 | static const char *geometry; 16 | +static unsigned long icon[ICON_WIDTH * ICON_HEIGHT + 2]; 17 | 18 | char *argv0; 19 | 20 | -------------------------------------------------------------------------------- /patches/tabbed-clientnumber-0.6.diff: -------------------------------------------------------------------------------- 1 | diff --git a/tabbed.c b/tabbed.c 2 | index d30206b..70642cb 100644 3 | --- a/tabbed.c 4 | +++ b/tabbed.c 5 | @@ -308,6 +308,7 @@ drawbar(void) { 6 | unsigned long *col; 7 | int c, fc, width, n = 0; 8 | char *name = NULL; 9 | + char tabtitle[256]; 10 | 11 | if(nclients == 0) { 12 | dc.x = 0; 13 | @@ -353,7 +354,9 @@ drawbar(void) { 14 | } else { 15 | col = dc.norm; 16 | } 17 | - drawtext(clients[c]->name, col); 18 | + snprintf(tabtitle, sizeof(tabtitle), "%d: %s", 19 | + c + 1, clients[c]->name); 20 | + drawtext(tabtitle, col); 21 | dc.x += dc.w; 22 | clients[c]->tabx = dc.x; 23 | } 24 | -------------------------------------------------------------------------------- /scripts/tabbed-slime: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Dependency: xwininfo, xdotool, dmenu 4 | 5 | # xwininfo -children -id $(xdotool getactivewindow) | grep ' 0x' | awk '{print $1, $2}' | tail -n +2 | dmenu -i -l 5 -p 'REPL window: ' | cut -b 1-9 | xargs printf '%d' 6 | 7 | nl=' 8 | ' 9 | 10 | info=$(xwininfo -children -id $(xdotool getactivewindow)) 11 | 12 | IFS="$nl" 13 | set -- $info 14 | unset IFS 15 | 16 | for line do 17 | case $line in 18 | ' 0x'*) 19 | line=${line#${line%%[![:space:]]*}} 20 | safe="$safe${nl}${line%%:*}" 21 | ;; 22 | esac 23 | done 24 | safe=${safe#*$nl} 25 | choice=$(printf '%s' "$safe" | dmenu -i -l 5 -p 'REPL window: ') 26 | case $choice in 27 | '') ;; 28 | *) printf '%d' ${choice%% *} 29 | esac 30 | -------------------------------------------------------------------------------- /config.mk: -------------------------------------------------------------------------------- 1 | # tabbed version 2 | VERSION = 0.6 3 | 4 | # Customize below to fit your system 5 | 6 | # paths 7 | PREFIX = /usr/local 8 | MANPREFIX = ${PREFIX}/share/man 9 | 10 | X11INC = /usr/X11R6/include 11 | X11LIB = /usr/X11R6/lib 12 | 13 | # freetype 14 | FREETYPELIBS = -lfontconfig -lXft 15 | FREETYPEINC = /usr/include/freetype2 16 | # OpenBSD (uncomment) 17 | #FREETYPEINC = ${X11INC}/freetype2 18 | 19 | # includes and libs 20 | INCS = -I. -I/usr/include -I$(X11INC) -I${FREETYPEINC} 21 | LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${FREETYPELIBS} -lXft -lXrender 22 | 23 | 24 | # flags 25 | CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE 26 | CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} 27 | LDFLAGS = -s ${LIBS} 28 | 29 | # Solaris 30 | #CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" 31 | #LDFLAGS = ${LIBS} 32 | 33 | # compiler and linker 34 | CC = cc 35 | -------------------------------------------------------------------------------- /xembed.c: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file for copyright and license details. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int 11 | main(int argc, char *argv[]) 12 | { 13 | char *xembed; 14 | int tty; 15 | pid_t pgrp, tcpgrp; 16 | 17 | if (argc < 3) { 18 | fprintf(stderr, "usage: %s flag cmd ...\n", argv[0]); 19 | return 2; 20 | } 21 | 22 | if (!(xembed = getenv("XEMBED"))) 23 | goto noembed; 24 | 25 | if ((tty = open("/dev/tty", O_RDONLY)) < 0) 26 | goto noembed; 27 | 28 | pgrp = getpgrp(); 29 | tcpgrp = tcgetpgrp(tty); 30 | 31 | close(tty); 32 | 33 | if (pgrp == tcpgrp) { /* in foreground of tty */ 34 | argv[0] = argv[2]; 35 | argv[2] = xembed; 36 | } else { 37 | noembed: 38 | argv += 2; 39 | } 40 | 41 | execvp(argv[0], argv); 42 | 43 | perror(argv[0]); /* failed to execute */ 44 | return 1; 45 | } 46 | -------------------------------------------------------------------------------- /xembed.1: -------------------------------------------------------------------------------- 1 | .TH XEMBED 1 tabbed\-VERSION 2 | .SH NAME 3 | xembed \- XEmbed foreground process 4 | .SH SYNOPSIS 5 | .B xembed 6 | .I flag command 7 | .RI [ "argument ..." ] 8 | .SH DESCRIPTION 9 | If the environment variable XEMBED is set, and 10 | .B xembed 11 | is in the foreground of its controlling tty, it will execute 12 | .IP 13 | command flag $XEMBED [argument ...] 14 | .LP 15 | Otherwise it will execute 16 | .IP 17 | command [argument ...] 18 | .LP 19 | .SH EXAMPLE 20 | In a terminal emulator within a 21 | .B tabbed 22 | session, the shell alias 23 | .IP 24 | $ alias surf='xembed -e surf' 25 | .LP 26 | will cause `surf' to open in a new tab, unless it is run in the background, 27 | i.e. `surf &', in which case it will instead open in a new window. 28 | .SH AUTHORS 29 | See the LICENSE file for the authors. 30 | .SH LICENSE 31 | See the LICENSE file for the terms of redistribution. 32 | .SH SEE ALSO 33 | .BR tabbed (1) 34 | .SH BUGS 35 | Please report them. 36 | -------------------------------------------------------------------------------- /arg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copy me if you can. 3 | * by 20h 4 | */ 5 | 6 | #ifndef ARG_H__ 7 | #define ARG_H__ 8 | 9 | extern char *argv0; 10 | 11 | /* use main(int argc, char *argv[]) */ 12 | #define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ 13 | argv[0] && argv[0][0] == '-'\ 14 | && argv[0][1];\ 15 | argc--, argv++) {\ 16 | char argc_;\ 17 | char **argv_;\ 18 | int brk_;\ 19 | if (argv[0][1] == '-' && argv[0][2] == '\0') {\ 20 | argv++;\ 21 | argc--;\ 22 | break;\ 23 | }\ 24 | for (brk_ = 0, argv[0]++, argv_ = argv;\ 25 | argv[0][0] && !brk_;\ 26 | argv[0]++) {\ 27 | if (argv_ != argv)\ 28 | break;\ 29 | argc_ = argv[0][0];\ 30 | switch (argc_) 31 | #define ARGEND }\ 32 | } 33 | 34 | #define ARGC() argc_ 35 | 36 | #define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ 37 | ((x), abort(), (char *)0) :\ 38 | (brk_ = 1, (argv[0][1] != '\0')?\ 39 | (&argv[0][1]) :\ 40 | (argc--, argv++, argv[0]))) 41 | 42 | #define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ 43 | (char *)0 :\ 44 | (brk_ = 1, (argv[0][1] != '\0')?\ 45 | (&argv[0][1]) :\ 46 | (argc--, argv++, argv[0]))) 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT/X Consortium License 2 | 3 | © 2009-2011 Enno Boland 4 | © 2011,2015 Connor Lane Smith 5 | © 2012-2015 Christoph Lohmann <20h@r-36.net> 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a 8 | copy of this software and associated documentation files (the "Software"), 9 | to deal in the Software without restriction, including without limitation 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 | and/or sell copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # tabbed - tabbing interface 2 | # See LICENSE file for copyright and license details. 3 | 4 | include config.mk 5 | 6 | SRC = tabbed.c xembed.c 7 | OBJ = ${SRC:.c=.o} 8 | BIN = ${OBJ:.o=} 9 | 10 | all: options ${BIN} 11 | 12 | options: 13 | @echo tabbed build options: 14 | @echo "CFLAGS = ${CFLAGS}" 15 | @echo "LDFLAGS = ${LDFLAGS}" 16 | @echo "CC = ${CC}" 17 | 18 | .c.o: 19 | @echo CC $< 20 | @${CC} -c ${CFLAGS} $< 21 | 22 | ${OBJ}: config.h config.mk 23 | 24 | config.h: 25 | @echo creating $@ from config.def.h 26 | @cp config.def.h $@ 27 | 28 | .o: 29 | @echo CC -o $@ 30 | @${CC} -o $@ $< ${LDFLAGS} 31 | 32 | clean: 33 | @echo cleaning 34 | @rm -f ${BIN} ${OBJ} tabbed-${VERSION}.tar.gz 35 | 36 | dist: clean 37 | @echo creating dist tarball 38 | @mkdir -p tabbed-${VERSION} 39 | @cp -R LICENSE Makefile README config.def.h config.mk \ 40 | tabbed.1 arg.h icon.h ${SRC} tabbed-${VERSION} 41 | @tar -cf tabbed-${VERSION}.tar tabbed-${VERSION} 42 | @gzip tabbed-${VERSION}.tar 43 | @rm -rf tabbed-${VERSION} 44 | 45 | install: all 46 | @echo installing executable files to ${DESTDIR}${PREFIX}/bin 47 | @mkdir -p "${DESTDIR}${PREFIX}/bin" 48 | @cp -f ${BIN} "${DESTDIR}${PREFIX}/bin" 49 | @chmod 755 "${DESTDIR}${PREFIX}/bin/tabbed" 50 | @echo installing manual pages to ${DESTDIR}${MANPREFIX}/man1 51 | @mkdir -p "${DESTDIR}${MANPREFIX}/man1" 52 | @sed "s/VERSION/${VERSION}/g" < tabbed.1 > "${DESTDIR}${MANPREFIX}/man1/tabbed.1" 53 | @chmod 644 "${DESTDIR}${MANPREFIX}/man1/tabbed.1" 54 | @sed "s/VERSION/${VERSION}/g" < xembed.1 > "${DESTDIR}${MANPREFIX}/man1/xembed.1" 55 | @chmod 644 "${DESTDIR}${MANPREFIX}/man1/xembed.1" 56 | 57 | uninstall: 58 | @echo removing executable files from ${DESTDIR}${PREFIX}/bin 59 | @rm -f "${DESTDIR}${PREFIX}/bin/tabbed" 60 | @rm -f "${DESTDIR}${PREFIX}/bin/xembed" 61 | @echo removing manual pages from ${DESTDIR}${MANPREFIX}/man1 62 | @rm -f "${DESTDIR}${MANPREFIX}/man1/tabbed.1" 63 | @rm -f "${DESTDIR}${MANPREFIX}/man1/xembed.1" 64 | 65 | .PHONY: all options clean dist install uninstall 66 | -------------------------------------------------------------------------------- /patches/tabbed-keyrelease-20191216-b5f9ec6.diff: -------------------------------------------------------------------------------- 1 | From 6c58b480b7b6ce6a28beafc60a096069fbd51532 Mon Sep 17 00:00:00 2001 2 | From: LeelaPakanati 3 | Date: Fri, 13 Dec 2019 16:56:42 -0500 4 | Subject: [PATCH] Add function handling at keyrelease 5 | 6 | --- 7 | config.def.h | 6 ++++++ 8 | tabbed.c | 30 +++++++++++++++++++++++++++++- 9 | 2 files changed, 35 insertions(+), 1 deletion(-) 10 | 11 | diff --git a/config.def.h b/config.def.h 12 | index defa426..7bfda30 100644 13 | --- a/config.def.h 14 | +++ b/config.def.h 15 | @@ -64,3 +64,9 @@ static Key keys[] = { 16 | 17 | { 0, XK_F11, fullscreen, { 0 } }, 18 | }; 19 | + 20 | +static Key keyreleases[] = { 21 | + /* modifier key function argument */ 22 | + { 0, XK_Shift_L, NULL, { 0 } }, 23 | + 24 | +}; 25 | diff --git a/tabbed.c b/tabbed.c 26 | index ff3ada0..fe38b9d 100644 27 | --- a/tabbed.c 28 | +++ b/tabbed.c 29 | @@ -113,6 +113,7 @@ static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); 30 | static void initfont(const char *fontstr); 31 | static Bool isprotodel(int c); 32 | static void keypress(const XEvent *e); 33 | +static void keyrelease(const XEvent *e); 34 | static void killclient(const Arg *arg); 35 | static void manage(Window win); 36 | static void maprequest(const XEvent *e); 37 | @@ -149,6 +150,7 @@ static void (*handler[LASTEvent]) (const XEvent *) = { 38 | [Expose] = expose, 39 | [FocusIn] = focusin, 40 | [KeyPress] = keypress, 41 | + [KeyRelease] = keyrelease, 42 | [MapRequest] = maprequest, 43 | [PropertyNotify] = propertynotify, 44 | }; 45 | @@ -664,6 +666,22 @@ keypress(const XEvent *e) 46 | } 47 | } 48 | 49 | +void 50 | +keyrelease(const XEvent *e) 51 | +{ 52 | + const XKeyEvent *ev = &e->xkey; 53 | + unsigned int i; 54 | + KeySym keysym; 55 | + 56 | + keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0); 57 | + for (i = 0; i < LENGTH(keyreleases); i++) { 58 | + if (keysym == keyreleases[i].keysym && 59 | + CLEANMASK(keyreleases[i].mod) == CLEANMASK(ev->state) && 60 | + keyreleases[i].func) 61 | + keyreleases[i].func(&(keyreleases[i].arg)); 62 | + } 63 | +} 64 | + 65 | void 66 | killclient(const Arg *arg) 67 | { 68 | @@ -714,6 +732,16 @@ manage(Window w) 69 | } 70 | } 71 | 72 | + for (i = 0; i < LENGTH(keyreleases); i++) { 73 | + if ((code = XKeysymToKeycode(dpy, keyreleases[i].keysym))) { 74 | + for (j = 0; j < LENGTH(modifiers); j++) { 75 | + XGrabKey(dpy, code, keyreleases[i].mod | 76 | + modifiers[j], w, True, 77 | + GrabModeAsync, GrabModeAsync); 78 | + } 79 | + } 80 | + } 81 | + 82 | c = ecalloc(1, sizeof *c); 83 | c->win = w; 84 | 85 | @@ -1036,7 +1064,7 @@ setup(void) 86 | XMapRaised(dpy, win); 87 | XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask | 88 | ButtonPressMask | ExposureMask | KeyPressMask | 89 | - PropertyChangeMask | StructureNotifyMask | 90 | + KeyReleaseMask | PropertyChangeMask | StructureNotifyMask | 91 | SubstructureRedirectMask); 92 | xerrorxlib = XSetErrorHandler(xerror); 93 | 94 | -- 95 | 2.24.0 96 | 97 | -------------------------------------------------------------------------------- /patches/tabbed-hidetabs-20191216-b5f9ec6.diff: -------------------------------------------------------------------------------- 1 | From 52708d468acace9543d01e6d8afae799f8d6fccd Mon Sep 17 00:00:00 2001 2 | From: LeelaPakanati 3 | Date: Mon, 16 Dec 2019 18:57:32 -0500 4 | Subject: [PATCH] Add hide tabs feature 5 | 6 | --- 7 | config.def.h | 7 +++++-- 8 | tabbed.c | 24 +++++++++++++++++++++--- 9 | 2 files changed, 26 insertions(+), 5 deletions(-) 10 | 11 | diff --git a/config.def.h b/config.def.h 12 | index 7bfda30..bb7ef0e 100644 13 | --- a/config.def.h 14 | +++ b/config.def.h 15 | @@ -63,10 +63,13 @@ static Key keys[] = { 16 | { MODKEY|ShiftMask, XK_u, toggle, { .v = (void*) &urgentswitch } }, 17 | 18 | { 0, XK_F11, fullscreen, { 0 } }, 19 | + 20 | + { MODKEY, XK_Shift_L, showbar, { .i = 1 } }, 21 | + { ShiftMask, XK_Control_L, showbar, { .i = 1 } }, 22 | }; 23 | 24 | static Key keyreleases[] = { 25 | /* modifier key function argument */ 26 | - { 0, XK_Shift_L, NULL, { 0 } }, 27 | - 28 | + { MODKEY|ShiftMask, XK_Shift_L, showbar, { .i = 0 } }, 29 | + { MODKEY|ShiftMask, XK_Control_L, showbar, { .i = 0 } }, 30 | }; 31 | diff --git a/tabbed.c b/tabbed.c 32 | index fe38b9d..352dab2 100644 33 | --- a/tabbed.c 34 | +++ b/tabbed.c 35 | @@ -127,6 +127,7 @@ static void sendxembed(int c, long msg, long detail, long d1, long d2); 36 | static void setcmd(int argc, char *argv[], int); 37 | static void setup(void); 38 | static void sigchld(int unused); 39 | +static void showbar(const Arg *arg); 40 | static void spawn(const Arg *arg); 41 | static int textnw(const char *text, unsigned int len); 42 | static void toggle(const Arg *arg); 43 | @@ -154,7 +155,7 @@ static void (*handler[LASTEvent]) (const XEvent *) = { 44 | [MapRequest] = maprequest, 45 | [PropertyNotify] = propertynotify, 46 | }; 47 | -static int bh, wx, wy, ww, wh; 48 | +static int bh, wx, wy, ww, wh, vbh; 49 | static unsigned int numlockmask; 50 | static Bool running = True, nextfocus, doinitspawn = True, 51 | fillagain = False, closelastclient = False, 52 | @@ -171,6 +172,7 @@ static char winid[64]; 53 | static char **cmd; 54 | static char *wmname = "tabbed"; 55 | static const char *geometry; 56 | +static Bool barvisibility = False; 57 | 58 | char *argv0; 59 | 60 | @@ -317,9 +319,18 @@ void 61 | drawbar(void) 62 | { 63 | XftColor *col; 64 | - int c, cc, fc, width; 65 | + int c, cc, fc, width, nbh; 66 | char *name = NULL; 67 | 68 | + nbh = barvisibility ? vbh : 0; 69 | + if (nbh != bh) { 70 | + bh = nbh; 71 | + for (c = 0; c < nclients; c++) 72 | + XMoveResizeWindow(dpy, clients[c]->win, 0, bh, ww, wh-bh); 73 | + } 74 | + 75 | + if (bh == 0) return; 76 | + 77 | if (nclients == 0) { 78 | dc.x = 0; 79 | dc.w = ww; 80 | @@ -1003,7 +1014,7 @@ setup(void) 81 | screen = DefaultScreen(dpy); 82 | root = RootWindow(dpy, screen); 83 | initfont(font); 84 | - bh = dc.h = dc.font.height + 2; 85 | + vbh = dc.h = dc.font.height + 2; 86 | 87 | /* init atoms */ 88 | wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 89 | @@ -1096,6 +1107,13 @@ setup(void) 90 | focus(-1); 91 | } 92 | 93 | +void 94 | +showbar(const Arg *arg) 95 | +{ 96 | + barvisibility = arg->i; 97 | + drawbar(); 98 | +} 99 | + 100 | void 101 | sigchld(int unused) 102 | { 103 | -- 104 | 2.24.0 105 | 106 | -------------------------------------------------------------------------------- /patches/alpha.diff: -------------------------------------------------------------------------------- 1 | diff --git a/config.mk b/config.mk 2 | index 3a71529..095cead 100644 3 | --- a/config.mk 4 | +++ b/config.mk 5 | @@ -9,7 +9,7 @@ MANPREFIX = ${PREFIX}/share/man 6 | 7 | # includes and libs 8 | INCS = -I. -I/usr/include -I/usr/include/freetype2 9 | -LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${FREETYPELIBS} 10 | +LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${FREETYPELIBS} -lXft -lXrender 11 | 12 | # flags 13 | CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE 14 | diff --git a/tabbed.c b/tabbed.c 15 | index 9a44795..b4d47d1 100644 16 | --- a/tabbed.c 17 | +++ b/tabbed.c 18 | @@ -170,6 +170,9 @@ static char **cmd; 19 | static char *wmname = "tabbed"; 20 | static const char *geometry; 21 | 22 | +static Colormap cmap; 23 | +static Visual *visual = NULL; 24 | + 25 | char *argv0; 26 | 27 | /* configuration, allows nested code to access above variables */ 28 | @@ -255,8 +258,8 @@ configurenotify(const XEvent *e) 29 | ww = ev->width; 30 | wh = ev->height; 31 | XFreePixmap(dpy, dc.drawable); 32 | - dc.drawable = XCreatePixmap(dpy, root, ww, wh, 33 | - DefaultDepth(dpy, screen)); 34 | + dc.drawable = XCreatePixmap(dpy, win, ww, wh, 35 | + 32); 36 | if (sel > -1) 37 | resize(sel, ww, wh - bh); 38 | XSync(dpy, False); 39 | @@ -399,7 +402,7 @@ drawtext(const char *text, XftColor col[ColLast]) 40 | ; 41 | } 42 | 43 | - d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen)); 44 | + d = XftDrawCreate(dpy, dc.drawable, visual, cmap); 45 | XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len); 46 | XftDrawDestroy(d); 47 | } 48 | @@ -564,7 +567,7 @@ getcolor(const char *colstr) 49 | { 50 | XftColor color; 51 | 52 | - if (!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color)) 53 | + if (!XftColorAllocName(dpy, visual, cmap, colstr, &color)) 54 | die("%s: cannot allocate color '%s'\n", argv0, colstr); 55 | 56 | return color; 57 | @@ -1016,18 +1019,60 @@ setup(void) 58 | wy = dh + wy - wh - 1; 59 | } 60 | 61 | + XVisualInfo *vis; 62 | + XRenderPictFormat *fmt; 63 | + int nvi; 64 | + int i; 65 | + 66 | + XVisualInfo tpl = { 67 | + .screen = screen, 68 | + .depth = 32, 69 | + .class = TrueColor 70 | + }; 71 | + 72 | + vis = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask | VisualClassMask, &tpl, &nvi); 73 | + for(i = 0; i < nvi; i ++) { 74 | + fmt = XRenderFindVisualFormat(dpy, vis[i].visual); 75 | + if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { 76 | + visual = vis[i].visual; 77 | + break; 78 | + } 79 | + } 80 | + 81 | + XFree(vis); 82 | + 83 | + if (! visual) { 84 | + fprintf(stderr, "Couldn't find ARGB visual.\n"); 85 | + exit(1); 86 | + } 87 | + 88 | + cmap = XCreateColormap( dpy, root, visual, None); 89 | dc.norm[ColBG] = getcolor(normbgcolor); 90 | dc.norm[ColFG] = getcolor(normfgcolor); 91 | dc.sel[ColBG] = getcolor(selbgcolor); 92 | dc.sel[ColFG] = getcolor(selfgcolor); 93 | dc.urg[ColBG] = getcolor(urgbgcolor); 94 | dc.urg[ColFG] = getcolor(urgfgcolor); 95 | - dc.drawable = XCreatePixmap(dpy, root, ww, wh, 96 | - DefaultDepth(dpy, screen)); 97 | - dc.gc = XCreateGC(dpy, root, 0, 0); 98 | 99 | - win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0, 100 | - dc.norm[ColFG].pixel, dc.norm[ColBG].pixel); 101 | + XSetWindowAttributes attrs; 102 | + attrs.background_pixel = dc.norm[ColBG].pixel; 103 | + attrs.border_pixel = dc.norm[ColFG].pixel; 104 | + attrs.bit_gravity = NorthWestGravity; 105 | + attrs.event_mask = FocusChangeMask | KeyPressMask 106 | + | ExposureMask | VisibilityChangeMask | StructureNotifyMask 107 | + | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask; 108 | + attrs.background_pixmap = None ; 109 | + attrs.colormap = cmap; 110 | + 111 | + win = XCreateWindow(dpy, root, wx, wy, 112 | + ww, wh, 0, 32, InputOutput, 113 | + visual, CWBackPixmap | CWBorderPixel | CWBitGravity 114 | + | CWEventMask | CWColormap, &attrs); 115 | + 116 | + dc.drawable = XCreatePixmap(dpy, win, ww, wh, 117 | + 32); 118 | + dc.gc = XCreateGC(dpy, dc.drawable, 0, 0); 119 | + 120 | XMapRaised(dpy, win); 121 | XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask | 122 | ButtonPressMask | ExposureMask | KeyPressMask | 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hui-Jun Chen's build of tabbed 2 | 3 | A missing tab layout for any windows manager / desktop environment 4 | 5 | 6 | 7 | * [Unique functionality](#unique-functionality) 8 | * [Key bindings](#key-bindings) 9 | * [Requirements](#requirements) 10 | * [Installation](#installation) 11 | * [Patches applied](#patches-applied) 12 | * [Scripts](#scripts) 13 | * [Configuration](#configuration) 14 | * [Rofi compatibility](#rofi-compatibility) 15 | 16 | 17 | 18 | ## Unique functionality 19 | 20 | - `Super-Shift-.` to ***open terminal application*** embeded in tabbed 21 | - `Super-Shift-/` to open new terminal window with the ***chosen terminal's path*** 22 | - `Super-Shift-a` to ***attach window*** from current workplace. 23 | - Require `wmctrl`. 24 | - Require windows manager / desktop environment with `EWMH` (Extended windows manager Hint) 25 | - Should use `-c` argument, i.e., `tabbed -c software -embedarg`. 26 | - `Super-Shift-s` to **attach window** by mouse selection. 27 | - Should use `-c` argument, i.e., `tabbed -c software -embedarg`. 28 | - `Super-Shift-equal` to ***automatically attach all windows*** from current workplace. 29 | - Require `wmctrl`. 30 | - Require windows manager / desktop environment with `EWMH` (Extended windows manager Hint) 31 | - Should use `-c` argument, i.e., `tabbed -c software -embedarg`. 32 | - `Super-Shift-d` to ***detach child window*** from current tabbed window. 33 | - Should use `-c` argument, i.e., `tabbed -c software -embedarg`. 34 | - `Super-Shift-minus` to ***automatically detach all child windows*** from current tabbed window. 35 | - Should use `-c` argument, i.e., `tabbed -c software -embedarg`. 36 | - `Super-Shift-[` to ***hide current tab*** in current tabbed window. 37 | - `Super-Shift-]` to ***show hidden tab*** in current tabbed window by dmenu prompt. 38 | - `Super-Shift-\` to ***show all hidden tabs*** in current tabbed window. 39 | 40 | ## Key bindings 41 | 42 | - `Super-Shift` to show tabs 43 | - `Super-Shift-Return` to open new tab 44 | - `Super-Shift-j` to previous tab 45 | - `Super-Shift-k` to next tab 46 | - `Super-Shift-h` to move selected tab one to the left 47 | - `Super-Shift-l` to move selected tab one to the right 48 | - `Super-Shift-u` to toggle autofocus of urgent tabs 49 | - `Control-Tab` to toggle between the selected and last selected tab 50 | - `Super-Shift-q` to close tab 51 | - `Ctrl-u` to focus next urgent tab 52 | - `Super-Shift-[0..9]` to jumps to nth tab 53 | 54 | ## Requirements 55 | 56 | - Xlib header files to build tabbed 57 | - [dmenu](https://tools.suckless.org/dmenu/) to show prompt 58 | - Or see [Rofi compatibility](#rofi-compatibility) to use `rofi` 59 | - `cut`, `xargs`, `grep`, `pstree`, `sed`, `wmctrl`, `xdotool`, `xprop`, `xwininfo` 60 | 61 | ## Installation 62 | 63 | ```sh 64 | git clone https://github.com/huijunchen9260/tabbed-hjc 65 | cd tabbed-hjc 66 | sudo make install 67 | ``` 68 | 69 | ## Patches applied 70 | 71 | - [alpha.diff](https://tools.suckless.org/tabbed/patches/alpha/) 72 | - [tabbed-clientnumber-0.6.diff](https://tools.suckless.org/tabbed/patches/clientnumber/) 73 | - [tabbed-keyrelease-20191216-b5f9ec6.diff](https://tools.suckless.org/tabbed/patches/keyrelease/) 74 | - [tabbed-0.6-xft.diff](https://tools.suckless.org/tabbed/patches/xft/) 75 | - [tabbed-hidetabs-20191216-b5f9ec6.diff](https://tools.suckless.org/tabbed/patches/hidetabs/) 76 | - [tabbed-icon-20200905-2da4e96.diff](https://tools.suckless.org/tabbed/patches/icon/) (modified by me, see `patches` folder) 77 | 78 | ## Scripts 79 | 80 | 1. `tabbed-slime`: use [vim-slime](https://github.com/jpalardy/vim-slime) with tabbed. 81 | Move `tabbed-slime` to your `$PATH` and add the following code into your `~/.vimrc`: 82 | 83 | ```vimL 84 | let g:slime_target = "x11" 85 | function SlimeOverrideConfig() 86 | let b:slime_config = {"window_id": ""} 87 | let b:slime_config["window_id"] = system("tabbed-slime") 88 | endfunction 89 | ``` 90 | 91 | 2. `tabbedize`: make non-tabbed window tabbed. 92 | 93 | 94 | ## Configuration 95 | 96 | ### Rofi compatibility 97 | 98 | `tabbed-hjc` use `dmenu` by default. If you want to use `rofi`, you can modify `config.def.h` (or `config.h`) by either 99 | 100 | 1. Replacing `dmenu` to `rofi`, with the corresponding arguments. The arguments I use for dmenu is `-i` for case insensitivity, `-l` for vertical prompt, and `-p` for prompt name, or 101 | 2. Simply replacing `dmenu` as `rofi -dmenu`, and leave all the other arguments as it. 102 | -------------------------------------------------------------------------------- /icon.h: -------------------------------------------------------------------------------- 1 | /* GIMP RGBA C-Source image dump (icon.c) */ 2 | 3 | #define ICON_WIDTH (16) 4 | #define ICON_HEIGHT (16) 5 | #define ICON_BYTES_PER_PIXEL (4) /* 2:RGB16, 3:RGB, 4:RGBA */ 6 | #define ICON_COMMENT \ 7 | "GIMP -> Export -> C-Source -> Prefixed name = ICON, Use macros, Save alpha" 8 | #define ICON_PIXEL_DATA ((unsigned char*) ICON_pixel_data) 9 | static const char ICON_pixel_data[16 * 16 * 4 + 1] = 10 | ("\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\000\000\000\000\000\000\000" 11 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000" 12 | "\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 13 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000" 14 | "\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 15 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000" 16 | "\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 17 | "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 18 | "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 19 | "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 20 | "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 21 | "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 22 | "\000\377\000\000\000\377\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000" 23 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000" 24 | "\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 25 | "\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 26 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377" 27 | "\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 28 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000" 29 | "\000\000\377\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 30 | "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 31 | "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 32 | "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 33 | "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 34 | "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 35 | "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000" 36 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 37 | "\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\001\000" 38 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000" 39 | "\000\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 40 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\000\000\000\000" 41 | "\000\000\000\000\000\000\000\000\377\000\000\000\377"); 42 | -------------------------------------------------------------------------------- /tabbed.1: -------------------------------------------------------------------------------- 1 | .TH TABBED\-HJC 1 tabbed\-hjc\-VERSION 2 | .SH NAME 3 | tabbed\-hjc \- generic tabbed interface (Hui-Jun Chen (https://huijunchen9260.github.io/)'s build) 4 | .SH SYNOPSIS 5 | .B tabbed 6 | .RB [ \-c ] 7 | .RB [ \-d ] 8 | .RB [ \-k ] 9 | .RB [ \-s ] 10 | .RB [ \-v ] 11 | .RB [ \-g 12 | .IR geometry ] 13 | .RB [ \-n 14 | .IR name ] 15 | .RB [ \-p 16 | .RB [ s {+/-} ] \fIpos\fR ] 17 | .RB [ \-o 18 | .IR normbgcol ] 19 | .RB [ \-O 20 | .IR normfgcol ] 21 | .RB [ \-t 22 | .IR selbgcol ] 23 | .RB [ \-T 24 | .IR selfgcol ] 25 | .RB [ \-u 26 | .IR urgbgcol ] 27 | .RB [ \-U 28 | .IR urgfgcol ] 29 | .RB [ \-r 30 | .IR narg ] 31 | .RI [ "command ..." ] 32 | .SH DESCRIPTION 33 | .B tabbed 34 | is a simple tabbed container for applications which support XEmbed. Tabbed 35 | will then run the provided command with the xid of tabbed as appended 36 | argument. (See EXAMPLES.) The automatic spawning of the command can be 37 | disabled by providing the -s parameter. If no command is provided 38 | tabbed will just print its xid and run no command. 39 | .SH OPTIONS 40 | .TP 41 | .B \-c 42 | close tabbed when the last tab is closed. Mutually exclusive with -f. 43 | .TP 44 | .B \-d 45 | detaches tabbed from the terminal and prints its XID to stdout. 46 | .TP 47 | .B \-f 48 | fill up tabbed again by spawning the provided command, when the last tab is 49 | closed. Mutually exclusive with -c. 50 | .TP 51 | .BI \-g " geometry" 52 | defines the X11 geometry string, which will fixate the height and width of 53 | tabbed. 54 | The syntax is 55 | .RI [=][ width {xX} height ][{+-} xoffset {+-} yoffset ]. 56 | See 57 | .BR XParseGeometry (3) 58 | for further details. 59 | .TP 60 | .B \-k 61 | close foreground tabbed client (instead of tabbed and all clients) when 62 | WM_DELETE_WINDOW is sent. 63 | .TP 64 | .BI \-n " name" 65 | will set the WM_CLASS attribute to 66 | .I name. 67 | .TP 68 | .BR \-p " [" s {+-}] \fIpos\fR 69 | will set the absolute or relative position of where to start a new tab. When 70 | .I pos 71 | is is given without 's' in front it is an absolute position. Then negative 72 | numbers will be the position from the last tab, where -1 is the last tab. 73 | If 's' is given, then 74 | .I pos 75 | is a relative position to the current selected tab. If this reaches the limits 76 | of the tabs; those limits then apply. 77 | .TP 78 | .BI \-r " narg" 79 | will replace the 80 | .I narg 81 | th argument in 82 | .I command 83 | with the window id, rather than appending it to the end. 84 | .TP 85 | .B \-s 86 | will disable automatic spawning of the command. 87 | .TP 88 | .BI \-o " normbgcol" 89 | defines the normal background color. 90 | .RI # RGB , 91 | .RI # RRGGBB , 92 | and X color names are supported. 93 | .TP 94 | .BI \-O " normfgcol" 95 | defines the normal foreground color. 96 | .TP 97 | .BI \-t " selbgcol" 98 | defines the selected background color. 99 | .TP 100 | .BI \-T " selfgbcol" 101 | defines the selected foreground color. 102 | .TP 103 | .BI \-u " urgbgcol" 104 | defines the urgent background color. 105 | .TP 106 | .BI \-U " urgfgbcol" 107 | defines the urgent foreground color. 108 | .TP 109 | .B \-v 110 | prints version information to stderr, then exits. 111 | .SH USAGE 112 | .TP 113 | .B Super\-Shift 114 | show tabs 115 | .TP 116 | .B Super\-Shift\-Return 117 | open new tab 118 | .TP 119 | .B Super\-Shift\-a 120 | attach window in current workspace. Require 121 | .BR wmctrl 122 | and windows manager / desktop environment supporting 123 | .BR EWMH 124 | to list windows in current workspace. Make sure you use 125 | .B -c 126 | argument for tabbed, i.e., 127 | .B tabbed -c software -embedarg 128 | for every software. 129 | .TP 130 | .B Super\-Shift\-s 131 | attach window by mouse selection. 132 | .TP 133 | .B Super\-Shift\-plus 134 | attach all windows in current workspace. Require 135 | .BR wmctrl 136 | and windows manager / desktop environment supporting 137 | .BR EWMH 138 | to list windows in current workspace. Make sure you use 139 | .B -c 140 | argument for tabbed, i.e., 141 | .B tabbed -c software -embedarg 142 | for every software. 143 | .TP 144 | .B Super\-Shift\-d 145 | detach child window from current tabbed window. Make sure you use 146 | .B -c 147 | argument for tabbed, i.e., 148 | .B tabbed -c software -embedarg 149 | for every software. 150 | .TP 151 | .B Super\-Shift\-minus 152 | detach all child windows from current tabbed window. Make sure you use 153 | .B -c 154 | argument for tabbed, i.e., 155 | .B tabbed -c software -embedarg 156 | for every software. 157 | .TP 158 | .B Super\-Shift\-[ 159 | hide current tab in current tabbed window. 160 | .TP 161 | .B Super\-Shift\-] 162 | show hidden tab in current tabbed window by dmenu prompt. 163 | .TP 164 | .B Super\-Shift\-backslash 165 | show all hidden tabs in current tabbed window. 166 | .TP 167 | .B Super\-Shift\-, 168 | open dmenu to either create a new tab appending the entered string or select 169 | an already existing tab. 170 | .TP 171 | .B Super\-Shift\-. 172 | open dmenu to open terminal software. Modify function 173 | .BR OPENTERMSOFT 174 | in 175 | .BR config.h 176 | to fit your software and terminal 177 | .TP 178 | .B Super\-Shift\-slash 179 | open new terminal with $PWD based on existing tabs. Modify function 180 | .BR OPENTERM 181 | in 182 | .BR config.h 183 | to fit your terminal 184 | .TP 185 | .B Super\-Shift\-j 186 | previous tab 187 | .TP 188 | .B Super\-Shift\-k 189 | next tab 190 | .TP 191 | .B Super\-Shift\-h 192 | move selected tab one to the left 193 | .TP 194 | .B Super\-Shift\-l 195 | move selected tab one to the right 196 | .TP 197 | .B Super\-Shift\-u 198 | toggle autofocus of urgent tabs 199 | .TP 200 | .B Control\-Tab 201 | toggle between the selected and last selected tab 202 | .TP 203 | .B Super\-Shift\-q 204 | close tab 205 | .TP 206 | .B Ctrl\-u 207 | focus next urgent tab 208 | .TP 209 | .B Super\-Shift\-[0..9] 210 | jumps to nth tab 211 | .TP 212 | .B F11 213 | Toggle fullscreen mode. 214 | .SH EXAMPLES 215 | $ tabbed surf -e 216 | .TP 217 | $ tabbed urxvt -embed 218 | .TP 219 | $ tabbed xterm -into 220 | .TP 221 | $ $(tabbed -d >/tmp/tabbed.xid); urxvt -embed $( 35 | #include 36 | #include 37 | +#include 38 | 39 | #include "arg.h" 40 | 41 | @@ -64,16 +65,15 @@ typedef struct { 42 | 43 | typedef struct { 44 | int x, y, w, h; 45 | - unsigned long norm[ColLast]; 46 | - unsigned long sel[ColLast]; 47 | + XftColor norm[ColLast]; 48 | + XftColor sel[ColLast]; 49 | Drawable drawable; 50 | GC gc; 51 | struct { 52 | int ascent; 53 | int descent; 54 | int height; 55 | - XFontSet set; 56 | - XFontStruct *xfont; 57 | + XftFont *xfont; 58 | } font; 59 | } DC; /* draw context */ 60 | 61 | @@ -95,7 +95,7 @@ static void createnotify(const XEvent *e); 62 | static void destroynotify(const XEvent *e); 63 | static void die(const char *errstr, ...); 64 | static void drawbar(void); 65 | -static void drawtext(const char *text, unsigned long col[ColLast]); 66 | +static void drawtext(const char *text, XftColor col[ColLast]); 67 | static void *emallocz(size_t size); 68 | static void *erealloc(void *o, size_t size); 69 | static void expose(const XEvent *e); 70 | @@ -105,7 +105,7 @@ static void focusonce(const Arg *arg); 71 | static void fullscreen(const Arg *arg); 72 | static char* getatom(int a); 73 | static int getclient(Window w); 74 | -static unsigned long getcolor(const char *colstr); 75 | +static XftColor getcolor(const char *colstr); 76 | static int getfirsttab(void); 77 | static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); 78 | static void initfont(const char *fontstr); 79 | @@ -219,12 +219,6 @@ cleanup(void) { 80 | free(clients); 81 | clients = NULL; 82 | 83 | - if(dc.font.set) { 84 | - XFreeFontSet(dpy, dc.font.set); 85 | - } else { 86 | - XFreeFont(dpy, dc.font.xfont); 87 | - } 88 | - 89 | XFreePixmap(dpy, dc.drawable); 90 | XFreeGC(dpy, dc.gc); 91 | XDestroyWindow(dpy, win); 92 | @@ -305,7 +299,7 @@ die(const char *errstr, ...) { 93 | 94 | void 95 | drawbar(void) { 96 | - unsigned long *col; 97 | + XftColor *col; 98 | int c, fc, width, n = 0; 99 | char *name = NULL; 100 | 101 | @@ -362,12 +356,13 @@ drawbar(void) { 102 | } 103 | 104 | void 105 | -drawtext(const char *text, unsigned long col[ColLast]) { 106 | +drawtext(const char *text, XftColor col[ColLast]) { 107 | int i, x, y, h, len, olen; 108 | char buf[256]; 109 | + XftDraw *d; 110 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; 111 | 112 | - XSetForeground(dpy, dc.gc, col[ColBG]); 113 | + XSetForeground(dpy, dc.gc, col[ColBG].pixel); 114 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); 115 | if(!text) 116 | return; 117 | @@ -388,13 +383,10 @@ drawtext(const char *text, unsigned long col[ColLast]) { 118 | for(i = len; i && i > len - 3; buf[--i] = '.'); 119 | } 120 | 121 | - XSetForeground(dpy, dc.gc, col[ColFG]); 122 | - if(dc.font.set) { 123 | - XmbDrawString(dpy, dc.drawable, dc.font.set, 124 | - dc.gc, x, y, buf, len); 125 | - } else { 126 | - XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); 127 | - } 128 | + d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen)); 129 | + 130 | + XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len); 131 | + XftDrawDestroy(d); 132 | } 133 | 134 | void * 135 | @@ -524,15 +516,14 @@ getclient(Window w) { 136 | return -1; 137 | } 138 | 139 | -unsigned long 140 | +XftColor 141 | getcolor(const char *colstr) { 142 | - Colormap cmap = DefaultColormap(dpy, screen); 143 | - XColor color; 144 | + XftColor color; 145 | 146 | - if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) 147 | + if(!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color)) 148 | die("tabbed: cannot allocate color '%s'\n", colstr); 149 | 150 | - return color.pixel; 151 | + return color; 152 | } 153 | 154 | int 155 | @@ -585,41 +576,12 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) { 156 | 157 | void 158 | initfont(const char *fontstr) { 159 | - char *def, **missing, **font_names; 160 | - int i, n; 161 | - XFontStruct **xfonts; 162 | - 163 | - missing = NULL; 164 | - if(dc.font.set) 165 | - XFreeFontSet(dpy, dc.font.set); 166 | - 167 | - dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); 168 | - if(missing) { 169 | - while(n--) 170 | - fprintf(stderr, "tabbed: missing fontset: %s\n", missing[n]); 171 | - XFreeStringList(missing); 172 | - } 173 | + if(!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr)) 174 | + && !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed"))) 175 | + die("error, cannot load font: '%s'\n", fontstr); 176 | 177 | - if(dc.font.set) { 178 | - dc.font.ascent = dc.font.descent = 0; 179 | - n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); 180 | - for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { 181 | - dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent); 182 | - dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent); 183 | - xfonts++; 184 | - } 185 | - } else { 186 | - if(dc.font.xfont) 187 | - XFreeFont(dpy, dc.font.xfont); 188 | - dc.font.xfont = NULL; 189 | - if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)) 190 | - && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed"))) { 191 | - die("tabbed: cannot load font: '%s'\n", fontstr); 192 | - } 193 | - 194 | - dc.font.ascent = dc.font.xfont->ascent; 195 | - dc.font.descent = dc.font.xfont->descent; 196 | - } 197 | + dc.font.ascent = dc.font.xfont->ascent; 198 | + dc.font.descent = dc.font.xfont->descent; 199 | dc.font.height = dc.font.ascent + dc.font.descent; 200 | } 201 | 202 | @@ -972,11 +934,9 @@ setup(void) { 203 | dc.drawable = XCreatePixmap(dpy, root, ww, wh, 204 | DefaultDepth(dpy, screen)); 205 | dc.gc = XCreateGC(dpy, root, 0, 0); 206 | - if(!dc.font.set) 207 | - XSetFont(dpy, dc.gc, dc.font.xfont->fid); 208 | 209 | win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0, 210 | - dc.norm[ColFG], dc.norm[ColBG]); 211 | + dc.norm[ColFG].pixel, dc.norm[ColBG].pixel); 212 | XMapRaised(dpy, win); 213 | XSelectInput(dpy, win, SubstructureNotifyMask|FocusChangeMask| 214 | ButtonPressMask|ExposureMask|KeyPressMask|PropertyChangeMask| 215 | @@ -1040,15 +1000,9 @@ spawn(const Arg *arg) { 216 | 217 | int 218 | textnw(const char *text, unsigned int len) { 219 | - XRectangle r; 220 | - 221 | - if(dc.font.set) { 222 | - XmbTextExtents(dc.font.set, text, len, NULL, &r); 223 | - 224 | - return r.width; 225 | - } 226 | - 227 | - return XTextWidth(dc.font.xfont, text, len); 228 | + XGlyphInfo ext; 229 | + XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext); 230 | + return ext.xOff; 231 | } 232 | 233 | void 234 | -------------------------------------------------------------------------------- /patches/tabbed-icon-20200905-2da4e96.diff: -------------------------------------------------------------------------------- 1 | diff --git a/Makefile b/Makefile 2 | index 1b95d15..e571818 100644 3 | --- a/Makefile 4 | +++ b/Makefile 5 | @@ -37,7 +37,7 @@ dist: clean 6 | @echo creating dist tarball 7 | @mkdir -p tabbed-${VERSION} 8 | @cp -R LICENSE Makefile README config.def.h config.mk \ 9 | - tabbed.1 arg.h ${SRC} tabbed-${VERSION} 10 | + tabbed.1 arg.h icon.h ${SRC} tabbed-${VERSION} 11 | @tar -cf tabbed-${VERSION}.tar tabbed-${VERSION} 12 | @gzip tabbed-${VERSION}.tar 13 | @rm -rf tabbed-${VERSION} 14 | diff --git a/TODO b/TODO 15 | index 8e1986d..dbcb930 100644 16 | --- a/TODO 17 | +++ b/TODO 18 | @@ -1,4 +1,3 @@ 19 | # TODO 20 | * add some way to detach windows 21 | * add some way to attach windows 22 | - 23 | diff --git a/icon.h b/icon.h 24 | new file mode 100644 25 | index 0000000..e2ef631 26 | --- /dev/null 27 | +++ b/icon.h 28 | @@ -0,0 +1,41 @@ 29 | +/* GIMP RGBA C-Source image dump (icon.c) */ 30 | + 31 | +#define ICON_WIDTH (16) 32 | +#define ICON_HEIGHT (16) 33 | +#define ICON_BYTES_PER_PIXEL (4) /* 2:RGB16, 3:RGB, 4:RGBA */ 34 | +#define ICON_COMMENT \ 35 | + "GIMP -> Export -> C-Source -> Prefixed name = ICON, Use macros, Save alpha" 36 | +#define ICON_PIXEL_DATA ((unsigned char*) ICON_pixel_data) 37 | +static const char ICON_pixel_data[16 * 16 * 4 + 1] = 38 | +("\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\000\000\000\000\000\000\000" 39 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000" 40 | + "\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 41 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000" 42 | + "\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 43 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000" 44 | + "\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 45 | + "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 46 | + "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 47 | + "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 48 | + "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 49 | + "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 50 | + "\000\377\000\000\000\377\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000" 51 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000" 52 | + "\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 53 | + "\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 54 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377" 55 | + "\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 56 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000" 57 | + "\000\000\377\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 58 | + "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 59 | + "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 60 | + "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 61 | + "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377" 62 | + "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000" 63 | + "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000" 64 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 65 | + "\000\000\377\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\001\000" 66 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000" 67 | + "\000\000\377\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 68 | + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\000\000\000\000\000\000\000" 69 | + "\000\000\000\000\000\000\000\000\377\000\000\000\377"); 70 | diff --git a/tabbed.c b/tabbed.c 71 | index eafe28a..ec6212e 100644 72 | --- a/tabbed.c 73 | +++ b/tabbed.c 74 | @@ -18,6 +18,7 @@ 75 | #include 76 | 77 | #include "arg.h" 78 | +#include "icon.h" 79 | 80 | /* XEMBED messages */ 81 | #define XEMBED_EMBEDDED_NOTIFY 0 82 | @@ -49,7 +50,7 @@ 83 | 84 | enum { ColFG, ColBG, ColLast }; /* color */ 85 | enum { WMProtocols, WMDelete, WMName, WMState, WMFullscreen, 86 | - XEmbed, WMSelectTab, WMLast }; /* default atoms */ 87 | + XEmbed, WMSelectTab, WMIcon, WMLast }; /* default atoms */ 88 | 89 | typedef union { 90 | int i; 91 | @@ -135,6 +136,7 @@ static void updatenumlockmask(void); 92 | static void updatetitle(int c); 93 | static int xerror(Display *dpy, XErrorEvent *ee); 94 | static void xsettitle(Window w, const char *str); 95 | +static void xseticon(void); 96 | 97 | /* variables */ 98 | static int screen; 99 | @@ -169,6 +171,7 @@ static char winid[64]; 100 | static char **cmd; 101 | static char *wmname = "tabbed"; 102 | static const char *geometry; 103 | +static unsigned long icon[ICON_WIDTH * ICON_HEIGHT + 2]; 104 | 105 | char *argv0; 106 | 107 | @@ -455,6 +458,8 @@ focus(int c) 108 | n += snprintf(&buf[n], sizeof(buf) - n, " %s", cmd[i]); 109 | 110 | xsettitle(win, buf); 111 | + XChangeProperty(dpy, win, wmatom[WMIcon], XA_CARDINAL, 32, 112 | + PropModeReplace, (unsigned char *) icon, ICON_WIDTH * ICON_HEIGHT + 2); 113 | XRaiseWindow(dpy, win); 114 | 115 | return; 116 | @@ -474,6 +479,7 @@ focus(int c) 117 | lastsel = sel; 118 | sel = c; 119 | } 120 | + xseticon(); 121 | 122 | if (clients[c]->urgent && (wmh = XGetWMHints(dpy, clients[c]->win))) { 123 | wmh->flags &= ~XUrgencyHint; 124 | @@ -868,9 +874,13 @@ propertynotify(const XEvent *e) 125 | } 126 | } 127 | XFree(wmh); 128 | + if (c == sel) 129 | + xseticon(); 130 | } else if (ev->state != PropertyDelete && ev->atom == XA_WM_NAME && 131 | (c = getclient(ev->window)) > -1) { 132 | updatetitle(c); 133 | + } else if (ev->atom == wmatom[WMIcon] && (c = getclient(ev->window)) > -1 && c == sel) { 134 | + xseticon(); 135 | } 136 | } 137 | 138 | @@ -995,6 +1005,7 @@ setup(void) 139 | wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False); 140 | wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 141 | wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False); 142 | + wmatom[WMIcon] = XInternAtom(dpy, "_NET_WM_ICON", False); 143 | 144 | /* init appearance */ 145 | wx = 0; 146 | @@ -1074,6 +1085,17 @@ setup(void) 147 | snprintf(winid, sizeof(winid), "%lu", win); 148 | setenv("XEMBED", winid, 1); 149 | 150 | + /* change icon from RGBA to ARGB */ 151 | + icon[0] = ICON_WIDTH; 152 | + icon[1] = ICON_HEIGHT; 153 | + for (int i = 0; i < ICON_WIDTH * ICON_HEIGHT; ++i) { 154 | + icon[i + 2] = 155 | + ICON_PIXEL_DATA[i * 4 + 3] << 24 | 156 | + ICON_PIXEL_DATA[i * 4 + 0] << 0 | 157 | + ICON_PIXEL_DATA[i * 4 + 1] << 8 | 158 | + ICON_PIXEL_DATA[i * 4 + 2] << 16 ; 159 | + } 160 | + 161 | nextfocus = foreground; 162 | focus(-1); 163 | } 164 | @@ -1265,6 +1287,46 @@ xsettitle(Window w, const char *str) 165 | } 166 | } 167 | 168 | +void 169 | +xseticon(void) 170 | +{ 171 | + Atom ret_type; 172 | + XWMHints *wmh, *cwmh; 173 | + int ret_format; 174 | + unsigned long ret_nitems, ret_nleft; 175 | + long offset = 0L; 176 | + unsigned char *data; 177 | + 178 | + wmh = XGetWMHints(dpy, win); 179 | + wmh->flags &= ~(IconPixmapHint | IconMaskHint); 180 | + wmh->icon_pixmap = wmh->icon_mask = None; 181 | + 182 | + 183 | + if (XGetWindowProperty(dpy, clients[sel]->win, wmatom[WMIcon], offset, LONG_MAX, False, 184 | + XA_CARDINAL, &ret_type, &ret_format, &ret_nitems, 185 | + &ret_nleft, &data) == Success && 186 | + ret_type == XA_CARDINAL && ret_format == 32) 187 | + { 188 | + XChangeProperty(dpy, win, wmatom[WMIcon], XA_CARDINAL, 32, 189 | + PropModeReplace, data, ret_nitems); 190 | + } else if ((cwmh = XGetWMHints(dpy, clients[sel]->win)) && cwmh->flags & IconPixmapHint) { 191 | + XDeleteProperty(dpy, win, wmatom[WMIcon]); 192 | + wmh->flags |= IconPixmapHint; 193 | + wmh->icon_pixmap = cwmh->icon_pixmap; 194 | + if (cwmh->flags & IconMaskHint) { 195 | + wmh->flags |= IconMaskHint; 196 | + wmh->icon_mask = cwmh->icon_mask; 197 | + } 198 | + XFree(cwmh); 199 | + } else { 200 | + XChangeProperty(dpy, win, wmatom[WMIcon], XA_CARDINAL, 32, 201 | + PropModeReplace, (unsigned char *) icon, ICON_WIDTH * ICON_HEIGHT + 2); 202 | + } 203 | + XSetWMHints(dpy, win, wmh); 204 | + XFree(wmh); 205 | + XFree(data); 206 | +} 207 | + 208 | void 209 | usage(void) 210 | { 211 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | 3 | /* appearance */ 4 | static const char font[] = "monospace:size=14"; 5 | static const char* normbgcolor = "#222222"; 6 | static const char* normfgcolor = "#cccccc"; 7 | static const char* selbgcolor = "#555555"; 8 | static const char* selfgcolor = "#ffffff"; 9 | static const char* urgbgcolor = "#111111"; 10 | static const char* urgfgcolor = "#cc0000"; 11 | static const char before[] = "<"; 12 | static const char after[] = ">"; 13 | static const char titletrim[] = "..."; 14 | static const int tabwidth = 200; 15 | static const Bool foreground = True; 16 | static Bool urgentswitch = True; 17 | 18 | /* 19 | * Where to place a new tab when it is opened. When npisrelative is True, 20 | * then the current position is changed + newposition. If npisrelative 21 | * is False, then newposition is an absolute position. 22 | */ 23 | static int newposition = 1; 24 | static Bool npisrelative = True; 25 | 26 | #define SETPROP(p) { \ 27 | .v = (char *[]){ "/bin/sh", "-c", \ 28 | "prop=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 29 | "sed -e's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@' |" \ 30 | "tail -n +2 | dmenu -i -l 10 -p 'Switch to: ')\" &&" \ 31 | "xprop -id $1 -f $0 8s -set $0 \"$prop\"", \ 32 | p, winid, NULL \ 33 | } \ 34 | } 35 | 36 | /* Modify the following line to match your terminal and software list */ 37 | #define OPENTERMSOFT(p) { \ 38 | .v = (char *[]){ "/bin/sh", "-c", \ 39 | "term='alacritty' && titlearg='-t' && embedarg='--embed' &&" \ 40 | "softlist=$(printf '%s\n' \"htop\" \"ncdu\" \"nvim\" \"fzf\" \"nnn\" \"ncmpcpp\" \"nmtui\") &&" \ 41 | "printf '%s' \"$softlist\" |" \ 42 | "dmenu -i -p 'Softwares to run: ' |" \ 43 | "xargs -I {} $term $titlearg \"{}\" $embedarg $1 -e \"{}\"", \ 44 | p, winid, NULL \ 45 | } \ 46 | } 47 | 48 | 49 | /* Modify the following line to match your terminal*/ 50 | #define OPENTERM(p) { \ 51 | .v = (char *[]){ "/bin/sh", "-c", \ 52 | "term='alacritty' && embedarg='--embed' &&" \ 53 | "cd \"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 54 | "sed -e's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@' |" \ 55 | "dmenu -i -l 10 -p 'New term path based on: ' |" \ 56 | "cut -f 1 | xargs -I {} xprop -id \"{}\" | grep _NET_WM_PID |" \ 57 | "cut -d ' ' -f 3 | xargs -I {} pstree -p \"{}\" |" \ 58 | "cut -d '(' -f 3 | cut -d ')' -f 1 |" \ 59 | "xargs -I {} readlink -e /proc/\"{}\"/cwd/)\" &&" \ 60 | "$term $embedarg $1", \ 61 | p, winid, NULL \ 62 | } \ 63 | } 64 | 65 | /* deskid: id for current workspace */ 66 | /* rootid: id for root window */ 67 | /* window: data for chosen window by dmenu */ 68 | /* wid: chosen window's window id */ 69 | /* wname: chosen window's name */ 70 | /* cwid: chosen window's child window id (tabbed window only) */ 71 | #define ATTACHWIN(p) { \ 72 | .v = (char *[]){ "/bin/sh", "-c", \ 73 | "deskid=$(xdotool get_desktop) &&" \ 74 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 75 | "window=\"$(wmctrl -x -l | grep -E \" $deskid \" |" \ 76 | "grep -v $(printf '0x0%x' \"$1\") |" \ 77 | "cut -d ' ' -f 1,4 | dmenu -i -l 5 -p \"Attach: \")\" &&" \ 78 | "wid=$(printf '%s' \"$window\" | cut -d ' ' -f 1) &&" \ 79 | "wname=$(printf '%s' \"$window\" | cut -d ' ' -f 2) &&" \ 80 | "[ \"$wname\" = \"tabbed.tabbed\" ] &&" \ 81 | "cwid=$(xwininfo -children -id \"$wid\" | grep '^ 0x' |" \ 82 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@') &&" \ 83 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$rootid\"; done &&" \ 84 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$1\"; done ||" \ 85 | "xdotool windowreparent \"$wid\" $1", \ 86 | p, winid, NULL \ 87 | } \ 88 | } 89 | 90 | #define ATTACHSELECTWIN(p) { \ 91 | .v = (char *[]){ "/bin/sh", "-c", \ 92 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 93 | "wid=$(xdotool selectwindow) &&" \ 94 | "wname=$(xwininfo -id \"$wid\" | grep 'Window id:' | cut -d ' ' -f 5-) &&" \ 95 | "[ \"$wname\" = \"(has no name)\" ] &&" \ 96 | "cwid=$(xwininfo -children -id \"$wid\" | grep '^ 0x' |" \ 97 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@') &&" \ 98 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$rootid\"; done &&" \ 99 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$1\"; done ||" \ 100 | "xdotool windowreparent \"$wid\" $1", \ 101 | p, winid, NULL \ 102 | } \ 103 | } 104 | 105 | #define ATTACHALL(p) { \ 106 | .v = (char *[]){ "/bin/sh", "-c", \ 107 | "deskid=$(xdotool get_desktop) &&" \ 108 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 109 | "window=\"$(wmctrl -x -l | grep -E \" $deskid \" |" \ 110 | "grep -v $(printf '0x0%x' \"$1\") | cut -d ' ' -f 1,4)\" &&" \ 111 | "IFS=':' &&" \ 112 | "for win in $(printf '%s' \"$window\" | tr '\n' ':'); do unset IFS &&" \ 113 | "wid=$(printf '%s' \"$win\" | cut -d ' ' -f 1) &&" \ 114 | "wname=$(printf '%s' \"$win\" | cut -d ' ' -f 2) &&" \ 115 | "[ \"$wname\" = \"tabbed.tabbed\" ] &&" \ 116 | "{ cwid=$(xwininfo -children -id \"$wid\" | grep '^ 0x' |" \ 117 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@') &&" \ 118 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$rootid\"; done &&" \ 119 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$1\"; done; } ||" \ 120 | "xdotool windowreparent \"$wid\" $1; done", \ 121 | p, winid, NULL \ 122 | } \ 123 | } 124 | 125 | 126 | #define DETACHWIN(p) { \ 127 | .v = (char *[]){ "/bin/sh", "-c", \ 128 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 129 | "wid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 130 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@' |" \ 131 | "dmenu -i -l 5 -p 'Detach: ' | cut -d ' ' -f 1)\" &&" \ 132 | "xwininfo -id $wid -stats | grep -q 'IsUnMapped' && xdotool windowmap $wid;" \ 133 | "xdotool windowreparent \"$wid\" \"$rootid\" &&" \ 134 | "xdotool windowactivate $1", \ 135 | p, winid, NULL \ 136 | } \ 137 | } 138 | 139 | 140 | 141 | #define DETACHALL(p) { \ 142 | .v = (char *[]){ "/bin/sh", "-c", \ 143 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 144 | "wid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 145 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@')\" &&" \ 146 | "IFS=':' &&" \ 147 | "for id in $(printf '%s' \"$wid\" | tr '\n' ':'); do unset IFS &&" \ 148 | "xdotool windowreparent \"$id\" \"$rootid\" &&" \ 149 | "xwininfo -id $id -stats |" \ 150 | "grep -q 'IsUnMapped' &&" \ 151 | "xdotool windowmap $id; done", \ 152 | p, winid, NULL \ 153 | } \ 154 | } 155 | 156 | 157 | 158 | #define SHOWHIDDEN(p) { \ 159 | .v = (char *[]){ "/bin/sh", "-c", \ 160 | "cwin=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 161 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@')\" &&" \ 162 | "IFS=':' &&" \ 163 | "for win in $(printf '%s' \"$cwin\" | tr '\n' ':'); do unset IFS &&" \ 164 | "cwid=$(printf '%s' \"$win\" | cut -d ' ' -f 1) &&" \ 165 | "xwininfo -id $cwid -stats |" \ 166 | "grep -q 'IsUnMapped' &&" \ 167 | "printf '%s\n' \"$win\"; done |" \ 168 | "dmenu -i -l 5 -p \"Show hidden window:\" |" \ 169 | "cut -d ' ' -f 1 |" \ 170 | "xargs -I {} xdotool windowmap \"{}\"", \ 171 | p, winid, NULL \ 172 | } \ 173 | } 174 | 175 | #define SHOWHIDDENALL(p) { \ 176 | .v = (char *[]){ "/bin/sh", "-c", \ 177 | "cwid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 178 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@')\" &&" \ 179 | "IFS=':' &&" \ 180 | "for id in $(printf '%s' \"$cwid\" | tr '\n' ':'); do unset IFS &&" \ 181 | "xwininfo -id $id -stats | " \ 182 | "grep -q 'IsUnMapped' &&" \ 183 | "xdotool windowmap $id; done", \ 184 | p, winid, NULL \ 185 | } \ 186 | } 187 | 188 | #define HIDEWINDOW(p) { \ 189 | .v = (char *[]){ "/bin/sh", "-c", \ 190 | "cwid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 191 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@')\" &&" \ 192 | "IFS=':' && winnum=0 &&" \ 193 | "for id in $(printf '%s' \"$cwid\" | tr '\n' ':'); do unset IFS &&" \ 194 | "xwininfo -id $id -stats | " \ 195 | "grep -q 'IsViewable' &&" \ 196 | "winnum=$(($winnum+1)); done;" \ 197 | "[ $winnum -gt 1 ] &&" \ 198 | "{ xwininfo -children -id $1 | grep '^ 0x' | head -n 1 |" \ 199 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@' |" \ 200 | "xargs -I {} xdotool windowunmap \"{}\"; }", \ 201 | p, winid, NULL \ 202 | } \ 203 | } 204 | 205 | 206 | 207 | #define MODKEY Mod4Mask 208 | static Key keys[] = { 209 | /* modifier key function argument */ 210 | { MODKEY|ShiftMask, XK_Return, focusonce, { 0 } }, 211 | { MODKEY|ShiftMask, XK_Return, spawn, { 0 } }, 212 | 213 | { MODKEY|ShiftMask, XK_k, rotate, { .i = +1 } }, 214 | { MODKEY|ShiftMask, XK_j, rotate, { .i = -1 } }, 215 | { MODKEY|ShiftMask, XK_h, movetab, { .i = -1 } }, 216 | { MODKEY|ShiftMask, XK_l, movetab, { .i = +1 } }, 217 | { ControlMask, XK_Tab, rotate, { .i = 0 } }, 218 | 219 | { MODKEY|ShiftMask, XK_1, move, { .i = 0 } }, 220 | { MODKEY|ShiftMask, XK_2, move, { .i = 1 } }, 221 | { MODKEY|ShiftMask, XK_3, move, { .i = 2 } }, 222 | { MODKEY|ShiftMask, XK_4, move, { .i = 3 } }, 223 | { MODKEY|ShiftMask, XK_5, move, { .i = 4 } }, 224 | { MODKEY|ShiftMask, XK_6, move, { .i = 5 } }, 225 | { MODKEY|ShiftMask, XK_7, move, { .i = 6 } }, 226 | { MODKEY|ShiftMask, XK_8, move, { .i = 7 } }, 227 | { MODKEY|ShiftMask, XK_9, move, { .i = 8 } }, 228 | { MODKEY|ShiftMask, XK_0, move, { .i = 9 } }, 229 | 230 | { MODKEY|ShiftMask, XK_q, killclient, { 0 } }, 231 | 232 | { MODKEY, XK_u, focusurgent, { 0 } }, 233 | { MODKEY|ShiftMask, XK_u, toggle, { .v = (void*) &urgentswitch } }, 234 | 235 | { 0, XK_F11, fullscreen, { 0 } }, 236 | 237 | { MODKEY|ShiftMask, XK_comma, spawn, SETPROP("_TABBED_SELECT_TAB") }, 238 | 239 | { MODKEY, XK_Shift_L, showbar, { .i = 1 } }, 240 | { ShiftMask, XK_Super_L, showbar, { .i = 1 } }, 241 | { ControlMask, XK_comma, showbar, { .i = 1 } }, 242 | 243 | /* Unique functionality */ 244 | { MODKEY|ShiftMask, XK_period, spawn, OPENTERMSOFT("_TABBED_SELECT_TERMAPP") }, 245 | { MODKEY|ShiftMask, XK_slash, spawn, OPENTERM("_TABBED_TERM") }, 246 | { MODKEY|ShiftMask, XK_a, spawn, ATTACHWIN("_TABBED_ATTACH_WIN") }, 247 | { MODKEY|ShiftMask, XK_s, spawn, ATTACHSELECTWIN("_TABBED_ATTACH_WIN") }, 248 | { MODKEY|ShiftMask, XK_equal, spawn, ATTACHALL("_TABBED_ATTACH_ALL") }, 249 | { MODKEY|ShiftMask, XK_d, spawn, DETACHWIN("_TABBED_DETACH_WIN") }, 250 | { MODKEY|ShiftMask, XK_minus, spawn, DETACHALL("_TABBED_DETACH_ALL") }, 251 | { MODKEY|ShiftMask, XK_bracketleft, spawn, HIDEWINDOW("_TABBED_HIDE_WINDOW") }, 252 | { MODKEY|ShiftMask, XK_bracketright, spawn, SHOWHIDDEN("_TABBED_SHOW_HIDDEN") }, 253 | { MODKEY|ShiftMask, XK_backslash, spawn, SHOWHIDDENALL("_TABBED_SHOW_HIDDEN_ALL") }, 254 | }; 255 | 256 | static Key keyreleases[] = { 257 | /* modifier key function argument */ 258 | { MODKEY|ShiftMask, XK_Shift_L, showbar, { .i = 0 } }, 259 | { MODKEY|ShiftMask, XK_Super_L, showbar, { .i = 0 } }, 260 | }; 261 | -------------------------------------------------------------------------------- /config.def.h: -------------------------------------------------------------------------------- 1 | /* See LICENSE file for copyright and license details. */ 2 | 3 | /* appearance */ 4 | static const char font[] = "monospace:size=14"; 5 | static const char* normbgcolor = "#222222"; 6 | static const char* normfgcolor = "#cccccc"; 7 | static const char* selbgcolor = "#555555"; 8 | static const char* selfgcolor = "#ffffff"; 9 | static const char* urgbgcolor = "#111111"; 10 | static const char* urgfgcolor = "#cc0000"; 11 | static const char before[] = "<"; 12 | static const char after[] = ">"; 13 | static const char titletrim[] = "..."; 14 | static const int tabwidth = 200; 15 | static const Bool foreground = True; 16 | static Bool urgentswitch = True; 17 | 18 | /* 19 | * Where to place a new tab when it is opened. When npisrelative is True, 20 | * then the current position is changed + newposition. If npisrelative 21 | * is False, then newposition is an absolute position. 22 | */ 23 | static int newposition = 1; 24 | static Bool npisrelative = True; 25 | 26 | #define SETPROP(p) { \ 27 | .v = (char *[]){ "/bin/sh", "-c", \ 28 | "prop=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 29 | "sed -e's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@' |" \ 30 | "tail -n +2 | dmenu -i -l 10 -p 'Switch to: ')\" &&" \ 31 | "xprop -id $1 -f $0 8s -set $0 \"$prop\"", \ 32 | p, winid, NULL \ 33 | } \ 34 | } 35 | 36 | /* Modify the following line to match your terminal and software list */ 37 | #define OPENTERMSOFT(p) { \ 38 | .v = (char *[]){ "/bin/sh", "-c", \ 39 | "term='alacritty' && titlearg='-t' && embedarg='--embed' &&" \ 40 | "softlist=$(printf '%s\n' \"htop\" \"ncdu\" \"nvim\" \"fzf\" \"nnn\" \"ncmpcpp\" \"nmtui\") &&" \ 41 | "printf '%s' \"$softlist\" |" \ 42 | "dmenu -i -p 'Softwares to run: ' |" \ 43 | "xargs -I {} $term $titlearg \"{}\" $embedarg $1 -e \"{}\"", \ 44 | p, winid, NULL \ 45 | } \ 46 | } 47 | 48 | 49 | /* Modify the following line to match your terminal*/ 50 | #define OPENTERM(p) { \ 51 | .v = (char *[]){ "/bin/sh", "-c", \ 52 | "term='alacritty' && embedarg='--embed' &&" \ 53 | "cd \"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 54 | "sed -e's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@' |" \ 55 | "dmenu -i -l 10 -p 'New term path based on: ' |" \ 56 | "cut -f 1 | xargs -I {} xprop -id \"{}\" | grep _NET_WM_PID |" \ 57 | "cut -d ' ' -f 3 | xargs -I {} pstree -p \"{}\" |" \ 58 | "cut -d '(' -f 3 | cut -d ')' -f 1 |" \ 59 | "xargs -I {} readlink -e /proc/\"{}\"/cwd/)\" &&" \ 60 | "$term $embedarg $1", \ 61 | p, winid, NULL \ 62 | } \ 63 | } 64 | 65 | /* deskid: id for current workspace */ 66 | /* rootid: id for root window */ 67 | /* window: data for chosen window by dmenu */ 68 | /* wid: chosen window's window id */ 69 | /* wname: chosen window's name */ 70 | /* cwid: chosen window's child window id (tabbed window only) */ 71 | #define ATTACHWIN(p) { \ 72 | .v = (char *[]){ "/bin/sh", "-c", \ 73 | "deskid=$(xdotool get_desktop) &&" \ 74 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 75 | "window=\"$(wmctrl -x -l | grep -E \" $deskid \" |" \ 76 | "grep -v $(printf '0x0%x' \"$1\") |" \ 77 | "cut -d ' ' -f 1,4 | dmenu -i -l 5 -p \"Attach: \")\" &&" \ 78 | "wid=$(printf '%s' \"$window\" | cut -d ' ' -f 1) &&" \ 79 | "wname=$(printf '%s' \"$window\" | cut -d ' ' -f 2) &&" \ 80 | "[ \"$wname\" = \"tabbed.tabbed\" ] &&" \ 81 | "cwid=$(xwininfo -children -id \"$wid\" | grep '^ 0x' |" \ 82 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@') &&" \ 83 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$rootid\"; done &&" \ 84 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$1\"; done ||" \ 85 | "xdotool windowreparent \"$wid\" $1", \ 86 | p, winid, NULL \ 87 | } \ 88 | } 89 | 90 | #define ATTACHSELECTWIN(p) { \ 91 | .v = (char *[]){ "/bin/sh", "-c", \ 92 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 93 | "wid=$(xdotool selectwindow) &&" \ 94 | "wname=$(xwininfo -id \"$wid\" | grep 'Window id:' | cut -d ' ' -f 5-) &&" \ 95 | "[ \"$wname\" = \"(has no name)\" ] &&" \ 96 | "cwid=$(xwininfo -children -id \"$wid\" | grep '^ 0x' |" \ 97 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@') &&" \ 98 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$rootid\"; done &&" \ 99 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$1\"; done ||" \ 100 | "xdotool windowreparent \"$wid\" $1", \ 101 | p, winid, NULL \ 102 | } \ 103 | } 104 | 105 | #define ATTACHALL(p) { \ 106 | .v = (char *[]){ "/bin/sh", "-c", \ 107 | "deskid=$(xdotool get_desktop) &&" \ 108 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 109 | "window=\"$(wmctrl -x -l | grep -E \" $deskid \" |" \ 110 | "grep -v $(printf '0x0%x' \"$1\") | cut -d ' ' -f 1,4)\" &&" \ 111 | "IFS=':' &&" \ 112 | "for win in $(printf '%s' \"$window\" | tr '\n' ':'); do unset IFS &&" \ 113 | "wid=$(printf '%s' \"$win\" | cut -d ' ' -f 1) &&" \ 114 | "wname=$(printf '%s' \"$win\" | cut -d ' ' -f 2) &&" \ 115 | "[ \"$wname\" = \"tabbed.tabbed\" ] &&" \ 116 | "{ cwid=$(xwininfo -children -id \"$wid\" | grep '^ 0x' |" \ 117 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@') &&" \ 118 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$rootid\"; done &&" \ 119 | "for id in $(printf '%s' \"$cwid\"); do xdotool windowreparent \"$id\" \"$1\"; done; } ||" \ 120 | "xdotool windowreparent \"$wid\" $1; done", \ 121 | p, winid, NULL \ 122 | } \ 123 | } 124 | 125 | 126 | #define DETACHWIN(p) { \ 127 | .v = (char *[]){ "/bin/sh", "-c", \ 128 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 129 | "wid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 130 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@' |" \ 131 | "dmenu -i -l 5 -p 'Detach: ' | cut -d ' ' -f 1)\" &&" \ 132 | "xwininfo -id $wid -stats | grep -q 'IsUnMapped' && xdotool windowmap $wid;" \ 133 | "xdotool windowreparent \"$wid\" \"$rootid\" &&" \ 134 | "xdotool windowactivate $1", \ 135 | p, winid, NULL \ 136 | } \ 137 | } 138 | 139 | 140 | 141 | #define DETACHALL(p) { \ 142 | .v = (char *[]){ "/bin/sh", "-c", \ 143 | "rootid=\"$(xwininfo -root | grep \"Window id\" | cut -d ' ' -f 4)\" &&" \ 144 | "wid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 145 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@')\" &&" \ 146 | "IFS=':' &&" \ 147 | "for id in $(printf '%s' \"$wid\" | tr '\n' ':'); do unset IFS &&" \ 148 | "xdotool windowreparent \"$id\" \"$rootid\" &&" \ 149 | "xwininfo -id $id -stats |" \ 150 | "grep -q 'IsUnMapped' &&" \ 151 | "xdotool windowmap $id; done", \ 152 | p, winid, NULL \ 153 | } \ 154 | } 155 | 156 | 157 | 158 | #define SHOWHIDDEN(p) { \ 159 | .v = (char *[]){ "/bin/sh", "-c", \ 160 | "cwin=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 161 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1 \\2@')\" &&" \ 162 | "IFS=':' &&" \ 163 | "for win in $(printf '%s' \"$cwin\" | tr '\n' ':'); do unset IFS &&" \ 164 | "cwid=$(printf '%s' \"$win\" | cut -d ' ' -f 1) &&" \ 165 | "xwininfo -id $cwid -stats |" \ 166 | "grep -q 'IsUnMapped' &&" \ 167 | "printf '%s\n' \"$win\"; done |" \ 168 | "dmenu -i -l 5 -p \"Show hidden window:\" |" \ 169 | "cut -d ' ' -f 1 |" \ 170 | "xargs -I {} xdotool windowmap \"{}\"", \ 171 | p, winid, NULL \ 172 | } \ 173 | } 174 | 175 | #define SHOWHIDDENALL(p) { \ 176 | .v = (char *[]){ "/bin/sh", "-c", \ 177 | "cwid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 178 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@')\" &&" \ 179 | "IFS=':' &&" \ 180 | "for id in $(printf '%s' \"$cwid\" | tr '\n' ':'); do unset IFS &&" \ 181 | "xwininfo -id $id -stats | " \ 182 | "grep -q 'IsUnMapped' &&" \ 183 | "xdotool windowmap $id; done", \ 184 | p, winid, NULL \ 185 | } \ 186 | } 187 | 188 | #define HIDEWINDOW(p) { \ 189 | .v = (char *[]){ "/bin/sh", "-c", \ 190 | "cwid=\"$(xwininfo -children -id $1 | grep '^ 0x' |" \ 191 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@')\" &&" \ 192 | "IFS=':' && winnum=0 &&" \ 193 | "for id in $(printf '%s' \"$cwid\" | tr '\n' ':'); do unset IFS &&" \ 194 | "xwininfo -id $id -stats | " \ 195 | "grep -q 'IsViewable' &&" \ 196 | "winnum=$(($winnum+1)); done;" \ 197 | "[ $winnum -gt 1 ] &&" \ 198 | "{ xwininfo -children -id $1 | grep '^ 0x' | head -n 1 |" \ 199 | "sed -e 's@^ *\\(0x[0-9a-f]*\\) \"\\([^\"]*\\)\".*@\\1@' |" \ 200 | "xargs -I {} xdotool windowunmap \"{}\"; }", \ 201 | p, winid, NULL \ 202 | } \ 203 | } 204 | 205 | 206 | 207 | #define MODKEY Mod4Mask 208 | static Key keys[] = { 209 | /* modifier key function argument */ 210 | { MODKEY|ShiftMask, XK_Return, focusonce, { 0 } }, 211 | { MODKEY|ShiftMask, XK_Return, spawn, { 0 } }, 212 | 213 | { MODKEY|ShiftMask, XK_k, rotate, { .i = +1 } }, 214 | { MODKEY|ShiftMask, XK_j, rotate, { .i = -1 } }, 215 | { MODKEY|ShiftMask, XK_h, movetab, { .i = -1 } }, 216 | { MODKEY|ShiftMask, XK_l, movetab, { .i = +1 } }, 217 | { ControlMask, XK_Tab, rotate, { .i = 0 } }, 218 | 219 | { MODKEY|ShiftMask, XK_1, move, { .i = 0 } }, 220 | { MODKEY|ShiftMask, XK_2, move, { .i = 1 } }, 221 | { MODKEY|ShiftMask, XK_3, move, { .i = 2 } }, 222 | { MODKEY|ShiftMask, XK_4, move, { .i = 3 } }, 223 | { MODKEY|ShiftMask, XK_5, move, { .i = 4 } }, 224 | { MODKEY|ShiftMask, XK_6, move, { .i = 5 } }, 225 | { MODKEY|ShiftMask, XK_7, move, { .i = 6 } }, 226 | { MODKEY|ShiftMask, XK_8, move, { .i = 7 } }, 227 | { MODKEY|ShiftMask, XK_9, move, { .i = 8 } }, 228 | { MODKEY|ShiftMask, XK_0, move, { .i = 9 } }, 229 | 230 | { MODKEY|ShiftMask, XK_q, killclient, { 0 } }, 231 | 232 | { MODKEY, XK_u, focusurgent, { 0 } }, 233 | { MODKEY|ShiftMask, XK_u, toggle, { .v = (void*) &urgentswitch } }, 234 | 235 | { 0, XK_F11, fullscreen, { 0 } }, 236 | 237 | { MODKEY|ShiftMask, XK_comma, spawn, SETPROP("_TABBED_SELECT_TAB") }, 238 | 239 | { MODKEY, XK_Shift_L, showbar, { .i = 1 } }, 240 | { ShiftMask, XK_Super_L, showbar, { .i = 1 } }, 241 | { ControlMask, XK_comma, showbar, { .i = 1 } }, 242 | 243 | /* Unique functionality */ 244 | { MODKEY|ShiftMask, XK_period, spawn, OPENTERMSOFT("_TABBED_SELECT_TERMAPP") }, 245 | { MODKEY|ShiftMask, XK_slash, spawn, OPENTERM("_TABBED_TERM") }, 246 | { MODKEY|ShiftMask, XK_a, spawn, ATTACHWIN("_TABBED_ATTACH_WIN") }, 247 | { MODKEY|ShiftMask, XK_s, spawn, ATTACHSELECTWIN("_TABBED_ATTACH_WIN") }, 248 | { MODKEY|ShiftMask, XK_equal, spawn, ATTACHALL("_TABBED_ATTACH_ALL") }, 249 | { MODKEY|ShiftMask, XK_d, spawn, DETACHWIN("_TABBED_DETACH_WIN") }, 250 | { MODKEY|ShiftMask, XK_minus, spawn, DETACHALL("_TABBED_DETACH_ALL") }, 251 | { MODKEY|ShiftMask, XK_bracketleft, spawn, HIDEWINDOW("_TABBED_HIDE_WINDOW") }, 252 | { MODKEY|ShiftMask, XK_bracketright, spawn, SHOWHIDDEN("_TABBED_SHOW_HIDDEN") }, 253 | { MODKEY|ShiftMask, XK_backslash, spawn, SHOWHIDDENALL("_TABBED_SHOW_HIDDEN_ALL") }, 254 | }; 255 | 256 | static Key keyreleases[] = { 257 | /* modifier key function argument */ 258 | { MODKEY|ShiftMask, XK_Shift_L, showbar, { .i = 0 } }, 259 | { MODKEY|ShiftMask, XK_Super_L, showbar, { .i = 0 } }, 260 | }; 261 | -------------------------------------------------------------------------------- /tabbed.c.orig: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file for copyright and license details. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 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, WMSelectApp, WMAttachWin, WMDetachWin, WMHiddenWin, 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 keyrelease(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 sigchld(int unused); 130 | static void showbar(const Arg *arg); 131 | static void spawn(const Arg *arg); 132 | static int textnw(const char *text, unsigned int len); 133 | static void toggle(const Arg *arg); 134 | static void unmanage(int c); 135 | static void unmapnotify(const XEvent *e); 136 | static void updatenumlockmask(void); 137 | static void updatetitle(int c); 138 | static int xerror(Display *dpy, XErrorEvent *ee); 139 | static void xsettitle(Window w, const char *str); 140 | 141 | /* variables */ 142 | static int screen; 143 | static void (*handler[LASTEvent]) (const XEvent *) = { 144 | [ButtonPress] = buttonpress, 145 | [ClientMessage] = clientmessage, 146 | [ConfigureNotify] = configurenotify, 147 | [ConfigureRequest] = configurerequest, 148 | [CreateNotify] = createnotify, 149 | [UnmapNotify] = unmapnotify, 150 | [DestroyNotify] = destroynotify, 151 | [Expose] = expose, 152 | [FocusIn] = focusin, 153 | [KeyPress] = keypress, 154 | [KeyRelease] = keyrelease, 155 | [MapRequest] = maprequest, 156 | [PropertyNotify] = propertynotify, 157 | }; 158 | static int bh, wx, wy, ww, wh, vbh; 159 | static unsigned int numlockmask; 160 | static Bool running = True, nextfocus, doinitspawn = True, 161 | fillagain = False, closelastclient = False, 162 | killclientsfirst = False; 163 | static Display *dpy; 164 | static DC dc; 165 | static Atom wmatom[WMLast]; 166 | static Window root, win; 167 | static Client **clients; 168 | static int nclients, sel = -1, lastsel = -1; 169 | static int (*xerrorxlib)(Display *, XErrorEvent *); 170 | static int cmd_append_pos; 171 | static char winid[64]; 172 | static char **cmd; 173 | static char *wmname = "tabbed"; 174 | static const char *geometry; 175 | static Bool barvisibility = False; 176 | 177 | static Colormap cmap; 178 | static Visual *visual = NULL; 179 | 180 | char *argv0; 181 | 182 | /* configuration, allows nested code to access above variables */ 183 | #include "config.h" 184 | 185 | void 186 | buttonpress(const XEvent *e) 187 | { 188 | const XButtonPressedEvent *ev = &e->xbutton; 189 | int i, fc; 190 | Arg arg; 191 | 192 | if (ev->y < 0 || ev->y > bh) 193 | return; 194 | 195 | if (((fc = getfirsttab()) > 0 && ev->x < TEXTW(before)) || ev->x < 0) 196 | return; 197 | 198 | for (i = fc; i < nclients; i++) { 199 | if (clients[i]->tabx > ev->x) { 200 | switch (ev->button) { 201 | case Button1: 202 | focus(i); 203 | break; 204 | case Button2: 205 | focus(i); 206 | killclient(NULL); 207 | break; 208 | case Button4: /* FALLTHROUGH */ 209 | case Button5: 210 | arg.i = ev->button == Button4 ? -1 : 1; 211 | rotate(&arg); 212 | break; 213 | } 214 | break; 215 | } 216 | } 217 | } 218 | 219 | void 220 | cleanup(void) 221 | { 222 | int i; 223 | 224 | for (i = 0; i < nclients; i++) { 225 | focus(i); 226 | killclient(NULL); 227 | XReparentWindow(dpy, clients[i]->win, root, 0, 0); 228 | unmanage(i); 229 | } 230 | free(clients); 231 | clients = NULL; 232 | 233 | XFreePixmap(dpy, dc.drawable); 234 | XFreeGC(dpy, dc.gc); 235 | XDestroyWindow(dpy, win); 236 | XSync(dpy, False); 237 | free(cmd); 238 | } 239 | 240 | void 241 | clientmessage(const XEvent *e) 242 | { 243 | const XClientMessageEvent *ev = &e->xclient; 244 | 245 | if (ev->message_type == wmatom[WMProtocols] && 246 | ev->data.l[0] == wmatom[WMDelete]) { 247 | if (nclients > 1 && killclientsfirst) { 248 | killclient(0); 249 | return; 250 | } 251 | running = False; 252 | } 253 | } 254 | 255 | void 256 | configurenotify(const XEvent *e) 257 | { 258 | const XConfigureEvent *ev = &e->xconfigure; 259 | 260 | if (ev->window == win && (ev->width != ww || ev->height != wh)) { 261 | ww = ev->width; 262 | wh = ev->height; 263 | XFreePixmap(dpy, dc.drawable); 264 | dc.drawable = XCreatePixmap(dpy, win, ww, wh, 265 | 32); 266 | if (sel > -1) 267 | resize(sel, ww, wh - bh); 268 | XSync(dpy, False); 269 | } 270 | } 271 | 272 | void 273 | configurerequest(const XEvent *e) 274 | { 275 | const XConfigureRequestEvent *ev = &e->xconfigurerequest; 276 | XWindowChanges wc; 277 | int c; 278 | 279 | if ((c = getclient(ev->window)) > -1) { 280 | wc.x = 0; 281 | wc.y = bh; 282 | wc.width = ww; 283 | wc.height = wh - bh; 284 | wc.border_width = 0; 285 | wc.sibling = ev->above; 286 | wc.stack_mode = ev->detail; 287 | XConfigureWindow(dpy, clients[c]->win, ev->value_mask, &wc); 288 | } 289 | } 290 | 291 | void 292 | createnotify(const XEvent *e) 293 | { 294 | const XCreateWindowEvent *ev = &e->xcreatewindow; 295 | 296 | if (ev->window != win && getclient(ev->window) < 0) 297 | manage(ev->window); 298 | } 299 | 300 | void 301 | destroynotify(const XEvent *e) 302 | { 303 | const XDestroyWindowEvent *ev = &e->xdestroywindow; 304 | int c; 305 | 306 | if ((c = getclient(ev->window)) > -1) 307 | unmanage(c); 308 | } 309 | 310 | void 311 | die(const char *errstr, ...) 312 | { 313 | va_list ap; 314 | 315 | va_start(ap, errstr); 316 | vfprintf(stderr, errstr, ap); 317 | va_end(ap); 318 | exit(EXIT_FAILURE); 319 | } 320 | 321 | void 322 | drawbar(void) 323 | { 324 | XftColor *col; 325 | int c, cc, fc, width, nbh; 326 | char *name = NULL; 327 | char tabtitle[256]; 328 | 329 | nbh = barvisibility ? vbh : 0; 330 | if (nbh != bh) { 331 | bh = nbh; 332 | for (c = 0; c < nclients; c++) 333 | XMoveResizeWindow(dpy, clients[c]->win, 0, bh, ww, wh-bh); 334 | } 335 | 336 | if (bh == 0) return; 337 | 338 | if (nclients == 0) { 339 | dc.x = 0; 340 | dc.w = ww; 341 | XFetchName(dpy, win, &name); 342 | drawtext(name ? name : "", dc.norm); 343 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0); 344 | XSync(dpy, False); 345 | 346 | return; 347 | } 348 | 349 | width = ww; 350 | cc = ww / tabwidth; 351 | if (nclients > cc) 352 | cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth; 353 | 354 | if ((fc = getfirsttab()) + cc < nclients) { 355 | dc.w = TEXTW(after); 356 | dc.x = width - dc.w; 357 | drawtext(after, dc.sel); 358 | width -= dc.w; 359 | } 360 | dc.x = 0; 361 | 362 | if (fc > 0) { 363 | dc.w = TEXTW(before); 364 | drawtext(before, dc.sel); 365 | dc.x += dc.w; 366 | width -= dc.w; 367 | } 368 | 369 | cc = MIN(cc, nclients); 370 | for (c = fc; c < fc + cc; c++) { 371 | dc.w = width / cc; 372 | if (c == sel) { 373 | col = dc.sel; 374 | dc.w += width % cc; 375 | } else { 376 | col = clients[c]->urgent ? dc.urg : dc.norm; 377 | } 378 | snprintf(tabtitle, sizeof(tabtitle), "%d: %s", 379 | c + 1, clients[c]->name); 380 | drawtext(tabtitle, col); 381 | dc.x += dc.w; 382 | clients[c]->tabx = dc.x; 383 | } 384 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0); 385 | XSync(dpy, False); 386 | } 387 | 388 | void 389 | drawtext(const char *text, XftColor col[ColLast]) 390 | { 391 | int i, j, x, y, h, len, olen; 392 | char buf[256]; 393 | XftDraw *d; 394 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; 395 | 396 | XSetForeground(dpy, dc.gc, col[ColBG].pixel); 397 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); 398 | if (!text) 399 | return; 400 | 401 | olen = strlen(text); 402 | h = dc.font.ascent + dc.font.descent; 403 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; 404 | x = dc.x + (h / 2); 405 | 406 | /* shorten text if necessary */ 407 | for (len = MIN(olen, sizeof(buf)); 408 | len && textnw(text, len) > dc.w - h; len--); 409 | 410 | if (!len) 411 | return; 412 | 413 | memcpy(buf, text, len); 414 | if (len < olen) { 415 | for (i = len, j = strlen(titletrim); j && i; 416 | buf[--i] = titletrim[--j]) 417 | ; 418 | } 419 | 420 | d = XftDrawCreate(dpy, dc.drawable, visual, cmap); 421 | XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len); 422 | XftDrawDestroy(d); 423 | } 424 | 425 | void * 426 | ecalloc(size_t n, size_t size) 427 | { 428 | void *p; 429 | 430 | if (!(p = calloc(n, size))) 431 | die("%s: cannot calloc\n", argv0); 432 | return p; 433 | } 434 | 435 | void * 436 | erealloc(void *o, size_t size) 437 | { 438 | void *p; 439 | 440 | if (!(p = realloc(o, size))) 441 | die("%s: cannot realloc\n", argv0); 442 | return p; 443 | } 444 | 445 | void 446 | expose(const XEvent *e) 447 | { 448 | const XExposeEvent *ev = &e->xexpose; 449 | 450 | if (ev->count == 0 && win == ev->window) 451 | drawbar(); 452 | } 453 | 454 | void 455 | focus(int c) 456 | { 457 | char buf[BUFSIZ] = "tabbed-"VERSION" ::"; 458 | size_t i, n; 459 | XWMHints* wmh; 460 | 461 | /* If c, sel and clients are -1, raise tabbed-win itself */ 462 | if (nclients == 0) { 463 | cmd[cmd_append_pos] = NULL; 464 | for(i = 0, n = strlen(buf); cmd[i] && n < sizeof(buf); i++) 465 | n += snprintf(&buf[n], sizeof(buf) - n, " %s", cmd[i]); 466 | 467 | xsettitle(win, buf); 468 | XRaiseWindow(dpy, win); 469 | 470 | return; 471 | } 472 | 473 | if (c < 0 || c >= nclients) 474 | return; 475 | 476 | resize(c, ww, wh - bh); 477 | XRaiseWindow(dpy, clients[c]->win); 478 | XSetInputFocus(dpy, clients[c]->win, RevertToParent, CurrentTime); 479 | sendxembed(c, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0); 480 | sendxembed(c, XEMBED_WINDOW_ACTIVATE, 0, 0, 0); 481 | xsettitle(win, clients[c]->name); 482 | 483 | if (sel != c) { 484 | lastsel = sel; 485 | sel = c; 486 | } 487 | 488 | if (clients[c]->urgent && (wmh = XGetWMHints(dpy, clients[c]->win))) { 489 | wmh->flags &= ~XUrgencyHint; 490 | XSetWMHints(dpy, clients[c]->win, wmh); 491 | clients[c]->urgent = False; 492 | XFree(wmh); 493 | } 494 | 495 | drawbar(); 496 | XSync(dpy, False); 497 | } 498 | 499 | void 500 | focusin(const XEvent *e) 501 | { 502 | const XFocusChangeEvent *ev = &e->xfocus; 503 | int dummy; 504 | Window focused; 505 | 506 | if (ev->mode != NotifyUngrab) { 507 | XGetInputFocus(dpy, &focused, &dummy); 508 | if (focused == win) 509 | focus(sel); 510 | } 511 | } 512 | 513 | void 514 | focusonce(const Arg *arg) 515 | { 516 | nextfocus = True; 517 | } 518 | 519 | void 520 | focusurgent(const Arg *arg) 521 | { 522 | int c; 523 | 524 | if (sel < 0) 525 | return; 526 | 527 | for (c = (sel + 1) % nclients; c != sel; c = (c + 1) % nclients) { 528 | if (clients[c]->urgent) { 529 | focus(c); 530 | return; 531 | } 532 | } 533 | } 534 | 535 | void 536 | fullscreen(const Arg *arg) 537 | { 538 | XEvent e; 539 | 540 | e.type = ClientMessage; 541 | e.xclient.window = win; 542 | e.xclient.message_type = wmatom[WMState]; 543 | e.xclient.format = 32; 544 | e.xclient.data.l[0] = 2; 545 | e.xclient.data.l[1] = wmatom[WMFullscreen]; 546 | e.xclient.data.l[2] = 0; 547 | XSendEvent(dpy, root, False, SubstructureNotifyMask, &e); 548 | } 549 | 550 | char * 551 | getatom(int a) 552 | { 553 | static char buf[BUFSIZ]; 554 | Atom adummy; 555 | int idummy; 556 | unsigned long ldummy; 557 | unsigned char *p = NULL; 558 | 559 | XGetWindowProperty(dpy, win, wmatom[a], 0L, BUFSIZ, False, XA_STRING, 560 | &adummy, &idummy, &ldummy, &ldummy, &p); 561 | if (p) 562 | strncpy(buf, (char *)p, LENGTH(buf)-1); 563 | else 564 | buf[0] = '\0'; 565 | XFree(p); 566 | 567 | return buf; 568 | } 569 | 570 | int 571 | getclient(Window w) 572 | { 573 | int i; 574 | 575 | for (i = 0; i < nclients; i++) { 576 | if (clients[i]->win == w) 577 | return i; 578 | } 579 | 580 | return -1; 581 | } 582 | 583 | XftColor 584 | getcolor(const char *colstr) 585 | { 586 | XftColor color; 587 | 588 | if (!XftColorAllocName(dpy, visual, cmap, colstr, &color)) 589 | die("%s: cannot allocate color '%s'\n", argv0, colstr); 590 | 591 | return color; 592 | } 593 | 594 | int 595 | getfirsttab(void) 596 | { 597 | int cc, ret; 598 | 599 | if (sel < 0) 600 | return 0; 601 | 602 | cc = ww / tabwidth; 603 | if (nclients > cc) 604 | cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth; 605 | 606 | ret = sel - cc / 2 + (cc + 1) % 2; 607 | return ret < 0 ? 0 : 608 | ret + cc > nclients ? MAX(0, nclients - cc) : 609 | ret; 610 | } 611 | 612 | Bool 613 | gettextprop(Window w, Atom atom, char *text, unsigned int size) 614 | { 615 | char **list = NULL; 616 | int n; 617 | XTextProperty name; 618 | 619 | if (!text || size == 0) 620 | return False; 621 | 622 | text[0] = '\0'; 623 | XGetTextProperty(dpy, w, &name, atom); 624 | if (!name.nitems) 625 | return False; 626 | 627 | if (name.encoding == XA_STRING) { 628 | strncpy(text, (char *)name.value, size - 1); 629 | } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success 630 | && n > 0 && *list) { 631 | strncpy(text, *list, size - 1); 632 | XFreeStringList(list); 633 | } 634 | text[size - 1] = '\0'; 635 | XFree(name.value); 636 | 637 | return True; 638 | } 639 | 640 | void 641 | initfont(const char *fontstr) 642 | { 643 | if (!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr)) 644 | && !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed"))) 645 | die("error, cannot load font: '%s'\n", fontstr); 646 | 647 | dc.font.ascent = dc.font.xfont->ascent; 648 | dc.font.descent = dc.font.xfont->descent; 649 | dc.font.height = dc.font.ascent + dc.font.descent; 650 | } 651 | 652 | Bool 653 | isprotodel(int c) 654 | { 655 | int i, n; 656 | Atom *protocols; 657 | Bool ret = False; 658 | 659 | if (XGetWMProtocols(dpy, clients[c]->win, &protocols, &n)) { 660 | for (i = 0; !ret && i < n; i++) { 661 | if (protocols[i] == wmatom[WMDelete]) 662 | ret = True; 663 | } 664 | XFree(protocols); 665 | } 666 | 667 | return ret; 668 | } 669 | 670 | void 671 | keypress(const XEvent *e) 672 | { 673 | const XKeyEvent *ev = &e->xkey; 674 | unsigned int i; 675 | KeySym keysym; 676 | 677 | keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0); 678 | for (i = 0; i < LENGTH(keys); i++) { 679 | if (keysym == keys[i].keysym && 680 | CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) && 681 | keys[i].func) 682 | keys[i].func(&(keys[i].arg)); 683 | } 684 | } 685 | 686 | void 687 | keyrelease(const XEvent *e) 688 | { 689 | const XKeyEvent *ev = &e->xkey; 690 | unsigned int i; 691 | KeySym keysym; 692 | 693 | keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0); 694 | for (i = 0; i < LENGTH(keyreleases); i++) { 695 | if (keysym == keyreleases[i].keysym && 696 | CLEANMASK(keyreleases[i].mod) == CLEANMASK(ev->state) && 697 | keyreleases[i].func) 698 | keyreleases[i].func(&(keyreleases[i].arg)); 699 | } 700 | } 701 | 702 | void 703 | killclient(const Arg *arg) 704 | { 705 | XEvent ev; 706 | 707 | if (sel < 0) 708 | return; 709 | 710 | if (isprotodel(sel) && !clients[sel]->closed) { 711 | ev.type = ClientMessage; 712 | ev.xclient.window = clients[sel]->win; 713 | ev.xclient.message_type = wmatom[WMProtocols]; 714 | ev.xclient.format = 32; 715 | ev.xclient.data.l[0] = wmatom[WMDelete]; 716 | ev.xclient.data.l[1] = CurrentTime; 717 | XSendEvent(dpy, clients[sel]->win, False, NoEventMask, &ev); 718 | clients[sel]->closed = True; 719 | } else { 720 | XKillClient(dpy, clients[sel]->win); 721 | } 722 | } 723 | 724 | void 725 | manage(Window w) 726 | { 727 | updatenumlockmask(); 728 | { 729 | int i, j, nextpos; 730 | unsigned int modifiers[] = { 0, LockMask, numlockmask, 731 | numlockmask | LockMask }; 732 | KeyCode code; 733 | Client *c; 734 | XEvent e; 735 | 736 | XWithdrawWindow(dpy, w, 0); 737 | XReparentWindow(dpy, w, win, 0, bh); 738 | XSelectInput(dpy, w, PropertyChangeMask | 739 | StructureNotifyMask | EnterWindowMask); 740 | XSync(dpy, False); 741 | 742 | for (i = 0; i < LENGTH(keys); i++) { 743 | if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) { 744 | for (j = 0; j < LENGTH(modifiers); j++) { 745 | XGrabKey(dpy, code, keys[i].mod | 746 | modifiers[j], w, True, 747 | GrabModeAsync, GrabModeAsync); 748 | } 749 | } 750 | } 751 | 752 | for (i = 0; i < LENGTH(keyreleases); i++) { 753 | if ((code = XKeysymToKeycode(dpy, keyreleases[i].keysym))) { 754 | for (j = 0; j < LENGTH(modifiers); j++) { 755 | XGrabKey(dpy, code, keyreleases[i].mod | 756 | modifiers[j], w, True, 757 | GrabModeAsync, GrabModeAsync); 758 | } 759 | } 760 | } 761 | 762 | c = ecalloc(1, sizeof *c); 763 | c->win = w; 764 | 765 | nclients++; 766 | clients = erealloc(clients, sizeof(Client *) * nclients); 767 | 768 | if(npisrelative) { 769 | nextpos = sel + newposition; 770 | } else { 771 | if (newposition < 0) 772 | nextpos = nclients - newposition; 773 | else 774 | nextpos = newposition; 775 | } 776 | if (nextpos >= nclients) 777 | nextpos = nclients - 1; 778 | if (nextpos < 0) 779 | nextpos = 0; 780 | 781 | if (nclients > 1 && nextpos < nclients - 1) 782 | memmove(&clients[nextpos + 1], &clients[nextpos], 783 | sizeof(Client *) * (nclients - nextpos - 1)); 784 | 785 | clients[nextpos] = c; 786 | updatetitle(nextpos); 787 | 788 | XLowerWindow(dpy, w); 789 | XMapWindow(dpy, w); 790 | 791 | e.xclient.window = w; 792 | e.xclient.type = ClientMessage; 793 | e.xclient.message_type = wmatom[XEmbed]; 794 | e.xclient.format = 32; 795 | e.xclient.data.l[0] = CurrentTime; 796 | e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY; 797 | e.xclient.data.l[2] = 0; 798 | e.xclient.data.l[3] = win; 799 | e.xclient.data.l[4] = 0; 800 | XSendEvent(dpy, root, False, NoEventMask, &e); 801 | 802 | XSync(dpy, False); 803 | 804 | /* Adjust sel before focus does set it to lastsel. */ 805 | if (sel >= nextpos) 806 | sel++; 807 | focus(nextfocus ? nextpos : 808 | sel < 0 ? 0 : 809 | sel); 810 | nextfocus = foreground; 811 | } 812 | } 813 | 814 | void 815 | maprequest(const XEvent *e) 816 | { 817 | const XMapRequestEvent *ev = &e->xmaprequest; 818 | 819 | if (getclient(ev->window) < 0) 820 | manage(ev->window); 821 | } 822 | 823 | void 824 | move(const Arg *arg) 825 | { 826 | if (arg->i >= 0 && arg->i < nclients) 827 | focus(arg->i); 828 | } 829 | 830 | void 831 | movetab(const Arg *arg) 832 | { 833 | int c; 834 | Client *new; 835 | 836 | if (sel < 0) 837 | return; 838 | 839 | c = (sel + arg->i) % nclients; 840 | if (c < 0) 841 | c += nclients; 842 | 843 | if (c == sel) 844 | return; 845 | 846 | new = clients[sel]; 847 | if (sel < c) 848 | memmove(&clients[sel], &clients[sel+1], 849 | sizeof(Client *) * (c - sel)); 850 | else 851 | memmove(&clients[c+1], &clients[c], 852 | sizeof(Client *) * (sel - c)); 853 | clients[c] = new; 854 | sel = c; 855 | 856 | drawbar(); 857 | } 858 | 859 | void 860 | propertynotify(const XEvent *e) 861 | { 862 | const XPropertyEvent *ev = &e->xproperty; 863 | XWMHints *wmh; 864 | int c; 865 | char* selection = NULL; 866 | Arg arg; 867 | 868 | if (ev->state == PropertyNewValue && ev->atom == wmatom[WMSelectTab]) { 869 | selection = getatom(WMSelectTab); 870 | if (!strncmp(selection, "0x", 2)) { 871 | arg.i = getclient(strtoul(selection, NULL, 0)); 872 | move(&arg); 873 | } else { 874 | cmd[cmd_append_pos] = selection; 875 | arg.v = cmd; 876 | spawn(&arg); 877 | } 878 | } else if (ev->state == PropertyNewValue && ev->atom == XA_WM_HINTS && 879 | (c = getclient(ev->window)) > -1 && 880 | (wmh = XGetWMHints(dpy, clients[c]->win))) { 881 | if (wmh->flags & XUrgencyHint) { 882 | XFree(wmh); 883 | wmh = XGetWMHints(dpy, win); 884 | if (c != sel) { 885 | if (urgentswitch && wmh && 886 | !(wmh->flags & XUrgencyHint)) { 887 | /* only switch, if tabbed was focused 888 | * since last urgency hint if WMHints 889 | * could not be received, 890 | * default to no switch */ 891 | focus(c); 892 | } else { 893 | /* if no switch should be performed, 894 | * mark tab as urgent */ 895 | clients[c]->urgent = True; 896 | drawbar(); 897 | } 898 | } 899 | if (wmh && !(wmh->flags & XUrgencyHint)) { 900 | /* update tabbed urgency hint 901 | * if not set already */ 902 | wmh->flags |= XUrgencyHint; 903 | XSetWMHints(dpy, win, wmh); 904 | } 905 | } 906 | XFree(wmh); 907 | } else if (ev->state != PropertyDelete && ev->atom == XA_WM_NAME && 908 | (c = getclient(ev->window)) > -1) { 909 | updatetitle(c); 910 | } 911 | } 912 | 913 | void 914 | resize(int c, int w, int h) 915 | { 916 | XConfigureEvent ce; 917 | XWindowChanges wc; 918 | 919 | ce.x = 0; 920 | ce.y = bh; 921 | ce.width = wc.width = w; 922 | ce.height = wc.height = h; 923 | ce.type = ConfigureNotify; 924 | ce.display = dpy; 925 | ce.event = clients[c]->win; 926 | ce.window = clients[c]->win; 927 | ce.above = None; 928 | ce.override_redirect = False; 929 | ce.border_width = 0; 930 | 931 | XConfigureWindow(dpy, clients[c]->win, CWWidth | CWHeight, &wc); 932 | XSendEvent(dpy, clients[c]->win, False, StructureNotifyMask, 933 | (XEvent *)&ce); 934 | } 935 | 936 | void 937 | rotate(const Arg *arg) 938 | { 939 | int nsel = -1; 940 | 941 | if (sel < 0) 942 | return; 943 | 944 | if (arg->i == 0) { 945 | if (lastsel > -1) 946 | focus(lastsel); 947 | } else if (sel > -1) { 948 | /* Rotating in an arg->i step around the clients. */ 949 | nsel = sel + arg->i; 950 | while (nsel >= nclients) 951 | nsel -= nclients; 952 | while (nsel < 0) 953 | nsel += nclients; 954 | focus(nsel); 955 | } 956 | } 957 | 958 | void 959 | run(void) 960 | { 961 | XEvent ev; 962 | 963 | /* main event loop */ 964 | XSync(dpy, False); 965 | drawbar(); 966 | if (doinitspawn == True) 967 | spawn(NULL); 968 | 969 | while (running) { 970 | XNextEvent(dpy, &ev); 971 | if (handler[ev.type]) 972 | (handler[ev.type])(&ev); /* call handler */ 973 | } 974 | } 975 | 976 | void 977 | sendxembed(int c, long msg, long detail, long d1, long d2) 978 | { 979 | XEvent e = { 0 }; 980 | 981 | e.xclient.window = clients[c]->win; 982 | e.xclient.type = ClientMessage; 983 | e.xclient.message_type = wmatom[XEmbed]; 984 | e.xclient.format = 32; 985 | e.xclient.data.l[0] = CurrentTime; 986 | e.xclient.data.l[1] = msg; 987 | e.xclient.data.l[2] = detail; 988 | e.xclient.data.l[3] = d1; 989 | e.xclient.data.l[4] = d2; 990 | XSendEvent(dpy, clients[c]->win, False, NoEventMask, &e); 991 | } 992 | 993 | void 994 | setcmd(int argc, char *argv[], int replace) 995 | { 996 | int i; 997 | 998 | cmd = ecalloc(argc + 3, sizeof(*cmd)); 999 | if (argc == 0) 1000 | return; 1001 | for (i = 0; i < argc; i++) 1002 | cmd[i] = argv[i]; 1003 | cmd[replace > 0 ? replace : argc] = winid; 1004 | cmd_append_pos = argc + !replace; 1005 | cmd[cmd_append_pos] = cmd[cmd_append_pos + 1] = NULL; 1006 | } 1007 | 1008 | void 1009 | setup(void) 1010 | { 1011 | int bitm, tx, ty, tw, th, dh, dw, isfixed; 1012 | XWMHints *wmh; 1013 | XClassHint class_hint; 1014 | XSizeHints *size_hint; 1015 | 1016 | /* clean up any zombies immediately */ 1017 | sigchld(0); 1018 | 1019 | /* init screen */ 1020 | screen = DefaultScreen(dpy); 1021 | root = RootWindow(dpy, screen); 1022 | initfont(font); 1023 | vbh = dc.h = dc.font.height + 2; 1024 | 1025 | /* init atoms */ 1026 | wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1027 | wmatom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); 1028 | wmatom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1029 | wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1030 | wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False); 1031 | wmatom[WMSelectApp] = XInternAtom(dpy, "_TABBED_SELECT_TERMAPP", False); 1032 | wmatom[WMSelectApp] = XInternAtom(dpy, "_TABBED_TERM", False); 1033 | wmatom[WMAttachWin] = XInternAtom(dpy, "_TABBED_ATTACH_WIN", False); 1034 | wmatom[WMAttachWin] = XInternAtom(dpy, "_TABBED_ATTACH_ALL", False); 1035 | wmatom[WMDetachWin] = XInternAtom(dpy, "_TABBED_DETACH_WIN", False); 1036 | wmatom[WMDetachWin] = XInternAtom(dpy, "_TABBED_DETACH_ALL", False); 1037 | wmatom[WMHiddenWin] = XInternAtom(dpy, "_TABBED_HIDE_WINDOW", False); 1038 | wmatom[WMHiddenWin] = XInternAtom(dpy, "_TABBED_SHOW_HIDDEN", False); 1039 | wmatom[WMHiddenWin] = XInternAtom(dpy, "_TABBED_SHOW_HIDDEN_ALL", False); 1040 | wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 1041 | wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False); 1042 | 1043 | /* init appearance */ 1044 | wx = 0; 1045 | wy = 0; 1046 | ww = 800; 1047 | wh = 600; 1048 | isfixed = 0; 1049 | 1050 | if (geometry) { 1051 | tx = ty = tw = th = 0; 1052 | bitm = XParseGeometry(geometry, &tx, &ty, (unsigned *)&tw, 1053 | (unsigned *)&th); 1054 | if (bitm & XValue) 1055 | wx = tx; 1056 | if (bitm & YValue) 1057 | wy = ty; 1058 | if (bitm & WidthValue) 1059 | ww = tw; 1060 | if (bitm & HeightValue) 1061 | wh = th; 1062 | if (bitm & XNegative && wx == 0) 1063 | wx = -1; 1064 | if (bitm & YNegative && wy == 0) 1065 | wy = -1; 1066 | if (bitm & (HeightValue | WidthValue)) 1067 | isfixed = 1; 1068 | 1069 | dw = DisplayWidth(dpy, screen); 1070 | dh = DisplayHeight(dpy, screen); 1071 | if (wx < 0) 1072 | wx = dw + wx - ww - 1; 1073 | if (wy < 0) 1074 | wy = dh + wy - wh - 1; 1075 | } 1076 | 1077 | XVisualInfo *vis; 1078 | XRenderPictFormat *fmt; 1079 | int nvi; 1080 | int i; 1081 | 1082 | XVisualInfo tpl = { 1083 | .screen = screen, 1084 | .depth = 32, 1085 | .class = TrueColor 1086 | }; 1087 | 1088 | vis = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask | VisualClassMask, &tpl, &nvi); 1089 | for(i = 0; i < nvi; i ++) { 1090 | fmt = XRenderFindVisualFormat(dpy, vis[i].visual); 1091 | if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { 1092 | visual = vis[i].visual; 1093 | break; 1094 | } 1095 | } 1096 | 1097 | XFree(vis); 1098 | 1099 | if (! visual) { 1100 | fprintf(stderr, "Couldn't find ARGB visual.\n"); 1101 | exit(1); 1102 | } 1103 | 1104 | cmap = XCreateColormap( dpy, root, visual, None); 1105 | dc.norm[ColBG] = getcolor(normbgcolor); 1106 | dc.norm[ColFG] = getcolor(normfgcolor); 1107 | dc.sel[ColBG] = getcolor(selbgcolor); 1108 | dc.sel[ColFG] = getcolor(selfgcolor); 1109 | dc.urg[ColBG] = getcolor(urgbgcolor); 1110 | dc.urg[ColFG] = getcolor(urgfgcolor); 1111 | 1112 | XSetWindowAttributes attrs; 1113 | attrs.background_pixel = dc.norm[ColBG].pixel; 1114 | attrs.border_pixel = dc.norm[ColFG].pixel; 1115 | attrs.bit_gravity = NorthWestGravity; 1116 | attrs.event_mask = FocusChangeMask | KeyPressMask 1117 | | ExposureMask | VisibilityChangeMask | StructureNotifyMask 1118 | | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask; 1119 | attrs.background_pixmap = None ; 1120 | attrs.colormap = cmap; 1121 | 1122 | win = XCreateWindow(dpy, root, wx, wy, 1123 | ww, wh, 0, 32, InputOutput, 1124 | visual, CWBackPixmap | CWBorderPixel | CWBitGravity 1125 | | CWEventMask | CWColormap, &attrs); 1126 | 1127 | dc.drawable = XCreatePixmap(dpy, win, ww, wh, 1128 | 32); 1129 | dc.gc = XCreateGC(dpy, dc.drawable, 0, 0); 1130 | 1131 | XMapRaised(dpy, win); 1132 | XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask | 1133 | ButtonPressMask | ExposureMask | KeyPressMask | 1134 | KeyReleaseMask | PropertyChangeMask | StructureNotifyMask | 1135 | SubstructureRedirectMask); 1136 | xerrorxlib = XSetErrorHandler(xerror); 1137 | 1138 | class_hint.res_name = wmname; 1139 | class_hint.res_class = "tabbed"; 1140 | XSetClassHint(dpy, win, &class_hint); 1141 | 1142 | size_hint = XAllocSizeHints(); 1143 | if (!isfixed) { 1144 | size_hint->flags = PSize; 1145 | size_hint->height = wh; 1146 | size_hint->width = ww; 1147 | } else { 1148 | size_hint->flags = PMaxSize | PMinSize; 1149 | size_hint->min_width = size_hint->max_width = ww; 1150 | size_hint->min_height = size_hint->max_height = wh; 1151 | } 1152 | wmh = XAllocWMHints(); 1153 | XSetWMProperties(dpy, win, NULL, NULL, NULL, 0, size_hint, wmh, NULL); 1154 | XFree(size_hint); 1155 | XFree(wmh); 1156 | 1157 | XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1); 1158 | 1159 | snprintf(winid, sizeof(winid), "%lu", win); 1160 | setenv("XEMBED", winid, 1); 1161 | 1162 | nextfocus = foreground; 1163 | focus(-1); 1164 | } 1165 | 1166 | void 1167 | showbar(const Arg *arg) 1168 | { 1169 | barvisibility = arg->i; 1170 | drawbar(); 1171 | } 1172 | 1173 | void 1174 | sigchld(int unused) 1175 | { 1176 | if (signal(SIGCHLD, sigchld) == SIG_ERR) 1177 | die("%s: cannot install SIGCHLD handler", argv0); 1178 | 1179 | while (0 < waitpid(-1, NULL, WNOHANG)); 1180 | } 1181 | 1182 | void 1183 | spawn(const Arg *arg) 1184 | { 1185 | if (fork() == 0) { 1186 | if(dpy) 1187 | close(ConnectionNumber(dpy)); 1188 | 1189 | setsid(); 1190 | if (arg && arg->v) { 1191 | execvp(((char **)arg->v)[0], (char **)arg->v); 1192 | fprintf(stderr, "%s: execvp %s", argv0, 1193 | ((char **)arg->v)[0]); 1194 | } else { 1195 | cmd[cmd_append_pos] = NULL; 1196 | execvp(cmd[0], cmd); 1197 | fprintf(stderr, "%s: execvp %s", argv0, cmd[0]); 1198 | } 1199 | perror(" failed"); 1200 | exit(0); 1201 | } 1202 | } 1203 | 1204 | int 1205 | textnw(const char *text, unsigned int len) 1206 | { 1207 | XGlyphInfo ext; 1208 | XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext); 1209 | return ext.xOff; 1210 | } 1211 | 1212 | void 1213 | toggle(const Arg *arg) 1214 | { 1215 | *(Bool*) arg->v = !*(Bool*) arg->v; 1216 | } 1217 | 1218 | void 1219 | unmanage(int c) 1220 | { 1221 | if (c < 0 || c >= nclients) { 1222 | drawbar(); 1223 | XSync(dpy, False); 1224 | return; 1225 | } 1226 | 1227 | if (!nclients) 1228 | return; 1229 | 1230 | if (c == 0) { 1231 | /* First client. */ 1232 | nclients--; 1233 | free(clients[0]); 1234 | memmove(&clients[0], &clients[1], sizeof(Client *) * nclients); 1235 | } else if (c == nclients - 1) { 1236 | /* Last client. */ 1237 | nclients--; 1238 | free(clients[c]); 1239 | clients = erealloc(clients, sizeof(Client *) * nclients); 1240 | } else { 1241 | /* Somewhere inbetween. */ 1242 | free(clients[c]); 1243 | memmove(&clients[c], &clients[c+1], 1244 | sizeof(Client *) * (nclients - (c + 1))); 1245 | nclients--; 1246 | } 1247 | 1248 | if (nclients <= 0) { 1249 | lastsel = sel = -1; 1250 | 1251 | if (closelastclient) 1252 | running = False; 1253 | else if (fillagain && running) 1254 | spawn(NULL); 1255 | } else { 1256 | if (lastsel >= nclients) 1257 | lastsel = nclients - 1; 1258 | else if (lastsel > c) 1259 | lastsel--; 1260 | 1261 | if (c == sel && lastsel >= 0) { 1262 | focus(lastsel); 1263 | } else { 1264 | if (sel > c) 1265 | sel--; 1266 | if (sel >= nclients) 1267 | sel = nclients - 1; 1268 | 1269 | focus(sel); 1270 | } 1271 | } 1272 | 1273 | drawbar(); 1274 | XSync(dpy, False); 1275 | } 1276 | 1277 | void 1278 | unmapnotify(const XEvent *e) 1279 | { 1280 | const XUnmapEvent *ev = &e->xunmap; 1281 | int c; 1282 | 1283 | if ((c = getclient(ev->window)) > -1) 1284 | unmanage(c); 1285 | } 1286 | 1287 | void 1288 | updatenumlockmask(void) 1289 | { 1290 | unsigned int i, j; 1291 | XModifierKeymap *modmap; 1292 | 1293 | numlockmask = 0; 1294 | modmap = XGetModifierMapping(dpy); 1295 | for (i = 0; i < 8; i++) { 1296 | for (j = 0; j < modmap->max_keypermod; j++) { 1297 | if (modmap->modifiermap[i * modmap->max_keypermod + j] 1298 | == XKeysymToKeycode(dpy, XK_Num_Lock)) 1299 | numlockmask = (1 << i); 1300 | } 1301 | } 1302 | XFreeModifiermap(modmap); 1303 | } 1304 | 1305 | void 1306 | updatetitle(int c) 1307 | { 1308 | if (!gettextprop(clients[c]->win, wmatom[WMName], clients[c]->name, 1309 | sizeof(clients[c]->name))) 1310 | gettextprop(clients[c]->win, XA_WM_NAME, clients[c]->name, 1311 | sizeof(clients[c]->name)); 1312 | if (sel == c) 1313 | xsettitle(win, clients[c]->name); 1314 | drawbar(); 1315 | } 1316 | 1317 | /* There's no way to check accesses to destroyed windows, thus those cases are 1318 | * ignored (especially on UnmapNotify's). Other types of errors call Xlibs 1319 | * default error handler, which may call exit. */ 1320 | int 1321 | xerror(Display *dpy, XErrorEvent *ee) 1322 | { 1323 | if (ee->error_code == BadWindow 1324 | || (ee->request_code == X_SetInputFocus && 1325 | ee->error_code == BadMatch) 1326 | || (ee->request_code == X_PolyText8 && 1327 | ee->error_code == BadDrawable) 1328 | || (ee->request_code == X_PolyFillRectangle && 1329 | ee->error_code == BadDrawable) 1330 | || (ee->request_code == X_PolySegment && 1331 | ee->error_code == BadDrawable) 1332 | || (ee->request_code == X_ConfigureWindow && 1333 | ee->error_code == BadMatch) 1334 | || (ee->request_code == X_GrabButton && 1335 | ee->error_code == BadAccess) 1336 | || (ee->request_code == X_GrabKey && 1337 | ee->error_code == BadAccess) 1338 | || (ee->request_code == X_CopyArea && 1339 | ee->error_code == BadDrawable)) 1340 | return 0; 1341 | 1342 | fprintf(stderr, "%s: fatal error: request code=%d, error code=%d\n", 1343 | argv0, ee->request_code, ee->error_code); 1344 | return xerrorxlib(dpy, ee); /* may call exit */ 1345 | } 1346 | 1347 | void 1348 | xsettitle(Window w, const char *str) 1349 | { 1350 | XTextProperty xtp; 1351 | 1352 | if (XmbTextListToTextProperty(dpy, (char **)&str, 1, 1353 | XCompoundTextStyle, &xtp) == Success) { 1354 | XSetTextProperty(dpy, w, &xtp, wmatom[WMName]); 1355 | XSetTextProperty(dpy, w, &xtp, XA_WM_NAME); 1356 | XFree(xtp.value); 1357 | } 1358 | } 1359 | 1360 | void 1361 | usage(void) 1362 | { 1363 | die("usage: %s [-dfksv] [-g geometry] [-n name] [-p [s+/-]pos]\n" 1364 | " [-r narg] [-o color] [-O color] [-t color] [-T color]\n" 1365 | " [-u color] [-U color] command...\n", argv0); 1366 | } 1367 | 1368 | int 1369 | main(int argc, char *argv[]) 1370 | { 1371 | Bool detach = False; 1372 | int replace = 0; 1373 | char *pstr; 1374 | 1375 | ARGBEGIN { 1376 | case 'c': 1377 | closelastclient = True; 1378 | fillagain = False; 1379 | break; 1380 | case 'd': 1381 | detach = True; 1382 | break; 1383 | case 'f': 1384 | fillagain = True; 1385 | break; 1386 | case 'g': 1387 | geometry = EARGF(usage()); 1388 | break; 1389 | case 'k': 1390 | killclientsfirst = True; 1391 | break; 1392 | case 'n': 1393 | wmname = EARGF(usage()); 1394 | break; 1395 | case 'O': 1396 | normfgcolor = EARGF(usage()); 1397 | break; 1398 | case 'o': 1399 | normbgcolor = EARGF(usage()); 1400 | break; 1401 | case 'p': 1402 | pstr = EARGF(usage()); 1403 | if (pstr[0] == 's') { 1404 | npisrelative = True; 1405 | newposition = atoi(&pstr[1]); 1406 | } else { 1407 | newposition = atoi(pstr); 1408 | } 1409 | break; 1410 | case 'r': 1411 | replace = atoi(EARGF(usage())); 1412 | break; 1413 | case 's': 1414 | doinitspawn = False; 1415 | break; 1416 | case 'T': 1417 | selfgcolor = EARGF(usage()); 1418 | break; 1419 | case 't': 1420 | selbgcolor = EARGF(usage()); 1421 | break; 1422 | case 'U': 1423 | urgfgcolor = EARGF(usage()); 1424 | break; 1425 | case 'u': 1426 | urgbgcolor = EARGF(usage()); 1427 | break; 1428 | case 'v': 1429 | die("tabbed-"VERSION", © 2009-2016 tabbed engineers, " 1430 | "see LICENSE for details.\n"); 1431 | break; 1432 | default: 1433 | usage(); 1434 | break; 1435 | } ARGEND; 1436 | 1437 | if (argc < 1) { 1438 | doinitspawn = False; 1439 | fillagain = False; 1440 | } 1441 | 1442 | setcmd(argc, argv, replace); 1443 | 1444 | if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 1445 | fprintf(stderr, "%s: no locale support\n", argv0); 1446 | if (!(dpy = XOpenDisplay(NULL))) 1447 | die("%s: cannot open display\n", argv0); 1448 | 1449 | setup(); 1450 | printf("0x%lx\n", win); 1451 | fflush(NULL); 1452 | 1453 | if (detach) { 1454 | if (fork() == 0) { 1455 | fclose(stdout); 1456 | } else { 1457 | if (dpy) 1458 | close(ConnectionNumber(dpy)); 1459 | return EXIT_SUCCESS; 1460 | } 1461 | } 1462 | 1463 | run(); 1464 | cleanup(); 1465 | XCloseDisplay(dpy); 1466 | 1467 | return EXIT_SUCCESS; 1468 | } 1469 | -------------------------------------------------------------------------------- /tabbed.c: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file for copyright and license details. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "arg.h" 21 | #include "icon.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, WMIcon, WMSelectApp, WMAttachWin, WMDetachWin, WMHiddenWin, 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 keyrelease(const XEvent *e); 118 | static void killclient(const Arg *arg); 119 | static void manage(Window win); 120 | static void maprequest(const XEvent *e); 121 | static void move(const Arg *arg); 122 | static void movetab(const Arg *arg); 123 | static void propertynotify(const XEvent *e); 124 | static void resize(int c, int w, int h); 125 | static void rotate(const Arg *arg); 126 | static void run(void); 127 | static void sendxembed(int c, long msg, long detail, long d1, long d2); 128 | static void setcmd(int argc, char *argv[], int); 129 | static void setup(void); 130 | static void sigchld(int unused); 131 | static void showbar(const Arg *arg); 132 | static void spawn(const Arg *arg); 133 | static int textnw(const char *text, unsigned int len); 134 | static void toggle(const Arg *arg); 135 | static void unmanage(int c); 136 | static void unmapnotify(const XEvent *e); 137 | static void updatenumlockmask(void); 138 | static void updatetitle(int c); 139 | static int xerror(Display *dpy, XErrorEvent *ee); 140 | static void xsettitle(Window w, const char *str); 141 | static void xseticon(void); 142 | 143 | /* variables */ 144 | static int screen; 145 | static void (*handler[LASTEvent]) (const XEvent *) = { 146 | [ButtonPress] = buttonpress, 147 | [ClientMessage] = clientmessage, 148 | [ConfigureNotify] = configurenotify, 149 | [ConfigureRequest] = configurerequest, 150 | [CreateNotify] = createnotify, 151 | [UnmapNotify] = unmapnotify, 152 | [DestroyNotify] = destroynotify, 153 | [Expose] = expose, 154 | [FocusIn] = focusin, 155 | [KeyPress] = keypress, 156 | [KeyRelease] = keyrelease, 157 | [MapRequest] = maprequest, 158 | [PropertyNotify] = propertynotify, 159 | }; 160 | static int bh, wx, wy, ww, wh, vbh; 161 | static unsigned int numlockmask; 162 | static Bool running = True, nextfocus, doinitspawn = True, 163 | fillagain = False, closelastclient = False, 164 | killclientsfirst = False; 165 | static Display *dpy; 166 | static DC dc; 167 | static Atom wmatom[WMLast]; 168 | static Window root, win; 169 | static Client **clients; 170 | static int nclients, sel = -1, lastsel = -1; 171 | static int (*xerrorxlib)(Display *, XErrorEvent *); 172 | static int cmd_append_pos; 173 | static char winid[64]; 174 | static char **cmd; 175 | static char *wmname = "tabbed"; 176 | static const char *geometry; 177 | static unsigned long icon[ICON_WIDTH * ICON_HEIGHT + 2]; 178 | static Bool barvisibility = False; 179 | 180 | static Colormap cmap; 181 | static Visual *visual = NULL; 182 | 183 | char *argv0; 184 | 185 | /* configuration, allows nested code to access above variables */ 186 | #include "config.h" 187 | 188 | void 189 | buttonpress(const XEvent *e) 190 | { 191 | const XButtonPressedEvent *ev = &e->xbutton; 192 | int i, fc; 193 | Arg arg; 194 | 195 | if (ev->y < 0 || ev->y > bh) 196 | return; 197 | 198 | if (((fc = getfirsttab()) > 0 && ev->x < TEXTW(before)) || ev->x < 0) 199 | return; 200 | 201 | for (i = fc; i < nclients; i++) { 202 | if (clients[i]->tabx > ev->x) { 203 | switch (ev->button) { 204 | case Button1: 205 | focus(i); 206 | break; 207 | case Button2: 208 | focus(i); 209 | killclient(NULL); 210 | break; 211 | case Button4: /* FALLTHROUGH */ 212 | case Button5: 213 | arg.i = ev->button == Button4 ? -1 : 1; 214 | rotate(&arg); 215 | break; 216 | } 217 | break; 218 | } 219 | } 220 | } 221 | 222 | void 223 | cleanup(void) 224 | { 225 | int i; 226 | 227 | for (i = 0; i < nclients; i++) { 228 | focus(i); 229 | killclient(NULL); 230 | XReparentWindow(dpy, clients[i]->win, root, 0, 0); 231 | unmanage(i); 232 | } 233 | free(clients); 234 | clients = NULL; 235 | 236 | XFreePixmap(dpy, dc.drawable); 237 | XFreeGC(dpy, dc.gc); 238 | XDestroyWindow(dpy, win); 239 | XSync(dpy, False); 240 | free(cmd); 241 | } 242 | 243 | void 244 | clientmessage(const XEvent *e) 245 | { 246 | const XClientMessageEvent *ev = &e->xclient; 247 | 248 | if (ev->message_type == wmatom[WMProtocols] && 249 | ev->data.l[0] == wmatom[WMDelete]) { 250 | if (nclients > 1 && killclientsfirst) { 251 | killclient(0); 252 | return; 253 | } 254 | running = False; 255 | } 256 | } 257 | 258 | void 259 | configurenotify(const XEvent *e) 260 | { 261 | const XConfigureEvent *ev = &e->xconfigure; 262 | 263 | if (ev->window == win && (ev->width != ww || ev->height != wh)) { 264 | ww = ev->width; 265 | wh = ev->height; 266 | XFreePixmap(dpy, dc.drawable); 267 | dc.drawable = XCreatePixmap(dpy, win, ww, wh, 268 | 32); 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, nbh; 329 | char *name = NULL; 330 | char tabtitle[256]; 331 | 332 | nbh = barvisibility ? vbh : 0; 333 | if (nbh != bh) { 334 | bh = nbh; 335 | for (c = 0; c < nclients; c++) 336 | XMoveResizeWindow(dpy, clients[c]->win, 0, bh, ww, wh-bh); 337 | } 338 | 339 | if (bh == 0) return; 340 | 341 | if (nclients == 0) { 342 | dc.x = 0; 343 | dc.w = ww; 344 | XFetchName(dpy, win, &name); 345 | drawtext(name ? name : "", dc.norm); 346 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0); 347 | XSync(dpy, False); 348 | 349 | return; 350 | } 351 | 352 | width = ww; 353 | cc = ww / tabwidth; 354 | if (nclients > cc) 355 | cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth; 356 | 357 | if ((fc = getfirsttab()) + cc < nclients) { 358 | dc.w = TEXTW(after); 359 | dc.x = width - dc.w; 360 | drawtext(after, dc.sel); 361 | width -= dc.w; 362 | } 363 | dc.x = 0; 364 | 365 | if (fc > 0) { 366 | dc.w = TEXTW(before); 367 | drawtext(before, dc.sel); 368 | dc.x += dc.w; 369 | width -= dc.w; 370 | } 371 | 372 | cc = MIN(cc, nclients); 373 | for (c = fc; c < fc + cc; c++) { 374 | dc.w = width / cc; 375 | if (c == sel) { 376 | col = dc.sel; 377 | dc.w += width % cc; 378 | } else { 379 | col = clients[c]->urgent ? dc.urg : dc.norm; 380 | } 381 | snprintf(tabtitle, sizeof(tabtitle), "%d: %s", 382 | c + 1, clients[c]->name); 383 | drawtext(tabtitle, col); 384 | dc.x += dc.w; 385 | clients[c]->tabx = dc.x; 386 | } 387 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0); 388 | XSync(dpy, False); 389 | } 390 | 391 | void 392 | drawtext(const char *text, XftColor col[ColLast]) 393 | { 394 | int i, j, x, y, h, len, olen; 395 | char buf[256]; 396 | XftDraw *d; 397 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; 398 | 399 | XSetForeground(dpy, dc.gc, col[ColBG].pixel); 400 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); 401 | if (!text) 402 | return; 403 | 404 | olen = strlen(text); 405 | h = dc.font.ascent + dc.font.descent; 406 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; 407 | x = dc.x + (h / 2); 408 | 409 | /* shorten text if necessary */ 410 | for (len = MIN(olen, sizeof(buf)); 411 | len && textnw(text, len) > dc.w - h; len--); 412 | 413 | if (!len) 414 | return; 415 | 416 | memcpy(buf, text, len); 417 | if (len < olen) { 418 | for (i = len, j = strlen(titletrim); j && i; 419 | buf[--i] = titletrim[--j]) 420 | ; 421 | } 422 | 423 | d = XftDrawCreate(dpy, dc.drawable, visual, cmap); 424 | XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len); 425 | XftDrawDestroy(d); 426 | } 427 | 428 | void * 429 | ecalloc(size_t n, size_t size) 430 | { 431 | void *p; 432 | 433 | if (!(p = calloc(n, size))) 434 | die("%s: cannot calloc\n", argv0); 435 | return p; 436 | } 437 | 438 | void * 439 | erealloc(void *o, size_t size) 440 | { 441 | void *p; 442 | 443 | if (!(p = realloc(o, size))) 444 | die("%s: cannot realloc\n", argv0); 445 | return p; 446 | } 447 | 448 | void 449 | expose(const XEvent *e) 450 | { 451 | const XExposeEvent *ev = &e->xexpose; 452 | 453 | if (ev->count == 0 && win == ev->window) 454 | drawbar(); 455 | } 456 | 457 | void 458 | focus(int c) 459 | { 460 | char buf[BUFSIZ] = "tabbed-"VERSION" ::"; 461 | size_t i, n; 462 | XWMHints* wmh; 463 | 464 | /* If c, sel and clients are -1, raise tabbed-win itself */ 465 | if (nclients == 0) { 466 | cmd[cmd_append_pos] = NULL; 467 | for(i = 0, n = strlen(buf); cmd[i] && n < sizeof(buf); i++) 468 | n += snprintf(&buf[n], sizeof(buf) - n, " %s", cmd[i]); 469 | 470 | xsettitle(win, buf); 471 | XChangeProperty(dpy, win, wmatom[WMIcon], XA_CARDINAL, 32, 472 | PropModeReplace, (unsigned char *) icon, ICON_WIDTH * ICON_HEIGHT + 2); 473 | XRaiseWindow(dpy, win); 474 | 475 | return; 476 | } 477 | 478 | if (c < 0 || c >= nclients) 479 | return; 480 | 481 | resize(c, ww, wh - bh); 482 | XRaiseWindow(dpy, clients[c]->win); 483 | XSetInputFocus(dpy, clients[c]->win, RevertToParent, CurrentTime); 484 | sendxembed(c, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0); 485 | sendxembed(c, XEMBED_WINDOW_ACTIVATE, 0, 0, 0); 486 | xsettitle(win, clients[c]->name); 487 | 488 | if (sel != c) { 489 | lastsel = sel; 490 | sel = c; 491 | } 492 | xseticon(); 493 | 494 | if (clients[c]->urgent && (wmh = XGetWMHints(dpy, clients[c]->win))) { 495 | wmh->flags &= ~XUrgencyHint; 496 | XSetWMHints(dpy, clients[c]->win, wmh); 497 | clients[c]->urgent = False; 498 | XFree(wmh); 499 | } 500 | 501 | drawbar(); 502 | XSync(dpy, False); 503 | } 504 | 505 | void 506 | focusin(const XEvent *e) 507 | { 508 | const XFocusChangeEvent *ev = &e->xfocus; 509 | int dummy; 510 | Window focused; 511 | 512 | if (ev->mode != NotifyUngrab) { 513 | XGetInputFocus(dpy, &focused, &dummy); 514 | if (focused == win) 515 | focus(sel); 516 | } 517 | } 518 | 519 | void 520 | focusonce(const Arg *arg) 521 | { 522 | nextfocus = True; 523 | } 524 | 525 | void 526 | focusurgent(const Arg *arg) 527 | { 528 | int c; 529 | 530 | if (sel < 0) 531 | return; 532 | 533 | for (c = (sel + 1) % nclients; c != sel; c = (c + 1) % nclients) { 534 | if (clients[c]->urgent) { 535 | focus(c); 536 | return; 537 | } 538 | } 539 | } 540 | 541 | void 542 | fullscreen(const Arg *arg) 543 | { 544 | XEvent e; 545 | 546 | e.type = ClientMessage; 547 | e.xclient.window = win; 548 | e.xclient.message_type = wmatom[WMState]; 549 | e.xclient.format = 32; 550 | e.xclient.data.l[0] = 2; 551 | e.xclient.data.l[1] = wmatom[WMFullscreen]; 552 | e.xclient.data.l[2] = 0; 553 | XSendEvent(dpy, root, False, SubstructureNotifyMask, &e); 554 | } 555 | 556 | char * 557 | getatom(int a) 558 | { 559 | static char buf[BUFSIZ]; 560 | Atom adummy; 561 | int idummy; 562 | unsigned long ldummy; 563 | unsigned char *p = NULL; 564 | 565 | XGetWindowProperty(dpy, win, wmatom[a], 0L, BUFSIZ, False, XA_STRING, 566 | &adummy, &idummy, &ldummy, &ldummy, &p); 567 | if (p) 568 | strncpy(buf, (char *)p, LENGTH(buf)-1); 569 | else 570 | buf[0] = '\0'; 571 | XFree(p); 572 | 573 | return buf; 574 | } 575 | 576 | int 577 | getclient(Window w) 578 | { 579 | int i; 580 | 581 | for (i = 0; i < nclients; i++) { 582 | if (clients[i]->win == w) 583 | return i; 584 | } 585 | 586 | return -1; 587 | } 588 | 589 | XftColor 590 | getcolor(const char *colstr) 591 | { 592 | XftColor color; 593 | 594 | if (!XftColorAllocName(dpy, visual, cmap, colstr, &color)) 595 | die("%s: cannot allocate color '%s'\n", argv0, colstr); 596 | 597 | return color; 598 | } 599 | 600 | int 601 | getfirsttab(void) 602 | { 603 | int cc, ret; 604 | 605 | if (sel < 0) 606 | return 0; 607 | 608 | cc = ww / tabwidth; 609 | if (nclients > cc) 610 | cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth; 611 | 612 | ret = sel - cc / 2 + (cc + 1) % 2; 613 | return ret < 0 ? 0 : 614 | ret + cc > nclients ? MAX(0, nclients - cc) : 615 | ret; 616 | } 617 | 618 | Bool 619 | gettextprop(Window w, Atom atom, char *text, unsigned int size) 620 | { 621 | char **list = NULL; 622 | int n; 623 | XTextProperty name; 624 | 625 | if (!text || size == 0) 626 | return False; 627 | 628 | text[0] = '\0'; 629 | XGetTextProperty(dpy, w, &name, atom); 630 | if (!name.nitems) 631 | return False; 632 | 633 | if (name.encoding == XA_STRING) { 634 | strncpy(text, (char *)name.value, size - 1); 635 | } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success 636 | && n > 0 && *list) { 637 | strncpy(text, *list, size - 1); 638 | XFreeStringList(list); 639 | } 640 | text[size - 1] = '\0'; 641 | XFree(name.value); 642 | 643 | return True; 644 | } 645 | 646 | void 647 | initfont(const char *fontstr) 648 | { 649 | if (!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr)) 650 | && !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed"))) 651 | die("error, cannot load font: '%s'\n", fontstr); 652 | 653 | dc.font.ascent = dc.font.xfont->ascent; 654 | dc.font.descent = dc.font.xfont->descent; 655 | dc.font.height = dc.font.ascent + dc.font.descent; 656 | } 657 | 658 | Bool 659 | isprotodel(int c) 660 | { 661 | int i, n; 662 | Atom *protocols; 663 | Bool ret = False; 664 | 665 | if (XGetWMProtocols(dpy, clients[c]->win, &protocols, &n)) { 666 | for (i = 0; !ret && i < n; i++) { 667 | if (protocols[i] == wmatom[WMDelete]) 668 | ret = True; 669 | } 670 | XFree(protocols); 671 | } 672 | 673 | return ret; 674 | } 675 | 676 | void 677 | keypress(const XEvent *e) 678 | { 679 | const XKeyEvent *ev = &e->xkey; 680 | unsigned int i; 681 | KeySym keysym; 682 | 683 | keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0); 684 | for (i = 0; i < LENGTH(keys); i++) { 685 | if (keysym == keys[i].keysym && 686 | CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) && 687 | keys[i].func) 688 | keys[i].func(&(keys[i].arg)); 689 | } 690 | } 691 | 692 | void 693 | keyrelease(const XEvent *e) 694 | { 695 | const XKeyEvent *ev = &e->xkey; 696 | unsigned int i; 697 | KeySym keysym; 698 | 699 | keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0); 700 | for (i = 0; i < LENGTH(keyreleases); i++) { 701 | if (keysym == keyreleases[i].keysym && 702 | CLEANMASK(keyreleases[i].mod) == CLEANMASK(ev->state) && 703 | keyreleases[i].func) 704 | keyreleases[i].func(&(keyreleases[i].arg)); 705 | } 706 | } 707 | 708 | void 709 | killclient(const Arg *arg) 710 | { 711 | XEvent ev; 712 | 713 | if (sel < 0) 714 | return; 715 | 716 | if (isprotodel(sel) && !clients[sel]->closed) { 717 | ev.type = ClientMessage; 718 | ev.xclient.window = clients[sel]->win; 719 | ev.xclient.message_type = wmatom[WMProtocols]; 720 | ev.xclient.format = 32; 721 | ev.xclient.data.l[0] = wmatom[WMDelete]; 722 | ev.xclient.data.l[1] = CurrentTime; 723 | XSendEvent(dpy, clients[sel]->win, False, NoEventMask, &ev); 724 | clients[sel]->closed = True; 725 | } else { 726 | XKillClient(dpy, clients[sel]->win); 727 | } 728 | } 729 | 730 | void 731 | manage(Window w) 732 | { 733 | updatenumlockmask(); 734 | { 735 | int i, j, nextpos; 736 | unsigned int modifiers[] = { 0, LockMask, numlockmask, 737 | numlockmask | LockMask }; 738 | KeyCode code; 739 | Client *c; 740 | XEvent e; 741 | 742 | XWithdrawWindow(dpy, w, 0); 743 | XReparentWindow(dpy, w, win, 0, bh); 744 | XSelectInput(dpy, w, PropertyChangeMask | 745 | StructureNotifyMask | EnterWindowMask); 746 | XSync(dpy, False); 747 | 748 | for (i = 0; i < LENGTH(keys); i++) { 749 | if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) { 750 | for (j = 0; j < LENGTH(modifiers); j++) { 751 | XGrabKey(dpy, code, keys[i].mod | 752 | modifiers[j], w, True, 753 | GrabModeAsync, GrabModeAsync); 754 | } 755 | } 756 | } 757 | 758 | for (i = 0; i < LENGTH(keyreleases); i++) { 759 | if ((code = XKeysymToKeycode(dpy, keyreleases[i].keysym))) { 760 | for (j = 0; j < LENGTH(modifiers); j++) { 761 | XGrabKey(dpy, code, keyreleases[i].mod | 762 | modifiers[j], w, True, 763 | GrabModeAsync, GrabModeAsync); 764 | } 765 | } 766 | } 767 | 768 | c = ecalloc(1, sizeof *c); 769 | c->win = w; 770 | 771 | nclients++; 772 | clients = erealloc(clients, sizeof(Client *) * nclients); 773 | 774 | if(npisrelative) { 775 | nextpos = sel + newposition; 776 | } else { 777 | if (newposition < 0) 778 | nextpos = nclients - newposition; 779 | else 780 | nextpos = newposition; 781 | } 782 | if (nextpos >= nclients) 783 | nextpos = nclients - 1; 784 | if (nextpos < 0) 785 | nextpos = 0; 786 | 787 | if (nclients > 1 && nextpos < nclients - 1) 788 | memmove(&clients[nextpos + 1], &clients[nextpos], 789 | sizeof(Client *) * (nclients - nextpos - 1)); 790 | 791 | clients[nextpos] = c; 792 | updatetitle(nextpos); 793 | 794 | XLowerWindow(dpy, w); 795 | XMapWindow(dpy, w); 796 | 797 | e.xclient.window = w; 798 | e.xclient.type = ClientMessage; 799 | e.xclient.message_type = wmatom[XEmbed]; 800 | e.xclient.format = 32; 801 | e.xclient.data.l[0] = CurrentTime; 802 | e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY; 803 | e.xclient.data.l[2] = 0; 804 | e.xclient.data.l[3] = win; 805 | e.xclient.data.l[4] = 0; 806 | XSendEvent(dpy, root, False, NoEventMask, &e); 807 | 808 | XSync(dpy, False); 809 | 810 | /* Adjust sel before focus does set it to lastsel. */ 811 | if (sel >= nextpos) 812 | sel++; 813 | focus(nextfocus ? nextpos : 814 | sel < 0 ? 0 : 815 | sel); 816 | nextfocus = foreground; 817 | } 818 | } 819 | 820 | void 821 | maprequest(const XEvent *e) 822 | { 823 | const XMapRequestEvent *ev = &e->xmaprequest; 824 | 825 | if (getclient(ev->window) < 0) 826 | manage(ev->window); 827 | } 828 | 829 | void 830 | move(const Arg *arg) 831 | { 832 | if (arg->i >= 0 && arg->i < nclients) 833 | focus(arg->i); 834 | } 835 | 836 | void 837 | movetab(const Arg *arg) 838 | { 839 | int c; 840 | Client *new; 841 | 842 | if (sel < 0) 843 | return; 844 | 845 | c = (sel + arg->i) % nclients; 846 | if (c < 0) 847 | c += nclients; 848 | 849 | if (c == sel) 850 | return; 851 | 852 | new = clients[sel]; 853 | if (sel < c) 854 | memmove(&clients[sel], &clients[sel+1], 855 | sizeof(Client *) * (c - sel)); 856 | else 857 | memmove(&clients[c+1], &clients[c], 858 | sizeof(Client *) * (sel - c)); 859 | clients[c] = new; 860 | sel = c; 861 | 862 | drawbar(); 863 | } 864 | 865 | void 866 | propertynotify(const XEvent *e) 867 | { 868 | const XPropertyEvent *ev = &e->xproperty; 869 | XWMHints *wmh; 870 | int c; 871 | char* selection = NULL; 872 | Arg arg; 873 | 874 | if (ev->state == PropertyNewValue && ev->atom == wmatom[WMSelectTab]) { 875 | selection = getatom(WMSelectTab); 876 | if (!strncmp(selection, "0x", 2)) { 877 | arg.i = getclient(strtoul(selection, NULL, 0)); 878 | move(&arg); 879 | } else { 880 | cmd[cmd_append_pos] = selection; 881 | arg.v = cmd; 882 | spawn(&arg); 883 | } 884 | } else if (ev->state == PropertyNewValue && ev->atom == XA_WM_HINTS && 885 | (c = getclient(ev->window)) > -1 && 886 | (wmh = XGetWMHints(dpy, clients[c]->win))) { 887 | if (wmh->flags & XUrgencyHint) { 888 | XFree(wmh); 889 | wmh = XGetWMHints(dpy, win); 890 | if (c != sel) { 891 | if (urgentswitch && wmh && 892 | !(wmh->flags & XUrgencyHint)) { 893 | /* only switch, if tabbed was focused 894 | * since last urgency hint if WMHints 895 | * could not be received, 896 | * default to no switch */ 897 | focus(c); 898 | } else { 899 | /* if no switch should be performed, 900 | * mark tab as urgent */ 901 | clients[c]->urgent = True; 902 | drawbar(); 903 | } 904 | } 905 | if (wmh && !(wmh->flags & XUrgencyHint)) { 906 | /* update tabbed urgency hint 907 | * if not set already */ 908 | wmh->flags |= XUrgencyHint; 909 | XSetWMHints(dpy, win, wmh); 910 | } 911 | } 912 | XFree(wmh); 913 | if (c == sel) 914 | xseticon(); 915 | } else if (ev->state != PropertyDelete && ev->atom == XA_WM_NAME && 916 | (c = getclient(ev->window)) > -1) { 917 | updatetitle(c); 918 | } else if (ev->atom == wmatom[WMIcon] && (c = getclient(ev->window)) > -1 && c == sel) { 919 | xseticon(); 920 | } 921 | } 922 | 923 | void 924 | resize(int c, int w, int h) 925 | { 926 | XConfigureEvent ce; 927 | XWindowChanges wc; 928 | 929 | ce.x = 0; 930 | ce.y = bh; 931 | ce.width = wc.width = w; 932 | ce.height = wc.height = h; 933 | ce.type = ConfigureNotify; 934 | ce.display = dpy; 935 | ce.event = clients[c]->win; 936 | ce.window = clients[c]->win; 937 | ce.above = None; 938 | ce.override_redirect = False; 939 | ce.border_width = 0; 940 | 941 | XConfigureWindow(dpy, clients[c]->win, CWWidth | CWHeight, &wc); 942 | XSendEvent(dpy, clients[c]->win, False, StructureNotifyMask, 943 | (XEvent *)&ce); 944 | } 945 | 946 | void 947 | rotate(const Arg *arg) 948 | { 949 | int nsel = -1; 950 | 951 | if (sel < 0) 952 | return; 953 | 954 | if (arg->i == 0) { 955 | if (lastsel > -1) 956 | focus(lastsel); 957 | } else if (sel > -1) { 958 | /* Rotating in an arg->i step around the clients. */ 959 | nsel = sel + arg->i; 960 | while (nsel >= nclients) 961 | nsel -= nclients; 962 | while (nsel < 0) 963 | nsel += nclients; 964 | focus(nsel); 965 | } 966 | } 967 | 968 | void 969 | run(void) 970 | { 971 | XEvent ev; 972 | 973 | /* main event loop */ 974 | XSync(dpy, False); 975 | drawbar(); 976 | if (doinitspawn == True) 977 | spawn(NULL); 978 | 979 | while (running) { 980 | XNextEvent(dpy, &ev); 981 | if (handler[ev.type]) 982 | (handler[ev.type])(&ev); /* call handler */ 983 | } 984 | } 985 | 986 | void 987 | sendxembed(int c, long msg, long detail, long d1, long d2) 988 | { 989 | XEvent e = { 0 }; 990 | 991 | e.xclient.window = clients[c]->win; 992 | e.xclient.type = ClientMessage; 993 | e.xclient.message_type = wmatom[XEmbed]; 994 | e.xclient.format = 32; 995 | e.xclient.data.l[0] = CurrentTime; 996 | e.xclient.data.l[1] = msg; 997 | e.xclient.data.l[2] = detail; 998 | e.xclient.data.l[3] = d1; 999 | e.xclient.data.l[4] = d2; 1000 | XSendEvent(dpy, clients[c]->win, False, NoEventMask, &e); 1001 | } 1002 | 1003 | void 1004 | setcmd(int argc, char *argv[], int replace) 1005 | { 1006 | int i; 1007 | 1008 | cmd = ecalloc(argc + 3, sizeof(*cmd)); 1009 | if (argc == 0) 1010 | return; 1011 | for (i = 0; i < argc; i++) 1012 | cmd[i] = argv[i]; 1013 | cmd[replace > 0 ? replace : argc] = winid; 1014 | cmd_append_pos = argc + !replace; 1015 | cmd[cmd_append_pos] = cmd[cmd_append_pos + 1] = NULL; 1016 | } 1017 | 1018 | void 1019 | setup(void) 1020 | { 1021 | int bitm, tx, ty, tw, th, dh, dw, isfixed; 1022 | XWMHints *wmh; 1023 | XClassHint class_hint; 1024 | XSizeHints *size_hint; 1025 | 1026 | /* clean up any zombies immediately */ 1027 | sigchld(0); 1028 | 1029 | /* init screen */ 1030 | screen = DefaultScreen(dpy); 1031 | root = RootWindow(dpy, screen); 1032 | initfont(font); 1033 | vbh = dc.h = dc.font.height + 2; 1034 | 1035 | /* init atoms */ 1036 | wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1037 | wmatom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); 1038 | wmatom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1039 | wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1040 | wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False); 1041 | wmatom[WMSelectApp] = XInternAtom(dpy, "_TABBED_SELECT_TERMAPP", False); 1042 | wmatom[WMSelectApp] = XInternAtom(dpy, "_TABBED_TERM", False); 1043 | wmatom[WMAttachWin] = XInternAtom(dpy, "_TABBED_ATTACH_WIN", False); 1044 | wmatom[WMAttachWin] = XInternAtom(dpy, "_TABBED_ATTACH_ALL", False); 1045 | wmatom[WMDetachWin] = XInternAtom(dpy, "_TABBED_DETACH_WIN", False); 1046 | wmatom[WMDetachWin] = XInternAtom(dpy, "_TABBED_DETACH_ALL", False); 1047 | wmatom[WMHiddenWin] = XInternAtom(dpy, "_TABBED_HIDE_WINDOW", False); 1048 | wmatom[WMHiddenWin] = XInternAtom(dpy, "_TABBED_SHOW_HIDDEN", False); 1049 | wmatom[WMHiddenWin] = XInternAtom(dpy, "_TABBED_SHOW_HIDDEN_ALL", False); 1050 | wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 1051 | wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False); 1052 | wmatom[WMIcon] = XInternAtom(dpy, "_NET_WM_ICON", False); 1053 | 1054 | /* init appearance */ 1055 | wx = 0; 1056 | wy = 0; 1057 | ww = 800; 1058 | wh = 600; 1059 | isfixed = 0; 1060 | 1061 | if (geometry) { 1062 | tx = ty = tw = th = 0; 1063 | bitm = XParseGeometry(geometry, &tx, &ty, (unsigned *)&tw, 1064 | (unsigned *)&th); 1065 | if (bitm & XValue) 1066 | wx = tx; 1067 | if (bitm & YValue) 1068 | wy = ty; 1069 | if (bitm & WidthValue) 1070 | ww = tw; 1071 | if (bitm & HeightValue) 1072 | wh = th; 1073 | if (bitm & XNegative && wx == 0) 1074 | wx = -1; 1075 | if (bitm & YNegative && wy == 0) 1076 | wy = -1; 1077 | if (bitm & (HeightValue | WidthValue)) 1078 | isfixed = 1; 1079 | 1080 | dw = DisplayWidth(dpy, screen); 1081 | dh = DisplayHeight(dpy, screen); 1082 | if (wx < 0) 1083 | wx = dw + wx - ww - 1; 1084 | if (wy < 0) 1085 | wy = dh + wy - wh - 1; 1086 | } 1087 | 1088 | XVisualInfo *vis; 1089 | XRenderPictFormat *fmt; 1090 | int nvi; 1091 | int i; 1092 | 1093 | XVisualInfo tpl = { 1094 | .screen = screen, 1095 | .depth = 32, 1096 | .class = TrueColor 1097 | }; 1098 | 1099 | vis = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask | VisualClassMask, &tpl, &nvi); 1100 | for(i = 0; i < nvi; i ++) { 1101 | fmt = XRenderFindVisualFormat(dpy, vis[i].visual); 1102 | if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { 1103 | visual = vis[i].visual; 1104 | break; 1105 | } 1106 | } 1107 | 1108 | XFree(vis); 1109 | 1110 | if (! visual) { 1111 | fprintf(stderr, "Couldn't find ARGB visual.\n"); 1112 | exit(1); 1113 | } 1114 | 1115 | cmap = XCreateColormap( dpy, root, visual, None); 1116 | dc.norm[ColBG] = getcolor(normbgcolor); 1117 | dc.norm[ColFG] = getcolor(normfgcolor); 1118 | dc.sel[ColBG] = getcolor(selbgcolor); 1119 | dc.sel[ColFG] = getcolor(selfgcolor); 1120 | dc.urg[ColBG] = getcolor(urgbgcolor); 1121 | dc.urg[ColFG] = getcolor(urgfgcolor); 1122 | 1123 | XSetWindowAttributes attrs; 1124 | attrs.background_pixel = dc.norm[ColBG].pixel; 1125 | attrs.border_pixel = dc.norm[ColFG].pixel; 1126 | attrs.bit_gravity = NorthWestGravity; 1127 | attrs.event_mask = FocusChangeMask | KeyPressMask 1128 | | ExposureMask | VisibilityChangeMask | StructureNotifyMask 1129 | | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask; 1130 | attrs.background_pixmap = None ; 1131 | attrs.colormap = cmap; 1132 | 1133 | win = XCreateWindow(dpy, root, wx, wy, 1134 | ww, wh, 0, 32, InputOutput, 1135 | visual, CWBackPixmap | CWBorderPixel | CWBitGravity 1136 | | CWEventMask | CWColormap, &attrs); 1137 | 1138 | dc.drawable = XCreatePixmap(dpy, win, ww, wh, 1139 | 32); 1140 | dc.gc = XCreateGC(dpy, dc.drawable, 0, 0); 1141 | 1142 | XMapRaised(dpy, win); 1143 | XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask | 1144 | ButtonPressMask | ExposureMask | KeyPressMask | 1145 | KeyReleaseMask | PropertyChangeMask | StructureNotifyMask | 1146 | SubstructureRedirectMask); 1147 | xerrorxlib = XSetErrorHandler(xerror); 1148 | 1149 | class_hint.res_name = wmname; 1150 | class_hint.res_class = "tabbed"; 1151 | XSetClassHint(dpy, win, &class_hint); 1152 | 1153 | size_hint = XAllocSizeHints(); 1154 | if (!isfixed) { 1155 | size_hint->flags = PSize; 1156 | size_hint->height = wh; 1157 | size_hint->width = ww; 1158 | } else { 1159 | size_hint->flags = PMaxSize | PMinSize; 1160 | size_hint->min_width = size_hint->max_width = ww; 1161 | size_hint->min_height = size_hint->max_height = wh; 1162 | } 1163 | wmh = XAllocWMHints(); 1164 | XSetWMProperties(dpy, win, NULL, NULL, NULL, 0, size_hint, wmh, NULL); 1165 | XFree(size_hint); 1166 | XFree(wmh); 1167 | 1168 | XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1); 1169 | 1170 | snprintf(winid, sizeof(winid), "%lu", win); 1171 | setenv("XEMBED", winid, 1); 1172 | 1173 | /* change icon from RGBA to ARGB */ 1174 | icon[0] = ICON_WIDTH; 1175 | icon[1] = ICON_HEIGHT; 1176 | for (int i = 0; i < ICON_WIDTH * ICON_HEIGHT; ++i) { 1177 | icon[i + 2] = 1178 | ICON_PIXEL_DATA[i * 4 + 3] << 24 | 1179 | ICON_PIXEL_DATA[i * 4 + 0] << 0 | 1180 | ICON_PIXEL_DATA[i * 4 + 1] << 8 | 1181 | ICON_PIXEL_DATA[i * 4 + 2] << 16 ; 1182 | } 1183 | 1184 | nextfocus = foreground; 1185 | focus(-1); 1186 | } 1187 | 1188 | void 1189 | showbar(const Arg *arg) 1190 | { 1191 | barvisibility = arg->i; 1192 | drawbar(); 1193 | } 1194 | 1195 | void 1196 | sigchld(int unused) 1197 | { 1198 | if (signal(SIGCHLD, sigchld) == SIG_ERR) 1199 | die("%s: cannot install SIGCHLD handler", argv0); 1200 | 1201 | while (0 < waitpid(-1, NULL, WNOHANG)); 1202 | } 1203 | 1204 | void 1205 | spawn(const Arg *arg) 1206 | { 1207 | if (fork() == 0) { 1208 | if(dpy) 1209 | close(ConnectionNumber(dpy)); 1210 | 1211 | setsid(); 1212 | if (arg && arg->v) { 1213 | execvp(((char **)arg->v)[0], (char **)arg->v); 1214 | fprintf(stderr, "%s: execvp %s", argv0, 1215 | ((char **)arg->v)[0]); 1216 | } else { 1217 | cmd[cmd_append_pos] = NULL; 1218 | execvp(cmd[0], cmd); 1219 | fprintf(stderr, "%s: execvp %s", argv0, cmd[0]); 1220 | } 1221 | perror(" failed"); 1222 | exit(0); 1223 | } 1224 | } 1225 | 1226 | int 1227 | textnw(const char *text, unsigned int len) 1228 | { 1229 | XGlyphInfo ext; 1230 | XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext); 1231 | return ext.xOff; 1232 | } 1233 | 1234 | void 1235 | toggle(const Arg *arg) 1236 | { 1237 | *(Bool*) arg->v = !*(Bool*) arg->v; 1238 | } 1239 | 1240 | void 1241 | unmanage(int c) 1242 | { 1243 | if (c < 0 || c >= nclients) { 1244 | drawbar(); 1245 | XSync(dpy, False); 1246 | return; 1247 | } 1248 | 1249 | if (!nclients) 1250 | return; 1251 | 1252 | if (c == 0) { 1253 | /* First client. */ 1254 | nclients--; 1255 | free(clients[0]); 1256 | memmove(&clients[0], &clients[1], sizeof(Client *) * nclients); 1257 | } else if (c == nclients - 1) { 1258 | /* Last client. */ 1259 | nclients--; 1260 | free(clients[c]); 1261 | clients = erealloc(clients, sizeof(Client *) * nclients); 1262 | } else { 1263 | /* Somewhere inbetween. */ 1264 | free(clients[c]); 1265 | memmove(&clients[c], &clients[c+1], 1266 | sizeof(Client *) * (nclients - (c + 1))); 1267 | nclients--; 1268 | } 1269 | 1270 | if (nclients <= 0) { 1271 | lastsel = sel = -1; 1272 | 1273 | if (closelastclient) 1274 | running = False; 1275 | else if (fillagain && running) 1276 | spawn(NULL); 1277 | } else { 1278 | if (lastsel >= nclients) 1279 | lastsel = nclients - 1; 1280 | else if (lastsel > c) 1281 | lastsel--; 1282 | 1283 | if (c == sel && lastsel >= 0) { 1284 | focus(lastsel); 1285 | } else { 1286 | if (sel > c) 1287 | sel--; 1288 | if (sel >= nclients) 1289 | sel = nclients - 1; 1290 | 1291 | focus(sel); 1292 | } 1293 | } 1294 | 1295 | drawbar(); 1296 | XSync(dpy, False); 1297 | } 1298 | 1299 | void 1300 | unmapnotify(const XEvent *e) 1301 | { 1302 | const XUnmapEvent *ev = &e->xunmap; 1303 | int c; 1304 | 1305 | if ((c = getclient(ev->window)) > -1) 1306 | unmanage(c); 1307 | } 1308 | 1309 | void 1310 | updatenumlockmask(void) 1311 | { 1312 | unsigned int i, j; 1313 | XModifierKeymap *modmap; 1314 | 1315 | numlockmask = 0; 1316 | modmap = XGetModifierMapping(dpy); 1317 | for (i = 0; i < 8; i++) { 1318 | for (j = 0; j < modmap->max_keypermod; j++) { 1319 | if (modmap->modifiermap[i * modmap->max_keypermod + j] 1320 | == XKeysymToKeycode(dpy, XK_Num_Lock)) 1321 | numlockmask = (1 << i); 1322 | } 1323 | } 1324 | XFreeModifiermap(modmap); 1325 | } 1326 | 1327 | void 1328 | updatetitle(int c) 1329 | { 1330 | if (!gettextprop(clients[c]->win, wmatom[WMName], clients[c]->name, 1331 | sizeof(clients[c]->name))) 1332 | gettextprop(clients[c]->win, XA_WM_NAME, clients[c]->name, 1333 | sizeof(clients[c]->name)); 1334 | if (sel == c) 1335 | xsettitle(win, clients[c]->name); 1336 | drawbar(); 1337 | } 1338 | 1339 | /* There's no way to check accesses to destroyed windows, thus those cases are 1340 | * ignored (especially on UnmapNotify's). Other types of errors call Xlibs 1341 | * default error handler, which may call exit. */ 1342 | int 1343 | xerror(Display *dpy, XErrorEvent *ee) 1344 | { 1345 | if (ee->error_code == BadWindow 1346 | || (ee->request_code == X_SetInputFocus && 1347 | ee->error_code == BadMatch) 1348 | || (ee->request_code == X_PolyText8 && 1349 | ee->error_code == BadDrawable) 1350 | || (ee->request_code == X_PolyFillRectangle && 1351 | ee->error_code == BadDrawable) 1352 | || (ee->request_code == X_PolySegment && 1353 | ee->error_code == BadDrawable) 1354 | || (ee->request_code == X_ConfigureWindow && 1355 | ee->error_code == BadMatch) 1356 | || (ee->request_code == X_GrabButton && 1357 | ee->error_code == BadAccess) 1358 | || (ee->request_code == X_GrabKey && 1359 | ee->error_code == BadAccess) 1360 | || (ee->request_code == X_CopyArea && 1361 | ee->error_code == BadDrawable)) 1362 | return 0; 1363 | 1364 | fprintf(stderr, "%s: fatal error: request code=%d, error code=%d\n", 1365 | argv0, ee->request_code, ee->error_code); 1366 | return xerrorxlib(dpy, ee); /* may call exit */ 1367 | } 1368 | 1369 | void 1370 | xsettitle(Window w, const char *str) 1371 | { 1372 | XTextProperty xtp; 1373 | 1374 | if (XmbTextListToTextProperty(dpy, (char **)&str, 1, 1375 | XCompoundTextStyle, &xtp) == Success) { 1376 | XSetTextProperty(dpy, w, &xtp, wmatom[WMName]); 1377 | XSetTextProperty(dpy, w, &xtp, XA_WM_NAME); 1378 | XFree(xtp.value); 1379 | } 1380 | } 1381 | 1382 | void 1383 | xseticon(void) 1384 | { 1385 | Atom ret_type; 1386 | XWMHints *wmh, *cwmh; 1387 | int ret_format; 1388 | unsigned long ret_nitems, ret_nleft; 1389 | long offset = 0L; 1390 | unsigned char *data; 1391 | 1392 | wmh = XGetWMHints(dpy, win); 1393 | wmh->flags &= ~(IconPixmapHint | IconMaskHint); 1394 | wmh->icon_pixmap = wmh->icon_mask = None; 1395 | 1396 | 1397 | if (XGetWindowProperty(dpy, clients[sel]->win, wmatom[WMIcon], offset, LONG_MAX, False, 1398 | XA_CARDINAL, &ret_type, &ret_format, &ret_nitems, 1399 | &ret_nleft, &data) == Success && 1400 | ret_type == XA_CARDINAL && ret_format == 32) 1401 | { 1402 | XChangeProperty(dpy, win, wmatom[WMIcon], XA_CARDINAL, 32, 1403 | PropModeReplace, data, ret_nitems); 1404 | } else if ((cwmh = XGetWMHints(dpy, clients[sel]->win)) && cwmh->flags & IconPixmapHint) { 1405 | XDeleteProperty(dpy, win, wmatom[WMIcon]); 1406 | wmh->flags |= IconPixmapHint; 1407 | wmh->icon_pixmap = cwmh->icon_pixmap; 1408 | if (cwmh->flags & IconMaskHint) { 1409 | wmh->flags |= IconMaskHint; 1410 | wmh->icon_mask = cwmh->icon_mask; 1411 | } 1412 | XFree(cwmh); 1413 | } else { 1414 | XChangeProperty(dpy, win, wmatom[WMIcon], XA_CARDINAL, 32, 1415 | PropModeReplace, (unsigned char *) icon, ICON_WIDTH * ICON_HEIGHT + 2); 1416 | } 1417 | XSetWMHints(dpy, win, wmh); 1418 | XFree(wmh); 1419 | XFree(data); 1420 | } 1421 | 1422 | void 1423 | usage(void) 1424 | { 1425 | die("usage: %s [-dfksv] [-g geometry] [-n name] [-p [s+/-]pos]\n" 1426 | " [-r narg] [-o color] [-O color] [-t color] [-T color]\n" 1427 | " [-u color] [-U color] command...\n", argv0); 1428 | } 1429 | 1430 | int 1431 | main(int argc, char *argv[]) 1432 | { 1433 | Bool detach = False; 1434 | int replace = 0; 1435 | char *pstr; 1436 | 1437 | ARGBEGIN { 1438 | case 'c': 1439 | closelastclient = True; 1440 | fillagain = False; 1441 | break; 1442 | case 'd': 1443 | detach = True; 1444 | break; 1445 | case 'f': 1446 | fillagain = True; 1447 | break; 1448 | case 'g': 1449 | geometry = EARGF(usage()); 1450 | break; 1451 | case 'k': 1452 | killclientsfirst = True; 1453 | break; 1454 | case 'n': 1455 | wmname = EARGF(usage()); 1456 | break; 1457 | case 'O': 1458 | normfgcolor = EARGF(usage()); 1459 | break; 1460 | case 'o': 1461 | normbgcolor = EARGF(usage()); 1462 | break; 1463 | case 'p': 1464 | pstr = EARGF(usage()); 1465 | if (pstr[0] == 's') { 1466 | npisrelative = True; 1467 | newposition = atoi(&pstr[1]); 1468 | } else { 1469 | newposition = atoi(pstr); 1470 | } 1471 | break; 1472 | case 'r': 1473 | replace = atoi(EARGF(usage())); 1474 | break; 1475 | case 's': 1476 | doinitspawn = False; 1477 | break; 1478 | case 'T': 1479 | selfgcolor = EARGF(usage()); 1480 | break; 1481 | case 't': 1482 | selbgcolor = EARGF(usage()); 1483 | break; 1484 | case 'U': 1485 | urgfgcolor = EARGF(usage()); 1486 | break; 1487 | case 'u': 1488 | urgbgcolor = EARGF(usage()); 1489 | break; 1490 | case 'v': 1491 | die("tabbed-"VERSION", © 2009-2016 tabbed engineers, " 1492 | "see LICENSE for details.\n"); 1493 | break; 1494 | default: 1495 | usage(); 1496 | break; 1497 | } ARGEND; 1498 | 1499 | if (argc < 1) { 1500 | doinitspawn = False; 1501 | fillagain = False; 1502 | } 1503 | 1504 | setcmd(argc, argv, replace); 1505 | 1506 | if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 1507 | fprintf(stderr, "%s: no locale support\n", argv0); 1508 | if (!(dpy = XOpenDisplay(NULL))) 1509 | die("%s: cannot open display\n", argv0); 1510 | 1511 | setup(); 1512 | printf("0x%lx\n", win); 1513 | fflush(NULL); 1514 | 1515 | if (detach) { 1516 | if (fork() == 0) { 1517 | fclose(stdout); 1518 | } else { 1519 | if (dpy) 1520 | close(ConnectionNumber(dpy)); 1521 | return EXIT_SUCCESS; 1522 | } 1523 | } 1524 | 1525 | run(); 1526 | cleanup(); 1527 | XCloseDisplay(dpy); 1528 | 1529 | return EXIT_SUCCESS; 1530 | } 1531 | --------------------------------------------------------------------------------