Exemple #1
0
        public static void Stop()
        {
            if (Program.useHidHide)
            {
                try {
                    HidHide.whitelistApplications(new List <string>(), false);
                } catch (Exception /*e*/) {
                    form.console.AppendText("Unable to purge whitelist.\r\n");
                }

                try {
                    HidHide.setStatus(false);
                } catch (Exception /*e*/) {
                    form.console.AppendText("Unable to disable HidHide.\r\n");
                }

                if (Boolean.Parse(ConfigurationManager.AppSettings["PurgeAffectedDevices"]))
                {
                    try {
                        HidHide.blacklistDevices(new List <string>(), false);
                    } catch (Exception /*e*/) {
                        form.console.AppendText("Unable to purge blacklisted devices.\r\n");
                    }
                }
            }

            keyboard.Dispose(); mouse.Dispose();
            server.Stop();
            mgr.OnApplicationQuit();
        }
Exemple #2
0
        public static void Start()
        {
            if (useHidHide)
            {
                form.console.AppendText("HidHide is enabled.\r\n");

                if (useHidHide && Boolean.Parse(ConfigurationManager.AppSettings["PurgeAffectedDevices"]))
                {
                    try {
                        HidHide.blacklistDevices(new List <string>(), false);
                    } catch (Exception /*e*/) {
                        form.console.AppendText("Unable to purge blacklisted devices.\r\n");
                        useHidHide = false;
                    }
                }

                if (useHidHide)
                {
                    try {
                        List <string> applications = new List <string>();
                        applications.Add(System.Environment.ProcessPath);
                        bool keepExisting = true;
                        if (Boolean.Parse(ConfigurationManager.AppSettings["PurgeWhitelist"]))
                        {
                            keepExisting = false;
                        }
                        HidHide.whitelistApplications(applications, keepExisting);
                    } catch (Exception /*e*/) {
                        form.console.AppendText("Unable to add program to whitelist.\r\n");
                        useHidHide = false;
                    }
                }

                if (useHidHide)
                {
                    try {
                        HidHide.setStatus(true);
                    } catch (Exception /*e*/) {
                        form.console.AppendText("Unable to hide devices.\r\n");
                        useHidHide = false;
                    }
                }
            }

            if (Boolean.Parse(ConfigurationManager.AppSettings["ShowAsXInput"]) || Boolean.Parse(ConfigurationManager.AppSettings["ShowAsDS4"]))
            {
                try {
                    emClient = new ViGEmClient(); // Manages emulated XInput
                } catch (Nefarius.ViGEm.Client.Exceptions.VigemBusNotFoundException) {
                    form.console.AppendText("Could not start VigemBus. Make sure drivers are installed correctly.\r\n");
                }
            }

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                // Get local BT host MAC
                if (nic.NetworkInterfaceType != NetworkInterfaceType.FastEthernetFx && nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
                {
                    if (nic.Name.Split()[0] == "Bluetooth")
                    {
                        btMAC = nic.GetPhysicalAddress();
                    }
                }
            }

            // a bit hacky
            _3rdPartyControllers partyForm = new _3rdPartyControllers();

            partyForm.CopyCustomControllers();

            mgr      = new JoyconManager();
            mgr.form = form;
            mgr.Awake();

            server      = new UdpServer(mgr.j);
            server.form = form;

            server.Start(IPAddress.Parse(ConfigurationManager.AppSettings["IP"]), Int32.Parse(ConfigurationManager.AppSettings["Port"]));

            // Capture keyboard + mouse events for binding's sake
            keyboard           = WindowsInput.Capture.Global.KeyboardAsync();
            keyboard.KeyEvent += Keyboard_KeyEvent;
            mouse              = WindowsInput.Capture.Global.MouseAsync();
            mouse.MouseEvent  += Mouse_MouseEvent;

            form.console.AppendText("All systems go\r\n");
            mgr.Start();
        }
