client-main.c 9.26 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
25
26
#ifdef HAVE_ATSPI
#include <dbus/dbus.h>
#include <atspi/atspi.h>
#endif  /* HAVE_ATSPI */
Daiki Ueno's avatar
Daiki Ueno committed
27
28
29
#ifdef HAVE_IBUS
#include <ibus.h>
#endif  /* HAVE_IBUS */
30
#include <gtk/gtk.h>
Daiki Ueno's avatar
Daiki Ueno committed
31
#include <glib/gi18n.h>
Daiki Ueno's avatar
Daiki Ueno committed
32
#include "eekboard/eekboard-client.h"
33
#include "client.h"
Daiki Ueno's avatar
Daiki Ueno committed
34

35
#define DEFAULT_KEYBOARD "us"
36

37
38
39
40
static gboolean opt_system = FALSE;
static gboolean opt_session = FALSE;
static gchar *opt_address = NULL;

41
42
43
static gboolean opt_focus = FALSE;
static gboolean opt_keystroke = FALSE;

44
45
static gchar *opt_keyboards = NULL;

46
static gboolean opt_fullscreen = FALSE;
Daiki Ueno's avatar
Daiki Ueno committed
47
48

static const GOptionEntry options[] = {
49
50
51
52
53
54
    {"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")},
Daiki Ueno's avatar
Daiki Ueno committed
55
#if ENABLE_FOCUS_LISTENER
56
    {"listen-focus", 'f', 0, G_OPTION_ARG_NONE, &opt_focus,
Daiki Ueno's avatar
Daiki Ueno committed
57
58
59
     N_("Listen focus change events")},
#endif  /* ENABLE_FOCUS_LISTENER */
#ifdef HAVE_ATSPI
60
61
    {"listen-keystroke", 's', 0, G_OPTION_ARG_NONE, &opt_keystroke,
     N_("Listen keystroke events with AT-SPI")},
Daiki Ueno's avatar
Daiki Ueno committed
62
#endif  /* HAVE_ATSPI */
63
64
    {"keyboards", 'k', 0, G_OPTION_ARG_STRING, &opt_keyboards,
     N_("Specify keyboards (comma separated)")},
65
66
    {"fullscreen", 'F', 0, G_OPTION_ARG_NONE, &opt_fullscreen,
     N_("Create window in fullscreen mode")},
Daiki Ueno's avatar
Daiki Ueno committed
67
68
69
70
    {NULL}
};

static void
71
72
on_context_destroyed (EekboardContext *context,
                      gpointer         user_data)
Daiki Ueno's avatar
Daiki Ueno committed
73
{
Daiki Ueno's avatar
Daiki Ueno committed
74
    gtk_main_quit ();
75
76
77
}

static void
Daiki Ueno's avatar
Daiki Ueno committed
78
on_destroyed (EekboardClient *eekboard,
79
80
              gpointer          user_data)
{
Daiki Ueno's avatar
Daiki Ueno committed
81
    gtk_main_quit ();
Daiki Ueno's avatar
Daiki Ueno committed
82
83
}

Daiki Ueno's avatar
Daiki Ueno committed
84
enum FocusListenerType {
Daiki Ueno's avatar
Daiki Ueno committed
85
86
87
88
89
    FOCUS_NONE,
    FOCUS_ATSPI,
    FOCUS_IBUS
};

90
static gboolean
Daiki Ueno's avatar
Daiki Ueno committed
91
92
set_keyboards (Client              *client,
               const gchar * const *keyboards)
93
{
Daiki Ueno's avatar
Daiki Ueno committed
94
    if (g_strv_length ((gchar **)keyboards) == 0) {
Daiki Ueno's avatar
Daiki Ueno committed
95
        if (!client_enable_xkl (client)) {
96
97
98
99
            g_printerr ("Can't register xklavier event listeners\n");
            return FALSE;
        }
    } else {
100
        if (!client_set_keyboards (client, keyboards)) {
Daiki Ueno's avatar
Daiki Ueno committed
101
            gchar *str = g_strjoinv (", ", (gchar **)keyboards);
102
103
            g_printerr ("Can't set keyboards \"%s\"\n", str);
            g_free (str);
104
105
106
107
108
109
            return FALSE;
        }
    }
    return TRUE;
}

