client-main.c 9.56 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
36
#define DEFAULT_LAYOUT "us-qwerty"

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

41
static gboolean opt_use_system_layout = FALSE;
42
static gboolean opt_focus = FALSE;
43
static gchar *opt_focus_listener = NULL;
44
45
static gboolean opt_keystroke = FALSE;

Daiki Ueno's avatar
Daiki Ueno committed
46
47
static gchar *opt_keyboard = NULL;

48
49
50
51
52
static gchar *opt_model = NULL;
static gchar *opt_layouts = NULL;
static gchar *opt_options = NULL;

static gboolean opt_fullscreen = FALSE;
Daiki Ueno's avatar
Daiki Ueno committed
53
54

static const GOptionEntry options[] = {
55
56
57
58
59
60
    {"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")},
61
62
    {"use-system-layout", 'x', 0, G_OPTION_ARG_NONE, &opt_use_system_layout,
     N_("Use system keyboard layout")},
Daiki Ueno's avatar
Daiki Ueno committed
63
#if ENABLE_FOCUS_LISTENER
64
    {"listen-focus", 'f', 0, G_OPTION_ARG_NONE, &opt_focus,
Daiki Ueno's avatar
Daiki Ueno committed
65
     N_("Listen focus change events")},
66
67
    {"focus-listener", '\0', 0, G_OPTION_ARG_STRING, &opt_focus_listener,
     N_("Use the given focus listener (\"atspi\" or \"ibus\")")},
Daiki Ueno's avatar
Daiki Ueno committed
68
69
#endif  /* ENABLE_FOCUS_LISTENER */
#ifdef HAVE_ATSPI
70
71
    {"listen-keystroke", 's', 0, G_OPTION_ARG_NONE, &opt_keystroke,
     N_("Listen keystroke events with AT-SPI")},
Daiki Ueno's avatar
Daiki Ueno committed
72
#endif  /* HAVE_ATSPI */
Daiki Ueno's avatar
Daiki Ueno committed
73
    {"keyboard", 'k', 0, G_OPTION_ARG_STRING, &opt_keyboard,
74
     N_("Specify keyboard")},
75
76
77
78
79
80
81
82
    {"model", '\0', 0, G_OPTION_ARG_STRING, &opt_model,
     N_("Specify model")},
    {"layouts", '\0', 0, G_OPTION_ARG_STRING, &opt_layouts,
     N_("Specify layouts")},
    {"options", '\0', 0, G_OPTION_ARG_STRING, &opt_options,
     N_("Specify options")},
    {"fullscreen", 'F', 0, G_OPTION_ARG_NONE, &opt_fullscreen,
     N_("Create window in fullscreen mode")},
Daiki Ueno's avatar
Daiki Ueno committed
83
84
85
86
    {NULL}
};

static void
87
88
89
on_notify_keyboard_visible (GObject    *object,
                            GParamSpec *spec,
                            gpointer    user_data)
Daiki Ueno's avatar
Daiki Ueno committed
90
{
91
92
93
94
95
96
97
98
    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
99
100
101
}

static void
102
103
on_context_destroyed (EekboardContext *context,
                      gpointer         user_data)
Daiki Ueno's avatar
Daiki Ueno committed
104
{
105
106
107
108
109
110
111
112
113
114
115
116
    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
117
118
}

Daiki Ueno's avatar
Daiki Ueno committed
119
120
121
122
123
124
enum {
    FOCUS_NONE,
    FOCUS_ATSPI,
    FOCUS_IBUS
};

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

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

143
    option_context = g_option_context_new ("eekboard-desktop-client");
144
145
146
    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
147

148
149
150
151
152
153
154
155
156
157
158
159
160
    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
161
            g_printerr ("Can't connect to the bus: %s\n", error->message);
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
176
177
178
179
180
181
                        opt_address,
                        error->message);
            exit (1);
        }
        break;
    default:
        g_assert_not_reached ();
        break;
Daiki Ueno's avatar
Daiki Ueno committed
182
183
    }

184
185
186
187
    client = eekboard_client_new (connection);
    if (client == NULL) {
        g_printerr ("Can't create a client\n");
        exit (1);
188
189
    }

Daiki Ueno's avatar
Daiki Ueno committed
190
191
    focus = FOCUS_NONE;
    if (opt_focus) {
192
193
        if (opt_focus_listener == NULL ||
            g_strcmp0 (opt_focus_listener, "atspi") == 0)
Daiki Ueno's avatar
Daiki Ueno committed
194
            focus = FOCUS_ATSPI;
195
        else if (g_strcmp0 (opt_focus_listener, "ibus") == 0)
Daiki Ueno's avatar
Daiki Ueno committed
196
197
            focus = FOCUS_IBUS;
        else {
198
199
            g_printerr ("Unknown focus listener \"%s\".  "
                        "Try \"atspi\" or \"ibus\"\n", opt_focus_listener);
Daiki Ueno's avatar
Daiki Ueno committed
200
201
202
203
            exit (1);
        }
    }
        
