2010-10-21 07:56:37 +00:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006 - 2010 the Open Toolkit library.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2010-10-21 12:32:00 +00:00
|
|
|
using System.Diagnostics;
|
2010-10-21 07:56:37 +00:00
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.X11
|
|
|
|
{
|
2010-10-29 11:27:40 +00:00
|
|
|
sealed class X11Mouse : IMouseDriver2
|
2010-10-21 07:56:37 +00:00
|
|
|
{
|
2010-11-04 17:57:09 +00:00
|
|
|
readonly IntPtr display;
|
2010-10-21 07:56:37 +00:00
|
|
|
MouseState mouse = new MouseState();
|
|
|
|
|
2010-11-04 17:57:09 +00:00
|
|
|
// Can either attach itself to the specified window or can hook the root window.
|
|
|
|
public X11Mouse()
|
2010-10-21 07:56:37 +00:00
|
|
|
{
|
2010-11-04 17:57:09 +00:00
|
|
|
Debug.WriteLine("Using X11Mouse.");
|
|
|
|
mouse.IsConnected = true;
|
|
|
|
display = API.DefaultDisplay;
|
2010-10-21 07:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MouseState GetState()
|
|
|
|
{
|
|
|
|
ProcessEvents();
|
|
|
|
return mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MouseState GetState(int index)
|
|
|
|
{
|
|
|
|
ProcessEvents();
|
2010-10-21 14:53:10 +00:00
|
|
|
// X11Mouse supports only one device
|
|
|
|
if (index == 0)
|
|
|
|
return mouse;
|
|
|
|
else
|
|
|
|
return new MouseState();
|
2010-10-21 07:56:37 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 17:17:13 +00:00
|
|
|
public void SetPosition(double x, double y)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2010-10-21 07:56:37 +00:00
|
|
|
void WriteBit(MouseButton offset, int enabled)
|
|
|
|
{
|
|
|
|
if (enabled != 0)
|
|
|
|
mouse.EnableBit((int)offset);
|
|
|
|
else
|
|
|
|
mouse.DisableBit((int)offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessEvents()
|
|
|
|
{
|
|
|
|
IntPtr root, child;
|
|
|
|
int root_x, root_y, win_x, win_y;
|
|
|
|
int buttons;
|
|
|
|
|
2010-11-04 17:57:09 +00:00
|
|
|
using (new XLock(display))
|
|
|
|
{
|
|
|
|
IntPtr window = Functions.XRootWindow(display, Functions.XDefaultScreen(display));
|
|
|
|
Functions.XQueryPointer(display, window, out root, out child,
|
|
|
|
out root_x, out root_y, out win_x, out win_y, out buttons);
|
|
|
|
|
|
|
|
mouse.X = root_x;
|
|
|
|
mouse.Y = root_y;
|
|
|
|
WriteBit(MouseButton.Left, buttons & (int)MouseMask.Button1Mask);
|
|
|
|
WriteBit(MouseButton.Middle, buttons & (int)MouseMask.Button2Mask);
|
|
|
|
WriteBit(MouseButton.Right, buttons & (int)MouseMask.Button3Mask);
|
|
|
|
// Note: this will never work right. After spending a week on this, I simply don't care
|
|
|
|
// anymore. If someone can fix it, please do.
|
|
|
|
// Note 2: I have tried passively grabbing those buttons - no go (BadAccess).
|
|
|
|
if ((buttons & (int)MouseMask.Button4Mask) != 0)
|
|
|
|
mouse.WheelPrecise++;
|
|
|
|
if ((buttons & (int)MouseMask.Button5Mask) != 0)
|
|
|
|
mouse.WheelPrecise--;
|
|
|
|
WriteBit(MouseButton.Button1, buttons & (int)MouseMask.Button6Mask);
|
|
|
|
WriteBit(MouseButton.Button2, buttons & (int)MouseMask.Button7Mask);
|
|
|
|
WriteBit(MouseButton.Button3, buttons & (int)MouseMask.Button8Mask);
|
|
|
|
}
|
2010-10-21 07:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|