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_X_WINDOW_PROPERTY_H_
12 #define MODULES_DESKTOP_CAPTURE_LINUX_X_WINDOW_PROPERTY_H_
13 
14 #include <X11/X.h>
15 #include <X11/Xlib.h>
16 
17 #include "rtc_base/constructor_magic.h"
18 
19 namespace webrtc {
20 
21 class XWindowPropertyBase {
22  public:
23   XWindowPropertyBase(Display* display,
24                       Window window,
25                       Atom property,
26                       int expected_size);
27   virtual ~XWindowPropertyBase();
28 
29   // True if we got properly value successfully.
is_valid()30   bool is_valid() const { return is_valid_; }
31 
32   // Size and value of the property.
size()33   size_t size() const { return size_; }
34 
35  protected:
36   unsigned char* data_ = nullptr;
37 
38  private:
39   bool is_valid_ = false;
40   unsigned long size_ = 0;  // NOLINT: type required by XGetWindowProperty
41 
42   RTC_DISALLOW_COPY_AND_ASSIGN(XWindowPropertyBase);
43 };
44 
45 // Convenience wrapper for XGetWindowProperty() results.
46 template <class PropertyType>
47 class XWindowProperty : public XWindowPropertyBase {
48  public:
XWindowProperty(Display * display,const Window window,const Atom property)49   XWindowProperty(Display* display, const Window window, const Atom property)
50       : XWindowPropertyBase(display, window, property, sizeof(PropertyType)) {}
51   ~XWindowProperty() override = default;
52 
data()53   const PropertyType* data() const {
54     return reinterpret_cast<PropertyType*>(data_);
55   }
data()56   PropertyType* data() { return reinterpret_cast<PropertyType*>(data_); }
57 
58   RTC_DISALLOW_COPY_AND_ASSIGN(XWindowProperty);
59 };
60 
61 }  // namespace webrtc
62 
63 #endif  // MODULES_DESKTOP_CAPTURE_LINUX_X_WINDOW_PROPERTY_H_
64