blob: 9f1e68521b3ceb61e5130da96d63e1abd9c6f47f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
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øgsberg1e4b86a2008-11-23 23:41:08 -050023#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
37static const char socket_name[] = "\0wayland";
38
39struct screenshooter {
40 uint32_t id;
41 struct wl_display *display;
42};
43
44static struct screenshooter *
45screenshooter_create(struct wl_display *display)
46{
47 struct screenshooter *screenshooter;
48 uint32_t id;
49
50 id = wl_display_get_object_id(display, "screenshooter");
51 if (id == 0) {
52 fprintf(stderr, "server doesn't support screenshooter interface\n");
53 return NULL;
54 }
55
56 screenshooter = malloc(sizeof screenshooter);
57 if (screenshooter == NULL)
58 return NULL;
59
60 screenshooter->id = id;
61 screenshooter->display = display;
62
63 return screenshooter;
64}
65
66#define SCREENSHOOTER_SHOOT 0
67
68static void
69screenshooter_shoot(struct screenshooter *screenshooter)
70{
71 uint32_t request[2];
72
73 request[0] = screenshooter->id;
74 request[1] = SCREENSHOOTER_SHOOT | ((sizeof request) << 16);
75
76 wl_display_write(screenshooter->display,
77 request, sizeof request);
78}
79
80int main(int argc, char *argv[])
81{
82 struct wl_display *display;
83 GMainLoop *loop;
84 GSource *source;
85 struct screenshooter *s;
86
87 display = wl_display_create(socket_name);
88 if (display == NULL) {
89 fprintf(stderr, "failed to create display: %m\n");
90 return -1;
91 }
92
93 loop = g_main_loop_new(NULL, FALSE);
94 source = wayland_source_new(display);
95 g_source_attach(source, NULL);
96
97 s = screenshooter_create(display);
98 if (s == NULL)
99 exit(-1);
100
101 screenshooter_shoot(s);
Kristian Høgsbergf53f4bb2008-11-24 11:32:43 -0500102 g_idle_add((GSourceFunc) g_main_loop_quit, loop);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500103 g_main_loop_run(loop);
104
105 return 0;
106}