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>
28 #include "statusbar.h"
30 #include "layouts/floating.h"
32 extern AwesomeConf globalconf
;
35 * \param screen Screen number
36 * \param statusbar statusbar
37 * \param padding Padding
41 get_screen_area(int screen
, Statusbar
*statusbar
, Padding
*padding
)
43 int screen_number
= 0;
44 XineramaScreenInfo
*si
;
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
;
61 /* emulate Xinerama info but only fill the screen we want */
64 area
.width
= DisplayWidth(globalconf
.display
, screen
);
65 area
.height
= DisplayHeight(globalconf
.display
, screen
);
68 /* make padding corrections */
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
)
83 area
.height
-= sb
->height
;
88 area
.width
-= sb
->height
;
98 * \param screen Screen number
99 * \param statusbar the statusbar
100 * \param padding Padding
104 get_display_area(int screen
, Statusbar
*statusbar
, Padding
*padding
)
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 */
123 area
.x
+= padding
->left
;
124 area
.y
+= padding
->top
;
125 area
.width
-= padding
->left
+ padding
->right
;
126 area
.height
-= padding
->top
+ padding
->bottom
;
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
)
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
)))
153 return DefaultScreen(globalconf
.display
);
156 /** Return the actual screen count
157 * \return the number of screen available
160 get_screen_count(void)
164 if(XineramaIsActive(globalconf
.display
))
165 XineramaQueryScreens(globalconf
.display
, &screen_number
);
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
);
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
192 move_client_to_screen(Client
*c
, int new_screen
, Bool doresize
)
195 int old_screen
= c
->screen
;
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
;
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 */
247 client_resize(c
, new_f_geometry
, False
);
248 /* otherwise just register them */
251 c
->f_geometry
= new_f_geometry
;
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
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
,
274 DefaultRootWindow(globalconf
.display
),
275 0, 0, 0, 0, area
.x
, area
.y
);
278 XWarpPointer(globalconf
.display
,
280 RootWindow(globalconf
.display
, screen
),
285 /** Switch focus to a specified screen
286 * \param screen Screen ID
287 * \param arg screen number
288 * \ingroup ui_callback
291 uicb_screen_focus(int screen
, char *arg
)
293 int new_screen
, numscreens
= get_screen_count();
296 new_screen
= compute_new_value_from_arg(arg
, screen
);
298 new_screen
= screen
+ 1;
301 new_screen
= numscreens
- 1;
302 if (new_screen
> (numscreens
- 1))
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
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
))
325 new_screen
= compute_new_value_from_arg(arg
, sel
->screen
);
327 new_screen
= sel
->screen
+ 1;
329 if(new_screen
>= get_screen_count())
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
);
340 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80