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 
29 #include <wayland-client.hpp>
30 #include <wayland-server.hpp>
31 #include <pingpong-server-protocol.hpp>
32 #include <pingpong-client-protocol.hpp>
33 
34 int main()
35 {
36  wayland::server::display_t server_display;
37  wayland::server::global_pingpong_t global_pingpong(server_display);
38  wayland::server::global_dummy_t dummy(server_display);
39  wayland::server::pingpong_t server_pingpong;
40 
41  // create wayland UNIX socket.
42  server_display.add_socket("pingpong");
43 
44  // Answer ping requests.
45  global_pingpong.on_bind() = [&] (const wayland::server::client_t& /*client*/, wayland::server::pingpong_t pingpong)
46  {
47  // Don't copy the "pingpog" resource into the event handler as this creates cyclic references.
48  server_pingpong = pingpong;
49  pingpong.on_ping() = [&] (const std::string& msg)
50  {
51  std::cout << "Server received: " << msg << std::endl;
52  server_pingpong.pong(msg);
53  };
54  };
55 
56  // Don't show anyone the dummy global.
57  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>(); });
58 
59  // Run server event loop in a thread.
60  bool running = true;
61  auto thread = std::thread([&] ()
62  {
63  auto el = server_display.get_event_loop();
64  while(running)
65  {
66  el.dispatch(1);
67  server_display.flush_clients();
68  }
69  });
70 
71 
72  // Connect to server.
73  wayland::display_t display("pingpong");
74 
75  // Bind to pingpong global.
76  wayland::pingpong_t pingpong;
77  auto registry = display.get_registry();
78  registry.on_global() = [&] (uint32_t name, const std::string& interface, uint32_t version)
79  {
80  std::cout << "Found global: " << interface << std::endl;
81  if(interface == wayland::pingpong_t::interface_name)
82  registry.bind(name, pingpong, version);
83  };
84  display.roundtrip();
85 
86  // Print pong answers.
87  pingpong.on_pong() = [&] (const std::string& msg)
88  {
89  std::cout << "Client received: " << msg << std::endl;
90  running = false;
91  };
92 
93  // Send ping request.
94  pingpong.ping("Hello World!");
95  display.roundtrip();
96 
97  thread.join();
98  return 0;
99 }
Represents a connection to the compositor and acts as a proxy to the display singleton object...