client-main.c 6.47 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 <stdlib.h>
Daiki Ueno's avatar
Daiki Ueno committed
23
24
#include <glib/gi18n.h>

25
#include "eekboard/eekboard.h"
Daiki Ueno's avatar
Daiki Ueno committed
26

27
28
29
30
static gboolean opt_system = FALSE;
static gboolean opt_session = FALSE;
static gchar *opt_address = NULL;

31
static gchar *opt_set_keyboard = NULL;
32
static gint opt_set_group = -1;
33
34
static gboolean opt_show_keyboard = FALSE;
static gboolean opt_hide_keyboard = FALSE;
Daiki Ueno's avatar
Daiki Ueno committed
35
36
static gint opt_press_key = -1;
static gint opt_release_key = -1;
Daiki Ueno's avatar
Daiki Ueno committed
37
38
39
static gboolean opt_listen = FALSE;

static const GOptionEntry options[] = {
40
41
42
43
44
45
    {"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")},
46
    {"set-keyboard", '\0', 0, G_OPTION_ARG_STRING, &opt_set_keyboard,
Daiki Ueno's avatar
Daiki Ueno committed
47
     N_("Upload keyboard description from an XML file")},
48
    {"set-group", '\0', 0, G_OPTION_ARG_INT, &opt_set_group,
Daiki Ueno's avatar
Daiki Ueno committed
49
     N_("Set group of the keyboard")},
50
    {"show-keyboard", '\0', 0, G_OPTION_ARG_NONE, &opt_show_keyboard,
Daiki Ueno's avatar
Daiki Ueno committed
51
     N_("Show keyboard")},
52
    {"hide-keyboard", '\0', 0, G_OPTION_ARG_NONE, &opt_hide_keyboard,
Daiki Ueno's avatar
Daiki Ueno committed
53
     N_("Hide keyboard")},
Daiki Ueno's avatar
Daiki Ueno committed
54
    {"press-key", '\0', 0, G_OPTION_ARG_INT, &opt_press_key,
Daiki Ueno's avatar
Daiki Ueno committed
55
     N_("Press key")},
Daiki Ueno's avatar
Daiki Ueno committed
56
    {"release-key", '\0', 0, G_OPTION_ARG_INT, &opt_release_key,
Daiki Ueno's avatar
Daiki Ueno committed
57
     N_("Release key")},
Daiki Ueno's avatar
Daiki Ueno committed
58
    {"listen", '\0', 0, G_OPTION_ARG_NONE, &opt_listen,
Daiki Ueno's avatar
Daiki Ueno committed
59
     N_("Listen events")},
Daiki Ueno's avatar
Daiki Ueno committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    {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)
{
78
    EekboardEekboard *eekboard = NULL;
79
    EekboardContext *context = NULL;
80
    GBusType bus_type;
Daiki Ueno's avatar
Daiki Ueno committed
81
82
    GDBusConnection *connection = NULL;
    GError *error;
83
    GOptionContext *option_context;
Daiki Ueno's avatar
Daiki Ueno committed
84
85
86
87
    GMainLoop *loop = NULL;
    gint retval = 0;

    g_type_init ();
88
    g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
Daiki Ueno's avatar
Daiki Ueno committed
89

90
91
92
93
    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);
Daiki Ueno's avatar
Daiki Ueno committed
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
121
122
123
124
125
126
127
128
    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;
Daiki Ueno's avatar
Daiki Ueno committed
129
130
    }

131
    eekboard = eekboard_eekboard_new (connection, NULL);
132
133
    if (eekboard == NULL) {
        g_printerr (_("Can't create eekboard proxy\n"));
Daiki Ueno's avatar
Daiki Ueno committed
134
135
136
137
        retval = 1;
        goto out;
    }

138
    context = eekboard_eekboard_create_context (eekboard,
139
140
141
142
                                                "eekboard-client",
                                                NULL);
    if (context == NULL) {
        g_printerr (_("Can't create context\n"));
143
144
145
146
        retval = 1;
        goto out;
    }

147
    eekboard_eekboard_push_context (eekboard, context, NULL);
148
149

    if (opt_set_keyboard) {
Daiki Ueno's avatar
Daiki Ueno committed
150
151
152
        GFile *file;
        GFileInputStream *input;
        EekLayout *layout;
153
        EekKeyboard *keyboard;
Daiki Ueno's avatar
Daiki Ueno committed
154

155
        file = g_file_new_for_path (opt_set_keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
156
157
158
159

        error = NULL;
        input = g_file_read (file, NULL, &error);
        if (error) {
160
            g_printerr (_("Can't read file %s: %s\n"),
161
                        opt_set_keyboard, error->message);
Daiki Ueno's avatar
Daiki Ueno committed
162
163
164
165
166
167
            retval = 1;
            goto out;
        }

        layout = eek_xml_layout_new (G_INPUT_STREAM(input));
        g_object_unref (input);
168
        keyboard = eek_keyboard_new (layout, 640, 480);
Daiki Ueno's avatar
Daiki Ueno committed
169
        g_object_unref (layout);
170
171
172

        eekboard_context_set_keyboard (context, keyboard, NULL);
        g_object_unref (keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
173
174
    }

175
    if (opt_set_group >= 0) {
176
        eekboard_context_set_group (context, opt_set_group, NULL);
177
178
    }

179
180
    if (opt_show_keyboard) {
        eekboard_context_show_keyboard (context, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
181
182
    }

183
184
    if (opt_hide_keyboard) {
        eekboard_context_hide_keyboard (context, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
185
186
    }

Daiki Ueno's avatar
Daiki Ueno committed
187
    if (opt_press_key >= 0) {
188
        eekboard_context_press_key (context, opt_press_key, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
189
190
191
    }

    if (opt_release_key >= 0) {
192
        eekboard_context_release_key (context, opt_release_key, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
193
194
    }

Daiki Ueno's avatar
Daiki Ueno committed
195
    if (opt_listen) {
196
        g_signal_connect (context, "key-pressed",
Daiki Ueno's avatar
Daiki Ueno committed
197
                          G_CALLBACK(on_key_pressed), NULL);
198
        g_signal_connect (context, "key-released",
Daiki Ueno's avatar
Daiki Ueno committed
199
200
201
202
203
204
                          G_CALLBACK(on_key_released), NULL);
        loop = g_main_loop_new (NULL, FALSE);
        g_main_loop_run (loop);
    }

 out:
205
206
    if (context)
        g_object_unref (context);
Daiki Ueno's avatar
Daiki Ueno committed
207
208
209
210
211
212
213
214
    if (connection)
        g_object_unref (connection);
    if (loop)
        g_main_loop_unref (loop);

    return retval;
}