Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 1 | /* |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 2 | * Copyright © 2012 Intel Corporation |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 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; |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 34 | struct wl_registry *registry; |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 35 | 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 | |
| 47 | static void |
| 48 | text_model_commit_string(void *data, |
| 49 | struct text_model *text_model, |
| 50 | const char *text, |
| 51 | uint32_t index) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | text_model_preedit_string(void *data, |
| 57 | struct text_model *text_model, |
| 58 | const char *text, |
| 59 | uint32_t index) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 64 | text_model_delete_surrounding_text(void *data, |
| 65 | struct text_model *text_model, |
| 66 | int32_t index, |
| 67 | uint32_t length) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | static void |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 72 | text_model_preedit_styling(void *data, |
| 73 | struct text_model *text_model) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | text_model_key(void *data, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 79 | struct text_model *text_model, |
| 80 | uint32_t key, |
| 81 | uint32_t state) |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 82 | { |
| 83 | } |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 84 | |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 85 | static void |
| 86 | text_model_selection_replacement(void *data, |
| 87 | struct text_model *text_model) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | text_model_direction(void *data, |
| 93 | struct text_model *text_model) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | text_model_locale(void *data, |
| 99 | struct text_model *text_model) |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | static void |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 104 | text_model_enter(void *data, |
| 105 | struct text_model *text_model, |
| 106 | struct wl_surface *surface) |
| 107 | |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 108 | { |
| 109 | struct display *display = data; |
| 110 | |
| 111 | fprintf(stderr, "%s\n", __FUNCTION__); |
| 112 | |
| 113 | display->activated += 1; |
| 114 | } |
| 115 | |
| 116 | static void |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 117 | text_model_leave(void *data, |
| 118 | struct text_model *text_model) |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 119 | { |
| 120 | struct display *display = data; |
| 121 | |
| 122 | display->deactivated += 1; |
| 123 | } |
| 124 | |
| 125 | static const struct text_model_listener text_model_listener = { |
| 126 | text_model_commit_string, |
| 127 | text_model_preedit_string, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 128 | text_model_delete_surrounding_text, |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 129 | text_model_preedit_styling, |
| 130 | text_model_key, |
| 131 | text_model_selection_replacement, |
| 132 | text_model_direction, |
| 133 | text_model_locale, |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 134 | text_model_enter, |
| 135 | text_model_leave |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static void |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 139 | handle_global(void *data, |
| 140 | struct wl_registry *registry, uint32_t id, |
| 141 | const char *interface, uint32_t version) |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 142 | { |
| 143 | struct display *display = data; |
| 144 | |
| 145 | if (strcmp(interface, "wl_compositor") == 0) { |
| 146 | display->compositor = |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 147 | wl_registry_bind(display->registry, |
| 148 | id, &wl_compositor_interface, 1); |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 149 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 150 | display->seat = wl_registry_bind(display->registry, id, |
| 151 | &wl_seat_interface, 1); |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 152 | } else if (strcmp(interface, "text_model_factory") == 0) { |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 153 | display->factory = wl_registry_bind(display->registry, id, |
| 154 | &text_model_factory_interface, 1); |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Kristian Høgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 158 | static const struct wl_registry_listener registry_listener = { |
| 159 | handle_global |
| 160 | }; |
| 161 | |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 162 | static void |
| 163 | create_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 | |
| 176 | static void |
| 177 | create_text_model(int fd, struct display *display) |
| 178 | { |
| 179 | char buf[64]; |
| 180 | int len; |
| 181 | |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 182 | display->text_model = text_model_factory_create_text_model(display->factory); |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 183 | 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 | |
| 191 | static void |
| 192 | write_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 | |
| 204 | static void |
| 205 | activate_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 | |
| 215 | static void |
| 216 | deactivate_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 | |
| 226 | int 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øgsberg | d1ecb28 | 2012-10-11 10:07:31 -0400 | [diff] [blame] | 241 | display->registry = wl_display_get_registry(display->display); |
| 242 | wl_registry_add_listener(display->registry, |
| 243 | ®istry_listener, display); |
| 244 | wl_display_dispatch(display->display); |
| 245 | wl_display_dispatch(display->display); |
Jan Arne Petersen | b00e663 | 2012-08-21 18:28:49 +0200 | [diff] [blame] | 246 | |
| 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 | } |