Wayland++ 1.0.0
C++ Bindings for Wayland
pingpong.cpp
1/*
2 * Copyright (c) 2022, Nils Christopher Brause
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <iostream>
27#include <thread>
28#include <cstdint>
29
30#include <wayland-client.hpp>
31#include <wayland-server.hpp>
32#include <pingpong-server-protocol.hpp>
33#include <pingpong-client-protocol.hpp>
34
35int main()
36{
37 wayland::server::display_t server_display;
38 wayland::server::global_pingpong_t global_pingpong(server_display);
39 wayland::server::global_dummy_t dummy(server_display);
40 wayland::server::pingpong_t server_pingpong;
41
42 // create wayland UNIX socket.
43 server_display.add_socket("pingpong");
44
45 // Answer ping requests.
46 global_pingpong.on_bind() = [&] (const wayland::server::client_t& /*client*/, wayland::server::pingpong_t pingpong)
47 {
48 // Don't copy the "pingpog" resource into the event handler as this creates cyclic references.
49 server_pingpong = pingpong;
50 pingpong.on_ping() = [&] (const std::string& msg)
51 {
52 std::cout << "Server received: " << msg << std::endl;
53 server_pingpong.pong(msg);
54 };
55 };
56
57 // Don't show anyone the dummy global.
58 server_display.set_global_filter([] (const wayland::server::client_t& /*client*/, wayland::server::global_base_t global) { return !global.has_interface<wayland::server::dummy_t>(); });
59
60 // Run server event loop in a thread.
61 bool running = true;
62 auto thread = std::thread([&] ()
63 {
64 auto el = server_display.get_event_loop();
65 while(running)
66 {
67 el.dispatch(1);
68 server_display.flush_clients();
69 }
70 });
71
72
73 // Connect to server.
74 wayland::display_t display("pingpong");
75
76 // Bind to pingpong global.
77 wayland::pingpong_t pingpong;
78 auto registry = display.get_registry();
79 registry.on_global() = [&] (uint32_t name, const std::string& interface, uint32_t version)
80 {
81 std::cout << "Found global: " << interface << std::endl;
82 if(interface == wayland::pingpong_t::interface_name)
83 registry.bind(name, pingpong, version);
84 };
85 display.roundtrip();
86
87 // Print pong answers.
88 pingpong.on_pong() = [&] (const std::string& msg)
89 {
90 std::cout << "Client received: " << msg << std::endl;
91 running = false;
92 };
93
94 // Send ping request.
95 pingpong.ping("Hello World!");
96 display.roundtrip();
97
98 thread.join();
99 return 0;
100}
Represents a connection to the compositor and acts as a proxy to the display singleton object.