Import utilities from Tomboy
authorIra W. Snyder <devel@irasnyder.com>
Wed, 16 Jan 2008 01:33:13 +0000 (17:33 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Wed, 16 Jan 2008 02:03:15 +0000 (18:03 -0800)
This imports the global keybinder, along with its dependencies, as well
as the improved presentation functions (to deal properly with focus-stealing)
from Tomboy (http://www.gnome.org/projects/tomboy/). Thanks GPL!

The keybinder is minimally modified from the tomboy version. Instead of
returning void, it returns a gboolean indicating whether or not the
key-grab was successful.

The utilities package has only the #include "config.h" removed.

Makefile
eggaccelerators.c [new file with mode: 0644]
eggaccelerators.h [new file with mode: 0644]
tomboykeybinder.c [new file with mode: 0644]
tomboykeybinder.h [new file with mode: 0644]
tomboyutil.c [new file with mode: 0644]
tomboyutil.h [new file with mode: 0644]

index 5144495..c743125 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ ALL_LIBS=`pkg-config --libs gtk+-2.0 vte dbus-glib-1`
 
 all: tilda
 
-tilda: tilda.o tilda-window.o tilda-terminal.o
+tilda: tilda.o tilda-window.o tilda-terminal.o tomboykeybinder.o tomboyutil.o eggaccelerators.o
        $(GCC) $(CFLAGS) $^ -o $@ $(ALL_LIBS)
 
 tilda.o: tilda.c
@@ -24,6 +24,15 @@ tilda-terminal.o: tilda-terminal.c tilda-terminal.h
        dbus-binding-tool --mode=glib-server --prefix=tilda_terminal tilda-terminal.xml > tilda-terminal-dbus-glue.h
        $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS)
 
+tomboykeybinder.o: tomboykeybinder.c tomboykeybinder.h
+       $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS)
+
+tomboyutil.o: tomboyutil.c tomboyutil.h
+       $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS)
+
+eggaccelerators.o: eggaccelerators.c eggaccelerators.h
+       $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS)
+
 memcheck: tilda
        valgrind --tool=memcheck ./tilda
 
