Пример #1
0
        private async void OnDesktopsChanged(object sender, DesktopsChangedEventArgs e)
        {
            await EnsureInitialized();

            await mutableStateLock.WaitAsync();

            LocalState.Controlling?controlling = localState as LocalState.Controlling;
            string?activeDisplayId             = null;

            if (controlling != null)
            {
                activeDisplayId = displayLayout.DisplayIds[controlling.ActiveDisplay];
            }

            DebugMessage("recreating display layout");
            displayLayout = CreateDisplayLayout(e.Desktops);
            DebugPrintDisplays();

            if (controlling != null && activeDisplayId != null)
            {
                var nextDisplay = displayLayout.DisplayById[activeDisplayId];
                DebugMessage($"moving from display {controlling.ActiveDisplay} to {nextDisplay}");
                var displayOriginDeltaX = nextDisplay.X - controlling.ActiveDisplay.X;
                var displayOriginDeltaY = nextDisplay.Y - controlling.ActiveDisplay.Y;
                localState = new LocalState.Controlling(controlling.ActiveDesktop, nextDisplay, controlling.CursorPosition.Offset(displayOriginDeltaX, displayOriginDeltaY));
            }

            mutableStateLock.Release();
        }
Пример #2
0
        private async Task HandleUncontrolledMouse(LocalState.Uncontrolled uncontrolled, MouseMoveEventArgs e)
        {
            var nextGlobalPoint = TranslateLocalToGlobal(e.Position);

            if (!uncontrolled.ActiveDisplay.Contains(nextGlobalPoint))
            {
                var next = displayLayout.FindPoint(nextGlobalPoint);
                if (next.HasValue)
                {
                    var(nextDesktop, nextDisplay) = next.Value;
                    if (nextDesktop != selfDesktop)
                    {
                        DebugMessage("blocking local input");
                        inputManager.BlockInput(true);
                        localCursorPosition = await inputManager.MouseListener.GetCursorPosition();

                        DebugMessage($"assuming control of {nextDesktop.Name} on display {nextDisplay} on position {nextGlobalPoint}");
                        localState = new LocalState.Controlling(nextDesktop, nextDisplay, nextGlobalPoint);
                        var nextClient = await workspaceNetwork.GetClientDesktop(nextDesktop.Name);

                        if (nextClient != null)
                        {
                            await nextClient.AssumeControl();

                            await nextClient.MoveMouse(nextGlobalPoint);
                        }
                    }
                    else
                    {
                        DebugMessage($"moved to display {nextDisplay}");
                        localState = new LocalState.Uncontrolled(nextDisplay, selfDesktop.PrimaryDisplay !.Value);
                    }
                }
            }
        }
Пример #3
0
        private async void OnClientDisplaysChanged(object sender, ClientDisplaysChangedEventArgs e)
        {
            await EnsureInitialized();

            await mutableStateLock.WaitAsync();

            var desktopName = e.DesktopName.NormalizeDesktopName();
            var desktop     = desktops.Find(d => d.Name == desktopName);

            if (desktop != null)
            {
                LocalState.Controlling?controlling = localState as LocalState.Controlling;
                string?activeDisplayId             = null;
                if (controlling != null)
                {
                    activeDisplayId = displayLayout.DisplayIds[controlling.ActiveDisplay];
                }

                DebugMessage($"displays for {desktopName} changed");
                desktop.Displays = e.Displays;
                displayLayout    = new DisplayLayout(desktops);
                DebugPrintDisplays();

                if (controlling != null && activeDisplayId != null)
                {
                    if (displayLayout.DisplayById.TryGetValue(activeDisplayId, out Rectangle nextDisplay))
                    {
                        DebugMessage($"moving from display {controlling.ActiveDisplay} to {nextDisplay}");
                        var displayOriginDeltaX = nextDisplay.X - controlling.ActiveDisplay.X;
                        var displayOriginDeltaY = nextDisplay.Y - controlling.ActiveDisplay.Y;
                        localState = new LocalState.Controlling(controlling.ActiveDesktop, nextDisplay, controlling.CursorPosition.Offset(displayOriginDeltaX, displayOriginDeltaY));
                    }
                    else
                    {
                        DebugMessage($"display {activeDisplayId} not found. Returning to local primary display");
                        await ReturnToPrimaryDisplay();
                    }
                }
            }

            mutableStateLock.Release();
        }
