Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 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 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <glib.h> |
| 29 | |
| 30 | #include "wayland-client.h" |
| 31 | #include "wayland-glib.h" |
| 32 | |
| 33 | /* The screenshooter is a good example of a custom object exposed by |
| 34 | * the compositor and serves as a test bed for implementing client |
| 35 | * side marshalling outside libwayland.so */ |
| 36 | |
| 37 | static const char socket_name[] = "\0wayland"; |
| 38 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 39 | #define SCREENSHOOTER_SHOOT 0 |
| 40 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 41 | struct screenshooter; |
| 42 | |
| 43 | static const struct wl_message screenshooter_requests[] = { |
| 44 | { "shoot", "" }, |
| 45 | }; |
| 46 | |
| 47 | static const struct wl_interface screenshooter_interface = { |
| 48 | "screenshooter", 1, |
| 49 | ARRAY_LENGTH(screenshooter_requests), screenshooter_requests, |
| 50 | 0, NULL |
| 51 | }; |
| 52 | |
| 53 | static inline void |
| 54 | screenshooter_shoot(struct screenshooter *shooter) |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 55 | { |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 56 | wl_proxy_marshal((struct wl_proxy *) shooter, SCREENSHOOTER_SHOOT); |
| 57 | } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 58 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 59 | static void |
| 60 | handle_global(struct wl_display *display, uint32_t id, |
| 61 | const char *interface, uint32_t version, void *data) |
| 62 | { |
| 63 | struct screenshooter **screenshooter = data; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 64 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 65 | if (strcmp(interface, "screenshooter") == 0) |
| 66 | *screenshooter = (struct screenshooter *) |
| 67 | wl_proxy_create_for_id(display, |
| 68 | &screenshooter_interface, id); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int main(int argc, char *argv[]) |
| 72 | { |
| 73 | struct wl_display *display; |
| 74 | GMainLoop *loop; |
| 75 | GSource *source; |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 76 | struct screenshooter *screenshooter; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 77 | |
Kristian Høgsberg | dc0f355 | 2008-12-07 15:22:22 -0500 | [diff] [blame] | 78 | display = wl_display_create(socket_name, sizeof socket_name); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 79 | if (display == NULL) { |
| 80 | fprintf(stderr, "failed to create display: %m\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 84 | screenshooter = NULL; |
| 85 | wl_display_add_global_listener(display, handle_global, &screenshooter); |
Kristian Høgsberg | 03fd86b | 2009-02-10 14:15:44 -0500 | [diff] [blame] | 86 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 87 | if (screenshooter == NULL) { |
| 88 | fprintf(stderr, "display doesn't support screenshooter\n"); |
| 89 | return -1; |
| 90 | } |
| 91 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 92 | loop = g_main_loop_new(NULL, FALSE); |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 93 | source = wl_glib_source_new(display); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 94 | g_source_attach(source, NULL); |
| 95 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame^] | 96 | screenshooter_shoot(screenshooter); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 97 | |
Kristian Høgsberg | f53f4bb | 2008-11-24 11:32:43 -0500 | [diff] [blame] | 98 | g_idle_add((GSourceFunc) g_main_loop_quit, loop); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 99 | g_main_loop_run(loop); |
| 100 | |
| 101 | return 0; |
| 102 | } |