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>
22 #define XEMBED_EMBEDDED_NOTIFY 0
23 #define XEMBED_WINDOW_ACTIVATE 1
24 #define XEMBED_WINDOW_DEACTIVATE 2
25 #define XEMBED_REQUEST_FOCUS 3
26 #define XEMBED_FOCUS_IN 4
27 #define XEMBED_FOCUS_OUT 5
28 #define XEMBED_FOCUS_NEXT 6
29 #define XEMBED_FOCUS_PREV 7
30 /* 8-9 were used for XEMBED_GRAB_KEY/XEMBED_UNGRAB_KEY */
31 #define XEMBED_MODALITY_ON 10
32 #define XEMBED_MODALITY_OFF 11
33 #define XEMBED_REGISTER_ACCELERATOR 12
34 #define XEMBED_UNREGISTER_ACCELERATOR 13
35 #define XEMBED_ACTIVATE_ACCELERATOR 14
37 /* Details for XEMBED_FOCUS_IN: */
38 #define XEMBED_FOCUS_CURRENT 0
39 #define XEMBED_FOCUS_FIRST 1
40 #define XEMBED_FOCUS_LAST 2
43 #define MAX(a, b) ((a) > (b) ? (a) : (b))
44 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #define LENGTH(x) (sizeof((x)) / sizeof(*(x)))
46 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
47 #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height)
49 enum { ColFG
, ColBG
, ColLast
}; /* color */
50 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMFullscreen
,
51 XEmbed
, WMSelectTab
, WMLast
}; /* default atoms */
61 void (*func
)(const Arg
*);
67 unsigned long norm
[ColLast
];
68 unsigned long sel
[ColLast
];
69 unsigned long 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
, unsigned long 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 unsigned long 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
;
161 static Atom wmatom
[WMLast
];
162 static Window root
, win
;
163 static Client
**clients
;
164 static int nclients
, sel
= -1, lastsel
= -1;
165 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
166 static int cmd_append_pos
;
167 static char winid
[64];
169 static char *wmname
= "tabbed";
170 static const char *geometry
;
174 /* configuration, allows nested code to access above variables */
178 buttonpress(const XEvent
*e
)
180 const XButtonPressedEvent
*ev
= &e
->xbutton
;
184 if (ev
->y
< 0 || ev
->y
> bh
)
187 if (((fc
= getfirsttab()) > 0 && ev
->x
< TEXTW(before
)) || ev
->x
< 0)
190 for (i
= fc
; i
< nclients
; i
++) {
191 if (clients
[i
]->tabx
> ev
->x
) {
192 switch (ev
->button
) {
200 case Button4
: /* FALLTHROUGH */
202 arg
.i
= ev
->button
== Button4
? -1 : 1;
216 for (i
= 0; i
< nclients
; i
++) {
220 XReparentWindow(dpy
, clients
[i
]->win
, root
, 0, 0);
227 XFreeFontSet(dpy
, dc
.font
.set
);
229 XFreeFont(dpy
, dc
.font
.xfont
);
231 XFreePixmap(dpy
, dc
.drawable
);
233 XDestroyWindow(dpy
, win
);
239 clientmessage(const XEvent
*e
)
241 const XClientMessageEvent
*ev
= &e
->xclient
;
243 if (ev
->message_type
== wmatom
[WMProtocols
] &&
244 ev
->data
.l
[0] == wmatom
[WMDelete
])
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 resize(sel
, ww
, wh
- bh
);
266 configurerequest(const XEvent
*e
)
268 const XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
272 if ((c
= getclient(ev
->window
)) > -1) {
278 wc
.sibling
= ev
->above
;
279 wc
.stack_mode
= ev
->detail
;
280 XConfigureWindow(dpy
, clients
[c
]->win
, ev
->value_mask
, &wc
);
285 createnotify(const XEvent
*e
)
287 const XCreateWindowEvent
*ev
= &e
->xcreatewindow
;
289 if (ev
->window
!= win
&& getclient(ev
->window
) < 0)
294 destroynotify(const XEvent
*e
)
296 const XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
299 if ((c
= getclient(ev
->window
)) > -1)
304 die(const char *errstr
, ...)
308 va_start(ap
, errstr
);
309 vfprintf(stderr
, errstr
, ap
);
318 int c
, cc
, fc
, width
;
324 XFetchName(dpy
, win
, &name
);
325 drawtext(name
? name
: "", dc
.norm
);
326 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, ww
, bh
, 0, 0);
335 cc
= (ww
- TEXTW(before
) - TEXTW(after
)) / tabwidth
;
337 if ((fc
= getfirsttab()) + cc
< nclients
) {
340 drawtext(after
, dc
.sel
);
346 dc
.w
= TEXTW(before
);
347 drawtext(before
, dc
.sel
);
352 cc
= MIN(cc
, nclients
);
353 for (c
= fc
; c
< fc
+ cc
; c
++) {
359 col
= clients
[c
]->urgent
? dc
.urg
: dc
.norm
;
361 drawtext(clients
[c
]->name
, col
);
363 clients
[c
]->tabx
= dc
.x
;
365 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, ww
, bh
, 0, 0);
370 drawtext(const char *text
, unsigned long col
[ColLast
])
372 int i
, x
, y
, h
, len
, olen
;
374 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
376 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
377 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
382 h
= dc
.font
.ascent
+ dc
.font
.descent
;
383 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
386 /* shorten text if necessary */
387 for (len
= MIN(olen
, sizeof(buf
));
388 len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
393 memcpy(buf
, text
, len
);
395 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
397 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
399 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
,
400 dc
.gc
, x
, y
, buf
, len
);
402 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
406 ecalloc(size_t n
, size_t size
)
410 if (!(p
= calloc(n
, size
)))
411 die("tabbed: cannot calloc\n");
416 erealloc(void *o
, size_t size
)
420 if (!(p
= realloc(o
, size
)))
421 die("tabbed: cannot realloc\n");
426 expose(const XEvent
*e
)
428 const XExposeEvent
*ev
= &e
->xexpose
;
430 if (ev
->count
== 0 && win
== ev
->window
)
437 char buf
[BUFSIZ
] = "tabbed-"VERSION
" ::";
441 /* If c, sel and clients are -1, raise tabbed-win itself */
443 cmd
[cmd_append_pos
] = NULL
;
444 for(i
= 0, n
= strlen(buf
); cmd
[i
] && n
< sizeof(buf
); i
++)
445 n
+= snprintf(&buf
[n
], sizeof(buf
) - n
, " %s", cmd
[i
]);
448 XRaiseWindow(dpy
, win
);
453 if (c
< 0 || c
>= nclients
)
456 resize(c
, ww
, wh
- bh
);
457 XRaiseWindow(dpy
, clients
[c
]->win
);
458 XSetInputFocus(dpy
, clients
[c
]->win
, RevertToParent
, CurrentTime
);
459 sendxembed(c
, XEMBED_FOCUS_IN
, XEMBED_FOCUS_CURRENT
, 0, 0);
460 sendxembed(c
, XEMBED_WINDOW_ACTIVATE
, 0, 0, 0);
461 xsettitle(win
, clients
[c
]->name
);
468 if (clients
[c
]->urgent
&& (wmh
= XGetWMHints(dpy
, clients
[c
]->win
))) {
469 wmh
->flags
&= ~XUrgencyHint
;
470 XSetWMHints(dpy
, clients
[c
]->win
, wmh
);
471 clients
[c
]->urgent
= False
;
480 focusin(const XEvent
*e
)
482 const XFocusChangeEvent
*ev
= &e
->xfocus
;
486 if (ev
->mode
!= NotifyUngrab
) {
487 XGetInputFocus(dpy
, &focused
, &dummy
);
494 focusonce(const Arg
*arg
)
500 focusurgent(const Arg
*arg
)
504 for (c
= (sel
+ 1) % nclients
; c
!= sel
; c
= (c
+ 1) % nclients
) {
505 if (clients
[c
]->urgent
) {
513 fullscreen(const Arg
*arg
)
517 e
.type
= ClientMessage
;
518 e
.xclient
.window
= win
;
519 e
.xclient
.message_type
= wmatom
[WMState
];
520 e
.xclient
.format
= 32;
521 e
.xclient
.data
.l
[0] = 2;
522 e
.xclient
.data
.l
[1] = wmatom
[WMFullscreen
];
523 e
.xclient
.data
.l
[2] = 0;
524 XSendEvent(dpy
, root
, False
, SubstructureNotifyMask
, &e
);
530 static char buf
[BUFSIZ
];
533 unsigned long ldummy
;
534 unsigned char *p
= NULL
;
536 XGetWindowProperty(dpy
, win
, wmatom
[a
], 0L, BUFSIZ
, False
, XA_STRING
,
537 &adummy
, &idummy
, &ldummy
, &ldummy
, &p
);
539 strncpy(buf
, (char *)p
, LENGTH(buf
)-1);
552 for (i
= 0; i
< nclients
; i
++) {
553 if (clients
[i
]->win
== w
)
561 getcolor(const char *colstr
)
563 Colormap cmap
= DefaultColormap(dpy
, screen
);
566 if (!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
567 die("tabbed: cannot allocate color '%s'\n", colstr
);
582 cc
= (ww
- TEXTW(before
) - TEXTW(after
)) / tabwidth
;
584 ret
= sel
- cc
/ 2 + (cc
+ 1) % 2;
586 ret
+ cc
> nclients
? MAX(0, nclients
- cc
) :
591 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
597 if (!text
|| size
== 0)
601 XGetTextProperty(dpy
, w
, &name
, atom
);
605 if (name
.encoding
== XA_STRING
) {
606 strncpy(text
, (char *)name
.value
, size
- 1);
607 } else if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
609 strncpy(text
, *list
, size
- 1);
610 XFreeStringList(list
);
612 text
[size
- 1] = '\0';
619 initfont(const char *fontstr
)
621 char *def
, **missing
, **font_names
;
622 XFontStruct
**xfonts
;
627 XFreeFontSet(dpy
, dc
.font
.set
);
629 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
632 fprintf(stderr
, "tabbed: missing fontset: %s\n", missing
[n
]);
633 XFreeStringList(missing
);
637 dc
.font
.ascent
= dc
.font
.descent
= 0;
638 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
639 for (i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0;
641 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
642 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
647 XFreeFont(dpy
, dc
.font
.xfont
);
648 dc
.font
.xfont
= NULL
;
649 if (!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)) &&
650 !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
651 die("tabbed: cannot load font: '%s'\n", fontstr
);
653 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
654 dc
.font
.descent
= dc
.font
.xfont
->descent
;
656 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
666 if (XGetWMProtocols(dpy
, clients
[c
]->win
, &protocols
, &n
)) {
667 for (i
= 0; !ret
&& i
< n
; i
++) {
668 if (protocols
[i
] == wmatom
[WMDelete
])
678 keypress(const XEvent
*e
)
680 const XKeyEvent
*ev
= &e
->xkey
;
684 keysym
= XkbKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0, 0);
685 for (i
= 0; i
< LENGTH(keys
); i
++) {
686 if (keysym
== keys
[i
].keysym
&&
687 CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
) &&
689 keys
[i
].func(&(keys
[i
].arg
));
694 killclient(const Arg
*arg
)
701 if (isprotodel(sel
) && !clients
[sel
]->closed
) {
702 ev
.type
= ClientMessage
;
703 ev
.xclient
.window
= clients
[sel
]->win
;
704 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
705 ev
.xclient
.format
= 32;
706 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
707 ev
.xclient
.data
.l
[1] = CurrentTime
;
708 XSendEvent(dpy
, clients
[sel
]->win
, False
, NoEventMask
, &ev
);
709 clients
[sel
]->closed
= True
;
711 XKillClient(dpy
, clients
[sel
]->win
);
721 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
,
722 numlockmask
| LockMask
};
727 XWithdrawWindow(dpy
, w
, 0);
728 XReparentWindow(dpy
, w
, win
, 0, bh
);
729 XSelectInput(dpy
, w
, PropertyChangeMask
|
730 StructureNotifyMask
| EnterWindowMask
);
733 for (i
= 0; i
< LENGTH(keys
); i
++) {
734 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
))) {
735 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
736 XGrabKey(dpy
, code
, keys
[i
].mod
|
737 modifiers
[j
], w
, True
,
738 GrabModeAsync
, GrabModeAsync
);
743 c
= ecalloc(1, sizeof *c
);
747 clients
= erealloc(clients
, sizeof(Client
*) * nclients
);
750 nextpos
= sel
+ newposition
;
753 nextpos
= nclients
- newposition
;
755 nextpos
= newposition
;
757 if (nextpos
>= nclients
)
758 nextpos
= nclients
- 1;
762 if (nclients
> 1 && nextpos
< nclients
- 1)
763 memmove(&clients
[nextpos
+ 1], &clients
[nextpos
],
764 sizeof(Client
*) * (nclients
- nextpos
- 1));
766 clients
[nextpos
] = c
;
767 updatetitle(nextpos
);
769 XLowerWindow(dpy
, w
);
772 e
.xclient
.window
= w
;
773 e
.xclient
.type
= ClientMessage
;
774 e
.xclient
.message_type
= wmatom
[XEmbed
];
775 e
.xclient
.format
= 32;
776 e
.xclient
.data
.l
[0] = CurrentTime
;
777 e
.xclient
.data
.l
[1] = XEMBED_EMBEDDED_NOTIFY
;
778 e
.xclient
.data
.l
[2] = 0;
779 e
.xclient
.data
.l
[3] = win
;
780 e
.xclient
.data
.l
[4] = 0;
781 XSendEvent(dpy
, root
, False
, NoEventMask
, &e
);
785 /* Adjust sel before focus does set it to lastsel. */
788 focus(nextfocus
? nextpos
:
791 nextfocus
= foreground
;
796 maprequest(const XEvent
*e
)
798 const XMapRequestEvent
*ev
= &e
->xmaprequest
;
800 if (getclient(ev
->window
) < 0)
807 if (arg
->i
>= 0 && arg
->i
< nclients
)
812 movetab(const Arg
*arg
)
817 c
= (sel
+ arg
->i
) % nclients
;
821 if (sel
< 0 || c
== sel
)
826 memmove(&clients
[sel
], &clients
[sel
+1],
827 sizeof(Client
*) * (c
- sel
));
829 memmove(&clients
[c
+1], &clients
[c
],
830 sizeof(Client
*) * (sel
- c
));
838 propertynotify(const XEvent
*e
)
840 const XPropertyEvent
*ev
= &e
->xproperty
;
843 char* selection
= NULL
;
846 if (ev
->state
== PropertyNewValue
&& ev
->atom
== wmatom
[WMSelectTab
]) {
847 selection
= getatom(WMSelectTab
);
848 if (!strncmp(selection
, "0x", 2)) {
849 arg
.i
= getclient(strtoul(selection
, NULL
, 0));
852 cmd
[cmd_append_pos
] = selection
;
856 } else if (ev
->state
== PropertyNewValue
&& ev
->atom
== XA_WM_HINTS
&&
857 (c
= getclient(ev
->window
)) > -1 &&
858 (wmh
= XGetWMHints(dpy
, clients
[c
]->win
))) {
859 if (wmh
->flags
& XUrgencyHint
) {
861 wmh
= XGetWMHints(dpy
, win
);
863 if (urgentswitch
&& wmh
&&
864 !(wmh
->flags
& XUrgencyHint
)) {
865 /* only switch, if tabbed was focused
866 * since last urgency hint if WMHints
867 * could not be received,
868 * default to no switch */
871 /* if no switch should be performed,
872 * mark tab as urgent */
873 clients
[c
]->urgent
= True
;
877 if (wmh
&& !(wmh
->flags
& XUrgencyHint
)) {
878 /* update tabbed urgency hint
879 * if not set already */
880 wmh
->flags
|= XUrgencyHint
;
881 XSetWMHints(dpy
, win
, wmh
);
885 } else if (ev
->state
!= PropertyDelete
&& ev
->atom
== XA_WM_NAME
&&
886 (c
= getclient(ev
->window
)) > -1) {
892 resize(int c
, int w
, int h
)
899 ce
.width
= wc
.width
= w
;
900 ce
.height
= wc
.height
= h
;
901 ce
.type
= ConfigureNotify
;
903 ce
.event
= clients
[c
]->win
;
904 ce
.window
= clients
[c
]->win
;
906 ce
.override_redirect
= False
;
909 XConfigureWindow(dpy
, clients
[c
]->win
, CWWidth
| CWHeight
, &wc
);
910 XSendEvent(dpy
, clients
[c
]->win
, False
, StructureNotifyMask
,
915 rotate(const Arg
*arg
)
925 } else if (sel
> -1) {
926 /* Rotating in an arg->i step around the clients. */
928 while (nsel
>= nclients
)
941 /* main event loop */
944 if (doinitspawn
== True
)
948 XNextEvent(dpy
, &ev
);
949 if (handler
[ev
.type
])
950 (handler
[ev
.type
])(&ev
); /* call handler */
955 sendxembed(int c
, long msg
, long detail
, long d1
, long d2
)
959 e
.xclient
.window
= clients
[c
]->win
;
960 e
.xclient
.type
= ClientMessage
;
961 e
.xclient
.message_type
= wmatom
[XEmbed
];
962 e
.xclient
.format
= 32;
963 e
.xclient
.data
.l
[0] = CurrentTime
;
964 e
.xclient
.data
.l
[1] = msg
;
965 e
.xclient
.data
.l
[2] = detail
;
966 e
.xclient
.data
.l
[3] = d1
;
967 e
.xclient
.data
.l
[4] = d2
;
968 XSendEvent(dpy
, clients
[c
]->win
, False
, NoEventMask
, &e
);
972 setcmd(int argc
, char *argv
[], int replace
)
976 cmd
= ecalloc(argc
+ 3, sizeof(*cmd
));
979 for (i
= 0; i
< argc
; i
++)
981 cmd
[replace
> 0 ? replace
: argc
] = winid
;
982 cmd_append_pos
= argc
+ !replace
;
983 cmd
[cmd_append_pos
] = cmd
[cmd_append_pos
+ 1] = NULL
;
989 int bitm
, tx
, ty
, tw
, th
, dh
, dw
, isfixed
;
991 XClassHint class_hint
;
992 XSizeHints
*size_hint
;
994 /* clean up any zombies immediately */
998 screen
= DefaultScreen(dpy
);
999 root
= RootWindow(dpy
, screen
);
1001 bh
= dc
.h
= dc
.font
.height
+ 2;
1004 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1005 wmatom
[WMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN",
1007 wmatom
[WMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1008 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1009 wmatom
[WMSelectTab
] = XInternAtom(dpy
, "_TABBED_SELECT_TAB", False
);
1010 wmatom
[WMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1011 wmatom
[XEmbed
] = XInternAtom(dpy
, "_XEMBED", False
);
1013 /* init appearance */
1021 tx
= ty
= tw
= th
= 0;
1022 bitm
= XParseGeometry(geometry
, &tx
, &ty
, (unsigned *)&tw
,
1028 if (bitm
& WidthValue
)
1030 if (bitm
& HeightValue
)
1032 if (bitm
& XNegative
&& wx
== 0)
1034 if (bitm
& YNegative
&& wy
== 0)
1036 if (bitm
& (HeightValue
| WidthValue
))
1039 dw
= DisplayWidth(dpy
, screen
);
1040 dh
= DisplayHeight(dpy
, screen
);
1042 wx
= dw
+ wx
- ww
- 1;
1044 wy
= dh
+ wy
- wh
- 1;
1047 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1048 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1049 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1050 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1051 dc
.urg
[ColBG
] = getcolor(urgbgcolor
);
1052 dc
.urg
[ColFG
] = getcolor(urgfgcolor
);
1053 dc
.drawable
= XCreatePixmap(dpy
, root
, ww
, wh
,
1054 DefaultDepth(dpy
, screen
));
1055 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1057 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1059 win
= XCreateSimpleWindow(dpy
, root
, wx
, wy
, ww
, wh
, 0,
1060 dc
.norm
[ColFG
], dc
.norm
[ColBG
]);
1061 XMapRaised(dpy
, win
);
1062 XSelectInput(dpy
, win
, SubstructureNotifyMask
| FocusChangeMask
|
1063 ButtonPressMask
| ExposureMask
| KeyPressMask
|
1064 PropertyChangeMask
| StructureNotifyMask
|
1065 SubstructureRedirectMask
);
1066 xerrorxlib
= XSetErrorHandler(xerror
);
1068 class_hint
.res_name
= wmname
;
1069 class_hint
.res_class
= "tabbed";
1070 XSetClassHint(dpy
, win
, &class_hint
);
1072 size_hint
= XAllocSizeHints();
1074 size_hint
->flags
= PSize
;
1075 size_hint
->height
= wh
;
1076 size_hint
->width
= ww
;
1078 size_hint
->flags
= PMaxSize
| PMinSize
;
1079 size_hint
->min_width
= size_hint
->max_width
= ww
;
1080 size_hint
->min_height
= size_hint
->max_height
= wh
;
1082 wmh
= XAllocWMHints();
1083 XSetWMProperties(dpy
, win
, NULL
, NULL
, NULL
, 0, size_hint
, wmh
, NULL
);
1087 XSetWMProtocols(dpy
, win
, &wmatom
[WMDelete
], 1);
1089 snprintf(winid
, sizeof(winid
), "%lu", win
);
1090 setenv("XEMBED", winid
, 1);
1092 nextfocus
= foreground
;
1099 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1100 die("tabbed: cannot install SIGCHLD handler");
1102 while (0 < waitpid(-1, NULL
, WNOHANG
));
1106 spawn(const Arg
*arg
)
1110 close(ConnectionNumber(dpy
));
1113 if (arg
&& arg
->v
) {
1114 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1115 fprintf(stderr
, "tabbed: execvp %s",
1116 ((char **)arg
->v
)[0]);
1118 cmd
[cmd_append_pos
] = NULL
;
1119 execvp(cmd
[0], cmd
);
1120 fprintf(stderr
, "tabbed: execvp %s", cmd
[0]);
1128 textnw(const char *text
, unsigned int len
)
1133 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1137 return XTextWidth(dc
.font
.xfont
, text
, len
);
1141 toggle(const Arg
*arg
)
1143 *(Bool
*) arg
->v
= !*(Bool
*) arg
->v
;
1149 if (c
< 0 || c
>= nclients
) {
1162 memmove(&clients
[0], &clients
[1], sizeof(Client
*) * nclients
);
1163 } else if (c
== nclients
- 1) {
1167 clients
= erealloc(clients
, sizeof(Client
*) * nclients
);
1169 /* Somewhere inbetween. */
1171 memmove(&clients
[c
], &clients
[c
+1],
1172 sizeof(Client
*) * (nclients
- (c
+ 1)));
1176 if (nclients
<= 0) {
1179 if (closelastclient
)
1181 else if (fillagain
&& running
)
1184 if (lastsel
>= nclients
)
1185 lastsel
= nclients
- 1;
1186 else if (lastsel
> c
)
1189 if (c
== sel
&& lastsel
>= 0) {
1194 if (sel
>= nclients
)
1206 unmapnotify(const XEvent
*e
)
1208 const XUnmapEvent
*ev
= &e
->xunmap
;
1211 if ((c
= getclient(ev
->window
)) > -1)
1216 updatenumlockmask(void)
1219 XModifierKeymap
*modmap
;
1222 modmap
= XGetModifierMapping(dpy
);
1223 for (i
= 0; i
< 8; i
++) {
1224 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
1225 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1226 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1227 numlockmask
= (1 << i
);
1230 XFreeModifiermap(modmap
);
1236 if (!gettextprop(clients
[c
]->win
, wmatom
[WMName
], clients
[c
]->name
,
1237 sizeof(clients
[c
]->name
)))
1238 gettextprop(clients
[c
]->win
, XA_WM_NAME
, clients
[c
]->name
,
1239 sizeof(clients
[c
]->name
));
1241 xsettitle(win
, clients
[c
]->name
);
1245 /* There's no way to check accesses to destroyed windows, thus those cases are
1246 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1247 * default error handler, which may call exit. */
1249 xerror(Display
*dpy
, XErrorEvent
*ee
)
1251 if (ee
->error_code
== BadWindow
1252 || (ee
->request_code
== X_SetInputFocus
&&
1253 ee
->error_code
== BadMatch
)
1254 || (ee
->request_code
== X_PolyText8
&&
1255 ee
->error_code
== BadDrawable
)
1256 || (ee
->request_code
== X_PolyFillRectangle
&&
1257 ee
->error_code
== BadDrawable
)
1258 || (ee
->request_code
== X_PolySegment
&&
1259 ee
->error_code
== BadDrawable
)
1260 || (ee
->request_code
== X_ConfigureWindow
&&
1261 ee
->error_code
== BadMatch
)
1262 || (ee
->request_code
== X_GrabButton
&&
1263 ee
->error_code
== BadAccess
)
1264 || (ee
->request_code
== X_GrabKey
&&
1265 ee
->error_code
== BadAccess
)
1266 || (ee
->request_code
== X_CopyArea
&&
1267 ee
->error_code
== BadDrawable
))
1270 fprintf(stderr
, "tabbed: fatal error: request code=%d, error code=%d\n",
1271 ee
->request_code
, ee
->error_code
);
1272 return xerrorxlib(dpy
, ee
); /* may call exit */
1276 xsettitle(Window w
, const char *str
)
1280 if (XmbTextListToTextProperty(dpy
, (char **)&str
, 1,
1281 XCompoundTextStyle
, &xtp
) == Success
) {
1282 XSetTextProperty(dpy
, w
, &xtp
, wmatom
[WMName
]);
1283 XSetTextProperty(dpy
, w
, &xtp
, XA_WM_NAME
);
1291 die("usage: %s [-dfhsv] [-g geometry] [-n name] [-p [s+/-]pos] "
1292 "[-r narg] [-o color] [-O color] [-t color] [-T color] "
1293 "[-u color] [-U color] command...\n", argv0
);
1297 main(int argc
, char *argv
[])
1299 Bool detach
= False
;
1305 closelastclient
= True
;
1315 geometry
= EARGF(usage());
1318 wmname
= EARGF(usage());
1321 normfgcolor
= EARGF(usage());
1324 normbgcolor
= EARGF(usage());
1327 pstr
= EARGF(usage());
1328 if (pstr
[0] == 's') {
1329 npisrelative
= True
;
1330 newposition
= atoi(&pstr
[1]);
1332 newposition
= atoi(pstr
);
1336 replace
= atoi(EARGF(usage()));
1339 doinitspawn
= False
;
1342 selfgcolor
= EARGF(usage());
1345 selbgcolor
= EARGF(usage());
1348 urgfgcolor
= EARGF(usage());
1351 urgbgcolor
= EARGF(usage());
1354 die("tabbed-"VERSION
", © 2009-2016"
1355 " tabbed engineers, see LICENSE"
1358 default: /* FALLTHROUGH */
1364 doinitspawn
= False
;
1368 setcmd(argc
, argv
, replace
);
1370 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1371 fprintf(stderr
, "%s: no locale support\n", argv0
);
1372 if (!(dpy
= XOpenDisplay(NULL
)))
1373 die("%s: cannot open display\n", argv0
);
1376 printf("0x%lx\n", win
);
1384 close(ConnectionNumber(dpy
));
1385 return EXIT_SUCCESS
;
1393 return EXIT_SUCCESS
;