Ejemplo n.º 1
0
        private static void MouseTest(MouseDevice mouse)
        {
            Console.WriteLine("Wait for usbip...");
            while (true)
            {
                Console.Write(".");

                if (mouse.Connected == true)
                {
                    break;
                }

                Thread.Sleep(1000);
            }

            while (true)
            {
                Console.Write("Mouse> ");
                string text = Console.ReadLine();

                Thread.Sleep(2000);

                if (text == "quit")
                {
                    mouse.Dispose();
                    break;
                }

                Thread.Sleep(2000);
                mouse.SendText(text);
            }
        }
Ejemplo n.º 2
0
        protected virtual unsafe IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool unicode = UnmanagedMethods.IsWindowUnicode(hWnd);

            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)UnmanagedMethods.GetMessageTime());

            RawInputEventArgs e = null;

            switch ((UnmanagedMethods.WindowsMessage)msg)
            {
            case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
                var wa = (UnmanagedMethods.WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case UnmanagedMethods.WindowActivate.WA_ACTIVE:
                case UnmanagedMethods.WindowActivate.WA_CLICKACTIVE:
                    Activated?.Invoke();
                    break;

                case UnmanagedMethods.WindowActivate.WA_INACTIVE:
                    Deactivated?.Invoke();
                    break;
                }

                return(IntPtr.Zero);

            case WindowsMessage.WM_NCCALCSIZE:
                if (ToInt32(wParam) == 1 && !_decorated)
                {
                    return(IntPtr.Zero);
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_CLOSE:
                bool?preventClosing = Closing?.Invoke();
                if (preventClosing == true)
                {
                    return(IntPtr.Zero);
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_DESTROY:
                //Window doesn't exist anymore
                _hwnd = IntPtr.Zero;
                //Remove root reference to this class, so unmanaged delegate can be collected
                s_instances.Remove(this);
                Closed?.Invoke();
                if (_parent != null)
                {
                    _parent._disabledBy.Remove(this);
                    _parent.UpdateEnabled();
                }
                _mouseDevice.Dispose();
                _touchDevice?.Dispose();
                //Free other resources
                Dispose();
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DPICHANGED:
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = Marshal.PtrToStructure <UnmanagedMethods.RECT>(lParam);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);
                SetWindowPos(hWnd,
                             IntPtr.Zero,
                             newDisplayRect.left,
                             newDisplayRect.top,
                             newDisplayRect.right - newDisplayRect.left,
                             newDisplayRect.bottom - newDisplayRect.top,
                             SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_KEYDOWN:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYDOWN:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_MENUCHAR:
                // mute the system beep
                return((IntPtr)((Int32)UnmanagedMethods.MenuCharParam.MNC_CLOSE << 16));

            case UnmanagedMethods.WindowsMessage.WM_KEYUP:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYUP:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_CHAR:
                // Ignore control chars
                if (ToInt32(wParam) >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp, _owner,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN
                            ? RawPointerEventType.LeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN
                                ? RawPointerEventType.RightButtonDown
                                : RawPointerEventType.MiddleButtonDown,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONUP:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONUP
                            ? RawPointerEventType.LeftButtonUp
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONUP
                                ? RawPointerEventType.RightButtonUp
                                : RawPointerEventType.MiddleButtonUp,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEMOVE:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                if (!_trackingMouse)
                {
                    var tm = new UnmanagedMethods.TRACKMOUSEEVENT
                    {
                        cbSize      = Marshal.SizeOf <UnmanagedMethods.TRACKMOUSEEVENT>(),
                        dwFlags     = 2,
                        hwndTrack   = _hwnd,
                        dwHoverTime = 0,
                    };

                    UnmanagedMethods.TrackMouseEvent(ref tm);
                }

                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    RawPointerEventType.Move,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));

                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEWHEEL:
                e = new RawMouseWheelEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    PointToClient(PointFromLParam(lParam)),
                    new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEHWHEEL:
                e = new RawMouseWheelEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    PointToClient(PointFromLParam(lParam)),
                    new Vector(-(ToInt32(wParam) >> 16) / wheelDelta, 0), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSELEAVE:
                _trackingMouse = false;
                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    RawPointerEventType.LeaveWindow,
                    new Point(-1, -1), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN
                            ? RawPointerEventType.NonClientLeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN
                                ? RawPointerEventType.RightButtonDown
                                : RawPointerEventType.MiddleButtonDown,
                    PointToClient(PointFromLParam(lParam)), GetMouseModifiers(wParam));
                break;

            case WindowsMessage.WM_TOUCH:
                var touchInputCount = wParam.ToInt32();

                var pTouchInputs = stackalloc TOUCHINPUT[touchInputCount];
                var touchInputs  = new Span <TOUCHINPUT>(pTouchInputs, touchInputCount);

                if (GetTouchInputInfo(lParam, (uint)touchInputCount, pTouchInputs, Marshal.SizeOf <TOUCHINPUT>()))
                {
                    foreach (var touchInput in touchInputs)
                    {
                        Input?.Invoke(new RawTouchEventArgs(_touchDevice, touchInput.Time,
                                                            _owner,
                                                            touchInput.Flags.HasFlagCustom(TouchInputFlags.TOUCHEVENTF_UP) ?
                                                            RawPointerEventType.TouchEnd :
                                                            touchInput.Flags.HasFlagCustom(TouchInputFlags.TOUCHEVENTF_DOWN) ?
                                                            RawPointerEventType.TouchBegin :
                                                            RawPointerEventType.TouchUpdate,
                                                            PointToClient(new PixelPoint(touchInput.X / 100, touchInput.Y / 100)),
                                                            WindowsKeyboardDevice.Instance.Modifiers,
                                                            touchInput.Id));
                    }
                    CloseTouchInputHandle(lParam);
                    return(IntPtr.Zero);
                }

                break;

            case WindowsMessage.WM_NCPAINT:
                if (!_decorated)
                {
                    return(IntPtr.Zero);
                }
                break;

            case WindowsMessage.WM_NCACTIVATE:
                if (!_decorated)
                {
                    return(new IntPtr(1));
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_PAINT:
                using (_rendererLock.Lock())
                {
                    UnmanagedMethods.PAINTSTRUCT ps;
                    if (UnmanagedMethods.BeginPaint(_hwnd, out ps) != IntPtr.Zero)
                    {
                        var f = Scaling;
                        var r = ps.rcPaint;
                        Paint?.Invoke(new Rect(r.left / f, r.top / f, (r.right - r.left) / f,
                                               (r.bottom - r.top) / f));
                        UnmanagedMethods.EndPaint(_hwnd, ref ps);
                    }
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_SIZE:
                using (_rendererLock.Lock())
                {
                    // Do nothing here, just block until the pending frame render is completed on the render thread
                }
                var size = (UnmanagedMethods.SizeCommand)wParam;

                if (Resized != null &&
                    (size == UnmanagedMethods.SizeCommand.Restored ||
                     size == UnmanagedMethods.SizeCommand.Maximized))
                {
                    var clientSize = new Size(ToInt32(lParam) & 0xffff, ToInt32(lParam) >> 16);
                    Resized(clientSize / Scaling);
                }

                var windowState = size == SizeCommand.Maximized ? WindowState.Maximized
                        : (size == SizeCommand.Minimized ? WindowState.Minimized : WindowState.Normal);

                if (windowState != _lastWindowState)
                {
                    _lastWindowState = windowState;
                    WindowStateChanged?.Invoke(windowState);
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_MOVE:
                PositionChanged?.Invoke(new PixelPoint((short)(ToInt32(lParam) & 0xffff), (short)(ToInt32(lParam) >> 16)));
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_GETMINMAXINFO:

                MINMAXINFO mmi = Marshal.PtrToStructure <UnmanagedMethods.MINMAXINFO>(lParam);

                if (_minSize.Width > 0)
                {
                    mmi.ptMinTrackSize.X = (int)((_minSize.Width * Scaling) + BorderThickness.Left + BorderThickness.Right);
                }

                if (_minSize.Height > 0)
                {
                    mmi.ptMinTrackSize.Y = (int)((_minSize.Height * Scaling) + BorderThickness.Top + BorderThickness.Bottom);
                }

                if (!Double.IsInfinity(_maxSize.Width) && _maxSize.Width > 0)
                {
                    mmi.ptMaxTrackSize.X = (int)((_maxSize.Width * Scaling) + BorderThickness.Left + BorderThickness.Right);
                }

                if (!Double.IsInfinity(_maxSize.Height) && _maxSize.Height > 0)
                {
                    mmi.ptMaxTrackSize.Y = (int)((_maxSize.Height * Scaling) + BorderThickness.Top + BorderThickness.Bottom);
                }

                Marshal.StructureToPtr(mmi, lParam, true);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DISPLAYCHANGE:
                (Screen as ScreenImpl)?.InvalidateScreensCache();
                return(IntPtr.Zero);
            }

#if USE_MANAGED_DRAG
            if (_managedDrag.PreprocessInputEvent(ref e))
            {
                return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
            }
#endif

            if (e != null && Input != null)
            {
                Input(e);

                if (e.Handled)
                {
                    return(IntPtr.Zero);
                }
            }

            using (_rendererLock.Lock())
                return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
        }
Ejemplo n.º 3
0
        protected virtual unsafe IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool unicode = UnmanagedMethods.IsWindowUnicode(hWnd);

            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)UnmanagedMethods.GetMessageTime());

            RawInputEventArgs e = null;

            switch ((UnmanagedMethods.WindowsMessage)msg)
            {
            case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
                var wa = (UnmanagedMethods.WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case UnmanagedMethods.WindowActivate.WA_ACTIVE:
                case UnmanagedMethods.WindowActivate.WA_CLICKACTIVE:
                    Activated?.Invoke();
                    break;

                case UnmanagedMethods.WindowActivate.WA_INACTIVE:
                    Deactivated?.Invoke();
                    break;
                }

                return(IntPtr.Zero);

            case WindowsMessage.WM_NCCALCSIZE:
                if (ToInt32(wParam) == 1 && _decorated != SystemDecorations.Full)
                {
                    return(IntPtr.Zero);
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_CLOSE:
                bool?preventClosing = Closing?.Invoke();
                if (preventClosing == true)
                {
                    return(IntPtr.Zero);
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_DESTROY:
                //Window doesn't exist anymore
                _hwnd = IntPtr.Zero;
                //Remove root reference to this class, so unmanaged delegate can be collected
                s_instances.Remove(this);
                Closed?.Invoke();
                if (_parent != null)
                {
                    _parent._disabledBy.Remove(this);
                    _parent.UpdateEnabled();
                }
                _mouseDevice.Dispose();
                _touchDevice?.Dispose();
                //Free other resources
                Dispose();
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DPICHANGED:
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = Marshal.PtrToStructure <UnmanagedMethods.RECT>(lParam);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);
                SetWindowPos(hWnd,
                             IntPtr.Zero,
                             newDisplayRect.left,
                             newDisplayRect.top,
                             newDisplayRect.right - newDisplayRect.left,
                             newDisplayRect.bottom - newDisplayRect.top,
                             SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_KEYDOWN:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYDOWN:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_MENUCHAR:
                // mute the system beep
                return((IntPtr)((Int32)UnmanagedMethods.MenuCharParam.MNC_CLOSE << 16));

            case UnmanagedMethods.WindowsMessage.WM_KEYUP:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYUP:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_CHAR:
                // Ignore control chars
                if (ToInt32(wParam) >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp, _owner,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_XBUTTONDOWN:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    (UnmanagedMethods.WindowsMessage)msg switch
                {
                    UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN => RawPointerEventType.LeftButtonDown,
                    UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN => RawPointerEventType.RightButtonDown,
                    UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN => RawPointerEventType.MiddleButtonDown,
                    UnmanagedMethods.WindowsMessage.WM_XBUTTONDOWN =>
                    HighWord(ToInt32(wParam)) == 1 ? RawPointerEventType.XButton1Down : RawPointerEventType.XButton2Down
                },
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;