JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made Layout a static struct in layout.c, added some convenience getters in layout...
[dwm.git] / layout.c
1 /* See LICENSE file for copyright and license details. */
2 #include "dwm.h"
3 #include <stdlib.h>
4
5 typedef struct {
6         const char *symbol;
7         void (*arrange)(void);
8 } Layout;
9
10 unsigned int blw = 0;
11 static Layout *lt = NULL;
12
13 /* static */
14
15 static void
16 floating(void) {
17         Client *c;
18
19         for(c = clients; c; c = c->next)
20                 if(isvisible(c))
21                         resize(c, c->x, c->y, c->w, c->h, True);
22 }
23
24 static unsigned int nlayouts = 0;
25
26 LAYOUTS
27
28 /* extern */
29
30 void
31 arrange(void) {
32         Client *c;
33
34         for(c = clients; c; c = c->next)
35                 if(isvisible(c))
36                         unban(c);
37                 else
38                         ban(c);
39         lt->arrange();
40         focus(NULL);
41         restack();
42 }
43
44 void
45 focusclient(const char *arg) {
46         Client *c;
47    
48         if(!sel || !arg)
49                 return;
50         if(atoi(arg) < 0) {
51                 for(c = sel->prev; c && !isvisible(c); c = c->prev);
52                 if(!c) {
53                         for(c = clients; c && c->next; c = c->next);
54                         for(; c && !isvisible(c); c = c->prev);
55                 }
56         }
57         else {
58                 for(c = sel->next; c && !isvisible(c); c = c->next);
59                 if(!c)
60                         for(c = clients; c && !isvisible(c); c = c->next);
61         }
62         if(c) {
63                 focus(c);
64                 restack();
65         }
66 }
67
68 const char *
69 getsymbol(void)
70 {
71         return lt->symbol;
72 }
73
74 Bool
75 isfloating(void) {
76         return lt->arrange == floating;
77 }
78
79 Bool
80 isarrange(void (*func)())
81 {
82         return func == lt->arrange;
83 }
84
85 void
86 initlayouts(void) {
87         unsigned int i, w;
88
89         lt = &layout[0];
90         nlayouts = sizeof layout / sizeof layout[0];
91         for(blw = i = 0; i < nlayouts; i++) {
92                 w = textw(layout[i].symbol);
93                 if(w > blw)
94                         blw = w;
95         }
96 }
97
98 Client *
99 nexttiled(Client *c) {
100         for(; c && (c->isfloating || !isvisible(c)); c = c->next);
101         return c;
102 }
103
104 void
105 restack(void) {
106         Client *c;
107         XEvent ev;
108         XWindowChanges wc;
109
110         drawstatus();
111         if(!sel)
112                 return;
113         if(sel->isfloating || lt->arrange == floating)
114                 XRaiseWindow(dpy, sel->win);
115         if(lt->arrange != floating) {
116                 wc.stack_mode = Below;
117                 wc.sibling = barwin;
118                 if(!sel->isfloating) {
119                         XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
120                         wc.sibling = sel->win;
121                 }
122                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
123                         if(c == sel)
124                                 continue;
125                         XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
126                         wc.sibling = c->win;
127                 }
128         }
129         XSync(dpy, False);
130         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
131 }
132
133 void
134 setlayout(const char *arg) {
135         int i;
136
137         if(!arg) {
138                 lt++;
139                 if(lt == layout + nlayouts)
140                         lt = layout;
141         }
142         else {
143                 i = atoi(arg);
144                 if(i < 0 || i >= nlayouts)
145                         return;
146                 lt = &layout[i];
147         }
148         if(sel)
149                 arrange();
150         else
151                 drawstatus();
152 }
153
154 void
155 togglebar(const char *arg) {
156         if(bpos == BarOff)
157                 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
158         else
159                 bpos = BarOff;
160         updatebarpos();
161         arrange();
162 }
163
164 void
165 togglemax(const char *arg) {
166         XEvent ev;
167
168         if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed)
169                 return;
170         if((sel->ismax = !sel->ismax)) {
171                 sel->rx = sel->x;
172                 sel->ry = sel->y;
173                 sel->rw = sel->w;
174                 sel->rh = sel->h;
175                 resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
176         }
177         else
178                 resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
179         drawstatus();
180         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
181 }