use Area to store window geoms
[awesome.git] / screen.c
blob5dce2e7f9d340d155bb982c84e6ca1214d96d393
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"
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);
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 if(doresize && old_screen != c->screen)
208 to = get_screen_area(c->screen, NULL, NULL);
209 from = get_screen_area(old_screen, NULL, NULL);
211 /* compute new coords in new screen */
212 c->f_geometry.x = (c->f_geometry.x - from.x) + to.x;
213 c->f_geometry.y = (c->f_geometry.y - from.y) + to.y;
214 /* check that new coords are still in the screen */
215 if(c->f_geometry.width > to.width)
216 c->f_geometry.width = to.width;
217 if(c->f_geometry.height > to.height)
218 c->f_geometry.height = to.height;
219 if(c->f_geometry.x + c->f_geometry.width >= to.x + to.width)
220 c->f_geometry.x = to.x + to.width - c->f_geometry.width - 2 * c->border;
221 if(c->f_geometry.y + c->f_geometry.height >= to.y + to.height)
222 c->f_geometry.y = to.y + to.height - c->f_geometry.height - 2 * c->border;
224 client_resize(c, c->f_geometry.x, c->f_geometry.y,
225 c->f_geometry.width, c->f_geometry.height, True, False);
228 focus(c, True, c->screen);
230 /* redraw statusbar on all screens */
231 statusbar_draw_all(old_screen);
232 statusbar_draw_all(new_screen);
235 /** Move mouse pointer to x_org and y_xorg of specified screen
236 * \param screen screen number
238 static void
239 move_mouse_pointer_to_screen(int screen)
241 if(XineramaIsActive(globalconf.display))
243 Area area = get_screen_area(screen, NULL, NULL);
244 XWarpPointer(globalconf.display,
245 None,
246 DefaultRootWindow(globalconf.display),
247 0, 0, 0, 0, area.x, area.y);
249 else
250 XWarpPointer(globalconf.display,
251 None,
252 RootWindow(globalconf.display, screen),
253 0, 0, 0, 0, 0, 0);
257 /** Switch focus to a specified screen
258 * \param screen Screen ID
259 * \param arg screen number
260 * \ingroup ui_callback
262 void
263 uicb_screen_focus(int screen, char *arg)
265 int new_screen, numscreens = get_screen_count();
267 if(arg)
268 new_screen = compute_new_value_from_arg(arg, screen);
269 else
270 new_screen = screen + 1;
272 if (new_screen < 0)
273 new_screen = numscreens - 1;
274 if (new_screen > (numscreens - 1))
275 new_screen = 0;
277 focus(focus_get_current_client(new_screen), True, new_screen);
279 move_mouse_pointer_to_screen(new_screen);
282 /** Move client to a virtual screen (if Xinerama is active)
283 * \param screen Screen ID
284 * \param arg screen number
285 * \ingroup ui_callback
287 void
288 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
290 int new_screen, prev_screen;
291 Client *sel = globalconf.focus->client;
293 if(!sel || !XineramaIsActive(globalconf.display))
294 return;
296 if(arg)
297 new_screen = compute_new_value_from_arg(arg, sel->screen);
298 else
299 new_screen = sel->screen + 1;
301 if(new_screen >= get_screen_count())
302 new_screen = 0;
303 else if(new_screen < 0)
304 new_screen = get_screen_count() - 1;
306 prev_screen = sel->screen;
307 move_client_to_screen(sel, new_screen, True);
308 move_mouse_pointer_to_screen(new_screen);
309 arrange(prev_screen);
310 arrange(new_screen);
312 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80