blob: 73423811c9130c27e5a840e5ec15324722c5cd19 [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;
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -040034 struct wl_registry *registry;
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020035 struct wl_compositor *compositor;
36
37 struct wl_surface *surface;
38 struct wl_seat *seat;
39
40 struct text_model_factory *factory;
41 struct text_model *text_model;
42
43 unsigned int activated;
44 unsigned int deactivated;
45};
46
47static void
48text_model_commit_string(void *data,
49 struct text_model *text_model,
50 const char *text,
51 uint32_t index)
52{
53}
54
55static void
56text_model_preedit_string(void *data,
57 struct text_model *text_model,
58 const char *text,
59 uint32_t index)
60{
61}
62
63static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +020064text_model_delete_surrounding_text(void *data,
65 struct text_model *text_model,
66 int32_t index,
67 uint32_t length)
68{
69}
70
71static void
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020072text_model_preedit_styling(void *data,
73 struct text_model *text_model)
74{
75}
76
77static void
78text_model_key(void *data,
Jan Arne Petersence8a4432012-09-09 23:08:45 +020079 struct text_model *text_model,
80 uint32_t key,
81 uint32_t state)
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020082{
83}
Jan Arne Petersence8a4432012-09-09 23:08:45 +020084
Jan Arne Petersenb00e6632012-08-21 18:28:49 +020085static void
86text_model_selection_replacement(void *data,
87 struct text_model *text_model)
88{
89}
90
91static void
92text_model_direction(void *data,
93 struct text_model *text_model)
94{
95}
96
97static void
98text_model_locale(void *data,
99 struct text_model *text_model)
100{
101}
102
103static void
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400104text_model_enter(void *data,
105 struct text_model *text_model,
106 struct wl_surface *surface)
107
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200108{
109 struct display *display = data;
110
111 fprintf(stderr, "%s\n", __FUNCTION__);
112
113 display->activated += 1;
114}
115
116static void
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400117text_model_leave(void *data,
118 struct text_model *text_model)
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200119{
120 struct display *display = data;
121
122 display->deactivated += 1;
123}
124
125static const struct text_model_listener text_model_listener = {
126 text_model_commit_string,
127 text_model_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200128 text_model_delete_surrounding_text,
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200129 text_model_preedit_styling,
130 text_model_key,
131 text_model_selection_replacement,
132 text_model_direction,
133 text_model_locale,
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400134 text_model_enter,
135 text_model_leave
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200136};
137
138static void
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400139handle_global(void *data,
140 struct wl_registry *registry, uint32_t id,
141 const char *interface, uint32_t version)
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200142{
143 struct display *display = data;
144
145 if (strcmp(interface, "wl_compositor") == 0) {
146 display->compositor =
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400147 wl_registry_bind(display->registry,
148 id, &wl_compositor_interface, 1);
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200149 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400150 display->seat = wl_registry_bind(display->registry, id,
151 &wl_seat_interface, 1);
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200152 } else if (strcmp(interface, "text_model_factory") == 0) {
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400153 display->factory = wl_registry_bind(display->registry, id,
154 &text_model_factory_interface, 1);
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200155 }
156}
157
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400158static const struct wl_registry_listener registry_listener = {
159 handle_global
160};
161
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200162static void
163create_surface(int fd, struct display *display)
164{
165 char buf[64];
166 int len;
167
168 display->surface = wl_compositor_create_surface(display->compositor);
169 wl_display_flush(display->display);
170
171 len = snprintf(buf, sizeof buf, "surface %d\n",
172 wl_proxy_get_id((struct wl_proxy *) display->surface));
173 assert(write(fd, buf, len) == len);
174}
175
176static void
177create_text_model(int fd, struct display *display)
178{
179 char buf[64];
180 int len;
181
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200182 display->text_model = text_model_factory_create_text_model(display->factory);
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200183 text_model_add_listener(display->text_model, &text_model_listener, display);
184 wl_display_flush(display->display);
185
186 len = snprintf(buf, sizeof buf, "text_model %d\n",
187 wl_proxy_get_id((struct wl_proxy *) display->text_model));
188 assert(write(fd, buf, len) == len);
189}
190
191static void
192write_state(int fd, struct display *display)
193{
194 char buf[64];
195 int len;
196
197 wl_display_flush(display->display);
198 len = snprintf(buf, sizeof buf, "activated %u deactivated %u\n",
199 display->activated, display->deactivated);
200 assert(write(fd, buf, len) == len);
201 wl_display_roundtrip(display->display);
202}
203
204static void
205activate_text_model(int fd, struct display *display)
206{
207 write_state(fd, display);
208
209 text_model_activate(display->text_model, display->seat, display->surface);
210
211 wl_display_flush(display->display);
212 wl_display_roundtrip(display->display);
213}
214
215static void
216deactivate_text_model(int fd, struct display *display)
217{
218 write_state(fd, display);
219
220 text_model_deactivate(display->text_model, display->seat);
221
222 wl_display_flush(display->display);
223 wl_display_roundtrip(display->display);
224}
225
226int main(int argc, char *argv[])
227{
228 struct display *display;
229 char buf[256], *p;
230 int ret, fd;
231
232 display = malloc(sizeof *display);
233 assert(display);
234
235 display->display = wl_display_connect(NULL);
236 assert(display->display);
237
238 display->activated = 0;
239 display->deactivated = 0;
240
Kristian Høgsbergd1ecb282012-10-11 10:07:31 -0400241 display->registry = wl_display_get_registry(display->display);
242 wl_registry_add_listener(display->registry,
243 &registry_listener, display);
244 wl_display_dispatch(display->display);
245 wl_display_dispatch(display->display);
Jan Arne Petersenb00e6632012-08-21 18:28:49 +0200246
247 fd = 0;
248 p = getenv("TEST_SOCKET");
249 if (p)
250 fd = strtol(p, NULL, 0);
251
252 while (1) {
253 ret = read(fd, buf, sizeof buf);
254 if (ret == -1) {
255 fprintf(stderr, "read error: fd %d, %m\n", fd);
256 return -1;
257 }
258
259 fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
260
261 if (strncmp(buf, "bye\n", ret) == 0) {
262 return 0;
263 } else if (strncmp(buf, "create-surface\n", ret) == 0) {
264 create_surface(fd, display);
265 } else if (strncmp(buf, "create-text-model\n", ret) == 0) {
266 create_text_model(fd, display);
267 } else if (strncmp(buf, "activate-text-model\n", ret) == 0) {
268 activate_text_model(fd, display);
269 } else if (strncmp(buf, "deactivate-text-model\n", ret) == 0) {
270 deactivate_text_model(fd, display);
271 } else if (strncmp(buf, "assert-state\n", ret) == 0) {
272 write_state(fd, display);
273 } else {
274 fprintf(stderr, "unknown command %.*s\n", ret, buf);
275 return -1;
276 }
277 }
278
279 assert(0);
280}