text-backend.c: Whitespace 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         struct weston_compositor *ec = text_model->input_method->ec;
52
53         if (text_model->input_method->active_model == text_model) {
54                 text_model->input_method->active_model = NULL;
55                 wl_signal_emit(&ec->hide_input_panel_signal, ec);
56         }
57 }
58
59 static void
60 destroy_text_model(struct wl_resource *resource)
61 {
62         struct text_model *text_model =
63                 container_of(resource, struct text_model, resource);
64
65         deactivate_text_model(text_model);
66
67         wl_list_remove(&text_model->link);
68         free(text_model);
69 }
70
71 static void
72 text_model_set_surrounding_text(struct wl_client *client,
73                                 struct wl_resource *resource,
74                                 const char *text)
75 {
76 }
77
78 static void
79 text_model_set_cursor_index(struct wl_client *client,
80                             struct wl_resource *resource,
81                             uint32_t index)
82 {
83 }
84
85 static void
86 text_model_activate(struct wl_client *client,
87                     struct wl_resource *resource)
88 {
89         struct text_model *text_model = resource->data;
90         struct weston_compositor *ec = text_model->input_method->ec;
91
92         text_model->input_method->active_model = text_model;
93
94         wl_signal_emit(&ec->show_input_panel_signal, ec);
95 }
96
97 static void
98 text_model_deactivate(struct wl_client *client,
99                       struct wl_resource *resource)
100 {
101         struct text_model *text_model = resource->data;
102
103         deactivate_text_model(text_model);
104 }
105
106 static void
107 text_model_set_selected_text(struct wl_client *client,
108                              struct wl_resource *resource,
109                              const char *text,
110                              int32_t index)
111 {
112 }
113
114 static void
115 text_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
124 static void
125 text_model_set_preedit(struct wl_client *client,
126                        struct wl_resource *resource)
127 {
128 }
129
130 static void
131 text_model_set_content_type(struct wl_client *client,
132                             struct wl_resource *resource)
133 {
134 }
135
136 struct 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
147 static 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
155         text_model = calloc(1, sizeof *text_model);
156
157         text_model->resource.destroy = destroy_text_model;
158
159         text_model->resource.object.id = id;
160         text_model->resource.object.interface = &text_model_interface;
161         text_model->resource.object.implementation =
162                 (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
172 static void
173 input_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
185 struct input_method_interface input_method_implementation = {
186         input_method_create_text_model,
187         input_method_commit_string
188 };
189
190 static void
191 bind_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
200 static void
201 input_method_notifier_destroy(struct wl_listener *listener, void *data)
202 {
203         struct input_method *input_method =
204                 container_of(listener, struct input_method, destroy_listener);
205
206         wl_display_remove_global(input_method->ec->wl_display,
207                                  input_method->global);
208         free(input_method);
209 }
210
211 void
212 input_method_create(struct weston_compositor *ec)
213 {
214         struct input_method *input_method;
215
216         input_method = calloc(1, sizeof *input_method);
217
218         input_method->base.interface = &input_method_interface;
219         input_method->base.implementation =
220                 (void(**)(void)) &input_method_implementation;
221         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,
228                                                      input_method,
229                                                      bind_input_method);
230
231         input_method->destroy_listener.notify = input_method_notifier_destroy;
232         wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener);
233 }