Grab the server when mapping a client and frame, to avoid race conditions
[mikachu/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_cycle.h"
29 #include "focus_cycle_indicator.h"
30 #include "moveresize.h"
31 #include "screen.h"
32 #include "render/theme.h"
33
34 #define FRAME_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
35                          ButtonPressMask | ButtonReleaseMask | \
36                          SubstructureRedirectMask | FocusChangeMask)
37 #define ELEMENT_EVENTMASK (ButtonPressMask | ButtonReleaseMask | \
38                            ButtonMotionMask | PointerMotionMask | \
39                            EnterWindowMask | LeaveWindowMask)
40
41 #define FRAME_ANIMATE_ICONIFY_TIME 150000 /* .15 seconds */
42 #define FRAME_ANIMATE_ICONIFY_STEP_TIME (G_USEC_PER_SEC / 60) /* 60 Hz */
43
44 #define FRAME_HANDLE_Y(f) (f->size.top + f->client->area.height + f->cbwidth_b)
45
46 static void flash_done(gpointer data);
47 static gboolean flash_timeout(gpointer data);
48
49 static void layout_title(ObFrame *self);
50 static void set_theme_statics(ObFrame *self);
51 static void free_theme_statics(ObFrame *self);
52 static gboolean frame_animate_iconify(gpointer self);
53 static void frame_adjust_cursors(ObFrame *self);
54
55 static Window createWindow(Window parent, Visual *visual,
56                            gulong mask, XSetWindowAttributes *attrib)
57 {
58     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
59                          (visual ? 32 : RrDepth(ob_rr_inst)), InputOutput,
60                          (visual ? visual : RrVisual(ob_rr_inst)),
61                          mask, attrib);
62
63 }
64
65 static Visual *check_32bit_client(ObClient *c)
66 {
67     XWindowAttributes wattrib;
68     Status ret;
69
70     /* we're already running at 32 bit depth, yay. we don't need to use their
71        visual */
72     if (RrDepth(ob_rr_inst) == 32)
73         return NULL;
74
75     ret = XGetWindowAttributes(ob_display, c->window, &wattrib);
76     g_assert(ret != BadDrawable);
77     g_assert(ret != BadWindow);
78
79     if (wattrib.depth == 32)
80         return wattrib.visual;
81     return NULL;
82 }
83
84 ObFrame *frame_new(ObClient *client)
85 {
86     XSetWindowAttributes attrib;
87     gulong mask;
88     ObFrame *self;
89     Visual *visual;
90
91     self = g_new0(ObFrame, 1);
92     self->client = client;
93
94     visual = check_32bit_client(client);
95
96     /* create the non-visible decor windows */
97
98     mask = 0;
99     if (visual) {
100         /* client has a 32-bit visual */
101         mask |= CWColormap | CWBackPixel | CWBorderPixel;
102         /* create a colormap with the visual */
103         self->colormap = attrib.colormap =
104             XCreateColormap(ob_display,
105                             RootWindow(ob_display, ob_screen),
106                             visual, AllocNone);
107         attrib.background_pixel = BlackPixel(ob_display, ob_screen);
108         attrib.border_pixel = BlackPixel(ob_display, ob_screen);
109     }
110     self->window = createWindow(RootWindow(ob_display, ob_screen), visual,
111                                 mask, &attrib);
112
113     /* create the visible decor windows */
114
115     mask = 0;
116     if (visual) {
117         /* client has a 32-bit visual */
118         mask |= CWColormap | CWBackPixel | CWBorderPixel;
119         attrib.colormap = RrColormap(ob_rr_inst);
120     }
121
122     self->backback = createWindow(self->window, NULL, mask, &attrib);
123     self->backfront = createWindow(self->backback, NULL, mask, &attrib);
124
125     mask |= CWEventMask;
126     attrib.event_mask = ELEMENT_EVENTMASK;
127     self->innerleft = createWindow(self->window, NULL, mask, &attrib);
128     self->innertop = createWindow(self->window, NULL, mask, &attrib);
129     self->innerright = createWindow(self->window, NULL, mask, &attrib);
130     self->innerbottom = createWindow(self->window, NULL, mask, &attrib);
131
132     self->title = createWindow(self->window, NULL, mask, &attrib);
133     self->titleleft = createWindow(self->window, NULL, mask, &attrib);
134     self->titletop = createWindow(self->window, NULL, mask, &attrib);
135     self->titletopleft = createWindow(self->window, NULL, mask, &attrib);
136     self->titletopright = createWindow(self->window, NULL, mask, &attrib);
137     self->titleright = createWindow(self->window, NULL, mask, &attrib);
138     self->titlebottom = createWindow(self->window, NULL, mask, &attrib);
139
140     self->topresize = createWindow(self->title, NULL, mask, &attrib);
141     self->tltresize = createWindow(self->title, NULL, mask, &attrib);
142     self->tllresize = createWindow(self->title, NULL, mask, &attrib);
143     self->trtresize = createWindow(self->title, NULL, mask, &attrib);
144     self->trrresize = createWindow(self->title, NULL, mask, &attrib);
145
146     self->left = createWindow(self->window, NULL, mask, &attrib);
147     self->right = createWindow(self->window, NULL, mask, &attrib);
148
149     self->label = createWindow(self->title, NULL, mask, &attrib);
150     self->max = createWindow(self->title, NULL, mask, &attrib);
151     self->close = createWindow(self->title, NULL, mask, &attrib);
152     self->desk = createWindow(self->title, NULL, mask, &attrib);
153     self->shade = createWindow(self->title, NULL, mask, &attrib);
154     self->icon = createWindow(self->title, NULL, mask, &attrib);
155     self->iconify = createWindow(self->title, NULL, mask, &attrib);
156
157     self->handle = createWindow(self->window, NULL, mask, &attrib);
158     self->lgrip = createWindow(self->handle, NULL, mask, &attrib);
159     self->rgrip = createWindow(self->handle, NULL, mask, &attrib);
160
161     self->handleleft = createWindow(self->handle, NULL, mask, &attrib);
162     self->handleright = createWindow(self->handle, NULL, mask, &attrib);
163
164     self->handletop = createWindow(self->window, NULL, mask, &attrib);
165     self->handlebottom = createWindow(self->window, NULL, mask, &attrib);
166     self->lgripleft = createWindow(self->window, NULL, mask, &attrib);
167     self->lgriptop = createWindow(self->window, NULL, mask, &attrib);
168     self->lgripbottom = createWindow(self->window, NULL, mask, &attrib);
169     self->rgripright = createWindow(self->window, NULL, mask, &attrib);
170     self->rgriptop = createWindow(self->window, NULL, mask, &attrib);
171     self->rgripbottom = createWindow(self->window, NULL, mask, &attrib);
172
173     self->focused = FALSE;
174
175     /* the other stuff is shown based on decor settings */
176     XMapWindow(ob_display, self->label);
177     XMapWindow(ob_display, self->backback);
178     XMapWindow(ob_display, self->backfront);
179
180     self->max_press = self->close_press = self->desk_press =
181         self->iconify_press = self->shade_press = FALSE;
182     self->max_hover = self->close_hover = self->desk_hover =
183         self->iconify_hover = self->shade_hover = FALSE;
184
185     set_theme_statics(self);
186
187     return (ObFrame*)self;
188 }
189
190 static void set_theme_statics(ObFrame *self)
191 {
192     /* set colors/appearance/sizes for stuff that doesn't change */
193     XResizeWindow(ob_display, self->max,
194                   ob_rr_theme->button_size, ob_rr_theme->button_size);
195     XResizeWindow(ob_display, self->iconify,
196                   ob_rr_theme->button_size, ob_rr_theme->button_size);
197     XResizeWindow(ob_display, self->icon,
198                   ob_rr_theme->button_size + 2, ob_rr_theme->button_size + 2);
199     XResizeWindow(ob_display, self->close,
200                   ob_rr_theme->button_size, ob_rr_theme->button_size);
201     XResizeWindow(ob_display, self->desk,
202                   ob_rr_theme->button_size, ob_rr_theme->button_size);
203     XResizeWindow(ob_display, self->shade,
204                   ob_rr_theme->button_size, ob_rr_theme->button_size);
205     XResizeWindow(ob_display, self->tltresize,
206                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
207     XResizeWindow(ob_display, self->trtresize,
208                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
209     XResizeWindow(ob_display, self->tllresize,
210                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
211     XResizeWindow(ob_display, self->trrresize,
212                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
213
214     /* set up the dynamic appearances */
215     self->a_unfocused_title = RrAppearanceCopy(ob_rr_theme->a_unfocused_title);
216     self->a_focused_title = RrAppearanceCopy(ob_rr_theme->a_focused_title);
217     self->a_unfocused_label = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
218     self->a_focused_label = RrAppearanceCopy(ob_rr_theme->a_focused_label);
219     self->a_unfocused_handle =
220         RrAppearanceCopy(ob_rr_theme->a_unfocused_handle);
221     self->a_focused_handle = RrAppearanceCopy(ob_rr_theme->a_focused_handle);
222     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_icon);
223 }
224
225 static void free_theme_statics(ObFrame *self)
226 {
227     RrAppearanceFree(self->a_unfocused_title);
228     RrAppearanceFree(self->a_focused_title);
229     RrAppearanceFree(self->a_unfocused_label);
230     RrAppearanceFree(self->a_focused_label);
231     RrAppearanceFree(self->a_unfocused_handle);
232     RrAppearanceFree(self->a_focused_handle);
233     RrAppearanceFree(self->a_icon);
234 }
235
236 void frame_free(ObFrame *self)
237 {
238     free_theme_statics(self);
239
240     XDestroyWindow(ob_display, self->window);
241     if (self->colormap)
242         XFreeColormap(ob_display, self->colormap);
243
244     g_free(self);
245 }
246
247 void frame_show(ObFrame *self)
248 {
249     if (!self->visible) {
250         self->visible = TRUE;
251         framerender_frame(self);
252         /* Grab the server to make sure that the frame window is mapped before
253            the client gets its MapNotify, i.e. to make sure the client is
254            _visible_ when it gets MapNotify. */
255         grab_server(TRUE);
256         XMapWindow(ob_display, self->client->window);
257         XMapWindow(ob_display, self->window);
258         grab_server(FALSE);
259     }
260 }
261
262 void frame_hide(ObFrame *self)
263 {
264     if (self->visible) {
265         self->visible = FALSE;
266         if (!frame_iconify_animating(self))
267             XUnmapWindow(ob_display, self->window);
268         /* we unmap the client itself so that we can get MapRequest
269            events, and because the ICCCM tells us to! */
270         XUnmapWindow(ob_display, self->client->window);
271         self->client->ignore_unmaps += 1;
272     }
273 }
274
275 void frame_adjust_theme(ObFrame *self)
276 {
277     free_theme_statics(self);
278     set_theme_statics(self);
279 }
280
281 void frame_adjust_shape(ObFrame *self)
282 {
283 #ifdef SHAPE
284     gint num;
285     XRectangle xrect[2];
286
287     if (!self->client->shaped) {
288         /* clear the shape on the frame window */
289         XShapeCombineMask(ob_display, self->window, ShapeBounding,
290                           self->size.left,
291                           self->size.top,
292                           None, ShapeSet);
293     } else {
294         /* make the frame's shape match the clients */
295         XShapeCombineShape(ob_display, self->window, ShapeBounding,
296                            self->size.left,
297                            self->size.top,
298                            self->client->window,
299                            ShapeBounding, ShapeSet);
300
301         num = 0;
302         if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
303             xrect[0].x = 0;
304             xrect[0].y = 0;
305             xrect[0].width = self->area.width;
306             xrect[0].height = self->size.top;
307             ++num;
308         }
309
310         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
311             ob_rr_theme->handle_height > 0)
312         {
313             xrect[1].x = 0;
314             xrect[1].y = FRAME_HANDLE_Y(self);
315             xrect[1].width = self->area.width;
316             xrect[1].height = ob_rr_theme->handle_height +
317                 self->bwidth * 2;
318             ++num;
319         }
320
321         XShapeCombineRectangles(ob_display, self->window,
322                                 ShapeBounding, 0, 0, xrect, num,
323                                 ShapeUnion, Unsorted);
324     }
325 #endif
326 }
327
328 void frame_adjust_area(ObFrame *self, gboolean moved,
329                        gboolean resized, gboolean fake)
330 {
331     Strut oldsize;
332
333     oldsize = self->size;
334
335     if (resized) {
336         /* do this before changing the frame's status like max_horz max_vert */
337         frame_adjust_cursors(self);
338
339         self->functions = self->client->functions;
340         self->decorations = self->client->decorations;
341         self->max_horz = self->client->max_horz;
342         self->max_vert = self->client->max_vert;
343         self->shaded = self->client->shaded;
344
345         if (self->decorations & OB_FRAME_DECOR_BORDER ||
346             (self->client->undecorated && config_theme_keepborder))
347             self->bwidth = ob_rr_theme->fbwidth;
348         else
349             self->bwidth = 0;
350
351         if (self->decorations & OB_FRAME_DECOR_BORDER) {
352             self->cbwidth_l = self->cbwidth_r = ob_rr_theme->cbwidthx;
353             self->cbwidth_t = self->cbwidth_b = ob_rr_theme->cbwidthy;
354         } else
355             self->cbwidth_l = self->cbwidth_t =
356                 self->cbwidth_r = self->cbwidth_b = 0;
357
358         if (self->max_horz) {
359             self->cbwidth_l = self->cbwidth_r = 0;
360             self->width = self->client->area.width;
361             if (self->max_vert)
362                 self->cbwidth_b = 0;
363         } else
364             self->width = self->client->area.width +
365                 self->cbwidth_l + self->cbwidth_r;
366
367         /* some elements are sized based of the width, so don't let them have
368            negative values */
369         self->width = MAX(self->width,
370                           (ob_rr_theme->grip_width + self->bwidth) * 2 + 1);
371
372         STRUT_SET(self->size,
373                   self->cbwidth_l + (!self->max_horz ? self->bwidth : 0),
374                   self->cbwidth_t + self->bwidth,
375                   self->cbwidth_r + (!self->max_horz ? self->bwidth : 0),
376                   self->cbwidth_b +
377                     (!self->max_horz || !self->max_vert ? self->bwidth : 0));
378
379         if (self->decorations & OB_FRAME_DECOR_TITLEBAR)
380             self->size.top += ob_rr_theme->title_height + self->bwidth;
381         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
382             ob_rr_theme->handle_height > 0)
383         {
384             self->size.bottom += ob_rr_theme->handle_height + self->bwidth;
385         }
386
387         /* position/size and map/unmap all the windows */
388
389         if (!fake) {
390             if (self->cbwidth_l) {
391                 XMoveResizeWindow(ob_display, self->innerleft,
392                                   self->size.left - self->cbwidth_l,
393                                   self->size.top,
394                                   self->cbwidth_l, self->client->area.height);
395
396                 XMapWindow(ob_display, self->innerleft);
397             } else
398                 XUnmapWindow(ob_display, self->innerleft);
399
400             if (self->cbwidth_r) {
401                 XMoveResizeWindow(ob_display, self->innerright,
402                                   self->size.left + self->client->area.width,
403                                   self->size.top,
404                                   self->cbwidth_r, self->client->area.height);
405
406                 XMapWindow(ob_display, self->innerright);
407             } else
408                 XUnmapWindow(ob_display, self->innerright);
409
410             if (self->cbwidth_t) {
411                 XMoveResizeWindow(ob_display, self->innertop,
412                                   self->size.left - self->cbwidth_l,
413                                   self->size.top - self->cbwidth_t,
414                                   self->client->area.width +
415                                   self->cbwidth_l + self->cbwidth_r,
416                                   self->cbwidth_t);
417
418                 XMapWindow(ob_display, self->innertop);
419             } else
420                 XUnmapWindow(ob_display, self->innertop);
421
422             if (self->cbwidth_b) {
423                 XMoveResizeWindow(ob_display, self->innerbottom,
424                                   self->size.left - self->cbwidth_l,
425                                   self->size.top + self->client->area.height,
426                                   self->client->area.width +
427                                   self->cbwidth_l + self->cbwidth_r,
428                                   self->cbwidth_b);
429
430                 XMapWindow(ob_display, self->innerbottom);
431             } else
432                 XUnmapWindow(ob_display, self->innerbottom);
433
434             if (self->bwidth) {
435                 gint titlesides;
436
437                 /* height of titleleft and titleright */
438                 titlesides = (!self->max_horz ? ob_rr_theme->grip_width : 0);
439
440                 XMoveResizeWindow(ob_display, self->titletop,
441                                   ob_rr_theme->grip_width + self->bwidth, 0,
442                                   /* width + bwidth*2 - bwidth*2 - grips*2 */
443                                   self->width - ob_rr_theme->grip_width * 2,
444                                   self->bwidth);
445                 XMoveResizeWindow(ob_display, self->titletopleft,
446                                   0, 0,
447                                   ob_rr_theme->grip_width + self->bwidth,
448                                   self->bwidth);
449                 XMoveResizeWindow(ob_display, self->titletopright,
450                                   self->client->area.width +
451                                   self->size.left + self->size.right -
452                                   ob_rr_theme->grip_width - self->bwidth,
453                                   0,
454                                   ob_rr_theme->grip_width + self->bwidth,
455                                   self->bwidth);
456
457                 if (titlesides > 0) {
458                     XMoveResizeWindow(ob_display, self->titleleft,
459                                       0, self->bwidth,
460                                       self->bwidth,
461                                       titlesides);
462                     XMoveResizeWindow(ob_display, self->titleright,
463                                       self->client->area.width +
464                                       self->size.left + self->size.right -
465                                       self->bwidth,
466                                       self->bwidth,
467                                       self->bwidth,
468                                       titlesides);
469
470                     XMapWindow(ob_display, self->titleleft);
471                     XMapWindow(ob_display, self->titleright);
472                 } else {
473                     XUnmapWindow(ob_display, self->titleleft);
474                     XUnmapWindow(ob_display, self->titleright);
475                 }
476
477                 XMapWindow(ob_display, self->titletop);
478                 XMapWindow(ob_display, self->titletopleft);
479                 XMapWindow(ob_display, self->titletopright);
480
481                 if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
482                     XMoveResizeWindow(ob_display, self->titlebottom,
483                                       (self->max_horz ? 0 : self->bwidth),
484                                       ob_rr_theme->title_height + self->bwidth,
485                                       self->width,
486                                       self->bwidth);
487
488                     XMapWindow(ob_display, self->titlebottom);
489                 } else
490                     XUnmapWindow(ob_display, self->titlebottom);
491             } else {
492                 XUnmapWindow(ob_display, self->titlebottom);
493
494                 XUnmapWindow(ob_display, self->titletop);
495                 XUnmapWindow(ob_display, self->titletopleft);
496                 XUnmapWindow(ob_display, self->titletopright);
497                 XUnmapWindow(ob_display, self->titleleft);
498                 XUnmapWindow(ob_display, self->titleright);
499             }
500
501             if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
502                 XMoveResizeWindow(ob_display, self->title,
503                                   (self->max_horz ? 0 : self->bwidth),
504                                   self->bwidth,
505                                   self->width, ob_rr_theme->title_height);
506
507                 XMapWindow(ob_display, self->title);
508
509                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
510                     XMoveResizeWindow(ob_display, self->topresize,
511                                       ob_rr_theme->grip_width,
512                                       0,
513                                       self->width - ob_rr_theme->grip_width *2,
514                                       ob_rr_theme->paddingy + 1);
515
516                     XMoveWindow(ob_display, self->tltresize, 0, 0);
517                     XMoveWindow(ob_display, self->tllresize, 0, 0);
518                     XMoveWindow(ob_display, self->trtresize,
519                                 self->width - ob_rr_theme->grip_width, 0);
520                     XMoveWindow(ob_display, self->trrresize,
521                                 self->width - ob_rr_theme->paddingx - 1, 0);
522
523                     XMapWindow(ob_display, self->topresize);
524                     XMapWindow(ob_display, self->tltresize);
525                     XMapWindow(ob_display, self->tllresize);
526                     XMapWindow(ob_display, self->trtresize);
527                     XMapWindow(ob_display, self->trrresize);
528                 } else {
529                     XUnmapWindow(ob_display, self->topresize);
530                     XUnmapWindow(ob_display, self->tltresize);
531                     XUnmapWindow(ob_display, self->tllresize);
532                     XUnmapWindow(ob_display, self->trtresize);
533                     XUnmapWindow(ob_display, self->trrresize);
534                 }
535             } else
536                 XUnmapWindow(ob_display, self->title);
537         }
538
539         if ((self->decorations & OB_FRAME_DECOR_TITLEBAR))
540             /* layout the title bar elements */
541             layout_title(self);
542
543         if (!fake) {
544             gint sidebwidth = self->max_horz ? 0 : self->bwidth;
545
546             if (self->bwidth && self->size.bottom) {
547                 XMoveResizeWindow(ob_display, self->handlebottom,
548                                   ob_rr_theme->grip_width +
549                                   self->bwidth + sidebwidth,
550                                   self->size.top + self->client->area.height +
551                                   self->size.bottom - self->bwidth,
552                                   self->width - (ob_rr_theme->grip_width +
553                                                  sidebwidth) * 2,
554                                   self->bwidth);
555
556
557                 if (sidebwidth) {
558                     XMoveResizeWindow(ob_display, self->lgripleft,
559                                       0,
560                                       self->size.top +
561                                       self->client->area.height +
562                                       self->size.bottom -
563                                       (!self->max_horz ?
564                                        ob_rr_theme->grip_width :
565                                        self->size.bottom - self->cbwidth_b),
566                                       self->bwidth,
567                                       (!self->max_horz ?
568                                        ob_rr_theme->grip_width :
569                                        self->size.bottom - self->cbwidth_b));
570                     XMoveResizeWindow(ob_display, self->rgripright,
571                                   self->size.left +
572                                       self->client->area.width +
573                                       self->size.right - self->bwidth,
574                                       self->size.top +
575                                       self->client->area.height +
576                                       self->size.bottom -
577                                       (!self->max_horz ?
578                                        ob_rr_theme->grip_width :
579                                        self->size.bottom - self->cbwidth_b),
580                                       self->bwidth,
581                                       (!self->max_horz ?
582                                        ob_rr_theme->grip_width :
583                                        self->size.bottom - self->cbwidth_b));
584
585                     XMapWindow(ob_display, self->lgripleft);
586                     XMapWindow(ob_display, self->rgripright);
587                 } else {
588                     XUnmapWindow(ob_display, self->lgripleft);
589                     XUnmapWindow(ob_display, self->rgripright);
590                 }
591
592                 XMoveResizeWindow(ob_display, self->lgripbottom,
593                                   sidebwidth,
594                                   self->size.top + self->client->area.height +
595                                   self->size.bottom - self->bwidth,
596                                   ob_rr_theme->grip_width + self->bwidth,
597                                   self->bwidth);
598                 XMoveResizeWindow(ob_display, self->rgripbottom,
599                                   self->size.left + self->client->area.width +
600                                   self->size.right - self->bwidth - sidebwidth -
601                                   ob_rr_theme->grip_width,
602                                   self->size.top + self->client->area.height +
603                                   self->size.bottom - self->bwidth,
604                                   ob_rr_theme->grip_width + self->bwidth,
605                                   self->bwidth);
606
607                 XMapWindow(ob_display, self->handlebottom);
608                 XMapWindow(ob_display, self->lgripbottom);
609                 XMapWindow(ob_display, self->rgripbottom);
610
611                 if (self->decorations & OB_FRAME_DECOR_HANDLE &&
612                     ob_rr_theme->handle_height > 0)
613                 {
614                     XMoveResizeWindow(ob_display, self->handletop,
615                                       ob_rr_theme->grip_width +
616                                       self->bwidth + sidebwidth,
617                                       FRAME_HANDLE_Y(self),
618                                       self->width - (ob_rr_theme->grip_width +
619                                                      sidebwidth) * 2,
620                                       self->bwidth);
621                     XMapWindow(ob_display, self->handletop);
622
623                     if (self->decorations & OB_FRAME_DECOR_GRIPS) {
624                         XMoveResizeWindow(ob_display, self->handleleft,
625                                           ob_rr_theme->grip_width,
626                                           0,
627                                           self->bwidth,
628                                           ob_rr_theme->handle_height);
629                         XMoveResizeWindow(ob_display, self->handleright,
630                                           self->width -
631                                           ob_rr_theme->grip_width -
632                                           self->bwidth,
633                                           0,
634                                           self->bwidth,
635                                           ob_rr_theme->handle_height);
636
637                         XMoveResizeWindow(ob_display, self->lgriptop,
638                                           sidebwidth,
639                                           FRAME_HANDLE_Y(self),
640                                           ob_rr_theme->grip_width +
641                                           self->bwidth,
642                                           self->bwidth);
643                         XMoveResizeWindow(ob_display, self->rgriptop,
644                                           self->size.left +
645                                           self->client->area.width +
646                                           self->size.right - self->bwidth -
647                                           sidebwidth - ob_rr_theme->grip_width,
648                                           FRAME_HANDLE_Y(self),
649                                           ob_rr_theme->grip_width +
650                                           self->bwidth,
651                                           self->bwidth);
652
653                         XMapWindow(ob_display, self->handleleft);
654                         XMapWindow(ob_display, self->handleright);
655                         XMapWindow(ob_display, self->lgriptop);
656                         XMapWindow(ob_display, self->rgriptop);
657                     } else {
658                         XUnmapWindow(ob_display, self->handleleft);
659                         XUnmapWindow(ob_display, self->handleright);
660                         XUnmapWindow(ob_display, self->lgriptop);
661                         XUnmapWindow(ob_display, self->rgriptop);
662                     }
663                 } else {
664                     XUnmapWindow(ob_display, self->handleleft);
665                     XUnmapWindow(ob_display, self->handleright);
666                     XUnmapWindow(ob_display, self->lgriptop);
667                     XUnmapWindow(ob_display, self->rgriptop);
668
669                     XUnmapWindow(ob_display, self->handletop);
670                 }
671             } else {
672                 XUnmapWindow(ob_display, self->handleleft);
673                 XUnmapWindow(ob_display, self->handleright);
674                 XUnmapWindow(ob_display, self->lgriptop);
675                 XUnmapWindow(ob_display, self->rgriptop);
676
677                 XUnmapWindow(ob_display, self->handletop);
678
679                 XUnmapWindow(ob_display, self->handlebottom);
680                 XUnmapWindow(ob_display, self->lgripleft);
681                 XUnmapWindow(ob_display, self->rgripright);
682                 XUnmapWindow(ob_display, self->lgripbottom);
683                 XUnmapWindow(ob_display, self->rgripbottom);
684             }
685
686             if (self->decorations & OB_FRAME_DECOR_HANDLE &&
687                 ob_rr_theme->handle_height > 0)
688             {
689                 XMoveResizeWindow(ob_display, self->handle,
690                                   sidebwidth,
691                                   FRAME_HANDLE_Y(self) + self->bwidth,
692                                   self->width, ob_rr_theme->handle_height);
693                 XMapWindow(ob_display, self->handle);
694
695                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
696                     XMoveResizeWindow(ob_display, self->lgrip,
697                                       0, 0,
698                                       ob_rr_theme->grip_width,
699                                       ob_rr_theme->handle_height);
700                     XMoveResizeWindow(ob_display, self->rgrip,
701                                       self->width - ob_rr_theme->grip_width,
702                                       0,
703                                       ob_rr_theme->grip_width,
704                                       ob_rr_theme->handle_height);
705
706                     XMapWindow(ob_display, self->lgrip);
707                     XMapWindow(ob_display, self->rgrip);
708                 } else {
709                     XUnmapWindow(ob_display, self->lgrip);
710                     XUnmapWindow(ob_display, self->rgrip);
711                 }
712             } else {
713                 XUnmapWindow(ob_display, self->lgrip);
714                 XUnmapWindow(ob_display, self->rgrip);
715
716                 XUnmapWindow(ob_display, self->handle);
717             }
718
719             if (self->bwidth && !self->max_horz) {
720                 XMoveResizeWindow(ob_display, self->left,
721                                   0,
722                                   self->bwidth + ob_rr_theme->grip_width,
723                                   self->bwidth,
724                                   self->client->area.height +
725                                   self->size.top + self->size.bottom -
726                                   ob_rr_theme->grip_width * 2);
727
728                 XMapWindow(ob_display, self->left);
729             } else
730                 XUnmapWindow(ob_display, self->left);
731
732             if (self->bwidth && !self->max_horz) {
733                 XMoveResizeWindow(ob_display, self->right,
734                                   self->client->area.width + self->cbwidth_l +
735                                   self->cbwidth_r + self->bwidth,
736                                   self->bwidth + ob_rr_theme->grip_width,
737                                   self->bwidth,
738                                   self->client->area.height +
739                                   self->size.top + self->size.bottom -
740                                   ob_rr_theme->grip_width * 2);
741
742                 XMapWindow(ob_display, self->right);
743             } else
744                 XUnmapWindow(ob_display, self->right);
745
746             XMoveResizeWindow(ob_display, self->backback,
747                               self->size.left, self->size.top,
748                               self->client->area.width,
749                               self->client->area.height);
750         }
751     }
752
753     /* shading can change without being moved or resized */
754     RECT_SET_SIZE(self->area,
755                   self->client->area.width +
756                   self->size.left + self->size.right,
757                   (self->client->shaded ?
758                    ob_rr_theme->title_height + self->bwidth * 2:
759                    self->client->area.height +
760                    self->size.top + self->size.bottom));
761
762     if ((moved || resized) && !fake) {
763         /* find the new coordinates, done after setting the frame.size, for
764            frame_client_gravity. */
765         self->area.x = self->client->area.x;
766         self->area.y = self->client->area.y;
767         frame_client_gravity(self, &self->area.x, &self->area.y);
768     }
769
770     if (!fake) {
771         if (!frame_iconify_animating(self))
772             /* move and resize the top level frame.
773                shading can change without being moved or resized.
774
775                but don't do this during an iconify animation. it will be
776                reflected afterwards.
777             */
778             XMoveResizeWindow(ob_display, self->window,
779                               self->area.x,
780                               self->area.y,
781                               self->area.width,
782                               self->area.height);
783
784         /* when the client has StaticGravity, it likes to move around.
785            also this correctly positions the client when it maps.
786            this also needs to be run when the frame's decorations sizes change!
787         */
788         XMoveWindow(ob_display, self->client->window,
789                     self->size.left, self->size.top);
790
791         if (resized) {
792             self->need_render = TRUE;
793             framerender_frame(self);
794             frame_adjust_shape(self);
795         }
796
797         if (!STRUT_EQUAL(self->size, oldsize)) {
798             gulong vals[4];
799             vals[0] = self->size.left;
800             vals[1] = self->size.right;
801             vals[2] = self->size.top;
802             vals[3] = self->size.bottom;
803             PROP_SETA32(self->client->window, net_frame_extents,
804                         cardinal, vals, 4);
805             PROP_SETA32(self->client->window, kde_net_wm_frame_strut,
806                         cardinal, vals, 4);
807         }
808
809         /* if this occurs while we are focus cycling, the indicator needs to
810            match the changes */
811         if (focus_cycle_target == self->client)
812             focus_cycle_draw_indicator(self->client);
813     }
814     if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR))
815         XResizeWindow(ob_display, self->label, self->label_width,
816                       ob_rr_theme->label_height);
817
818 }
819
820 static void frame_adjust_cursors(ObFrame *self)
821 {
822     if ((self->functions & OB_CLIENT_FUNC_RESIZE) !=
823         (self->client->functions & OB_CLIENT_FUNC_RESIZE) ||
824         self->max_horz != self->client->max_horz ||
825         self->max_vert != self->client->max_vert ||
826         self->shaded != self->client->shaded)
827     {
828         gboolean r = (self->client->functions & OB_CLIENT_FUNC_RESIZE) &&
829             !(self->client->max_horz && self->client->max_vert);
830         gboolean topbot = !self->client->max_vert;
831         gboolean sh = self->client->shaded;
832         XSetWindowAttributes a;
833
834         /* these ones turn off when max vert, and some when shaded */
835         a.cursor = ob_cursor(r && topbot && !sh ?
836                              OB_CURSOR_NORTH : OB_CURSOR_NONE);
837         XChangeWindowAttributes(ob_display, self->topresize, CWCursor, &a);
838         XChangeWindowAttributes(ob_display, self->titletop, CWCursor, &a);
839         a.cursor = ob_cursor(r && topbot ? OB_CURSOR_SOUTH : OB_CURSOR_NONE);
840         XChangeWindowAttributes(ob_display, self->handle, CWCursor, &a);
841         XChangeWindowAttributes(ob_display, self->handletop, CWCursor, &a);
842         XChangeWindowAttributes(ob_display, self->handlebottom, CWCursor, &a);
843         XChangeWindowAttributes(ob_display, self->innerbottom, CWCursor, &a);
844
845         /* these ones change when shaded */
846         a.cursor = ob_cursor(r ? (sh ? OB_CURSOR_WEST : OB_CURSOR_NORTHWEST) :
847                              OB_CURSOR_NONE);
848         XChangeWindowAttributes(ob_display, self->titleleft, CWCursor, &a);
849         XChangeWindowAttributes(ob_display, self->tltresize, CWCursor, &a);
850         XChangeWindowAttributes(ob_display, self->tllresize, CWCursor, &a);
851         XChangeWindowAttributes(ob_display, self->titletopleft, CWCursor, &a);
852         a.cursor = ob_cursor(r ? (sh ? OB_CURSOR_EAST : OB_CURSOR_NORTHEAST) :
853                              OB_CURSOR_NONE);
854         XChangeWindowAttributes(ob_display, self->titleright, CWCursor, &a);
855         XChangeWindowAttributes(ob_display, self->trtresize, CWCursor, &a);
856         XChangeWindowAttributes(ob_display, self->trrresize, CWCursor, &a);
857         XChangeWindowAttributes(ob_display, self->titletopright, CWCursor, &a);
858
859         /* these ones are pretty static */
860         a.cursor = ob_cursor(r ? OB_CURSOR_WEST : OB_CURSOR_NONE);
861         XChangeWindowAttributes(ob_display, self->left, CWCursor, &a);
862         XChangeWindowAttributes(ob_display, self->innerleft, CWCursor, &a);
863         a.cursor = ob_cursor(r ? OB_CURSOR_EAST : OB_CURSOR_NONE);
864         XChangeWindowAttributes(ob_display, self->right, CWCursor, &a);
865         XChangeWindowAttributes(ob_display, self->innerright, CWCursor, &a);
866         a.cursor = ob_cursor(r ? OB_CURSOR_SOUTHWEST : OB_CURSOR_NONE);
867         XChangeWindowAttributes(ob_display, self->lgrip, CWCursor, &a);
868         XChangeWindowAttributes(ob_display, self->handleleft, CWCursor, &a);
869         XChangeWindowAttributes(ob_display, self->lgripleft, CWCursor, &a);
870         XChangeWindowAttributes(ob_display, self->lgriptop, CWCursor, &a);
871         XChangeWindowAttributes(ob_display, self->lgripbottom, CWCursor, &a);
872         a.cursor = ob_cursor(r ? OB_CURSOR_SOUTHEAST : OB_CURSOR_NONE);
873         XChangeWindowAttributes(ob_display, self->rgrip, CWCursor, &a);
874         XChangeWindowAttributes(ob_display, self->handleright, CWCursor, &a);
875         XChangeWindowAttributes(ob_display, self->rgripright, CWCursor, &a);
876         XChangeWindowAttributes(ob_display, self->rgriptop, CWCursor, &a);
877         XChangeWindowAttributes(ob_display, self->rgripbottom, CWCursor, &a);
878     }
879 }
880
881 void frame_adjust_client_area(ObFrame *self)
882 {
883     /* adjust the window which is there to prevent flashing on unmap */
884     XMoveResizeWindow(ob_display, self->backfront, 0, 0,
885                       self->client->area.width,
886                       self->client->area.height);
887 }
888
889 void frame_adjust_state(ObFrame *self)
890 {
891     self->need_render = TRUE;
892     framerender_frame(self);
893 }
894
895 void frame_adjust_focus(ObFrame *self, gboolean hilite)
896 {
897     self->focused = hilite;
898     self->need_render = TRUE;
899     framerender_frame(self);
900     XFlush(ob_display);
901 }
902
903 void frame_adjust_title(ObFrame *self)
904 {
905     self->need_render = TRUE;
906     framerender_frame(self);
907 }
908
909 void frame_adjust_icon(ObFrame *self)
910 {
911     self->need_render = TRUE;
912     framerender_frame(self);
913 }
914
915 void frame_grab_client(ObFrame *self)
916 {
917     /* DO NOT map the client window here. we used to do that, but it is bogus.
918        we need to set up the client's dimensions and everything before we
919        send a mapnotify or we create race conditions.
920     */
921
922     /* reparent the client to the frame */
923     XReparentWindow(ob_display, self->client->window, self->window, 0, 0);
924
925     /*
926       When reparenting the client window, it is usually not mapped yet, since
927       this occurs from a MapRequest. However, in the case where Openbox is
928       starting up, the window is already mapped, so we'll see an unmap event
929       for it.
930     */
931     if (ob_state() == OB_STATE_STARTING)
932         ++self->client->ignore_unmaps;
933
934     /* select the event mask on the client's parent (to receive config/map
935        req's) the ButtonPress is to catch clicks on the client border */
936     XSelectInput(ob_display, self->window, FRAME_EVENTMASK);
937
938     /* set all the windows for the frame in the window_map */
939     g_hash_table_insert(window_map, &self->window, self->client);
940     g_hash_table_insert(window_map, &self->backback, self->client);
941     g_hash_table_insert(window_map, &self->backfront, self->client);
942     g_hash_table_insert(window_map, &self->innerleft, self->client);
943     g_hash_table_insert(window_map, &self->innertop, self->client);
944     g_hash_table_insert(window_map, &self->innerright, self->client);
945     g_hash_table_insert(window_map, &self->innerbottom, self->client);
946     g_hash_table_insert(window_map, &self->title, self->client);
947     g_hash_table_insert(window_map, &self->label, self->client);
948     g_hash_table_insert(window_map, &self->max, self->client);
949     g_hash_table_insert(window_map, &self->close, self->client);
950     g_hash_table_insert(window_map, &self->desk, self->client);
951     g_hash_table_insert(window_map, &self->shade, self->client);
952     g_hash_table_insert(window_map, &self->icon, self->client);
953     g_hash_table_insert(window_map, &self->iconify, self->client);
954     g_hash_table_insert(window_map, &self->handle, self->client);
955     g_hash_table_insert(window_map, &self->lgrip, self->client);
956     g_hash_table_insert(window_map, &self->rgrip, self->client);
957     g_hash_table_insert(window_map, &self->topresize, self->client);
958     g_hash_table_insert(window_map, &self->tltresize, self->client);
959     g_hash_table_insert(window_map, &self->tllresize, self->client);
960     g_hash_table_insert(window_map, &self->trtresize, self->client);
961     g_hash_table_insert(window_map, &self->trrresize, self->client);
962     g_hash_table_insert(window_map, &self->left, self->client);
963     g_hash_table_insert(window_map, &self->right, self->client);
964     g_hash_table_insert(window_map, &self->titleleft, self->client);
965     g_hash_table_insert(window_map, &self->titletop, self->client);
966     g_hash_table_insert(window_map, &self->titletopleft, self->client);
967     g_hash_table_insert(window_map, &self->titletopright, self->client);
968     g_hash_table_insert(window_map, &self->titleright, self->client);
969     g_hash_table_insert(window_map, &self->titlebottom, self->client);
970     g_hash_table_insert(window_map, &self->handleleft, self->client);
971     g_hash_table_insert(window_map, &self->handletop, self->client);
972     g_hash_table_insert(window_map, &self->handleright, self->client);
973     g_hash_table_insert(window_map, &self->handlebottom, self->client);
974     g_hash_table_insert(window_map, &self->lgripleft, self->client);
975     g_hash_table_insert(window_map, &self->lgriptop, self->client);
976     g_hash_table_insert(window_map, &self->lgripbottom, self->client);
977     g_hash_table_insert(window_map, &self->rgripright, self->client);
978     g_hash_table_insert(window_map, &self->rgriptop, self->client);
979     g_hash_table_insert(window_map, &self->rgripbottom, self->client);
980 }
981
982 void frame_release_client(ObFrame *self)
983 {
984     XEvent ev;
985     gboolean reparent = TRUE;
986
987     /* if there was any animation going on, kill it */
988     ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
989                                      self, FALSE);
990
991     /* check if the app has already reparented its window away */
992     while (XCheckTypedWindowEvent(ob_display, self->client->window,
993                                   ReparentNotify, &ev))
994     {
995         /* This check makes sure we don't catch our own reparent action to
996            our frame window. This doesn't count as the app reparenting itself
997            away of course.
998
999            Reparent events that are generated by us are just discarded here.
1000            They are of no consequence to us anyhow.
1001         */
1002         if (ev.xreparent.parent != self->window) {
1003             reparent = FALSE;
1004             XPutBackEvent(ob_display, &ev);
1005             break;
1006         }
1007     }
1008
1009     if (reparent) {
1010         /* according to the ICCCM - if the client doesn't reparent itself,
1011            then we will reparent the window to root for them */
1012         XReparentWindow(ob_display, self->client->window,
1013                         RootWindow(ob_display, ob_screen),
1014                         self->client->area.x,
1015                         self->client->area.y);
1016     }
1017
1018     /* remove all the windows for the frame from the window_map */
1019     g_hash_table_remove(window_map, &self->window);
1020     g_hash_table_remove(window_map, &self->backback);
1021     g_hash_table_remove(window_map, &self->backfront);
1022     g_hash_table_remove(window_map, &self->innerleft);
1023     g_hash_table_remove(window_map, &self->innertop);
1024     g_hash_table_remove(window_map, &self->innerright);
1025     g_hash_table_remove(window_map, &self->innerbottom);
1026     g_hash_table_remove(window_map, &self->title);
1027     g_hash_table_remove(window_map, &self->label);
1028     g_hash_table_remove(window_map, &self->max);
1029     g_hash_table_remove(window_map, &self->close);
1030     g_hash_table_remove(window_map, &self->desk);
1031     g_hash_table_remove(window_map, &self->shade);
1032     g_hash_table_remove(window_map, &self->icon);
1033     g_hash_table_remove(window_map, &self->iconify);
1034     g_hash_table_remove(window_map, &self->handle);
1035     g_hash_table_remove(window_map, &self->lgrip);
1036     g_hash_table_remove(window_map, &self->rgrip);
1037     g_hash_table_remove(window_map, &self->topresize);
1038     g_hash_table_remove(window_map, &self->tltresize);
1039     g_hash_table_remove(window_map, &self->tllresize);
1040     g_hash_table_remove(window_map, &self->trtresize);
1041     g_hash_table_remove(window_map, &self->trrresize);
1042     g_hash_table_remove(window_map, &self->left);
1043     g_hash_table_remove(window_map, &self->right);
1044     g_hash_table_remove(window_map, &self->titleleft);
1045     g_hash_table_remove(window_map, &self->titletop);
1046     g_hash_table_remove(window_map, &self->titletopleft);
1047     g_hash_table_remove(window_map, &self->titletopright);
1048     g_hash_table_remove(window_map, &self->titleright);
1049     g_hash_table_remove(window_map, &self->titlebottom);
1050     g_hash_table_remove(window_map, &self->handleleft);
1051     g_hash_table_remove(window_map, &self->handletop);
1052     g_hash_table_remove(window_map, &self->handleright);
1053     g_hash_table_remove(window_map, &self->handlebottom);
1054     g_hash_table_remove(window_map, &self->lgripleft);
1055     g_hash_table_remove(window_map, &self->lgriptop);
1056     g_hash_table_remove(window_map, &self->lgripbottom);
1057     g_hash_table_remove(window_map, &self->rgripright);
1058     g_hash_table_remove(window_map, &self->rgriptop);
1059     g_hash_table_remove(window_map, &self->rgripbottom);
1060
1061     ob_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
1062 }
1063
1064 /* is there anything present between us and the label? */
1065 static gboolean is_button_present(ObFrame *self, const gchar *lc, gint dir) {
1066     for (; *lc != '\0' && lc >= config_title_layout; lc += dir) {
1067         if (*lc == ' ') continue; /* it was invalid */
1068         if (*lc == 'N' && self->decorations & OB_FRAME_DECOR_ICON)
1069             return TRUE;
1070         if (*lc == 'D' && self->decorations & OB_FRAME_DECOR_ALLDESKTOPS)
1071             return TRUE;
1072         if (*lc == 'S' && self->decorations & OB_FRAME_DECOR_SHADE)
1073             return TRUE;
1074         if (*lc == 'I' && self->decorations & OB_FRAME_DECOR_ICONIFY)
1075             return TRUE;
1076         if (*lc == 'M' && self->decorations & OB_FRAME_DECOR_MAXIMIZE)
1077             return TRUE;
1078         if (*lc == 'C' && self->decorations & OB_FRAME_DECOR_CLOSE)
1079             return TRUE;
1080         if (*lc == 'L') return FALSE;
1081     }
1082     return FALSE;
1083 }
1084
1085 static void layout_title(ObFrame *self)
1086 {
1087     gchar *lc;
1088     gint i;
1089
1090     const gint bwidth = ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
1091     /* position of the left most button */
1092     const gint left = ob_rr_theme->paddingx + 1;
1093     /* position of the right most button */
1094     const gint right = self->width;
1095
1096     /* turn them all off */
1097     self->icon_on = self->desk_on = self->shade_on = self->iconify_on =
1098         self->max_on = self->close_on = self->label_on = FALSE;
1099     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
1100     self->leftmost = self->rightmost = OB_FRAME_CONTEXT_NONE;
1101
1102     /* figure out what's being show, find each element's position, and the
1103        width of the label
1104
1105        do the ones before the label, then after the label,
1106        i will be +1 the first time through when working to the left,
1107        and -1 the second time through when working to the right */
1108     for (i = 1; i >= -1; i-=2) {
1109         gint x;
1110         ObFrameContext *firstcon;
1111
1112         if (i > 0) {
1113             x = left;
1114             lc = config_title_layout;
1115             firstcon = &self->leftmost;
1116         } else {
1117             x = right;
1118             lc = config_title_layout + strlen(config_title_layout)-1;
1119             firstcon = &self->rightmost;
1120         }
1121
1122         /* stop at the end of the string (or the label, which calls break) */
1123         for (; *lc != '\0' && lc >= config_title_layout; lc+=i) {
1124             if (*lc == 'L') {
1125                 if (i > 0) {
1126                     self->label_on = TRUE;
1127                     self->label_x = x;
1128                 }
1129                 break; /* break the for loop, do other side of label */
1130             } else if (*lc == 'N') {
1131                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICON;
1132                 if ((self->icon_on = is_button_present(self, lc, i))) {
1133                     /* icon is bigger than buttons */
1134                     self->label_width -= bwidth + 2;
1135                     if (i > 0) self->icon_x = x;
1136                     x += i * (bwidth + 2);
1137                     if (i < 0) self->icon_x = x;
1138                 }
1139             } else if (*lc == 'D') {
1140                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ALLDESKTOPS;
1141                 if ((self->desk_on = is_button_present(self, lc, i))) {
1142                     self->label_width -= bwidth;
1143                     if (i > 0) self->desk_x = x;
1144                     x += i * bwidth;
1145                     if (i < 0) self->desk_x = x;
1146                 }
1147             } else if (*lc == 'S') {
1148                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_SHADE;
1149                 if ((self->shade_on = is_button_present(self, lc, i))) {
1150                     self->label_width -= bwidth;
1151                     if (i > 0) self->shade_x = x;
1152                     x += i * bwidth;
1153                     if (i < 0) self->shade_x = x;
1154                 }
1155             } else if (*lc == 'I') {
1156                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICONIFY;
1157                 if ((self->iconify_on = is_button_present(self, lc, i))) {
1158                     self->label_width -= bwidth;
1159                     if (i > 0) self->iconify_x = x;
1160                     x += i * bwidth;
1161                     if (i < 0) self->iconify_x = x;
1162                 }
1163             } else if (*lc == 'M') {
1164                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_MAXIMIZE;
1165                 if ((self->max_on = is_button_present(self, lc, i))) {
1166                     self->label_width -= bwidth;
1167                     if (i > 0) self->max_x = x;
1168                     x += i * bwidth;
1169                     if (i < 0) self->max_x = x;
1170                 }
1171             } else if (*lc == 'C') {
1172                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_CLOSE;
1173                 if ((self->close_on = is_button_present(self, lc, i))) {
1174                     self->label_width -= bwidth;
1175                     if (i > 0) self->close_x = x;
1176                     x += i * bwidth;
1177                     if (i < 0) self->close_x = x;
1178                 }
1179             } else
1180                 continue; /* don't set firstcon */
1181             firstcon = NULL;
1182         }
1183     }
1184
1185     /* position and map the elements */
1186     if (self->icon_on) {
1187         XMapWindow(ob_display, self->icon);
1188         XMoveWindow(ob_display, self->icon, self->icon_x,
1189                     ob_rr_theme->paddingy);
1190     } else
1191         XUnmapWindow(ob_display, self->icon);
1192
1193     if (self->desk_on) {
1194         XMapWindow(ob_display, self->desk);
1195         XMoveWindow(ob_display, self->desk, self->desk_x,
1196                     ob_rr_theme->paddingy + 1);
1197     } else
1198         XUnmapWindow(ob_display, self->desk);
1199
1200     if (self->shade_on) {
1201         XMapWindow(ob_display, self->shade);
1202         XMoveWindow(ob_display, self->shade, self->shade_x,
1203                     ob_rr_theme->paddingy + 1);
1204     } else
1205         XUnmapWindow(ob_display, self->shade);
1206
1207     if (self->iconify_on) {
1208         XMapWindow(ob_display, self->iconify);
1209         XMoveWindow(ob_display, self->iconify, self->iconify_x,
1210                     ob_rr_theme->paddingy + 1);
1211     } else
1212         XUnmapWindow(ob_display, self->iconify);
1213
1214     if (self->max_on) {
1215         XMapWindow(ob_display, self->max);
1216         XMoveWindow(ob_display, self->max, self->max_x,
1217                     ob_rr_theme->paddingy + 1);
1218     } else
1219         XUnmapWindow(ob_display, self->max);
1220
1221     if (self->close_on) {
1222         XMapWindow(ob_display, self->close);
1223         XMoveWindow(ob_display, self->close, self->close_x,
1224                     ob_rr_theme->paddingy + 1);
1225     } else
1226         XUnmapWindow(ob_display, self->close);
1227
1228     if (self->label_on) {
1229         self->label_width = MAX(1, self->label_width); /* no lower than 1 */
1230         XMapWindow(ob_display, self->label);
1231         XMoveWindow(ob_display, self->label, self->label_x,
1232                     ob_rr_theme->paddingy);
1233     } else
1234         XUnmapWindow(ob_display, self->label);
1235 }
1236
1237 ObFrameContext frame_context_from_string(const gchar *name)
1238 {
1239     if (!g_ascii_strcasecmp("Desktop", name))
1240         return OB_FRAME_CONTEXT_DESKTOP;
1241     else if (!g_ascii_strcasecmp("Root", name))
1242         return OB_FRAME_CONTEXT_ROOT;
1243     else if (!g_ascii_strcasecmp("Client", name))
1244         return OB_FRAME_CONTEXT_CLIENT;
1245     else if (!g_ascii_strcasecmp("Titlebar", name))
1246         return OB_FRAME_CONTEXT_TITLEBAR;
1247     else if (!g_ascii_strcasecmp("Frame", name))
1248         return OB_FRAME_CONTEXT_FRAME;
1249     else if (!g_ascii_strcasecmp("TLCorner", name))
1250         return OB_FRAME_CONTEXT_TLCORNER;
1251     else if (!g_ascii_strcasecmp("TRCorner", name))
1252         return OB_FRAME_CONTEXT_TRCORNER;
1253     else if (!g_ascii_strcasecmp("BLCorner", name))
1254         return OB_FRAME_CONTEXT_BLCORNER;
1255     else if (!g_ascii_strcasecmp("BRCorner", name))
1256         return OB_FRAME_CONTEXT_BRCORNER;
1257     else if (!g_ascii_strcasecmp("Top", name))
1258         return OB_FRAME_CONTEXT_TOP;
1259     else if (!g_ascii_strcasecmp("Bottom", name))
1260         return OB_FRAME_CONTEXT_BOTTOM;
1261     else if (!g_ascii_strcasecmp("Left", name))
1262         return OB_FRAME_CONTEXT_LEFT;
1263     else if (!g_ascii_strcasecmp("Right", name))
1264         return OB_FRAME_CONTEXT_RIGHT;
1265     else if (!g_ascii_strcasecmp("Maximize", name))
1266         return OB_FRAME_CONTEXT_MAXIMIZE;
1267     else if (!g_ascii_strcasecmp("AllDesktops", name))
1268         return OB_FRAME_CONTEXT_ALLDESKTOPS;
1269     else if (!g_ascii_strcasecmp("Shade", name))
1270         return OB_FRAME_CONTEXT_SHADE;
1271     else if (!g_ascii_strcasecmp("Iconify", name))
1272         return OB_FRAME_CONTEXT_ICONIFY;
1273     else if (!g_ascii_strcasecmp("Icon", name))
1274         return OB_FRAME_CONTEXT_ICON;
1275     else if (!g_ascii_strcasecmp("Close", name))
1276         return OB_FRAME_CONTEXT_CLOSE;
1277     else if (!g_ascii_strcasecmp("MoveResize", name))
1278         return OB_FRAME_CONTEXT_MOVE_RESIZE;
1279     return OB_FRAME_CONTEXT_NONE;
1280 }
1281
1282 ObFrameContext frame_context(ObClient *client, Window win, gint x, gint y)
1283 {
1284     ObFrame *self;
1285
1286     if (moveresize_in_progress)
1287         return OB_FRAME_CONTEXT_MOVE_RESIZE;
1288
1289     if (win == RootWindow(ob_display, ob_screen))
1290         return OB_FRAME_CONTEXT_ROOT ;
1291     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
1292     if (win == client->window) {
1293         /* conceptually, this is the desktop, as far as users are
1294            concerned */
1295         if (client->type == OB_CLIENT_TYPE_DESKTOP)
1296             return OB_FRAME_CONTEXT_DESKTOP;
1297         return OB_FRAME_CONTEXT_CLIENT;
1298     }
1299
1300     self = client->frame;
1301
1302     /* when the user clicks in the corners of the titlebar and the client
1303        is fully maximized, then treat it like they clicked in the
1304        button that is there */
1305     if (self->max_horz && self->max_vert &&
1306         (win == self->title || win == self->titletop ||
1307          win == self->titleleft || win == self->titletopleft ||
1308          win == self->titleright || win == self->titletopright))
1309     {
1310         /* get the mouse coords in reference to the whole frame */
1311         gint fx = x;
1312         gint fy = y;
1313
1314         /* these windows are down a border width from the top of the frame */
1315         if (win == self->title ||
1316             win == self->titleleft || win == self->titleright)
1317             fy += self->bwidth;
1318
1319         /* title is a border width in from the edge */
1320         if (win == self->title)
1321             fx += self->bwidth;
1322         /* titletop is a bit to the right */
1323         else if (win == self->titletop)
1324             fx += ob_rr_theme->grip_width + self->bwidth;
1325         /* titletopright is way to the right edge */
1326         else if (win == self->titletopright)
1327             fx += self->area.width - (ob_rr_theme->grip_width + self->bwidth);
1328         /* titleright is even more way to the right edge */
1329         else if (win == self->titleright)
1330             fx += self->area.width - self->bwidth;
1331
1332         /* figure out if we're over the area that should be considered a
1333            button */
1334         if (fy < self->bwidth + ob_rr_theme->paddingy + 1 +
1335             ob_rr_theme->button_size)
1336         {
1337             if (fx < (self->bwidth + ob_rr_theme->paddingx + 1 +
1338                       ob_rr_theme->button_size))
1339             {
1340                 if (self->leftmost != OB_FRAME_CONTEXT_NONE)
1341                     return self->leftmost;
1342             }
1343             else if (fx >= (self->area.width -
1344                             (self->bwidth + ob_rr_theme->paddingx + 1 +
1345                              ob_rr_theme->button_size)))
1346             {
1347                 if (self->rightmost != OB_FRAME_CONTEXT_NONE)
1348                     return self->rightmost;
1349             }
1350         }
1351
1352         /* there is no resizing maximized windows so make them the titlebar
1353            context */
1354         return OB_FRAME_CONTEXT_TITLEBAR;
1355     }
1356     else if (self->max_vert &&
1357              (win == self->titletop || win == self->topresize))
1358         /* can't resize vertically when max vert */
1359         return OB_FRAME_CONTEXT_TITLEBAR;
1360     else if (self->shaded &&
1361              (win == self->titletop || win == self->topresize))
1362         /* can't resize vertically when shaded */
1363         return OB_FRAME_CONTEXT_TITLEBAR;
1364
1365     if (win == self->window)            return OB_FRAME_CONTEXT_FRAME;
1366     if (win == self->label)             return OB_FRAME_CONTEXT_TITLEBAR;
1367     if (win == self->handle)            return OB_FRAME_CONTEXT_BOTTOM;
1368     if (win == self->handletop)         return OB_FRAME_CONTEXT_BOTTOM;
1369     if (win == self->handlebottom)      return OB_FRAME_CONTEXT_BOTTOM;
1370     if (win == self->handleleft)        return OB_FRAME_CONTEXT_BLCORNER;
1371     if (win == self->lgrip)             return OB_FRAME_CONTEXT_BLCORNER;
1372     if (win == self->lgripleft)         return OB_FRAME_CONTEXT_BLCORNER;
1373     if (win == self->lgriptop)          return OB_FRAME_CONTEXT_BLCORNER;
1374     if (win == self->lgripbottom)       return OB_FRAME_CONTEXT_BLCORNER;
1375     if (win == self->handleright)       return OB_FRAME_CONTEXT_BRCORNER;
1376     if (win == self->rgrip)             return OB_FRAME_CONTEXT_BRCORNER;
1377     if (win == self->rgripright)        return OB_FRAME_CONTEXT_BLCORNER;
1378     if (win == self->rgriptop)          return OB_FRAME_CONTEXT_BLCORNER;
1379     if (win == self->rgripbottom)       return OB_FRAME_CONTEXT_BLCORNER;
1380     if (win == self->title)             return OB_FRAME_CONTEXT_TITLEBAR;
1381     if (win == self->titlebottom)       return OB_FRAME_CONTEXT_TITLEBAR;
1382     if (win == self->titleleft)         return OB_FRAME_CONTEXT_TLCORNER;
1383     if (win == self->titletopleft)      return OB_FRAME_CONTEXT_TLCORNER;
1384     if (win == self->titleright)        return OB_FRAME_CONTEXT_TRCORNER;
1385     if (win == self->titletopright)     return OB_FRAME_CONTEXT_TRCORNER;
1386     if (win == self->titletop)          return OB_FRAME_CONTEXT_TOP;
1387     if (win == self->topresize)         return OB_FRAME_CONTEXT_TOP;
1388     if (win == self->tltresize)         return OB_FRAME_CONTEXT_TLCORNER;
1389     if (win == self->tllresize)         return OB_FRAME_CONTEXT_TLCORNER;
1390     if (win == self->trtresize)         return OB_FRAME_CONTEXT_TRCORNER;
1391     if (win == self->trrresize)         return OB_FRAME_CONTEXT_TRCORNER;
1392     if (win == self->left)              return OB_FRAME_CONTEXT_LEFT;
1393     if (win == self->right)             return OB_FRAME_CONTEXT_RIGHT;
1394     if (win == self->innertop)          return OB_FRAME_CONTEXT_TITLEBAR;
1395     if (win == self->innerleft)         return OB_FRAME_CONTEXT_LEFT;
1396     if (win == self->innerbottom)       return OB_FRAME_CONTEXT_BOTTOM;
1397     if (win == self->innerright)        return OB_FRAME_CONTEXT_RIGHT;
1398     if (win == self->max)               return OB_FRAME_CONTEXT_MAXIMIZE;
1399     if (win == self->iconify)           return OB_FRAME_CONTEXT_ICONIFY;
1400     if (win == self->close)             return OB_FRAME_CONTEXT_CLOSE;
1401     if (win == self->icon)              return OB_FRAME_CONTEXT_ICON;
1402     if (win == self->desk)              return OB_FRAME_CONTEXT_ALLDESKTOPS;
1403     if (win == self->shade)             return OB_FRAME_CONTEXT_SHADE;
1404
1405     return OB_FRAME_CONTEXT_NONE;
1406 }
1407
1408 void frame_client_gravity(ObFrame *self, gint *x, gint *y)
1409 {
1410     /* horizontal */
1411     switch (self->client->gravity) {
1412     default:
1413     case NorthWestGravity:
1414     case SouthWestGravity:
1415     case WestGravity:
1416         break;
1417
1418     case NorthGravity:
1419     case SouthGravity:
1420     case CenterGravity:
1421         /* the middle of the client will be the middle of the frame */
1422         *x -= (self->size.right - self->size.left) / 2;
1423         break;
1424
1425     case NorthEastGravity:
1426     case SouthEastGravity:
1427     case EastGravity:
1428         /* the right side of the client will be the right side of the frame */
1429         *x -= self->size.right + self->size.left -
1430             self->client->border_width * 2;
1431         break;
1432
1433     case ForgetGravity:
1434     case StaticGravity:
1435         /* the client's position won't move */
1436         *x -= self->size.left - self->client->border_width;
1437         break;
1438     }
1439
1440     /* vertical */
1441     switch (self->client->gravity) {
1442     default:
1443     case NorthWestGravity:
1444     case NorthEastGravity:
1445     case NorthGravity:
1446         break;
1447
1448     case CenterGravity:
1449     case EastGravity:
1450     case WestGravity:
1451         /* the middle of the client will be the middle of the frame */
1452         *y -= (self->size.bottom - self->size.top) / 2;
1453         break;
1454
1455     case SouthWestGravity:
1456     case SouthEastGravity:
1457     case SouthGravity:
1458         /* the bottom of the client will be the bottom of the frame */
1459         *y -= self->size.bottom + self->size.top -
1460             self->client->border_width * 2;
1461         break;
1462
1463     case ForgetGravity:
1464     case StaticGravity:
1465         /* the client's position won't move */
1466         *y -= self->size.top - self->client->border_width;
1467         break;
1468     }
1469 }
1470
1471 void frame_frame_gravity(ObFrame *self, gint *x, gint *y)
1472 {
1473     /* horizontal */
1474     switch (self->client->gravity) {
1475     default:
1476     case NorthWestGravity:
1477     case WestGravity:
1478     case SouthWestGravity:
1479         break;
1480     case NorthGravity:
1481     case CenterGravity:
1482     case SouthGravity:
1483         /* the middle of the client will be the middle of the frame */
1484         *x += (self->size.right - self->size.left) / 2;
1485         break;
1486     case NorthEastGravity:
1487     case EastGravity:
1488     case SouthEastGravity:
1489         /* the right side of the client will be the right side of the frame */
1490         *x += self->size.right + self->size.left -
1491             self->client->border_width * 2;
1492         break;
1493     case StaticGravity:
1494     case ForgetGravity:
1495         /* the client's position won't move */
1496         *x += self->size.left - self->client->border_width;
1497         break;
1498     }
1499
1500     /* vertical */
1501     switch (self->client->gravity) {
1502     default:
1503     case NorthWestGravity:
1504     case NorthGravity:
1505     case NorthEastGravity:
1506         break;
1507     case WestGravity:
1508     case CenterGravity:
1509     case EastGravity:
1510         /* the middle of the client will be the middle of the frame */
1511         *y += (self->size.bottom - self->size.top) / 2;
1512         break;
1513     case SouthWestGravity:
1514     case SouthGravity:
1515     case SouthEastGravity:
1516         /* the bottom of the client will be the bottom of the frame */
1517         *y += self->size.bottom + self->size.top -
1518             self->client->border_width * 2;
1519         break;
1520     case StaticGravity:
1521     case ForgetGravity:
1522         /* the client's position won't move */
1523         *y += self->size.top - self->client->border_width;
1524         break;
1525     }
1526 }
1527
1528 void frame_rect_to_frame(ObFrame *self, Rect *r)
1529 {
1530     r->width += self->size.left + self->size.right;
1531     r->height += self->size.top + self->size.bottom;
1532     frame_client_gravity(self, &r->x, &r->y);
1533 }
1534
1535 static void flash_done(gpointer data)
1536 {
1537     ObFrame *self = data;
1538
1539     if (self->focused != self->flash_on)
1540         frame_adjust_focus(self, self->focused);
1541 }
1542
1543 static gboolean flash_timeout(gpointer data)
1544 {
1545     ObFrame *self = data;
1546     GTimeVal now;
1547
1548     g_get_current_time(&now);
1549     if (now.tv_sec > self->flash_end.tv_sec ||
1550         (now.tv_sec == self->flash_end.tv_sec &&
1551          now.tv_usec >= self->flash_end.tv_usec))
1552         self->flashing = FALSE;
1553
1554     if (!self->flashing)
1555         return FALSE; /* we are done */
1556
1557     self->flash_on = !self->flash_on;
1558     if (!self->focused) {
1559         frame_adjust_focus(self, self->flash_on);
1560         self->focused = FALSE;
1561     }
1562
1563     return TRUE; /* go again */
1564 }
1565
1566 void frame_flash_start(ObFrame *self)
1567 {
1568     self->flash_on = self->focused;
1569
1570     if (!self->flashing)
1571         ob_main_loop_timeout_add(ob_main_loop,
1572                                  G_USEC_PER_SEC * 0.6,
1573                                  flash_timeout,
1574                                  self,
1575                                  g_direct_equal,
1576                                  flash_done);
1577     g_get_current_time(&self->flash_end);
1578     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
1579
1580     self->flashing = TRUE;
1581 }
1582
1583 void frame_flash_stop(ObFrame *self)
1584 {
1585     self->flashing = FALSE;
1586 }
1587
1588 static gulong frame_animate_iconify_time_left(ObFrame *self,
1589                                               const GTimeVal *now)
1590 {
1591     glong sec, usec;
1592     sec = self->iconify_animation_end.tv_sec - now->tv_sec;
1593     usec = self->iconify_animation_end.tv_usec - now->tv_usec;
1594     if (usec < 0) {
1595         usec += G_USEC_PER_SEC;
1596         sec--;
1597     }
1598     /* no negative values */
1599     return MAX(sec * G_USEC_PER_SEC + usec, 0);
1600 }
1601
1602 static gboolean frame_animate_iconify(gpointer p)
1603 {
1604     ObFrame *self = p;
1605     gint x, y, w, h;
1606     gint iconx, icony, iconw;
1607     GTimeVal now;
1608     gulong time;
1609     gboolean iconifying;
1610
1611     if (self->client->icon_geometry.width == 0) {
1612         /* there is no icon geometry set so just go straight down */
1613         Rect *a = screen_physical_area_monitor
1614             (screen_find_monitor(&self->area));
1615         iconx = self->area.x + self->area.width / 2 + 32;
1616         icony = a->y + a->width;
1617         iconw = 64;
1618         g_free(a);
1619     } else {
1620         iconx = self->client->icon_geometry.x;
1621         icony = self->client->icon_geometry.y;
1622         iconw = self->client->icon_geometry.width;
1623     }
1624
1625     iconifying = self->iconify_animation_going > 0;
1626
1627     /* how far do we have left to go ? */
1628     g_get_current_time(&now);
1629     time = frame_animate_iconify_time_left(self, &now);
1630
1631     if (time == 0 || iconifying) {
1632         /* start where the frame is supposed to be */
1633         x = self->area.x;
1634         y = self->area.y;
1635         w = self->area.width;
1636         h = self->area.height;
1637     } else {
1638         /* start at the icon */
1639         x = iconx;
1640         y = icony;
1641         w = iconw;
1642         h = self->size.top; /* just the titlebar */
1643     }
1644
1645     if (time > 0) {
1646         glong dx, dy, dw;
1647         glong elapsed;
1648
1649         dx = self->area.x - iconx;
1650         dy = self->area.y - icony;
1651         dw = self->area.width - self->bwidth * 2 - iconw;
1652          /* if restoring, we move in the opposite direction */
1653         if (!iconifying) { dx = -dx; dy = -dy; dw = -dw; }
1654
1655         elapsed = FRAME_ANIMATE_ICONIFY_TIME - time;
1656         x = x - (dx * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1657         y = y - (dy * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1658         w = w - (dw * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1659         h = self->size.top; /* just the titlebar */
1660     }
1661
1662     if (time == 0)
1663         frame_end_iconify_animation(self);
1664     else {
1665         XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1666         XFlush(ob_display);
1667     }
1668
1669     return time > 0; /* repeat until we're out of time */
1670 }
1671
1672 void frame_end_iconify_animation(ObFrame *self)
1673 {
1674     /* see if there is an animation going */
1675     if (self->iconify_animation_going == 0) return;
1676
1677     if (!self->visible)
1678         XUnmapWindow(ob_display, self->window);
1679     else {
1680         /* Send a ConfigureNotify when the animation is done, this fixes
1681            KDE's pager showing the window in the wrong place.  since the
1682            window is mapped at a different location and is then moved, we
1683            need to send the synthetic configurenotify, since apps may have
1684            read the position when the client mapped, apparently. */
1685         client_reconfigure(self->client, TRUE);
1686     }
1687
1688     /* we're not animating any more ! */
1689     self->iconify_animation_going = 0;
1690
1691     XMoveResizeWindow(ob_display, self->window,
1692                       self->area.x, self->area.y,
1693                       self->area.width, self->area.height);
1694     /* we delay re-rendering until after we're done animating */
1695     framerender_frame(self);
1696     XFlush(ob_display);
1697 }
1698
1699 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying)
1700 {
1701     gulong time;
1702     gboolean new_anim = FALSE;
1703     gboolean set_end = TRUE;
1704     GTimeVal now;
1705
1706     /* if there is no titlebar, just don't animate for now
1707        XXX it would be nice tho.. */
1708     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1709         return;
1710
1711     /* get the current time */
1712     g_get_current_time(&now);
1713
1714     /* get how long until the end */
1715     time = FRAME_ANIMATE_ICONIFY_TIME;
1716     if (self->iconify_animation_going) {
1717         if (!!iconifying != (self->iconify_animation_going > 0)) {
1718             /* animation was already going on in the opposite direction */
1719             time = time - frame_animate_iconify_time_left(self, &now);
1720         } else
1721             /* animation was already going in the same direction */
1722             set_end = FALSE;
1723     } else
1724         new_anim = TRUE;
1725     self->iconify_animation_going = iconifying ? 1 : -1;
1726
1727     /* set the ending time */
1728     if (set_end) {
1729         self->iconify_animation_end.tv_sec = now.tv_sec;
1730         self->iconify_animation_end.tv_usec = now.tv_usec;
1731         g_time_val_add(&self->iconify_animation_end, time);
1732     }
1733
1734     if (new_anim) {
1735         ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
1736                                          self, FALSE);
1737         ob_main_loop_timeout_add(ob_main_loop,
1738                                  FRAME_ANIMATE_ICONIFY_STEP_TIME,
1739                                  frame_animate_iconify, self,
1740                                  g_direct_equal, NULL);
1741
1742         /* do the first step */
1743         frame_animate_iconify(self);
1744
1745         /* show it during the animation even if it is not "visible" */
1746         if (!self->visible)
1747             XMapWindow(ob_display, self->window);
1748     }
1749 }