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>
26 #include <xcb/randr.h>
30 #include "objects/tag.h"
31 #include "objects/client.h"
32 #include "objects/drawin.h"
34 #include "common/xutil.h"
36 struct screen_output_t
38 /** The XRandR names of the output */
40 /** The size in millimeters */
41 uint32_t mm_width
, mm_height
;
44 ARRAY_FUNCS(screen_output_t
, screen_output
, DO_NOTHING
)
47 screen_xsitoarea(xcb_xinerama_screen_info_t si
)
60 screen_add(screen_t new_screen
)
62 foreach(screen_to_test
, globalconf
.screens
)
63 if(new_screen
.geometry
.x
== screen_to_test
->geometry
.x
64 && new_screen
.geometry
.y
== screen_to_test
->geometry
.y
)
66 /* we already have a screen for this area, just check if
67 * it's not bigger and drop it */
68 screen_to_test
->geometry
.width
=
69 MAX(new_screen
.geometry
.width
, screen_to_test
->geometry
.width
);
70 screen_to_test
->geometry
.height
=
71 MAX(new_screen
.geometry
.height
, screen_to_test
->geometry
.height
);
74 signal_add(&new_screen
.signals
, "property::workarea");
75 screen_array_append(&globalconf
.screens
, new_screen
);
81 return globalconf
.screens
.len
> 0;
85 screen_scan_randr(void)
87 /* Check for extension before checking for XRandR */
88 if(xcb_get_extension_data(globalconf
.connection
, &xcb_randr_id
)->present
)
90 xcb_randr_query_version_reply_t
*version_reply
=
91 xcb_randr_query_version_reply(globalconf
.connection
,
92 xcb_randr_query_version(globalconf
.connection
, 1, 1), 0);
95 p_delete(&version_reply
);
97 /* A quick XRandR recall:
98 * You have CRTC that manages a part of a SCREEN.
99 * Each CRTC can draw stuff on one or more OUTPUT. */
100 xcb_randr_get_screen_resources_cookie_t screen_res_c
= xcb_randr_get_screen_resources(globalconf
.connection
, globalconf
.screen
->root
);
101 xcb_randr_get_screen_resources_reply_t
*screen_res_r
= xcb_randr_get_screen_resources_reply(globalconf
.connection
, screen_res_c
, NULL
);
103 /* Only use the data from XRandR if there is more than one screen
104 * defined. This should work around the broken nvidia driver. */
105 if (screen_res_r
->num_crtcs
<= 1)
107 p_delete(&screen_res_r
);
111 /* We go through CRTC, and build a screen for each one. */
112 xcb_randr_crtc_t
*randr_crtcs
= xcb_randr_get_screen_resources_crtcs(screen_res_r
);
114 for(int i
= 0; i
< screen_res_r
->num_crtcs
; i
++)
116 /* Get info on the output crtc */
117 xcb_randr_get_crtc_info_cookie_t crtc_info_c
= xcb_randr_get_crtc_info(globalconf
.connection
, randr_crtcs
[i
], XCB_CURRENT_TIME
);
118 xcb_randr_get_crtc_info_reply_t
*crtc_info_r
= xcb_randr_get_crtc_info_reply(globalconf
.connection
, crtc_info_c
, NULL
);
120 /* If CRTC has no OUTPUT, ignore it */
121 if(!xcb_randr_get_crtc_info_outputs_length(crtc_info_r
))
124 /* Prepare the new screen */
126 p_clear(&new_screen
, 1);
127 new_screen
.geometry
.x
= crtc_info_r
->x
;
128 new_screen
.geometry
.y
= crtc_info_r
->y
;
129 new_screen
.geometry
.width
= crtc_info_r
->width
;
130 new_screen
.geometry
.height
= crtc_info_r
->height
;
132 xcb_randr_output_t
*randr_outputs
= xcb_randr_get_crtc_info_outputs(crtc_info_r
);
134 for(int j
= 0; j
< xcb_randr_get_crtc_info_outputs_length(crtc_info_r
); j
++)
136 xcb_randr_get_output_info_cookie_t output_info_c
= xcb_randr_get_output_info(globalconf
.connection
, randr_outputs
[j
], XCB_CURRENT_TIME
);
137 xcb_randr_get_output_info_reply_t
*output_info_r
= xcb_randr_get_output_info_reply(globalconf
.connection
, output_info_c
, NULL
);
139 int len
= xcb_randr_get_output_info_name_length(output_info_r
);
140 /* name is not NULL terminated */
141 char *name
= memcpy(p_new(char *, len
+ 1), xcb_randr_get_output_info_name(output_info_r
), len
);
144 screen_output_array_append(&new_screen
.outputs
,
145 (screen_output_t
) { .name
= name
,
146 .mm_width
= output_info_r
->mm_width
,
147 .mm_height
= output_info_r
->mm_height
});
149 p_delete(&output_info_r
);
152 screen_add(new_screen
);
154 p_delete(&crtc_info_r
);
157 p_delete(&screen_res_r
);
159 return screens_exist();
167 screen_scan_xinerama(void)
169 bool xinerama_is_active
= false;
171 /* Check for extension before checking for Xinerama */
172 if(xcb_get_extension_data(globalconf
.connection
, &xcb_xinerama_id
)->present
)
174 xcb_xinerama_is_active_reply_t
*xia
;
175 xia
= xcb_xinerama_is_active_reply(globalconf
.connection
, xcb_xinerama_is_active(globalconf
.connection
), NULL
);
176 xinerama_is_active
= xia
->state
;
180 if(xinerama_is_active
)
182 xcb_xinerama_query_screens_reply_t
*xsq
;
183 xcb_xinerama_screen_info_t
*xsi
;
184 int xinerama_screen_number
;
186 xsq
= xcb_xinerama_query_screens_reply(globalconf
.connection
,
187 xcb_xinerama_query_screens_unchecked(globalconf
.connection
),
190 xsi
= xcb_xinerama_query_screens_screen_info(xsq
);
191 xinerama_screen_number
= xcb_xinerama_query_screens_screen_info_length(xsq
);
193 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
194 for(int screen
= 0; screen
< xinerama_screen_number
; screen
++)
198 s
.geometry
= screen_xsitoarea(xsi
[screen
]);
204 return screens_exist();
210 static void screen_scan_x11(void)
212 /* One screen only / Zaphod mode */
213 xcb_screen_t
*xcb_screen
= globalconf
.screen
;
218 s
.geometry
.width
= xcb_screen
->width_in_pixels
;
219 s
.geometry
.height
= xcb_screen
->height_in_pixels
;
223 /** Get screens informations and fill global configuration.
228 if(!screen_scan_randr() && !screen_scan_xinerama())
232 /** Return the Xinerama screen number where the coordinates belongs to.
233 * \param screen The logical screen number.
234 * \param x X coordinate
235 * \param y Y coordinate
236 * \return Screen pointer or screen param if no match or no multi-head.
239 screen_getbycoord(int x
, int y
)
241 foreach(s
, globalconf
.screens
)
242 if((x
< 0 || (x
>= s
->geometry
.x
&& x
< s
->geometry
.x
+ s
->geometry
.width
))
243 && (y
< 0 || (y
>= s
->geometry
.y
&& y
< s
->geometry
.y
+ s
->geometry
.height
)))
246 /* No screen found, let's be creative. */
247 return &globalconf
.screens
.tab
[0];
250 /** Get screens info.
251 * \param screen Screen.
252 * \param strut Honor windows strut.
253 * \return The screen area.
256 screen_area_get(screen_t
*screen
, bool strut
)
259 return screen
->geometry
;
261 area_t area
= screen
->geometry
;
262 uint16_t top
= 0, bottom
= 0, left
= 0, right
= 0;
264 #define COMPUTE_STRUT(o) \
266 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
269 top = MAX(top, (o)->strut.top); \
271 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
273 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
275 if((o)->strut.bottom) \
276 bottom = MAX(bottom, (o)->strut.bottom); \
278 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
280 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
282 if((o)->strut.left) \
283 left = MAX(left, (o)->strut.left); \
285 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
287 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
289 if((o)->strut.right) \
290 right = MAX(right, (o)->strut.right); \
292 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
296 foreach(c
, globalconf
.clients
)
297 if((*c
)->screen
== screen
&& client_isvisible(*c
))
300 foreach(drawin
, globalconf
.drawins
)
301 if((*drawin
)->visible
)
304 screen_getbycoord((*drawin
)->geometry
.x
, (*drawin
)->geometry
.y
);
305 if (d_screen
== screen
)
306 COMPUTE_STRUT(*drawin
)
313 area
.width
-= left
+ right
;
314 area
.height
-= top
+ bottom
;
319 /** Get display info.
320 * \return The display area.
323 display_area_get(void)
325 xcb_screen_t
*s
= globalconf
.screen
;
326 area_t area
= { .x
= 0,
328 .width
= s
->width_in_pixels
,
329 .height
= s
->height_in_pixels
};
333 /** Move a client to a virtual screen.
334 * \param c The client to move.
335 * \param new_screen The destination screen.
336 * \param doresize Set to true if we also move the client to the new x and
337 * y of the new screen.
340 screen_client_moveto(client_t
*c
, screen_t
*new_screen
, bool doresize
)
342 screen_t
*old_screen
= c
->screen
;
344 bool had_focus
= false;
346 if(new_screen
== c
->screen
)
349 if (globalconf
.focus
.client
== c
)
352 c
->screen
= new_screen
;
356 luaA_object_push(globalconf
.L
, c
);
357 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
358 lua_pop(globalconf
.L
, 1);
364 from
= screen_area_get(old_screen
, false);
365 to
= screen_area_get(c
->screen
, false);
367 area_t new_geometry
= c
->geometry
;
369 new_geometry
.x
= to
.x
+ new_geometry
.x
- from
.x
;
370 new_geometry
.y
= to
.y
+ new_geometry
.y
- from
.y
;
372 /* resize the client if it doesn't fit the new screen */
373 if(new_geometry
.width
> to
.width
)
374 new_geometry
.width
= to
.width
;
375 if(new_geometry
.height
> to
.height
)
376 new_geometry
.height
= to
.height
;
378 /* make sure the client is still on the screen */
379 if(new_geometry
.x
+ new_geometry
.width
> to
.x
+ to
.width
)
380 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
;
381 if(new_geometry
.y
+ new_geometry
.height
> to
.y
+ to
.height
)
382 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
;
384 /* move / resize the client */
385 client_resize(c
, new_geometry
, false);
386 luaA_object_push(globalconf
.L
, c
);
387 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
388 lua_pop(globalconf
.L
, 1);
393 /** Push a screen onto the stack.
394 * \param L The Lua VM state.
395 * \param s The screen to push.
396 * \return The number of elements pushed on stack.
399 luaA_pushscreen(lua_State
*L
, screen_t
*s
)
401 screen_t
**ps
= lua_newuserdata(L
, sizeof(*ps
));
403 luaL_getmetatable(L
, "screen");
404 lua_setmetatable(L
, -2);
409 * \param L The Lua VM state.
410 * \return The number of elements pushed on stack.
412 * \lfield number The screen number, to get a screen.
415 luaA_screen_module_index(lua_State
*L
)
419 if((name
= lua_tostring(L
, 2)))
420 foreach(screen
, globalconf
.screens
)
421 foreach(output
, screen
->outputs
)
422 if(A_STREQ(output
->name
, name
))
423 return luaA_pushscreen(L
, screen
);
425 int screen
= luaL_checknumber(L
, 2) - 1;
426 luaA_checkscreen(screen
);
427 return luaA_pushscreen(L
, &globalconf
.screens
.tab
[screen
]);
431 * \param L The Lua VM state.
432 * \return The number of elements pushed on stack.
434 * \lfield coords The screen coordinates. Immutable.
435 * \lfield workarea The screen workarea.
438 luaA_screen_index(lua_State
*L
)
444 /* Get metatable of the screen. */
445 lua_getmetatable(L
, 1);
449 /* Do we have a field like that? */
450 if(!lua_isnil(L
, -1))
452 /* Yes, so return it! */
456 /* No, so remove everything. */
459 buf
= luaL_checkstring(L
, 2);
460 ps
= luaL_checkudata(L
, 1, "screen");
463 if(A_STREQ(buf
, "index"))
465 lua_pushinteger(L
, screen_array_indexof(&globalconf
.screens
, s
) + 1);
469 if(A_STREQ(buf
, "geometry"))
471 luaA_pusharea(L
, s
->geometry
);
475 if(A_STREQ(buf
, "workarea"))
477 luaA_pusharea(L
, screen_area_get(s
, true));
481 if(A_STREQ(buf
, "outputs"))
483 lua_createtable(L
, 0, s
->outputs
.len
);
484 foreach(output
, s
->outputs
)
486 lua_createtable(L
, 2, 0);
488 lua_pushinteger(L
, output
->mm_width
);
489 lua_setfield(L
, -2, "mm_width");
490 lua_pushinteger(L
, output
->mm_height
);
491 lua_setfield(L
, -2, "mm_height");
493 lua_setfield(L
, -2, output
->name
);
495 /* The table of tables we created. */
503 * \param L The Lua VM state.
504 * \return The number of elements pushed on stack.
507 luaA_screen_equal(lua_State
*L
)
512 ps1
= luaL_checkudata(L
, 1, "screen");
513 ps2
= luaL_checkudata(L
, 2, "screen");
514 lua_pushboolean(L
, *ps1
== *ps2
);
519 /** Add a signal to a screen.
520 * \param L The Lua VM state.
521 * \return The number of elements pushed on stack.
524 * \lparam A signal name.
527 luaA_screen_add_signal(lua_State
*L
)
529 screen_t
**ps
= luaL_checkudata(L
, 1, "screen");
531 const char *name
= luaL_checkstring(L
, 2);
532 signal_add(&s
->signals
, name
);
536 /** Connect a screen's signal.
537 * \param L The Lua VM state.
538 * \return The number of elements pushed on stack.
541 * \lparam A signal name.
542 * \lparam A function to call when the signal is emitted.
545 luaA_screen_connect_signal(lua_State
*L
)
547 screen_t
**ps
= luaL_checkudata(L
, 1, "screen");
549 const char *name
= luaL_checkstring(L
, 2);
550 luaA_checkfunction(L
, 3);
551 signal_connect(&s
->signals
, name
, luaA_object_ref(L
, 3));
555 /** Remove a signal to a screen.
556 * \param L The Lua VM state.
557 * \return The number of elements pushed on stack.
560 * \lparam A signal name.
561 * \lparam A function to remove
564 luaA_screen_disconnect_signal(lua_State
*L
)
566 screen_t
**ps
= luaL_checkudata(L
, 1, "screen");
568 const char *name
= luaL_checkstring(L
, 2);
569 luaA_checkfunction(L
, 3);
570 const void *ref
= lua_topointer(L
, 3);
571 signal_disconnect(&s
->signals
, name
, ref
);
572 luaA_object_unref(L
, (void *) ref
);
576 /** Emit a signal to a screen.
577 * \param L The Lua VM state.
578 * \param screen The screen.
579 * \param name The signal name.
580 * \param nargs The number of arguments to the signal function.
583 screen_emit_signal(lua_State
*L
, screen_t
*screen
, const char *name
, int nargs
)
585 luaA_pushscreen(L
, screen
);
586 lua_insert(L
, - nargs
- 1);
587 signal_object_emit(L
, &screen
->signals
, name
, nargs
+ 1);
590 /** Emit a signal to a screen.
591 * \param L The Lua VM state.
592 * \return The number of elements pushed on stack.
595 * \lparam A signal name.
596 * \lparam Various arguments, optional.
599 luaA_screen_emit_signal(lua_State
*L
)
601 screen_t
**ps
= luaL_checkudata(L
, 1, "screen");
602 screen_emit_signal(L
, *ps
, luaL_checkstring(L
, 2), lua_gettop(L
) - 2);
606 /** Get the screen count.
607 * \param L The Lua VM state.
608 * \return The number of elements pushed on stack.
611 * \lreturn The screen count, at least 1.
614 luaA_screen_count(lua_State
*L
)
616 lua_pushnumber(L
, globalconf
.screens
.len
);
620 const struct luaL_Reg awesome_screen_methods
[] =
622 { "count", luaA_screen_count
},
623 { "__index", luaA_screen_module_index
},
627 const struct luaL_Reg awesome_screen_meta
[] =
629 { "add_signal", luaA_screen_add_signal
},
630 { "connect_signal", luaA_screen_connect_signal
},
631 { "disconnect_signal", luaA_screen_disconnect_signal
},
632 { "emit_signal", luaA_screen_emit_signal
},
633 { "__index", luaA_screen_index
},
634 { "__eq", luaA_screen_equal
},
638 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80