Skip to content

Commit

Permalink
Safer way to fetch X11 UI size
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Oct 13, 2021
1 parent e081724 commit d87e6f8
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions source/utils/CarlaPluginUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef void (*EventProcPtr)(XEvent* ev);

static const uint X11Key_Escape = 9;
static bool gErrorTriggered = false;
static pthread_mutex_t gErrorMutex = PTHREAD_MUTEX_INITIALIZER;

static int temporaryErrorHandler(Display*, XErrorEvent*)
{
Expand Down Expand Up @@ -153,47 +154,65 @@ class X11PluginUI : public CarlaPluginUI
{
if (! fSetSizeCalledAtLeastOnce)
{
XSizeHints hints;
carla_zeroStruct(hints);
int width = 0;
int height = 0;

if (XGetNormalHints(fDisplay, childWindow, &hints))
XWindowAttributes attrs;
carla_zeroStruct(attrs);

pthread_mutex_lock(&gErrorMutex);
const XErrorHandler oldErrorHandler = XSetErrorHandler(temporaryErrorHandler);
gErrorTriggered = false;

if (XGetWindowAttributes(fDisplay, childWindow, &attrs))
{
int width = 0;
int height = 0;
width = attrs.width;
height = attrs.height;
}

if (hints.flags & PSize)
{
width = hints.width;
height = hints.height;
}
else if (hints.flags & PBaseSize)
{
width = hints.base_width;
height = hints.base_height;
}
else if (hints.flags & PMinSize)
XSetErrorHandler(oldErrorHandler);
pthread_mutex_unlock(&gErrorMutex);

if (width == 0 && height == 0)
{
XSizeHints sizeHints;
carla_zeroStruct(sizeHints);

if (XGetNormalHints(fDisplay, childWindow, &sizeHints))
{
width = hints.min_width;
height = hints.min_height;
if (sizeHints.flags & PSize)
{
width = sizeHints.width;
height = sizeHints.height;
}
else if (sizeHints.flags & PBaseSize)
{
width = sizeHints.base_width;
height = sizeHints.base_height;
}
}

if (width > 0 && height > 0)
setSize(static_cast<uint>(width), static_cast<uint>(height), false);
}

if (width > 1 && height > 1)
setSize(static_cast<uint>(width), static_cast<uint>(height), false);
}

const Atom _xevp = XInternAtom(fDisplay, "_XEventProc", False);

gErrorTriggered = false;
pthread_mutex_lock(&gErrorMutex);
const XErrorHandler oldErrorHandler(XSetErrorHandler(temporaryErrorHandler));
gErrorTriggered = false;

Atom actualType;
int actualFormat;
ulong nitems, bytesAfter;
uchar* data = nullptr;

XGetWindowProperty(fDisplay, childWindow, _xevp, 0, 1, False, AnyPropertyType, &actualType, &actualFormat, &nitems, &bytesAfter, &data);
XGetWindowProperty(fDisplay, childWindow, _xevp, 0, 1, False, AnyPropertyType,
&actualType, &actualFormat, &nitems, &bytesAfter, &data);

XSetErrorHandler(oldErrorHandler);
pthread_mutex_unlock(&gErrorMutex);

if (nitems == 1 && ! gErrorTriggered)
{
Expand Down Expand Up @@ -255,8 +274,9 @@ class X11PluginUI : public CarlaPluginUI
{
if (! fChildWindowConfigured)
{
gErrorTriggered = false;
pthread_mutex_lock(&gErrorMutex);
const XErrorHandler oldErrorHandler = XSetErrorHandler(temporaryErrorHandler);
gErrorTriggered = false;

XSizeHints sizeHints;
carla_zeroStruct(sizeHints);
Expand All @@ -273,6 +293,7 @@ class X11PluginUI : public CarlaPluginUI

fChildWindowConfigured = true;
XSetErrorHandler(oldErrorHandler);
pthread_mutex_unlock(&gErrorMutex);
}

if (fChildWindow != 0)
Expand Down

0 comments on commit d87e6f8

Please sign in to comment.