fix wrong coords computing with left/right statusbar click
[awesome.git] / layout.c
blob4be1d411e93e1a428a5492a51701e210c083fd06
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 /** Arrange windows following current selected layout
42 * \param screen the screen to arrange
44 static void
45 arrange(int screen)
47 Client *c;
48 Layout *curlay = get_current_layout(screen);
49 unsigned int dui;
50 int di, x, y;
51 Window rootwin, childwin;
53 for(c = globalconf.clients; c; c = c->next)
55 if(client_isvisible(c, screen) && !c->newcomer)
56 client_unban(c);
57 /* we don't touch other screens windows */
58 else if(c->screen == screen || c->newcomer)
59 client_ban(c);
62 curlay->arrange(screen);
64 for(c = globalconf.clients; c; c = c->next)
65 if(c->newcomer && client_isvisible(c, screen))
67 c->newcomer = False;
68 client_unban(c);
69 if(globalconf.screens[screen].new_get_focus)
70 client_focus(c, screen, False);
73 /* if we have a valid client that could be focused but currently no window
74 * are focused, then set the focus on this window */
75 if((c = focus_get_current_client(screen)) && !globalconf.focus->client)
76 client_focus(c, screen, False);
78 /* check that the mouse is on a window or not */
79 if(XQueryPointer(globalconf.display, RootWindow(globalconf.display,
80 get_phys_screen(screen)),
81 &rootwin, &childwin, &x, &y, &di, &di, &dui)
82 && (rootwin == None || childwin == None || childwin == rootwin))
83 window_root_grabbuttons(screen);
85 /* reset status */
86 globalconf.screens[screen].need_arrange = False;
89 int
90 layout_refresh(void)
92 int screen;
93 int arranged = 0;
95 for(screen = 0; screen < globalconf.nscreen; screen++)
96 if(globalconf.screens[screen].need_arrange)
98 arrange(screen);
99 arranged++;
102 return arranged;
105 Layout *
106 get_current_layout(int screen)
108 Tag **curtags = tags_get_current(screen);
109 Layout *l = curtags[0]->layout;
110 p_delete(&curtags);
111 return l;
114 void
115 loadawesomeprops(int screen)
117 int i, ntags = 0;
118 char *prop;
119 Tag *tag;
121 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
122 ntags++;
124 prop = p_new(char, ntags + 1);
126 if(xgettextprop(RootWindow(globalconf.display, get_phys_screen(screen)),
127 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
128 prop, ntags + 1))
129 for(i = 0, tag = globalconf.screens[screen].tags; tag && prop[i]; i++, tag = tag->next)
130 tag_view_byindex(screen, i, prop[i] == '1');
132 p_delete(&prop);
135 void
136 saveawesomeprops(int screen)
138 int i, ntags = 0;
139 char *prop;
140 Tag *tag;
142 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
143 ntags++;
145 prop = p_new(char, ntags + 1);
147 for(i = 0, tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
148 prop[i] = tag->selected ? '1' : '0';
150 prop[i] = '\0';
151 XChangeProperty(globalconf.display,
152 RootWindow(globalconf.display, get_phys_screen(screen)),
153 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
154 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
155 p_delete(&prop);
158 /** Set layout for tag
159 * \param screen Screen ID
160 * \param arg Layout specifier
161 * \ingroup ui_callback
163 void
164 uicb_tag_setlayout(int screen, char *arg)
166 Layout *l = globalconf.screens[screen].layouts;
167 Tag *tag, **curtags;
168 int i;
170 if(arg)
172 curtags = tags_get_current(screen);
173 for(i = 0; l && l != curtags[0]->layout; i++, l = l->next);
174 p_delete(&curtags);
176 if(!l)
177 i = 0;
179 i = compute_new_value_from_arg(arg, (double) i);
181 if(i >= 0)
182 for(l = globalconf.screens[screen].layouts; l && i > 0; i--)
183 l = l->next;
184 else
185 for(l = globalconf.screens[screen].layouts; l && i < 0; i++)
186 l = layout_list_prev_cycle(&globalconf.screens[screen].layouts, l);
188 if(!l)
189 l = globalconf.screens[screen].layouts;
192 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
193 if(tag->selected)
194 tag->layout = l;
196 if(globalconf.focus->client)
197 arrange(screen);
199 widget_invalidate_cache(screen, WIDGET_CACHE_LAYOUTS);
201 saveawesomeprops(screen);
204 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80