Debian Codegraph @

gtk/gtkapplication-win32.c

raw symbols SCIP
1 /*
2 * GtkApplication implementation for Win32.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 * SPDX-FileCopyrightText: 2025-2026 GNOME Foundation
6 *
7 * ## LOGOFF inhibition
8 * https://learn.microsoft.com/en-us/windows/win32/shutdown/logging-off
9 * It seems that only visible windows are able to block logout requests,
10 * so delegate the events handling to the HWND of each application window.
11 * Requests counters are managed on GdkWin32 side.
12 * Because we need to track added and removed windows, only windows attached
13 * to the application are supported.
14 * Using SetProcessShutdownParameters() with higher priority will ensure the
15 * mainloop catches and responds to session events before all other HWNDs.
16 *
17 * ## IDLE and SUSPEND inhibition
18 * https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-powersetrequest
19 * Requests counters are automatically managed by PowerSetRequest().
20 * It could be possible to use the old SetThreadExecutionState() API instead,
21 * but this one needs to be called periodically.
22 * The underlying Win32 APIs need a reason string, if not provided the
23 * inhibition will be ignored.
24 * Note that power requests will be cancelled if the user manually stops them,
25 * e.g. by sleeping from the start menu or by closing the lid.
26 */
27
28 #define WIN32_LEAN_AND_MEAN
29
30 #include "config.h"
31
32 #include "gtkapplicationprivate.h"
33 #include "gtknative.h"
34
35 #include "win32/gdkprivate-win32.h"
36
37 #include <windows.h>
38
39
40 typedef GtkApplicationImplClass GtkApplicationImplWin32Class;
41
42 typedef struct
43 {
44 GtkApplicationImpl impl;
45 GSList *inhibitors;
46 guint next_cookie;
47 } GtkApplicationImplWin32;
48
49 typedef struct
50 {
51 guint cookie;
52 GtkApplicationInhibitFlags flags;
53 HANDLE pwr_handle;
54 GdkSurface *surface;
55 } GtkApplicationWin32Inhibitor;
56
57 G_DEFINE_TYPE (GtkApplicationImplWin32, gtk_application_impl_win32, GTK_TYPE_APPLICATION_IMPL)
58
59
60 static void
61 appwin32_inhibitor_free (GtkApplicationWin32Inhibitor *inhibitor)
62 {
63 CloseHandle (inhibitor->pwr_handle);
64 g_clear_object (&inhibitor->surface);
65 g_free (inhibitor);
66 }
67
68 static void
69 cb_appwin32_session_query_end (void)
70 {
71 GApplication *application = g_application_get_default ();
72
73 if (GTK_IS_APPLICATION (application))
74 g_signal_emit_by_name (application, "query-end");
75 }
76
77 static void
78 cb_appwin32_session_end (void)
79 {
80 GApplication *application = g_application_get_default ();
81
82 if (G_IS_APPLICATION (application))
83 g_application_quit (application);
84 }
85
86 static void
87 gtk_application_impl_win32_shutdown (GtkApplicationImpl *impl)
88 {
89 GtkApplicationImplWin32 *appwin32 = (GtkApplicationImplWin32 *) impl;
90 GList *iter;
91
92 for (iter = gtk_application_get_windows (impl->application); iter; iter = iter->next)
93 {
94 if (!gtk_widget_get_realized (GTK_WIDGET (iter->data)))
95 continue;
96
97 gdk_win32_surface_set_session_callbacks (gtk_native_get_surface (GTK_NATIVE (iter->data)),
98 NULL,
99 NULL);
100 }
101
102 g_slist_free_full (appwin32->inhibitors, (GDestroyNotify) appwin32_inhibitor_free);
103 appwin32->inhibitors = NULL;
104 }
105
106 static void
107 gtk_application_impl_win32_handle_window_realize (GtkApplicationImpl *impl,
108 GtkWindow *window)
109 {
110 g_return_if_fail (GTK_IS_WINDOW (window));
111
112 gdk_win32_surface_set_session_callbacks (gtk_native_get_surface (GTK_NATIVE (window)),
113 cb_appwin32_session_query_end,
114 cb_appwin32_session_end);
115 }
116
117 static void
118 gtk_application_impl_win32_window_added (GtkApplicationImpl *impl,
119 GtkWindow *window,
120 GVariant *state)
121 {
122 g_return_if_fail (GTK_IS_WINDOW (window));
123
124 if (!gtk_widget_get_realized (GTK_WIDGET (window)))
125 return; /* no surface yet, wait for handle_window_realize */
126
127 gdk_win32_surface_set_session_callbacks (gtk_native_get_surface (GTK_NATIVE (window)),
128 cb_appwin32_session_query_end,
129 cb_appwin32_session_end);
130 }
131
132 static void
133 gtk_application_impl_win32_window_removed (GtkApplicationImpl *impl,
134 GtkWindow *window)
135 {
136 GtkApplicationImplWin32 *appwin32 = (GtkApplicationImplWin32 *) impl;
137 GdkSurface *surface = gtk_native_get_surface (GTK_NATIVE (window));
138 GSList *iter;
139
140 if (!surface)
141 return;
142
143 gdk_win32_surface_set_session_callbacks (surface,
144 NULL,
145 NULL);
146
147 /* Ensure we don't keep a ref of the surface in the inhibitors,
148 * should the window get removed before uninhibit is called.
149 */
150 for (iter = appwin32->inhibitors; iter; iter = iter->next)
151 {
152 GtkApplicationWin32Inhibitor *inhibitor = iter->data;
153
154 if ((inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT) &&
155 (inhibitor->surface == surface))
156 {
157 gdk_win32_surface_uninhibit_logout (inhibitor->surface);
158 inhibitor->flags &= ~GTK_APPLICATION_INHIBIT_LOGOUT;
159 g_clear_object (&inhibitor->surface);
160 }
161 }
162 }
163
164 static guint
165 gtk_application_impl_win32_inhibit (GtkApplicationImpl *impl,
166 GtkWindow *window,
167 GtkApplicationInhibitFlags flags,
168 const char *reason)
169 {
170 GtkApplicationImplWin32 *appwin32 = (GtkApplicationImplWin32 *) impl;
171 GtkApplicationWin32Inhibitor *inhibitor = g_new0 (GtkApplicationWin32Inhibitor, 1);
172 wchar_t *reason_w = g_utf8_to_utf16 (reason, -1, NULL, NULL, NULL);
173
174 inhibitor->cookie = ++appwin32->next_cookie;
175
176 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
177 {
178 if (GTK_IS_WINDOW (window) &&
179 impl->application == gtk_window_get_application (window))
180 {
181 GdkSurface *surface = gtk_native_get_surface (GTK_NATIVE (window));
182
183 if (gdk_win32_surface_inhibit_logout (surface, reason_w))
184 {
185 inhibitor->surface = g_object_ref (surface);
186 inhibitor->flags |= GTK_APPLICATION_INHIBIT_LOGOUT;
187 }
188 }
189 else
190 g_warning ("Logout inhibition is only supported on application windows");
191 }
192
193 if (flags & (GTK_APPLICATION_INHIBIT_SUSPEND | GTK_APPLICATION_INHIBIT_IDLE))
194 {
195 REASON_CONTEXT context;
196
197 memset (&context, 0, sizeof (context));
198 context.Version = POWER_REQUEST_CONTEXT_VERSION;
199 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
200 context.Reason.SimpleReasonString = reason_w;
201
202 inhibitor->pwr_handle = PowerCreateRequest (&context);
203
204 if (flags & GTK_APPLICATION_INHIBIT_SUSPEND)
205 {
206 if (PowerSetRequest (inhibitor->pwr_handle, PowerRequestSystemRequired))
207 inhibitor->flags |= GTK_APPLICATION_INHIBIT_SUSPEND;
208 else
209 g_warning ("Failed to apply suspend inhibition");
210 }
211
212 if (flags & GTK_APPLICATION_INHIBIT_IDLE)
213 {
214 if (PowerSetRequest (inhibitor->pwr_handle, PowerRequestDisplayRequired))
215 inhibitor->flags |= GTK_APPLICATION_INHIBIT_IDLE;
216 else
217 g_warning ("Failed to apply idle inhibition");
218 }
219 }
220
221 g_free (reason_w);
222
223 if (inhibitor->flags)
224 {
225 appwin32->inhibitors = g_slist_prepend (appwin32->inhibitors, inhibitor);
226 return inhibitor->cookie;
227 }
228
229 appwin32_inhibitor_free (inhibitor);
230 return 0;
231 }
232
233 static void
234 gtk_application_impl_win32_uninhibit (GtkApplicationImpl *impl, guint cookie)
235 {
236 GtkApplicationImplWin32 *appwin32 = (GtkApplicationImplWin32 *) impl;
237 GSList *iter;
238
239 for (iter = appwin32->inhibitors; iter; iter = iter->next)
240 {
241 GtkApplicationWin32Inhibitor *inhibitor = iter->data;
242
243 if (inhibitor->cookie == cookie)
244 {
245 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
246 gdk_win32_surface_uninhibit_logout (inhibitor->surface);
247
248 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_SUSPEND)
249 PowerClearRequest (inhibitor->pwr_handle, PowerRequestSystemRequired);
250
251 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_IDLE)
252 PowerClearRequest (inhibitor->pwr_handle, PowerRequestDisplayRequired);
253
254 appwin32_inhibitor_free (inhibitor);
255 appwin32->inhibitors = g_slist_delete_link (appwin32->inhibitors, iter);
256 return;
257 }
258 }
259
260 g_warning ("Invalid inhibitor cookie: %d", cookie);
261 }
262
263 static gboolean
264 gtk_application_impl_win32_is_inhibited (GtkApplicationImpl *impl,
265 GtkApplicationInhibitFlags flags)
266 {
267 GtkApplicationImplWin32 *appwin32 = (GtkApplicationImplWin32 *) impl;
268 GtkApplicationInhibitFlags active_flags = 0;
269 GSList *iter;
270
271 for (iter = appwin32->inhibitors; iter; iter = iter->next)
272 {
273 GtkApplicationWin32Inhibitor *inhibitor = iter->data;
274 active_flags |= inhibitor->flags;
275 }
276
277 return (active_flags & flags) == flags;
278 }
279
280 static void
281 gtk_application_impl_win32_class_init (GtkApplicationImplClass *klass)
282 {
283 klass->shutdown = gtk_application_impl_win32_shutdown;
284 klass->handle_window_realize = gtk_application_impl_win32_handle_window_realize;
285 klass->window_added = gtk_application_impl_win32_window_added;
286 klass->window_removed = gtk_application_impl_win32_window_removed;
287 klass->inhibit = gtk_application_impl_win32_inhibit;
288 klass->uninhibit = gtk_application_impl_win32_uninhibit;
289 klass->is_inhibited = gtk_application_impl_win32_is_inhibited;
290 }
291
292 static void
293 gtk_application_impl_win32_init (GtkApplicationImplWin32 *appwin32)
294 {
295 DWORD dwLevel, dwFlags;
296
297 appwin32->next_cookie = 0;
298 appwin32->inhibitors = NULL;
299
300 if (GetProcessShutdownParameters (&dwLevel, &dwFlags))
301 {
302 dwLevel = MAX (dwLevel, 0x300);
303 SetProcessShutdownParameters (dwLevel, dwFlags);
304 }
305 }