client-main.c 8.96 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>
32
#include "eekboard/eekboard.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
44
static gboolean opt_focus = FALSE;
static gboolean opt_keystroke = FALSE;

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

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

static void
67
68
69
on_notify_keyboard_visible (GObject    *object,
                            GParamSpec *spec,
                            gpointer    user_data)
Daiki Ueno's avatar
Daiki Ueno committed
70
{
71
72
73
74
75
76
77
78
    GMainLoop *loop = user_data;
    gboolean visible;

    g_object_get (object, "keyboard-visible", &visible, NULL);

    /* user explicitly closed the window */
    if (!visible && eekboard_context_is_enabled (EEKBOARD_CONTEXT(object)))
        g_main_loop_quit (loop);
Daiki Ueno's avatar
Daiki Ueno committed
79
80
81
}

static void
82
83
on_context_destroyed (EekboardContext *context,
                      gpointer         user_data)
Daiki Ueno's avatar
Daiki Ueno committed
84
{
85
86
87
88
89
90
91
92
93
94
95
96
    GMainLoop *loop = user_data;

    g_main_loop_quit (loop);
}

static void
on_destroyed (EekboardEekboard *eekboard,
              gpointer          user_data)
{
    GMainLoop *loop = user_data;

    g_main_loop_quit (loop);
Daiki Ueno's avatar
Daiki Ueno committed
97
98
}

Daiki Ueno's avatar
Daiki Ueno committed
99
100
101
102
103
104
enum {
    FOCUS_NONE,
    FOCUS_ATSPI,
    FOCUS_IBUS
};

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
static gboolean
set_keyboard (EekboardClient *client,
              const gchar    *keyboard)
{
    if (g_strcmp0 (keyboard, "system") == 0) {
        if (!eekboard_client_enable_xkl (client)) {
            g_printerr ("Can't register xklavier event listeners\n");
            return FALSE;
        }
    } else {
        if (!eekboard_client_set_keyboard (client, keyboard)) {
            g_printerr ("Can't set keyboard \"%s\"\n", keyboard);
            return FALSE;
        }
    }
    return TRUE;
}

Daiki Ueno's avatar
Daiki Ueno committed
123
124
125
int
main (int argc, char **argv)
{
126
    EekboardClient *client = NULL;
127
128
    EekboardEekboard *eekboard;
    EekboardContext *context;
129
    GBusType bus_type;
130
    GDBusConnection *connection;
Daiki Ueno's avatar
Daiki Ueno committed
131
    GError *error;
132
    GOptionContext *option_context;
133
    GMainLoop *loop = NULL;
Daiki Ueno's avatar
Daiki Ueno committed
134
    gint focus;
135
    GSettings *settings = NULL;
136
    gchar *keyboard;
137
    gint retval = 0;
Daiki Ueno's avatar
Daiki Ueno committed
138

139
140
141
142
    if (!gtk_init_check (&argc, &argv)) {
        g_printerr ("Can't init GTK\n");
        exit (1);
    }
Daiki Ueno's avatar
Daiki Ueno committed
143

Daiki Ueno's avatar
Daiki Ueno committed
144
    eek_init ();
145

146
    option_context = g_option_context_new ("eekboard-desktop-client");
147
148
149
    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
150

151
152
153
154
155
156
157
158
159
160
161
162
163
    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) {
Daiki Ueno's avatar
Daiki Ueno committed
164
            g_printerr ("Can't connect to the bus: %s\n", error->message);
165
166
167
168
169
170
171
172
173
174
175
            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
176
            g_printerr ("Can't connect to the bus at %s: %s\n",
177
178
179
180
181
182
183
184
                        opt_address,
                        error->message);
            exit (1);
        }
        break;
    default:
        g_assert_not_reached ();
        break;
Daiki Ueno's avatar
Daiki Ueno committed
185
186
    }

187
    client = eekboard_client_new (connection);
188
189
    g_object_unref (connection);

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

195
    settings = g_settings_new ("org.fedorahosted.eekboard");
Daiki Ueno's avatar
Daiki Ueno committed
196
197
    focus = FOCUS_NONE;
    if (opt_focus) {
198
199
200
201
        gchar *focus_listener = g_settings_get_string (settings,
                                                       "focus-listener");

        if (g_strcmp0 (focus_listener, "atspi") == 0)
Daiki Ueno's avatar
Daiki Ueno committed
202
            focus = FOCUS_ATSPI;
203
        else if (g_strcmp0 (focus_listener, "ibus") == 0)
Daiki Ueno's avatar
Daiki Ueno committed
204
205
            focus = FOCUS_IBUS;
        else {
206
            g_printerr ("Unknown focus listener \"%s\".  "
207
                        "Try \"atspi\" or \"ibus\"\n", focus_listener);
208
209
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
210
211
212
        }
    }
        
