fix rearrange
[awesome.git] / screen.c
blob1a9f2cab0b405e481389e3cd2445935088498a5e
1 /*
2 * screen.c - screen 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/extensions/Xinerama.h>
24 #include "util.h"
25 #include "screen.h"
26 #include "tag.h"
27 #include "focus.h"
28 #include "statusbar.h"
29 #include "client.h"
30 #include "layouts/floating.h"
32 extern AwesomeConf globalconf;
34 /** Get screens info
35 * \param screen Screen number
36 * \param statusbar statusbar
37 * \param padding Padding
38 * \return Area
40 Area
41 get_screen_area(int screen, Statusbar *statusbar, Padding *padding)
43 int screen_number = 0;
44 XineramaScreenInfo *si;
45 Area area;
46 Statusbar *sb;
48 if(XineramaIsActive(globalconf.display))
50 si = XineramaQueryScreens(globalconf.display, &screen_number);
51 if (screen_number < screen)
52 eprint("Info request for unknown screen.");
53 area.x = si[screen].x_org;
54 area.y = si[screen].y_org;
55 area.width = si[screen].width;
56 area.height = si[screen].height;
57 XFree(si);
59 else
61 /* emulate Xinerama info but only fill the screen we want */
62 area.x = 0;
63 area.y = 0;
64 area.width = DisplayWidth(globalconf.display, screen);
65 area.height = DisplayHeight(globalconf.display, screen);
68 /* make padding corrections */
69 if(padding)
71 area.x += padding->left;
72 area.y += padding->top;
73 area.width -= padding->left + padding->right;
74 area.height -= padding->top + padding->bottom;
77 for(sb = statusbar; sb; sb = sb->next)
78 switch(sb->position)
80 case Top:
81 area.y += sb->height;
82 case Bottom:
83 area.height -= sb->height;
84 break;
85 case Left:
86 area.x += sb->height;
87 case Right:
88 area.width -= sb->height;
89 break;
90 case Off:
91 break;
94 return area;
97 /** Get display info
98 * \param screen Screen number
99 * \param statusbar the statusbar
100 * \param padding Padding
101 * \return Area
103 Area
104 get_display_area(int screen, Statusbar *statusbar, Padding *padding)
106 Area area;
107 Statusbar *sb;
109 area.x = 0;
110 area.y = 0;
111 area.width = DisplayWidth(globalconf.display, screen);
112 area.height = DisplayHeight(globalconf.display, screen);
114 for(sb = statusbar; sb; sb = sb->next)
116 area.y += sb->position == Top ? sb->height : 0;
117 area.height -= (sb->position == Top || sb->position == Bottom) ? sb->height : 0;
120 /* make padding corrections */
121 if(padding)
123 area.x += padding->left;
124 area.y += padding->top;
125 area.width -= padding->left + padding->right;
126 area.height -= padding->top + padding->bottom;
128 return area;
131 /** Return the Xinerama screen number where the coordinates belongs to
132 * \param x x coordinate of the window
133 * \param y y coordinate of the window
134 * \return screen number or DefaultScreen of disp on no match
137 get_screen_bycoord(int x, int y)
139 int i;
140 Area area;
142 /* don't waste our time */
143 if(!XineramaIsActive(globalconf.display))
144 return DefaultScreen(globalconf.display);
146 for(i = 0; i < get_screen_count(); i++)
148 area = get_screen_area(i, NULL, NULL);
149 if((x < 0 || (x >= area.x && x < area.x + area.width))
150 && (y < 0 || (y >= area.y && y < area.y + area.height)))
151 return i;
153 return DefaultScreen(globalconf.display);
156 /** Return the actual screen count
157 * \return the number of screen available
160 get_screen_count(void)
162 int screen_number;
164 if(XineramaIsActive(globalconf.display))
165 XineramaQueryScreens(globalconf.display, &screen_number);
166 else
167 return ScreenCount(globalconf.display);
169 return screen_number;
172 /** This returns the real X screen number for a logical
173 * screen if Xinerama is active.
174 * \param screen the logical screen
175 * \return the X screen
178 get_phys_screen(int screen)
180 if(XineramaIsActive(globalconf.display))
181 return DefaultScreen(globalconf.display);
182 return screen;
185 /** Move a client to a virtual screen
186 * \param c the client
187 * \param new_screen The destinatiuon screen
188 * \param doresize set to True if we also move the client to the new x and
189 * y of the new screen
191 void
192 move_client_to_screen(Client *c, int new_screen, Bool doresize)
194 Tag *tag;
195 int old_screen = c->screen;
196 Area from, to;
198 for(tag = globalconf.screens[old_screen].tags; tag; tag = tag->next)
199 untag_client(c, tag);
201 c->screen = new_screen;
203 /* tag client with new screen tags */
204 tag_client_with_current_selected(c);
206 /* resize the windows if it's floating */
207 if(doresize && old_screen != c->screen)
209 Area new_f_geometry = c->f_geometry;
211 to = get_screen_area(c->screen, NULL, NULL);
212 from = get_screen_area(old_screen, NULL, NULL);
214 /* compute new coords in new screen */
215 new_f_geometry.x = (c->f_geometry.x - from.x) + to.x;
216 new_f_geometry.y = (c->f_geometry.y - from.y) + to.y;
218 /* check that new coords are still in the screen */
219 if(new_f_geometry.width > to.width)
220 new_f_geometry.width = to.width;
221 if(new_f_geometry.height > to.height)
222 new_f_geometry.height = to.height;
223 if(new_f_geometry.x + new_f_geometry.width >= to.x + to.width)
224 new_f_geometry.x = to.x + to.width - new_f_geometry.width - 2 * c->border;
225 if(new_f_geometry.y + new_f_geometry.height >= to.y + to.height)
226 new_f_geometry.y = to.y + to.height - new_f_geometry.height - 2 * c->border;
228 if(c->ismax)
230 /* compute new coords for max in new screen */
231 c->m_geometry.x = (c->m_geometry.x - from.x) + to.x;
232 c->m_geometry.y = (c->m_geometry.y - from.y) + to.y;
234 /* check that new coords are still in the screen */
235 if(c->m_geometry.width > to.width)
236 c->m_geometry.width = to.width;
237 if(c->m_geometry.height > to.height)
238 c->m_geometry.height = to.height;
239 if(c->m_geometry.x + c->m_geometry.width >= to.x + to.width)
240 c->m_geometry.x = to.x + to.width - c->m_geometry.width - 2 * c->border;
241 if(c->m_geometry.y + c->m_geometry.height >= to.y + to.height)
242 c->m_geometry.y = to.y + to.height - c->m_geometry.height - 2 * c->border;
245 /* if floating, move to this new coords */
246 if(c->isfloating)
247 client_resize(c, new_f_geometry, False);
248 /* otherwise just register them */
249 else
251 c->f_geometry = new_f_geometry;
252 arrange(c->screen);
256 focus(c, True, c->screen);
258 /* redraw statusbar on all screens */
259 statusbar_draw_all(old_screen);
260 statusbar_draw_all(new_screen);
263 /** Move mouse pointer to x_org and y_xorg of specified screen
264 * \param screen screen number
266 static void
267 move_mouse_pointer_to_screen(int screen)
269 if(XineramaIsActive(globalconf.display))
271 Area area = get_screen_area(screen, NULL, NULL);
272 XWarpPointer(globalconf.display,
273 None,
274 DefaultRootWindow(globalconf.display),
275 0, 0, 0, 0, area.x, area.y);
277 else
278 XWarpPointer(globalconf.display,
279 None,
280 RootWindow(globalconf.display, screen),
281 0, 0, 0, 0, 0, 0);
285 /** Switch focus to a specified screen
286 * \param screen Screen ID
287 * \param arg screen number
288 * \ingroup ui_callback
290 void
291 uicb_screen_focus(int screen, char *arg)
293 int new_screen, numscreens = get_screen_count();
295 if(arg)
296 new_screen = compute_new_value_from_arg(arg, screen);
297 else
298 new_screen = screen + 1;
300 if (new_screen < 0)
301 new_screen = numscreens - 1;
302 if (new_screen > (numscreens - 1))
303 new_screen = 0;
305 focus(focus_get_current_client(new_screen), True, new_screen);
307 move_mouse_pointer_to_screen(new_screen);
310 /** Move client to a virtual screen (if Xinerama is active)
311 * \param screen Screen ID
312 * \param arg screen number
313 * \ingroup ui_callback
315 void
316 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
318 int new_screen, prev_screen;
319 Client *sel = globalconf.focus->client;
321 if(!sel || !XineramaIsActive(globalconf.display))
322 return;
324 if(arg)
325 new_screen = compute_new_value_from_arg(arg, sel->screen);
326 else
327 new_screen = sel->screen + 1;
329 if(new_screen >= get_screen_count())
330 new_screen = 0;
331 else if(new_screen < 0)
332 new_screen = get_screen_count() - 1;
334 prev_screen = sel->screen;
335 move_client_to_screen(sel, new_screen, True);
336 move_mouse_pointer_to_screen(new_screen);
337 arrange(prev_screen);
338 arrange(new_screen);
340 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80