2014-07-14 15:42:17 +00:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// LinuxKeyboardLibInput.cs
|
|
|
|
|
//
|
|
|
|
|
// Author:
|
|
|
|
|
// Stefanos A. <stapostol@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2006-2014 Stefanos Apostolopoulos
|
|
|
|
|
//
|
|
|
|
|
// 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;
|
2014-07-16 10:18:24 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-07-14 15:42:17 +00:00
|
|
|
|
using System.Diagnostics;
|
2014-07-16 10:18:24 +00:00
|
|
|
|
using System.Drawing;
|
2014-07-14 15:42:17 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Linux
|
|
|
|
|
{
|
|
|
|
|
class LinuxInput : IKeyboardDriver2, IMouseDriver2, IDisposable
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
class DeviceBase
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
|
|
|
|
readonly IntPtr Device;
|
|
|
|
|
string name;
|
|
|
|
|
string output;
|
2014-07-18 07:02:01 +00:00
|
|
|
|
string logical_seat;
|
|
|
|
|
string physical_seat;
|
2014-07-14 23:15:44 +00:00
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
public DeviceBase(IntPtr device, int id)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
|
|
|
|
Device = device;
|
|
|
|
|
Id = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Id
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetId(Device);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
LibInput.DeviceSetData(Device, (IntPtr)value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
name = name ?? LibInput.DeviceGetName(Device);
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 07:02:01 +00:00
|
|
|
|
public IntPtr Seat
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return LibInput.DeviceGetSeat(Device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string LogicalSeatName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
logical_seat = logical_seat ?? LibInput.SeatGetLogicalName(Seat);
|
|
|
|
|
return logical_seat;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string PhysicalSeatName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
physical_seat = physical_seat ?? LibInput.SeatGetPhysicalName(Seat);
|
|
|
|
|
return physical_seat;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 23:15:44 +00:00
|
|
|
|
public string Output
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
output = output ?? LibInput.DeviceGetOutputName(Device);
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-16 09:24:53 +00:00
|
|
|
|
}
|
2014-07-14 23:15:44 +00:00
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
class KeyboardDevice : DeviceBase
|
|
|
|
|
{
|
2014-07-14 23:15:44 +00:00
|
|
|
|
public KeyboardState State;
|
2014-07-16 09:24:53 +00:00
|
|
|
|
|
|
|
|
|
public KeyboardDevice(IntPtr device, int id)
|
|
|
|
|
: base(device, id)
|
|
|
|
|
{
|
|
|
|
|
State.SetIsConnected(true);
|
|
|
|
|
}
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
class MouseDevice : DeviceBase
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
|
|
|
|
public MouseState State;
|
2014-07-16 09:24:53 +00:00
|
|
|
|
|
|
|
|
|
public MouseDevice(IntPtr device, int id)
|
|
|
|
|
: base(device, id)
|
|
|
|
|
{
|
|
|
|
|
State.SetIsConnected(true);
|
|
|
|
|
}
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
static readonly object Sync = new object();
|
2014-07-15 07:09:11 +00:00
|
|
|
|
static readonly Key[] KeyMap = Evdev.KeyMap;
|
2014-07-15 15:28:31 +00:00
|
|
|
|
static long DeviceFDCount;
|
2014-07-14 23:15:44 +00:00
|
|
|
|
DeviceCollection<KeyboardDevice> Keyboards = new DeviceCollection<KeyboardDevice>();
|
|
|
|
|
DeviceCollection<MouseDevice> Mice = new DeviceCollection<MouseDevice>();
|
|
|
|
|
|
2014-07-16 10:18:24 +00:00
|
|
|
|
// Todo: do we need to maintain the geometry of each display separately?
|
|
|
|
|
Rectangle bounds;
|
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
// Global mouse cursor state
|
|
|
|
|
Vector2 CursorPosition = Vector2.Zero;
|
|
|
|
|
// Global mouse cursor offset (used for emulating SetPosition)
|
|
|
|
|
Vector2 CursorOffset = Vector2.Zero;
|
|
|
|
|
|
2014-07-14 15:42:17 +00:00
|
|
|
|
IntPtr udev;
|
|
|
|
|
IntPtr input_context;
|
|
|
|
|
InputInterface input_interface = new InputInterface(
|
|
|
|
|
OpenRestricted, CloseRestricted);
|
|
|
|
|
int fd;
|
|
|
|
|
Thread input_thread;
|
|
|
|
|
long exit;
|
|
|
|
|
|
|
|
|
|
public LinuxInput()
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Linux] Initializing {0}", GetType().Name);
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Debug.Indent();
|
|
|
|
|
try
|
2014-07-14 15:42:17 +00:00
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Semaphore ready = new Semaphore(0, 1);
|
|
|
|
|
input_thread = new Thread(InputThreadLoop);
|
|
|
|
|
input_thread.IsBackground = true;
|
|
|
|
|
input_thread.Start(ready);
|
|
|
|
|
|
|
|
|
|
// Wait until the input thread is ready.
|
|
|
|
|
// Note: it would be nicer if we could avoid this.
|
|
|
|
|
// however we need to marshal errors back to the caller
|
|
|
|
|
// as exceptions.
|
|
|
|
|
// Todo: in a future version, we should add an "Application" object
|
|
|
|
|
// to handle all communication with the OS (including event processing.)
|
|
|
|
|
// Once we do that, we can remove all separate input threads.
|
|
|
|
|
ready.WaitOne();
|
|
|
|
|
if (exit != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
2014-07-15 15:28:31 +00:00
|
|
|
|
finally
|
2014-07-14 15:42:17 +00:00
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Debug.Print("Initialization {0}", exit == 0 ?
|
|
|
|
|
"complete" : "failed");
|
|
|
|
|
Debug.Unindent();
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
|
|
|
|
|
static CloseRestrictedCallback CloseRestricted = CloseRestrictedHandler;
|
|
|
|
|
static void CloseRestrictedHandler(int fd, IntPtr data)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] Closing fd {0}", fd);
|
2014-07-15 15:28:31 +00:00
|
|
|
|
int ret = Libc.close(fd);
|
|
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] Failed to close fd {0}. Error: {1}", fd, ret);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Decrement(ref DeviceFDCount);
|
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static OpenRestrictedCallback OpenRestricted = OpenRestrictedHandler;
|
|
|
|
|
static int OpenRestrictedHandler(IntPtr path, int flags, IntPtr data)
|
|
|
|
|
{
|
|
|
|
|
int fd = Libc.open(path, (OpenFlags)flags);
|
|
|
|
|
Debug.Print("[Input] Opening '{0}' with flags {1}. fd:{2}",
|
|
|
|
|
Marshal.PtrToStringAnsi(path), (OpenFlags)flags, fd);
|
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
if (fd >= 0)
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Increment(ref DeviceFDCount);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 15:42:17 +00:00
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
void InputThreadLoop(object semaphore)
|
2014-07-14 15:42:17 +00:00
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Debug.Print("[Input] Running on thread {0}", Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
Setup();
|
|
|
|
|
|
|
|
|
|
// Inform the parent thread that initialization has completed successfully
|
|
|
|
|
(semaphore as Semaphore).Release();
|
|
|
|
|
Debug.Print("[Input] Released main thread.", input_context);
|
|
|
|
|
|
|
|
|
|
// Use a blocking poll for input messages, in order to reduce CPU usage
|
2014-07-14 15:42:17 +00:00
|
|
|
|
PollFD poll_fd = new PollFD();
|
|
|
|
|
poll_fd.fd = fd;
|
|
|
|
|
poll_fd.events = PollFlags.In;
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Debug.Print("[Input] Created PollFD({0}, {1})", poll_fd.fd, poll_fd.events);
|
2014-07-14 15:42:17 +00:00
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Debug.Print("[Input] Entering input loop.", poll_fd.fd, poll_fd.events);
|
2014-07-14 15:42:17 +00:00
|
|
|
|
while (Interlocked.Read(ref exit) == 0)
|
|
|
|
|
{
|
|
|
|
|
int ret = Libc.poll(ref poll_fd, 1, -1);
|
2014-07-16 09:24:53 +00:00
|
|
|
|
ErrorNumber error = (ErrorNumber)Marshal.GetLastWin32Error();
|
|
|
|
|
bool is_error =
|
|
|
|
|
ret < 0 && !(error == ErrorNumber.Again || error == ErrorNumber.Interrupted) ||
|
|
|
|
|
(poll_fd.revents & (PollFlags.Hup | PollFlags.Error | PollFlags.Invalid)) != 0;
|
|
|
|
|
|
2014-07-16 10:18:24 +00:00
|
|
|
|
// We need to query the desktop bounds in order to position the mouse cursor correctly.
|
|
|
|
|
// This value will be used for the current bunch of input events. If a monitor changes
|
|
|
|
|
// resolution in the meantime, we might be slightly off in our calculations - this error
|
|
|
|
|
// will be corrected when the next bunch of input events arrives.
|
|
|
|
|
UpdateDisplayBounds();
|
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
if (ret > 0 && (poll_fd.revents & (PollFlags.In | PollFlags.Pri)) != 0)
|
2014-07-14 15:42:17 +00:00
|
|
|
|
{
|
2014-07-15 07:09:11 +00:00
|
|
|
|
ProcessEvents(input_context);
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
2014-07-15 15:28:31 +00:00
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
if (is_error)
|
2014-07-14 15:42:17 +00:00
|
|
|
|
{
|
2014-07-15 20:54:04 +00:00
|
|
|
|
Debug.Print("[Input] Exiting input loop {0} due to poll error [ret:{1} events:{2}]. Error: {3}.",
|
2014-07-16 09:24:53 +00:00
|
|
|
|
input_thread.ManagedThreadId, ret, poll_fd.revents, error);
|
2014-07-14 15:42:17 +00:00
|
|
|
|
Interlocked.Increment(ref exit);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-15 15:28:31 +00:00
|
|
|
|
Debug.Print("[Input] Exited input loop.", poll_fd.fd, poll_fd.events);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 10:18:24 +00:00
|
|
|
|
void UpdateDisplayBounds()
|
|
|
|
|
{
|
|
|
|
|
bounds = Rectangle.Empty;
|
|
|
|
|
for (DisplayIndex i = DisplayIndex.First; i < DisplayIndex.Sixth; i++)
|
|
|
|
|
{
|
|
|
|
|
DisplayDevice display = DisplayDevice.GetDisplay(i);
|
|
|
|
|
if (display != null)
|
|
|
|
|
{
|
|
|
|
|
bounds = Rectangle.Union(bounds, display.Bounds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-17 07:25:16 +00:00
|
|
|
|
void UpdateCursor()
|
|
|
|
|
{
|
|
|
|
|
Point p = new Point(
|
|
|
|
|
(int)Math.Round(CursorPosition.X + CursorOffset.X),
|
|
|
|
|
(int)Math.Round(CursorPosition.Y + CursorOffset.Y));
|
|
|
|
|
|
2014-07-17 09:20:01 +00:00
|
|
|
|
DisplayDevice display = DisplayDevice.FromPoint(p.X, p.Y) ?? DisplayDevice.Default;
|
2014-07-17 07:25:16 +00:00
|
|
|
|
if (display != null)
|
|
|
|
|
{
|
|
|
|
|
LinuxDisplay d = (LinuxDisplay)display.Id;
|
|
|
|
|
Drm.MoveCursor(d.FD, d.Id, p.X, p.Y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
void Setup()
|
|
|
|
|
{
|
|
|
|
|
// Todo: add static path fallback when udev is not installed.
|
|
|
|
|
udev = Udev.New();
|
|
|
|
|
if (udev == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] Udev.New() failed.");
|
|
|
|
|
Interlocked.Increment(ref exit);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Debug.Print("[Input] Udev.New() = {0:x}", udev);
|
|
|
|
|
|
|
|
|
|
input_context = LibInput.CreateContext(input_interface, IntPtr.Zero, udev, "seat0");
|
|
|
|
|
if (input_context == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] LibInput.CreateContext({0:x}) failed.", udev);
|
|
|
|
|
Interlocked.Increment(ref exit);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Debug.Print("[Input] LibInput.CreateContext({0:x}) = {1:x}", udev, input_context);
|
|
|
|
|
|
|
|
|
|
fd = LibInput.GetFD(input_context);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] LibInput.GetFD({0:x}) failed.", input_context);
|
|
|
|
|
Interlocked.Increment(ref exit);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Debug.Print("[Input] LibInput.GetFD({0:x}) = {1}.", input_context, fd);
|
|
|
|
|
|
|
|
|
|
ProcessEvents(input_context);
|
|
|
|
|
LibInput.Resume(input_context);
|
|
|
|
|
Debug.Print("[Input] LibInput.Resume({0:x})", input_context);
|
|
|
|
|
|
|
|
|
|
if (Interlocked.Read(ref DeviceFDCount) <= 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Error] Failed to open any input devices.");
|
|
|
|
|
Debug.Print("[Error] Ensure that you have access to '/dev/input/event*'.");
|
|
|
|
|
Interlocked.Increment(ref exit);
|
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-15 07:09:11 +00:00
|
|
|
|
void ProcessEvents(IntPtr input_context)
|
|
|
|
|
{
|
|
|
|
|
// Process all events in the event queue
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
// Data available
|
|
|
|
|
int ret = LibInput.Dispatch(input_context);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] LibInput.Dispatch({0:x}) failed. Error: {1}",
|
|
|
|
|
input_context, ret);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntPtr pevent = LibInput.GetEvent(input_context);
|
|
|
|
|
if (pevent == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntPtr device = LibInput.GetDevice(pevent);
|
|
|
|
|
InputEventType type = LibInput.GetEventType(pevent);
|
|
|
|
|
|
2014-07-15 15:28:31 +00:00
|
|
|
|
lock (Sync)
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case InputEventType.DeviceAdded:
|
|
|
|
|
HandleDeviceAdded(input_context, device);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputEventType.DeviceRemoved:
|
|
|
|
|
HandleDeviceRemoved(input_context, device);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputEventType.KeyboardKey:
|
2014-07-16 09:24:53 +00:00
|
|
|
|
HandleKeyboard(GetKeyboard(device), LibInput.GetKeyboardEvent(pevent));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputEventType.PointerAxis:
|
|
|
|
|
HandlePointerAxis(GetMouse(device), LibInput.GetPointerEvent(pevent));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputEventType.PointerButton:
|
|
|
|
|
HandlePointerButton(GetMouse(device), LibInput.GetPointerEvent(pevent));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputEventType.PointerMotion:
|
|
|
|
|
HandlePointerMotion(GetMouse(device), LibInput.GetPointerEvent(pevent));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputEventType.PointerMotionAbsolute:
|
|
|
|
|
HandlePointerMotionAbsolute(GetMouse(device), LibInput.GetPointerEvent(pevent));
|
2014-07-15 15:28:31 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2014-07-15 07:09:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LibInput.DestroyEvent(pevent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 23:15:44 +00:00
|
|
|
|
void HandleDeviceAdded(IntPtr context, IntPtr device)
|
|
|
|
|
{
|
|
|
|
|
if (LibInput.DeviceHasCapability(device, DeviceCapability.Keyboard))
|
|
|
|
|
{
|
|
|
|
|
KeyboardDevice keyboard = new KeyboardDevice(device, Keyboards.Count);
|
|
|
|
|
Keyboards.Add(keyboard.Id, keyboard);
|
2014-07-18 07:02:01 +00:00
|
|
|
|
Debug.Print("[Input] Added keyboard device {0} '{1}' on '{2}' ('{3}')",
|
|
|
|
|
keyboard.Id, keyboard.Name, keyboard.LogicalSeatName, keyboard.PhysicalSeatName);
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (LibInput.DeviceHasCapability(device, DeviceCapability.Mouse))
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
MouseDevice mouse = new MouseDevice(device, Mice.Count);
|
|
|
|
|
Mice.Add(mouse.Id, mouse);
|
2014-07-18 07:02:01 +00:00
|
|
|
|
Debug.Print("[Input] Added mouse device {0} '{1}' on '{2}' ('{3}')",
|
|
|
|
|
mouse.Id, mouse.Name, mouse.LogicalSeatName, mouse.PhysicalSeatName);
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (LibInput.DeviceHasCapability(device, DeviceCapability.Touch))
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
Debug.Print("[Input] Todo: touch device.");
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleDeviceRemoved(IntPtr context, IntPtr device)
|
|
|
|
|
{
|
|
|
|
|
if (LibInput.DeviceHasCapability(device, DeviceCapability.Keyboard))
|
|
|
|
|
{
|
|
|
|
|
int id = GetId(device);
|
|
|
|
|
Keyboards.Remove(id);
|
|
|
|
|
}
|
2014-07-16 09:24:53 +00:00
|
|
|
|
|
|
|
|
|
if (LibInput.DeviceHasCapability(device, DeviceCapability.Mouse))
|
|
|
|
|
{
|
|
|
|
|
int id = GetId(device);
|
|
|
|
|
Mice.Remove(id);
|
|
|
|
|
}
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
void HandleKeyboard(KeyboardDevice device, KeyboardEvent e)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
if (device != null)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
2014-07-15 07:09:11 +00:00
|
|
|
|
Key key = Key.Unknown;
|
|
|
|
|
uint raw = e.Key;
|
|
|
|
|
if (raw >= 0 && raw < KeyMap.Length)
|
|
|
|
|
{
|
|
|
|
|
key = KeyMap[raw];
|
|
|
|
|
}
|
2014-07-15 20:54:04 +00:00
|
|
|
|
|
|
|
|
|
if (key == Key.Unknown)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Linux] Unknown key with code '{0}'", raw);
|
|
|
|
|
}
|
2014-07-15 07:09:11 +00:00
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
device.State.SetKeyState(key, e.KeyState == KeyState.Pressed);
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
2014-07-16 09:24:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandlePointerAxis(MouseDevice mouse, PointerEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (mouse != null)
|
|
|
|
|
{
|
|
|
|
|
double value = e.AxisValue;
|
|
|
|
|
PointerAxis axis = e.Axis;
|
|
|
|
|
switch (axis)
|
|
|
|
|
{
|
|
|
|
|
case PointerAxis.HorizontalScroll:
|
|
|
|
|
mouse.State.SetScrollRelative((float)value, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PointerAxis.VerticalScroll:
|
|
|
|
|
mouse.State.SetScrollRelative(0, (float)value);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Debug.Print("[Input] Unknown scroll axis {0}.", axis);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandlePointerButton(MouseDevice mouse, PointerEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (mouse != null)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
MouseButton button = Evdev.GetMouseButton(e.Button);
|
|
|
|
|
ButtonState state = e.ButtonState;
|
|
|
|
|
mouse.State[(MouseButton)button] = state == ButtonState.Pressed;
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
void HandlePointerMotion(MouseDevice mouse, PointerEvent e)
|
|
|
|
|
{
|
|
|
|
|
Vector2 delta = new Vector2((float)e.X, (float)e.Y);
|
|
|
|
|
if (mouse != null)
|
|
|
|
|
{
|
|
|
|
|
mouse.State.Position += delta;
|
|
|
|
|
}
|
2014-07-16 10:18:24 +00:00
|
|
|
|
|
|
|
|
|
CursorPosition = new Vector2(
|
2014-07-17 09:20:01 +00:00
|
|
|
|
MathHelper.Clamp(CursorPosition.X + delta.X, bounds.Left, bounds.Right - 1),
|
|
|
|
|
MathHelper.Clamp(CursorPosition.Y + delta.Y, bounds.Top, bounds.Bottom - 1));
|
2014-07-17 07:25:16 +00:00
|
|
|
|
UpdateCursor();
|
2014-07-16 09:24:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandlePointerMotionAbsolute(MouseDevice mouse, PointerEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (mouse != null)
|
|
|
|
|
{
|
2014-07-16 10:18:24 +00:00
|
|
|
|
mouse.State.Position = new Vector2(e.X, e.Y);
|
2014-07-16 09:24:53 +00:00
|
|
|
|
}
|
2014-07-16 10:18:24 +00:00
|
|
|
|
|
|
|
|
|
CursorPosition = new Vector2(
|
|
|
|
|
e.TransformedX(bounds.Width),
|
|
|
|
|
e.TransformedY(bounds.Height));
|
2014-07-17 07:25:16 +00:00
|
|
|
|
UpdateCursor();
|
2014-07-16 09:24:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 23:15:44 +00:00
|
|
|
|
static int GetId(IntPtr device)
|
|
|
|
|
{
|
|
|
|
|
return LibInput.DeviceGetData(device).ToInt32();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 09:24:53 +00:00
|
|
|
|
KeyboardDevice GetKeyboard(IntPtr device)
|
|
|
|
|
{
|
|
|
|
|
int id = GetId(device);
|
|
|
|
|
KeyboardDevice keyboard = Keyboards.FromHardwareId(id);
|
|
|
|
|
if (keyboard == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] Keyboard {0} does not exist in device list.", id);
|
|
|
|
|
}
|
|
|
|
|
return keyboard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MouseDevice GetMouse(IntPtr device)
|
|
|
|
|
{
|
|
|
|
|
int id = GetId(device);
|
|
|
|
|
MouseDevice mouse = Mice.FromHardwareId(id);
|
|
|
|
|
if (mouse == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] Mouse {0} does not exist in device list.", id);
|
|
|
|
|
}
|
|
|
|
|
return mouse;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 15:42:17 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IKeyboardDriver2 implementation
|
|
|
|
|
|
|
|
|
|
KeyboardState IKeyboardDriver2.GetState()
|
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
lock (Sync)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
KeyboardState state = new KeyboardState();
|
|
|
|
|
foreach (KeyboardDevice keyboard in Keyboards)
|
|
|
|
|
{
|
|
|
|
|
state.MergeBits(keyboard.State);
|
|
|
|
|
}
|
|
|
|
|
return state;
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KeyboardState IKeyboardDriver2.GetState(int index)
|
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
lock (Sync)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
KeyboardDevice device = Keyboards.FromIndex(index);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
return device.State;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new KeyboardState();
|
|
|
|
|
}
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string IKeyboardDriver2.GetDeviceName(int index)
|
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
lock (Sync)
|
2014-07-14 23:15:44 +00:00
|
|
|
|
{
|
2014-07-15 15:28:31 +00:00
|
|
|
|
KeyboardDevice device = Keyboards.FromIndex(index);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
return device.Name;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
2014-07-14 23:15:44 +00:00
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IMouseDriver2 implementation
|
|
|
|
|
|
|
|
|
|
MouseState IMouseDriver2.GetState()
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
lock (Sync)
|
|
|
|
|
{
|
|
|
|
|
MouseState state = new MouseState();
|
|
|
|
|
foreach (MouseDevice mouse in Mice)
|
|
|
|
|
{
|
|
|
|
|
state.MergeBits(mouse.State);
|
|
|
|
|
}
|
|
|
|
|
return state;
|
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MouseState IMouseDriver2.GetState(int index)
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
lock (Sync)
|
|
|
|
|
{
|
|
|
|
|
MouseDevice device = Mice.FromIndex(index);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
return device.State;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new MouseState();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IMouseDriver2.SetPosition(double x, double y)
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
// Todo: this does not appear to be supported in libinput.
|
|
|
|
|
// We will have to emulate this in the KMS mouse rendering code.
|
2014-07-17 07:25:16 +00:00
|
|
|
|
CursorOffset = new Vector2(
|
|
|
|
|
(float)x - CursorPosition.X,
|
|
|
|
|
(float)y - CursorPosition.Y);
|
|
|
|
|
UpdateCursor();
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MouseState IMouseDriver2.GetCursorState()
|
|
|
|
|
{
|
2014-07-16 09:24:53 +00:00
|
|
|
|
MouseState state = (this as IMouseDriver2).GetState();
|
|
|
|
|
state.Position = CursorPosition + CursorOffset;
|
|
|
|
|
return state;
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IDisposable implementation
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
if (input_context != IntPtr.Zero)
|
|
|
|
|
{
|
2014-07-18 08:05:13 +00:00
|
|
|
|
Debug.Print("[Input] Destroying libinput context");
|
2014-07-14 15:42:17 +00:00
|
|
|
|
LibInput.Suspend(input_context);
|
|
|
|
|
Interlocked.Increment(ref exit);
|
|
|
|
|
|
|
|
|
|
LibInput.DestroyContext(input_context);
|
|
|
|
|
input_context = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (udev != IntPtr.Zero)
|
|
|
|
|
{
|
2014-07-18 08:05:13 +00:00
|
|
|
|
Debug.Print("[Input] Destroying udev context");
|
2014-07-14 15:42:17 +00:00
|
|
|
|
Udev.Destroy(udev);
|
|
|
|
|
udev = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input_interface = null;
|
|
|
|
|
}
|
2014-07-18 08:05:13 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Input] {0} leaked. Did you forget to call Dispose()?", GetType().FullName);
|
|
|
|
|
}
|
2014-07-14 15:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~LinuxInput()
|
|
|
|
|
{
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|