summaryrefslogtreecommitdiff
path: root/src/x.hh
blob: 9da925bc58c1b0854bd4fdfa8d4a7afe2af7cb1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#ifndef X_HH
#define X_HH

#include <memory>
#include <string>
#include <xcb/render.h>
#include <xcb/xcb.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xproto.h>
#include <xcb/render.h>

namespace x {

namespace priv {
struct XcbConnectionDelete {
  void operator()(xcb_connection_t* ptr) const {
    xcb_disconnect(ptr);
  }
};

struct XcbWindowDelete {
  void operator()(xcb_connection_t* conn, xcb_window_t id) const {
    xcb_destroy_window(conn, id);
  }
};

struct XcbPixmapDelete {
  void operator()(xcb_connection_t* conn, xcb_window_t id) const {
    xcb_free_pixmap(conn, id);
  }
};

struct XcbGContextDelete {
  void operator()(xcb_connection_t* conn, xcb_gcontext_t id) const {
    xcb_free_gc(conn, id);
  }
};

struct XcbRenderPictureDelete {
  void operator()(xcb_connection_t* conn, xcb_render_picture_t pic) const {
    xcb_render_free_picture(conn, pic);
  }
};

struct XcbColormapDelete {
  void operator()(xcb_connection_t* conn, xcb_colormap_t id) const {
    xcb_free_colormap(conn, id);
  }
};

}  // priv

class shared_connection {
public:
  shared_connection() {
  }
  shared_connection(std::nullptr_t) {
  }
  explicit shared_connection(xcb_connection_t* conn)
    : shared_(conn, priv::XcbConnectionDelete()) {
  }
  shared_connection(shared_connection const& shared)
    : shared_(shared.shared_) {
  }
  shared_connection(shared_connection&& shared)
    : shared_(std::move(shared.shared_)) {
  }

  shared_connection& operator=(shared_connection const& shared) {
    shared_ = shared.shared_;
    return *this;
  }

  shared_connection& operator=(shared_connection&& shared) {
    shared_ = std::move(shared.shared_);
    return *this;
  }

  void swap(shared_connection& shared) {
    shared_.swap(shared.shared_);
  }

  void reset() {
    shared_.reset();
  }

  void reset(xcb_connection_t* ptr) {
    shared_.reset(ptr, priv::XcbConnectionDelete());
  }

  xcb_connection_t* get() const {
    return shared_.get();
  }

  explicit operator bool() const {
    return shared_.get() != nullptr;
  }

private:
  std::shared_ptr<xcb_connection_t> shared_;
};

template<typename T, typename D>
class unique_resource {
public:
  unique_resource()
    : id_(XCB_NONE) {
  }
  explicit unique_resource(shared_connection const& conn)
    : conn_(conn), id_(conn ? xcb_generate_id(conn.get()) : XCB_NONE) {
  }
  unique_resource(shared_connection const& conn, T id)
    : conn_(conn), id_(id) {
    if (id_ == XCB_NONE) conn_.reset();
  }
  unique_resource(unique_resource&& res)
    : conn_(res.conn_), id_(res.release()) {
  }

  ~unique_resource() {
    destroy();
  }

  unique_resource& operator=(shared_connection const& conn) {
    reset(conn);
    return *this;
  }

  unique_resource& operator=(unique_resource&& res) {
    swap(res);
    return *this;
  }

  xcb_connection_t* conn() const {
    return conn_.get();
  }

  shared_connection const& shared_conn() const {
    return conn_;
  }

  T get() const {
    return id_;
  }

  explicit operator bool() const {
    return id_ != XCB_NONE;
  }

  void reset() {
    destroy();
    conn_.reset();
    id_ = XCB_NONE;
  }

  void reset(shared_connection const& conn) {
    if (conn) {
      reset(conn, xcb_generate_id(conn.get()));
    } else {
      reset();
    }
  }

  void reset(shared_connection const& conn, T id) {
    destroy();
    conn_ = conn;
    id_ = id;
    if (id_ == XCB_NONE) conn_.reset();
  }

  void swap(unique_resource& res) {
    conn_.swap(res.conn_);
    auto tmp = id_;
    id_ = res.id_;
    res.id_ = tmp;
  }

private:
  unique_resource(unique_resource const&) = delete;
  unique_resource& operator=(unique_resource const&) = delete;

  void destroy() {
    if (id_ == XCB_NONE) return;
    D()(conn_.get(), id_);
  }

  T release() {
    T ret = id_;
    id_ = XCB_NONE;
    conn_.reset();
    return ret;
  }

  shared_connection conn_;
  T id_;
};

typedef unique_resource<xcb_window_t, priv::XcbWindowDelete> unique_window;
typedef unique_resource<xcb_pixmap_t, priv::XcbPixmapDelete> unique_pixmap;
typedef unique_resource<xcb_gcontext_t,
                        priv::XcbGContextDelete> unique_gcontext;
typedef unique_resource<xcb_render_picture_t,
                        priv::XcbRenderPictureDelete> unique_picture;
typedef unique_resource<xcb_colormap_t,
                        priv::XcbColormapDelete> unique_colormap;

struct Format {
  static uint8_t const FLAG_NEED_BORDER;
  static uint8_t const FLAG_NEED_COLORMAP;

  uint8_t depth;
  xcb_visualtype_t* visual;
  xcb_render_pictforminfo_t* render;
  xcb_render_pictforminfo_t* render_alpha;
  uint8_t flags;

  bool good() const {
    return depth > 0 && visual;
  }

  bool need_border() const {
    return flags & FLAG_NEED_BORDER;
  }

  bool need_colormap() const {
    return flags & FLAG_NEED_COLORMAP;
  }

  Format(uint8_t depth, xcb_visualtype_t* visual,
         xcb_render_pictforminfo_t* render,
         xcb_render_pictforminfo_t* render_alpha,
         uint8_t flags)
    : depth(depth), visual(visual), render(render), render_alpha(render_alpha),
      flags(flags) {
  }
  Format()
    : depth(0), visual(nullptr), render(nullptr), render_alpha(nullptr),
      flags(0) {
  }
};

xcb_screen_t const* get_screen(xcb_connection_t* conn, int screen);
void prefetch_extensions(xcb_connection_t* conn);
Format get_best_format(xcb_connection_t* conn, xcb_screen_t const* screen,
                       bool allow_render = true);

class Atoms {
public:
  virtual ~Atoms() {}

  static std::unique_ptr<Atoms> create(shared_connection const& conn);

  virtual void preload(std::string const& name, bool create = true) = 0;
  virtual xcb_atom_t get(std::string const& name, bool create = true) = 0;

protected:
  Atoms() {}
  Atoms(Atoms const&) = delete;
  Atoms& operator=(Atoms const&) = delete;
};

class Ewmh {
public:
  virtual ~Ewmh() {}

  static std::unique_ptr<Ewmh> create(
      shared_connection const& conn, int screen);

  virtual xcb_ewmh_connection_t* conn() = 0;
  virtual bool supported(xcb_atom_t atom) = 0;

protected:
  Ewmh() {}
  Ewmh(Ewmh const&) = delete;
  Ewmh& operator=(Ewmh const&) = delete;
};

}  // namespace x

#endif  // X_HH