cosmetic
[awesome.git] / layout.c
blobd96389c4bbfe56df69b01f0bbfe4e47e6bbb506c
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 "screen.h"
26 #include "layout.h"
27 #include "tag.h"
28 #include "util.h"
29 #include "xutil.h"
30 #include "focus.h"
31 #include "statusbar.h"
32 #include "layouts/tile.h"
33 #include "layouts/max.h"
34 #include "layouts/fibonacci.h"
35 #include "layouts/floating.h"
37 extern AwesomeConf globalconf;
39 const NameFuncLink LayoutsList[] =
41 {"tile", layout_tile},
42 {"tileleft", layout_tileleft},
43 {"max", layout_max},
44 {"spiral", layout_spiral},
45 {"dwindle", layout_dwindle},
46 {"floating", layout_floating},
47 {NULL, NULL}
50 /** Find the index of the first currently selected tag
51 * \param screen the screen to search
52 * \return tag
54 #warning this function is bugged and should be replaced
55 Tag *
56 get_current_tag(int screen)
58 Tag *tag;
60 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
61 if(tag->selected)
62 return tag;
64 return NULL;
67 /** Arrange windows following current selected layout
68 * \param screen the screen to arrange
70 void
71 arrange(int screen)
73 Client *c;
74 Tag *curtag = get_current_tag(screen);
76 for(c = globalconf.clients; c; c = c->next)
78 if(client_isvisible(c, screen))
79 client_unban(c);
80 /* we don't touch other screens windows */
81 else if(c->screen == screen)
82 client_ban(c);
85 curtag->layout->arrange(screen);
86 focus(focus_get_latest_client_for_tag(screen, curtag),
87 True, screen);
88 restack(screen);
91 Layout *
92 get_current_layout(int screen)
94 Tag *curtag;
96 if ((curtag = get_current_tag(screen)))
97 return curtag->layout;
99 return NULL;
102 /** Send focus to next client in stack
103 * \param screen Screen ID
104 * \param arg Unused
105 * \ingroup ui_callback
107 void
108 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
110 Client *c, *sel = globalconf.focus->client;
112 if(!sel)
113 return;
114 for(c = sel->next; c && !client_isvisible(c, screen); c = c->next);
115 if(!c)
116 for(c = globalconf.clients; c && !client_isvisible(c, screen); c = c->next);
117 if(c)
119 focus(c, True, screen);
120 restack(screen);
124 /** Send focus to previous client in stack
125 * \param screen Screen ID
126 * \param arg Unused
127 * \ingroup ui_callback
129 void
130 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
132 Client *c, *sel = globalconf.focus->client;
134 if(!sel)
135 return;
136 for(c = sel->prev; c && !client_isvisible(c, screen); c = c->prev);
137 if(!c)
139 for(c = globalconf.clients; c && c->next; c = c->next);
140 for(; c && !client_isvisible(c, screen); c = c->prev);
142 if(c)
144 focus(c, True, screen);
145 restack(screen);
149 void
150 loadawesomeprops(int screen)
152 int i, ntags = 0;
153 char *prop;
154 Tag *tag;
156 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
157 ntags++;
159 prop = p_new(char, ntags + 1);
161 if(xgettextprop(globalconf.display,
162 RootWindow(globalconf.display, get_phys_screen(screen)),
163 AWESOMEPROPS_ATOM(globalconf.display), prop, ntags + 1))
164 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
165 if(prop[i] == '1')
166 tag->selected = True;
167 else
168 tag->selected = False;
170 p_delete(&prop);
173 void
174 restack(int screen)
176 Client *c, *sel = globalconf.focus->client;
177 XEvent ev;
178 XWindowChanges wc;
180 statusbar_draw(screen);
181 if(!sel)
182 return;
183 if(globalconf.screens[screen].allow_lower_floats)
184 XRaiseWindow(globalconf.display, sel->win);
185 else
187 if(sel->isfloating ||
188 get_current_layout(screen)->arrange == layout_floating)
189 XRaiseWindow(sel->display, sel->win);
190 if(!(get_current_layout(screen)->arrange == layout_floating))
192 wc.stack_mode = Below;
193 wc.sibling = globalconf.screens[screen].statusbar->window;
194 if(!sel->isfloating)
196 XConfigureWindow(sel->display, sel->win, CWSibling | CWStackMode, &wc);
197 wc.sibling = sel->win;
199 for(c = globalconf.clients; c; c = c->next)
201 if(!IS_TILED(c, screen) || c == sel)
202 continue;
203 XConfigureWindow(globalconf.display, c->win, CWSibling | CWStackMode, &wc);
204 wc.sibling = c->win;
208 if(globalconf.screens[screen].focus_move_pointer)
209 XWarpPointer(globalconf.display, None, sel->win, 0, 0, 0, 0, sel->w / 2, sel->h / 2);
210 XSync(globalconf.display, False);
211 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &ev));
214 void
215 saveawesomeprops(int screen)
217 int i, ntags = 0;
218 char *prop;
219 Tag *tag;
221 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
222 ntags++;
224 prop = p_new(char, ntags + 1);
226 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
227 prop[i] = tag->selected ? '1' : '0';
229 prop[i] = '\0';
230 XChangeProperty(globalconf.display,
231 RootWindow(globalconf.display, get_phys_screen(screen)),
232 AWESOMEPROPS_ATOM(globalconf.display), XA_STRING, 8,
233 PropModeReplace, (unsigned char *) prop, i);
234 p_delete(&prop);
237 /** Set layout for tag
238 * \param screen Screen ID
239 * \param arg Layout specifier
240 * \ingroup ui_callback
242 void
243 uicb_tag_setlayout(int screen, char *arg)
245 Layout *l = globalconf.screens[screen].layouts;
246 Tag *tag;
247 int i;
249 if(arg)
251 for(i = 0; l && l != get_current_layout(screen); i++, l = l->next);
252 if(!l)
253 i = 0;
254 for(i = compute_new_value_from_arg(arg, (double) i),
255 l = globalconf.screens[screen].layouts; l && i > 0; i--)
256 l = l->next;
257 if(!l)
258 l = globalconf.screens[screen].layouts;
261 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
262 if(tag->selected)
263 tag->layout = l;
265 if(globalconf.focus->client)
266 arrange(screen);
267 else
268 statusbar_draw(screen);
270 saveawesomeprops(screen);
273 static void
274 maximize(int x, int y, int w, int h, int screen)
276 Client *sel = globalconf.focus->client;
278 if(!sel)
279 return;
281 if((sel->ismax = !sel->ismax))
283 sel->wasfloating = sel->isfloating;
284 sel->isfloating = True;
285 client_resize(sel, x, y, w, h, True, !sel->isfloating);
287 else if(sel->wasfloating)
288 client_resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True, False);
289 else
290 sel->isfloating = False;
292 arrange(screen);
295 /** Toggle maximize for client
296 * \param screen Screen ID
297 * \param arg Unused
298 * \ingroup ui_callback
300 void
301 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
303 ScreenInfo *si = get_screen_info(screen,
304 globalconf.screens[screen].statusbar,
305 &globalconf.screens[screen].padding);
307 maximize(si[screen].x_org, si[screen].y_org,
308 si[screen].width - 2 * globalconf.screens[screen].borderpx,
309 si[screen].height - 2 * globalconf.screens[screen].borderpx,
310 screen);
311 p_delete(&si);
314 /** Toggle vertical maximize for client
315 * \param screen Screen ID
316 * \param arg Unused
317 * \ingroup ui_callback
319 void
320 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
322 Client *sel = globalconf.focus->client;
323 ScreenInfo *si = get_screen_info(screen,
324 globalconf.screens[screen].statusbar,
325 &globalconf.screens[screen].padding);
327 if(sel)
328 maximize(sel->x,
329 si[screen].y_org,
330 sel->w,
331 si[screen].height - 2 * globalconf.screens[screen].borderpx,
332 screen);
333 p_delete(&si);
337 /** Toggle horizontal maximize for client
338 * \param screen Screen ID
339 * \param arg Unused
340 * \ingroup ui_callback
342 void
343 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
345 Client *sel = globalconf.focus->client;
346 ScreenInfo *si = get_screen_info(screen,
347 globalconf.screens[screen].statusbar,
348 &globalconf.screens[screen].padding);
350 if(sel)
351 maximize(si[screen].x_org,
352 sel->y,
353 si[screen].height - 2 * globalconf.screens[screen].borderpx,
354 sel->h,
355 screen);
356 p_delete(&si);
359 /** Zoom client
360 * \param screen Screen ID
361 * \param arg Unused
362 * \ingroup ui_callback
364 void
365 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
367 Client *sel = globalconf.focus->client;
369 if(globalconf.clients == sel)
370 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
372 if(!sel)
373 return;
375 client_detach(sel);
376 client_attach(sel);
378 focus(sel, True, screen);
379 arrange(screen);
382 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80