Make the dock a context and add actions LowerDock and RaiseDock
[dana/openbox.git] / openbox / frame.c
index e8a532c..bd9527b 100644 (file)
 #include "client.h"
 #include "openbox.h"
 #include "grab.h"
+#include "debug.h"
 #include "config.h"
 #include "framerender.h"
 #include "focus_cycle.h"
 #include "focus_cycle_indicator.h"
 #include "moveresize.h"
 #include "screen.h"
-#include "render/theme.h"
+#include "obrender/theme.h"
 #include "obt/display.h"
+#include "obt/xqueue.h"
 #include "obt/prop.h"
 
 #define FRAME_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
@@ -39,7 +41,7 @@
                            EnterWindowMask | LeaveWindowMask)
 
 #define FRAME_ANIMATE_ICONIFY_TIME 150000 /* .15 seconds */
-#define FRAME_ANIMATE_ICONIFY_STEP_TIME (G_USEC_PER_SEC / 60) /* 60 Hz */
+#define FRAME_ANIMATE_ICONIFY_STEP_TIME (1000 / 60) /* 60 Hz */
 
 #define FRAME_HANDLE_Y(f) (f->size.top + f->client->area.height + f->cbwidth_b)
 
@@ -88,7 +90,7 @@ ObFrame *frame_new(ObClient *client)
     ObFrame *self;
     Visual *visual;
 
-    self = g_new0(ObFrame, 1);
+    self = g_slice_new0(ObFrame);
     self->client = client;
 
     visual = check_32bit_client(client);
@@ -98,7 +100,7 @@ ObFrame *frame_new(ObClient *client)
     mask = 0;
     if (visual) {
         /* client has a 32-bit visual */
-        mask |= CWColormap | CWBackPixel | CWBorderPixel;
+        mask = CWColormap | CWBackPixel | CWBorderPixel;
         /* create a colormap with the visual */
         self->colormap = attrib.colormap =
             XCreateColormap(obt_display, obt_root(ob_screen),
@@ -114,7 +116,7 @@ ObFrame *frame_new(ObClient *client)
     mask = 0;
     if (visual) {
         /* client has a 32-bit visual */
-        mask |= CWColormap | CWBackPixel | CWBorderPixel;
+        mask = CWColormap | CWBackPixel | CWBorderPixel;
         attrib.colormap = RrColormap(ob_rr_inst);
     }
 
@@ -186,9 +188,13 @@ ObFrame *frame_new(ObClient *client)
     self->max_hover = self->close_hover = self->desk_hover =
         self->iconify_hover = self->shade_hover = FALSE;
 
+    /* make sure the size will be different the first time, so the extent hints
+       will be set */
+    STRUT_SET(self->oldsize, -1, -1, -1, -1);
+
     set_theme_statics(self);
 
-    return (ObFrame*)self;
+    return self;
 }
 
 static void set_theme_statics(ObFrame *self)
@@ -228,7 +234,7 @@ void frame_free(ObFrame *self)
     if (self->colormap)
         XFreeColormap(obt_display, self->colormap);
 
-    g_free(self);
+    g_slice_free(ObFrame, self);
 }
 
 void frame_show(ObFrame *self)
@@ -265,25 +271,26 @@ void frame_adjust_theme(ObFrame *self)
     set_theme_statics(self);
 }
 
