rename opts to awesome_opts
[awesome.git] / screen.c
blob4135a88e461694da5f2ad3261b5b22bce5d951b8
1 /*
2 * screen.c - screen management
4 * Copyright © 2007-2008 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 "screen.h"
25 #include "tag.h"
26 #include "focus.h"
27 #include "client.h"
28 #include "layouts/floating.h"
30 extern AwesomeConf globalconf;
32 /** Get screens info
33 * \param screen Screen number
34 * \param statusbar statusbar
35 * \param padding Padding
36 * \return Area
38 Area
39 screen_get_area(int screen, Statusbar *statusbar, Padding *padding)
41 Area area;
42 Statusbar *sb;
44 area = globalconf.screens[screen].geometry;
46 /* make padding corrections */
47 if(padding)
49 area.x += padding->left;
50 area.y += padding->top;
51 area.width -= padding->left + padding->right;
52 area.height -= padding->top + padding->bottom;
55 for(sb = statusbar; sb; sb = sb->next)
56 switch(sb->position)
58 case Top:
59 area.y += sb->height;
60 case Bottom:
61 area.height -= sb->height;
62 break;
63 case Left:
64 area.x += sb->height;
65 case Right:
66 area.width -= sb->height;
67 break;
68 case Off:
69 break;
72 return area;
75 /** Get display info
76 * \param screen Screen number
77 * \param statusbar the statusbar
78 * \param padding Padding
79 * \return Area
81 Area
82 get_display_area(int screen, Statusbar *statusbar, Padding *padding)
84 Area area = { 0, 0, 0, 0, NULL };
85 Statusbar *sb;
87 area.width = DisplayWidth(globalconf.display, screen);
88 area.height = DisplayHeight(globalconf.display, screen);
90 for(sb = statusbar; sb; sb = sb->next)
92 area.y += sb->position == Top ? sb->height : 0;
93 area.height -= (sb->position == Top || sb->position == Bottom) ? sb->height : 0;
96 /* make padding corrections */
97 if(padding)
99 area.x += padding->left;
100 area.y += padding->top;
101 area.width -= padding->left + padding->right;
102 area.height -= padding->top + padding->bottom;
104 return area;
107 /** Return the Xinerama screen number where the coordinates belongs to
108 * \param x x coordinate of the window
109 * \param y y coordinate of the window
110 * \return screen number or DefaultScreen of disp on no match
113 screen_get_bycoord(int x, int y)
115 int i;
116 Area area;
118 /* don't waste our time */
119 if(!XineramaIsActive(globalconf.display))
120 return DefaultScreen(globalconf.display);
122 for(i = 0; i < globalconf.nscreen; i++)
124 area = screen_get_area(i, NULL, NULL);
125 if((x < 0 || (x >= area.x && x < area.x + area.width))
126 && (y < 0 || (y >= area.y && y < area.y + area.height)))
127 return i;
129 return DefaultScreen(globalconf.display);
133 static inline Area
134 screen_xsi_to_area(XineramaScreenInfo si)
136 Area a;
138 a.x = si.x_org;
139 a.y = si.y_org;
140 a.width = si.width;
141 a.height = si.height;
142 a.next = NULL;
144 return a;
147 void
148 screen_build_screens(void)
150 XineramaScreenInfo *si;
151 int xinerama_screen_number, screen, screen_to_test;
152 Bool drop;
154 if(XineramaIsActive(globalconf.display))
156 si = XineramaQueryScreens(globalconf.display, &xinerama_screen_number);
157 globalconf.screens = p_new(VirtScreen, xinerama_screen_number);
158 globalconf.nscreen = 0;
160 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
161 for(screen = 0; screen < xinerama_screen_number; screen++)
163 drop = False;
164 for(screen_to_test = 0; screen_to_test < globalconf.nscreen; screen_to_test++)
165 if(si[screen].x_org == globalconf.screens[screen_to_test].geometry.x
166 && si[screen].y_org == globalconf.screens[screen_to_test].geometry.y)
168 /* we already have a screen for this area, just check if
169 * it's not bigger and drop it */
170 drop = True;
171 globalconf.screens[screen_to_test].geometry.width =
172 MAX(si[screen].width, si[screen_to_test].width);
173 globalconf.screens[screen_to_test].geometry.height =
174 MAX(si[screen].height, si[screen_to_test].height);
176 if(!drop)
177 globalconf.screens[globalconf.nscreen++].geometry = screen_xsi_to_area(si[screen]);
180 /* realloc smaller if xinerama_screen_number != screen registered */
181 if(xinerama_screen_number != globalconf.nscreen)
183 VirtScreen *newscreens = p_new(VirtScreen, globalconf.nscreen);
184 memcpy(newscreens, globalconf.screens, globalconf.nscreen * sizeof(VirtScreen));
185 p_delete(&globalconf.screens);
186 globalconf.screens = newscreens;
189 XFree(si);
191 else
193 globalconf.nscreen = ScreenCount(globalconf.display);
194 globalconf.screens = p_new(VirtScreen, globalconf.nscreen);
195 for(screen = 0; screen < globalconf.nscreen; screen++)
197 globalconf.screens[screen].geometry.x = 0;
198 globalconf.screens[screen].geometry.y = 0;
199 globalconf.screens[screen].geometry.width =
200 DisplayWidth(globalconf.display, screen);
201 globalconf.screens[screen].geometry.height =
202 DisplayHeight(globalconf.display, screen);
208 /** This returns the real X screen number for a logical
209 * screen if Xinerama is active.
210 * \param screen the logical screen
211 * \return the X screen
214 get_phys_screen(int screen)
216 if(XineramaIsActive(globalconf.display))
217 return DefaultScreen(globalconf.display);
218 return screen;
221 /** Move a client to a virtual screen
222 * \param c the client
223 * \param new_screen The destinatiuon screen
224 * \param doresize set to True if we also move the client to the new x and
225 * y of the new screen
227 void
228 move_client_to_screen(Client *c, int new_screen, Bool doresize)
230 Tag *tag;
231 int old_screen = c->screen;
232 Area from, to;
234 for(tag = globalconf.screens[old_screen].tags; tag; tag = tag->next)
235 untag_client(c, tag);
237 c->screen = new_screen;
239 /* tag client with new screen tags */
240 tag_client_with_current_selected(c);
242 /* resize the windows if it's floating */
243 if(doresize && old_screen != c->screen)
245 Area new_geometry, new_f_geometry;
246 new_f_geometry = c->f_geometry;
248 to = screen_get_area(c->screen, NULL, NULL);
249 from = screen_get_area(old_screen, NULL, NULL);
251 /* compute new coords in new screen */
252 new_f_geometry.x = (c->f_geometry.x - from.x) + to.x;
253 new_f_geometry.y = (c->f_geometry.y - from.y) + to.y;
255 /* check that new coords are still in the screen */
256 if(new_f_geometry.width > to.width)
257 new_f_geometry.width = to.width;
258 if(new_f_geometry.height > to.height)
259 new_f_geometry.height = to.height;
260 if(new_f_geometry.x + new_f_geometry.width >= to.x + to.width)
261 new_f_geometry.x = to.x + to.width - new_f_geometry.width - 2 * c->border;
262 if(new_f_geometry.y + new_f_geometry.height >= to.y + to.height)
263 new_f_geometry.y = to.y + to.height - new_f_geometry.height - 2 * c->border;
265 if(c->ismax)
267 new_geometry = c->geometry;
269 /* compute new coords in new screen */
270 new_geometry.x = (c->geometry.x - from.x) + to.x;
271 new_geometry.y = (c->geometry.y - from.y) + to.y;
273 /* check that new coords are still in the screen */
274 if(new_geometry.width > to.width)
275 new_geometry.width = to.width;
276 if(new_geometry.height > to.height)
277 new_geometry.height = to.height;
278 if(new_geometry.x + new_geometry.width >= to.x + to.width)
279 new_geometry.x = to.x + to.width - new_geometry.width - 2 * c->border;
280 if(new_geometry.y + new_geometry.height >= to.y + to.height)
281 new_geometry.y = to.y + to.height - new_geometry.height - 2 * c->border;
283 /* compute new coords for max in new screen */
284 c->m_geometry.x = (c->m_geometry.x - from.x) + to.x;
285 c->m_geometry.y = (c->m_geometry.y - from.y) + to.y;
287 /* check that new coords are still in the screen */
288 if(c->m_geometry.width > to.width)
289 c->m_geometry.width = to.width;
290 if(c->m_geometry.height > to.height)
291 c->m_geometry.height = to.height;
292 if(c->m_geometry.x + c->m_geometry.width >= to.x + to.width)
293 c->m_geometry.x = to.x + to.width - c->m_geometry.width - 2 * c->border;
294 if(c->m_geometry.y + c->m_geometry.height >= to.y + to.height)
295 c->m_geometry.y = to.y + to.height - c->m_geometry.height - 2 * c->border;
297 client_resize(c, new_geometry, False);
299 /* if floating, move to this new coords */
300 else if(c->isfloating)
301 client_resize(c, new_f_geometry, False);
302 /* otherwise just register them */
303 else
305 c->f_geometry = new_f_geometry;
306 globalconf.screens[old_screen].need_arrange = True;
307 globalconf.screens[c->screen].need_arrange = True;
312 /** Move mouse pointer to x_org and y_xorg of specified screen
313 * \param screen screen number
315 static void
316 move_mouse_pointer_to_screen(int screen)
318 if(XineramaIsActive(globalconf.display))
320 Area area = screen_get_area(screen, NULL, NULL);
321 XWarpPointer(globalconf.display,
322 None,
323 DefaultRootWindow(globalconf.display),
324 0, 0, 0, 0, area.x, area.y);
326 else
327 XWarpPointer(globalconf.display,
328 None,
329 RootWindow(globalconf.display, screen),
330 0, 0, 0, 0, 0, 0);
334 /** Switch focus to a specified screen
335 * \param screen Screen ID
336 * \param arg screen number
337 * \ingroup ui_callback
339 void
340 uicb_screen_focus(int screen, char *arg)
342 int new_screen;
344 if(arg)
345 new_screen = compute_new_value_from_arg(arg, screen);
346 else
347 new_screen = screen + 1;
349 if (new_screen < 0)
350 new_screen = globalconf.nscreen - 1;
351 if (new_screen > (globalconf.nscreen - 1))
352 new_screen = 0;
354 client_focus(NULL, new_screen, False);
356 move_mouse_pointer_to_screen(new_screen);
359 /** Move client to a virtual screen (if Xinerama is active)
360 * \param screen Screen ID
361 * \param arg screen number
362 * \ingroup ui_callback
364 void
365 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
367 int new_screen, prev_screen;
368 Client *sel = globalconf.focus->client;
370 if(!sel || !XineramaIsActive(globalconf.display))
371 return;
373 if(arg)
374 new_screen = compute_new_value_from_arg(arg, sel->screen);
375 else
376 new_screen = sel->screen + 1;
378 if(new_screen >= globalconf.nscreen)
379 new_screen = 0;
380 else if(new_screen < 0)
381 new_screen = globalconf.nscreen - 1;
383 prev_screen = sel->screen;
384 move_client_to_screen(sel, new_screen, True);
385 move_mouse_pointer_to_screen(new_screen);
386 client_focus(sel, sel->screen, False);
388 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80