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.
22 #include "objects/screen.h"
24 #include "objects/client.h"
25 #include "objects/drawin.h"
30 #include <xcb/xinerama.h>
31 #include <xcb/randr.h>
33 struct screen_output_t
35 /** The XRandR names of the output */
37 /** The size in millimeters */
38 uint32_t mm_width
, mm_height
;
42 screen_output_wipe(screen_output_t
*output
)
44 p_delete(&output
->name
);
47 ARRAY_FUNCS(screen_output_t
, screen_output
, screen_output_wipe
)
49 static lua_class_t screen_class
;
50 LUA_OBJECT_FUNCS(screen_class
, screen_t
, screen
)
52 /** Collect a screen. */
54 screen_wipe(screen_t
*s
)
56 screen_output_array_wipe(&s
->outputs
);
59 /** Get a screen argument from the lua stack */
61 luaA_checkscreen(lua_State
*L
, int sidx
)
63 if (lua_isnumber(L
, sidx
))
65 int screen
= lua_tointeger(L
, sidx
);
66 if(screen
< 1 || screen
> globalconf
.screens
.len
)
67 luaL_error(L
, "invalid screen number: %d", screen
);
68 return globalconf
.screens
.tab
[screen
- 1];
70 return luaA_checkudata(L
, sidx
, &screen_class
);
74 screen_xsitoarea(xcb_xinerama_screen_info_t si
)
87 screen_add(lua_State
*L
, int sidx
)
89 screen_t
*new_screen
= luaA_checkudata(L
, sidx
, &screen_class
);
91 foreach(screen_to_test
, globalconf
.screens
)
92 if(new_screen
->geometry
.x
== (*screen_to_test
)->geometry
.x
93 && new_screen
->geometry
.y
== (*screen_to_test
)->geometry
.y
)
95 /* we already have a screen for this area, just check if
96 * it's not bigger and drop it */
97 (*screen_to_test
)->geometry
.width
=
98 MAX(new_screen
->geometry
.width
, (*screen_to_test
)->geometry
.width
);
99 (*screen_to_test
)->geometry
.height
=
100 MAX(new_screen
->geometry
.height
, (*screen_to_test
)->geometry
.height
);
105 luaA_object_ref(L
, sidx
);
106 screen_array_append(&globalconf
.screens
, new_screen
);
112 return globalconf
.screens
.len
> 0;
116 screen_scan_randr(void)
118 /* Check for extension before checking for XRandR */
119 if(xcb_get_extension_data(globalconf
.connection
, &xcb_randr_id
)->present
)
121 xcb_randr_query_version_reply_t
*version_reply
=
122 xcb_randr_query_version_reply(globalconf
.connection
,
123 xcb_randr_query_version(globalconf
.connection
, 1, 1), 0);
126 p_delete(&version_reply
);
128 /* A quick XRandR recall:
129 * You have CRTC that manages a part of a SCREEN.
130 * Each CRTC can draw stuff on one or more OUTPUT. */
131 xcb_randr_get_screen_resources_cookie_t screen_res_c
= xcb_randr_get_screen_resources(globalconf
.connection
, globalconf
.screen
->root
);
132 xcb_randr_get_screen_resources_reply_t
*screen_res_r
= xcb_randr_get_screen_resources_reply(globalconf
.connection
, screen_res_c
, NULL
);
134 /* Only use the data from XRandR if there is more than one screen
135 * defined. This should work around the broken nvidia driver. */
136 if (screen_res_r
->num_crtcs
<= 1)
138 p_delete(&screen_res_r
);
142 /* We go through CRTC, and build a screen for each one. */
143 xcb_randr_crtc_t
*randr_crtcs
= xcb_randr_get_screen_resources_crtcs(screen_res_r
);
145 for(int i
= 0; i
< screen_res_r
->num_crtcs
; i
++)
147 /* Get info on the output crtc */
148 xcb_randr_get_crtc_info_cookie_t crtc_info_c
= xcb_randr_get_crtc_info(globalconf
.connection
, randr_crtcs
[i
], XCB_CURRENT_TIME
);
149 xcb_randr_get_crtc_info_reply_t
*crtc_info_r
= xcb_randr_get_crtc_info_reply(globalconf
.connection
, crtc_info_c
, NULL
);
151 /* If CRTC has no OUTPUT, ignore it */
152 if(!xcb_randr_get_crtc_info_outputs_length(crtc_info_r
))
155 /* Prepare the new screen */
156 screen_t
*new_screen
= screen_new(globalconf
.L
);
157 new_screen
->geometry
.x
= crtc_info_r
->x
;
158 new_screen
->geometry
.y
= crtc_info_r
->y
;
159 new_screen
->geometry
.width
= crtc_info_r
->width
;
160 new_screen
->geometry
.height
= crtc_info_r
->height
;
162 xcb_randr_output_t
*randr_outputs
= xcb_randr_get_crtc_info_outputs(crtc_info_r
);
164 for(int j
= 0; j
< xcb_randr_get_crtc_info_outputs_length(crtc_info_r
); j
++)
166 xcb_randr_get_output_info_cookie_t output_info_c
= xcb_randr_get_output_info(globalconf
.connection
, randr_outputs
[j
], XCB_CURRENT_TIME
);
167 xcb_randr_get_output_info_reply_t
*output_info_r
= xcb_randr_get_output_info_reply(globalconf
.connection
, output_info_c
, NULL
);
169 int len
= xcb_randr_get_output_info_name_length(output_info_r
);
170 /* name is not NULL terminated */
171 char *name
= memcpy(p_new(char *, len
+ 1), xcb_randr_get_output_info_name(output_info_r
), len
);
174 screen_output_array_append(&new_screen
->outputs
,
175 (screen_output_t
) { .name
= name
,
176 .mm_width
= output_info_r
->mm_width
,
177 .mm_height
= output_info_r
->mm_height
});
179 p_delete(&output_info_r
);
182 screen_add(globalconf
.L
, -1);
184 p_delete(&crtc_info_r
);
187 p_delete(&screen_res_r
);
189 return screens_exist();
197 screen_scan_xinerama(void)
199 bool xinerama_is_active
= false;
201 /* Check for extension before checking for Xinerama */
202 if(xcb_get_extension_data(globalconf
.connection
, &xcb_xinerama_id
)->present
)
204 xcb_xinerama_is_active_reply_t
*xia
;
205 xia
= xcb_xinerama_is_active_reply(globalconf
.connection
, xcb_xinerama_is_active(globalconf
.connection
), NULL
);
206 xinerama_is_active
= xia
->state
;
210 if(xinerama_is_active
)
212 xcb_xinerama_query_screens_reply_t
*xsq
;
213 xcb_xinerama_screen_info_t
*xsi
;
214 int xinerama_screen_number
;
216 xsq
= xcb_xinerama_query_screens_reply(globalconf
.connection
,
217 xcb_xinerama_query_screens_unchecked(globalconf
.connection
),
220 xsi
= xcb_xinerama_query_screens_screen_info(xsq
);
221 xinerama_screen_number
= xcb_xinerama_query_screens_screen_info_length(xsq
);
223 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
224 for(int screen
= 0; screen
< xinerama_screen_number
; screen
++)
226 screen_t
*s
= screen_new(globalconf
.L
);
227 s
->geometry
= screen_xsitoarea(xsi
[screen
]);
228 screen_add(globalconf
.L
, -1);
233 return screens_exist();
239 static void screen_scan_x11(void)
241 /* One screen only / Zaphod mode */
242 xcb_screen_t
*xcb_screen
= globalconf
.screen
;
243 screen_t
*s
= screen_new(globalconf
.L
);
246 s
->geometry
.width
= xcb_screen
->width_in_pixels
;
247 s
->geometry
.height
= xcb_screen
->height_in_pixels
;
248 screen_add(globalconf
.L
, -1);
251 /** Get screens informations and fill global configuration.
256 if(!screen_scan_randr() && !screen_scan_xinerama())
260 /** Return the Xinerama screen number where the coordinates belongs to.
261 * \param screen The logical screen number.
262 * \param x X coordinate
263 * \param y Y coordinate
264 * \return Screen pointer or screen param if no match or no multi-head.
267 screen_getbycoord(int x
, int y
)
269 foreach(s
, globalconf
.screens
)
270 if((x
< 0 || (x
>= (*s
)->geometry
.x
&& x
< (*s
)->geometry
.x
+ (*s
)->geometry
.width
))
271 && (y
< 0 || (y
>= (*s
)->geometry
.y
&& y
< (*s
)->geometry
.y
+ (*s
)->geometry
.height
)))
274 /* No screen found, let's be creative. */
275 return globalconf
.screens
.tab
[0];
278 /** Get screens info.
279 * \param screen Screen.
280 * \param strut Honor windows strut.
281 * \return The screen area.
284 screen_area_get(screen_t
*screen
, bool strut
)
287 return screen
->geometry
;
289 area_t area
= screen
->geometry
;
290 uint16_t top
= 0, bottom
= 0, left
= 0, right
= 0;
292 #define COMPUTE_STRUT(o) \
294 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
297 top = MAX(top, (o)->strut.top); \
299 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
301 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
303 if((o)->strut.bottom) \
304 bottom = MAX(bottom, (o)->strut.bottom); \
306 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
308 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
310 if((o)->strut.left) \
311 left = MAX(left, (o)->strut.left); \
313 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
315 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
317 if((o)->strut.right) \
318 right = MAX(right, (o)->strut.right); \
320 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
324 foreach(c
, globalconf
.clients
)
325 if((*c
)->screen
== screen
&& client_isvisible(*c
))
328 foreach(drawin
, globalconf
.drawins
)
329 if((*drawin
)->visible
)
332 screen_getbycoord((*drawin
)->geometry
.x
, (*drawin
)->geometry
.y
);
333 if (d_screen
== screen
)
334 COMPUTE_STRUT(*drawin
)
341 area
.width
-= left
+ right
;
342 area
.height
-= top
+ bottom
;
347 /** Get display info.
348 * \return The display area.
351 display_area_get(void)
353 xcb_screen_t
*s
= globalconf
.screen
;
354 area_t area
= { .x
= 0,
356 .width
= s
->width_in_pixels
,
357 .height
= s
->height_in_pixels
};
361 /** Move a client to a virtual screen.
362 * \param c The client to move.
363 * \param new_screen The destination screen.
364 * \param doresize Set to true if we also move the client to the new x and
365 * y of the new screen.
368 screen_client_moveto(client_t
*c
, screen_t
*new_screen
, bool doresize
)
370 screen_t
*old_screen
= c
->screen
;
372 bool had_focus
= false;
374 if(new_screen
== c
->screen
)
377 if (globalconf
.focus
.client
== c
)
380 c
->screen
= new_screen
;
384 luaA_object_push(globalconf
.L
, c
);
385 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
386 lua_pop(globalconf
.L
, 1);
392 from
= screen_area_get(old_screen
, false);
393 to
= screen_area_get(c
->screen
, false);
395 area_t new_geometry
= c
->geometry
;
397 new_geometry
.x
= to
.x
+ new_geometry
.x
- from
.x
;
398 new_geometry
.y
= to
.y
+ new_geometry
.y
- from
.y
;
400 /* resize the client if it doesn't fit the new screen */
401 if(new_geometry
.width
> to
.width
)
402 new_geometry
.width
= to
.width
;
403 if(new_geometry
.height
> to
.height
)
404 new_geometry
.height
= to
.height
;
406 /* make sure the client is still on the screen */
407 if(new_geometry
.x
+ new_geometry
.width
> to
.x
+ to
.width
)
408 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
;
409 if(new_geometry
.y
+ new_geometry
.height
> to
.y
+ to
.height
)
410 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
;
412 /* move / resize the client */
413 client_resize(c
, new_geometry
, false);
414 luaA_object_push(globalconf
.L
, c
);
415 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
416 lua_pop(globalconf
.L
, 1);
421 /** Get a screen's index. */
423 screen_get_index(screen_t
*s
)
426 foreach(screen
, globalconf
.screens
)
437 * \param L The Lua VM state.
438 * \return The number of elements pushed on stack.
440 * \lfield number The screen number, to get a screen.
443 luaA_screen_module_index(lua_State
*L
)
447 if((name
= lua_tostring(L
, 2)))
448 foreach(screen
, globalconf
.screens
)
449 foreach(output
, (*screen
)->outputs
)
450 if(A_STREQ(output
->name
, name
))
451 return luaA_object_push(L
, screen
);
453 return luaA_object_push(L
, luaA_checkscreen(L
, 2));
456 LUA_OBJECT_EXPORT_PROPERTY(screen
, screen_t
, geometry
, luaA_pusharea
)
459 luaA_screen_get_index(lua_State
*L
, screen_t
*s
)
461 lua_pushinteger(L
, screen_get_index(s
));
466 luaA_screen_get_outputs(lua_State
*L
, screen_t
*s
)
468 lua_createtable(L
, 0, s
->outputs
.len
);
469 foreach(output
, s
->outputs
)
471 lua_createtable(L
, 2, 0);
473 lua_pushinteger(L
, output
->mm_width
);
474 lua_setfield(L
, -2, "mm_width");
475 lua_pushinteger(L
, output
->mm_height
);
476 lua_setfield(L
, -2, "mm_height");
478 lua_setfield(L
, -2, output
->name
);
480 /* The table of tables we created. */
485 luaA_screen_get_workarea(lua_State
*L
, screen_t
*s
)
487 luaA_pusharea(L
, screen_area_get(s
, true));
491 /** Get the screen count.
492 * \param L The Lua VM state.
493 * \return The number of elements pushed on stack.
496 * \lreturn The screen count, at least 1.
499 luaA_screen_count(lua_State
*L
)
501 lua_pushnumber(L
, globalconf
.screens
.len
);
506 screen_class_setup(lua_State
*L
)
508 static const struct luaL_Reg screen_methods
[] =
510 LUA_CLASS_METHODS(screen
)
511 { "count", luaA_screen_count
},
512 { "__index", luaA_screen_module_index
},
513 { "__newindex", luaA_default_newindex
},
517 static const struct luaL_Reg screen_meta
[] =
519 LUA_OBJECT_META(screen
)
524 luaA_class_setup(L
, &screen_class
, "screen", NULL
,
525 (lua_class_allocator_t
) screen_new
,
526 (lua_class_collector_t
) screen_wipe
,
528 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
529 screen_methods
, screen_meta
);
530 luaA_class_add_property(&screen_class
, "geometry",
532 (lua_class_propfunc_t
) luaA_screen_get_geometry
,
534 luaA_class_add_property(&screen_class
, "index",
536 (lua_class_propfunc_t
) luaA_screen_get_index
,
538 luaA_class_add_property(&screen_class
, "outputs",
540 (lua_class_propfunc_t
) luaA_screen_get_outputs
,
542 luaA_class_add_property(&screen_class
, "workarea",
544 (lua_class_propfunc_t
) luaA_screen_get_workarea
,
546 signal_add(&screen_class
.signals
, "property::workarea");
549 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80