Пример #4
0
        private async void OnClientDisconnected(object sender, ClientConnectionChangedEventArgs e)
        {
            await EnsureInitialized();

            await mutableStateLock.WaitAsync();

            string desktopName = e.DesktopName.NormalizeDesktopName();

            LocalState.Controlling?controlling = localState as LocalState.Controlling;
            string?activeDisplayId             = null;

            if (controlling != null && controlling.ActiveDesktop.Name != desktopName)
            {
                activeDisplayId = displayLayout.DisplayIds[controlling.ActiveDisplay];
            }

            DebugMessage($"removing client {desktopName}");
            desktops      = desktops.Where(d => d.Name != desktopName).ToList();
            displayLayout = new DisplayLayout(desktops);
            DebugPrintDisplays();

            if (controlling != null)
            {
                if (activeDisplayId != null)
                {
                    var nextDisplay = displayLayout.DisplayById[activeDisplayId];
                    DebugMessage($"moving from display {controlling.ActiveDisplay} to {nextDisplay}");
                    var displayOriginDeltaX = nextDisplay.X - controlling.ActiveDisplay.X;
                    var displayOriginDeltaY = nextDisplay.Y - controlling.ActiveDisplay.Y;
                    localState = new LocalState.Controlling(controlling.ActiveDesktop, nextDisplay, controlling.CursorPosition.Offset(displayOriginDeltaX, displayOriginDeltaY));
                }
                else
                {
                    DebugMessage($"active desktop disconnected. Returning to local primary display");
                    await ReturnToPrimaryDisplay();
                }
            }

            mutableStateLock.Release();
        }
Пример #5
0
        private async void OnClientConnected(object sender, ClientConnectionChangedEventArgs e)
        {
            await EnsureInitialized();

            string desktopName = e.DesktopName.NormalizeDesktopName();

            if (desktopName == selfName)
            {
                return;
            }

            await mutableStateLock.WaitAsync();

            LocalState.Controlling?controlling = localState as LocalState.Controlling;
            string?activeDisplayId             = null;

            if (controlling != null)
            {
                activeDisplayId = displayLayout.DisplayIds[controlling.ActiveDisplay];
            }

            DebugMessage($"adding client {desktopName}");
            desktops.Add(new Desktop()
            {
                Name = desktopName
            });
            displayLayout = CreateDisplayLayout(configurationProvider.GetDesktopOrder());
            DebugPrintDisplays();

            if (controlling != null && activeDisplayId != null)
            {
                var nextDisplay         = displayLayout.DisplayById[activeDisplayId];
                var displayOriginDeltaX = nextDisplay.X - controlling.ActiveDisplay.X;
                var displayOriginDeltaY = nextDisplay.Y - controlling.ActiveDisplay.Y;
                localState = new LocalState.Controlling(controlling.ActiveDesktop, nextDisplay, controlling.CursorPosition.Offset(displayOriginDeltaX, displayOriginDeltaY));
            }

            mutableStateLock.Release();
        }
Пример #6
0
        private async void OnDisplaysChanged(object sender, DisplaysChangedEventArgs e)
        {
            await EnsureInitialized();

            await mutableStateLock.WaitAsync();

            LocalState.Controlling?controlling = localState as LocalState.Controlling;
            string?activeDisplayId             = null;

            if (controlling != null)
            {
                activeDisplayId = displayLayout.DisplayIds[controlling.ActiveDisplay];
            }

            DebugMessage("own displays changed");
            selfDesktop.Displays       = e.Displays;
            selfDesktop.PrimaryDisplay = e.PrimaryDisplay;
            mouseDeltaDebounceValueX   = e.PrimaryDisplay.Width / 2;
            mouseDeltaDebounceValueY   = e.PrimaryDisplay.Height / 2;
            displayLayout = new DisplayLayout(desktops);
            var allClients = await workspaceNetwork.GetAllClientDesktops();

            await allClients.DisplaysChanged(e.Displays);

            DebugPrintDisplays();

            if (controlling != null && activeDisplayId != null)
            {
                var nextDisplay = displayLayout.DisplayById[activeDisplayId];
                DebugMessage($"moving from display {controlling.ActiveDisplay} to {nextDisplay}");
                var displayOriginDeltaX = nextDisplay.X - controlling.ActiveDisplay.X;
                var displayOriginDeltaY = nextDisplay.Y - controlling.ActiveDisplay.Y;
                DebugMessage(controlling.CursorPosition.Offset(displayOriginDeltaX, displayOriginDeltaY).ToString());
                localState = new LocalState.Controlling(controlling.ActiveDesktop, nextDisplay, controlling.CursorPosition.Offset(displayOriginDeltaX, displayOriginDeltaY));
            }

            mutableStateLock.Release();
        }
