blob: d123da9c6ae4be15412e7812a6b0212cc4ab2ad8 [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>
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +010025#include <string.h>
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020026
27#include "compositor.h"
28#include "text-server-protocol.h"
Jan Arne Petersen30b66ef2012-09-09 23:08:41 +020029#include "input-method-server-protocol.h"
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020030
31struct input_method;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020032struct input_method_context;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +010033struct text_backend;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020034
35struct text_model {
36 struct wl_resource resource;
37
Jan Arne Petersene829adc2012-08-10 16:47:22 +020038 struct weston_compositor *ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020039
Jan Arne Petersene829adc2012-08-10 16:47:22 +020040 struct wl_list input_methods;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020041
42 struct wl_surface *surface;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020043};
44
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +020045struct text_model_factory {
Jan Arne Petersen51963742012-08-10 16:47:20 +020046 struct wl_global *text_model_factory_global;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020047 struct wl_listener destroy_listener;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020048
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +020049 struct weston_compositor *ec;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +020050};
51
52struct input_method {
53 struct wl_resource *input_method_binding;
54 struct wl_global *input_method_global;
55 struct wl_listener destroy_listener;
56
57 struct weston_seat *seat;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020058 struct text_model *model;
59
60 struct wl_list link;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020061
62 struct wl_listener keyboard_focus_listener;
63
64 int focus_listener_initialized;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020065
66 struct input_method_context *context;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +010067
68 struct text_backend *text_backend;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020069};
70
Jan Arne Petersen620cd622012-09-09 23:08:32 +020071struct input_method_context {
72 struct wl_resource resource;
73
74 struct text_model *model;
Jan Arne Petersen466b9c12012-11-18 19:06:45 +010075 struct input_method *input_method;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020076
77 struct wl_list link;
Jan Arne Petersen466b9c12012-11-18 19:06:45 +010078
79 struct wl_resource *keyboard;
80 struct wl_keyboard_grab grab;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020081};
82
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +010083struct text_backend {
84 struct weston_compositor *compositor;
85
86 struct {
87 char *path;
88 struct wl_resource *binding;
89 struct weston_process process;
90 struct wl_client *client;
91 } input_method;
92
93 struct wl_listener seat_created_listener;
94 struct wl_listener destroy_listener;
95};
96
Jan Arne Petersen620cd622012-09-09 23:08:32 +020097static void input_method_context_create(struct text_model *model,
98 struct input_method *input_method);
Jan Arne Petersen466b9c12012-11-18 19:06:45 +010099static void input_method_context_end_keyboard_grab(struct input_method_context *context);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200100
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200101static void input_method_init_seat(struct weston_seat *seat);
102
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200103static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200104deactivate_text_model(struct text_model *text_model,
105 struct input_method *input_method)
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200106{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200107 struct weston_compositor *ec = text_model->ec;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200108
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200109 if (input_method->model == text_model) {
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100110 if (input_method->context && input_method->input_method_binding) {
111 input_method_context_end_keyboard_grab(input_method->context);
112 input_method_send_deactivate(input_method->input_method_binding,
113 &input_method->context->resource);
114 }
115
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200116 wl_list_remove(&input_method->link);
117 input_method->model = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200118 input_method->context = NULL;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400119 wl_signal_emit(&ec->hide_input_panel_signal, ec);
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200120 text_model_send_leave(&text_model->resource);
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200121 }
122}
123
124static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200125destroy_text_model(struct wl_resource *resource)
126{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400127 struct text_model *text_model =
128 container_of(resource, struct text_model, resource);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200129 struct input_method *input_method, *next;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200130
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200131 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
132 deactivate_text_model(text_model, input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200133
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200134 free(text_model);
135}
136
137static void
138text_model_set_surrounding_text(struct wl_client *client,
139 struct wl_resource *resource,
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200140 const char *text,
141 uint32_t cursor,
142 uint32_t anchor)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200143{
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200144 struct text_model *text_model = resource->data;
145 struct input_method *input_method, *next;
146
147 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
148 if (!input_method->context)
149 continue;
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200150 input_method_context_send_surrounding_text(&input_method->context->resource,
151 text,
152 cursor,
153 anchor);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200154 }
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200155}
156
157static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200158text_model_activate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200159 struct wl_resource *resource,
160 struct wl_resource *seat,
161 struct wl_resource *surface)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200162{
163 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200164 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200165 struct input_method *input_method = weston_seat->input_method;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200166 struct text_model *old = weston_seat->input_method->model;
167 struct weston_compositor *ec = text_model->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200168
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200169 if (old == text_model)
170 return;
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200171
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200172 if (old) {
173 deactivate_text_model(old,
174 weston_seat->input_method);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200175 }
176
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200177 input_method->model = text_model;
178 wl_list_insert(&text_model->input_methods, &input_method->link);
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200179 input_method_init_seat(weston_seat);
180
181 text_model->surface = surface->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200182
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200183 input_method_context_create(text_model, input_method);
184
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400185 wl_signal_emit(&ec->show_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200186
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200187 text_model_send_enter(&text_model->resource, &text_model->surface->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200188}
189
190static void
191text_model_deactivate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200192 struct wl_resource *resource,
193 struct wl_resource *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200194{
195 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200196 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200197
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200198 deactivate_text_model(text_model,
199 weston_seat->input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200200}
201
202static void
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200203text_model_reset(struct wl_client *client,
204 struct wl_resource *resource)
205{
206 struct text_model *text_model = resource->data;
207 struct input_method *input_method, *next;
208
209 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
210 if (!input_method->context)
211 continue;
212 input_method_context_send_reset(&input_method->context->resource);
213 }
214}
215
216static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200217text_model_set_micro_focus(struct wl_client *client,
218 struct wl_resource *resource,
219 int32_t x,
220 int32_t y,
221 int32_t width,
222 int32_t height)
223{
224}
225
226static void
227text_model_set_preedit(struct wl_client *client,
228 struct wl_resource *resource)
229{
230}
231
232static void
233text_model_set_content_type(struct wl_client *client,
234 struct wl_resource *resource)
235{
236}
237
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200238static const struct text_model_interface text_model_implementation = {
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200239 text_model_set_surrounding_text,
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200240 text_model_activate,
241 text_model_deactivate,
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200242 text_model_reset,
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200243 text_model_set_micro_focus,
244 text_model_set_preedit,
245 text_model_set_content_type
246};
247
Jan Arne Petersen51963742012-08-10 16:47:20 +0200248static void text_model_factory_create_text_model(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200249 struct wl_resource *resource,
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200250 uint32_t id)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200251{
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200252 struct text_model_factory *text_model_factory = resource->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200253 struct text_model *text_model;
254
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200255 text_model = calloc(1, sizeof *text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200256
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200257 text_model->resource.object.id = id;
258 text_model->resource.object.interface = &text_model_interface;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200259 text_model->resource.object.implementation =
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200260 (void (**)(void)) &text_model_implementation;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200261
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200262 text_model->resource.data = text_model;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200263 text_model->resource.destroy = destroy_text_model;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200264
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200265 text_model->ec = text_model_factory->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200266
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200267 wl_list_init(&text_model->input_methods);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200268
269 wl_client_add_resource(client, &text_model->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200270};
271
Jan Arne Petersen51963742012-08-10 16:47:20 +0200272static const struct text_model_factory_interface text_model_factory_implementation = {
273 text_model_factory_create_text_model
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200274};
275
276static void
Jan Arne Petersen51963742012-08-10 16:47:20 +0200277bind_text_model_factory(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200278 void *data,
279 uint32_t version,
280 uint32_t id)
281{
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200282 struct text_model_factory *text_model_factory = data;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200283
284 /* No checking for duplicate binding necessary.
285 * No events have to be sent, so we don't need the return value. */
Jan Arne Petersen51963742012-08-10 16:47:20 +0200286 wl_client_add_object(client, &text_model_factory_interface,
287 &text_model_factory_implementation,
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200288 id, text_model_factory);
289}
290
291static void
292text_model_factory_notifier_destroy(struct wl_listener *listener, void *data)
293{
294 struct text_model_factory *text_model_factory =
295 container_of(listener, struct text_model_factory, destroy_listener);
296
297 wl_display_remove_global(text_model_factory->ec->wl_display,
298 text_model_factory->text_model_factory_global);
299
300 free(text_model_factory);
301}
302
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100303static void
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200304text_model_factory_create(struct weston_compositor *ec)
305{
306 struct text_model_factory *text_model_factory;
307
308 text_model_factory = calloc(1, sizeof *text_model_factory);
309
310 text_model_factory->ec = ec;
311
312 text_model_factory->text_model_factory_global =
313 wl_display_add_global(ec->wl_display,
314 &text_model_factory_interface,
315 text_model_factory, bind_text_model_factory);
316
317 text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy;
318 wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener);
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200319}
320
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200321static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200322input_method_context_destroy(struct wl_client *client,
323 struct wl_resource *resource)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200324{
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200325 wl_resource_destroy(resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200326}
327
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200328static void
329input_method_context_commit_string(struct wl_client *client,
330 struct wl_resource *resource,
331 const char *text,
332 uint32_t index)
333{
334 struct input_method_context *context = resource->data;
335
336 text_model_send_commit_string(&context->model->resource, text, index);
337}
338
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200339static void
340input_method_context_preedit_string(struct wl_client *client,
341 struct wl_resource *resource,
342 const char *text,
343 uint32_t index)
344{
345 struct input_method_context *context = resource->data;
346
347 text_model_send_preedit_string(&context->model->resource, text, index);
348}
349
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200350static void
351input_method_context_delete_surrounding_text(struct wl_client *client,
352 struct wl_resource *resource,
353 int32_t index,
354 uint32_t length)
355{
356 struct input_method_context *context = resource->data;
357
358 text_model_send_delete_surrounding_text(&context->model->resource, index, length);
359}
360
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200361static void
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100362input_method_context_modifiers_map(struct wl_client *client,
363 struct wl_resource *resource,
364 struct wl_array *map)
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200365{
366 struct input_method_context *context = resource->data;
367
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100368 text_model_send_modifiers_map(&context->model->resource, map);
369}
370
371static void
372input_method_context_keysym(struct wl_client *client,
373 struct wl_resource *resource,
374 uint32_t serial,
375 uint32_t time,
376 uint32_t sym,
377 uint32_t state,
378 uint32_t modifiers)
379{
380 struct input_method_context *context = resource->data;
381
382 text_model_send_keysym(&context->model->resource, serial, time,
383 sym, state, modifiers);
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200384}
385
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100386static void
387unbind_keyboard(struct wl_resource *resource)
388{
389 struct input_method_context *context = resource->data;
390
391 input_method_context_end_keyboard_grab(context);
392 context->keyboard = NULL;
393
394 free(resource);
395}
396
397static void
398input_method_context_grab_key(struct wl_keyboard_grab *grab,
399 uint32_t time, uint32_t key, uint32_t state_w)
400{
401 struct input_method_context *input_method_context = container_of(grab, struct input_method_context, grab);
402 uint32_t serial;
403
404 if (input_method_context->keyboard) {
405 serial = wl_display_next_serial(input_method_context->model->ec->wl_display);
406 wl_keyboard_send_key(input_method_context->keyboard,
407 serial, time, key, state_w);
408 }
409}
410
411static void
412input_method_context_grab_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
413 uint32_t mods_depressed, uint32_t mods_latched,
414 uint32_t mods_locked, uint32_t group)
415{
416 struct input_method_context *input_method_context = container_of(grab, struct input_method_context, grab);
417
418 if (!input_method_context->keyboard)
419 return;
420
421 wl_keyboard_send_modifiers(input_method_context->keyboard,
422 serial, mods_depressed, mods_latched,
423 mods_locked, group);
424}
425
426static const struct wl_keyboard_grab_interface input_method_context_grab = {
427 input_method_context_grab_key,
428 input_method_context_grab_modifier,
429};
430
431static void
432input_method_context_grab_keyboard(struct wl_client *client,
433 struct wl_resource *resource,
434 uint32_t id)
435{
436 struct input_method_context *context = resource->data;
437 struct wl_resource *cr;
438 struct weston_seat *seat = context->input_method->seat;
439 struct wl_keyboard *keyboard = seat->seat.keyboard;
440
441 cr = wl_client_add_object(client, &wl_keyboard_interface,
442 NULL, id, context);
443 cr->destroy = unbind_keyboard;
444
445 context->keyboard = cr;
446
447 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
448 seat->xkb_info.keymap_fd,
449 seat->xkb_info.keymap_size);
450
451 if (keyboard->grab != &keyboard->default_grab) {
452 wl_keyboard_end_grab(keyboard);
453 }
454 wl_keyboard_start_grab(keyboard, &context->grab);
455}
456
Jan Arne Petersen337df952012-11-18 19:06:46 +0100457static void
458input_method_context_key(struct wl_client *client,
459 struct wl_resource *resource,
460 uint32_t serial,
461 uint32_t time,
462 uint32_t key,
463 uint32_t state_w)
464{
465 struct input_method_context *context = resource->data;
466 struct weston_seat *seat = context->input_method->seat;
467 struct wl_keyboard *keyboard = seat->seat.keyboard;
468 struct wl_keyboard_grab *default_grab = &keyboard->default_grab;
469
470 default_grab->interface->key(default_grab, time, key, state_w);
471}
472
473static void
474input_method_context_modifiers(struct wl_client *client,
475 struct wl_resource *resource,
476 uint32_t serial,
477 uint32_t mods_depressed,
478 uint32_t mods_latched,
479 uint32_t mods_locked,
480 uint32_t group)
481{
482 struct input_method_context *context = resource->data;
483
484 struct weston_seat *seat = context->input_method->seat;
485 struct wl_keyboard *keyboard = seat->seat.keyboard;
486 struct wl_keyboard_grab *default_grab = &keyboard->default_grab;
487
488 default_grab->interface->modifiers(default_grab,
489 serial, mods_depressed,
490 mods_latched, mods_locked,
491 group);
492}
493
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200494static const struct input_method_context_interface input_method_context_implementation = {
495 input_method_context_destroy,
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200496 input_method_context_commit_string,
497 input_method_context_preedit_string,
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200498 input_method_context_delete_surrounding_text,
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100499 input_method_context_modifiers_map,
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100500 input_method_context_keysym,
Jan Arne Petersen337df952012-11-18 19:06:46 +0100501 input_method_context_grab_keyboard,
502 input_method_context_key,
503 input_method_context_modifiers
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200504};
505
506static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200507destroy_input_method_context(struct wl_resource *resource)
508{
509 struct input_method_context *context = resource->data;
510
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100511 if (context->keyboard) {
512 wl_resource_destroy(context->keyboard);
513 }
514
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200515 free(context);
516}
517
518static void
519input_method_context_create(struct text_model *model,
520 struct input_method *input_method)
521{
522 struct input_method_context *context;
523
524 if (!input_method->input_method_binding)
525 return;
526
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100527 context = calloc(1, sizeof *context);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200528 if (context == NULL)
529 return;
530
531 context->resource.destroy = destroy_input_method_context;
532 context->resource.object.id = 0;
533 context->resource.object.interface = &input_method_context_interface;
534 context->resource.object.implementation =
535 (void (**)(void)) &input_method_context_implementation;
536 context->resource.data = context;
537 wl_signal_init(&context->resource.destroy_signal);
538
539 context->model = model;
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100540 context->input_method = input_method;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200541 input_method->context = context;
542
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100543 context->grab.interface = &input_method_context_grab;
544
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200545 wl_client_add_resource(input_method->input_method_binding->client, &context->resource);
546
547 input_method_send_activate(input_method->input_method_binding, &context->resource);
548}
549
550static void
Jan Arne Petersen466b9c12012-11-18 19:06:45 +0100551input_method_context_end_keyboard_grab(struct input_method_context *context)
552{
553 struct wl_keyboard_grab *grab = &context->grab;
554
555 if (grab->keyboard && (grab->keyboard->grab == grab)) {
556 wl_keyboard_end_grab(grab->keyboard);
557 }
558}
559
560
561static void
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200562unbind_input_method(struct wl_resource *resource)
563{
564 struct input_method *input_method = resource->data;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100565 struct text_backend *text_backend = input_method->text_backend;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200566
567 input_method->input_method_binding = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200568 input_method->context = NULL;
569
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100570 text_backend->input_method.binding = NULL;
571
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200572 free(resource);
573}
574
575static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200576bind_input_method(struct wl_client *client,
577 void *data,
578 uint32_t version,
579 uint32_t id)
580{
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200581 struct input_method *input_method = data;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100582 struct text_backend *text_backend = input_method->text_backend;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200583 struct wl_resource *resource;
584
585 resource = wl_client_add_object(client, &input_method_interface,
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200586 NULL,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200587 id, input_method);
588
589 if (input_method->input_method_binding == NULL) {
590 resource->destroy = unbind_input_method;
591 input_method->input_method_binding = resource;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100592
593 text_backend->input_method.binding = resource;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200594 return;
595 }
596
597 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
598 "interface object already bound");
599 wl_resource_destroy(resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200600}
601
602static void
603input_method_notifier_destroy(struct wl_listener *listener, void *data)
604{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400605 struct input_method *input_method =
606 container_of(listener, struct input_method, destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200607
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200608 if (input_method->model)
609 deactivate_text_model(input_method->model, input_method);
610
611 wl_display_remove_global(input_method->seat->compositor->wl_display,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200612 input_method->input_method_global);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200613
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200614 free(input_method);
615}
616
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200617static void
618handle_keyboard_focus(struct wl_listener *listener, void *data)
619{
620 struct wl_keyboard *keyboard = data;
621 struct input_method *input_method =
622 container_of(listener, struct input_method, keyboard_focus_listener);
623 struct wl_surface *surface = keyboard->focus;
624
625 if (!input_method->model)
626 return;
627
628 if (!surface || input_method->model->surface != surface)
629 deactivate_text_model(input_method->model,
630 input_method);
631}
632
633static void
634input_method_init_seat(struct weston_seat *seat)
635{
636 if (seat->input_method->focus_listener_initialized)
637 return;
638
639 if (seat->has_keyboard) {
640 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
641 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
642 }
643
644 seat->input_method->focus_listener_initialized = 1;
645}
646
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100647static void
648handle_input_method_sigchld(struct weston_process *process, int status)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200649{
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100650 struct text_backend *text_backend =
651 container_of(process, struct text_backend, input_method.process);
652
653 text_backend->input_method.process.pid = 0;
654 text_backend->input_method.client = NULL;
655}
656
657static void
658launch_input_method(struct text_backend *text_backend)
659{
660 if (text_backend->input_method.binding)
661 return;
662
663 if (!text_backend->input_method.path)
664 return;
665
666 if (text_backend->input_method.process.pid != 0)
667 return;
668
669 text_backend->input_method.client = weston_client_launch(text_backend->compositor,
670 &text_backend->input_method.process,
671 text_backend->input_method.path,
672 handle_input_method_sigchld);
673
674 if (!text_backend->input_method.client)
675 weston_log("not able to start %s\n", text_backend->input_method.path);
676}
677
678static void
679handle_seat_created(struct wl_listener *listener,
680 void *data)
681{
682 struct weston_seat *seat = data;
683 struct text_backend *text_backend =
684 container_of(listener, struct text_backend,
685 seat_created_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200686 struct input_method *input_method;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100687 struct weston_compositor *ec = seat->compositor;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200688
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200689 input_method = calloc(1, sizeof *input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200690
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200691 input_method->seat = seat;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200692 input_method->model = NULL;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200693 input_method->focus_listener_initialized = 0;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200694 input_method->context = NULL;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100695 input_method->text_backend = text_backend;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200696
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200697 input_method->input_method_global =
698 wl_display_add_global(ec->wl_display,
699 &input_method_interface,
700 input_method, bind_input_method);
701
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200702 input_method->destroy_listener.notify = input_method_notifier_destroy;
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +0200703 wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200704
705 seat->input_method = input_method;
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100706
707 launch_input_method(text_backend);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200708}
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200709
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +0100710static void
711text_backend_configuration(struct text_backend *text_backend)
712{
713 char *config_file;
714 char *path = NULL;
715
716 struct config_key input_method_keys[] = {
717 { "path", CONFIG_KEY_STRING, &path }
718 };
719
720 struct config_section cs[] = {
721 { "input-method", input_method_keys, ARRAY_LENGTH(input_method_keys), NULL }
722 };
723
724 config_file = config_file_path("weston.ini");
725 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), text_backend);
726 free(config_file);
727
728 if (path)
729 text_backend->input_method.path = path;
730 else
731 text_backend->input_method.path = strdup(LIBEXECDIR "/weston-keyboard");
732}
733
734static void
735text_backend_notifier_destroy(struct wl_listener *listener, void *data)
736{
737 struct text_backend *text_backend =
738 container_of(listener, struct text_backend, destroy_listener);
739
740 if (text_backend->input_method.client)
741 wl_client_destroy(text_backend->input_method.client);
742
743 free(text_backend->input_method.path);
744
745 free(text_backend);
746}
747
748
749WL_EXPORT int
750text_backend_init(struct weston_compositor *ec)
751{
752 struct text_backend *text_backend;
753
754 text_backend = calloc(1, sizeof(*text_backend));
755
756 text_backend->compositor = ec;
757
758 text_backend->seat_created_listener.notify = handle_seat_created;
759 wl_signal_add(&ec->seat_created_signal,
760 &text_backend->seat_created_listener);
761
762 text_backend->destroy_listener.notify = text_backend_notifier_destroy;
763 wl_signal_add(&ec->destroy_signal, &text_backend->destroy_listener);
764
765 text_backend_configuration(text_backend);
766
767 text_model_factory_create(ec);
768
769 return 0;
770}