cosmetic
[awesome.git] / layout.c
blob82fcd6df96e3d66ecfbdcde10cfe55b6e210509f
1 /*
2 * layout.c - layout management
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <X11/Xatom.h>
23 #include <X11/Xutil.h>
25 #include "tag.h"
26 #include "util.h"
27 #include "xutil.h"
28 #include "focus.h"
29 #include "widget.h"
30 #include "window.h"
31 #include "ewmh.h"
32 #include "client.h"
33 #include "screen.h"
34 #include "layouts/tile.h"
35 #include "layouts/max.h"
36 #include "layouts/fibonacci.h"
37 #include "layouts/floating.h"
39 extern AwesomeConf globalconf;
41 const NameFuncLink LayoutsList[] =
43 {"tile", layout_tile},
44 {"tileleft", layout_tileleft},
45 {"max", layout_max},
46 {"spiral", layout_spiral},
47 {"dwindle", layout_dwindle},
48 {"floating", layout_floating},
49 {NULL, NULL}
52 /** Arrange windows following current selected layout
53 * \param screen the screen to arrange
55 void
56 arrange(int screen)
58 Client *c;
59 Tag **curtags = get_current_tags(screen);
60 Window client_win, root_win;
61 int x, y, d;
62 unsigned int m;
64 for(c = globalconf.clients; c; c = c->next)
66 if(client_isvisible(c, screen))
67 client_unban(c);
68 /* we don't touch other screens windows */
69 else if(c->screen == screen)
70 client_ban(c);
73 curtags[0]->layout->arrange(screen);
74 c = focus_get_current_client(screen);
75 focus(c, True, screen);
76 if(c && XQueryPointer(globalconf.display, RootWindow(globalconf.display, screen),
77 &root_win, &client_win, &x, &y, &d, &d, &m) &&
78 (root_win == None || client_win == None || client_win == root_win))
79 window_grabbuttons(c->screen, c->win, False, False);
81 p_delete(&curtags);
82 restack(screen);
86 Layout *
87 get_current_layout(int screen)
89 Tag **curtags = get_current_tags(screen);
90 Layout *l = curtags[0]->layout;
91 p_delete(&curtags);
92 return l;
95 /** Send focus to next client in stack
96 * \param screen Screen ID
97 * \param arg Unused
98 * \ingroup ui_callback
100 void
101 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
103 Client *c, *sel = globalconf.focus->client;
105 if(!sel)
106 return;
107 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
108 if(!c)
109 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
110 if(c)
112 focus(c, True, screen);
113 restack(screen);
117 /** Send focus to previous client in stack
118 * \param screen Screen ID
119 * \param arg Unused
120 * \ingroup ui_callback
122 void
123 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
125 Client *c, *sel = globalconf.focus->client;
127 if(!sel)
128 return;
129 for(c = sel->prev; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
130 if(!c)
132 for(c = globalconf.clients; c && c->next; c = c->next);
133 for(; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
135 if(c)
137 focus(c, True, screen);
138 restack(screen);
142 void
143 loadawesomeprops(int screen)
145 int i, ntags = 0;
146 char *prop;
147 Tag *tag;
149 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
150 ntags++;
152 prop = p_new(char, ntags + 1);
154 if(xgettextprop(RootWindow(globalconf.display, get_phys_screen(screen)),
155 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
156 prop, ntags + 1))
157 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
158 if(prop[i] == '1')
159 tag->selected = True;
160 else
161 tag->selected = False;
163 p_delete(&prop);
165 ewmh_update_net_current_desktop(get_phys_screen(screen));
168 void
169 restack(int screen)
171 Client *c, *sel = globalconf.focus->client;
172 XWindowChanges wc;
173 Tag **curtags;
175 if(!sel)
176 return;
178 if(globalconf.screens[screen].allow_lower_floats)
179 XRaiseWindow(globalconf.display, sel->win);
180 else
182 curtags = get_current_tags(screen);
183 if(sel->isfloating ||
184 curtags[0]->layout->arrange == layout_floating)
185 XRaiseWindow(globalconf.display, sel->win);
186 if(!(curtags[0]->layout->arrange == layout_floating))
188 wc.stack_mode = Below;
189 if(!sel->isfloating)
190 XConfigureWindow(globalconf.display, sel->win, CWStackMode, &wc);
191 for(c = globalconf.clients; c; c = c->next)
193 if(!IS_TILED(c, screen) || c == sel)
194 continue;
195 XConfigureWindow(globalconf.display, c->win, CWStackMode, &wc);
198 p_delete(&curtags);
200 if(globalconf.screens[screen].focus_move_pointer)
201 XWarpPointer(globalconf.display, None, sel->win, 0, 0, 0, 0,
202 sel->geometry.width / 2, sel->geometry.height / 2);
203 XSync(globalconf.display, False);
206 void
207 saveawesomeprops(int screen)
209 int i, ntags = 0;
210 char *prop;
211 Tag *tag;
213 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
214 ntags++;
216 prop = p_new(char, ntags + 1);
218 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
219 prop[i] = tag->selected ? '1' : '0';
221 prop[i] = '\0';
222 XChangeProperty(globalconf.display,
223 RootWindow(globalconf.display, get_phys_screen(screen)),
224 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
225 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
226 p_delete(&prop);
229 /** Set layout for tag
230 * \param screen Screen ID
231 * \param arg Layout specifier
232 * \ingroup ui_callback
234 void
235 uicb_tag_setlayout(int screen, char *arg)
237 Layout *l = globalconf.screens[screen].layouts;
238 Tag *tag, **curtags;
239 int i;
241 if(arg)
243 curtags = get_current_tags(screen);
244 for(i = 0; l && l != curtags[0]->layout; i++, l = l->next);
245 p_delete(&curtags);
247 if(!l)
248 i = 0;
250 i = compute_new_value_from_arg(arg, (double) i);
252 if(i >= 0)
253 for(l = globalconf.screens[screen].layouts; l && i > 0; i--)
254 l = l->next;
255 else
256 for(l = globalconf.screens[screen].layouts; l && i < 0; i++)
257 l = l->prev;
259 if(!l)
260 l = globalconf.screens[screen].layouts;
263 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
264 if(tag->selected)
265 tag->layout = l;
267 if(globalconf.focus->client)
268 arrange(screen);
270 widget_invalidate_cache(screen, WIDGET_CACHE_LAYOUTS);
272 saveawesomeprops(screen);
275 /** Toggle floating state of a client
276 * \param screen Screen ID
277 * \param arg unused
278 * \ingroup ui_callback
280 void
281 uicb_client_togglefloating(int screen, char *arg)
283 Client *sel = globalconf.focus->client;
285 if(!sel)
286 return;
288 if((sel->isfloating = !sel->isfloating))
290 if(!arg)
291 client_resize(sel, sel->f_geometry, False);
293 else if(sel->ismax)
294 client_resize(sel, sel->m_geometry, False);
296 widget_invalidate_cache(sel->screen, WIDGET_CACHE_CLIENTS);
297 client_saveprops(sel);
298 arrange(screen);
301 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80