Esempio n. 1
0
 public Mouse(int deviceID, VGDevice d)
 {
     deviceNum = deviceID;
     dev       = d;
     cursor    = new MouseCursor(deviceNum);
     wheel     = new MouseWheel(deviceNum);
 }
Esempio n. 2
0
 public string getDS4Battery(int index)
 {
     if (DS4Controllers[index] != null)
     {
         VGDevice d = DS4Controllers[index];
         String   battery;
         if (!d.IsAlive())
         {
             battery = "...";
         }
         if (d.Charging)
         {
             if (d.Battery >= 100)
             {
                 battery = Properties.Resources.Full;
             }
             //battery = "full";
             else
             {
                 battery = d.Battery + "%+";
             }
         }
         else
         {
             battery = d.Battery + "%";
         }
         return(battery);
     }
     else
     {
         return(Properties.Resources.NA);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 获取可以找到和正在运行的控制器列表,从getDS4Controllers而来
 /// </summary>
 /// <returns></returns>
 public static IEnumerable <VGDevice> getVGControllers()
 {
     lock (Devices)
     {
         VGDevice[] controllers = new VGDevice[Devices.Count];
         Devices.Values.CopyTo(controllers, 0);
         return(controllers);
     }
 }
Esempio n. 4
0
 //called when devices is diconnected, timed out or has input reading failure
 /// <summary>
 /// 当 设备断开\超时和输入错误的时候 调用
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public static void On_Removal(object sender, EventArgs e)
 {
     lock (Devices)
     {
         VGDevice device = (VGDevice)sender;
         device.HidDevice.CloseDevice();
         Devices.Remove(device.MacAddress);
         DevicePaths.Remove(device.HidDevice.DevicePath);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 获取一个设备的MAC地址
 /// </summary>
 /// <param name="mac"></param>
 /// <returns></returns>
 public static VGDevice getVGController(string mac)
 {
     lock (Devices)
     {
         VGDevice device = null;
         try
         {
             Devices.TryGetValue(mac, out device);
         }
         catch (ArgumentNullException) { }
         return(device);
     }
 }
Esempio n. 6
0
        /// <summary>
        /// 寻找获取控制器列表,系统主要函数
        /// </summary>
        public static void findControllers()
        {
            lock (Devices)
            {
                int[] pid = { 0xBA0, 0x5C4, 0x09CC };
                IEnumerable <HidDevice> hDevices = HidDevices.Enumerate(0x054C, pid);
                // Sort Bluetooth first in case USB is also connected on the same controller.
                hDevices = hDevices.OrderBy <HidDevice, ConnectionType>((HidDevice d) => { return(VGDevice.HidConnectionType(d)); });

                foreach (HidDevice hDevice in hDevices)
                {
                    if (DevicePaths.Contains(hDevice.DevicePath))
                    {
                        continue; // BT/USB endpoint already open once
                    }
                    if (!hDevice.IsOpen)
                    {
                        hDevice.OpenDevice(isExclusiveMode);
                        if (!hDevice.IsOpen && isExclusiveMode)
                        {
                            try
                            {
                                WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
                                WindowsPrincipal principal = new WindowsPrincipal(identity);
                                bool             elevated  = principal.IsInRole(WindowsBuiltInRole.Administrator);

                                if (!elevated)
                                {
                                    // Launches an elevated child process to re-enable device
                                    string           exeName   = Process.GetCurrentProcess().MainModule.FileName;
                                    ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
                                    startInfo.Verb      = "runas";
                                    startInfo.Arguments = "re-enabledevice " + devicePathToInstanceId(hDevice.DevicePath);
                                    Process child = Process.Start(startInfo);
                                    if (!child.WaitForExit(5000))
                                    {
                                        child.Kill();
                                    }
                                    else if (child.ExitCode == 0)
                                    {
                                        hDevice.OpenDevice(isExclusiveMode);
                                    }
                                }
                                else
                                {
                                    reEnableDevice(devicePathToInstanceId(hDevice.DevicePath));
                                    hDevice.OpenDevice(isExclusiveMode);
                                }
                            }
                            catch (Exception) { }
                        }

                        // TODO in exclusive mode, try to hold both open when both are connected
                        if (isExclusiveMode && !hDevice.IsOpen)
                        {
                            hDevice.OpenDevice(false);
                        }
                    }
                    if (hDevice.IsOpen)
                    {
                        if (Devices.ContainsKey(hDevice.readSerial()))
                        {
                            continue; // happens when the BT endpoint already is open and the USB is plugged into the same host
                        }
                        else
                        {
                            VGDevice vgDevice = new VGDevice(hDevice);
                            vgDevice.Removal += On_Removal;
                            Devices.Add(vgDevice.MacAddress, vgDevice);
                            DevicePaths.Add(hDevice.DevicePath);
                            vgDevice.StartUpdate();
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public static void updateLightBar(VGDevice device, int deviceNum, VGState cState, VGStateExposed eState, Mouse tp)
        {
            VGColor color;

            if (!defualtLight && !forcelight[deviceNum])
            {
                if (UseCustomLed[deviceNum])
                {
                    if (LedAsBatteryIndicator[deviceNum])
                    {
                        VGColor fullColor = CustomColor[deviceNum];
                        VGColor lowColor  = LowColor[deviceNum];

                        color = getTransitionedColor(lowColor, fullColor, device.Battery);
                    }
                    else
                    {
                        color = CustomColor[deviceNum];
                    }
                }
                else
                {
                    if (Rainbow[deviceNum] > 0)
                    {                                                      // Display rainbow
                        DateTime now = DateTime.UtcNow;
                        if (now >= oldnow + TimeSpan.FromMilliseconds(10)) //update by the millisecond that way it's a smooth transtion
                        {
                            oldnow = now;
                            if (device.Charging)
                            {
                                counters[deviceNum] -= 1.5 * 3 / Rainbow[deviceNum];
                            }
                            else
                            {
                                counters[deviceNum] += 1.5 * 3 / Rainbow[deviceNum];
                            }
                        }
                        if (counters[deviceNum] < 0)
                        {
                            counters[deviceNum] = 180000;
                        }
                        if (counters[deviceNum] > 180000)
                        {
                            counters[deviceNum] = 0;
                        }
                        if (LedAsBatteryIndicator[deviceNum])
                        {
                            color = HuetoRGB((float)counters[deviceNum] % 360, (byte)(2.55 * device.Battery));
                        }
                        else
                        {
                            color = HuetoRGB((float)counters[deviceNum] % 360, 255);
                        }
                    }
                    else if (LedAsBatteryIndicator[deviceNum])
                    {
                        //if (device.Charging == false || device.Battery >= 100) // when charged, don't show the charging animation
                        {
                            VGColor fullColor = MainColor[deviceNum];
                            VGColor lowColor  = LowColor[deviceNum];

                            color = getTransitionedColor(lowColor, fullColor, (uint)device.Battery);
                        }
                    }
                    else
                    {
                        color = MainColor[deviceNum];
                    }
                }

                if (device.Battery <= FlashAt[deviceNum] && !defualtLight && !device.Charging)
                {
                    if (!(FlashColor[deviceNum].red == 0 &&
                          FlashColor[deviceNum].green == 0 &&
                          FlashColor[deviceNum].blue == 0))
                    {
                        color = FlashColor[deviceNum];
                    }
                    if (FlashType[deviceNum] == 1)
                    {
                        if (fadetimer[deviceNum] <= 0)
                        {
                            fadedirection[deviceNum] = true;
                        }
                        else if (fadetimer[deviceNum] >= 100)
                        {
                            fadedirection[deviceNum] = false;
                        }
                        if (fadedirection[deviceNum])
                        {
                            fadetimer[deviceNum] += 1;
                        }
                        else
                        {
                            fadetimer[deviceNum] -= 1;
                        }
                        color = getTransitionedColor(color, new VGColor(0, 0, 0), fadetimer[deviceNum]);
                    }
                }

                if (IdleDisconnectTimeout[deviceNum] > 0 && LedAsBatteryIndicator[deviceNum] && (!device.Charging || device.Battery >= 100))
                {//Fade lightbar by idle time
                    TimeSpan timeratio = new TimeSpan(DateTime.UtcNow.Ticks - device.lastActive.Ticks);
                    double   botratio  = timeratio.TotalMilliseconds;
                    double   topratio  = TimeSpan.FromSeconds(IdleDisconnectTimeout[deviceNum]).TotalMilliseconds;
                    double   ratio     = ((botratio / topratio) * 100);
                    if (ratio >= 50 && ratio <= 100)
                    {
                        color = getTransitionedColor(color, new VGColor(0, 0, 0), (uint)((ratio - 50) * 2));
                    }
                    else if (ratio >= 100)
                    {
                        color = getTransitionedColor(color, new VGColor(0, 0, 0), 100);
                    }
                }
                if (device.Charging && device.Battery < 100)
                {
                    switch (ChargingType[deviceNum])
                    {
                    case 1:
                        if (fadetimer[deviceNum] <= 0)
                        {
                            fadedirection[deviceNum] = true;
                        }
                        else if (fadetimer[deviceNum] >= 105)
                        {
                            fadedirection[deviceNum] = false;
                        }
                        if (fadedirection[deviceNum])
                        {
                            fadetimer[deviceNum] += .1;
                        }
                        else
                        {
                            fadetimer[deviceNum] -= .1;
                        }
                        color = getTransitionedColor(color, new VGColor(0, 0, 0), fadetimer[deviceNum]);
                        break;

                    case 2:
                        counters[deviceNum] += .167;
                        color = HuetoRGB((float)counters[deviceNum] % 360, 255);
                        break;

                    case 3:
                        color = ChargingColor[deviceNum];
                        break;

                    default:
                        break;
                    }
                }
            }
            else if (forcelight[deviceNum])
            {
                color = forcedColor[deviceNum];
            }
            else if (shuttingdown)
            {
                color = new VGColor(0, 0, 0);
            }
            else
            {
                if (device.ConnectionType == ConnectionType.BT)
                {
                    color = new VGColor(32, 64, 64);
                }
                else
                {
                    color = new VGColor(0, 0, 0);
                }
            }
            bool distanceprofile = (ProfilePath[deviceNum].ToLower().Contains("distance") || tempprofilename[deviceNum].ToLower().Contains("distance"));

            if (distanceprofile && !defualtLight)
            { //Thing I did for Distance
                float rumble = device.LeftHeavySlowRumble / 2.55f;
                byte  max    = Max(color.red, Max(color.green, color.blue));
                if (device.LeftHeavySlowRumble > 100)
                {
                    color = getTransitionedColor(new VGColor(max, max, 0), new VGColor(255, 0, 0), rumble);
                }
                else
                {
                    color = getTransitionedColor(color, getTransitionedColor(new VGColor(max, max, 0), new VGColor(255, 0, 0), 39.6078f), device.LeftHeavySlowRumble);
                }
            }
            VGHapticState haptics = new VGHapticState
            {
                LightBarColor = color
            };

            if (haptics.IsLightBarSet())
            {
                if (forcelight[deviceNum] && forcedFlash[deviceNum] > 0)
                {
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)(25 - forcedFlash[deviceNum]);
                    haptics.LightBarExplicitlyOff    = true;
                }
                else if (device.Battery <= FlashAt[deviceNum] && FlashType[deviceNum] == 0 && !defualtLight && !device.Charging)
                {
                    int level = device.Battery / 10;
                    //if (level >= 10)
                    //level = 0; // all values of ~0% or >~100% are rendered the same
                    haptics.LightBarFlashDurationOn  = BatteryIndicatorDurations[level, 0];
                    haptics.LightBarFlashDurationOff = BatteryIndicatorDurations[level, 1];
                }
                else if (distanceprofile && device.LeftHeavySlowRumble > 155) //also part of Distance
                {
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)((-device.LeftHeavySlowRumble + 265));
                    haptics.LightBarExplicitlyOff    = true;
                }
                else
                {
                    //haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1;
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 0;
                    haptics.LightBarExplicitlyOff    = true;
                }
            }
            else
            {
                haptics.LightBarExplicitlyOff = true;
            }
            if (device.LightBarOnDuration != haptics.LightBarFlashDurationOn && device.LightBarOnDuration != 1 && haptics.LightBarFlashDurationOn == 0)
            {
                haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1;
            }
            if (device.LightBarOnDuration == 1) //helps better reset the color
            {
                System.Threading.Thread.Sleep(5);
            }
            device.pushHapticState(haptics);
        }