WIP x11.
authorHuang Peng <shawn.p.huang@gmail.com>
Fri, 4 Jul 2008 02:17:33 +0000 (10:17 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Fri, 4 Jul 2008 02:17:33 +0000 (10:17 +0800)
Makefile.am
configure.ac
x11/IMdkit.i [new file with mode: 0644]
x11/Makefile.am [new file with mode: 0644]
x11/test.py [new file with mode: 0644]

index 79b877a..5fac4ae 100644 (file)
@@ -26,6 +26,7 @@ SUBDIRS = \
        engine \
        gtk2 \
        qt4 \
+       x11 \
        icons \
        m4 \
        po \
index e340e6c..7a5f431 100644 (file)
@@ -66,11 +66,15 @@ PKG_CHECK_MODULES(PYGOBJECT2, [
 ])
 
 
-# check gtk & pygtk
+# check gtk, gdk & pygtk
 PKG_CHECK_MODULES(GTK2, [
        gtk+-2.0
 ])
 
+PKG_CHECK_MODULES(GDK2, [
+       gdk-2.0
+])
+
 PKG_CHECK_MODULES(PYGTK2, [
        pygtk-2.0
 ])
@@ -88,12 +92,39 @@ PKG_CHECK_MODULES(QT, [
        Qt >= 4.3.5
 ])
 
+
+# Check for x11
+PKG_CHECK_MODULES(X11, [
+       x11
+])
+
+
+# check swig
+AC_PATH_PROG(SWIG, swig)
+if test x"$SWIG" == x""; then
+        AC_MSG_ERROR([can not find swig])
+fi
+AC_SUBST(SWIG)
+
+
 # check env
 AC_PATH_PROG(ENV, env)
 AC_SUBST(ENV)
 
+
 # check python
 AM_PATH_PYTHON([2.5])
+PYTHON_CONFIG=`type -p python$PYTHON_VERSION-config`
+if test "$PYTHON_CONFIG" != ""; then
+    PYTHON_CFLAGS=`$PYTHON_CONFIG --includes`
+    PYTHON_LIBS=`$PYTHON_CONFIG --libs`
+else
+    PYTHON_CFLAGS=`$PYTHON $srcdir/python-config.py --includes`
+    PYTHON_LIBS=`$PYTHON $srcdir/python-config.py --libs`
+fi
+AC_SUBST(PYTHON_CFLAGS)
+AC_SUBST(PYTHON_LIBS)
+
 
 # define GETTEXT_* variables
 GETTEXT_PACKAGE=ibus
@@ -117,6 +148,8 @@ panel/ibus-panel
 engine/Makefile
 gtk2/Makefile
 qt4/Makefile
+x11/Makefile
+x11/IMdkit/Makefile
 icons/Makefile
 m4/Makefile
 ])
