xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/linux/x11/x_window_property.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_LINUX_X11_X_WINDOW_PROPERTY_H_
12 #define MODULES_DESKTOP_CAPTURE_LINUX_X11_X_WINDOW_PROPERTY_H_
13 
14 #include <X11/X.h>
15 #include <X11/Xlib.h>
16 
17 namespace webrtc {
18 
19 class XWindowPropertyBase {
20  public:
21   XWindowPropertyBase(Display* display,
22                       Window window,
23                       Atom property,
24                       int expected_size);
25   virtual ~XWindowPropertyBase();
26 
27   XWindowPropertyBase(const XWindowPropertyBase&) = delete;
28   XWindowPropertyBase& operator=(const XWindowPropertyBase&) = delete;
29 
30   // True if we got properly value successfully.
is_valid()31   bool is_valid() const { return is_valid_; }
32 
33   // Size and value of the property.
size()34   size_t size() const { return size_; }
35 
36  protected:
37   unsigned char* data_ = nullptr;
38 
39  private:
40   bool is_valid_ = false;
41   unsigned long size_ = 0;  // NOLINT: type required by XGetWindowProperty
42 };
43 
44 // Convenience wrapper for XGetWindowProperty() results.
45 template <class PropertyType>
46 class XWindowProperty : public XWindowPropertyBase {
47  public:
XWindowProperty(Display * display,const Window window,const Atom property)48   XWindowProperty(Display* display, const Window window, const Atom property)
49       : XWindowPropertyBase(display, window, property, sizeof(PropertyType)) {}
50   ~XWindowProperty() override = default;
51 
52   XWindowProperty(const XWindowProperty&) = delete;
53   XWindowProperty& operator=(const XWindowProperty&) = delete;
54 
data()55   const PropertyType* data() const {
56     return reinterpret_cast<PropertyType*>(data_);
57   }
data()58   PropertyType* data() { return reinterpret_cast<PropertyType*>(data_); }
59 };
60 
61 }  // namespace webrtc
62 
63 #endif  // MODULES_DESKTOP_CAPTURE_LINUX_X11_X_WINDOW_PROPERTY_H_
64