From 3b77502b97dd3184c41f9066a6251f9a947c7be1 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Thu, 24 Feb 2011 13:05:02 +0900 Subject: [PATCH] Add Python binding. --- bindings/Makefile.am | 10 ++-- bindings/python/Makefile.am | 18 +++++++ bindings/python/eekboard/Makefile.am | 21 ++++++++ bindings/python/eekboard/__init__.py | 63 ++++++++++++++++++++++++ bindings/python/eekboard/context.py | 73 ++++++++++++++++++++++++++++ bindings/python/eekboard/eekboard.py | 42 ++++++++++++++++ configure.ac | 33 +++++++++++++ 7 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 bindings/python/Makefile.am create mode 100644 bindings/python/eekboard/Makefile.am create mode 100644 bindings/python/eekboard/__init__.py create mode 100644 bindings/python/eekboard/context.py create mode 100644 bindings/python/eekboard/eekboard.py diff --git a/bindings/Makefile.am b/bindings/Makefile.am index e3225c9b..2c76b56a 100644 --- a/bindings/Makefile.am +++ b/bindings/Makefile.am @@ -16,8 +16,12 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 USA -if ENABLE_VALA -SUBDIRS = vala -else SUBDIRS = + +if ENABLE_PYTHON +SUBDIRS += python +endif + +if ENABLE_VALA +SUBDIRS += vala endif diff --git a/bindings/python/Makefile.am b/bindings/python/Makefile.am new file mode 100644 index 00000000..92d1820f --- /dev/null +++ b/bindings/python/Makefile.am @@ -0,0 +1,18 @@ +# Copyright (C) 2011 Daiki Ueno +# Copyright (C) 2011 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program 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 +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# . + +SUBDIRS = eekboard diff --git a/bindings/python/eekboard/Makefile.am b/bindings/python/eekboard/Makefile.am new file mode 100644 index 00000000..d3d1cb23 --- /dev/null +++ b/bindings/python/eekboard/Makefile.am @@ -0,0 +1,21 @@ +# Copyright (C) 2011 Daiki Ueno +# Copyright (C) 2011 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program 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 +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# . + +pkgpython_PYTHON = \ + __init__.py \ + eekboard.py \ + context.py diff --git a/bindings/python/eekboard/__init__.py b/bindings/python/eekboard/__init__.py new file mode 100644 index 00000000..480097c5 --- /dev/null +++ b/bindings/python/eekboard/__init__.py @@ -0,0 +1,63 @@ +# Copyright (C) 2011 Daiki Ueno +# Copyright (C) 2011 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program 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 +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# . + +from gi.repository import Eek, EekXkl, Gio + +from eekboard import Eekboard +from context import Context + +Keyboard = Eek.Keyboard +Section = Eek.Section +Key = Eek.Key +Symbol = Eek.Symbol +Keysym = Eek.Keysym + +MODIFIER_BEHAVIOR_NONE, \ +MODIFIER_BEHAVIOR_LOCK, \ +MODIFIER_BEHAVIOR_LATCH = \ +(Eek.ModifierBehavior.NONE, + Eek.ModifierBehavior.LOCK, + Eek.ModifierBehavior.LATCH) + +CSW = 640 +CSH = 480 + +def XmlKeyboard(path, modifier_behavior=MODIFIER_BEHAVIOR_NONE): + _file = Gio.file_new_for_path(path) + layout = Eek.XmlLayout.new(_file.read()) + keyboard = Eek.Keyboard.new(layout, CSW, CSH) + keyboard.set_modifier_behavior(modifier_behavior) + return keyboard + +def XklKeyboard(modifier_behavior=MODIFIER_BEHAVIOR_NONE): + layout = EekXkl.Layout.new() + keyboard = Eek.Keyboard.new(layout, CSW, CSH) + keyboard.set_modifier_behavior(modifier_behavior) + return keyboard + +__all__ = ['Eekboard', + 'Context', + 'Keyboard', + 'Section', + 'Key', + 'Symbol', + 'Keysym', + 'MODIFIER_BEHAVIOR_NONE', + 'MODIFIER_BEHAVIOR_LOCK', + 'MODIFIER_BEHAVIOR_LATCH', + 'XmlKeyboard', + 'XklKeyboard'] diff --git a/bindings/python/eekboard/context.py b/bindings/python/eekboard/context.py new file mode 100644 index 00000000..92314c88 --- /dev/null +++ b/bindings/python/eekboard/context.py @@ -0,0 +1,73 @@ +# Copyright (C) 2011 Daiki Ueno +# Copyright (C) 2011 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program 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 +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# . + +from gi.repository import Eekboard +import gobject + +class Context(gobject.GObject): + __gtype_name__ = "PYEekboardContext" + __gsignals__ = { + 'enabled': ( + gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()), + 'disabled': ( + gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()), + 'key-pressed': ( + gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_UINT,)), + 'key-released': ( + gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_UINT,)) + } + + def __init__(self, giobject): + super(Context, self).__init__() + import sys + self.__giobject = giobject + self.__giobject.connect('enabled', lambda *args: self.emit('enabled')) + self.__giobject.connect('disabled', lambda *args: self.emit('disabled')) + self.__giobject.connect('key-pressed', lambda *args: self.emit('key-pressed', args[1])) + self.__giobject.connect('key-released', lambda *args: self.emit('key-released', args[1])) + + def get_giobject(self): + return self.__giobject + + def set_keyboard(self, keyboard): + self.__giobject.set_keyboard(keyboard, None) + + def show_keyboard(self): + self.__giobject.show_keyboard(None) + + def hide_keyboard(self): + self.__giobject.show_keyboard(None) + + def set_group(self, group): + self.__giobject.set_group(group, None) + + def press_key(self, keycode): + self.__giobject.press_key(keycode, None) + + def release_key(self, keycode): + self.__giobject.release_key(keycode, None) + + def is_enabled(self): + return self.__giobject.is_enabled() diff --git a/bindings/python/eekboard/eekboard.py b/bindings/python/eekboard/eekboard.py new file mode 100644 index 00000000..2482d390 --- /dev/null +++ b/bindings/python/eekboard/eekboard.py @@ -0,0 +1,42 @@ +# Copyright (C) 2011 Daiki Ueno +# Copyright (C) 2011 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program 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 +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# . + +from gi.repository import Gio +import gi.repository +import gobject +from context import Context + +class Eekboard(gobject.GObject): + __gtype_name__ = "PYEekboardEekboard" + + def __init__(self): + super(Eekboard, self).__init__() + self.__connection = Gio.bus_get_sync(Gio.BusType.SESSION, None) + self.__eekboard = gi.repository.Eekboard.Eekboard.new(self.__connection, None); + + def create_context(self, client_name): + context = self.__eekboard.create_context(client_name, None) + return Context(context) + + def push_context(self, context): + self.__eekboard.push_context(context.get_giobject(), None) + + def pop_context(self): + self.__eekboard.pop_context(None) + + def destroy_context(self, context): + self.__eekboard.destroy_context(context.get_giobject(), None) diff --git a/configure.ac b/configure.ac index 05f031f5..5ef210ee 100644 --- a/configure.ac +++ b/configure.ac @@ -132,6 +132,37 @@ fi AC_MSG_RESULT($enable_cspi) AM_CONDITIONAL(ENABLE_CSPI, [test x$enable_cspi = xyes]) +dnl Python language binding +AC_MSG_CHECKING([whether you enable Python language support]) +AC_ARG_ENABLE(python, + AS_HELP_STRING([--enable-python=no/yes], + [Enable Python language binding default=yes]),, + enable_python=yes) +AC_MSG_RESULT($enable_python) +AM_CONDITIONAL(ENABLE_PYTHON, [test x$enable_python = xyes]) + +if test x"$enable_python" = x"yes"; then + # check python + AM_PATH_PYTHON([2.5]) + AC_PATH_PROG(PYTHON_CONFIG, python$PYTHON_VERSION-config) + if test x"$PYTHON_CONFIG" = x""; then + AC_PATH_PROG(PYTHON_CONFIG, python-config) + fi + if test x"$PYTHON_CONFIG" != x""; 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 + PYTHON_INCLUDES="$PYTHON_CFLAGS" + AC_SUBST(PYTHON_CFLAGS) + AC_SUBST(PYTHON_INCLUDES) + AC_SUBST(PYTHON_LIBS) +else + enable_python="no (disabled, use --enable-python to enable)" +fi + dnl Vala langauge binding AC_MSG_CHECKING([whether you enable Vala language support]) AC_ARG_ENABLE(vala, @@ -195,6 +226,8 @@ eekboard/Makefile src/Makefile tests/Makefile bindings/Makefile +bindings/python/Makefile +bindings/python/eekboard/Makefile bindings/vala/Makefile docs/Makefile docs/reference/Makefile