Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Cursor Behavior on Linux #328

Open
JosiahJack opened this issue Jan 30, 2023 · 0 comments
Open

BUG: Cursor Behavior on Linux #328

JosiahJack opened this issue Jan 30, 2023 · 0 comments
Assignees
Labels

Comments

@JosiahJack
Copy link
Owner

Describe the bug
When changing from Shoot Mode to Inventory Mode, the system cursor is unhidden and revealed to have never actually been locked and is thus in some strange position usually near an edge of the screen.

Attempted to use native xlib call but the system fights it and this only seems to work if I am not actively moving my mouse:

using System.Runtime.InteropServices;
using System;

public class MouseCursor : MonoBehaviour {

...

	#if UNITY_STANDALONE_LINUX
		[DllImport("libX11")]
		static extern IntPtr XOpenDisplay(string display);

		[DllImport("libX11")]
		static extern int XCloseDisplay(IntPtr display);

		[DllImport("libX11")]
		static extern int XWarpPointer(IntPtr display, IntPtr src_w, 
									   IntPtr dest_w, int src_x, int src_y,
									   uint src_width, uint src_height, 
									   int dest_x, int dest_y);
	#elif UNITY_STANDALONE_WIN
		[DllImport("user32.dll")]
		public static extern bool SetCursorPos(int X, int Y);
	#endif

	public static void SetCursorPosInternal(int x, int y) {
		return; // Still experiencing issues, best to live with this bug a while yet.

		#if UNITY_STANDALONE_LINUX
			IntPtr display = XOpenDisplay(null);
			if (display == IntPtr.Zero) {
				throw new Exception("Failed to open display");
			}

			Debug.Log("warping pointer to " + x.ToString() + ", " + y.ToString());
			XWarpPointer(display, IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, x, y);
			XCloseDisplay(display);
		#elif UNITY_STANDALONE_WIN
			SetCursorPos((int)(Screen.width * 0.5f),(int)(Screen.height * 0.5f));
		#endif
	}
	
...
@JosiahJack JosiahJack added the bug label Jan 30, 2023
@JosiahJack JosiahJack self-assigned this Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant