1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
|
#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;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using OpenTK.Input;
namespace OpenTK.Platform.X11
{
sealed class XI2MouseKeyboard : IKeyboardDriver2, IMouseDriver2, IDisposable
{
static readonly IntPtr ExitAtom;
readonly object Sync = new object();
readonly Thread ProcessingThread;
readonly X11KeyMap KeyMap;
bool disposed;
class XIMouse
{
public MouseState State;
public XIDeviceInfo DeviceInfo;
public XIScrollClassInfo ScrollX = new XIScrollClassInfo { number = -1 };
public XIScrollClassInfo ScrollY = new XIScrollClassInfo { number = -1 };
public XIValuatorClassInfo MotionX = new XIValuatorClassInfo { number = -1 };
public XIValuatorClassInfo MotionY = new XIValuatorClassInfo { number = -1 };
public string Name;
}
class XIKeyboard
{
public KeyboardState State;
public XIDeviceInfo DeviceInfo;
public string Name;
}
// Atoms
//static readonly IntPtr ButtonLeft;
//static readonly IntPtr ButtonMiddle;
////static readonly IntPtr ButtonRight;
//static readonly IntPtr ButtonWheelUp;
//static readonly IntPtr ButtonWheelDown;
//static readonly IntPtr ButtonWheelLeft;
//static readonly IntPtr ButtonWheelRight;
static readonly IntPtr RelX;
static readonly IntPtr RelY;
//static readonly IntPtr RelHorizScroll;
//static readonly IntPtr RelVertScroll;
//static readonly IntPtr RelHorizWheel;
//static readonly IntPtr RelVertWheel;
long cursor_x, cursor_y; // For GetCursorState()
List<XIMouse> devices = new List<XIMouse>(); // list of connected mice
Dictionary<int, int> rawids = new Dictionary<int, int>(); // maps hardware device ids to XIMouse ids
List<XIKeyboard> keyboards = new List<XIKeyboard>(); // list of connected keybords
Dictionary<int, int> keyboard_ids = new Dictionary<int, int>(); // maps hardware device ids to XIKeyboard ids
internal readonly X11WindowInfo window;
internal static int XIOpCode { get; private set; }
internal static int XIVersion { get; private set; }
static readonly Functions.EventPredicate PredicateImpl = IsEventValid;
readonly IntPtr Predicate = Marshal.GetFunctionPointerForDelegate(PredicateImpl);
static XI2MouseKeyboard()
{
using (new XLock(API.DefaultDisplay))
{
// Mouse
//ButtonLeft = Functions.XInternAtom(API.DefaultDisplay, "Button Left", false);
//ButtonMiddle = Functions.XInternAtom(API.DefaultDisplay, "Button Middle", false);
//ButtonRight = Functions.XInternAtom(API.DefaultDisplay, "Button Right", false);
//ButtonWheelUp = Functions.XInternAtom(API.DefaultDisplay, "Button Wheel Up", false);
//ButtonWheelDown = Functions.XInternAtom(API.DefaultDisplay, "Button Wheel Down", false);
//ButtonWheelLeft = Functions.XInternAtom(API.DefaultDisplay, "Button Horiz Wheel Left", false);
//ButtonWheelRight = Functions.XInternAtom(API.DefaultDisplay, "Button Horiz Wheel Right", false);
RelX = Functions.XInternAtom(API.DefaultDisplay, "Rel X", false);
RelY = Functions.XInternAtom(API.DefaultDisplay, "Rel Y", false);
//RelHorizWheel = Functions.XInternAtom(API.DefaultDisplay, "Rel Horiz Wheel", false);
//RelVertWheel = Functions.XInternAtom(API.DefaultDisplay, "Rel Vert Wheel", false);
//RelHorizScroll = Functions.XInternAtom(API.DefaultDisplay, "Rel Horiz Scroll", false);
//RelVertScroll = Functions.XInternAtom(API.DefaultDisplay, "Rel Vert Scroll", false);
// Multitouch
//TouchX = Functions.XInternAtom(API.DefaultDisplay, "Abs MT Position X", false);
//TouchY = Functions.XInternAtom(API.DefaultDisplay, "Abs MT Position Y", false);
//TouchMajor = Functions.XInternAtom(API.DefaultDisplay, "Abs MT Touch Major", false);
//TouchMinor = Functions.XInternAtom(API.DefaultDisplay, "Abs MT Touch Minor", false);
//TouchPressure = Functions.XInternAtom(API.DefaultDisplay, "Abs MT Pressure", false);
//TouchId = Functions.XInternAtom(API.DefaultDisplay, "Abs MT Tracking ID", false);
//TouchMaxContacts = Functions.XInternAtom(API.DefaultDisplay, "Max Contacts", false);
// Custom
ExitAtom = Functions.XInternAtom(API.DefaultDisplay, "Exit Input Thread Message", false);
}
}
public XI2MouseKeyboard()
{
window = new X11WindowInfo();
window.Display = Functions.XOpenDisplay(IntPtr.Zero);
using (new XLock(window.Display))
{
XSetWindowAttributes attr = new XSetWindowAttributes();
window.Screen = Functions.XDefaultScreen(window.Display);
window.RootWindow = Functions.XRootWindow(window.Display, window.Screen);
window.Handle = Functions.XCreateWindow(window.Display, window.RootWindow,
0, 0, 1, 1, 0, 0,
CreateWindowArgs.InputOnly, IntPtr.Zero,
SetWindowValuemask.Nothing, attr);
KeyMap = new X11KeyMap(window.Display);
}
if (!IsSupported(window.Display))
throw new NotSupportedException("XInput2 not supported.");
// Enable XI2 mouse/keyboard events
// Note: the input event loop blocks waiting for these events
// *or* a custom ClientMessage event that instructs us to exit.
// See SendExitEvent() below.
using (new XLock(window.Display))
using (XIEventMask mask = new XIEventMask(1,
XIEventMasks.RawKeyPressMask |
XIEventMasks.RawKeyReleaseMask |
XIEventMasks.RawButtonPressMask |
XIEventMasks.RawButtonReleaseMask |
XIEventMasks.RawMotionMask |
XIEventMasks.MotionMask |
XIEventMasks.DeviceChangedMask))
{
XI.SelectEvents(window.Display, window.RootWindow, mask);
UpdateDevices();
}
ProcessingThread = new Thread(ProcessEvents);
ProcessingThread.IsBackground = true;
ProcessingThread.Start();
}
// Checks whether XInput2 is supported on the specified display.
// If a display is not specified, the default display is used.
internal static bool IsSupported(IntPtr display)
{
try
{
if (display == IntPtr.Zero)
{
display = API.DefaultDisplay;
}
using (new XLock(display))
{
int major, ev, error;
if (Functions.XQueryExtension(display, "XInputExtension", out major, out ev, out error) != 0)
{
XIOpCode = major;
int minor = 2;
while (minor >= 0)
{
if (XI.QueryVersion(display, ref major, ref minor) == ErrorCodes.Success)
{
XIVersion = major * 100 + minor * 10;
return true;
}
minor--;
}
}
}
}
catch (DllNotFoundException e)
{
Debug.Print(e.ToString());
Debug.Print("XInput2 extension not supported. Mouse support will suffer.");
}
return false;
}
#region IKeyboardDriver2 Members
KeyboardState IKeyboardDriver2.GetState()
{
lock (Sync)
{
KeyboardState state = new KeyboardState();
foreach (XIKeyboard k in keyboards)
{
state.MergeBits(k.State);
}
return state;
}
}
KeyboardState IKeyboardDriver2.GetState(int index)
{
lock (Sync)
{
if (index >= 0 && index < keyboards.Count)
{
return keyboards[index].State;
}
return new KeyboardState();
}
}
string IKeyboardDriver2.GetDeviceName(int index)
{
lock (Sync)
{
if (index >= 0 && index < keyboards.Count)
{
return keyboards[index].Name;
}
return String.Empty;
}
}
#endregion
#region IMouseDriver2 Members
MouseState IMouseDriver2.GetState()
{
lock (Sync)
{
MouseState master = new MouseState();
foreach (var d in devices)
{
master.MergeBits(d.State);
}
return master;
}
}
MouseState IMouseDriver2.GetState(int index)
{
lock (Sync)
{
if (index >= 0 && index < devices.Count)
{
return devices[index].State;
}
return new MouseState();
}
}
MouseState IMouseDriver2.GetCursorState()
{
lock (Sync)
{
MouseState master = (this as IMouseDriver2).GetState();
master.X = (int)Interlocked.Read(ref cursor_x);
master.Y = (int)Interlocked.Read(ref cursor_y);
return master;
}
}
void IMouseDriver2.SetPosition(double x, double y)
{
// Note: we cannot use window.Display here, because
// that will deadlock the input thread, which is
// blocking inside XIfEvent
using (new XLock(API.DefaultDisplay))
{
Functions.XWarpPointer(API.DefaultDisplay,
IntPtr.Zero, window.RootWindow, 0, 0, 0, 0, (int)x, (int)y);
Functions.XFlush(API.DefaultDisplay);
Interlocked.Exchange(ref cursor_x, (long)x);
Interlocked.Exchange(ref cursor_y, (long)y);
}
}
#endregion
#region Private Members
void UpdateDevices()
{
lock (Sync)
{
devices.Clear();
keyboards.Clear();
int count;
unsafe
{
XIDeviceInfo* list = (XIDeviceInfo*)XI.QueryDevice(window.Display,
XI.XIAllDevices, out count);
Debug.Print("Refreshing input device list");
Debug.Print("{0} input devices detected", count);
for (int i = 0; i < count; i++)
{
switch ((list + i)->use)
{
case XIDeviceType.MasterKeyboard:
//case XIDeviceType.SlaveKeyboard:
{
XIKeyboard k = new XIKeyboard();
k.DeviceInfo = *(list + i);
k.State.SetIsConnected(k.DeviceInfo.enabled);
k.Name = Marshal.PtrToStringAnsi(k.DeviceInfo.name);
int id = k.DeviceInfo.deviceid;
if (!keyboard_ids.ContainsKey(id))
{
keyboard_ids.Add(k.DeviceInfo.deviceid, 0);
}
keyboard_ids[id] = keyboards.Count;
keyboards.Add(k);
}
break;
case XIDeviceType.MasterPointer:
//case XIDeviceType.SlavePointer:
case XIDeviceType.FloatingSlave:
{
XIMouse d = new XIMouse();
d.DeviceInfo = *(list + i);
d.State.SetIsConnected(d.DeviceInfo.enabled);
d.Name = Marshal.PtrToStringAnsi(d.DeviceInfo.name);
Debug.Print("Device {0} \"{1}\" is {2} and has:",
i, d.Name, d.DeviceInfo.enabled ? "enabled" : "disabled");
// Decode the XIDeviceInfo to axes, buttons and scroll types
for (int j = 0; j < d.DeviceInfo.num_classes; j++)
{
XIAnyClassInfo* class_info = *((XIAnyClassInfo**)d.DeviceInfo.classes + j);
switch (class_info->type)
{
case XIClassType.Button:
{
XIButtonClassInfo* button = (XIButtonClassInfo*)class_info;
Debug.Print("\t{0} buttons", button->num_buttons);
}
break;
case XIClassType.Scroll:
{
XIScrollClassInfo* scroll = (XIScrollClassInfo*)class_info;
switch (scroll->scroll_type)
{
case XIScrollType.Vertical:
Debug.WriteLine("\tSmooth vertical scrolling");
d.ScrollY = *scroll;
break;
case XIScrollType.Horizontal:
Debug.WriteLine("\tSmooth horizontal scrolling");
d.ScrollX = *scroll;
break;
default:
Debug.Print("\tUnknown scrolling type {0}", scroll->scroll_type);
break;
}
}
break;
case XIClassType.Valuator:
{
XIValuatorClassInfo* valuator = (XIValuatorClassInfo*)class_info;
if (valuator->label == RelX)
{
Debug.WriteLine("\tRelative X movement");
d.MotionX = *valuator;
}
else if (valuator->label == RelY)
{
Debug.WriteLine("\tRelative Y movement");
d.MotionY = *valuator;
}
}
break;
}
}
// Map the hardware device id to the current XIMouse id
int id = d.DeviceInfo.deviceid;
if (!rawids.ContainsKey(id))
{
rawids.Add(id, 0);
}
rawids[id] = devices.Count;
devices.Add(d);
}
break;
}
}
XI.FreeDeviceInfo((IntPtr)list);
}
}
}
void ProcessEvents()
{
while (!disposed)
{
XEvent e = new XEvent();
XGenericEventCookie cookie;
using (new XLock(window.Display))
{
Functions.XIfEvent(window.Display, ref e, Predicate, new IntPtr(XIOpCode));
if (e.type == XEventName.ClientMessage && e.ClientMessageEvent.ptr1 == ExitAtom)
{
Functions.XDestroyWindow(window.Display, window.Handle);
window.Handle = IntPtr.Zero;
break;
}
if (e.type == XEventName.GenericEvent && e.GenericEvent.extension == XIOpCode)
{
IntPtr dummy;
int x, y, dummy2;
Functions.XQueryPointer(window.Display, window.RootWindow,
out dummy, out dummy, out x, out y,
out dummy2, out dummy2, out dummy2);
Interlocked.Exchange(ref cursor_x, (long)x);
Interlocked.Exchange(ref cursor_y, (long)y);
cookie = e.GenericEventCookie;
if (Functions.XGetEventData(window.Display, ref cookie) != 0)
{
switch ((XIEventType)cookie.evtype)
{
case XIEventType.Motion:
// Nothing to do
break;
case XIEventType.RawKeyPress:
case XIEventType.RawKeyRelease:
case XIEventType.RawMotion:
case XIEventType.RawButtonPress:
case XIEventType.RawButtonRelease:
// Delivered to all XIMouse instances
ProcessRawEvent(ref cookie);
break;
case XIEventType.DeviceChanged:
UpdateDevices();
break;
}
}
Functions.XFreeEventData(window.Display, ref cookie);
}
}
}
Debug.WriteLine("[X11] Exiting input event loop.");
}
void ProcessRawEvent(ref XGenericEventCookie cookie)
{
lock (Sync)
{
unsafe
{
XIRawEvent raw = *(XIRawEvent*)cookie.data;
XIMouse mouse;
XIKeyboard keyboard;
switch (raw.evtype)
{
case XIEventType.RawMotion:
if (GetMouseDevice(raw.deviceid, out mouse))
{
ProcessRawMotion(mouse, ref raw);
}
break;
case XIEventType.RawButtonPress:
case XIEventType.RawButtonRelease:
if (GetMouseDevice(raw.deviceid, out mouse))
{
float dx, dy;
MouseButton button = X11KeyMap.TranslateButton(raw.detail, out dx, out dy);
mouse.State[button] = raw.evtype == XIEventType.RawButtonPress;
}
break;
case XIEventType.RawKeyPress:
case XIEventType.RawKeyRelease:
if (GetKeyboardDevice(raw.deviceid, out keyboard))
{
Key key;
if (KeyMap.TranslateKey(raw.detail, out key))
{
keyboard.State[key] = raw.evtype == XIEventType.RawKeyPress;
}
}
break;
}
}
}
}
bool GetMouseDevice(int deviceid, out XIMouse mouse)
{
if (!rawids.ContainsKey(deviceid))
{
Debug.Print("Unknown mouse device {0} encountered, ignoring.", deviceid);
mouse = null;
return false;
}
mouse = devices[rawids[deviceid]];
return true;
}
bool GetKeyboardDevice(int deviceid, out XIKeyboard keyboard)
{
if (!keyboard_ids.ContainsKey(deviceid))
{
Debug.Print("Unknown keyboard device {0} encountered, ignoring.", deviceid);
keyboard = null;
return false;
}
keyboard = keyboards[keyboard_ids[deviceid]];
return true;
}
unsafe static void ProcessRawMotion(XIMouse d, ref XIRawEvent raw)
{
// Note: we use the raw values here, without pointer
// ballistics and any other modification.
double x = ReadRawValue(ref raw, d.MotionX.number);
double y = ReadRawValue(ref raw, d.MotionY.number);
double h = ReadRawValue(ref raw, d.ScrollX.number) / d.ScrollX.increment;
double v = ReadRawValue(ref raw, d.ScrollY.number) / d.ScrollY.increment;
d.State.X += (int)Math.Round(x);
d.State.Y += (int)Math.Round(y);
// Note: OpenTK follows the windows scrolling convention where
// (+h, +v) = (right, up). XI2 uses (+h, +v) = (right, down)
// instead, so we need to flip the vertical offset.
d.State.SetScrollRelative((float)h, (float)(-v));
}
unsafe static double ReadRawValue(ref XIRawEvent raw, int bit)
{
double value = 0;
if (IsBitSet(raw.valuators.mask, bit))
{
// Find the offset where this value is stored.
// The offset is equal to the number of bits
// set in raw.valuators.mask between [0, bit)
int offset = 0;
for (int i = 0; i < bit; i++)
{
if (IsBitSet(raw.valuators.mask, i))
{
offset++;
}
}
value = *((double*)raw.raw_values + offset);
}
return value;
}
static bool IsEventValid(IntPtr display, ref XEvent e, IntPtr arg)
{
bool valid = false;
switch (e.type)
{
case XEventName.GenericEvent:
{
long extension = (long)e.GenericEventCookie.extension;
valid =
extension == arg.ToInt64() &&
(e.GenericEventCookie.evtype == (int)XIEventType.RawKeyPress ||
e.GenericEventCookie.evtype == (int)XIEventType.RawKeyRelease ||
e.GenericEventCookie.evtype == (int)XIEventType.RawMotion ||
e.GenericEventCookie.evtype == (int)XIEventType.RawButtonPress ||
e.GenericEventCookie.evtype == (int)XIEventType.RawButtonRelease ||
e.GenericEventCookie.evtype == (int)XIEventType.DeviceChanged);
valid |= extension == 0;
break;
}
case XEventName.ClientMessage:
valid =
e.ClientMessageEvent.ptr1 == ExitAtom;
break;
}
return valid;
}
static bool IsBitSet(IntPtr mask, int bit)
{
unsafe
{
return bit >= 0 && (*((byte*)mask + (bit >> 3)) & (1 << (bit & 7))) != 0;
}
}
void SendExitEvent()
{
// Send a ClientMessage event to unblock XIfEvent
// and exit the input event loop.
using (new XLock(API.DefaultDisplay))
{
XEvent ev = new XEvent();
ev.type = XEventName.ClientMessage;
ev.ClientMessageEvent.display = window.Display;
ev.ClientMessageEvent.window = window.Handle;
ev.ClientMessageEvent.format = 32;
ev.ClientMessageEvent.ptr1 = ExitAtom;
Functions.XSendEvent(API.DefaultDisplay, window.Handle, false, 0, ref ev);
Functions.XFlush(API.DefaultDisplay);
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (!disposed)
{
disposed = true;
SendExitEvent();
}
}
~XI2MouseKeyboard()
{
Dispose(false);
}
#endregion
}
}
|