Daiki Ueno's avatar
Daiki Ueno committed
110
111
112
int
main (int argc, char **argv)
{
Daiki Ueno's avatar
Daiki Ueno committed
113
114
    Client *client = NULL;
    EekboardClient *eekboard;
115
    EekboardContext *context;
116
    GBusType bus_type;
117
    GDBusConnection *connection;
Daiki Ueno's avatar
Daiki Ueno committed
118
    GError *error;
119
    GOptionContext *option_context;
Daiki Ueno's avatar
Daiki Ueno committed
120
    gint focus;
121
    GSettings *settings = NULL;
122
    gchar **keyboards = NULL;
123
    gint retval = 0;
Daiki Ueno's avatar
Daiki Ueno committed
124

125
126
127
128
    if (!gtk_init_check (&argc, &argv)) {
        g_printerr ("Can't init GTK\n");
        exit (1);
    }
Daiki Ueno's avatar
Daiki Ueno committed
129

Daiki Ueno's avatar
Daiki Ueno committed
130
    eek_init ();
131

132
    option_context = g_option_context_new ("eekboard-desktop-client");
133
134
135
    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
136

137
138
139
140
141
142
143
144
145
    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:
Daiki Ueno's avatar
Daiki Ueno committed
146
147
148
149
150
        error = NULL;
        connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
        if (connection == NULL) {
            g_printerr ("Can't connect to the system bus: %s\n",
                        error->message);
Daiki Ueno's avatar
Daiki Ueno committed
151
            g_error_free (error);
Daiki Ueno's avatar
Daiki Ueno committed
152
153
154
            exit (1);
        }
        break;
155
156
157
158
    case G_BUS_TYPE_SESSION:
        error = NULL;
        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
        if (connection == NULL) {
Daiki Ueno's avatar
Daiki Ueno committed
159
160
            g_printerr ("Can't connect to the session bus: %s\n",
                        error->message);
Daiki Ueno's avatar
Daiki Ueno committed
161
            g_error_free (error);
162
163
164
165
166
167
168
169
170
171
172
            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) {
Daiki Ueno's avatar
Daiki Ueno committed
173
            g_printerr ("Can't connect to the bus at %s: %s\n",
174
175
                        opt_address,
                        error->message);
Daiki Ueno's avatar
Daiki Ueno committed
176
            g_error_free (error);
177
178
179
180
181
182
            exit (1);
        }
        break;
    default:
        g_assert_not_reached ();
        break;
Daiki Ueno's avatar
Daiki Ueno committed
183
184
    }

Daiki Ueno's avatar
Daiki Ueno committed
185
    client = client_new (connection);
186
187
    g_object_unref (connection);

188
189
190
    if (client == NULL) {
        g_printerr ("Can't create a client\n");
        exit (1);
191
192
    }

193
    settings = g_settings_new ("org.fedorahosted.eekboard");
Daiki Ueno's avatar
Daiki Ueno committed
194
195
    focus = FOCUS_NONE;
    if (opt_focus) {
196
197
        gchar *focus_listener = g_settings_get_string (settings,
                                                       "focus-listener");
Daiki Ueno's avatar
Daiki Ueno committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        const struct {
            const gchar *name;
            enum FocusListenerType type;
        } focus_listeners[] = {
#ifdef HAVE_ATSPI
            { "atspi", FOCUS_ATSPI },
#endif
#ifdef HAVE_IBUS
            { "ibus", FOCUS_IBUS },
#endif
            { NULL }
        };
        gint i;

        focus = FOCUS_NONE;
        for (i = 0; focus_listeners[i].name; i++) {
            if (g_strcmp0 (focus_listener, focus_listeners[i].name) == 0)
                focus = focus_listeners[i].type;
        }
        if (focus == FOCUS_NONE) {
218
            g_printerr ("Unknown focus listener \"%s\".  "
219
                        "Try \"atspi\" or \"ibus\"\n", focus_listener);
220
221
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
222
223
224
        }
    }
        
