Debian Codegraph @

src/gui/scaling.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) 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 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <string.h>
21 #include "scaling.h"
22 #include "../ta-log.h"
23 #include <SDL.h>
24 #include <SDL_syswm.h>
25
26 #ifdef _WIN32
27 #include <windows.h>
28 #include "shellScalingStub.h"
29 typedef HRESULT (WINAPI *GDFM)(HMONITOR,MONITOR_DPI_TYPE,UINT*,UINT*);
30 #endif
31
32 #ifdef __APPLE__
33 extern "C" {
34 #include "macstuff.h"
35 }
36 #endif
37
38 #if defined(__unix__) || defined(ANDROID)
39 #include <dlfcn.h>
40 typedef void* (*XOD)(const char*);
41 typedef int (*XCD)(void*);
42 typedef int (*XDS)(void*);
43 typedef int (*XDW)(void*,int);
44 #endif
45
46 double getScaleFactor(const char* driverHint, void* windowHint) {
47 double ret=1.0;
48
49 // Windows
50 #ifdef _WIN32
51 POINT nullPoint;
52 if (windowHint!=NULL) {
53 int px=0;
54 int py=0;
55
56 SDL_GetWindowPosition((SDL_Window*)windowHint,&px,&py);
57 nullPoint.x=px;
58 nullPoint.y=py;
59 } else {
60 nullPoint.x=-1;
61 nullPoint.y=-1;
62 }
63 HMONITOR disp=MonitorFromPoint(nullPoint,MONITOR_DEFAULTTOPRIMARY);
64
65 if (disp==NULL) {
66 logW("could not find a monitor - no scaling detection available!");
67 return 1.0;
68 }
69
70 HMODULE shcore=LoadLibraryW(L"shcore.dll");
71 if (shcore==NULL) {
72 logW("could not find shcore.dll (%.8x) - no scaling detection available!",GetLastError());
73 return 1.0;
74 }
75 GDFM ta_GetDpiForMonitor=(GDFM)GetProcAddress(shcore,"GetDpiForMonitor");
76 if (ta_GetDpiForMonitor==NULL) {
77 logW("GetDpiForMonitor not found (%.8x) - no scaling detection available!",GetLastError());
78
79 if (!FreeLibrary(shcore)) {
80 logE("could not free shcore.dll (%.8x)!",GetLastError());
81 }
82 return 1.0;
83 }
84
85 UINT dpiX=96;
86 UINT dpiY=96;
87 HRESULT result=ta_GetDpiForMonitor(disp,MDT_EFFECTIVE_DPI,&dpiX,&dpiY);
88 if (result!=S_OK) {
89 logW("GetDpiForMonitor failure (%.8x) - no scaling detection available!",result);
90
91 if (!FreeLibrary(shcore)) {
92 logE("could not free shcore.dll (%.8x)!",GetLastError());
93 }
94 return 1.0;
95 }
96
97 ret=(double)(dpiX+dpiY)/192.0;
98
99 if (!FreeLibrary(shcore)) {
100 logE("could not free shcore.dll (%.8x)!",GetLastError());
101 }
102
103 return ret;
104 #endif
105
106 // macOS
107 #ifdef __APPLE__
108 if (driverHint==NULL) {
109 return getMacDPIScale(NULL,false);
110 } else if (strcmp(driverHint,"cocoa")==0) {
111 #ifdef SDL_VIDEO_DRIVER_COCOA
112 void* nsWindow=NULL;
113 SDL_SysWMinfo wmInfo;
114 if (windowHint!=NULL) {
115 SDL_VERSION(&wmInfo.version)
116 if (SDL_GetWindowWMInfo((SDL_Window*)windowHint,&wmInfo)==SDL_TRUE) {
117 nsWindow=wmInfo.info.cocoa.window;
118 }
119 }
120 return getMacDPIScale(nsWindow,false);
121 #endif
122 } else if (strcmp(driverHint,"uikit")==0) {
123 #ifdef SDL_VIDEO_DRIVER_UIKIT
124 void* uiWindow=NULL;
125 SDL_SysWMinfo wmInfo;
126 if (windowHint!=NULL) {
127 SDL_VERSION(&wmInfo.version)
128 if (SDL_GetWindowWMInfo((SDL_Window*)windowHint,&wmInfo)==SDL_TRUE) {
129 uiWindow=wmInfo.info.uikit.window;
130 }
131 }
132 return getMacDPIScale(uiWindow,true);
133 #endif
134 }
135 #endif
136
137 #if defined(__unix__) || defined(ANDROID)
138 if (driverHint==NULL) {
139 return ret;
140 }
141
142 // X11
143 if (strcmp(driverHint,"x11")==0) {
144 void* libX11=dlopen("libX11.so",RTLD_LAZY|RTLD_LOCAL);
145 if (libX11==NULL) {
146 logW("could not load libX11.so (%s) - no scaling detection available!",dlerror());
147 return 1.0;
148 }
149
150 XOD ta_XOpenDisplay=(XOD)dlsym(libX11,"XOpenDisplay");
151 if (ta_XOpenDisplay==NULL) {
152 logW("XOpenDisplay not found (%s) - no scaling detection available!",dlerror());
153 if (dlclose(libX11)!=0) {
154 logE("could not free libX11.so (%s)!",dlerror());
155 }
156 return 1.0;
157 }
158
159 XCD ta_XCloseDisplay=(XCD)dlsym(libX11,"XCloseDisplay");
160 if (ta_XCloseDisplay==NULL) {
161 logW("XCloseDisplay not found (%s) - no scaling detection available!",dlerror());
162 if (dlclose(libX11)!=0) {
163 logE("could not free libX11.so (%s)!",dlerror());
164 }
165 return 1.0;
166 }
167
168 XDS ta_XDefaultScreen=(XDS)dlsym(libX11,"XDefaultScreen");
169 if (ta_XDefaultScreen==NULL) {
170 logW("XDefaultScreen not found (%s) - no scaling detection available!",dlerror());
171 if (dlclose(libX11)!=0) {
172 logE("could not free libX11.so (%s)!",dlerror());
173 }
174 return 1.0;
175 }
176
177 XDW ta_XDisplayWidth=(XDW)dlsym(libX11,"XDisplayWidth");
178 if (ta_XDisplayWidth==NULL) {
179 logW("XDisplayWidth not found (%s) - no scaling detection available!",dlerror());
180 if (dlclose(libX11)!=0) {
181 logE("could not free libX11.so (%s)!",dlerror());
182 }
183 return 1.0;
184 }
185
186 XDW ta_XDisplayWidthMM=(XDW)dlsym(libX11,"XDisplayWidthMM");
187 if (ta_XDisplayWidthMM==NULL) {
188 logW("XDisplayWidthMM not found (%s) - no scaling detection available!",dlerror());
189 if (dlclose(libX11)!=0) {
190 logE("could not free libX11.so (%s)!",dlerror());
191 }
192 return 1.0;
193 }
194
195 // dl mess
196 void* disp=NULL;
197 int screen=0;
198 int dpi=96;
199
200 disp=ta_XOpenDisplay(NULL);
201 if (disp==NULL) {
202 logW("couldn't open X display - no scaling detection available!",dlerror());
203 if (dlclose(libX11)!=0) {
204 logE("could not free libX11.so (%s)!",dlerror());
205 }
206 return 1.0;
207 }
208
209 screen=ta_XDefaultScreen(disp);
210
211 dpi=(int)(0.5+(25.4*(double)ta_XDisplayWidth(disp,screen)/(double)ta_XDisplayWidthMM(disp,screen)));
212
213 ta_XCloseDisplay(disp);
214
215 ret=round(dpi/96.0);
216
217 if (dlclose(libX11)!=0) {
218 logE("could not free libX11.so (%s)!",dlerror());
219 }
220
221 return ret;
222 }
223
224 // Wayland
225 if (strcmp(driverHint,"wayland")==0) {
226 // give up (we handle scaling factor detection after window creation)
227 return 1.0;
228 }
229 #endif
230
231 // SDL fallback
232 #ifdef ANDROID
233 float dpiScaleF=160.0f;
234 if (SDL_GetDisplayDPI(0,&dpiScaleF,NULL,NULL)==0) {
235 ret=dpiScaleF/160.0f;
236 if (ret<1) ret=1;
237 logI("dpiScaleF: %f",dpiScaleF);
238 logI("ret: %f",ret);
239 }
240
241 #else
242 float dpiScaleF=96.0f;
243 if (SDL_GetDisplayDPI(0,&dpiScaleF,NULL,NULL)==0) {
244 ret=round(dpiScaleF/96.0f);
245 if (ret<1) ret=1;
246 }
247 #endif
248
249 // couldn't detect scaling factor :<
250 return ret;
251 }