text-backend: minor fixes
[profile/ivi/weston.git] / src / text-backend.c
1 /*
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
28 struct input_method;
29
30 struct text_model {
31         struct wl_resource resource;
32
33         struct wl_list link;
34
35         struct input_method *input_method;
36 };
37
38 struct 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
48 static void
49 deactivate_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
58 static void
59 destroy_text_model(struct wl_resource *resource)
60 {
61         struct text_model *text_model = container_of(resource,
62                                                      struct text_model, resource);
63
64         deactivate_text_model(text_model);
65
66         wl_list_remove(&text_model->link);
67         free(text_model);
68 }
69
70 static void
71 text_model_set_surrounding_text(struct wl_client *client,
72                                 struct wl_resource *resource,
73                                 const char *text)
74 {
75 }
76
77 static void
78 text_model_set_cursor_index(struct wl_client *client,
79                             struct wl_resource *resource,
80                             uint32_t index)
81 {
82 }
83
84 static void
85 text_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
95 static void
96 text_model_deactivate(struct wl_client *client,
97                       struct wl_resource *resource)
98 {
99         struct text_model *text_model = resource->data;
100
101         deactivate_text_model(text_model);
102 }
103
104 static void
105 text_model_set_selected_text(struct wl_client *client,
106                              struct wl_resource *resource,
107                              const char *text,
108                              int32_t index)
109 {
110 }
111
112 static void
113 text_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
122 static void
123 text_model_set_preedit(struct wl_client *client,
124                        struct wl_resource *resource)
125 {
126 }
127
128 static void
129 text_model_set_content_type(struct wl_client *client,
130                             struct wl_resource *resource)
131 {
132 }
133
134 struct 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
145 static 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
153         text_model = calloc(1, sizeof *text_model);
154
155         text_model->resource.destroy = destroy_text_model;
156
157         text_model->resource.object.id = id;
158         text_model->resource.object.interface = &text_model_interface;
159         text_model->resource.object.implementation =
160                 (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
170 static void
171 input_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
183 struct input_method_interface input_method_implementation = {
184         input_method_create_text_model,
185         input_method_commit_string
186 };
187
188 static void
189 bind_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
198 static void
199 input_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
207 void
208 input_method_create(struct weston_compositor *ec)
209 {
210         struct input_method *input_method;
211
212         input_method = calloc(1, sizeof *input_method);
213
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;
226         wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
227 }