Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stddef.h> |
| 4 | #include <stdio.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/un.h> |
| 10 | #include <ffi.h> |
| 11 | #include "wayland.h" |
| 12 | |
| 13 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 14 | |
| 15 | struct wl_region { |
| 16 | }; |
| 17 | |
| 18 | struct wl_client { |
| 19 | char protocol_buffer[4096]; |
| 20 | struct wl_event_source *source; |
| 21 | struct wl_display *display; |
| 22 | }; |
| 23 | |
| 24 | struct wl_display { |
| 25 | struct wl_object base; |
| 26 | struct wl_event_loop *loop; |
| 27 | struct wl_hash objects; |
| 28 | }; |
| 29 | |
| 30 | struct wl_surface { |
| 31 | struct wl_object base; |
| 32 | |
| 33 | /* provided by client */ |
| 34 | int width, height; |
| 35 | int buffer; |
| 36 | int stride; |
| 37 | |
| 38 | /* these are used by the wayland server, not set from client */ |
| 39 | void (*screen_to_surface)(int sx, int sy, int *wx, int *wy); |
| 40 | void (*surface_to_screen)(int sx, int sy, int *wx, int *wy); |
| 41 | |
| 42 | /* how to convert buffer contents to pixels in screen format; |
| 43 | * yuv->rgb, indexed->rgb, svg->rgb, but mostly just rgb->rgb. */ |
| 44 | |
| 45 | /* how to transform/render rectangular contents to polygons. */ |
| 46 | }; |
| 47 | |
| 48 | static void |
| 49 | wl_surface_get_interface(struct wl_client *client, |
| 50 | struct wl_surface *surface, |
| 51 | const char *interface, uint32_t id) |
| 52 | { |
| 53 | /* client sends a new object id, and an interface identifier |
| 54 | * to name the object that implements the interface */ |
| 55 | printf("surface::get_interface\n"); |
| 56 | } |
| 57 | |
| 58 | static const struct wl_argument get_interface_arguments[] = { |
| 59 | { WL_ARGUMENT_STRING }, |
| 60 | { WL_ARGUMENT_NEW_ID } |
| 61 | }; |
| 62 | |
| 63 | static void |
| 64 | wl_surface_post(struct wl_client *client, |
| 65 | struct wl_surface *surface, uint32_t name, |
| 66 | uint32_t width, uint32_t height, uint32_t stride) |
| 67 | { |
| 68 | printf("surface::post\n"); |
| 69 | } |
| 70 | |
| 71 | static const struct wl_argument post_arguments[] = { |
| 72 | { WL_ARGUMENT_UINT32 }, |
| 73 | { WL_ARGUMENT_UINT32 }, |
| 74 | { WL_ARGUMENT_UINT32 }, |
| 75 | { WL_ARGUMENT_UINT32 }, |
| 76 | }; |
| 77 | |
| 78 | void |
| 79 | wl_surface_update(struct wl_surface *surface, |
| 80 | uint32_t source_name, struct wl_region *region) |
| 81 | { |
| 82 | /* FIXME: how to demarshal region? */ |
| 83 | /* copy region from buffer into current window contents. */ |
| 84 | } |
| 85 | |
| 86 | static const struct wl_argument update_arguments[] = { |
| 87 | { WL_ARGUMENT_UINT32 }, |
| 88 | { WL_ARGUMENT_UINT32 }, |
| 89 | }; |
| 90 | |
| 91 | static const struct wl_method surface_methods[] = { |
| 92 | { "get_interface", wl_surface_get_interface, |
| 93 | ARRAY_LENGTH(get_interface_arguments), get_interface_arguments }, |
| 94 | { "post", wl_surface_post, |
| 95 | ARRAY_LENGTH(post_arguments), post_arguments } |
| 96 | }; |
| 97 | |
| 98 | static const struct wl_interface surface_interface = { |
| 99 | "surface", 1, |
| 100 | ARRAY_LENGTH(surface_methods), |
| 101 | surface_methods, |
| 102 | }; |
| 103 | |
| 104 | struct wl_surface * |
| 105 | wl_surface_create(struct wl_display *display, uint32_t id) |
| 106 | { |
| 107 | struct wl_surface *surface; |
| 108 | |
| 109 | surface = malloc(sizeof *surface); |
| 110 | if (surface == NULL) |
| 111 | return NULL; |
| 112 | |
| 113 | surface->base.id = id; |
| 114 | surface->base.interface = &surface_interface; |
| 115 | /* doesn't create any pixel buffers, just the wl_surface |
| 116 | * object. the client allocates and attaches pixel buffers |
| 117 | * itself and renders to them, then posts them using |
| 118 | * wl_window_post. */ |
| 119 | |
| 120 | /* add to display list */ |
| 121 | |
| 122 | return surface; |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | wl_client_data(int fd, uint32_t mask, void *data); |
| 127 | |
| 128 | struct wl_client * |
| 129 | wl_client_create(struct wl_display *display, int fd) |
| 130 | { |
| 131 | struct wl_client *client; |
| 132 | |
| 133 | client = malloc(sizeof *client); |
| 134 | if (client == NULL) |
| 135 | return NULL; |
| 136 | |
| 137 | client->display = display; |
| 138 | |
| 139 | client->source = wl_event_loop_add_fd(display->loop, fd, |
| 140 | WL_EVENT_READABLE | |
| 141 | WL_EVENT_READABLE, |
| 142 | wl_client_data, client); |
| 143 | |
| 144 | printf("new client: %p\n", client); |
| 145 | |
| 146 | /* Send global objects to client in hand shake response, eg: |
| 147 | * |
| 148 | * display: 0, |
| 149 | * glyph_cache: 1 |
| 150 | * some other extension global object: 2 |
| 151 | * |
| 152 | * etc */ |
| 153 | |
| 154 | return client; |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | wl_client_destroy(struct wl_client *client) |
| 159 | { |
| 160 | printf("disconnect from client %p\n", client); |
| 161 | wl_event_loop_remove_source(client->display->loop, client->source); |
| 162 | free(client); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | demarshal(struct wl_client *client, struct wl_object *target, |
| 167 | const struct wl_method *method, uint32_t *data, int len) |
| 168 | { |
| 169 | ffi_type *types[10]; |
| 170 | ffi_cif cif; |
| 171 | uint32_t *p, result; |
| 172 | int i; |
| 173 | union { |
| 174 | uint32_t uint32; |
| 175 | const char *string; |
| 176 | void *object; |
| 177 | uint32_t new_id; |
| 178 | } values[10]; |
| 179 | void *args[10]; |
| 180 | struct wl_object *object; |
| 181 | |
| 182 | if (method->argument_count > ARRAY_LENGTH(types)) { |
| 183 | printf("too many args (%d)\n", method->argument_count); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | types[0] = &ffi_type_pointer; |
| 188 | values[0].object = client; |
| 189 | args[0] = &values[0]; |
| 190 | |
| 191 | types[1] = &ffi_type_pointer; |
| 192 | values[1].object = target; |
| 193 | args[1] = &values[1]; |
| 194 | |
| 195 | p = data; |
| 196 | for (i = 0; i < method->argument_count; i++) { |
| 197 | switch (method->arguments[i].type) { |
| 198 | case WL_ARGUMENT_UINT32: |
| 199 | types[i + 2] = &ffi_type_uint32; |
| 200 | values[i + 2].uint32 = *p; |
| 201 | printf("got uint32 (%d)\n", *p); |
| 202 | p++; |
| 203 | break; |
| 204 | case WL_ARGUMENT_STRING: |
| 205 | printf("got string\n"); |
| 206 | types[i + 2] = &ffi_type_pointer; |
| 207 | /* FIXME */ |
| 208 | values[i + 2].uint32 = *p++; |
| 209 | break; |
| 210 | case WL_ARGUMENT_OBJECT: |
| 211 | types[i + 2] = &ffi_type_pointer; |
| 212 | object = wl_hash_lookup(&client->display->objects, *p); |
| 213 | if (object == NULL) |
| 214 | printf("unknown object (%d)\n", *p); |
| 215 | if (object->interface != method->arguments[i].data) |
| 216 | printf("wrong object type\n"); |
| 217 | values[i + 2].object = object; |
| 218 | printf("got object (%d)\n", *p); |
| 219 | p++; |
| 220 | break; |
| 221 | case WL_ARGUMENT_NEW_ID: |
| 222 | types[i + 2] = &ffi_type_uint32; |
| 223 | values[i + 2].new_id = *p; |
| 224 | object = wl_hash_lookup(&client->display->objects, *p); |
| 225 | if (object != NULL) |
| 226 | printf("object already exists (%d)\n", *p); |
| 227 | printf("got new_id (%d)\n", *p); |
| 228 | p++; |
| 229 | break; |
| 230 | default: |
| 231 | printf("unknown type\n"); |
| 232 | break; |
| 233 | } |
| 234 | args[i + 2] = &values[i + 2]; |
| 235 | } |
| 236 | |
| 237 | ffi_prep_cif(&cif, FFI_DEFAULT_ABI, method->argument_count + 2, |
| 238 | &ffi_type_uint32, types); |
| 239 | ffi_call(&cif, FFI_FN(method->func), &result, args); |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | wl_client_data(int fd, uint32_t mask, void *data) |
| 244 | { |
| 245 | struct wl_client *client = data; |
| 246 | struct wl_object *object; |
| 247 | const struct wl_method *method; |
| 248 | char buffer[256]; |
| 249 | uint32_t *p; |
| 250 | int len; |
| 251 | |
| 252 | if (mask & WL_EVENT_READABLE) { |
| 253 | len = read(fd, buffer, sizeof buffer); |
| 254 | if (len == 0) { |
| 255 | wl_client_destroy(client); |
| 256 | } else { |
| 257 | printf("got %d bytes from client %p\n", |
| 258 | len, client); |
| 259 | if (len < 2 * sizeof *p) |
| 260 | /* read more... */ |
| 261 | return; |
| 262 | |
| 263 | p = (uint32_t *) buffer; |
| 264 | object = wl_hash_lookup(&client->display->objects, |
| 265 | p[0]); |
| 266 | if (object == NULL) { |
| 267 | /* send error */ |
| 268 | printf("invalid object\n"); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (p[1] >= object->interface->method_count) { |
| 273 | /* send error */ |
| 274 | printf("invalid method\n"); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | method = &object->interface->methods[p[1]]; |
| 279 | printf("calling method %s on interface %s\n", |
| 280 | method->name, object->interface->name); |
| 281 | demarshal(client, object, method, p + 2, len - 8); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (mask & WL_EVENT_WRITEABLE) { |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static void |
| 290 | wl_display_get_interface(struct wl_client *client, |
| 291 | struct wl_display *display, |
| 292 | const char *interface, uint32_t id) |
| 293 | { |
| 294 | /* client sends a new object id, and an interface identifier |
| 295 | * to name the object that implements the interface */ |
| 296 | printf("display::get_interface\n"); |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | wl_display_create_surface(struct wl_client *client, |
| 301 | struct wl_display *display, uint32_t id) |
| 302 | { |
| 303 | struct wl_surface *surface; |
| 304 | |
| 305 | printf("display::create_surface, client %p, display %p, new_id=%d\n", |
| 306 | client, display, id); |
| 307 | |
| 308 | surface = wl_surface_create(display, id); |
| 309 | wl_hash_insert(&display->objects, &surface->base); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static const struct wl_argument create_surface_arguments[] = { |
| 315 | { WL_ARGUMENT_NEW_ID } |
| 316 | }; |
| 317 | |
| 318 | static const struct wl_method display_methods[] = { |
| 319 | { "get_interface", wl_display_get_interface, |
| 320 | ARRAY_LENGTH(get_interface_arguments), get_interface_arguments }, |
| 321 | { "create_surface", wl_display_create_surface, |
| 322 | ARRAY_LENGTH(create_surface_arguments), create_surface_arguments }, |
| 323 | }; |
| 324 | |
| 325 | static const struct wl_interface display_interface = { |
| 326 | "display", 1, ARRAY_LENGTH(display_methods), display_methods, |
| 327 | }; |
| 328 | |
| 329 | struct wl_display * |
| 330 | wl_display_create(void) |
| 331 | { |
| 332 | struct wl_display *display; |
| 333 | |
| 334 | display = malloc(sizeof *display); |
| 335 | if (display == NULL) |
| 336 | return NULL; |
| 337 | |
| 338 | display->loop = wl_event_loop_create(); |
| 339 | if (display->loop == NULL) { |
| 340 | free(display); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | display->base.id = 0; |
| 345 | display->base.interface = &display_interface; |
| 346 | wl_hash_insert(&display->objects, &display->base); |
| 347 | |
| 348 | return display; |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | wl_display_run(struct wl_display *display) |
| 353 | { |
| 354 | while (1) |
| 355 | wl_event_loop_wait(display->loop); |
| 356 | } |
| 357 | |
| 358 | /* The plan here is to generate a random anonymous socket name and |
| 359 | * advertise that through a service on the session dbus. |
| 360 | */ |
| 361 | static const char socket_name[] = "\0wayland"; |
| 362 | |
| 363 | static void |
| 364 | socket_data(int fd, uint32_t mask, void *data) |
| 365 | { |
| 366 | struct wl_display *display = data; |
| 367 | struct sockaddr_un name; |
| 368 | socklen_t length; |
| 369 | int client_fd; |
| 370 | |
| 371 | length = sizeof name; |
| 372 | client_fd = accept (fd, (struct sockaddr *) &name, &length); |
| 373 | if (client_fd < 0) |
| 374 | fprintf(stderr, "failed to accept\n"); |
| 375 | |
| 376 | wl_client_create(display, client_fd); |
| 377 | } |
| 378 | |
| 379 | int |
| 380 | wl_display_add_socket(struct wl_display *display) |
| 381 | { |
| 382 | struct sockaddr_un name; |
| 383 | int sock; |
| 384 | socklen_t size; |
| 385 | |
| 386 | sock = socket(PF_LOCAL, SOCK_STREAM, 0); |
| 387 | if (sock < 0) |
| 388 | return -1; |
| 389 | |
| 390 | name.sun_family = AF_LOCAL; |
| 391 | memcpy(name.sun_path, socket_name, sizeof socket_name); |
| 392 | |
| 393 | size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name; |
| 394 | if (bind(sock, (struct sockaddr *) &name, size) < 0) |
| 395 | return -1; |
| 396 | |
| 397 | if (listen(sock, 1) < 0) |
| 398 | return -1; |
| 399 | |
| 400 | wl_event_loop_add_fd(display->loop, sock, WL_EVENT_READABLE, |
| 401 | socket_data, display); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | int main(int argc, char *argv[]) |
| 408 | { |
| 409 | struct wl_display *display; |
| 410 | |
| 411 | display = wl_display_create(); |
| 412 | |
| 413 | if (wl_display_add_socket(display)) { |
| 414 | fprintf(stderr, "failed to add socket: %m\n"); |
| 415 | exit(EXIT_FAILURE); |
| 416 | } |
| 417 | |
| 418 | printf("wayland online, display is %p\n", display); |
| 419 | |
| 420 | wl_display_run(display); |
| 421 | |
| 422 | return 0; |
| 423 | } |