Skip to content

Commit

Permalink
Merge pull request #93072 from bruvzg/x11_screen_cap
Browse files Browse the repository at this point in the history
[X11] Detect XWayland and disable screen capture support.
  • Loading branch information
akien-mga committed Jun 12, 2024
2 parents d7302a2 + 3cabf51 commit 5e2bdd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
31 changes: 28 additions & 3 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ bool DisplayServerX11::has_feature(Feature p_feature) const {
#endif
case FEATURE_CLIPBOARD_PRIMARY:
case FEATURE_TEXT_TO_SPEECH:
case FEATURE_SCREEN_CAPTURE:
return true;
case FEATURE_SCREEN_CAPTURE:
return !xwayland;
default: {
}
}
Expand Down Expand Up @@ -1494,9 +1495,20 @@ int DisplayServerX11::screen_get_dpi(int p_screen) const {
return 96;
}

int get_image_errorhandler(Display *dpy, XErrorEvent *ev) {
return 0;
}

Color DisplayServerX11::screen_get_pixel(const Point2i &p_position) const {
Point2i pos = p_position;

if (xwayland) {
return Color();
}

int (*old_handler)(Display *, XErrorEvent *) = XSetErrorHandler(&get_image_errorhandler);

Color color;
int number_of_screens = XScreenCount(x11_display);
for (int i = 0; i < number_of_screens; i++) {
Window root = XRootWindow(x11_display, i);
Expand All @@ -1509,12 +1521,15 @@ Color DisplayServerX11::screen_get_pixel(const Point2i &p_position) const {
c.pixel = XGetPixel(image, 0, 0);
XFree(image);
XQueryColor(x11_display, XDefaultColormap(x11_display, i), &c);
return Color(float(c.red) / 65535.0, float(c.green) / 65535.0, float(c.blue) / 65535.0, 1.0);
color = Color(float(c.red) / 65535.0, float(c.green) / 65535.0, float(c.blue) / 65535.0, 1.0);
break;
}
}
}

return Color();
XSetErrorHandler(old_handler);

return color;
}

Ref<Image> DisplayServerX11::screen_get_image(int p_screen) const {
Expand All @@ -1533,6 +1548,12 @@ Ref<Image> DisplayServerX11::screen_get_image(int p_screen) const {

ERR_FAIL_COND_V(p_screen < 0, Ref<Image>());

if (xwayland) {
return Ref<Image>();
}

int (*old_handler)(Display *, XErrorEvent *) = XSetErrorHandler(&get_image_errorhandler);

XImage *image = nullptr;

bool found = false;
Expand Down Expand Up @@ -1575,6 +1596,8 @@ Ref<Image> DisplayServerX11::screen_get_image(int p_screen) const {
}
}

XSetErrorHandler(old_handler);

Ref<Image> img;
if (image) {
int width = image->width;
Expand Down Expand Up @@ -5815,6 +5838,8 @@ static ::XIMStyle _get_best_xim_style(const ::XIMStyle &p_style_a, const ::XIMSt
DisplayServerX11::DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, Error &r_error) {
KeyMappingX11::initialize();

xwayland = OS::get_singleton()->get_environment("XDG_SESSION_TYPE").to_lower() == "wayland";

native_menu = memnew(NativeMenu);
context = p_context;

Expand Down
1 change: 1 addition & 0 deletions platform/linuxbsd/x11/display_server_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ class DisplayServerX11 : public DisplayServer {
bool xrandr_ext_ok = true;
bool xinerama_ext_ok = true;
bool xshaped_ext_ok = true;
bool xwayland = false;

struct Property {
unsigned char *data;
Expand Down

0 comments on commit 5e2bdd7

Please sign in to comment.