text: Introduce input_method_context interface
[profile/ivi/weston.git] / src / text-backend.c
1 /*
2  * Copyright © 2012 Openismus GmbH
3  * Copyright © 2012 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include <stdlib.h>
25
26 #include "compositor.h"
27 #include "text-server-protocol.h"
28
29 struct input_method;
30 struct input_method_context;
31
32 struct text_model {
33         struct wl_resource resource;
34
35         struct weston_compositor *ec;
36
37         struct wl_list input_methods;
38
39         struct wl_surface *surface;
40 };
41
42 struct text_model_factory {
43         struct wl_global *text_model_factory_global;
44         struct wl_listener destroy_listener;
45
46         struct weston_compositor *ec;
47 };
48
49 struct input_method {
50         struct wl_resource *input_method_binding;
51         struct wl_global *input_method_global;
52         struct wl_listener destroy_listener;
53
54         struct weston_seat *seat;
55         struct text_model *model;
56
57         struct wl_list link;
58
59         struct wl_listener keyboard_focus_listener;
60
61         int focus_listener_initialized;
62
63         struct input_method_context *context;
64 };
65
66 struct input_method_context {
67         struct wl_resource resource;
68
69         struct text_model *model;
70
71         struct wl_list link;
72 };
73
74 static void input_method_context_create(struct text_model *model,
75                                         struct input_method *input_method);
76
77 static void input_method_init_seat(struct weston_seat *seat);
78
79 static void
80 deactivate_text_model(struct text_model *text_model,
81                       struct input_method *input_method)
82 {
83         struct weston_compositor *ec = text_model->ec;
84
85         if (input_method->model == text_model) {
86                 if (input_method->input_method_binding)
87                         input_method_send_deactivate(input_method->input_method_binding, &input_method->context->resource);
88                 wl_list_remove(&input_method->link);
89                 input_method->model = NULL;
90                 input_method->context = NULL;
91                 wl_signal_emit(&ec->hide_input_panel_signal, ec);
92                 text_model_send_deactivated(&text_model->resource);
93         }
94 }
95
96 static void
97 destroy_text_model(struct wl_resource *resource)
98 {
99         struct text_model *text_model =
100                 container_of(resource, struct text_model, resource);
101         struct input_method *input_method, *next;
102
103         wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
104                 deactivate_text_model(text_model, input_method);
105
106         free(text_model);
107 }
108
109 static void
110 text_model_set_surrounding_text(struct wl_client *client,
111                                 struct wl_resource *resource,
112                                 const char *text)
113 {
114         struct text_model *text_model = resource->data;
115         struct input_method *input_method, *next;
116
117         wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
118                 if (!input_method->context)
119                         continue;
120                 input_method_context_send_set_surrounding_text(&input_method->context->resource, text);
121         }
122 }
123
124 static void
125 text_model_set_cursor_index(struct wl_client *client,
126                             struct wl_resource *resource,
127                             uint32_t index)
128 {
129 }
130
131 static void
132 text_model_activate(struct wl_client *client,
133                     struct wl_resource *resource,
134                     struct wl_resource *seat,
135                     struct wl_resource *surface)
136 {
137         struct text_model *text_model = resource->data;
138         struct weston_seat *weston_seat = seat->data;
139         struct input_method *input_method = weston_seat->input_method;
140         struct text_model *old = weston_seat->input_method->model;
141         struct weston_compositor *ec = text_model->ec;
142
143         if (old == text_model)
144                 return;
145
146         if (old) {
147                 deactivate_text_model(old,
148                                       weston_seat->input_method);
149         }
150
151         input_method->model = text_model;
152         wl_list_insert(&text_model->input_methods, &input_method->link);
153         input_method_init_seat(weston_seat);
154
155         text_model->surface = surface->data;
156
157         input_method_context_create(text_model, input_method);
158
159         wl_signal_emit(&ec->show_input_panel_signal, ec);
160
161         text_model_send_activated(&text_model->resource);
162 }
163
164 static void
165 text_model_deactivate(struct wl_client *client,
166                       struct wl_resource *resource,
167                       struct wl_resource *seat)
168 {
169         struct text_model *text_model = resource->data;
170         struct weston_seat *weston_seat = seat->data;
171
172         deactivate_text_model(text_model,
173                               weston_seat->input_method);
174 }
175
176 static void
177 text_model_set_selected_text(struct wl_client *client,
178                              struct wl_resource *resource,
179                              const char *text,
180                              int32_t index)
181 {
182 }
183
184 static void
185 text_model_set_micro_focus(struct wl_client *client,
186                            struct wl_resource *resource,
187                            int32_t x,
188                            int32_t y,
189                            int32_t width,
190                            int32_t height)
191 {
192 }
193
194 static void
195 text_model_set_preedit(struct wl_client *client,
196                        struct wl_resource *resource)
197 {
198 }
199
200 static void
201 text_model_set_content_type(struct wl_client *client,
202                             struct wl_resource *resource)
203 {
204 }
205
206 static const struct text_model_interface text_model_implementation = {
207         text_model_set_surrounding_text,
208         text_model_set_cursor_index,
209         text_model_activate,
210         text_model_deactivate,
211         text_model_set_selected_text,
212         text_model_set_micro_focus,
213         text_model_set_preedit,
214         text_model_set_content_type
215 };
216
217 static void text_model_factory_create_text_model(struct wl_client *client,
218                                                  struct wl_resource *resource,
219                                                  uint32_t id)
220 {
221         struct text_model_factory *text_model_factory = resource->data;
222         struct text_model *text_model;
223
224         text_model = calloc(1, sizeof *text_model);
225
226         text_model->resource.object.id = id;
227         text_model->resource.object.interface = &text_model_interface;
228         text_model->resource.object.implementation =
229                 (void (**)(void)) &text_model_implementation;
230
231         text_model->resource.data = text_model;
232         text_model->resource.destroy = destroy_text_model;
233
234         text_model->ec = text_model_factory->ec;
235
236         wl_list_init(&text_model->input_methods);
237
238         wl_client_add_resource(client, &text_model->resource);
239 };
240
241 static const struct text_model_factory_interface text_model_factory_implementation = {
242         text_model_factory_create_text_model
243 };
244
245 static void
246 bind_text_model_factory(struct wl_client *client,
247                         void *data,
248                         uint32_t version,
249                         uint32_t id)
250 {
251         struct text_model_factory *text_model_factory = data;
252
253         /* No checking for duplicate binding necessary.
254          * No events have to be sent, so we don't need the return value. */
255         wl_client_add_object(client, &text_model_factory_interface,
256                              &text_model_factory_implementation,
257                              id, text_model_factory);
258 }
259
260 static void
261 text_model_factory_notifier_destroy(struct wl_listener *listener, void *data)
262 {
263         struct text_model_factory *text_model_factory =
264                 container_of(listener, struct text_model_factory, destroy_listener);
265
266         wl_display_remove_global(text_model_factory->ec->wl_display,
267                                  text_model_factory->text_model_factory_global);
268
269         free(text_model_factory);
270 }
271
272 WL_EXPORT void
273 text_model_factory_create(struct weston_compositor *ec)
274 {
275         struct text_model_factory *text_model_factory;
276
277         text_model_factory = calloc(1, sizeof *text_model_factory);
278
279         text_model_factory->ec = ec;
280
281         text_model_factory->text_model_factory_global =
282                 wl_display_add_global(ec->wl_display,
283                                       &text_model_factory_interface,
284                                       text_model_factory, bind_text_model_factory);
285
286         text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy;
287         wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener);
288 }
289
290 static void
291 input_method_context_destroy(struct wl_client *client,
292                              struct wl_resource *resource)
293 {
294         wl_resource_destroy(resource);
295 }
296
297 static void
298 input_method_context_commit_string(struct wl_client *client,
299                                    struct wl_resource *resource,
300                                    const char *text,
301                                    uint32_t index)
302 {
303         struct input_method_context *context = resource->data;
304
305         text_model_send_commit_string(&context->model->resource, text, index);
306 }
307
308 static const struct input_method_context_interface input_method_context_implementation = {
309         input_method_context_destroy,
310         input_method_context_commit_string
311 };
312
313 static void
314 destroy_input_method_context(struct wl_resource *resource)
315 {
316         struct input_method_context *context = resource->data;
317
318         free(context);
319 }
320
321 static void
322 input_method_context_create(struct text_model *model,
323                             struct input_method *input_method)
324 {
325         struct input_method_context *context;
326
327         if (!input_method->input_method_binding)
328                 return;
329
330         context = malloc(sizeof *context);
331         if (context == NULL)
332                 return;
333
334         context->resource.destroy = destroy_input_method_context;
335         context->resource.object.id = 0;
336         context->resource.object.interface = &input_method_context_interface;
337         context->resource.object.implementation =
338                 (void (**)(void)) &input_method_context_implementation;
339         context->resource.data = context;
340         wl_signal_init(&context->resource.destroy_signal);
341
342         context->model = model;
343         input_method->context = context;
344
345         wl_client_add_resource(input_method->input_method_binding->client, &context->resource);
346
347         input_method_send_activate(input_method->input_method_binding, &context->resource);
348 }
349
350 static void
351 unbind_input_method(struct wl_resource *resource)
352 {
353         struct input_method *input_method = resource->data;
354
355         input_method->input_method_binding = NULL;
356         input_method->context = NULL;
357
358         free(resource);
359 }
360
361 static void
362 bind_input_method(struct wl_client *client,
363                   void *data,
364                   uint32_t version,
365                   uint32_t id)
366 {
367         struct input_method *input_method = data;
368         struct wl_resource *resource;
369
370         resource = wl_client_add_object(client, &input_method_interface,
371                                         NULL,
372                                         id, input_method);
373
374         if (input_method->input_method_binding == NULL) {
375                 resource->destroy = unbind_input_method;
376                 input_method->input_method_binding = resource;
377                 return;
378         }
379
380         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
381                                "interface object already bound");
382         wl_resource_destroy(resource);
383 }
384
385 static void
386 input_method_notifier_destroy(struct wl_listener *listener, void *data)
387 {
388         struct input_method *input_method =
389                 container_of(listener, struct input_method, destroy_listener);
390
391         if (input_method->model)
392                 deactivate_text_model(input_method->model, input_method);
393
394         wl_display_remove_global(input_method->seat->compositor->wl_display,
395                                  input_method->input_method_global);
396
397         free(input_method);
398 }
399
400 static void
401 handle_keyboard_focus(struct wl_listener *listener, void *data)
402 {
403         struct wl_keyboard *keyboard = data;
404         struct input_method *input_method =
405                 container_of(listener, struct input_method, keyboard_focus_listener);
406         struct wl_surface *surface = keyboard->focus;
407
408         if (!input_method->model)
409                 return;
410
411         if (!surface || input_method->model->surface != surface)
412                 deactivate_text_model(input_method->model,
413                                       input_method);
414 }
415
416 static void
417 input_method_init_seat(struct weston_seat *seat)
418 {
419         if (seat->input_method->focus_listener_initialized)
420                 return;
421
422         if (seat->has_keyboard) {
423                 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
424                 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
425         }
426
427         seat->input_method->focus_listener_initialized = 1;
428 }
429
430 WL_EXPORT void
431 input_method_create(struct weston_compositor *ec,
432                     struct weston_seat *seat)
433 {
434         struct input_method *input_method;
435
436         input_method = calloc(1, sizeof *input_method);
437
438         input_method->seat = seat;
439         input_method->model = NULL;
440         input_method->focus_listener_initialized = 0;
441         input_method->context = NULL;
442
443         input_method->input_method_global =
444                 wl_display_add_global(ec->wl_display,
445                                       &input_method_interface,
446                                       input_method, bind_input_method);
447
448         input_method->destroy_listener.notify = input_method_notifier_destroy;
449         wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
450
451         seat->input_method = input_method;
452 }
453