fix segfault in client_zoom
[awesome.git] / layout.c
blobad1578d8d153cb16d7616cc740c05120b6a6b4d0
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 "xutil.h"
27 #include "focus.h"
28 #include "widget.h"
29 #include "window.h"
30 #include "client.h"
31 #include "screen.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 #include "layoutgen.h"
41 static void
42 layout_raise_floatings(int screen)
44 Client *c;
46 for(c = globalconf.clients; c; c = c->next)
47 if(c->isfloating && client_isvisible(c, screen))
48 XRaiseWindow(globalconf.display, c->win);
51 /** Arrange windows following current selected layout
52 * \param screen the screen to arrange
54 static void
55 arrange(int screen)
57 Client *c;
58 Layout *curlay = get_current_layout(screen);
59 unsigned int dui;
60 int di, x, y;
61 Window rootwin, childwin;
63 for(c = globalconf.clients; c; c = c->next)
65 if(client_isvisible(c, screen) && !c->newcomer)
66 client_unban(c);
67 /* we don't touch other screens windows */
68 else if(c->screen == screen || c->newcomer)
69 client_ban(c);
72 curlay->arrange(screen);
74 for(c = globalconf.clients; c; c = c->next)
75 if(c->newcomer && client_isvisible(c, screen))
77 c->newcomer = False;
78 client_unban(c);
79 if(globalconf.screens[screen].new_get_focus)
80 client_focus(c, screen, False);
83 layout_raise_floatings(screen);
85 /* if we have a valid client that could be focused but currently no window
86 * are focused, then set the focus on this window */
87 if((c = focus_get_current_client(screen)) && !globalconf.focus->client)
88 client_focus(c, screen, False);
90 /* check that the mouse is on a window or not */
91 if(XQueryPointer(globalconf.display, RootWindow(globalconf.display,
92 get_phys_screen(screen)),
93 &rootwin, &childwin, &x, &y, &di, &di, &dui)
94 && (rootwin == None || childwin == None || childwin == rootwin))
95 window_root_grabbuttons(screen);
97 /* reset status */
98 globalconf.screens[screen].need_arrange = False;
102 layout_refresh(void)
104 int screen;
105 int arranged = 0;
107 for(screen = 0; screen < globalconf.nscreen; screen++)
108 if(globalconf.screens[screen].need_arrange)
110 arrange(screen);
111 arranged++;
114 return arranged;
117 Layout *
118 get_current_layout(int screen)
120 Tag **curtags = tags_get_current(screen);
121 Layout *l = curtags[0]->layout;
122 p_delete(&curtags);
123 return l;
126 void
127 loadawesomeprops(int screen)
129 int i, ntags = 0;
130 char *prop;
131 Tag *tag;
133 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
134 ntags++;
136 prop = p_new(char, ntags + 1);
138 if(xgettextprop(RootWindow(globalconf.display, get_phys_screen(screen)),
139 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
140 prop, ntags + 1))
141 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
142 tag_view_byindex(screen, i, prop[i] == '1');
144 p_delete(&prop);
147 void
148 saveawesomeprops(int screen)
150 int i, ntags = 0;
151 char *prop;
152 Tag *tag;
154 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
155 ntags++;
157 prop = p_new(char, ntags + 1);
159 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
160 prop[i] = tag->selected ? '1' : '0';
162 prop[i] = '\0';
163 XChangeProperty(globalconf.display,
164 RootWindow(globalconf.display, get_phys_screen(screen)),
165 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
166 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
167 p_delete(&prop);
170 /** Set layout for tag
171 * \param screen Screen ID
172 * \param arg Layout specifier
173 * \ingroup ui_callback
175 void
176 uicb_tag_setlayout(int screen, char *arg)
178 Layout *l = globalconf.screens[screen].layouts;
179 Tag *tag, **curtags;
180 int i;
182 if(arg)
184 curtags = tags_get_current(screen);
185 for(i = 0; l && l != curtags[0]->layout; i++, l = l->next);
186 p_delete(&curtags);
188 if(!l)
189 i = 0;
191 i = compute_new_value_from_arg(arg, (double) i);
193 if(i >= 0)
194 for(l = globalconf.screens[screen].layouts; l && i > 0; i--)
195 l = l->next;
196 else
197 for(l = globalconf.screens[screen].layouts; l && i < 0; i++)
198 l = layout_list_prev_cycle(&globalconf.screens[screen].layouts, l);
200 if(!l)
201 l = globalconf.screens[screen].layouts;
204 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
205 if(tag->selected)
206 tag->layout = l;
208 if(globalconf.focus->client)
209 arrange(screen);
211 widget_invalidate_cache(screen, WIDGET_CACHE_LAYOUTS);
213 saveawesomeprops(screen);
216 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80