ronantakizawa/codereview-bench huggingface dataset:
Repository: godotengine/godot
File: editor/plugins/path_3d_editor_plugin.cpp (C++)

Code before:
	} else {
		n += " Out";
	}

	return n;
}

Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return Variant();
	}

	if (!p_secondary) {
		original = c->get_point_position(p_id);
		return original;
	}

	// (p_id + 1) Accounts for the first point only having an "out" handle
	int idx = (p_id + 1) / 2;

	Vector3 ofs;
	if (_is_in_handle(p_id, c->get_point_count())) {
		ofs = c->get_point_in(idx);
	} else {
		ofs = c->get_point_out(idx);
	}

	original = ofs + c->get_point_position(idx);

	return ofs;
}

void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return;
	}

	Transform3D gt = path->get_global_transform();
	Transform3D gi = gt.affine_inverse();
	Vector3 ray_from = p_camera->project_ray_origin(p_point);
	Vector3 ray_dir = p_camera->project_ray_normal(p_point);

	// Setting curve point positions
	if (!p_secondary) {
		const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));

		Vector3 inters;

		if (p.intersects_ray(ray_from, ray_dir, &inters)) {

Review comment (suggestion):
```suggestion
	// Primary handles: position.
```

Code after:
		return Variant();
	}

	// Primary handles: position.
	if (!p_secondary) {
		original = c->get_point_position(p_id);
		return original;
	}

	// Secondary handles: in, out, tilt.
	const HandleInfo info = _secondary_handles_info[p_id];
	Vector3 ofs;
	switch (info.type) {
		case HandleType::HANDLE_TYPE_TILT:
			return c->get_point_tilt(info.point_idx);
		case HandleType::HANDLE_TYPE_IN:
			ofs = c->get_point_in(info.point_idx);
			break;
		case HandleType::HANDLE_TYPE_OUT:
			ofs = c->get_point_out(info.point_idx);
			break;
	}

	original = ofs + c->get_point_position(info.point_idx);
	return ofs;
}

void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return;
	}

	const Transform3D gt = path->get_global_transform();
	const Transform3D gi = gt.affine_inverse();
	const Vector3 ray_from = p_camera->project_ray_origin(p_point);
	const Vector3 ray_dir = p_camera->project_ray_normal(p_point);
	const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));

	// Primary handles: position.
	if (!p_secondary) {
		Vector3 inters;
		// Special cas for primary handle, the handle id equals control point id.
		const int idx = p_id;
		if (p.intersects_ray(ray_from, ray_dir, &inters)) {
			if (Node3DEditor::get_singleton()->is_snap_enabled()) {
				float snap = Node3DEditor::get_singleton()->get_translate_snap();
				inters.snap(Vector3(snap, snap, snap));
			}

			Vector3 local = gi.xform(inters);
Repository: godotengine/godot
File: editor/plugins/path_3d_editor_plugin.cpp (C++)

Code before:
#include "node_3d_editor_plugin.h"
#include "scene/gui/menu_button.h"
#include "scene/resources/curve.h"

static bool _is_in_handle(int p_id, int p_num_points) {
	int t = (p_id + 1) % 2;
	int idx = (p_id + 1) / 2;
	// order of points is [out_0, out_1, in_1, out_2, in_2, ... out_n-1, in_n-1, in_n]
	if (idx == 0) {
		return false;
	} else if (idx == (p_num_points - 1)) {
		return true;
	} else {
		return (t == 1);
	}
}

String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return "";
	}

	if (!p_secondary) {
		return TTR("Curve Point #") + itos(p_id);
	}

	// (p_id + 1) Accounts for the first point only having an "out" handle
	int idx = (p_id + 1) / 2;
	String n = TTR("Curve Point #") + itos(idx);
	if (_is_in_handle(p_id, c->get_point_count())) {
		n += " In";
	} else {
		n += " Out";
	}

	return n;
}

Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return Variant();
	}

	if (!p_secondary) {
		original = c->get_point_position(p_id);
		return original;
	}

	// (p_id + 1) Accounts for the first point only having an "out" handle