Daiki Ueno's avatar
Daiki Ueno committed
213
#ifdef HAVE_ATSPI
Daiki Ueno's avatar
Daiki Ueno committed
214
    if (focus == FOCUS_ATSPI || opt_keystroke) {
Daiki Ueno's avatar
Daiki Ueno committed
215
216
        GSettings *desktop_settings =
            g_settings_new ("org.gnome.desktop.interface");
217
218
        gboolean accessibility_enabled =
            g_settings_get_boolean (settings, "toolkit-accessibility");
Daiki Ueno's avatar
Daiki Ueno committed
219
        g_object_unref (desktop_settings);
Daiki Ueno's avatar
Daiki Ueno committed
220

221
        if (accessibility_enabled) {
Daiki Ueno's avatar
Daiki Ueno committed
222
223
            if (atspi_init () != 0) {
                g_printerr ("Can't init AT-SPI 2\n");
224
225
                retval = 1;
                goto out;
226
227
            }

Daiki Ueno's avatar
Daiki Ueno committed
228
            if (focus == FOCUS_ATSPI &&
Daiki Ueno's avatar
Daiki Ueno committed
229
                !eekboard_client_enable_atspi_focus (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
230
                g_printerr ("Can't register AT-SPI focus change event listeners\n");
231
232
                retval = 1;
                goto out;
233
234
235
            }

            if (opt_keystroke &&
Daiki Ueno's avatar
Daiki Ueno committed
236
                !eekboard_client_enable_atspi_keystroke (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
237
                g_printerr ("Can't register AT-SPI keystroke event listeners\n");
238
239
                retval = 1;
                goto out;
240
241
242
            }
        } else {
            g_printerr ("Desktop accessibility support is disabled\n");
243
244
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
245
        }
246
    }
Daiki Ueno's avatar
Daiki Ueno committed
247
#endif  /* HAVE_ATSPI */
248

Daiki Ueno's avatar
Daiki Ueno committed
249
250
251
252
#ifdef HAVE_IBUS
    if (focus == FOCUS_IBUS) {
        ibus_init ();

253
        if (!eekboard_client_enable_ibus_focus (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
254
            g_printerr ("Can't register IBus focus change event listeners\n");
255
256
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
257
258
259
260
        }
    }
#endif  /* HAVE_IBUS */

261
262
263
#ifdef HAVE_XTEST
    if (!eekboard_client_enable_xtest (client)) {
        g_printerr ("Can't init xtest\n");
264
        g_object_unref (client);
265
        exit (1);
Daiki Ueno's avatar
Daiki Ueno committed
266
    }
267
#endif  /* HAVE_XTEST */
268
269

    loop = g_main_loop_new (NULL, FALSE);
270

271
272
273
274
275
276
277
    if (!opt_focus) {
        g_object_get (client, "context", &context, NULL);
        g_signal_connect (context, "notify::keyboard-visible",
                          G_CALLBACK(on_notify_keyboard_visible), loop);
        g_signal_connect (context, "destroyed",
                          G_CALLBACK(on_context_destroyed), loop);
        g_object_unref (context);
Daiki Ueno's avatar
Daiki Ueno committed
278
279
    }

280
281
    if (opt_fullscreen ||
        g_settings_get_boolean (settings, "start-fullscreen")) {
282
283
284
        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
285
286
    }

287
288
289
    g_object_get (client, "eekboard", &eekboard, NULL);
    g_signal_connect (eekboard, "destroyed",
                      G_CALLBACK(on_destroyed), loop);
290
291
292
293
294
295
296
297
298
    g_object_unref (eekboard);

    keyboard = g_settings_get_string (settings, "keyboard");
    if (!set_keyboard (client, keyboard)) {
        g_free (keyboard);
        retval = 1;
        goto out;
    }
    g_free (keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
299

300
    g_main_loop_run (loop);
Daiki Ueno's avatar
Daiki Ueno committed
301

302
303
304
305
306
307
308
309
310
 out:
    if (loop)
        g_main_loop_unref (loop);
    if (client)
        g_object_unref (client);
    if (settings)
        g_object_unref (settings);

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