File: maliitinputmethod.c

package info (click to toggle)
maliit-framework 2.3.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,488 kB
  • sloc: cpp: 13,098; ansic: 2,506; xml: 299; sh: 34; makefile: 23; sed: 4
file content (266 lines) | stat: -rw-r--r-- 8,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
73
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This file is part of Maliit framework
 *
 * Copyright (C) 2012 One Laptop per Child Association
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the licence, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "maliitinputmethod.h"
#include "maliitmarshallers.h"
#include "maliitbus.h"

/**
 * SECTION:maliitinputmethod
 * @short_description: input method handling
 * @title: MaliitInputMethod
 * @stability: Stable
 * @include: maliit/maliitinputmethod.h
 *
 * #MaliitInputMethod class can be used by application to query
 * maliit-server for currently shown IM plugin area and to request
 * maliit-server to show or hide the IM plugin.
 */

struct _MaliitInputMethodPrivate
{
    int area[4];

    MaliitServer *maliit_proxy;
};

G_DEFINE_TYPE_WITH_CODE (MaliitInputMethod, maliit_input_method, G_TYPE_OBJECT,
                         G_ADD_PRIVATE (MaliitInputMethod))

enum
{
    AREA_CHANGED,

    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
maliit_input_method_finalize (GObject *object)
{
    G_OBJECT_CLASS (maliit_input_method_parent_class)->finalize (object);
}

static void
maliit_input_method_dispose (GObject *object)
{
    GError *error = NULL;
    MaliitInputMethod *input_method = MALIIT_INPUT_METHOD (object);
    MaliitInputMethodPrivate *priv = input_method->priv;
    MaliitContext *context = maliit_get_context_sync (NULL, &error);

    if (context) {
        g_signal_handlers_disconnect_by_data (context, input_method);
    } else {
        g_warning ("Unable to connect to context: %s", error->message);
        g_clear_error (&error);
    }

    g_clear_object (&priv->maliit_proxy);

    G_OBJECT_CLASS (maliit_input_method_parent_class)->dispose (object);
}

static void
maliit_input_method_class_init (MaliitInputMethodClass *input_method_class)
{
    GObjectClass *g_object_class = G_OBJECT_CLASS (input_method_class);

    g_object_class->finalize = maliit_input_method_finalize;
    g_object_class->dispose = maliit_input_method_dispose;

    /**
     * MaliitInputMethod::area-changed:
     * @input_method: The #MaliitInputMethod emitting the signal.
     * @x: X coordinate of new input method area's top-left corner.
     * @y: Y coordinate of new input method area's top-left corner.
     *.
     * @width: Width of new input method area.
     * @height: Height of new input method area.
     *
     * Informs application that the input method area (area on the screen
     * occupied by a virtual keyboard) is changed.
     */
    signals[AREA_CHANGED] =
        g_signal_new ("area-changed",
                      MALIIT_TYPE_INPUT_METHOD,
                      G_SIGNAL_RUN_FIRST,
                      0,
                      NULL,
                      NULL,
                      maliit_marshal_VOID__INT_INT_INT_INT,
                      G_TYPE_NONE,
                      4,
                      G_TYPE_INT,
                      G_TYPE_INT,
                      G_TYPE_INT,
                      G_TYPE_INT);
}

static gboolean
update_input_method_area(MaliitInputMethod *input_method,
                         GDBusMethodInvocation *invocation G_GNUC_UNUSED,
                         int x, int y, int width, int height,
                         gpointer user_data G_GNUC_UNUSED)
{
    input_method->priv->area[0] = x;
    input_method->priv->area[1] = y;
    input_method->priv->area[2] = width;
    input_method->priv->area[3] = height;

    g_signal_emit(input_method, signals[AREA_CHANGED], 0, x, y, width, height);

    return FALSE;
}

static void
maliit_input_method_init (MaliitInputMethod *input_method)
{
    MaliitInputMethodPrivate *priv = maliit_input_method_get_instance_private (input_method);
    MaliitContext *context;
    GError *error = NULL;

    priv->area[0] = priv->area[1] = 0;
    priv->area[2] = priv->area[3] = 0;

    priv->maliit_proxy = maliit_get_server_sync (NULL, &error);

    if (priv->maliit_proxy) {
        g_object_ref (priv->maliit_proxy);
    } else {
        g_warning ("Unable to connect to server: %s", error->message);
        g_clear_error (&error);
    }

    input_method->priv = priv;

    context = maliit_get_context_sync (NULL, &error);

    if (context) {
        g_signal_connect_swapped (context,
                                  "handle-update-input-method-area",
                                  G_CALLBACK (update_input_method_area),
                                  input_method);
    } else {
        g_warning ("Unable to connect to context: %s", error->message);
        g_clear_error (&error);
    }
}

/**
 * maliit_input_method_new:
 *
 * Returns a new maliit input method.
 *
 * Returns: (transfer full): A new #MaliitInputMethod.
 */
MaliitInputMethod *
maliit_input_method_new (void)
{
    return MALIIT_INPUT_METHOD (g_object_new (MALIIT_TYPE_INPUT_METHOD,
                                              NULL));
}

/**
 * maliit_input_method_get_area:
 * @input_method: (transfer none): The #MaliitInputMethod which input method area you want to get.
 * @x: (out): X coordinate of current input method area's top-left corner.
 * @y: (out): Y coordinate of current input method area's top-left corner.
 * @width: (out): Width of current input method area.
 * @height: (out): Height of current input method area.
 *
 * Get the current input method area in @x, @y, @width and @height.
 */
void
maliit_input_method_get_area (MaliitInputMethod *input_method,
                              int *x, int *y,
                              int *width, int *height)
{
    g_return_if_fail (MALIIT_IS_INPUT_METHOD (input_method));

    if (x)
        *x = input_method->priv->area[0];
    if (y)
        *y = input_method->priv->area[1];
    if (width)
        *width = input_method->priv->area[2];
    if (height)
        *height = input_method->priv->area[3];
}

/**
 * maliit_input_method_show:
 * @input_method: (transfer none): The #MaliitInputMethod which you want to show.
 *
 * Request to explicitly show the Maliit virtual keyboard.
 */
void
maliit_input_method_show (MaliitInputMethod *input_method)
{
    GError *error = NULL;

    g_return_if_fail (MALIIT_IS_INPUT_METHOD (input_method));
    g_return_if_fail (input_method->priv->maliit_proxy);

    if (!maliit_server_call_activate_context_sync (input_method->priv->maliit_proxy,
                                                   NULL,
                                                   &error)) {
        g_warning ("Unable to activate context: %s", error->message);
        g_clear_error (&error);
    }

    if (!maliit_server_call_show_input_method_sync (input_method->priv->maliit_proxy,
                                                    NULL,
                                                    &error)) {
        g_warning ("Unable to show input method: %s", error->message);
        g_clear_error (&error);
    }
}

/**
 * maliit_input_method_hide:
 * @input_method: (transfer none): The #MaliitInputMethod which you want to hide.
 *
 * Request to explicitly hide the Maliit virtual keyboard.
 */
void
maliit_input_method_hide (MaliitInputMethod *input_method)
{
    GError *error = NULL;

    g_return_if_fail (MALIIT_IS_INPUT_METHOD (input_method));
    g_return_if_fail (input_method->priv->maliit_proxy);

    if (!maliit_server_call_activate_context_sync (input_method->priv->maliit_proxy,
                                                   NULL,
                                                   &error)) {
        g_warning ("Unable to activate context: %s", error->message);
        g_clear_error (&error);
    }

    if (!maliit_server_call_hide_input_method_sync (input_method->priv->maliit_proxy,
                                                    NULL,
                                                    &error)) {
        g_warning ("Unable to hide input method: %s", error->message);
        g_clear_error (&error);
    }
}