Review comment (suggestion):
It is redundant to convert the HandleType by index even though the struct has the HandleType. Conversion to arrays makes sense in cases like storing `.tres` where structs cannot be used, but that is not the case here.

Code after:
#include "node_3d_editor_plugin.h"
#include "scene/gui/menu_button.h"
#include "scene/resources/curve.h"

String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return "";
	}

	// Primary handles: position.
	if (!p_secondary) {
		return TTR("Curve Point #") + itos(p_id);
	}

	// Secondary handles: in, out, tilt.
	const HandleInfo info = _secondary_handles_info[p_id];
	switch (info.type) {
		case HandleType::HANDLE_TYPE_IN:
			return TTR("Handle In #") + itos(info.point_idx);
		case HandleType::HANDLE_TYPE_OUT:
			return TTR("Handle Out #") + itos(info.point_idx);
		case HandleType::HANDLE_TYPE_TILT:
			return TTR("Handle Tilt #") + itos(info.point_idx);
	}

	return "";
}

Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
	Ref<Curve3D> c = path->get_curve();
	if (c.is_null()) {
		return Variant();
	}

	// Primary handles: position.
	if (!p_secondary) {
		original = c->get_point_position(p_id);
		return original;
	}

	// Secondary handles: in, out, tilt.
	const HandleInfo info = _secondary_handles_info[p_id];
	Vector3 ofs;
	switch (info.type) {
		case HandleType::HANDLE_TYPE_TILT:
			return c->get_point_tilt(info.point_idx);
		case HandleType::HANDLE_TYPE_IN:
			ofs = c->get_point_in(info.point_idx);
			break;
		case HandleType::HANDLE_TYPE_OUT:
Repository: godotengine/godot
File: editor/plugins/path_3d_editor_plugin.h (C/C++)

Code before:

protected:
	void _notification(int p_what);
	static void _bind_methods();

public:
	Path3D *get_edited_path() { return path; }

	static Path3DEditorPlugin *singleton;
	virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;

	virtual String get_name() const override { return "Path3D"; }
	bool has_main_screen() const override { return false; }
	virtual void edit(Object *p_object) override;
	virtual bool handles(Object *p_object) const override;
	virtual void make_visible(bool p_visible) override;

	bool mirror_angle_enabled() { return mirror_handle_angle; }
	bool mirror_length_enabled() { return mirror_handle_length; }
	bool is_handle_clicked() { return handle_clicked; }
	void set_handle_clicked(bool clicked) { handle_clicked = clicked; }

	Path3DEditorPlugin();
	~Path3DEditorPlugin();
};

#endif // PATH_3D_EDITOR_PLUGIN_H


Review comment (suggestion):
```suggestion
```

If you don't need it, remove it.

Code after:
	Path3D *path = nullptr;

	void _update_theme();

	void _mode_changed(int p_mode);
	void _close_curve();
	void _handle_option_pressed(int p_option);
	bool handle_clicked = false;
	bool mirror_handle_angle;
	bool mirror_handle_length;

	enum HandleOption {
		HANDLE_OPTION_ANGLE,
		HANDLE_OPTION_LENGTH
	};

protected:
	void _notification(int p_what);
	static void _bind_methods();

public:
	Path3D *get_edited_path() { return path; }

	static Path3DEditorPlugin *singleton;
	virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;

	virtual String get_name() const override { return "Path3D"; }
	bool has_main_screen() const override { return false; }
	virtual void edit(Object *p_object) override;
	virtual bool handles(Object *p_object) const override;
	virtual void make_visible(bool p_visible) override;

	bool mirror_angle_enabled() { return mirror_handle_angle; }
	bool mirror_length_enabled() { return mirror_handle_length; }
	bool is_handle_clicked() { return handle_clicked; }
	void set_handle_clicked(bool clicked) { handle_clicked = clicked; }

	Path3DEditorPlugin();
	~Path3DEditorPlugin();
};

