2 * See LICENSE file for copyright and license details.
13 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
16 #include <X11/Xutil.h>
17 #include <X11/XKBlib.h>
18 #include <X11/Xft/Xft.h>
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
38 /* Details for XEMBED_FOCUS_IN: */
39 #define XEMBED_FOCUS_CURRENT 0
40 #define XEMBED_FOCUS_FIRST 1
41 #define XEMBED_FOCUS_LAST 2
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)
50 enum { ColFG
, ColBG
, ColLast
}; /* color */
51 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMFullscreen
,
52 XEmbed
, WMSelectTab
, WMLast
}; /* default atoms */
62 void (*func
)(const Arg
*);
68 XftColor norm
[ColLast
];
69 XftColor sel
[ColLast
];
70 XftColor urg
[ColLast
];
79 } DC
; /* draw context */
89 /* function declarations */
90 static void buttonpress(const XEvent
*e
);
91 static void cleanup(void);
92 static void clientmessage(const XEvent
*e
);
93 static void configurenotify(const XEvent
*e
);
94 static void configurerequest(const XEvent
*e
);
95 static void createnotify(const XEvent
*e
);
96 static void destroynotify(const XEvent
*e
);
97 static void die(const char *errstr
, ...);
98 static void drawbar(void);
99 static void drawtext(const char *text
, XftColor col
[ColLast
]);
100 static void *ecalloc(size_t n
, size_t size
);
101 static void *erealloc(void *o
, size_t size
);
102 static void expose(const XEvent
*e
);
103 static void focus(int c
);
104 static void focusin(const XEvent
*e
);
105 static void focusonce(const Arg
*arg
);
106 static void focusurgent(const Arg
*arg
);
107 static void fullscreen(const Arg
*arg
);
108 static char *getatom(int a
);
109 static int getclient(Window w
);
110 static XftColor
getcolor(const char *colstr
);
111 static int getfirsttab(void);
112 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
113 static void initfont(const char *fontstr
);
114 static Bool
isprotodel(int c
);
115 static void keypress(const XEvent
*e
);
116 static void killclient(const Arg
*arg
);
117 static void manage(Window win
);
118 static void maprequest(const XEvent
*e
);
119 static void move(const Arg
*arg
);
120 static void movetab(const Arg
*arg
);
121 static void propertynotify(const XEvent
*e
);
122 static void resize(int c
, int w
, int h
);
123 static void rotate(const Arg
*arg
);
124 static void run(void);
125 static void sendxembed(int c
, long msg
, long detail
, long d1
, long d2
);
126 static void setcmd(int argc
, char *argv
[], int);
127 static void setup(void);
128 static void sigchld(int unused
);
129 static void spawn(const Arg
*arg
);
130 static int textnw(const char *text
, unsigned int len
);
131 static void toggle(const Arg
*arg
);
132 static void unmanage(int c
);
133 static void unmapnotify(const XEvent
*e
);
134 static void updatenumlockmask(void);
135 static void updatetitle(int c
);
136 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
137 static void xsettitle(Window w
, const char *str
);
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
, 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
++) {
221 XReparentWindow(dpy
, clients
[i
]->win
, root
, 0, 0);
227 XFreePixmap(dpy
, dc
.drawable
);
229 XDestroyWindow(dpy
, win
);
235 clientmessage(const XEvent
*e
)
237 const XClientMessageEvent
*ev
= &e
->xclient
;
239 if (ev
->message_type
== wmatom
[WMProtocols
] &&
240 ev
->data
.l
[0] == wmatom
[WMDelete
]) {
241 if (nclients
> 1 && killclientsfirst
) {
250 configurenotify(const XEvent
*e
)
252 const XConfigureEvent
*ev
= &e
->xconfigure
;
254 if (ev
->window
== win
&& (ev
->width
!= ww
|| ev
->height
!= wh
)) {
257 XFreePixmap(dpy
, dc
.drawable
);
258 dc
.drawable
= XCreatePixmap(dpy
, root
, ww
, wh
,
259 DefaultDepth(dpy
, screen
));
261 resize(sel
, ww
, wh
- bh
);
267 configurerequest(const XEvent
*e
)
269 const XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
273 if ((c
= getclient(ev
->window
)) > -1) {
279 wc
.sibling
= ev
->above
;
280 wc
.stack_mode
= ev
->detail
;
281 XConfigureWindow(dpy
, clients
[c
]->win
, ev
->value_mask
, &wc
);
286 createnotify(const XEvent
*e
)
288 const XCreateWindowEvent
*ev
= &e
->xcreatewindow
;
290 if (ev
->window
!= win
&& getclient(ev
->window
) < 0)
295 destroynotify(const XEvent
*e
)
297 const XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
300 if ((c
= getclient(ev
->window
)) > -1)
305 die(const char *errstr
, ...)
309 va_start(ap
, errstr
);
310 vfprintf(stderr
, errstr
, ap
);
319 int c
, cc
, fc
, width
;
325 XFetchName(dpy
, win
, &name
);
326 drawtext(name
? name
: "", dc
.norm
);
327 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, ww
, bh
, 0, 0);
336 cc
= (ww
- TEXTW(before
) - TEXTW(after
)) / tabwidth
;
338 if ((fc
= getfirsttab()) + cc
< nclients
) {
341 drawtext(after
, dc
.sel
);
347 dc
.w
= TEXTW(before
);
348 drawtext(before
, dc
.sel
);
353 cc
= MIN(cc
, nclients
);
354 for (c
= fc
; c
< fc
+ cc
; c
++) {
360 col
= clients
[c
]->urgent
? dc
.urg
: dc
.norm
;
362 drawtext(clients
[c
]->name
, col
);
364 clients
[c
]->tabx
= dc
.x
;
366 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, ww
, bh
, 0, 0);
371 drawtext(const char *text
, XftColor col
[ColLast
])
373 int i
, j
, x
, y
, h
, len
, olen
;
376 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
378 XSetForeground(dpy
, dc
.gc
, col
[ColBG
].pixel
);
379 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
384 h
= dc
.font
.ascent
+ dc
.font
.descent
;
385 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
388 /* shorten text if necessary */
389 for (len
= MIN(olen
, sizeof(buf
));
390 len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
395 memcpy(buf
, text
, len
);
397 for (i
= len
, j
= strlen(titletrim
); j
&& i
;
398 buf
[--i
] = titletrim
[--j
])
402 d
= XftDrawCreate(dpy
, dc
.drawable
, DefaultVisual(dpy
, screen
), DefaultColormap(dpy
, screen
));
403 XftDrawStringUtf8(d
, &col
[ColFG
], dc
.font
.xfont
, x
, y
, (XftChar8
*) buf
, len
);
408 ecalloc(size_t n
, size_t size
)
412 if (!(p
= calloc(n
, size
)))
413 die("%s: cannot calloc\n", argv0
);
418 erealloc(void *o
, size_t size
)
422 if (!(p
= realloc(o
, size
)))
423 die("%s: cannot realloc\n", argv0
);
428 expose(const XEvent
*e
)
430 const XExposeEvent
*ev
= &e
->xexpose
;
432 if (ev
->count
== 0 && win
== ev
->window
)
439 char buf
[BUFSIZ
] = "tabbed-"VERSION
" ::";
443 /* If c, sel and clients are -1, raise tabbed-win itself */
445 cmd
[cmd_append_pos
] = NULL
;
446 for(i
= 0, n
= strlen(buf
); cmd
[i
] && n
< sizeof(buf
); i
++)
447 n
+= snprintf(&buf
[n
], sizeof(buf
) - n
, " %s", cmd
[i
]);
450 XRaiseWindow(dpy
, win
);
455 if (c
< 0 || c
>= nclients
)
458 resize(c
, ww
, wh
- bh
);
459 XRaiseWindow(dpy
, clients
[c
]->win
);
460 XSetInputFocus(dpy
, clients
[c
]->win
, RevertToParent
, CurrentTime
);
461 sendxembed(c
, XEMBED_FOCUS_IN
, XEMBED_FOCUS_CURRENT
, 0, 0);
462 sendxembed(c
, XEMBED_WINDOW_ACTIVATE
, 0, 0, 0);
463 xsettitle(win
, clients
[c
]->name
);
470 if (clients
[c
]->urgent
&& (wmh
= XGetWMHints(dpy
, clients
[c
]->win
))) {
471 wmh
->flags
&= ~XUrgencyHint
;
472 XSetWMHints(dpy
, clients
[c
]->win
, wmh
);
473 clients
[c
]->urgent
= False
;
482 focusin(const XEvent
*e
)
484 const XFocusChangeEvent
*ev
= &e
->xfocus
;
488 if (ev
->mode
!= NotifyUngrab
) {
489 XGetInputFocus(dpy
, &focused
, &dummy
);
496 focusonce(const Arg
*arg
)
502 focusurgent(const Arg
*arg
)
506 for (c
= (sel
+ 1) % nclients
; c
!= sel
; c
= (c
+ 1) % nclients
) {
507 if (clients
[c
]->urgent
) {
515 fullscreen(const Arg
*arg
)
519 e
.type
= ClientMessage
;
520 e
.xclient
.window
= win
;
521 e
.xclient
.message_type
= wmatom
[WMState
];
522 e
.xclient
.format
= 32;
523 e
.xclient
.data
.l
[0] = 2;
524 e
.xclient
.data
.l
[1] = wmatom
[WMFullscreen
];
525 e
.xclient
.data
.l
[2] = 0;
526 XSendEvent(dpy
, root
, False
, SubstructureNotifyMask
, &e
);
532 static char buf
[BUFSIZ
];
535 unsigned long ldummy
;
536 unsigned char *p
= NULL
;
538 XGetWindowProperty(dpy
, win
, wmatom
[a
], 0L, BUFSIZ
, False
, XA_STRING
,
539 &adummy
, &idummy
, &ldummy
, &ldummy
, &p
);
541 strncpy(buf
, (char *)p
, LENGTH(buf
)-1);
554 for (i
= 0; i
< nclients
; i
++) {
555 if (clients
[i
]->win
== w
)
563 getcolor(const char *colstr
)
567 if (!XftColorAllocName(dpy
, DefaultVisual(dpy
, screen
), DefaultColormap(dpy
, screen
), colstr
, &color
))
568 die("%s: cannot allocate color '%s'\n", argv0
, colstr
);
583 cc
= (ww
- TEXTW(before
) - TEXTW(after
)) / tabwidth
;
585 ret
= sel
- cc
/ 2 + (cc
+ 1) % 2;
587 ret
+ cc
> nclients
? MAX(0, nclients
- cc
) :
592 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
598 if (!text
|| size
== 0)
602 XGetTextProperty(dpy
, w
, &name
, atom
);
606 if (name
.encoding
== XA_STRING
) {
607 strncpy(text
, (char *)name
.value
, size
- 1);
608 } else if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
610 strncpy(text
, *list
, size
- 1);
611 XFreeStringList(list
);
613 text
[size
- 1] = '\0';
620 initfont(const char *fontstr
)
622 if (!(dc
.font
.xfont
= XftFontOpenName(dpy
, screen
, fontstr
))
623 && !(dc
.font
.xfont
= XftFontOpenName(dpy
, screen
, "fixed")))
624 die("error, cannot load font: '%s'\n", fontstr
);
626 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
627 dc
.font
.descent
= dc
.font
.xfont
->descent
;
628 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
638 if (XGetWMProtocols(dpy
, clients
[c
]->win
, &protocols
, &n
)) {
639 for (i
= 0; !ret
&& i
< n
; i
++) {
640 if (protocols
[i
] == wmatom
[WMDelete
])
650 keypress(const XEvent
*e
)
652 const XKeyEvent
*ev
= &e
->xkey
;
656 keysym
= XkbKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0, 0);
657 for (i
= 0; i
< LENGTH(keys
); i
++) {
658 if (keysym
== keys
[i
].keysym
&&
659 CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
) &&
661 keys
[i
].func(&(keys
[i
].arg
));
666 killclient(const Arg
*arg
)
673 if (isprotodel(sel
) && !clients
[sel
]->closed
) {
674 ev
.type
= ClientMessage
;
675 ev
.xclient
.window
= clients
[sel
]->win
;
676 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
677 ev
.xclient
.format
= 32;
678 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
679 ev
.xclient
.data
.l
[1] = CurrentTime
;
680 XSendEvent(dpy
, clients
[sel
]->win
, False
, NoEventMask
, &ev
);
681 clients
[sel
]->closed
= True
;
683 XKillClient(dpy
, clients
[sel
]->win
);
693 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
,
694 numlockmask
| LockMask
};
699 XWithdrawWindow(dpy
, w
, 0);
700 XReparentWindow(dpy
, w
, win
, 0, bh
);
701 XSelectInput(dpy
, w
, PropertyChangeMask
|
702 StructureNotifyMask
| EnterWindowMask
);
705 for (i
= 0; i
< LENGTH(keys
); i
++) {
706 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
))) {
707 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
708 XGrabKey(dpy
, code
, keys
[i
].mod
|
709 modifiers
[j
], w
, True
,
710 GrabModeAsync
, GrabModeAsync
);
715 c
= ecalloc(1, sizeof *c
);
719 clients
= erealloc(clients
, sizeof(Client
*) * nclients
);
722 nextpos
= sel
+ newposition
;
725 nextpos
= nclients
- newposition
;
727 nextpos
= newposition
;
729 if (nextpos
>= nclients
)
730 nextpos
= nclients
- 1;
734 if (nclients
> 1 && nextpos
< nclients
- 1)
735 memmove(&clients
[nextpos
+ 1], &clients
[nextpos
],
736 sizeof(Client
*) * (nclients
- nextpos
- 1));
738 clients
[nextpos
] = c
;
739 updatetitle(nextpos
);
741 XLowerWindow(dpy
, w
);
744 e
.xclient
.window
= w
;
745 e
.xclient
.type
= ClientMessage
;
746 e
.xclient
.message_type
= wmatom
[XEmbed
];
747 e
.xclient
.format
= 32;
748 e
.xclient
.data
.l
[0] = CurrentTime
;
749 e
.xclient
.data
.l
[1] = XEMBED_EMBEDDED_NOTIFY
;
750 e
.xclient
.data
.l
[2] = 0;
751 e
.xclient
.data
.l
[3] = win
;
752 e
.xclient
.data
.l
[4] = 0;
753 XSendEvent(dpy
, root
, False
, NoEventMask
, &e
);
757 /* Adjust sel before focus does set it to lastsel. */
760 focus(nextfocus
? nextpos
:
763 nextfocus
= foreground
;
768 maprequest(const XEvent
*e
)
770 const XMapRequestEvent
*ev
= &e
->xmaprequest
;
772 if (getclient(ev
->window
) < 0)
779 if (arg
->i
>= 0 && arg
->i
< nclients
)
784 movetab(const Arg
*arg
)
789 c
= (sel
+ arg
->i
) % nclients
;
793 if (sel
< 0 || c
== sel
)
798 memmove(&clients
[sel
], &clients
[sel
+1],
799 sizeof(Client
*) * (c
- sel
));
801 memmove(&clients
[c
+1], &clients
[c
],
802 sizeof(Client
*) * (sel
- c
));
810 propertynotify(const XEvent
*e
)
812 const XPropertyEvent
*ev
= &e
->xproperty
;
815 char* selection
= NULL
;
818 if (ev
->state
== PropertyNewValue
&& ev
->atom
== wmatom
[WMSelectTab
]) {
819 selection
= getatom(WMSelectTab
);
820 if (!strncmp(selection
, "0x", 2)) {
821 arg
.i
= getclient(strtoul(selection
, NULL
, 0));
824 cmd
[cmd_append_pos
] = selection
;
828 } else if (ev
->state
== PropertyNewValue
&& ev
->atom
== XA_WM_HINTS
&&
829 (c
= getclient(ev
->window
)) > -1 &&
830 (wmh
= XGetWMHints(dpy
, clients
[c
]->win
))) {
831 if (wmh
->flags
& XUrgencyHint
) {
833 wmh
= XGetWMHints(dpy
, win
);
835 if (urgentswitch
&& wmh
&&
836 !(wmh
->flags
& XUrgencyHint
)) {
837 /* only switch, if tabbed was focused
838 * since last urgency hint if WMHints
839 * could not be received,
840 * default to no switch */
843 /* if no switch should be performed,
844 * mark tab as urgent */
845 clients
[c
]->urgent
= True
;
849 if (wmh
&& !(wmh
->flags
& XUrgencyHint
)) {
850 /* update tabbed urgency hint
851 * if not set already */
852 wmh
->flags
|= XUrgencyHint
;
853 XSetWMHints(dpy
, win
, wmh
);
857 } else if (ev
->state
!= PropertyDelete
&& ev
->atom
== XA_WM_NAME
&&
858 (c
= getclient(ev
->window
)) > -1) {
864 resize(int c
, int w
, int h
)
871 ce
.width
= wc
.width
= w
;
872 ce
.height
= wc
.height
= h
;
873 ce
.type
= ConfigureNotify
;
875 ce
.event
= clients
[c
]->win
;
876 ce
.window
= clients
[c
]->win
;
878 ce
.override_redirect
= False
;
881 XConfigureWindow(dpy
, clients
[c
]->win
, CWWidth
| CWHeight
, &wc
);
882 XSendEvent(dpy
, clients
[c
]->win
, False
, StructureNotifyMask
,
887 rotate(const Arg
*arg
)
897 } else if (sel
> -1) {
898 /* Rotating in an arg->i step around the clients. */
900 while (nsel
>= nclients
)
913 /* main event loop */
916 if (doinitspawn
== True
)
920 XNextEvent(dpy
, &ev
);
921 if (handler
[ev
.type
])
922 (handler
[ev
.type
])(&ev
); /* call handler */
927 sendxembed(int c
, long msg
, long detail
, long d1
, long d2
)
931 e
.xclient
.window
= clients
[c
]->win
;
932 e
.xclient
.type
= ClientMessage
;
933 e
.xclient
.message_type
= wmatom
[XEmbed
];
934 e
.xclient
.format
= 32;
935 e
.xclient
.data
.l
[0] = CurrentTime
;
936 e
.xclient
.data
.l
[1] = msg
;
937 e
.xclient
.data
.l
[2] = detail
;
938 e
.xclient
.data
.l
[3] = d1
;
939 e
.xclient
.data
.l
[4] = d2
;
940 XSendEvent(dpy
, clients
[c
]->win
, False
, NoEventMask
, &e
);
944 setcmd(int argc
, char *argv
[], int replace
)
948 cmd
= ecalloc(argc
+ 3, sizeof(*cmd
));
951 for (i
= 0; i
< argc
; i
++)
953 cmd
[replace
> 0 ? replace
: argc
] = winid
;
954 cmd_append_pos
= argc
+ !replace
;
955 cmd
[cmd_append_pos
] = cmd
[cmd_append_pos
+ 1] = NULL
;
961 int bitm
, tx
, ty
, tw
, th
, dh
, dw
, isfixed
;
963 XClassHint class_hint
;
964 XSizeHints
*size_hint
;
966 /* clean up any zombies immediately */
970 screen
= DefaultScreen(dpy
);
971 root
= RootWindow(dpy
, screen
);
973 bh
= dc
.h
= dc
.font
.height
+ 2;
976 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
977 wmatom
[WMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN",
979 wmatom
[WMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
980 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
981 wmatom
[WMSelectTab
] = XInternAtom(dpy
, "_TABBED_SELECT_TAB", False
);
982 wmatom
[WMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
983 wmatom
[XEmbed
] = XInternAtom(dpy
, "_XEMBED", False
);
985 /* init appearance */
993 tx
= ty
= tw
= th
= 0;
994 bitm
= XParseGeometry(geometry
, &tx
, &ty
, (unsigned *)&tw
,
1000 if (bitm
& WidthValue
)
1002 if (bitm
& HeightValue
)
1004 if (bitm
& XNegative
&& wx
== 0)
1006 if (bitm
& YNegative
&& wy
== 0)
1008 if (bitm
& (HeightValue
| WidthValue
))
1011 dw
= DisplayWidth(dpy
, screen
);
1012 dh
= DisplayHeight(dpy
, screen
);
1014 wx
= dw
+ wx
- ww
- 1;
1016 wy
= dh
+ wy
- wh
- 1;
1019 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1020 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1021 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1022 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1023 dc
.urg
[ColBG
] = getcolor(urgbgcolor
);
1024 dc
.urg
[ColFG
] = getcolor(urgfgcolor
);
1025 dc
.drawable
= XCreatePixmap(dpy
, root
, ww
, wh
,
1026 DefaultDepth(dpy
, screen
));
1027 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1029 win
= XCreateSimpleWindow(dpy
, root
, wx
, wy
, ww
, wh
, 0,
1030 dc
.norm
[ColFG
].pixel
, dc
.norm
[ColBG
].pixel
);
1031 XMapRaised(dpy
, win
);
1032 XSelectInput(dpy
, win
, SubstructureNotifyMask
| FocusChangeMask
|
1033 ButtonPressMask
| ExposureMask
| KeyPressMask
|
1034 PropertyChangeMask
| StructureNotifyMask
|
1035 SubstructureRedirectMask
);
1036 xerrorxlib
= XSetErrorHandler(xerror
);
1038 class_hint
.res_name
= wmname
;
1039 class_hint
.res_class
= "tabbed";
1040 XSetClassHint(dpy
, win
, &class_hint
);
1042 size_hint
= XAllocSizeHints();
1044 size_hint
->flags
= PSize
;
1045 size_hint
->height
= wh
;
1046 size_hint
->width
= ww
;
1048 size_hint
->flags
= PMaxSize
| PMinSize
;
1049 size_hint
->min_width
= size_hint
->max_width
= ww
;
1050 size_hint
->min_height
= size_hint
->max_height
= wh
;
1052 wmh
= XAllocWMHints();
1053 XSetWMProperties(dpy
, win
, NULL
, NULL
, NULL
, 0, size_hint
, wmh
, NULL
);
1057 XSetWMProtocols(dpy
, win
, &wmatom
[WMDelete
], 1);
1059 snprintf(winid
, sizeof(winid
), "%lu", win
);
1060 setenv("XEMBED", winid
, 1);
1062 nextfocus
= foreground
;
1069 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1070 die("%s: cannot install SIGCHLD handler", argv0
);
1072 while (0 < waitpid(-1, NULL
, WNOHANG
));
1076 spawn(const Arg
*arg
)
1080 close(ConnectionNumber(dpy
));
1083 if (arg
&& arg
->v
) {
1084 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1085 fprintf(stderr
, "%s: execvp %s", argv0
,
1086 ((char **)arg
->v
)[0]);
1088 cmd
[cmd_append_pos
] = NULL
;
1089 execvp(cmd
[0], cmd
);
1090 fprintf(stderr
, "%s: execvp %s", argv0
, cmd
[0]);
1098 textnw(const char *text
, unsigned int len
)
1101 XftTextExtentsUtf8(dpy
, dc
.font
.xfont
, (XftChar8
*) text
, len
, &ext
);
1106 toggle(const Arg
*arg
)
1108 *(Bool
*) arg
->v
= !*(Bool
*) arg
->v
;
1114 if (c
< 0 || c
>= nclients
) {
1127 memmove(&clients
[0], &clients
[1], sizeof(Client
*) * nclients
);
1128 } else if (c
== nclients
- 1) {
1132 clients
= erealloc(clients
, sizeof(Client
*) * nclients
);
1134 /* Somewhere inbetween. */
1136 memmove(&clients
[c
], &clients
[c
+1],
1137 sizeof(Client
*) * (nclients
- (c
+ 1)));
1141 if (nclients
<= 0) {
1144 if (closelastclient
)
1146 else if (fillagain
&& running
)
1149 if (lastsel
>= nclients
)
1150 lastsel
= nclients
- 1;
1151 else if (lastsel
> c
)
1154 if (c
== sel
&& lastsel
>= 0) {
1159 if (sel
>= nclients
)
1171 unmapnotify(const XEvent
*e
)
1173 const XUnmapEvent
*ev
= &e
->xunmap
;
1176 if ((c
= getclient(ev
->window
)) > -1)
1181 updatenumlockmask(void)
1184 XModifierKeymap
*modmap
;
1187 modmap
= XGetModifierMapping(dpy
);
1188 for (i
= 0; i
< 8; i
++) {
1189 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
1190 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1191 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1192 numlockmask
= (1 << i
);
1195 XFreeModifiermap(modmap
);
1201 if (!gettextprop(clients
[c
]->win
, wmatom
[WMName
], clients
[c
]->name
,
1202 sizeof(clients
[c
]->name
)))
1203 gettextprop(clients
[c
]->win
, XA_WM_NAME
, clients
[c
]->name
,
1204 sizeof(clients
[c
]->name
));
1206 xsettitle(win
, clients
[c
]->name
);
1210 /* There's no way to check accesses to destroyed windows, thus those cases are
1211 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1212 * default error handler, which may call exit. */
1214 xerror(Display
*dpy
, XErrorEvent
*ee
)
1216 if (ee
->error_code
== BadWindow
1217 || (ee
->request_code
== X_SetInputFocus
&&
1218 ee
->error_code
== BadMatch
)
1219 || (ee
->request_code
== X_PolyText8
&&
1220 ee
->error_code
== BadDrawable
)
1221 || (ee
->request_code
== X_PolyFillRectangle
&&
1222 ee
->error_code
== BadDrawable
)
1223 || (ee
->request_code
== X_PolySegment
&&
1224 ee
->error_code
== BadDrawable
)
1225 || (ee
->request_code
== X_ConfigureWindow
&&
1226 ee
->error_code
== BadMatch
)
1227 || (ee
->request_code
== X_GrabButton
&&
1228 ee
->error_code
== BadAccess
)
1229 || (ee
->request_code
== X_GrabKey
&&
1230 ee
->error_code
== BadAccess
)
1231 || (ee
->request_code
== X_CopyArea
&&
1232 ee
->error_code
== BadDrawable
))
1235 fprintf(stderr
, "%s: fatal error: request code=%d, error code=%d\n",
1236 argv0
, ee
->request_code
, ee
->error_code
);
1237 return xerrorxlib(dpy
, ee
); /* may call exit */
1241 xsettitle(Window w
, const char *str
)
1245 if (XmbTextListToTextProperty(dpy
, (char **)&str
, 1,
1246 XCompoundTextStyle
, &xtp
) == Success
) {
1247 XSetTextProperty(dpy
, w
, &xtp
, wmatom
[WMName
]);
1248 XSetTextProperty(dpy
, w
, &xtp
, XA_WM_NAME
);
1256 die("usage: %s [-dfksv] [-g geometry] [-n name] [-p [s+/-]pos]\n"
1257 " [-r narg] [-o color] [-O color] [-t color] [-T color]\n"
1258 " [-u color] [-U color] command...\n", argv0
);
1262 main(int argc
, char *argv
[])
1264 Bool detach
= False
;
1270 closelastclient
= True
;
1280 geometry
= EARGF(usage());
1283 killclientsfirst
= True
;
1286 wmname
= EARGF(usage());
1289 normfgcolor
= EARGF(usage());
1292 normbgcolor
= EARGF(usage());
1295 pstr
= EARGF(usage());
1296 if (pstr
[0] == 's') {
1297 npisrelative
= True
;
1298 newposition
= atoi(&pstr
[1]);
1300 newposition
= atoi(pstr
);
1304 replace
= atoi(EARGF(usage()));
1307 doinitspawn
= False
;
1310 selfgcolor
= EARGF(usage());
1313 selbgcolor
= EARGF(usage());
1316 urgfgcolor
= EARGF(usage());
1319 urgbgcolor
= EARGF(usage());
1322 die("tabbed-"VERSION
", © 2009-2016 tabbed engineers, "
1323 "see LICENSE for details.\n");
1331 doinitspawn
= False
;
1335 setcmd(argc
, argv
, replace
);
1337 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1338 fprintf(stderr
, "%s: no locale support\n", argv0
);
1339 if (!(dpy
= XOpenDisplay(NULL
)))
1340 die("%s: cannot open display\n", argv0
);
1343 printf("0x%lx\n", win
);
1351 close(ConnectionNumber(dpy
));
1352 return EXIT_SUCCESS
;
1360 return EXIT_SUCCESS
;