2 * Copyright © 2012 Openismus GmbH
3 * Copyright © 2012 Intel Corporation
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.
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.
26 #include "compositor.h"
27 #include "text-server-protocol.h"
28 #include "input-method-server-protocol.h"
31 struct input_method_context;
34 struct wl_resource resource;
36 struct weston_compositor *ec;
38 struct wl_list input_methods;
40 struct wl_surface *surface;
43 struct text_model_factory {
44 struct wl_global *text_model_factory_global;
45 struct wl_listener destroy_listener;
47 struct weston_compositor *ec;
51 struct wl_resource *input_method_binding;
52 struct wl_global *input_method_global;
53 struct wl_listener destroy_listener;
55 struct weston_seat *seat;
56 struct text_model *model;
60 struct wl_listener keyboard_focus_listener;
62 int focus_listener_initialized;
64 struct input_method_context *context;
67 struct input_method_context {
68 struct wl_resource resource;
70 struct text_model *model;
75 static void input_method_context_create(struct text_model *model,
76 struct input_method *input_method);
78 static void input_method_init_seat(struct weston_seat *seat);
81 deactivate_text_model(struct text_model *text_model,
82 struct input_method *input_method)
84 struct weston_compositor *ec = text_model->ec;
86 if (input_method->model == text_model) {
87 if (input_method->input_method_binding)
88 input_method_send_deactivate(input_method->input_method_binding, &input_method->context->resource);
89 wl_list_remove(&input_method->link);
90 input_method->model = NULL;
91 input_method->context = NULL;
92 wl_signal_emit(&ec->hide_input_panel_signal, ec);
93 text_model_send_leave(&text_model->resource);
98 destroy_text_model(struct wl_resource *resource)
100 struct text_model *text_model =
101 container_of(resource, struct text_model, resource);
102 struct input_method *input_method, *next;
104 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
105 deactivate_text_model(text_model, input_method);
111 text_model_set_surrounding_text(struct wl_client *client,
112 struct wl_resource *resource,
117 struct text_model *text_model = resource->data;
118 struct input_method *input_method, *next;
120 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
121 if (!input_method->context)
123 input_method_context_send_surrounding_text(&input_method->context->resource,
131 text_model_activate(struct wl_client *client,
132 struct wl_resource *resource,
133 struct wl_resource *seat,
134 struct wl_resource *surface)
136 struct text_model *text_model = resource->data;
137 struct weston_seat *weston_seat = seat->data;
138 struct input_method *input_method = weston_seat->input_method;
139 struct text_model *old = weston_seat->input_method->model;
140 struct weston_compositor *ec = text_model->ec;
142 if (old == text_model)
146 deactivate_text_model(old,
147 weston_seat->input_method);
150 input_method->model = text_model;
151 wl_list_insert(&text_model->input_methods, &input_method->link);
152 input_method_init_seat(weston_seat);
154 text_model->surface = surface->data;
156 input_method_context_create(text_model, input_method);
158 wl_signal_emit(&ec->show_input_panel_signal, ec);
160 text_model_send_enter(&text_model->resource, &text_model->surface->resource);
164 text_model_deactivate(struct wl_client *client,
165 struct wl_resource *resource,
166 struct wl_resource *seat)
168 struct text_model *text_model = resource->data;
169 struct weston_seat *weston_seat = seat->data;
171 deactivate_text_model(text_model,
172 weston_seat->input_method);
176 text_model_reset(struct wl_client *client,
177 struct wl_resource *resource)
179 struct text_model *text_model = resource->data;
180 struct input_method *input_method, *next;
182 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
183 if (!input_method->context)
185 input_method_context_send_reset(&input_method->context->resource);
190 text_model_set_micro_focus(struct wl_client *client,
191 struct wl_resource *resource,
200 text_model_set_preedit(struct wl_client *client,
201 struct wl_resource *resource)
206 text_model_set_content_type(struct wl_client *client,
207 struct wl_resource *resource)
211 static const struct text_model_interface text_model_implementation = {
212 text_model_set_surrounding_text,
214 text_model_deactivate,
216 text_model_set_micro_focus,
217 text_model_set_preedit,
218 text_model_set_content_type
221 static void text_model_factory_create_text_model(struct wl_client *client,
222 struct wl_resource *resource,
225 struct text_model_factory *text_model_factory = resource->data;
226 struct text_model *text_model;
228 text_model = calloc(1, sizeof *text_model);
230 text_model->resource.object.id = id;
231 text_model->resource.object.interface = &text_model_interface;
232 text_model->resource.object.implementation =
233 (void (**)(void)) &text_model_implementation;
235 text_model->resource.data = text_model;
236 text_model->resource.destroy = destroy_text_model;
238 text_model->ec = text_model_factory->ec;
240 wl_list_init(&text_model->input_methods);
242 wl_client_add_resource(client, &text_model->resource);
245 static const struct text_model_factory_interface text_model_factory_implementation = {
246 text_model_factory_create_text_model
250 bind_text_model_factory(struct wl_client *client,
255 struct text_model_factory *text_model_factory = data;
257 /* No checking for duplicate binding necessary.
258 * No events have to be sent, so we don't need the return value. */
259 wl_client_add_object(client, &text_model_factory_interface,
260 &text_model_factory_implementation,
261 id, text_model_factory);
265 text_model_factory_notifier_destroy(struct wl_listener *listener, void *data)
267 struct text_model_factory *text_model_factory =
268 container_of(listener, struct text_model_factory, destroy_listener);
270 wl_display_remove_global(text_model_factory->ec->wl_display,
271 text_model_factory->text_model_factory_global);
273 free(text_model_factory);
277 text_model_factory_create(struct weston_compositor *ec)
279 struct text_model_factory *text_model_factory;
281 text_model_factory = calloc(1, sizeof *text_model_factory);
283 text_model_factory->ec = ec;
285 text_model_factory->text_model_factory_global =
286 wl_display_add_global(ec->wl_display,
287 &text_model_factory_interface,
288 text_model_factory, bind_text_model_factory);
290 text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy;
291 wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener);
295 input_method_context_destroy(struct wl_client *client,
296 struct wl_resource *resource)
298 wl_resource_destroy(resource);
302 input_method_context_commit_string(struct wl_client *client,
303 struct wl_resource *resource,
307 struct input_method_context *context = resource->data;
309 text_model_send_commit_string(&context->model->resource, text, index);
313 input_method_context_preedit_string(struct wl_client *client,
314 struct wl_resource *resource,
318 struct input_method_context *context = resource->data;
320 text_model_send_preedit_string(&context->model->resource, text, index);
324 input_method_context_delete_surrounding_text(struct wl_client *client,
325 struct wl_resource *resource,
329 struct input_method_context *context = resource->data;
331 text_model_send_delete_surrounding_text(&context->model->resource, index, length);
335 input_method_context_key(struct wl_client *client,
336 struct wl_resource *resource,
340 struct input_method_context *context = resource->data;
342 text_model_send_key(&context->model->resource, key, state);
345 static const struct input_method_context_interface input_method_context_implementation = {
346 input_method_context_destroy,
347 input_method_context_commit_string,
348 input_method_context_preedit_string,
349 input_method_context_delete_surrounding_text,
350 input_method_context_key
354 destroy_input_method_context(struct wl_resource *resource)
356 struct input_method_context *context = resource->data;
362 input_method_context_create(struct text_model *model,
363 struct input_method *input_method)
365 struct input_method_context *context;
367 if (!input_method->input_method_binding)
370 context = malloc(sizeof *context);
374 context->resource.destroy = destroy_input_method_context;
375 context->resource.object.id = 0;
376 context->resource.object.interface = &input_method_context_interface;
377 context->resource.object.implementation =
378 (void (**)(void)) &input_method_context_implementation;
379 context->resource.data = context;
380 wl_signal_init(&context->resource.destroy_signal);
382 context->model = model;
383 input_method->context = context;
385 wl_client_add_resource(input_method->input_method_binding->client, &context->resource);
387 input_method_send_activate(input_method->input_method_binding, &context->resource);
391 unbind_input_method(struct wl_resource *resource)
393 struct input_method *input_method = resource->data;
395 input_method->input_method_binding = NULL;
396 input_method->context = NULL;
402 bind_input_method(struct wl_client *client,
407 struct input_method *input_method = data;
408 struct wl_resource *resource;
410 resource = wl_client_add_object(client, &input_method_interface,
414 if (input_method->input_method_binding == NULL) {
415 resource->destroy = unbind_input_method;
416 input_method->input_method_binding = resource;
420 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
421 "interface object already bound");
422 wl_resource_destroy(resource);
426 input_method_notifier_destroy(struct wl_listener *listener, void *data)
428 struct input_method *input_method =
429 container_of(listener, struct input_method, destroy_listener);
431 if (input_method->model)
432 deactivate_text_model(input_method->model, input_method);
434 wl_display_remove_global(input_method->seat->compositor->wl_display,
435 input_method->input_method_global);
441 handle_keyboard_focus(struct wl_listener *listener, void *data)
443 struct wl_keyboard *keyboard = data;
444 struct input_method *input_method =
445 container_of(listener, struct input_method, keyboard_focus_listener);
446 struct wl_surface *surface = keyboard->focus;
448 if (!input_method->model)
451 if (!surface || input_method->model->surface != surface)
452 deactivate_text_model(input_method->model,
457 input_method_init_seat(struct weston_seat *seat)
459 if (seat->input_method->focus_listener_initialized)
462 if (seat->has_keyboard) {
463 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
464 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
467 seat->input_method->focus_listener_initialized = 1;
471 input_method_create(struct weston_compositor *ec,
472 struct weston_seat *seat)
474 struct input_method *input_method;
476 input_method = calloc(1, sizeof *input_method);
478 input_method->seat = seat;
479 input_method->model = NULL;
480 input_method->focus_listener_initialized = 0;
481 input_method->context = NULL;
483 input_method->input_method_global =
484 wl_display_add_global(ec->wl_display,
485 &input_method_interface,
486 input_method, bind_input_method);
488 input_method->destroy_listener.notify = input_method_notifier_destroy;
489 wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
491 seat->input_method = input_method;