summaryrefslogtreecommitdiff
path: root/src/x/connection.h
blob: 83c6b18aff82f7fb564c21dad46ef2c24caafdf5 (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
// Copyright 2021, 2022 Benjamin Barenblat
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// The main connection to the X server.

#ifndef GLPLANET_SRC_X_CONNECTION_H_
#define GLPLANET_SRC_X_CONNECTION_H_

#include <X11/Xlib.h>
#include <stdint.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>

#include <deque>
#include <thread>
#include <vector>

#include "src/undo_xlib_dot_h_namespace_pollution.h"
//

#include "src/x/event.h"
#include "src/x/rpc.h"
#include "src/x/screen.h"
#include "src/x/types.h"
#include "third_party/abseil/absl/base/thread_annotations.h"
#include "third_party/abseil/absl/strings/string_view.h"
#include "third_party/abseil/absl/synchronization/mutex.h"
#include "third_party/abseil/absl/time/time.h"
#include "third_party/abseil/absl/types/optional.h"

namespace x {

enum class WindowClass : uint16_t {
  kCopyFromParent = XCB_WINDOW_CLASS_COPY_FROM_PARENT,
  kInputOutput = XCB_WINDOW_CLASS_INPUT_OUTPUT,
  kInputOnly = XCB_WINDOW_CLASS_INPUT_ONLY,
};

enum EventMask : uint32_t {
  kNoEvent = XCB_EVENT_MASK_NO_EVENT,
  kKeyPress = XCB_EVENT_MASK_KEY_PRESS,
  kKeyRelease = XCB_EVENT_MASK_KEY_RELEASE,
  kButtonPress = XCB_EVENT_MASK_BUTTON_PRESS,
  kButtonRelease = XCB_EVENT_MASK_BUTTON_RELEASE,
  kEnterWindow = XCB_EVENT_MASK_ENTER_WINDOW,
  kLeaveWindow = XCB_EVENT_MASK_LEAVE_WINDOW,
  kPointerMotion = XCB_EVENT_MASK_POINTER_MOTION,
  kPointerMotionHint = XCB_EVENT_MASK_POINTER_MOTION_HINT,
  kButton1Motion = XCB_EVENT_MASK_BUTTON_1_MOTION,
  kButton2Motion = XCB_EVENT_MASK_BUTTON_2_MOTION,
  kButton3Motion = XCB_EVENT_MASK_BUTTON_3_MOTION,
  kButton4Motion = XCB_EVENT_MASK_BUTTON_4_MOTION,
  kButton5Motion = XCB_EVENT_MASK_BUTTON_5_MOTION,
  kButtonMotion = XCB_EVENT_MASK_BUTTON_MOTION,
  kKeymapState = XCB_EVENT_MASK_KEYMAP_STATE,
  kExposure = XCB_EVENT_MASK_EXPOSURE,
  kVisibilityChange = XCB_EVENT_MASK_VISIBILITY_CHANGE,
  kStructureNotify = XCB_EVENT_MASK_STRUCTURE_NOTIFY,
  kResizeRedirect = XCB_EVENT_MASK_RESIZE_REDIRECT,
  kSubstructureNotify = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
  kSubstructureRedirect = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
  kFocusChange = XCB_EVENT_MASK_FOCUS_CHANGE,
  kPropertyChange = XCB_EVENT_MASK_PROPERTY_CHANGE,
  kColorMapChange = XCB_EVENT_MASK_COLOR_MAP_CHANGE,
  kOwnerGrabButton = XCB_EVENT_MASK_OWNER_GRAB_BUTTON,
};

// A connection to the X server.
//
// This class is thread-safe.
class Connection final {
 public:
  struct CreateWindowOptions {
    uint8_t depth;
    Id window;
    Id parent;
    int16_t x;
    int16_t y;
    uint16_t width;
    uint16_t height;
    uint16_t border_width;
    WindowClass window_class;
    Id visual_id;

    // TODO(bbarenblat@gmail.com): Support additional attributes.
    absl::optional<Id> background_pixel = absl::nullopt;
    absl::optional<uint32_t> event_mask = absl::nullopt;
  };

  explicit Connection(const char* display_name);

  Connection(Connection&&) noexcept = default;
  Connection& operator=(Connection&&) noexcept = default;

  ~Connection() noexcept { XCloseDisplay(xlib_); }

  Id GenerateId() noexcept { return xcb_generate_id(xcb_); }

  Screen DefaultScreen() const noexcept {
    return Screen(xcb_aux_get_screen(xcb_, xlib::DefaultScreen(xlib_)));
  }

  VoidCompletion CreateWindow(const CreateWindowOptions& options) noexcept;

  VoidCompletion DestroyWindow(Id window) noexcept {
    return VoidCompletion(xcb_, xcb_destroy_window_checked(xcb_, window));
  }

  VoidCompletion MapWindow(Id window) noexcept {
    return VoidCompletion(xcb_, xcb_map_window_checked(xcb_, window));
  }

  // Per the X specification, name should be encoded as ISO 8859-1.
  InternAtomCompletion InternOrGetAtomByName(absl::string_view name) noexcept {
    return InternAtom(name, /*only_if_exists=*/false);
  }

  // Per the X specification, name should be encoded as ISO 8859-1.
  InternAtomCompletion GetAtomByName(absl::string_view name) noexcept {
    return InternAtom(name, /*only_if_exists=*/true);
  }

  VoidCompletion SendEvent(const Event&, bool propagate, Id destination_window,
                           const std::vector<EventMask>&);

  // Escape hatches: raw handles to the X server as Xlib and XCB objects. Use
  // these to interface with other libraries.
  //
  // Be careful with the Xlib handle--XCB, not Xlib, owns the event loop.
  Display* AsXlibDisplay() noexcept { return xlib_; }
  xcb_connection_t* AsXcbConnection() noexcept { return xcb_; }

 private:
  InternAtomCompletion InternAtom(absl::string_view name,
                                  bool only_if_exists) noexcept {
    return InternAtomCompletion(
        xcb_, xcb_intern_atom(xcb_, only_if_exists, name.size(), name.data()));
  }

  Display* xlib_;
  xcb_connection_t* xcb_;
};

// A class that monitors for X events. You probably only want one of these for
// each connection; otherwise, events will be delivered nondeterministically to
// the monitors.
//
// This class is thread-safe.
class EventMonitor final {
 public:
  // Starts monitoring for X events on the specified connection.
  explicit EventMonitor(Connection&);

  EventMonitor(EventMonitor&&) noexcept = default;
  EventMonitor& operator=(EventMonitor&&) noexcept = default;

  ~EventMonitor();

  absl::optional<Event> GetEventIfReady() noexcept ABSL_LOCKS_EXCLUDED(mu_) {
    return WaitForEventWithTimeout(absl::ZeroDuration());
  }

  Event WaitForEvent() noexcept ABSL_LOCKS_EXCLUDED(mu_) {
    return *WaitForEventWithTimeout(absl::InfiniteDuration());
  }

  absl::optional<Event> WaitForEventWithTimeout(absl::Duration) noexcept
      ABSL_LOCKS_EXCLUDED(mu_);

 private:
  static constexpr absl::string_view kDoneAtomName =
      "_GLPLANET_SRC_X_CONNECTION_EVENTMONITOR_DONE";

  void WatcherThreadMain() noexcept ABSL_LOCKS_EXCLUDED(mu_);

  bool EventsPresent() const noexcept {
    mu_.AssertReaderHeld();
    return !pending_events_.empty();
  };

  Connection& x_;
  Id communication_window_;
  Id close_connection_atom_;

  absl::Mutex mu_;
  std::deque<Event> pending_events_ ABSL_GUARDED_BY(mu_);

  absl::optional<std::thread> watcher_thread_;
};

}  // namespace x

#endif  // GLPLANET_SRC_X_CONNECTION_H_