Wayland++ 1.0.0
C++ Bindings for Wayland
Loading...
Searching...
No Matches
proxy_wrapper.cpp
1/*
2 * Copyright (c) 2017-2019, Philipp Kerling
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
34
35#include <iostream>
36#include <thread>
37#include <unistd.h>
38
39#include <wayland-client.hpp>
40
41using namespace wayland;
42
43class binder
44{
45private:
46 display_t display;
47
48 void bind(bool safe)
49 {
50 registry_t registry;
51 seat_t seat;
52
53 auto queue = display.create_queue();
54 if(safe)
55 {
56 auto display_wrapper = display.proxy_create_wrapper();
57 display_wrapper.set_queue(queue);
58 registry = display_wrapper.get_registry();
59 }
60 else
61 {
62 registry = display.get_registry();
63 // This is the race: registry will be set to the default queue for a very
64 // short while between get_registry() and set_queue() - events dispatched
65 // in between get stuck in the default queue and ultimately lost.
66 registry.set_queue(queue);
67 }
68
69 registry.on_global() = [&seat, &registry](std::uint32_t name, const std::string& interface, std::uint32_t version)
70 {
71 if(interface == seat_t::interface_name)
72 registry.bind(name, seat, version);
73 };
74 display.roundtrip_queue(queue);
75 if(!seat)
76 throw std::runtime_error("Did NOT get seat interface - thread-safety issue!");
77
78 // Now pretend that again another part of the application wants to use the
79 // seat and get the keyboard from it
80 // Note that it would not be necessary to do this in this example, but
81 // this code is useful for testing proxy wrappers with normal interface
82 // objects (display_t is special)
83 auto queue2 = display.create_queue();
84 if(safe)
85 {
86 seat = seat.proxy_create_wrapper();
87 }
88 seat.set_queue(queue2);
89 keyboard_t kbd = seat.get_keyboard();
90 bool have_keymap = false;
91 kbd.on_keymap() = [&have_keymap](keyboard_keymap_format /*unused*/, int fd, std::uint32_t /*unused*/)
92 {
93 close(fd);
94 have_keymap = true;
95 };
96 display.roundtrip_queue(queue2);
97 if(!have_keymap)
98 {
99 throw std::runtime_error("Did NOT get keymap - thread-safety issue!");
100 }
101 }
102
103 std::thread bind_thread(bool safe)
104 {
105 return std::thread{std::bind(&binder::bind, this, safe)};
106 }
107
108public:
109 binder() = default;
110 binder(const binder&) = delete;
111 binder(binder&&) noexcept = delete;
112 ~binder() noexcept = default;
113 binder& operator=(const binder&) = delete;
114 binder& operator=(binder&&) noexcept = delete;
115
116 void run(int thread_count, int round_count, bool safe)
117 {
118 std::atomic<bool> stop{false};
119 std::cout << "Using " << thread_count << " threads, safe: " << safe << std::endl;
120 for(int round = 0; round < round_count; round++)
121 {
122 if(round % 100 == 0)
123 {
124 std::cout << "Round " << round << "/" << round_count << std::endl;
125 }
126 std::vector<std::thread> threads;
127 threads.reserve(thread_count);
128 for(int i = 0; i < thread_count; i++)
129 {
130 threads.emplace_back(bind_thread(safe));
131 }
132 for(auto& thread : threads)
133 {
134 thread.join();
135 }
136 }
137 stop = true;
138 }
139};
140
141int main(int argc, char** argv)
142{
143 if(argc != 4)
144 {
145 std::cerr << "Usage: " << argv[0] << " <thread count> <run count> <use safe mechanism?>" << std::endl;
146 return -1;
147 }
148 binder b;
149 b.run(std::stoi(argv[1]), std::stoi(argv[2]), std::stoi(argv[3]));
150 return 0;
151}
std::function< void(keyboard_keymap_format, int, uint32_t)> & on_keymap()
keyboard mapping
void set_queue(event_queue_t queue)
Assign a proxy to an event queue.
std::function< void(uint32_t, std::string, uint32_t)> & on_global()
announce global object
proxy_t bind(uint32_t name, proxy_t &interface, uint32_t version)
bind an object to the display
keyboard_t get_keyboard()
return keyboard object