blob: 5104686b6b45ac51f23e8589053dc7ffb8fed763 [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{
51
52 if (text_model->input_method->active_model == text_model) {
53 text_model->input_method->active_model = NULL;
54 wl_signal_emit(&text_model->input_method->ec->hide_input_panel_signal, text_model->input_method->ec);
55 }
56}
57
58static void
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020059destroy_text_model(struct wl_resource *resource)
60{
61 struct text_model *text_model = container_of(resource,
62 struct text_model, resource);
63
Philipp Brüschweiler17467812012-07-11 22:25:30 +020064 deactivate_text_model(text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +020065
66 wl_list_remove(&text_model->link);
67 free(text_model);
68}
69
70static void
71text_model_set_surrounding_text(struct wl_client *client,
72 struct wl_resource *resource,
73 const char *text)
74{
75}
76
77static void
78text_model_set_cursor_index(struct wl_client *client,
79 struct wl_resource *resource,
80 uint32_t index)
81{
82}
83
84static void
85text_model_activate(struct wl_client *client,
86 struct wl_resource *resource)
87{
88 struct text_model *text_model = resource->data;
89
90 text_model->input_method->active_model = text_model;
91
92 wl_signal_emit(&text_model->input_method->ec->show_input_panel_signal, text_model->input_method->ec);
93}
94
95static void
96text_model_deactivate(struct wl_client *client,
97 struct wl_resource *resource)
98{
99 struct text_model *text_model = resource->data;
100
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200101 deactivate_text_model(text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200102}
103
104static void
105text_model_set_selected_text(struct wl_client *client,
106 struct wl_resource *resource,
107 const char *text,
108 int32_t index)
109{
110}
111
112static void
113text_model_set_micro_focus(struct wl_client *client,
114 struct wl_resource *resource,
115 int32_t x,
116 int32_t y,
117 int32_t width,
118 int32_t height)
119{
120}
121
122static void
123text_model_set_preedit(struct wl_client *client,
124 struct wl_resource *resource)
125{
126}
127
128static void
129text_model_set_content_type(struct wl_client *client,
130 struct wl_resource *resource)
131{
132}
133
134struct text_model_interface text_model_implementation = {
135 text_model_set_surrounding_text,
136 text_model_set_cursor_index,
137 text_model_activate,
138 text_model_deactivate,
139 text_model_set_selected_text,
140 text_model_set_micro_focus,
141 text_model_set_preedit,
142 text_model_set_content_type
143};
144
145static void input_method_create_text_model(struct wl_client *client,
146 struct wl_resource *resource,
147 uint32_t id,
148 struct wl_resource *surface)
149{
150 struct input_method *input_method = resource->data;
151 struct text_model *text_model;
152
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200153 text_model = calloc(1, sizeof *text_model);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200154
155 text_model->resource.destroy = destroy_text_model;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200156
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200157 text_model->resource.object.id = id;
158 text_model->resource.object.interface = &text_model_interface;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200159 text_model->resource.object.implementation =
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200160 (void (**)(void)) &text_model_implementation;
161 text_model->resource.data = text_model;
162
163 text_model->input_method = input_method;
164
165 wl_client_add_resource(client, &text_model->resource);
166
167 wl_list_insert(&input_method->models, &text_model->link);
168};
169
170static void
171input_method_commit_string(struct wl_client *client,
172 struct wl_resource *resource,
173 const char *text,
174 uint32_t index)
175{
176 struct input_method *input_method = resource->data;
177
178 if (input_method->active_model) {
179 text_model_send_commit_string(&input_method->active_model->resource, text, index);
180 }
181}
182
183struct input_method_interface input_method_implementation = {
184 input_method_create_text_model,
185 input_method_commit_string
186};
187
188static void
189bind_input_method(struct wl_client *client,
190 void *data,
191 uint32_t version,
192 uint32_t id)
193{
194 wl_client_add_object(client, &input_method_interface,
195 &input_method_implementation, id, data);
196}
197
198static void
199input_method_notifier_destroy(struct wl_listener *listener, void *data)
200{
201 struct input_method *input_method = container_of(listener, struct input_method, destroy_listener);
202
203 wl_display_remove_global(input_method->ec->wl_display, input_method->global);
204 free(input_method);
205}
206
207void
208input_method_create(struct weston_compositor *ec)
209{
210 struct input_method *input_method;
211
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200212 input_method = calloc(1, sizeof *input_method);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200213
214 input_method->base.interface = &input_method_interface;
215 input_method->base.implementation = (void(**)(void)) &input_method_implementation;
216 input_method->ec = ec;
217 input_method->active_model = NULL;
218
219 wl_list_init(&input_method->models);
220
221 input_method->global = wl_display_add_global(ec->wl_display,
222 &input_method_interface,
223 input_method, bind_input_method);
224
225 input_method->destroy_listener.notify = input_method_notifier_destroy;
Philipp Brüschweiler17467812012-07-11 22:25:30 +0200226 wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
Jan Arne Petersen1f17be42012-06-21 21:52:18 +0200227}