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