예제 #1
0
        public static void Stop()
        {
            //Dispose of the PS3 tablet
            foreach (var t in Tablets)
            {
                var ps3 = t.Tablet as PS3InputDevice;

                if (ps3 != null)
                {
                    ps3.Dispose();
                }
            }

            //Dispose of the Xbox 360 wireless USB receiver
            if (Receiver != null)
            {
                Receiver.Dispose();
                Receiver = null;
            }

            Tablets.Clear();

            PPJoyInterface.CloseAllHandles();
        }
예제 #2
0
        private static void _HandleTabletEvents(TabletConnection conn)
        {
            if (conn != null)
            {
                const float TABLET_PAD_WIDTH  = 1920;
                const float TABLET_PAD_HEIGHT = 1080;

                double threshold = ((conn.Settings.PenPressureThreshold / 10.0) *
                                    (_MAX_PEN_PRESSURE_THRESHOLD - _MIN_PEN_PRESSURE_THRESHOLD)) + _MIN_PEN_PRESSURE_THRESHOLD;
                bool penClicked = (conn.Tablet.PenPressure >= threshold);
                if (conn.LastPressure != (conn.Tablet.PenPressure >= threshold))
                {
                    _PerformAction(conn, TabletOptionButton.TabletButton.PenClick, conn.Settings.ClickAction, penClicked);
                }
                conn.LastPressure = (conn.Tablet.PenPressure >= threshold);

                //Send PPJoy data, if necessary
                if (conn.Settings.PPJoyNumber > 0)
                {
                    var buttons = new byte[16];
                    buttons[0]  = (byte)(conn.Tablet.ButtonState.CrossHeld ? 1 : 0);
                    buttons[1]  = (byte)(conn.Tablet.ButtonState.CircleHeld ? 1 : 0);
                    buttons[2]  = (byte)(conn.Tablet.ButtonState.TriangleHeld ? 1 : 0);
                    buttons[3]  = (byte)(conn.Tablet.ButtonState.SquareHeld ? 1 : 0);
                    buttons[4]  = (byte)(conn.Tablet.DPadState.UpHeld ? 1 : 0);
                    buttons[5]  = (byte)(conn.Tablet.DPadState.DownHeld ? 1 : 0);
                    buttons[6]  = (byte)(conn.Tablet.DPadState.LeftHeld ? 1 : 0);
                    buttons[7]  = (byte)(conn.Tablet.DPadState.RightHeld ? 1 : 0);
                    buttons[8]  = (byte)(conn.Tablet.ButtonState.PSHeld ? 1 : 0);
                    buttons[9]  = (byte)(conn.Tablet.ButtonState.SelectHeld ? 1 : 0);
                    buttons[10] = (byte)(conn.Tablet.ButtonState.StartHeld ? 1 : 0);
                    buttons[11] = (byte)(penClicked ? 1 : 0);
                    buttons[12] = (byte)(conn.Tablet.ButtonState.LeftStickHeld ? 1 : 0);
                    buttons[13] = (byte)(conn.Tablet.ButtonState.RightStickHeld ? 1 : 0);
                    buttons[14] = (byte)(conn.Tablet.ButtonState.LeftButtonHeld ? 1 : 0);
                    buttons[15] = (byte)(conn.Tablet.ButtonState.RightButtonHeld ? 1 : 0);
                    var axes = new int[11];
                    axes[0] = conn.Tablet.AccelerometerData.XAxis * 32767;
                    axes[1] = conn.Tablet.AccelerometerData.YAxis * 32767;
                    axes[2] = conn.Tablet.AccelerometerData.ZAxis * 32767;
                    if (conn.Tablet.PressureType == TabletPressureType.FingerPressed ||
                        conn.Tablet.PressureType == TabletPressureType.PenPressed)
                    {
                        axes[3] = Convert.ToInt32(32767 * (conn.Tablet.PressurePoint.X / TABLET_PAD_WIDTH));
                        axes[4] = Convert.ToInt32(32767 * (conn.Tablet.PressurePoint.Y / TABLET_PAD_HEIGHT));
                    }
                    else
                    {
                        //Centered
                        axes[3] = Convert.ToInt32(32767 * (TABLET_PAD_WIDTH / 2.0));
                        axes[4] = Convert.ToInt32(32767 * (TABLET_PAD_HEIGHT / 2.0));
                    }
                    var controller = conn.Tablet as Xbox360InputDevice;
                    if (controller != null)
                    {
                        axes[5]  = 32767 * (controller.LeftTrigger / 255);
                        axes[6]  = 32767 * (controller.RightTrigger / 255);
                        axes[7]  = 32767 * ((controller.X1 + 32768) / 65535);
                        axes[8]  = 32767 * ((controller.Y1 + 32768) / 65535);
                        axes[9]  = 32767 * ((controller.X2 + 32768) / 65535);
                        axes[10] = 32767 * ((controller.Y2 + 32768) / 65535);
                    }
                    PPJoyInterface.Update(conn.Settings.PPJoyNumber, axes, buttons);
                }

                bool doUp    = _IsActionRequested(conn, TabletOptionButton.ButtonAction.MoveUp);
                bool doDown  = _IsActionRequested(conn, TabletOptionButton.ButtonAction.MoveDown);
                bool doLeft  = _IsActionRequested(conn, TabletOptionButton.ButtonAction.MoveLeft);
                bool doRight = _IsActionRequested(conn, TabletOptionButton.ButtonAction.MoveRight);

                if ((conn.Tablet.PressureType == TabletPressureType.PenPressed) ||
                    (conn.Settings.AllowFingerMovement && conn.Tablet.PressureType == TabletPressureType.FingerPressed))
                {
                    if (conn.Settings.MovementType == TabletSettings.TabletMovementType.Absolute)
                    {
                        //Calculate the absolute coordinates of the new mouse position
                        float actualWidth  = conn.Settings.AllowAllDisplays ? SystemInformation.VirtualScreen.Width : conn.CurrentDisplay.Bounds.Width;
                        float actualHeight = conn.Settings.AllowAllDisplays ? SystemInformation.VirtualScreen.Height : conn.CurrentDisplay.Bounds.Height;
                        float width        = 65536;
                        float height       = 65536;
                        float xStart       = 0;
                        float yStart       = 0;
                        float x            = 0;
                        float y            = 0;

                        if (conn.Settings.RestrictToCurrentWindow)
                        {
                            //We're only looking at the current window, which could be on any display
                            var  v = SystemInformation.VirtualScreen;
                            RECT rect;
                            var  hWnd = GetForegroundWindow(); GetWindowRect(hWnd, out rect);
                            int  windowWidth = rect.Right - rect.Left, windowHeight = rect.Bottom - rect.Top, windowX = rect.Left, windowY = rect.Top;
                            actualWidth  = windowWidth;
                            actualHeight = windowHeight;

                            width  = (float)Math.Round((float)((float)windowWidth / (float)v.Width) * 65536.0, 0);
                            height = (float)Math.Round((float)((float)windowHeight / (float)v.Height) * 65536.0, 0);
                            xStart = ((((float)windowX - (float)v.X) / (float)v.Width) * (float)65536.0);
                            yStart = ((((float)windowY - (float)v.Y) / (float)v.Height) * (float)65536.0);
                        }
                        else if (!conn.Settings.AllowAllDisplays)
                        {
                            //Find the absolute coordinates for start of current display (which will be X and Y starting offsets)
                            var v = SystemInformation.VirtualScreen;
                            width = (float)Math.Round((float)((float)conn.CurrentDisplay.Bounds.Width /
                                                              (float)v.Width) * 65536.0, 0);
                            height = (float)Math.Round((float)((float)conn.CurrentDisplay.Bounds.Height /
                                                               (float)v.Height) * 65536.0, 0);
                            xStart = ((((float)conn.CurrentDisplay.Bounds.X -
                                        (float)v.X) / (float)v.Width) * (float)65536.0);
                            yStart = ((((float)conn.CurrentDisplay.Bounds.Y -
                                        (float)v.Y) / (float)v.Height) * (float)65536.0);
                        }

                        if (conn.Settings.MaintainAspectRatio)
                        {
                            //Get the current style based on which is higher, width or height
                            var style = ((TABLET_PAD_HEIGHT / TABLET_PAD_WIDTH) >= (actualHeight / actualWidth)) ?
                                        DockOption.DockStyle.Vertical : DockOption.DockStyle.Horizontal;

                            //Translate the width and height to tablet proportions
                            float tabletWidth = (style == DockOption.DockStyle.Vertical ? TABLET_PAD_WIDTH :
                                                 TABLET_PAD_WIDTH * ((TABLET_PAD_HEIGHT / TABLET_PAD_WIDTH) / (actualHeight / actualWidth)));
                            float tabletHeight = (style == DockOption.DockStyle.Horizontal ? TABLET_PAD_HEIGHT :
                                                  TABLET_PAD_HEIGHT * ((actualHeight / actualWidth) / (TABLET_PAD_HEIGHT / TABLET_PAD_WIDTH)));
                            float tabletX = 0;
                            float tabletY = 0;
                            if (style == DockOption.DockStyle.Horizontal)
                            {
                                switch (conn.Settings.HorizontalDock)
                                {
                                case DockOption.DockOptionValue.Center:
                                    tabletX = (TABLET_PAD_WIDTH - tabletWidth) / 2;
                                    break;

                                case DockOption.DockOptionValue.Right:
                                    tabletX = TABLET_PAD_WIDTH - tabletWidth;
                                    break;

                                default:
                                    break;
                                }
                            }
                            else
                            {
                                switch (conn.Settings.VerticalDock)
                                {
                                case DockOption.DockOptionValue.Center:
                                    tabletY = (TABLET_PAD_HEIGHT - tabletHeight) / 2;
                                    break;

                                case DockOption.DockOptionValue.Right:
                                    tabletY = TABLET_PAD_HEIGHT - tabletHeight;
                                    break;

                                default:
                                    break;
                                }
                            }

                            //Determine whether our current pressure point is within the box
                            x = conn.Tablet.PressurePoint.X;
                            y = conn.Tablet.PressurePoint.Y;
                            if (x < tabletX)
                            {
                                x = tabletX;
                            }
                            if (x > (tabletX + tabletWidth))
                            {
                                x = tabletX + tabletWidth;
                            }
                            if (y < tabletY)
                            {
                                y = tabletY;
                            }
                            if (y > (tabletY + tabletHeight))
                            {
                                y = tabletY + tabletHeight;
                            }

                            //It is, so set the coordinates appropriately
                            x = (((x - tabletX) / tabletWidth) * width) + xStart;
                            y = (((y - tabletY) / tabletHeight) * height) + yStart;
                        }
                        else
                        {
                            x = ((conn.Tablet.PressurePoint.X / TABLET_PAD_WIDTH) * width) + xStart;
                            y = (conn.Tablet.PressurePoint.Y / TABLET_PAD_HEIGHT * height) + yStart;
                        }

                        if (Cursor.Position.X != x && Cursor.Position.Y != y) //not sure if this respects virtual desktop coordinates
                        {
                            mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK,
                                        (int)x, (int)y, 0, UIntPtr.Zero);
                        }
                    }
                    else if (conn.Settings.MovementType == TabletSettings.TabletMovementType.Relative)
                    {
                        //Based on last position, determine whether to move in a certain direction or not
                        if (conn.LastPressurePoint.HasValue)
                        {
                            const int DELTA     = 1;
                            int       precision = conn.Settings.Precision;
                            if ((conn.Tablet.PressurePoint.X - conn.LastPressurePoint.Value.X) >= precision)
                            {
                                conn.HorizontalDelta++;
                            }
                            if ((conn.Tablet.PressurePoint.Y - conn.LastPressurePoint.Value.Y) >= precision)
                            {
                                conn.VerticalDelta++;
                            }
                            if ((conn.Tablet.PressurePoint.X - conn.LastPressurePoint.Value.X) <= -precision)
                            {
                                conn.HorizontalDelta--;
                            }
                            if ((conn.Tablet.PressurePoint.Y - conn.LastPressurePoint.Value.Y) <= -precision)
                            {
                                conn.VerticalDelta--;
                            }

                            if (conn.VerticalDelta <= -DELTA)
                            {
                                doUp = true;
                            }
                            if (conn.VerticalDelta >= DELTA)
                            {
                                doDown = true;
                            }
                            if (conn.HorizontalDelta <= -DELTA)
                            {
                                doLeft = true;
                            }
                            if (conn.HorizontalDelta >= DELTA)
                            {
                                doRight = true;
                            }
                        }

                        if (!conn.LastPressurePoint.HasValue)
                        {
                            conn.LastPressurePoint = new Point(conn.Tablet.PressurePoint.X, conn.Tablet.PressurePoint.Y);
                        }
                        if (doUp || doDown)
                        {
                            conn.LastPressurePoint = new Point(conn.LastPressurePoint.Value.X, conn.Tablet.PressurePoint.Y);
                        }
                        if (doLeft || doRight)
                        {
                            conn.LastPressurePoint = new Point(conn.Tablet.PressurePoint.X, conn.LastPressurePoint.Value.Y);
                        }
                    }
                }
                else
                {
                    conn.LastPressurePoint = null;
                }

                if (doUp || doDown)
                {
                    conn.VerticalDelta = 0;
                }
                if (doLeft || doRight)
                {
                    conn.HorizontalDelta = 0;
                }

                if (doUp || doDown || doLeft || doRight)
                {
                    _accel = Math.Min(conn.Settings.MovementSpeed, _accel + 2);
                }
                else
                {
                    _accel = Math.Max(1, _accel - 1);
                }

                if (doDown)
                {
                    mouse_event(MOUSEEVENTF_MOVE, 0, _accel, 0, UIntPtr.Zero);
                }
                if (doUp)
                {
                    mouse_event(MOUSEEVENTF_MOVE, 0, 0 - _accel, 0, UIntPtr.Zero);
                }
                if (doLeft)
                {
                    mouse_event(MOUSEEVENTF_MOVE, 0 - _accel, 0, 0, UIntPtr.Zero);
                }
                if (doRight)
                {
                    mouse_event(MOUSEEVENTF_MOVE, _accel, 0, 0, UIntPtr.Zero);
                }

                _CheckKeys(conn.Settings.AAction, TabletOptionButton.TabletButton.ACross, conn.Tablet.ButtonState.CrossHeld);
                _CheckKeys(conn.Settings.BAction, TabletOptionButton.TabletButton.BCircle, conn.Tablet.ButtonState.CircleHeld);
                _CheckKeys(conn.Settings.XAction, TabletOptionButton.TabletButton.XSquare, conn.Tablet.ButtonState.SquareHeld);
                _CheckKeys(conn.Settings.YAction, TabletOptionButton.TabletButton.YTriangle, conn.Tablet.ButtonState.TriangleHeld);
                _CheckKeys(conn.Settings.UpAction, TabletOptionButton.TabletButton.Up, conn.Tablet.DPadState.UpHeld);
                _CheckKeys(conn.Settings.DownAction, TabletOptionButton.TabletButton.Down, conn.Tablet.DPadState.DownHeld);
                _CheckKeys(conn.Settings.LeftAction, TabletOptionButton.TabletButton.Left, conn.Tablet.DPadState.LeftHeld);
                _CheckKeys(conn.Settings.RightAction, TabletOptionButton.TabletButton.Right, conn.Tablet.DPadState.RightHeld);
                _CheckKeys(conn.Settings.BackAction, TabletOptionButton.TabletButton.BackSelect, conn.Tablet.ButtonState.SelectHeld);
                _CheckKeys(conn.Settings.StartAction, TabletOptionButton.TabletButton.Start, conn.Tablet.ButtonState.StartHeld);
                _CheckKeys(conn.Settings.GuideAction, TabletOptionButton.TabletButton.PSXboxGuide, conn.Tablet.ButtonState.PSHeld);
            }
        }