Add root.wallpaper() for querying the wallpaper
[awesome.git] / screen.c
blob3dc70d49c2567b14f065e273215aecef49634ea5
1 /*
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 <stdio.h>
24 #include <xcb/xcb.h>
25 #include <xcb/xinerama.h>
26 #include <xcb/randr.h>
28 #include "screen.h"
29 #include "ewmh.h"
30 #include "objects/tag.h"
31 #include "objects/client.h"
32 #include "objects/drawin.h"
33 #include "luaa.h"
34 #include "common/xutil.h"
36 struct screen_output_t
38 /** The XRandR names of the output */
39 char *name;
40 /** The size in millimeters */
41 uint32_t mm_width, mm_height;
44 ARRAY_FUNCS(screen_output_t, screen_output, DO_NOTHING)
46 static inline area_t
47 screen_xsitoarea(xcb_xinerama_screen_info_t si)
49 area_t a =
51 .x = si.x_org,
52 .y = si.y_org,
53 .width = si.width,
54 .height = si.height
56 return a;
59 static void
60 screen_add(screen_t new_screen)
62 signal_add(&new_screen.signals, "property::workarea");
63 signal_add(&new_screen.signals, "tag::attach");
64 signal_add(&new_screen.signals, "tag::detach");
65 screen_array_append(&globalconf.screens, new_screen);
68 static bool
69 screen_scan_randr(void)
71 /* Check for extension before checking for XRandR */
72 if(xcb_get_extension_data(globalconf.connection, &xcb_randr_id)->present)
74 xcb_randr_query_version_reply_t *version_reply =
75 xcb_randr_query_version_reply(globalconf.connection,
76 xcb_randr_query_version(globalconf.connection, 1, 1), 0);
77 if(version_reply)
79 p_delete(&version_reply);
81 /* A quick XRandR recall:
82 * You have CRTC that manages a part of a SCREEN.
83 * Each CRTC can draw stuff on one or more OUTPUT. */
84 xcb_randr_get_screen_resources_cookie_t screen_res_c = xcb_randr_get_screen_resources(globalconf.connection, globalconf.screen->root);
85 xcb_randr_get_screen_resources_reply_t *screen_res_r = xcb_randr_get_screen_resources_reply(globalconf.connection, screen_res_c, NULL);
87 /* Only use the data from XRandR if there is more than one screen
88 * defined. This should work around the broken nvidia driver. */
89 if (screen_res_r->num_crtcs <= 1)
91 p_delete(&screen_res_r);
92 return false;
95 /* We go through CRTC, and build a screen for each one. */
96 xcb_randr_crtc_t *randr_crtcs = xcb_randr_get_screen_resources_crtcs(screen_res_r);
98 for(int i = 0; i < screen_res_r->num_crtcs; i++)
100 /* Get info on the output crtc */
101 xcb_randr_get_crtc_info_cookie_t crtc_info_c = xcb_randr_get_crtc_info(globalconf.connection, randr_crtcs[i], XCB_CURRENT_TIME);
102 xcb_randr_get_crtc_info_reply_t *crtc_info_r = xcb_randr_get_crtc_info_reply(globalconf.connection, crtc_info_c, NULL);
104 /* If CRTC has no OUTPUT, ignore it */
105 if(!xcb_randr_get_crtc_info_outputs_length(crtc_info_r))
106 continue;
108 /* Prepare the new screen */
109 screen_t new_screen;
110 p_clear(&new_screen, 1);
111 new_screen.geometry.x = crtc_info_r->x;
112 new_screen.geometry.y = crtc_info_r->y;
113 new_screen.geometry.width= crtc_info_r->width;
114 new_screen.geometry.height= crtc_info_r->height;
116 xcb_randr_output_t *randr_outputs = xcb_randr_get_crtc_info_outputs(crtc_info_r);
118 for(int j = 0; j < xcb_randr_get_crtc_info_outputs_length(crtc_info_r); j++)
120 xcb_randr_get_output_info_cookie_t output_info_c = xcb_randr_get_output_info(globalconf.connection, randr_outputs[j], XCB_CURRENT_TIME);
121 xcb_randr_get_output_info_reply_t *output_info_r = xcb_randr_get_output_info_reply(globalconf.connection, output_info_c, NULL);
123 int len = xcb_randr_get_output_info_name_length(output_info_r);
124 /* name is not NULL terminated */
125 char *name = memcpy(p_new(char *, len + 1), xcb_randr_get_output_info_name(output_info_r), len);
126 name[len] = '\0';
128 screen_output_array_append(&new_screen.outputs,
129 (screen_output_t) { .name = name,
130 .mm_width = output_info_r->mm_width,
131 .mm_height = output_info_r->mm_height });
133 p_delete(&output_info_r);
136 screen_add(new_screen);
138 p_delete(&crtc_info_r);
141 p_delete(&screen_res_r);
143 return true;
147 return false;
150 static bool
151 screen_scan_xinerama(void)
153 bool xinerama_is_active = false;
155 /* Check for extension before checking for Xinerama */
156 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
158 xcb_xinerama_is_active_reply_t *xia;
159 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
160 xinerama_is_active = xia->state;
161 p_delete(&xia);
164 if(xinerama_is_active)
166 xcb_xinerama_query_screens_reply_t *xsq;
167 xcb_xinerama_screen_info_t *xsi;
168 int xinerama_screen_number;
170 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
171 xcb_xinerama_query_screens_unchecked(globalconf.connection),
172 NULL);
174 xsi = xcb_xinerama_query_screens_screen_info(xsq);
175 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
177 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
178 for(int screen = 0; screen < xinerama_screen_number; screen++)
180 bool drop = false;
181 foreach(screen_to_test, globalconf.screens)
182 if(xsi[screen].x_org == screen_to_test->geometry.x
183 && xsi[screen].y_org == screen_to_test->geometry.y)
185 /* we already have a screen for this area, just check if
186 * it's not bigger and drop it */
187 drop = true;
188 int i = screen_array_indexof(&globalconf.screens, screen_to_test);
189 screen_to_test->geometry.width =
190 MAX(xsi[screen].width, xsi[i].width);
191 screen_to_test->geometry.height =
192 MAX(xsi[screen].height, xsi[i].height);
194 if(!drop)
196 screen_t s;
197 p_clear(&s, 1);
198 s.geometry = screen_xsitoarea(xsi[screen]);
199 screen_add(s);
203 p_delete(&xsq);
205 return true;
208 return false;
211 static void screen_scan_x11(void)
213 /* One screen only / Zaphod mode */
214 xcb_screen_t *xcb_screen = globalconf.screen;
215 screen_t s;
216 p_clear(&s, 1);
217 s.geometry.x = 0;
218 s.geometry.y = 0;
219 s.geometry.width = xcb_screen->width_in_pixels;
220 s.geometry.height = xcb_screen->height_in_pixels;
221 screen_add(s);
224 /** Get screens informations and fill global configuration.
226 void
227 screen_scan(void)
229 if(!screen_scan_randr() && !screen_scan_xinerama())
230 screen_scan_x11();
233 /** Return the Xinerama screen number where the coordinates belongs to.
234 * \param screen The logical screen number.
235 * \param x X coordinate
236 * \param y Y coordinate
237 * \return Screen pointer or screen param if no match or no multi-head.
239 screen_t *
240 screen_getbycoord(int x, int y)
242 foreach(s, globalconf.screens)
243 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
244 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
245 return s;
247 /* No screen found, let's be creative. */
248 return &globalconf.screens.tab[0];
251 /** Get screens info.
252 * \param screen Screen.
253 * \param strut Honor windows strut.
254 * \return The screen area.
256 area_t
257 screen_area_get(screen_t *screen, bool strut)
259 if(!strut)
260 return screen->geometry;
262 area_t area = screen->geometry;
263 uint16_t top = 0, bottom = 0, left = 0, right = 0;
265 #define COMPUTE_STRUT(o) \
267 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
269 if((o)->strut.top) \
270 top = MAX(top, (o)->strut.top); \
271 else \
272 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
274 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
276 if((o)->strut.bottom) \
277 bottom = MAX(bottom, (o)->strut.bottom); \
278 else \
279 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
281 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
283 if((o)->strut.left) \
284 left = MAX(left, (o)->strut.left); \
285 else \
286 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
288 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
290 if((o)->strut.right) \
291 right = MAX(right, (o)->strut.right); \
292 else \
293 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
297 foreach(c, globalconf.clients)
298 if((*c)->screen == screen && client_isvisible(*c))
299 COMPUTE_STRUT(*c)
301 foreach(drawin, globalconf.drawins)
302 if((*drawin)->visible)
304 screen_t *d_screen =
305 screen_getbycoord((*drawin)->geometry.x, (*drawin)->geometry.y);
306 if (d_screen == screen)
307 COMPUTE_STRUT(*drawin)
310 #undef COMPUTE_STRUT
312 area.x += left;
313 area.y += top;
314 area.width -= left + right;
315 area.height -= top + bottom;
317 return area;
320 /** Get display info.
321 * \return The display area.
323 area_t
324 display_area_get(void)
326 xcb_screen_t *s = globalconf.screen;
327 area_t area = { .x = 0,
328 .y = 0,
329 .width = s->width_in_pixels,
330 .height = s->height_in_pixels };
331 return area;
334 /** Move a client to a virtual screen.
335 * \param c The client to move.
336 * \param new_screen The destination screen.
337 * \param doresize Set to true if we also move the client to the new x and
338 * y of the new screen.
340 void
341 screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize)
343 screen_t *old_screen = c->screen;
344 area_t from, to;
345 bool had_focus = false;
347 if(new_screen == c->screen)
348 return;
350 if (globalconf.focus.client == c)
351 had_focus = true;
353 c->screen = new_screen;
355 /* If client was on a screen, remove old tags */
356 if(old_screen)
357 foreach(old_tag, old_screen->tags)
358 untag_client(c, *old_tag);
360 if(!doresize)
362 luaA_object_push(globalconf.L, c);
363 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
364 lua_pop(globalconf.L, 1);
365 if(had_focus)
366 client_focus(c);
367 return;
370 from = screen_area_get(old_screen, false);
371 to = screen_area_get(c->screen, false);
373 area_t new_geometry = c->geometry;
375 new_geometry.x = to.x + new_geometry.x - from.x;
376 new_geometry.y = to.y + new_geometry.y - from.y;
378 /* resize the client if it doesn't fit the new screen */
379 if(new_geometry.width > to.width)
380 new_geometry.width = to.width;
381 if(new_geometry.height > to.height)
382 new_geometry.height = to.height;
384 /* make sure the client is still on the screen */
385 if(new_geometry.x + new_geometry.width > to.x + to.width)
386 new_geometry.x = to.x + to.width - new_geometry.width;
387 if(new_geometry.y + new_geometry.height > to.y + to.height)
388 new_geometry.y = to.y + to.height - new_geometry.height;
390 /* move / resize the client */
391 client_resize(c, new_geometry);
392 luaA_object_push(globalconf.L, c);
393 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
394 lua_pop(globalconf.L, 1);
395 if(had_focus)
396 client_focus(c);
399 /** Push a screen onto the stack.
400 * \param L The Lua VM state.
401 * \param s The screen to push.
402 * \return The number of elements pushed on stack.
404 static int
405 luaA_pushscreen(lua_State *L, screen_t *s)
407 screen_t **ps = lua_newuserdata(L, sizeof(*ps));
408 *ps = s;
409 luaL_getmetatable(L, "screen");
410 lua_setmetatable(L, -2);
411 return 1;
414 /** Screen module.
415 * \param L The Lua VM state.
416 * \return The number of elements pushed on stack.
417 * \luastack
418 * \lfield number The screen number, to get a screen.
420 static int
421 luaA_screen_module_index(lua_State *L)
423 const char *name;
425 if((name = lua_tostring(L, 2)))
426 foreach(screen, globalconf.screens)
427 foreach(output, screen->outputs)
428 if(A_STREQ(output->name, name))
429 return luaA_pushscreen(L, screen);
431 int screen = luaL_checknumber(L, 2) - 1;
432 luaA_checkscreen(screen);
433 return luaA_pushscreen(L, &globalconf.screens.tab[screen]);
436 /** Get screen tags.
437 * \param L The Lua VM state.
438 * \return The number of elements pushed on stack.
439 * \luastack
440 * \lparam None or a table of tags to set to the screen.
441 * The table must contains at least one tag.
442 * \return A table with all screen tags.
444 static int
445 luaA_screen_tags(lua_State *L)
447 screen_t **ps = luaL_checkudata(L, 1, "screen");
448 screen_t *s = *ps;
450 if(lua_gettop(L) == 2)
452 luaA_checktable(L, 2);
454 /* Detach all tags, but go backward since the array len will change */
455 for(int i = s->tags.len - 1; i >= 0; i--)
456 tag_remove_from_screen(s->tags.tab[i]);
458 lua_pushnil(L);
459 while(lua_next(L, 2))
460 tag_append_to_screen(L, -1, s);
462 else
464 lua_createtable(L, s->tags.len, 0);
465 for(int i = 0; i < s->tags.len; i++)
467 luaA_object_push(L, s->tags.tab[i]);
468 lua_rawseti(L, -2, i + 1);
472 return 1;
475 /** A screen.
476 * \param L The Lua VM state.
477 * \return The number of elements pushed on stack.
478 * \luastack
479 * \lfield coords The screen coordinates. Immutable.
480 * \lfield workarea The screen workarea.
482 static int
483 luaA_screen_index(lua_State *L)
485 const char *buf;
486 screen_t **ps;
487 screen_t *s;
489 /* Get metatable of the screen. */
490 lua_getmetatable(L, 1);
491 /* Get the field */
492 lua_pushvalue(L, 2);
493 lua_rawget(L, -2);
494 /* Do we have a field like that? */
495 if(!lua_isnil(L, -1))
497 /* Yes, so return it! */
498 lua_remove(L, -2);
499 return 1;
501 /* No, so remove everything. */
502 lua_pop(L, 2);
504 buf = luaL_checkstring(L, 2);
505 ps = luaL_checkudata(L, 1, "screen");
506 s = *ps;
508 if(A_STREQ(buf, "index"))
510 lua_pushinteger(L, screen_array_indexof(&globalconf.screens, s) + 1);
511 return 1;
514 if(A_STREQ(buf, "geometry"))
516 luaA_pusharea(L, s->geometry);
517 return 1;
520 if(A_STREQ(buf, "workarea"))
522 luaA_pusharea(L, screen_area_get(s, true));
523 return 1;
526 if(A_STREQ(buf, "outputs"))
528 lua_createtable(L, 0, s->outputs.len);
529 foreach(output, s->outputs)
531 lua_createtable(L, 2, 0);
533 lua_pushinteger(L, output->mm_width);
534 lua_setfield(L, -2, "mm_width");
535 lua_pushinteger(L, output->mm_height);
536 lua_setfield(L, -2, "mm_height");
538 lua_setfield(L, -2, output->name);
540 /* The table of tables we created. */
541 return 1;
544 return 0;
547 /** A screen.
548 * \param L The Lua VM state.
549 * \return The number of elements pushed on stack.
551 static int
552 luaA_screen_equal(lua_State *L)
554 screen_t **ps1;
555 screen_t **ps2;
557 ps1 = luaL_checkudata(L, 1, "screen");
558 ps2 = luaL_checkudata(L, 2, "screen");
559 lua_pushboolean(L, *ps1 == *ps2);
561 return 1;
564 /** Add a signal to a screen.
565 * \param L The Lua VM state.
566 * \return The number of elements pushed on stack.
567 * \luastack
568 * \lvalue A screen.
569 * \lparam A signal name.
571 static int
572 luaA_screen_add_signal(lua_State *L)
574 screen_t **ps = luaL_checkudata(L, 1, "screen");
575 screen_t *s = *ps;
576 const char *name = luaL_checkstring(L, 2);
577 signal_add(&s->signals, name);
578 return 0;
581 /** Connect a screen's signal.
582 * \param L The Lua VM state.
583 * \return The number of elements pushed on stack.
584 * \luastack
585 * \lvalue A screen.
586 * \lparam A signal name.
587 * \lparam A function to call when the signal is emitted.
589 static int
590 luaA_screen_connect_signal(lua_State *L)
592 screen_t **ps = luaL_checkudata(L, 1, "screen");
593 screen_t *s = *ps;
594 const char *name = luaL_checkstring(L, 2);
595 luaA_checkfunction(L, 3);
596 signal_connect(&s->signals, name, luaA_object_ref(L, 3));
597 return 0;
600 /** Remove a signal to a screen.
601 * \param L The Lua VM state.
602 * \return The number of elements pushed on stack.
603 * \luastack
604 * \lvalue A screen.
605 * \lparam A signal name.
606 * \lparam A function to remove
608 static int
609 luaA_screen_disconnect_signal(lua_State *L)
611 screen_t **ps = luaL_checkudata(L, 1, "screen");
612 screen_t *s = *ps;
613 const char *name = luaL_checkstring(L, 2);
614 luaA_checkfunction(L, 3);
615 const void *ref = lua_topointer(L, 3);
616 signal_disconnect(&s->signals, name, ref);
617 luaA_object_unref(L, (void *) ref);
618 return 0;
621 /** Emit a signal to a screen.
622 * \param L The Lua VM state.
623 * \param screen The screen.
624 * \param name The signal name.
625 * \param nargs The number of arguments to the signal function.
627 void
628 screen_emit_signal(lua_State *L, screen_t *screen, const char *name, int nargs)
630 luaA_pushscreen(L, screen);
631 lua_insert(L, - nargs - 1);
632 signal_object_emit(L, &screen->signals, name, nargs + 1);
635 /** Emit a signal to a screen.
636 * \param L The Lua VM state.
637 * \return The number of elements pushed on stack.
638 * \luastack
639 * \lvalue A screen.
640 * \lparam A signal name.
641 * \lparam Various arguments, optional.
643 static int
644 luaA_screen_emit_signal(lua_State *L)
646 screen_t **ps = luaL_checkudata(L, 1, "screen");
647 screen_emit_signal(L, *ps, luaL_checkstring(L, 2), lua_gettop(L) - 2);
648 return 0;
651 /** Get the screen count.
652 * \param L The Lua VM state.
653 * \return The number of elements pushed on stack.
655 * \luastack
656 * \lreturn The screen count, at least 1.
658 static int
659 luaA_screen_count(lua_State *L)
661 lua_pushnumber(L, globalconf.screens.len);
662 return 1;
665 const struct luaL_reg awesome_screen_methods[] =
667 { "count", luaA_screen_count },
668 { "__index", luaA_screen_module_index },
669 { NULL, NULL }
672 const struct luaL_reg awesome_screen_meta[] =
674 { "add_signal", luaA_screen_add_signal },
675 { "connect_signal", luaA_screen_connect_signal },
676 { "disconnect_signal", luaA_screen_disconnect_signal },
677 { "emit_signal", luaA_screen_emit_signal },
678 { "tags", luaA_screen_tags },
679 { "__index", luaA_screen_index },
680 { "__eq", luaA_screen_equal },
681 { NULL, NULL }
684 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80