blob: 897909af549fd1600f438c61ab73fa44788eff9b [file] [log] [blame]
Jan Arne Petersenb00e6632012-08-21 18:28:49 +02001/*
Jan Arne Petersen4c265182012-09-09 23:08:30 +02002 * Copyright © 2012 Intel Corporation
Jan Arne Petersenb00e6632012-08-21 18:28:49 +02003 *
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
32struct 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
46static void
47text_model_commit_string(void *data,
48 struct text_model *text_model,
49 const char *text,
50 uint32_t index)
51{
52}
53
54static void
55text_model_preedit_string(void *data,
56 struct text_model *text_model,
57 const char *text,
58 uint32_t index)
59{
60}
61
62static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +020063text_model_delete_surrounding_text(void *data,
64 struct text_model *text_model,
65 int32_t index,
66 uint32_t length)
67{
68}
69
70static void
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020071text_model_preedit_styling(void *data,
72 struct text_model *text_model)
73{
74}
75
76static void
77text_model_key(void *data,
Jan Arne Petersence8a4432012-09-09 23:08:45 +020078 struct text_model *text_model,
79 uint32_t key,
80 uint32_t state)
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020081{
82}
Jan Arne Petersence8a4432012-09-09 23:08:45 +020083
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020084static void
85text_model_selection_replacement(void *data,
86 struct text_model *text_model)
87{
88}
89
90static void
91text_model_direction(void *data,
92 struct text_model *text_model)
93{
94}
95
96static void
97text_model_locale(void *data,
98 struct text_model *text_model)
99{
100}
101
102static void
103text_model_activated(void *data,
104 struct text_model *text_model)
105{
106 struct display *display = data;
107
108 fprintf(stderr, "%s\n", __FUNCTION__);
109
110 display->activated += 1;
111}
112
113static void
114text_model_deactivated(void *data,
115 struct text_model *text_model)
116{
117 struct display *display = data;
118
119 display->deactivated += 1;
120}
121
122static const struct text_model_listener text_model_listener = {
123 text_model_commit_string,
124 text_model_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200125 text_model_delete_surrounding_text,
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200126 text_model_preedit_styling,
127 text_model_key,
128 text_model_selection_replacement,
129 text_model_direction,
130 text_model_locale,
131 text_model_activated,
132 text_model_deactivated
133};
134
135static void
136handle_global(struct wl_display *_display, uint32_t id,
137 const char *interface, uint32_t version, void *data)
138{
139 struct display *display = data;
140
141 if (strcmp(interface, "wl_compositor") == 0) {
142 display->compositor =
143 wl_display_bind(display->display,
144 id, &wl_compositor_interface);
145 } else if (strcmp(interface, "wl_seat") == 0) {
146 display->seat = wl_display_bind(display->display, id,
147 &wl_seat_interface);
148 } else if (strcmp(interface, "text_model_factory") == 0) {
149 display->factory = wl_display_bind(display->display, id,
150 &text_model_factory_interface);
151 }
152}
153
154static void
155create_surface(int fd, struct display *display)
156{
157 char buf[64];
158 int len;
159
160 display->surface = wl_compositor_create_surface(display->compositor);
161 wl_display_flush(display->display);
162
163 len = snprintf(buf, sizeof buf, "surface %d\n",
164 wl_proxy_get_id((struct wl_proxy *) display->surface));
165 assert(write(fd, buf, len) == len);
166}
167
168static void
169create_text_model(int fd, struct display *display)
170{
171 char buf[64];
172 int len;
173
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200174 display->text_model = text_model_factory_create_text_model(display->factory);
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200175 text_model_add_listener(display->text_model, &text_model_listener, display);
176 wl_display_flush(display->display);
177
178 len = snprintf(buf, sizeof buf, "text_model %d\n",
179 wl_proxy_get_id((struct wl_proxy *) display->text_model));
180 assert(write(fd, buf, len) == len);
181}
182
183static void
184write_state(int fd, struct display *display)
185{
186 char buf[64];
187 int len;
188
189 wl_display_flush(display->display);
190 len = snprintf(buf, sizeof buf, "activated %u deactivated %u\n",
191 display->activated, display->deactivated);
192 assert(write(fd, buf, len) == len);
193 wl_display_roundtrip(display->display);
194}
195
196static void
197activate_text_model(int fd, struct display *display)
198{
199 write_state(fd, display);
200
201 text_model_activate(display->text_model, display->seat, display->surface);
202
203 wl_display_flush(display->display);
204 wl_display_roundtrip(display->display);
205}
206
207static void
208deactivate_text_model(int fd, struct display *display)
209{
210 write_state(fd, display);
211
212 text_model_deactivate(display->text_model, display->seat);
213
214 wl_display_flush(display->display);
215 wl_display_roundtrip(display->display);
216}
217
218int main(int argc, char *argv[])
219{
220 struct display *display;
221 char buf[256], *p;
222 int ret, fd;
223
224 display = malloc(sizeof *display);
225 assert(display);
226
227 display->display = wl_display_connect(NULL);
228 assert(display->display);
229
230 display->activated = 0;
231 display->deactivated = 0;
232
233 wl_display_add_global_listener(display->display,
234 handle_global, display);
235 wl_display_iterate(display->display, WL_DISPLAY_READABLE);
236 wl_display_roundtrip(display->display);
237
238 fd = 0;
239 p = getenv("TEST_SOCKET");
240 if (p)
241 fd = strtol(p, NULL, 0);
242
243 while (1) {
244 ret = read(fd, buf, sizeof buf);
245 if (ret == -1) {
246 fprintf(stderr, "read error: fd %d, %m\n", fd);
247 return -1;
248 }
249
250 fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
251
252 if (strncmp(buf, "bye\n", ret) == 0) {
253 return 0;
254 } else if (strncmp(buf, "create-surface\n", ret) == 0) {
255 create_surface(fd, display);
256 } else if (strncmp(buf, "create-text-model\n", ret) == 0) {
257 create_text_model(fd, display);
258 } else if (strncmp(buf, "activate-text-model\n", ret) == 0) {
259 activate_text_model(fd, display);
260 } else if (strncmp(buf, "deactivate-text-model\n", ret) == 0) {
261 deactivate_text_model(fd, display);
262 } else if (strncmp(buf, "assert-state\n", ret) == 0) {
263 write_state(fd, display);
264 } else {
265 fprintf(stderr, "unknown command %.*s\n", ret, buf);
266 return -1;
267 }
268 }
269
270 assert(0);
271}