fix colors handling
[awesome.git] / layout.c
blobcaa9d824226dbd21402781c9e72806c31cf25829
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 "ewmh.h"
33 #include "layouts/tile.h"
34 #include "layouts/max.h"
35 #include "layouts/fibonacci.h"
36 #include "layouts/floating.h"
38 extern AwesomeConf globalconf;
40 const NameFuncLink LayoutsList[] =
42 {"tile", layout_tile},
43 {"tileleft", layout_tileleft},
44 {"max", layout_max},
45 {"spiral", layout_spiral},
46 {"dwindle", layout_dwindle},
47 {"floating", layout_floating},
48 {NULL, NULL}
51 /** Arrange windows following current selected layout
52 * \param screen the screen to arrange
54 void
55 arrange(int screen)
57 Client *c;
58 Tag **curtags = get_current_tags(screen);
60 for(c = globalconf.clients; c; c = c->next)
62 if(client_isvisible(c, screen))
63 client_unban(c);
64 /* we don't touch other screens windows */
65 else if(c->screen == screen)
66 client_ban(c);
69 curtags[0]->layout->arrange(screen);
70 focus(focus_get_current_client(screen), True, screen);
71 p_delete(&curtags);
72 restack(screen);
75 /** Send focus to next client in stack
76 * \param screen Screen ID
77 * \param arg Unused
78 * \ingroup ui_callback
80 void
81 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
83 Client *c, *sel = globalconf.focus->client;
85 if(!sel)
86 return;
87 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
88 if(!c)
89 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
90 if(c)
92 focus(c, True, screen);
93 restack(screen);
97 /** Send focus to previous client in stack
98 * \param screen Screen ID
99 * \param arg Unused
100 * \ingroup ui_callback
102 void
103 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
105 Client *c, *sel = globalconf.focus->client;
107 if(!sel)
108 return;
109 for(c = sel->prev; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
110 if(!c)
112 for(c = globalconf.clients; c && c->next; c = c->next);
113 for(; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
115 if(c)
117 focus(c, True, screen);
118 restack(screen);
122 void
123 loadawesomeprops(int screen)
125 int i, ntags = 0;
126 char *prop;
127 Tag *tag;
129 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
130 ntags++;
132 prop = p_new(char, ntags + 1);
134 if(xgettextprop(globalconf.display,
135 RootWindow(globalconf.display, get_phys_screen(screen)),
136 AWESOMEPROPS_ATOM(globalconf.display), prop, ntags + 1))
137 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
138 if(prop[i] == '1')
139 tag->selected = True;
140 else
141 tag->selected = False;
143 p_delete(&prop);
145 ewmh_update_net_current_desktop(get_phys_screen(screen));
148 void
149 restack(int screen)
151 Client *c, *sel = globalconf.focus->client;
152 XEvent ev;
153 XWindowChanges wc;
154 Tag **curtags;
156 statusbar_draw(screen);
158 if(!sel)
159 return;
161 if(globalconf.screens[screen].allow_lower_floats)
162 XRaiseWindow(globalconf.display, sel->win);
163 else
165 curtags = get_current_tags(screen);
166 if(sel->isfloating ||
167 curtags[0]->layout->arrange == layout_floating)
168 XRaiseWindow(globalconf.display, sel->win);
169 if(!(curtags[0]->layout->arrange == layout_floating))
171 wc.stack_mode = Below;
172 wc.sibling = globalconf.screens[screen].statusbar->window;
173 if(!sel->isfloating)
175 XConfigureWindow(globalconf.display, sel->win, CWSibling | CWStackMode, &wc);
176 wc.sibling = sel->win;
178 for(c = globalconf.clients; c; c = c->next)
180 if(!IS_TILED(c, screen) || c == sel)
181 continue;
182 XConfigureWindow(globalconf.display, c->win, CWSibling | CWStackMode, &wc);
183 wc.sibling = c->win;
186 p_delete(&curtags);
188 if(globalconf.screens[screen].focus_move_pointer)
189 XWarpPointer(globalconf.display, None, sel->win, 0, 0, 0, 0, sel->w / 2, sel->h / 2);
190 XSync(globalconf.display, False);
191 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &ev));
194 void
195 saveawesomeprops(int screen)
197 int i, ntags = 0;
198 char *prop;
199 Tag *tag;
201 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
202 ntags++;
204 prop = p_new(char, ntags + 1);
206 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
207 prop[i] = tag->selected ? '1' : '0';
209 prop[i] = '\0';
210 XChangeProperty(globalconf.display,
211 RootWindow(globalconf.display, get_phys_screen(screen)),
212 AWESOMEPROPS_ATOM(globalconf.display), XA_STRING, 8,
213 PropModeReplace, (unsigned char *) prop, i);
214 p_delete(&prop);
217 /** Set layout for tag
218 * \param screen Screen ID
219 * \param arg Layout specifier
220 * \ingroup ui_callback
222 void
223 uicb_tag_setlayout(int screen, char *arg)
225 Layout *l = globalconf.screens[screen].layouts;
226 Tag *tag, **curtags;
227 int i;
229 if(arg)
231 curtags = get_current_tags(screen);
232 for(i = 0; l && l != curtags[0]->layout; i++, l = l->next);
233 p_delete(&curtags);
234 if(!l)
235 i = 0;
236 for(i = compute_new_value_from_arg(arg, (double) i),
237 l = globalconf.screens[screen].layouts; l && i > 0; i--)
238 l = l->next;
239 if(!l)
240 l = globalconf.screens[screen].layouts;
243 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
244 if(tag->selected)
245 tag->layout = l;
247 if(globalconf.focus->client)
248 arrange(screen);
249 else
250 statusbar_draw(screen);
252 saveawesomeprops(screen);
255 void
256 client_maximize(Client *c, int x, int y, int w, int h)
258 if((c->ismax = !c->ismax))
260 c->oldborder = c->border;
261 c->border = 0;
262 c->wasfloating = c->isfloating;
263 c->isfloating = True;
264 client_resize(c, x, y, w, h, False, True);
266 else if(c->wasfloating)
267 client_resize(c, c->rx, c->ry, c->rw, c->rh, True, False);
268 else
269 c->isfloating = False;
271 c->border = c->oldborder;
273 arrange(c->screen);
276 /** Toggle maximize for client
277 * \param screen Screen ID
278 * \param arg Unused
279 * \ingroup ui_callback
281 void
282 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
284 Client *sel = globalconf.focus->client;
285 Area area = get_screen_area(screen,
286 globalconf.screens[screen].statusbar,
287 &globalconf.screens[screen].padding);
288 if(sel)
289 client_maximize(sel, area.x, area.y,
290 area.width - 2 * globalconf.screens[screen].borderpx,
291 area.height - 2 * globalconf.screens[screen].borderpx);
294 /** Toggle vertical maximize for client
295 * \param screen Screen ID
296 * \param arg Unused
297 * \ingroup ui_callback
299 void
300 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
302 Client *sel = globalconf.focus->client;
303 Area area = get_screen_area(screen,
304 globalconf.screens[screen].statusbar,
305 &globalconf.screens[screen].padding);
307 if(sel)
308 client_maximize(sel, sel->x, area.y,
309 sel->w,
310 area.height - 2 * globalconf.screens[screen].borderpx);
314 /** Toggle horizontal maximize for client
315 * \param screen Screen ID
316 * \param arg Unused
317 * \ingroup ui_callback
319 void
320 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
322 Client *sel = globalconf.focus->client;
323 Area area = get_screen_area(screen,
324 globalconf.screens[screen].statusbar,
325 &globalconf.screens[screen].padding);
327 if(sel)
328 client_maximize(sel, area.x, sel->y,
329 area.height - 2 * globalconf.screens[screen].borderpx,
330 sel->h);
333 /** Zoom client
334 * \param screen Screen ID
335 * \param arg Unused
336 * \ingroup ui_callback
338 void
339 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
341 Client *sel = globalconf.focus->client;
343 if(globalconf.clients == sel)
344 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
346 if(!sel)
347 return;
349 client_detach(sel);
350 client_attach(sel);
352 focus(sel, True, screen);
353 arrange(screen);
356 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80