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

Code before:
		Window focus_child = 0;
		int x = 0, y = 0;
		XTranslateCoordinates(x11_display, focus, DefaultRootWindow(x11_display), 0, 0, &x, &y, &focus_child);

		XWindowAttributes xwa;
		XGetWindowAttributes(x11_display, focus, &xwa);
		Rect2i window_rect = Rect2i(x, y, xwa.width, xwa.height);

		// Find which monitor has the largest overlap with the given window.
		int screen_index = 0;
		int max_area = 0;
		for (int i = 0; i < count; i++) {
			Rect2i screen_rect = _screen_get_rect(i);
			Rect2i intersection = screen_rect.intersection(window_rect);
			int area = intersection.get_area();
			if (area > max_area) {
				max_area = area;
				screen_index = i;
			}
		}
		return screen_index;
	}

	return get_primary_screen();
}

Rect2i DisplayServerX11::_screen_get_rect(int p_screen) const {
	Rect2i rect(0, 0, 0, 0);

	p_screen = _get_screen_index(p_screen);
	ERR_FAIL_COND_V(p_screen < 0, rect);

	// Using Xinerama Extension.
	int event_base, error_base;
	if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) {
		int count;
		XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);

		// Check if screen is valid.
		if (p_screen < count) {
			rect.position.x = xsi[p_screen].x_org;
			rect.position.y = xsi[p_screen].y_org;
			rect.size.width = xsi[p_screen].width;
			rect.size.height = xsi[p_screen].height;
		} else {
			ERR_PRINT("Invalid screen index: " + itos(p_screen) + "(count: " + itos(count) + ").");
		}

		if (xsi) {
			XFree(xsi);
		}

Review comment (bug):
```suggestion
			ERR_FAIL_COND_V_MSG(len != 1, ret, "Incremental transfer initial value was not length.");
```

Code after:
				transfer_prop, // output property
				x11_window, CurrentTime);

		XFlush(x11_display);

		// Blocking wait for predicate to be True and remove the event from the queue.
		XEvent event;
		XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window);

		// Do not get any data, see how much data is there.
		Atom type;
		int format, result;
		unsigned long len, bytes_left, dummy;
		unsigned char *data;
		XGetWindowProperty(x11_display, x11_window,
				transfer_prop, // Property data is transferred through
				0, 1, // offset, len (4 so we can get the size if INCR is used)
				0, // Delete 0==FALSE
				AnyPropertyType, // flag
				&type, // return type
				&format, // return format
				&len, &bytes_left, // data length
				&data);

		if (type == XInternAtom(x11_display, "INCR", 0)) {
			ERR_FAIL_COND_V_MSG(len != 1, ret, "Incremental transfer initial value was not length.");

			// Data is going to be received incrementally.
			DEBUG_LOG_X11("INCR selection started.\n");

			LocalVector<uint8_t> incr_data;
			uint32_t data_size = 0;
			bool success = false;

			// Initial response is the lower bound of the length of the transferred data.
			incr_data.resize(*(unsigned long *)data);
			XFree(data);
			data = nullptr;

			// Delete INCR property to notify the owner.
			XDeleteProperty(x11_display, x11_window, transfer_prop);

			// Process events from the queue.
			bool done = false;
			while (!done) {
				if (!_wait_for_events()) {
					// Error or timeout, abort.
					break;
				}
				// Non-blocking wait for next event and remove it from the queue.
				XEvent ev;