Daiki Ueno's avatar
Daiki Ueno committed
204
#ifdef HAVE_ATSPI
Daiki Ueno's avatar
Daiki Ueno committed
205
    if (focus == FOCUS_ATSPI || opt_keystroke) {
Daiki Ueno's avatar
Daiki Ueno committed
206
207
208
209
210
        GSettings *settings = g_settings_new ("org.gnome.desktop.interface");

        if (g_settings_get_boolean (settings, "toolkit-accessibility")) {
            if (atspi_init () != 0) {
                g_printerr ("Can't init AT-SPI 2\n");
211
212
213
                exit (1);
            }

Daiki Ueno's avatar
Daiki Ueno committed
214
            if (focus == FOCUS_ATSPI &&
Daiki Ueno's avatar
Daiki Ueno committed
215
                !eekboard_client_enable_atspi_focus (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
216
                g_printerr ("Can't register AT-SPI focus change event listeners\n");
217
218
219
220
                exit (1);
            }

            if (opt_keystroke &&
Daiki Ueno's avatar
Daiki Ueno committed
221
                !eekboard_client_enable_atspi_keystroke (client)) {
Daiki Ueno's avatar
Daiki Ueno committed
222
                g_printerr ("Can't register AT-SPI keystroke event listeners\n");
223
224
225
226
227
                exit (1);
            }
        } else {
            g_printerr ("Desktop accessibility support is disabled\n");
            exit (1);
Daiki Ueno's avatar
Daiki Ueno committed
228
        }
229
    }
Daiki Ueno's avatar
Daiki Ueno committed
230
#endif  /* HAVE_ATSPI */
231

Daiki Ueno's avatar
Daiki Ueno committed
232
233
234
235
236
237
238
239
240
241
242
243
#ifdef HAVE_IBUS
    if (focus == FOCUS_IBUS) {
        ibus_init ();

        if (focus == FOCUS_IBUS &&
            !eekboard_client_enable_ibus_focus (client)) {
            g_printerr ("Can't register IBus focus change event listeners\n");
            exit (1);
        }
    }
#endif  /* HAVE_IBUS */

244
245
    if (opt_use_system_layout && (opt_keyboard || opt_model || opt_layouts || opt_options)) {
        g_printerr ("Can't use --use-system-layout option with keyboard options\n");
Daiki Ueno's avatar
Daiki Ueno committed
246
247
248
        exit (1);
    }

249
250
251
252
253
254
255
256
257
258
259
260
261
262
    if (opt_use_system_layout) {
        if (!eekboard_client_enable_xkl (client)) {
            g_printerr ("Can't register xklavier event listeners\n");
            exit (1);
        }
    } else if (opt_model || opt_layouts || opt_options) {
        if (!eekboard_client_set_xkl_config (client,
                                             opt_model,
                                             opt_layouts,
                                             opt_options)) {
            g_printerr ("Can't set xklavier config\n");
            exit (1);
        }
    } else {
263
264
        gchar *file;

265
266
267
        if (!opt_keyboard)
            opt_keyboard = DEFAULT_LAYOUT;

268
269
270
271
272
273
274
        if (g_str_has_suffix (opt_keyboard, ".xml"))
            file = g_strdup (opt_keyboard);
        else
            file = g_strdup_printf ("%s/%s.xml", KEYBOARDDIR, opt_keyboard);
        if (!eekboard_client_load_keyboard_from_file (client, file)) {
            g_printerr ("Can't load keyboard file %s\n", file);
            g_free (file);
Daiki Ueno's avatar
Daiki Ueno committed
275
276
            exit (1);
        }
277
        g_free (file);
Daiki Ueno's avatar
Daiki Ueno committed
278
279
    }

280
281
282
283
#ifdef HAVE_FAKEKEY
    if (!eekboard_client_enable_fakekey (client)) {
        g_printerr ("Can't init fakekey\n");
        exit (1);
Daiki Ueno's avatar
Daiki Ueno committed
284
    }
285
286
287
288
289
290
291
292
293
294
#endif  /* HAVE_FAKEKEY */

    loop = g_main_loop_new (NULL, FALSE);
    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
295
296
    }

297
298
299
300
    if (opt_fullscreen) {
        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
301
302
    }

303
304
305
    g_object_get (client, "eekboard", &eekboard, NULL);
    g_signal_connect (eekboard, "destroyed",
                      G_CALLBACK(on_destroyed), loop);
Daiki Ueno's avatar
Daiki Ueno committed
306

307
308
    g_main_loop_run (loop);
    g_main_loop_unref (loop);
Daiki Ueno's avatar
Daiki Ueno committed
309

310
    return 0;
Daiki Ueno's avatar
Daiki Ueno committed
311
}