2 * screen.c - screen management
4 * Copyright © 2007-2009 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>
34 #include "common/xutil.h"
37 screen_xsitoarea(xcb_xinerama_screen_info_t si
)
49 static xcb_visualtype_t
*
50 screen_default_visual(xcb_screen_t
*s
)
52 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
55 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
56 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator(depth_iter
.data
);
57 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
58 if(s
->root_visual
== visual_iter
.data
->visual_id
)
59 return visual_iter
.data
;
64 /** Get screens informations and fill global configuration.
69 /* Check for extension before checking for Xinerama */
70 if(xcb_get_extension_data(globalconf
.connection
, &xcb_xinerama_id
)->present
)
72 xcb_xinerama_is_active_reply_t
*xia
;
73 xia
= xcb_xinerama_is_active_reply(globalconf
.connection
, xcb_xinerama_is_active(globalconf
.connection
), NULL
);
74 globalconf
.xinerama_is_active
= xia
->state
;
78 if(globalconf
.xinerama_is_active
)
80 xcb_xinerama_query_screens_reply_t
*xsq
;
81 xcb_xinerama_screen_info_t
*xsi
;
82 int xinerama_screen_number
;
84 xsq
= xcb_xinerama_query_screens_reply(globalconf
.connection
,
85 xcb_xinerama_query_screens_unchecked(globalconf
.connection
),
88 xsi
= xcb_xinerama_query_screens_screen_info(xsq
);
89 xinerama_screen_number
= xcb_xinerama_query_screens_screen_info_length(xsq
);
91 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
92 for(int screen
= 0; screen
< xinerama_screen_number
; screen
++)
95 foreach(screen_to_test
, globalconf
.screens
)
96 if(xsi
[screen
].x_org
== screen_to_test
->geometry
.x
97 && xsi
[screen
].y_org
== screen_to_test
->geometry
.y
)
99 /* we already have a screen for this area, just check if
100 * it's not bigger and drop it */
102 int i
= screen_array_indexof(&globalconf
.screens
, screen_to_test
);
103 screen_to_test
->geometry
.width
=
104 MAX(xsi
[screen
].width
, xsi
[i
].width
);
105 screen_to_test
->geometry
.height
=
106 MAX(xsi
[screen
].height
, xsi
[i
].height
);
112 s
.geometry
= screen_xsitoarea(xsi
[screen
]);
113 screen_array_append(&globalconf
.screens
, s
);
119 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
120 globalconf
.screens
.tab
[0].visual
= screen_default_visual(s
);
123 /* One screen only / Zaphod mode */
125 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
128 xcb_screen_t
*xcb_screen
= xutil_screen_get(globalconf
.connection
, screen
);
133 s
.geometry
.width
= xcb_screen
->width_in_pixels
;
134 s
.geometry
.height
= xcb_screen
->height_in_pixels
;
135 s
.visual
= screen_default_visual(xcb_screen
);
136 screen_array_append(&globalconf
.screens
, s
);
139 globalconf
.screen_focus
= globalconf
.screens
.tab
;
142 /** Return the Xinerama screen number where the coordinates belongs to.
143 * \param screen The logical screen number.
144 * \param x X coordinate
145 * \param y Y coordinate
146 * \return Screen pointer or screen param if no match or no multi-head.
149 screen_getbycoord(screen_t
*screen
, int x
, int y
)
151 /* don't waste our time */
152 if(!globalconf
.xinerama_is_active
)
155 foreach(s
, globalconf
.screens
)
156 if((x
< 0 || (x
>= s
->geometry
.x
&& x
< s
->geometry
.x
+ s
->geometry
.width
))
157 && (y
< 0 || (y
>= s
->geometry
.y
&& y
< s
->geometry
.y
+ s
->geometry
.height
)))
163 /** Get screens info.
164 * \param screen Screen.
165 * \param strut Honor windows strut.
166 * \return The screen area.
169 screen_area_get(screen_t
*screen
, bool strut
)
171 area_t area
= screen
->geometry
;
172 uint16_t top
= 0, bottom
= 0, left
= 0, right
= 0;
175 foreach(_c
, globalconf
.clients
)
178 if(client_isvisible(c
, screen
))
180 if(c
->strut
.top_start_x
|| c
->strut
.top_end_x
)
183 top
= MAX(top
, c
->strut
.top
);
185 top
= MAX(top
, (c
->geometry
.y
- area
.y
) + c
->geometry
.height
);
187 if(c
->strut
.bottom_start_x
|| c
->strut
.bottom_end_x
)
190 bottom
= MAX(bottom
, c
->strut
.bottom
);
192 bottom
= MAX(bottom
, (area
.y
+ area
.height
) - c
->geometry
.y
);
194 if(c
->strut
.left_start_y
|| c
->strut
.left_end_y
)
197 left
= MAX(left
, c
->strut
.left
);
199 left
= MAX(left
, (c
->geometry
.x
- area
.x
) + c
->geometry
.width
);
201 if(c
->strut
.right_start_y
|| c
->strut
.right_end_y
)
204 right
= MAX(right
, c
->strut
.right
);
206 right
= MAX(right
, (area
.x
+ area
.width
) - c
->geometry
.x
);
213 area
.width
-= left
+ right
;
214 area
.height
-= top
+ bottom
;
219 /** Get display info.
220 * \param phys_screen Physical screen number.
221 * \return The display area.
224 display_area_get(int phys_screen
)
226 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
227 area_t area
= { .x
= 0,
229 .width
= s
->width_in_pixels
,
230 .height
= s
->height_in_pixels
};
234 /** This returns the real X screen number for a logical
235 * screen if Xinerama is active.
236 * \param screen The logical screen.
237 * \return The X screen.
240 screen_virttophys(int screen
)
242 if(globalconf
.xinerama_is_active
)
243 return globalconf
.default_screen
;
247 /** Move a client to a virtual screen.
248 * \param c The client to move.
249 * \param new_screen The destinatiuon screen.
250 * \param dotag Set to true if we also change tags.
251 * \param doresize Set to true if we also move the client to the new x and
252 * y of the new screen.
255 screen_client_moveto(client_t
*c
, screen_t
*new_screen
, bool dotag
, bool doresize
)
257 screen_t
*old_screen
= c
->screen
;
260 if(new_screen
== c
->screen
)
263 c
->screen
= new_screen
;
266 c
->titlebar
->screen
= new_screen
;
270 /* remove old tags */
271 foreach(old_tag
, old_screen
->tags
)
272 untag_client(c
, *old_tag
);
274 /* \todo move this to Lua */
277 foreach(new_tag
, new_screen
->tags
)
278 if(tag_get_selected(*new_tag
))
280 luaA_object_push(globalconf
.L
, *new_tag
);
287 hook_property(c
, "screen");
291 from
= screen_area_get(old_screen
, false);
292 to
= screen_area_get(c
->screen
, false);
294 area_t new_geometry
= c
->geometry
;
299 area_t new_f_geometry
= c
->geometries
.fullscreen
;
301 new_f_geometry
.x
= to
.x
+ new_f_geometry
.x
- from
.x
;
302 new_f_geometry
.y
= to
.y
+ new_f_geometry
.y
- from
.x
;
304 /* resize the client's original geometry if it doesn't fit the screen */
305 if(new_f_geometry
.width
> to
.width
)
306 new_f_geometry
.width
= to
.width
;
307 if(new_f_geometry
.height
> to
.height
)
308 new_f_geometry
.height
= to
.height
;
310 /* make sure the client is still on the screen */
311 if(new_f_geometry
.x
+ new_f_geometry
.width
> to
.x
+ to
.width
)
312 new_f_geometry
.x
= to
.x
+ to
.width
- new_f_geometry
.width
;
313 if(new_f_geometry
.y
+ new_f_geometry
.height
> to
.y
+ to
.height
)
314 new_f_geometry
.y
= to
.y
+ to
.height
- new_f_geometry
.height
;
316 c
->geometries
.fullscreen
= new_f_geometry
;
320 new_geometry
.x
= to
.x
+ new_geometry
.x
- from
.x
;
321 new_geometry
.y
= to
.y
+ new_geometry
.y
- from
.y
;
323 /* resize the client if it doesn't fit the new screen */
324 if(new_geometry
.width
> to
.width
)
325 new_geometry
.width
= to
.width
;
326 if(new_geometry
.height
> to
.height
)
327 new_geometry
.height
= to
.height
;
329 /* make sure the client is still on the screen */
330 if(new_geometry
.x
+ new_geometry
.width
> to
.x
+ to
.width
)
331 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
;
332 if(new_geometry
.y
+ new_geometry
.height
> to
.y
+ to
.height
)
333 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
;
335 /* move / resize the client */
336 client_resize(c
, new_geometry
, false);
337 hook_property(c
, "screen");
340 /** Push a screen onto the stack.
341 * \param L The Lua VM state.
342 * \param s The scren to push.
343 * \return The number of elements pushed on stack.
346 luaA_pushscreen(lua_State
*L
, screen_t
*s
)
348 lua_pushlightuserdata(L
, s
);
349 luaL_getmetatable(L
, "screen");
350 lua_setmetatable(L
, -2);
355 * \param L The Lua VM state.
356 * \return The number of elements pushed on stack.
358 * \lfield number The screen number, to get a screen.
361 luaA_screen_module_index(lua_State
*L
)
363 int screen
= luaL_checknumber(L
, 2) - 1;
364 luaA_checkscreen(screen
);
365 return luaA_pushscreen(L
, &globalconf
.screens
.tab
[screen
]);
368 /** Get or set screen tags.
369 * \param L The Lua VM state.
370 * \return The number of elements pushed on stack.
372 * \lparam None or a table of tags to set to the screen.
373 * The table must contains at least one tag.
374 * \return A table with all screen tags.
377 luaA_screen_tags(lua_State
*L
)
380 screen_t
*s
= luaL_checkudata(L
, 1, "screen");
382 if(lua_gettop(L
) == 2)
384 luaA_checktable(L
, 2);
386 /* remove current tags */
387 foreach(tag
, s
->tags
)
388 tag_set_screen(*tag
, NULL
);
390 tag_array_wipe(&s
->tags
);
391 tag_array_init(&s
->tags
);
393 s
->need_reban
= true;
397 while(lua_next(L
, 2))
398 tag_append_to_screen(L
, -1, s
);
402 lua_createtable(L
, s
->tags
.len
, 0);
403 for(i
= 0; i
< s
->tags
.len
; i
++)
405 luaA_object_push(L
, s
->tags
.tab
[i
]);
406 lua_rawseti(L
, -2, i
+ 1);
414 * \param L The Lua VM state.
415 * \return The number of elements pushed on stack.
417 * \lfield coords The screen coordinates. Immutable.
418 * \lfield workarea The screen workarea.
421 luaA_screen_index(lua_State
*L
)
427 if(luaA_usemetatable(L
, 1, 2))
430 buf
= luaL_checklstring(L
, 2, &len
);
431 s
= lua_touserdata(L
, 1);
433 switch(a_tokenize(buf
, len
))
436 lua_pushinteger(L
, screen_array_indexof(&globalconf
.screens
, s
) + 1);
439 luaA_pusharea(L
, s
->geometry
);
442 luaA_pusharea(L
, screen_area_get(s
, true));
451 /** Add a signal to a screen.
452 * \param L The Lua VM state.
453 * \return The number of elements pushed on stack.
456 * \lparam A signal name.
457 * \lparam A function to call when the signal is emited.
460 luaA_screen_add_signal(lua_State
*L
)
462 screen_t
*s
= lua_touserdata(L
, 1);
463 const char *name
= luaL_checkstring(L
, 2);
464 luaA_checkfunction(L
, 3);
465 signal_add(&s
->signals
, name
, luaA_object_ref(L
, 3));
469 /** Remove a signal to a screen.
470 * \param L The Lua VM state.
471 * \return The number of elements pushed on stack.
474 * \lparam A signal name.
475 * \lparam A function to remove
478 luaA_screen_remove_signal(lua_State
*L
)
480 screen_t
*s
= lua_touserdata(L
, 1);
481 const char *name
= luaL_checkstring(L
, 2);
482 luaA_checkfunction(L
, 3);
483 const void *ref
= lua_topointer(L
, 3);
484 signal_remove(&s
->signals
, name
, ref
);
485 luaA_object_unref(L
, (void *) ref
);
489 /** Emit a signal to a screen.
490 * \param L The Lua VM state.
491 * \param screen The screen.
492 * \param name The signal name.
493 * \param nargs The number of arguments to the signal function.
496 screen_emit_signal(lua_State
*L
, screen_t
*screen
, const char *name
, int nargs
)
498 signal_t
*sigfound
= signal_array_getbyid(&screen
->signals
,
499 a_strhash((const unsigned char *) name
));
501 foreach(func
, sigfound
->sigfuncs
)
503 luaA_pushscreen(L
, screen
);
504 for(int i
= 0; i
< nargs
; i
++)
505 lua_pushvalue(L
, - nargs
- 1);
506 luaA_object_push(L
, (void *) *func
);
507 luaA_dofunction(L
, nargs
+ 1, 0);
512 /** Emit a signal to a screen.
513 * \param L The Lua VM state.
514 * \return The number of elements pushed on stack.
517 * \lparam A signal name.
518 * \lparam Various arguments, optional.
521 luaA_screen_emit_signal(lua_State
*L
)
523 screen_emit_signal(L
, lua_touserdata(L
, 1), luaL_checkstring(L
, 2), lua_gettop(L
) - 2);
527 /** Get the screen count.
528 * \param L The Lua VM state.
529 * \return The number of elements pushed on stack.
532 * \lreturn The screen count, at least 1.
535 luaA_screen_count(lua_State
*L
)
537 lua_pushnumber(L
, globalconf
.screens
.len
);
541 const struct luaL_reg awesome_screen_methods
[] =
543 { "count", luaA_screen_count
},
544 { "__index", luaA_screen_module_index
},
548 const struct luaL_reg awesome_screen_meta
[] =
550 { "add_signal", luaA_screen_add_signal
},
551 { "remove_signal", luaA_screen_remove_signal
},
552 { "emit_signal", luaA_screen_emit_signal
},
553 { "tags", luaA_screen_tags
},
554 { "__index", luaA_screen_index
},
558 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80