Repository: godotengine/godot
File: platform/linuxbsd/x11/display_server_x11.cpp (C++)

Code before:
// It is not exposed through the API, accesses X11 internals,
// and is much more complex, so this is a less complete simplified error X11 printer.
int default_window_error_handler(Display *display, XErrorEvent *error) {
	static char message[1024];
	XGetErrorText(display, error->error_code, message, sizeof(message));

	ERR_PRINT(vformat("Unhandled XServer error: %s"
					  "\n   Major opcode of failed request: %d"
					  "\n   Serial number of failed request: %d"
					  "\n   Current serial number in output stream: %d",
			String::utf8(message), (uint64_t)error->request_code, (uint64_t)error->minor_code, (uint64_t)error->serial));
	return 0;
}

bool g_bad_window = false;
int bad_window_error_handler(Display *display, XErrorEvent *error) {
	if (error->error_code == BadWindow) {
		g_bad_window = true;
	} else {
		return default_window_error_handler(display, error);
	}
	return 0;
}

Rect2i DisplayServerX11::screen_get_usable_rect(int p_screen) const {
	_THREAD_SAFE_METHOD_

	p_screen = _get_screen_index(p_screen);
	int screen_count = get_screen_count();

	// Check if screen is valid.
	ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i(0, 0, 0, 0));

	bool is_multiscreen = screen_count > 1;

	// Use full monitor size as fallback.
	Rect2i rect = _screen_get_rect(p_screen);

	// There's generally only one screen reported by xlib even in multi-screen setup,
	// in this case it's just one virtual screen composed of all physical monitors.
	int x11_screen_count = ScreenCount(x11_display);
	Window x11_window = RootWindow(x11_display, p_screen < x11_screen_count ? p_screen : 0);

	Atom type;
	int format = 0;
	unsigned long remaining = 0;

	// Find active desktop for the root window.
	unsigned int desktop_index = 0;
	Atom desktop_prop = XInternAtom(x11_display, "_NET_CURRENT_DESKTOP", True);
	if (desktop_prop != None) {

Review comment (suggestion):
```suggestion
				PNGDriverCommon::png_to_image((uint8_t *)data, bytes_left, false, ret);
```

Code after:
					}

					if (done) {
						break;
					}
				}
			}

			if (success && (data_size > 0)) {
				ret.instantiate();
				PNGDriverCommon::png_to_image(incr_data.ptr(), incr_data.size(), false, ret);
			}
		} else if (bytes_left > 0) {
			if (data) {
				XFree(data);
				data = nullptr;
			}
			// Data is ready and can be processed all at once.
			result = XGetWindowProperty(x11_display, x11_window,
					transfer_prop, 0, bytes_left + 4, 0,
					AnyPropertyType, &type, &format,
					&len, &dummy, &data);
			if (result == Success) {
				ret.instantiate();
				PNGDriverCommon::png_to_image((uint8_t *)data, bytes_left, false, ret);
			} else {
				print_verbose("Failed to get selection data.");
			}

			if (data) {
				XFree(data);
			}
		}
	}

	return ret;
}

bool DisplayServerX11::clipboard_has_image() const {
	Atom target = _clipboard_get_image_target(
			XInternAtom(x11_display, "CLIPBOARD", 0),
			windows[MAIN_WINDOW_ID].x11_window);
	return target != None;
}

Bool DisplayServerX11::_predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg) {
	if (event->xany.window == *(Window *)arg) {
		return (event->type == SelectionRequest) ||
				(event->type == SelectionNotify);
	} else {
		return False;