Ejemplo n.º 1
0
        /// <summary>
        /// Registers the devices that supply the raw input data.
        /// </summary>
        /// <param name="usagePage">The usage page.</param>
        /// <param name="usageId">The usage id.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="target">The target.</param>
        /// <param name="options">The options.</param>
        public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, RegisterDeviceOptions options = RegisterDeviceOptions.Default)
        {
            var rawInputDevices = new RawInputDevice[1];

            rawInputDevices[0].UsagePage = (short)usagePage;
            rawInputDevices[0].Usage     = (short)usageId;
            rawInputDevices[0].Flags     = (int)flags;
            rawInputDevices[0].Target    = target;

            // Register this device
            RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf <RawInputDevice>());

            if (options != RegisterDeviceOptions.NoFiltering && rawInputMessageFilter == null)
            {
                rawInputMessageFilter = new RawInputMessageFilter();
                if (options == RegisterDeviceOptions.Default)
                {
                    Application.AddMessageFilter(rawInputMessageFilter);
                }
                else
                {
                    MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter);
                }
            }
        }
Ejemplo n.º 2
0
 public static void ClearMessageFilter()
 {
     if (m_filter != null)
     {
         MessageFilterHook.RemoveMessageFilter(m_filterWindow, m_filter);
         m_filter       = null;
         m_filterWindow = IntPtr.Zero;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Function to unregister the device from the raw input provider.
        /// </summary>
        /// <param name="device">The device to unregister from the raw input provider.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="device"/> parameter is <b>null</b>.</exception>
        /// <remarks>
        /// This will unregister a previously registered <see cref="IGorgonRawInputDevice"/>. When the last device of a specific type (e.g. a mouse, keyboard, etc...) is unregistered, then the
        /// Raw Input messages for that device type will also be unregistered and the application will no longer receive messages from that type of device.
        /// </remarks>
        public void UnregisterDevice(IGorgonRawInputDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            lock (_syncLock)
            {
                var key = new DeviceKey
                {
                    DeviceType   = device.DeviceType,
                    DeviceHandle = device.Handle
                };

                switch (device.DeviceType)
                {
                case RawInputType.Keyboard:
                    _keyboardDevices.Remove(key);
                    break;

                case RawInputType.Mouse:
                    _mouseDevices.Remove(key);
                    break;

                case RawInputType.HID:
                    _hids.Remove(key);
                    break;

                default:
                    throw new ArgumentException(string.Format(Resources.GORINP_RAW_ERR_UNKNOWN_DEVICE_TYPE, device.DeviceType), nameof(device));
                }

                if (!_devices.ContainsKey(key))
                {
                    return;
                }

                _devices.Remove(key);

                // If all devices of this type have been unregistered, unregister with raw input as well.
                if ((_devices.Count(item => item.Value.DeviceType == device.DeviceType) == 0) &&
                    (RawInputApi.GetDeviceRegistration(device.DeviceUsage) != null))
                {
                    RawInputApi.UnregisterRawInputDevice(device.DeviceUsage);
                }

                // If we have no more registered devices, then uninstall the filter.
                if (_devices.Count != 0)
                {
                    return;
                }

                _filter?.Dispose();
                _filter = null;
            }
        }
Ejemplo n.º 4
0
 // This allocates, however it should be used only when launching app
 public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target)
 {
     Device.RegisterDevice(usagePage, usageId, flags, target, false);
     if (m_filter == null)
     {
         m_filterWindow = target;
         m_filter       = new RawInputMessageFilter();
         MessageFilterHook.AddMessageFilter(m_filterWindow, m_filter);
     }
 }
Ejemplo n.º 5
0
        public static void FixKeyboard(Game game)
        {
            IntPtr hWnd = game.Window.Handle;

            Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, DeviceFlags.None, hWnd, RegisterDeviceOptions.Default);
            var filter = new RawInputMessageFilter();

            filterCache[hWnd] = filter;
            MessageFilterHook.AddMessageFilter(hWnd, filter);
        }
