blob: 1eb49f1478d151cc1cb19a95dc28e79e9fc2e064 [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
33 struct wl_list link;
34
35 struct input_method *input_method;
36};
37
38struct input_method {
39 struct wl_object base;
40 struct weston_compositor *ec;
41 struct wl_global *global;
42 struct wl_listener destroy_listener;
43 struct wl_list models;
44
45 struct text_model *active_model;
46};
47
48static void
Philipp Brüschweiler17467812012-07-11 22:25:30 +020049deactivate_text_model(struct text_model *text_model)
50{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040051 struct weston_compositor *ec = text_model->input_method->ec;
Philipp Brüschweiler17467812012-07-11 22:25:30 +020052
53 if (text_model->input_method->active_model == text_model) {
54 text_model->input_method->active_model = NULL;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040055 wl_signal_emit(&ec->hide_input_panel_signal, ec);
Philipp Brüschweiler17467812012-07-11 22:25:30 +020056 }
57}
58
59static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020060destroy_text_model(struct wl_resource *resource)
61{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040062 struct text_model *text_model =
63 container_of(resource, struct text_model, resource);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020064
Philipp Brüschweiler17467812012-07-11 22:25:30 +020065 deactivate_text_model(text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020066
67 wl_list_remove(&text_model->link);
68 free(text_model);
69}
70
71static void
72text_model_set_surrounding_text(struct wl_client *client,
73 struct wl_resource *resource,
74 const char *text)
75{
76}
77
78static void
79text_model_set_cursor_index(struct wl_client *client,
80 struct wl_resource *resource,
81 uint32_t index)
82{
83}
84
85static void
86text_model_activate(struct wl_client *client,
87 struct wl_resource *resource)
88{
89 struct text_model *text_model = resource->data;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040090 struct weston_compositor *ec = text_model->input_method->ec;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020091
92 text_model->input_method->active_model = text_model;
93
Kristian Høgsbergf97f3792012-07-22 11:51:42 -040094 wl_signal_emit(&ec->show_input_panel_signal, ec);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020095}
96
97static void
98text_model_deactivate(struct wl_client *client,
99 struct wl_resource *resource)
100{
101 struct text_model *text_model = resource->data;
102
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200103 deactivate_text_model(text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200104}
105
106static void
107text_model_set_selected_text(struct wl_client *client,
108 struct wl_resource *resource,
109 const char *text,
110 int32_t index)
111{
112}
113
114static void
115text_model_set_micro_focus(struct wl_client *client,
116 struct wl_resource *resource,
117 int32_t x,
118 int32_t y,
119 int32_t width,
120 int32_t height)
121{
122}
123
124static void
125text_model_set_preedit(struct wl_client *client,
126 struct wl_resource *resource)
127{
128}
129
130static void
131text_model_set_content_type(struct wl_client *client,
132 struct wl_resource *resource)
133{
134}
135
136struct text_model_interface text_model_implementation = {
137 text_model_set_surrounding_text,
138 text_model_set_cursor_index,
139 text_model_activate,
140 text_model_deactivate,
141 text_model_set_selected_text,
142 text_model_set_micro_focus,
143 text_model_set_preedit,
144 text_model_set_content_type
145};
146
147static void input_method_create_text_model(struct wl_client *client,
148 struct wl_resource *resource,
149 uint32_t id,
150 struct wl_resource *surface)
151{
152 struct input_method *input_method = resource->data;
153 struct text_model *text_model;
154
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200155 text_model = calloc(1, sizeof *text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200156
157 text_model->resource.destroy = destroy_text_model;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200158
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200159 text_model->resource.object.id = id;
160 text_model->resource.object.interface = &text_model_interface;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200161 text_model->resource.object.implementation =
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200162 (void (**)(void)) &text_model_implementation;
163 text_model->resource.data = text_model;
164
165 text_model->input_method = input_method;
166
167 wl_client_add_resource(client, &text_model->resource);
168
169 wl_list_insert(&input_method->models, &text_model->link);
170};
171
172static void
173input_method_commit_string(struct wl_client *client,
174 struct wl_resource *resource,
175 const char *text,
176 uint32_t index)
177{
178 struct input_method *input_method = resource->data;
179
180 if (input_method->active_model) {
181 text_model_send_commit_string(&input_method->active_model->resource, text, index);
182 }
183}
184
185struct input_method_interface input_method_implementation = {
186 input_method_create_text_model,
187 input_method_commit_string
188};
189
190static void
191bind_input_method(struct wl_client *client,
192 void *data,
193 uint32_t version,
194 uint32_t id)
195{
196 wl_client_add_object(client, &input_method_interface,
197 &input_method_implementation, id, data);
198}
199
200static void
201input_method_notifier_destroy(struct wl_listener *listener, void *data)
202{
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400203 struct input_method *input_method =
204 container_of(listener, struct input_method, destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200205
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400206 wl_display_remove_global(input_method->ec->wl_display,
207 input_method->global);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200208 free(input_method);
209}
210
211void
212input_method_create(struct weston_compositor *ec)
213{
214 struct input_method *input_method;
215
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200216 input_method = calloc(1, sizeof *input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200217
218 input_method->base.interface = &input_method_interface;
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400219 input_method->base.implementation =
220 (void(**)(void)) &input_method_implementation;
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200221 input_method->ec = ec;
222 input_method->active_model = NULL;
223
224 wl_list_init(&input_method->models);
225
226 input_method->global = wl_display_add_global(ec->wl_display,
227 &input_method_interface,
Kristian Høgsbergf97f3792012-07-22 11:51:42 -0400228 input_method,
229 bind_input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200230
231 input_method->destroy_listener.notify = input_method_notifier_destroy;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200232 wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200233}