client-main.c 4.67 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_description = 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
static gboolean opt_listen = FALSE;

static const GOptionEntry options[] = {
Daiki Ueno's avatar
Daiki Ueno committed
33
34
    {"set-description", '\0', 0, G_OPTION_ARG_STRING, &opt_set_description,
     "Set keyboard description 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
    {"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
    EekboardKeyboard *keyboard = NULL;
Daiki Ueno's avatar
Daiki Ueno committed
66
67
68
69
70
71
72
    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

    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;
89
    keyboard = eekboard_keyboard_new ("/com/redhat/eekboard/Keyboard",
Daiki Ueno's avatar
Daiki Ueno committed
90
91
92
                                      connection,
                                      NULL,
                                      &error);
Daiki Ueno's avatar
Daiki Ueno committed
93
94
95
96
97
98
    if (error) {
        g_printerr ("%s\n", error->message);
        retval = 1;
        goto out;
    }

99
    if (opt_set_description) {
Daiki Ueno's avatar
Daiki Ueno committed
100
101
102
        GFile *file;
        GFileInputStream *input;
        EekLayout *layout;
103
        EekKeyboard *description;
Daiki Ueno's avatar
Daiki Ueno committed
104
105
        GError *error;

106
        file = g_file_new_for_path (opt_set_description);
Daiki Ueno's avatar
Daiki Ueno committed
107
108
109
110
111

        error = NULL;
        input = g_file_read (file, NULL, &error);
        if (error) {
            g_printerr ("Can't read file %s: %s\n",
112
                        opt_set_description, error->message);
Daiki Ueno's avatar
Daiki Ueno committed
113
114
115
116
117
118
            retval = 1;
            goto out;
        }

        layout = eek_xml_layout_new (G_INPUT_STREAM(input));
        g_object_unref (input);
119
        description = eek_keyboard_new (layout, 640, 480);
Daiki Ueno's avatar
Daiki Ueno committed
120
        g_object_unref (layout);
121
122
        eekboard_keyboard_set_description (keyboard, description);
        g_object_unref (description);
Daiki Ueno's avatar
Daiki Ueno committed
123
124
    }

125
    if (opt_set_group >= 0) {
126
        eekboard_keyboard_set_group (keyboard, opt_set_group);
127
128
    }

Daiki Ueno's avatar
Daiki Ueno committed
129
    if (opt_show) {
130
        eekboard_keyboard_show (keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
131
132
133
    }

    if (opt_hide) {
134
        eekboard_keyboard_hide (keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
135
136
    }

Daiki Ueno's avatar
Daiki Ueno committed
137
    if (opt_press_key >= 0) {
138
        eekboard_keyboard_press_key (keyboard, opt_press_key);
Daiki Ueno's avatar
Daiki Ueno committed
139
140
141
    }

    if (opt_release_key >= 0) {
142
        eekboard_keyboard_release_key (keyboard, opt_release_key);
Daiki Ueno's avatar
Daiki Ueno committed
143
144
    }

Daiki Ueno's avatar
Daiki Ueno committed
145
    if (opt_listen) {
146
        g_signal_connect (keyboard, "key-pressed",
Daiki Ueno's avatar
Daiki Ueno committed
147
                          G_CALLBACK(on_key_pressed), NULL);
148
        g_signal_connect (keyboard, "key-released",
Daiki Ueno's avatar
Daiki Ueno committed
149
150
151
152
153
154
                          G_CALLBACK(on_key_released), NULL);
        loop = g_main_loop_new (NULL, FALSE);
        g_main_loop_run (loop);
    }

 out:
155
156
    if (keyboard)
        g_object_unref (keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
157
158
159
160
161
162
163
164
    if (connection)
        g_object_unref (connection);
    if (loop)
        g_main_loop_unref (loop);

    return retval;
}