Ejemplo n.º 6
0
 // This allocates, however it should be used only when launching app
 public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target)
 {
     Device.RegisterDevice(usagePage, usageId, flags, target, false);
     if (m_filter == null)
     {
         m_filterWindow = target;
         m_filter = new RawInputMessageFilter();
         MessageFilterHook.AddMessageFilter(m_filterWindow, m_filter);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            // Multiple threads should -not- call dispose.
            lock (_syncLock)
            {
                UnhookRawInput();

                _mouseDevices.Clear();
                _keyboardDevices.Clear();
                _hids.Clear();

                _devices.Clear();
                _filter?.Dispose();
                _filter = null;
            }

            GC.SuppressFinalize(this);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Registers the devices that supply the raw input data.
        /// </summary>
        /// <param name="usagePage">The usage page.</param>
        /// <param name="usageId">The usage id.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="target">The target.</param>
        /// <param name="options">The options.</param>
        public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, RegisterDeviceOptions options = RegisterDeviceOptions.Default)
        {
            var rawInputDevices = new RawInputDevice[1];
            rawInputDevices[0].UsagePage = (short)usagePage;
            rawInputDevices[0].Usage = (short)usageId;
            rawInputDevices[0].Flags = (int)flags;
            rawInputDevices[0].Target = target;

            // Register this device
            RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf<RawInputDevice>());

            if (options != RegisterDeviceOptions.NoFiltering && rawInputMessageFilter == null)
            {
                rawInputMessageFilter = new RawInputMessageFilter();
                if (options == RegisterDeviceOptions.Default)
                {
                    Application.AddMessageFilter(rawInputMessageFilter);
                }
                else
                {
                    MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Function to register the device with the raw input provider.
        /// </summary>
        /// <param name="device">The device to register with the raw input provider.</param>
        /// <param name="settings">[Optional] Settings for the device type.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="device"/> parameter is <b>null</b>.</exception>
        /// <remarks>
        /// <para>
        /// This will register the <see cref="IGorgonRawInputDevice"/> with the application. For the very first device of a specific type (e.g. a mouse, keyboard, etc...) the Raw Input object will set up
        /// the device type registration for the device. This enables an application to start receiving Raw Input messages from a device type.
        /// </para>
        /// <para>
        /// The optional <paramref name="settings"/> parameter allows an application change how raw input handles the device being registered. It can be used to set up background input monitoring, or a
        /// target window for raw input messages (which must be set if the background option is turned on). By default, there is no background message processing and no target window (messages go to
        /// whichever window has focus).
        /// </para>
        /// <para>
        /// Every call to this method should be paired with a call to <see cref="UnregisterDevice"/> when the device(s) are no longer needed.
        /// </para>
        /// </remarks>
        public void RegisterDevice(IGorgonRawInputDevice device, GorgonRawInputSettings?settings = null)
        {
            IntPtr targetHandle = IntPtr.Zero;

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (settings == null)
            {
                settings = new GorgonRawInputSettings();
            }

            lock (_syncLock)
            {
                // If we've not set up the filter yet, then add it to the window now.
                if (_filter == null)
                {
                    _filter = new RawInputMessageFilter(_keyboardDevices, _mouseDevices, _hids, _applicationWindow, _useNativeHook);
                }

                var key = new DeviceKey
                {
                    DeviceType   = device.DeviceType,
                    DeviceHandle = device.Handle
                };

                if (_devices.ContainsKey(key))
                {
                    return;
                }

                switch (device.DeviceType)
                {
                case RawInputType.Keyboard:
                    _keyboardDevices.Add(key, (IRawInputDeviceData <GorgonRawKeyboardData>)device);
                    break;

                case RawInputType.Mouse:
                    _mouseDevices.Add(key, (IRawInputDeviceData <GorgonRawMouseData>)device);
                    break;

                case RawInputType.HID:
                    _hids.Add(key, (IRawInputDeviceData <GorgonRawHIDData>)device);
                    break;

                default:
                    throw new ArgumentException(string.Format(Resources.GORINP_RAW_ERR_UNKNOWN_DEVICE_TYPE, device.DeviceType), nameof(device));
                }

                _devices.Add(key, device);

                // Get the current device registration properties.
                RAWINPUTDEVICE?deviceReg = RawInputApi.GetDeviceRegistration(device.DeviceUsage);
                if (deviceReg != null)
                {
                    // Remove the device before updating it.
                    UnregisterDevice(device);
                }

                // If we omit the target window, and specify background messages, we'll use the application window instead.
                // This is because Raw Input requires that background devices have a window target.
                if ((settings.Value.TargetWindow.IsNull) && (settings.Value.AllowBackground))
                {
                    unsafe
                    {
                        settings = new GorgonRawInputSettings
                        {
                            TargetWindow    = new GorgonReadOnlyPointer((void *)_applicationWindow, IntPtr.Size),
                            AllowBackground = true
                        };
                    }
                }

                RawInputDeviceFlags flags = RawInputDeviceFlags.None;

                if (settings.Value.AllowBackground)
                {
                    flags |= RawInputDeviceFlags.InputSink;
                }

                RawInputApi.RegisterRawInputDevice(device.DeviceUsage, targetHandle, flags);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Registers the devices that supply the raw input data.
        /// </summary>
        /// <param name="usagePage">The usage page.</param>
        /// <param name="usageId">The usage id.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="target">The target.</param>
        /// <param name="addMessageFilter">if set to <c>true</c> register message filter to Application.AddMessageFilter.</param>
        public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addMessageFilter = true)
        {
            if (target == IntPtr.Zero)
            {
                target = Win32Native.GetFocus();
            }

            var rawInputDevices = new RawInputDevice[1];
            rawInputDevices[0].UsagePage = (short) usagePage;
            rawInputDevices[0].Usage = (short) usageId;
            rawInputDevices[0].Flags = (int) flags;
            rawInputDevices[0].Target = target;

            // Register this device
            RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf<RawInputDevice>());

            if (rawInputMessageFilter == null && addMessageFilter)
            {
                rawInputMessageFilter = new RawInputMessageFilter();
                MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Registers the devices that supply the raw input data.
        /// </summary>
        /// <param name="usagePage">The usage page.</param>
        /// <param name="usageId">The usage id.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="target">The target.</param>
        /// <param name="addMessageFilter">if set to <c>true</c> register message filter to Application.AddMessageFilter.</param>
        public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addMessageFilter = true)
        {
            var rawInputDevices = new RawInputDevice[1];
            rawInputDevices[0].UsagePage = (short) usagePage;
            rawInputDevices[0].Usage = (short) usageId;
            rawInputDevices[0].Flags = (int) flags;
            rawInputDevices[0].Target = target;

            // Register this device
            RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf<RawInputDevice>());

            if (rawInputMessageFilter == null && addMessageFilter)
            {
                rawInputMessageFilter = new RawInputMessageFilter();
                Application.AddMessageFilter(rawInputMessageFilter);
            }
        }
Ejemplo n.º 12
0
 public static void ClearMessageFilter()
 {
     if (m_filter != null)
     {
         MessageFilterHook.RemoveMessageFilter(m_filterWindow, m_filter);
         m_filter = null;
         m_filterWindow = IntPtr.Zero;
     }
 }