client-main.c 4.54 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
22
23
24
/* 
 * 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 */

#include "proxy.h"

static gchar *opt_set_keyboard = NULL;
25
static gint opt_set_group = -1;
Daiki Ueno's avatar
Daiki Ueno committed
26
27
static gboolean opt_show = FALSE;
static gboolean opt_hide = 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
33
34
static gboolean opt_listen = FALSE;

static const GOptionEntry options[] = {
    {"set-keyboard", '\0', 0, G_OPTION_ARG_STRING, &opt_set_keyboard,
     "Set keyboard from an XML file"},
35
36
    {"set-group", '\0', 0, G_OPTION_ARG_INT, &opt_set_group,
     "Set group of the keyboard"},
Daiki Ueno's avatar
Daiki Ueno committed
37
38
39
40
    {"show", '\0', 0, G_OPTION_ARG_NONE, &opt_show,
     "Show keyboard"},
    {"hide", '\0', 0, G_OPTION_ARG_NONE, &opt_hide,
     "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
65
66
67
68
69
70
71
72
    {"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)
{
    EekboardProxy *proxy = NULL;
    GDBusConnection *connection = NULL;
    GError *error;
    GOptionContext *context;
    GMainLoop *loop = NULL;
    gint retval = 0;

    g_type_init ();
73
    g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
Daiki Ueno's avatar
Daiki Ueno committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

    context = g_option_context_new ("eekboard-client");
    g_option_context_add_main_entries (context, options, NULL);
    g_option_context_parse (context, &argc, &argv, NULL);
    g_option_context_free (context);

    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;
    }

    error = NULL;
    proxy = eekboard_proxy_new ("/com/redhat/eekboard/Keyboard",
                                 connection,
                                 NULL,
                                 &error);
    if (error) {
        g_printerr ("%s\n", error->message);
        retval = 1;
        goto out;
    }

    if (opt_set_keyboard) {
        GFile *file;
        GFileInputStream *input;
        EekLayout *layout;
        EekKeyboard *keyboard;
        GError *error;

        file = g_file_new_for_path (opt_set_keyboard);

        error = NULL;
        input = g_file_read (file, NULL, &error);
        if (error) {
            g_printerr ("Can't read file %s: %s\n",
                        opt_set_keyboard, error->message);
            retval = 1;
            goto out;
        }

        layout = eek_xml_layout_new (G_INPUT_STREAM(input));
        g_object_unref (input);
        keyboard = eek_keyboard_new (layout, 640, 480);
        g_object_unref (layout);
        eekboard_proxy_set_keyboard (proxy, keyboard);
        g_object_unref (keyboard);
    }

125
126
127
128
    if (opt_set_group >= 0) {
        eekboard_proxy_set_group (proxy, opt_set_group);
    }

Daiki Ueno's avatar
Daiki Ueno committed
129
130
131
132
133
134
135
136
    if (opt_show) {
        eekboard_proxy_show (proxy);
    }

    if (opt_hide) {
        eekboard_proxy_hide (proxy);
    }

Daiki Ueno's avatar
Daiki Ueno committed
137
138
139
140
141
142
143
144
    if (opt_press_key >= 0) {
        eekboard_proxy_press_key (proxy, opt_press_key);
    }

    if (opt_release_key >= 0) {
        eekboard_proxy_release_key (proxy, opt_release_key);
    }

Daiki Ueno's avatar
Daiki Ueno committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    if (opt_listen) {
        g_signal_connect (proxy, "key-pressed",
                          G_CALLBACK(on_key_pressed), NULL);
        g_signal_connect (proxy, "key-released",
                          G_CALLBACK(on_key_released), NULL);
        loop = g_main_loop_new (NULL, FALSE);
        g_main_loop_run (loop);
    }

 out:
    if (proxy)
        g_object_unref (proxy);
    if (connection)
        g_object_unref (connection);
    if (loop)
        g_main_loop_unref (loop);

    return retval;
}