Daiki Ueno's avatar
Daiki Ueno committed
225
#ifdef HAVE_ATSPI
Daiki Ueno's avatar
Daiki Ueno committed
226
    if (focus == FOCUS_ATSPI || opt_keystroke) {
Daiki Ueno's avatar
Daiki Ueno committed
227
228
        GSettings *desktop_settings =
            g_settings_new ("org.gnome.desktop.interface");
229
        gboolean accessibility_enabled =
Daiki Ueno's avatar
Daiki Ueno committed
230
            g_settings_get_boolean (desktop_settings, "toolkit-accessibility");
Daiki Ueno's avatar
Daiki Ueno committed
231
        g_object_unref (desktop_settings);
Daiki Ueno's avatar
Daiki Ueno committed
232

233
        if (accessibility_enabled) {
Daiki Ueno's avatar
Daiki Ueno committed
234
235
            if (atspi_init () != 0) {
                g_printerr ("Can't init AT-SPI 2\n");
236
237
                retval = 1;
                goto out;
238
239
            }

Daiki Ueno's avatar
Daiki Ueno committed
240
            if (focus == FOCUS_ATSPI &&
Daiki Ueno's avatar
Daiki Ueno committed
241
                !client_enable_atspi_focus (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
242
                g_printerr ("Can't register AT-SPI focus change event listeners\n");
243
244
                retval = 1;
                goto out;
245
246
247
            }

            if (opt_keystroke &&
Daiki Ueno's avatar
Daiki Ueno committed
248
                !client_enable_atspi_keystroke (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
249
                g_printerr ("Can't register AT-SPI keystroke event listeners\n");
250
251
                retval = 1;
                goto out;
252
253
254
            }
        } else {
            g_printerr ("Desktop accessibility support is disabled\n");
255
256
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
257
        }
258
    }
Daiki Ueno's avatar
Daiki Ueno committed
259
#endif  /* HAVE_ATSPI */
260

Daiki Ueno's avatar
Daiki Ueno committed
261
262
263
264
#ifdef HAVE_IBUS
    if (focus == FOCUS_IBUS) {
        ibus_init ();

Daiki Ueno's avatar
Daiki Ueno committed
265
        if (!client_enable_ibus_focus (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
266
            g_printerr ("Can't register IBus focus change event listeners\n");
267
268
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
269
270
271
272
        }
    }
#endif  /* HAVE_IBUS */

273
#ifdef HAVE_XTEST
Daiki Ueno's avatar
Daiki Ueno committed
274
    if (!client_enable_xtest (client)) {
275
        g_printerr ("Can't init xtest\n");
276
        g_object_unref (client);
277
        exit (1);
Daiki Ueno's avatar
Daiki Ueno committed
278
    }
279
#endif  /* HAVE_XTEST */
280
281
282
283

    if (!opt_focus) {
        g_object_get (client, "context", &context, NULL);
        g_signal_connect (context, "destroyed",
Daiki Ueno's avatar
Daiki Ueno committed
284
                          G_CALLBACK(on_context_destroyed), NULL);
285
        g_object_unref (context);
Daiki Ueno's avatar
Daiki Ueno committed
286
287
    }

288
289
    if (opt_fullscreen ||
        g_settings_get_boolean (settings, "start-fullscreen")) {
290
291
292
        g_object_get (client, "context", &context, NULL);
        eekboard_context_set_fullscreen (context, TRUE, NULL);
        g_object_unref (context);
Daiki Ueno's avatar
Daiki Ueno committed
293
294
    }

295
296
    g_object_get (client, "eekboard", &eekboard, NULL);
    g_signal_connect (eekboard, "destroyed",
Daiki Ueno's avatar
Daiki Ueno committed
297
                      G_CALLBACK(on_destroyed), NULL);
298
    g_object_unref (eekboard);
299
    
300
    if (opt_keyboards != NULL) {
301
        keyboards = g_strsplit (opt_keyboards, ",", -1);
302
303
304
305
306
307

        if (!set_keyboards (client, (const gchar * const *)keyboards)) {
            g_strfreev (keyboards);
            retval = 1;
            goto out;
        }
308
        g_strfreev (keyboards);
309
    }
Daiki Ueno's avatar
Daiki Ueno committed
310

Daiki Ueno's avatar
Daiki Ueno committed
311
    gtk_main ();
Daiki Ueno's avatar
Daiki Ueno committed
312

313
314
315
316
317
318
319
 out:
    if (client)
        g_object_unref (client);
    if (settings)
        g_object_unref (settings);

    return retval;
Daiki Ueno's avatar
Daiki Ueno committed
320
}