awful.client.toggletag: Use tag.getscreen()
[awesome.git] / screen.c
blob3380959bf6fc1efd4cdc9d42f88b5dcbf8f3e6b3
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 screen_array_append(&globalconf.screens, new_screen);
66 static bool
67 screen_scan_randr(void)
69 /* Check for extension before checking for XRandR */
70 if(xcb_get_extension_data(globalconf.connection, &xcb_randr_id)->present)
72 xcb_randr_query_version_reply_t *version_reply =
73 xcb_randr_query_version_reply(globalconf.connection,
74 xcb_randr_query_version(globalconf.connection, 1, 1), 0);
75 if(version_reply)
77 p_delete(&version_reply);
79 /* A quick XRandR recall:
80 * You have CRTC that manages a part of a SCREEN.
81 * Each CRTC can draw stuff on one or more OUTPUT. */
82 xcb_randr_get_screen_resources_cookie_t screen_res_c = xcb_randr_get_screen_resources(globalconf.connection, globalconf.screen->root);
83 xcb_randr_get_screen_resources_reply_t *screen_res_r = xcb_randr_get_screen_resources_reply(globalconf.connection, screen_res_c, NULL);
85 /* Only use the data from XRandR if there is more than one screen
86 * defined. This should work around the broken nvidia driver. */
87 if (screen_res_r->num_crtcs <= 1)
89 p_delete(&screen_res_r);
90 return false;
93 /* We go through CRTC, and build a screen for each one. */
94 xcb_randr_crtc_t *randr_crtcs = xcb_randr_get_screen_resources_crtcs(screen_res_r);
96 for(int i = 0; i < screen_res_r->num_crtcs; i++)
98 /* Get info on the output crtc */
99 xcb_randr_get_crtc_info_cookie_t crtc_info_c = xcb_randr_get_crtc_info(globalconf.connection, randr_crtcs[i], XCB_CURRENT_TIME);
100 xcb_randr_get_crtc_info_reply_t *crtc_info_r = xcb_randr_get_crtc_info_reply(globalconf.connection, crtc_info_c, NULL);
102 /* If CRTC has no OUTPUT, ignore it */
103 if(!xcb_randr_get_crtc_info_outputs_length(crtc_info_r))
104 continue;
106 /* Prepare the new screen */
107 screen_t new_screen;
108 p_clear(&new_screen, 1);
109 new_screen.geometry.x = crtc_info_r->x;
110 new_screen.geometry.y = crtc_info_r->y;
111 new_screen.geometry.width= crtc_info_r->width;
112 new_screen.geometry.height= crtc_info_r->height;
114 xcb_randr_output_t *randr_outputs = xcb_randr_get_crtc_info_outputs(crtc_info_r);
116 for(int j = 0; j < xcb_randr_get_crtc_info_outputs_length(crtc_info_r); j++)
118 xcb_randr_get_output_info_cookie_t output_info_c = xcb_randr_get_output_info(globalconf.connection, randr_outputs[j], XCB_CURRENT_TIME);
119 xcb_randr_get_output_info_reply_t *output_info_r = xcb_randr_get_output_info_reply(globalconf.connection, output_info_c, NULL);
121 int len = xcb_randr_get_output_info_name_length(output_info_r);
122 /* name is not NULL terminated */
123 char *name = memcpy(p_new(char *, len + 1), xcb_randr_get_output_info_name(output_info_r), len);
124 name[len] = '\0';
126 screen_output_array_append(&new_screen.outputs,
127 (screen_output_t) { .name = name,
128 .mm_width = output_info_r->mm_width,
129 .mm_height = output_info_r->mm_height });
131 p_delete(&output_info_r);
134 screen_add(new_screen);
136 p_delete(&crtc_info_r);
139 p_delete(&screen_res_r);
141 return true;
145 return false;
148 static bool
149 screen_scan_xinerama(void)
151 bool xinerama_is_active = false;
153 /* Check for extension before checking for Xinerama */
154 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
156 xcb_xinerama_is_active_reply_t *xia;
157 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
158 xinerama_is_active = xia->state;
159 p_delete(&xia);
162 if(xinerama_is_active)
164 xcb_xinerama_query_screens_reply_t *xsq;
165 xcb_xinerama_screen_info_t *xsi;
166 int xinerama_screen_number;
168 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
169 xcb_xinerama_query_screens_unchecked(globalconf.connection),
170 NULL);
172 xsi = xcb_xinerama_query_screens_screen_info(xsq);
173 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
175 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
176 for(int screen = 0; screen < xinerama_screen_number; screen++)
178 bool drop = false;
179 foreach(screen_to_test, globalconf.screens)
180 if(xsi[screen].x_org == screen_to_test->geometry.x
181 && xsi[screen].y_org == screen_to_test->geometry.y)
183 /* we already have a screen for this area, just check if
184 * it's not bigger and drop it */
185 drop = true;
186 int i = screen_array_indexof(&globalconf.screens, screen_to_test);
187 screen_to_test->geometry.width =
188 MAX(xsi[screen].width, xsi[i].width);
189 screen_to_test->geometry.height =
190 MAX(xsi[screen].height, xsi[i].height);
192 if(!drop)
194 screen_t s;
195 p_clear(&s, 1);
196 s.geometry = screen_xsitoarea(xsi[screen]);
197 screen_add(s);
201 p_delete(&xsq);
203 return true;
206 return false;
209 static void screen_scan_x11(void)
211 /* One screen only / Zaphod mode */
212 xcb_screen_t *xcb_screen = globalconf.screen;
213 screen_t s;
214 p_clear(&s, 1);
215 s.geometry.x = 0;
216 s.geometry.y = 0;
217 s.geometry.width = xcb_screen->width_in_pixels;
218 s.geometry.height = xcb_screen->height_in_pixels;
219 screen_add(s);
222 /** Get screens informations and fill global configuration.
224 void
225 screen_scan(void)
227 if(!screen_scan_randr() && !screen_scan_xinerama())
228 screen_scan_x11();
231 /** Return the Xinerama screen number where the coordinates belongs to.
232 * \param screen The logical screen number.
233 * \param x X coordinate
234 * \param y Y coordinate
235 * \return Screen pointer or screen param if no match or no multi-head.
237 screen_t *
238 screen_getbycoord(int x, int y)
240 foreach(s, globalconf.screens)
241 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
242 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
243 return s;
245 /* No screen found, let's be creative. */
246 return &globalconf.screens.tab[0];
249 /** Get screens info.
250 * \param screen Screen.
251 * \param strut Honor windows strut.
252 * \return The screen area.
254 area_t
255 screen_area_get(screen_t *screen, bool strut)
257 if(!strut)
258 return screen->geometry;
260 area_t area = screen->geometry;
261 uint16_t top = 0, bottom = 0, left = 0, right = 0;
263 #define COMPUTE_STRUT(o) \
265 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
267 if((o)->strut.top) \
268 top = MAX(top, (o)->strut.top); \
269 else \
270 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
272 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
274 if((o)->strut.bottom) \
275 bottom = MAX(bottom, (o)->strut.bottom); \
276 else \
277 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
279 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
281 if((o)->strut.left) \
282 left = MAX(left, (o)->strut.left); \
283 else \
284 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
286 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
288 if((o)->strut.right) \
289 right = MAX(right, (o)->strut.right); \
290 else \
291 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
295 foreach(c, globalconf.clients)
296 if((*c)->screen == screen && client_isvisible(*c))
297 COMPUTE_STRUT(*c)
299 foreach(drawin, globalconf.drawins)
300 if((*drawin)->visible)
302 screen_t *d_screen =
303 screen_getbycoord((*drawin)->geometry.x, (*drawin)->geometry.y);
304 if (d_screen == screen)
305 COMPUTE_STRUT(*drawin)
308 #undef COMPUTE_STRUT
310 area.x += left;
311 area.y += top;
312 area.width -= left + right;
313 area.height -= top + bottom;
315 return area;
318 /** Get display info.
319 * \return The display area.
321 area_t
322 display_area_get(void)
324 xcb_screen_t *s = globalconf.screen;
325 area_t area = { .x = 0,
326 .y = 0,
327 .width = s->width_in_pixels,
328 .height = s->height_in_pixels };
329 return area;
332 /** Move a client to a virtual screen.
333 * \param c The client to move.
334 * \param new_screen The destination screen.
335 * \param doresize Set to true if we also move the client to the new x and
336 * y of the new screen.
338 void
339 screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize)
341 screen_t *old_screen = c->screen;
342 area_t from, to;
343 bool had_focus = false;
345 if(new_screen == c->screen)
346 return;
348 if (globalconf.focus.client == c)
349 had_focus = true;
351 c->screen = new_screen;
353 if(!doresize)
355 luaA_object_push(globalconf.L, c);
356 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
357 lua_pop(globalconf.L, 1);
358 if(had_focus)
359 client_focus(c);
360 return;
363 from = screen_area_get(old_screen, false);
364 to = screen_area_get(c->screen, false);
366 area_t new_geometry = c->geometry;
368 new_geometry.x = to.x + new_geometry.x - from.x;
369 new_geometry.y = to.y + new_geometry.y - from.y;
371 /* resize the client if it doesn't fit the new screen */
372 if(new_geometry.width > to.width)
373 new_geometry.width = to.width;
374 if(new_geometry.height > to.height)
375 new_geometry.height = to.height;
377 /* make sure the client is still on the screen */
378 if(new_geometry.x + new_geometry.width > to.x + to.width)
379 new_geometry.x = to.x + to.width - new_geometry.width;
380 if(new_geometry.y + new_geometry.height > to.y + to.height)
381 new_geometry.y = to.y + to.height - new_geometry.height;
383 /* move / resize the client */
384 client_resize(c, new_geometry);
385 luaA_object_push(globalconf.L, c);
386 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
387 lua_pop(globalconf.L, 1);
388 if(had_focus)
389 client_focus(c);
392 /** Push a screen onto the stack.
393 * \param L The Lua VM state.
394 * \param s The screen to push.
395 * \return The number of elements pushed on stack.
397 static int
398 luaA_pushscreen(lua_State *L, screen_t *s)
400 screen_t **ps = lua_newuserdata(L, sizeof(*ps));
401 *ps = s;
402 luaL_getmetatable(L, "screen");
403 lua_setmetatable(L, -2);
404 return 1;
407 /** Screen module.
408 * \param L The Lua VM state.
409 * \return The number of elements pushed on stack.
410 * \luastack
411 * \lfield number The screen number, to get a screen.
413 static int
414 luaA_screen_module_index(lua_State *L)
416 const char *name;
418 if((name = lua_tostring(L, 2)))
419 foreach(screen, globalconf.screens)
420 foreach(output, screen->outputs)
421 if(A_STREQ(output->name, name))
422 return luaA_pushscreen(L, screen);
424 int screen = luaL_checknumber(L, 2) - 1;
425 luaA_checkscreen(screen);
426 return luaA_pushscreen(L, &globalconf.screens.tab[screen]);
429 /** A screen.
430 * \param L The Lua VM state.
431 * \return The number of elements pushed on stack.
432 * \luastack
433 * \lfield coords The screen coordinates. Immutable.
434 * \lfield workarea The screen workarea.
436 static int
437 luaA_screen_index(lua_State *L)
439 const char *buf;
440 screen_t **ps;
441 screen_t *s;
443 /* Get metatable of the screen. */
444 lua_getmetatable(L, 1);
445 /* Get the field */
446 lua_pushvalue(L, 2);
447 lua_rawget(L, -2);
448 /* Do we have a field like that? */
449 if(!lua_isnil(L, -1))
451 /* Yes, so return it! */
452 lua_remove(L, -2);
453 return 1;
455 /* No, so remove everything. */
456 lua_pop(L, 2);
458 buf = luaL_checkstring(L, 2);
459 ps = luaL_checkudata(L, 1, "screen");
460 s = *ps;
462 if(A_STREQ(buf, "index"))
464 lua_pushinteger(L, screen_array_indexof(&globalconf.screens, s) + 1);
465 return 1;
468 if(A_STREQ(buf, "geometry"))
470 luaA_pusharea(L, s->geometry);
471 return 1;
474 if(A_STREQ(buf, "workarea"))
476 luaA_pusharea(L, screen_area_get(s, true));
477 return 1;
480 if(A_STREQ(buf, "outputs"))
482 lua_createtable(L, 0, s->outputs.len);
483 foreach(output, s->outputs)
485 lua_createtable(L, 2, 0);
487 lua_pushinteger(L, output->mm_width);
488 lua_setfield(L, -2, "mm_width");
489 lua_pushinteger(L, output->mm_height);
490 lua_setfield(L, -2, "mm_height");
492 lua_setfield(L, -2, output->name);
494 /* The table of tables we created. */
495 return 1;
498 return 0;
501 /** A screen.
502 * \param L The Lua VM state.
503 * \return The number of elements pushed on stack.
505 static int
506 luaA_screen_equal(lua_State *L)
508 screen_t **ps1;
509 screen_t **ps2;
511 ps1 = luaL_checkudata(L, 1, "screen");
512 ps2 = luaL_checkudata(L, 2, "screen");
513 lua_pushboolean(L, *ps1 == *ps2);
515 return 1;
518 /** Add a signal to a screen.
519 * \param L The Lua VM state.
520 * \return The number of elements pushed on stack.
521 * \luastack
522 * \lvalue A screen.
523 * \lparam A signal name.
525 static int
526 luaA_screen_add_signal(lua_State *L)
528 screen_t **ps = luaL_checkudata(L, 1, "screen");
529 screen_t *s = *ps;
530 const char *name = luaL_checkstring(L, 2);
531 signal_add(&s->signals, name);
532 return 0;
535 /** Connect a screen's signal.
536 * \param L The Lua VM state.
537 * \return The number of elements pushed on stack.
538 * \luastack
539 * \lvalue A screen.
540 * \lparam A signal name.
541 * \lparam A function to call when the signal is emitted.
543 static int
544 luaA_screen_connect_signal(lua_State *L)
546 screen_t **ps = luaL_checkudata(L, 1, "screen");
547 screen_t *s = *ps;
548 const char *name = luaL_checkstring(L, 2);
549 luaA_checkfunction(L, 3);
550 signal_connect(&s->signals, name, luaA_object_ref(L, 3));
551 return 0;
554 /** Remove a signal to a screen.
555 * \param L The Lua VM state.
556 * \return The number of elements pushed on stack.
557 * \luastack
558 * \lvalue A screen.
559 * \lparam A signal name.
560 * \lparam A function to remove
562 static int
563 luaA_screen_disconnect_signal(lua_State *L)
565 screen_t **ps = luaL_checkudata(L, 1, "screen");
566 screen_t *s = *ps;
567 const char *name = luaL_checkstring(L, 2);
568 luaA_checkfunction(L, 3);
569 const void *ref = lua_topointer(L, 3);
570 signal_disconnect(&s->signals, name, ref);
571 luaA_object_unref(L, (void *) ref);
572 return 0;
575 /** Emit a signal to a screen.
576 * \param L The Lua VM state.
577 * \param screen The screen.
578 * \param name The signal name.
579 * \param nargs The number of arguments to the signal function.
581 void
582 screen_emit_signal(lua_State *L, screen_t *screen, const char *name, int nargs)
584 luaA_pushscreen(L, screen);
585 lua_insert(L, - nargs - 1);
586 signal_object_emit(L, &screen->signals, name, nargs + 1);
589 /** Emit a signal to a screen.
590 * \param L The Lua VM state.
591 * \return The number of elements pushed on stack.
592 * \luastack
593 * \lvalue A screen.
594 * \lparam A signal name.
595 * \lparam Various arguments, optional.
597 static int
598 luaA_screen_emit_signal(lua_State *L)
600 screen_t **ps = luaL_checkudata(L, 1, "screen");
601 screen_emit_signal(L, *ps, luaL_checkstring(L, 2), lua_gettop(L) - 2);
602 return 0;
605 /** Get the screen count.
606 * \param L The Lua VM state.
607 * \return The number of elements pushed on stack.
609 * \luastack
610 * \lreturn The screen count, at least 1.
612 static int
613 luaA_screen_count(lua_State *L)
615 lua_pushnumber(L, globalconf.screens.len);
616 return 1;
619 const struct luaL_Reg awesome_screen_methods[] =
621 { "count", luaA_screen_count },
622 { "__index", luaA_screen_module_index },
623 { NULL, NULL }
626 const struct luaL_Reg awesome_screen_meta[] =
628 { "add_signal", luaA_screen_add_signal },
629 { "connect_signal", luaA_screen_connect_signal },
630 { "disconnect_signal", luaA_screen_disconnect_signal },
631 { "emit_signal", luaA_screen_emit_signal },
632 { "__index", luaA_screen_index },
633 { "__eq", luaA_screen_equal },
634 { NULL, NULL }
637 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80