LMMS
Loading...
Searching...
No Matches
Windows.cpp
Go to the documentation of this file.
1/*
2 * Carla Plugin Host
3 * Copyright (C) 2011-2022 Filipe Coelho <falktx@falktx.com>
4 *
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.
9 *
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.
14 *
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16 */
17
18#include "CarlaUtils.h"
19
20#include "CarlaMathUtils.hpp"
21
22#if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
23# import <Cocoa/Cocoa.h>
24#endif
25
26#ifdef HAVE_X11
27# include <X11/Xlib.h>
28# include <X11/Xresource.h>
29#endif
30
31namespace CB = CARLA_BACKEND_NAMESPACE;
32
33// -------------------------------------------------------------------------------------------------------------------
34
36{
37 // allow custom scale for testing
38 if (const char* const scale = getenv("DPF_SCALE_FACTOR"))
39 return std::max(1.0, std::atof(scale));
40 // Qt env var for the same
41 if (const char* const scale = getenv("QT_SCALE_FACTOR"))
42 return std::max(1.0, std::atof(scale));
43
44#if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
45 return [NSScreen mainScreen].backingScaleFactor;
46#endif
47#ifdef HAVE_X11
48 if (::Display* const display = XOpenDisplay(nullptr))
49 {
50 XrmInitialize();
51
52 if (char* const rms = XResourceManagerString(display))
53 {
54 if (const XrmDatabase sdb = XrmGetStringDatabase(rms))
55 {
56 char* type = nullptr;
57 XrmValue ret;
58
59 if (XrmGetResource(sdb, "Xft.dpi", "String", &type, &ret)
60 && ret.addr != nullptr
61 && type != nullptr
62 && std::strncmp("String", type, 6) == 0)
63 {
64 const double dpi = std::atof(ret.addr);
65 if (carla_isNotZero(dpi))
66 return dpi / 96;
67 }
68
69 XrmDestroyDatabase(sdb);
70 }
71 }
72
73 XCloseDisplay(display);
74 }
75#endif
76
77 return 1.0;
78}
79
80// -------------------------------------------------------------------------------------------------------------------
81
82int carla_cocoa_get_window(void* nsViewPtr)
83{
84 CARLA_SAFE_ASSERT_RETURN(nsViewPtr != nullptr, 0);
85
86#if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
87 NSView* const nsView = (NSView*)nsViewPtr;
88 return [[nsView window] windowNumber];
89#else
90 return 0;
91#endif
92}
93
94void carla_cocoa_set_transient_window_for(void* nsViewChildPtr, void* nsViewParentPtr)
95{
96 CARLA_SAFE_ASSERT_RETURN(nsViewChildPtr != nullptr,);
97 CARLA_SAFE_ASSERT_RETURN(nsViewParentPtr != nullptr,);
98
99#if defined(CARLA_OS_MAC) && !defined(CARLA_PLUGIN_BUILD)
100 NSView* const nsViewChild = (NSView*)nsViewChildPtr;
101 NSView* const nsViewParent = (NSView*)nsViewParentPtr;
102 [[nsViewParent window] addChildWindow:[nsViewChild window]
103 ordered:NSWindowAbove];
104#endif
105}
106
107void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
108{
109 CARLA_SAFE_ASSERT_RETURN(winId1 != 0,);
110 CARLA_SAFE_ASSERT_RETURN(winId2 != 0,);
111
112#ifdef HAVE_X11
113 if (::Display* const disp = XOpenDisplay(nullptr))
114 {
115 XReparentWindow(disp, winId1, winId2, 0, 0);
116 XMapWindow(disp, winId1);
117 XCloseDisplay(disp);
118 }
119#endif
120}
121
122void carla_x11_move_window(uintptr_t winId, int x, int y)
123{
124 CARLA_SAFE_ASSERT_RETURN(winId != 0,);
125
126#ifdef HAVE_X11
127 if (::Display* const disp = XOpenDisplay(nullptr))
128 {
129 XMoveWindow(disp, winId, x, y);
130 XCloseDisplay(disp);
131 }
132#else
133 // unused
134 return; (void)x; (void)y;
135#endif
136}
137
138int* carla_x11_get_window_pos(uintptr_t winId)
139{
140 static int pos[4];
141
142 if (winId == 0)
143 {
144 pos[0] = 0;
145 pos[1] = 0;
146 pos[2] = 0;
147 pos[3] = 0;
148 }
149#ifdef HAVE_X11
150 else if (::Display* const disp = XOpenDisplay(nullptr))
151 {
152 int x, y;
153 Window child;
154 XWindowAttributes xwa;
155 XTranslateCoordinates(disp, winId, XRootWindow(disp, 0), 0, 0, &x, &y, &child);
156 XGetWindowAttributes(disp, winId, &xwa);
157 XCloseDisplay(disp);
158 pos[0] = x - xwa.x;
159 pos[1] = y - xwa.y;
160 pos[2] = xwa.x;
161 pos[3] = xwa.y;
162 }
163#endif
164 else
165 {
166 pos[0] = 0;
167 pos[1] = 0;
168 pos[2] = 0;
169 pos[3] = 0;
170 }
171
172 return pos;
173}
174
175// -------------------------------------------------------------------------------------------------------------------
#define CARLA_BACKEND_NAMESPACE
Definition CarlaBackend.h:32
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
int y
Definition inflate.c:1588
unsigned x[BMAX+1]
Definition inflate.c:1586
int * carla_x11_get_window_pos(uintptr_t winId)
Definition Windows.cpp:138
void carla_cocoa_set_transient_window_for(void *nsViewChildPtr, void *nsViewParentPtr)
Definition Windows.cpp:94
void carla_x11_move_window(uintptr_t winId, int x, int y)
Definition Windows.cpp:122
double carla_get_desktop_scale_factor()
Definition Windows.cpp:35
void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
Definition Windows.cpp:107
int carla_cocoa_get_window(void *nsViewPtr)
Definition Windows.cpp:82
CARLA_PLUGIN_EXPORT int XMapWindow(Display *display, Window window)
Definition interposer-x11.cpp:103
char * getenv()
#define void
Definition unzip.h:396