2 * See LICENSE file for copyright and license details.
13 #include <X11/Xatom.h>
14 #include <X11/keysym.h>
16 #include <X11/Xproto.h>
17 #include <X11/Xutil.h>
18 #include <X11/XKBlib.h>
19 #include <X11/Xft/Xft.h>
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
39 /* Details for XEMBED_FOCUS_IN: */
40 #define XEMBED_FOCUS_CURRENT 0
41 #define XEMBED_FOCUS_FIRST 1
42 #define XEMBED_FOCUS_LAST 2
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)
51 enum { ColFG
, ColBG
, ColLast
}; /* color */
52 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMFullscreen
,
53 XEmbed
, WMSelectTab
, WMLast
}; /* default atoms */
63 void (*func
)(const Arg
*);
69 XftColor norm
[ColLast
];
70 XftColor sel
[ColLast
];
71 XftColor urg
[ColLast
];
80 } DC
; /* draw context */
90 /* function declarations */
91 static void buttonpress(const XEvent
*e
);
92 static void cleanup(void);
93 static void clientmessage(const XEvent
*e
);
94 static void configurenotify(const XEvent
*e
);
95 static void configurerequest(const XEvent
*e
);
96 static void createnotify(const XEvent
*e
);
97 static void destroynotify(const XEvent
*e
);
98 static void die(const char *errstr
, ...);
99 static void drawbar(void);
100 static void drawtext(const char *text
, XftColor col
[ColLast
]);
101 static void *ecalloc(size_t n
, size_t size
);
102 static void *erealloc(void *o
, size_t size
);
103 static void expose(const XEvent
*e
);
104 static void focus(int c
);
105 static void focusin(const XEvent
*e
);
106 static void focusonce(const Arg
*arg
);
107 static void focusurgent(const Arg
*arg
);
108 static void fullscreen(const Arg
*arg
);
109 static char *getatom(int a
);
110 static int getclient(Window w
);
111 static XftColor
getcolor(const char *colstr
);
112 static int getfirsttab(void);
113 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
114 static void initfont(const char *fontstr
);
115 static Bool
isprotodel(int c
);
116 static void keypress(const XEvent
*e
);
117 static void killclient(const Arg
*arg
);
118 static void manage(Window win
);
119 static void maprequest(const XEvent
*e
);
120 static void move(const Arg
*arg
);
121 static void movetab(const Arg
*arg
);
122 static void propertynotify(const XEvent
*e
);
123 static void resize(int c
, int w
, int h
);
124 static void rotate(const Arg
*arg
);
125 static void run(void);
126 static void sendxembed(int c
, long msg
, long detail
, long d1
, long d2
);
127 static void setcmd(int argc
, char *argv
[], int);
128 static void setup(void);
129 static void spawn(const Arg
*arg
);
130 static int textnw(const char *text
, unsigned int len
);
131 static void toggle(const Arg
*arg
);
132 static void unmanage(int c
);
133 static void unmapnotify(const XEvent
*e
);
134 static void updatenumlockmask(void);
135 static void updatetitle(int c
);
136 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
137 static void xsettitle(Window w
, const char *str
);
141 static void (*handler
[LASTEvent
]) (const XEvent
*) = {
142 [ButtonPress
] = buttonpress
,
143 [ClientMessage
] = clientmessage
,
144 [ConfigureNotify
] = configurenotify
,
145 [ConfigureRequest
] = configurerequest
,
146 [CreateNotify
] = createnotify
,
147 [UnmapNotify
] = unmapnotify
,
148 [DestroyNotify
] = destroynotify
,
151 [KeyPress
] = keypress
,
152 [MapRequest
] = maprequest
,
153 [PropertyNotify
] = propertynotify
,
155 static int bh
, obh
, wx
, wy
, ww
, wh
;
156 static unsigned int numlockmask
;
157 static Bool running
= True
, nextfocus
, doinitspawn
= True
,
158 fillagain
= False
, closelastclient
= False
,
159 killclientsfirst
= False
;
162 static Atom wmatom
[WMLast
];
163 static Window root
, win
;
164 static Client
**clients
;
165 static int nclients
, sel
= -1, lastsel
= -1;
166 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
167 static int cmd_append_pos
;
168 static char winid
[64];
170 static char *wmname
= "tabbed";
171 static const char *geometry
;
175 /* configuration, allows nested code to access above variables */
179 buttonpress(const XEvent
*e
)
181 const XButtonPressedEvent
*ev
= &e
->xbutton
;
185 if (ev
->y
< 0 || ev
->y
> bh
)
188 if (((fc
= getfirsttab()) > 0 && ev
->x
< TEXTW(before
)) || ev
->x
< 0)
191 for (i
= fc
; i
< nclients
; i
++) {
192 if (clients
[i
]->tabx
> ev
->x
) {
193 switch (ev
->button
) {
201 case Button4
: /* FALLTHROUGH */
203 arg
.i
= ev
->button
== Button4
? -1 : 1;
217 for (i
= 0; i
< nclients
; i
++) {
220 XReparentWindow(dpy
, clients
[i
]->win
, root
, 0, 0);
226 XFreePixmap(dpy
, dc
.drawable
);
228 XDestroyWindow(dpy
, win
);
234 clientmessage(const XEvent
*e
)
236 const XClientMessageEvent
*ev
= &e
->xclient
;
238 if (ev
->message_type
== wmatom
[WMProtocols
] &&
239 ev
->data
.l
[0] == wmatom
[WMDelete
]) {
240 if (nclients
> 1 && killclientsfirst
) {
249 configurenotify(const XEvent
*e
)
251 const XConfigureEvent
*ev
= &e
->xconfigure
;
253 if (ev
->window
== win
&& (ev
->width
!= ww
|| ev
->height
!= wh
)) {
256 XFreePixmap(dpy
, dc
.drawable
);
257 dc
.drawable
= XCreatePixmap(dpy
, root
, ww
, wh
,
258 DefaultDepth(dpy
, screen
));
260 if (!obh
&& (wh
<= bh
)) {
263 } else if (!bh
&& (wh
> obh
)) {
269 resize(sel
, ww
, wh
- bh
);
275 configurerequest(const XEvent
*e
)
277 const XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
281 if ((c
= getclient(ev
->window
)) > -1) {
287 wc
.sibling
= ev
->above
;
288 wc
.stack_mode
= ev
->detail
;
289 XConfigureWindow(dpy
, clients
[c
]->win
, ev
->value_mask
, &wc
);
294 createnotify(const XEvent
*e
)
296 const XCreateWindowEvent
*ev
= &e
->xcreatewindow
;
298 if (ev
->window
!= win
&& getclient(ev
->window
) < 0)
303 destroynotify(const XEvent
*e
)
305 const XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
308 if ((c
= getclient(ev
->window
)) > -1)
313 die(const char *errstr
, ...)
317 va_start(ap
, errstr
);
318 vfprintf(stderr
, errstr
, ap
);
327 int c
, cc
, fc
, width
;
333 XFetchName(dpy
, win
, &name
);
334 drawtext(name
? name
: "", dc
.norm
);
335 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, ww
, bh
, 0, 0);
344 cc
= (ww
- TEXTW(before
) - TEXTW(after
)) / tabwidth
;
346 if ((fc
= getfirsttab()) + cc
< nclients
) {
349 drawtext(after
, dc
.sel
);
355 dc
.w
= TEXTW(before
);
356 drawtext(before
, dc
.sel
);
361 cc
= MIN(cc
, nclients
);
362 for (c
= fc
; c
< fc
+ cc
; c
++) {
368 col
= clients
[c
]->urgent
? dc
.urg
: dc
.norm
;
370 drawtext(clients
[c
]->name
, col
);
372 clients
[c
]->tabx
= dc
.x
;
374 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, ww
, bh
, 0, 0);
379 drawtext(const char *text
, XftColor col
[ColLast
])
381 int i
, j
, x
, y
, h
, len
, olen
;
384 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
386 XSetForeground(dpy
, dc
.gc
, col
[ColBG
].pixel
);
387 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
392 h
= dc
.font
.ascent
+ dc
.font
.descent
;
393 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
396 /* shorten text if necessary */
397 for (len
= MIN(olen
, sizeof(buf
));
398 len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
403 memcpy(buf
, text
, len
);
405 for (i
= len
, j
= strlen(titletrim
); j
&& i
;
406 buf
[--i
] = titletrim
[--j
])
410 d
= XftDrawCreate(dpy
, dc
.drawable
, DefaultVisual(dpy
, screen
), DefaultColormap(dpy
, screen
));
411 XftDrawStringUtf8(d
, &col
[ColFG
], dc
.font
.xfont
, x
, y
, (XftChar8
*) buf
, len
);
416 ecalloc(size_t n
, size_t size
)
420 if (!(p
= calloc(n
, size
)))
421 die("%s: cannot calloc\n", argv0
);
426 erealloc(void *o
, size_t size
)
430 if (!(p
= realloc(o
, size
)))
431 die("%s: cannot realloc\n", argv0
);
436 expose(const XEvent
*e
)
438 const XExposeEvent
*ev
= &e
->xexpose
;
440 if (ev
->count
== 0 && win
== ev
->window
)
447 char buf
[BUFSIZ
] = "tabbed-"VERSION
" ::";
451 /* If c, sel and clients are -1, raise tabbed-win itself */
453 cmd
[cmd_append_pos
] = NULL
;
454 for(i
= 0, n
= strlen(buf
); cmd
[i
] && n
< sizeof(buf
); i
++)
455 n
+= snprintf(&buf
[n
], sizeof(buf
) - n
, " %s", cmd
[i
]);
458 XRaiseWindow(dpy
, win
);
463 if (c
< 0 || c
>= nclients
)
466 resize(c
, ww
, wh
- bh
);
467 XRaiseWindow(dpy
, clients
[c
]->win
);
468 XSetInputFocus(dpy
, clients
[c
]->win
, RevertToParent
, CurrentTime
);
469 sendxembed(c
, XEMBED_FOCUS_IN
, XEMBED_FOCUS_CURRENT
, 0, 0);
470 sendxembed(c
, XEMBED_WINDOW_ACTIVATE
, 0, 0, 0);
471 xsettitle(win
, clients
[c
]->name
);
478 if (clients
[c
]->urgent
&& (wmh
= XGetWMHints(dpy
, clients
[c
]->win
))) {
479 wmh
->flags
&= ~XUrgencyHint
;
480 XSetWMHints(dpy
, clients
[c
]->win
, wmh
);
481 clients
[c
]->urgent
= False
;
490 focusin(const XEvent
*e
)
492 const XFocusChangeEvent
*ev
= &e
->xfocus
;
496 if (ev
->mode
!= NotifyUngrab
) {
497 XGetInputFocus(dpy
, &focused
, &dummy
);
504 focusonce(const Arg
*arg
)
510 focusurgent(const Arg
*arg
)
517 for (c
= (sel
+ 1) % nclients
; c
!= sel
; c
= (c
+ 1) % nclients
) {
518 if (clients
[c
]->urgent
) {
526 fullscreen(const Arg
*arg
)
530 e
.type
= ClientMessage
;
531 e
.xclient
.window
= win
;
532 e
.xclient
.message_type
= wmatom
[WMState
];
533 e
.xclient
.format
= 32;
534 e
.xclient
.data
.l
[0] = 2;
535 e
.xclient
.data
.l
[1] = wmatom
[WMFullscreen
];
536 e
.xclient
.data
.l
[2] = 0;
537 XSendEvent(dpy
, root
, False
, SubstructureNotifyMask
, &e
);
543 static char buf
[BUFSIZ
];
546 unsigned long ldummy
;
547 unsigned char *p
= NULL
;
549 XGetWindowProperty(dpy
, win
, wmatom
[a
], 0L, BUFSIZ
, False
, XA_STRING
,
550 &adummy
, &idummy
, &ldummy
, &ldummy
, &p
);
552 strncpy(buf
, (char *)p
, LENGTH(buf
)-1);
565 for (i
= 0; i
< nclients
; i
++) {
566 if (clients
[i
]->win
== w
)
574 getcolor(const char *colstr
)
578 if (!XftColorAllocName(dpy
, DefaultVisual(dpy
, screen
), DefaultColormap(dpy
, screen
), colstr
, &color
))
579 die("%s: cannot allocate color '%s'\n", argv0
, colstr
);
594 cc
= (ww
- TEXTW(before
) - TEXTW(after
)) / tabwidth
;
596 ret
= sel
- cc
/ 2 + (cc
+ 1) % 2;
598 ret
+ cc
> nclients
? MAX(0, nclients
- cc
) :
603 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
609 if (!text
|| size
== 0)
613 XGetTextProperty(dpy
, w
, &name
, atom
);
617 if (name
.encoding
== XA_STRING
) {
618 strncpy(text
, (char *)name
.value
, size
- 1);
619 } else if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
621 strncpy(text
, *list
, size
- 1);
622 XFreeStringList(list
);
624 text
[size
- 1] = '\0';
631 initfont(const char *fontstr
)
633 if (!(dc
.font
.xfont
= XftFontOpenName(dpy
, screen
, fontstr
))
634 && !(dc
.font
.xfont
= XftFontOpenName(dpy
, screen
, "fixed")))
635 die("error, cannot load font: '%s'\n", fontstr
);
637 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
638 dc
.font
.descent
= dc
.font
.xfont
->descent
;
639 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
649 if (XGetWMProtocols(dpy
, clients
[c
]->win
, &protocols
, &n
)) {
650 for (i
= 0; !ret
&& i
< n
; i
++) {
651 if (protocols
[i
] == wmatom
[WMDelete
])
661 keypress(const XEvent
*e
)
663 const XKeyEvent
*ev
= &e
->xkey
;
667 keysym
= XkbKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0, 0);
668 for (i
= 0; i
< LENGTH(keys
); i
++) {
669 if (keysym
== keys
[i
].keysym
&&
670 CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
) &&
672 keys
[i
].func(&(keys
[i
].arg
));
677 killclient(const Arg
*arg
)
684 if (isprotodel(sel
) && !clients
[sel
]->closed
) {
685 ev
.type
= ClientMessage
;
686 ev
.xclient
.window
= clients
[sel
]->win
;
687 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
688 ev
.xclient
.format
= 32;
689 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
690 ev
.xclient
.data
.l
[1] = CurrentTime
;
691 XSendEvent(dpy
, clients
[sel
]->win
, False
, NoEventMask
, &ev
);
692 clients
[sel
]->closed
= True
;
694 XKillClient(dpy
, clients
[sel
]->win
);
704 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
,
705 numlockmask
| LockMask
};
710 XWithdrawWindow(dpy
, w
, 0);
711 XReparentWindow(dpy
, w
, win
, 0, bh
);
712 XSelectInput(dpy
, w
, PropertyChangeMask
|
713 StructureNotifyMask
| EnterWindowMask
);
716 for (i
= 0; i
< LENGTH(keys
); i
++) {
717 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
))) {
718 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
719 XGrabKey(dpy
, code
, keys
[i
].mod
|
720 modifiers
[j
], w
, True
,
721 GrabModeAsync
, GrabModeAsync
);
726 c
= ecalloc(1, sizeof *c
);
730 clients
= erealloc(clients
, sizeof(Client
*) * nclients
);
733 nextpos
= sel
+ newposition
;
736 nextpos
= nclients
- newposition
;
738 nextpos
= newposition
;
740 if (nextpos
>= nclients
)
741 nextpos
= nclients
- 1;
745 if (nclients
> 1 && nextpos
< nclients
- 1)
746 memmove(&clients
[nextpos
+ 1], &clients
[nextpos
],
747 sizeof(Client
*) * (nclients
- nextpos
- 1));
749 clients
[nextpos
] = c
;
750 updatetitle(nextpos
);
752 XLowerWindow(dpy
, w
);
755 e
.xclient
.window
= w
;
756 e
.xclient
.type
= ClientMessage
;
757 e
.xclient
.message_type
= wmatom
[XEmbed
];
758 e
.xclient
.format
= 32;
759 e
.xclient
.data
.l
[0] = CurrentTime
;
760 e
.xclient
.data
.l
[1] = XEMBED_EMBEDDED_NOTIFY
;
761 e
.xclient
.data
.l
[2] = 0;
762 e
.xclient
.data
.l
[3] = win
;
763 e
.xclient
.data
.l
[4] = 0;
764 XSendEvent(dpy
, root
, False
, NoEventMask
, &e
);
768 /* Adjust sel before focus does set it to lastsel. */
771 focus(nextfocus
? nextpos
:
774 nextfocus
= foreground
;
779 maprequest(const XEvent
*e
)
781 const XMapRequestEvent
*ev
= &e
->xmaprequest
;
783 if (getclient(ev
->window
) < 0)
790 if (arg
->i
>= 0 && arg
->i
< nclients
)
795 movetab(const Arg
*arg
)
803 c
= (sel
+ arg
->i
) % nclients
;
812 memmove(&clients
[sel
], &clients
[sel
+1],
813 sizeof(Client
*) * (c
- sel
));
815 memmove(&clients
[c
+1], &clients
[c
],
816 sizeof(Client
*) * (sel
- c
));
824 propertynotify(const XEvent
*e
)
826 const XPropertyEvent
*ev
= &e
->xproperty
;
829 char* selection
= NULL
;
832 if (ev
->state
== PropertyNewValue
&& ev
->atom
== wmatom
[WMSelectTab
]) {
833 selection
= getatom(WMSelectTab
);
834 if (!strncmp(selection
, "0x", 2)) {
835 arg
.i
= getclient(strtoul(selection
, NULL
, 0));
838 cmd
[cmd_append_pos
] = selection
;
842 } else if (ev
->state
== PropertyNewValue
&& ev
->atom
== XA_WM_HINTS
&&
843 (c
= getclient(ev
->window
)) > -1 &&
844 (wmh
= XGetWMHints(dpy
, clients
[c
]->win
))) {
845 if (wmh
->flags
& XUrgencyHint
) {
847 wmh
= XGetWMHints(dpy
, win
);
849 if (urgentswitch
&& wmh
&&
850 !(wmh
->flags
& XUrgencyHint
)) {
851 /* only switch, if tabbed was focused
852 * since last urgency hint if WMHints
853 * could not be received,
854 * default to no switch */
857 /* if no switch should be performed,
858 * mark tab as urgent */
859 clients
[c
]->urgent
= True
;
863 if (wmh
&& !(wmh
->flags
& XUrgencyHint
)) {
864 /* update tabbed urgency hint
865 * if not set already */
866 wmh
->flags
|= XUrgencyHint
;
867 XSetWMHints(dpy
, win
, wmh
);
871 } else if (ev
->state
!= PropertyDelete
&& ev
->atom
== XA_WM_NAME
&&
872 (c
= getclient(ev
->window
)) > -1) {
878 resize(int c
, int w
, int h
)
885 ce
.width
= wc
.width
= w
;
886 ce
.height
= wc
.height
= h
;
887 ce
.type
= ConfigureNotify
;
889 ce
.event
= clients
[c
]->win
;
890 ce
.window
= clients
[c
]->win
;
892 ce
.override_redirect
= False
;
895 XConfigureWindow(dpy
, clients
[c
]->win
, CWY
| CWWidth
| CWHeight
, &wc
);
896 XSendEvent(dpy
, clients
[c
]->win
, False
, StructureNotifyMask
,
901 rotate(const Arg
*arg
)
911 } else if (sel
> -1) {
912 /* Rotating in an arg->i step around the clients. */
914 while (nsel
>= nclients
)
927 /* main event loop */
930 if (doinitspawn
== True
)
934 XNextEvent(dpy
, &ev
);
935 if (handler
[ev
.type
])
936 (handler
[ev
.type
])(&ev
); /* call handler */
941 sendxembed(int c
, long msg
, long detail
, long d1
, long d2
)
945 e
.xclient
.window
= clients
[c
]->win
;
946 e
.xclient
.type
= ClientMessage
;
947 e
.xclient
.message_type
= wmatom
[XEmbed
];
948 e
.xclient
.format
= 32;
949 e
.xclient
.data
.l
[0] = CurrentTime
;
950 e
.xclient
.data
.l
[1] = msg
;
951 e
.xclient
.data
.l
[2] = detail
;
952 e
.xclient
.data
.l
[3] = d1
;
953 e
.xclient
.data
.l
[4] = d2
;
954 XSendEvent(dpy
, clients
[c
]->win
, False
, NoEventMask
, &e
);
958 setcmd(int argc
, char *argv
[], int replace
)
962 cmd
= ecalloc(argc
+ 3, sizeof(*cmd
));
965 for (i
= 0; i
< argc
; i
++)
967 cmd
[replace
> 0 ? replace
: argc
] = winid
;
968 cmd_append_pos
= argc
+ !replace
;
969 cmd
[cmd_append_pos
] = cmd
[cmd_append_pos
+ 1] = NULL
;
975 int bitm
, tx
, ty
, tw
, th
, dh
, dw
, isfixed
;
977 XClassHint class_hint
;
978 XSizeHints
*size_hint
;
981 /* do not transform children into zombies when they terminate */
982 sigemptyset(&sa
.sa_mask
);
983 sa
.sa_flags
= SA_NOCLDSTOP
| SA_NOCLDWAIT
| SA_RESTART
;
984 sa
.sa_handler
= SIG_IGN
;
985 sigaction(SIGCHLD
, &sa
, NULL
);
987 /* clean up any zombies that might have been inherited */
988 while (waitpid(-1, NULL
, WNOHANG
) > 0);
991 screen
= DefaultScreen(dpy
);
992 root
= RootWindow(dpy
, screen
);
994 bh
= dc
.h
= dc
.font
.height
+ 2;
997 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
998 wmatom
[WMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN",
1000 wmatom
[WMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1001 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1002 wmatom
[WMSelectTab
] = XInternAtom(dpy
, "_TABBED_SELECT_TAB", False
);
1003 wmatom
[WMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1004 wmatom
[XEmbed
] = XInternAtom(dpy
, "_XEMBED", False
);
1006 /* init appearance */
1014 tx
= ty
= tw
= th
= 0;
1015 bitm
= XParseGeometry(geometry
, &tx
, &ty
, (unsigned *)&tw
,
1021 if (bitm
& WidthValue
)
1023 if (bitm
& HeightValue
)
1025 if (bitm
& XNegative
&& wx
== 0)
1027 if (bitm
& YNegative
&& wy
== 0)
1029 if (bitm
& (HeightValue
| WidthValue
))
1032 dw
= DisplayWidth(dpy
, screen
);
1033 dh
= DisplayHeight(dpy
, screen
);
1035 wx
= dw
+ wx
- ww
- 1;
1037 wy
= dh
+ wy
- wh
- 1;
1040 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1041 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1042 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1043 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1044 dc
.urg
[ColBG
] = getcolor(urgbgcolor
);
1045 dc
.urg
[ColFG
] = getcolor(urgfgcolor
);
1046 dc
.drawable
= XCreatePixmap(dpy
, root
, ww
, wh
,
1047 DefaultDepth(dpy
, screen
));
1048 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1050 win
= XCreateSimpleWindow(dpy
, root
, wx
, wy
, ww
, wh
, 0,
1051 dc
.norm
[ColFG
].pixel
, dc
.norm
[ColBG
].pixel
);
1052 XMapRaised(dpy
, win
);
1053 XSelectInput(dpy
, win
, SubstructureNotifyMask
| FocusChangeMask
|
1054 ButtonPressMask
| ExposureMask
| KeyPressMask
|
1055 PropertyChangeMask
| StructureNotifyMask
|
1056 SubstructureRedirectMask
);
1057 xerrorxlib
= XSetErrorHandler(xerror
);
1059 class_hint
.res_name
= wmname
;
1060 class_hint
.res_class
= "tabbed";
1061 XSetClassHint(dpy
, win
, &class_hint
);
1063 size_hint
= XAllocSizeHints();
1065 size_hint
->flags
= PSize
| PMinSize
;
1066 size_hint
->height
= wh
;
1067 size_hint
->width
= ww
;
1068 size_hint
->min_height
= bh
+ 1;
1070 size_hint
->flags
= PMaxSize
| PMinSize
;
1071 size_hint
->min_width
= size_hint
->max_width
= ww
;
1072 size_hint
->min_height
= size_hint
->max_height
= wh
;
1074 wmh
= XAllocWMHints();
1075 XSetWMProperties(dpy
, win
, NULL
, NULL
, NULL
, 0, size_hint
, wmh
, NULL
);
1079 XSetWMProtocols(dpy
, win
, &wmatom
[WMDelete
], 1);
1081 snprintf(winid
, sizeof(winid
), "%lu", win
);
1082 setenv("XEMBED", winid
, 1);
1084 nextfocus
= foreground
;
1089 spawn(const Arg
*arg
)
1091 struct sigaction sa
;
1095 close(ConnectionNumber(dpy
));
1099 sigemptyset(&sa
.sa_mask
);
1101 sa
.sa_handler
= SIG_DFL
;
1102 sigaction(SIGCHLD
, &sa
, NULL
);
1104 if (arg
&& arg
->v
) {
1105 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1106 fprintf(stderr
, "%s: execvp %s", argv0
,
1107 ((char **)arg
->v
)[0]);
1109 cmd
[cmd_append_pos
] = NULL
;
1110 execvp(cmd
[0], cmd
);
1111 fprintf(stderr
, "%s: execvp %s", argv0
, cmd
[0]);
1119 textnw(const char *text
, unsigned int len
)
1122 XftTextExtentsUtf8(dpy
, dc
.font
.xfont
, (XftChar8
*) text
, len
, &ext
);
1127 toggle(const Arg
*arg
)
1129 *(Bool
*) arg
->v
= !*(Bool
*) arg
->v
;
1135 if (c
< 0 || c
>= nclients
) {
1148 memmove(&clients
[0], &clients
[1], sizeof(Client
*) * nclients
);
1149 } else if (c
== nclients
- 1) {
1153 clients
= erealloc(clients
, sizeof(Client
*) * nclients
);
1155 /* Somewhere inbetween. */
1157 memmove(&clients
[c
], &clients
[c
+1],
1158 sizeof(Client
*) * (nclients
- (c
+ 1)));
1162 if (nclients
<= 0) {
1165 if (closelastclient
)
1167 else if (fillagain
&& running
)
1170 if (lastsel
>= nclients
)
1171 lastsel
= nclients
- 1;
1172 else if (lastsel
> c
)
1175 if (c
== sel
&& lastsel
>= 0) {
1180 if (sel
>= nclients
)
1192 unmapnotify(const XEvent
*e
)
1194 const XUnmapEvent
*ev
= &e
->xunmap
;
1197 if ((c
= getclient(ev
->window
)) > -1)
1202 updatenumlockmask(void)
1205 XModifierKeymap
*modmap
;
1208 modmap
= XGetModifierMapping(dpy
);
1209 for (i
= 0; i
< 8; i
++) {
1210 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
1211 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1212 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1213 numlockmask
= (1 << i
);
1216 XFreeModifiermap(modmap
);
1222 if (!gettextprop(clients
[c
]->win
, wmatom
[WMName
], clients
[c
]->name
,
1223 sizeof(clients
[c
]->name
)))
1224 gettextprop(clients
[c
]->win
, XA_WM_NAME
, clients
[c
]->name
,
1225 sizeof(clients
[c
]->name
));
1227 xsettitle(win
, clients
[c
]->name
);
1231 /* There's no way to check accesses to destroyed windows, thus those cases are
1232 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1233 * default error handler, which may call exit. */
1235 xerror(Display
*dpy
, XErrorEvent
*ee
)
1237 if (ee
->error_code
== BadWindow
1238 || (ee
->request_code
== X_SetInputFocus
&&
1239 ee
->error_code
== BadMatch
)
1240 || (ee
->request_code
== X_PolyText8
&&
1241 ee
->error_code
== BadDrawable
)
1242 || (ee
->request_code
== X_PolyFillRectangle
&&
1243 ee
->error_code
== BadDrawable
)
1244 || (ee
->request_code
== X_PolySegment
&&
1245 ee
->error_code
== BadDrawable
)
1246 || (ee
->request_code
== X_ConfigureWindow
&&
1247 ee
->error_code
== BadMatch
)
1248 || (ee
->request_code
== X_GrabButton
&&
1249 ee
->error_code
== BadAccess
)
1250 || (ee
->request_code
== X_GrabKey
&&
1251 ee
->error_code
== BadAccess
)
1252 || (ee
->request_code
== X_CopyArea
&&
1253 ee
->error_code
== BadDrawable
))
1256 fprintf(stderr
, "%s: fatal error: request code=%d, error code=%d\n",
1257 argv0
, ee
->request_code
, ee
->error_code
);
1258 return xerrorxlib(dpy
, ee
); /* may call exit */
1262 xsettitle(Window w
, const char *str
)
1266 if (XmbTextListToTextProperty(dpy
, (char **)&str
, 1,
1267 XCompoundTextStyle
, &xtp
) == Success
) {
1268 XSetTextProperty(dpy
, w
, &xtp
, wmatom
[WMName
]);
1269 XSetTextProperty(dpy
, w
, &xtp
, XA_WM_NAME
);
1277 die("usage: %s [-dfksv] [-g geometry] [-n name] [-p [s+/-]pos]\n"
1278 " [-r narg] [-o color] [-O color] [-t color] [-T color]\n"
1279 " [-u color] [-U color] command...\n", argv0
);
1283 main(int argc
, char *argv
[])
1285 Bool detach
= False
;
1291 closelastclient
= True
;
1301 geometry
= EARGF(usage());
1304 killclientsfirst
= True
;
1307 wmname
= EARGF(usage());
1310 normfgcolor
= EARGF(usage());
1313 normbgcolor
= EARGF(usage());
1316 pstr
= EARGF(usage());
1317 if (pstr
[0] == 's') {
1318 npisrelative
= True
;
1319 newposition
= atoi(&pstr
[1]);
1321 newposition
= atoi(pstr
);
1325 replace
= atoi(EARGF(usage()));
1328 doinitspawn
= False
;
1331 selfgcolor
= EARGF(usage());
1334 selbgcolor
= EARGF(usage());
1337 urgfgcolor
= EARGF(usage());
1340 urgbgcolor
= EARGF(usage());
1343 die("tabbed-"VERSION
", © 2009-2016 tabbed engineers, "
1344 "see LICENSE for details.\n");
1352 doinitspawn
= False
;
1356 setcmd(argc
, argv
, replace
);
1358 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1359 fprintf(stderr
, "%s: no locale support\n", argv0
);
1360 if (!(dpy
= XOpenDisplay(NULL
)))
1361 die("%s: cannot open display\n", argv0
);
1364 printf("0x%lx\n", win
);
1372 close(ConnectionNumber(dpy
));
1373 return EXIT_SUCCESS
;
1381 return EXIT_SUCCESS
;