awesomerc: remove marking
[awesome.git] / screen.c
blob76512e523c9344058d60b0815ffa6323b0ed48e2
1 /*
2 * screen.c - screen management
4 * Copyright © 2007-2009 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 static xcb_visualtype_t *
49 screen_default_visual(xcb_screen_t *s)
51 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
53 if(depth_iter.data)
54 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
55 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
56 visual_iter.rem; xcb_visualtype_next (&visual_iter))
57 if(s->root_visual == visual_iter.data->visual_id)
58 return visual_iter.data;
60 return NULL;
63 /** Get screens informations and fill global configuration.
65 void
66 screen_scan(void)
68 /* Check for extension before checking for Xinerama */
69 if(xcb_get_extension_data(globalconf.connection, &xcb_xinerama_id)->present)
71 xcb_xinerama_is_active_reply_t *xia;
72 xia = xcb_xinerama_is_active_reply(globalconf.connection, xcb_xinerama_is_active(globalconf.connection), NULL);
73 globalconf.xinerama_is_active = xia->state;
74 p_delete(&xia);
77 if(globalconf.xinerama_is_active)
79 xcb_xinerama_query_screens_reply_t *xsq;
80 xcb_xinerama_screen_info_t *xsi;
81 int xinerama_screen_number;
83 xsq = xcb_xinerama_query_screens_reply(globalconf.connection,
84 xcb_xinerama_query_screens_unchecked(globalconf.connection),
85 NULL);
87 xsi = xcb_xinerama_query_screens_screen_info(xsq);
88 xinerama_screen_number = xcb_xinerama_query_screens_screen_info_length(xsq);
90 /* now check if screens overlaps (same x,y): if so, we take only the biggest one */
91 for(int screen = 0; screen < xinerama_screen_number; screen++)
93 bool drop = false;
94 foreach(screen_to_test, globalconf.screens)
95 if(xsi[screen].x_org == screen_to_test->geometry.x
96 && xsi[screen].y_org == screen_to_test->geometry.y)
98 /* we already have a screen for this area, just check if
99 * it's not bigger and drop it */
100 drop = true;
101 int i = screen_array_indexof(&globalconf.screens, screen_to_test);
102 screen_to_test->geometry.width =
103 MAX(xsi[screen].width, xsi[i].width);
104 screen_to_test->geometry.height =
105 MAX(xsi[screen].height, xsi[i].height);
107 if(!drop)
109 screen_t s;
110 p_clear(&s, 1);
111 s.geometry = screen_xsitoarea(xsi[screen]);
112 screen_array_append(&globalconf.screens, s);
116 p_delete(&xsq);
118 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
119 globalconf.screens.tab[0].visual = screen_default_visual(s);
121 else
122 /* One screen only / Zaphod mode */
123 for(int screen = 0;
124 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
125 screen++)
127 xcb_screen_t *xcb_screen = xutil_screen_get(globalconf.connection, screen);
128 screen_t s;
129 p_clear(&s, 1);
130 s.geometry.x = 0;
131 s.geometry.y = 0;
132 s.geometry.width = xcb_screen->width_in_pixels;
133 s.geometry.height = xcb_screen->height_in_pixels;
134 s.visual = screen_default_visual(xcb_screen);
135 screen_array_append(&globalconf.screens, s);
138 globalconf.screen_focus = globalconf.screens.tab;
141 /** Return the Xinerama screen number where the coordinates belongs to.
142 * \param screen The logical screen number.
143 * \param x X coordinate
144 * \param y Y coordinate
145 * \return Screen pointer or screen param if no match or no multi-head.
147 screen_t *
148 screen_getbycoord(screen_t *screen, int x, int y)
150 /* don't waste our time */
151 if(!globalconf.xinerama_is_active)
152 return screen;
154 foreach(s, globalconf.screens)
155 if((x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width))
156 && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)))
157 return s;
159 return screen;
162 /** Get screens info.
163 * \param screen Screen.
164 * \param strut Honor windows strut.
165 * \return The screen area.
167 area_t
168 screen_area_get(screen_t *screen, bool strut)
170 area_t area = screen->geometry;
171 uint16_t top = 0, bottom = 0, left = 0, right = 0;
173 if(strut)
174 foreach(_c, globalconf.clients)
176 client_t *c = *_c;
177 if(client_isvisible(c, screen))
179 if(c->strut.top_start_x || c->strut.top_end_x)
181 if(c->strut.top)
182 top = MAX(top, c->strut.top);
183 else
184 top = MAX(top, (c->geometry.y - area.y) + c->geometry.height);
186 if(c->strut.bottom_start_x || c->strut.bottom_end_x)
188 if(c->strut.bottom)
189 bottom = MAX(bottom, c->strut.bottom);
190 else
191 bottom = MAX(bottom, (area.y + area.height) - c->geometry.y);
193 if(c->strut.left_start_y || c->strut.left_end_y)
195 if(c->strut.left)
196 left = MAX(left, c->strut.left);
197 else
198 left = MAX(left, (c->geometry.x - area.x) + c->geometry.width);
200 if(c->strut.right_start_y || c->strut.right_end_y)
202 if(c->strut.right)
203 right = MAX(right, c->strut.right);
204 else
205 right = MAX(right, (area.x + area.width) - c->geometry.x);
210 area.x += left;
211 area.y += top;
212 area.width -= left + right;
213 area.height -= top + bottom;
215 return area;
218 /** Get display info.
219 * \param phys_screen Physical screen number.
220 * \return The display area.
222 area_t
223 display_area_get(int phys_screen)
225 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
226 area_t area = { .x = 0,
227 .y = 0,
228 .width = s->width_in_pixels,
229 .height = s->height_in_pixels };
230 return area;
233 /** This returns the real X screen number for a logical
234 * screen if Xinerama is active.
235 * \param screen The logical screen.
236 * \return The X screen.
239 screen_virttophys(int screen)
241 if(globalconf.xinerama_is_active)
242 return globalconf.default_screen;
243 return screen;
246 /** Move a client to a virtual screen.
247 * \param c The client to move.
248 * \param new_screen The destinatiuon screen.
249 * \param dotag Set to true if we also change tags.
250 * \param doresize Set to true if we also move the client to the new x and
251 * y of the new screen.
253 void
254 screen_client_moveto(client_t *c, screen_t *new_screen, bool dotag, bool doresize)
256 screen_t *old_screen = c->screen;
257 area_t from, to;
259 if(new_screen == c->screen)
260 return;
262 c->screen = new_screen;
264 if(c->titlebar)
265 c->titlebar->screen = new_screen;
267 if(dotag)
269 /* remove old tags */
270 foreach(old_tag, old_screen->tags)
271 untag_client(c, *old_tag);
273 /* \todo move this to Lua */
274 if(!c->issticky)
275 /* add new tags */
276 foreach(new_tag, new_screen->tags)
277 if((*new_tag)->selected)
279 tag_push(globalconf.L, *new_tag);
280 tag_client(c);
284 if(!doresize)
286 hook_property(client, c, "screen");
287 return;
290 from = screen_area_get(old_screen, false);
291 to = screen_area_get(c->screen, false);
293 area_t new_geometry = c->geometry;
295 if(c->isfullscreen)
297 new_geometry = to;
298 area_t new_f_geometry = c->geometries.fullscreen;
300 new_f_geometry.x = to.x + new_f_geometry.x - from.x;
301 new_f_geometry.y = to.y + new_f_geometry.y - from.x;
303 /* resize the client's original geometry if it doesn't fit the screen */
304 if(new_f_geometry.width > to.width)
305 new_f_geometry.width = to.width;
306 if(new_f_geometry.height > to.height)
307 new_f_geometry.height = to.height;
309 /* make sure the client is still on the screen */
310 if(new_f_geometry.x + new_f_geometry.width > to.x + to.width)
311 new_f_geometry.x = to.x + to.width - new_f_geometry.width;
312 if(new_f_geometry.y + new_f_geometry.height > to.y + to.height)
313 new_f_geometry.y = to.y + to.height - new_f_geometry.height;
315 c->geometries.fullscreen = new_f_geometry;
317 else
319 new_geometry.x = to.x + new_geometry.x - from.x;
320 new_geometry.y = to.y + new_geometry.y - from.y;
322 /* resize the client if it doesn't fit the new screen */
323 if(new_geometry.width > to.width)
324 new_geometry.width = to.width;
325 if(new_geometry.height > to.height)
326 new_geometry.height = to.height;
328 /* make sure the client is still on the screen */
329 if(new_geometry.x + new_geometry.width > to.x + to.width)
330 new_geometry.x = to.x + to.width - new_geometry.width;
331 if(new_geometry.y + new_geometry.height > to.y + to.height)
332 new_geometry.y = to.y + to.height - new_geometry.height;
334 /* move / resize the client */
335 client_resize(c, new_geometry, false);
336 hook_property(client, c, "screen");
339 /** Push a screen onto the stack.
340 * \param L The Lua VM state.
341 * \param s The scren to push.
342 * \return The number of elements pushed on stack.
345 luaA_pushscreen(lua_State *L, screen_t *s)
347 lua_pushlightuserdata(L, s);
348 luaL_getmetatable(L, "screen");
349 lua_setmetatable(L, -2);
350 return 1;
353 /** Screen module.
354 * \param L The Lua VM state.
355 * \return The number of elements pushed on stack.
356 * \luastack
357 * \lfield number The screen number, to get a screen.
359 static int
360 luaA_screen_module_index(lua_State *L)
362 int screen = luaL_checknumber(L, 2) - 1;
363 luaA_checkscreen(screen);
364 return luaA_pushscreen(L, &globalconf.screens.tab[screen]);
367 /** Get or set screen tags.
368 * \param L The Lua VM state.
369 * \return The number of elements pushed on stack.
370 * \luastack
371 * \lparam None or a table of tags to set to the screen.
372 * The table must contains at least one tag.
373 * \return A table with all screen tags.
375 static int
376 luaA_screen_tags(lua_State *L)
378 int i;
379 screen_t *s = luaL_checkudata(L, 1, "screen");
381 if(lua_gettop(L) == 2)
383 luaA_checktable(L, 2);
385 /* remove current tags */
386 for(i = 0; i < s->tags.len; i++)
387 s->tags.tab[i]->screen = NULL;
389 tag_array_wipe(&s->tags);
390 tag_array_init(&s->tags);
392 s->need_reban = true;
394 /* push new tags */
395 lua_pushnil(L);
396 while(lua_next(L, 2))
397 tag_append_to_screen(s);
399 else
401 lua_createtable(L, s->tags.len, 0);
402 for(i = 0; i < s->tags.len; i++)
404 tag_push(L, s->tags.tab[i]);
405 lua_rawseti(L, -2, i + 1);
409 return 1;
412 /** A screen.
413 * \param L The Lua VM state.
414 * \return The number of elements pushed on stack.
415 * \luastack
416 * \lfield coords The screen coordinates. Immutable.
417 * \lfield workarea The screen workarea.
419 static int
420 luaA_screen_index(lua_State *L)
422 size_t len;
423 const char *buf;
424 screen_t *s;
426 if(luaA_usemetatable(L, 1, 2))
427 return 1;
429 buf = luaL_checklstring(L, 2, &len);
430 s = lua_touserdata(L, 1);
432 switch(a_tokenize(buf, len))
434 case A_TK_GEOMETRY:
435 luaA_pusharea(L, s->geometry);
436 break;
437 case A_TK_WORKAREA:
438 luaA_pusharea(L, screen_area_get(s, true));
439 break;
440 default:
441 return 0;
444 return 1;
447 /** Get the screen count.
448 * \param L The Lua VM state.
449 * \return The number of elements pushed on stack.
451 * \luastack
452 * \lreturn The screen count, at least 1.
454 static int
455 luaA_screen_count(lua_State *L)
457 lua_pushnumber(L, globalconf.screens.len);
458 return 1;
461 const struct luaL_reg awesome_screen_methods[] =
463 { "count", luaA_screen_count },
464 { "__index", luaA_screen_module_index },
465 { NULL, NULL }
468 const struct luaL_reg awesome_screen_meta[] =
470 { "tags", luaA_screen_tags },
471 { "__index", luaA_screen_index },
472 { NULL, NULL }
475 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80