Rewrite simple-client in python.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -48,7 +48,6 @@ tests/eek-xkb-test
|
||||
tests/eek-xml-test
|
||||
src/eekboard
|
||||
src/eekboard-server
|
||||
src/eekboard-xml
|
||||
docs/reference/eek/*.stamp
|
||||
docs/reference/eek/*.txt
|
||||
docs/reference/eek/eek.types
|
||||
@ -80,5 +79,3 @@ py-compile
|
||||
data/org.fedorahosted.eekboard.gschema.xml
|
||||
data/eekboard-server.service
|
||||
data/*.desktop
|
||||
examples/eekboard-inscript/eekboard-inscript
|
||||
examples/simple-client/simple-client
|
||||
|
||||
@ -1,12 +1 @@
|
||||
noinst_PROGRAMS = simple-client
|
||||
|
||||
simple_client_CFLAGS = \
|
||||
-I$(top_srcdir) \
|
||||
$(GIO2_CFLAGS)
|
||||
|
||||
simple_client_LDADD = \
|
||||
$(top_builddir)/eekboard/libeekboard.la \
|
||||
$(top_builddir)/eek/libeek.la \
|
||||
$(GIO2_LIBS)
|
||||
|
||||
simple_client_SOURCES = main.c
|
||||
EXTRA_DIST = simple-client
|
||||
|
||||
@ -1,198 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2011 Daiki Ueno <ueno@unixuser.org>
|
||||
* Copyright (C) 2010-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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <glib/gi18n.h>
|
||||
|
||||
#include "eekboard/eekboard-client.h"
|
||||
|
||||
static gboolean opt_system = FALSE;
|
||||
static gboolean opt_session = FALSE;
|
||||
static gchar *opt_address = NULL;
|
||||
|
||||
static gchar *opt_set_keyboard = NULL;
|
||||
static gint opt_set_group = -1;
|
||||
static gboolean opt_show_keyboard = FALSE;
|
||||
static gboolean opt_hide_keyboard = FALSE;
|
||||
static gint opt_press_key = -1;
|
||||
static gint opt_release_key = -1;
|
||||
static gboolean opt_listen = FALSE;
|
||||
|
||||
static const GOptionEntry options[] = {
|
||||
{"system", 'y', 0, G_OPTION_ARG_NONE, &opt_system,
|
||||
N_("Connect to the system bus")},
|
||||
{"session", 'e', 0, G_OPTION_ARG_NONE, &opt_session,
|
||||
N_("Connect to the session bus")},
|
||||
{"address", 'a', 0, G_OPTION_ARG_STRING, &opt_address,
|
||||
N_("Connect to the given D-Bus address")},
|
||||
{"set-keyboard", '\0', 0, G_OPTION_ARG_STRING, &opt_set_keyboard,
|
||||
N_("Upload keyboard description from an XML file")},
|
||||
{"set-group", '\0', 0, G_OPTION_ARG_INT, &opt_set_group,
|
||||
N_("Set group of the keyboard")},
|
||||
{"show-keyboard", '\0', 0, G_OPTION_ARG_NONE, &opt_show_keyboard,
|
||||
N_("Show keyboard")},
|
||||
{"hide-keyboard", '\0', 0, G_OPTION_ARG_NONE, &opt_hide_keyboard,
|
||||
N_("Hide keyboard")},
|
||||
{"press-key", '\0', 0, G_OPTION_ARG_INT, &opt_press_key,
|
||||
N_("Press key")},
|
||||
{"release-key", '\0', 0, G_OPTION_ARG_INT, &opt_release_key,
|
||||
N_("Release key")},
|
||||
{"listen", '\0', 0, G_OPTION_ARG_NONE, &opt_listen,
|
||||
N_("Listen events")},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
static void
|
||||
on_key_pressed (EekboardContext *context,
|
||||
const gchar *keyname,
|
||||
EekSymbol *symbol,
|
||||
guint modifiers,
|
||||
gpointer user_data)
|
||||
{
|
||||
g_print ("KeyPressed %s %s\n", keyname, eek_symbol_get_name (symbol));
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
EekboardClient *eekboard = NULL;
|
||||
EekboardContext *context = NULL;
|
||||
GBusType bus_type;
|
||||
GDBusConnection *connection = NULL;
|
||||
GError *error;
|
||||
GOptionContext *option_context;
|
||||
GMainLoop *loop = NULL;
|
||||
gint retval = 0;
|
||||
|
||||
eek_init ();
|
||||
g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
|
||||
|
||||
option_context = g_option_context_new ("eekboard-client");
|
||||
g_option_context_add_main_entries (option_context, options, NULL);
|
||||
g_option_context_parse (option_context, &argc, &argv, NULL);
|
||||
g_option_context_free (option_context);
|
||||
|
||||
if (opt_system)
|
||||
bus_type = G_BUS_TYPE_SYSTEM;
|
||||
else if (opt_address)
|
||||
bus_type = G_BUS_TYPE_NONE;
|
||||
else
|
||||
bus_type = G_BUS_TYPE_SESSION;
|
||||
|
||||
switch (bus_type) {
|
||||
case G_BUS_TYPE_SYSTEM:
|
||||
case G_BUS_TYPE_SESSION:
|
||||
error = NULL;
|
||||
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
|
||||
if (connection == NULL) {
|
||||
g_printerr ("Can't connect to the bus: %s\n", error->message);
|
||||
exit (1);
|
||||
}
|
||||
break;
|
||||
case G_BUS_TYPE_NONE:
|
||||
error = NULL;
|
||||
connection = g_dbus_connection_new_for_address_sync (opt_address,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
&error);
|
||||
if (connection == NULL) {
|
||||
g_printerr ("Can't connect to the bus at %s: %s\n",
|
||||
opt_address,
|
||||
error->message);
|
||||
exit (1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
break;
|
||||
}
|
||||
|
||||
eekboard = eekboard_client_new (connection, NULL);
|
||||
if (eekboard == NULL) {
|
||||
g_printerr ("Can't create eekboard proxy\n");
|
||||
retval = 1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
context = eekboard_client_create_context (eekboard,
|
||||
"eekboard-client",
|
||||
NULL);
|
||||
if (context == NULL) {
|
||||
g_printerr ("Can't create context\n");
|
||||
retval = 1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
eekboard_client_push_context (eekboard, context, NULL);
|
||||
|
||||
if (opt_set_keyboard) {
|
||||
guint keyboard_id;
|
||||
|
||||
keyboard_id = eekboard_context_add_keyboard (context,
|
||||
opt_set_keyboard,
|
||||
NULL);
|
||||
if (keyboard_id == 0) {
|
||||
g_printerr ("Can't create keyboard\n");
|
||||
retval = 1;
|
||||
goto out;
|
||||
}
|
||||
eekboard_context_set_keyboard (context, keyboard_id, NULL);
|
||||
}
|
||||
|
||||
if (opt_set_group >= 0) {
|
||||
eekboard_context_set_group (context, opt_set_group, NULL);
|
||||
}
|
||||
|
||||
if (opt_show_keyboard) {
|
||||
eekboard_context_show_keyboard (context, NULL);
|
||||
}
|
||||
|
||||
if (opt_hide_keyboard) {
|
||||
eekboard_context_hide_keyboard (context, NULL);
|
||||
}
|
||||
|
||||
if (opt_press_key >= 0) {
|
||||
eekboard_context_press_keycode (context, opt_press_key, NULL);
|
||||
}
|
||||
|
||||
if (opt_release_key >= 0) {
|
||||
eekboard_context_release_keycode (context, opt_release_key, NULL);
|
||||
}
|
||||
|
||||
if (opt_listen) {
|
||||
g_signal_connect (context, "key-pressed",
|
||||
G_CALLBACK(on_key_pressed), NULL);
|
||||
loop = g_main_loop_new (NULL, FALSE);
|
||||
g_main_loop_run (loop);
|
||||
}
|
||||
|
||||
out:
|
||||
if (context)
|
||||
g_object_unref (context);
|
||||
if (connection)
|
||||
g_object_unref (connection);
|
||||
if (loop)
|
||||
g_main_loop_unref (loop);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
47
examples/simple-client/simple-client
Executable file
47
examples/simple-client/simple-client
Executable file
@ -0,0 +1,47 @@
|
||||
#!/usr/bin/python
|
||||
# Copyright (C) 2011 Daiki Ueno <ueno@unixuser.org>
|
||||
# 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
|
||||
# <http://www.gnu.org/licenses/>.
|
||||
|
||||
import eekboard
|
||||
import glib
|
||||
|
||||
class SimpleClient(object):
|
||||
def __init__(self):
|
||||
client = eekboard.Client()
|
||||
self.__context = client.create_context('simple-client')
|
||||
client.push_context(self.__context)
|
||||
keyboard_id = self.__context.add_keyboard('us')
|
||||
self.__context.set_keyboard(keyboard_id)
|
||||
|
||||
def __key_pressed_cb(self, c, keyname, symbol, modifiers):
|
||||
print (keyname, symbol, modifiers)
|
||||
|
||||
def __notify_visible_cb(self, c, p, mainloop):
|
||||
if not c.props.visible:
|
||||
mainloop.quit()
|
||||
|
||||
def run(self):
|
||||
mainloop = glib.MainLoop()
|
||||
self.__context.connect('key-pressed', self.__key_pressed_cb)
|
||||
self.__context.connect('notify::visible', self.__notify_visible_cb,
|
||||
mainloop)
|
||||
self.__context.show_keyboard();
|
||||
mainloop.run()
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = SimpleClient()
|
||||
client.run()
|
||||
Reference in New Issue
Block a user