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