Fix compile failure when X_HAVE_UTF8_STRING is not defined
[dana/openbox.git] / obt / keyboard.c
index 82161e5..60caf0e 100644 (file)
@@ -49,7 +49,7 @@ void obt_keyboard_context_renew(ObtIC *ic);
 static XModifierKeymap *modmap;
 static KeySym *keymap;
 static gint min_keycode, max_keycode, keysyms_per_keycode;
-/* This is a bitmask of the different masks for each modifier key */
+/*! This is a bitmask of the different masks for each modifier key */
 static guchar modkeys_keys[OBT_KEYBOARD_NUM_MODKEYS];
 
 static gboolean alt_l = FALSE;
@@ -116,10 +116,19 @@ void obt_keyboard_reload(void)
 
 void obt_keyboard_shutdown(void)
 {
+    GSList *it;
+
     XFreeModifiermap(modmap);
     modmap = NULL;
     XFree(keymap);
     keymap = NULL;
+    for (it = xic_all; it; it = g_slist_next(it)) {
+        ObtIC* ic = it->data;
+        if (ic->xic) {
+            XDestroyIC(ic->xic);
+            ic->xic = NULL;
+        }
+    }
     if (xim) XCloseIM(xim);
     xim = NULL;
     xim_style = 0;
@@ -138,7 +147,6 @@ void xim_init(void)
     if (g_ascii_islower(aclass[0]))
         aclass[0] = g_ascii_toupper(aclass[0]);
 
-    g_print("Opening Input Method for %s %s\n", aname, aclass);
     xim = XOpenIM(obt_display, NULL, aname, aclass);
 
     if (!xim)
@@ -182,23 +190,20 @@ void xim_init(void)
     g_free(aname);
 }
 
-guint obt_keyboard_keycode_to_modmask(guint keycode)
+guint obt_keyboard_keyevent_to_modmask(XEvent *e)
 {
-    gint i, j;
-    guint mask = 0;
+    gint i, masknum;
 
-    if (keycode == NoSymbol) return 0;
+    g_return_val_if_fail(e->type == KeyPress || e->type == KeyRelease,
+                         OBT_KEYBOARD_MODKEY_NONE);
 
-    /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
-    for (i = 0; i < NUM_MASKS; ++i) {
-        /* go through each keycode that is bound to the mask */
-        for (j = 0; j < modmap->max_keypermod; ++j) {
-            /* compare with a keycode that is bound to the mask (i) */
-            if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
-                mask |= nth_mask(i);
+    for (masknum = 0; masknum < NUM_MASKS; ++masknum)
+        for (i = 0; i < modmap->max_keypermod; ++i) {
+            KeyCode c = modmap->modifiermap[masknum*modmap->max_keypermod + i];
+            if (c == e->xkey.keycode)
+                return 1<<masknum;
         }
-    }
-    return mask;
+    return 0;
 }
 
 guint obt_keyboard_only_modmasks(guint mask)
@@ -274,14 +279,14 @@ KeyCode* obt_keyboard_keysym_to_keycode(KeySym sym)
     for (i = min_keycode; i <= max_keycode; ++i)
         for (j = 0; j < keysyms_per_keycode; ++j)
             if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j]) {
-                ret = g_renew(KeyCode, ret, ++n);
+                ret = g_renew(KeyCode, ret, ++n + 1);
                 ret[n-1] = i;
                 ret[n] = 0;
             }
     return ret;
 }
 
-gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
+gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XEvent *ev)
 {
     gunichar unikey = 0;
     KeySym sym;
@@ -290,6 +295,8 @@ gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
     gint len, bufsz;
     gboolean got_string = FALSE;
 
+    g_return_val_if_fail(ev->type == KeyPress, 0);
+
     if (!ic)
         g_warning("Using obt_keyboard_keypress_to_unichar() without an "
                   "Input Context.  No i18n support!");
@@ -299,9 +306,9 @@ gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
         bufsz = sizeof(fixbuf);
 
 #ifdef X_HAVE_UTF8_STRING
-        len = Xutf8LookupString(ic->xic, ev, buf, bufsz, &sym, &status);
+        len = Xutf8LookupString(ic->xic, &ev->xkey, buf, bufsz, &sym, &status);
 #else
-        len = XmbLookupString(ic->xic, ev, buf, bufsz, &sym, &status);
+        len = XmbLookupString(ic->xic, &ev->xkey, buf, bufsz, &sym, &status);
 #endif
 
         if (status == XBufferOverflow) {
@@ -309,9 +316,11 @@ gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
             bufsz = len;
 
 #ifdef X_HAVE_UTF8_STRING
-            len = Xutf8LookupString(ic->xic, ev, buf, bufsz, &sym, &status);
+            len = Xutf8LookupString(ic->xic, &ev->xkey, buf, bufsz, &sym,
+                                    &status);
 #else
-            len = XmbLookupString(ic->xic, ev, buf, bufsz, &sym, &status);
+            len = XmbLookupString(ic->xic, &ev->xkey, buf, bufsz, &sym,
+                                  &status);
 #endif
         }
 
@@ -320,13 +329,16 @@ gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
 #ifndef X_HAVE_UTF8_STRING
                 /* convert to utf8 */
                 gchar *buf2 = buf;
-                buf = g_locale_to_utf8(buf2, r, NULL, NULL, NULL);
+                buf = g_locale_to_utf8(buf2, len, NULL, NULL, NULL);
                 g_free(buf2);
 #endif
 
                 got_string = TRUE;
             }
         }
+        else if (status == XLookupKeySym)
+            /* this key doesn't have a text representation, it is a command
+               key of some sort */;
         else
             g_message("Bad keycode lookup. Keysym 0x%x Status: %s\n",
                       (guint) sym,
@@ -338,7 +350,7 @@ gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
     else {
         buf = fixbuf;
         bufsz = sizeof(fixbuf);
-        len = XLookupString(ev, buf, bufsz, &sym, NULL);
+        len = XLookupString(&ev->xkey, buf, bufsz, &sym, NULL);
         if ((guchar)buf[0] >= 32) /* not an ascii control character */
             got_string = TRUE;
     }
@@ -354,13 +366,19 @@ gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XKeyPressedEvent *ev)
     return unikey;
 }
 
-void obt_keyboard_context_renew(ObtIC *ic)
+KeySym obt_keyboard_keypress_to_keysym(XEvent *ev)
 {
-    if (ic->xic) {
-        XDestroyIC(ic->xic);
-        ic->xic = NULL;
-    }
+    KeySym sym;
+
+    g_return_val_if_fail(ev->type == KeyPress, None);
 
+    sym = None;
+    XLookupString(&ev->xkey, NULL, 0, &sym, NULL);
+    return sym;
+}
+
+void obt_keyboard_context_renew(ObtIC *ic)
+{
     if (xim) {
         ic->xic = XCreateIC(xim,
                             XNInputStyle, xim_style,
@@ -379,7 +397,7 @@ ObtIC* obt_keyboard_context_new(Window client, Window focus)
 
     g_return_val_if_fail(client != None && focus != None, NULL);
 
-    ic = g_new(ObtIC, 1);
+    ic = g_slice_new(ObtIC);
     ic->ref = 1;
     ic->client = client;
     ic->focus = focus;
@@ -400,7 +418,8 @@ void obt_keyboard_context_unref(ObtIC *ic)
 {
     if (--ic->ref < 1) {
         xic_all = g_slist_remove(xic_all, ic);
-        XDestroyIC(ic->xic);
-        g_free(ic);
+        if (ic->xic)
+            XDestroyIC(ic->xic);
+        g_slice_free(ObtIC, ic);
     }
 }