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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
#include "../include/damage.h"
#include "../include/utils.h"
#include <stdio.h>
#include <string.h>
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/Xrandr.h>
typedef struct {
vec2i pos;
vec2i size;
} gsr_rectangle;
static bool rectangles_intersect(gsr_rectangle rect1, gsr_rectangle rect2) {
return rect1.pos.x < rect2.pos.x + rect2.size.x && rect1.pos.x + rect1.size.x > rect2.pos.x &&
rect1.pos.y < rect2.pos.y + rect2.size.y && rect1.pos.y + rect1.size.y > rect2.pos.y;
}
static bool xrandr_is_supported(Display *display) {
int major_version = 0;
int minor_version = 0;
if(!XRRQueryVersion(display, &major_version, &minor_version))
return false;
return major_version > 1 || (major_version == 1 && minor_version >= 2);
}
bool gsr_damage_init(gsr_damage *self, gsr_egl *egl, bool track_cursor) {
memset(self, 0, sizeof(*self));
self->egl = egl;
self->track_cursor = track_cursor;
if(gsr_egl_get_display_server(egl) != GSR_DISPLAY_SERVER_X11) {
fprintf(stderr, "gsr warning: gsr_damage_init: damage tracking is not supported on wayland\n");
return false;
}
if(!XDamageQueryExtension(self->egl->x11.dpy, &self->damage_event, &self->damage_error)) {
fprintf(stderr, "gsr warning: gsr_damage_init: XDamage is not supported by your X11 server\n");
gsr_damage_deinit(self);
return false;
}
if(!XRRQueryExtension(self->egl->x11.dpy, &self->randr_event, &self->randr_error)) {
fprintf(stderr, "gsr warning: gsr_damage_init: XRandr is not supported by your X11 server\n");
gsr_damage_deinit(self);
return false;
}
if(!xrandr_is_supported(self->egl->x11.dpy)) {
fprintf(stderr, "gsr warning: gsr_damage_init: your X11 randr version is too old\n");
gsr_damage_deinit(self);
return false;
}
XRRSelectInput(self->egl->x11.dpy, DefaultRootWindow(self->egl->x11.dpy), RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask | RROutputChangeNotifyMask);
self->damaged = true;
return true;
}
void gsr_damage_deinit(gsr_damage *self) {
if(self->damage) {
XDamageDestroy(self->egl->x11.dpy, self->damage);
self->damage = None;
}
self->damage_event = 0;
self->damage_error = 0;
self->randr_event = 0;
self->randr_error = 0;
}
bool gsr_damage_set_target_window(gsr_damage *self, uint64_t window) {
if(self->damage_event == 0)
return false;
if(window == self->window)
return true;
if(self->damage) {
XDamageDestroy(self->egl->x11.dpy, self->damage);
self->damage = None;
}
if(self->window)
XSelectInput(self->egl->x11.dpy, self->window, 0);
self->window = window;
XSelectInput(self->egl->x11.dpy, self->window, StructureNotifyMask | ExposureMask);
XWindowAttributes win_attr;
win_attr.x = 0;
win_attr.y = 0;
win_attr.width = 0;
win_attr.height = 0;
if(!XGetWindowAttributes(self->egl->x11.dpy, self->window, &win_attr))
fprintf(stderr, "gsr warning: gsr_damage_set_target_window failed: failed to get window attributes: %ld\n", (long)self->window);
//self->window_pos.x = win_attr.x;
//self->window_pos.y = win_attr.y;
self->window_size.x = win_attr.width;
self->window_size.y = win_attr.height;
self->damage = XDamageCreate(self->egl->x11.dpy, window, XDamageReportNonEmpty);
if(self->damage) {
XDamageSubtract(self->egl->x11.dpy, self->damage, None, None);
self->damaged = true;
self->track_type = GSR_DAMAGE_TRACK_WINDOW;
return true;
} else {
fprintf(stderr, "gsr warning: gsr_damage_set_target_window: XDamageCreate failed\n");
self->track_type = GSR_DAMAGE_TRACK_NONE;
return false;
}
}
bool gsr_damage_set_target_monitor(gsr_damage *self, const char *monitor_name) {
if(self->damage_event == 0)
return false;
if(strcmp(self->monitor_name, monitor_name) == 0)
return true;
if(self->damage) {
XDamageDestroy(self->egl->x11.dpy, self->damage);
self->damage = None;
}
memset(&self->monitor, 0, sizeof(self->monitor));
if(!get_monitor_by_name(self->egl, GSR_CONNECTION_X11, monitor_name, &self->monitor))
fprintf(stderr, "gsr warning: gsr_damage_set_target_monitor: failed to find monitor: %s\n", monitor_name);
if(self->window)
XSelectInput(self->egl->x11.dpy, self->window, 0);
self->window = DefaultRootWindow(self->egl->x11.dpy);
self->damage = XDamageCreate(self->egl->x11.dpy, self->window, XDamageReportNonEmpty);
if(self->damage) {
XDamageSubtract(self->egl->x11.dpy, self->damage, None, None);
self->damaged = true;
snprintf(self->monitor_name, sizeof(self->monitor_name), "%s", monitor_name);
self->track_type = GSR_DAMAGE_TRACK_MONITOR;
return true;
} else {
fprintf(stderr, "gsr warning: gsr_damage_set_target_monitor: XDamageCreate failed\n");
self->track_type = GSR_DAMAGE_TRACK_NONE;
return false;
}
}
static void gsr_damage_on_crtc_change(gsr_damage *self, XEvent *xev) {
const XRRCrtcChangeNotifyEvent *rr_crtc_change_event = (XRRCrtcChangeNotifyEvent*)xev;
if(rr_crtc_change_event->crtc == 0 || self->monitor.monitor_identifier == 0)
return;
if(rr_crtc_change_event->crtc != self->monitor.monitor_identifier)
return;
if(rr_crtc_change_event->width == 0 || rr_crtc_change_event->height == 0)
return;
if(rr_crtc_change_event->x != self->monitor.pos.x || rr_crtc_change_event->y != self->monitor.pos.y ||
(int)rr_crtc_change_event->width != self->monitor.size.x || (int)rr_crtc_change_event->height != self->monitor.size.y) {
self->monitor.pos.x = rr_crtc_change_event->x;
self->monitor.pos.y = rr_crtc_change_event->y;
self->monitor.size.x = rr_crtc_change_event->width;
self->monitor.size.y = rr_crtc_change_event->height;
}
}
static void gsr_damage_on_output_change(gsr_damage *self, XEvent *xev) {
const XRROutputChangeNotifyEvent *rr_output_change_event = (XRROutputChangeNotifyEvent*)xev;
if(!rr_output_change_event->output || self->monitor.monitor_identifier == 0)
return;
XRRScreenResources *screen_res = XRRGetScreenResources(self->egl->x11.dpy, DefaultRootWindow(self->egl->x11.dpy));
if(!screen_res)
return;
XRROutputInfo *out_info = XRRGetOutputInfo(self->egl->x11.dpy, screen_res, rr_output_change_event->output);
if(out_info && out_info->crtc && out_info->crtc == self->monitor.monitor_identifier) {
XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(self->egl->x11.dpy, screen_res, out_info->crtc);
if(crtc_info && (crtc_info->x != self->monitor.pos.x || crtc_info->y != self->monitor.pos.y ||
(int)crtc_info->width != self->monitor.size.x || (int)crtc_info->height != self->monitor.size.y))
{
self->monitor.pos.x = crtc_info->x;
self->monitor.pos.y = crtc_info->y;
self->monitor.size.x = crtc_info->width;
self->monitor.size.y = crtc_info->height;
}
if(crtc_info)
XRRFreeCrtcInfo(crtc_info);
}
if(out_info)
XRRFreeOutputInfo(out_info);
XRRFreeScreenResources(screen_res);
}
static void gsr_damage_on_randr_event(gsr_damage *self, XEvent *xev) {
const XRRNotifyEvent *rr_event = (XRRNotifyEvent*)xev;
switch(rr_event->subtype) {
case RRNotify_CrtcChange:
gsr_damage_on_crtc_change(self, xev);
break;
case RRNotify_OutputChange:
gsr_damage_on_output_change(self, xev);
break;
}
}
static void gsr_damage_on_damage_event(gsr_damage *self, XEvent *xev) {
const XDamageNotifyEvent *de = (XDamageNotifyEvent*)xev;
XserverRegion region = XFixesCreateRegion(self->egl->x11.dpy, NULL, 0);
/* Subtract all the damage, repairing the window */
XDamageSubtract(self->egl->x11.dpy, de->damage, None, region);
if(self->track_type == GSR_DAMAGE_TRACK_WINDOW || (self->track_type == GSR_DAMAGE_TRACK_MONITOR && self->monitor.connector_id == 0)) {
self->damaged = true;
} else {
int num_rectangles = 0;
XRectangle *rectangles = XFixesFetchRegion(self->egl->x11.dpy, region, &num_rectangles);
if(rectangles) {
const gsr_rectangle monitor_region = { self->monitor.pos, self->monitor.size };
for(int i = 0; i < num_rectangles; ++i) {
const gsr_rectangle damage_region = { (vec2i){rectangles[i].x, rectangles[i].y}, (vec2i){rectangles[i].width, rectangles[i].height} };
self->damaged = rectangles_intersect(monitor_region, damage_region);
if(self->damaged)
break;
}
XFree(rectangles);
}
}
XFixesDestroyRegion(self->egl->x11.dpy, region);
XFlush(self->egl->x11.dpy);
}
static void gsr_damage_on_event_cursor(gsr_damage *self) {
Window root_return = None;
Window child_return = None;
int dummy_i;
unsigned int dummy_u;
vec2i cursor_position = {0, 0};
XQueryPointer(self->egl->x11.dpy, self->window, &root_return, &child_return, &dummy_i, &dummy_i, &cursor_position.x, &cursor_position.y, &dummy_u);
if(cursor_position.x != self->cursor_position.x || cursor_position.y != self->cursor_position.y) {
self->cursor_position = cursor_position;
const gsr_rectangle cursor_region = { self->cursor_position, {64, 64} }; // TODO: Track cursor size
switch(self->track_type) {
case GSR_DAMAGE_TRACK_NONE: {
self->damaged = true;
break;
}
case GSR_DAMAGE_TRACK_WINDOW: {
const gsr_rectangle window_region = { (vec2i){0, 0}, self->window_size };
self->damaged = self->window_size.x == 0 || rectangles_intersect(window_region, cursor_region);
break;
}
case GSR_DAMAGE_TRACK_MONITOR: {
const gsr_rectangle monitor_region = { self->monitor.pos, self->monitor.size };
self->damaged = self->monitor.monitor_identifier == 0 || rectangles_intersect(monitor_region, cursor_region);
break;
}
}
}
}
static void gsr_damage_on_window_configure_notify(gsr_damage *self, XEvent *xev) {
if(xev->xconfigure.window != self->window)
return;
//self->window_pos.x = xev->xconfigure.x;
//self->window_pos.y = xev->xconfigure.y;
self->window_size.x = xev->xconfigure.width;
self->window_size.y = xev->xconfigure.height;
}
void gsr_damage_on_event(gsr_damage *self, XEvent *xev) {
if(self->damage_event == 0 || self->track_type == GSR_DAMAGE_TRACK_NONE)
return;
if(self->track_type == GSR_DAMAGE_TRACK_WINDOW && xev->type == ConfigureNotify)
gsr_damage_on_window_configure_notify(self, xev);
if(self->randr_event) {
if(xev->type == self->randr_event + RRScreenChangeNotify)
XRRUpdateConfiguration(xev);
if(xev->type == self->randr_event + RRNotify)
gsr_damage_on_randr_event(self, xev);
}
if(self->damage_event && xev->type == self->damage_event + XDamageNotify)
gsr_damage_on_damage_event(self, xev);
}
void gsr_damage_tick(gsr_damage *self) {
if(self->damage_event == 0 || self->track_type == GSR_DAMAGE_TRACK_NONE)
return;
if(self->track_cursor && !self->damaged)
gsr_damage_on_event_cursor(self);
}
bool gsr_damage_is_damaged(gsr_damage *self) {
return self->damage_event == 0 || !self->damage || self->damaged || self->track_type == GSR_DAMAGE_TRACK_NONE;
}
void gsr_damage_clear(gsr_damage *self) {
self->damaged = false;
}
|