#endif // PATH_3D_EDITOR_PLUGIN_H
Repository: godotengine/godot
File: editor/plugins/path_2d_editor_plugin.h (C/C++)

Code before:
		ACTION_NONE,
		ACTION_MOVING_POINT,
		ACTION_MOVING_IN,
		ACTION_MOVING_OUT,
	};

	Action action;
	int action_point = 0;
	Point2 moving_from;
	Point2 moving_screen_from;
	float orig_in_length = 0.0f;
	float orig_out_length = 0.0f;
	Vector2 edge_point;

	void _mode_selected(int p_mode);
	void _handle_option_pressed(int p_option);

	void _node_visibility_changed();
	friend class Path2DEditorPlugin;

protected:
	void _notification(int p_what);
	void _node_removed(Node *p_node);
	static void _bind_methods();

public:
	bool forward_gui_input(const Ref<InputEvent> &p_event);
	void forward_canvas_draw_over_viewport(Control *p_overlay);
	void edit(Node *p_path2d);
	Path2DEditor();
};

class Path2DEditorPlugin : public EditorPlugin {
	GDCLASS(Path2DEditorPlugin, EditorPlugin);

	Path2DEditor *path2d_editor = nullptr;

public:
	virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return path2d_editor->forward_gui_input(p_event); }
	virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { path2d_editor->forward_canvas_draw_over_viewport(p_overlay); }

	virtual String get_name() const override { return "Path2D"; }
	bool has_main_screen() const override { return false; }
	virtual void edit(Object *p_object) override;
	virtual bool handles(Object *p_object) const override;
	virtual void make_visible(bool p_visible) override;

	Path2DEditorPlugin();
	~Path2DEditorPlugin();
};


Review comment (suggestion):
```suggestion
	void restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points);
```

Code after:
		HANDLE_OPTION_ANGLE,
		HANDLE_OPTION_LENGTH,
	};

	enum Action {
		ACTION_NONE,
		ACTION_MOVING_POINT,
		ACTION_MOVING_IN,
		ACTION_MOVING_OUT,
	};

	Action action;
	int action_point = 0;
	Point2 moving_from;
	Point2 moving_screen_from;
	float orig_in_length = 0.0f;
	float orig_out_length = 0.0f;
	Vector2 edge_point;

	void _mode_selected(int p_mode);
	void _handle_option_pressed(int p_option);

	void _node_visibility_changed();

	void _confirm_clear_points();
	void _clear_curve_points(Path2D *p_path2d);
	void _restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points);

protected:
	void _notification(int p_what);
	void _node_removed(Node *p_node);
	static void _bind_methods();

public:
	bool forward_gui_input(const Ref<InputEvent> &p_event);
	void forward_canvas_draw_over_viewport(Control *p_overlay);
	void edit(Node *p_path2d);
	Path2DEditor();
};

class Path2DEditorPlugin : public EditorPlugin {
	GDCLASS(Path2DEditorPlugin, EditorPlugin);

	Path2DEditor *path2d_editor = nullptr;

public:
	virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return path2d_editor->forward_gui_input(p_event); }
	virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { path2d_editor->forward_canvas_draw_over_viewport(p_overlay); }

	virtual String get_name() const override { return "Path2D"; }
	bool has_main_screen() const override { return false; }
Repository: godotengine/godot
File: editor/plugins/path_2d_editor_plugin.h (C/C++)

Code before:
#ifndef PATH_2D_EDITOR_PLUGIN_H
#define PATH_2D_EDITOR_PLUGIN_H

#include "editor/editor_plugin.h"
#include "scene/2d/path_2d.h"
#include "scene/gui/box_container.h"

class CanvasItemEditor;
class MenuButton;

class Path2DEditor : public HBoxContainer {
	GDCLASS(Path2DEditor, HBoxContainer);

	CanvasItemEditor *canvas_item_editor = nullptr;
	Panel *panel = nullptr;
	Path2D *node = nullptr;

	HBoxContainer *base_hb = nullptr;

