blob: ddeec202b8ce4c3168574710444743881f13be5f [file] [log] [blame]
Jan Arne Petersen1f17be42012-06-21 21:52:18 +02001/*
2 * Copyright © 2012 Openismus GmbH
3 *
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.
13 *
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.
21 */
22
23#include <stdlib.h>
24
25#include "compositor.h"
26#include "text-server-protocol.h"
27
28struct input_method;
29
30struct text_model {
31 struct wl_resource resource;
32
Jan Arne Petersene829adc2012-08-10 16:47:22 +020033 struct weston_compositor *ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020034
Jan Arne Petersene829adc2012-08-10 16:47:22 +020035 struct wl_list input_methods;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020036
37 struct wl_surface *surface;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020038};
39
40struct input_method {
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +020041 struct wl_resource *input_method_binding;
42 struct wl_global *input_method_global;
Jan Arne Petersen51963742012-08-10 16:47:20 +020043 struct wl_global *text_model_factory_global;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020044 struct wl_listener destroy_listener;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020045
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +020046 struct weston_compositor *ec;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020047 struct text_model *model;
48
49 struct wl_list link;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020050
51 struct wl_listener keyboard_focus_listener;
52
53 int focus_listener_initialized;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020054};
55
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +020056static void input_method_init_seat(struct weston_seat *seat);
57
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020058static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +020059deactivate_text_model(struct text_model *text_model,
60 struct input_method *input_method)
Philipp Brüschweiler17467812012-07-11 22:25:30 +020061{
Jan Arne Petersene829adc2012-08-10 16:47:22 +020062 struct weston_compositor *ec = text_model->ec;
Philipp Brüschweiler17467812012-07-11 22:25:30 +020063
Jan Arne Petersene829adc2012-08-10 16:47:22 +020064 if (input_method->model == text_model) {
65 wl_list_remove(&input_method->link);
66 input_method->model = NULL;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040067 wl_signal_emit(&ec->hide_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +020068 text_model_send_deactivated(&text_model->resource);
Philipp Brüschweiler17467812012-07-11 22:25:30 +020069 }
70}
71
72static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020073destroy_text_model(struct wl_resource *resource)
74{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040075 struct text_model *text_model =
76 container_of(resource, struct text_model, resource);
Jan Arne Petersene829adc2012-08-10 16:47:22 +020077 struct input_method *input_method, *next;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020078
Jan Arne Petersene829adc2012-08-10 16:47:22 +020079 wl_list_for_each_safe(input_method, next, &text_model->input_methods, link)
80 deactivate_text_model(text_model, input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020081
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020082 free(text_model);
83}
84
85static void
86text_model_set_surrounding_text(struct wl_client *client,
87 struct wl_resource *resource,
88 const char *text)
89{
90}
91
92static void
93text_model_set_cursor_index(struct wl_client *client,
94 struct wl_resource *resource,
95 uint32_t index)
96{
97}
98
99static void
100text_model_activate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200101 struct wl_resource *resource,
102 struct wl_resource *seat,
103 struct wl_resource *surface)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200104{
105 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200106 struct weston_seat *weston_seat = seat->data;
107 struct text_model *old = weston_seat->input_method->model;
108 struct weston_compositor *ec = text_model->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200109
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200110 if (old == text_model)
111 return;
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200112
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200113 if (old) {
114 deactivate_text_model(old,
115 weston_seat->input_method);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200116 }
117
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200118 weston_seat->input_method->model = text_model;
119 wl_list_insert(&text_model->input_methods, &weston_seat->input_method->link);
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200120 input_method_init_seat(weston_seat);
121
122 text_model->surface = surface->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200123
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400124 wl_signal_emit(&ec->show_input_panel_signal, ec);
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200125
126 text_model_send_activated(&text_model->resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200127}
128
129static void
130text_model_deactivate(struct wl_client *client,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200131 struct wl_resource *resource,
132 struct wl_resource *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200133{
134 struct text_model *text_model = resource->data;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200135 struct weston_seat *weston_seat = seat->data;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200136
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200137 deactivate_text_model(text_model,
138 weston_seat->input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200139}
140
141static void
142text_model_set_selected_text(struct wl_client *client,
143 struct wl_resource *resource,
144 const char *text,
145 int32_t index)
146{
147}
148
149static void
150text_model_set_micro_focus(struct wl_client *client,
151 struct wl_resource *resource,
152 int32_t x,
153 int32_t y,
154 int32_t width,
155 int32_t height)
156{
157}
158
159static void
160text_model_set_preedit(struct wl_client *client,
161 struct wl_resource *resource)
162{
163}
164
165static void
166text_model_set_content_type(struct wl_client *client,
167 struct wl_resource *resource)
168{
169}
170
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200171static const struct text_model_interface text_model_implementation = {
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200172 text_model_set_surrounding_text,
173 text_model_set_cursor_index,
174 text_model_activate,
175 text_model_deactivate,
176 text_model_set_selected_text,
177 text_model_set_micro_focus,
178 text_model_set_preedit,
179 text_model_set_content_type
180};
181
Jan Arne Petersen51963742012-08-10 16:47:20 +0200182static void text_model_factory_create_text_model(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200183 struct wl_resource *resource,
184 uint32_t id,
185 struct wl_resource *surface)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200186{
187 struct input_method *input_method = resource->data;
188 struct text_model *text_model;
189
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200190 text_model = calloc(1, sizeof *text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200191
192 text_model->resource.destroy = destroy_text_model;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200193
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200194 text_model->resource.object.id = id;
195 text_model->resource.object.interface = &text_model_interface;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200196 text_model->resource.object.implementation =
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200197 (void (**)(void)) &text_model_implementation;
198 text_model->resource.data = text_model;
199
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200200 text_model->ec = input_method->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200201
202 wl_client_add_resource(client, &text_model->resource);
203
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200204 wl_list_init(&text_model->input_methods);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200205};
206
Jan Arne Petersen51963742012-08-10 16:47:20 +0200207static const struct text_model_factory_interface text_model_factory_implementation = {
208 text_model_factory_create_text_model
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200209};
210
211static void
Jan Arne Petersen51963742012-08-10 16:47:20 +0200212bind_text_model_factory(struct wl_client *client,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200213 void *data,
214 uint32_t version,
215 uint32_t id)
216{
217 struct input_method *input_method = data;
218
219 /* No checking for duplicate binding necessary.
220 * No events have to be sent, so we don't need the return value. */
Jan Arne Petersen51963742012-08-10 16:47:20 +0200221 wl_client_add_object(client, &text_model_factory_interface,
222 &text_model_factory_implementation,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200223 id, input_method);
224}
225
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200226static void
227input_method_commit_string(struct wl_client *client,
228 struct wl_resource *resource,
229 const char *text,
230 uint32_t index)
231{
232 struct input_method *input_method = resource->data;
233
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200234 if (input_method->model) {
235 text_model_send_commit_string(&input_method->model->resource, text, index);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200236 }
237}
238
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200239static const struct input_method_interface input_method_implementation = {
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200240 input_method_commit_string
241};
242
243static void
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200244unbind_input_method(struct wl_resource *resource)
245{
246 struct input_method *input_method = resource->data;
247
248 input_method->input_method_binding = NULL;
249 free(resource);
250}
251
252static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200253bind_input_method(struct wl_client *client,
254 void *data,
255 uint32_t version,
256 uint32_t id)
257{
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200258 struct input_method *input_method = data;
259 struct wl_resource *resource;
260
261 resource = wl_client_add_object(client, &input_method_interface,
262 &input_method_implementation,
263 id, input_method);
264
265 if (input_method->input_method_binding == NULL) {
266 resource->destroy = unbind_input_method;
267 input_method->input_method_binding = resource;
268 return;
269 }
270
271 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
272 "interface object already bound");
273 wl_resource_destroy(resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200274}
275
276static void
277input_method_notifier_destroy(struct wl_listener *listener, void *data)
278{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400279 struct input_method *input_method =
280 container_of(listener, struct input_method, destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200281
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400282 wl_display_remove_global(input_method->ec->wl_display,
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200283 input_method->input_method_global);
284 wl_display_remove_global(input_method->ec->wl_display,
Jan Arne Petersen51963742012-08-10 16:47:20 +0200285 input_method->text_model_factory_global);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200286 free(input_method);
287}
288
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200289static void
290handle_keyboard_focus(struct wl_listener *listener, void *data)
291{
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;
296
297 if (!input_method->model)
298 return;
299
300 if (!surface || input_method->model->surface != surface)
301 deactivate_text_model(input_method->model,
302 input_method);
303}
304
305static void
306input_method_init_seat(struct weston_seat *seat)
307{
308 if (seat->input_method->focus_listener_initialized)
309 return;
310
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);
314 }
315
316 seat->input_method->focus_listener_initialized = 1;
317}
318
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200319void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200320input_method_create(struct weston_compositor *ec,
321 struct weston_seat *seat)
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200322{
323 struct input_method *input_method;
324
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200325 input_method = calloc(1, sizeof *input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200326
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200327 input_method->ec = ec;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200328 input_method->model = NULL;
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200329 input_method->focus_listener_initialized = 0;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200330
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200331 input_method->input_method_global =
332 wl_display_add_global(ec->wl_display,
333 &input_method_interface,
334 input_method, bind_input_method);
335
Jan Arne Petersen51963742012-08-10 16:47:20 +0200336 input_method->text_model_factory_global =
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200337 wl_display_add_global(ec->wl_display,
Jan Arne Petersen51963742012-08-10 16:47:20 +0200338 &text_model_factory_interface,
339 input_method, bind_text_model_factory);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200340
341 input_method->destroy_listener.notify = input_method_notifier_destroy;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200342 wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200343
344 seat->input_method = input_method;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200345}
Jan Arne Petersencd8cdcc2012-08-10 16:47:23 +0200346