Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame^] | 1 | #include <stdint.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <glib.h> |
| 7 | |
| 8 | #include "wayland-client.h" |
| 9 | #include "wayland-glib.h" |
| 10 | |
| 11 | /* The screenshooter is a good example of a custom object exposed by |
| 12 | * the compositor and serves as a test bed for implementing client |
| 13 | * side marshalling outside libwayland.so */ |
| 14 | |
| 15 | static const char socket_name[] = "\0wayland"; |
| 16 | |
| 17 | struct screenshooter { |
| 18 | uint32_t id; |
| 19 | struct wl_display *display; |
| 20 | }; |
| 21 | |
| 22 | static struct screenshooter * |
| 23 | screenshooter_create(struct wl_display *display) |
| 24 | { |
| 25 | struct screenshooter *screenshooter; |
| 26 | uint32_t id; |
| 27 | |
| 28 | id = wl_display_get_object_id(display, "screenshooter"); |
| 29 | if (id == 0) { |
| 30 | fprintf(stderr, "server doesn't support screenshooter interface\n"); |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | screenshooter = malloc(sizeof screenshooter); |
| 35 | if (screenshooter == NULL) |
| 36 | return NULL; |
| 37 | |
| 38 | screenshooter->id = id; |
| 39 | screenshooter->display = display; |
| 40 | |
| 41 | return screenshooter; |
| 42 | } |
| 43 | |
| 44 | #define SCREENSHOOTER_SHOOT 0 |
| 45 | |
| 46 | static void |
| 47 | screenshooter_shoot(struct screenshooter *screenshooter) |
| 48 | { |
| 49 | uint32_t request[2]; |
| 50 | |
| 51 | request[0] = screenshooter->id; |
| 52 | request[1] = SCREENSHOOTER_SHOOT | ((sizeof request) << 16); |
| 53 | |
| 54 | wl_display_write(screenshooter->display, |
| 55 | request, sizeof request); |
| 56 | } |
| 57 | |
| 58 | int main(int argc, char *argv[]) |
| 59 | { |
| 60 | struct wl_display *display; |
| 61 | GMainLoop *loop; |
| 62 | GSource *source; |
| 63 | struct screenshooter *s; |
| 64 | |
| 65 | display = wl_display_create(socket_name); |
| 66 | if (display == NULL) { |
| 67 | fprintf(stderr, "failed to create display: %m\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | loop = g_main_loop_new(NULL, FALSE); |
| 72 | source = wayland_source_new(display); |
| 73 | g_source_attach(source, NULL); |
| 74 | |
| 75 | s = screenshooter_create(display); |
| 76 | if (s == NULL) |
| 77 | exit(-1); |
| 78 | |
| 79 | screenshooter_shoot(s); |
| 80 | |
| 81 | g_main_loop_run(loop); |
| 82 | |
| 83 | return 0; |
| 84 | } |