Various changes to awful.util and invaders
[awesome.git] / screen.c
blob22826b785088bd4cb03168f492feb26e50d1a99c
1 /*
2 * screen.c - screen management
4 * Copyright © 2007-2008 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 "layouts/tile.h"
35 extern awesome_t globalconf;
37 static inline area_t
38 screen_xsitoarea(xcb_xinerama_screen_info_t si)
40 area_t a =
42 .x = si.x_org,
43 .y = si.y_org,
44 .width = si.width,
45 .height = si.height
47 return a;
50 /** Get screens informations and fill global configuration.
52 void
53 screen_scan(void)
55 /* Check for extension before checking for Xinerama */
56 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
58 xcb_xinerama_is_active_reply_t *xia;
59 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
60 globalconf.xinerama_is_active = xia->state;
61 p_delete(&xia);
64 if(globalconf.xinerama_is_active)
66 xcb_xinerama_query_screens_reply_t *xsq;
67 xcb_xinerama_screen_info_t *xsi;
68 int xinerama_screen_number;
70 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
71 xcb_xinerama_query_screens_unchecked(globalconf.connection),
72 NULL);
74 xsi = xcb_xinerama_query_screens_screen_info(xsq);
75 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
77 globalconf.screens = p_new(screen_t, xinerama_screen_number);
79 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
80 for(int screen = 0; screen < xinerama_screen_number; screen++)
82 bool drop = false;
83 for(int screen_to_test = 0; screen_to_test < globalconf.nscreen; screen_to_test++)
84 if(xsi[screen].x_org == globalconf.screens[screen_to_test].geometry.x
85 && xsi[screen].y_org == globalconf.screens[screen_to_test].geometry.y)
87 /* we already have a screen for this area, just check if
88 * it's not bigger and drop it */
89 drop = true;
90 globalconf.screens[screen_to_test].geometry.width =
91 MAX(xsi[screen].width, xsi[screen_to_test].width);
92 globalconf.screens[screen_to_test].geometry.height =
93 MAX(xsi[screen].height, xsi[screen_to_test].height);
95 if(!drop)
97 globalconf.screens[globalconf.nscreen].index = screen;
98 globalconf.screens[globalconf.nscreen++].geometry = screen_xsitoarea(xsi[screen]);
102 /* realloc smaller if xinerama_screen_number != screen registered */
103 if(xinerama_screen_number != globalconf.nscreen)
105 screen_t *new = p_new(screen_t, globalconf.nscreen);
106 memcpy(new, globalconf.screens, globalconf.nscreen * sizeof(screen_t));
107 p_delete(&globalconf.screens);
108 globalconf.screens = new;
111 p_delete(&xsq);
113 else
115 globalconf.nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
116 globalconf.screens = p_new(screen_t, globalconf.nscreen);
117 for(int screen = 0; screen < globalconf.nscreen; screen++)
119 xcb_screen_t *s = xutil_screen_get(globalconf.connection, screen);
120 globalconf.screens[screen].index = screen;
121 globalconf.screens[screen].geometry.x = 0;
122 globalconf.screens[screen].geometry.y = 0;
123 globalconf.screens[screen].geometry.width = s->width_in_pixels;
124 globalconf.screens[screen].geometry.height = s->height_in_pixels;
128 globalconf.screen_focus = globalconf.screens;
131 /** Return the Xinerama screen number where the coordinates belongs to.
132 * \param screen The logical screen number.
133 * \param x X coordinate
134 * \param y Y coordinate
135 * \return Screen number or screen param if no match or no multi-head.
138 screen_getbycoord(int screen, int x, int y)
140 int i;
142 /* don't waste our time */
143 if(!globalconf.xinerama_is_active)
144 return screen;
146 for(i = 0; i < globalconf.nscreen; i++)
148 screen_t *s = &globalconf.screens[i];
149 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
150 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
151 return i;
154 return screen;
157 /** Get screens info.
158 * \param screen Screen number.
159 * \param wiboxes Wiboxes list to remove.
160 * \param padding Padding.
161 * \param strut Honor windows strut.
162 * \return The screen area.
164 area_t
165 screen_area_get(int screen, wibox_array_t *wiboxes,
166 padding_t *padding, bool strut)
168 area_t area = globalconf.screens[screen].geometry;
169 uint16_t top = 0, bottom = 0, left = 0, right = 0;
171 /* make padding corrections */
172 if(padding)
174 area.x += padding->left;
175 area.y += padding->top;
176 area.width -= padding->left + padding->right;
177 area.height -= padding->top + padding->bottom;
180 if(strut)
182 client_t *c;
183 for(c = globalconf.clients; c; c = c->next)
184 if(client_isvisible(c, screen))
186 if(c->strut.top_start_x || c->strut.top_end_x)
188 if(c->strut.top)
189 top = MAX(top, c->strut.top);
190 else
191 top = MAX(top, (c->geometry.y - area.y) + c->geometry.height);
193 if(c->strut.bottom_start_x || c->strut.bottom_end_x)
195 if(c->strut.bottom)
196 bottom = MAX(bottom, c->strut.bottom);
197 else
198 bottom = MAX(bottom, (area.y + area.height) - c->geometry.y);
200 if(c->strut.left_start_y || c->strut.left_end_y)
202 if(c->strut.left)
203 left = MAX(left, c->strut.left);
204 else
205 left = MAX(left, (c->geometry.x - area.x) + c->geometry.width);
207 if(c->strut.right_start_y || c->strut.right_end_y)
209 if(c->strut.right)
210 right = MAX(right, c->strut.right);
211 else
212 right = MAX(right, (area.x + area.width) - c->geometry.x);
218 if(wiboxes)
219 for(int i = 0; i < wiboxes->len; i++)
221 wibox_t *w = wiboxes->tab[i];
222 if(w->isvisible)
223 switch(w->position)
225 case Top:
226 top = MAX(top, (uint16_t) (w->sw.geometry.y - area.y) + w->sw.geometry.height);
227 break;
228 case Bottom:
229 bottom = MAX(bottom, (uint16_t) (area.y + area.height) - w->sw.geometry.y);
230 break;
231 case Left:
232 left = MAX(left, (uint16_t) (w->sw.geometry.x - area.x) + w->sw.geometry.width);
233 break;
234 case Right:
235 right = MAX(right, (uint16_t) (area.x + area.width) - w->sw.geometry.x);
236 break;
237 default:
238 break;
242 area.x += left;
243 area.y += top;
244 area.width -= left + right;
245 area.height -= top + bottom;
247 return area;
250 /** Get display info.
251 * \param phys_screen Physical screen number.
252 * \param wiboxes The wiboxes.
253 * \param padding Padding.
254 * \return The display area.
256 area_t
257 display_area_get(int phys_screen, wibox_array_t *wiboxes, padding_t *padding)
259 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
260 area_t area = { .x = 0,
261 .y = 0,
262 .width = s->width_in_pixels,
263 .height = s->height_in_pixels };
265 if(wiboxes)
266 for(int i = 0; i < wiboxes->len; i++)
268 wibox_t *w = wiboxes->tab[i];
269 area.y += w->position == Top ? w->sw.geometry.height : 0;
270 area.height -= (w->position == Top || w->position == Bottom) ? w->sw.geometry.height : 0;
273 /* make padding corrections */
274 if(padding)
276 area.x += padding->left;
277 area.y += padding->top;
278 area.width -= padding->left + padding->right;
279 area.height -= padding->top + padding->bottom;
281 return area;
284 /** This returns the real X screen number for a logical
285 * screen if Xinerama is active.
286 * \param screen The logical screen.
287 * \return The X screen.
290 screen_virttophys(int screen)
292 if(globalconf.xinerama_is_active)
293 return globalconf.default_screen;
294 return screen;
297 /** Move a client to a virtual screen.
298 * \param c The client to move.
299 * \param new_screen The destinatiuon screen number.
300 * \param dotag Set to true if we also change tags.
301 * \param doresize Set to true if we also move the client to the new x and
302 * y of the new screen.
304 void
305 screen_client_moveto(client_t *c, int new_screen, bool dotag, bool doresize)
307 int i, old_screen = c->screen;
308 tag_array_t *old_tags = &globalconf.screens[old_screen].tags,
309 *new_tags = &globalconf.screens[new_screen].tags;
310 area_t from, to;
311 bool wasvisible = client_isvisible(c, c->screen);
313 c->screen = new_screen;
315 if(c->titlebar)
316 c->titlebar->screen = new_screen;
318 widget_invalidate_cache(old_screen, WIDGET_CACHE_CLIENTS);
319 widget_invalidate_cache(new_screen, WIDGET_CACHE_CLIENTS);
321 if(dotag && !c->issticky)
323 /* remove old tags */
324 for(i = 0; i < old_tags->len; i++)
325 untag_client(c, old_tags->tab[i]);
327 /* add new tags */
328 for(i = 0; i < new_tags->len; i++)
329 if(new_tags->tab[i]->selected)
330 tag_client(c, new_tags->tab[i]);
333 /* resize the windows if it's floating */
334 if(doresize && old_screen != c->screen)
336 area_t new_geometry, new_f_geometry;
337 new_f_geometry = c->f_geometry;
339 to = screen_area_get(c->screen,
340 NULL, NULL, false);
341 from = screen_area_get(old_screen,
342 NULL, NULL, false);
344 /* compute new coords in new screen */
345 new_f_geometry.x = (c->f_geometry.x - from.x) + to.x;
346 new_f_geometry.y = (c->f_geometry.y - from.y) + to.y;
348 /* check that new coords are still in the screen */
349 if(new_f_geometry.width > to.width)
350 new_f_geometry.width = to.width;
351 if(new_f_geometry.height > to.height)
352 new_f_geometry.height = to.height;
353 if(new_f_geometry.x + new_f_geometry.width >= to.x + to.width)
354 new_f_geometry.x = to.x + to.width - new_f_geometry.width - 2 * c->border;
355 if(new_f_geometry.y + new_f_geometry.height >= to.y + to.height)
356 new_f_geometry.y = to.y + to.height - new_f_geometry.height - 2 * c->border;
358 if(c->isfullscreen)
360 new_geometry = c->geometry;
362 /* compute new coords in new screen */
363 new_geometry.x = (c->geometry.x - from.x) + to.x;
364 new_geometry.y = (c->geometry.y - from.y) + to.y;
366 /* check that new coords are still in the 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;
371 if(new_geometry.x + new_geometry.width >= to.x + to.width)
372 new_geometry.x = to.x + to.width - new_geometry.width - 2 * c->border;
373 if(new_geometry.y + new_geometry.height >= to.y + to.height)
374 new_geometry.y = to.y + to.height - new_geometry.height - 2 * c->border;
376 /* compute new coords for max in new screen */
377 c->m_geometry.x = (c->m_geometry.x - from.x) + to.x;
378 c->m_geometry.y = (c->m_geometry.y - from.y) + to.y;
380 /* check that new coords are still in the screen */
381 if(c->m_geometry.width > to.width)
382 c->m_geometry.width = to.width;
383 if(c->m_geometry.height > to.height)
384 c->m_geometry.height = to.height;
385 if(c->m_geometry.x + c->m_geometry.width >= to.x + to.width)
386 c->m_geometry.x = to.x + to.width - c->m_geometry.width - 2 * c->border;
387 if(c->m_geometry.y + c->m_geometry.height >= to.y + to.height)
388 c->m_geometry.y = to.y + to.height - c->m_geometry.height - 2 * c->border;
390 client_resize(c, new_geometry, false);
392 /* if floating, move to this new coords */
393 else if(client_isfloating(c))
394 client_resize(c, new_f_geometry, false);
395 /* otherwise just register them */
396 else
398 c->f_geometry = new_f_geometry;
399 if(wasvisible)
400 globalconf.screens[old_screen].need_arrange = true;
401 client_need_arrange(c);
406 /** Screen module.
407 * \param L The Lua VM state.
408 * \return The number of elements pushed on stack.
409 * \luastack
410 * \lfield number The screen number, to get a screen.
412 static int
413 luaA_screen_module_index(lua_State *L)
415 int screen = luaL_checknumber(L, 2) - 1;
417 luaA_checkscreen(screen);
418 lua_pushlightuserdata(L, &globalconf.screens[screen]);
419 return luaA_settype(L, "screen");
422 /** Get or set screen tags.
423 * \param L The Lua VM state.
424 * \return The number of elements pushed on stack.
425 * \luastack
426 * \lparam None or a table of tags to set to the screen.
427 * The table must contains at least one tag.
428 * \return A table with all screen tags.
430 static int
431 luaA_screen_tags(lua_State *L)
433 int i;
434 screen_t *s = lua_touserdata(L, 1);
436 if(!s)
437 luaL_typerror(L, 1, "screen");
439 if(lua_gettop(L) == 2)
441 tag_t **tag;
443 luaA_checktable(L, 2);
445 /* remove current tags */
446 for(i = 0; i < s->tags.len; i++)
447 s->tags.tab[i]->screen = SCREEN_UNDEF;
449 tag_array_wipe(&s->tags);
450 tag_array_init(&s->tags);
452 s->need_arrange = true;
454 /* push new tags */
455 lua_pushnil(L);
456 while(lua_next(L, 2))
458 tag = luaA_checkudata(L, -1, "tag");
459 tag_append_to_screen(*tag, s);
460 lua_pop(L, 1);
463 /* check there's at least one tag! */
464 if(!s->tags.len)
466 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0), s);
467 luaL_error(L, "no tag were added on screen %d, taking last resort action and adding default tag\n", s->index);
470 else
472 lua_newtable(L);
473 for(i = 0; i < s->tags.len; i++)
475 luaA_tag_userdata_new(L, s->tags.tab[i]);
476 lua_rawseti(L, -2, i + 1);
480 return 1;
483 /** A screen.
484 * \param L The Lua VM state.
485 * \return The number of elements pushed on stack.
486 * \luastack
487 * \lfield coords The screen coordinates. Immutable.
488 * \lfield workarea The screen workarea, i.e. without wiboxes.
490 static int
491 luaA_screen_index(lua_State *L)
493 size_t len;
494 const char *buf;
495 screen_t *s;
497 if(luaA_usemetatable(L, 1, 2))
498 return 1;
500 buf = luaL_checklstring(L, 2, &len);
501 s = lua_touserdata(L, 1);
503 switch(a_tokenize(buf, len))
505 case A_TK_COORDS:
506 luaA_pusharea(L, s->geometry);
507 break;
508 case A_TK_WORKAREA:
509 luaA_pusharea(L, screen_area_get(s->index, &s->wiboxes, &s->padding, true));
510 break;
511 default:
512 return 0;
515 return 1;
518 /** Set or get the screen padding.
519 * \param L The Lua VM state.
520 * \return The number of elements pushed on stack.
521 * \luastack
522 * \lparam None or a table with new padding values.
523 * \lreturn The screen padding. A table with top, right, left and bottom
524 * keys and values in pixel.
526 static int
527 luaA_screen_padding(lua_State *L)
529 screen_t *s = lua_touserdata(L, 1);
531 if(!s)
532 luaL_typerror(L, 1, "screen");
534 if(lua_gettop(L) == 2)
536 luaA_checktable(L, 2);
538 s->padding.right = luaA_getopt_number(L, 2, "right", 0);
539 s->padding.left = luaA_getopt_number(L, 2, "left", 0);
540 s->padding.top = luaA_getopt_number(L, 2, "top", 0);
541 s->padding.bottom = luaA_getopt_number(L, 2, "bottom", 0);
543 s->need_arrange = true;
545 /* All the wiboxes repositioned */
546 for(int i = 0; i < s->wiboxes.len; i++)
547 wibox_position_update(s->wiboxes.tab[i]);
549 ewmh_update_workarea(screen_virttophys(s->index));
551 else
553 lua_newtable(L);
554 lua_pushnumber(L, s->padding.right);
555 lua_setfield(L, -2, "right");
556 lua_pushnumber(L, s->padding.left);
557 lua_setfield(L, -2, "left");
558 lua_pushnumber(L, s->padding.top);
559 lua_setfield(L, -2, "top");
560 lua_pushnumber(L, s->padding.bottom);
561 lua_setfield(L, -2, "bottom");
564 return 1;
567 /** Get the screen count.
568 * \param L The Lua VM state.
569 * \return The number of elements pushed on stack.
571 * \luastack
572 * \lreturn The screen count, at least 1.
574 static int
575 luaA_screen_count(lua_State *L)
577 lua_pushnumber(L, globalconf.nscreen);
578 return 1;
581 const struct luaL_reg awesome_screen_methods[] =
583 { "count", luaA_screen_count },
584 { "__index", luaA_screen_module_index },
585 { NULL, NULL }
588 const struct luaL_reg awesome_screen_meta[] =
590 { "tags", luaA_screen_tags },
591 { "padding", luaA_screen_padding },
592 { "__index", luaA_screen_index },
593 { NULL, NULL }
596 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80