blob: 7ae023aeda986ee5b1b077c114c9e2b6d0947785 [file] [log] [blame]
Jan Arne Petersen1f17be42012-06-21 21:52:18 +02001/*
2 * Copyright © 2012 Openismus GmbH
Jan Arne Petersen4c265182012-09-09 23:08:30 +02003 * Copyright © 2012 Intel Corporation
Jan Arne Petersen1f17be42012-06-21 21:52:18 +02004 *
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
29struct input_method;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020030struct input_method_context;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020031
32struct text_model {
33 struct wl_resource resource;
34
Jan Arne Petersene829adc2012-08-10 16:47:22 +020035 struct weston_compositor *ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020036
Jan Arne Petersene829adc2012-08-10 16:47:22 +020037 struct wl_list input_methods;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020038
39 struct wl_surface *surface;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020040};
41
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +020042struct text_model_factory {
Jan Arne Petersen51963742012-08-10 16:47:20 +020043 struct wl_global *text_model_factory_global;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020044 struct wl_listener destroy_listener;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020045
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +020046 struct weston_compositor *ec;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +020047};
48
49struct 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;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020055 struct text_model *model;
56
57 struct wl_list link;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020058
59 struct wl_listener keyboard_focus_listener;
60
61 int focus_listener_initialized;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020062
63 struct input_method_context *context;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020064};
65
Jan Arne Petersen620cd622012-09-09 23:08:32 +020066struct input_method_context {
67 struct wl_resource resource;
68
69 struct text_model *model;
70
71 struct wl_list link;
72};
73
74static void input_method_context_create(struct text_model *model,
75 struct input_method *input_method);
76
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020077static void input_method_init_seat(struct weston_seat *seat);
78
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020079static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +020080deactivate_text_model(struct text_model *text_model,
81 struct input_method *input_method)
Philipp Brüschweiler17467812012-07-11 22:25:30 +020082{
Jan Arne Petersene829adc2012-08-10 16:47:22 +020083 struct weston_compositor *ec = text_model->ec;
Philipp Brüschweiler17467812012-07-11 22:25:30 +020084
Jan Arne Petersene829adc2012-08-10 16:47:22 +020085 if (input_method->model == text_model) {
Jan Arne Petersen620cd622012-09-09 23:08:32 +020086 if (input_method->input_method_binding)
87 input_method_send_deactivate(input_method->input_method_binding, &input_method->context->resource);
Jan Arne Petersene829adc2012-08-10 16:47:22 +020088 wl_list_remove(&input_method->link);
89 input_method->model = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020090 input_method->context = NULL;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040091 wl_signal_emit(&ec->hide_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +020092 text_model_send_deactivated(&text_model->resource);
Philipp Brüschweiler17467812012-07-11 22:25:30 +020093 }
94}
95
96static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020097destroy_text_model(struct wl_resource *resource)
98{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040099 struct text_model *text_model =
100 container_of(resource, struct text_model, resource);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200101 struct input_method *input_method, *next;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200102
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200103 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
104 deactivate_text_model(text_model, input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200105
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200106 free(text_model);
107}
108
109static void
110text_model_set_surrounding_text(struct wl_client *client,
111 struct wl_resource *resource,
112 const char *text)
113{
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200114 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 }
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200122}
123
124static void
125text_model_set_cursor_index(struct wl_client *client,
126 struct wl_resource *resource,
127 uint32_t index)
128{
129}
130
131static void
132text_model_activate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200133 struct wl_resource *resource,
134 struct wl_resource *seat,
135 struct wl_resource *surface)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200136{
137 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200138 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200139 struct input_method *input_method = weston_seat->input_method;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200140 struct text_model *old = weston_seat->input_method->model;
141 struct weston_compositor *ec = text_model->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200142
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200143 if (old == text_model)
144 return;
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200145
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200146 if (old) {
147 deactivate_text_model(old,
148 weston_seat->input_method);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200149 }
150
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200151 input_method->model = text_model;
152 wl_list_insert(&text_model->input_methods, &input_method->link);
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200153 input_method_init_seat(weston_seat);
154
155 text_model->surface = surface->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200156
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200157 input_method_context_create(text_model, input_method);
158
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400159 wl_signal_emit(&ec->show_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200160
161 text_model_send_activated(&text_model->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200162}
163
164static void
165text_model_deactivate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200166 struct wl_resource *resource,
167 struct wl_resource *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200168{
169 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200170 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200171
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200172 deactivate_text_model(text_model,
173 weston_seat->input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200174}
175
176static void
177text_model_set_selected_text(struct wl_client *client,
178 struct wl_resource *resource,
179 const char *text,
180 int32_t index)
181{
182}
183
184static void
185text_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
194static void
195text_model_set_preedit(struct wl_client *client,
196 struct wl_resource *resource)
197{
198}
199
200static void
201text_model_set_content_type(struct wl_client *client,
202 struct wl_resource *resource)
203{
204}
205
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200206static const struct text_model_interface text_model_implementation = {
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200207 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
Jan Arne Petersen51963742012-08-10 16:47:20 +0200217static void text_model_factory_create_text_model(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200218 struct wl_resource *resource,
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200219 uint32_t id)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200220{
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200221 struct text_model_factory *text_model_factory = resource->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200222 struct text_model *text_model;
223
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200224 text_model = calloc(1, sizeof *text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200225
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200226 text_model->resource.object.id = id;
227 text_model->resource.object.interface = &text_model_interface;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200228 text_model->resource.object.implementation =
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200229 (void (**)(void)) &text_model_implementation;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200230
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200231 text_model->resource.data = text_model;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200232 text_model->resource.destroy = destroy_text_model;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200233
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200234 text_model->ec = text_model_factory->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200235
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200236 wl_list_init(&text_model->input_methods);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200237
238 wl_client_add_resource(client, &text_model->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200239};
240
Jan Arne Petersen51963742012-08-10 16:47:20 +0200241static const struct text_model_factory_interface text_model_factory_implementation = {
242 text_model_factory_create_text_model
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200243};
244
245static void
Jan Arne Petersen51963742012-08-10 16:47:20 +0200246bind_text_model_factory(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200247 void *data,
248 uint32_t version,
249 uint32_t id)
250{
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200251 struct text_model_factory *text_model_factory = data;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200252
253 /* No checking for duplicate binding necessary.
254 * No events have to be sent, so we don't need the return value. */
Jan Arne Petersen51963742012-08-10 16:47:20 +0200255 wl_client_add_object(client, &text_model_factory_interface,
256 &text_model_factory_implementation,
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200257 id, text_model_factory);
258}
259
260static void
261text_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
272WL_EXPORT void
273text_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);
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200288}
289
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200290static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200291input_method_context_destroy(struct wl_client *client,
292 struct wl_resource *resource)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200293{
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200294 wl_resource_destroy(resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200295}
296
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200297static void
298input_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
308static const struct input_method_context_interface input_method_context_implementation = {
309 input_method_context_destroy,
310 input_method_context_commit_string
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200311};
312
313static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200314destroy_input_method_context(struct wl_resource *resource)
315{
316 struct input_method_context *context = resource->data;
317
318 free(context);
319}
320
321static void
322input_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
350static void
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200351unbind_input_method(struct wl_resource *resource)
352{
353 struct input_method *input_method = resource->data;
354
355 input_method->input_method_binding = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200356 input_method->context = NULL;
357
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200358 free(resource);
359}
360
361static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200362bind_input_method(struct wl_client *client,
363 void *data,
364 uint32_t version,
365 uint32_t id)
366{
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200367 struct input_method *input_method = data;
368 struct wl_resource *resource;
369
370 resource = wl_client_add_object(client, &input_method_interface,
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200371 NULL,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200372 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);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200383}
384
385static void
386input_method_notifier_destroy(struct wl_listener *listener, void *data)
387{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400388 struct input_method *input_method =
389 container_of(listener, struct input_method, destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200390
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200391 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,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200395 input_method->input_method_global);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200396
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200397 free(input_method);
398}
399
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200400static void
401handle_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
416static void
417input_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
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200430WL_EXPORT void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200431input_method_create(struct weston_compositor *ec,
432 struct weston_seat *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200433{
434 struct input_method *input_method;
435
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200436 input_method = calloc(1, sizeof *input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200437
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200438 input_method->seat = seat;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200439 input_method->model = NULL;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200440 input_method->focus_listener_initialized = 0;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200441 input_method->context = NULL;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200442
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200443 input_method->input_method_global =
444 wl_display_add_global(ec->wl_display,
445 &input_method_interface,
446 input_method, bind_input_method);
447
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200448 input_method->destroy_listener.notify = input_method_notifier_destroy;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200449 wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200450
451 seat->input_method = input_method;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200452}
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200453