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