Use x11 event loop with XPending instead of XCheckTypedWindowEvent

This commit is contained in:
dec05eba 2023-04-21 20:23:11 +02:00
parent beeb9a57aa
commit b6227c07f2
2 changed files with 87 additions and 41 deletions

View File

@ -278,32 +278,55 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v
cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT); cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT);
if(!cap_xcomp->params.follow_focused && XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, DestroyNotify, &cap_xcomp->xev)) { bool init_new_window = false;
while(XPending(cap_xcomp->dpy)) {
XNextEvent(cap_xcomp->dpy, &cap_xcomp->xev);
switch(cap_xcomp->xev.type) {
case DestroyNotify: {
/* Window died (when not following focused window), so we stop recording */
if(!cap_xcomp->params.follow_focused && cap_xcomp->xev.xdestroywindow.window == cap_xcomp->window) {
cap_xcomp->should_stop = true; cap_xcomp->should_stop = true;
cap_xcomp->stop_is_error = false; cap_xcomp->stop_is_error = false;
} }
break;
if(XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, Expose, &cap_xcomp->xev) && cap_xcomp->xev.xexpose.count == 0) { }
case Expose: {
/* Requires window texture recreate */
if(cap_xcomp->xev.xexpose.count == 0 && cap_xcomp->xev.xexpose.window == cap_xcomp->window) {
cap_xcomp->window_resize_timer = clock_get_monotonic_seconds(); cap_xcomp->window_resize_timer = clock_get_monotonic_seconds();
cap_xcomp->window_resized = true; cap_xcomp->window_resized = true;
} }
break;
if(XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, ConfigureNotify, &cap_xcomp->xev) && cap_xcomp->xev.xconfigure.window == cap_xcomp->window) { }
while(XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, ConfigureNotify, &cap_xcomp->xev)) {} case ConfigureNotify: {
/* Window resized */
/* Window resize */ if(cap_xcomp->xev.xconfigure.window == cap_xcomp->window && (cap_xcomp->xev.xconfigure.width != cap_xcomp->window_size.x || cap_xcomp->xev.xconfigure.height != cap_xcomp->window_size.y)) {
if(cap_xcomp->xev.xconfigure.width != cap_xcomp->window_size.x || cap_xcomp->xev.xconfigure.height != cap_xcomp->window_size.y) {
cap_xcomp->window_size.x = max_int(cap_xcomp->xev.xconfigure.width, 0); cap_xcomp->window_size.x = max_int(cap_xcomp->xev.xconfigure.width, 0);
cap_xcomp->window_size.y = max_int(cap_xcomp->xev.xconfigure.height, 0); cap_xcomp->window_size.y = max_int(cap_xcomp->xev.xconfigure.height, 0);
cap_xcomp->window_resize_timer = clock_get_monotonic_seconds(); cap_xcomp->window_resize_timer = clock_get_monotonic_seconds();
cap_xcomp->window_resized = true; cap_xcomp->window_resized = true;
} }
break;
}
case PropertyNotify: {
/* Focused window changed */
if(cap_xcomp->params.follow_focused && cap_xcomp->xev.xproperty.atom == cap_xcomp->net_active_window_atom) {
init_new_window = true;
}
break;
}
}
} }
if(cap_xcomp->params.follow_focused && (!cap_xcomp->follow_focused_initialized || (XCheckTypedWindowEvent(cap_xcomp->dpy, DefaultRootWindow(cap_xcomp->dpy), PropertyNotify, &cap_xcomp->xev) && cap_xcomp->xev.xproperty.atom == cap_xcomp->net_active_window_atom))) { if(cap_xcomp->params.follow_focused && !cap_xcomp->follow_focused_initialized) {
Window focused_window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom);
if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) {
cap_xcomp->follow_focused_initialized = true; cap_xcomp->follow_focused_initialized = true;
init_new_window = true;
}
if(init_new_window) {
Window focused_window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom);
if(focused_window != cap_xcomp->window) {
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, 0); XSelectInput(cap_xcomp->dpy, cap_xcomp->window, 0);
cap_xcomp->window = focused_window; cap_xcomp->window = focused_window;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask);

