Commits

André Schnabel committed a37cc6c

Fixed mouselook on linux

Comments (0)

Files changed (1)

Platforms/Desktop/DesktopBasecode.cs

 using System.Threading;
 using OpenTK.Audio;
 using PixelFormat = OpenTK.Graphics.OpenGL.PixelFormat;
+using System.Runtime.InteropServices;
 
 namespace Kosu.Platforms.Desktop {
 	/// <summary>
 				state = (State) constructor.Invoke(new object[] {this});
 			}
 
-			//if(Environment.OSVersion.Platform == PlatformID.Unix && IsRunningOnMac())
-			RegisterMouseHandlers();
+			if(Environment.OSVersion.Platform != PlatformID.Unix || IsRunningOnMac())
+				RegisterMouseHandlers();
+		}
+
+		//From Managed.Windows.Forms/XplatUI
+		[DllImport("libc")]
+		private static extern int uname(IntPtr buf);
+
+		private static bool IsRunningOnMac() {
+			IntPtr buf = IntPtr.Zero;
+			try {
+				buf = Marshal.AllocHGlobal(8192);
+				// This is a hacktastic way of getting sysname from uname ()
+				if(uname(buf) == 0) {
+					string os = Marshal.PtrToStringAnsi(buf);
+					if(os == "Darwin")
+						return true;
+				}
+			}
+			catch {}
+			finally {
+				if(buf != IntPtr.Zero)
+					Marshal.FreeHGlobal(buf);
+			}
+			return false;
 		}
 
 		protected override void OnMove(EventArgs e) {
 				}
 			}
 
-			int deltaX = mousePos.X - oldPos.X;
-			int deltaY = mousePos.Y - oldPos.Y;
-			int mouseX = mousePos.X;
-			int mouseY = mousePos.Y;
-			oldPos = mousePos;
+			int mouseX, mouseY;
+			int deltaX, deltaY;
+
+			// Linux case
+			if(Environment.OSVersion.Platform == PlatformID.Unix && !IsRunningOnMac()) {
+				MouseState mstate = OpenTK.Input.Mouse.GetState();
+				mouseX = Mouse.X;
+				mouseY = Mouse.Y;
+				deltaX = mstate.X - oldPos.X;
+				deltaY = mstate.Y - oldPos.Y;
+				oldPos.X = mstate.X;
+				oldPos.Y = mstate.Y;
+				mbtns.Lmb = mstate.LeftButton == ButtonState.Pressed;
+				mbtns.Mmb = mstate.MiddleButton == ButtonState.Pressed;
+				mbtns.Rmb = mstate.RightButton == ButtonState.Pressed;
+			}
+			else {
+				deltaX = mousePos.X - oldPos.X;
+				deltaY = mousePos.Y - oldPos.Y;
+				mouseX = mousePos.X;
+				mouseY = mousePos.Y;
+				oldPos = mousePos;
+			}
 
 			if(!Globals.PhoneMode) {
 				state.ProcessMouseInput(mouseX, mouseY, deltaX, deltaY, mbtns, delta);