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
;
167 foreach(_c
, globalconf
.clients
)
170 if(client_isvisible(c
, screen
))
172 if(c
->strut
.top_start_x
|| c
->strut
.top_end_x
)
175 top
= MAX(top
, c
->strut
.top
);
177 top
= MAX(top
, (c
->geometry
.y
- area
.y
) + c
->geometry
.height
);
179 if(c
->strut
.bottom_start_x
|| c
->strut
.bottom_end_x
)
182 bottom
= MAX(bottom
, c
->strut
.bottom
);
184 bottom
= MAX(bottom
, (area
.y
+ area
.height
) - c
->geometry
.y
);
186 if(c
->strut
.left_start_y
|| c
->strut
.left_end_y
)
189 left
= MAX(left
, c
->strut
.left
);
191 left
= MAX(left
, (c
->geometry
.x
- area
.x
) + c
->geometry
.width
);
193 if(c
->strut
.right_start_y
|| c
->strut
.right_end_y
)
196 right
= MAX(right
, c
->strut
.right
);
198 right
= MAX(right
, (area
.x
+ area
.width
) - c
->geometry
.x
);
205 foreach(_w
, *wiboxes
)
212 top
= MAX(top
, (uint16_t) (w
->sw
.geometry
.y
- area
.y
) + w
->sw
.geometry
.height
+ 2 * w
->sw
.border
.width
);
215 bottom
= MAX(bottom
, (uint16_t) (area
.y
+ area
.height
) - w
->sw
.geometry
.y
);
218 left
= MAX(left
, (uint16_t) (w
->sw
.geometry
.x
- area
.x
) + w
->sw
.geometry
.width
+ 2 * w
->sw
.border
.width
);
221 right
= MAX(right
, (uint16_t) (area
.x
+ area
.width
) - w
->sw
.geometry
.x
);
230 area
.width
-= left
+ right
;
231 area
.height
-= top
+ bottom
;
236 /** Get display info.
237 * \param phys_screen Physical screen number.
238 * \param wiboxes The wiboxes.
239 * \param padding Padding.
240 * \return The display area.
243 display_area_get(int phys_screen
, wibox_array_t
*wiboxes
, padding_t
*padding
)
245 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
246 area_t area
= { .x
= 0,
248 .width
= s
->width_in_pixels
,
249 .height
= s
->height_in_pixels
};
252 foreach(_w
, *wiboxes
)
255 area
.y
+= w
->position
== Top
? w
->sw
.geometry
.height
: 0;
256 area
.height
-= (w
->position
== Top
|| w
->position
== Bottom
) ? w
->sw
.geometry
.height
: 0;
259 /* make padding corrections */
262 area
.x
+= padding
->left
;
263 area
.y
+= padding
->top
;
264 area
.width
-= padding
->left
+ padding
->right
;
265 area
.height
-= padding
->top
+ padding
->bottom
;
270 /** This returns the real X screen number for a logical
271 * screen if Xinerama is active.
272 * \param screen The logical screen.
273 * \return The X screen.
276 screen_virttophys(int screen
)
278 if(globalconf
.xinerama_is_active
)
279 return globalconf
.default_screen
;
283 /** Move a client to a virtual screen.
284 * \param c The client to move.
285 * \param new_screen The destinatiuon screen.
286 * \param dotag Set to true if we also change tags.
287 * \param doresize Set to true if we also move the client to the new x and
288 * y of the new screen.
291 screen_client_moveto(client_t
*c
, screen_t
*new_screen
, bool dotag
, bool doresize
)
294 screen_t
*old_screen
= c
->screen
;
295 tag_array_t
*old_tags
= &old_screen
->tags
,
296 *new_tags
= &new_screen
->tags
;
298 bool wasvisible
= client_isvisible(c
, c
->screen
);
300 if(new_screen
== c
->screen
)
303 c
->screen
= new_screen
;
306 c
->titlebar
->screen
= new_screen
;
308 if(dotag
&& !c
->issticky
)
310 /* remove old tags */
311 for(i
= 0; i
< old_tags
->len
; i
++)
312 untag_client(c
, old_tags
->tab
[i
]);
315 foreach(new_tag
, *new_tags
)
316 if((*new_tag
)->selected
)
318 tag_push(globalconf
.L
, *new_tag
);
324 old_screen
->need_arrange
= true;
325 client_need_arrange(c
);
330 from
= screen_area_get(old_screen
, NULL
, NULL
, false);
331 to
= screen_area_get(c
->screen
, NULL
, NULL
, false);
333 area_t new_geometry
= c
->geometry
;
338 area_t new_f_geometry
= c
->geometries
.fullscreen
;
340 new_f_geometry
.x
= to
.x
+ new_f_geometry
.x
- from
.x
;
341 new_f_geometry
.y
= to
.y
+ new_f_geometry
.y
- from
.x
;
343 /* resize the client's original geometry if it doesn't fit the screen */
344 if(new_f_geometry
.width
> to
.width
)
345 new_f_geometry
.width
= to
.width
;
346 if(new_f_geometry
.height
> to
.height
)
347 new_f_geometry
.height
= to
.height
;
349 /* make sure the client is still on the screen */
350 if(new_f_geometry
.x
+ new_f_geometry
.width
> to
.x
+ to
.width
)
351 new_f_geometry
.x
= to
.x
+ to
.width
- new_f_geometry
.width
;
352 if(new_f_geometry
.y
+ new_f_geometry
.height
> to
.y
+ to
.height
)
353 new_f_geometry
.y
= to
.y
+ to
.height
- new_f_geometry
.height
;
355 c
->geometries
.fullscreen
= new_f_geometry
;
359 new_geometry
.x
= to
.x
+ new_geometry
.x
- from
.x
;
360 new_geometry
.y
= to
.y
+ new_geometry
.y
- from
.y
;
362 /* resize the client if it doesn't fit the new screen */
363 if(new_geometry
.width
> to
.width
)
364 new_geometry
.width
= to
.width
;
365 if(new_geometry
.height
> to
.height
)
366 new_geometry
.height
= to
.height
;
368 /* make sure the client is still on the screen */
369 if(new_geometry
.x
+ new_geometry
.width
> to
.x
+ to
.width
)
370 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
;
371 if(new_geometry
.y
+ new_geometry
.height
> to
.y
+ to
.height
)
372 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
;
374 /* move / resize the client */
375 client_resize(c
, new_geometry
, false);
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
382 * \lfield number The screen number, to get a screen.
385 luaA_screen_module_index(lua_State
*L
)
387 int screen
= luaL_checknumber(L
, 2) - 1;
389 luaA_checkscreen(screen
);
390 lua_pushlightuserdata(L
, &globalconf
.screens
.tab
[screen
]);
391 return luaA_settype(L
, "screen");
394 /** Get or set screen tags.
395 * \param L The Lua VM state.
396 * \return The number of elements pushed on stack.
398 * \lparam None or a table of tags to set to the screen.
399 * The table must contains at least one tag.
400 * \return A table with all screen tags.
403 luaA_screen_tags(lua_State
*L
)
406 screen_t
*s
= lua_touserdata(L
, 1);
409 luaL_typerror(L
, 1, "screen");
411 if(lua_gettop(L
) == 2)
413 luaA_checktable(L
, 2);
415 /* remove current tags */
416 for(i
= 0; i
< s
->tags
.len
; i
++)
417 s
->tags
.tab
[i
]->screen
= NULL
;
419 tag_array_wipe(&s
->tags
);
420 tag_array_init(&s
->tags
);
422 s
->need_arrange
= true;
426 while(lua_next(L
, 2))
427 tag_append_to_screen(s
);
431 lua_createtable(L
, s
->tags
.len
, 0);
432 for(i
= 0; i
< s
->tags
.len
; i
++)
434 tag_push(L
, s
->tags
.tab
[i
]);
435 lua_rawseti(L
, -2, i
+ 1);
443 * \param L The Lua VM state.
444 * \return The number of elements pushed on stack.
446 * \lfield coords The screen coordinates. Immutable.
447 * \lfield workarea The screen workarea, i.e. without wiboxes.
450 luaA_screen_index(lua_State
*L
)
456 if(luaA_usemetatable(L
, 1, 2))
459 buf
= luaL_checklstring(L
, 2, &len
);
460 s
= lua_touserdata(L
, 1);
462 switch(a_tokenize(buf
, len
))
465 luaA_pusharea(L
, s
->geometry
);
468 luaA_pusharea(L
, screen_area_get(s
, &s
->wiboxes
, &s
->padding
, true));
477 /** Set or get the screen padding.
478 * \param L The Lua VM state.
479 * \return The number of elements pushed on stack.
481 * \lparam None or a table with new padding values.
482 * \lreturn The screen padding. A table with top, right, left and bottom
483 * keys and values in pixel.
486 luaA_screen_padding(lua_State
*L
)
488 screen_t
*s
= lua_touserdata(L
, 1);
491 luaL_typerror(L
, 1, "screen");
493 if(lua_gettop(L
) == 2)
495 s
->padding
= luaA_getopt_padding(L
, 2, &s
->padding
);
497 s
->need_arrange
= true;
499 /* All the wiboxes repositioned */
500 foreach(w
, s
->wiboxes
)
501 wibox_position_update(*w
);
503 ewmh_update_workarea(screen_virttophys(screen_array_indexof(&globalconf
.screens
, s
)));
505 return luaA_pushpadding(L
, &s
->padding
);
508 /** Get the screen count.
509 * \param L The Lua VM state.
510 * \return The number of elements pushed on stack.
513 * \lreturn The screen count, at least 1.
516 luaA_screen_count(lua_State
*L
)
518 lua_pushnumber(L
, globalconf
.screens
.len
);
522 const struct luaL_reg awesome_screen_methods
[] =
524 { "count", luaA_screen_count
},
525 { "__index", luaA_screen_module_index
},
529 const struct luaL_reg awesome_screen_meta
[] =
531 { "tags", luaA_screen_tags
},
532 { "padding", luaA_screen_padding
},
533 { "__index", luaA_screen_index
},
537 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80