change codename
[awesome.git] / screen.c
blob9fd2c06554373aa946416981ef733b8a31bd3430
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 "common/xutil.h"
35 static inline area_t
36 screen_xsitoarea(xcb_xinerama_screen_info_t si)
38 area_t a =
40 .x = si.x_org,
41 .y = si.y_org,
42 .width = si.width,
43 .height = si.height
45 return a;
48 /** Get screens informations and fill global configuration.
50 void
51 screen_scan(void)
53 /* Check for extension before checking for Xinerama */
54 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
56 xcb_xinerama_is_active_reply_t *xia;
57 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
58 globalconf.xinerama_is_active = xia->state;
59 p_delete(&xia);
62 if(globalconf.xinerama_is_active)
64 xcb_xinerama_query_screens_reply_t *xsq;
65 xcb_xinerama_screen_info_t *xsi;
66 int xinerama_screen_number;
68 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
69 xcb_xinerama_query_screens_unchecked(globalconf.connection),
70 NULL);
72 xsi = xcb_xinerama_query_screens_screen_info(xsq);
73 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
75 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
76 for(int screen = 0; screen < xinerama_screen_number; screen++)
78 bool drop = false;
79 foreach(screen_to_test, globalconf.screens)
80 if(xsi[screen].x_org == screen_to_test->geometry.x
81 && xsi[screen].y_org == screen_to_test->geometry.y)
83 /* we already have a screen for this area, just check if
84 * it's not bigger and drop it */
85 drop = true;
86 int i = screen_array_indexof(&globalconf.screens, screen_to_test);
87 screen_to_test->geometry.width =
88 MAX(xsi[screen].width, xsi[i].width);
89 screen_to_test->geometry.height =
90 MAX(xsi[screen].height, xsi[i].height);
92 if(!drop)
94 screen_t s;
95 p_clear(&s, 1);
96 s.geometry = screen_xsitoarea(xsi[screen]);
97 screen_array_append(&globalconf.screens, s);
101 p_delete(&xsq);
103 else
104 /* One screen only / Zaphod mode */
105 for(int screen = 0;
106 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
107 screen++)
109 xcb_screen_t *xcb_screen = xutil_screen_get(globalconf.connection, screen);
110 screen_t s;
111 p_clear(&s, 1);
112 s.geometry.x = 0;
113 s.geometry.y = 0;
114 s.geometry.width = xcb_screen->width_in_pixels;
115 s.geometry.height = xcb_screen->height_in_pixels;
116 screen_array_append(&globalconf.screens, s);
119 globalconf.screen_focus = globalconf.screens.tab;
122 /** Return the Xinerama screen number where the coordinates belongs to.
123 * \param screen The logical screen number.
124 * \param x X coordinate
125 * \param y Y coordinate
126 * \return Screen pointer or screen param if no match or no multi-head.
128 screen_t *
129 screen_getbycoord(screen_t *screen, int x, int y)
131 /* don't waste our time */
132 if(!globalconf.xinerama_is_active)
133 return screen;
135 foreach(s, globalconf.screens)
136 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
137 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
138 return s;
140 return screen;
143 /** Get screens info.
144 * \param screen Screen.
145 * \param wiboxes Wiboxes list to remove.
146 * \param padding Padding.
147 * \param strut Honor windows strut.
148 * \return The screen area.
150 area_t
151 screen_area_get(screen_t *screen, wibox_array_t *wiboxes,
152 padding_t *padding, bool strut)
154 area_t area = screen->geometry;
155 uint16_t top = 0, bottom = 0, left = 0, right = 0;
157 /* make padding corrections */
158 if(padding)
160 area.x += padding->left;
161 area.y += padding->top;
162 area.width -= padding->left + padding->right;
163 area.height -= padding->top + padding->bottom;
166 if(strut)
167 foreach(_c, globalconf.clients)
169 client_t *c = *_c;
170 if(client_isvisible(c, screen))
172 if(c->strut.top_start_x || c->strut.top_end_x)
174 if(c->strut.top)
175 top = MAX(top, c->strut.top);
176 else
177 top = MAX(top, (c->geometry.y - area.y) + c->geometry.height);
179 if(c->strut.bottom_start_x || c->strut.bottom_end_x)
181 if(c->strut.bottom)
182 bottom = MAX(bottom, c->strut.bottom);
183 else
184 bottom = MAX(bottom, (area.y + area.height) - c->geometry.y);
186 if(c->strut.left_start_y || c->strut.left_end_y)
188 if(c->strut.left)
189 left = MAX(left, c->strut.left);
190 else
191 left = MAX(left, (c->geometry.x - area.x) + c->geometry.width);
193 if(c->strut.right_start_y || c->strut.right_end_y)
195 if(c->strut.right)
196 right = MAX(right, c->strut.right);
197 else
198 right = MAX(right, (area.x + area.width) - c->geometry.x);
204 if(wiboxes)
205 foreach(_w, *wiboxes)
207 wibox_t *w = *_w;
208 if(w->isvisible)
209 switch(w->position)
211 case Top:
212 top = MAX(top, (uint16_t) (w->sw.geometry.y - area.y) + w->sw.geometry.height + 2 * w->sw.border.width);
213 break;
214 case Bottom:
215 bottom = MAX(bottom, (uint16_t) (area.y + area.height) - w->sw.geometry.y);
216 break;
217 case Left:
218 left = MAX(left, (uint16_t) (w->sw.geometry.x - area.x) + w->sw.geometry.width + 2 * w->sw.border.width);
219 break;
220 case Right:
221 right = MAX(right, (uint16_t) (area.x + area.width) - w->sw.geometry.x);
222 break;
223 default:
224 break;
228 area.x += left;
229 area.y += top;
230 area.width -= left + right;
231 area.height -= top + bottom;
233 return area;
236 /** Get display info.
237 * \param phys_screen Physical screen number.
238 * \param wiboxes The wiboxes.
239 * \param padding Padding.
240 * \return The display area.
242 area_t
243 display_area_get(int phys_screen, wibox_array_t *wiboxes, padding_t *padding)
245 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
246 area_t area = { .x = 0,
247 .y = 0,
248 .width = s->width_in_pixels,
249 .height = s->height_in_pixels };
251 if(wiboxes)
252 foreach(_w, *wiboxes)
254 wibox_t *w = *_w;
255 area.y += w->position == Top ? w->sw.geometry.height : 0;
256 area.height -= (w->position == Top || w->position == Bottom) ? w->sw.geometry.height : 0;
259 /* make padding corrections */
260 if(padding)
262 area.x += padding->left;
263 area.y += padding->top;
264 area.width -= padding->left + padding->right;
265 area.height -= padding->top + padding->bottom;
267 return area;
270 /** This returns the real X screen number for a logical
271 * screen if Xinerama is active.
272 * \param screen The logical screen.
273 * \return The X screen.
276 screen_virttophys(int screen)
278 if(globalconf.xinerama_is_active)
279 return globalconf.default_screen;
280 return screen;
283 /** Move a client to a virtual screen.
284 * \param c The client to move.
285 * \param new_screen The destinatiuon screen.
286 * \param dotag Set to true if we also change tags.
287 * \param doresize Set to true if we also move the client to the new x and
288 * y of the new screen.
290 void
291 screen_client_moveto(client_t *c, screen_t *new_screen, bool dotag, bool doresize)
293 int i;
294 screen_t *old_screen = c->screen;
295 tag_array_t *old_tags = &old_screen->tags,
296 *new_tags = &new_screen->tags;
297 area_t from, to;
298 bool wasvisible = client_isvisible(c, c->screen);
300 if(new_screen == c->screen)
301 return;
303 c->screen = new_screen;
305 if(c->titlebar)
306 c->titlebar->screen = new_screen;
308 if(dotag && !c->issticky)
310 /* remove old tags */
311 for(i = 0; i < old_tags->len; i++)
312 untag_client(c, old_tags->tab[i]);
314 /* add new tags */
315 foreach(new_tag, *new_tags)
316 if((*new_tag)->selected)
318 tag_push(globalconf.L, *new_tag);
319 tag_client(c);
323 if(wasvisible)
324 old_screen->need_arrange = true;
325 client_need_arrange(c);
327 if(!doresize)
328 return;
330 from = screen_area_get(old_screen, NULL, NULL, false);
331 to = screen_area_get(c->screen, NULL, NULL, false);
333 area_t new_geometry = c->geometry;
335 if(c->isfullscreen)
337 new_geometry = to;
338 area_t new_f_geometry = c->geometries.fullscreen;
340 new_f_geometry.x = to.x + new_f_geometry.x - from.x;
341 new_f_geometry.y = to.y + new_f_geometry.y - from.x;
343 /* resize the client's original geometry if it doesn't fit the screen */
344 if(new_f_geometry.width > to.width)
345 new_f_geometry.width = to.width;
346 if(new_f_geometry.height > to.height)
347 new_f_geometry.height = to.height;
349 /* make sure the client is still on the screen */
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;
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;
355 c->geometries.fullscreen = new_f_geometry;
357 else
359 new_geometry.x = to.x + new_geometry.x - from.x;
360 new_geometry.y = to.y + new_geometry.y - from.y;
362 /* resize the client if it doesn't fit the new screen */
363 if(new_geometry.width > to.width)
364 new_geometry.width = to.width;
365 if(new_geometry.height > to.height)
366 new_geometry.height = to.height;
368 /* make sure the client is still on the screen */
369 if(new_geometry.x + new_geometry.width > to.x + to.width)
370 new_geometry.x = to.x + to.width - new_geometry.width;
371 if(new_geometry.y + new_geometry.height > to.y + to.height)
372 new_geometry.y = to.y + to.height - new_geometry.height;
374 /* move / resize the client */
375 client_resize(c, new_geometry, false);
378 /** Screen module.
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
381 * \luastack
382 * \lfield number The screen number, to get a screen.
384 static int
385 luaA_screen_module_index(lua_State *L)
387 int screen = luaL_checknumber(L, 2) - 1;
389 luaA_checkscreen(screen);
390 lua_pushlightuserdata(L, &globalconf.screens.tab[screen]);
391 return luaA_settype(L, "screen");
394 /** Get or set screen tags.
395 * \param L The Lua VM state.
396 * \return The number of elements pushed on stack.
397 * \luastack
398 * \lparam None or a table of tags to set to the screen.
399 * The table must contains at least one tag.
400 * \return A table with all screen tags.
402 static int
403 luaA_screen_tags(lua_State *L)
405 int i;
406 screen_t *s = lua_touserdata(L, 1);
408 if(!s)
409 luaL_typerror(L, 1, "screen");
411 if(lua_gettop(L) == 2)
413 luaA_checktable(L, 2);
415 /* remove current tags */
416 for(i = 0; i < s->tags.len; i++)
417 s->tags.tab[i]->screen = NULL;
419 tag_array_wipe(&s->tags);
420 tag_array_init(&s->tags);
422 s->need_arrange = true;
424 /* push new tags */
425 lua_pushnil(L);
426 while(lua_next(L, 2))
427 tag_append_to_screen(s);
429 else
431 lua_createtable(L, s->tags.len, 0);
432 for(i = 0; i < s->tags.len; i++)
434 tag_push(L, s->tags.tab[i]);
435 lua_rawseti(L, -2, i + 1);
439 return 1;
442 /** A screen.
443 * \param L The Lua VM state.
444 * \return The number of elements pushed on stack.
445 * \luastack
446 * \lfield coords The screen coordinates. Immutable.
447 * \lfield workarea The screen workarea, i.e. without wiboxes.
449 static int
450 luaA_screen_index(lua_State *L)
452 size_t len;
453 const char *buf;
454 screen_t *s;
456 if(luaA_usemetatable(L, 1, 2))
457 return 1;
459 buf = luaL_checklstring(L, 2, &len);
460 s = lua_touserdata(L, 1);
462 switch(a_tokenize(buf, len))
464 case A_TK_GEOMETRY:
465 luaA_pusharea(L, s->geometry);
466 break;
467 case A_TK_WORKAREA:
468 luaA_pusharea(L, screen_area_get(s, &s->wiboxes, &s->padding, true));
469 break;
470 default:
471 return 0;
474 return 1;
477 /** Set or get the screen padding.
478 * \param L The Lua VM state.
479 * \return The number of elements pushed on stack.
480 * \luastack
481 * \lparam None or a table with new padding values.
482 * \lreturn The screen padding. A table with top, right, left and bottom
483 * keys and values in pixel.
485 static int
486 luaA_screen_padding(lua_State *L)
488 screen_t *s = lua_touserdata(L, 1);
490 if(!s)
491 luaL_typerror(L, 1, "screen");
493 if(lua_gettop(L) == 2)
495 s->padding = luaA_getopt_padding(L, 2, &s->padding);
497 s->need_arrange = true;
499 /* All the wiboxes repositioned */
500 foreach(w, s->wiboxes)
501 wibox_position_update(*w);
503 ewmh_update_workarea(screen_virttophys(screen_array_indexof(&globalconf.screens, s)));
505 return luaA_pushpadding(L, &s->padding);
508 /** Get the screen count.
509 * \param L The Lua VM state.
510 * \return The number of elements pushed on stack.
512 * \luastack
513 * \lreturn The screen count, at least 1.
515 static int
516 luaA_screen_count(lua_State *L)
518 lua_pushnumber(L, globalconf.screens.len);
519 return 1;
522 const struct luaL_reg awesome_screen_methods[] =
524 { "count", luaA_screen_count },
525 { "__index", luaA_screen_module_index },
526 { NULL, NULL }
529 const struct luaL_reg awesome_screen_meta[] =
531 { "tags", luaA_screen_tags },
532 { "padding", luaA_screen_padding },
533 { "__index", luaA_screen_index },
534 { NULL, NULL }
537 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80