JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ScopedWindowAssociation_linux.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29extern XContext windowHandleXContext;
30
31/* Attaches a pointer to a given window, so that it can be retrieved with XFindContext on
32 the windowHandleXContext.
33*/
35{
36public:
37 ScopedWindowAssociation() = default;
38
40 : associatedPointer ([&]() -> void*
41 {
42 if (associatedIn == nullptr)
43 return nullptr;
44
45 // If you hit this, there's already a pointer associated with this window.
46 const auto display = XWindowSystem::getInstance()->getDisplay();
47 jassert (! getAssociatedPointer (display, windowIn).has_value());
48
49 if (X11Symbols::getInstance()->xSaveContext (display,
50 static_cast<XID> (windowIn),
51 windowHandleXContext,
53 {
55 return nullptr;
56 }
57
58 return associatedIn;
59 }()),
60 window (static_cast<XID> (windowIn)) {}
61
63 ScopedWindowAssociation& operator= (const ScopedWindowAssociation&) = delete;
64
66 : associatedPointer (std::exchange (other.associatedPointer, nullptr)), window (other.window) {}
67
69 {
70 ScopedWindowAssociation { std::move (other) }.swap (*this);
71 return *this;
72 }
73
75 {
76 if (associatedPointer == nullptr)
77 return;
78
79 const auto display = XWindowSystem::getInstance()->getDisplay();
80 const auto ptr = getAssociatedPointer (display, window);
81
82 if (! ptr.has_value())
83 {
84 // If you hit this, something else has cleared this association before we were able to.
86 return;
87 }
88
89 jassert (unalignedPointerCast<XPointer> (associatedPointer) == *ptr);
90
91 if (X11Symbols::getInstance()->xDeleteContext (display, window, windowHandleXContext) != 0)
93 }
94
95 bool isValid() const { return associatedPointer != nullptr; }
96
97private:
98 static std::optional<XPointer> getAssociatedPointer (Display* display, Window window)
99 {
100 XPointer ptr{};
101
102 if (X11Symbols::getInstance()->xFindContext (display, window, windowHandleXContext, &ptr) != 0)
103 return std::nullopt;
104
105 return ptr;
106 }
107
108 void swap (ScopedWindowAssociation& other) noexcept
109 {
110 std::swap (other.associatedPointer, associatedPointer);
111 std::swap (other.window, window);
112 }
113
114 void* associatedPointer = nullptr;
115 XID window{};
116};
117
118} // namespace juce
T exchange(T... args)
#define jassert(expression)
Platform-independent assertion macro.
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T swap(T... args)