text: Assign text_model to a wl_seat
[profile/ivi/weston.git] / src / text-backend.c
1 /*
2  * Copyright © 2012 Openismus GmbH
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <stdlib.h>
24
25 #include "compositor.h"
26 #include "text-server-protocol.h"
27
28 struct input_method;
29
30 struct text_model {
31         struct wl_resource resource;
32
33         struct weston_compositor *ec;
34
35         struct wl_list input_methods;
36 };
37
38 struct input_method {
39         struct wl_resource *input_method_binding;
40         struct wl_global *input_method_global;
41         struct wl_global *text_model_factory_global;
42         struct wl_listener destroy_listener;
43
44         struct weston_compositor *ec;
45         struct text_model *model;
46
47         struct wl_list link;
48 };
49
50 static void
51 deactivate_text_model(struct text_model *text_model,
52                       struct input_method *input_method)
53 {
54         struct weston_compositor *ec = text_model->ec;
55
56         if (input_method->model == text_model) {
57                 wl_list_remove(&input_method->link);
58                 input_method->model = NULL;
59                 wl_signal_emit(&ec->hide_input_panel_signal, ec);
60                 text_model_send_deactivated(&text_model->resource);
61         }
62 }
63
64 static void
65 destroy_text_model(struct wl_resource *resource)
66 {
67         struct text_model *text_model =
68                 container_of(resource, struct text_model, resource);
69         struct input_method *input_method, *next;
70
71         wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
72                 deactivate_text_model(text_model, input_method);
73
74         free(text_model);
75 }
76
77 static void
78 text_model_set_surrounding_text(struct wl_client *client,
79                                 struct wl_resource *resource,
80                                 const char *text)
81 {
82 }
83
84 static void
85 text_model_set_cursor_index(struct wl_client *client,
86                             struct wl_resource *resource,
87                             uint32_t index)
88 {
89 }
90
91 static void
92 text_model_activate(struct wl_client *client,
93                     struct wl_resource *resource,
94                     struct wl_resource *seat,
95                     struct wl_resource *surface)
96 {
97         struct text_model *text_model = resource->data;
98         struct weston_seat *weston_seat = seat->data;
99         struct text_model *old = weston_seat->input_method->model;
100         struct weston_compositor *ec = text_model->ec;
101
102         if (old == text_model)
103                 return;
104
105         if (old) {
106                 deactivate_text_model(old,
107                                       weston_seat->input_method);
108         }
109
110         weston_seat->input_method->model = text_model;
111         wl_list_insert(&text_model->input_methods, &weston_seat->input_method->link);
112
113         wl_signal_emit(&ec->show_input_panel_signal, ec);
114
115         text_model_send_activated(&text_model->resource);
116 }
117
118 static void
119 text_model_deactivate(struct wl_client *client,
120                       struct wl_resource *resource,
121                       struct wl_resource *seat)
122 {
123         struct text_model *text_model = resource->data;
124         struct weston_seat *weston_seat = seat->data;
125
126         deactivate_text_model(text_model,
127                               weston_seat->input_method);
128 }
129
130 static void
131 text_model_set_selected_text(struct wl_client *client,
132                              struct wl_resource *resource,
133                              const char *text,
134                              int32_t index)
135 {
136 }
137
138 static void
139 text_model_set_micro_focus(struct wl_client *client,
140                            struct wl_resource *resource,
141                            int32_t x,
142                            int32_t y,
143                            int32_t width,
144                            int32_t height)
145 {
146 }
147
148 static void
149 text_model_set_preedit(struct wl_client *client,
150                        struct wl_resource *resource)
151 {
152 }
153
154 static void
155 text_model_set_content_type(struct wl_client *client,
156                             struct wl_resource *resource)
157 {
158 }
159
160 static const struct text_model_interface text_model_implementation = {
161         text_model_set_surrounding_text,
162         text_model_set_cursor_index,
163         text_model_activate,
164         text_model_deactivate,
165         text_model_set_selected_text,
166         text_model_set_micro_focus,
167         text_model_set_preedit,
168         text_model_set_content_type
169 };
170
171 static void text_model_factory_create_text_model(struct wl_client *client,
172                                                  struct wl_resource *resource,
173                                                  uint32_t id,
174                                                  struct wl_resource *surface)
175 {
176         struct input_method *input_method = resource->data;
177         struct text_model *text_model;
178
179         text_model = calloc(1, sizeof *text_model);
180
181         text_model->resource.destroy = destroy_text_model;
182
183         text_model->resource.object.id = id;
184         text_model->resource.object.interface = &text_model_interface;
185         text_model->resource.object.implementation =
186                 (void (**)(void)) &text_model_implementation;
187         text_model->resource.data = text_model;
188
189         text_model->ec = input_method->ec;
190
191         wl_client_add_resource(client, &text_model->resource);
192
193         wl_list_init(&text_model->input_methods);
194 };
195
196 static const struct text_model_factory_interface text_model_factory_implementation = {
197         text_model_factory_create_text_model
198 };
199
200 static void
201 bind_text_model_factory(struct wl_client *client,
202                         void *data,
203                         uint32_t version,
204                         uint32_t id)
205 {
206         struct input_method *input_method = data;
207
208         /* No checking for duplicate binding necessary.
209          * No events have to be sent, so we don't need the return value. */
210         wl_client_add_object(client, &text_model_factory_interface,
211                              &text_model_factory_implementation,
212                              id, input_method);
213 }
214
215 static void
216 input_method_commit_string(struct wl_client *client,
217                            struct wl_resource *resource,
218                            const char *text,
219                            uint32_t index)
220 {
221         struct input_method *input_method = resource->data;
222
223         if (input_method->model) {
224                 text_model_send_commit_string(&input_method->model->resource, text, index);
225         }
226 }
227
228 static const struct input_method_interface input_method_implementation = {
229         input_method_commit_string
230 };
231
232 static void
233 unbind_input_method(struct wl_resource *resource)
234 {
235         struct input_method *input_method = resource->data;
236
237         input_method->input_method_binding = NULL;
238         free(resource);
239 }
240
241 static void
242 bind_input_method(struct wl_client *client,
243                   void *data,
244                   uint32_t version,
245                   uint32_t id)
246 {
247         struct input_method *input_method = data;
248         struct wl_resource *resource;
249
250         resource = wl_client_add_object(client, &input_method_interface,
251                                         &input_method_implementation,
252                                         id, input_method);
253
254         if (input_method->input_method_binding == NULL) {
255                 resource->destroy = unbind_input_method;
256                 input_method->input_method_binding = resource;
257                 return;
258         }
259
260         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
261                                "interface object already bound");
262         wl_resource_destroy(resource);
263 }
264
265 static void
266 input_method_notifier_destroy(struct wl_listener *listener, void *data)
267 {
268         struct input_method *input_method =
269                 container_of(listener, struct input_method, destroy_listener);
270
271         wl_display_remove_global(input_method->ec->wl_display,
272                                  input_method->input_method_global);
273         wl_display_remove_global(input_method->ec->wl_display,
274                                  input_method->text_model_factory_global);
275         free(input_method);
276 }
277
278 void
279 input_method_create(struct weston_compositor *ec,
280                     struct weston_seat *seat)
281 {
282         struct input_method *input_method;
283
284         input_method = calloc(1, sizeof *input_method);
285
286         input_method->ec = ec;
287         input_method->model = NULL;
288
289         input_method->input_method_global =
290                 wl_display_add_global(ec->wl_display,
291                                       &input_method_interface,
292                                       input_method, bind_input_method);
293
294         input_method->text_model_factory_global =
295                 wl_display_add_global(ec->wl_display,
296                                       &text_model_factory_interface,
297                                       input_method, bind_text_model_factory);
298
299         input_method->destroy_listener.notify = input_method_notifier_destroy;
300         wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
301
302         seat->input_method = input_method;
303 }