JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
96e125ade5dfb0ea5b4d156ca5c0944a0a4c63aa
[dwm.git] / layout.c
1 /* See LICENSE file for copyright and license details. */
2 #include "dwm.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <X11/Xatom.h>
6 #include <X11/Xutil.h>
7
8 /* static */
9
10 typedef struct {
11         const char *symbol;
12         void (*arrange)(void);
13 } Layout;
14
15 unsigned int blw = 0;
16 static char prop[128];
17 static unsigned int ltidx = 0; /* default */
18
19 static void
20 floating(void) { /* default floating layout */
21         Client *c;
22
23         for(c = clients; c; c = c->next)
24                 if(isvisible(c))
25                         resize(c, c->x, c->y, c->w, c->h, True);
26 }
27
28 static unsigned int nlayouts = 0;
29
30 LAYOUTS
31
32 /* extern */
33
34 void
35 arrange(void) {
36         Client *c;
37
38         for(c = clients; c; c = c->next)
39                 if(isvisible(c))
40                         unban(c);
41                 else
42                         ban(c);
43         layouts[ltidx].arrange();
44         focus(NULL);
45         restack();
46 }
47
48 void
49 focusnext(const char *arg) {
50         Client *c;
51
52         if(!sel)
53                 return;
54         for(c = sel->next; c && !isvisible(c); c = c->next);
55         if(!c)
56                 for(c = clients; c && !isvisible(c); c = c->next);
57         if(c) {
58                 focus(c);
59                 restack();
60         }
61 }
62
63 void
64 focusprev(const char *arg) {
65         Client *c;
66
67         if(!sel)
68                 return;
69         for(c = sel->prev; c && !isvisible(c); c = c->prev);
70         if(!c) {
71                 for(c = clients; c && c->next; c = c->next);
72                 for(; c && !isvisible(c); c = c->prev);
73         }
74         if(c) {
75                 focus(c);
76                 restack();
77         }
78 }
79
80 const char *
81 getsymbol(void)
82 {
83         return layouts[ltidx].symbol;
84 }
85
86 Bool
87 isfloating(void) {
88         return layouts[ltidx].arrange == floating;
89 }
90
91 Bool
92 isarrange(void (*func)())
93 {
94         return func == layouts[ltidx].arrange;
95 }
96
97 void
98 initlayouts(void) {
99         unsigned int i, w;
100
101         /* TODO deserialize ltidx if present */
102         nlayouts = sizeof layouts / sizeof layouts[0];
103         for(blw = i = 0; i < nlayouts; i++) {
104                 w = textw(layouts[i].symbol);
105                 if(w > blw)
106                         blw = w;
107         }
108 }
109
110 void
111 loaddwmprops(void) {
112         unsigned int i;
113         XTextProperty name;
114
115         /* check if window has set a property */
116         name.nitems = 0;
117         XGetTextProperty(dpy, root, &name, dwmprops);
118         if(name.nitems && name.encoding == XA_STRING) {
119                 strncpy(prop, (char *)name.value, sizeof prop - 1);
120                 prop[sizeof prop - 1] = '\0';
121                 XFree(name.value);
122                 for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++)
123                         seltags[i] = prop[i] == '1';
124                 if(i < sizeof prop - 1 && prop[i] != '\0') {
125                         i = prop[i];
126                         if(i < nlayouts)
127                                 ltidx = i;
128                 }
129         }
130 }
131
132 Client *
133 nexttiled(Client *c) {
134         for(; c && (c->isfloating || !isvisible(c)); c = c->next);
135         return c;
136 }
137
138 void
139 restack(void) {
140         Client *c;
141         XEvent ev;
142         XWindowChanges wc;
143
144         drawstatus();
145         if(!sel)
146                 return;
147         if(sel->isfloating || isfloating())
148                 XRaiseWindow(dpy, sel->win);
149         if(!isfloating()) {
150                 wc.stack_mode = Below;
151                 wc.sibling = barwin;
152                 if(!sel->isfloating) {
153                         XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
154                         wc.sibling = sel->win;
155                 }
156                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
157                         if(c == sel)
158                                 continue;
159                         XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
160                         wc.sibling = c->win;
161                 }
162         }
163         XSync(dpy, False);
164         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
165 }
166
167 void
168 savedwmprops(void) {
169         unsigned int i;
170
171         for(i = 0; i < ntags && i < sizeof prop - 1; i++)
172                 prop[i] = seltags[i] ? '1' : '0';
173         if(i < sizeof prop - 1)
174                 prop[i++] = (char)ltidx;
175         prop[i] = '\0';
176         XChangeProperty(dpy, root, dwmprops, XA_STRING, 8,
177                         PropModeReplace, (unsigned char *)prop, i);
178 }
179
180 void
181 setlayout(const char *arg) {
182         int i;
183
184         if(!arg) {
185                 if(++ltidx == nlayouts)
186                         ltidx = 0;;
187         }
188         else {
189                 i = atoi(arg);
190                 if(i < 0 || i >= nlayouts)
191                         return;
192                 ltidx = i;
193         }
194         if(sel)
195                 arrange();
196         else
197                 drawstatus();
198         savedwmprops();
199 }
200
201 void
202 togglebar(const char *arg) {
203         if(bpos == BarOff)
204                 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
205         else
206                 bpos = BarOff;
207         updatebarpos();
208         arrange();
209 }
210
211 void
212 togglemax(const char *arg) {
213         XEvent ev;
214
215         if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed)
216                 return;
217         if((sel->ismax = !sel->ismax)) {
218                 sel->rx = sel->x;
219                 sel->ry = sel->y;
220                 sel->rw = sel->w;
221                 sel->rh = sel->h;
222                 resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
223         }
224         else
225                 resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
226         drawstatus();
227         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
228 }