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 "layouts/floating.h"
29 #include "common/util.h"
31 extern AwesomeConf globalconf
;
34 * \param screen Screen number
35 * \param statusbar statusbar
36 * \param padding Padding
40 get_screen_area(int screen
, Statusbar
*statusbar
, Padding
*padding
)
45 area
= globalconf
.screens
[screen
].geometry
;
47 /* make padding corrections */
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
)
62 area
.height
-= sb
->height
;
67 area
.width
-= sb
->height
;
77 * \param screen Screen number
78 * \param statusbar the statusbar
79 * \param padding Padding
83 get_display_area(int screen
, Statusbar
*statusbar
, Padding
*padding
)
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 */
102 area
.x
+= padding
->left
;
103 area
.y
+= padding
->top
;
104 area
.width
-= padding
->left
+ padding
->right
;
105 area
.height
-= padding
->top
+ padding
->bottom
;
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
)
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
)))
132 return DefaultScreen(globalconf
.display
);
137 screen_xsi_to_area(XineramaScreenInfo si
)
144 a
.height
= si
.height
;
150 screen_build_screens(void)
152 XineramaScreenInfo
*si
;
153 int xinerama_screen_number
, screen
, screen_to_test
;
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
++)
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 */
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
);
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
;
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
);
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
230 move_client_to_screen(Client
*c
, int new_screen
, Bool doresize
)
233 int old_screen
= c
->screen
;
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
;
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 */
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
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
,
325 DefaultRootWindow(globalconf
.display
),
326 0, 0, 0, 0, area
.x
, area
.y
);
329 XWarpPointer(globalconf
.display
,
331 RootWindow(globalconf
.display
, screen
),
336 /** Switch focus to a specified screen
337 * \param screen Screen ID
338 * \param arg screen number
339 * \ingroup ui_callback
342 uicb_screen_focus(int screen
, char *arg
)
347 new_screen
= compute_new_value_from_arg(arg
, screen
);
349 new_screen
= screen
+ 1;
352 new_screen
= globalconf
.nscreen
- 1;
353 if (new_screen
> (globalconf
.nscreen
- 1))
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
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
))
376 new_screen
= compute_new_value_from_arg(arg
, sel
->screen
);
378 new_screen
= sel
->screen
+ 1;
380 if(new_screen
>= globalconf
.nscreen
)
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