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.
25 #include <xcb/xinerama.h>
34 extern awesome_t globalconf
;
37 screen_xsitoarea(xcb_xinerama_screen_info_t si
)
49 /** Get screens informations and fill global configuration.
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
;
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
),
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
++)
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 */
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
);
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;
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
)
141 /* don't waste our time */
142 if(!globalconf
.xinerama_is_active
)
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
)))
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.
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 */
173 area
.x
+= padding
->left
;
174 area
.y
+= padding
->top
;
175 area
.width
-= padding
->left
+ padding
->right
;
176 area
.height
-= padding
->top
+ padding
->bottom
;
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
)
188 top
= MAX(top
, c
->strut
.top
);
190 top
= MAX(top
, (c
->geometry
.y
- area
.y
) + c
->geometry
.height
);
192 if(c
->strut
.bottom_start_x
|| c
->strut
.bottom_end_x
)
195 bottom
= MAX(bottom
, c
->strut
.bottom
);
197 bottom
= MAX(bottom
, (area
.y
+ area
.height
) - c
->geometry
.y
);
199 if(c
->strut
.left_start_y
|| c
->strut
.left_end_y
)
202 left
= MAX(left
, c
->strut
.left
);
204 left
= MAX(left
, (c
->geometry
.x
- area
.x
) + c
->geometry
.width
);
206 if(c
->strut
.right_start_y
|| c
->strut
.right_end_y
)
209 right
= MAX(right
, c
->strut
.right
);
211 right
= MAX(right
, (area
.x
+ area
.width
) - c
->geometry
.x
);
218 for(int i
= 0; i
< wiboxes
->len
; i
++)
220 wibox_t
*w
= wiboxes
->tab
[i
];
225 top
= MAX(top
, (uint16_t) (w
->sw
.geometry
.y
- area
.y
) + w
->sw
.geometry
.height
+ 2 * w
->sw
.border
.width
);
228 bottom
= MAX(bottom
, (uint16_t) (area
.y
+ area
.height
) - w
->sw
.geometry
.y
);
231 left
= MAX(left
, (uint16_t) (w
->sw
.geometry
.x
- area
.x
) + w
->sw
.geometry
.width
+ 2 * w
->sw
.border
.width
);
234 right
= MAX(right
, (uint16_t) (area
.x
+ area
.width
) - w
->sw
.geometry
.x
);
243 area
.width
-= left
+ right
;
244 area
.height
-= top
+ bottom
;
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.
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,
261 .width
= s
->width_in_pixels
,
262 .height
= s
->height_in_pixels
};
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 */
275 area
.x
+= padding
->left
;
276 area
.y
+= padding
->top
;
277 area
.width
-= padding
->left
+ padding
->right
;
278 area
.height
-= padding
->top
+ padding
->bottom
;
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
;
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.
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
;
310 bool wasvisible
= client_isvisible(c
, c
->screen
);
312 c
->screen
= new_screen
;
315 c
->titlebar
->screen
= new_screen
;
317 if(dotag
&& !c
->issticky
)
319 /* remove old tags */
320 for(i
= 0; i
< old_tags
->len
; i
++)
321 untag_client(c
, old_tags
->tab
[i
]);
324 for(i
= 0; i
< new_tags
->len
; i
++)
325 if(new_tags
->tab
[i
]->selected
)
326 tag_client(c
, new_tags
->tab
[i
]);
329 /* move and resize the windows */
330 if(doresize
&& old_screen
!= c
->screen
)
332 area_t new_geometry
, new_f_geometry
;
333 new_f_geometry
= c
->geometry
;
335 to
= screen_area_get(c
->screen
,
337 from
= screen_area_get(old_screen
,
340 /* compute new coords in new screen */
341 new_f_geometry
.x
= (c
->geometry
.x
- from
.x
) + to
.x
;
342 new_f_geometry
.y
= (c
->geometry
.y
- from
.y
) + to
.y
;
344 /* check that new coords are still in the screen */
345 if(new_f_geometry
.width
> to
.width
)
346 new_f_geometry
.width
= to
.width
;
347 if(new_f_geometry
.height
> to
.height
)
348 new_f_geometry
.height
= to
.height
;
349 if(new_f_geometry
.x
+ new_f_geometry
.width
>= to
.x
+ to
.width
)
350 new_f_geometry
.x
= to
.x
+ to
.width
- new_f_geometry
.width
- 2 * c
->border
;
351 if(new_f_geometry
.y
+ new_f_geometry
.height
>= to
.y
+ to
.height
)
352 new_f_geometry
.y
= to
.y
+ to
.height
- new_f_geometry
.height
- 2 * c
->border
;
356 new_geometry
= c
->geometry
;
358 /* compute new coords in new screen */
359 new_geometry
.x
= (c
->geometry
.x
- from
.x
) + to
.x
;
360 new_geometry
.y
= (c
->geometry
.y
- from
.y
) + to
.y
;
362 /* check that new coords are still in the 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
;
367 if(new_geometry
.x
+ new_geometry
.width
>= to
.x
+ to
.width
)
368 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
- 2 * c
->border
;
369 if(new_geometry
.y
+ new_geometry
.height
>= to
.y
+ to
.height
)
370 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
- 2 * c
->border
;
372 /* compute new coords for max in new screen */
373 c
->geometries
.fullscreen
.x
= (c
->geometries
.fullscreen
.x
- from
.x
) + to
.x
;
374 c
->geometries
.fullscreen
.y
= (c
->geometries
.fullscreen
.y
- from
.y
) + to
.y
;
376 /* check that new coords are still in the screen */
377 if(c
->geometries
.fullscreen
.width
> to
.width
)
378 c
->geometries
.fullscreen
.width
= to
.width
;
379 if(c
->geometries
.fullscreen
.height
> to
.height
)
380 c
->geometries
.fullscreen
.height
= to
.height
;
381 if(c
->geometries
.fullscreen
.x
+ c
->geometries
.fullscreen
.width
>= to
.x
+ to
.width
)
382 c
->geometries
.fullscreen
.x
= to
.x
+ to
.width
- c
->geometries
.fullscreen
.width
- 2 * c
->border
;
383 if(c
->geometries
.fullscreen
.y
+ c
->geometries
.fullscreen
.height
>= to
.y
+ to
.height
)
384 c
->geometries
.fullscreen
.y
= to
.y
+ to
.height
- c
->geometries
.fullscreen
.height
- 2 * c
->border
;
386 client_resize(c
, new_geometry
, false);
388 /* move to this new coords */
391 client_resize(c
, new_f_geometry
, false);
393 globalconf
.screens
[old_screen
].need_arrange
= true;
394 client_need_arrange(c
);
400 * \param L The Lua VM state.
401 * \return The number of elements pushed on stack.
403 * \lfield number The screen number, to get a screen.
406 luaA_screen_module_index(lua_State
*L
)
408 int screen
= luaL_checknumber(L
, 2) - 1;
410 luaA_checkscreen(screen
);
411 lua_pushlightuserdata(L
, &globalconf
.screens
[screen
]);
412 return luaA_settype(L
, "screen");
415 /** Get or set screen tags.
416 * \param L The Lua VM state.
417 * \return The number of elements pushed on stack.
419 * \lparam None or a table of tags to set to the screen.
420 * The table must contains at least one tag.
421 * \return A table with all screen tags.
424 luaA_screen_tags(lua_State
*L
)
427 screen_t
*s
= lua_touserdata(L
, 1);
430 luaL_typerror(L
, 1, "screen");
432 if(lua_gettop(L
) == 2)
436 luaA_checktable(L
, 2);
438 /* remove current tags */
439 for(i
= 0; i
< s
->tags
.len
; i
++)
440 s
->tags
.tab
[i
]->screen
= SCREEN_UNDEF
;
442 tag_array_wipe(&s
->tags
);
443 tag_array_init(&s
->tags
);
445 s
->need_arrange
= true;
449 while(lua_next(L
, 2))
451 tag
= luaA_checkudata(L
, -1, "tag");
452 tag_append_to_screen(*tag
, s
);
456 /* check there's at least one tag! */
459 luaA_warn(L
, "screen %d has no tag, taking last resort action and adding default tag\n",
461 tag_append_to_screen(tag_new("default", sizeof("default") - 1), s
);
468 for(i
= 0; i
< s
->tags
.len
; i
++)
470 luaA_tag_userdata_new(L
, s
->tags
.tab
[i
]);
471 lua_rawseti(L
, -2, i
+ 1);
479 * \param L The Lua VM state.
480 * \return The number of elements pushed on stack.
482 * \lfield coords The screen coordinates. Immutable.
483 * \lfield workarea The screen workarea, i.e. without wiboxes.
486 luaA_screen_index(lua_State
*L
)
492 if(luaA_usemetatable(L
, 1, 2))
495 buf
= luaL_checklstring(L
, 2, &len
);
496 s
= lua_touserdata(L
, 1);
498 switch(a_tokenize(buf
, len
))
501 luaA_pusharea(L
, s
->geometry
);
504 luaA_pusharea(L
, screen_area_get(s
->index
, &s
->wiboxes
, &s
->padding
, true));
513 /** Set or get the screen padding.
514 * \param L The Lua VM state.
515 * \return The number of elements pushed on stack.
517 * \lparam None or a table with new padding values.
518 * \lreturn The screen padding. A table with top, right, left and bottom
519 * keys and values in pixel.
522 luaA_screen_padding(lua_State
*L
)
524 screen_t
*s
= lua_touserdata(L
, 1);
527 luaL_typerror(L
, 1, "screen");
529 if(lua_gettop(L
) == 2)
531 s
->padding
= luaA_getopt_padding(L
, 2, &s
->padding
);
533 s
->need_arrange
= true;
535 /* All the wiboxes repositioned */
536 for(int i
= 0; i
< s
->wiboxes
.len
; i
++)
537 wibox_position_update(s
->wiboxes
.tab
[i
]);
539 ewmh_update_workarea(screen_virttophys(s
->index
));
541 return luaA_pushpadding(L
, &s
->padding
);
544 /** Get the screen count.
545 * \param L The Lua VM state.
546 * \return The number of elements pushed on stack.
549 * \lreturn The screen count, at least 1.
552 luaA_screen_count(lua_State
*L
)
554 lua_pushnumber(L
, globalconf
.nscreen
);
558 const struct luaL_reg awesome_screen_methods
[] =
560 { "count", luaA_screen_count
},
561 { "__index", luaA_screen_module_index
},
565 const struct luaL_reg awesome_screen_meta
[] =
567 { "tags", luaA_screen_tags
},
568 { "padding", luaA_screen_padding
},
569 { "__index", luaA_screen_index
},
573 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80