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 if(new_screen
== c
->screen
)
315 c
->screen
= new_screen
;
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
]);
327 for(i
= 0; i
< new_tags
->len
; i
++)
328 if(new_tags
->tab
[i
]->selected
)
329 tag_client(c
, new_tags
->tab
[i
]);
333 globalconf
.screens
[old_screen
].need_arrange
= true;
334 client_need_arrange(c
);
339 from
= screen_area_get(old_screen
, NULL
, NULL
, false);
340 to
= screen_area_get(c
->screen
, NULL
, NULL
, false);
342 area_t new_geometry
= c
->geometry
;
347 area_t new_f_geometry
= c
->geometries
.fullscreen
;
349 new_f_geometry
.x
= to
.x
+ new_f_geometry
.x
- from
.x
;
350 new_f_geometry
.y
= to
.y
+ new_f_geometry
.y
- from
.x
;
352 /* resize the client's original geometry if it doesn't fit the screen */
353 if (new_f_geometry
.width
> to
.width
)
354 new_f_geometry
.width
= to
.width
;
355 if (new_f_geometry
.height
> to
.height
)
356 new_f_geometry
.height
= to
.height
;
358 /* make sure the client is still on the screen */
359 if (new_f_geometry
.x
+ new_f_geometry
.width
> to
.x
+ to
.width
)
360 new_f_geometry
.x
= to
.x
+ to
.width
- new_f_geometry
.width
;
361 if (new_f_geometry
.y
+ new_f_geometry
.height
> to
.y
+ to
.height
)
362 new_f_geometry
.y
= to
.y
+ to
.height
- new_f_geometry
.height
;
364 c
->geometries
.fullscreen
= new_f_geometry
;
368 new_geometry
.x
= to
.x
+ new_geometry
.x
- from
.x
;
369 new_geometry
.y
= to
.y
+ new_geometry
.y
- from
.y
;
371 /* resize the client if it doesn't fit the new screen */
372 if(new_geometry
.width
> to
.width
)
373 new_geometry
.width
= to
.width
;
374 if(new_geometry
.height
> to
.height
)
375 new_geometry
.height
= to
.height
;
377 /* make sure the client is still on the screen */
378 if(new_geometry
.x
+ new_geometry
.width
> to
.x
+ to
.width
)
379 new_geometry
.x
= to
.x
+ to
.width
- new_geometry
.width
;
380 if(new_geometry
.y
+ new_geometry
.height
> to
.y
+ to
.height
)
381 new_geometry
.y
= to
.y
+ to
.height
- new_geometry
.height
;
383 /* move / resize the client */
384 client_resize(c
, new_geometry
, false);
388 * \param L The Lua VM state.
389 * \return The number of elements pushed on stack.
391 * \lfield number The screen number, to get a screen.
394 luaA_screen_module_index(lua_State
*L
)
396 int screen
= luaL_checknumber(L
, 2) - 1;
398 luaA_checkscreen(screen
);
399 lua_pushlightuserdata(L
, &globalconf
.screens
[screen
]);
400 return luaA_settype(L
, "screen");
403 /** Get or set screen tags.
404 * \param L The Lua VM state.
405 * \return The number of elements pushed on stack.
407 * \lparam None or a table of tags to set to the screen.
408 * The table must contains at least one tag.
409 * \return A table with all screen tags.
412 luaA_screen_tags(lua_State
*L
)
415 screen_t
*s
= lua_touserdata(L
, 1);
418 luaL_typerror(L
, 1, "screen");
420 if(lua_gettop(L
) == 2)
424 luaA_checktable(L
, 2);
426 /* remove current tags */
427 for(i
= 0; i
< s
->tags
.len
; i
++)
428 s
->tags
.tab
[i
]->screen
= SCREEN_UNDEF
;
430 tag_array_wipe(&s
->tags
);
431 tag_array_init(&s
->tags
);
433 s
->need_arrange
= true;
437 while(lua_next(L
, 2))
439 tag
= luaA_checkudata(L
, -1, "tag");
440 tag_append_to_screen(*tag
, s
);
444 /* check there's at least one tag! */
447 luaA_warn(L
, "screen %d has no tag, taking last resort action and adding default tag\n",
449 tag_append_to_screen(tag_new("default", sizeof("default") - 1), s
);
456 for(i
= 0; i
< s
->tags
.len
; i
++)
458 luaA_tag_userdata_new(L
, s
->tags
.tab
[i
]);
459 lua_rawseti(L
, -2, i
+ 1);
467 * \param L The Lua VM state.
468 * \return The number of elements pushed on stack.
470 * \lfield coords The screen coordinates. Immutable.
471 * \lfield workarea The screen workarea, i.e. without wiboxes.
474 luaA_screen_index(lua_State
*L
)
480 if(luaA_usemetatable(L
, 1, 2))
483 buf
= luaL_checklstring(L
, 2, &len
);
484 s
= lua_touserdata(L
, 1);
486 switch(a_tokenize(buf
, len
))
489 luaA_pusharea(L
, s
->geometry
);
492 luaA_pusharea(L
, screen_area_get(s
->index
, &s
->wiboxes
, &s
->padding
, true));
501 /** Set or get the screen padding.
502 * \param L The Lua VM state.
503 * \return The number of elements pushed on stack.
505 * \lparam None or a table with new padding values.
506 * \lreturn The screen padding. A table with top, right, left and bottom
507 * keys and values in pixel.
510 luaA_screen_padding(lua_State
*L
)
512 screen_t
*s
= lua_touserdata(L
, 1);
515 luaL_typerror(L
, 1, "screen");
517 if(lua_gettop(L
) == 2)
519 s
->padding
= luaA_getopt_padding(L
, 2, &s
->padding
);
521 s
->need_arrange
= true;
523 /* All the wiboxes repositioned */
524 for(int i
= 0; i
< s
->wiboxes
.len
; i
++)
525 wibox_position_update(s
->wiboxes
.tab
[i
]);
527 ewmh_update_workarea(screen_virttophys(s
->index
));
529 return luaA_pushpadding(L
, &s
->padding
);
532 /** Get the screen count.
533 * \param L The Lua VM state.
534 * \return The number of elements pushed on stack.
537 * \lreturn The screen count, at least 1.
540 luaA_screen_count(lua_State
*L
)
542 lua_pushnumber(L
, globalconf
.nscreen
);
546 const struct luaL_reg awesome_screen_methods
[] =
548 { "count", luaA_screen_count
},
549 { "__index", luaA_screen_module_index
},
553 const struct luaL_reg awesome_screen_meta
[] =
555 { "tags", luaA_screen_tags
},
556 { "padding", luaA_screen_padding
},
557 { "__index", luaA_screen_index
},
561 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80