weston: migrate x11 to head-based output API
[platform/upstream/weston.git] / compositor / text-backend.c
1 /*
2  * Copyright © 2012 Openismus GmbH
3  * Copyright © 2012 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include "config.h"
28
29 #include <stdbool.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <time.h>
35
36 #include "compositor.h"
37 #include "weston.h"
38 #include "text-input-unstable-v1-server-protocol.h"
39 #include "input-method-unstable-v1-server-protocol.h"
40 #include "shared/helpers.h"
41 #include "shared/timespec-util.h"
42
43 struct text_input_manager;
44 struct input_method;
45 struct input_method_context;
46 struct text_backend;
47
48 struct text_input {
49         struct wl_resource *resource;
50
51         struct weston_compositor *ec;
52
53         struct wl_list input_methods;
54
55         struct weston_surface *surface;
56
57         pixman_box32_t cursor_rectangle;
58
59         bool input_panel_visible;
60
61         struct text_input_manager *manager;
62 };
63
64 struct text_input_manager {
65         struct wl_global *text_input_manager_global;
66         struct wl_listener destroy_listener;
67
68         struct text_input *current_text_input;
69
70         struct weston_compositor *ec;
71 };
72
73 struct input_method {
74         struct wl_resource *input_method_binding;
75         struct wl_global *input_method_global;
76         struct wl_listener destroy_listener;
77
78         struct weston_seat *seat;
79         struct text_input *input;
80
81         struct wl_list link;
82
83         struct wl_listener keyboard_focus_listener;
84
85         bool focus_listener_initialized;
86
87         struct input_method_context *context;
88
89         struct text_backend *text_backend;
90 };
91
92 struct input_method_context {
93         struct wl_resource *resource;
94
95         struct text_input *input;
96         struct input_method *input_method;
97
98         struct wl_resource *keyboard;
99 };
100
101 struct text_backend {
102         struct weston_compositor *compositor;
103
104         struct {
105                 char *path;
106                 struct wl_client *client;
107
108                 unsigned deathcount;
109                 struct timespec deathstamp;
110         } input_method;
111
112         struct wl_listener client_listener;
113         struct wl_listener seat_created_listener;
114 };
115
116 static void
117 input_method_context_create(struct text_input *input,
118                             struct input_method *input_method);
119 static void
120 input_method_context_end_keyboard_grab(struct input_method_context *context);
121
122 static void
123 input_method_init_seat(struct weston_seat *seat);
124
125 static void
126 deactivate_input_method(struct input_method *input_method)
127 {
128         struct text_input *text_input = input_method->input;
129         struct weston_compositor *ec = text_input->ec;
130
131         if (input_method->context && input_method->input_method_binding) {
132                 input_method_context_end_keyboard_grab(input_method->context);
133                 zwp_input_method_v1_send_deactivate(
134                         input_method->input_method_binding,
135                         input_method->context->resource);
136                 input_method->context->input = NULL;
137         }
138
139         wl_list_remove(&input_method->link);
140         input_method->input = NULL;
141         input_method->context = NULL;
142
143         if (wl_list_empty(&text_input->input_methods) &&
144             text_input->input_panel_visible &&
145             text_input->manager->current_text_input == text_input) {
146                 wl_signal_emit(&ec->hide_input_panel_signal, ec);
147                 text_input->input_panel_visible = false;
148         }
149
150         if (text_input->manager->current_text_input == text_input)
151                 text_input->manager->current_text_input = NULL;
152
153         zwp_text_input_v1_send_leave(text_input->resource);
154 }
155
156 static void
157 destroy_text_input(struct wl_resource *resource)
158 {
159         struct text_input *text_input = wl_resource_get_user_data(resource);
160         struct input_method *input_method, *next;
161
162         wl_list_for_each_safe(input_method, next,
163                               &text_input->input_methods, link)
164                 deactivate_input_method(input_method);
165
166         free(text_input);
167 }
168
169 static void
170 text_input_set_surrounding_text(struct wl_client *client,
171                                 struct wl_resource *resource,
172                                 const char *text,
173                                 uint32_t cursor,
174                                 uint32_t anchor)
175 {
176         struct text_input *text_input = wl_resource_get_user_data(resource);
177         struct input_method *input_method, *next;
178
179         wl_list_for_each_safe(input_method, next,
180                               &text_input->input_methods, link) {
181                 if (!input_method->context)
182                         continue;
183                 zwp_input_method_context_v1_send_surrounding_text(
184                         input_method->context->resource, text, cursor, anchor);
185         }
186 }
187
188 static void
189 text_input_activate(struct wl_client *client,
190                     struct wl_resource *resource,
191                     struct wl_resource *seat,
192                     struct wl_resource *surface)
193 {
194         struct text_input *text_input = wl_resource_get_user_data(resource);
195         struct weston_seat *weston_seat = wl_resource_get_user_data(seat);
196         struct input_method *input_method;
197         struct weston_compositor *ec = text_input->ec;
198         struct text_input *current;
199
200         if (!weston_seat)
201                 return;
202
203         input_method = weston_seat->input_method;
204         if (input_method->input == text_input)
205                 return;
206
207         if (input_method->input)
208                 deactivate_input_method(input_method);
209
210         input_method->input = text_input;
211         wl_list_insert(&text_input->input_methods, &input_method->link);
212         input_method_init_seat(weston_seat);
213
214         text_input->surface = wl_resource_get_user_data(surface);
215
216         input_method_context_create(text_input, input_method);
217
218         current = text_input->manager->current_text_input;
219
220         if (current && current != text_input) {
221                 current->input_panel_visible = false;
222                 wl_signal_emit(&ec->hide_input_panel_signal, ec);
223         }
224
225         if (text_input->input_panel_visible) {
226                 wl_signal_emit(&ec->show_input_panel_signal,
227                                text_input->surface);
228                 wl_signal_emit(&ec->update_input_panel_signal,
229                                &text_input->cursor_rectangle);
230         }
231         text_input->manager->current_text_input = text_input;
232
233         zwp_text_input_v1_send_enter(text_input->resource,
234                                      text_input->surface->resource);
235 }
236
237 static void
238 text_input_deactivate(struct wl_client *client,
239                       struct wl_resource *resource,
240                       struct wl_resource *seat)
241 {
242         struct weston_seat *weston_seat = wl_resource_get_user_data(seat);
243
244         if (weston_seat && weston_seat->input_method->input)
245                 deactivate_input_method(weston_seat->input_method);
246 }
247
248 static void
249 text_input_reset(struct wl_client *client,
250                  struct wl_resource *resource)
251 {
252         struct text_input *text_input = wl_resource_get_user_data(resource);
253         struct input_method *input_method, *next;
254
255         wl_list_for_each_safe(input_method, next,
256                               &text_input->input_methods, link) {
257                 if (!input_method->context)
258                         continue;
259                 zwp_input_method_context_v1_send_reset(
260                         input_method->context->resource);
261         }
262 }
263
264 static void
265 text_input_set_cursor_rectangle(struct wl_client *client,
266                                 struct wl_resource *resource,
267                                 int32_t x,
268                                 int32_t y,
269                                 int32_t width,
270                                 int32_t height)
271 {
272         struct text_input *text_input = wl_resource_get_user_data(resource);
273         struct weston_compositor *ec = text_input->ec;
274
275         text_input->cursor_rectangle.x1 = x;
276         text_input->cursor_rectangle.y1 = y;
277         text_input->cursor_rectangle.x2 = x + width;
278         text_input->cursor_rectangle.y2 = y + height;
279
280         wl_signal_emit(&ec->update_input_panel_signal,
281                        &text_input->cursor_rectangle);
282 }
283
284 static void
285 text_input_set_content_type(struct wl_client *client,
286                             struct wl_resource *resource,
287                             uint32_t hint,
288                             uint32_t purpose)
289 {
290         struct text_input *text_input = wl_resource_get_user_data(resource);
291         struct input_method *input_method, *next;
292
293         wl_list_for_each_safe(input_method, next,
294                               &text_input->input_methods, link) {
295                 if (!input_method->context)
296                         continue;
297                 zwp_input_method_context_v1_send_content_type(
298                         input_method->context->resource, hint, purpose);
299         }
300 }
301
302 static void
303 text_input_invoke_action(struct wl_client *client,
304                          struct wl_resource *resource,
305                          uint32_t button,
306                          uint32_t index)
307 {
308         struct text_input *text_input = wl_resource_get_user_data(resource);
309         struct input_method *input_method, *next;
310
311         wl_list_for_each_safe(input_method, next,
312                               &text_input->input_methods, link) {
313                 if (!input_method->context)
314                         continue;
315                 zwp_input_method_context_v1_send_invoke_action(
316                         input_method->context->resource, button, index);
317         }
318 }
319
320 static void
321 text_input_commit_state(struct wl_client *client,
322                         struct wl_resource *resource,
323                         uint32_t serial)
324 {
325         struct text_input *text_input = wl_resource_get_user_data(resource);
326         struct input_method *input_method, *next;
327
328         wl_list_for_each_safe(input_method, next,
329                               &text_input->input_methods, link) {
330                 if (!input_method->context)
331                         continue;
332                 zwp_input_method_context_v1_send_commit_state(
333                         input_method->context->resource, serial);
334         }
335 }
336
337 static void
338 text_input_show_input_panel(struct wl_client *client,
339                             struct wl_resource *resource)
340 {
341         struct text_input *text_input = wl_resource_get_user_data(resource);
342         struct weston_compositor *ec = text_input->ec;
343
344         text_input->input_panel_visible = true;
345
346         if (!wl_list_empty(&text_input->input_methods) &&
347             text_input == text_input->manager->current_text_input) {
348                 wl_signal_emit(&ec->show_input_panel_signal,
349                                text_input->surface);
350                 wl_signal_emit(&ec->update_input_panel_signal,
351                                &text_input->cursor_rectangle);
352         }
353 }
354
355 static void
356 text_input_hide_input_panel(struct wl_client *client,
357                             struct wl_resource *resource)
358 {
359         struct text_input *text_input = wl_resource_get_user_data(resource);
360         struct weston_compositor *ec = text_input->ec;
361
362         text_input->input_panel_visible = false;
363
364         if (!wl_list_empty(&text_input->input_methods) &&
365             text_input == text_input->manager->current_text_input)
366                 wl_signal_emit(&ec->hide_input_panel_signal, ec);
367 }
368
369 static void
370 text_input_set_preferred_language(struct wl_client *client,
371                                   struct wl_resource *resource,
372                                   const char *language)
373 {
374         struct text_input *text_input = wl_resource_get_user_data(resource);
375         struct input_method *input_method, *next;
376
377         wl_list_for_each_safe(input_method, next,
378                               &text_input->input_methods, link) {
379                 if (!input_method->context)
380                         continue;
381                 zwp_input_method_context_v1_send_preferred_language(
382                         input_method->context->resource, language);
383         }
384 }
385
386 static const struct zwp_text_input_v1_interface text_input_implementation = {
387         text_input_activate,
388         text_input_deactivate,
389         text_input_show_input_panel,
390         text_input_hide_input_panel,
391         text_input_reset,
392         text_input_set_surrounding_text,
393         text_input_set_content_type,
394         text_input_set_cursor_rectangle,
395         text_input_set_preferred_language,
396         text_input_commit_state,
397         text_input_invoke_action
398 };
399
400 static void text_input_manager_create_text_input(struct wl_client *client,
401                                                  struct wl_resource *resource,
402                                                  uint32_t id)
403 {
404         struct text_input_manager *text_input_manager =
405                 wl_resource_get_user_data(resource);
406         struct text_input *text_input;
407
408         text_input = zalloc(sizeof *text_input);
409         if (text_input == NULL)
410                 return;
411
412         text_input->resource =
413                 wl_resource_create(client, &zwp_text_input_v1_interface, 1, id);
414         wl_resource_set_implementation(text_input->resource,
415                                        &text_input_implementation,
416                                        text_input, destroy_text_input);
417
418         text_input->ec = text_input_manager->ec;
419         text_input->manager = text_input_manager;
420
421         wl_list_init(&text_input->input_methods);
422 };
423
424 static const struct zwp_text_input_manager_v1_interface manager_implementation = {
425         text_input_manager_create_text_input
426 };
427
428 static void
429 bind_text_input_manager(struct wl_client *client,
430                         void *data,
431                         uint32_t version,
432                         uint32_t id)
433 {
434         struct text_input_manager *text_input_manager = data;
435         struct wl_resource *resource;
436
437         /* No checking for duplicate binding necessary.  */
438         resource =
439                 wl_resource_create(client,
440                                    &zwp_text_input_manager_v1_interface, 1, id);
441         if (resource)
442                 wl_resource_set_implementation(resource,
443                                                &manager_implementation,
444                                                text_input_manager, NULL);
445 }
446
447 static void
448 text_input_manager_notifier_destroy(struct wl_listener *listener, void *data)
449 {
450         struct text_input_manager *text_input_manager =
451                 container_of(listener,
452                              struct text_input_manager,
453                              destroy_listener);
454
455         wl_global_destroy(text_input_manager->text_input_manager_global);
456
457         free(text_input_manager);
458 }
459
460 static void
461 text_input_manager_create(struct weston_compositor *ec)
462 {
463         struct text_input_manager *text_input_manager;
464
465         text_input_manager = zalloc(sizeof *text_input_manager);
466         if (text_input_manager == NULL)
467                 return;
468
469         text_input_manager->ec = ec;
470
471         text_input_manager->text_input_manager_global =
472                 wl_global_create(ec->wl_display,
473                                  &zwp_text_input_manager_v1_interface, 1,
474                                  text_input_manager, bind_text_input_manager);
475
476         text_input_manager->destroy_listener.notify =
477                 text_input_manager_notifier_destroy;
478         wl_signal_add(&ec->destroy_signal,
479                       &text_input_manager->destroy_listener);
480 }
481
482 static void
483 input_method_context_destroy(struct wl_client *client,
484                              struct wl_resource *resource)
485 {
486         wl_resource_destroy(resource);
487 }
488
489 static void
490 input_method_context_commit_string(struct wl_client *client,
491                                    struct wl_resource *resource,
492                                    uint32_t serial,
493                                    const char *text)
494 {
495         struct input_method_context *context =
496                 wl_resource_get_user_data(resource);
497
498         if (context->input)
499                 zwp_text_input_v1_send_commit_string(context->input->resource,
500                                                      serial, text);
501 }
502
503 static void
504 input_method_context_preedit_string(struct wl_client *client,
505                                     struct wl_resource *resource,
506                                     uint32_t serial,
507                                     const char *text,
508                                     const char *commit)
509 {
510         struct input_method_context *context =
511                 wl_resource_get_user_data(resource);
512
513         if (context->input)
514                 zwp_text_input_v1_send_preedit_string(context->input->resource,
515                                                       serial, text, commit);
516 }
517
518 static void
519 input_method_context_preedit_styling(struct wl_client *client,
520                                      struct wl_resource *resource,
521                                      uint32_t index,
522                                      uint32_t length,
523                                      uint32_t style)
524 {
525         struct input_method_context *context =
526                 wl_resource_get_user_data(resource);
527
528         if (context->input)
529                 zwp_text_input_v1_send_preedit_styling(context->input->resource,
530                                                        index, length, style);
531 }
532
533 static void
534 input_method_context_preedit_cursor(struct wl_client *client,
535                                     struct wl_resource *resource,
536                                     int32_t cursor)
537 {
538         struct input_method_context *context =
539                 wl_resource_get_user_data(resource);
540
541         if (context->input)
542                 zwp_text_input_v1_send_preedit_cursor(context->input->resource,
543                                                       cursor);
544 }
545
546 static void
547 input_method_context_delete_surrounding_text(struct wl_client *client,
548                                              struct wl_resource *resource,
549                                              int32_t index,
550                                              uint32_t length)
551 {
552         struct input_method_context *context =
553                 wl_resource_get_user_data(resource);
554
555         if (context->input)
556                 zwp_text_input_v1_send_delete_surrounding_text(
557                         context->input->resource, index, length);
558 }
559
560 static void
561 input_method_context_cursor_position(struct wl_client *client,
562                                      struct wl_resource *resource,
563                                      int32_t index,
564                                      int32_t anchor)
565 {
566         struct input_method_context *context =
567                 wl_resource_get_user_data(resource);
568
569         if (context->input)
570                 zwp_text_input_v1_send_cursor_position(context->input->resource,
571                                                        index, anchor);
572 }
573
574 static void
575 input_method_context_modifiers_map(struct wl_client *client,
576                                    struct wl_resource *resource,
577                                    struct wl_array *map)
578 {
579         struct input_method_context *context =
580                 wl_resource_get_user_data(resource);
581
582         if (context->input)
583                 zwp_text_input_v1_send_modifiers_map(context->input->resource,
584                                                      map);
585 }
586
587 static void
588 input_method_context_keysym(struct wl_client *client,
589                             struct wl_resource *resource,
590                             uint32_t serial,
591                             uint32_t time,
592                             uint32_t sym,
593                             uint32_t state,
594                             uint32_t modifiers)
595 {
596         struct input_method_context *context =
597                 wl_resource_get_user_data(resource);
598
599         if (context->input)
600                 zwp_text_input_v1_send_keysym(context->input->resource,
601                                               serial, time,
602                                               sym, state, modifiers);
603 }
604
605 static void
606 unbind_keyboard(struct wl_resource *resource)
607 {
608         struct input_method_context *context =
609                 wl_resource_get_user_data(resource);
610
611         input_method_context_end_keyboard_grab(context);
612         context->keyboard = NULL;
613 }
614
615 static void
616 input_method_context_grab_key(struct weston_keyboard_grab *grab,
617                               const struct timespec *time, uint32_t key,
618                               uint32_t state_w)
619 {
620         struct weston_keyboard *keyboard = grab->keyboard;
621         struct wl_display *display;
622         uint32_t serial;
623         uint32_t msecs;
624
625         if (!keyboard->input_method_resource)
626                 return;
627
628         display = wl_client_get_display(
629                 wl_resource_get_client(keyboard->input_method_resource));
630         serial = wl_display_next_serial(display);
631         msecs = timespec_to_msec(time);
632         wl_keyboard_send_key(keyboard->input_method_resource,
633                              serial, msecs, key, state_w);
634 }
635
636 static void
637 input_method_context_grab_modifier(struct weston_keyboard_grab *grab,
638                                    uint32_t serial,
639                                    uint32_t mods_depressed,
640                                    uint32_t mods_latched,
641                                    uint32_t mods_locked,
642                                    uint32_t group)
643 {
644         struct weston_keyboard *keyboard = grab->keyboard;
645
646         if (!keyboard->input_method_resource)
647                 return;
648
649         wl_keyboard_send_modifiers(keyboard->input_method_resource,
650                                    serial, mods_depressed, mods_latched,
651                                    mods_locked, group);
652 }
653
654 static void
655 input_method_context_grab_cancel(struct weston_keyboard_grab *grab)
656 {
657         weston_keyboard_end_grab(grab->keyboard);
658 }
659
660 static const struct weston_keyboard_grab_interface input_method_context_grab = {
661         input_method_context_grab_key,
662         input_method_context_grab_modifier,
663         input_method_context_grab_cancel,
664 };
665
666 static void
667 input_method_context_grab_keyboard(struct wl_client *client,
668                                    struct wl_resource *resource,
669                                    uint32_t id)
670 {
671         struct input_method_context *context =
672                 wl_resource_get_user_data(resource);
673         struct wl_resource *cr;
674         struct weston_seat *seat = context->input_method->seat;
675         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
676
677         cr = wl_resource_create(client, &wl_keyboard_interface, 1, id);
678         wl_resource_set_implementation(cr, NULL, context, unbind_keyboard);
679
680         context->keyboard = cr;
681
682         wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
683                                 keyboard->xkb_info->keymap_fd,
684                                 keyboard->xkb_info->keymap_size);
685
686         if (keyboard->grab != &keyboard->default_grab) {
687                 weston_keyboard_end_grab(keyboard);
688         }
689         weston_keyboard_start_grab(keyboard, &keyboard->input_method_grab);
690         keyboard->input_method_resource = cr;
691 }
692
693 static void
694 input_method_context_key(struct wl_client *client,
695                          struct wl_resource *resource,
696                          uint32_t serial,
697                          uint32_t time,
698                          uint32_t key,
699                          uint32_t state_w)
700 {
701         struct input_method_context *context =
702                 wl_resource_get_user_data(resource);
703         struct weston_seat *seat = context->input_method->seat;
704         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
705         struct weston_keyboard_grab *default_grab = &keyboard->default_grab;
706         struct timespec ts;
707
708         timespec_from_msec(&ts, time);
709
710         default_grab->interface->key(default_grab, &ts, key, state_w);
711 }
712
713 static void
714 input_method_context_modifiers(struct wl_client *client,
715                                struct wl_resource *resource,
716                                uint32_t serial,
717                                uint32_t mods_depressed,
718                                uint32_t mods_latched,
719                                uint32_t mods_locked,
720                                uint32_t group)
721 {
722         struct input_method_context *context =
723                 wl_resource_get_user_data(resource);
724
725         struct weston_seat *seat = context->input_method->seat;
726         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
727         struct weston_keyboard_grab *default_grab = &keyboard->default_grab;
728
729         default_grab->interface->modifiers(default_grab,
730                                            serial, mods_depressed,
731                                            mods_latched, mods_locked,
732                                            group);
733 }
734
735 static void
736 input_method_context_language(struct wl_client *client,
737                               struct wl_resource *resource,
738                               uint32_t serial,
739                               const char *language)
740 {
741         struct input_method_context *context =
742                 wl_resource_get_user_data(resource);
743
744         if (context->input)
745                 zwp_text_input_v1_send_language(context->input->resource,
746                                                 serial, language);
747 }
748
749 static void
750 input_method_context_text_direction(struct wl_client *client,
751                                     struct wl_resource *resource,
752                                     uint32_t serial,
753                                     uint32_t direction)
754 {
755         struct input_method_context *context =
756                 wl_resource_get_user_data(resource);
757
758         if (context->input)
759                 zwp_text_input_v1_send_text_direction(context->input->resource,
760                                                       serial, direction);
761 }
762
763
764 static const struct zwp_input_method_context_v1_interface context_implementation = {
765         input_method_context_destroy,
766         input_method_context_commit_string,
767         input_method_context_preedit_string,
768         input_method_context_preedit_styling,
769         input_method_context_preedit_cursor,
770         input_method_context_delete_surrounding_text,
771         input_method_context_cursor_position,
772         input_method_context_modifiers_map,
773         input_method_context_keysym,
774         input_method_context_grab_keyboard,
775         input_method_context_key,
776         input_method_context_modifiers,
777         input_method_context_language,
778         input_method_context_text_direction
779 };
780
781 static void
782 destroy_input_method_context(struct wl_resource *resource)
783 {
784         struct input_method_context *context =
785                 wl_resource_get_user_data(resource);
786
787         if (context->keyboard)
788                 wl_resource_destroy(context->keyboard);
789
790         if (context->input_method && context->input_method->context == context)
791                 context->input_method->context = NULL;
792
793         free(context);
794 }
795
796 static void
797 input_method_context_create(struct text_input *input,
798                             struct input_method *input_method)
799 {
800         struct input_method_context *context;
801         struct wl_resource *binding;
802
803         if (!input_method->input_method_binding)
804                 return;
805
806         context = zalloc(sizeof *context);
807         if (context == NULL)
808                 return;
809
810         binding = input_method->input_method_binding;
811         context->resource =
812                 wl_resource_create(wl_resource_get_client(binding),
813                                    &zwp_input_method_context_v1_interface,
814                                    1, 0);
815         wl_resource_set_implementation(context->resource,
816                                        &context_implementation,
817                                        context, destroy_input_method_context);
818
819         context->input = input;
820         context->input_method = input_method;
821         input_method->context = context;
822
823
824         zwp_input_method_v1_send_activate(binding, context->resource);
825 }
826
827 static void
828 input_method_context_end_keyboard_grab(struct input_method_context *context)
829 {
830         struct weston_keyboard_grab *grab;
831         struct weston_keyboard *keyboard;
832
833         keyboard = weston_seat_get_keyboard(context->input_method->seat);
834         if (!keyboard)
835                 return;
836
837         grab = &keyboard->input_method_grab;
838         keyboard = grab->keyboard;
839         if (!keyboard)
840                 return;
841
842         if (keyboard->grab == grab)
843                 weston_keyboard_end_grab(keyboard);
844
845         keyboard->input_method_resource = NULL;
846 }
847
848 static void
849 unbind_input_method(struct wl_resource *resource)
850 {
851         struct input_method *input_method = wl_resource_get_user_data(resource);
852
853         input_method->input_method_binding = NULL;
854         input_method->context = NULL;
855 }
856
857 static void
858 bind_input_method(struct wl_client *client,
859                   void *data,
860                   uint32_t version,
861                   uint32_t id)
862 {
863         struct input_method *input_method = data;
864         struct text_backend *text_backend = input_method->text_backend;
865         struct wl_resource *resource;
866
867         resource =
868                 wl_resource_create(client,
869                                    &zwp_input_method_v1_interface, 1, id);
870
871         if (input_method->input_method_binding != NULL) {
872                 wl_resource_post_error(resource,
873                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
874                                        "interface object already bound");
875                 return;
876         }
877
878         if (text_backend->input_method.client != client) {
879                 wl_resource_post_error(resource,
880                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
881                                        "permission to bind "
882                                        "input_method denied");
883                 return;
884         }
885
886         wl_resource_set_implementation(resource, NULL, input_method,
887                                        unbind_input_method);
888         input_method->input_method_binding = resource;
889 }
890
891 static void
892 input_method_notifier_destroy(struct wl_listener *listener, void *data)
893 {
894         struct input_method *input_method =
895                 container_of(listener, struct input_method, destroy_listener);
896
897         if (input_method->input)
898                 deactivate_input_method(input_method);
899
900         wl_global_destroy(input_method->input_method_global);
901         wl_list_remove(&input_method->destroy_listener.link);
902
903         free(input_method);
904 }
905
906 static void
907 handle_keyboard_focus(struct wl_listener *listener, void *data)
908 {
909         struct weston_keyboard *keyboard = data;
910         struct input_method *input_method =
911                 container_of(listener, struct input_method,
912                              keyboard_focus_listener);
913         struct weston_surface *surface = keyboard->focus;
914
915         if (!input_method->input)
916                 return;
917
918         if (!surface || input_method->input->surface != surface)
919                 deactivate_input_method(input_method);
920 }
921
922 static void
923 input_method_init_seat(struct weston_seat *seat)
924 {
925         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
926
927         if (seat->input_method->focus_listener_initialized)
928                 return;
929
930         if (keyboard) {
931                 seat->input_method->keyboard_focus_listener.notify =
932                         handle_keyboard_focus;
933                 wl_signal_add(&keyboard->focus_signal,
934                               &seat->input_method->keyboard_focus_listener);
935                 keyboard->input_method_grab.interface =
936                         &input_method_context_grab;
937         }
938
939         seat->input_method->focus_listener_initialized = true;
940 }
941
942 static void launch_input_method(struct text_backend *text_backend);
943
944 static void
945 respawn_input_method_process(struct text_backend *text_backend)
946 {
947         struct timespec time;
948         int64_t tdiff;
949
950         /* if input_method dies more than 5 times in 10 seconds, give up */
951         weston_compositor_get_time(&time);
952         tdiff = timespec_sub_to_msec(&time,
953                                      &text_backend->input_method.deathstamp);
954         if (tdiff > 10000) {
955                 text_backend->input_method.deathstamp = time;
956                 text_backend->input_method.deathcount = 0;
957         }
958
959         text_backend->input_method.deathcount++;
960         if (text_backend->input_method.deathcount > 5) {
961                 weston_log("input_method disconnected, giving up.\n");
962                 return;
963         }
964
965         weston_log("input_method disconnected, respawning...\n");
966         launch_input_method(text_backend);
967 }
968
969 static void
970 input_method_client_notifier(struct wl_listener *listener, void *data)
971 {
972         struct text_backend *text_backend;
973
974         text_backend = container_of(listener, struct text_backend,
975                                     client_listener);
976
977         text_backend->input_method.client = NULL;
978         respawn_input_method_process(text_backend);
979 }
980
981 static void
982 launch_input_method(struct text_backend *text_backend)
983 {
984         if (!text_backend->input_method.path)
985                 return;
986
987         if (strcmp(text_backend->input_method.path, "") == 0)
988                 return;
989
990         text_backend->input_method.client =
991                 weston_client_start(text_backend->compositor,
992                                     text_backend->input_method.path);
993
994         if (!text_backend->input_method.client) {
995                 weston_log("not able to start %s\n",
996                            text_backend->input_method.path);
997                 return;
998         }
999
1000         text_backend->client_listener.notify = input_method_client_notifier;
1001         wl_client_add_destroy_listener(text_backend->input_method.client,
1002                                        &text_backend->client_listener);
1003 }
1004
1005 static void
1006 text_backend_seat_created(struct text_backend *text_backend,
1007                           struct weston_seat *seat)
1008 {
1009         struct input_method *input_method;
1010         struct weston_compositor *ec = seat->compositor;
1011
1012         input_method = zalloc(sizeof *input_method);
1013         if (input_method == NULL)
1014                 return;
1015
1016         input_method->seat = seat;
1017         input_method->input = NULL;
1018         input_method->focus_listener_initialized = false;
1019         input_method->context = NULL;
1020         input_method->text_backend = text_backend;
1021
1022         input_method->input_method_global =
1023                 wl_global_create(ec->wl_display,
1024                                  &zwp_input_method_v1_interface, 1,
1025                                  input_method, bind_input_method);
1026
1027         input_method->destroy_listener.notify = input_method_notifier_destroy;
1028         wl_signal_add(&seat->destroy_signal, &input_method->destroy_listener);
1029
1030         seat->input_method = input_method;
1031 }
1032
1033 static void
1034 handle_seat_created(struct wl_listener *listener, void *data)
1035 {
1036         struct weston_seat *seat = data;
1037         struct text_backend *text_backend =
1038                 container_of(listener, struct text_backend,
1039                              seat_created_listener);
1040
1041         text_backend_seat_created(text_backend, seat);
1042 }
1043
1044 static void
1045 text_backend_configuration(struct text_backend *text_backend)
1046 {
1047         struct weston_config *config = wet_get_config(text_backend->compositor);
1048         struct weston_config_section *section;
1049         char *client;
1050         int ret;
1051
1052         section = weston_config_get_section(config,
1053                                             "input-method", NULL, NULL);
1054         ret = asprintf(&client, "%s/weston-keyboard",
1055                        weston_config_get_libexec_dir());
1056         if (ret < 0)
1057                 client = NULL;
1058         weston_config_section_get_string(section, "path",
1059                                          &text_backend->input_method.path,
1060                                          client);
1061         free(client);
1062 }
1063
1064 WL_EXPORT void
1065 text_backend_destroy(struct text_backend *text_backend)
1066 {
1067         if (text_backend->input_method.client) {
1068                 /* disable respawn */
1069                 wl_list_remove(&text_backend->client_listener.link);
1070                 wl_client_destroy(text_backend->input_method.client);
1071         }
1072
1073         free(text_backend->input_method.path);
1074         free(text_backend);
1075 }
1076
1077 WL_EXPORT struct text_backend *
1078 text_backend_init(struct weston_compositor *ec)
1079 {
1080         struct text_backend *text_backend;
1081         struct weston_seat *seat;
1082
1083         text_backend = zalloc(sizeof(*text_backend));
1084         if (text_backend == NULL)
1085                 return NULL;
1086
1087         text_backend->compositor = ec;
1088
1089         text_backend_configuration(text_backend);
1090
1091         wl_list_for_each(seat, &ec->seat_list, link)
1092                 text_backend_seat_created(text_backend, seat);
1093         text_backend->seat_created_listener.notify = handle_seat_created;
1094         wl_signal_add(&ec->seat_created_signal,
1095                       &text_backend->seat_created_listener);
1096
1097         text_input_manager_create(ec);
1098
1099         launch_input_method(text_backend);
1100
1101         return text_backend;
1102 }