awesome-client send now everything in one message
[awesome.git] / layout.c
blobd441d4634e6cb55a198b88dbb7ec3349d63792f1
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 "ewmh.h"
31 #include "client.h"
32 #include "screen.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);
76 Layout *
77 get_current_layout(int screen)
79 Tag **curtags = get_current_tags(screen);
80 Layout *l = curtags[0]->layout;
81 p_delete(&curtags);
82 return l;
85 /** Send focus to next client in stack
86 * \param screen Screen ID
87 * \param arg Unused
88 * \ingroup ui_callback
90 void
91 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
93 Client *c, *sel = globalconf.focus->client;
95 if(!sel)
96 return;
97 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
98 if(!c)
99 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
100 if(c)
102 focus(c, True, screen);
103 restack(screen);
107 /** Send focus to previous client in stack
108 * \param screen Screen ID
109 * \param arg Unused
110 * \ingroup ui_callback
112 void
113 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
115 Client *c, *sel = globalconf.focus->client;
117 if(!sel)
118 return;
119 for(c = sel->prev; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
120 if(!c)
122 for(c = globalconf.clients; c && c->next; c = c->next);
123 for(; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
125 if(c)
127 focus(c, True, screen);
128 restack(screen);
132 void
133 loadawesomeprops(int screen)
135 int i, ntags = 0;
136 char *prop;
137 Tag *tag;
139 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
140 ntags++;
142 prop = p_new(char, ntags + 1);
144 if(xgettextprop(RootWindow(globalconf.display, get_phys_screen(screen)),
145 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
146 prop, ntags + 1))
147 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
148 if(prop[i] == '1')
149 tag->selected = True;
150 else
151 tag->selected = False;
153 p_delete(&prop);
155 ewmh_update_net_current_desktop(get_phys_screen(screen));
158 void
159 restack(int screen)
161 Client *c, *sel = globalconf.focus->client;
162 XEvent ev;
163 XWindowChanges wc;
164 Tag **curtags;
166 if(!sel)
167 return;
169 if(globalconf.screens[screen].allow_lower_floats)
170 XRaiseWindow(globalconf.display, sel->win);
171 else
173 curtags = get_current_tags(screen);
174 if(sel->isfloating ||
175 curtags[0]->layout->arrange == layout_floating)
176 XRaiseWindow(globalconf.display, sel->win);
177 if(!(curtags[0]->layout->arrange == layout_floating))
179 wc.stack_mode = Below;
180 if(!sel->isfloating)
181 XConfigureWindow(globalconf.display, sel->win, CWStackMode, &wc);
182 for(c = globalconf.clients; c; c = c->next)
184 if(!IS_TILED(c, screen) || c == sel)
185 continue;
186 XConfigureWindow(globalconf.display, c->win, CWStackMode, &wc);
189 p_delete(&curtags);
191 if(globalconf.screens[screen].focus_move_pointer)
192 XWarpPointer(globalconf.display, None, sel->win, 0, 0, 0, 0,
193 sel->geometry.width / 2, sel->geometry.height / 2);
194 XSync(globalconf.display, False);
195 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &ev));
198 void
199 saveawesomeprops(int screen)
201 int i, ntags = 0;
202 char *prop;
203 Tag *tag;
205 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
206 ntags++;
208 prop = p_new(char, ntags + 1);
210 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
211 prop[i] = tag->selected ? '1' : '0';
213 prop[i] = '\0';
214 XChangeProperty(globalconf.display,
215 RootWindow(globalconf.display, get_phys_screen(screen)),
216 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
217 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
218 p_delete(&prop);
221 /** Set layout for tag
222 * \param screen Screen ID
223 * \param arg Layout specifier
224 * \ingroup ui_callback
226 void
227 uicb_tag_setlayout(int screen, char *arg)
229 Layout *l = globalconf.screens[screen].layouts;
230 Tag *tag, **curtags;
231 int i;
233 if(arg)
235 curtags = get_current_tags(screen);
236 for(i = 0; l && l != curtags[0]->layout; i++, l = l->next);
237 p_delete(&curtags);
238 if(!l)
239 i = 0;
240 for(i = compute_new_value_from_arg(arg, (double) i),
241 l = globalconf.screens[screen].layouts; l && i > 0; i--)
242 l = l->next;
243 if(!l)
244 l = globalconf.screens[screen].layouts;
247 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
248 if(tag->selected)
249 tag->layout = l;
251 if(globalconf.focus->client)
252 arrange(screen);
253 else
254 widget_invalidate_cache(screen, WIDGET_CACHE_LAYOUTS);
256 saveawesomeprops(screen);
259 /** Toggle floating state of a client
260 * \param screen Screen ID
261 * \param arg unused
262 * \ingroup ui_callback
264 void
265 uicb_client_togglefloating(int screen, char *arg)
267 Client *sel = globalconf.focus->client;
269 if(!sel)
270 return;
272 if((sel->isfloating = !sel->isfloating))
274 if(!arg)
275 client_resize(sel, sel->f_geometry, False);
277 else if(sel->ismax)
278 client_resize(sel, sel->m_geometry, False);
280 widget_invalidate_cache(sel->screen, WIDGET_CACHE_CLIENTS);
281 client_saveprops(sel);
282 arrange(screen);
285 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80