diff --git a/x11/IMdkit.i b/x11/IMdkit.i
new file mode 100644 (file)
index 0000000..68e4ffb
--- /dev/null
@@ -0,0 +1,214 @@
+/* vim:set et ts=4: */
+/* IBus - The Input Bus
+ * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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.
+ */
+
+%module IMdkit
+%{
+#include <X11/Xproto.h>
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+#include <X11/Xutil.h>
+#include <Xi18n.h>
+#include <pygobject.h>
+#include <pygtk/pygtk.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+%}
+
+%init %{
+    pygobject_init (-1, -1, -1);
+    init_pygtk ();
+    /*
+    gint argc = 1;
+    gchar *argv [] = { NULL, NULL};
+    argv[0] = g_strdup (Py_GetProgramName ());
+    g_debug ("1 %s", argv[0]);
+    gdk_init (&argc, (gchar ***)&argv);
+    g_debug ("2");
+    g_debug ("3");
+    */
+%}
+
+%{
+/* define XIM structure */
+struct XIM {
+    struct XIMS *xims;
+    PyObject *window;
+    PyObject *protocol_handler;
+};
+
+static struct XIM * _xim = NULL;
+
+/* define protocol handler */
+static int
+_ims_protocol_handler (XIMS xims, IMProtocol *call_data)
+{
+    PyObject *xim = NULL;
+
+    if (_xim == NULL || _xim->xims != xims)
+        return 0;
+    
+    return 0;
+}
+
+%}
+
+/* define exception */
+%exception {
+    $action;
+    if (PyErr_Occurred ()) {
+        return NULL;
+    }
+}
+
+/* define type maps */
+%typemap (in) PyObject * {
+    $1 = $input;
+}
+
+%typemap (out) PyObject * {
+    $result = $1;
+}
+
+struct XIM {
+    /* define property */
+    %immutable;
+    PyObject *window;
+    %mutable;
+};
+
+%extend XIM {
+    XIM (char *name, char *locale, PyObject *protocol_handler) {
+        struct XIM *self = NULL;
+
+        if (name == NULL) {
+            PyErr_Format (PyExc_TypeError,
+                "Argument 1 of XIM must be a string.");
+            goto failed;
+        }
+        
+        if (locale == NULL) {
+            PyErr_Format (PyExc_TypeError,
+                "Argument 2 of XIM must be a string.");
+            goto failed;
+        }
+
+        if (!PyCallable_Check (protocol_handler)) {
+            PyErr_Format (PyExc_TypeError,
+                "Argument 3 of XIM must be a callable object.");
+            goto failed;
+        }
+        
+        if (_xim != NULL) {
+            PyErr_Format (PyExc_RuntimeError,
+                "XIM can not be created second time.");
+            goto failed;
+            
+        }
+
+        self = g_new (struct XIM, 1);
+
+        Py_INCREF (protocol_handler);
+        self->protocol_handler = protocol_handler;
+
+        XIMStyle ims_styles_overspot [] = {
+            XIMPreeditPosition  | XIMStatusNothing,
+            XIMPreeditNothing   | XIMStatusNothing,
+            XIMPreeditPosition  | XIMStatusCallbacks,
+            XIMPreeditNothing   | XIMStatusCallbacks,
+            0
+        };
+
+        XIMStyle ims_styles_onspot [] = {
+            XIMPreeditPosition  | XIMStatusNothing,
+            XIMPreeditCallbacks | XIMStatusNothing,
+            XIMPreeditNothing   | XIMStatusNothing,
+            XIMPreeditPosition  | XIMStatusCallbacks,
+            XIMPreeditCallbacks | XIMStatusCallbacks,
+            XIMPreeditNothing   | XIMStatusCallbacks,
+            0
+        };
+    
+        XIMEncoding ims_encodings[] = {
+            "COMPOUND_TEXT",
+            0
+        };
+    
+        GdkWindowAttr window_attr = {
+            title :     "xim2gtkim",
+            event_mask :    GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
+            wclass:     GDK_INPUT_OUTPUT,
+            window_type:    GDK_WINDOW_TOPLEVEL,
+            override_redirect: 1,
+        };
+
+        GdkWindow *window = gdk_window_new (NULL, &window_attr, GDK_WA_TITLE);
+        self->window = pygobject_new (window);
+        Py_INCREF (self->window);
+        
+        XIMStyles styles;
+        XIMEncodings encodings;
+
+        styles.count_styles =
+            sizeof (ims_styles_onspot)/sizeof (XIMStyle) - 1;
+        styles.supported_styles = ims_styles_onspot;
+        
+        encodings.count_encodings =
+            sizeof (ims_encodings)/sizeof (XIMEncoding) - 1;
+        encodings.supported_encodings = ims_encodings;
+        
+        self->xims = IMOpenIM (GDK_DISPLAY (),
+            IMModifiers, "Xi18n",
+            IMServerWindow, GDK_WINDOW_XWINDOW (window),
+            IMServerName, name,
+            IMLocale, locale,
+            IMServerTransport, "X/",
+            IMInputStyles, &styles,
+            IMEncodingList, &encodings,
+            IMProtocolHandler, _ims_protocol_handler,
+            IMFilterEventMask, KeyPressMask | KeyReleaseMask,
+            NULL
+            );
+
+        _xim = self;
+        return self;
+
+        failed:
+        if (self) {
+            Py_XDECREF (self->window);
+            Py_XDECREF (self->protocol_handler);
+            g_free (self);
+        }
+        return NULL;
+    }
+
+    ~XIM () {
+        if (self) {
+            if (self->xims) {
+                IMCloseIM (self->xims);
+            }
+            Py_XDECREF (self->window);
+            Py_XDECREF (self->protocol_handler);
+            g_free (self);
+        }
+        _xim = NULL;
+    }
+
+}
+
diff --git a/x11/Makefile.am b/x11/Makefile.am
new file mode 100644 (file)
index 0000000..0f2deb5
--- /dev/null
@@ -0,0 +1,97 @@
+# vim:set noet ts=4:
+#
+# ibus - The Input Bus
+#
+# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+# Boston, MA  02111-1307  USA
+
+SUBDIRS = \
+       IMdkit \
+       $(NULL)
+
+xim_PYTHON = \
+       engine.py \
+       factory.py \
+       main.py \
+       tables.py \
+       test.py \
+       $(NULL)
+
+ximdir = $(datadir)/ibus/xim
+
+IMdkit_DATA = \
+       IMdkit.py \
+       $(NULL)
+
+IMdkit_LTLIBRARIES = _IMdkit.la
+
+IMdkitdir = @pyexecdir@
+
+_IMdkit_la_SOURCES = \
+       $(NULL)
+
+nodist__IMdkit_la_SOURCES = \
+       IMdkit_wrap.c \
+       $(NULL)
+
+_IMdkit_la_CFLAGS = \
+       @X11_CFLAGS@ \
+       @GDK2_CFLAGS@ \
+       @PYGOBJECT2_CFLAGS@ \
+       @PYGTK2_CFLAGS@ \
+       @GTK2_CFLAGS@ \
+       @PYTHON_CFLAGS@ \
+       -IIMdkit \
+       $(NULL)
+
+_IMdkit_la_LDFLAGS = \
+       @X11_LIBS@ \
+       @GDK2_LIBS@ \
+       @PYGOBJECT2_LIBS@ \
+       @PYGTK2_LIBS@ \
+       @GTK2_LIBS@ \
+       @PYTHON_LIBS@ \
+       -rpath $(IMdkitdir) \
+       -avoid-version \
+       -module \
+       IMdkit/libIMdkit.la \
+       $(NULL)
+
+#libexec_SCRIPTS = ibus-xim
+
+IMdkit.py IMdkit_wrap.c: IMdkit.i
+       $(SWIG) -python -I/usr/include -o IMdkit_wrap.c $(srcdir)/IMdkit.i
+
+test: all
+       $(ENV) PYTHONPATH=$(builddir)/.libs python test.py
+
+test-ipython: all
+       $(ENV) PYTHONPATH=$(builddir)/.libs ipython
+
+EXTRA_DIST = \
+       IMdkit.i \
+       ibus-xim.in \
+       $(NULL)
+
+CLEANFILES = \
+       IMdkit.py \
+       IMdkit_wrap.* \
+       *.pyc \
+       $(MULL)
+
+DISTCLEANFILES = \
+       $(MULL)
diff --git a/x11/test.py b/x11/test.py
new file mode 100644 (file)
index 0000000..528fb4c
--- /dev/null
@@ -0,0 +1,8 @@
+import IMdkit
+
+def func ():
+       pass
+
+xim = IMdkit.XIM ("a", "zh", func)
+print xim
+print xim.window