blob: b2f90947500673439835215ab7d1197075f00ceb [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"
Jan Arne Petersen30b66ef2012-09-09 23:08:41 +020028#include "input-method-server-protocol.h"
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020029
30struct input_method;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020031struct input_method_context;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020032
33struct text_model {
34 struct wl_resource resource;
35
Jan Arne Petersene829adc2012-08-10 16:47:22 +020036 struct weston_compositor *ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020037
Jan Arne Petersene829adc2012-08-10 16:47:22 +020038 struct wl_list input_methods;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020039
40 struct wl_surface *surface;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020041};
42
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +020043struct text_model_factory {
Jan Arne Petersen51963742012-08-10 16:47:20 +020044 struct wl_global *text_model_factory_global;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020045 struct wl_listener destroy_listener;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020046
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +020047 struct weston_compositor *ec;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +020048};
49
50struct input_method {
51 struct wl_resource *input_method_binding;
52 struct wl_global *input_method_global;
53 struct wl_listener destroy_listener;
54
55 struct weston_seat *seat;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020056 struct text_model *model;
57
58 struct wl_list link;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020059
60 struct wl_listener keyboard_focus_listener;
61
62 int focus_listener_initialized;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020063
64 struct input_method_context *context;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020065};
66
Jan Arne Petersen620cd622012-09-09 23:08:32 +020067struct input_method_context {
68 struct wl_resource resource;
69
70 struct text_model *model;
71
72 struct wl_list link;
73};
74
75static void input_method_context_create(struct text_model *model,
76 struct input_method *input_method);
77
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020078static void input_method_init_seat(struct weston_seat *seat);
79
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020080static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +020081deactivate_text_model(struct text_model *text_model,
82 struct input_method *input_method)
Philipp Brüschweiler17467812012-07-11 22:25:30 +020083{
Jan Arne Petersene829adc2012-08-10 16:47:22 +020084 struct weston_compositor *ec = text_model->ec;
Philipp Brüschweiler17467812012-07-11 22:25:30 +020085
Jan Arne Petersene829adc2012-08-10 16:47:22 +020086 if (input_method->model == text_model) {
Jan Arne Petersen620cd622012-09-09 23:08:32 +020087 if (input_method->input_method_binding)
88 input_method_send_deactivate(input_method->input_method_binding, &input_method->context->resource);
Jan Arne Petersene829adc2012-08-10 16:47:22 +020089 wl_list_remove(&input_method->link);
90 input_method->model = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020091 input_method->context = NULL;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040092 wl_signal_emit(&ec->hide_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +020093 text_model_send_deactivated(&text_model->resource);
Philipp Brüschweiler17467812012-07-11 22:25:30 +020094 }
95}
96
97static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020098destroy_text_model(struct wl_resource *resource)
99{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400100 struct text_model *text_model =
101 container_of(resource, struct text_model, resource);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200102 struct input_method *input_method, *next;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200103
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200104 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
105 deactivate_text_model(text_model, input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200106
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200107 free(text_model);
108}
109
110static void
111text_model_set_surrounding_text(struct wl_client *client,
112 struct wl_resource *resource,
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200113 const char *text,
114 uint32_t cursor,
115 uint32_t anchor)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200116{
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200117 struct text_model *text_model = resource->data;
118 struct input_method *input_method, *next;
119
120 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
121 if (!input_method->context)
122 continue;
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200123 input_method_context_send_surrounding_text(&input_method->context->resource,
124 text,
125 cursor,
126 anchor);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200127 }
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200128}
129
130static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200131text_model_activate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200132 struct wl_resource *resource,
133 struct wl_resource *seat,
134 struct wl_resource *surface)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200135{
136 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200137 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200138 struct input_method *input_method = weston_seat->input_method;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200139 struct text_model *old = weston_seat->input_method->model;
140 struct weston_compositor *ec = text_model->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200141
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200142 if (old == text_model)
143 return;
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200144
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200145 if (old) {
146 deactivate_text_model(old,
147 weston_seat->input_method);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200148 }
149
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200150 input_method->model = text_model;
151 wl_list_insert(&text_model->input_methods, &input_method->link);
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200152 input_method_init_seat(weston_seat);
153
154 text_model->surface = surface->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200155
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200156 input_method_context_create(text_model, input_method);
157
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400158 wl_signal_emit(&ec->show_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200159
160 text_model_send_activated(&text_model->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200161}
162
163static void
164text_model_deactivate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200165 struct wl_resource *resource,
166 struct wl_resource *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200167{
168 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200169 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200170
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200171 deactivate_text_model(text_model,
172 weston_seat->input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200173}
174
175static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200176text_model_set_micro_focus(struct wl_client *client,
177 struct wl_resource *resource,
178 int32_t x,
179 int32_t y,
180 int32_t width,
181 int32_t height)
182{
183}
184
185static void
186text_model_set_preedit(struct wl_client *client,
187 struct wl_resource *resource)
188{
189}
190
191static void
192text_model_set_content_type(struct wl_client *client,
193 struct wl_resource *resource)
194{
195}
196
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200197static const struct text_model_interface text_model_implementation = {
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200198 text_model_set_surrounding_text,
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200199 text_model_activate,
200 text_model_deactivate,
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200201 text_model_set_micro_focus,
202 text_model_set_preedit,
203 text_model_set_content_type
204};
205
Jan Arne Petersen51963742012-08-10 16:47:20 +0200206static void text_model_factory_create_text_model(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200207 struct wl_resource *resource,
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200208 uint32_t id)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200209{
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200210 struct text_model_factory *text_model_factory = resource->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200211 struct text_model *text_model;
212
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200213 text_model = calloc(1, sizeof *text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200214
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200215 text_model->resource.object.id = id;
216 text_model->resource.object.interface = &text_model_interface;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200217 text_model->resource.object.implementation =
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200218 (void (**)(void)) &text_model_implementation;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200219
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200220 text_model->resource.data = text_model;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200221 text_model->resource.destroy = destroy_text_model;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200222
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200223 text_model->ec = text_model_factory->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200224
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200225 wl_list_init(&text_model->input_methods);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200226
227 wl_client_add_resource(client, &text_model->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200228};
229
Jan Arne Petersen51963742012-08-10 16:47:20 +0200230static const struct text_model_factory_interface text_model_factory_implementation = {
231 text_model_factory_create_text_model
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200232};
233
234static void
Jan Arne Petersen51963742012-08-10 16:47:20 +0200235bind_text_model_factory(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200236 void *data,
237 uint32_t version,
238 uint32_t id)
239{
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200240 struct text_model_factory *text_model_factory = data;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200241
242 /* No checking for duplicate binding necessary.
243 * No events have to be sent, so we don't need the return value. */
Jan Arne Petersen51963742012-08-10 16:47:20 +0200244 wl_client_add_object(client, &text_model_factory_interface,
245 &text_model_factory_implementation,
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200246 id, text_model_factory);
247}
248
249static void
250text_model_factory_notifier_destroy(struct wl_listener *listener, void *data)
251{
252 struct text_model_factory *text_model_factory =
253 container_of(listener, struct text_model_factory, destroy_listener);
254
255 wl_display_remove_global(text_model_factory->ec->wl_display,
256 text_model_factory->text_model_factory_global);
257
258 free(text_model_factory);
259}
260
261WL_EXPORT void
262text_model_factory_create(struct weston_compositor *ec)
263{
264 struct text_model_factory *text_model_factory;
265
266 text_model_factory = calloc(1, sizeof *text_model_factory);
267
268 text_model_factory->ec = ec;
269
270 text_model_factory->text_model_factory_global =
271 wl_display_add_global(ec->wl_display,
272 &text_model_factory_interface,
273 text_model_factory, bind_text_model_factory);
274
275 text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy;
276 wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener);
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200277}
278
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200279static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200280input_method_context_destroy(struct wl_client *client,
281 struct wl_resource *resource)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200282{
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200283 wl_resource_destroy(resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200284}
285
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200286static void
287input_method_context_commit_string(struct wl_client *client,
288 struct wl_resource *resource,
289 const char *text,
290 uint32_t index)
291{
292 struct input_method_context *context = resource->data;
293
294 text_model_send_commit_string(&context->model->resource, text, index);
295}
296
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200297static void
298input_method_context_preedit_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_preedit_string(&context->model->resource, text, index);
306}
307
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200308static void
309input_method_context_delete_surrounding_text(struct wl_client *client,
310 struct wl_resource *resource,
311 int32_t index,
312 uint32_t length)
313{
314 struct input_method_context *context = resource->data;
315
316 text_model_send_delete_surrounding_text(&context->model->resource, index, length);
317}
318
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200319static const struct input_method_context_interface input_method_context_implementation = {
320 input_method_context_destroy,
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200321 input_method_context_commit_string,
322 input_method_context_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200323 input_method_context_delete_surrounding_text
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200324};
325
326static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200327destroy_input_method_context(struct wl_resource *resource)
328{
329 struct input_method_context *context = resource->data;
330
331 free(context);
332}
333
334static void
335input_method_context_create(struct text_model *model,
336 struct input_method *input_method)
337{
338 struct input_method_context *context;
339
340 if (!input_method->input_method_binding)
341 return;
342
343 context = malloc(sizeof *context);
344 if (context == NULL)
345 return;
346
347 context->resource.destroy = destroy_input_method_context;
348 context->resource.object.id = 0;
349 context->resource.object.interface = &input_method_context_interface;
350 context->resource.object.implementation =
351 (void (**)(void)) &input_method_context_implementation;
352 context->resource.data = context;
353 wl_signal_init(&context->resource.destroy_signal);
354
355 context->model = model;
356 input_method->context = context;
357
358 wl_client_add_resource(input_method->input_method_binding->client, &context->resource);
359
360 input_method_send_activate(input_method->input_method_binding, &context->resource);
361}
362
363static void
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200364unbind_input_method(struct wl_resource *resource)
365{
366 struct input_method *input_method = resource->data;
367
368 input_method->input_method_binding = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200369 input_method->context = NULL;
370
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200371 free(resource);
372}
373
374static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200375bind_input_method(struct wl_client *client,
376 void *data,
377 uint32_t version,
378 uint32_t id)
379{
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200380 struct input_method *input_method = data;
381 struct wl_resource *resource;
382
383 resource = wl_client_add_object(client, &input_method_interface,
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200384 NULL,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200385 id, input_method);
386
387 if (input_method->input_method_binding == NULL) {
388 resource->destroy = unbind_input_method;
389 input_method->input_method_binding = resource;
390 return;
391 }
392
393 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
394 "interface object already bound");
395 wl_resource_destroy(resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200396}
397
398static void
399input_method_notifier_destroy(struct wl_listener *listener, void *data)
400{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400401 struct input_method *input_method =
402 container_of(listener, struct input_method, destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200403
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200404 if (input_method->model)
405 deactivate_text_model(input_method->model, input_method);
406
407 wl_display_remove_global(input_method->seat->compositor->wl_display,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200408 input_method->input_method_global);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200409
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200410 free(input_method);
411}
412
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200413static void
414handle_keyboard_focus(struct wl_listener *listener, void *data)
415{
416 struct wl_keyboard *keyboard = data;
417 struct input_method *input_method =
418 container_of(listener, struct input_method, keyboard_focus_listener);
419 struct wl_surface *surface = keyboard->focus;
420
421 if (!input_method->model)
422 return;
423
424 if (!surface || input_method->model->surface != surface)
425 deactivate_text_model(input_method->model,
426 input_method);
427}
428
429static void
430input_method_init_seat(struct weston_seat *seat)
431{
432 if (seat->input_method->focus_listener_initialized)
433 return;
434
435 if (seat->has_keyboard) {
436 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
437 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
438 }
439
440 seat->input_method->focus_listener_initialized = 1;
441}
442
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200443WL_EXPORT void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200444input_method_create(struct weston_compositor *ec,
445 struct weston_seat *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200446{
447 struct input_method *input_method;
448
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200449 input_method = calloc(1, sizeof *input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200450
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200451 input_method->seat = seat;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200452 input_method->model = NULL;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200453 input_method->focus_listener_initialized = 0;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200454 input_method->context = NULL;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200455
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200456 input_method->input_method_global =
457 wl_display_add_global(ec->wl_display,
458 &input_method_interface,
459 input_method, bind_input_method);
460
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200461 input_method->destroy_listener.notify = input_method_notifier_destroy;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200462 wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200463
464 seat->input_method = input_method;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200465}
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200466