	enum Mode {
		MODE_CREATE,
		MODE_EDIT,
		MODE_EDIT_CURVE,
		MODE_DELETE,
		ACTION_CLOSE
	};

	Mode mode;
	Button *curve_create = nullptr;
	Button *curve_edit = nullptr;
	Button *curve_edit_curve = nullptr;
	Button *curve_del = nullptr;
	Button *curve_close = nullptr;
	MenuButton *handle_menu = nullptr;

	bool mirror_handle_angle;
	bool mirror_handle_length;
	bool on_edge;

	enum HandleOption {
		HANDLE_OPTION_ANGLE,
		HANDLE_OPTION_LENGTH
	};

	enum Action {
		ACTION_NONE,
		ACTION_MOVING_POINT,
		ACTION_MOVING_IN,
		ACTION_MOVING_OUT,
	};


Review comment (suggestion):
```suggestion
		MODE_CLEAR_POINTS,
```
More friendly for adding more modes.

Code after:
#ifndef PATH_2D_EDITOR_PLUGIN_H
#define PATH_2D_EDITOR_PLUGIN_H

#include "editor/editor_plugin.h"
#include "scene/2d/path_2d.h"
#include "scene/gui/box_container.h"

class CanvasItemEditor;
class ConfirmationDialog;
class MenuButton;

class Path2DEditor : public HBoxContainer {
	GDCLASS(Path2DEditor, HBoxContainer);

	friend class Path2DEditorPlugin;

	CanvasItemEditor *canvas_item_editor = nullptr;
	Panel *panel = nullptr;
	Path2D *node = nullptr;

	enum Mode {
		MODE_CREATE,
		MODE_EDIT,
		MODE_EDIT_CURVE,
		MODE_DELETE,
		MODE_CLOSE,
		MODE_CLEAR_POINTS,
	};

	Mode mode;
	Button *curve_clear_points = nullptr;
	Button *curve_close = nullptr;
	Button *curve_create = nullptr;
	Button *curve_del = nullptr;
	Button *curve_edit = nullptr;
	Button *curve_edit_curve = nullptr;
	MenuButton *handle_menu = nullptr;

	ConfirmationDialog *clear_points_dialog = nullptr;

	bool mirror_handle_angle;
	bool mirror_handle_length;
	bool on_edge;

	enum HandleOption {
		HANDLE_OPTION_ANGLE,
		HANDLE_OPTION_LENGTH,
	};

	enum Action {
		ACTION_NONE,
Repository: godotengine/godot
File: platform/linuxbsd/x11/display_server_x11.cpp (C++)

Code before:
			}

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

	return ret;
}

String DisplayServerX11::_clipboard_get(Atom p_source, Window x11_window) const {
	String ret;
	Atom utf8_atom = XInternAtom(x11_display, "UTF8_STRING", True);
	if (utf8_atom != None) {
		ret = _clipboard_get_impl(p_source, x11_window, utf8_atom);
	}
	if (ret.is_empty()) {
		ret = _clipboard_get_impl(p_source, x11_window, XA_STRING);
	}
	return ret;
}

String DisplayServerX11::clipboard_get() const {
	_THREAD_SAFE_METHOD_

	String ret;
	ret = _clipboard_get(XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window);

	if (ret.is_empty()) {
		ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window);
	}

	return ret;
}

String DisplayServerX11::clipboard_get_primary() const {
	_THREAD_SAFE_METHOD_

	String ret;
	ret = _clipboard_get(XInternAtom(x11_display, "PRIMARY", 0), windows[MAIN_WINDOW_ID].x11_window);

	if (ret.is_empty()) {
		ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window);
	}

	return ret;
}

Bool DisplayServerX11::_predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg) {
	if (event->xany.window == *(Window *)arg) {

Review comment (suggestion):
```suggestion
		XConvertSelection(x11_display, p_source, target, selection, x11_window, CurrentTime);
```
These lines aren't really wide enough to warrant splitting the line, they become less clear by splitting them, and lines below are as wide or wider

Code after:
				print_verbose("Failed to get selection data.");
			}

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

	return ret;
}

