Add check for --help and --version support
[awesome.git] / layout.c
blob848919d9648d9f7605ec6bde23f702dc6e5250a8
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) && !c->newcomer)
67 client_unban(c);
68 /* we don't touch other screens windows */
69 else if(c->screen == screen || c->newcomer)
70 client_ban(c);
73 curtags[0]->layout->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);
80 c = focus_get_current_client(screen);
81 focus(c, True, screen);
82 if(c && XQueryPointer(globalconf.display, RootWindow(globalconf.display, get_phys_screen(screen)),
83 &root_win, &client_win, &x, &y, &d, &d, &m) &&
84 (root_win == None || client_win == None || client_win == root_win))
85 window_grabbuttons(c->screen, c->win, False, False);
87 p_delete(&curtags);
88 restack(screen);
92 Layout *
93 get_current_layout(int screen)
95 Tag **curtags = get_current_tags(screen);
96 Layout *l = curtags[0]->layout;
97 p_delete(&curtags);
98 return l;
101 /** Send focus to next client in stack
102 * \param screen Screen ID
103 * \param arg Unused
104 * \ingroup ui_callback
106 void
107 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
109 Client *c, *sel = globalconf.focus->client;
111 if(!sel)
112 return;
113 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
114 if(!c)
115 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
116 if(c)
118 focus(c, True, screen);
119 restack(screen);
123 /** Send focus to previous client in stack
124 * \param screen Screen ID
125 * \param arg Unused
126 * \ingroup ui_callback
128 void
129 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
131 Client *c, *sel = globalconf.focus->client;
133 if(!sel)
134 return;
135 for(c = sel->prev; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
136 if(!c)
138 for(c = globalconf.clients; c && c->next; c = c->next);
139 for(; c && (c->skip || !client_isvisible(c, screen)); c = c->prev);
141 if(c)
143 focus(c, True, screen);
144 restack(screen);
148 void
149 loadawesomeprops(int screen)
151 int i, ntags = 0;
152 char *prop;
153 Tag *tag;
155 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
156 ntags++;
158 prop = p_new(char, ntags + 1);
160 if(xgettextprop(RootWindow(globalconf.display, get_phys_screen(screen)),
161 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
162 prop, ntags + 1))
163 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
164 if(prop[i] == '1')
165 tag->selected = True;
166 else
167 tag->selected = False;
169 p_delete(&prop);
171 ewmh_update_net_current_desktop(get_phys_screen(screen));
174 void
175 restack(int screen)
177 Client *c, *sel = globalconf.focus->client;
178 XWindowChanges wc;
179 Tag **curtags;
181 if(!sel)
182 return;
184 if(globalconf.screens[screen].allow_lower_floats)
185 XRaiseWindow(globalconf.display, sel->win);
186 else
188 curtags = get_current_tags(screen);
189 if(sel->isfloating ||
190 curtags[0]->layout->arrange == layout_floating)
191 XRaiseWindow(globalconf.display, sel->win);
192 if(!(curtags[0]->layout->arrange == layout_floating))
194 wc.stack_mode = Below;
195 if(!sel->isfloating)
196 XConfigureWindow(globalconf.display, sel->win, CWStackMode, &wc);
197 for(c = globalconf.clients; c; c = c->next)
199 if(!IS_TILED(c, screen) || c == sel)
200 continue;
201 XConfigureWindow(globalconf.display, c->win, CWStackMode, &wc);
204 p_delete(&curtags);
206 if(globalconf.screens[screen].focus_move_pointer)
207 XWarpPointer(globalconf.display, None, sel->win, 0, 0, 0, 0,
208 sel->geometry.width / 2, sel->geometry.height / 2);
211 void
212 saveawesomeprops(int screen)
214 int i, ntags = 0;
215 char *prop;
216 Tag *tag;
218 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
219 ntags++;
221 prop = p_new(char, ntags + 1);
223 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
224 prop[i] = tag->selected ? '1' : '0';
226 prop[i] = '\0';
227 XChangeProperty(globalconf.display,
228 RootWindow(globalconf.display, get_phys_screen(screen)),
229 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
230 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
231 p_delete(&prop);
234 /** Set layout for tag
235 * \param screen Screen ID
236 * \param arg Layout specifier
237 * \ingroup ui_callback
239 void
240 uicb_tag_setlayout(int screen, char *arg)
242 Layout *l = globalconf.screens[screen].layouts;
243 Tag *tag, **curtags;
244 int i;
246 if(arg)
248 curtags = get_current_tags(screen);
249 for(i = 0; l && l != curtags[0]->layout; i++, l = l->next);
250 p_delete(&curtags);
252 if(!l)
253 i = 0;
255 i = compute_new_value_from_arg(arg, (double) i);
257 if(i >= 0)
258 for(l = globalconf.screens[screen].layouts; l && i > 0; i--)
259 l = l->next;
260 else
261 for(l = globalconf.screens[screen].layouts; l && i < 0; i++)
262 l = l->prev;
264 if(!l)
265 l = globalconf.screens[screen].layouts;
268 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
269 if(tag->selected)
270 tag->layout = l;
272 if(globalconf.focus->client)
273 arrange(screen);
275 widget_invalidate_cache(screen, WIDGET_CACHE_LAYOUTS);
277 saveawesomeprops(screen);
280 /** Toggle floating state of a client
281 * \param screen Screen ID
282 * \param arg unused
283 * \ingroup ui_callback
285 void
286 uicb_client_togglefloating(int screen, char *arg)
288 Client *sel = globalconf.focus->client;
290 if(!sel)
291 return;
293 if((sel->isfloating = !sel->isfloating))
295 if(!arg)
296 client_resize(sel, sel->f_geometry, False);
298 else if(sel->ismax)
299 client_resize(sel, sel->m_geometry, False);
301 widget_invalidate_cache(sel->screen, WIDGET_CACHE_CLIENTS);
302 client_saveprops(sel);
303 arrange(screen);
306 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80