View File

@ -207,32 +207,55 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext *
// TODO: // TODO:
//cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT); //cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT);
if(!cap_xcomp->params.follow_focused && XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, DestroyNotify, &cap_xcomp->xev)) { bool init_new_window = false;
while(XPending(cap_xcomp->dpy)) {
XNextEvent(cap_xcomp->dpy, &cap_xcomp->xev);
switch(cap_xcomp->xev.type) {
case DestroyNotify: {
/* Window died (when not following focused window), so we stop recording */
if(!cap_xcomp->params.follow_focused && cap_xcomp->xev.xdestroywindow.window == cap_xcomp->window) {
cap_xcomp->should_stop = true; cap_xcomp->should_stop = true;
cap_xcomp->stop_is_error = false; cap_xcomp->stop_is_error = false;
} }
break;
if(XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, Expose, &cap_xcomp->xev) && cap_xcomp->xev.xexpose.count == 0) { }
case Expose: {
/* Requires window texture recreate */
if(cap_xcomp->xev.xexpose.count == 0 && cap_xcomp->xev.xexpose.window == cap_xcomp->window) {
cap_xcomp->window_resize_timer = clock_get_monotonic_seconds(); cap_xcomp->window_resize_timer = clock_get_monotonic_seconds();
cap_xcomp->window_resized = true; cap_xcomp->window_resized = true;
} }
break;
if(XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, ConfigureNotify, &cap_xcomp->xev) && cap_xcomp->xev.xconfigure.window == cap_xcomp->window) { }
while(XCheckTypedWindowEvent(cap_xcomp->dpy, cap_xcomp->window, ConfigureNotify, &cap_xcomp->xev)) {} case ConfigureNotify: {
/* Window resized */
/* Window resize */ if(cap_xcomp->xev.xconfigure.window == cap_xcomp->window && (cap_xcomp->xev.xconfigure.width != cap_xcomp->window_size.x || cap_xcomp->xev.xconfigure.height != cap_xcomp->window_size.y)) {
if(cap_xcomp->xev.xconfigure.width != cap_xcomp->window_size.x || cap_xcomp->xev.xconfigure.height != cap_xcomp->window_size.y) {
cap_xcomp->window_size.x = max_int(cap_xcomp->xev.xconfigure.width, 0); cap_xcomp->window_size.x = max_int(cap_xcomp->xev.xconfigure.width, 0);
cap_xcomp->window_size.y = max_int(cap_xcomp->xev.xconfigure.height, 0); cap_xcomp->window_size.y = max_int(cap_xcomp->xev.xconfigure.height, 0);
cap_xcomp->window_resize_timer = clock_get_monotonic_seconds(); cap_xcomp->window_resize_timer = clock_get_monotonic_seconds();
cap_xcomp->window_resized = true; cap_xcomp->window_resized = true;
} }
break;
}
case PropertyNotify: {
/* Focused window changed */
if(cap_xcomp->params.follow_focused && cap_xcomp->xev.xproperty.atom == cap_xcomp->net_active_window_atom) {
init_new_window = true;
}
break;
}
}
} }
if(cap_xcomp->params.follow_focused && (!cap_xcomp->follow_focused_initialized || (XCheckTypedWindowEvent(cap_xcomp->dpy, DefaultRootWindow(cap_xcomp->dpy), PropertyNotify, &cap_xcomp->xev) && cap_xcomp->xev.xproperty.atom == cap_xcomp->net_active_window_atom))) { if(cap_xcomp->params.follow_focused && !cap_xcomp->follow_focused_initialized) {
cap_xcomp->follow_focused_initialized = true;
init_new_window = true;
}
if(init_new_window) {
Window focused_window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom); Window focused_window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom);
if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) { if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) {
cap_xcomp->follow_focused_initialized = true;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, 0); XSelectInput(cap_xcomp->dpy, cap_xcomp->window, 0);
cap_xcomp->window = focused_window; cap_xcomp->window = focused_window;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask);