Debian Codegraph @

Source/Core/UICommon/X11Utils.cpp

raw symbols SCIP
1 // Copyright 2010 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "UICommon/X11Utils.h"
5
6 #include <cstdio>
7 #include <cstdlib>
8 #include <cstring>
9 #include <string>
10
11 #include <fmt/format.h>
12
13 #include "Common/Contains.h"
14 #include "Common/Logging/Log.h"
15 #include "Core/Config/MainSettings.h"
16
17 extern char** environ;
18
19 namespace X11Utils
20 {
21 bool ToggleFullscreen(Display* dpy, Window win)
22 {
23 // Init X event structure for _NET_WM_STATE_FULLSCREEN client message
24 XEvent event;
25 event.xclient.type = ClientMessage;
26 event.xclient.message_type = XInternAtom(dpy, "_NET_WM_STATE", False);
27 event.xclient.window = win;
28 event.xclient.format = 32;
29 event.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;
30 event.xclient.data.l[1] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
31
32 // Send the event
33 if (!XSendEvent(dpy, DefaultRootWindow(dpy), False,
34 SubstructureRedirectMask | SubstructureNotifyMask, &event))
35 {
36 ERROR_LOG_FMT(VIDEO, "Failed to switch fullscreen/windowed mode.");
37 return false;
38 }
39
40 return true;
41 }
42
43 #ifdef HAVE_XRANDR
44 XRRConfiguration::XRRConfiguration(Display* _dpy, Window _win)
45 : dpy(_dpy), win(_win), screenResources(nullptr), outputInfo(nullptr), crtcInfo(nullptr),
46 fullMode(0), fs_fb_width(0), fs_fb_height(0), fs_fb_width_mm(0), fs_fb_height_mm(0),
47 bValid(true), bIsFullscreen(false)
48 {
49 int XRRMajorVersion, XRRMinorVersion;
50
51 if (!XRRQueryVersion(dpy, &XRRMajorVersion, &XRRMinorVersion) ||
52 (XRRMajorVersion < 1 || (XRRMajorVersion == 1 && XRRMinorVersion < 3)))
53 {
54 WARN_LOG_FMT(VIDEO, "XRRExtension not supported.");
55 bValid = false;
56 return;
57 }
58
59 screenResources = XRRGetScreenResourcesCurrent(dpy, win);
60
61 screen = DefaultScreen(dpy);
62 fb_width = DisplayWidth(dpy, screen);
63 fb_height = DisplayHeight(dpy, screen);
64 fb_width_mm = DisplayWidthMM(dpy, screen);
65 fb_height_mm = DisplayHeightMM(dpy, screen);
66
67 INFO_LOG_FMT(VIDEO, "XRRExtension-Version {}.{}", XRRMajorVersion, XRRMinorVersion);
68 Update();
69 }
70
71 XRRConfiguration::~XRRConfiguration()
72 {
73 if (bValid && bIsFullscreen)
74 ToggleDisplayMode(False);
75 if (screenResources)
76 XRRFreeScreenResources(screenResources);
77 if (outputInfo)
78 XRRFreeOutputInfo(outputInfo);
79 if (crtcInfo)
80 XRRFreeCrtcInfo(crtcInfo);
81 }
82
83 void XRRConfiguration::Update()
84 {
85 const std::string fullscreen_display_res = Config::Get(Config::MAIN_FULLSCREEN_DISPLAY_RES);
86 if (fullscreen_display_res == "Auto")
87 return;
88
89 if (!bValid)
90 return;
91
92 if (outputInfo)
93 {
94 XRRFreeOutputInfo(outputInfo);
95 outputInfo = nullptr;
96 }
97 if (crtcInfo)
98 {
99 XRRFreeCrtcInfo(crtcInfo);
100 crtcInfo = nullptr;
101 }
102 fullMode = 0;
103
104 // Get the resolution settings for fullscreen mode
105 unsigned int fullWidth, fullHeight;
106 char* output_name = nullptr;
107 char auxFlag = '\0';
108 if (!fullscreen_display_res.contains(':'))
109 {
110 fullWidth = fb_width;
111 fullHeight = fb_height;
112 }
113 else
114 {
115 sscanf(fullscreen_display_res.c_str(), "%m[^:]: %ux%u%c", &output_name, &fullWidth, &fullHeight,
116 &auxFlag);
117 }
118 bool want_interlaced = ('i' == auxFlag);
119
120 for (int i = 0; i < screenResources->noutput; i++)
121 {
122 XRROutputInfo* output_info =
123 XRRGetOutputInfo(dpy, screenResources, screenResources->outputs[i]);
124 if (output_info && output_info->crtc && output_info->connection == RR_Connected)
125 {
126 XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(dpy, screenResources, output_info->crtc);
127 if (crtc_info)
128 {
129 if (!output_name || !strcmp(output_name, output_info->name))
130 {
131 // Use the first output for the default setting.
132 if (!output_name)
133 {
134 output_name = strdup(output_info->name);
135 Config::SetBaseOrCurrent(
136 Config::MAIN_FULLSCREEN_DISPLAY_RES,
137 fmt::format("{}: {}x{}", output_info->name, fullWidth, fullHeight));
138 }
139 outputInfo = output_info;
140 crtcInfo = crtc_info;
141 for (int j = 0; j < output_info->nmode && fullMode == 0; j++)
142 {
143 for (int k = 0; k < screenResources->nmode && fullMode == 0; k++)
144 {
145 if (output_info->modes[j] == screenResources->modes[k].id)
146 {
147 if (fullWidth == screenResources->modes[k].width &&
148 fullHeight == screenResources->modes[k].height &&
149 want_interlaced == !!(screenResources->modes[k].modeFlags & RR_Interlace))
150 {
151 fullMode = screenResources->modes[k].id;
152 if (crtcInfo->x + (int)screenResources->modes[k].width > fs_fb_width)
153 fs_fb_width = crtcInfo->x + screenResources->modes[k].width;
154 if (crtcInfo->y + (int)screenResources->modes[k].height > fs_fb_height)
155 fs_fb_height = crtcInfo->y + screenResources->modes[k].height;
156 }
157 }
158 }
159 }
160 }
161 else
162 {
163 if (crtc_info->x + (int)crtc_info->width > fs_fb_width)
164 fs_fb_width = crtc_info->x + crtc_info->width;
165 if (crtc_info->y + (int)crtc_info->height > fs_fb_height)
166 fs_fb_height = crtc_info->y + crtc_info->height;
167 }
168 }
169 if (crtc_info && crtcInfo != crtc_info)
170 XRRFreeCrtcInfo(crtc_info);
171 }
172 if (output_info && outputInfo != output_info)
173 XRRFreeOutputInfo(output_info);
174 }
175 fs_fb_width_mm = fs_fb_width * DisplayHeightMM(dpy, screen) / DisplayHeight(dpy, screen);
176 fs_fb_height_mm = fs_fb_height * DisplayHeightMM(dpy, screen) / DisplayHeight(dpy, screen);
177
178 if (output_name)
179 free(output_name);
180
181 if (outputInfo && crtcInfo && fullMode)
182 {
183 INFO_LOG_FMT(VIDEO, "Fullscreen Resolution {}x{}", fullWidth, fullHeight);
184 }
185 else
186 {
187 ERROR_LOG_FMT(VIDEO, "Failed to obtain fullscreen size.\n"
188 "Using current desktop resolution for fullscreen.");
189 }
190 }
191
192 void XRRConfiguration::ToggleDisplayMode(bool bFullscreen)
193 {
194 if (!bValid || !screenResources || !outputInfo || !crtcInfo || !fullMode)
195 return;
196 if (bFullscreen == bIsFullscreen)
197 return;
198
199 XGrabServer(dpy);
200 if (bFullscreen)
201 {
202 XRRSetCrtcConfig(dpy, screenResources, outputInfo->crtc, CurrentTime, crtcInfo->x, crtcInfo->y,
203 fullMode, crtcInfo->rotation, crtcInfo->outputs, crtcInfo->noutput);
204 XRRSetScreenSize(dpy, win, fs_fb_width, fs_fb_height, fs_fb_width_mm, fs_fb_height_mm);
205 bIsFullscreen = true;
206 }
207 else
208 {
209 XRRSetCrtcConfig(dpy, screenResources, outputInfo->crtc, CurrentTime, crtcInfo->x, crtcInfo->y,
210 crtcInfo->mode, crtcInfo->rotation, crtcInfo->outputs, crtcInfo->noutput);
211 XRRSetScreenSize(dpy, win, fb_width, fb_height, fb_width_mm, fb_height_mm);
212 bIsFullscreen = false;
213 }
214 XUngrabServer(dpy);
215 XSync(dpy, false);
216 }
217
218 void XRRConfiguration::AddResolutions(std::vector<std::string>& resos)
219 {
220 if (!bValid || !screenResources)
221 return;
222
223 // Get all full screen resolutions for the config dialog
224 for (int i = 0; i < screenResources->noutput; i++)
225 {
226 XRROutputInfo* output_info =
227 XRRGetOutputInfo(dpy, screenResources, screenResources->outputs[i]);
228
229 if (output_info && output_info->crtc && output_info->connection == RR_Connected)
230 {
231 for (int j = 0; j < output_info->nmode; j++)
232 {
233 for (int k = 0; k < screenResources->nmode; k++)
234 {
235 if (output_info->modes[j] == screenResources->modes[k].id)
236 {
237 bool interlaced = !!(screenResources->modes[k].modeFlags & RR_Interlace);
238 const std::string strRes = std::string(output_info->name) + ": " +
239 std::string(screenResources->modes[k].name) +
240 (interlaced ? "i" : "");
241 // Only add unique resolutions
242 if (!Common::Contains(resos, strRes))
243 {
244 resos.push_back(strRes);
245 }
246 }
247 }
248 }
249 }
250 if (output_info)
251 XRRFreeOutputInfo(output_info);
252 }
253 }
254
255 #endif
256 } // namespace X11Utils