themes/sky: Titlebar icons: use current default theme paths
[awesome.git] / screen.c
blob434dd67509dd3005194feec308d87e5639e415a8
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 /* Struts are additive, to allow for multiple clients at the screen edge. */
167 /* Some clients request more space than their size, because another window of the same app already has some space. */
168 /* So we clamp the strut size. */
169 if(strut)
170 foreach(_c, globalconf.clients)
172 client_t *c = *_c;
173 if(client_isvisible(c, screen) && !c->ignore_strut)
175 if(c->strut.top_start_x || c->strut.top_end_x)
177 if(c->strut.top)
178 top += MIN(c->strut.top, c->geometry.height);
179 else
180 top += c->geometry.height;
182 if(c->strut.bottom_start_x || c->strut.bottom_end_x)
184 if(c->strut.bottom)
185 bottom += MIN(c->strut.bottom, c->geometry.height);
186 else
187 bottom += c->geometry.height;
189 if(c->strut.left_start_y || c->strut.left_end_y)
191 if(c->strut.left)
192 left += MIN(c->strut.left, c->geometry.width);
193 else
194 left += c->geometry.width;
196 if(c->strut.right_start_y || c->strut.right_end_y)
198 if(c->strut.right)
199 right += MIN(c->strut.right, c->geometry.width);
200 else
201 right += c->geometry.width;
206 /* swindow geometry includes borders. */
207 if(wiboxes)
208 foreach(_w, *wiboxes)
210 wibox_t *w = *_w;
211 if(w->isvisible)
212 switch(w->position)
214 case Top:
215 top += w->sw.geometry.height;
216 break;
217 case Bottom:
218 bottom += w->sw.geometry.height;
219 break;
220 case Left:
221 left += w->sw.geometry.width;
222 break;
223 case Right:
224 right += w->sw.geometry.width;
225 break;
226 default:
227 break;
231 area.x += left;
232 area.y += top;
233 area.width -= left + right;
234 area.height -= top + bottom;
236 return area;
239 /** Get display info.
240 * \param phys_screen Physical screen number.
241 * \param wiboxes The wiboxes.
242 * \param padding Padding.
243 * \return The display area.
245 area_t
246 display_area_get(int phys_screen, wibox_array_t *wiboxes, padding_t *padding)
248 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
249 area_t area = { .x = 0,
250 .y = 0,
251 .width = s->width_in_pixels,
252 .height = s->height_in_pixels };
254 if(wiboxes)
255 foreach(_w, *wiboxes)
257 wibox_t *w = *_w;
258 area.y += w->position == Top ? w->sw.geometry.height : 0;
259 area.height -= (w->position == Top || w->position == Bottom) ? w->sw.geometry.height : 0;
262 /* make padding corrections */
263 if(padding)
265 area.x += padding->left;
266 area.y += padding->top;
267 area.width -= padding->left + padding->right;
268 area.height -= padding->top + padding->bottom;
270 return area;
273 /** This returns the real X screen number for a logical
274 * screen if Xinerama is active.
275 * \param screen The logical screen.
276 * \return The X screen.
279 screen_virttophys(int screen)
281 if(globalconf.xinerama_is_active)
282 return globalconf.default_screen;
283 return screen;
286 /** Move a client to a virtual screen.
287 * \param c The client to move.
288 * \param new_screen The destinatiuon screen.
289 * \param dotag Set to true if we also change tags.
290 * \param doresize Set to true if we also move the client to the new x and
291 * y of the new screen.
293 void
294 screen_client_moveto(client_t *c, screen_t *new_screen, bool dotag, bool doresize)
296 int i;
297 screen_t *old_screen = c->screen;
298 tag_array_t *old_tags = &old_screen->tags,
299 *new_tags = &new_screen->tags;
300 area_t from, to;
301 bool wasvisible = client_isvisible(c, c->screen);
303 if(new_screen == c->screen)
304 return;
306 c->screen = new_screen;
308 if(c->titlebar)
309 c->titlebar->screen = new_screen;
311 if(dotag && !c->issticky)
313 /* remove old tags */
314 for(i = 0; i < old_tags->len; i++)
315 untag_client(c, old_tags->tab[i]);
317 /* add new tags */
318 foreach(new_tag, *new_tags)
319 if((*new_tag)->selected)
321 tag_push(globalconf.L, *new_tag);
322 tag_client(c);
326 if(wasvisible)
327 old_screen->need_arrange = true;
328 client_need_arrange(c);
330 if(!doresize)
331 return;
333 from = screen_area_get(old_screen, NULL, NULL, false);
334 to = screen_area_get(c->screen, NULL, NULL, false);
336 area_t new_geometry = c->geometry;
338 if(c->isfullscreen)
340 new_geometry = to;
341 area_t new_f_geometry = c->geometries.fullscreen;
343 new_f_geometry.x = to.x + new_f_geometry.x - from.x;
344 new_f_geometry.y = to.y + new_f_geometry.y - from.x;
346 /* resize the client's original geometry if it doesn't fit the screen */
347 if(new_f_geometry.width > to.width)
348 new_f_geometry.width = to.width;
349 if(new_f_geometry.height > to.height)
350 new_f_geometry.height = to.height;
352 /* make sure the client is still on the screen */
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;
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;
358 c->geometries.fullscreen = new_f_geometry;
360 else
362 new_geometry.x = to.x + new_geometry.x - from.x;
363 new_geometry.y = to.y + new_geometry.y - from.y;
365 /* resize the client if it doesn't fit the new screen */
366 if(new_geometry.width > to.width)
367 new_geometry.width = to.width;
368 if(new_geometry.height > to.height)
369 new_geometry.height = to.height;
371 /* make sure the client is still on the screen */
372 if(new_geometry.x + new_geometry.width > to.x + to.width)
373 new_geometry.x = to.x + to.width - new_geometry.width;
374 if(new_geometry.y + new_geometry.height > to.y + to.height)
375 new_geometry.y = to.y + to.height - new_geometry.height;
377 /* move / resize the client */
378 client_resize(c, new_geometry, false);
381 /** Screen module.
382 * \param L The Lua VM state.
383 * \return The number of elements pushed on stack.
384 * \luastack
385 * \lfield number The screen number, to get a screen.
387 static int
388 luaA_screen_module_index(lua_State *L)
390 int screen = luaL_checknumber(L, 2) - 1;
392 luaA_checkscreen(screen);
393 lua_pushlightuserdata(L, &globalconf.screens.tab[screen]);
394 return luaA_settype(L, "screen");
397 /** Get or set screen tags.
398 * \param L The Lua VM state.
399 * \return The number of elements pushed on stack.
400 * \luastack
401 * \lparam None or a table of tags to set to the screen.
402 * The table must contains at least one tag.
403 * \return A table with all screen tags.
405 static int
406 luaA_screen_tags(lua_State *L)
408 int i;
409 screen_t *s = lua_touserdata(L, 1);
411 if(!s)
412 luaL_typerror(L, 1, "screen");
414 if(lua_gettop(L) == 2)
416 luaA_checktable(L, 2);
418 /* remove current tags */
419 for(i = 0; i < s->tags.len; i++)
420 s->tags.tab[i]->screen = NULL;
422 tag_array_wipe(&s->tags);
423 tag_array_init(&s->tags);
425 s->need_arrange = true;
427 /* push new tags */
428 lua_pushnil(L);
429 while(lua_next(L, 2))
430 tag_append_to_screen(s);
432 else
434 lua_createtable(L, s->tags.len, 0);
435 for(i = 0; i < s->tags.len; i++)
437 tag_push(L, s->tags.tab[i]);
438 lua_rawseti(L, -2, i + 1);
442 return 1;
445 /** A screen.
446 * \param L The Lua VM state.
447 * \return The number of elements pushed on stack.
448 * \luastack
449 * \lfield coords The screen coordinates. Immutable.
450 * \lfield workarea The screen workarea, i.e. without wiboxes.
452 static int
453 luaA_screen_index(lua_State *L)
455 size_t len;
456 const char *buf;
457 screen_t *s;
459 if(luaA_usemetatable(L, 1, 2))
460 return 1;
462 buf = luaL_checklstring(L, 2, &len);
463 s = lua_touserdata(L, 1);
465 switch(a_tokenize(buf, len))
467 case A_TK_GEOMETRY:
468 luaA_pusharea(L, s->geometry);
469 break;
470 case A_TK_WORKAREA:
471 luaA_pusharea(L, screen_area_get(s, &s->wiboxes, &s->padding, true));
472 break;
473 default:
474 return 0;
477 return 1;
480 /** Set or get the screen padding.
481 * \param L The Lua VM state.
482 * \return The number of elements pushed on stack.
483 * \luastack
484 * \lparam None or a table with new padding values.
485 * \lreturn The screen padding. A table with top, right, left and bottom
486 * keys and values in pixel.
488 static int
489 luaA_screen_padding(lua_State *L)
491 screen_t *s = lua_touserdata(L, 1);
493 if(!s)
494 luaL_typerror(L, 1, "screen");
496 if(lua_gettop(L) == 2)
498 s->padding = luaA_getopt_padding(L, 2, &s->padding);
500 s->need_arrange = true;
502 /* All the wiboxes repositioned */
503 foreach(w, s->wiboxes)
504 wibox_position_update(*w);
506 ewmh_update_workarea(screen_virttophys(screen_array_indexof(&globalconf.screens, s)));
508 return luaA_pushpadding(L, &s->padding);
511 /** Get the screen count.
512 * \param L The Lua VM state.
513 * \return The number of elements pushed on stack.
515 * \luastack
516 * \lreturn The screen count, at least 1.
518 static int
519 luaA_screen_count(lua_State *L)
521 lua_pushnumber(L, globalconf.screens.len);
522 return 1;
525 const struct luaL_reg awesome_screen_methods[] =
527 { "count", luaA_screen_count },
528 { "__index", luaA_screen_module_index },
529 { NULL, NULL }
532 const struct luaL_reg awesome_screen_meta[] =
534 { "tags", luaA_screen_tags },
535 { "padding", luaA_screen_padding },
536 { "__index", luaA_screen_index },
537 { NULL, NULL }
540 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80