text: Fix set_surrounding_text request
[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                                 uint32_t cursor,
114                                 uint32_t anchor)
115 {
116         struct text_model *text_model = resource->data;
117         struct input_method *input_method, *next;
118
119         wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
120                 if (!input_method->context)
121                         continue;
122                 input_method_context_send_surrounding_text(&input_method->context->resource,
123                                                            text,
124                                                            cursor,
125                                                            anchor);
126         }
127 }
128
129 static void
130 text_model_activate(struct wl_client *client,
131                     struct wl_resource *resource,
132                     struct wl_resource *seat,
133                     struct wl_resource *surface)
134 {
135         struct text_model *text_model = resource->data;
136         struct weston_seat *weston_seat = seat->data;
137         struct input_method *input_method = weston_seat->input_method;
138         struct text_model *old = weston_seat->input_method->model;
139         struct weston_compositor *ec = text_model->ec;
140
141         if (old == text_model)
142                 return;
143
144         if (old) {
145                 deactivate_text_model(old,
146                                       weston_seat->input_method);
147         }
148
149         input_method->model = text_model;
150         wl_list_insert(&text_model->input_methods, &input_method->link);
151         input_method_init_seat(weston_seat);
152
153         text_model->surface = surface->data;
154
155         input_method_context_create(text_model, input_method);
156
157         wl_signal_emit(&ec->show_input_panel_signal, ec);
158
159         text_model_send_activated(&text_model->resource);
160 }
161
162 static void
163 text_model_deactivate(struct wl_client *client,
164                       struct wl_resource *resource,
165                       struct wl_resource *seat)
166 {
167         struct text_model *text_model = resource->data;
168         struct weston_seat *weston_seat = seat->data;
169
170         deactivate_text_model(text_model,
171                               weston_seat->input_method);
172 }
173
174 static void
175 text_model_set_micro_focus(struct wl_client *client,
176                            struct wl_resource *resource,
177                            int32_t x,
178                            int32_t y,
179                            int32_t width,
180                            int32_t height)
181 {
182 }
183
184 static void
185 text_model_set_preedit(struct wl_client *client,
186                        struct wl_resource *resource)
187 {
188 }
189
190 static void
191 text_model_set_content_type(struct wl_client *client,
192                             struct wl_resource *resource)
193 {
194 }
195
196 static const struct text_model_interface text_model_implementation = {
197         text_model_set_surrounding_text,
198         text_model_activate,
199         text_model_deactivate,
200         text_model_set_micro_focus,
201         text_model_set_preedit,
202         text_model_set_content_type
203 };
204
205 static void text_model_factory_create_text_model(struct wl_client *client,
206                                                  struct wl_resource *resource,
207                                                  uint32_t id)
208 {
209         struct text_model_factory *text_model_factory = resource->data;
210         struct text_model *text_model;
211
212         text_model = calloc(1, sizeof *text_model);
213
214         text_model->resource.object.id = id;
215         text_model->resource.object.interface = &text_model_interface;
216         text_model->resource.object.implementation =
217                 (void (**)(void)) &text_model_implementation;
218
219         text_model->resource.data = text_model;
220         text_model->resource.destroy = destroy_text_model;
221
222         text_model->ec = text_model_factory->ec;
223
224         wl_list_init(&text_model->input_methods);
225
226         wl_client_add_resource(client, &text_model->resource);
227 };
228
229 static const struct text_model_factory_interface text_model_factory_implementation = {
230         text_model_factory_create_text_model
231 };
232
233 static void
234 bind_text_model_factory(struct wl_client *client,
235                         void *data,
236                         uint32_t version,
237                         uint32_t id)
238 {
239         struct text_model_factory *text_model_factory = data;
240
241         /* No checking for duplicate binding necessary.
242          * No events have to be sent, so we don't need the return value. */
243         wl_client_add_object(client, &text_model_factory_interface,
244                              &text_model_factory_implementation,
245                              id, text_model_factory);
246 }
247
248 static void
249 text_model_factory_notifier_destroy(struct wl_listener *listener, void *data)
250 {
251         struct text_model_factory *text_model_factory =
252                 container_of(listener, struct text_model_factory, destroy_listener);
253
254         wl_display_remove_global(text_model_factory->ec->wl_display,
255                                  text_model_factory->text_model_factory_global);
256
257         free(text_model_factory);
258 }
259
260 WL_EXPORT void
261 text_model_factory_create(struct weston_compositor *ec)
262 {
263         struct text_model_factory *text_model_factory;
264
265         text_model_factory = calloc(1, sizeof *text_model_factory);
266
267         text_model_factory->ec = ec;
268
269         text_model_factory->text_model_factory_global =
270                 wl_display_add_global(ec->wl_display,
271                                       &text_model_factory_interface,
272                                       text_model_factory, bind_text_model_factory);
273
274         text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy;
275         wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener);
276 }
277
278 static void
279 input_method_context_destroy(struct wl_client *client,
280                              struct wl_resource *resource)
281 {
282         wl_resource_destroy(resource);
283 }
284
285 static void
286 input_method_context_commit_string(struct wl_client *client,
287                                    struct wl_resource *resource,
288                                    const char *text,
289                                    uint32_t index)
290 {
291         struct input_method_context *context = resource->data;
292
293         text_model_send_commit_string(&context->model->resource, text, index);
294 }
295
296 static const struct input_method_context_interface input_method_context_implementation = {
297         input_method_context_destroy,
298         input_method_context_commit_string
299 };
300
301 static void
302 destroy_input_method_context(struct wl_resource *resource)
303 {
304         struct input_method_context *context = resource->data;
305
306         free(context);
307 }
308
309 static void
310 input_method_context_create(struct text_model *model,
311                             struct input_method *input_method)
312 {
313         struct input_method_context *context;
314
315         if (!input_method->input_method_binding)
316                 return;
317
318         context = malloc(sizeof *context);
319         if (context == NULL)
320                 return;
321
322         context->resource.destroy = destroy_input_method_context;
323         context->resource.object.id = 0;
324         context->resource.object.interface = &input_method_context_interface;
325         context->resource.object.implementation =
326                 (void (**)(void)) &input_method_context_implementation;
327         context->resource.data = context;
328         wl_signal_init(&context->resource.destroy_signal);
329
330         context->model = model;
331         input_method->context = context;
332
333         wl_client_add_resource(input_method->input_method_binding->client, &context->resource);
334
335         input_method_send_activate(input_method->input_method_binding, &context->resource);
336 }
337
338 static void
339 unbind_input_method(struct wl_resource *resource)
340 {
341         struct input_method *input_method = resource->data;
342
343         input_method->input_method_binding = NULL;
344         input_method->context = NULL;
345
346         free(resource);
347 }
348
349 static void
350 bind_input_method(struct wl_client *client,
351                   void *data,
352                   uint32_t version,
353                   uint32_t id)
354 {
355         struct input_method *input_method = data;
356         struct wl_resource *resource;
357
358         resource = wl_client_add_object(client, &input_method_interface,
359                                         NULL,
360                                         id, input_method);
361
362         if (input_method->input_method_binding == NULL) {
363                 resource->destroy = unbind_input_method;
364                 input_method->input_method_binding = resource;
365                 return;
366         }
367
368         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
369                                "interface object already bound");
370         wl_resource_destroy(resource);
371 }
372
373 static void
374 input_method_notifier_destroy(struct wl_listener *listener, void *data)
375 {
376         struct input_method *input_method =
377                 container_of(listener, struct input_method, destroy_listener);
378
379         if (input_method->model)
380                 deactivate_text_model(input_method->model, input_method);
381
382         wl_display_remove_global(input_method->seat->compositor->wl_display,
383                                  input_method->input_method_global);
384
385         free(input_method);
386 }
387
388 static void
389 handle_keyboard_focus(struct wl_listener *listener, void *data)
390 {
391         struct wl_keyboard *keyboard = data;
392         struct input_method *input_method =
393                 container_of(listener, struct input_method, keyboard_focus_listener);
394         struct wl_surface *surface = keyboard->focus;
395
396         if (!input_method->model)
397                 return;
398
399         if (!surface || input_method->model->surface != surface)
400                 deactivate_text_model(input_method->model,
401                                       input_method);
402 }
403
404 static void
405 input_method_init_seat(struct weston_seat *seat)
406 {
407         if (seat->input_method->focus_listener_initialized)
408                 return;
409
410         if (seat->has_keyboard) {
411                 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
412                 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
413         }
414
415         seat->input_method->focus_listener_initialized = 1;
416 }
417
418 WL_EXPORT void
419 input_method_create(struct weston_compositor *ec,
420                     struct weston_seat *seat)
421 {
422         struct input_method *input_method;
423
424         input_method = calloc(1, sizeof *input_method);
425
426         input_method->seat = seat;
427         input_method->model = NULL;
428         input_method->focus_listener_initialized = 0;
429         input_method->context = NULL;
430
431         input_method->input_method_global =
432                 wl_display_add_global(ec->wl_display,
433                                       &input_method_interface,
434                                       input_method, bind_input_method);
435
436         input_method->destroy_listener.notify = input_method_notifier_destroy;
437         wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
438
439         seat->input_method = input_method;
440 }
441