text: Remove surface arg in create_text_model
[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_preedit_styling(void *data,
64                            struct text_model *text_model)
65 {
66 }
67
68 static void 
69 text_model_key(void *data,
70                struct text_model *text_model)
71 {
72 }
73         
74 static void
75 text_model_selection_replacement(void *data,
76                                  struct text_model *text_model)
77 {
78 }
79
80 static void
81 text_model_direction(void *data,
82                      struct text_model *text_model)
83 {
84 }
85
86 static void
87 text_model_locale(void *data,
88                   struct text_model *text_model)
89 {
90 }
91
92 static void
93 text_model_activated(void *data,
94                      struct text_model *text_model)
95 {
96         struct display *display = data;
97
98         fprintf(stderr, "%s\n", __FUNCTION__);
99
100         display->activated += 1;
101 }
102
103 static void
104 text_model_deactivated(void *data,
105                        struct text_model *text_model)
106 {
107         struct display *display = data;
108
109         display->deactivated += 1;
110 }
111
112 static const struct text_model_listener text_model_listener = {
113         text_model_commit_string,
114         text_model_preedit_string,
115         text_model_preedit_styling,
116         text_model_key,
117         text_model_selection_replacement,
118         text_model_direction,
119         text_model_locale,
120         text_model_activated,
121         text_model_deactivated
122 };
123
124 static void
125 handle_global(struct wl_display *_display, uint32_t id,
126               const char *interface, uint32_t version, void *data)
127 {
128         struct display *display = data;
129
130         if (strcmp(interface, "wl_compositor") == 0) {
131                 display->compositor =
132                         wl_display_bind(display->display,
133                                         id, &wl_compositor_interface);
134         } else if (strcmp(interface, "wl_seat") == 0) {
135                 display->seat = wl_display_bind(display->display, id,
136                                                 &wl_seat_interface);
137         } else if (strcmp(interface, "text_model_factory") == 0) {
138                 display->factory = wl_display_bind(display->display, id,
139                                                    &text_model_factory_interface);
140         }
141 }
142
143 static void
144 create_surface(int fd, struct display *display)
145 {
146         char buf[64];
147         int len;
148
149         display->surface = wl_compositor_create_surface(display->compositor);
150         wl_display_flush(display->display);
151
152         len = snprintf(buf, sizeof buf, "surface %d\n",
153                        wl_proxy_get_id((struct wl_proxy *) display->surface));
154         assert(write(fd, buf, len) == len);
155 }
156
157 static void
158 create_text_model(int fd, struct display *display)
159 {
160         char buf[64];
161         int len;
162
163         display->text_model = text_model_factory_create_text_model(display->factory);
164         text_model_add_listener(display->text_model, &text_model_listener, display);
165         wl_display_flush(display->display);
166
167         len = snprintf(buf, sizeof buf, "text_model %d\n",
168                        wl_proxy_get_id((struct wl_proxy *) display->text_model));
169         assert(write(fd, buf, len) == len);
170 }
171
172 static void
173 write_state(int fd, struct display *display)
174 {
175         char buf[64];
176         int len;
177
178         wl_display_flush(display->display);
179         len = snprintf(buf, sizeof buf, "activated %u deactivated %u\n",
180                        display->activated, display->deactivated);
181         assert(write(fd, buf, len) == len);
182         wl_display_roundtrip(display->display);
183 }
184
185 static void
186 activate_text_model(int fd, struct display *display)
187 {
188         write_state(fd, display);
189
190         text_model_activate(display->text_model, display->seat, display->surface);
191
192         wl_display_flush(display->display);
193         wl_display_roundtrip(display->display);
194 }
195
196 static void
197 deactivate_text_model(int fd, struct display *display)
198 {
199         write_state(fd, display);
200
201         text_model_deactivate(display->text_model, display->seat);
202
203         wl_display_flush(display->display);
204         wl_display_roundtrip(display->display);
205 }
206
207 int main(int argc, char *argv[])
208 {
209         struct display *display;
210         char buf[256], *p;
211         int ret, fd;
212
213         display = malloc(sizeof *display);
214         assert(display);
215
216         display->display = wl_display_connect(NULL);
217         assert(display->display);
218
219         display->activated = 0;
220         display->deactivated = 0;
221
222         wl_display_add_global_listener(display->display,
223                                        handle_global, display);
224         wl_display_iterate(display->display, WL_DISPLAY_READABLE);
225         wl_display_roundtrip(display->display);
226
227         fd = 0;
228         p = getenv("TEST_SOCKET");
229         if (p)
230                 fd = strtol(p, NULL, 0);
231
232         while (1) {
233                 ret = read(fd, buf, sizeof buf);
234                 if (ret == -1) {
235                         fprintf(stderr, "read error: fd %d, %m\n", fd);
236                         return -1;
237                 }
238
239                 fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
240
241                 if (strncmp(buf, "bye\n", ret) == 0) {
242                         return 0;
243                 } else if (strncmp(buf, "create-surface\n", ret) == 0) {
244                         create_surface(fd, display);
245                 } else if (strncmp(buf, "create-text-model\n", ret) == 0) {
246                         create_text_model(fd, display);
247                 } else if (strncmp(buf, "activate-text-model\n", ret) == 0) {
248                         activate_text_model(fd, display);
249                 } else if (strncmp(buf, "deactivate-text-model\n", ret) == 0) {
250                         deactivate_text_model(fd, display);
251                 } else if (strncmp(buf, "assert-state\n", ret) == 0) {
252                         write_state(fd, display);
253                 } else {
254                         fprintf(stderr, "unknown command %.*s\n", ret, buf);
255                         return -1;
256                 }
257         }
258
259         assert(0);
260 }