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

Code before:
		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);
		}
	} else {
		int count = XScreenCount(x11_display);
		if (p_screen < count) {
			Window root = XRootWindow(x11_display, p_screen);
			XWindowAttributes xwa;
			XGetWindowAttributes(x11_display, root, &xwa);
			rect.position.x = xwa.x;
			rect.position.y = xwa.y;
			rect.size.width = xwa.width;
			rect.size.height = xwa.height;

Review comment (suggestion):
```suggestion
			// Initial response is the lower bound of the length of the transferred data.
```

Code after:
		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;
				while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_incr, (XPointer)&transfer_prop)) {
					result = XGetWindowProperty(x11_display, x11_window,
							transfer_prop, // output property
							0, LONG_MAX, // offset - len
							True, // delete property to notify the owner
							AnyPropertyType, // flag
							&type, // return type
							&format, // return format
							&len, &bytes_left, // data length
							&data);