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"
30 struct input_method_context;
33 struct wl_resource resource;
35 struct weston_compositor *ec;
37 struct wl_list input_methods;
39 struct wl_surface *surface;
42 struct text_model_factory {
43 struct wl_global *text_model_factory_global;
44 struct wl_listener destroy_listener;
46 struct weston_compositor *ec;
50 struct wl_resource *input_method_binding;
51 struct wl_global *input_method_global;
52 struct wl_listener destroy_listener;
54 struct weston_seat *seat;
55 struct text_model *model;
59 struct wl_listener keyboard_focus_listener;
61 int focus_listener_initialized;
63 struct input_method_context *context;
66 struct input_method_context {
67 struct wl_resource resource;
69 struct text_model *model;
74 static void input_method_context_create(struct text_model *model,
75 struct input_method *input_method);
77 static void input_method_init_seat(struct weston_seat *seat);
80 deactivate_text_model(struct text_model *text_model,
81 struct input_method *input_method)
83 struct weston_compositor *ec = text_model->ec;
85 if (input_method->model == text_model) {
86 if (input_method->input_method_binding)
87 input_method_send_deactivate(input_method->input_method_binding, &input_method->context->resource);
88 wl_list_remove(&input_method->link);
89 input_method->model = NULL;
90 input_method->context = NULL;
91 wl_signal_emit(&ec->hide_input_panel_signal, ec);
92 text_model_send_deactivated(&text_model->resource);
97 destroy_text_model(struct wl_resource *resource)
99 struct text_model *text_model =
100 container_of(resource, struct text_model, resource);
101 struct input_method *input_method, *next;
103 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
104 deactivate_text_model(text_model, input_method);
110 text_model_set_surrounding_text(struct wl_client *client,
111 struct wl_resource *resource,
116 struct text_model *text_model = resource->data;
117 struct input_method *input_method, *next;
119 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) {
120 if (!input_method->context)
122 input_method_context_send_surrounding_text(&input_method->context->resource,
130 text_model_activate(struct wl_client *client,
131 struct wl_resource *resource,
132 struct wl_resource *seat,
133 struct wl_resource *surface)
135 struct text_model *text_model = resource->data;
136 struct weston_seat *weston_seat = seat->data;
137 struct input_method *input_method = weston_seat->input_method;
138 struct text_model *old = weston_seat->input_method->model;
139 struct weston_compositor *ec = text_model->ec;
141 if (old == text_model)
145 deactivate_text_model(old,
146 weston_seat->input_method);
149 input_method->model = text_model;
150 wl_list_insert(&text_model->input_methods, &input_method->link);
151 input_method_init_seat(weston_seat);
153 text_model->surface = surface->data;
155 input_method_context_create(text_model, input_method);
157 wl_signal_emit(&ec->show_input_panel_signal, ec);
159 text_model_send_activated(&text_model->resource);
163 text_model_deactivate(struct wl_client *client,
164 struct wl_resource *resource,
165 struct wl_resource *seat)
167 struct text_model *text_model = resource->data;
168 struct weston_seat *weston_seat = seat->data;
170 deactivate_text_model(text_model,
171 weston_seat->input_method);
175 text_model_set_micro_focus(struct wl_client *client,
176 struct wl_resource *resource,
185 text_model_set_preedit(struct wl_client *client,
186 struct wl_resource *resource)
191 text_model_set_content_type(struct wl_client *client,
192 struct wl_resource *resource)
196 static const struct text_model_interface text_model_implementation = {
197 text_model_set_surrounding_text,
199 text_model_deactivate,
200 text_model_set_micro_focus,
201 text_model_set_preedit,
202 text_model_set_content_type
205 static void text_model_factory_create_text_model(struct wl_client *client,
206 struct wl_resource *resource,
209 struct text_model_factory *text_model_factory = resource->data;
210 struct text_model *text_model;
212 text_model = calloc(1, sizeof *text_model);
214 text_model->resource.object.id = id;
215 text_model->resource.object.interface = &text_model_interface;
216 text_model->resource.object.implementation =
217 (void (**)(void)) &text_model_implementation;
219 text_model->resource.data = text_model;
220 text_model->resource.destroy = destroy_text_model;
222 text_model->ec = text_model_factory->ec;
224 wl_list_init(&text_model->input_methods);
226 wl_client_add_resource(client, &text_model->resource);
229 static const struct text_model_factory_interface text_model_factory_implementation = {
230 text_model_factory_create_text_model
234 bind_text_model_factory(struct wl_client *client,
239 struct text_model_factory *text_model_factory = data;
241 /* No checking for duplicate binding necessary.
242 * No events have to be sent, so we don't need the return value. */
243 wl_client_add_object(client, &text_model_factory_interface,
244 &text_model_factory_implementation,
245 id, text_model_factory);
249 text_model_factory_notifier_destroy(struct wl_listener *listener, void *data)
251 struct text_model_factory *text_model_factory =
252 container_of(listener, struct text_model_factory, destroy_listener);
254 wl_display_remove_global(text_model_factory->ec->wl_display,
255 text_model_factory->text_model_factory_global);
257 free(text_model_factory);
261 text_model_factory_create(struct weston_compositor *ec)
263 struct text_model_factory *text_model_factory;
265 text_model_factory = calloc(1, sizeof *text_model_factory);
267 text_model_factory->ec = ec;
269 text_model_factory->text_model_factory_global =
270 wl_display_add_global(ec->wl_display,
271 &text_model_factory_interface,
272 text_model_factory, bind_text_model_factory);
274 text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy;
275 wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener);
279 input_method_context_destroy(struct wl_client *client,
280 struct wl_resource *resource)
282 wl_resource_destroy(resource);
286 input_method_context_commit_string(struct wl_client *client,
287 struct wl_resource *resource,
291 struct input_method_context *context = resource->data;
293 text_model_send_commit_string(&context->model->resource, text, index);
296 static const struct input_method_context_interface input_method_context_implementation = {
297 input_method_context_destroy,
298 input_method_context_commit_string
302 destroy_input_method_context(struct wl_resource *resource)
304 struct input_method_context *context = resource->data;
310 input_method_context_create(struct text_model *model,
311 struct input_method *input_method)
313 struct input_method_context *context;
315 if (!input_method->input_method_binding)
318 context = malloc(sizeof *context);
322 context->resource.destroy = destroy_input_method_context;
323 context->resource.object.id = 0;
324 context->resource.object.interface = &input_method_context_interface;
325 context->resource.object.implementation =
326 (void (**)(void)) &input_method_context_implementation;
327 context->resource.data = context;
328 wl_signal_init(&context->resource.destroy_signal);
330 context->model = model;
331 input_method->context = context;
333 wl_client_add_resource(input_method->input_method_binding->client, &context->resource);
335 input_method_send_activate(input_method->input_method_binding, &context->resource);
339 unbind_input_method(struct wl_resource *resource)
341 struct input_method *input_method = resource->data;
343 input_method->input_method_binding = NULL;
344 input_method->context = NULL;
350 bind_input_method(struct wl_client *client,
355 struct input_method *input_method = data;
356 struct wl_resource *resource;
358 resource = wl_client_add_object(client, &input_method_interface,
362 if (input_method->input_method_binding == NULL) {
363 resource->destroy = unbind_input_method;
364 input_method->input_method_binding = resource;
368 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
369 "interface object already bound");
370 wl_resource_destroy(resource);
374 input_method_notifier_destroy(struct wl_listener *listener, void *data)
376 struct input_method *input_method =
377 container_of(listener, struct input_method, destroy_listener);
379 if (input_method->model)
380 deactivate_text_model(input_method->model, input_method);
382 wl_display_remove_global(input_method->seat->compositor->wl_display,
383 input_method->input_method_global);
389 handle_keyboard_focus(struct wl_listener *listener, void *data)
391 struct wl_keyboard *keyboard = data;
392 struct input_method *input_method =
393 container_of(listener, struct input_method, keyboard_focus_listener);
394 struct wl_surface *surface = keyboard->focus;
396 if (!input_method->model)
399 if (!surface || input_method->model->surface != surface)
400 deactivate_text_model(input_method->model,
405 input_method_init_seat(struct weston_seat *seat)
407 if (seat->input_method->focus_listener_initialized)
410 if (seat->has_keyboard) {
411 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
412 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
415 seat->input_method->focus_listener_initialized = 1;
419 input_method_create(struct weston_compositor *ec,
420 struct weston_seat *seat)
422 struct input_method *input_method;
424 input_method = calloc(1, sizeof *input_method);
426 input_method->seat = seat;
427 input_method->model = NULL;
428 input_method->focus_listener_initialized = 0;
429 input_method->context = NULL;
431 input_method->input_method_global =
432 wl_display_add_global(ec->wl_display,
433 &input_method_interface,
434 input_method, bind_input_method);
436 input_method->destroy_listener.notify = input_method_notifier_destroy;
437 wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener);
439 seat->input_method = input_method;