]> Dogcows Code - chaz/openbox/blobdiff - obt/keyboard.c
add interface in obt to create an Input Context for a window
[chaz/openbox] / obt / keyboard.c
index 710a94c23b33ccad97af9421f514214a71b474e8..bd5d5dfd98a5876c0109608d7c2ec674da148bbd 100644 (file)
 #include <X11/Xlib.h>
 #include <X11/keysym.h>
 
+struct _ObtIC
+{
+    guint ref;
+    XIC xic;
+};
+
 /* These masks are constants and the modifier keys are bound to them as
    anyone sees fit:
         ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
@@ -34,6 +40,7 @@
 #define nth_mask(n) (1 << n)
 
 static void set_modkey_mask(guchar mask, KeySym sym);
+static void xim_init(void);
 void obt_keyboard_shutdown();
 
 static XModifierKeymap *modmap;
@@ -49,19 +56,25 @@ static gboolean hyper_l = FALSE;
 
 static gboolean started = FALSE;
 
-void obt_keyboard_reload()
+static XIM xim = NULL;
+static XIMStyle xim_style = 0;
+
+void obt_keyboard_reload(void)
 {
     gint i, j, k;
 
     if (started) obt_keyboard_shutdown(); /* free stuff */
     started = TRUE;
 
+    xim_init();
+
     /* reset the keys to not be bound to any masks */
     for (i = 0; i < OBT_KEYBOARD_NUM_MODKEYS; ++i)
         modkeys_keys[i] = 0;
 
     modmap = XGetModifierMapping(obt_display);
-    g_assert(modmap->max_keypermod > 0);
+    /* note: modmap->max_keypermod can be 0 when there is no valid key layout
+       available */
 
     XDisplayKeycodes(obt_display, &min_keycode, &max_keycode);
     keymap = XGetKeyboardMapping(obt_display, min_keycode,
@@ -97,15 +110,69 @@ void obt_keyboard_reload()
     modkeys_keys[OBT_KEYBOARD_MODKEY_CONTROL] = ControlMask;
 }
 
-void obt_keyboard_shutdown()
+void obt_keyboard_shutdown(void)
 {
     XFreeModifiermap(modmap);
     modmap = NULL;
     XFree(keymap);
     keymap = NULL;
+    if (xim) XCloseIM(xim);
+    xim = NULL;
+    xim_style = 0;
     started = FALSE;
 }
 
+void xim_init(void)
+{
+    gchar *aname, *aclass;
+
+    aname = g_strdup(g_get_prgname());
+    if (!aname) aname = g_strdup("obt");
+
+    aclass = g_strdup(aname);
+    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)
+        g_message("Failed to open an Input Method");
+    else {
+        XIMStyles *xim_styles = NULL;
+        char *r;
+
+        /* get the input method styles */
+        r = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
+        if (r || !xim_styles)
+            g_message("Input Method does not support any styles");
+        if (xim_styles) {
+            int i;
+
+            /* find a style that doesnt need preedit or status */
+            for (i = 0; i < xim_styles->count_styles; ++i) {
+                if (xim_styles->supported_styles[i] == 
+                    (XIMPreeditNothing | XIMStatusNothing))
+                {
+                    xim_style = xim_styles->supported_styles[i];
+                    break;
+                }
+            }
+            XFree(xim_styles);
+        }
+
+        if (!xim_style) {
+            g_message("Input Method does not support a usable style");
+
+            XCloseIM(xim);
+            xim = NULL;
+        }
+    }
+
+    g_free(aclass);
+    g_free(aname);
+}
+
 guint obt_keyboard_keycode_to_modmask(guint keycode)
 {
     gint i, j;
@@ -185,16 +252,24 @@ static void set_modkey_mask(guchar mask, KeySym sym)
     /* CapsLock, Shift, and Control are special and hard-coded */
 }
 
-KeyCode obt_keyboard_keysym_to_keycode(KeySym sym)
+KeyCode* obt_keyboard_keysym_to_keycode(KeySym sym)
 {
-    gint i, j;
+    KeyCode *ret;
+    gint i, j, n;
+
+    ret = g_new(KeyCode, 1);
+    n = 0;
+    ret[n] = 0;
 
     /* go through each keycode and look for the keysym */
     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])
-                return i;
-    return 0;
+            if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j]) {
+                ret = g_renew(KeyCode, ret, ++n);
+                ret[n-1] = i;
+                ret[n] = 0;
+            }
+    return ret;
 }
 
 gchar *obt_keyboard_keycode_to_string(guint keycode)
@@ -222,3 +297,35 @@ gunichar obt_keyboard_keycode_to_unichar(guint keycode)
     g_free(key);
     return unikey;
 }
+
+ObtIC* obt_keyboard_context_new(Window w)
+{
+    ObtIC *ic = NULL;
+
+    if (w != None) {
+        ic = g_new(ObtIC, 1);
+        ic->ref = 1;
+        ic->xic = NULL;
+
+        if (xim)
+            ic->xic = XCreateIC(xim,
+                                XNInputStyle, xim_style,
+                                XNClientWindow, w,
+                                XNFocusWindow, w,
+                                NULL);
+    }
+    return ic;
+}
+
+void obt_keyboard_context_ref(ObtIC *ic)
+{
+    ++ic->ref;
+}
+
+void obt_keyboard_context_unref(ObtIC *ic)
+{
+    if (--ic->ref < 1) {
+        XDestroyIC(ic->xic);
+        g_free(ic);
+    }
+}
This page took 0.025902 seconds and 4 git commands to generate.