rename initxcolor to draw_color_new() and move it to draw.c
[awesome.git] / screen.c
blobfc61056a609d329a734c2b74b8f72cf4ac793c31
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 "screen.h"
25 #include "tag.h"
26 #include "focus.h"
27 #include "client.h"
28 #include "layouts/floating.h"
29 #include "common/util.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 Area area;
43 Statusbar *sb;
45 area = globalconf.screens[screen].geometry;
47 /* make padding corrections */
48 if(padding)
50 area.x += padding->left;
51 area.y += padding->top;
52 area.width -= padding->left + padding->right;
53 area.height -= padding->top + padding->bottom;
56 for(sb = statusbar; sb; sb = sb->next)
57 switch(sb->position)
59 case Top:
60 area.y += sb->height;
61 case Bottom:
62 area.height -= sb->height;
63 break;
64 case Left:
65 area.x += sb->height;
66 case Right:
67 area.width -= sb->height;
68 break;
69 case Off:
70 break;
73 return area;
76 /** Get display info
77 * \param screen Screen number
78 * \param statusbar the statusbar
79 * \param padding Padding
80 * \return Area
82 Area
83 get_display_area(int screen, Statusbar *statusbar, Padding *padding)
85 Area area;
86 Statusbar *sb;
88 area.x = 0;
89 area.y = 0;
90 area.width = DisplayWidth(globalconf.display, screen);
91 area.height = DisplayHeight(globalconf.display, screen);
93 for(sb = statusbar; sb; sb = sb->next)
95 area.y += sb->position == Top ? sb->height : 0;
96 area.height -= (sb->position == Top || sb->position == Bottom) ? sb->height : 0;
99 /* make padding corrections */
100 if(padding)
102 area.x += padding->left;
103 area.y += padding->top;
104 area.width -= padding->left + padding->right;
105 area.height -= padding->top + padding->bottom;
107 return area;
110 /** Return the Xinerama screen number where the coordinates belongs to
111 * \param x x coordinate of the window
112 * \param y y coordinate of the window
113 * \return screen number or DefaultScreen of disp on no match
116 get_screen_bycoord(int x, int y)
118 int i;
119 Area area;
121 /* don't waste our time */
122 if(!XineramaIsActive(globalconf.display))
123 return DefaultScreen(globalconf.display);
125 for(i = 0; i < globalconf.nscreen; i++)
127 area = get_screen_area(i, NULL, NULL);
128 if((x < 0 || (x >= area.x && x < area.x + area.width))
129 && (y < 0 || (y >= area.y && y < area.y + area.height)))
130 return i;
132 return DefaultScreen(globalconf.display);
136 static inline Area
137 screen_xsi_to_area(XineramaScreenInfo si)
139 Area a;
141 a.x = si.x_org;
142 a.y = si.y_org;
143 a.width = si.width;
144 a.height = si.height;
146 return a;
149 void
150 screen_build_screens(void)
152 XineramaScreenInfo *si;
153 int xinerama_screen_number, screen, screen_to_test;
154 Bool drop;
156 if(XineramaIsActive(globalconf.display))
158 si = XineramaQueryScreens(globalconf.display, &xinerama_screen_number);
159 globalconf.screens = p_new(VirtScreen, xinerama_screen_number);
160 globalconf.nscreen = 0;
162 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
163 for(screen = 0; screen < xinerama_screen_number; screen++)
165 drop = False;
166 for(screen_to_test = 0; screen_to_test < globalconf.nscreen; screen_to_test++)
167 if(si[screen].x_org == globalconf.screens[screen_to_test].geometry.x
168 && si[screen].y_org == globalconf.screens[screen_to_test].geometry.y)
170 /* we already have a screen for this area, just check if
171 * it's not bigger and drop it */
172 drop = True;
173 globalconf.screens[screen_to_test].geometry.width =
174 MAX(si[screen].width, si[screen_to_test].width);
175 globalconf.screens[screen_to_test].geometry.height =
176 MAX(si[screen].height, si[screen_to_test].height);
178 if(!drop)
179 globalconf.screens[globalconf.nscreen++].geometry = screen_xsi_to_area(si[screen]);
182 /* realloc smaller if xinerama_screen_number != screen registered */
183 if(xinerama_screen_number != globalconf.nscreen)
185 VirtScreen *newscreens = p_new(VirtScreen, globalconf.nscreen);
186 memcpy(newscreens, globalconf.screens, globalconf.nscreen * sizeof(VirtScreen));
187 p_delete(&globalconf.screens);
188 globalconf.screens = newscreens;
191 XFree(si);
193 else
195 globalconf.nscreen = ScreenCount(globalconf.display);
196 globalconf.screens = p_new(VirtScreen, globalconf.nscreen);
197 for(screen = 0; screen < globalconf.nscreen; screen++)
199 globalconf.screens[screen].geometry.x = 0;
200 globalconf.screens[screen].geometry.y = 0;
201 globalconf.screens[screen].geometry.width =
202 DisplayWidth(globalconf.display, screen);
203 globalconf.screens[screen].geometry.height =
204 DisplayHeight(globalconf.display, screen);
210 /** This returns the real X screen number for a logical
211 * screen if Xinerama is active.
212 * \param screen the logical screen
213 * \return the X screen
216 get_phys_screen(int screen)
218 if(XineramaIsActive(globalconf.display))
219 return DefaultScreen(globalconf.display);
220 return screen;
223 /** Move a client to a virtual screen
224 * \param c the client
225 * \param new_screen The destinatiuon screen
226 * \param doresize set to True if we also move the client to the new x and
227 * y of the new screen
229 void
230 move_client_to_screen(Client *c, int new_screen, Bool doresize)
232 Tag *tag;
233 int old_screen = c->screen;
234 Area from, to;
236 for(tag = globalconf.screens[old_screen].tags; tag; tag = tag->next)
237 untag_client(c, tag);
239 c->screen = new_screen;
241 /* tag client with new screen tags */
242 tag_client_with_current_selected(c);
244 /* resize the windows if it's floating */
245 if(doresize && old_screen != c->screen)
247 Area new_geometry, new_f_geometry;
248 new_f_geometry = c->f_geometry;
250 to = get_screen_area(c->screen, NULL, NULL);
251 from = get_screen_area(old_screen, NULL, NULL);
253 /* compute new coords in new screen */
254 new_f_geometry.x = (c->f_geometry.x - from.x) + to.x;
255 new_f_geometry.y = (c->f_geometry.y - from.y) + to.y;
257 /* check that new coords are still in the screen */
258 if(new_f_geometry.width > to.width)
259 new_f_geometry.width = to.width;
260 if(new_f_geometry.height > to.height)
261 new_f_geometry.height = to.height;
262 if(new_f_geometry.x + new_f_geometry.width >= to.x + to.width)
263 new_f_geometry.x = to.x + to.width - new_f_geometry.width - 2 * c->border;
264 if(new_f_geometry.y + new_f_geometry.height >= to.y + to.height)
265 new_f_geometry.y = to.y + to.height - new_f_geometry.height - 2 * c->border;
267 if(c->ismax)
269 new_geometry = c->geometry;
271 /* compute new coords in new screen */
272 new_geometry.x = (c->geometry.x - from.x) + to.x;
273 new_geometry.y = (c->geometry.y - from.y) + to.y;
275 /* check that new coords are still in the screen */
276 if(new_geometry.width > to.width)
277 new_geometry.width = to.width;
278 if(new_geometry.height > to.height)
279 new_geometry.height = to.height;
280 if(new_geometry.x + new_geometry.width >= to.x + to.width)
281 new_geometry.x = to.x + to.width - new_geometry.width - 2 * c->border;
282 if(new_geometry.y + new_geometry.height >= to.y + to.height)
283 new_geometry.y = to.y + to.height - new_geometry.height - 2 * c->border;
285 /* compute new coords for max in new screen */
286 c->m_geometry.x = (c->m_geometry.x - from.x) + to.x;
287 c->m_geometry.y = (c->m_geometry.y - from.y) + to.y;
289 /* check that new coords are still in the screen */
290 if(c->m_geometry.width > to.width)
291 c->m_geometry.width = to.width;
292 if(c->m_geometry.height > to.height)
293 c->m_geometry.height = to.height;
294 if(c->m_geometry.x + c->m_geometry.width >= to.x + to.width)
295 c->m_geometry.x = to.x + to.width - c->m_geometry.width - 2 * c->border;
296 if(c->m_geometry.y + c->m_geometry.height >= to.y + to.height)
297 c->m_geometry.y = to.y + to.height - c->m_geometry.height - 2 * c->border;
299 client_resize(c, new_geometry, False);
301 /* if floating, move to this new coords */
302 else if(c->isfloating)
303 client_resize(c, new_f_geometry, False);
304 /* otherwise just register them */
305 else
307 c->f_geometry = new_f_geometry;
308 globalconf.screens[old_screen].need_arrange = True;
309 globalconf.screens[c->screen].need_arrange = True;
314 /** Move mouse pointer to x_org and y_xorg of specified screen
315 * \param screen screen number
317 static void
318 move_mouse_pointer_to_screen(int screen)
320 if(XineramaIsActive(globalconf.display))
322 Area area = get_screen_area(screen, NULL, NULL);
323 XWarpPointer(globalconf.display,
324 None,
325 DefaultRootWindow(globalconf.display),
326 0, 0, 0, 0, area.x, area.y);
328 else
329 XWarpPointer(globalconf.display,
330 None,
331 RootWindow(globalconf.display, screen),
332 0, 0, 0, 0, 0, 0);
336 /** Switch focus to a specified screen
337 * \param screen Screen ID
338 * \param arg screen number
339 * \ingroup ui_callback
341 void
342 uicb_screen_focus(int screen, char *arg)
344 int new_screen;
346 if(arg)
347 new_screen = compute_new_value_from_arg(arg, screen);
348 else
349 new_screen = screen + 1;
351 if (new_screen < 0)
352 new_screen = globalconf.nscreen - 1;
353 if (new_screen > (globalconf.nscreen - 1))
354 new_screen = 0;
356 focus(NULL, new_screen, False);
358 move_mouse_pointer_to_screen(new_screen);
361 /** Move client to a virtual screen (if Xinerama is active)
362 * \param screen Screen ID
363 * \param arg screen number
364 * \ingroup ui_callback
366 void
367 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
369 int new_screen, prev_screen;
370 Client *sel = globalconf.focus->client;
372 if(!sel || !XineramaIsActive(globalconf.display))
373 return;
375 if(arg)
376 new_screen = compute_new_value_from_arg(arg, sel->screen);
377 else
378 new_screen = sel->screen + 1;
380 if(new_screen >= globalconf.nscreen)
381 new_screen = 0;
382 else if(new_screen < 0)
383 new_screen = globalconf.nscreen - 1;
385 prev_screen = sel->screen;
386 move_client_to_screen(sel, new_screen, True);
387 move_mouse_pointer_to_screen(new_screen);
388 focus(sel, sel->screen, False);
390 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80