-void frame_adjust_shape(ObFrame *self)
-{
 #ifdef SHAPE
+void frame_adjust_shape_kind(ObFrame *self, int kind)
+{
     gint num;
     XRectangle xrect[2];
 
-    if (!self->client->shaped) {
+    if (!((kind == ShapeBounding && self->client->shaped) ||
+          (kind == ShapeInput && self->client->shaped_input))) {
         /* clear the shape on the frame window */
-        XShapeCombineMask(obt_display, self->window, ShapeBounding,
+        XShapeCombineMask(obt_display, self->window, kind,
                           self->size.left,
                           self->size.top,
                           None, ShapeSet);
     } else {
         /* make the frame's shape match the clients */
-        XShapeCombineShape(obt_display, self->window, ShapeBounding,
+        XShapeCombineShape(obt_display, self->window, kind,
                            self->size.left,
                            self->size.top,
                            self->client->window,
-                           ShapeBounding, ShapeSet);
+                           kind, ShapeSet);
 
         num = 0;
         if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
@@ -309,16 +316,20 @@ void frame_adjust_shape(ObFrame *self)
                                 ShapeBounding, 0, 0, xrect, num,
                                 ShapeUnion, Unsorted);
     }
+}
+#endif
+
+void frame_adjust_shape(ObFrame *self)
+{
+#ifdef SHAPE
+  frame_adjust_shape_kind(self, ShapeBounding);
+  frame_adjust_shape_kind(self, ShapeInput);
 #endif
 }
 
 void frame_adjust_area(ObFrame *self, gboolean moved,
                        gboolean resized, gboolean fake)
 {
-    Strut oldsize;
-
-    oldsize = self->size;
-
     if (resized) {
         /* do this before changing the frame's status like max_horz max_vert */
         frame_adjust_cursors(self);
@@ -329,13 +340,14 @@ void frame_adjust_area(ObFrame *self, gboolean moved,
         self->max_vert = self->client->max_vert;
         self->shaded = self->client->shaded;
 
-        if (self->decorations & OB_FRAME_DECOR_BORDER ||
-            (self->client->undecorated && config_theme_keepborder))
+        if (self->decorations & OB_FRAME_DECOR_BORDER)
             self->bwidth = ob_rr_theme->fbwidth;
         else
             self->bwidth = 0;
 
-        if (self->decorations & OB_FRAME_DECOR_BORDER) {
+        if (self->decorations & OB_FRAME_DECOR_BORDER &&
+            !self->client->undecorated)
+        {
             self->cbwidth_l = self->cbwidth_r = ob_rr_theme->cbwidthx;
             self->cbwidth_t = self->cbwidth_b = ob_rr_theme->cbwidthy;
         } else
@@ -358,9 +370,7 @@ void frame_adjust_area(ObFrame *self, gboolean moved,
 
         STRUT_SET(self->size,
                   self->cbwidth_l + (!self->max_horz ? self->bwidth : 0),
-                  self->cbwidth_t +
-                  (!self->max_horz || !self->max_vert ||
-                   !self->client->undecorated ? self->bwidth : 0),
+                  self->cbwidth_t + self->bwidth,
                   self->cbwidth_r + (!self->max_horz ? self->bwidth : 0),
                   self->cbwidth_b +
                   (!self->max_horz || !self->max_vert ? self->bwidth : 0));
@@ -835,7 +845,7 @@ void frame_adjust_area(ObFrame *self, gboolean moved,
             frame_adjust_shape(self);
         }
 
-        if (!STRUT_EQUAL(self->size, oldsize)) {
+        if (!STRUT_EQUAL(self->size, self->oldsize)) {
             gulong vals[4];
             vals[0] = self->size.left;
             vals[1] = self->size.right;
@@ -845,17 +855,20 @@ void frame_adjust_area(ObFrame *self, gboolean moved,
                             CARDINAL, vals, 4);
             OBT_PROP_SETA32(self->client->window, KDE_NET_WM_FRAME_STRUT,
                             CARDINAL, vals, 4);
+            self->oldsize = self->size;
         }
 
         /* if this occurs while we are focus cycling, the indicator needs to
            match the changes */
         if (focus_cycle_target == self->client)
-            focus_cycle_draw_indicator(self->client);
+            focus_cycle_update_indicator(self->client);
     }
-    if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR))
+    if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR) &&
+        self->label_width)
+    {
         XResizeWindow(obt_display, self->label, self->label_width,
                       ob_rr_theme->label_height);
-
+    }
 }
 
 static void frame_adjust_cursors(ObFrame *self)
@@ -939,6 +952,9 @@ void frame_adjust_state(ObFrame *self)
 
 void frame_adjust_focus(ObFrame *self, gboolean hilite)
 {
+    ob_debug_type(OB_DEBUG_FOCUS,
+                  "Frame for 0x%x has focus: %d\n",
+                  self->client->window, hilite);
     self->focused = hilite;
     self->need_render = TRUE;
     framerender_frame(self);
@@ -1028,34 +1044,24 @@ void frame_grab_client(ObFrame *self)
     window_add(&self->rgripbottom, CLIENT_AS_WINDOW(self->client));
 }
 
-void frame_release_client(ObFrame *self)
+static gboolean find_reparent(XEvent *e, gpointer data)
 {
-    XEvent ev;
-    gboolean reparent = TRUE;
+    const ObFrame *self = data;
+
+    /* Find ReparentNotify events for the window that aren't being reparented into the
+       frame, thus the client reparenting itself off the frame. */
+    return e->type == ReparentNotify && e->xreparent.window == self->client->window &&
+        e->xreparent.parent != self->window;
+}
 
+void frame_release_client(ObFrame *self)
+{
     /* if there was any animation going on, kill it */
-    obt_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
-                                      self, FALSE);
+    if (self->iconify_animation_timer)
+        g_source_remove(self->iconify_animation_timer);
 
     /* check if the app has already reparented its window away */
-    while (XCheckTypedWindowEvent(obt_display, self->client->window,
-                                  ReparentNotify, &ev))
-    {
-        /* This check makes sure we don't catch our own reparent action to
-           our frame window. This doesn't count as the app reparenting itself
-           away of course.
-
-           Reparent events that are generated by us are just discarded here.
-           They are of no consequence to us anyhow.
-        */
-        if (ev.xreparent.parent != self->window) {
-            reparent = FALSE;
-            XPutBackEvent(obt_display, &ev);
-            break;
-        }
-    }
-
-    if (reparent) {
+    if (!xqueue_exists_local(find_reparent, self)) {
         /* according to the ICCCM - if the client doesn't reparent itself,
            then we will reparent the window to root for them */
         XReparentWindow(obt_display, self->client->window, obt_root(ob_screen),
@@ -1109,7 +1115,7 @@ void frame_release_client(ObFrame *self)
     window_remove(self->rgriptop);
     window_remove(self->rgripbottom);
 
-    obt_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
+    if (self->flash_timer) g_source_remove(self->flash_timer);
 }
 
 /* is there anything present between us and the label? */
@@ -1133,15 +1139,37 @@ static gboolean is_button_present(ObFrame *self, const gchar *lc, gint dir) {
     return FALSE;
 }
 
+static void place_button(ObFrame *self, const char *lc, gint bwidth,
+                         gint left, gint i,
+                         gint *x, gint *button_on, gint *button_x)
+{
+  if (!(*button_on = is_button_present(self, lc, i)))
+    return;
+
+  self->label_width -= bwidth;
+  if (i > 0)
+    *button_x = *x;
+  *x += i * bwidth;
+  if (i < 0) {
+    if (self->label_x <= left || *x > self->label_x) {
+      *button_x = *x;
+    } else {
+      /* the button would have been drawn on top of another button */
+      *button_on = FALSE;
+      self->label_width += bwidth;
+    }
+  }
+}
+
 static void layout_title(ObFrame *self)
 {
     gchar *lc;
     gint i;
 
     const gint bwidth = ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
-    /* position of the left most button */
+    /* position of the leftmost button */
     const gint left = ob_rr_theme->paddingx + 1;
-    /* position of the right most button */
+    /* position of the rightmost button */
     const gint right = self->width;
 
     /* turn them all off */
@@ -1150,7 +1178,7 @@ static void layout_title(ObFrame *self)
     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
     self->leftmost = self->rightmost = OB_FRAME_CONTEXT_NONE;
 
-    /* figure out what's being show, find each element's position, and the
+    /* figure out what's being shown, find each element's position, and the
        width of the label
 
        do the ones before the label, then after the label,
@@ -1180,53 +1208,23 @@ static void layout_title(ObFrame *self)
                 break; /* break the for loop, do other side of label */
             } else if (*lc == 'N') {
                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICON;
-                if ((self->icon_on = is_button_present(self, lc, i))) {
-                    /* icon is bigger than buttons */
-                    self->label_width -= bwidth + 2;
-                    if (i > 0) self->icon_x = x;
-                    x += i * (bwidth + 2);
-                    if (i < 0) self->icon_x = x;
-                }
+                /* icon is bigger than buttons */
+                place_button(self, lc, bwidth + 2, left, i, &x, &self->icon_on, &self->icon_x);
             } else if (*lc == 'D') {
                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ALLDESKTOPS;
-                if ((self->desk_on = is_button_present(self, lc, i))) {
-                    self->label_width -= bwidth;
-                    if (i > 0) self->desk_x = x;
-                    x += i * bwidth;
-                    if (i < 0) self->desk_x = x;
-                }
+                place_button(self, lc, bwidth, left, i, &x, &self->desk_on, &self->desk_x);
             } else if (*lc == 'S') {
                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_SHADE;
-                if ((self->shade_on = is_button_present(self, lc, i))) {
-                    self->label_width -= bwidth;
-                    if (i > 0) self->shade_x = x;
-                    x += i * bwidth;
-                    if (i < 0) self->shade_x = x;
-                }
+                place_button(self, lc, bwidth, left, i, &x, &self->shade_on, &self->shade_x);
             } else if (*lc == 'I') {
                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICONIFY;
-                if ((self->iconify_on = is_button_present(self, lc, i))) {
-                    self->label_width -= bwidth;
-                    if (i > 0) self->iconify_x = x;
-                    x += i * bwidth;
-                    if (i < 0) self->iconify_x = x;
-                }
+                place_button(self, lc, bwidth, left, i, &x, &self->iconify_on, &self->iconify_x);
             } else if (*lc == 'M') {
                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_MAXIMIZE;
-                if ((self->max_on = is_button_present(self, lc, i))) {
-                    self->label_width -= bwidth;
-                    if (i > 0) self->max_x = x;
-                    x += i * bwidth;
-                    if (i < 0) self->max_x = x;
-                }
+                place_button(self, lc, bwidth, left, i, &x, &self->max_on, &self->max_x);
             } else if (*lc == 'C') {
                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_CLOSE;
-                if ((self->close_on = is_button_present(self, lc, i))) {
-                    self->label_width -= bwidth;
-                    if (i > 0) self->close_x = x;
-                    x += i * bwidth;
-                    if (i < 0) self->close_x = x;
-                }
+                place_button(self, lc, bwidth, left, i, &x, &self->close_on, &self->close_x);
             } else
                 continue; /* don't set firstcon */
             firstcon = NULL;
@@ -1276,8 +1274,7 @@ static void layout_title(ObFrame *self)
     } else
         XUnmapWindow(obt_display, self->close);
 
-    if (self->label_on) {
-        self->label_width = MAX(1, self->label_width); /* no lower than 1 */
+    if (self->label_on && self->label_width > 0) {
         XMapWindow(obt_display, self->label);
         XMoveWindow(obt_display, self->label, self->label_x,
                     ob_rr_theme->paddingy);
@@ -1285,6 +1282,50 @@ static void layout_title(ObFrame *self)
         XUnmapWindow(obt_display, self->label);
 }
 
+gboolean frame_next_context_from_string(gchar *names, ObFrameContext *cx)
+{
+    gchar *p, *n;
+
+    if (!*names) /* empty string */
+        return FALSE;
+
+    /* find the first space */
+    for (p = names; *p; p = g_utf8_next_char(p)) {
+        const gunichar c = g_utf8_get_char(p);
+        if (g_unichar_isspace(c)) break;
+    }
+
+    if (p == names) {
+        /* leading spaces in the string */
+        n = g_utf8_next_char(names);
+        if (!frame_next_context_from_string(n, cx))
+            return FALSE;
+    } else {
+        n = p;
+        if (*p) {
+            /* delete the space with null zero(s) */
+            while (n < g_utf8_next_char(p))
+                *(n++) = '\0';
+        }
+
+        *cx = frame_context_from_string(names);
+
+        /* find the next non-space */
+        for (; *n; n = g_utf8_next_char(n)) {
+            const gunichar c = g_utf8_get_char(n);
+            if (!g_unichar_isspace(c)) break;
+        }
+    }
+
+    /* delete everything we just read (copy everything at n to the start of
+       the string */
+    for (p = names; *n; ++p, ++n)
+        *p = *n;
+    *p = *n;
+
+    return TRUE;
+}
+
 ObFrameContext frame_context_from_string(const gchar *name)
 {
     if (!g_ascii_strcasecmp("Desktop", name))
@@ -1327,18 +1368,27 @@ ObFrameContext frame_context_from_string(const gchar *name)
         return OB_FRAME_CONTEXT_CLOSE;
     else if (!g_ascii_strcasecmp("MoveResize", name))
         return OB_FRAME_CONTEXT_MOVE_RESIZE;
+    else if (!g_ascii_strcasecmp("Dock", name))
+        return OB_FRAME_CONTEXT_DOCK;
+
     return OB_FRAME_CONTEXT_NONE;
 }
 
 ObFrameContext frame_context(ObClient *client, Window win, gint x, gint y)
 {
     ObFrame *self;
+    ObWindow *obwin;
 
     if (moveresize_in_progress)
         return OB_FRAME_CONTEXT_MOVE_RESIZE;
 
     if (win == obt_root(ob_screen))
-        return OB_FRAME_CONTEXT_ROOT ;
+        return OB_FRAME_CONTEXT_ROOT;
+    if ((obwin = window_find(win))) {
+        if (WINDOW_IS_DOCK(obwin)) {
+          return OB_FRAME_CONTEXT_DOCK;
+        }
+    }
     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
     if (win == client->window) {
         /* conceptually, this is the desktop, as far as users are
@@ -1622,6 +1672,7 @@ static gboolean flash_timeout(gpointer data)
         self->focused = FALSE;
     }
 
+    XFlush(obt_display);
     return TRUE; /* go again */
 }
 
@@ -1630,12 +1681,9 @@ void frame_flash_start(ObFrame *self)
     self->flash_on = self->focused;
 
     if (!self->flashing)
-        obt_main_loop_timeout_add(ob_main_loop,
-                                  G_USEC_PER_SEC * 0.6,
-                                  flash_timeout,
-                                  self,
-                                  g_direct_equal,
-                                  flash_done);
+        self->flash_timer = g_timeout_add_full(G_PRIORITY_DEFAULT,
+                                               600, flash_timeout, self,
+                                               flash_done);
     g_get_current_time(&self->flash_end);
     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
 
@@ -1672,12 +1720,12 @@ static gboolean frame_animate_iconify(gpointer p)
 
     if (self->client->icon_geometry.width == 0) {
         /* there is no icon geometry set so just go straight down */
-        Rect *a = screen_physical_area_monitor
-            (screen_find_monitor(&self->area));
+        const Rect *a;
+
+        a = screen_physical_area_monitor(screen_find_monitor(&self->area));
         iconx = self->area.x + self->area.width / 2 + 32;
         icony = a->y + a->width;
         iconw = 64;
-        g_free(a);
     } else {
         iconx = self->client->icon_geometry.x;
         icony = self->client->icon_geometry.y;
@@ -1722,11 +1770,11 @@ static gboolean frame_animate_iconify(gpointer p)
     }
 
     XMoveResizeWindow(obt_display, self->window, x, y, w, h);
-    XFlush(obt_display);
 
     if (time == 0)
         frame_end_iconify_animation(self);
 
+    XFlush(obt_display);
     return time > 0; /* repeat until we're out of time */
 }
 
@@ -1793,12 +1841,13 @@ void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying)
     }
 
     if (new_anim) {
-        obt_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
-                                          self, FALSE);
-        obt_main_loop_timeout_add(ob_main_loop,
-                                  FRAME_ANIMATE_ICONIFY_STEP_TIME,
-                                  frame_animate_iconify, self,
-                                  g_direct_equal, NULL);
+        if (self->iconify_animation_timer)
+            g_source_remove(self->iconify_animation_timer);
+        self->iconify_animation_timer =
+            g_timeout_add_full(G_PRIORITY_DEFAULT,
+                               FRAME_ANIMATE_ICONIFY_STEP_TIME,
+                               frame_animate_iconify, self, NULL);
+                               
 
         /* do the first step */
         frame_animate_iconify(self);