client-main.c 4.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 "eekboard/eekboard.h"
Daiki Ueno's avatar
Daiki Ueno committed
23

24
static gchar *opt_set_keyboard = NULL;
25
static gint opt_set_group = -1;
26
27
static gboolean opt_show_keyboard = FALSE;
static gboolean opt_hide_keyboard = FALSE;
Daiki Ueno's avatar
Daiki Ueno committed
28
29
static gint opt_press_key = -1;
static gint opt_release_key = -1;
Daiki Ueno's avatar
Daiki Ueno committed
30
31
32
static gboolean opt_listen = FALSE;

static const GOptionEntry options[] = {
33
34
    {"set-keyboard", '\0', 0, G_OPTION_ARG_STRING, &opt_set_keyboard,
     "Set keyboard keyboard from an XML file"},
35
36
    {"set-group", '\0', 0, G_OPTION_ARG_INT, &opt_set_group,
     "Set group of the keyboard"},
37
    {"show-keyboard", '\0', 0, G_OPTION_ARG_NONE, &opt_show_keyboard,
Daiki Ueno's avatar
Daiki Ueno committed
38
     "Show keyboard"},
39
    {"hide-keyboard", '\0', 0, G_OPTION_ARG_NONE, &opt_hide_keyboard,
Daiki Ueno's avatar
Daiki Ueno committed
40
     "Hide keyboard"},
Daiki Ueno's avatar
Daiki Ueno committed
41
42
43
44
    {"press-key", '\0', 0, G_OPTION_ARG_INT, &opt_press_key,
     "Press key"},
    {"release-key", '\0', 0, G_OPTION_ARG_INT, &opt_release_key,
     "Release key"},
Daiki Ueno's avatar
Daiki Ueno committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    {"listen", '\0', 0, G_OPTION_ARG_NONE, &opt_listen,
     "Listen events"},
    {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)
{
65
66
    EekboardServer *server = NULL;
    EekboardContext *context = NULL;
Daiki Ueno's avatar
Daiki Ueno committed
67
68
    GDBusConnection *connection = NULL;
    GError *error;
69
    GOptionContext *option_context;
Daiki Ueno's avatar
Daiki Ueno committed
70
71
72
73
    GMainLoop *loop = NULL;
    gint retval = 0;

    g_type_init ();
74
    g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
Daiki Ueno's avatar
Daiki Ueno committed
75

76
77
78
79
    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
80
81
82
83
84
85
86
87
88

    error = NULL;
    connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
    if (error) {
        g_printerr ("%s\n", error->message);
        retval = 1;
        goto out;
    }

89
90
91
    server = eekboard_server_new (connection, NULL);
    if (!server) {
        g_printerr ("Can't create server\n");
Daiki Ueno's avatar
Daiki Ueno committed
92
93
94
95
        retval = 1;
        goto out;
    }

96
97
98
99
100
101
102
103
104
105
106
107
    context = eekboard_server_create_context (server,
                                              "eekboard-client",
                                              NULL);
    if (!context) {
        g_printerr ("Can't create context\n");
        retval = 1;
        goto out;
    }

    eekboard_server_push_context (server, context, NULL);

    if (opt_set_keyboard) {
Daiki Ueno's avatar
Daiki Ueno committed
108
109
110
        GFile *file;
        GFileInputStream *input;
        EekLayout *layout;
111
        EekKeyboard *keyboard;
Daiki Ueno's avatar
Daiki Ueno committed
112

113
        file = g_file_new_for_path (opt_set_keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
114
115
116
117
118

        error = NULL;
        input = g_file_read (file, NULL, &error);
        if (error) {
            g_printerr ("Can't read file %s: %s\n",
119
                        opt_set_keyboard, error->message);
Daiki Ueno's avatar
Daiki Ueno committed
120
121
122
123
124
125
            retval = 1;
            goto out;
        }

        layout = eek_xml_layout_new (G_INPUT_STREAM(input));
        g_object_unref (input);
126
        keyboard = eek_keyboard_new (layout, 640, 480);
Daiki Ueno's avatar
Daiki Ueno committed
127
        g_object_unref (layout);
128
129
130

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

133
    if (opt_set_group >= 0) {
134
        eekboard_context_set_group (context, opt_set_group, NULL);
135
136
    }

137
138
    if (opt_show_keyboard) {
        eekboard_context_show_keyboard (context, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
139
140
    }

141
142
    if (opt_hide_keyboard) {
        eekboard_context_hide_keyboard (context, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
143
144
    }

Daiki Ueno's avatar
Daiki Ueno committed
145
    if (opt_press_key >= 0) {
146
        eekboard_context_press_key (context, opt_press_key, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
147
148
149
    }

    if (opt_release_key >= 0) {
150
        eekboard_context_release_key (context, opt_release_key, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
151
152
    }

Daiki Ueno's avatar
Daiki Ueno committed
153
    if (opt_listen) {
154
        g_signal_connect (context, "key-pressed",
Daiki Ueno's avatar
Daiki Ueno committed
155
                          G_CALLBACK(on_key_pressed), NULL);
156
        g_signal_connect (context, "key-released",
Daiki Ueno's avatar
Daiki Ueno committed
157
158
159
160
161
162
                          G_CALLBACK(on_key_released), NULL);
        loop = g_main_loop_new (NULL, FALSE);
        g_main_loop_run (loop);
    }

 out:
163
164
    if (context)
        g_object_unref (context);
Daiki Ueno's avatar
Daiki Ueno committed
165
166
167
168
169
170
171
172
    if (connection)
        g_object_unref (connection);
    if (loop)
        g_main_loop_unref (loop);

    return retval;
}