Atom DisplayServerX11::_clipboard_get_image_target(Atom p_source, Window x11_window) const {
	Atom target = XInternAtom(x11_display, "TARGETS", 0);
	Atom png = XInternAtom(x11_display, "image/png", 0);
	Atom *valid_targets = nullptr;
	unsigned long atom_count = 0;

	Window selection_owner = XGetSelectionOwner(x11_display, p_source);
	if (selection_owner != None) {
		// Block events polling while processing selection events.
		MutexLock mutex_lock(events_mutex);

		Atom selection = XA_PRIMARY;
		XConvertSelection(x11_display, p_source, target, selection, 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;
		XGetWindowProperty(x11_display, x11_window,
				selection, // Tricky..
				0, 0, // offset - len
				0, // Delete 0==FALSE
				XA_ATOM, // flag
				&type, // return type
				&format, // return format
				&len, &bytes_left, // data length
				(unsigned char **)&valid_targets);

		if (valid_targets) {
			XFree(valid_targets);
			valid_targets = nullptr;
		}

		if (type == XA_ATOM && bytes_left > 0) {
Repository: godotengine/godot
File: platform/linuxbsd/x11/display_server_x11.cpp (C++)

Code before:

	return _screen_get_rect(p_screen).position;
}

Size2i DisplayServerX11::screen_get_size(int p_screen) const {
	_THREAD_SAFE_METHOD_

	return _screen_get_rect(p_screen).size;
}

// A Handler to avoid crashing on non-fatal X errors by default.
//
// The original X11 error formatter `_XPrintDefaultError` is defined here:
// https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/e45ca7b41dcd3ace7681d6897505f85d374640f2/src/XlibInt.c#L1322
// 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);

Review comment (suggestion):
```suggestion
				PNGDriverCommon::png_to_image(incr_data.ptr(), incr_data.size(), false, ret);
```

Code after:
						} else if (!(format == 0 && len == 0)) {
							// For unclear reasons the first GetWindowProperty always returns a length and format of 0.
							// Otherwise, last chunk, process finished.
							done = true;
							success = true;
						}
					} else {
						print_verbose("Failed to get selection data chunk.");
						done = true;
					}

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

					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;
}
Repository: godotengine/godot
File: platform/linuxbsd/x11/display_server_x11.cpp (C++)

Code before:
		XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
		XFree(xsi);
	} else {
		count = XScreenCount(x11_display);
	}

	return count;
}

int DisplayServerX11::get_primary_screen() const {
	int event_base, error_base;
	if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) {
		return 0;
	} else {
		return XDefaultScreen(x11_display);
	}
}

int DisplayServerX11::get_keyboard_focus_screen() const {
	int count = get_screen_count();
	if (count < 2) {
		// Early exit with single monitor.
		return 0;
	}

	Window focus = 0;
	int revert_to = 0;

	XGetInputFocus(x11_display, &focus, &revert_to);
	if (focus) {
		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;

Review comment (suggestion):
```suggestion
		// Identifier for the property the other window
		// will send the converted data to.
```

Code after:

	if (ret.is_empty()) {
		ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window);
	}

	return ret;
}

Ref<Image> DisplayServerX11::clipboard_get_image() const {
	_THREAD_SAFE_METHOD_
	Atom clipboard = XInternAtom(x11_display, "CLIPBOARD", 0);
	Window x11_window = windows[MAIN_WINDOW_ID].x11_window;
	Ref<Image> ret;
	Atom target = _clipboard_get_image_target(clipboard, x11_window);
	if (target == None) {
		return ret;
	}

	Window selection_owner = XGetSelectionOwner(x11_display, clipboard);

	if (selection_owner != None) {
		// Block events polling while processing selection events.
		MutexLock mutex_lock(events_mutex);

		// Identifier for the property the other window
		// will send the converted data to.
		Atom transfer_prop = XA_PRIMARY;
		XConvertSelection(x11_display,
				clipboard, // source selection
				target, // format to convert to
				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