awesomerc: make vile default layout
[awesome.git] / screen.c
blobe2cce317c001d0a211da358a777fbdf30ffd15a7
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"
34 extern awesome_t globalconf;
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 /** Get screens informations and fill global configuration.
51 void
52 screen_scan(void)
54 /* Check for extension before checking for Xinerama */
55 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
57 xcb_xinerama_is_active_reply_t *xia;
58 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
59 globalconf.xinerama_is_active = xia->state;
60 p_delete(&xia);
63 if(globalconf.xinerama_is_active)
65 xcb_xinerama_query_screens_reply_t *xsq;
66 xcb_xinerama_screen_info_t *xsi;
67 int xinerama_screen_number;
69 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
70 xcb_xinerama_query_screens_unchecked(globalconf.connection),
71 NULL);
73 xsi = xcb_xinerama_query_screens_screen_info(xsq);
74 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
76 globalconf.screens = p_new(screen_t, xinerama_screen_number);
78 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
79 for(int screen = 0; screen < xinerama_screen_number; screen++)
81 bool drop = false;
82 for(int screen_to_test = 0; screen_to_test < globalconf.nscreen; screen_to_test++)
83 if(xsi[screen].x_org == globalconf.screens[screen_to_test].geometry.x
84 && xsi[screen].y_org == globalconf.screens[screen_to_test].geometry.y)
86 /* we already have a screen for this area, just check if
87 * it's not bigger and drop it */
88 drop = true;
89 globalconf.screens[screen_to_test].geometry.width =
90 MAX(xsi[screen].width, xsi[screen_to_test].width);
91 globalconf.screens[screen_to_test].geometry.height =
92 MAX(xsi[screen].height, xsi[screen_to_test].height);
94 if(!drop)
96 globalconf.screens[globalconf.nscreen].index = screen;
97 globalconf.screens[globalconf.nscreen++].geometry = screen_xsitoarea(xsi[screen]);
101 /* realloc smaller if xinerama_screen_number != screen registered */
102 if(xinerama_screen_number != globalconf.nscreen)
104 screen_t *new = p_new(screen_t, globalconf.nscreen);
105 memcpy(new, globalconf.screens, globalconf.nscreen * sizeof(screen_t));
106 p_delete(&globalconf.screens);
107 globalconf.screens = new;
110 p_delete(&xsq);
112 else
114 globalconf.nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
115 globalconf.screens = p_new(screen_t, globalconf.nscreen);
116 for(int screen = 0; screen < globalconf.nscreen; screen++)
118 xcb_screen_t *s = xutil_screen_get(globalconf.connection, screen);
119 globalconf.screens[screen].index = screen;
120 globalconf.screens[screen].geometry.x = 0;
121 globalconf.screens[screen].geometry.y = 0;
122 globalconf.screens[screen].geometry.width = s->width_in_pixels;
123 globalconf.screens[screen].geometry.height = s->height_in_pixels;
127 globalconf.screen_focus = globalconf.screens;
130 /** Return the Xinerama screen number where the coordinates belongs to.
131 * \param screen The logical screen number.
132 * \param x X coordinate
133 * \param y Y coordinate
134 * \return Screen number or screen param if no match or no multi-head.
137 screen_getbycoord(int screen, int x, int y)
139 int i;
141 /* don't waste our time */
142 if(!globalconf.xinerama_is_active)
143 return screen;
145 for(i = 0; i < globalconf.nscreen; i++)
147 screen_t *s = &globalconf.screens[i];
148 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
149 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
150 return i;
153 return screen;
156 /** Get screens info.
157 * \param screen Screen number.
158 * \param wiboxes Wiboxes list to remove.
159 * \param padding Padding.
160 * \param strut Honor windows strut.
161 * \return The screen area.
163 area_t
164 screen_area_get(int screen, wibox_array_t *wiboxes,
165 padding_t *padding, bool strut)
167 area_t area = globalconf.screens[screen].geometry;
168 uint16_t top = 0, bottom = 0, left = 0, right = 0;
170 /* make padding corrections */
171 if(padding)
173 area.x += padding->left;
174 area.y += padding->top;
175 area.width -= padding->left + padding->right;
176 area.height -= padding->top + padding->bottom;
179 if(strut)
181 client_t *c;
182 for(c = globalconf.clients; c; c = c->next)
183 if(client_isvisible(c, screen))
185 if(c->strut.top_start_x || c->strut.top_end_x)
187 if(c->strut.top)
188 top = MAX(top, c->strut.top);
189 else
190 top = MAX(top, (c->geometry.y - area.y) + c->geometry.height);
192 if(c->strut.bottom_start_x || c->strut.bottom_end_x)
194 if(c->strut.bottom)
195 bottom = MAX(bottom, c->strut.bottom);
196 else
197 bottom = MAX(bottom, (area.y + area.height) - c->geometry.y);
199 if(c->strut.left_start_y || c->strut.left_end_y)
201 if(c->strut.left)
202 left = MAX(left, c->strut.left);
203 else
204 left = MAX(left, (c->geometry.x - area.x) + c->geometry.width);
206 if(c->strut.right_start_y || c->strut.right_end_y)
208 if(c->strut.right)
209 right = MAX(right, c->strut.right);
210 else
211 right = MAX(right, (area.x + area.width) - c->geometry.x);
217 if(wiboxes)
218 for(int i = 0; i < wiboxes->len; i++)
220 wibox_t *w = wiboxes->tab[i];
221 if(w->isvisible)
222 switch(w->position)
224 case Top:
225 top = MAX(top, (uint16_t) (w->sw.geometry.y - area.y) + w->sw.geometry.height + 2 * w->sw.border.width);
226 break;
227 case Bottom:
228 bottom = MAX(bottom, (uint16_t) (area.y + area.height) - w->sw.geometry.y);
229 break;
230 case Left:
231 left = MAX(left, (uint16_t) (w->sw.geometry.x - area.x) + w->sw.geometry.width + 2 * w->sw.border.width);
232 break;
233 case Right:
234 right = MAX(right, (uint16_t) (area.x + area.width) - w->sw.geometry.x);
235 break;
236 default:
237 break;
241 area.x += left;
242 area.y += top;
243 area.width -= left + right;
244 area.height -= top + bottom;
246 return area;
249 /** Get display info.
250 * \param phys_screen Physical screen number.
251 * \param wiboxes The wiboxes.
252 * \param padding Padding.
253 * \return The display area.
255 area_t
256 display_area_get(int phys_screen, wibox_array_t *wiboxes, padding_t *padding)
258 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
259 area_t area = { .x = 0,
260 .y = 0,
261 .width = s->width_in_pixels,
262 .height = s->height_in_pixels };
264 if(wiboxes)
265 for(int i = 0; i < wiboxes->len; i++)
267 wibox_t *w = wiboxes->tab[i];
268 area.y += w->position == Top ? w->sw.geometry.height : 0;
269 area.height -= (w->position == Top || w->position == Bottom) ? w->sw.geometry.height : 0;
272 /* make padding corrections */
273 if(padding)
275 area.x += padding->left;
276 area.y += padding->top;
277 area.width -= padding->left + padding->right;
278 area.height -= padding->top + padding->bottom;
280 return area;
283 /** This returns the real X screen number for a logical
284 * screen if Xinerama is active.
285 * \param screen The logical screen.
286 * \return The X screen.
289 screen_virttophys(int screen)
291 if(globalconf.xinerama_is_active)
292 return globalconf.default_screen;
293 return screen;
296 /** Move a client to a virtual screen.
297 * \param c The client to move.
298 * \param new_screen The destinatiuon screen number.
299 * \param dotag Set to true if we also change tags.
300 * \param doresize Set to true if we also move the client to the new x and
301 * y of the new screen.
303 void
304 screen_client_moveto(client_t *c, int new_screen, bool dotag, bool doresize)
306 int i, old_screen = c->screen;
307 tag_array_t *old_tags = &globalconf.screens[old_screen].tags,
308 *new_tags = &globalconf.screens[new_screen].tags;
309 area_t from, to;
310 bool wasvisible = client_isvisible(c, c->screen);
312 if(new_screen == c->screen)
313 return;
315 c->screen = new_screen;
317 if(c->titlebar)
318 c->titlebar->screen = new_screen;
320 if(dotag && !c->issticky)
322 /* remove old tags */
323 for(i = 0; i < old_tags->len; i++)
324 untag_client(c, old_tags->tab[i]);
326 /* add new tags */
327 for(i = 0; i < new_tags->len; i++)
328 if(new_tags->tab[i]->selected)
329 tag_client(c, new_tags->tab[i]);
332 if (!doresize)
333 return;
335 from = screen_area_get(old_screen, NULL, NULL, false);
336 to = screen_area_get(c->screen, NULL, NULL, false);
338 area_t new_geometry = c->geometry;
340 if(c->isfullscreen)
342 new_geometry = to;
343 area_t new_f_geometry = c->geometries.fullscreen;
345 new_f_geometry.x = to.x + new_f_geometry.x - from.x;
346 new_f_geometry.y = to.y + new_f_geometry.y - from.x;
348 /* resize the client's original geometry if it doesn't fit the screen */
349 if (new_f_geometry.width > to.width)
350 new_f_geometry.width = to.width - 2 * c->border;
351 if (new_f_geometry.height > to.height)
352 new_f_geometry.height = to.height - 2 * c->border;
354 /* make sure the client is still on the screen */
355 if (new_f_geometry.x + new_f_geometry.width > to.x + to.width)
356 new_f_geometry.x = to.x + to.width - new_f_geometry.width;
357 if (new_f_geometry.y + new_f_geometry.height > to.y + to.height)
358 new_f_geometry.y = to.y + to.height - new_f_geometry.height;
360 c->geometries.fullscreen = new_f_geometry;
362 else
364 new_geometry.x = to.x + new_geometry.x - from.x;
365 new_geometry.y = to.y + new_geometry.y - from.y;
367 /* resize the client if it doesn't fit the new screen */
368 if(new_geometry.width > to.width)
369 new_geometry.width = to.width - 2 * c->border;
370 if(new_geometry.height > to.height)
371 new_geometry.height = to.height - 2 * c->border;
373 /* make sure the client is still on the screen */
374 if(new_geometry.x + new_geometry.width > to.x + to.width)
375 new_geometry.x = to.x + to.width - new_geometry.width;
376 if(new_geometry.y + new_geometry.height > to.y + to.height)
377 new_geometry.y = to.y + to.height - new_geometry.height;
379 /* move / resize the client */
380 client_resize(c, new_geometry, false);
381 if(wasvisible)
382 globalconf.screens[old_screen].need_arrange = true;
383 client_need_arrange(c);
386 /** Screen module.
387 * \param L The Lua VM state.
388 * \return The number of elements pushed on stack.
389 * \luastack
390 * \lfield number The screen number, to get a screen.
392 static int
393 luaA_screen_module_index(lua_State *L)
395 int screen = luaL_checknumber(L, 2) - 1;
397 luaA_checkscreen(screen);
398 lua_pushlightuserdata(L, &globalconf.screens[screen]);
399 return luaA_settype(L, "screen");
402 /** Get or set screen tags.
403 * \param L The Lua VM state.
404 * \return The number of elements pushed on stack.
405 * \luastack
406 * \lparam None or a table of tags to set to the screen.
407 * The table must contains at least one tag.
408 * \return A table with all screen tags.
410 static int
411 luaA_screen_tags(lua_State *L)
413 int i;
414 screen_t *s = lua_touserdata(L, 1);
416 if(!s)
417 luaL_typerror(L, 1, "screen");
419 if(lua_gettop(L) == 2)
421 tag_t **tag;
423 luaA_checktable(L, 2);
425 /* remove current tags */
426 for(i = 0; i < s->tags.len; i++)
427 s->tags.tab[i]->screen = SCREEN_UNDEF;
429 tag_array_wipe(&s->tags);
430 tag_array_init(&s->tags);
432 s->need_arrange = true;
434 /* push new tags */
435 lua_pushnil(L);
436 while(lua_next(L, 2))
438 tag = luaA_checkudata(L, -1, "tag");
439 tag_append_to_screen(*tag, s);
440 lua_pop(L, 1);
443 /* check there's at least one tag! */
444 if(!s->tags.len)
446 luaA_warn(L, "screen %d has no tag, taking last resort action and adding default tag\n",
447 s->index);
448 tag_append_to_screen(tag_new("default", sizeof("default") - 1), s);
449 return 1;
452 else
454 lua_newtable(L);
455 for(i = 0; i < s->tags.len; i++)
457 luaA_tag_userdata_new(L, s->tags.tab[i]);
458 lua_rawseti(L, -2, i + 1);
462 return 1;
465 /** A screen.
466 * \param L The Lua VM state.
467 * \return The number of elements pushed on stack.
468 * \luastack
469 * \lfield coords The screen coordinates. Immutable.
470 * \lfield workarea The screen workarea, i.e. without wiboxes.
472 static int
473 luaA_screen_index(lua_State *L)
475 size_t len;
476 const char *buf;
477 screen_t *s;
479 if(luaA_usemetatable(L, 1, 2))
480 return 1;
482 buf = luaL_checklstring(L, 2, &len);
483 s = lua_touserdata(L, 1);
485 switch(a_tokenize(buf, len))
487 case A_TK_GEOMETRY:
488 luaA_pusharea(L, s->geometry);
489 break;
490 case A_TK_WORKAREA:
491 luaA_pusharea(L, screen_area_get(s->index, &s->wiboxes, &s->padding, true));
492 break;
493 default:
494 return 0;
497 return 1;
500 /** Set or get the screen padding.
501 * \param L The Lua VM state.
502 * \return The number of elements pushed on stack.
503 * \luastack
504 * \lparam None or a table with new padding values.
505 * \lreturn The screen padding. A table with top, right, left and bottom
506 * keys and values in pixel.
508 static int
509 luaA_screen_padding(lua_State *L)
511 screen_t *s = lua_touserdata(L, 1);
513 if(!s)
514 luaL_typerror(L, 1, "screen");
516 if(lua_gettop(L) == 2)
518 s->padding = luaA_getopt_padding(L, 2, &s->padding);
520 s->need_arrange = true;
522 /* All the wiboxes repositioned */
523 for(int i = 0; i < s->wiboxes.len; i++)
524 wibox_position_update(s->wiboxes.tab[i]);
526 ewmh_update_workarea(screen_virttophys(s->index));
528 return luaA_pushpadding(L, &s->padding);
531 /** Get the screen count.
532 * \param L The Lua VM state.
533 * \return The number of elements pushed on stack.
535 * \luastack
536 * \lreturn The screen count, at least 1.
538 static int
539 luaA_screen_count(lua_State *L)
541 lua_pushnumber(L, globalconf.nscreen);
542 return 1;
545 const struct luaL_reg awesome_screen_methods[] =
547 { "count", luaA_screen_count },
548 { "__index", luaA_screen_module_index },
549 { NULL, NULL }
552 const struct luaL_reg awesome_screen_meta[] =
554 { "tags", luaA_screen_tags },
555 { "padding", luaA_screen_padding },
556 { "__index", luaA_screen_index },
557 { NULL, NULL }
560 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80