client-main.c 4.58 KB
Newer Older
Daiki Ueno's avatar
Daiki Ueno committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 
 * 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 */

22
#include "eekboard/eekboard.h"
Daiki Ueno's avatar
Daiki Ueno committed
23
24

static gchar *opt_set_keyboard = NULL;
25
static gint opt_set_group = -1;
Daiki Ueno's avatar
Daiki Ueno committed
26
27
static gboolean opt_show = FALSE;
static gboolean opt_hide = FALSE;
Daiki Ueno's avatar
Daiki Ueno committed
28
29
static gint opt_press_key = -1;
static gint opt_release_key = -1;
Daiki Ueno's avatar
Daiki Ueno committed
30
31
32
33
34
static gboolean opt_listen = FALSE;

static const GOptionEntry options[] = {
    {"set-keyboard", '\0', 0, G_OPTION_ARG_STRING, &opt_set_keyboard,
     "Set keyboard from an XML file"},
35
36
    {"set-group", '\0', 0, G_OPTION_ARG_INT, &opt_set_group,
     "Set group of the keyboard"},
Daiki Ueno's avatar
Daiki Ueno committed
37
38
39
40
    {"show", '\0', 0, G_OPTION_ARG_NONE, &opt_show,
     "Show keyboard"},
    {"hide", '\0', 0, G_OPTION_ARG_NONE, &opt_hide,
     "Hide keyboard"},
Daiki Ueno's avatar
Daiki Ueno committed
41
42
43
44
    {"press-key", '\0', 0, G_OPTION_ARG_INT, &opt_press_key,
     "Press key"},
    {"release-key", '\0', 0, G_OPTION_ARG_INT, &opt_release_key,
     "Release key"},
Daiki Ueno's avatar
Daiki Ueno committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    {"listen", '\0', 0, G_OPTION_ARG_NONE, &opt_listen,
     "Listen events"},
    {NULL}
};

static void
on_key_pressed (guint keycode, gpointer user_data)
{
    g_print ("KeyPressed %u\n", keycode);
}

static void
on_key_released (guint keycode, gpointer user_data)
{
    g_print ("KeyReleased %u\n", keycode);
}

int
main (int argc, char **argv)
{
65
    EekboardDevice *device = NULL;
Daiki Ueno's avatar
Daiki Ueno committed
66
67
68
69
70
71
72
    GDBusConnection *connection = NULL;
    GError *error;
    GOptionContext *context;
    GMainLoop *loop = NULL;
    gint retval = 0;

    g_type_init ();
73
    g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
Daiki Ueno's avatar
Daiki Ueno committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    context = g_option_context_new ("eekboard-client");
    g_option_context_add_main_entries (context, options, NULL);
    g_option_context_parse (context, &argc, &argv, NULL);
    g_option_context_free (context);

    error = NULL;
    connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
    if (error) {
        g_printerr ("%s\n", error->message);
        retval = 1;
        goto out;
    }

    error = NULL;
89
90
91
92
    device = eekboard_device_new ("/com/redhat/eekboard/Device",
                                  connection,
                                  NULL,
                                  &error);
Daiki Ueno's avatar
Daiki Ueno committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    if (error) {
        g_printerr ("%s\n", error->message);
        retval = 1;
        goto out;
    }

    if (opt_set_keyboard) {
        GFile *file;
        GFileInputStream *input;
        EekLayout *layout;
        EekKeyboard *keyboard;
        GError *error;

        file = g_file_new_for_path (opt_set_keyboard);

        error = NULL;
        input = g_file_read (file, NULL, &error);
        if (error) {
            g_printerr ("Can't read file %s: %s\n",
                        opt_set_keyboard, error->message);
            retval = 1;
            goto out;
        }

        layout = eek_xml_layout_new (G_INPUT_STREAM(input));
        g_object_unref (input);
        keyboard = eek_keyboard_new (layout, 640, 480);
        g_object_unref (layout);
121
        eekboard_device_set_keyboard (device, keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
122
123
124
        g_object_unref (keyboard);
    }

125
    if (opt_set_group >= 0) {
126
        eekboard_device_set_group (device, opt_set_group);
127
128
    }

Daiki Ueno's avatar
Daiki Ueno committed
129
    if (opt_show) {
130
        eekboard_device_show (device);
Daiki Ueno's avatar
Daiki Ueno committed
131
132
133
    }

    if (opt_hide) {
134
        eekboard_device_hide (device);
Daiki Ueno's avatar
Daiki Ueno committed
135
136
    }

Daiki Ueno's avatar
Daiki Ueno committed
137
    if (opt_press_key >= 0) {
138
        eekboard_device_press_key (device, opt_press_key);
Daiki Ueno's avatar
Daiki Ueno committed
139
140
141
    }

    if (opt_release_key >= 0) {
142
        eekboard_device_release_key (device, opt_release_key);
Daiki Ueno's avatar
Daiki Ueno committed
143
144
    }

Daiki Ueno's avatar
Daiki Ueno committed
145
    if (opt_listen) {
146
        g_signal_connect (device, "key-pressed",
Daiki Ueno's avatar
Daiki Ueno committed
147
                          G_CALLBACK(on_key_pressed), NULL);
148
        g_signal_connect (device, "key-released",
Daiki Ueno's avatar
Daiki Ueno committed
149
150
151
152
153
154
                          G_CALLBACK(on_key_released), NULL);
        loop = g_main_loop_new (NULL, FALSE);
        g_main_loop_run (loop);
    }

 out:
155
156
    if (device)
        g_object_unref (device);
Daiki Ueno's avatar
Daiki Ueno committed
157
158
159
160
161
162
163
164
    if (connection)
        g_object_unref (connection);
    if (loop)
        g_main_loop_unref (loop);

    return retval;
}