Beispiel #1
0
        static void Main(string[] args)
        {
            Toolbox.RegisterLogCallback(ErrorCallback, LogMask.ERROR);
            Toolbox.SetDefinitionLoadCallback(File.ReadAllText);
            Toolbox.SetResourceLoadCallback(File.ReadAllBytes);
            Comet.InitModule();
            Nebula.InitModule();
            Engine.InitModule("SBG/MouseDeamon");

            while (!Engine.IsLocalRunning())
            {
                Console.WriteLine("Waiting for local engine...");
                System.Threading.Thread.Sleep(1000);
            }

            try {
                Satellite.GetSelf();
            } catch (StarburstException) {
                Satellite.Deploy();
            }
            Component mouseComp = new Component("SBG/MouseClient");

            Topic.CometReceiver mousePositionTopic = new Topic.CometReceiver("mousePosition", Topic.CometReceiver.StorageType.STREAM);
            Topic.CometReceiver mouseEventTopic    = new Topic.CometReceiver("mouseEvent", Topic.CometReceiver.StorageType.QUEUE);

            Satellite self    = Satellite.GetSelf();
            string    address = EngineClient.Get(self.EngineClientID).Address;

            Session se = null;

            while (!Session.TryFind(out se))
            {
                System.Threading.Thread.Sleep(1000);
            }
            Engine.JoinSession(se);
            while (mouseComp.Action != ComponentAction.START)
            {
                System.Threading.Thread.Sleep(100);
            }
            mouseComp.ReportState(ComponentState.STARTED);

            CMouseInput mi = new CMouseInput {
                dwFlags = CMouseInput.FLAG_ABSOLUTE
            };

            var  inputPtr     = Marshal.AllocHGlobal(Marshal.SizeOf <CInput>());
            bool inputChanged = false;

            while (mouseComp.Action != ComponentAction.STOP)
            {
                Comet c = mousePositionTopic.GetComet();                    // Range: -1 to 1
                if (c != null)
                {
                    if ((mi.dx != WinRange(c["X"].AsFloat()) || mi.dy != WinRange(c["Y"].AsFloat())) && c["Client"].AsString() == address)
                    {
                        mi.dx        = WinRange(c["X"].AsFloat());                    // Range: 0 to 65536
                        mi.dy        = WinRange(c["Y"].AsFloat());
                        mi.dwFlags  |= CMouseInput.FLAG_MOUSEMOVED;
                        inputChanged = true;
                    }

                    c.Dispose();
                }

                c = mouseEventTopic.GetComet();
                if (c != null)
                {
                    if (c["Client"].AsString() == address)
                    {
                        mi.dwFlags  |= c["LeftDown"].AsBool() ? CMouseInput.FLAG_LEFTDOWN : 0;
                        mi.dwFlags  |= c["LeftUp"].AsBool() ? CMouseInput.FLAG_LEFTUP : 0;
                        mi.dwFlags  |= c["RightDown"].AsBool() ? CMouseInput.FLAG_RIGHTDOWN : 0;
                        mi.dwFlags  |= c["RightUp"].AsBool() ? CMouseInput.FLAG_RIGHTUP : 0;
                        inputChanged = true;
                    }

                    c.Dispose();
                }

                if (inputChanged)
                {
                    Marshal.StructureToPtr(mi.AsCInput(), inputPtr, true);
                    if (SendInput(1, inputPtr, Marshal.SizeOf <CInput>()) < 1)
                    {
                        throw new Exception("Couldn't send input : error " + GetLastError());
                    }
                    mi.dwFlags   = CMouseInput.FLAG_ABSOLUTE;                    // reseting flags
                    inputChanged = false;
                }

                System.Threading.Thread.Sleep(20);
            }
            mouseComp.ReportState(ComponentState.STOPPED);
        }