Exemple #3
0
        public void CheckForNewControllers()
        {
            // move all code for initializing devices here and well as the initial code from Start()
            bool   isLeft  = false;
            IntPtr ptr     = HIDapi.hid_enumerate(0x0, 0x0);
            IntPtr top_ptr = ptr;

            hid_device_info enumerate; // Add device to list
            bool            foundNew = false;

            while (ptr != IntPtr.Zero)
            {
                SController thirdParty = null;
                enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));

                if (enumerate.serial_number == null)
                {
                    ptr = enumerate.next; // can't believe it took me this long to figure out why USB connections used up so much CPU.
                                          // it was getting stuck in an inf loop here!
                    continue;
                }

                bool validController = (enumerate.product_id == product_l || enumerate.product_id == product_r ||
                                        enumerate.product_id == product_pro || enumerate.product_id == product_snes) && enumerate.vendor_id == vendor_id;
                // check list of custom controllers specified
                foreach (SController v in Program.thirdPartyCons)
                {
                    if (enumerate.vendor_id == v.vendor_id && enumerate.product_id == v.product_id && enumerate.serial_number == v.serial_number)
                    {
                        validController = true;
                        thirdParty      = v;
                        break;
                    }
                }

                ushort prod_id = thirdParty == null ? enumerate.product_id : TypeToProdId(thirdParty.type);
                if (prod_id == 0)
                {
                    ptr = enumerate.next; // controller was not assigned a type, but advance ptr anyway
                    continue;
                }

                if (validController && !ControllerAlreadyAdded(enumerate.path))
                {
                    switch (prod_id)
                    {
                    case product_l:
                        isLeft = true;
                        form.AppendTextBox("Left Joy-Con connected.\r\n"); break;

                    case product_r:
                        isLeft = false;
                        form.AppendTextBox("Right Joy-Con connected.\r\n"); break;

                    case product_pro:
                        isLeft = true;
                        form.AppendTextBox("Pro controller connected.\r\n"); break;

                    case product_snes:
                        isLeft = true;
                        form.AppendTextBox("SNES controller connected.\r\n"); break;

                    default:
                        form.AppendTextBox("Non Joy-Con Nintendo input device skipped.\r\n"); break;
                    }

                    // Add controller to block-list for HidHide
                    if (Program.useHidHide)
                    {
                        List <string> devices    = new List <string>();
                        var           pathTokens = enumerate.path.Split('#');
                        if (pathTokens.Length < 2)
                        {
                            form.AppendTextBox("Unable to add controller to block-list : incorrect device path.\r\n");
                        }
                        else
                        {
                            string device = @"HID\" + pathTokens[1].ToUpper() + @"\" + pathTokens[2].ToLower();
                            devices.Add(device);
                            device = @"USB\" + pathTokens[1].ToUpper() + @"\" + enumerate.serial_number;
                            devices.Add(device);

                            try {
                                HidHide.blacklistDevices(devices);
                            } catch {
                                form.AppendTextBox("Unable to add controller to block-list.\r\n");
                            }
                        }
                    }
                    // -------------------- //

                    IntPtr handle = HIDapi.hid_open_path(enumerate.path);
                    try {
                        HIDapi.hid_set_nonblocking(handle, 1);
                    } catch {
                        form.AppendTextBox("Unable to open path to device - are you using the correct (64 vs 32-bit) version for your PC?\r\n");
                        break;
                    }

                    bool isPro  = prod_id == product_pro;
                    bool isSnes = prod_id == product_snes;
                    j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, isPro, isSnes, thirdParty != null));

                    foundNew = true;
                    j.Last().form = form;

                    if (j.Count < 5)
                    {
                        int ii = -1;
                        foreach (Button v in form.con)
                        {
                            ii++;
                            if (!v.Enabled)
                            {
                                System.Drawing.Bitmap temp;
                                switch (prod_id)
                                {
                                case (product_l):
                                    temp = Properties.Resources.jc_left_s; break;

                                case (product_r):
                                    temp = Properties.Resources.jc_right_s; break;

                                case (product_pro):
                                    temp = Properties.Resources.pro; break;

                                case (product_snes):
                                    temp = Properties.Resources.snes; break;

                                default:
                                    temp = Properties.Resources.cross; break;
                                }

                                v.Invoke(new MethodInvoker(delegate {
                                    v.Tag             = j.Last(); // assign controller to button
                                    v.Enabled         = true;
                                    v.Click          += new EventHandler(form.conBtnClick);
                                    v.BackgroundImage = temp;
                                }));

                                form.loc[ii].Invoke(new MethodInvoker(delegate {
                                    form.loc[ii].Tag    = v;
                                    form.loc[ii].Click += new EventHandler(form.locBtnClickAsync);
                                }));

                                break;
                            }
                        }
                    }

                    byte[] mac = new byte[6];
                    try {
                        for (int n = 0; n < 6; n++)
                        {
                            mac[n] = byte.Parse(enumerate.serial_number.Substring(n * 2, 2), System.Globalization.NumberStyles.HexNumber);
                        }
                    } catch (Exception /*e*/) {
                        // could not parse mac address
                    }
                    j[j.Count - 1].PadMacAddress = new PhysicalAddress(mac);
                }

                ptr = enumerate.next;
            }

            if (foundNew)   // attempt to auto join-up joycons on connection
            {
                Joycon temp = null;
                foreach (Joycon v in j)
                {
                    // Do not attach two controllers if they are either:
                    // - Not a Joycon
                    // - Already attached to another Joycon (that isn't itself)
                    if (v.isPro || (v.other != null && v.other != v))
                    {
                        continue;
                    }

                    // Otherwise, iterate through and find the Joycon with the lowest
                    // id that has not been attached already (Does not include self)
                    if (temp == null)
                    {
                        temp = v;
                    }
                    else if (temp.isLeft != v.isLeft && v.other == null)
                    {
                        temp.other = v;
                        v.other    = temp;

                        if (temp.out_xbox != null)
                        {
                            try {
                                temp.out_xbox.Disconnect();
                            } catch (Exception /*e*/) {
                                // it wasn't connected in the first place, go figure
                            }
                        }
                        if (temp.out_ds4 != null)
                        {
                            try {
                                temp.out_ds4.Disconnect();
                            } catch (Exception /*e*/) {
                                // it wasn't connected in the first place, go figure
                            }
                        }
                        temp.out_xbox = null;
                        temp.out_ds4  = null;

                        foreach (Button b in form.con)
                        {
                            if (b.Tag == v || b.Tag == temp)
                            {
                                Joycon tt = (b.Tag == v) ? v : (b.Tag == temp) ? temp : v;
                                b.BackgroundImage = tt.isLeft ? Properties.Resources.jc_left : Properties.Resources.jc_right;
                            }
                        }

                        temp = null;    // repeat
                    }
                }
            }

            HIDapi.hid_free_enumeration(top_ptr);

            bool dropped = false;
            bool on      = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["HomeLEDOn"].Value.ToLower() == "true";

            foreach (Joycon jc in j)   // Connect device straight away
            {
                if (jc.state == Joycon.state_.NOT_ATTACHED)
                {
                    if (jc.out_xbox != null)
                    {
                        jc.out_xbox.Connect();
                    }
                    if (jc.out_ds4 != null)
                    {
                        jc.out_ds4.Connect();
                    }

                    try {
                        jc.Attach();
                    } catch (Exception /*e*/) {
                        jc.state = Joycon.state_.DROPPED;
                        dropped  = true;
                        continue;
                    }

                    jc.SetHomeLight(on);

                    jc.Begin();
                    if (form.allowCalibration)
                    {
                        jc.getActiveIMUData();
                        jc.getActiveSticksData();
                    }
                }
            }
            if (dropped)
            {
                controllerCheck.Interval = 500;
            }
            else
            {
                controllerCheck.Interval = 5000;
            }
        }