Exemple #1
0
        private void RefreshControllerList()
        {
            list_allControllers.Items.Clear();
            IntPtr ptr     = HIDapi.hid_enumerate(0x0, 0x0);
            IntPtr top_ptr = ptr;

            hid_device_info enumerate; // Add device to list

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

                if (!ContainsText(list_customControllers, enumerate.product_string) && !ContainsText(list_allControllers, enumerate.product_string))
                {
                    list_allControllers.Items.Add(new { Text = enumerate.product_string, Value = enumerate });
                }

                ptr = enumerate.next;
            }
        }
Exemple #2
0
        public void CheckForNewControllers()
        {
            CleanUp();

            // move all code for initializing devices here and well as the initial code from Start()
            bool   isLeft  = false;
            IntPtr ptr     = HIDapi.hid_enumerate(vendor_id, 0x0);
            IntPtr top_ptr = ptr;

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

            while (ptr != IntPtr.Zero)
            {
                enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));
                if (form.nonOriginal)
                {
                    enumerate.product_id = product_pro;
                }

                DolphinJoyConfig dolphinJoyConfig = Program.generic_manager.Contains(enumerate.serial_number);

                if (dolphinJoyConfig.serial_number != "-1")
                {
                    enumerate.product_id = dolphinJoyConfig.product_id;
                }

                if ((enumerate.product_id == product_l || enumerate.product_id == product_r || enumerate.product_id == product_pro) && !ControllerAlreadyAdded(enumerate.path))
                {
                    switch (enumerate.product_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;

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

                    // Add controller to block-list for HidGuardian
                    if (useHIDG)
                    {
                        HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(@"http://*****:*****@"hwids=HID\" + enumerate.path.Split('#')[1].ToUpper();
                        var            data     = Encoding.UTF8.GetBytes(postData);

                        request.Method        = "POST";
                        request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
                        request.ContentLength = data.Length;

                        using (var stream = request.GetRequestStream())
                            stream.Write(data, 0, data.Length);

                        try {
                            var response       = (HttpWebResponse)request.GetResponse();
                            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        } catch {
                            form.AppendTextBox("Unable to add controller to block-list.\r\n");
                        }
                    }
                    else                         // Remove affected devices from list
                    {
                        try {
                            HttpWebResponse r1 = (HttpWebResponse)WebRequest.Create(@"http://localhost:26762/api/v1/hidguardian/affected/purge/").GetResponse();
                        } catch { }
                    }
                    // -------------------- //

                    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;
                    }

                    j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, enumerate.product_id == product_pro));

                    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 (enumerate.product_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;

                                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.locBtnClick);
                                }));



                                form.configBtn[ii].Invoke(new MethodInvoker(delegate {
                                    form.configBtn[ii].Tag     = j.Last();
                                    form.configBtn[ii].Enabled = true;

                                    j.Last().configAction = delegate(object sender, EventArgs e)
                                    { form.configBtnClick(sender, e, (Joycon)form.configBtn[ii].Tag); };

                                    form.configBtn[ii].Click += ((Joycon)form.configBtn[ii].Tag).configAction;
                                }));

                                int port = -1;

                                bool success = int.TryParse(dolphinPorts[ii], out port);

                                j.Last().SetPack(port, dolphinJoyConfig.force, dolphinJoyConfig.inverse);

                                break;
                            }
                        }
                    }

                    byte[] mac = new byte[6];
                    for (int n = 0; n < 6; n++)
                    {
                        mac[n] = byte.Parse(enumerate.serial_number.Substring(n * 2, 2), System.Globalization.NumberStyles.HexNumber);
                    }
                    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)
                {
                    if (!v.isPro)
                    {
                        if (temp == null)
                        {
                            temp = v;
                        }
                        else if (temp.isLeft != v.isLeft && v.other == null)
                        {
                            temp.other = v;
                            v.other    = temp;

                            //Set both Joycon LEDs to the one with the lowest ID
                            byte led = temp.LED <= v.LED ? temp.LED : v.LED;
                            temp.LED = led;
                            v.LED    = led;
                            temp.SetPlayerLED(led);
                            v.SetPlayerLED(led);

                            temp.xin.Dispose();
                            temp.xin = 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);

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

                    jc.Attach(leds_: jc.LED);

                    bool on = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["HomeLEDOn"].Value.ToLower() == "true";
                    foreach (Joycon j in Program.mgr.j)
                    {
                        j.SetHomeLight(on);
                    }

                    jc.Begin();
                    if (form.nonOriginal)
                    {
                        jc.getActiveData();
                    }
                }
            }
        }