client-main.c 9.23 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 36 37 38
static gboolean opt_system = FALSE;
static gboolean opt_session = FALSE;
static gchar *opt_address = NULL;

39 40 41
static gboolean opt_focus = FALSE;
static gboolean opt_keystroke = FALSE;

42 43
static gchar *opt_keyboards = NULL;

44
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
    {"keyboards", 'k', 0, G_OPTION_ARG_STRING, &opt_keyboards,
     N_("Specify keyboards (comma separated)")},
63 64
    {"fullscreen", 'F', 0, G_OPTION_ARG_NONE, &opt_fullscreen,
     N_("Create window in fullscreen mode")},
Daiki Ueno's avatar
Daiki Ueno committed
65 66 67 68
    {NULL}
};

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

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

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

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

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

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

Daiki Ueno's avatar
Daiki Ueno committed
128
    eek_init ();
129

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

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

Daiki Ueno's avatar
Daiki Ueno committed
183
    client = client_new (connection);
184 185
    g_object_unref (connection);

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

191
    settings = g_settings_new ("org.fedorahosted.eekboard");
Daiki Ueno's avatar
Daiki Ueno committed
192 193
    focus = FOCUS_NONE;
    if (opt_focus) {
194 195
        gchar *focus_listener = g_settings_get_string (settings,
                                                       "focus-listener");
Daiki Ueno's avatar
Daiki Ueno committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        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) {
216
            g_printerr ("Unknown focus listener \"%s\".  "
217
                        "Try \"atspi\" or \"ibus\"\n", focus_listener);
218 219
            retval = 1;
            goto out;
Daiki Ueno's avatar
Daiki Ueno committed
220 221 222
        }
    }
        
Daiki Ueno's avatar
Daiki Ueno committed
223
#ifdef HAVE_ATSPI
Daiki Ueno's avatar
Daiki Ueno committed
224
    if (focus == FOCUS_ATSPI || opt_keystroke) {
Daiki Ueno's avatar
Daiki Ueno committed
225 226
        GSettings *desktop_settings =
            g_settings_new ("org.gnome.desktop.interface");
227
        gboolean accessibility_enabled =
Daiki Ueno's avatar
Daiki Ueno committed
228
            g_settings_get_boolean (desktop_settings, "toolkit-accessibility");
Daiki Ueno's avatar
Daiki Ueno committed
229
        g_object_unref (desktop_settings);
Daiki Ueno's avatar
Daiki Ueno committed
230

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

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

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

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

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

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

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

286 287
    if (opt_fullscreen ||
        g_settings_get_boolean (settings, "start-fullscreen")) {
288 289 290
        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
291 292
    }

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

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

Daiki Ueno's avatar
Daiki Ueno committed
309
    gtk_main ();
Daiki Ueno's avatar
Daiki Ueno committed
310

311 312 313 314 315 316 317
 out:
    if (client)
        g_object_unref (client);
    if (settings)
        g_object_unref (settings);

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