Fix wine build
[carla.git] / source / interposer / interposer-x11.cpp
blob6035d61bd6261aceabc41e8d6964f2a463c94b8f
1 /*
2 * Carla Interposer for X11 Window Mapping
3 * Copyright (C) 2014-2022 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaUtils.hpp"
20 #include <dlfcn.h>
21 #include <X11/Xlib.h>
23 // --------------------------------------------------------------------------------------------------------------------
24 // Function typedefs
26 typedef int (*XWindowFunc)(Display*, Window);
28 // --------------------------------------------------------------------------------------------------------------------
29 // Calling the real functions
31 static int real_XMapWindow(Display* display, Window window)
33 static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapWindow");
34 CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
36 return func(display, window);
39 static int real_XMapRaised(Display* display, Window window)
41 static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapRaised");
42 CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
44 return func(display, window);
47 static int real_XMapSubwindows(Display* display, Window window)
49 static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapSubwindows");
50 CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
52 return func(display, window);
55 static int real_XUnmapWindow(Display* display, Window window)
57 static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XUnmapWindow");
58 CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
60 return func(display, window);
63 // --------------------------------------------------------------------------------------------------------------------
64 // Custom carla window handling
66 static int carlaWindowMap(Display* const display, const Window window, const int fallbackFnType)
68 for (;;)
70 if (const char* const winIdStr = std::getenv("CARLA_ENGINE_OPTION_FRONTEND_WIN_ID"))
72 CARLA_SAFE_ASSERT_BREAK(winIdStr[0] != '\0');
74 const long long winIdLL(std::strtoll(winIdStr, nullptr, 16));
75 CARLA_SAFE_ASSERT_BREAK(winIdLL > 0);
77 const Window winId(static_cast<Window>(winIdLL));
78 XSetTransientForHint(display, window, static_cast<Window>(winId));
80 carla_stdout("Transient hint correctly applied before mapping window");
83 break;
86 switch (fallbackFnType)
88 case 1:
89 return real_XMapWindow(display, window);
90 case 2:
91 return real_XMapRaised(display, window);
92 case 3:
93 return real_XMapSubwindows(display, window);
94 default:
95 return 0;
99 // --------------------------------------------------------------------------------------------------------------------
100 // Our custom X11 functions
102 CARLA_PLUGIN_EXPORT
103 int XMapWindow(Display* display, Window window)
105 carla_debug("XMapWindow(%p, %lu)", display, window);
106 return carlaWindowMap(display, window, 1);
109 CARLA_PLUGIN_EXPORT
110 int XMapRaised(Display* display, Window window)
112 carla_debug("XMapRaised(%p, %lu)", display, window);
113 return carlaWindowMap(display, window, 2);
116 CARLA_PLUGIN_EXPORT
117 int XMapSubwindows(Display* display, Window window)
119 carla_debug("XMapSubwindows(%p, %lu)", display, window);
120 return carlaWindowMap(display, window, 3);
123 CARLA_PLUGIN_EXPORT
124 int XUnmapWindow(Display* display, Window window)
126 carla_debug("XUnmapWindow(%p, %lu)", display, window);
127 return real_XUnmapWindow(display, window);
130 // --------------------------------------------------------------------------------------------------------------------