blob: 4891fd799b654b2574ba7457f04cd45e4778904c [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
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050039#define SCREENSHOOTER_SHOOT 0
40
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040041struct screenshooter;
42
43static const struct wl_message screenshooter_requests[] = {
44 { "shoot", "" },
45};
46
47static const struct wl_interface screenshooter_interface = {
48 "screenshooter", 1,
49 ARRAY_LENGTH(screenshooter_requests), screenshooter_requests,
50 0, NULL
51};
52
53static inline void
54screenshooter_shoot(struct screenshooter *shooter)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050055{
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040056 wl_proxy_marshal((struct wl_proxy *) shooter, SCREENSHOOTER_SHOOT);
57}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050058
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040059static void
60handle_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øgsberg1e4b86a2008-11-23 23:41:08 -050064
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040065 if (strcmp(interface, "screenshooter") == 0)
66 *screenshooter = (struct screenshooter *)
67 wl_proxy_create_for_id(display,
68 &screenshooter_interface, id);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050069}
70
71int main(int argc, char *argv[])
72{
73 struct wl_display *display;
74 GMainLoop *loop;
75 GSource *source;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040076 struct screenshooter *screenshooter;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050077
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -050078 display = wl_display_create(socket_name, sizeof socket_name);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050079 if (display == NULL) {
80 fprintf(stderr, "failed to create display: %m\n");
81 return -1;
82 }
83
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040084 screenshooter = NULL;
85 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -050086 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040087 if (screenshooter == NULL) {
88 fprintf(stderr, "display doesn't support screenshooter\n");
89 return -1;
90 }
91
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050092 loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050093 source = wl_glib_source_new(display);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050094 g_source_attach(source, NULL);
95
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040096 screenshooter_shoot(screenshooter);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050097
Kristian Høgsbergf53f4bb2008-11-24 11:32:43 -050098 g_idle_add((GSourceFunc) g_main_loop_quit, loop);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050099 g_main_loop_run(loop);
100
101 return 0;
102}