Use awesome.conffile in menu
[awesome.git] / screen.c
blob94be2a23a7761c48e0c1550e30c5c210ff0b597c
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>
27 #include "screen.h"
28 #include "ewmh.h"
29 #include "tag.h"
30 #include "client.h"
31 #include "widget.h"
32 #include "wibox.h"
33 #include "luaa.h"
34 #include "common/xutil.h"
36 static inline area_t
37 screen_xsitoarea(xcb_xinerama_screen_info_t si)
39 area_t a =
41 .x = si.x_org,
42 .y = si.y_org,
43 .width = si.width,
44 .height = si.height
46 return a;
49 static xcb_visualtype_t *
50 screen_default_visual(xcb_screen_t *s)
52 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
54 if(depth_iter.data)
55 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
56 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
57 visual_iter.rem; xcb_visualtype_next (&visual_iter))
58 if(s->root_visual == visual_iter.data->visual_id)
59 return visual_iter.data;
61 return NULL;
64 /** Get screens informations and fill global configuration.
66 void
67 screen_scan(void)
69 /* Check for extension before checking for Xinerama */
70 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
72 xcb_xinerama_is_active_reply_t *xia;
73 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
74 globalconf.xinerama_is_active = xia->state;
75 p_delete(&xia);
78 if(globalconf.xinerama_is_active)
80 xcb_xinerama_query_screens_reply_t *xsq;
81 xcb_xinerama_screen_info_t *xsi;
82 int xinerama_screen_number;
84 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
85 xcb_xinerama_query_screens_unchecked(globalconf.connection),
86 NULL);
88 xsi = xcb_xinerama_query_screens_screen_info(xsq);
89 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
91 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
92 for(int screen = 0; screen < xinerama_screen_number; screen++)
94 bool drop = false;
95 foreach(screen_to_test, globalconf.screens)
96 if(xsi[screen].x_org == screen_to_test->geometry.x
97 && xsi[screen].y_org == screen_to_test->geometry.y)
99 /* we already have a screen for this area, just check if
100 * it's not bigger and drop it */
101 drop = true;
102 int i = screen_array_indexof(&globalconf.screens, screen_to_test);
103 screen_to_test->geometry.width =
104 MAX(xsi[screen].width, xsi[i].width);
105 screen_to_test->geometry.height =
106 MAX(xsi[screen].height, xsi[i].height);
108 if(!drop)
110 screen_t s;
111 p_clear(&s, 1);
112 s.geometry = screen_xsitoarea(xsi[screen]);
113 screen_array_append(&globalconf.screens, s);
117 p_delete(&xsq);
119 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
120 globalconf.screens.tab[0].visual = screen_default_visual(s);
122 else
123 /* One screen only / Zaphod mode */
124 for(int screen = 0;
125 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
126 screen++)
128 xcb_screen_t *xcb_screen = xutil_screen_get(globalconf.connection, screen);
129 screen_t s;
130 p_clear(&s, 1);
131 s.geometry.x = 0;
132 s.geometry.y = 0;
133 s.geometry.width = xcb_screen->width_in_pixels;
134 s.geometry.height = xcb_screen->height_in_pixels;
135 s.visual = screen_default_visual(xcb_screen);
136 screen_array_append(&globalconf.screens, s);
139 globalconf.screen_focus = globalconf.screens.tab;
142 /** Return the Xinerama screen number where the coordinates belongs to.
143 * \param screen The logical screen number.
144 * \param x X coordinate
145 * \param y Y coordinate
146 * \return Screen pointer or screen param if no match or no multi-head.
148 screen_t *
149 screen_getbycoord(screen_t *screen, int x, int y)
151 /* don't waste our time */
152 if(!globalconf.xinerama_is_active)
153 return screen;
155 foreach(s, globalconf.screens)
156 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
157 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
158 return s;
160 return screen;
163 /** Get screens info.
164 * \param screen Screen.
165 * \param strut Honor windows strut.
166 * \return The screen area.
168 area_t
169 screen_area_get(screen_t *screen, bool strut)
171 if(!strut)
172 return screen->geometry;
174 area_t area = screen->geometry;
175 uint16_t top = 0, bottom = 0, left = 0, right = 0;
177 #define COMPUTE_STRUT(o) \
179 if((o)->strut.top_start_x || (o)->strut.top_end_x || (o)->strut.top) \
181 if((o)->strut.top) \
182 top = MAX(top, (o)->strut.top); \
183 else \
184 top = MAX(top, ((o)->geometry.y - area.y) + (o)->geometry.height); \
186 if((o)->strut.bottom_start_x || (o)->strut.bottom_end_x || (o)->strut.bottom) \
188 if((o)->strut.bottom) \
189 bottom = MAX(bottom, (o)->strut.bottom); \
190 else \
191 bottom = MAX(bottom, (area.y + area.height) - (o)->geometry.y); \
193 if((o)->strut.left_start_y || (o)->strut.left_end_y || (o)->strut.left) \
195 if((o)->strut.left) \
196 left = MAX(left, (o)->strut.left); \
197 else \
198 left = MAX(left, ((o)->geometry.x - area.x) + (o)->geometry.width); \
200 if((o)->strut.right_start_y || (o)->strut.right_end_y || (o)->strut.right) \
202 if((o)->strut.right) \
203 right = MAX(right, (o)->strut.right); \
204 else \
205 right = MAX(right, (area.x + area.width) - (o)->geometry.x); \
209 foreach(c, globalconf.clients)
210 if(client_isvisible(*c, screen))
211 COMPUTE_STRUT(*c)
213 foreach(wibox, globalconf.wiboxes)
214 if((*wibox)->visible
215 && (*wibox)->screen == screen)
216 COMPUTE_STRUT(*wibox)
218 #undef COMPUTE_STRUT
220 area.x += left;
221 area.y += top;
222 area.width -= left + right;
223 area.height -= top + bottom;
225 return area;
228 /** Get display info.
229 * \param phys_screen Physical screen number.
230 * \return The display area.
232 area_t
233 display_area_get(int phys_screen)
235 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
236 area_t area = { .x = 0,
237 .y = 0,
238 .width = s->width_in_pixels,
239 .height = s->height_in_pixels };
240 return area;
243 /** This returns the real X screen number for a logical
244 * screen if Xinerama is active.
245 * \param screen The logical screen.
246 * \return The X screen.
249 screen_virttophys(int screen)
251 if(globalconf.xinerama_is_active)
252 return globalconf.default_screen;
253 return screen;
256 /** Move a client to a virtual screen.
257 * \param c The client to move.
258 * \param new_screen The destination screen.
259 * \param doresize Set to true if we also move the client to the new x and
260 * y of the new screen.
262 void
263 screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize)
265 screen_t *old_screen = c->screen;
266 area_t from, to;
267 bool had_focus = false;
269 if(new_screen == c->screen)
270 return;
272 if (globalconf.screens.tab[c->phys_screen].client_focus == c)
273 had_focus = true;
275 c->screen = new_screen;
277 if(c->titlebar)
278 c->titlebar->screen = new_screen;
280 /* If client was on a screen, remove old tags */
281 if(old_screen)
282 foreach(old_tag, old_screen->tags)
283 untag_client(c, *old_tag);
285 if(!doresize)
287 hook_property(c, "screen");
288 luaA_object_push(globalconf.L, c);
289 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
290 lua_pop(globalconf.L, 1);
291 if(had_focus)
292 client_focus(c);
293 return;
296 from = screen_area_get(old_screen, false);
297 to = screen_area_get(c->screen, false);
299 area_t new_geometry = c->geometry;
301 if(c->fullscreen)
303 new_geometry = to;
304 area_t new_f_geometry = c->geometries.fullscreen;
306 new_f_geometry.x = to.x + new_f_geometry.x - from.x;
307 new_f_geometry.y = to.y + new_f_geometry.y - from.x;
309 /* resize the client's original geometry if it doesn't fit the screen */
310 if(new_f_geometry.width > to.width)
311 new_f_geometry.width = to.width;
312 if(new_f_geometry.height > to.height)
313 new_f_geometry.height = to.height;
315 /* make sure the client is still on the screen */
316 if(new_f_geometry.x + new_f_geometry.width > to.x + to.width)
317 new_f_geometry.x = to.x + to.width - new_f_geometry.width;
318 if(new_f_geometry.y + new_f_geometry.height > to.y + to.height)
319 new_f_geometry.y = to.y + to.height - new_f_geometry.height;
321 c->geometries.fullscreen = new_f_geometry;
323 else
325 new_geometry.x = to.x + new_geometry.x - from.x;
326 new_geometry.y = to.y + new_geometry.y - from.y;
328 /* resize the client if it doesn't fit the new screen */
329 if(new_geometry.width > to.width)
330 new_geometry.width = to.width;
331 if(new_geometry.height > to.height)
332 new_geometry.height = to.height;
334 /* make sure the client is still on the screen */
335 if(new_geometry.x + new_geometry.width > to.x + to.width)
336 new_geometry.x = to.x + to.width - new_geometry.width;
337 if(new_geometry.y + new_geometry.height > to.y + to.height)
338 new_geometry.y = to.y + to.height - new_geometry.height;
340 /* move / resize the client */
341 client_resize(c, new_geometry, false);
342 hook_property(c, "screen");
343 luaA_object_push(globalconf.L, c);
344 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
345 lua_pop(globalconf.L, 1);
346 if(had_focus)
347 client_focus(c);
350 /** Push a screen onto the stack.
351 * \param L The Lua VM state.
352 * \param s The screen to push.
353 * \return The number of elements pushed on stack.
355 static int
356 luaA_pushscreen(lua_State *L, screen_t *s)
358 lua_pushlightuserdata(L, s);
359 luaL_getmetatable(L, "screen");
360 lua_setmetatable(L, -2);
361 return 1;
364 /** Screen module.
365 * \param L The Lua VM state.
366 * \return The number of elements pushed on stack.
367 * \luastack
368 * \lfield number The screen number, to get a screen.
370 static int
371 luaA_screen_module_index(lua_State *L)
373 int screen = luaL_checknumber(L, 2) - 1;
374 luaA_checkscreen(screen);
375 return luaA_pushscreen(L, &globalconf.screens.tab[screen]);
378 /** Get screen tags.
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
381 * \luastack
382 * \lparam None or a table of tags to set to the screen.
383 * The table must contains at least one tag.
384 * \return A table with all screen tags.
386 static int
387 luaA_screen_tags(lua_State *L)
389 screen_t *s = luaL_checkudata(L, 1, "screen");
391 if(lua_gettop(L) == 2)
393 luaA_checktable(L, 2);
395 /* Detach all tags, but go backward since the array len will change */
396 for(int i = s->tags.len - 1; i >= 0; i--)
397 tag_remove_from_screen(s->tags.tab[i]);
399 lua_pushnil(L);
400 while(lua_next(L, 2))
401 tag_append_to_screen(L, -1, s);
403 else
405 lua_createtable(L, s->tags.len, 0);
406 for(int i = 0; i < s->tags.len; i++)
408 luaA_object_push(L, s->tags.tab[i]);
409 lua_rawseti(L, -2, i + 1);
413 return 1;
416 /** A screen.
417 * \param L The Lua VM state.
418 * \return The number of elements pushed on stack.
419 * \luastack
420 * \lfield coords The screen coordinates. Immutable.
421 * \lfield workarea The screen workarea.
423 static int
424 luaA_screen_index(lua_State *L)
426 size_t len;
427 const char *buf;
428 screen_t *s;
430 if(luaA_usemetatable(L, 1, 2))
431 return 1;
433 buf = luaL_checklstring(L, 2, &len);
434 s = lua_touserdata(L, 1);
436 switch(a_tokenize(buf, len))
438 case A_TK_INDEX:
439 lua_pushinteger(L, screen_array_indexof(&globalconf.screens, s) + 1);
440 break;
441 case A_TK_GEOMETRY:
442 luaA_pusharea(L, s->geometry);
443 break;
444 case A_TK_WORKAREA:
445 luaA_pusharea(L, screen_area_get(s, true));
446 break;
447 default:
448 return 0;
451 return 1;
454 /** Add a signal to a screen.
455 * \param L The Lua VM state.
456 * \return The number of elements pushed on stack.
457 * \luastack
458 * \lvalue A screen.
459 * \lparam A signal name.
460 * \lparam A function to call when the signal is emitted.
462 static int
463 luaA_screen_add_signal(lua_State *L)
465 screen_t *s = lua_touserdata(L, 1);
466 const char *name = luaL_checkstring(L, 2);
467 luaA_checkfunction(L, 3);
468 signal_add(&s->signals, name, luaA_object_ref(L, 3));
469 return 0;
472 /** Remove a signal to a screen.
473 * \param L The Lua VM state.
474 * \return The number of elements pushed on stack.
475 * \luastack
476 * \lvalue A screen.
477 * \lparam A signal name.
478 * \lparam A function to remove
480 static int
481 luaA_screen_remove_signal(lua_State *L)
483 screen_t *s = lua_touserdata(L, 1);
484 const char *name = luaL_checkstring(L, 2);
485 luaA_checkfunction(L, 3);
486 const void *ref = lua_topointer(L, 3);
487 signal_remove(&s->signals, name, ref);
488 luaA_object_unref(L, (void *) ref);
489 return 0;
492 /** Emit a signal to a screen.
493 * \param L The Lua VM state.
494 * \param screen The screen.
495 * \param name The signal name.
496 * \param nargs The number of arguments to the signal function.
498 void
499 screen_emit_signal(lua_State *L, screen_t *screen, const char *name, int nargs)
501 luaA_pushscreen(L, screen);
502 lua_insert(L, - nargs - 1);
503 signal_object_emit(L, &screen->signals, name, nargs + 1);
506 /** Emit a signal to a screen.
507 * \param L The Lua VM state.
508 * \return The number of elements pushed on stack.
509 * \luastack
510 * \lvalue A screen.
511 * \lparam A signal name.
512 * \lparam Various arguments, optional.
514 static int
515 luaA_screen_emit_signal(lua_State *L)
517 screen_emit_signal(L, lua_touserdata(L, 1), luaL_checkstring(L, 2), lua_gettop(L) - 2);
518 return 0;
521 /** Get the screen count.
522 * \param L The Lua VM state.
523 * \return The number of elements pushed on stack.
525 * \luastack
526 * \lreturn The screen count, at least 1.
528 static int
529 luaA_screen_count(lua_State *L)
531 lua_pushnumber(L, globalconf.screens.len);
532 return 1;
535 const struct luaL_reg awesome_screen_methods[] =
537 { "count", luaA_screen_count },
538 { "__index", luaA_screen_module_index },
539 { NULL, NULL }
542 const struct luaL_reg awesome_screen_meta[] =
544 { "add_signal", luaA_screen_add_signal },
545 { "remove_signal", luaA_screen_remove_signal },
546 { "emit_signal", luaA_screen_emit_signal },
547 { "tags", luaA_screen_tags },
548 { "__index", luaA_screen_index },
549 { NULL, NULL }
552 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80