]> icculus.org git repositories - dana/openbox.git/blob - openbox/frame.c
add support for _NET_REQUEST_FRAME_EXTENTS
[dana/openbox.git] / openbox / frame.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    frame.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "frame.h"
21 #include "client.h"
22 #include "openbox.h"
23 #include "extensions.h"
24 #include "prop.h"
25 #include "config.h"
26 #include "framerender.h"
27 #include "mainloop.h"
28 #include "focus.h"
29 #include "moveresize.h"
30 #include "screen.h"
31 #include "render/theme.h"
32
33 #define PLATE_EVENTMASK (SubstructureRedirectMask | FocusChangeMask)
34 #define FRAME_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
35                          ButtonPressMask | ButtonReleaseMask)
36 #define ELEMENT_EVENTMASK (ButtonPressMask | ButtonReleaseMask | \
37                            ButtonMotionMask | \
38                            EnterWindowMask | LeaveWindowMask)
39 /* The inner window does not need enter/leave events.
40    If it does get them, then it needs its own context for enter events
41    because sloppy focus will focus the window when you enter the inner window
42    from the frame. */
43 #define INNER_EVENTMASK (ButtonPressMask)
44
45 #define FRAME_ANIMATE_ICONIFY_TIME 150000 /* .15 seconds */
46 #define FRAME_ANIMATE_ICONIFY_STEP_TIME (G_USEC_PER_SEC / 60) /* 60 Hz */
47
48 #define FRAME_HANDLE_Y(f) (f->innersize.top + f->client->area.height + \
49                            f->cbwidth_y)
50
51 /* the offsets for the titlebar elements from the edge of the titlebar.
52    negative means from the right edge. */
53 gint icon_off;
54 gint label_off;
55 gint iconify_off;
56 gint desk_off;
57 gint shade_off;
58 gint max_off;
59 gint close_off;
60
61
62 static void flash_done(gpointer data);
63 static gboolean flash_timeout(gpointer data);
64
65 static void layout_title(ObFrame *self);
66 static void set_theme_statics(ObFrame *self);
67 static void free_theme_statics(ObFrame *self);
68 static gboolean frame_animate_iconify(gpointer self);
69
70 static Window createWindow(Window parent, Visual *visual,
71                            gulong mask, XSetWindowAttributes *attrib)
72 {
73     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
74                          (visual ? 32 : RrDepth(ob_rr_inst)), InputOutput,
75                          (visual ? visual : RrVisual(ob_rr_inst)),
76                          mask, attrib);
77                        
78 }
79
80 static Visual *check_32bit_client(ObClient *c)
81 {
82     XWindowAttributes wattrib;
83     Status ret;
84
85     ret = XGetWindowAttributes(ob_display, c->window, &wattrib);
86     g_assert(ret != BadDrawable);
87     g_assert(ret != BadWindow);
88
89     if (wattrib.depth == 32)
90         return wattrib.visual;
91     return NULL;
92 }
93
94 ObFrame *frame_new(ObClient *client)
95 {
96     XSetWindowAttributes attrib;
97     gulong mask;
98     ObFrame *self;
99     Visual *visual;
100
101     self = g_new0(ObFrame, 1);
102     self->client = client;
103
104     visual = check_32bit_client(client);
105
106     /* create the non-visible decor windows */
107
108     mask = CWEventMask;
109     if (visual) {
110         /* client has a 32-bit visual */
111         mask |= CWColormap | CWBackPixel | CWBorderPixel;
112         /* create a colormap with the visual */
113         self->colormap = attrib.colormap =
114             XCreateColormap(ob_display,
115                             RootWindow(ob_display, ob_screen),
116                             visual, AllocNone);
117         attrib.background_pixel = BlackPixel(ob_display, 0);
118         attrib.border_pixel = BlackPixel(ob_display, 0);
119     }
120     attrib.event_mask = FRAME_EVENTMASK;
121     self->window = createWindow(RootWindow(ob_display, ob_screen), visual,
122                                 mask, &attrib);
123
124     attrib.event_mask = INNER_EVENTMASK;
125     self->inner = createWindow(self->window, visual, mask, &attrib);
126
127     mask &= ~CWEventMask;
128     self->plate = createWindow(self->inner, visual, mask, &attrib);
129
130     /* create the visible decor windows */
131
132     mask = CWEventMask;
133     if (visual) {
134         /* client has a 32-bit visual */
135         mask |= CWColormap | CWBackPixel | CWBorderPixel;
136         attrib.colormap = RrColormap(ob_rr_inst);
137     }
138     attrib.event_mask = ELEMENT_EVENTMASK;
139     self->title = createWindow(self->window, NULL, mask, &attrib);
140
141     mask |= CWCursor;
142     attrib.cursor = ob_cursor(OB_CURSOR_NORTHWEST);
143     self->tltresize = createWindow(self->title, NULL, mask, &attrib);
144     self->tllresize = createWindow(self->title, NULL, mask, &attrib);
145     attrib.cursor = ob_cursor(OB_CURSOR_NORTHEAST);
146     self->trtresize = createWindow(self->title, NULL, mask, &attrib);
147     self->trrresize = createWindow(self->title, NULL, mask, &attrib);
148
149     mask &= ~CWCursor;
150     self->label = createWindow(self->title, NULL, mask, &attrib);
151     self->max = createWindow(self->title, NULL, mask, &attrib);
152     self->close = createWindow(self->title, NULL, mask, &attrib);
153     self->desk = createWindow(self->title, NULL, mask, &attrib);
154     self->shade = createWindow(self->title, NULL, mask, &attrib);
155     self->icon = createWindow(self->title, NULL, mask, &attrib);
156     self->iconify = createWindow(self->title, NULL, mask, &attrib);
157     self->handle = createWindow(self->window, NULL, mask, &attrib);
158
159     mask |= CWCursor;
160     attrib.cursor = ob_cursor(OB_CURSOR_SOUTHWEST);
161     self->lgrip = createWindow(self->handle, NULL, mask, &attrib);
162     attrib.cursor = ob_cursor(OB_CURSOR_SOUTHEAST);
163     self->rgrip = createWindow(self->handle, NULL, mask, &attrib); 
164
165     self->focused = FALSE;
166
167     /* the other stuff is shown based on decor settings */
168     XMapWindow(ob_display, self->plate);
169     XMapWindow(ob_display, self->inner);
170     XMapWindow(ob_display, self->lgrip);
171     XMapWindow(ob_display, self->rgrip);
172     XMapWindow(ob_display, self->label);
173
174     self->max_press = self->close_press = self->desk_press = 
175         self->iconify_press = self->shade_press = FALSE;
176     self->max_hover = self->close_hover = self->desk_hover = 
177         self->iconify_hover = self->shade_hover = FALSE;
178
179     set_theme_statics(self);
180
181     return (ObFrame*)self;
182 }
183
184 static void set_theme_statics(ObFrame *self)
185 {
186     /* set colors/appearance/sizes for stuff that doesn't change */
187     XSetWindowBorder(ob_display, self->window,
188                      RrColorPixel(ob_rr_theme->frame_b_color));
189     XSetWindowBorder(ob_display, self->inner,
190                      RrColorPixel(ob_rr_theme->frame_b_color));
191     XSetWindowBorder(ob_display, self->title,
192                      RrColorPixel(ob_rr_theme->frame_b_color));
193     XSetWindowBorder(ob_display, self->handle,
194                      RrColorPixel(ob_rr_theme->frame_b_color));
195     XSetWindowBorder(ob_display, self->rgrip,
196                      RrColorPixel(ob_rr_theme->frame_b_color));
197     XSetWindowBorder(ob_display, self->lgrip,
198                      RrColorPixel(ob_rr_theme->frame_b_color));
199
200     XResizeWindow(ob_display, self->max,
201                   ob_rr_theme->button_size, ob_rr_theme->button_size);
202     XResizeWindow(ob_display, self->iconify,
203                   ob_rr_theme->button_size, ob_rr_theme->button_size);
204     XResizeWindow(ob_display, self->icon,
205                   ob_rr_theme->button_size + 2, ob_rr_theme->button_size + 2);
206     XResizeWindow(ob_display, self->close,
207                   ob_rr_theme->button_size, ob_rr_theme->button_size);
208     XResizeWindow(ob_display, self->desk,
209                   ob_rr_theme->button_size, ob_rr_theme->button_size);
210     XResizeWindow(ob_display, self->shade,
211                   ob_rr_theme->button_size, ob_rr_theme->button_size);
212     if (ob_rr_theme->handle_height > 0) {
213         XResizeWindow(ob_display, self->lgrip,
214                       ob_rr_theme->grip_width, ob_rr_theme->handle_height);
215         XResizeWindow(ob_display, self->rgrip,
216                       ob_rr_theme->grip_width, ob_rr_theme->handle_height);
217     }
218     XResizeWindow(ob_display, self->tltresize,
219                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
220     XResizeWindow(ob_display, self->trtresize,
221                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
222     XResizeWindow(ob_display, self->tllresize,
223                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
224     XResizeWindow(ob_display, self->trrresize,
225                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
226
227     /* set up the dynamic appearances */
228     self->a_unfocused_title = RrAppearanceCopy(ob_rr_theme->a_unfocused_title);
229     self->a_focused_title = RrAppearanceCopy(ob_rr_theme->a_focused_title);
230     self->a_unfocused_label = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
231     self->a_focused_label = RrAppearanceCopy(ob_rr_theme->a_focused_label);
232     self->a_unfocused_handle =
233         RrAppearanceCopy(ob_rr_theme->a_unfocused_handle);
234     self->a_focused_handle = RrAppearanceCopy(ob_rr_theme->a_focused_handle);
235     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_icon);
236 }
237
238 static void free_theme_statics(ObFrame *self)
239 {
240     RrAppearanceFree(self->a_unfocused_title); 
241     RrAppearanceFree(self->a_focused_title);
242     RrAppearanceFree(self->a_unfocused_label);
243     RrAppearanceFree(self->a_focused_label);
244     RrAppearanceFree(self->a_unfocused_handle);
245     RrAppearanceFree(self->a_focused_handle);
246     RrAppearanceFree(self->a_icon);
247 }
248
249 void frame_free(ObFrame *self)
250 {
251     free_theme_statics(self);
252
253     XDestroyWindow(ob_display, self->window);
254     if (self->colormap)
255         XFreeColormap(ob_display, self->colormap);
256
257     g_free(self);
258 }
259
260 void frame_show(ObFrame *self)
261 {
262     if (!self->visible) {
263         self->visible = TRUE;
264         XMapWindow(ob_display, self->client->window);
265         XMapWindow(ob_display, self->window);
266     }
267 }
268
269 void frame_hide(ObFrame *self)
270 {
271     if (self->visible) {
272         self->visible = FALSE;
273         if (!frame_iconify_animating(self))
274             XUnmapWindow(ob_display, self->window);
275         /* we unmap the client itself so that we can get MapRequest
276            events, and because the ICCCM tells us to! */
277         XUnmapWindow(ob_display, self->client->window);
278         self->client->ignore_unmaps += 1;
279     }
280 }
281
282 void frame_adjust_theme(ObFrame *self)
283 {
284     free_theme_statics(self);
285     set_theme_statics(self);
286 }
287
288 void frame_adjust_shape(ObFrame *self)
289 {
290 #ifdef SHAPE
291     gint num;
292     XRectangle xrect[2];
293
294     if (!self->client->shaped) {
295         /* clear the shape on the frame window */
296         XShapeCombineMask(ob_display, self->window, ShapeBounding,
297                           self->innersize.left,
298                           self->innersize.top,
299                           None, ShapeSet);
300     } else {
301         /* make the frame's shape match the clients */
302         XShapeCombineShape(ob_display, self->window, ShapeBounding,
303                            self->innersize.left,
304                            self->innersize.top,
305                            self->client->window,
306                            ShapeBounding, ShapeSet);
307
308         num = 0;
309         if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
310             xrect[0].x = -ob_rr_theme->fbwidth;
311             xrect[0].y = -ob_rr_theme->fbwidth;
312             xrect[0].width = self->width + self->rbwidth * 2;
313             xrect[0].height = ob_rr_theme->title_height +
314                 self->bwidth * 2;
315             ++num;
316         }
317
318         if (self->decorations & OB_FRAME_DECOR_HANDLE) {
319             xrect[1].x = -ob_rr_theme->fbwidth;
320             xrect[1].y = FRAME_HANDLE_Y(self);
321             xrect[1].width = self->width + self->rbwidth * 2;
322             xrect[1].height = ob_rr_theme->handle_height +
323                 self->bwidth * 2;
324             ++num;
325         }
326
327         XShapeCombineRectangles(ob_display, self->window,
328                                 ShapeBounding, 0, 0, xrect, num,
329                                 ShapeUnion, Unsorted);
330     }
331 #endif
332 }
333
334 void frame_adjust_area(ObFrame *self, gboolean moved,
335                        gboolean resized, gboolean fake)
336 {
337     Strut oldsize;
338
339     oldsize = self->size;
340
341     if (resized) {
342         self->decorations = self->client->decorations;
343         self->max_horz = self->client->max_horz;
344
345         if (self->decorations & OB_FRAME_DECOR_BORDER) {
346             self->bwidth = ob_rr_theme->fbwidth;
347             self->cbwidth_x = ob_rr_theme->cbwidthx;
348             self->cbwidth_y = ob_rr_theme->cbwidthy;
349         } else {
350             self->bwidth = self->cbwidth_x = self->cbwidth_y = 0;
351         }
352         self->rbwidth = self->bwidth;
353
354         if (self->max_horz)
355             self->bwidth = self->cbwidth_x = 0;
356
357         STRUT_SET(self->innersize,
358                   self->cbwidth_x,
359                   self->cbwidth_y,
360                   self->cbwidth_x,
361                   self->cbwidth_y);
362         self->width = self->client->area.width + self->cbwidth_x * 2 -
363             (self->max_horz ? self->rbwidth * 2 : 0);
364         self->width = MAX(self->width, 1); /* no lower than 1 */
365
366         /* set border widths */
367         if (!fake) {
368             XSetWindowBorderWidth(ob_display, self->window, self->bwidth);
369             XSetWindowBorderWidth(ob_display, self->inner, self->bwidth);
370             XSetWindowBorderWidth(ob_display, self->title,  self->rbwidth);
371             XSetWindowBorderWidth(ob_display, self->handle, self->rbwidth);
372             XSetWindowBorderWidth(ob_display, self->lgrip,  self->rbwidth);
373             XSetWindowBorderWidth(ob_display, self->rgrip,  self->rbwidth);
374         }
375
376         if (self->decorations & OB_FRAME_DECOR_TITLEBAR)
377             self->innersize.top += ob_rr_theme->title_height + self->rbwidth +
378                 (self->rbwidth - self->bwidth);
379         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
380             ob_rr_theme->handle_height > 0)
381             self->innersize.bottom += ob_rr_theme->handle_height +
382                 self->rbwidth + (self->rbwidth - self->bwidth);
383   
384         /* position/size and map/unmap all the windows */
385
386         if (!fake) {
387             if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
388                 XMoveResizeWindow(ob_display, self->title,
389                                   -self->bwidth, -self->bwidth,
390                                   self->width, ob_rr_theme->title_height);
391                 XMapWindow(ob_display, self->title);
392
393                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
394                     XMoveWindow(ob_display, self->tltresize, 0, 0);
395                     XMoveWindow(ob_display, self->tllresize, 0, 0);
396                     XMoveWindow(ob_display, self->trtresize,
397                                 self->width - ob_rr_theme->grip_width, 0);
398                     XMoveWindow(ob_display, self->trrresize,
399                                 self->width - ob_rr_theme->paddingx - 1, 0);
400                     XMapWindow(ob_display, self->tltresize);
401                     XMapWindow(ob_display, self->tllresize);
402                     XMapWindow(ob_display, self->trtresize);
403                     XMapWindow(ob_display, self->trrresize);
404                 } else {
405                     XUnmapWindow(ob_display, self->tltresize);
406                     XUnmapWindow(ob_display, self->tllresize);
407                     XUnmapWindow(ob_display, self->trtresize);
408                     XUnmapWindow(ob_display, self->trrresize);
409                 }
410             } else
411                 XUnmapWindow(ob_display, self->title);
412         }
413
414         if ((self->decorations & OB_FRAME_DECOR_TITLEBAR))
415             /* layout the title bar elements */
416             layout_title(self);
417
418         if (!fake) {
419             if (self->decorations & OB_FRAME_DECOR_HANDLE &&
420                 ob_rr_theme->handle_height > 0)
421             {
422                 XMoveResizeWindow(ob_display, self->handle,
423                                   -self->bwidth, FRAME_HANDLE_Y(self),
424                                   self->width, ob_rr_theme->handle_height);
425                 XMapWindow(ob_display, self->handle);
426
427                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
428                     XMoveWindow(ob_display, self->lgrip,
429                                 -self->rbwidth, -self->rbwidth);
430                     XMoveWindow(ob_display, self->rgrip,
431                                 -self->rbwidth + self->width -
432                                 ob_rr_theme->grip_width, -self->rbwidth);
433                     XMapWindow(ob_display, self->lgrip);
434                     XMapWindow(ob_display, self->rgrip);
435                 } else {
436                     XUnmapWindow(ob_display, self->lgrip);
437                     XUnmapWindow(ob_display, self->rgrip);
438                 }
439             } else
440                 XUnmapWindow(ob_display, self->handle);
441
442             /* move and resize the inner border window which contains the plate
443              */
444             XMoveResizeWindow(ob_display, self->inner,
445                               self->innersize.left - self->cbwidth_x -
446                               self->bwidth,
447                               self->innersize.top - self->cbwidth_y -
448                               self->bwidth,
449                               self->client->area.width +
450                               self->cbwidth_x * 2,
451                               self->client->area.height +
452                               self->cbwidth_y * 2);
453
454             /* move the plate */
455             XMoveWindow(ob_display, self->plate,
456                         self->cbwidth_x, self->cbwidth_y);
457
458             /* when the client has StaticGravity, it likes to move around. */
459             XMoveWindow(ob_display, self->client->window, 0, 0);
460         }
461
462         STRUT_SET(self->size,
463                   self->innersize.left + self->bwidth,
464                   self->innersize.top + self->bwidth,
465                   self->innersize.right + self->bwidth,
466                   self->innersize.bottom + self->bwidth);
467     }
468
469     /* shading can change without being moved or resized */
470     RECT_SET_SIZE(self->area,
471                   self->client->area.width +
472                   self->size.left + self->size.right,
473                   (self->client->shaded ?
474                    ob_rr_theme->title_height + self->rbwidth * 2:
475                    self->client->area.height +
476                    self->size.top + self->size.bottom));
477
478     if (moved || resized) {
479         /* find the new coordinates, done after setting the frame.size, for
480            frame_client_gravity. */
481         self->area.x = self->client->area.x;
482         self->area.y = self->client->area.y;
483         frame_client_gravity(self, &self->area.x, &self->area.y,
484                              self->client->area.width,
485                              self->client->area.height);
486     }
487
488     if (!fake) {
489         if (!frame_iconify_animating(self))
490             /* move and resize the top level frame.
491                shading can change without being moved or resized.
492                
493                but don't do this during an iconify animation. it will be
494                reflected afterwards.
495             */
496             XMoveResizeWindow(ob_display, self->window,
497                               self->area.x, self->area.y,
498                               self->area.width - self->bwidth * 2,
499                               self->area.height - self->bwidth * 2);
500
501         if (resized) {
502             framerender_frame(self);
503             frame_adjust_shape(self);
504         }
505
506         if (!STRUT_EQUAL(self->size, oldsize)) {
507             gulong vals[4];
508             vals[0] = self->size.left;
509             vals[1] = self->size.right;
510             vals[2] = self->size.top;
511             vals[3] = self->size.bottom;
512             PROP_SETA32(self->client->window, net_frame_extents,
513                         cardinal, vals, 4);
514             PROP_SETA32(self->client->window, kde_net_wm_frame_strut,
515                         cardinal, vals, 4);
516         }
517
518         /* if this occurs while we are focus cycling, the indicator needs to
519            match the changes */
520         if (focus_cycle_target == self->client)
521             focus_cycle_draw_indicator();
522     }
523     if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR))
524         XResizeWindow(ob_display, self->label, self->label_width,
525                       ob_rr_theme->label_height);
526 }
527
528 void frame_adjust_client_area(ObFrame *self)
529 {
530     /* resize the plate */
531     XResizeWindow(ob_display, self->plate,
532                   self->client->area.width, self->client->area.height);
533 }
534
535 void frame_adjust_state(ObFrame *self)
536 {
537     framerender_frame(self);
538 }
539
540 void frame_adjust_focus(ObFrame *self, gboolean hilite)
541 {
542     self->focused = hilite;
543     framerender_frame(self);
544     XFlush(ob_display);
545 }
546
547 void frame_adjust_title(ObFrame *self)
548 {
549     framerender_frame(self);
550 }
551
552 void frame_adjust_icon(ObFrame *self)
553 {
554     framerender_frame(self);
555 }
556
557 void frame_grab_client(ObFrame *self)
558 {
559     /* reparent the client to the frame */
560     XReparentWindow(ob_display, self->client->window, self->plate, 0, 0);
561     /*
562       When reparenting the client window, it is usually not mapped yet, since
563       this occurs from a MapRequest. However, in the case where Openbox is
564       starting up, the window is already mapped, so we'll see unmap events for
565       it. There are 2 unmap events generated that we see, one with the 'event'
566       member set the root window, and one set to the client, but both get
567       handled and need to be ignored.
568     */
569     if (ob_state() == OB_STATE_STARTING)
570         self->client->ignore_unmaps += 2;
571
572     /* select the event mask on the client's parent (to receive config/map
573        req's) the ButtonPress is to catch clicks on the client border */
574     XSelectInput(ob_display, self->plate, PLATE_EVENTMASK);
575
576     /* map the client so it maps when the frame does */
577     XMapWindow(ob_display, self->client->window);
578
579     /* adjust the frame to the client's size */
580     frame_adjust_area(self, FALSE, TRUE, FALSE);
581
582     /* set all the windows for the frame in the window_map */
583     g_hash_table_insert(window_map, &self->window, self->client);
584     g_hash_table_insert(window_map, &self->plate, self->client);
585     g_hash_table_insert(window_map, &self->inner, self->client);
586     g_hash_table_insert(window_map, &self->title, self->client);
587     g_hash_table_insert(window_map, &self->label, self->client);
588     g_hash_table_insert(window_map, &self->max, self->client);
589     g_hash_table_insert(window_map, &self->close, self->client);
590     g_hash_table_insert(window_map, &self->desk, self->client);
591     g_hash_table_insert(window_map, &self->shade, self->client);
592     g_hash_table_insert(window_map, &self->icon, self->client);
593     g_hash_table_insert(window_map, &self->iconify, self->client);
594     g_hash_table_insert(window_map, &self->handle, self->client);
595     g_hash_table_insert(window_map, &self->lgrip, self->client);
596     g_hash_table_insert(window_map, &self->rgrip, self->client);
597     g_hash_table_insert(window_map, &self->tltresize, self->client);
598     g_hash_table_insert(window_map, &self->tllresize, self->client);
599     g_hash_table_insert(window_map, &self->trtresize, self->client);
600     g_hash_table_insert(window_map, &self->trrresize, self->client);
601 }
602
603 void frame_release_client(ObFrame *self)
604 {
605     XEvent ev;
606     gboolean reparent = TRUE;
607
608     /* if there was any animation going on, kill it */
609     ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
610                                      self, FALSE);
611
612     /* check if the app has already reparented its window away */
613     while (XCheckTypedWindowEvent(ob_display, self->client->window,
614                                   ReparentNotify, &ev))
615     {
616         /* This check makes sure we don't catch our own reparent action to
617            our frame window. This doesn't count as the app reparenting itself
618            away of course.
619
620            Reparent events that are generated by us are just discarded here.
621            They are of no consequence to us anyhow.
622         */
623         if (ev.xreparent.parent != self->plate) {
624             reparent = FALSE;
625             XPutBackEvent(ob_display, &ev);
626             break;
627         }
628     }
629
630     if (reparent) {
631         /* according to the ICCCM - if the client doesn't reparent itself,
632            then we will reparent the window to root for them */
633         XReparentWindow(ob_display, self->client->window,
634                         RootWindow(ob_display, ob_screen),
635                         self->client->area.x,
636                         self->client->area.y);
637     }
638
639     /* remove all the windows for the frame from the window_map */
640     g_hash_table_remove(window_map, &self->window);
641     g_hash_table_remove(window_map, &self->plate);
642     g_hash_table_remove(window_map, &self->inner);
643     g_hash_table_remove(window_map, &self->title);
644     g_hash_table_remove(window_map, &self->label);
645     g_hash_table_remove(window_map, &self->max);
646     g_hash_table_remove(window_map, &self->close);
647     g_hash_table_remove(window_map, &self->desk);
648     g_hash_table_remove(window_map, &self->shade);
649     g_hash_table_remove(window_map, &self->icon);
650     g_hash_table_remove(window_map, &self->iconify);
651     g_hash_table_remove(window_map, &self->handle);
652     g_hash_table_remove(window_map, &self->lgrip);
653     g_hash_table_remove(window_map, &self->rgrip);
654     g_hash_table_remove(window_map, &self->tltresize);
655     g_hash_table_remove(window_map, &self->tllresize);
656     g_hash_table_remove(window_map, &self->trtresize);
657     g_hash_table_remove(window_map, &self->trrresize);
658
659     ob_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
660 }
661
662 /* is there anything present between us and the label? */
663 static gboolean is_button_present(ObFrame *self, const gchar *lc, gint dir) {
664     for (; *lc != '\0' && lc >= config_title_layout; lc += dir) {
665         if (*lc == ' ') continue; /* it was invalid */
666         if (*lc == 'N' && self->decorations & OB_FRAME_DECOR_ICON)
667             return TRUE;
668         if (*lc == 'D' && self->decorations & OB_FRAME_DECOR_ALLDESKTOPS)
669             return TRUE;
670         if (*lc == 'S' && self->decorations & OB_FRAME_DECOR_SHADE)
671             return TRUE;
672         if (*lc == 'I' && self->decorations & OB_FRAME_DECOR_ICONIFY)
673             return TRUE;
674         if (*lc == 'M' && self->decorations & OB_FRAME_DECOR_MAXIMIZE)
675             return TRUE;
676         if (*lc == 'C' && self->decorations & OB_FRAME_DECOR_CLOSE)
677             return TRUE;
678         if (*lc == 'L') return FALSE;
679     }
680     return FALSE;
681 }
682
683 static void layout_title(ObFrame *self)
684 {
685     gchar *lc;
686     gint i, x;
687
688     const gint bwidth = ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
689     /* position of the left most button */
690     const gint left = ob_rr_theme->paddingx + 1;
691     /* position of the right most button */
692     const gint right = self->width - bwidth;
693
694     /* turn them all off */
695     self->icon_on = self->desk_on = self->shade_on = self->iconify_on =
696         self->max_on = self->close_on = self->label_on = FALSE;
697     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
698
699     /* figure out what's being show, find each element's position, and the
700        width of the label
701
702        do the ones before the label, then after the label,
703        i will be +1 the first time through when working to the left,
704        and -1 the second time through when working to the right */
705     for (i = 1; i >= -1; i-=2) {
706         if (i > 0) {
707             x = left;
708             lc = config_title_layout;
709         } else {
710             x = right;
711             lc = config_title_layout + strlen(config_title_layout)-1;
712         }
713
714         /* stop at the end of the string (or the label, which calls break) */
715         for (; *lc != '\0' && lc >= config_title_layout; lc+=i)
716             if (*lc == 'L') {
717                 if (i > 0) {
718                     self->label_on = TRUE;
719                     self->label_x = x;
720                 }
721                 break; /* break the for loop, do other side of label */
722             } else if (*lc == 'N') {
723                 if ((self->icon_on = is_button_present(self, lc, i))) {
724                     /* icon gets extra padding */
725                     self->label_width -= bwidth + 2;
726                     self->icon_x = x + (i * 1);
727                     x += i * (bwidth + 2);
728                 }
729             } else if (*lc == 'D') {
730                 if ((self->desk_on = is_button_present(self, lc, i))) {
731                     self->label_width -= bwidth;
732                     self->desk_x = x;
733                     x += i * bwidth;
734                 }
735             } else if (*lc == 'S') {
736                 if ((self->shade_on = is_button_present(self, lc, i))) {
737                     self->label_width -= bwidth;
738                     self->shade_x = x;
739                     x += i * bwidth;
740                 }
741             } else if (*lc == 'I') {
742                 if ((self->iconify_on = is_button_present(self, lc, i))) {
743                     self->label_width -= bwidth;
744                     self->iconify_x = x;
745                     x += i * bwidth;
746                 }
747             } else if (*lc == 'M') {
748                 if ((self->max_on = is_button_present(self, lc, i))) {
749                     self->label_width -= bwidth;
750                     self->max_x = x;
751                     x += i * bwidth;
752                 }
753             } else if (*lc == 'C') {
754                 if ((self->close_on = is_button_present(self, lc, i))) {
755                     self->label_width -= bwidth;
756                     self->close_x = x;
757                     x += i * bwidth;
758                 }
759             }
760     }
761
762     /* position and map the elements */
763     if (self->icon_on) {
764         XMapWindow(ob_display, self->icon);
765         XMoveWindow(ob_display, self->icon, self->icon_x,
766                     ob_rr_theme->paddingy);
767     } else
768         XUnmapWindow(ob_display, self->icon);
769
770     if (self->desk_on) {
771         XMapWindow(ob_display, self->desk);
772         XMoveWindow(ob_display, self->desk, self->desk_x,
773                     ob_rr_theme->paddingy + 1);
774     } else
775         XUnmapWindow(ob_display, self->desk);
776
777     if (self->shade_on) {
778         XMapWindow(ob_display, self->shade);
779         XMoveWindow(ob_display, self->shade, self->shade_x,
780                     ob_rr_theme->paddingy + 1);
781     } else
782         XUnmapWindow(ob_display, self->shade);
783
784     if (self->iconify_on) {
785         XMapWindow(ob_display, self->iconify);
786         XMoveWindow(ob_display, self->iconify, self->iconify_x,
787                     ob_rr_theme->paddingy + 1);
788     } else
789         XUnmapWindow(ob_display, self->iconify);
790
791     if (self->max_on) {
792         XMapWindow(ob_display, self->max);
793         XMoveWindow(ob_display, self->max, self->max_x,
794                     ob_rr_theme->paddingy + 1);
795     } else
796         XUnmapWindow(ob_display, self->max);
797
798     if (self->close_on) {
799         XMapWindow(ob_display, self->close);
800         XMoveWindow(ob_display, self->close, self->close_x,
801                     ob_rr_theme->paddingy + 1);
802     } else
803         XUnmapWindow(ob_display, self->close);
804
805     if (self->label_on) {
806         self->label_width = MAX(1, self->label_width); /* no lower than 1 */
807         XMapWindow(ob_display, self->label);
808         XMoveWindow(ob_display, self->label, self->label_x,
809                     ob_rr_theme->paddingy);
810     } else
811         XUnmapWindow(ob_display, self->label);
812 }
813
814 ObFrameContext frame_context_from_string(const gchar *name)
815 {
816     if (!g_ascii_strcasecmp("Desktop", name))
817         return OB_FRAME_CONTEXT_DESKTOP;
818     else if (!g_ascii_strcasecmp("Client", name))
819         return OB_FRAME_CONTEXT_CLIENT;
820     else if (!g_ascii_strcasecmp("Titlebar", name))
821         return OB_FRAME_CONTEXT_TITLEBAR;
822     else if (!g_ascii_strcasecmp("Handle", name))
823         return OB_FRAME_CONTEXT_HANDLE;
824     else if (!g_ascii_strcasecmp("Frame", name))
825         return OB_FRAME_CONTEXT_FRAME;
826     else if (!g_ascii_strcasecmp("TLCorner", name))
827         return OB_FRAME_CONTEXT_TLCORNER;
828     else if (!g_ascii_strcasecmp("TRCorner", name))
829         return OB_FRAME_CONTEXT_TRCORNER;
830     else if (!g_ascii_strcasecmp("BLCorner", name))
831         return OB_FRAME_CONTEXT_BLCORNER;
832     else if (!g_ascii_strcasecmp("BRCorner", name))
833         return OB_FRAME_CONTEXT_BRCORNER;
834     else if (!g_ascii_strcasecmp("Maximize", name))
835         return OB_FRAME_CONTEXT_MAXIMIZE;
836     else if (!g_ascii_strcasecmp("AllDesktops", name))
837         return OB_FRAME_CONTEXT_ALLDESKTOPS;
838     else if (!g_ascii_strcasecmp("Shade", name))
839         return OB_FRAME_CONTEXT_SHADE;
840     else if (!g_ascii_strcasecmp("Iconify", name))
841         return OB_FRAME_CONTEXT_ICONIFY;
842     else if (!g_ascii_strcasecmp("Icon", name))
843         return OB_FRAME_CONTEXT_ICON;
844     else if (!g_ascii_strcasecmp("Close", name))
845         return OB_FRAME_CONTEXT_CLOSE;
846     else if (!g_ascii_strcasecmp("MoveResize", name))
847         return OB_FRAME_CONTEXT_MOVE_RESIZE;
848     return OB_FRAME_CONTEXT_NONE;
849 }
850
851 ObFrameContext frame_context(ObClient *client, Window win)
852 {
853     ObFrame *self;
854
855     if (moveresize_in_progress)
856         return OB_FRAME_CONTEXT_MOVE_RESIZE;
857
858     if (win == RootWindow(ob_display, ob_screen))
859         return OB_FRAME_CONTEXT_DESKTOP;
860     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
861     if (win == client->window) {
862         /* conceptually, this is the desktop, as far as users are
863            concerned */
864         if (client->type == OB_CLIENT_TYPE_DESKTOP)
865             return OB_FRAME_CONTEXT_DESKTOP;
866         return OB_FRAME_CONTEXT_CLIENT;
867     }
868
869     self = client->frame;
870     if (win == self->inner || win == self->plate) {
871         /* conceptually, this is the desktop, as far as users are
872            concerned */
873         if (client->type == OB_CLIENT_TYPE_DESKTOP)
874             return OB_FRAME_CONTEXT_DESKTOP;
875         return OB_FRAME_CONTEXT_CLIENT;
876     }
877
878     if (win == self->window)    return OB_FRAME_CONTEXT_FRAME;
879     if (win == self->title)     return OB_FRAME_CONTEXT_TITLEBAR;
880     if (win == self->label)     return OB_FRAME_CONTEXT_TITLEBAR;
881     if (win == self->handle)    return OB_FRAME_CONTEXT_HANDLE;
882     if (win == self->lgrip)     return OB_FRAME_CONTEXT_BLCORNER;
883     if (win == self->rgrip)     return OB_FRAME_CONTEXT_BRCORNER;
884     if (win == self->tltresize) return OB_FRAME_CONTEXT_TLCORNER;
885     if (win == self->tllresize) return OB_FRAME_CONTEXT_TLCORNER;
886     if (win == self->trtresize) return OB_FRAME_CONTEXT_TRCORNER;
887     if (win == self->trrresize) return OB_FRAME_CONTEXT_TRCORNER;
888     if (win == self->max)       return OB_FRAME_CONTEXT_MAXIMIZE;
889     if (win == self->iconify)   return OB_FRAME_CONTEXT_ICONIFY;
890     if (win == self->close)     return OB_FRAME_CONTEXT_CLOSE;
891     if (win == self->icon)      return OB_FRAME_CONTEXT_ICON;
892     if (win == self->desk)      return OB_FRAME_CONTEXT_ALLDESKTOPS;
893     if (win == self->shade)     return OB_FRAME_CONTEXT_SHADE;
894
895     return OB_FRAME_CONTEXT_NONE;
896 }
897
898 void frame_client_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
899 {
900     /* horizontal */
901     switch (self->client->gravity) {
902     default:
903     case NorthWestGravity:
904     case SouthWestGravity:
905     case WestGravity:
906         break;
907
908     case NorthGravity:
909     case SouthGravity:
910     case CenterGravity:
911         *x -= (self->size.left + w) / 2;
912         break;
913
914     case NorthEastGravity:
915     case SouthEastGravity:
916     case EastGravity:
917         *x -= (self->size.left + self->size.right + w) - 1;
918         break;
919
920     case ForgetGravity:
921     case StaticGravity:
922         *x -= self->size.left;
923         break;
924     }
925
926     /* vertical */
927     switch (self->client->gravity) {
928     default:
929     case NorthWestGravity:
930     case NorthEastGravity:
931     case NorthGravity:
932         break;
933
934     case CenterGravity:
935     case EastGravity:
936     case WestGravity:
937         *y -= (self->size.top + h) / 2;
938         break;
939
940     case SouthWestGravity:
941     case SouthEastGravity:
942     case SouthGravity:
943         *y -= (self->size.top + self->size.bottom + h) - 1;
944         break;
945
946     case ForgetGravity:
947     case StaticGravity:
948         *y -= self->size.top;
949         break;
950     }
951 }
952
953 void frame_frame_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
954 {
955     /* horizontal */
956     switch (self->client->gravity) {
957     default:
958     case NorthWestGravity:
959     case WestGravity:
960     case SouthWestGravity:
961         break;
962     case NorthGravity:
963     case CenterGravity:
964     case SouthGravity:
965         *x += (self->size.left + w) / 2;
966         break;
967     case NorthEastGravity:
968     case EastGravity:
969     case SouthEastGravity:
970         *x += (self->size.left + self->size.right + w) - 1;
971         break;
972     case StaticGravity:
973     case ForgetGravity:
974         *x += self->size.left;
975         break;
976     }
977
978     /* vertical */
979     switch (self->client->gravity) {
980     default:
981     case NorthWestGravity:
982     case NorthGravity:
983     case NorthEastGravity:
984         break;
985     case WestGravity:
986     case CenterGravity:
987     case EastGravity:
988         *y += (self->size.top + h) / 2;
989         break;
990     case SouthWestGravity:
991     case SouthGravity:
992     case SouthEastGravity:
993         *y += (self->size.top + self->size.bottom + h) - 1;
994         break;
995     case StaticGravity:
996     case ForgetGravity:
997         *y += self->size.top;
998         break;
999     }
1000 }
1001
1002 static void flash_done(gpointer data)
1003 {
1004     ObFrame *self = data;
1005
1006     if (self->focused != self->flash_on)
1007         frame_adjust_focus(self, self->focused);
1008 }
1009
1010 static gboolean flash_timeout(gpointer data)
1011 {
1012     ObFrame *self = data;
1013     GTimeVal now;
1014
1015     g_get_current_time(&now);
1016     if (now.tv_sec > self->flash_end.tv_sec ||
1017         (now.tv_sec == self->flash_end.tv_sec &&
1018          now.tv_usec >= self->flash_end.tv_usec))
1019         self->flashing = FALSE;
1020
1021     if (!self->flashing)
1022         return FALSE; /* we are done */
1023
1024     self->flash_on = !self->flash_on;
1025     if (!self->focused) {
1026         frame_adjust_focus(self, self->flash_on);
1027         self->focused = FALSE;
1028     }
1029
1030     return TRUE; /* go again */
1031 }
1032
1033 void frame_flash_start(ObFrame *self)
1034 {
1035     self->flash_on = self->focused;
1036
1037     if (!self->flashing)
1038         ob_main_loop_timeout_add(ob_main_loop,
1039                                  G_USEC_PER_SEC * 0.6,
1040                                  flash_timeout,
1041                                  self,
1042                                  g_direct_equal,
1043                                  flash_done);
1044     g_get_current_time(&self->flash_end);
1045     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
1046     
1047     self->flashing = TRUE;
1048 }
1049
1050 void frame_flash_stop(ObFrame *self)
1051 {
1052     self->flashing = FALSE;
1053 }
1054
1055 static gulong frame_animate_iconify_time_left(ObFrame *self,
1056                                               const GTimeVal *now)
1057 {
1058     glong sec, usec;
1059     sec = self->iconify_animation_end.tv_sec - now->tv_sec;
1060     usec = self->iconify_animation_end.tv_usec - now->tv_usec;
1061     if (usec < 0) {
1062         usec += G_USEC_PER_SEC;
1063         sec--;
1064     }
1065     /* no negative values */
1066     return MAX(sec * G_USEC_PER_SEC + usec, 0);
1067 }
1068
1069 static gboolean frame_animate_iconify(gpointer p)
1070 {
1071     ObFrame *self = p;
1072     gint x, y, w, h;
1073     gint iconx, icony, iconw;
1074     GTimeVal now;
1075     gulong time;
1076     gboolean iconifying;
1077
1078     if (self->client->icon_geometry.width == 0) {
1079         /* there is no icon geometry set so just go straight down */
1080         Rect *a = screen_physical_area();
1081         iconx = self->area.x + self->area.width / 2 + 32;
1082         icony = a->y + a->width;
1083         iconw = 64;
1084     } else {
1085         iconx = self->client->icon_geometry.x;
1086         icony = self->client->icon_geometry.y;
1087         iconw = self->client->icon_geometry.width;
1088     }
1089
1090     iconifying = self->iconify_animation_going > 0;
1091
1092     /* how far do we have left to go ? */
1093     g_get_current_time(&now);
1094     time = frame_animate_iconify_time_left(self, &now);
1095     
1096     if (time == 0 || iconifying) {
1097         /* start where the frame is supposed to be */
1098         x = self->area.x;
1099         y = self->area.y;
1100         w = self->area.width - self->bwidth * 2;
1101         h = self->area.height - self->bwidth * 2;
1102     } else {
1103         /* start at the icon */
1104         x = iconx;
1105         y = icony;
1106         w = iconw;
1107         h = self->innersize.top; /* just the titlebar */
1108     }
1109
1110     if (time > 0) {
1111         glong dx, dy, dw;
1112         glong elapsed;
1113
1114         dx = self->area.x - iconx;
1115         dy = self->area.y - icony;
1116         dw = self->area.width - self->bwidth * 2 - iconw;
1117          /* if restoring, we move in the opposite direction */
1118         if (!iconifying) { dx = -dx; dy = -dy; dw = -dw; }
1119
1120         elapsed = FRAME_ANIMATE_ICONIFY_TIME - time;
1121         x = x - (dx * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1122         y = y - (dy * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1123         w = w - (dw * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1124         h = self->innersize.top; /* just the titlebar */
1125     }
1126
1127     if (time == 0)
1128         frame_end_iconify_animation(self);
1129     else {
1130         XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1131         XFlush(ob_display);
1132     }
1133
1134     return time > 0; /* repeat until we're out of time */
1135 }
1136
1137 void frame_end_iconify_animation(ObFrame *self)
1138 {
1139     /* see if there is an animation going */
1140     if (self->iconify_animation_going == 0) return;
1141
1142     if (!self->visible)
1143         XUnmapWindow(ob_display, self->window);
1144
1145     /* we're not animating any more ! */
1146     self->iconify_animation_going = 0;
1147
1148     XMoveResizeWindow(ob_display, self->window,
1149                       self->area.x, self->area.y,
1150                       self->area.width - self->bwidth * 2,
1151                       self->area.height - self->bwidth * 2);
1152     XFlush(ob_display);
1153 }
1154
1155 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying)
1156 {
1157     gulong time;
1158     gboolean new_anim = FALSE;
1159     gboolean set_end = TRUE;
1160     GTimeVal now;
1161
1162     /* if there is no titlebar, just don't animate for now
1163        XXX it would be nice tho.. */
1164     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1165         return;
1166
1167     /* get the current time */
1168     g_get_current_time(&now);
1169
1170     /* get how long until the end */
1171     time = FRAME_ANIMATE_ICONIFY_TIME;
1172     if (self->iconify_animation_going) {
1173         if (!!iconifying != (self->iconify_animation_going > 0)) {
1174             /* animation was already going on in the opposite direction */
1175             time = time - frame_animate_iconify_time_left(self, &now);
1176         } else
1177             /* animation was already going in the same direction */
1178             set_end = FALSE;
1179     } else
1180         new_anim = TRUE;
1181     self->iconify_animation_going = iconifying ? 1 : -1;
1182
1183     /* set the ending time */
1184     if (set_end) {
1185         self->iconify_animation_end.tv_sec = now.tv_sec;
1186         self->iconify_animation_end.tv_usec = now.tv_usec;
1187         g_time_val_add(&self->iconify_animation_end, time);
1188     }
1189
1190     if (new_anim) {
1191         ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
1192                                          self, FALSE);
1193         ob_main_loop_timeout_add(ob_main_loop,
1194                                  FRAME_ANIMATE_ICONIFY_STEP_TIME,
1195                                  frame_animate_iconify, self,
1196                                  g_direct_equal, NULL);
1197
1198         /* do the first step */
1199         frame_animate_iconify(self);
1200
1201         /* show it during the animation even if it is not "visible" */
1202         if (!self->visible)
1203             XMapWindow(ob_display, self->window);
1204     }
1205 }