Only set client's urgent after startup
[awesome.git] / screen.c
blobe04e310b167426691c73a8385dec61001a636be6
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;
75 signal_add(&new_screen.signals, "property::workarea");
76 screen_array_append(&globalconf.screens, new_screen);
78 /* Allocate the lua userdata object representing this screen */
79 screen_t *s = &globalconf.screens.tab[globalconf.screens.len-1];
80 screen_t **ps = lua_newuserdata(globalconf.L, sizeof(*ps));
81 *ps = s;
82 luaL_getmetatable(globalconf.L, "screen");
83 lua_setmetatable(globalconf.L, -2);
84 s->userdata = luaA_object_ref(globalconf.L, -1);
87 static bool
88 screens_exist(void)
90 return globalconf.screens.len > 0;
93 static bool
94 screen_scan_randr(void)
96 /* Check for extension before checking for XRandR */
97 if(xcb_get_extension_data(globalconf.connection, &xcb_randr_id)->present)
99 xcb_randr_query_version_reply_t *version_reply =
100 xcb_randr_query_version_reply(globalconf.connection,
101 xcb_randr_query_version(globalconf.connection, 1, 1), 0);
102 if(version_reply)
104 p_delete(&version_reply);
106 /* A quick XRandR recall:
107 * You have CRTC that manages a part of a SCREEN.
108 * Each CRTC can draw stuff on one or more OUTPUT. */
109 xcb_randr_get_screen_resources_cookie_t screen_res_c = xcb_randr_get_screen_resources(globalconf.connection, globalconf.screen->root);
110 xcb_randr_get_screen_resources_reply_t *screen_res_r = xcb_randr_get_screen_resources_reply(globalconf.connection, screen_res_c, NULL);
112 /* Only use the data from XRandR if there is more than one screen
113 * defined. This should work around the broken nvidia driver. */
114 if (screen_res_r->num_crtcs <= 1)
116 p_delete(&screen_res_r);
117 return false;
120 /* We go through CRTC, and build a screen for each one. */
121 xcb_randr_crtc_t *randr_crtcs = xcb_randr_get_screen_resources_crtcs(screen_res_r);
123 for(int i = 0; i < screen_res_r->num_crtcs; i++)
125 /* Get info on the output crtc */
126 xcb_randr_get_crtc_info_cookie_t crtc_info_c = xcb_randr_get_crtc_info(globalconf.connection, randr_crtcs[i], XCB_CURRENT_TIME);
127 xcb_randr_get_crtc_info_reply_t *crtc_info_r = xcb_randr_get_crtc_info_reply(globalconf.connection, crtc_info_c, NULL);
129 /* If CRTC has no OUTPUT, ignore it */
130 if(!xcb_randr_get_crtc_info_outputs_length(crtc_info_r))
131 continue;
133 /* Prepare the new screen */
134 screen_t new_screen;
135 p_clear(&new_screen, 1);
136 new_screen.geometry.x = crtc_info_r->x;
137 new_screen.geometry.y = crtc_info_r->y;
138 new_screen.geometry.width= crtc_info_r->width;
139 new_screen.geometry.height= crtc_info_r->height;
141 xcb_randr_output_t *randr_outputs = xcb_randr_get_crtc_info_outputs(crtc_info_r);
143 for(int j = 0; j < xcb_randr_get_crtc_info_outputs_length(crtc_info_r); j++)
145 xcb_randr_get_output_info_cookie_t output_info_c = xcb_randr_get_output_info(globalconf.connection, randr_outputs[j], XCB_CURRENT_TIME);
146 xcb_randr_get_output_info_reply_t *output_info_r = xcb_randr_get_output_info_reply(globalconf.connection, output_info_c, NULL);
148 int len = xcb_randr_get_output_info_name_length(output_info_r);
149 /* name is not NULL terminated */
150 char *name = memcpy(p_new(char *, len + 1), xcb_randr_get_output_info_name(output_info_r), len);
151 name[len] = '\0';
153 screen_output_array_append(&new_screen.outputs,
154 (screen_output_t) { .name = name,
155 .mm_width = output_info_r->mm_width,
156 .mm_height = output_info_r->mm_height });
158 p_delete(&output_info_r);
161 screen_add(new_screen);
163 p_delete(&crtc_info_r);
166 p_delete(&screen_res_r);
168 return screens_exist();
172 return false;
175 static bool
176 screen_scan_xinerama(void)
178 bool xinerama_is_active = false;
180 /* Check for extension before checking for Xinerama */
181 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
183 xcb_xinerama_is_active_reply_t *xia;
184 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
185 xinerama_is_active = xia->state;
186 p_delete(&xia);
189 if(xinerama_is_active)
191 xcb_xinerama_query_screens_reply_t *xsq;
192 xcb_xinerama_screen_info_t *xsi;
193 int xinerama_screen_number;
195 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
196 xcb_xinerama_query_screens_unchecked(globalconf.connection),
197 NULL);
199 xsi = xcb_xinerama_query_screens_screen_info(xsq);
200 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
202 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
203 for(int screen = 0; screen < xinerama_screen_number; screen++)
205 screen_t s;
206 p_clear(&s, 1);
207 s.geometry = screen_xsitoarea(xsi[screen]);
208 screen_add(s);
211 p_delete(&xsq);
213 return screens_exist();
216 return false;
219 static void screen_scan_x11(void)
221 /* One screen only / Zaphod mode */
222 xcb_screen_t *xcb_screen = globalconf.screen;
223 screen_t s;
224 p_clear(&s, 1);
225 s.geometry.x = 0;
226 s.geometry.y = 0;
227 s.geometry.width = xcb_screen->width_in_pixels;
228 s.geometry.height = xcb_screen->height_in_pixels;
229 screen_add(s);
232 /** Get screens informations and fill global configuration.
234 void
235 screen_scan(void)
237 if(!screen_scan_randr() && !screen_scan_xinerama())
238 screen_scan_x11();
241 /** Return the Xinerama screen number where the coordinates belongs to.
242 * \param screen The logical screen number.
243 * \param x X coordinate
244 * \param y Y coordinate
245 * \return Screen pointer or screen param if no match or no multi-head.
247 screen_t *
248 screen_getbycoord(int x, int y)
250 foreach(s, globalconf.screens)
251 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
252 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
253 return s;
255 /* No screen found, let's be creative. */
256 return &globalconf.screens.tab[0];
259 /** Get screens info.
260 * \param screen Screen.
261 * \param strut Honor windows strut.
262 * \return The screen area.
264 area_t
265 screen_area_get(screen_t *screen, bool strut)
267 if(!strut)
268 return screen->geometry;
270 area_t area = screen->geometry;
271 uint16_t top = 0, bottom = 0, left = 0, right = 0;
273 #define COMPUTE_STRUT(o) \
275 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
277 if((o)->strut.top) \
278 top = MAX(top, (o)->strut.top); \
279 else \
280 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
282 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
284 if((o)->strut.bottom) \
285 bottom = MAX(bottom, (o)->strut.bottom); \
286 else \
287 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
289 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
291 if((o)->strut.left) \
292 left = MAX(left, (o)->strut.left); \
293 else \
294 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
296 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
298 if((o)->strut.right) \
299 right = MAX(right, (o)->strut.right); \
300 else \
301 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
305 foreach(c, globalconf.clients)
306 if((*c)->screen == screen && client_isvisible(*c))
307 COMPUTE_STRUT(*c)
309 foreach(drawin, globalconf.drawins)
310 if((*drawin)->visible)
312 screen_t *d_screen =
313 screen_getbycoord((*drawin)->geometry.x, (*drawin)->geometry.y);
314 if (d_screen == screen)
315 COMPUTE_STRUT(*drawin)
318 #undef COMPUTE_STRUT
320 area.x += left;
321 area.y += top;
322 area.width -= left + right;
323 area.height -= top + bottom;
325 return area;
328 /** Get display info.
329 * \return The display area.
331 area_t
332 display_area_get(void)
334 xcb_screen_t *s = globalconf.screen;
335 area_t area = { .x = 0,
336 .y = 0,
337 .width = s->width_in_pixels,
338 .height = s->height_in_pixels };
339 return area;
342 /** Move a client to a virtual screen.
343 * \param c The client to move.
344 * \param new_screen The destination screen.
345 * \param doresize Set to true if we also move the client to the new x and
346 * y of the new screen.
348 void
349 screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize)
351 screen_t *old_screen = c->screen;
352 area_t from, to;
353 bool had_focus = false;
355 if(new_screen == c->screen)
356 return;
358 if (globalconf.focus.client == c)
359 had_focus = true;
361 c->screen = new_screen;
363 if(!doresize)
365 luaA_object_push(globalconf.L, c);
366 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
367 lua_pop(globalconf.L, 1);
368 if(had_focus)
369 client_focus(c);
370 return;
373 from = screen_area_get(old_screen, false);
374 to = screen_area_get(c->screen, false);
376 area_t new_geometry = c->geometry;
378 new_geometry.x = to.x + new_geometry.x - from.x;
379 new_geometry.y = to.y + new_geometry.y - from.y;
381 /* resize the client if it doesn't fit the new screen */
382 if(new_geometry.width > to.width)
383 new_geometry.width = to.width;
384 if(new_geometry.height > to.height)
385 new_geometry.height = to.height;
387 /* make sure the client is still on the screen */
388 if(new_geometry.x + new_geometry.width > to.x + to.width)
389 new_geometry.x = to.x + to.width - new_geometry.width;
390 if(new_geometry.y + new_geometry.height > to.y + to.height)
391 new_geometry.y = to.y + to.height - new_geometry.height;
393 /* move / resize the client */
394 client_resize(c, new_geometry, false);
395 luaA_object_push(globalconf.L, c);
396 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
397 lua_pop(globalconf.L, 1);
398 if(had_focus)
399 client_focus(c);
402 /** Push a screen onto the stack.
403 * \param L The Lua VM state.
404 * \param s The screen to push.
405 * \return The number of elements pushed on stack.
407 static int
408 luaA_pushscreen(lua_State *L, screen_t *s)
410 luaA_object_push(L, s->userdata);
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 /** A screen.
437 * \param L The Lua VM state.
438 * \return The number of elements pushed on stack.
439 * \luastack
440 * \lfield coords The screen coordinates. Immutable.
441 * \lfield workarea The screen workarea.
443 static int
444 luaA_screen_index(lua_State *L)
446 const char *buf;
447 screen_t **ps;
448 screen_t *s;
450 /* Get metatable of the screen. */
451 lua_getmetatable(L, 1);
452 /* Get the field */
453 lua_pushvalue(L, 2);
454 lua_rawget(L, -2);
455 /* Do we have a field like that? */
456 if(!lua_isnil(L, -1))
458 /* Yes, so return it! */
459 lua_remove(L, -2);
460 return 1;
462 /* No, so remove everything. */
463 lua_pop(L, 2);
465 buf = luaL_checkstring(L, 2);
466 ps = luaL_checkudata(L, 1, "screen");
467 s = *ps;
469 if(A_STREQ(buf, "index"))
471 lua_pushinteger(L, screen_array_indexof(&globalconf.screens, s) + 1);
472 return 1;
475 if(A_STREQ(buf, "geometry"))
477 luaA_pusharea(L, s->geometry);
478 return 1;
481 if(A_STREQ(buf, "workarea"))
483 luaA_pusharea(L, screen_area_get(s, true));
484 return 1;
487 if(A_STREQ(buf, "outputs"))
489 lua_createtable(L, 0, s->outputs.len);
490 foreach(output, s->outputs)
492 lua_createtable(L, 2, 0);
494 lua_pushinteger(L, output->mm_width);
495 lua_setfield(L, -2, "mm_width");
496 lua_pushinteger(L, output->mm_height);
497 lua_setfield(L, -2, "mm_height");
499 lua_setfield(L, -2, output->name);
501 /* The table of tables we created. */
502 return 1;
505 return 0;
508 /** Add a signal to a screen.
509 * \param L The Lua VM state.
510 * \return The number of elements pushed on stack.
511 * \luastack
512 * \lvalue A screen.
513 * \lparam A signal name.
515 static int
516 luaA_screen_add_signal(lua_State *L)
518 screen_t **ps = luaL_checkudata(L, 1, "screen");
519 screen_t *s = *ps;
520 const char *name = luaL_checkstring(L, 2);
521 signal_add(&s->signals, name);
522 return 0;
525 /** Connect a screen's signal.
526 * \param L The Lua VM state.
527 * \return The number of elements pushed on stack.
528 * \luastack
529 * \lvalue A screen.
530 * \lparam A signal name.
531 * \lparam A function to call when the signal is emitted.
533 static int
534 luaA_screen_connect_signal(lua_State *L)
536 screen_t **ps = luaL_checkudata(L, 1, "screen");
537 screen_t *s = *ps;
538 const char *name = luaL_checkstring(L, 2);
539 luaA_checkfunction(L, 3);
540 signal_connect(&s->signals, name, luaA_object_ref(L, 3));
541 return 0;
544 /** Remove a signal to a screen.
545 * \param L The Lua VM state.
546 * \return The number of elements pushed on stack.
547 * \luastack
548 * \lvalue A screen.
549 * \lparam A signal name.
550 * \lparam A function to remove
552 static int
553 luaA_screen_disconnect_signal(lua_State *L)
555 screen_t **ps = luaL_checkudata(L, 1, "screen");
556 screen_t *s = *ps;
557 const char *name = luaL_checkstring(L, 2);
558 luaA_checkfunction(L, 3);
559 const void *ref = lua_topointer(L, 3);
560 signal_disconnect(&s->signals, name, ref);
561 luaA_object_unref(L, (void *) ref);
562 return 0;
565 /** Emit a signal to a screen.
566 * \param L The Lua VM state.
567 * \param screen The screen.
568 * \param name The signal name.
569 * \param nargs The number of arguments to the signal function.
571 void
572 screen_emit_signal(lua_State *L, screen_t *screen, const char *name, int nargs)
574 luaA_pushscreen(L, screen);
575 lua_insert(L, - nargs - 1);
576 signal_object_emit(L, &screen->signals, name, nargs + 1);
579 /** Emit a signal to a screen.
580 * \param L The Lua VM state.
581 * \return The number of elements pushed on stack.
582 * \luastack
583 * \lvalue A screen.
584 * \lparam A signal name.
585 * \lparam Various arguments, optional.
587 static int
588 luaA_screen_emit_signal(lua_State *L)
590 screen_t **ps = luaL_checkudata(L, 1, "screen");
591 screen_emit_signal(L, *ps, luaL_checkstring(L, 2), lua_gettop(L) - 2);
592 return 0;
595 /** Get the screen count.
596 * \param L The Lua VM state.
597 * \return The number of elements pushed on stack.
599 * \luastack
600 * \lreturn The screen count, at least 1.
602 static int
603 luaA_screen_count(lua_State *L)
605 lua_pushnumber(L, globalconf.screens.len);
606 return 1;
609 const struct luaL_Reg awesome_screen_methods[] =
611 { "count", luaA_screen_count },
612 { "__index", luaA_screen_module_index },
613 { NULL, NULL }
616 const struct luaL_Reg awesome_screen_meta[] =
618 { "add_signal", luaA_screen_add_signal },
619 { "connect_signal", luaA_screen_connect_signal },
620 { "disconnect_signal", luaA_screen_disconnect_signal },
621 { "emit_signal", luaA_screen_emit_signal },
622 { "__index", luaA_screen_index },
623 { NULL, NULL }
626 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80