change codename
[awesome.git] / screen.c
blob45a78d12c5da53c2a51c8bf373f3011f92753744
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 + 2 * w->sw.border.width);
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 + 2 * w->sw.border.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 if(dotag && !c->issticky)
320 /* remove old tags */
321 for(i = 0; i < old_tags->len; i++)
322 untag_client(c, old_tags->tab[i]);
324 /* add new tags */
325 for(i = 0; i < new_tags->len; i++)
326 if(new_tags->tab[i]->selected)
327 tag_client(c, new_tags->tab[i]);
330 /* resize the windows if it's floating */
331 if(doresize && old_screen != c->screen)
333 area_t new_geometry, new_f_geometry;
334 new_f_geometry = c->f_geometry;
336 to = screen_area_get(c->screen,
337 NULL, NULL, false);
338 from = screen_area_get(old_screen,
339 NULL, NULL, false);
341 /* compute new coords in new screen */
342 new_f_geometry.x = (c->f_geometry.x - from.x) + to.x;
343 new_f_geometry.y = (c->f_geometry.y - from.y) + to.y;
345 /* check that new coords are still in the screen */
346 if(new_f_geometry.width > to.width)
347 new_f_geometry.width = to.width;
348 if(new_f_geometry.height > to.height)
349 new_f_geometry.height = to.height;
350 if(new_f_geometry.x + new_f_geometry.width >= to.x + to.width)
351 new_f_geometry.x = to.x + to.width - new_f_geometry.width - 2 * c->border;
352 if(new_f_geometry.y + new_f_geometry.height >= to.y + to.height)
353 new_f_geometry.y = to.y + to.height - new_f_geometry.height - 2 * c->border;
355 if(c->isfullscreen)
357 new_geometry = c->geometry;
359 /* compute new coords in new screen */
360 new_geometry.x = (c->geometry.x - from.x) + to.x;
361 new_geometry.y = (c->geometry.y - from.y) + to.y;
363 /* check that new coords are still in the screen */
364 if(new_geometry.width > to.width)
365 new_geometry.width = to.width;
366 if(new_geometry.height > to.height)
367 new_geometry.height = to.height;
368 if(new_geometry.x + new_geometry.width >= to.x + to.width)
369 new_geometry.x = to.x + to.width - new_geometry.width - 2 * c->border;
370 if(new_geometry.y + new_geometry.height >= to.y + to.height)
371 new_geometry.y = to.y + to.height - new_geometry.height - 2 * c->border;
373 /* compute new coords for max in new screen */
374 c->m_geometry.x = (c->m_geometry.x - from.x) + to.x;
375 c->m_geometry.y = (c->m_geometry.y - from.y) + to.y;
377 /* check that new coords are still in the screen */
378 if(c->m_geometry.width > to.width)
379 c->m_geometry.width = to.width;
380 if(c->m_geometry.height > to.height)
381 c->m_geometry.height = to.height;
382 if(c->m_geometry.x + c->m_geometry.width >= to.x + to.width)
383 c->m_geometry.x = to.x + to.width - c->m_geometry.width - 2 * c->border;
384 if(c->m_geometry.y + c->m_geometry.height >= to.y + to.height)
385 c->m_geometry.y = to.y + to.height - c->m_geometry.height - 2 * c->border;
387 client_resize(c, new_geometry, false);
389 /* if floating, move to this new coords */
390 else if(client_isfloating(c))
391 client_resize(c, new_f_geometry, false);
392 /* otherwise just register them */
393 else
395 c->f_geometry = new_f_geometry;
396 if(wasvisible)
397 globalconf.screens[old_screen].need_arrange = true;
398 client_need_arrange(c);
403 /** Screen module.
404 * \param L The Lua VM state.
405 * \return The number of elements pushed on stack.
406 * \luastack
407 * \lfield number The screen number, to get a screen.
409 static int
410 luaA_screen_module_index(lua_State *L)
412 int screen = luaL_checknumber(L, 2) - 1;
414 luaA_checkscreen(screen);
415 lua_pushlightuserdata(L, &globalconf.screens[screen]);
416 return luaA_settype(L, "screen");
419 /** Get or set screen tags.
420 * \param L The Lua VM state.
421 * \return The number of elements pushed on stack.
422 * \luastack
423 * \lparam None or a table of tags to set to the screen.
424 * The table must contains at least one tag.
425 * \return A table with all screen tags.
427 static int
428 luaA_screen_tags(lua_State *L)
430 int i;
431 screen_t *s = lua_touserdata(L, 1);
433 if(!s)
434 luaL_typerror(L, 1, "screen");
436 if(lua_gettop(L) == 2)
438 tag_t **tag;
440 luaA_checktable(L, 2);
442 /* remove current tags */
443 for(i = 0; i < s->tags.len; i++)
444 s->tags.tab[i]->screen = SCREEN_UNDEF;
446 tag_array_wipe(&s->tags);
447 tag_array_init(&s->tags);
449 s->need_arrange = true;
451 /* push new tags */
452 lua_pushnil(L);
453 while(lua_next(L, 2))
455 tag = luaA_checkudata(L, -1, "tag");
456 tag_append_to_screen(*tag, s);
457 lua_pop(L, 1);
460 /* check there's at least one tag! */
461 if(!s->tags.len)
463 luaA_warn(L, "screen %d has no tag, taking last resort action and adding default tag\n",
464 s->index);
465 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0), s);
466 return 1;
469 else
471 lua_newtable(L);
472 for(i = 0; i < s->tags.len; i++)
474 luaA_tag_userdata_new(L, s->tags.tab[i]);
475 lua_rawseti(L, -2, i + 1);
479 return 1;
482 /** A screen.
483 * \param L The Lua VM state.
484 * \return The number of elements pushed on stack.
485 * \luastack
486 * \lfield coords The screen coordinates. Immutable.
487 * \lfield workarea The screen workarea, i.e. without wiboxes.
489 static int
490 luaA_screen_index(lua_State *L)
492 size_t len;
493 const char *buf;
494 screen_t *s;
496 if(luaA_usemetatable(L, 1, 2))
497 return 1;
499 buf = luaL_checklstring(L, 2, &len);
500 s = lua_touserdata(L, 1);
502 switch(a_tokenize(buf, len))
504 case A_TK_COORDS:
505 deprecate(L, "geometry attribute");
506 case A_TK_GEOMETRY:
507 luaA_pusharea(L, s->geometry);
508 break;
509 case A_TK_WORKAREA:
510 luaA_pusharea(L, screen_area_get(s->index, &s->wiboxes, &s->padding, true));
511 break;
512 default:
513 return 0;
516 return 1;
519 /** Set or get the screen padding.
520 * \param L The Lua VM state.
521 * \return The number of elements pushed on stack.
522 * \luastack
523 * \lparam None or a table with new padding values.
524 * \lreturn The screen padding. A table with top, right, left and bottom
525 * keys and values in pixel.
527 static int
528 luaA_screen_padding(lua_State *L)
530 screen_t *s = lua_touserdata(L, 1);
532 if(!s)
533 luaL_typerror(L, 1, "screen");
535 if(lua_gettop(L) == 2)
537 luaA_checktable(L, 2);
539 s->padding.right = luaA_getopt_number(L, 2, "right", 0);
540 s->padding.left = luaA_getopt_number(L, 2, "left", 0);
541 s->padding.top = luaA_getopt_number(L, 2, "top", 0);
542 s->padding.bottom = luaA_getopt_number(L, 2, "bottom", 0);
544 s->need_arrange = true;
546 /* All the wiboxes repositioned */
547 for(int i = 0; i < s->wiboxes.len; i++)
548 wibox_position_update(s->wiboxes.tab[i]);
550 ewmh_update_workarea(screen_virttophys(s->index));
552 else
554 lua_newtable(L);
555 lua_pushnumber(L, s->padding.right);
556 lua_setfield(L, -2, "right");
557 lua_pushnumber(L, s->padding.left);
558 lua_setfield(L, -2, "left");
559 lua_pushnumber(L, s->padding.top);
560 lua_setfield(L, -2, "top");
561 lua_pushnumber(L, s->padding.bottom);
562 lua_setfield(L, -2, "bottom");
565 return 1;
568 /** Get the screen count.
569 * \param L The Lua VM state.
570 * \return The number of elements pushed on stack.
572 * \luastack
573 * \lreturn The screen count, at least 1.
575 static int
576 luaA_screen_count(lua_State *L)
578 lua_pushnumber(L, globalconf.nscreen);
579 return 1;
582 const struct luaL_reg awesome_screen_methods[] =
584 { "count", luaA_screen_count },
585 { "__index", luaA_screen_module_index },
586 { NULL, NULL }
589 const struct luaL_reg awesome_screen_meta[] =
591 { "tags", luaA_screen_tags },
592 { "padding", luaA_screen_padding },
593 { "__index", luaA_screen_index },
594 { NULL, NULL }
597 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80