change codename
[awesome.git] / screen.c
blob81e063ee4eb0b065d69b6fbf58e2e8770e281efd
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 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);
72 return;
74 signal_add(&new_screen.signals, "property::workarea");
75 screen_array_append(&globalconf.screens, new_screen);
78 static bool
79 screen_scan_randr(void)
81 /* Check for extension before checking for XRandR */
82 if(xcb_get_extension_data(globalconf.connection, &xcb_randr_id)->present)
84 xcb_randr_query_version_reply_t *version_reply =
85 xcb_randr_query_version_reply(globalconf.connection,
86 xcb_randr_query_version(globalconf.connection, 1, 1), 0);
87 if(version_reply)
89 p_delete(&version_reply);
91 /* A quick XRandR recall:
92 * You have CRTC that manages a part of a SCREEN.
93 * Each CRTC can draw stuff on one or more OUTPUT. */
94 xcb_randr_get_screen_resources_cookie_t screen_res_c = xcb_randr_get_screen_resources(globalconf.connection, globalconf.screen->root);
95 xcb_randr_get_screen_resources_reply_t *screen_res_r = xcb_randr_get_screen_resources_reply(globalconf.connection, screen_res_c, NULL);
97 /* Only use the data from XRandR if there is more than one screen
98 * defined. This should work around the broken nvidia driver. */
99 if (screen_res_r->num_crtcs <= 1)
101 p_delete(&screen_res_r);
102 return false;
105 /* We go through CRTC, and build a screen for each one. */
106 xcb_randr_crtc_t *randr_crtcs = xcb_randr_get_screen_resources_crtcs(screen_res_r);
108 for(int i = 0; i < screen_res_r->num_crtcs; i++)
110 /* Get info on the output crtc */
111 xcb_randr_get_crtc_info_cookie_t crtc_info_c = xcb_randr_get_crtc_info(globalconf.connection, randr_crtcs[i], XCB_CURRENT_TIME);
112 xcb_randr_get_crtc_info_reply_t *crtc_info_r = xcb_randr_get_crtc_info_reply(globalconf.connection, crtc_info_c, NULL);
114 /* If CRTC has no OUTPUT, ignore it */
115 if(!xcb_randr_get_crtc_info_outputs_length(crtc_info_r))
116 continue;
118 /* Prepare the new screen */
119 screen_t new_screen;
120 p_clear(&new_screen, 1);
121 new_screen.geometry.x = crtc_info_r->x;
122 new_screen.geometry.y = crtc_info_r->y;
123 new_screen.geometry.width= crtc_info_r->width;
124 new_screen.geometry.height= crtc_info_r->height;
126 xcb_randr_output_t *randr_outputs = xcb_randr_get_crtc_info_outputs(crtc_info_r);
128 for(int j = 0; j < xcb_randr_get_crtc_info_outputs_length(crtc_info_r); j++)
130 xcb_randr_get_output_info_cookie_t output_info_c = xcb_randr_get_output_info(globalconf.connection, randr_outputs[j], XCB_CURRENT_TIME);
131 xcb_randr_get_output_info_reply_t *output_info_r = xcb_randr_get_output_info_reply(globalconf.connection, output_info_c, NULL);
133 int len = xcb_randr_get_output_info_name_length(output_info_r);
134 /* name is not NULL terminated */
135 char *name = memcpy(p_new(char *, len + 1), xcb_randr_get_output_info_name(output_info_r), len);
136 name[len] = '\0';
138 screen_output_array_append(&new_screen.outputs,
139 (screen_output_t) { .name = name,
140 .mm_width = output_info_r->mm_width,
141 .mm_height = output_info_r->mm_height });
143 p_delete(&output_info_r);
146 screen_add(new_screen);
148 p_delete(&crtc_info_r);
151 p_delete(&screen_res_r);
153 return true;
157 return false;
160 static bool
161 screen_scan_xinerama(void)
163 bool xinerama_is_active = false;
165 /* Check for extension before checking for Xinerama */
166 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
168 xcb_xinerama_is_active_reply_t *xia;
169 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
170 xinerama_is_active = xia->state;
171 p_delete(&xia);
174 if(xinerama_is_active)
176 xcb_xinerama_query_screens_reply_t *xsq;
177 xcb_xinerama_screen_info_t *xsi;
178 int xinerama_screen_number;
180 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
181 xcb_xinerama_query_screens_unchecked(globalconf.connection),
182 NULL);
184 xsi = xcb_xinerama_query_screens_screen_info(xsq);
185 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
187 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
188 for(int screen = 0; screen < xinerama_screen_number; screen++)
190 screen_t s;
191 p_clear(&s, 1);
192 s.geometry = screen_xsitoarea(xsi[screen]);
193 screen_add(s);
196 p_delete(&xsq);
198 return true;
201 return false;
204 static void screen_scan_x11(void)
206 /* One screen only / Zaphod mode */
207 xcb_screen_t *xcb_screen = globalconf.screen;
208 screen_t s;
209 p_clear(&s, 1);
210 s.geometry.x = 0;
211 s.geometry.y = 0;
212 s.geometry.width = xcb_screen->width_in_pixels;
213 s.geometry.height = xcb_screen->height_in_pixels;
214 screen_add(s);
217 /** Get screens informations and fill global configuration.
219 void
220 screen_scan(void)
222 if(!screen_scan_randr() && !screen_scan_xinerama())
223 screen_scan_x11();
226 /** Return the Xinerama screen number where the coordinates belongs to.
227 * \param screen The logical screen number.
228 * \param x X coordinate
229 * \param y Y coordinate
230 * \return Screen pointer or screen param if no match or no multi-head.
232 screen_t *
233 screen_getbycoord(int x, int y)
235 foreach(s, globalconf.screens)
236 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
237 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
238 return s;
240 /* No screen found, let's be creative. */
241 return &globalconf.screens.tab[0];
244 /** Get screens info.
245 * \param screen Screen.
246 * \param strut Honor windows strut.
247 * \return The screen area.
249 area_t
250 screen_area_get(screen_t *screen, bool strut)
252 if(!strut)
253 return screen->geometry;
255 area_t area = screen->geometry;
256 uint16_t top = 0, bottom = 0, left = 0, right = 0;
258 #define COMPUTE_STRUT(o) \
260 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
262 if((o)->strut.top) \
263 top = MAX(top, (o)->strut.top); \
264 else \
265 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
267 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
269 if((o)->strut.bottom) \
270 bottom = MAX(bottom, (o)->strut.bottom); \
271 else \
272 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
274 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
276 if((o)->strut.left) \
277 left = MAX(left, (o)->strut.left); \
278 else \
279 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
281 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
283 if((o)->strut.right) \
284 right = MAX(right, (o)->strut.right); \
285 else \
286 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
290 foreach(c, globalconf.clients)
291 if((*c)->screen == screen && client_isvisible(*c))
292 COMPUTE_STRUT(*c)
294 foreach(drawin, globalconf.drawins)
295 if((*drawin)->visible)
297 screen_t *d_screen =
298 screen_getbycoord((*drawin)->geometry.x, (*drawin)->geometry.y);
299 if (d_screen == screen)
300 COMPUTE_STRUT(*drawin)
303 #undef COMPUTE_STRUT
305 area.x += left;
306 area.y += top;
307 area.width -= left + right;
308 area.height -= top + bottom;
310 return area;
313 /** Get display info.
314 * \return The display area.
316 area_t
317 display_area_get(void)
319 xcb_screen_t *s = globalconf.screen;
320 area_t area = { .x = 0,
321 .y = 0,
322 .width = s->width_in_pixels,
323 .height = s->height_in_pixels };
324 return area;
327 /** Move a client to a virtual screen.
328 * \param c The client to move.
329 * \param new_screen The destination screen.
330 * \param doresize Set to true if we also move the client to the new x and
331 * y of the new screen.
333 void
334 screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize)
336 screen_t *old_screen = c->screen;
337 area_t from, to;
338 bool had_focus = false;
340 if(new_screen == c->screen)
341 return;
343 if (globalconf.focus.client == c)
344 had_focus = true;
346 c->screen = new_screen;
348 if(!doresize)
350 luaA_object_push(globalconf.L, c);
351 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
352 lua_pop(globalconf.L, 1);
353 if(had_focus)
354 client_focus(c);
355 return;
358 from = screen_area_get(old_screen, false);
359 to = screen_area_get(c->screen, false);
361 area_t new_geometry = c->geometry;
363 new_geometry.x = to.x + new_geometry.x - from.x;
364 new_geometry.y = to.y + new_geometry.y - from.y;
366 /* resize the client if it doesn't fit the new screen */
367 if(new_geometry.width > to.width)
368 new_geometry.width = to.width;
369 if(new_geometry.height > to.height)
370 new_geometry.height = to.height;
372 /* make sure the client is still on the screen */
373 if(new_geometry.x + new_geometry.width > to.x + to.width)
374 new_geometry.x = to.x + to.width - new_geometry.width;
375 if(new_geometry.y + new_geometry.height > to.y + to.height)
376 new_geometry.y = to.y + to.height - new_geometry.height;
378 /* move / resize the client */
379 client_resize(c, new_geometry);
380 luaA_object_push(globalconf.L, c);
381 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
382 lua_pop(globalconf.L, 1);
383 if(had_focus)
384 client_focus(c);
387 /** Push a screen onto the stack.
388 * \param L The Lua VM state.
389 * \param s The screen to push.
390 * \return The number of elements pushed on stack.
392 static int
393 luaA_pushscreen(lua_State *L, screen_t *s)
395 screen_t **ps = lua_newuserdata(L, sizeof(*ps));
396 *ps = s;
397 luaL_getmetatable(L, "screen");
398 lua_setmetatable(L, -2);
399 return 1;
402 /** Screen module.
403 * \param L The Lua VM state.
404 * \return The number of elements pushed on stack.
405 * \luastack
406 * \lfield number The screen number, to get a screen.
408 static int
409 luaA_screen_module_index(lua_State *L)
411 const char *name;
413 if((name = lua_tostring(L, 2)))
414 foreach(screen, globalconf.screens)
415 foreach(output, screen->outputs)
416 if(A_STREQ(output->name, name))
417 return luaA_pushscreen(L, screen);
419 int screen = luaL_checknumber(L, 2) - 1;
420 luaA_checkscreen(screen);
421 return luaA_pushscreen(L, &globalconf.screens.tab[screen]);
424 /** A screen.
425 * \param L The Lua VM state.
426 * \return The number of elements pushed on stack.
427 * \luastack
428 * \lfield coords The screen coordinates. Immutable.
429 * \lfield workarea The screen workarea.
431 static int
432 luaA_screen_index(lua_State *L)
434 const char *buf;
435 screen_t **ps;
436 screen_t *s;
438 /* Get metatable of the screen. */
439 lua_getmetatable(L, 1);
440 /* Get the field */
441 lua_pushvalue(L, 2);
442 lua_rawget(L, -2);
443 /* Do we have a field like that? */
444 if(!lua_isnil(L, -1))
446 /* Yes, so return it! */
447 lua_remove(L, -2);
448 return 1;
450 /* No, so remove everything. */
451 lua_pop(L, 2);
453 buf = luaL_checkstring(L, 2);
454 ps = luaL_checkudata(L, 1, "screen");
455 s = *ps;
457 if(A_STREQ(buf, "index"))
459 lua_pushinteger(L, screen_array_indexof(&globalconf.screens, s) + 1);
460 return 1;
463 if(A_STREQ(buf, "geometry"))
465 luaA_pusharea(L, s->geometry);
466 return 1;
469 if(A_STREQ(buf, "workarea"))
471 luaA_pusharea(L, screen_area_get(s, true));
472 return 1;
475 if(A_STREQ(buf, "outputs"))
477 lua_createtable(L, 0, s->outputs.len);
478 foreach(output, s->outputs)
480 lua_createtable(L, 2, 0);
482 lua_pushinteger(L, output->mm_width);
483 lua_setfield(L, -2, "mm_width");
484 lua_pushinteger(L, output->mm_height);
485 lua_setfield(L, -2, "mm_height");
487 lua_setfield(L, -2, output->name);
489 /* The table of tables we created. */
490 return 1;
493 return 0;
496 /** A screen.
497 * \param L The Lua VM state.
498 * \return The number of elements pushed on stack.
500 static int
501 luaA_screen_equal(lua_State *L)
503 screen_t **ps1;
504 screen_t **ps2;
506 ps1 = luaL_checkudata(L, 1, "screen");
507 ps2 = luaL_checkudata(L, 2, "screen");
508 lua_pushboolean(L, *ps1 == *ps2);
510 return 1;
513 /** Add a signal to a screen.
514 * \param L The Lua VM state.
515 * \return The number of elements pushed on stack.
516 * \luastack
517 * \lvalue A screen.
518 * \lparam A signal name.
520 static int
521 luaA_screen_add_signal(lua_State *L)
523 screen_t **ps = luaL_checkudata(L, 1, "screen");
524 screen_t *s = *ps;
525 const char *name = luaL_checkstring(L, 2);
526 signal_add(&s->signals, name);
527 return 0;
530 /** Connect a screen's signal.
531 * \param L The Lua VM state.
532 * \return The number of elements pushed on stack.
533 * \luastack
534 * \lvalue A screen.
535 * \lparam A signal name.
536 * \lparam A function to call when the signal is emitted.
538 static int
539 luaA_screen_connect_signal(lua_State *L)
541 screen_t **ps = luaL_checkudata(L, 1, "screen");
542 screen_t *s = *ps;
543 const char *name = luaL_checkstring(L, 2);
544 luaA_checkfunction(L, 3);
545 signal_connect(&s->signals, name, luaA_object_ref(L, 3));
546 return 0;
549 /** Remove a signal to a screen.
550 * \param L The Lua VM state.
551 * \return The number of elements pushed on stack.
552 * \luastack
553 * \lvalue A screen.
554 * \lparam A signal name.
555 * \lparam A function to remove
557 static int
558 luaA_screen_disconnect_signal(lua_State *L)
560 screen_t **ps = luaL_checkudata(L, 1, "screen");
561 screen_t *s = *ps;
562 const char *name = luaL_checkstring(L, 2);
563 luaA_checkfunction(L, 3);
564 const void *ref = lua_topointer(L, 3);
565 signal_disconnect(&s->signals, name, ref);
566 luaA_object_unref(L, (void *) ref);
567 return 0;
570 /** Emit a signal to a screen.
571 * \param L The Lua VM state.
572 * \param screen The screen.
573 * \param name The signal name.
574 * \param nargs The number of arguments to the signal function.
576 void
577 screen_emit_signal(lua_State *L, screen_t *screen, const char *name, int nargs)
579 luaA_pushscreen(L, screen);
580 lua_insert(L, - nargs - 1);
581 signal_object_emit(L, &screen->signals, name, nargs + 1);
584 /** Emit a signal to a screen.
585 * \param L The Lua VM state.
586 * \return The number of elements pushed on stack.
587 * \luastack
588 * \lvalue A screen.
589 * \lparam A signal name.
590 * \lparam Various arguments, optional.
592 static int
593 luaA_screen_emit_signal(lua_State *L)
595 screen_t **ps = luaL_checkudata(L, 1, "screen");
596 screen_emit_signal(L, *ps, luaL_checkstring(L, 2), lua_gettop(L) - 2);
597 return 0;
600 /** Get the screen count.
601 * \param L The Lua VM state.
602 * \return The number of elements pushed on stack.
604 * \luastack
605 * \lreturn The screen count, at least 1.
607 static int
608 luaA_screen_count(lua_State *L)
610 lua_pushnumber(L, globalconf.screens.len);
611 return 1;
614 const struct luaL_Reg awesome_screen_methods[] =
616 { "count", luaA_screen_count },
617 { "__index", luaA_screen_module_index },
618 { NULL, NULL }
621 const struct luaL_Reg awesome_screen_meta[] =
623 { "add_signal", luaA_screen_add_signal },
624 { "connect_signal", luaA_screen_connect_signal },
625 { "disconnect_signal", luaA_screen_disconnect_signal },
626 { "emit_signal", luaA_screen_emit_signal },
627 { "__index", luaA_screen_index },
628 { "__eq", luaA_screen_equal },
629 { NULL, NULL }
632 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80