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
]);
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
;
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
;
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);
382 globalconf
.screens
[old_screen
].need_arrange
= true;
383 client_need_arrange(c
);
387 * \param L The Lua VM state.
388 * \return The number of elements pushed on stack.
390 * \lfield number The screen number, to get a screen.
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.
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.
411 luaA_screen_tags(lua_State
*L
)
414 screen_t
*s
= lua_touserdata(L
, 1);
417 luaL_typerror(L
, 1, "screen");
419 if(lua_gettop(L
) == 2)
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;
436 while(lua_next(L
, 2))
438 tag
= luaA_checkudata(L
, -1, "tag");
439 tag_append_to_screen(*tag
, s
);
443 /* check there's at least one tag! */
446 luaA_warn(L
, "screen %d has no tag, taking last resort action and adding default tag\n",
448 tag_append_to_screen(tag_new("default", sizeof("default") - 1), s
);
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);
466 * \param L The Lua VM state.
467 * \return The number of elements pushed on stack.
469 * \lfield coords The screen coordinates. Immutable.
470 * \lfield workarea The screen workarea, i.e. without wiboxes.
473 luaA_screen_index(lua_State
*L
)
479 if(luaA_usemetatable(L
, 1, 2))
482 buf
= luaL_checklstring(L
, 2, &len
);
483 s
= lua_touserdata(L
, 1);
485 switch(a_tokenize(buf
, len
))
488 luaA_pusharea(L
, s
->geometry
);
491 luaA_pusharea(L
, screen_area_get(s
->index
, &s
->wiboxes
, &s
->padding
, true));
500 /** Set or get the screen padding.
501 * \param L The Lua VM state.
502 * \return The number of elements pushed on stack.
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.
509 luaA_screen_padding(lua_State
*L
)
511 screen_t
*s
= lua_touserdata(L
, 1);
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.
536 * \lreturn The screen count, at least 1.
539 luaA_screen_count(lua_State
*L
)
541 lua_pushnumber(L
, globalconf
.nscreen
);
545 const struct luaL_reg awesome_screen_methods
[] =
547 { "count", luaA_screen_count
},
548 { "__index", luaA_screen_module_index
},
552 const struct luaL_reg awesome_screen_meta
[] =
554 { "tags", luaA_screen_tags
},
555 { "padding", luaA_screen_padding
},
556 { "__index", luaA_screen_index
},
560 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80