Wayland++ 1.0.0
C++ Bindings for Wayland
Loading...
Searching...
No Matches
foreign_display.cpp
1/*
2 * Copyright (c) 2018-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
30#include <iostream>
31#include <memory>
32#include <cstdint>
33
34#include <wayland-client.hpp>
35
36using namespace wayland;
37
38class foreign_display
39{
40private:
41 // global objects
42 wl_display* c_display = nullptr;
43 std::unique_ptr<display_t> display;
44 registry_t registry;
45
46public:
47 foreign_display() = default;
48 foreign_display(const foreign_display&) = delete;
49 foreign_display(foreign_display&&) noexcept = delete;
50 foreign_display& operator=(const foreign_display&) = delete;
51 foreign_display& operator=(foreign_display&&) noexcept = delete;
52
53
54 ~foreign_display() noexcept
55 {
56 // wl_display_disconnect destroys all remaining proxy instances implicitly,
57 // so we have to make sure this is already gone in order to not run into a
58 // double-free
59 registry.proxy_release();
60 // This is not called by the display_t destructor for foreign instances!
61 wl_display_disconnect(c_display);
62 }
63
64 void run()
65 {
66 c_display = wl_display_connect(nullptr);
67 if(!c_display)
68 {
69 std::cerr << "Cannot connect to Wayland display";
70 return;
71 }
72
73 display.reset(new display_t(c_display));
74 registry = display->get_registry();
75 registry.on_global() = [&] (uint32_t name, const std::string& interface, uint32_t version)
76 {
77 std::cout << "* Global interface " << interface << " (name " << name << " version " << version << ")" << std::endl;
78 };
79 display->roundtrip();
80 }
81};
82
83int main()
84{
85 foreign_display d;
86 d.run();
87 return 0;
88}
Represents a connection to the compositor and acts as a proxy to the display singleton object.
void proxy_release()
Release the wrapped object (if any), making this an empty wrapper.
std::function< void(uint32_t, std::string, uint32_t)> & on_global()
announce global object