iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsLinux.h
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
13// X11/Xlib.h and X11/X.h define macros that conflict with C++ identifiers in
14// IGraphics and IPlug headers. We include X11 headers first to get the type
15// declarations, then undef the conflicting macros, then include IGraphics headers.
16// GL/glx.h must come AFTER IGraphics_select.h (which includes glad) because
17// glad checks that GL/gl.h has not been included yet.
18#include <X11/Xlib.h>
19#include <X11/Xutil.h>
20
21// Undef X11 macros that conflict with C++ enum identifiers and type names.
22// IGraphicsLinux.cpp includes X11 headers directly before this header, so the
23// macros are available there for X11 API calls. Function bodies use 0/1 literals
24// instead of None/False/True to avoid relying on the undefined names.
25#ifdef None
26 #undef None
27#endif
28#ifdef Bool
29 #undef Bool
30#endif
31#ifdef Status
32 #undef Status
33#endif
34#ifdef True
35 #undef True
36#endif
37#ifdef False
38 #undef False
39#endif
40#ifdef Success
41 #undef Success
42#endif
43#ifdef Complex
44 #undef Complex
45#endif
46#ifdef Above
47 #undef Above
48#endif
49#ifdef Below
50 #undef Below
51#endif
52#ifdef Convex
53 #undef Convex
54#endif
55#ifdef CurrentTime
56 #undef CurrentTime
57#endif
58
59// IGraphics_select.h includes <glad/glad.h> for Linux (defines __gl_h_).
60// GL/glx.h must be included BEFORE this header (in the .cpp file) while Bool
61// is still defined from Xlib.h; the GLX_H include guard prevents re-inclusion.
62#include <atomic>
63#include <vector>
64#include "IPlugTimer.h"
65#include "IGraphics_select.h"
66
67BEGIN_IPLUG_NAMESPACE
68BEGIN_IGRAPHICS_NAMESPACE
69
72class IGraphicsLinux final : public IGRAPHICS_DRAW_CLASS
73{
74public:
75 IGraphicsLinux(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
77
78 void ForceEndUserEdit() override;
82 float GetPlatformWindowScale() const override { return GetScreenScale(); }
83
86 int GetX11Fd() const { return mDisplay ? XConnectionNumber(mDisplay) : -1; }
87
91 void SetHostDriven(bool hostDriven)
92 {
93 mHostDriven = hostDriven;
94 if (hostDriven)
95 StopTimer();
96 }
97
99 void OnDisplayTimer();
101 void ProcessX11Events();
103 bool WindowCloseRequested() const { return mCloseRequested; }
104 void RequestWindowClose() { mCloseRequested = true; }
105 bool IsInPlatformCallback() const { return mDispatchDepth != 0; }
106
107 void PlatformResize(bool parentHasResized) override;
109 void SetScreenScaleFromHost(float scale);
111 void ResizeFromHost(int width, int height);
113 void ConstrainHostResize(int& width, int& height);
114
115 void HideMouseCursor(bool hide, bool lock) override;
116 void MoveMouseCursor(float x, float y) override;
117 ECursor SetMouseCursor(ECursor cursorType) override;
118
119 void GetMouseLocation(float& x, float& y) const override;
120
121 EMsgBoxResult ShowMessageBox(const char* str, const char* title, EMsgBoxType type, IMsgBoxCompletionHandlerFunc completionHandler) override;
122
123 void* OpenWindow(void* pParent) override;
124 void CloseWindow() override;
125 bool WindowIsOpen() override { return mPlugWnd != 0; }
126
127 void UpdateTooltips() override;
128
129 bool RevealPathInExplorerOrFinder(WDL_String& path, bool select) override;
130 void PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action, const char* ext, IFileDialogCompletionHandlerFunc completionHandler) override;
131 void PromptForDirectory(WDL_String& dir, IFileDialogCompletionHandlerFunc completionHandler) override;
132 bool PromptForColor(IColor& color, const char* str, IColorPickerHandlerFunc func) override;
133
134 bool OpenURL(const char* url, const char* msgWindowTitle, const char* confirmMsg, const char* errMsgOnFailure) override;
135
136 void* GetWindow() override { return (void*)(uintptr_t)mPlugWnd; }
137
138 const char* GetPlatformAPIStr() override { return "x11"; }
139
140 bool GetTextFromClipboard(WDL_String& str) override;
141 bool SetTextInClipboard(const char* str) override;
142
143protected:
144 IPopupMenu* CreatePlatformPopupMenu(IPopupMenu& menu, const IRECT bounds, bool& isAsync) override;
145 void CreatePlatformTextEntry(int paramIdx, const IText& text, const IRECT& bounds, int length, const char* str) override;
146
147private:
148#if defined IGRAPHICS_SKIA && defined IGRAPHICS_CPU
149 void PresentCPUFrame(const SkPixmap& pixels) override;
150 void DestroyCPUImage();
151 XImage* mCPUImage = nullptr;
152 GC mCPUGC = nullptr;
153#endif
154
155 void CalculateHostResize(int width, int height, int& designWidth, int& designHeight, float& scale);
156 struct DispatchScope
157 {
158 explicit DispatchScope(std::atomic<int>& depth)
159 : mDepth(depth)
160 {
161 ++mDepth;
162 }
163 ~DispatchScope() { --mDepth; }
164 std::atomic<int>& mDepth;
165 };
166
167 void DispatchX11Events();
168 void DispatchKeyEvent(XKeyEvent& event);
169 void CreateInputContext();
170 void DestroyInputContext();
171 void ResetDrag();
172 void HideTooltip();
173 void ShowTooltip(float x, float y);
174 void DrawTooltip();
175 PlatformFontPtr LoadPlatformFont(const char* fontID, const char* fileNameOrResID) override;
176 PlatformFontPtr LoadPlatformFont(const char* fontID, const char* fontName, ETextStyle style) override;
177 PlatformFontPtr LoadPlatformFont(const char* fontID, void* pData, int dataSize) override;
178 void CachePlatformFont(const char* fontID, const PlatformFontPtr& font) override;
179
180 void ActivateGLContext() override;
181 void DeactivateGLContext() override;
182
183 struct GLContextState
184 {
185 Display* display;
186 void* context;
187 unsigned long drawable;
188 unsigned long readDrawable;
189 };
190 std::vector<GLContextState> mGLContextStack;
191
192 void* CreateGdkButtonPressEvent(void* gtkHandle, void* gdkDisplay, void* gdkWindow, int gdkScale); // caller frees with gdk_event_free
193 void ReleaseX11PointerGrab(); // hand the pointer to GTK menus/dialogs opened from a click
194 XVisualInfo* CreateGLContext(); // returns the visual used; caller must XFree() it
195 void DestroyGLContext();
196 void* mGLContext = nullptr; // GLXContext
197 Colormap mGLColormap = 0;
198
199 Display* mDisplay = nullptr;
200 XIM mInputMethod = nullptr;
201 XIC mInputContext = nullptr;
202 Window mPlugWnd = 0;
203 Window mParentWnd = 0;
204 Atom mWMDeleteMessage = 0;
205 Atom mXdndAware = 0, mXdndEnter = 0, mXdndPosition = 0, mXdndStatus = 0;
206 Atom mXdndLeave = 0, mXdndDrop = 0, mXdndFinished = 0, mXdndSelection = 0;
207 Atom mXdndTypeList = 0, mXdndActionCopy = 0, mUriList = 0, mDropProperty = 0;
208 Window mDragSource = 0;
209 bool mDragSupported = false;
210 float mDropX = 0.f, mDropY = 0.f;
211 Window mTooltipWindow = 0;
212 XFontSet mTooltipFont = nullptr;
213 int mTooltipControl = -1;
214 float mTooltipX = 0.f, mTooltipY = 0.f;
215 unsigned long mTooltipHoverTime = 0;
216 bool mInputMethodDisconnected = false;
217 bool mTimerRunning = false;
218
219 float mHiddenCursorX = 0.f;
220 float mHiddenCursorY = 0.f;
221 Cursor mBlankCursor = 0; // cached invisible cursor for HideMouseCursor
222 bool mNeedsRedraw = false;
223 std::atomic<bool> mCloseRequested{false};
224 std::atomic<int> mDispatchDepth{0};
225 unsigned long mLastLeftClickTime = 0; // X11 server timestamp of last left-button press
226 // Last button press, used as the trigger event for GTK popup menus
227 unsigned long mLastPressTime = 0;
228 unsigned int mLastPressButton = 0;
229 int mLastPressX = 0, mLastPressY = 0, mLastPressRootX = 0, mLastPressRootY = 0;
230 float mLastLeftClickX = 0.f;
231 float mLastLeftClickY = 0.f;
232
233 // Clipboard atoms (interned in OpenWindow)
234 Atom mClipboardAtom = 0;
235 Atom mUTF8StringAtom = 0;
236 Atom mSelDataAtom = 0;
237
238 bool mHostDriven = false; // set true before OpenWindow when host drives the event loop
239 bool mInConfigureNotify = false; // suppress PlatformResize re-entry from ConfigureNotify
240
241 bool StartTimer();
242 void StopTimer();
243
244 std::unique_ptr<Timer> mMainLoopTimer;
245};
246
247END_IGRAPHICS_NAMESPACE
248END_IPLUG_NAMESPACE
Used for choosing a drawing backend.
Platform timer primitives.
An editor delegate base class that uses IGraphics for the UI.
IGraphics platform class for Linux.
void ProcessX11Events()
Called by CLAP host posix-fd callback to drain pending X11 events.
int GetX11Fd() const
Returns the X11 display file descriptor, or -1 if not open.
void SetScreenScaleFromHost(float scale)
Apply a new host scale without interpreting older native sizes at the new scale.
void ResizeFromHost(int width, int height)
Apply a host/X11 size in physical pixels, preserving the configured resize mode.
float GetPlatformWindowScale() const override
Returns the screen scale factor so that requestResize() and OnParentWindowResize() pass physical pixe...
void ConstrainHostResize(int &width, int &height)
Constrain a proposed host size in physical pixels without resizing the editor.
bool WindowCloseRequested() const
A native close is completed by the owner after X11 dispatch returns.
void OnDisplayTimer()
Called by CLAP host timer callback to advance one display frame.
void SetHostDriven(bool hostDriven)
When host-driven = true the internal timer is suppressed.
A class for setting the contents of a pop up menu.
Used to manage color data, independent of draw class/platform.
Used to manage a rectangular area, independent of draw class/platform.
IText is used to manage font and text/text entry style for a piece of text on the UI,...