do not try to guess where we should XSync(), XSync after bunch of XEvents
[awesome.git] / screen.c
blobcf5be654d833f3002162ae65682d220f2fd71d2d
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 "client.h"
29 #include "layouts/floating.h"
31 extern AwesomeConf globalconf;
33 /** Get screens info
34 * \param screen Screen number
35 * \param statusbar statusbar
36 * \param padding Padding
37 * \return Area
39 Area
40 get_screen_area(int screen, Statusbar *statusbar, Padding *padding)
42 int screen_number = 0;
43 XineramaScreenInfo *si;
44 Area area;
45 Statusbar *sb;
47 if(XineramaIsActive(globalconf.display))
49 si = XineramaQueryScreens(globalconf.display, &screen_number);
50 if (screen_number < screen)
51 eprint("Info request for unknown screen.");
52 area.x = si[screen].x_org;
53 area.y = si[screen].y_org;
54 area.width = si[screen].width;
55 area.height = si[screen].height;
56 XFree(si);
58 else
60 /* emulate Xinerama info but only fill the screen we want */
61 area.x = 0;
62 area.y = 0;
63 area.width = DisplayWidth(globalconf.display, screen);
64 area.height = DisplayHeight(globalconf.display, screen);
67 /* make padding corrections */
68 if(padding)
70 area.x += padding->left;
71 area.y += padding->top;
72 area.width -= padding->left + padding->right;
73 area.height -= padding->top + padding->bottom;
76 for(sb = statusbar; sb; sb = sb->next)
77 switch(sb->position)
79 case Top:
80 area.y += sb->height;
81 case Bottom:
82 area.height -= sb->height;
83 break;
84 case Left:
85 area.x += sb->height;
86 case Right:
87 area.width -= sb->height;
88 break;
89 case Off:
90 break;
93 return area;
96 /** Get display info
97 * \param screen Screen number
98 * \param statusbar the statusbar
99 * \param padding Padding
100 * \return Area
102 Area
103 get_display_area(int screen, Statusbar *statusbar, Padding *padding)
105 Area area;
106 Statusbar *sb;
108 area.x = 0;
109 area.y = 0;
110 area.width = DisplayWidth(globalconf.display, screen);
111 area.height = DisplayHeight(globalconf.display, screen);
113 for(sb = statusbar; sb; sb = sb->next)
115 area.y += sb->position == Top ? sb->height : 0;
116 area.height -= (sb->position == Top || sb->position == Bottom) ? sb->height : 0;
119 /* make padding corrections */
120 if(padding)
122 area.x += padding->left;
123 area.y += padding->top;
124 area.width -= padding->left + padding->right;
125 area.height -= padding->top + padding->bottom;
127 return area;
130 /** Return the Xinerama screen number where the coordinates belongs to
131 * \param x x coordinate of the window
132 * \param y y coordinate of the window
133 * \return screen number or DefaultScreen of disp on no match
136 get_screen_bycoord(int x, int y)
138 int i;
139 Area area;
141 /* don't waste our time */
142 if(!XineramaIsActive(globalconf.display))
143 return DefaultScreen(globalconf.display);
145 for(i = 0; i < globalconf.nscreens; i++)
147 area = get_screen_area(i, NULL, NULL);
148 if((x < 0 || (x >= area.x && x < area.x + area.width))
149 && (y < 0 || (y >= area.y && y < area.y + area.height)))
150 return i;
152 return DefaultScreen(globalconf.display);
155 /** Return the actual screen count
156 * \return the number of screen available
159 get_screen_count(void)
161 int screen_number;
163 if(XineramaIsActive(globalconf.display))
164 XFree(XineramaQueryScreens(globalconf.display, &screen_number));
165 else
166 screen_number = ScreenCount(globalconf.display);
168 return screen_number;
171 /** This returns the real X screen number for a logical
172 * screen if Xinerama is active.
173 * \param screen the logical screen
174 * \return the X screen
177 get_phys_screen(int screen)
179 if(XineramaIsActive(globalconf.display))
180 return DefaultScreen(globalconf.display);
181 return screen;
184 /** Move a client to a virtual screen
185 * \param c the client
186 * \param new_screen The destinatiuon screen
187 * \param doresize set to True if we also move the client to the new x and
188 * y of the new screen
190 void
191 move_client_to_screen(Client *c, int new_screen, Bool doresize)
193 Tag *tag;
194 int old_screen = c->screen;
195 Area from, to;
197 for(tag = globalconf.screens[old_screen].tags; tag; tag = tag->next)
198 untag_client(c, tag);
200 c->screen = new_screen;
202 /* tag client with new screen tags */
203 tag_client_with_current_selected(c);
205 /* resize the windows if it's floating */
206 if(doresize && old_screen != c->screen)
208 Area new_geometry, new_f_geometry;
209 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 new_geometry = c->geometry;
232 /* compute new coords in new screen */
233 new_geometry.x = (c->geometry.x - from.x) + to.x;
234 new_geometry.y = (c->geometry.y - from.y) + to.y;
236 /* check that new coords are still in the screen */
237 if(new_geometry.width > to.width)
238 new_geometry.width = to.width;
239 if(new_geometry.height > to.height)
240 new_geometry.height = to.height;
241 if(new_geometry.x + new_geometry.width >= to.x + to.width)
242 new_geometry.x = to.x + to.width - new_geometry.width - 2 * c->border;
243 if(new_geometry.y + new_geometry.height >= to.y + to.height)
244 new_geometry.y = to.y + to.height - new_geometry.height - 2 * c->border;
246 /* compute new coords for max in new screen */
247 c->m_geometry.x = (c->m_geometry.x - from.x) + to.x;
248 c->m_geometry.y = (c->m_geometry.y - from.y) + to.y;
250 /* check that new coords are still in the screen */
251 if(c->m_geometry.width > to.width)
252 c->m_geometry.width = to.width;
253 if(c->m_geometry.height > to.height)
254 c->m_geometry.height = to.height;
255 if(c->m_geometry.x + c->m_geometry.width >= to.x + to.width)
256 c->m_geometry.x = to.x + to.width - c->m_geometry.width - 2 * c->border;
257 if(c->m_geometry.y + c->m_geometry.height >= to.y + to.height)
258 c->m_geometry.y = to.y + to.height - c->m_geometry.height - 2 * c->border;
260 client_resize(c, new_geometry, False);
262 /* if floating, move to this new coords */
263 else if(c->isfloating)
264 client_resize(c, new_f_geometry, False);
265 /* otherwise just register them */
266 else
268 c->f_geometry = new_f_geometry;
269 arrange(c->screen);
274 /** Move mouse pointer to x_org and y_xorg of specified screen
275 * \param screen screen number
277 static void
278 move_mouse_pointer_to_screen(int screen)
280 if(XineramaIsActive(globalconf.display))
282 Area area = get_screen_area(screen, NULL, NULL);
283 XWarpPointer(globalconf.display,
284 None,
285 DefaultRootWindow(globalconf.display),
286 0, 0, 0, 0, area.x, area.y);
288 else
289 XWarpPointer(globalconf.display,
290 None,
291 RootWindow(globalconf.display, screen),
292 0, 0, 0, 0, 0, 0);
296 /** Switch focus to a specified screen
297 * \param screen Screen ID
298 * \param arg screen number
299 * \ingroup ui_callback
301 void
302 uicb_screen_focus(int screen, char *arg)
304 int new_screen;
306 if(arg)
307 new_screen = compute_new_value_from_arg(arg, screen);
308 else
309 new_screen = screen + 1;
311 if (new_screen < 0)
312 new_screen = globalconf.nscreens - 1;
313 if (new_screen > (globalconf.nscreens - 1))
314 new_screen = 0;
316 focus(focus_get_current_client(new_screen), True, new_screen);
318 move_mouse_pointer_to_screen(new_screen);
321 /** Move client to a virtual screen (if Xinerama is active)
322 * \param screen Screen ID
323 * \param arg screen number
324 * \ingroup ui_callback
326 void
327 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
329 int new_screen, prev_screen;
330 Client *sel = globalconf.focus->client;
332 if(!sel || !XineramaIsActive(globalconf.display))
333 return;
335 if(arg)
336 new_screen = compute_new_value_from_arg(arg, sel->screen);
337 else
338 new_screen = sel->screen + 1;
340 if(new_screen >= globalconf.nscreens)
341 new_screen = 0;
342 else if(new_screen < 0)
343 new_screen = globalconf.nscreens - 1;
345 prev_screen = sel->screen;
346 move_client_to_screen(sel, new_screen, True);
347 move_mouse_pointer_to_screen(new_screen);
348 arrange(prev_screen);
349 arrange(new_screen);
350 focus(sel, True, sel->screen);
352 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80