1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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) && defined(SDL_VIDEO_DRIVER_X11_XSYNC)
25
26#include "SDL_x11video.h"
27#include "SDL_x11xsync.h"
28
29static bool xsync_initialized = false;
30
31static int query_xsync_version(Display *display, int major, int minor)
32{
33 /* We don't care if this fails, so long as it sets major/minor on it's way out the door. */
34 X11_XSyncInitialize(display, &major, &minor);
35 return (major * 1000) + minor;
36}
37
38static bool xsync_version_atleast(const int version, const int wantmajor, const int wantminor)
39{
40 return version >= ((wantmajor * 1000) + wantminor);
41}
42
43void X11_InitXsync(SDL_VideoDevice *_this)
44{
45 SDL_VideoData *data = _this->internal;
46
47 int version = 0;
48 int event, error;
49 int sync_opcode;
50
51 if (!SDL_X11_HAVE_XSYNC ||
52 !X11_XQueryExtension(data->display, "SYNC", &sync_opcode, &event, &error)) {
53 return;
54 }
55
56 /* We need at least 5.0 for barriers. */
57 version = query_xsync_version(data->display, 5, 0);
58 if (!xsync_version_atleast(version, 3, 0)) {
59 return; /* X server does not support the version we want at all. */
60 }
61
62 xsync_initialized = true;
63}
64
65bool X11_XsyncIsInitialized(void)
66{
67 return xsync_initialized;
68}
69
70bool X11_InitResizeSync(SDL_Window *window)
71{
72 SDL_assert(window != NULL);
73 SDL_WindowData *data = window->internal;
74 Display *display = data->videodata->display;
75 Atom counter_prop = data->videodata->atoms._NET_WM_SYNC_REQUEST_COUNTER;
76 XSyncCounter counter;
77 CARD32 counter_id;
78
79 if (!X11_XsyncIsInitialized()){
80 return SDL_Unsupported();
81 }
82
83 counter = X11_XSyncCreateCounter(display, (XSyncValue){0, 0});
84 data->resize_counter = counter;
85 data->resize_id.lo = 0;
86 data->resize_id.hi = 0;
87 data->resize_in_progress = false;
88
89 if (counter == None){
90 return SDL_Unsupported();
91 }
92
93 counter_id = counter;
94 X11_XChangeProperty(display, data->xwindow, counter_prop, XA_CARDINAL, 32,
95 PropModeReplace, (unsigned char *)&counter_id, 1);
96
97 return true;
98}
99
100void X11_TermResizeSync(SDL_Window *window)
101{
102 SDL_WindowData *data = window->internal;
103 Display *display = data->videodata->display;
104 Atom counter_prop = data->videodata->atoms._NET_WM_SYNC_REQUEST_COUNTER;
105 XSyncCounter counter = data->resize_counter;
106
107 X11_XDeleteProperty(display, data->xwindow, counter_prop);
108 if (counter != None) {
109 X11_XSyncDestroyCounter(display, counter);
110 }
111}
112
113void X11_HandleSyncRequest(SDL_Window *window, XClientMessageEvent *event)
114{
115 SDL_WindowData *data = window->internal;
116
117 data->resize_id.lo = event->data.l[2];
118 data->resize_id.hi = event->data.l[3];
119 data->resize_in_progress = false;
120}
121
122void X11_HandleConfigure(SDL_Window *window, XConfigureEvent *event)
123{
124 SDL_WindowData *data = window->internal;
125
126 if (data->resize_id.lo || data->resize_id.hi) {
127 data->resize_in_progress = true;
128 }
129}
130
131void X11_HandlePresent(SDL_Window *window)
132{
133 SDL_WindowData *data = window->internal;
134 Display *display = data->videodata->display;
135 XSyncCounter counter = data->resize_counter;
136
137 if ((counter == None) || (!data->resize_in_progress)) {
138 return;
139 }
140
141 X11_XSyncSetCounter(display, counter, data->resize_id);
142
143 data->resize_id.lo = 0;
144 data->resize_id.hi = 0;
145 data->resize_in_progress = false;
146}
147
148#endif // SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_DRIVER_X11_XSYNC
149