text: Add delete_surrounding_text to protocol
[profile/ivi/weston.git] / tests / test-text-client.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <poll.h>
29 #include <wayland-client.h>
30 #include "../clients/text-client-protocol.h"
31
32 struct display {
33         struct wl_display *display;
34         struct wl_compositor *compositor;
35
36         struct wl_surface *surface;
37         struct wl_seat *seat;
38
39         struct text_model_factory *factory;
40         struct text_model *text_model;
41
42         unsigned int activated;
43         unsigned int deactivated;
44 };
45
46 static void 
47 text_model_commit_string(void *data,
48                          struct text_model *text_model,
49                          const char *text,
50                          uint32_t index)
51 {
52 }
53
54 static void
55 text_model_preedit_string(void *data,
56                           struct text_model *text_model,
57                           const char *text,
58                           uint32_t index)
59 {
60 }
61
62 static void
63 text_model_delete_surrounding_text(void *data,
64                                    struct text_model *text_model,
65                                    int32_t index,
66                                    uint32_t length)
67 {
68 }
69
70 static void
71 text_model_preedit_styling(void *data,
72                            struct text_model *text_model)
73 {
74 }
75
76 static void 
77 text_model_key(void *data,
78                struct text_model *text_model)
79 {
80 }
81         
82 static void
83 text_model_selection_replacement(void *data,
84                                  struct text_model *text_model)
85 {
86 }
87
88 static void
89 text_model_direction(void *data,
90                      struct text_model *text_model)
91 {
92 }
93
94 static void
95 text_model_locale(void *data,
96                   struct text_model *text_model)
97 {
98 }
99
100 static void
101 text_model_activated(void *data,
102                      struct text_model *text_model)
103 {
104         struct display *display = data;
105
106         fprintf(stderr, "%s\n", __FUNCTION__);
107
108         display->activated += 1;
109 }
110
111 static void
112 text_model_deactivated(void *data,
113                        struct text_model *text_model)
114 {
115         struct display *display = data;
116
117         display->deactivated += 1;
118 }
119
120 static const struct text_model_listener text_model_listener = {
121         text_model_commit_string,
122         text_model_preedit_string,
123         text_model_delete_surrounding_text,
124         text_model_preedit_styling,
125         text_model_key,
126         text_model_selection_replacement,
127         text_model_direction,
128         text_model_locale,
129         text_model_activated,
130         text_model_deactivated
131 };
132
133 static void
134 handle_global(struct wl_display *_display, uint32_t id,
135               const char *interface, uint32_t version, void *data)
136 {
137         struct display *display = data;
138
139         if (strcmp(interface, "wl_compositor") == 0) {
140                 display->compositor =
141                         wl_display_bind(display->display,
142                                         id, &wl_compositor_interface);
143         } else if (strcmp(interface, "wl_seat") == 0) {
144                 display->seat = wl_display_bind(display->display, id,
145                                                 &wl_seat_interface);
146         } else if (strcmp(interface, "text_model_factory") == 0) {
147                 display->factory = wl_display_bind(display->display, id,
148                                                    &text_model_factory_interface);
149         }
150 }
151
152 static void
153 create_surface(int fd, struct display *display)
154 {
155         char buf[64];
156         int len;
157
158         display->surface = wl_compositor_create_surface(display->compositor);
159         wl_display_flush(display->display);
160
161         len = snprintf(buf, sizeof buf, "surface %d\n",
162                        wl_proxy_get_id((struct wl_proxy *) display->surface));
163         assert(write(fd, buf, len) == len);
164 }
165
166 static void
167 create_text_model(int fd, struct display *display)
168 {
169         char buf[64];
170         int len;
171
172         display->text_model = text_model_factory_create_text_model(display->factory);
173         text_model_add_listener(display->text_model, &text_model_listener, display);
174         wl_display_flush(display->display);
175
176         len = snprintf(buf, sizeof buf, "text_model %d\n",
177                        wl_proxy_get_id((struct wl_proxy *) display->text_model));
178         assert(write(fd, buf, len) == len);
179 }
180
181 static void
182 write_state(int fd, struct display *display)
183 {
184         char buf[64];
185         int len;
186
187         wl_display_flush(display->display);
188         len = snprintf(buf, sizeof buf, "activated %u deactivated %u\n",
189                        display->activated, display->deactivated);
190         assert(write(fd, buf, len) == len);
191         wl_display_roundtrip(display->display);
192 }
193
194 static void
195 activate_text_model(int fd, struct display *display)
196 {
197         write_state(fd, display);
198
199         text_model_activate(display->text_model, display->seat, display->surface);
200
201         wl_display_flush(display->display);
202         wl_display_roundtrip(display->display);
203 }
204
205 static void
206 deactivate_text_model(int fd, struct display *display)
207 {
208         write_state(fd, display);
209
210         text_model_deactivate(display->text_model, display->seat);
211
212         wl_display_flush(display->display);
213         wl_display_roundtrip(display->display);
214 }
215
216 int main(int argc, char *argv[])
217 {
218         struct display *display;
219         char buf[256], *p;
220         int ret, fd;
221
222         display = malloc(sizeof *display);
223         assert(display);
224
225         display->display = wl_display_connect(NULL);
226         assert(display->display);
227
228         display->activated = 0;
229         display->deactivated = 0;
230
231         wl_display_add_global_listener(display->display,
232                                        handle_global, display);
233         wl_display_iterate(display->display, WL_DISPLAY_READABLE);
234         wl_display_roundtrip(display->display);
235
236         fd = 0;
237         p = getenv("TEST_SOCKET");
238         if (p)
239                 fd = strtol(p, NULL, 0);
240
241         while (1) {
242                 ret = read(fd, buf, sizeof buf);
243                 if (ret == -1) {
244                         fprintf(stderr, "read error: fd %d, %m\n", fd);
245                         return -1;
246                 }
247
248                 fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
249
250                 if (strncmp(buf, "bye\n", ret) == 0) {
251                         return 0;
252                 } else if (strncmp(buf, "create-surface\n", ret) == 0) {
253                         create_surface(fd, display);
254                 } else if (strncmp(buf, "create-text-model\n", ret) == 0) {
255                         create_text_model(fd, display);
256                 } else if (strncmp(buf, "activate-text-model\n", ret) == 0) {
257                         activate_text_model(fd, display);
258                 } else if (strncmp(buf, "deactivate-text-model\n", ret) == 0) {
259                         deactivate_text_model(fd, display);
260                 } else if (strncmp(buf, "assert-state\n", ret) == 0) {
261                         write_state(fd, display);
262                 } else {
263                         fprintf(stderr, "unknown command %.*s\n", ret, buf);
264                         return -1;
265                 }
266         }
267
268         assert(0);
269 }