luaa: close accepted socket on exec
[awesome.git] / titlebar.c
blobf75a4ddb10a711ad8b555ae70cfd97479e914273
1 /*
2 * titlebar.c - titlebar management
4 * Copyright © 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 <xcb/xcb.h>
24 #include "titlebar.h"
25 #include "client.h"
26 #include "widget.h"
27 #include "wibox.h"
28 #include "screen.h"
30 extern awesome_t globalconf;
32 /** Get a client by its titlebar.
33 * \param titlebar The titlebar.
34 * \return A client.
36 client_t *
37 client_getbytitlebar(wibox_t *titlebar)
39 client_t *c;
41 for(c = globalconf.clients; c; c = c->next)
42 if(c->titlebar == titlebar)
43 return c;
45 return NULL;
48 /** Get a client by its titlebar window.
49 * \param win The window.
50 * \return A client.
52 client_t *
53 client_getbytitlebarwin(xcb_window_t win)
55 client_t *c;
57 for(c = globalconf.clients; c; c = c->next)
58 if(c->titlebar && c->titlebar->sw.window == win)
59 return c;
61 return NULL;
64 /** Move a titlebar out of the viewport.
65 * \param titlebar The titlebar.
67 void
68 titlebar_ban(wibox_t *titlebar)
70 /* Do it manually because client geometry remains unchanged. */
71 if(titlebar && !titlebar->isbanned)
73 client_t *c;
74 simple_window_t *sw = &titlebar->sw;
76 if(sw->window)
78 uint32_t request[] = { - sw->geometry.width, - sw->geometry.height };
79 /* Move the titlebar to the same place as the window. */
80 xcb_configure_window(globalconf.connection, sw->window,
81 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
82 request);
85 /* Remove titlebar geometry from client. */
86 if((c = client_getbytitlebar(titlebar)))
87 c->geometry = titlebar_geometry_remove(titlebar, 0, c->geometry);
89 titlebar->isbanned = true;
93 /** Move a titlebar on top of its client.
94 * \param titlebar The titlebar.
96 void
97 titlebar_unban(wibox_t *titlebar)
99 /* Do this manually because the system doesn't know we moved the toolbar.
100 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
101 * end up offscreen anyway. */
102 if(titlebar && titlebar->isbanned)
104 client_t *c;
105 simple_window_t *sw = &titlebar->sw;
107 if(sw->window)
109 /* All resizing is done, so only move now. */
110 uint32_t request[] = { sw->geometry.x, sw->geometry.y };
112 xcb_configure_window(globalconf.connection, sw->window,
113 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
114 request);
117 titlebar->isbanned = false;
119 /* Add titlebar geometry from client. */
120 if((c = client_getbytitlebar(titlebar)))
121 c->geometry = titlebar_geometry_add(titlebar, 0, c->geometry);
125 /** Get titlebar area.
126 * \param c The client
127 * \param geometry The client geometry including borders, excluding titlebars.
128 * \param res Pointer to area of titlebar, must be allocated already.
130 void
131 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
133 int height, width, x_offset = 0, y_offset = 0;
135 switch(c->titlebar->position)
137 default:
138 return;
139 case Top:
140 width = MAX(1, geometry.width);
141 switch(c->titlebar->align)
143 default:
144 break;
145 case AlignRight:
146 x_offset = geometry.width - width;
147 break;
148 case AlignCenter:
149 x_offset = (geometry.width - width) / 2;
150 break;
152 res->x = geometry.x + x_offset;
153 res->y = geometry.y - c->titlebar->sw.geometry.height;
154 res->width = width;
155 res->height = c->titlebar->sw.geometry.height;
156 break;
157 case Bottom:
158 width = MAX(1, geometry.width);
159 switch(c->titlebar->align)
161 default:
162 break;
163 case AlignRight:
164 x_offset = geometry.width - width;
165 break;
166 case AlignCenter:
167 x_offset = (geometry.width - width) / 2;
168 break;
170 res->x = geometry.x + x_offset;
171 res->y = geometry.y + geometry.height;
172 res->width = width;
173 res->height = c->titlebar->sw.geometry.height;
174 break;
175 case Left:
176 height = MAX(1, geometry.height);
177 switch(c->titlebar->align)
179 default:
180 break;
181 case AlignRight:
182 y_offset = geometry.height - height;
183 break;
184 case AlignCenter:
185 y_offset = (geometry.height - height) / 2;
186 break;
188 res->x = geometry.x - c->titlebar->sw.geometry.width;
189 res->y = geometry.y + y_offset;
190 res->width = c->titlebar->sw.geometry.width;
191 res->height = height;
192 break;
193 case Right:
194 height = MAX(1, geometry.height);
195 switch(c->titlebar->align)
197 default:
198 break;
199 case AlignRight:
200 y_offset = geometry.height - height;
201 break;
202 case AlignCenter:
203 y_offset = (geometry.height - height) / 2;
204 break;
206 res->x = geometry.x + geometry.width;
207 res->y = geometry.y + y_offset;
208 res->width = c->titlebar->sw.geometry.width;
209 res->height = height;
210 break;
214 /** Detach a wibox titlebar from its client.
215 * \param c The client.
217 void
218 titlebar_client_detach(client_t *c)
220 /* If client has a titlebar, kick it out. */
221 if(c && c->titlebar)
223 /* Update client geometry to exclude the titlebar. */
224 c->geometry = titlebar_geometry_remove(c->titlebar, 0, c->geometry);
225 simplewindow_wipe(&c->titlebar->sw);
226 c->titlebar->type = WIBOX_TYPE_NORMAL;
227 c->titlebar->screen = SCREEN_UNDEF;
228 wibox_unref(&c->titlebar);
229 c->titlebar = NULL;
230 client_need_arrange(c);
231 client_stack();
235 /** Attach a wibox to a client as its titlebar.
236 * \param c The client.
237 * \param t The wibox/titlebar.
239 void
240 titlebar_client_attach(client_t *c, wibox_t *t)
242 titlebar_client_detach(c);
244 if(c && t)
246 area_t wingeom;
248 /* check if titlebar is already on a client */
249 titlebar_client_detach(client_getbytitlebar(t));
251 /* check if client already has a titlebar. */
252 titlebar_client_detach(c);
254 c->titlebar = wibox_ref(&t);
255 t->type = WIBOX_TYPE_TITLEBAR;
256 t->screen = c->screen;
258 switch(t->position)
260 case Floating:
261 t->position = Top;
262 case Top:
263 case Bottom:
264 if(!t->sw.geometry.height)
265 t->sw.geometry.height = 1.5 * globalconf.font->height;
266 break;
267 case Left:
268 case Right:
269 if(!t->sw.geometry.width)
270 t->sw.geometry.width = 1.5 * globalconf.font->height;
271 break;
274 /* Update client geometry to include the titlebar. */
275 c->geometry = titlebar_geometry_add(c->titlebar, 0, c->geometry);
277 /* Client geometry without titlebar, but including borders, since that is always consistent. */
278 titlebar_geometry_compute(c, titlebar_geometry_remove(c->titlebar, 0, c->geometry), &wingeom);
280 simplewindow_init(&t->sw, c->phys_screen,
281 wingeom, 0, t->sw.orientation,
282 &t->sw.ctx.fg, &t->sw.ctx.bg);
283 simplewindow_border_color_set(&t->sw, &t->sw.border.color);
285 t->need_update = true;
287 /* Call update geometry. This will move the wibox to the right place,
288 * which might be the same as `wingeom', but then it will ban the
289 * titlebar if needed. */
290 titlebar_update_geometry(c);
292 xcb_map_window(globalconf.connection, t->sw.window);
294 client_need_arrange(c);
295 client_stack();
299 /** Map or unmap a titlebar wibox.
300 * \param t The wibox/titlebar.
301 * \param visible The new state of the titlebar.
303 void
304 titlebar_set_visible(wibox_t *t, bool visible)
306 if(visible != t->isvisible)
308 if((t->isvisible = visible))
309 titlebar_unban(t);
310 else
311 titlebar_ban(t);
313 globalconf.screens[t->screen].need_arrange = true;
314 client_stack();
318 /** Titlebar newindex.
319 * \param L The Lua VM state.
320 * \param titlebar The wibox titlebar.
321 * \param tok The attribute token.
322 * \return The number of elements pushed on stack.
325 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
327 client_t *c = NULL;
329 switch(tok)
331 position_t position;
332 int i;
333 size_t len;
334 const char *buf;
336 case A_TK_ALIGN:
337 if((buf = luaL_checklstring(L, 3, &len)))
338 titlebar->align = draw_align_fromstr(buf, len);
339 else
340 return 0;
341 break;
342 case A_TK_BORDER_WIDTH:
343 if((i = luaL_checknumber(L, 3)) >= 0)
344 simplewindow_border_width_set(&titlebar->sw, i);
345 else
346 return 0;
347 break;
348 case A_TK_BORDER_COLOR:
349 if((buf = luaL_checklstring(L, 3, &len)))
350 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->sw.border.color, buf, len)))
351 simplewindow_border_color_set(&titlebar->sw, &titlebar->sw.border.color);
352 return 0;
353 case A_TK_POSITION:
354 buf = luaL_checklstring(L, 3, &len);
355 position = position_fromstr(buf, len);
356 if(position != titlebar->position)
358 switch(position)
360 case Left:
361 switch(titlebar->position)
363 int tmp;
364 case Left:
365 case Right:
366 break;
367 case Top:
368 case Bottom:
369 case Floating:
370 tmp = titlebar->sw.geometry.width;
371 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
372 titlebar->sw.geometry.height = tmp;
373 break;
375 simplewindow_orientation_set(&titlebar->sw, North);
376 break;
377 case Right:
378 switch(titlebar->position)
380 int tmp;
381 case Left:
382 case Right:
383 break;
384 case Top:
385 case Bottom:
386 case Floating:
387 tmp = titlebar->sw.geometry.width;
388 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
389 titlebar->sw.geometry.height = tmp;
390 break;
392 simplewindow_orientation_set(&titlebar->sw, South);
393 break;
394 case Top:
395 case Bottom:
396 case Floating:
397 switch(titlebar->position)
399 int tmp;
400 case Left:
401 case Right:
402 tmp = titlebar->sw.geometry.width;
403 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
404 titlebar->sw.geometry.height = tmp;
405 break;
406 case Top:
407 case Bottom:
408 case Floating:
409 break;
411 simplewindow_orientation_set(&titlebar->sw, East);
412 break;
414 titlebar->position = position;
415 if((c = client_getbytitlebar(titlebar)))
417 titlebar_update_geometry(c);
418 /* call geometry hook for client because some like to
419 * set titlebar width in that hook, which make sense */
420 hooks_property(c, "geometry");
423 break;
424 default:
425 return 0;
428 if((c || (c = client_getbytitlebar(titlebar))))
429 client_need_arrange(c);
431 return 0;
434 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80