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>
29 #include "layouts/floating.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
)
42 int screen_number
= 0;
43 XineramaScreenInfo
*si
;
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
;
60 /* emulate Xinerama info but only fill the screen we want */
63 area
.width
= DisplayWidth(globalconf
.display
, screen
);
64 area
.height
= DisplayHeight(globalconf
.display
, screen
);
67 /* make padding corrections */
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
)
82 area
.height
-= sb
->height
;
87 area
.width
-= sb
->height
;
97 * \param screen Screen number
98 * \param statusbar the statusbar
99 * \param padding Padding
103 get_display_area(int screen
, Statusbar
*statusbar
, Padding
*padding
)
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 */
122 area
.x
+= padding
->left
;
123 area
.y
+= padding
->top
;
124 area
.width
-= padding
->left
+ padding
->right
;
125 area
.height
-= padding
->top
+ padding
->bottom
;
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
)
141 /* don't waste our time */
142 if(!XineramaIsActive(globalconf
.display
))
143 return DefaultScreen(globalconf
.display
);
145 for(i
= 0; i
< get_screen_count(); i
++)
147 area
= get_screen_area(i
, NULL
, NULL
);
148 if((x
< 0 || (x
>= area
.x
&& x
< area
.x
+ area
.width
))
149 && (y
< 0 || (y
>= area
.y
&& y
< area
.y
+ area
.height
)))
152 return DefaultScreen(globalconf
.display
);
155 /** Return the actual screen count
156 * \return the number of screen available
159 get_screen_count(void)
163 if(XineramaIsActive(globalconf
.display
))
164 XineramaQueryScreens(globalconf
.display
, &screen_number
);
166 return ScreenCount(globalconf
.display
);
168 return screen_number
;
171 /** This returns the real X screen number for a logical
172 * screen if Xinerama is active.
173 * \param screen the logical screen
174 * \return the X screen
177 get_phys_screen(int screen
)
179 if(XineramaIsActive(globalconf
.display
))
180 return DefaultScreen(globalconf
.display
);
184 /** Move a client to a virtual screen
185 * \param c the client
186 * \param new_screen The destinatiuon screen
187 * \param doresize set to True if we also move the client to the new x and
188 * y of the new screen
191 move_client_to_screen(Client
*c
, int new_screen
, Bool doresize
)
194 int old_screen
= c
->screen
;
197 for(tag
= globalconf
.screens
[old_screen
].tags
; tag
; tag
= tag
->next
)
198 untag_client(c
, tag
);
200 c
->screen
= new_screen
;
202 /* tag client with new screen tags */
203 tag_client_with_current_selected(c
);
205 /* resize the windows if it's floating */
206 if(doresize
&& old_screen
!= c
->screen
)
208 Area new_f_geometry
= c
->f_geometry
;
210 to
= get_screen_area(c
->screen
, NULL
, NULL
);
211 from
= get_screen_area(old_screen
, NULL
, NULL
);
213 /* compute new coords in new screen */
214 new_f_geometry
.x
= (c
->f_geometry
.x
- from
.x
) + to
.x
;
215 new_f_geometry
.y
= (c
->f_geometry
.y
- from
.y
) + to
.y
;
217 /* check that new coords are still in the screen */
218 if(new_f_geometry
.width
> to
.width
)
219 new_f_geometry
.width
= to
.width
;
220 if(new_f_geometry
.height
> to
.height
)
221 new_f_geometry
.height
= to
.height
;
222 if(new_f_geometry
.x
+ new_f_geometry
.width
>= to
.x
+ to
.width
)
223 new_f_geometry
.x
= to
.x
+ to
.width
- new_f_geometry
.width
- 2 * c
->border
;
224 if(new_f_geometry
.y
+ new_f_geometry
.height
>= to
.y
+ to
.height
)
225 new_f_geometry
.y
= to
.y
+ to
.height
- new_f_geometry
.height
- 2 * c
->border
;
229 /* compute new coords for max in new screen */
230 c
->m_geometry
.x
= (c
->m_geometry
.x
- from
.x
) + to
.x
;
231 c
->m_geometry
.y
= (c
->m_geometry
.y
- from
.y
) + to
.y
;
233 /* check that new coords are still in the screen */
234 if(c
->m_geometry
.width
> to
.width
)
235 c
->m_geometry
.width
= to
.width
;
236 if(c
->m_geometry
.height
> to
.height
)
237 c
->m_geometry
.height
= to
.height
;
238 if(c
->m_geometry
.x
+ c
->m_geometry
.width
>= to
.x
+ to
.width
)
239 c
->m_geometry
.x
= to
.x
+ to
.width
- c
->m_geometry
.width
- 2 * c
->border
;
240 if(c
->m_geometry
.y
+ c
->m_geometry
.height
>= to
.y
+ to
.height
)
241 c
->m_geometry
.y
= to
.y
+ to
.height
- c
->m_geometry
.height
- 2 * c
->border
;
244 /* if floating, move to this new coords */
246 client_resize(c
, new_f_geometry
, False
);
247 /* otherwise just register them */
250 c
->f_geometry
= new_f_geometry
;
256 /** Move mouse pointer to x_org and y_xorg of specified screen
257 * \param screen screen number
260 move_mouse_pointer_to_screen(int screen
)
262 if(XineramaIsActive(globalconf
.display
))
264 Area area
= get_screen_area(screen
, NULL
, NULL
);
265 XWarpPointer(globalconf
.display
,
267 DefaultRootWindow(globalconf
.display
),
268 0, 0, 0, 0, area
.x
, area
.y
);
271 XWarpPointer(globalconf
.display
,
273 RootWindow(globalconf
.display
, screen
),
278 /** Switch focus to a specified screen
279 * \param screen Screen ID
280 * \param arg screen number
281 * \ingroup ui_callback
284 uicb_screen_focus(int screen
, char *arg
)
286 int new_screen
, numscreens
= get_screen_count();
289 new_screen
= compute_new_value_from_arg(arg
, screen
);
291 new_screen
= screen
+ 1;
294 new_screen
= numscreens
- 1;
295 if (new_screen
> (numscreens
- 1))
298 focus(focus_get_current_client(new_screen
), True
, new_screen
);
300 move_mouse_pointer_to_screen(new_screen
);
303 /** Move client to a virtual screen (if Xinerama is active)
304 * \param screen Screen ID
305 * \param arg screen number
306 * \ingroup ui_callback
309 uicb_client_movetoscreen(int screen
__attribute__ ((unused
)), char *arg
)
311 int new_screen
, prev_screen
;
312 Client
*sel
= globalconf
.focus
->client
;
314 if(!sel
|| !XineramaIsActive(globalconf
.display
))
318 new_screen
= compute_new_value_from_arg(arg
, sel
->screen
);
320 new_screen
= sel
->screen
+ 1;
322 if(new_screen
>= get_screen_count())
324 else if(new_screen
< 0)
325 new_screen
= get_screen_count() - 1;
327 prev_screen
= sel
->screen
;
328 move_client_to_screen(sel
, new_screen
, True
);
329 move_mouse_pointer_to_screen(new_screen
);
330 arrange(prev_screen
);
332 focus(sel
, True
, sel
->screen
);
334 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80