diff --git a/eggaccelerators.c b/eggaccelerators.c
new file mode 100644 (file)
index 0000000..ca63e78
--- /dev/null
@@ -0,0 +1,657 @@
+/* eggaccelerators.c
+ * Copyright (C) 2002  Red Hat, Inc.; Copyright 1998, 2001 Tim Janik
+ * Developed by Havoc Pennington, Tim Janik
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "eggaccelerators.h"
+
+#include <string.h>
+#include <gdk/gdkx.h>
+#include <gdk/gdkkeysyms.h>
+
+enum
+{
+  EGG_MODMAP_ENTRY_SHIFT   = 0,
+  EGG_MODMAP_ENTRY_LOCK    = 1,
+  EGG_MODMAP_ENTRY_CONTROL = 2,
+  EGG_MODMAP_ENTRY_MOD1    = 3,
+  EGG_MODMAP_ENTRY_MOD2    = 4,
+  EGG_MODMAP_ENTRY_MOD3    = 5,
+  EGG_MODMAP_ENTRY_MOD4    = 6,
+  EGG_MODMAP_ENTRY_MOD5    = 7,
+  EGG_MODMAP_ENTRY_LAST    = 8
+};
+
+#define MODMAP_ENTRY_TO_MODIFIER(x) (1 << (x))
+
+typedef struct
+{
+  EggVirtualModifierType mapping[EGG_MODMAP_ENTRY_LAST];
+
+} EggModmap;
+
+const EggModmap* egg_keymap_get_modmap (GdkKeymap *keymap);
+
+static inline gboolean
+is_alt (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'a' || string[1] == 'A') &&
+         (string[2] == 'l' || string[2] == 'L') &&
+         (string[3] == 't' || string[3] == 'T') &&
+         (string[4] == '>'));
+}
+
+static inline gboolean
+is_ctl (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'c' || string[1] == 'C') &&
+         (string[2] == 't' || string[2] == 'T') &&
+         (string[3] == 'l' || string[3] == 'L') &&
+         (string[4] == '>'));
+}
+
+static inline gboolean
+is_modx (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'm' || string[1] == 'M') &&
+         (string[2] == 'o' || string[2] == 'O') &&
+         (string[3] == 'd' || string[3] == 'D') &&
+         (string[4] >= '1' && string[4] <= '5') &&
+         (string[5] == '>'));
+}
+
+static inline gboolean
+is_ctrl (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'c' || string[1] == 'C') &&
+         (string[2] == 't' || string[2] == 'T') &&
+         (string[3] == 'r' || string[3] == 'R') &&
+         (string[4] == 'l' || string[4] == 'L') &&
+         (string[5] == '>'));
+}
+
+static inline gboolean
+is_shft (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 's' || string[1] == 'S') &&
+         (string[2] == 'h' || string[2] == 'H') &&
+         (string[3] == 'f' || string[3] == 'F') &&
+         (string[4] == 't' || string[4] == 'T') &&
+         (string[5] == '>'));
+}
+
+static inline gboolean
+is_shift (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 's' || string[1] == 'S') &&
+         (string[2] == 'h' || string[2] == 'H') &&
+         (string[3] == 'i' || string[3] == 'I') &&
+         (string[4] == 'f' || string[4] == 'F') &&
+         (string[5] == 't' || string[5] == 'T') &&
+         (string[6] == '>'));
+}
+
+static inline gboolean
+is_control (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'c' || string[1] == 'C') &&
+         (string[2] == 'o' || string[2] == 'O') &&
+         (string[3] == 'n' || string[3] == 'N') &&
+         (string[4] == 't' || string[4] == 'T') &&
+         (string[5] == 'r' || string[5] == 'R') &&
+         (string[6] == 'o' || string[6] == 'O') &&
+         (string[7] == 'l' || string[7] == 'L') &&
+         (string[8] == '>'));
+}
+
+static inline gboolean
+is_release (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'r' || string[1] == 'R') &&
+         (string[2] == 'e' || string[2] == 'E') &&
+         (string[3] == 'l' || string[3] == 'L') &&
+         (string[4] == 'e' || string[4] == 'E') &&
+         (string[5] == 'a' || string[5] == 'A') &&
+         (string[6] == 's' || string[6] == 'S') &&
+         (string[7] == 'e' || string[7] == 'E') &&
+         (string[8] == '>'));
+}
+
+static inline gboolean
+is_meta (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'm' || string[1] == 'M') &&
+         (string[2] == 'e' || string[2] == 'E') &&
+         (string[3] == 't' || string[3] == 'T') &&
+         (string[4] == 'a' || string[4] == 'A') &&
+         (string[5] == '>'));
+}
+
+static inline gboolean
+is_super (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 's' || string[1] == 'S') &&
+         (string[2] == 'u' || string[2] == 'U') &&
+         (string[3] == 'p' || string[3] == 'P') &&
+         (string[4] == 'e' || string[4] == 'E') &&
+         (string[5] == 'r' || string[5] == 'R') &&
+         (string[6] == '>'));
+}
+
+static inline gboolean
+is_hyper (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'h' || string[1] == 'H') &&
+         (string[2] == 'y' || string[2] == 'Y') &&
+         (string[3] == 'p' || string[3] == 'P') &&
+         (string[4] == 'e' || string[4] == 'E') &&
+         (string[5] == 'r' || string[5] == 'R') &&
+         (string[6] == '>'));
+}
+
+/**
+ * egg_accelerator_parse_virtual:
+ * @accelerator:      string representing an accelerator
+ * @accelerator_key:  return location for accelerator keyval
+ * @accelerator_mods: return location for accelerator modifier mask
+ *
+ * Parses a string representing a virtual accelerator. The format
+ * looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
+ * "&lt;Release&gt;z" (the last one is for key release).  The parser
+ * is fairly liberal and allows lower or upper case, and also
+ * abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
+ *
+ * If the parse fails, @accelerator_key and @accelerator_mods will
+ * be set to 0 (zero) and %FALSE will be returned. If the string contains
+ * only modifiers, @accelerator_key will be set to 0 but %TRUE will be
+ * returned.
+ *
+ * The virtual vs. concrete accelerator distinction is a relic of
+ * how the X Window System works; there are modifiers Mod2-Mod5 that
+ * can represent various keyboard keys (numlock, meta, hyper, etc.),
+ * the virtual modifier represents the keyboard key, the concrete
+ * modifier the actual Mod2-Mod5 bits in the key press event.
+ * 
+ * Returns: %TRUE on success.
+ */
+gboolean
+egg_accelerator_parse_virtual (const gchar            *accelerator,
+                               guint                  *accelerator_key,
+                               EggVirtualModifierType *accelerator_mods)
+{
+  guint keyval;
+  GdkModifierType mods;
+  gint len;
+  gboolean bad_keyval;
+  
+  if (accelerator_key)
+    *accelerator_key = 0;
+  if (accelerator_mods)
+    *accelerator_mods = 0;
+
+  g_return_val_if_fail (accelerator != NULL, FALSE);
+
+  bad_keyval = FALSE;
+  
+  keyval = 0;
+  mods = 0;
+  len = strlen (accelerator);
+  while (len)
+    {
+      if (*accelerator == '<')
+       {
+         if (len >= 9 && is_release (accelerator))
+           {
+             accelerator += 9;
+             len -= 9;
+             mods |= EGG_VIRTUAL_RELEASE_MASK;
+           }
+         else if (len >= 9 && is_control (accelerator))
+           {
+             accelerator += 9;
+             len -= 9;
+             mods |= EGG_VIRTUAL_CONTROL_MASK;
+           }
+         else if (len >= 7 && is_shift (accelerator))
+           {
+             accelerator += 7;
+             len -= 7;
+             mods |= EGG_VIRTUAL_SHIFT_MASK;
+           }
+         else if (len >= 6 && is_shft (accelerator))
+           {
+             accelerator += 6;
+             len -= 6;
+             mods |= EGG_VIRTUAL_SHIFT_MASK;
+           }
+         else if (len >= 6 && is_ctrl (accelerator))
+           {
+             accelerator += 6;
+             len -= 6;
+             mods |= EGG_VIRTUAL_CONTROL_MASK;
+           }
+         else if (len >= 6 && is_modx (accelerator))
+           {
+             static const guint mod_vals[] = {
+               EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
+               EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
+             };
+
+             len -= 6;
+             accelerator += 4;
+             mods |= mod_vals[*accelerator - '1'];
+             accelerator += 2;
+           }
+         else if (len >= 5 && is_ctl (accelerator))
+           {
+             accelerator += 5;
+             len -= 5;
+             mods |= EGG_VIRTUAL_CONTROL_MASK;
+           }
+         else if (len >= 5 && is_alt (accelerator))
+           {
+             accelerator += 5;
+             len -= 5;
+             mods |= EGG_VIRTUAL_ALT_MASK;
+           }
+          else if (len >= 6 && is_meta (accelerator))
+           {
+             accelerator += 6;
+             len -= 6;
+             mods |= EGG_VIRTUAL_META_MASK;
+           }
+          else if (len >= 7 && is_hyper (accelerator))
+           {
+             accelerator += 7;
+             len -= 7;
+             mods |= EGG_VIRTUAL_HYPER_MASK;
+           }
+          else if (len >= 7 && is_super (accelerator))
+           {
+             accelerator += 7;
+             len -= 7;
+             mods |= EGG_VIRTUAL_SUPER_MASK;
+           }
+         else
+           {
+             gchar last_ch;
+             
+             last_ch = *accelerator;
+             while (last_ch && last_ch != '>')
+               {
+                 last_ch = *accelerator;
+                 accelerator += 1;
+                 len -= 1;
+               }
+           }
+       }
+      else
+       {
+          keyval = gdk_keyval_from_name (accelerator);
+          
+          if (keyval == 0)
+            bad_keyval = TRUE;
+          
+          accelerator += len;
+          len -= len;              
+       }
+    }
+  
+  if (accelerator_key)
+    *accelerator_key = gdk_keyval_to_lower (keyval);
+  if (accelerator_mods)
+    *accelerator_mods = mods;
+
+  return !bad_keyval;
+}
+
+
+/**
+ * egg_virtual_accelerator_name:
+ * @accelerator_key:  accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ * @returns:          a newly-allocated accelerator name
+ * 
+ * Converts an accelerator keyval and modifier mask
+ * into a string parseable by egg_accelerator_parse_virtual().
+ * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
+ * this function returns "&lt;Control&gt;q".
+ *
+ * The caller of this function must free the returned string.
+ */
+gchar*
+egg_virtual_accelerator_name (guint                  accelerator_key,
+                              EggVirtualModifierType accelerator_mods)
+{
+  static const gchar text_release[] = "<Release>";
+  static const gchar text_shift[] = "<Shift>";
+  static const gchar text_control[] = "<Control>";
+  static const gchar text_mod1[] = "<Alt>";
+  static const gchar text_mod2[] = "<Mod2>";
+  static const gchar text_mod3[] = "<Mod3>";
+  static const gchar text_mod4[] = "<Mod4>";
+  static const gchar text_mod5[] = "<Mod5>";
+  static const gchar text_meta[] = "<Meta>";
+  static const gchar text_super[] = "<Super>";
+  static const gchar text_hyper[] = "<Hyper>";
+  guint l;
+  gchar *keyval_name;
+  gchar *accelerator;
+
+  accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
+
+  keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
+  if (!keyval_name)
+    keyval_name = "";
+
+  l = 0;
+  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
+    l += sizeof (text_release) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
+    l += sizeof (text_shift) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
+    l += sizeof (text_control) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
+    l += sizeof (text_mod1) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
+    l += sizeof (text_mod2) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
+    l += sizeof (text_mod3) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
+    l += sizeof (text_mod4) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
+    l += sizeof (text_mod5) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
+    l += sizeof (text_meta) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
+    l += sizeof (text_hyper) - 1;
+  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
+    l += sizeof (text_super) - 1;
+  l += strlen (keyval_name);
+
+  accelerator = g_new (gchar, l + 1);
+
+  l = 0;
+  accelerator[l] = 0;
+  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
+    {
+      strcpy (accelerator + l, text_release);
+      l += sizeof (text_release) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
+    {
+      strcpy (accelerator + l, text_shift);
+      l += sizeof (text_shift) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
+    {
+      strcpy (accelerator + l, text_control);
+      l += sizeof (text_control) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
+    {
+      strcpy (accelerator + l, text_mod1);
+      l += sizeof (text_mod1) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
+    {
+      strcpy (accelerator + l, text_mod2);
+      l += sizeof (text_mod2) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
+    {
+      strcpy (accelerator + l, text_mod3);
+      l += sizeof (text_mod3) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
+    {
+      strcpy (accelerator + l, text_mod4);
+      l += sizeof (text_mod4) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
+    {
+      strcpy (accelerator + l, text_mod5);
+      l += sizeof (text_mod5) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
+    {
+      strcpy (accelerator + l, text_meta);
+      l += sizeof (text_meta) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
+    {
+      strcpy (accelerator + l, text_hyper);
+      l += sizeof (text_hyper) - 1;
+    }
+  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
+    {
+      strcpy (accelerator + l, text_super);
+      l += sizeof (text_super) - 1;
+    }
+  
+  strcpy (accelerator + l, keyval_name);
+
+  return accelerator;
+}
+
+void
+egg_keymap_resolve_virtual_modifiers (GdkKeymap              *keymap,
+                                      EggVirtualModifierType  virtual_mods,
+                                      GdkModifierType        *concrete_mods)
+{
+  GdkModifierType concrete;
+  int i;
+  const EggModmap *modmap;
+
+  g_return_if_fail (GDK_IS_KEYMAP (keymap));
+  g_return_if_fail (concrete_mods != NULL);
+  
+  modmap = egg_keymap_get_modmap (keymap);
+  
+  /* Not so sure about this algorithm. */
+  
+  concrete = 0;
+  i = 0;
+  while (i < EGG_MODMAP_ENTRY_LAST)
+    {
+      if (modmap->mapping[i] & virtual_mods)
+        concrete |= (1 << i);
+
+      ++i;
+    }
+
+  *concrete_mods = concrete;
+}
+
+void
+egg_keymap_virtualize_modifiers (GdkKeymap              *keymap,
+                                 GdkModifierType         concrete_mods,
+                                 EggVirtualModifierType *virtual_mods)
+{
+  GdkModifierType virtual;
+  int i;
+  const EggModmap *modmap;
+  
+  g_return_if_fail (GDK_IS_KEYMAP (keymap));
+  g_return_if_fail (virtual_mods != NULL);
+
+  modmap = egg_keymap_get_modmap (keymap);
+  
+  /* Not so sure about this algorithm. */
+  
+  virtual = 0;
+  i = 0;
+  while (i < EGG_MODMAP_ENTRY_LAST)
+    {
+      if ((1 << i) & concrete_mods)
+        {
+          EggVirtualModifierType cleaned;
+          
+          cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
+                                           EGG_VIRTUAL_MOD3_MASK |
+                                           EGG_VIRTUAL_MOD4_MASK |
+                                           EGG_VIRTUAL_MOD5_MASK);
+          
+          if (cleaned != 0)
+            {
+              virtual |= cleaned;
+            }
+          else
+            {
+              /* Rather than dropping mod2->mod5 if not bound,
+               * go ahead and use the concrete names
+               */
+              virtual |= modmap->mapping[i];
+            }
+        }
+      
+      ++i;
+    }
+  
+  *virtual_mods = virtual;
+}
+
+static void
+reload_modmap (GdkKeymap *keymap,
+               EggModmap *modmap)
+{
+  XModifierKeymap *xmodmap;
+  int map_size;
+  int i;
+
+  /* FIXME multihead */
+  xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
+
+  memset (modmap->mapping, 0, sizeof (modmap->mapping));
+  
+  /* there are 8 modifiers, and the first 3 are shift, shift lock,
+   * and control
+   */
+  map_size = 8 * xmodmap->max_keypermod;
+  i = 3 * xmodmap->max_keypermod;
+  while (i < map_size)
+    {
+      /* get the key code at this point in the map,
+       * see if its keysym is one we're interested in
+       */
+      int keycode = xmodmap->modifiermap[i];
+      GdkKeymapKey *keys;
+      guint *keyvals;
+      int n_entries;
+      int j;
+      EggVirtualModifierType mask;
+      
+      keys = NULL;
+      keyvals = NULL;
+      n_entries = 0;
+
+      gdk_keymap_get_entries_for_keycode (keymap,
+                                          keycode,
+                                          &keys, &keyvals, &n_entries);
+      
+      mask = 0;
+      j = 0;
+      while (j < n_entries)
+        {          
+          if (keyvals[j] == GDK_Num_Lock)
+            mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
+          else if (keyvals[j] == GDK_Scroll_Lock)
+            mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
+          else if (keyvals[j] == GDK_Meta_L ||
+                   keyvals[j] == GDK_Meta_R)
+            mask |= EGG_VIRTUAL_META_MASK;
+          else if (keyvals[j] == GDK_Hyper_L ||
+                   keyvals[j] == GDK_Hyper_R)
+            mask |= EGG_VIRTUAL_HYPER_MASK;
+          else if (keyvals[j] == GDK_Super_L ||
+                   keyvals[j] == GDK_Super_R)
+            mask |= EGG_VIRTUAL_SUPER_MASK;
+          else if (keyvals[j] == GDK_Mode_switch)
+            mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
+          
+          ++j;
+        }
+
+      /* Mod1Mask is 1 << 3 for example, i.e. the
+       * fourth modifier, i / keyspermod is the modifier
+       * index
+       */      
+      modmap->mapping[i/xmodmap->max_keypermod] |= mask;
+      
+      g_free (keyvals);
+      g_free (keys);      
+      
+      ++i;
+    }
+
+  /* Add in the not-really-virtual fixed entries */
+  modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
+  modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
+  
+  XFreeModifiermap (xmodmap);
+}
+
+const EggModmap*
+egg_keymap_get_modmap (GdkKeymap *keymap)
+{
+  EggModmap *modmap;
+
+  /* This is all a hack, much simpler when we can just
+   * modify GDK directly.
+   */
+  
+  modmap = g_object_get_data (G_OBJECT (keymap),
+                              "egg-modmap");
+
+  if (modmap == NULL)
+    {
+      modmap = g_new0 (EggModmap, 1);
+
+      /* FIXME modify keymap change events with an event filter
+       * and force a reload if we get one
+       */
+      
+      reload_modmap (keymap, modmap);
+      
+      g_object_set_data_full (G_OBJECT (keymap),
+                              "egg-modmap",
+                              modmap,
+                              g_free);
+    }
+
+  g_assert (modmap != NULL);
+  
+  return modmap;
+}
diff --git a/eggaccelerators.h b/eggaccelerators.h
new file mode 100644 (file)
index 0000000..e4df317
--- /dev/null
@@ -0,0 +1,87 @@
+/* eggaccelerators.h
+ * Copyright (C) 2002  Red Hat, Inc.
+ * Developed by Havoc Pennington
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __EGG_ACCELERATORS_H__
+#define __EGG_ACCELERATORS_H__
+
+#include <gtk/gtkaccelgroup.h>
+#include <gdk/gdk.h>
+
+G_BEGIN_DECLS
+
+/* Where a value is also in GdkModifierType we coincide,
+ * otherwise we don't overlap.
+ */
+typedef enum
+{
+  EGG_VIRTUAL_SHIFT_MASK    = 1 << 0,
+  EGG_VIRTUAL_LOCK_MASK            = 1 << 1,
+  EGG_VIRTUAL_CONTROL_MASK  = 1 << 2,
+
+  EGG_VIRTUAL_ALT_MASK      = 1 << 3, /* fixed as Mod1 */
+  
+  EGG_VIRTUAL_MOD2_MASK            = 1 << 4,
+  EGG_VIRTUAL_MOD3_MASK            = 1 << 5,
+  EGG_VIRTUAL_MOD4_MASK            = 1 << 6,
+  EGG_VIRTUAL_MOD5_MASK            = 1 << 7,
+
+#if 0
+  GDK_BUTTON1_MASK  = 1 << 8,
+  GDK_BUTTON2_MASK  = 1 << 9,
+  GDK_BUTTON3_MASK  = 1 << 10,
+  GDK_BUTTON4_MASK  = 1 << 11,
+  GDK_BUTTON5_MASK  = 1 << 12,
+  /* 13, 14 are used by Xkb for the keyboard group */
+#endif
+  
+  EGG_VIRTUAL_META_MASK = 1 << 24,
+  EGG_VIRTUAL_SUPER_MASK = 1 << 25,
+  EGG_VIRTUAL_HYPER_MASK = 1 << 26,
+  EGG_VIRTUAL_MODE_SWITCH_MASK = 1 << 27, 
+  EGG_VIRTUAL_NUM_LOCK_MASK = 1 << 28,
+  EGG_VIRTUAL_SCROLL_LOCK_MASK = 1 << 29,
+
+  /* Also in GdkModifierType */
+  EGG_VIRTUAL_RELEASE_MASK  = 1 << 30,
+
+  /*     28-31 24-27 20-23 16-19 12-15 8-11 4-7 0-3
+   *       7     f     0     0     0    0    f   f
+   */  
+  EGG_VIRTUAL_MODIFIER_MASK = 0x7f0000ff
+
+} EggVirtualModifierType;
+
+gboolean egg_accelerator_parse_virtual        (const gchar            *accelerator,
+                                               guint                  *accelerator_key,
+                                               EggVirtualModifierType *accelerator_mods);
+void     egg_keymap_resolve_virtual_modifiers (GdkKeymap              *keymap,
+                                               EggVirtualModifierType  virtual_mods,
+                                               GdkModifierType        *concrete_mods);
+void     egg_keymap_virtualize_modifiers      (GdkKeymap              *keymap,
+                                               GdkModifierType         concrete_mods,
+                                               EggVirtualModifierType *virtual_mods);
+
+gchar* egg_virtual_accelerator_name (guint                  accelerator_key,
+                                     EggVirtualModifierType accelerator_mods);
+
+G_END_DECLS
+
+
+#endif /* __EGG_ACCELERATORS_H__ */
diff --git a/tomboykeybinder.c b/tomboykeybinder.c
new file mode 100644 (file)
index 0000000..b29e2a1
--- /dev/null
@@ -0,0 +1,322 @@
+#include <string.h>
+
+#include <gdk/gdk.h>
+#include <gdk/gdkwindow.h>
+#include <gdk/gdkx.h>
+#include <X11/Xlib.h>
+
+#include "eggaccelerators.h"
+#include "tomboykeybinder.h"
+
+/* Uncomment the next line to print a debug trace. */
+/* #define DEBUG */
+
+#ifdef DEBUG
+#  define TRACE(x) x
+#else
+#  define TRACE(x) do {} while (FALSE);
+#endif
+
+typedef struct _Binding {
+       TomboyBindkeyHandler  handler;
+       gpointer              user_data;
+       char                 *keystring;
+       uint                  keycode;
+       uint                  modifiers;
+} Binding;
+
+static GSList *bindings = NULL;
+static guint32 last_event_time = 0;
+static gboolean processing_event = FALSE;
+
+static guint num_lock_mask, caps_lock_mask, scroll_lock_mask;
+
+static void
+lookup_ignorable_modifiers (GdkKeymap *keymap)
+{
+       egg_keymap_resolve_virtual_modifiers (keymap, 
+                                             EGG_VIRTUAL_LOCK_MASK,
+                                             &caps_lock_mask);
+
+       egg_keymap_resolve_virtual_modifiers (keymap, 
+                                             EGG_VIRTUAL_NUM_LOCK_MASK,
+                                             &num_lock_mask);
+
+       egg_keymap_resolve_virtual_modifiers (keymap, 
+                                             EGG_VIRTUAL_SCROLL_LOCK_MASK,
+                                             &scroll_lock_mask);
+}
+
+static void
+grab_ungrab_with_ignorable_modifiers (GdkWindow *rootwin, 
+                                     Binding   *binding,
+                                     gboolean   grab)
+{
+       guint mod_masks [] = {
+               0, /* modifier only */
+               num_lock_mask,
+               caps_lock_mask,
+               scroll_lock_mask,
+               num_lock_mask  | caps_lock_mask,
+               num_lock_mask  | scroll_lock_mask,
+               caps_lock_mask | scroll_lock_mask,
+               num_lock_mask  | caps_lock_mask | scroll_lock_mask,
+       };
+       int i;
+
+       for (i = 0; i < G_N_ELEMENTS (mod_masks); i++) {
+               if (grab) {
+                       XGrabKey (GDK_WINDOW_XDISPLAY (rootwin), 
+                                 binding->keycode, 
+                                 binding->modifiers | mod_masks [i], 
+                                 GDK_WINDOW_XWINDOW (rootwin), 
+                                 False, 
+                                 GrabModeAsync,
+                                 GrabModeAsync);
+               } else {
+                       XUngrabKey (GDK_WINDOW_XDISPLAY (rootwin),
+                                   binding->keycode,
+                                   binding->modifiers | mod_masks [i], 
+                                   GDK_WINDOW_XWINDOW (rootwin));
+               }
+       }
+}
+
+static gboolean 
+do_grab_key (Binding *binding)
+{
+       GdkKeymap *keymap = gdk_keymap_get_default ();
+       GdkWindow *rootwin = gdk_get_default_root_window ();
+
+       EggVirtualModifierType virtual_mods = 0;
+       guint keysym = 0;
+
+       if (keymap == NULL || rootwin == NULL)
+               return FALSE;
+
+       if (!egg_accelerator_parse_virtual (binding->keystring, 
+                                           &keysym, 
+                                           &virtual_mods))
+               return FALSE;
+
+       TRACE (g_print ("Got accel %d, %d\n", keysym, virtual_mods));
+
+       binding->keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (rootwin), 
+                                            keysym);
+       if (binding->keycode == 0)
+               return FALSE;
+
+       TRACE (g_print ("Got keycode %d\n", binding->keycode));
+
+       egg_keymap_resolve_virtual_modifiers (keymap,
+                                             virtual_mods,
+                                             &binding->modifiers);
+
+       TRACE (g_print ("Got modmask %d\n", binding->modifiers));
+
+       gdk_error_trap_push ();
+
+       grab_ungrab_with_ignorable_modifiers (rootwin, 
+                                             binding, 
+                                             TRUE /* grab */);
+
+       gdk_flush ();
+
+       if (gdk_error_trap_pop ()) {
+          g_warning ("Binding '%s' failed!\n", binding->keystring);
+          return FALSE;
+       }
+
+       return TRUE;
+}
+
+static gboolean 
+do_ungrab_key (Binding *binding)
+{
+       GdkWindow *rootwin = gdk_get_default_root_window ();
+
+       TRACE (g_print ("Removing grab for '%s'\n", binding->keystring));
+
+       grab_ungrab_with_ignorable_modifiers (rootwin, 
+                                             binding, 
+                                             FALSE /* ungrab */);
+
+       return TRUE;
+}
+
+static GdkFilterReturn
+filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
+{
+       GdkFilterReturn return_val = GDK_FILTER_CONTINUE;
+       XEvent *xevent = (XEvent *) gdk_xevent;
+       guint event_mods;
+       GSList *iter;
+
+       TRACE (g_print ("Got Event! %d, %d\n", xevent->type, event->type));
+
+       switch (xevent->type) {
+       case KeyPress:
+               TRACE (g_print ("Got KeyPress! keycode: %d, modifiers: %d\n", 
+                               xevent->xkey.keycode, 
+                               xevent->xkey.state));
+
+               /* 
+                * Set the last event time for use when showing
+                * windows to avoid anti-focus-stealing code.
+                */
+               processing_event = TRUE;
+               last_event_time = xevent->xkey.time;
+
+               event_mods = xevent->xkey.state & ~(num_lock_mask  | 
+                                                   caps_lock_mask | 
+                                                   scroll_lock_mask);
+
+               for (iter = bindings; iter != NULL; iter = iter->next) {
+                       Binding *binding = (Binding *) iter->data;
+                                                      
+                       if (binding->keycode == xevent->xkey.keycode &&
+                           binding->modifiers == event_mods) {
+
+                               TRACE (g_print ("Calling handler for '%s'...\n", 
+                                               binding->keystring));
+
+                               (binding->handler) (binding->keystring, 
+                                                   binding->user_data);
+                       }
+               }
+
+               processing_event = FALSE;
+               break;
+       case KeyRelease:
+               TRACE (g_print ("Got KeyRelease! \n"));
+               break;
+       }
+
+       return return_val;
+}
+
+static void 
+keymap_changed (GdkKeymap *map)
+{
+       GdkKeymap *keymap = gdk_keymap_get_default ();
+       GSList *iter;
+
+       TRACE (g_print ("Keymap changed! Regrabbing keys..."));
+
+       for (iter = bindings; iter != NULL; iter = iter->next) {
+               Binding *binding = (Binding *) iter->data;
+               do_ungrab_key (binding);
+       }
+
+       lookup_ignorable_modifiers (keymap);
+
+       for (iter = bindings; iter != NULL; iter = iter->next) {
+               Binding *binding = (Binding *) iter->data;
+               do_grab_key (binding);
+       }
+}
+
+void 
+tomboy_keybinder_init (void)
+{
+       GdkKeymap *keymap = gdk_keymap_get_default ();
+       GdkWindow *rootwin = gdk_get_default_root_window ();
+
+       lookup_ignorable_modifiers (keymap);
+
+       gdk_window_add_filter (rootwin, 
+                              filter_func, 
+                              NULL);
+
+       g_signal_connect (keymap, 
+                         "keys_changed",
+                         G_CALLBACK (keymap_changed),
+                         NULL);
+}
+
+gboolean
+tomboy_keybinder_bind (const char           *keystring,
+                      TomboyBindkeyHandler  handler,
+                      gpointer              user_data)
+{
+       Binding *binding;
+       gboolean success;
+
+       binding = g_new0 (Binding, 1);
+       binding->keystring = g_strdup (keystring);
+       binding->handler = handler;
+       binding->user_data = user_data;
+
+       /* Sets the binding's keycode and modifiers */
+       success = do_grab_key (binding);
+
+       if (success) {
+               bindings = g_slist_prepend (bindings, binding);
+       } else {
+               g_free (binding->keystring);
+               g_free (binding);
+       }
+
+       return success;
+}
+
+void
+tomboy_keybinder_unbind (const char           *keystring, 
+                        TomboyBindkeyHandler  handler)
+{
+       GSList *iter;
+
+       for (iter = bindings; iter != NULL; iter = iter->next) {
+               Binding *binding = (Binding *) iter->data;
+
+               if (strcmp (keystring, binding->keystring) != 0 ||
+                   handler != binding->handler) 
+                       continue;
+
+               do_ungrab_key (binding);
+
+               bindings = g_slist_remove (bindings, binding);
+
+               g_free (binding->keystring);
+               g_free (binding);
+               break;
+       }
+}
+
+/* 
+ * From eggcellrenderkeys.c.
+ */
+gboolean
+tomboy_keybinder_is_modifier (guint keycode)
+{
+       gint i;
+       gint map_size;
+       XModifierKeymap *mod_keymap;
+       gboolean retval = FALSE;
+
+       mod_keymap = XGetModifierMapping (gdk_display);
+
+       map_size = 8 * mod_keymap->max_keypermod;
+
+       i = 0;
+       while (i < map_size) {
+               if (keycode == mod_keymap->modifiermap[i]) {
+                       retval = TRUE;
+                       break;
+               }
+               ++i;
+       }
+
+       XFreeModifiermap (mod_keymap);
+
+       return retval;
+}
+
+guint32
+tomboy_keybinder_get_current_event_time (void)
+{
+       if (processing_event) 
+               return last_event_time;
+       else
+               return GDK_CURRENT_TIME;
+}
diff --git a/tomboykeybinder.h b/tomboykeybinder.h
new file mode 100644 (file)
index 0000000..0516eea
--- /dev/null
@@ -0,0 +1,27 @@
+
+#ifndef __TOMBOY_KEY_BINDER_H__
+#define __TOMBOY_KEY_BINDER_H__
+
+#include <glib/gtypes.h>
+
+G_BEGIN_DECLS
+
+typedef void (* TomboyBindkeyHandler) (char *keystring, gpointer user_data);
+
+void tomboy_keybinder_init   (void);
+
+gboolean tomboy_keybinder_bind (const char           *keystring,
+                               TomboyBindkeyHandler  handler,
+                               gpointer              user_data);
+
+void tomboy_keybinder_unbind   (const char           *keystring,
+                               TomboyBindkeyHandler  handler);
+
+gboolean tomboy_keybinder_is_modifier (guint keycode);
+
+guint32 tomboy_keybinder_get_current_event_time (void);
+
+G_END_DECLS
+
+#endif /* __TOMBOY_KEY_BINDER_H__ */
+
diff --git a/tomboyutil.c b/tomboyutil.c
new file mode 100644 (file)
index 0000000..5c46ed1
--- /dev/null
@@ -0,0 +1,144 @@
+
+
+#include <gdk/gdk.h>
+#include <gdk/gdkwindow.h>
+#include <gdk/gdkx.h>
+#include <gtk/gtk.h>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
+#include "tomboykeybinder.h"
+#include "tomboyutil.h"
+
+/* Uncomment the next line to print a debug trace. */
+/* #define DEBUG */
+
+#ifdef DEBUG
+#  define TRACE(x) x
+#else
+#  define TRACE(x) do {} while (FALSE);
+#endif
+
+gint
+tomboy_window_get_workspace (GtkWindow *window)
+{
+       GdkWindow *gdkwin = GTK_WIDGET (window)->window;
+       GdkAtom wm_desktop = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
+       GdkAtom out_type;
+       gint out_format, out_length;
+       gulong *out_val;
+       int workspace;
+
+       if (!gdk_property_get (gdkwin,
+                              wm_desktop,
+                              _GDK_MAKE_ATOM (XA_CARDINAL),
+                              0, G_MAXLONG,
+                              FALSE,
+                              &out_type,
+                              &out_format,
+                              &out_length,
+                              (guchar **) &out_val))
+               return -1;
+
+       workspace = *out_val;
+       g_free (out_val);
+
+       return workspace;
+}
+
+void
+tomboy_window_move_to_current_workspace (GtkWindow *window)
+{
+       GdkWindow *gdkwin = GTK_WIDGET (window)->window;
+       GdkWindow *rootwin = 
+               gdk_screen_get_root_window (gdk_drawable_get_screen (gdkwin));
+
+       GdkAtom current_desktop = 
+               gdk_atom_intern ("_NET_CURRENT_DESKTOP", FALSE);
+       GdkAtom wm_desktop = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
+       GdkAtom out_type;
+       gint out_format, out_length;
+       gulong *out_val;
+       int workspace;
+       XEvent xev;
+
+       if (!gdk_property_get (rootwin,
+                              current_desktop,
+                              _GDK_MAKE_ATOM (XA_CARDINAL),
+                              0, G_MAXLONG,
+                              FALSE,
+                              &out_type,
+                              &out_format,
+                              &out_length,
+                              (guchar **) &out_val))
+               return;
+
+       workspace = *out_val;
+       g_free (out_val);
+
+       TRACE (g_print ("Setting _NET_WM_DESKTOP to: %d\n", workspace));
+
+       xev.xclient.type = ClientMessage;
+       xev.xclient.serial = 0;
+       xev.xclient.send_event = True;
+       xev.xclient.display = GDK_WINDOW_XDISPLAY (gdkwin);
+       xev.xclient.window = GDK_WINDOW_XWINDOW (gdkwin);
+       xev.xclient.message_type = 
+               gdk_x11_atom_to_xatom_for_display(
+                       gdk_drawable_get_display (gdkwin),
+                       wm_desktop);
+       xev.xclient.format = 32;
+       xev.xclient.data.l[0] = workspace;
+       xev.xclient.data.l[1] = 0;
+       xev.xclient.data.l[2] = 0;
+
+       XSendEvent (GDK_WINDOW_XDISPLAY (rootwin),
+                   GDK_WINDOW_XWINDOW (rootwin),
+                   False,
+                   SubstructureRedirectMask | SubstructureNotifyMask,
+                   &xev);
+}
+
+static void
+tomboy_window_override_user_time (GtkWindow *window)
+{
+       guint32 ev_time = gtk_get_current_event_time();
+
+       if (ev_time == 0) {
+               /* 
+                * FIXME: Global keypresses use an event filter on the root
+                * window, which processes events before GDK sees them.
+                */
+               ev_time = tomboy_keybinder_get_current_event_time ();
+       }
+       if (ev_time == 0) {
+               gint ev_mask = gtk_widget_get_events (GTK_WIDGET(window));
+               if (!(ev_mask & GDK_PROPERTY_CHANGE_MASK)) {
+                       gtk_widget_add_events (GTK_WIDGET (window),
+                                              GDK_PROPERTY_CHANGE_MASK);
+               }
+
+               /* 
+                * NOTE: Last resort for D-BUS or other non-interactive
+                *       openings.  Causes roundtrip to server.  Lame. 
+                */
+               ev_time = gdk_x11_get_server_time (GTK_WIDGET(window)->window);
+       }
+
+       TRACE (g_print("Setting _NET_WM_USER_TIME to: %d\n", ev_time));
+       gdk_x11_window_set_user_time (GTK_WIDGET(window)->window, ev_time);
+}
+
+void
+tomboy_window_present_hardcore (GtkWindow *window)
+{
+       if (!GTK_WIDGET_REALIZED (window))
+               gtk_widget_realize (GTK_WIDGET (window));
+       else if (GTK_WIDGET_VISIBLE (window))
+               tomboy_window_move_to_current_workspace (window);
+
+       tomboy_window_override_user_time (window);
+
+       gtk_window_present (window);
+}
+
diff --git a/tomboyutil.h b/tomboyutil.h
new file mode 100644 (file)
index 0000000..939d326
--- /dev/null
@@ -0,0 +1,19 @@
+
+#ifndef __TOMBOY_UTIL_H__
+#define __TOMBOY_UTIL_H__
+
+#include <gdk/gdkpixmap.h>
+#include <gtk/gtkwidget.h>
+#include <gtk/gtkwindow.h>
+
+G_BEGIN_DECLS
+
+gint tomboy_window_get_workspace (GtkWindow *window);
+
+void tomboy_window_move_to_current_workspace (GtkWindow *window);
+
+void tomboy_window_present_hardcore (GtkWindow *window);
+
+G_END_DECLS
+
+#endif /* __TOMBOY_UTIL_H__ */