1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#if defined(SDL_VIDEO_DRIVER_X11) && defined(SDL_VIDEO_DRIVER_X11_XSYNC)
#include "SDL_x11video.h"
#include "SDL_x11xsync.h"
static int xsync_initialized = 0;
static int query_xsync_version(Display *display, int major, int minor)
{
/* We don't care if this fails, so long as it sets major/minor on it's way out the door. */
X11_XSyncInitialize(display, &major, &minor);
return (major * 1000) + minor;
}
static bool xsync_version_atleast(const int version, const int wantmajor, const int wantminor)
{
return version >= ((wantmajor * 1000) + wantminor);
}
void X11_InitXsync(SDL_VideoDevice *_this)
{
SDL_VideoData *data = _this->internal;
int version = 0;
int event, error;
int sync_opcode;
if (!SDL_X11_HAVE_XSYNC ||
!X11_XQueryExtension(data->display, "SYNC", &sync_opcode, &event, &error)) {
return;
}
/* We need at least 5.0 for barriers. */
version = query_xsync_version(data->display, 5, 0);
if (!xsync_version_atleast(version, 3, 0)) {
return; /* X server does not support the version we want at all. */
}
xsync_initialized = 1;
}
int X11_XsyncIsInitialized(void)
{
return xsync_initialized;
}
int X11_InitResizeSync(SDL_Window *window)
{
SDL_assert(window != NULL);
SDL_WindowData *data = window->internal;
Display *display = data->videodata->display;
Atom counter_prop = data->videodata->atoms._NET_WM_SYNC_REQUEST_COUNTER;
XSyncCounter counter;
CARD32 counter_id;
if (!X11_XsyncIsInitialized()){
return SDL_Unsupported();
}
counter = X11_XSyncCreateCounter(display, (XSyncValue){0, 0});
data->resize_counter = counter;
data->resize_id.lo = 0;
data->resize_id.hi = 0;
data->resize_in_progress = false;
if (counter == None){
return SDL_Unsupported();
}
counter_id = counter;
X11_XChangeProperty(display, data->xwindow, counter_prop, XA_CARDINAL, 32,
PropModeReplace, (unsigned char *)&counter_id, 1);
return 0;
}
void X11_TermResizeSync(SDL_Window *window)
{
SDL_WindowData *data = window->internal;
Display *display = data->videodata->display;
Atom counter_prop = data->videodata->atoms._NET_WM_SYNC_REQUEST_COUNTER;
XSyncCounter counter = data->resize_counter;
X11_XDeleteProperty(display, data->xwindow, counter_prop);
if (counter != None) {
X11_XSyncDestroyCounter(display, counter);
}
}
void X11_HandleSyncRequest(SDL_Window *window, XClientMessageEvent *event)
{
SDL_WindowData *data = window->internal;
data->resize_id.lo = event->data.l[2];
data->resize_id.hi = event->data.l[3];
data->resize_in_progress = false;
}
void X11_HandleConfigure(SDL_Window *window, XConfigureEvent *event)
{
SDL_WindowData *data = window->internal;
if (data->resize_id.lo || data->resize_id.hi) {
data->resize_in_progress = true;
}
}
void X11_HandlePresent(SDL_Window *window)
{
SDL_WindowData *data = window->internal;
Display *display = data->videodata->display;
XSyncCounter counter = data->resize_counter;
if ((counter == None) || (!data->resize_in_progress)) {
return;
}
X11_XSyncSetCounter(display, counter, data->resize_id);
data->resize_id.lo = 0;
data->resize_id.hi = 0;
data->resize_in_progress = false;
}
#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_DRIVER_X11_XSYNC */
|