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"
32 struct wl_resource resource;
34 struct weston_compositor *ec;
36 struct wl_list input_methods;
38 struct wl_surface *surface;
42 struct wl_resource *input_method_binding;
43 struct wl_global *input_method_global;
44 struct wl_global *text_model_factory_global;
45 struct wl_listener destroy_listener;
47 struct weston_compositor *ec;
48 struct text_model *model;
52 struct wl_listener keyboard_focus_listener;
54 int focus_listener_initialized;
57 static void input_method_init_seat(struct weston_seat *seat);
60 deactivate_text_model(struct text_model *text_model,
61 struct input_method *input_method)
63 struct weston_compositor *ec = text_model->ec;
65 if (input_method->model == text_model) {
66 wl_list_remove(&input_method->link);
67 input_method->model = NULL;
68 wl_signal_emit(&ec->hide_input_panel_signal, ec);
69 text_model_send_deactivated(&text_model->resource);
74 destroy_text_model(struct wl_resource *resource)
76 struct text_model *text_model =
77 container_of(resource, struct text_model, resource);
78 struct input_method *input_method, *next;
80 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
81 deactivate_text_model(text_model, input_method);
87 text_model_set_surrounding_text(struct wl_client *client,
88 struct wl_resource *resource,
94 text_model_set_cursor_index(struct wl_client *client,
95 struct wl_resource *resource,
101 text_model_activate(struct wl_client *client,
102 struct wl_resource *resource,
103 struct wl_resource *seat,
104 struct wl_resource *surface)
106 struct text_model *text_model = resource->data;
107 struct weston_seat *weston_seat = seat->data;
108 struct text_model *old = weston_seat->input_method->model;
109 struct weston_compositor *ec = text_model->ec;
111 if (old == text_model)
115 deactivate_text_model(old,
116 weston_seat->input_method);
119 weston_seat->input_method->model = text_model;
120 wl_list_insert(&text_model->input_methods, &weston_seat->input_method->link);
121 input_method_init_seat(weston_seat);
123 text_model->surface = surface->data;
125 wl_signal_emit(&ec->show_input_panel_signal, ec);
127 text_model_send_activated(&text_model->resource);
131 text_model_deactivate(struct wl_client *client,
132 struct wl_resource *resource,
133 struct wl_resource *seat)
135 struct text_model *text_model = resource->data;
136 struct weston_seat *weston_seat = seat->data;
138 deactivate_text_model(text_model,
139 weston_seat->input_method);
143 text_model_set_selected_text(struct wl_client *client,
144 struct wl_resource *resource,
151 text_model_set_micro_focus(struct wl_client *client,
152 struct wl_resource *resource,
161 text_model_set_preedit(struct wl_client *client,
162 struct wl_resource *resource)
167 text_model_set_content_type(struct wl_client *client,
168 struct wl_resource *resource)
172 static const struct text_model_interface text_model_implementation = {
173 text_model_set_surrounding_text,
174 text_model_set_cursor_index,
176 text_model_deactivate,
177 text_model_set_selected_text,
178 text_model_set_micro_focus,
179 text_model_set_preedit,
180 text_model_set_content_type
183 static void text_model_factory_create_text_model(struct wl_client *client,
184 struct wl_resource *resource,
187 struct input_method *input_method = resource->data;
188 struct text_model *text_model;
190 text_model = calloc(1, sizeof *text_model);
192 text_model->resource.destroy = destroy_text_model;
194 text_model->resource.object.id = id;
195 text_model->resource.object.interface = &text_model_interface;
196 text_model->resource.object.implementation =
197 (void (**)(void)) &text_model_implementation;
198 text_model->resource.data = text_model;
200 text_model->ec = input_method->ec;
202 wl_client_add_resource(client, &text_model->resource);
204 wl_list_init(&text_model->input_methods);
207 static const struct text_model_factory_interface text_model_factory_implementation = {
208 text_model_factory_create_text_model
212 bind_text_model_factory(struct wl_client *client,
217 struct input_method *input_method = data;
219 /* No checking for duplicate binding necessary.
220 * No events have to be sent, so we don't need the return value. */
221 wl_client_add_object(client, &text_model_factory_interface,
222 &text_model_factory_implementation,
227 input_method_commit_string(struct wl_client *client,
228 struct wl_resource *resource,
232 struct input_method *input_method = resource->data;
234 if (input_method->model) {
235 text_model_send_commit_string(&input_method->model->resource, text, index);
239 static const struct input_method_interface input_method_implementation = {
240 input_method_commit_string
244 unbind_input_method(struct wl_resource *resource)
246 struct input_method *input_method = resource->data;
248 input_method->input_method_binding = NULL;
253 bind_input_method(struct wl_client *client,
258 struct input_method *input_method = data;
259 struct wl_resource *resource;
261 resource = wl_client_add_object(client, &input_method_interface,
262 &input_method_implementation,
265 if (input_method->input_method_binding == NULL) {
266 resource->destroy = unbind_input_method;
267 input_method->input_method_binding = resource;
271 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
272 "interface object already bound");
273 wl_resource_destroy(resource);
277 input_method_notifier_destroy(struct wl_listener *listener, void *data)
279 struct input_method *input_method =
280 container_of(listener, struct input_method, destroy_listener);
282 wl_display_remove_global(input_method->ec->wl_display,
283 input_method->input_method_global);
284 wl_display_remove_global(input_method->ec->wl_display,
285 input_method->text_model_factory_global);
290 handle_keyboard_focus(struct wl_listener *listener, void *data)
292 struct wl_keyboard *keyboard = data;
293 struct input_method *input_method =
294 container_of(listener, struct input_method, keyboard_focus_listener);
295 struct wl_surface *surface = keyboard->focus;
297 if (!input_method->model)
300 if (!surface || input_method->model->surface != surface)
301 deactivate_text_model(input_method->model,
306 input_method_init_seat(struct weston_seat *seat)
308 if (seat->input_method->focus_listener_initialized)
311 if (seat->has_keyboard) {
312 seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus;
313 wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener);
316 seat->input_method->focus_listener_initialized = 1;
320 input_method_create(struct weston_compositor *ec,
321 struct weston_seat *seat)
323 struct input_method *input_method;
325 input_method = calloc(1, sizeof *input_method);
327 input_method->ec = ec;
328 input_method->model = NULL;
329 input_method->focus_listener_initialized = 0;
331 input_method->input_method_global =
332 wl_display_add_global(ec->wl_display,
333 &input_method_interface,
334 input_method, bind_input_method);
336 input_method->text_model_factory_global =
337 wl_display_add_global(ec->wl_display,
338 &text_model_factory_interface,
339 input_method, bind_text_model_factory);
341 input_method->destroy_listener.notify = input_method_notifier_destroy;
342 wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
344 seat->input_method = input_method;