JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
introduced tile.c, some refactoring of functions
[dwm.git] / view.c
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2  * See LICENSE file for license details.
3  */
4 #include "dwm.h"
5
6 /* extern */
7
8 void (*arrange)(void) = DEFMODE;
9
10 void
11 detach(Client *c) {
12         if(c->prev)
13                 c->prev->next = c->next;
14         if(c->next)
15                 c->next->prev = c->prev;
16         if(c == clients)
17                 clients = c->next;
18         c->next = c->prev = NULL;
19 }
20
21 void
22 dofloat(void) {
23         Client *c;
24
25         for(c = clients; c; c = c->next) {
26                 if(isvisible(c)) {
27                         if(c->isbanned)
28                                 XMoveWindow(dpy, c->win, c->x, c->y);
29                         c->isbanned = False;
30                         resize(c, c->x, c->y, c->w, c->h, True);
31                 }
32                 else {
33                         c->isbanned = True;
34                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
35                 }
36         }
37         if(!sel || !isvisible(sel)) {
38                 for(c = stack; c && !isvisible(c); c = c->snext);
39                 focus(c);
40         }
41         restack();
42 }
43
44 void
45 focusnext(Arg *arg) {
46         Client *c;
47    
48         if(!sel)
49                 return;
50         for(c = sel->next; c && !isvisible(c); c = c->next);
51         if(!c)
52                 for(c = clients; c && !isvisible(c); c = c->next);
53         if(c) {
54                 focus(c);
55                 restack();
56         }
57 }
58
59 void
60 focusprev(Arg *arg) {
61         Client *c;
62
63         if(!sel)
64                 return;
65         for(c = sel->prev; c && !isvisible(c); c = c->prev);
66         if(!c) {
67                 for(c = clients; c && c->next; c = c->next);
68                 for(; c && !isvisible(c); c = c->prev);
69         }
70         if(c) {
71                 focus(c);
72                 restack();
73         }
74 }
75
76 Bool
77 isvisible(Client *c) {
78         unsigned int i;
79
80         for(i = 0; i < ntags; i++)
81                 if(c->tags[i] && seltag[i])
82                         return True;
83         return False;
84 }
85
86 Client *
87 nextmanaged(Client *c) {
88         for(; c && (c->isfloat || !isvisible(c)); c = c->next);
89         return c;
90 }
91
92 void
93 restack(void) {
94         Client *c;
95         XEvent ev;
96
97         drawstatus();
98         if(!sel)
99                 return;
100         if(sel->isfloat || arrange == dofloat)
101                 XRaiseWindow(dpy, sel->win);
102         if(arrange != dofloat) {
103                 if(!sel->isfloat)
104                         XLowerWindow(dpy, sel->win);
105                 for(c = nextmanaged(clients); c; c = nextmanaged(c->next)) {
106                         if(c == sel)
107                                 continue;
108                         XLowerWindow(dpy, c->win);
109                 }
110         }
111         XSync(dpy, False);
112         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
113 }
114
115 void
116 togglefloat(Arg *arg) {
117         if(!sel || arrange == dofloat)
118                 return;
119         sel->isfloat = !sel->isfloat;
120         arrange();
121 }
122
123 void
124 togglemode(Arg *arg) {
125         arrange = (arrange == dofloat) ? dotile : dofloat;
126         if(sel)
127                 arrange();
128         else
129                 drawstatus();
130 }
131
132 void
133 toggleview(Arg *arg) {
134         unsigned int i;
135
136         seltag[arg->i] = !seltag[arg->i];
137         for(i = 0; i < ntags && !seltag[i]; i++);
138         if(i == ntags)
139                 seltag[arg->i] = True; /* cannot toggle last view */
140         arrange();
141 }
142
143 void
144 view(Arg *arg) {
145         unsigned int i;
146
147         for(i = 0; i < ntags; i++)
148                 seltag[i] = (arg->i == -1) ? True : False;
149         if(arg->i >= 0 && arg->i < ntags)
150                 seltag[arg->i] = True;
151         arrange();
152 }
153