use const Rect* not Rect const*
[dana/openbox.git] / openbox / keyboard.c
index 0aade9a..ad4e4f3 100644 (file)
@@ -17,7 +17,6 @@
    See the COPYING file for a copy of the GNU General Public License.
 */
 
-#include "mainloop.h"
 #include "focus.h"
 #include "screen.h"
 #include "frame.h"
@@ -26,7 +25,6 @@
 #include "grab.h"
 #include "client.h"
 #include "actions.h"
-#include "prop.h"
 #include "menuframe.h"
 #include "config.h"
 #include "keytree.h"
@@ -35,6 +33,7 @@
 #include "moveresize.h"
 #include "popup.h"
 #include "gettext.h"
+#include "obt/keyboard.h"
 
 #include <glib.h>
 
@@ -46,19 +45,20 @@ static void grab_keys(gboolean grab)
 {
     KeyBindingTree *p;
 
-    ungrab_all_keys(RootWindow(ob_display, ob_screen));
+    ungrab_all_keys(obt_root(ob_screen));
 
     if (grab) {
         p = curpos ? curpos->first_child : keyboard_firstnode;
         while (p) {
-            grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
-                     GrabModeAsync);
+            if (p->key)
+                grab_key(p->key, p->state, obt_root(ob_screen),
+                         GrabModeAsync);
             p = p->next_sibling;
         }
         if (curpos)
             grab_key(config_keyboard_reset_keycode,
                      config_keyboard_reset_state,
-                     RootWindow(ob_display, ob_screen), GrabModeAsync);
+                     obt_root(ob_screen), GrabModeAsync);
     }
 }
 
@@ -79,7 +79,7 @@ static void set_curpos(KeyBindingTree *newpos)
     if (curpos != NULL) {
         gchar *text = NULL;
         GList *it;
-        Rect *a;
+        const Rect *a;
 
         for (it = curpos->keylist; it; it = g_list_next(it)) {
             gchar *oldtext = text;
@@ -90,12 +90,11 @@ static void set_curpos(KeyBindingTree *newpos)
             g_free(oldtext);
         }
 
-        a = screen_physical_area_active();
+        a = screen_physical_area_primary(FALSE);
         popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
         /* 1 second delay for the popup to show */
         popup_delay_show(popup, G_USEC_PER_SEC, text);
         g_free(text);
-        g_free(a);
     } else {
         popup_hide(popup);
     }
@@ -176,9 +175,12 @@ gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
     gboolean handled = FALSE;
     gboolean done = FALSE;
     gboolean cancel = FALSE;
+    guint mods;
+
+    mods = obt_keyboard_only_modmasks(ev->xkey.state);
 
     if (istate.active) {
-        if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
+        if ((e->type == KeyRelease && !(istate.state & mods))) {
             done = TRUE;
             handled = TRUE;
         } else if (e->type == KeyPress) {
@@ -205,44 +207,47 @@ gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
 }
 #endif
 
-void keyboard_event(ObClient *client, const XEvent *e)
+gboolean keyboard_event(ObClient *client, const XEvent *e)
 {
     KeyBindingTree *p;
+    gboolean used;
+    guint mods;
 
     if (e->type == KeyRelease) {
         grab_key_passive_count(-1);
-        return;
+        return FALSE;
     }
 
     g_assert(e->type == KeyPress);
     grab_key_passive_count(1);
 
+    mods = obt_keyboard_only_modmasks(e->xkey.state);
+
     if (e->xkey.keycode == config_keyboard_reset_keycode &&
-        e->xkey.state == config_keyboard_reset_state)
+        mods == config_keyboard_reset_state)
     {
-        ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
+        obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
         keyboard_reset_chains(-1);
-        return;
+        return TRUE;
     }
 
+    used = FALSE;
     if (curpos == NULL)
         p = keyboard_firstnode;
     else
         p = curpos->first_child;
     while (p) {
-        if (p->key == e->xkey.keycode &&
-            p->state == e->xkey.state)
-        {
+        if (p->key == e->xkey.keycode && p->state == mods) {
             /* if we hit a key binding, then close any open menus and run it */
             if (menu_frame_visible)
                 menu_frame_hide_all();
 
             if (p->first_child != NULL) { /* part of a chain */
-                ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
+                obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
                 /* 3 second timeout for chains */
-                ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
-                                         chain_timeout, NULL,
-                                         g_direct_equal, NULL);
+                obt_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
+                                          chain_timeout, NULL,
+                                          g_direct_equal, NULL);
                 set_curpos(p);
             } else if (p->chroot)         /* an empty chroot */
                 set_curpos(p);
@@ -259,9 +264,53 @@ void keyboard_event(ObClient *client, const XEvent *e)
                                  0, OB_FRAME_CONTEXT_NONE, client);
             }
             break;
+            used = TRUE;
         }
         p = p->next_sibling;
     }
+    return used;
+}
+
+static void node_rebind(KeyBindingTree *node)
+{
+    if (node->first_child) {
+        /* find leaf nodes */
+        node_rebind(node->first_child);
+
+        /* for internal nodes, add them to the tree if they
+           are a chroot, but do this after adding their
+           children */
+        if (node->chroot)
+            keyboard_chroot(node->keylist);
+    }
+    else {
+        /* for leaf nodes, rebind each action assigned to it */
+        while (node->actions) {
+            /* add each action, and remove them from the original tree so
+               they don't get free'd on us */
+            keyboard_bind(node->keylist, node->actions->data);
+            node->actions = g_slist_delete_link(node->actions, node->actions);
+        }
+
+        if (node->chroot)
+            keyboard_chroot(node->keylist);
+    }
+
+    /* go through each sibling */
+    if (node->next_sibling) node_rebind(node->next_sibling);
+}
+
+void keyboard_rebind(void)
+{
+    KeyBindingTree *old;
+
+    old = keyboard_firstnode;
+    keyboard_firstnode = NULL;
+    node_rebind(old);
+
+    tree_destroy(old);
+    set_curpos(NULL);
+    grab_keys(TRUE);
 }
 
 void keyboard_startup(gboolean reconfig)
@@ -273,7 +322,7 @@ void keyboard_startup(gboolean reconfig)
 
 void keyboard_shutdown(gboolean reconfig)
 {
-    ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
+    obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
 
     keyboard_unbind_all();
     set_curpos(NULL);
@@ -281,4 +330,3 @@ void keyboard_shutdown(gboolean reconfig)
     popup_free(popup);
     popup = NULL;
 }
-