2 * Copyright © 2012 Openismus GmbH
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include "compositor.h"
26 #include "text-server-protocol.h"
31 struct wl_resource resource;
33 struct weston_compositor *ec;
35 struct wl_list input_methods;
39 struct wl_resource *input_method_binding;
40 struct wl_global *input_method_global;
41 struct wl_global *text_model_factory_global;
42 struct wl_listener destroy_listener;
44 struct weston_compositor *ec;
45 struct text_model *model;
51 deactivate_text_model(struct text_model *text_model,
52 struct input_method *input_method)
54 struct weston_compositor *ec = text_model->ec;
56 if (input_method->model == text_model) {
57 wl_list_remove(&input_method->link);
58 input_method->model = NULL;
59 wl_signal_emit(&ec->hide_input_panel_signal, ec);
60 text_model_send_deactivated(&text_model->resource);
65 destroy_text_model(struct wl_resource *resource)
67 struct text_model *text_model =
68 container_of(resource, struct text_model, resource);
69 struct input_method *input_method, *next;
71 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
72 deactivate_text_model(text_model, input_method);
78 text_model_set_surrounding_text(struct wl_client *client,
79 struct wl_resource *resource,
85 text_model_set_cursor_index(struct wl_client *client,
86 struct wl_resource *resource,
92 text_model_activate(struct wl_client *client,
93 struct wl_resource *resource,
94 struct wl_resource *seat,
95 struct wl_resource *surface)
97 struct text_model *text_model = resource->data;
98 struct weston_seat *weston_seat = seat->data;
99 struct text_model *old = weston_seat->input_method->model;
100 struct weston_compositor *ec = text_model->ec;
102 if (old == text_model)
106 deactivate_text_model(old,
107 weston_seat->input_method);
110 weston_seat->input_method->model = text_model;
111 wl_list_insert(&text_model->input_methods, &weston_seat->input_method->link);
113 wl_signal_emit(&ec->show_input_panel_signal, ec);
115 text_model_send_activated(&text_model->resource);
119 text_model_deactivate(struct wl_client *client,
120 struct wl_resource *resource,
121 struct wl_resource *seat)
123 struct text_model *text_model = resource->data;
124 struct weston_seat *weston_seat = seat->data;
126 deactivate_text_model(text_model,
127 weston_seat->input_method);
131 text_model_set_selected_text(struct wl_client *client,
132 struct wl_resource *resource,
139 text_model_set_micro_focus(struct wl_client *client,
140 struct wl_resource *resource,
149 text_model_set_preedit(struct wl_client *client,
150 struct wl_resource *resource)
155 text_model_set_content_type(struct wl_client *client,
156 struct wl_resource *resource)
160 static const struct text_model_interface text_model_implementation = {
161 text_model_set_surrounding_text,
162 text_model_set_cursor_index,
164 text_model_deactivate,
165 text_model_set_selected_text,
166 text_model_set_micro_focus,
167 text_model_set_preedit,
168 text_model_set_content_type
171 static void text_model_factory_create_text_model(struct wl_client *client,
172 struct wl_resource *resource,
174 struct wl_resource *surface)
176 struct input_method *input_method = resource->data;
177 struct text_model *text_model;
179 text_model = calloc(1, sizeof *text_model);
181 text_model->resource.destroy = destroy_text_model;
183 text_model->resource.object.id = id;
184 text_model->resource.object.interface = &text_model_interface;
185 text_model->resource.object.implementation =
186 (void (**)(void)) &text_model_implementation;
187 text_model->resource.data = text_model;
189 text_model->ec = input_method->ec;
191 wl_client_add_resource(client, &text_model->resource);
193 wl_list_init(&text_model->input_methods);
196 static const struct text_model_factory_interface text_model_factory_implementation = {
197 text_model_factory_create_text_model
201 bind_text_model_factory(struct wl_client *client,
206 struct input_method *input_method = data;
208 /* No checking for duplicate binding necessary.
209 * No events have to be sent, so we don't need the return value. */
210 wl_client_add_object(client, &text_model_factory_interface,
211 &text_model_factory_implementation,
216 input_method_commit_string(struct wl_client *client,
217 struct wl_resource *resource,
221 struct input_method *input_method = resource->data;
223 if (input_method->model) {
224 text_model_send_commit_string(&input_method->model->resource, text, index);
228 static const struct input_method_interface input_method_implementation = {
229 input_method_commit_string
233 unbind_input_method(struct wl_resource *resource)
235 struct input_method *input_method = resource->data;
237 input_method->input_method_binding = NULL;
242 bind_input_method(struct wl_client *client,
247 struct input_method *input_method = data;
248 struct wl_resource *resource;
250 resource = wl_client_add_object(client, &input_method_interface,
251 &input_method_implementation,
254 if (input_method->input_method_binding == NULL) {
255 resource->destroy = unbind_input_method;
256 input_method->input_method_binding = resource;
260 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
261 "interface object already bound");
262 wl_resource_destroy(resource);
266 input_method_notifier_destroy(struct wl_listener *listener, void *data)
268 struct input_method *input_method =
269 container_of(listener, struct input_method, destroy_listener);
271 wl_display_remove_global(input_method->ec->wl_display,
272 input_method->input_method_global);
273 wl_display_remove_global(input_method->ec->wl_display,
274 input_method->text_model_factory_global);
279 input_method_create(struct weston_compositor *ec,
280 struct weston_seat *seat)
282 struct input_method *input_method;
284 input_method = calloc(1, sizeof *input_method);
286 input_method->ec = ec;
287 input_method->model = NULL;
289 input_method->input_method_global =
290 wl_display_add_global(ec->wl_display,
291 &input_method_interface,
292 input_method, bind_input_method);
294 input_method->text_model_factory_global =
295 wl_display_add_global(ec->wl_display,
296 &text_model_factory_interface,
297 input_method, bind_text_model_factory);
299 input_method->destroy_listener.notify = input_method_notifier_destroy;
300 wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
302 seat->input_method = input_method;