1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright 2024 Igalia S.L. |
4 | |
5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. |
8 | |
9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: |
12 | |
13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ |
21 | |
22 | #include "SDL_internal.h" |
23 | |
24 | #if defined(SDL_VIDEO_DRIVER_X11) |
25 | |
26 | #include "SDL_x11video.h" |
27 | #include "SDL_x11settings.h" |
28 | |
29 | #define SDL_XSETTINGS_GDK_WINDOW_SCALING_FACTOR "Gdk/WindowScalingFactor" |
30 | #define SDL_XSETTINGS_XFT_DPI "Xft/DPI" |
31 | |
32 | static void X11_XsettingsNotify(const char *name, XSettingsAction action, XSettingsSetting *setting, void *data) |
33 | { |
34 | SDL_VideoDevice *_this = data; |
35 | float scale_factor = 1.0; |
36 | int i; |
37 | |
38 | if (SDL_strcmp(name, SDL_XSETTINGS_GDK_WINDOW_SCALING_FACTOR) != 0 || |
39 | SDL_strcmp(name, SDL_XSETTINGS_XFT_DPI) != 0) { |
40 | return; |
41 | } |
42 | |
43 | if (setting->type != XSETTINGS_TYPE_INT) { |
44 | return; |
45 | } |
46 | |
47 | switch (action) { |
48 | case XSETTINGS_ACTION_NEW: |
49 | SDL_FALLTHROUGH; |
50 | case XSETTINGS_ACTION_CHANGED: |
51 | scale_factor = setting->data.v_int; |
52 | if (SDL_strcmp(name, SDL_XSETTINGS_XFT_DPI) == 0) { |
53 | scale_factor = scale_factor / 1024.0f / 96.0f; |
54 | } |
55 | break; |
56 | case XSETTINGS_ACTION_DELETED: |
57 | scale_factor = 1.0; |
58 | break; |
59 | } |
60 | |
61 | if (_this) { |
62 | for (i = 0; i < _this->num_displays; ++i) { |
63 | SDL_SetDisplayContentScale(_this->displays[i], scale_factor); |
64 | } |
65 | } |
66 | } |
67 | |
68 | void X11_InitXsettings(SDL_VideoDevice *_this) |
69 | { |
70 | SDL_VideoData *data = _this->internal; |
71 | SDLX11_SettingsData *xsettings_data = &data->xsettings_data; |
72 | |
73 | xsettings_data->xsettings = xsettings_client_new(data->display, |
74 | DefaultScreen(data->display), X11_XsettingsNotify, NULL, _this); |
75 | |
76 | } |
77 | |
78 | void X11_QuitXsettings(SDL_VideoDevice *_this) |
79 | { |
80 | SDL_VideoData *data = _this->internal; |
81 | SDLX11_SettingsData *xsettings_data = &data->xsettings_data; |
82 | |
83 | if (xsettings_data->xsettings) { |
84 | xsettings_client_destroy(xsettings_data->xsettings); |
85 | xsettings_data->xsettings = NULL; |
86 | } |
87 | } |
88 | |
89 | void X11_HandleXsettings(SDL_VideoDevice *_this, const XEvent *xevent) |
90 | { |
91 | SDL_VideoData *data = _this->internal; |
92 | SDLX11_SettingsData *xsettings_data = &data->xsettings_data; |
93 | |
94 | if (xsettings_data->xsettings) { |
95 | if (!xsettings_client_process_event(xsettings_data->xsettings, xevent)) { |
96 | xsettings_client_destroy(xsettings_data->xsettings); |
97 | xsettings_data->xsettings = NULL; |
98 | } |
99 | } |
100 | } |
101 | |
102 | int X11_GetXsettingsIntKey(SDL_VideoDevice *_this, const char *key, int fallback_value) { |
103 | SDL_VideoData *data = _this->internal; |
104 | SDLX11_SettingsData *xsettings_data = &data->xsettings_data; |
105 | XSettingsSetting *setting = NULL; |
106 | int res = fallback_value; |
107 | |
108 | |
109 | if (xsettings_data->xsettings) { |
110 | if (xsettings_client_get_setting(xsettings_data->xsettings, key, &setting) != XSETTINGS_SUCCESS) { |
111 | goto no_key; |
112 | } |
113 | |
114 | if (setting->type != XSETTINGS_TYPE_INT) { |
115 | goto no_key; |
116 | } |
117 | |
118 | res = setting->data.v_int; |
119 | } |
120 | |
121 | no_key: |
122 | if (setting) { |
123 | xsettings_setting_free(setting); |
124 | } |
125 | |
126 | return res; |
127 | } |
128 | |
129 | #endif // SDL_VIDEO_DRIVER_X11 |
130 | |