Пример #7
0
        private async Task HandleControlledMouse(LocalState.Controlled controlled, MouseMoveEventArgs e)
        {
            var nextGlobalPoint = TranslateLocalToGlobal(e.Position);

            if (!selfDesktop.Displays.Any(d => d.Contains(e.Position))) // selfDesktop.Displays uses local coordinates
            {
                var next = displayLayout.FindPoint(nextGlobalPoint);
                if (next.HasValue)
                {
                    var(nextDesktop, nextDisplay) = next.Value;
                    var controllers = await workspaceNetwork.GetClientDesktops(controlled.By);

                    DebugMessage("resigning from control");
                    await controllers.ResignFromControl();

                    DebugMessage("blocking local input");
                    inputManager.BlockInput(true);
                    localCursorPosition = await inputManager.MouseListener.GetCursorPosition();

                    DebugMessage($"assuming control of {nextDesktop.Name} on display {nextDisplay} on position {nextGlobalPoint}");
                    localState = new LocalState.Controlling(nextDesktop, nextDisplay, nextGlobalPoint);
                    var nextClient = await workspaceNetwork.GetClientDesktop(nextDesktop.Name);

                    if (nextClient != null)
                    {
                        await nextClient.AssumeControl();

                        await nextClient.MoveMouse(nextGlobalPoint);
                    }
                }
            }
            else
            {
                var controllers = await workspaceNetwork.GetClientDesktops(controlled.By);

                await controllers.ControlledMouseMove(nextGlobalPoint);
            }
        }
Пример #8
0
        private async Task HandleControllingMouse(LocalState.Controlling controlling, MouseMoveEventArgs e)
        {
            var deltaX = e.Position.X - localCursorPosition.X;
            var deltaY = e.Position.Y - localCursorPosition.Y;

            if (!e.Blocked || Math.Abs(deltaX) > mouseDeltaDebounceValueX || Math.Abs(deltaY) > mouseDeltaDebounceValueY)
            {
                DebugMessage($"Mouse delta larger than debounce value ({deltaX} > {mouseDeltaDebounceValueX} || {deltaY} > {mouseDeltaDebounceValueY}).");
                return;
            }

            var nextGlobalPoint = controlling.CursorPosition.Offset(deltaX, deltaY);
            var client          = await workspaceNetwork.GetClientDesktop(controlling.ActiveDesktop.Name);

            if (!controlling.ActiveDisplay.Contains(nextGlobalPoint))
            {
                var next = displayLayout.FindPoint(nextGlobalPoint);
                if (next.HasValue)
                {
                    var(nextDesktop, nextDisplay) = next.Value;
                    if (nextDesktop != controlling.ActiveDesktop)
                    {
                        DebugMessage($"relinquishing {controlling.ActiveDesktop}");
                        if (client != null)
                        {
                            await client.RelinquishControl();
                        }
                        if (nextDesktop == selfDesktop)
                        {
                            DebugMessage($"moving to local display {nextDisplay}");
                            var localPoint = TranslateGlobalToLocal(nextGlobalPoint);
                            DebugMessage($"placing cursor at {localPoint} (global {nextGlobalPoint}");
                            await inputManager.MouseController.MoveMouse(localPoint);

                            await Task.Yield();

                            DebugMessage("unblocking local input");
                            inputManager.BlockInput(false);
                            localState = new LocalState.Uncontrolled(nextDisplay, selfDesktop.PrimaryDisplay !.Value);
                        }
                        else
                        {
                            localState = new LocalState.Controlling(nextDesktop, nextDisplay, nextGlobalPoint);
                            DebugMessage($"assuming control of {nextDesktop.Name}");
                            var nextClient = await workspaceNetwork.GetClientDesktop(nextDesktop.Name);

                            if (nextClient != null)
                            {
                                await nextClient.AssumeControl();

                                await nextClient.MoveMouse(nextGlobalPoint);
                            }
                        }
                    }
                    else
                    {
                        DebugMessage($"moving to display {nextDisplay} on same desktop {nextDesktop}");
                        localState = new LocalState.Controlling(nextDesktop, nextDisplay, nextGlobalPoint);
                        if (client != null)
                        {
                            await client.MoveMouse(nextGlobalPoint);
                        }
                    }
                }
                else
                {
                    var clampedNextGlobalPoint = controlling.ActiveDisplay.Clamp(nextGlobalPoint);
                    DebugMessage($"moving to {clampedNextGlobalPoint} on {controlling.ActiveDesktop}");
                    controlling.CursorPosition = clampedNextGlobalPoint;
                    if (client != null)
                    {
                        await client.MoveMouse(clampedNextGlobalPoint);
                    }
                }
            }
            else
            {
                DebugMessage($"moving to {nextGlobalPoint} on {controlling.ActiveDesktop}");
                controlling.CursorPosition = nextGlobalPoint;
                if (client != null)
                {
                    await client.MoveMouse(nextGlobalPoint);
                }
            }
        }