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.
25 #include <xcb/xinerama.h>
33 #include "common/xutil.h"
36 screen_xsitoarea(xcb_xinerama_screen_info_t si
)
48 /** Get screens informations and fill global configuration.
53 /* Check for extension before checking for Xinerama */
54 if(xcb_get_extension_data(globalconf
.connection
, &xcb_xinerama_id
)->present
)
56 xcb_xinerama_is_active_reply_t
*xia
;
57 xia
= xcb_xinerama_is_active_reply(globalconf
.connection
, xcb_xinerama_is_active(globalconf
.connection
), NULL
);
58 globalconf
.xinerama_is_active
= xia
->state
;
62 if(globalconf
.xinerama_is_active
)
64 xcb_xinerama_query_screens_reply_t
*xsq
;
65 xcb_xinerama_screen_info_t
*xsi
;
66 int xinerama_screen_number
;
68 xsq
= xcb_xinerama_query_screens_reply(globalconf
.connection
,
69 xcb_xinerama_query_screens_unchecked(globalconf
.connection
),
72 xsi
= xcb_xinerama_query_screens_screen_info(xsq
);
73 xinerama_screen_number
= xcb_xinerama_query_screens_screen_info_length(xsq
);
75 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
76 for(int screen
= 0; screen
< xinerama_screen_number
; screen
++)
79 foreach(screen_to_test
, globalconf
.screens
)
80 if(xsi
[screen
].x_org
== screen_to_test
->geometry
.x
81 && xsi
[screen
].y_org
== screen_to_test
->geometry
.y
)
83 /* we already have a screen for this area, just check if
84 * it's not bigger and drop it */
86 int i
= screen_array_indexof(&globalconf
.screens
, screen_to_test
);
87 screen_to_test
->geometry
.width
=
88 MAX(xsi
[screen
].width
, xsi
[i
].width
);
89 screen_to_test
->geometry
.height
=
90 MAX(xsi
[screen
].height
, xsi
[i
].height
);
96 s
.geometry
= screen_xsitoarea(xsi
[screen
]);
97 screen_array_append(&globalconf
.screens
, s
);
104 /* One screen only / Zaphod mode */
106 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
109 xcb_screen_t
*xcb_screen
= xutil_screen_get(globalconf
.connection
, screen
);
114 s
.geometry
.width
= xcb_screen
->width_in_pixels
;
115 s
.geometry
.height
= xcb_screen
->height_in_pixels
;
116 screen_array_append(&globalconf
.screens
, s
);
119 globalconf
.screen_focus
= globalconf
.screens
.tab
;
122 /** Return the Xinerama screen number where the coordinates belongs to.
123 * \param screen The logical screen number.
124 * \param x X coordinate
125 * \param y Y coordinate
126 * \return Screen pointer or screen param if no match or no multi-head.
129 screen_getbycoord(screen_t
*screen
, int x
, int y
)
131 /* don't waste our time */
132 if(!globalconf
.xinerama_is_active
)
135 foreach(s
, globalconf
.screens
)
136 if((x
< 0 || (x
>= s
->geometry
.x
&& x
< s
->geometry
.x
+ s
->geometry
.width
))
137 && (y
< 0 || (y
>= s
->geometry
.y
&& y
< s
->geometry
.y
+ s
->geometry
.height
)))
143 /** Get screens info.
144 * \param screen Screen.
145 * \param wiboxes Wiboxes list to remove.
146 * \param padding Padding.
147 * \param strut Honor windows strut.
148 * \return The screen area.
151 screen_area_get(screen_t
*screen
, wibox_array_t
*wiboxes
,
152 padding_t
*padding
, bool strut
)
154 area_t area
= screen
->geometry
;
155 uint16_t top
= 0, bottom
= 0, left
= 0, right
= 0;
157 /* make padding corrections */
160 area
.x
+= padding
->left
;
161 area
.y
+= padding
->top
;
162 area
.width
-= padding
->left
+ padding
->right
;
163 area
.height
-= padding
->top
+ padding
->bottom
;
166 /* Struts are additive, to allow for multiple clients at the screen edge. */
167 /* Some clients request more space than their size, because another window of the same app already has some space. */
168 /* So we clamp the strut size. */
170 foreach(_c
, globalconf
.clients
)
173 if(client_isvisible(c
, screen
) && !c
->ignore_strut
)
175 if(c
->strut
.top_start_x
|| c
->strut
.top_end_x
)
178 top
+= MIN(c
->strut
.top
, c
->geometry
.height
);
180 top
+= c
->geometry
.height
;
182 if(c
->strut
.bottom_start_x
|| c
->strut
.bottom_end_x
)
185 bottom
+= MIN(c
->strut
.bottom
, c
->geometry
.height
);
187 bottom
+= c
->geometry
.height
;
189 if(c
->strut
.left_start_y
|| c
->strut
.left_end_y
)
192 left
+= MIN(c
->strut
.left
, c
->geometry
.width
);
194 left
+= c
->geometry
.width
;
196 if(c
->strut
.right_start_y
|| c
->strut
.right_end_y
)
199 right
+= MIN(c
->strut
.right
, c
->geometry
.width
);
201 right
+= c
->geometry
.width
;
206 /* swindow geometry includes borders. */
208 foreach(_w
, *wiboxes
)
215 top
+= w
->sw
.geometry
.height
;
218 bottom
+= w
->sw
.geometry
.height
;
221 left
+= w
->sw
.geometry
.width
;
224 right
+= w
->sw
.geometry
.width
;
233 area
.width
-= left
+ right
;
234 area
.height
-= top
+ bottom
;
239 /** Get display info.
240 * \param phys_screen Physical screen number.
241 * \param wiboxes The wiboxes.
242 * \param padding Padding.
243 * \return The display area.
246 display_area_get(int phys_screen
, wibox_array_t
*wiboxes
, padding_t
*padding
)
248 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
249 area_t area
= { .x
= 0,
251 .width
= s
->width_in_pixels
,
252 .height
= s
->height_in_pixels
};
255 foreach(_w
, *wiboxes
)
258 area
.y
+= w
->position
== Top
? w
->sw
.geometry
.height
: 0;
259 area
.height
-= (w
->position
== Top
|| w
->position
== Bottom
) ? w
->sw
.geometry
.height
: 0;
262 /* make padding corrections */
265 area
.x
+= padding
->left
;
266 area
.y
+= padding
->top
;
267 area
.width
-= padding
->left
+ padding
->right
;
268 area
.height
-= padding
->top
+ padding
->bottom
;
273 /** This returns the real X screen number for a logical
274 * screen if Xinerama is active.
275 * \param screen The logical screen.
276 * \return The X screen.
279 screen_virttophys(int screen
)
281 if(globalconf
.xinerama_is_active
)
282 return globalconf
.default_screen
;
286 /** Move a client to a virtual screen.
287 * \param c The client to move.
288 * \param new_screen The destinatiuon screen.
289 * \param dotag Set to true if we also change tags.
290 * \param doresize Set to true if we also move the client to the new x and
291 * y of the new screen.
294 screen_client_moveto(client_t
*c
, screen_t
*new_screen
, bool dotag
, bool doresize
)
297 screen_t
*old_screen
= c
->screen
;
298 tag_array_t
*old_tags
= &old_screen
->tags
,
299 *new_tags
= &new_screen
->tags
;
301 bool wasvisible
= client_isvisible(c
, c
->screen
);
303 if(new_screen
== c
->screen
)
306 c
->screen
= new_screen
;
309 c
->titlebar
->screen
= new_screen
;
311 if(dotag
&& !c
->issticky
)
313 /* remove old tags */
314 for(i
= 0; i
< old_tags
->len
; i
++)
315 untag_client(c
, old_tags
->tab
[i
]);
318 foreach(new_tag
, *new_tags
)
319 if((*new_tag
)->selected
)
321 tag_push(globalconf
.L
, *new_tag
);
327 old_screen
->need_arrange
= true;
328 client_need_arrange(c
);
333 from
= screen_area_get(old_screen
, NULL
, NULL
, false);
334 to
= screen_area_get(c
->screen
, NULL
, NULL
, false);
336 area_t new_geometry
= c
->geometry
;
341 area_t new_f_geometry
= c
->geometries
.fullscreen
;
343 new_f_geometry
.x
= to
.x
+ new_f_geometry
.x
- from
.x
;
344 new_f_geometry
.y
= to
.y
+ new_f_geometry
.y
- from
.x
;
346 /* resize the client's original geometry if it doesn't fit the screen */
347 if(new_f_geometry
.width
> to
.width
)
348 new_f_geometry
.width
= to
.width
;
349 if(new_f_geometry
.height
> to
.height
)
350 new_f_geometry
.height
= to
.height
;
352 /* make sure the client is still on the screen */
353 if(new_f_geometry
.x
+ new_f_geometry
.width
> to
.x
+ to
.width
)
354 new_f_geometry
.x
= to
.x
+ to
.width
- new_f_geometry
.width
;
355 if(new_f_geometry
.y
+ new_f_geometry
.height
> to
.y
+ to
.height
)
356 new_f_geometry
.y
= to
.y
+ to
.height
- new_f_geometry
.height
;
358 c
->geometries
.fullscreen
= new_f_geometry
;
362 new_geometry
.x
= to
.x
+ new_geometry
.x
- from
.x
;
363 new_geometry
.y
= to
.y
+ new_geometry
.y
- from
.y
;
365 /* resize the client if it doesn't fit the new screen */
366 if(new_geometry
.width
> to
.width
)
367 new_geometry
.width
= to
.width
;
368 if(new_geometry
.height
> to
.height
)
369 new_geometry
.height
= to
.height
;
371 /* make sure the client is still on the screen */
372 if(new_geometry
.x
+ new_geometry
.width
> to
.x
+ to
.width
)
373 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
;
374 if(new_geometry
.y
+ new_geometry
.height
> to
.y
+ to
.height
)
375 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
;
377 /* move / resize the client */
378 client_resize(c
, new_geometry
, false);
382 * \param L The Lua VM state.
383 * \return The number of elements pushed on stack.
385 * \lfield number The screen number, to get a screen.
388 luaA_screen_module_index(lua_State
*L
)
390 int screen
= luaL_checknumber(L
, 2) - 1;
392 luaA_checkscreen(screen
);
393 lua_pushlightuserdata(L
, &globalconf
.screens
.tab
[screen
]);
394 return luaA_settype(L
, "screen");
397 /** Get or set screen tags.
398 * \param L The Lua VM state.
399 * \return The number of elements pushed on stack.
401 * \lparam None or a table of tags to set to the screen.
402 * The table must contains at least one tag.
403 * \return A table with all screen tags.
406 luaA_screen_tags(lua_State
*L
)
409 screen_t
*s
= lua_touserdata(L
, 1);
412 luaL_typerror(L
, 1, "screen");
414 if(lua_gettop(L
) == 2)
416 luaA_checktable(L
, 2);
418 /* remove current tags */
419 for(i
= 0; i
< s
->tags
.len
; i
++)
420 s
->tags
.tab
[i
]->screen
= NULL
;
422 tag_array_wipe(&s
->tags
);
423 tag_array_init(&s
->tags
);
425 s
->need_arrange
= true;
429 while(lua_next(L
, 2))
430 tag_append_to_screen(s
);
434 lua_createtable(L
, s
->tags
.len
, 0);
435 for(i
= 0; i
< s
->tags
.len
; i
++)
437 tag_push(L
, s
->tags
.tab
[i
]);
438 lua_rawseti(L
, -2, i
+ 1);
446 * \param L The Lua VM state.
447 * \return The number of elements pushed on stack.
449 * \lfield coords The screen coordinates. Immutable.
450 * \lfield workarea The screen workarea, i.e. without wiboxes.
453 luaA_screen_index(lua_State
*L
)
459 if(luaA_usemetatable(L
, 1, 2))
462 buf
= luaL_checklstring(L
, 2, &len
);
463 s
= lua_touserdata(L
, 1);
465 switch(a_tokenize(buf
, len
))
468 luaA_pusharea(L
, s
->geometry
);
471 luaA_pusharea(L
, screen_area_get(s
, &s
->wiboxes
, &s
->padding
, true));
480 /** Set or get the screen padding.
481 * \param L The Lua VM state.
482 * \return The number of elements pushed on stack.
484 * \lparam None or a table with new padding values.
485 * \lreturn The screen padding. A table with top, right, left and bottom
486 * keys and values in pixel.
489 luaA_screen_padding(lua_State
*L
)
491 screen_t
*s
= lua_touserdata(L
, 1);
494 luaL_typerror(L
, 1, "screen");
496 if(lua_gettop(L
) == 2)
498 s
->padding
= luaA_getopt_padding(L
, 2, &s
->padding
);
500 s
->need_arrange
= true;
502 /* All the wiboxes repositioned */
503 foreach(w
, s
->wiboxes
)
504 wibox_position_update(*w
);
506 ewmh_update_workarea(screen_virttophys(screen_array_indexof(&globalconf
.screens
, s
)));
508 return luaA_pushpadding(L
, &s
->padding
);
511 /** Get the screen count.
512 * \param L The Lua VM state.
513 * \return The number of elements pushed on stack.
516 * \lreturn The screen count, at least 1.
519 luaA_screen_count(lua_State
*L
)
521 lua_pushnumber(L
, globalconf
.screens
.len
);
525 const struct luaL_reg awesome_screen_methods
[] =
527 { "count", luaA_screen_count
},
528 { "__index", luaA_screen_module_index
},
532 const struct luaL_reg awesome_screen_meta
[] =
534 { "tags", luaA_screen_tags
},
535 { "padding", luaA_screen_padding
},
536 { "__index", luaA_screen_index
},
540 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80