Пример #1
0
        public static QueryValue?Query(string Scheme, string SubGroup, string Settings)
        {
            List <string> output = new List <string>();
            List <string> error  = new List <string>();

            var p = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = "powercfg.exe",
                    Arguments              = $"-q {Scheme} {SubGroup} {Settings}".Trim(),
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    CreateNoWindow         = true
                }
            };

            p.Start();

            QueryValue?   qv           = null;
            QuerySubGroup?qsg          = null;
            QuerySetting? qs           = null;
            string        lastGuidType = "";
            QueryValue    queryValue;
            QuerySubGroup querySubGroup;
            QuerySetting  querySetting;
            int           possibleSettingIndex = 0;

            Regex queryValueRegex = new Regex("(.+)\\s\\((.+)\\)");

            while (!p.StandardOutput.EndOfStream)
            {
                string line = p.StandardOutput.ReadLine();
                if (line.Trim() == "")
                {
                    continue;
                }
                string[] parsed = line.Split(':');
                string   key    = parsed[0].Trim();
                string   value  = parsed[1].Trim();
                Match    m;

                switch (key)
                {
                case "Power Scheme GUID":
                    if (qv.HasValue)
                    {
                        throw new System.InvalidOperationException("Duplicate Power Scheme GUID found");
                    }
                    m                                  = queryValueRegex.Match(value);
                    queryValue                         = new QueryValue();
                    queryValue.SubGroups               = new List <QuerySubGroup>();
                    queryValue.PowerSchemeGuid         = m.Groups[1].Value.Trim();
                    queryValue.PowerSchemeFriendlyName = m.Groups[2].Value.Trim();
                    qv                                 = queryValue;
                    lastGuidType                       = "PowerScheme";
                    break;

                case "Subgroup GUID":
                    if (!qv.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Scheme defined");
                    }
                    if (qsg.HasValue)
                    {
                        if (qs.HasValue)
                        {
                            querySubGroup = qsg.Value;
                            querySubGroup.Settings.Add(qs.Value);
                            qsg = querySubGroup;
                            qs  = null;
                        }
                        queryValue = qv.Value;
                        queryValue.SubGroups.Add(qsg.Value);
                        qv = queryValue;
                    }
                    m                                  = queryValueRegex.Match(value);
                    querySubGroup                      = new QuerySubGroup();
                    querySubGroup.Settings             = new List <QuerySetting>();
                    querySubGroup.SubGroupGuid         = m.Groups[1].Value.Trim();
                    querySubGroup.SubGroupFriendlyName = m.Groups[2].Value.Trim();
                    qsg                                = querySubGroup;
                    lastGuidType                       = "SubGroup";
                    break;

                case "Power Setting GUID":
                    if (!qv.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Scheme defined");
                    }
                    if (!qsg.HasValue)
                    {
                        throw new System.InvalidOperationException("No Subgroup defined");
                    }
                    if (qs.HasValue)
                    {
                        querySubGroup = qsg.Value;
                        querySubGroup.Settings.Add(qs.Value);
                        qsg = querySubGroup;
                    }
                    m            = queryValueRegex.Match(value);
                    querySetting = new QuerySetting();
                    querySetting.PossibleSettings    = new QueryPossibleSetting[0];
                    querySetting.SettingGuid         = m.Groups[1].Value.Trim();
                    querySetting.SettingFriendlyName = m.Groups[2].Value.Trim();
                    qs           = querySetting;
                    lastGuidType = "PowerSetting";
                    break;

                case "Possible Setting Index":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    possibleSettingIndex = int.Parse(value);
                    break;

                case "Possible Setting Friendly Name":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    QueryPossibleSetting qps = new QueryPossibleSetting();
                    qps.FriendlyName = value;
                    querySetting     = qs.Value;
                    if (querySetting.PossibleSettings.Length < possibleSettingIndex + 1)
                    {
                        Array.Resize(ref querySetting.PossibleSettings, possibleSettingIndex + 1);
                    }
                    querySetting.PossibleSettings[possibleSettingIndex] = qps;
                    qs = querySetting;
                    break;

                case "Minimum Possible Setting":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    querySetting = qs.Value;
                    querySetting.MinimumPossibleSetting = Convert.ToInt32(value, 16);
                    qs = querySetting;
                    break;

                case "Maximum Possible Setting":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    querySetting = qs.Value;
                    querySetting.MaximumPossibleSetting = Convert.ToInt32(value, 16);
                    qs = querySetting;
                    break;

                case "Possible Settings increment":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    querySetting = qs.Value;
                    querySetting.PossibleSettingIncrement = Convert.ToInt32(value, 16);
                    qs = querySetting;
                    break;

                case "Possible Settings units":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    querySetting = qs.Value;
                    querySetting.PossibleSettingUnit = value;
                    qs = querySetting;
                    break;

                case "Current AC Power Setting Index":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    querySetting           = qs.Value;
                    querySetting.ACSetting = Convert.ToInt32(value, 16);
                    qs = querySetting;
                    break;

                case "Current DC Power Setting Index":
                    if (!qs.HasValue)
                    {
                        throw new System.InvalidOperationException("No Power Settings defined");
                    }
                    querySetting           = qs.Value;
                    querySetting.DCSetting = Convert.ToInt32(value, 16);
                    qs = querySetting;
                    break;

                case "GUID Alias":
                    switch (lastGuidType)
                    {
                    case "PowerScheme":
                        throw new System.InvalidOperationException("PowerScheme cannot have alias");

                    case "SubGroup":
                        if (!qsg.HasValue)
                        {
                            throw new System.InvalidOperationException("Subgroup missing");
                        }
                        querySubGroup = qsg.Value;
                        querySubGroup.SubGroupAlias = value;
                        qsg = querySubGroup;
                        break;

                    case "PowerSetting":
                        if (!qs.HasValue)
                        {
                            throw new System.InvalidOperationException("PowerSetting missing");
                        }
                        querySetting = qs.Value;
                        querySetting.SettingAlias = value;
                        qs = querySetting;
                        break;
                    }
                    break;
                }
            }
            while (!p.StandardError.EndOfStream)
            {
                string line = p.StandardError.ReadLine();
                error.Add(line);
            }

            if (qv.HasValue)
            {
                if (qsg.HasValue)
                {
                    if (qs.HasValue)
                    {
                        querySubGroup = qsg.Value;
                        querySubGroup.Settings.Add(qs.Value);
                        qsg = querySubGroup;
                    }
                    queryValue = qv.Value;
                    queryValue.SubGroups.Add(qsg.Value);
                    qv = queryValue;
                }
            }

            return(qv);
        }
Пример #2
0
        private void tempCheckTimer_Tick(object sender, EventArgs e)
        {
            // Get CPU temp
            float?cpuTemp = getCPUTemp();

            if (cpuTemp != null)
            {
                // Get current settings
                PowerCfg.QueryValue?qv = PowerCfg.PowerCfgBroker.Query("scheme_current", "sub_processor", "perfboostmode");
                if (qv == null || (qv.Value.SubGroups.Count < 1) || (qv.Value.SubGroups[0].Settings.Count < 1))
                {
                    return;
                }
                PowerCfg.QuerySetting perfBoostMode = qv.Value.SubGroups[0].Settings[0];
                int overTempVal   = Array.FindIndex(perfBoostMode.PossibleSettings, setting => setting.FriendlyName == Properties.Settings.Default.OverTempName);
                int acEnabledVal  = Array.FindIndex(perfBoostMode.PossibleSettings, setting => setting.FriendlyName == Properties.Settings.Default.EnabledNameAC);
                int acDisabledVal = Array.FindIndex(perfBoostMode.PossibleSettings, setting => setting.FriendlyName == Properties.Settings.Default.DisabledNameAC);
                int dcEnabledVal  = Array.FindIndex(perfBoostMode.PossibleSettings, setting => setting.FriendlyName == Properties.Settings.Default.EnabledNameDC);
                int dcDisabledVal = Array.FindIndex(perfBoostMode.PossibleSettings, setting => setting.FriendlyName == Properties.Settings.Default.DisabledNameDC);

                int curACValue = perfBoostMode.ACSetting;
                int curDCValue = perfBoostMode.DCSetting;

                float?enableTemp = null, disableTemp = null;
                bool  ac = false, dc = false;
                if (!Utility.IsRunningOnBattery() && Properties.Settings.Default.EnabledAC)
                {
                    enableTemp  = Properties.Settings.Default.EnableTempAC;
                    disableTemp = Properties.Settings.Default.DisableTempAC;
                    ac          = true;
                }
                else if (Utility.IsRunningOnBattery() && Properties.Settings.Default.EnabledDC)
                {
                    enableTemp  = Properties.Settings.Default.EnableTempDC;
                    disableTemp = Properties.Settings.Default.DisableTempDC;
                    dc          = true;
                }
                if (Properties.Settings.Default.EnabledAC || Properties.Settings.Default.EnabledDC)
                {
                    // For now this is only enabled if either AC/DC is enabled
                    if ((cpuTemp > Properties.Settings.Default.OverTemp) && overTempVal != -1)
                    {
                        // Putting the power policy into Efficient Enabled seems to knock it out of the aggressive boost state
                        if (ac)
                        {
                            PowerCfg.PowerCfgBroker.SetValueIndex(PowerCfg.ValueIndex.AC, "scheme_current", "sub_processor", "perfboostmode", overTempVal.ToString());
                            PowerCfg.PowerCfgBroker.SetActive("scheme_current");
                        }
                        if (dc)
                        {
                            PowerCfg.PowerCfgBroker.SetValueIndex(PowerCfg.ValueIndex.DC, "scheme_current", "sub_processor", "perfboostmode", overTempVal.ToString());
                            PowerCfg.PowerCfgBroker.SetActive("scheme_current");
                        }
                        mainTrayIcon.Text = $"WARNING Temp over limit: {cpuTemp.ToString()}c";
                        return;
                    }
                }
                bool boostEnabled = false;
                if (cpuTemp <= enableTemp)
                {
                    if (ac && (curACValue != acEnabledVal))
                    {
                        PowerCfg.PowerCfgBroker.SetValueIndex(PowerCfg.ValueIndex.AC, "scheme_current", "sub_processor", "perfboostmode", acEnabledVal.ToString());
                        PowerCfg.PowerCfgBroker.SetActive("scheme_current");
                    }
                    if (dc && (curACValue != dcEnabledVal))
                    {
                        PowerCfg.PowerCfgBroker.SetValueIndex(PowerCfg.ValueIndex.DC, "scheme_current", "sub_processor", "perfboostmode", dcEnabledVal.ToString());
                        PowerCfg.PowerCfgBroker.SetActive("scheme_current");
                    }
                    boostEnabled = true;
                }
                else if (cpuTemp >= disableTemp)
                {
                    if (ac && (curACValue != acDisabledVal))
                    {
                        PowerCfg.PowerCfgBroker.SetValueIndex(PowerCfg.ValueIndex.AC, "scheme_current", "sub_processor", "perfboostmode", acDisabledVal.ToString());
                        PowerCfg.PowerCfgBroker.SetActive("scheme_current");
                    }
                    if (dc && (curACValue != dcDisabledVal))
                    {
                        PowerCfg.PowerCfgBroker.SetValueIndex(PowerCfg.ValueIndex.DC, "scheme_current", "sub_processor", "perfboostmode", dcDisabledVal.ToString());
                        PowerCfg.PowerCfgBroker.SetActive("scheme_current");
                    }
                    boostEnabled = false;
                }
                if (boostEnabled)
                {
                    mainTrayIcon.Text = $"Boost Enabled, Temp: {cpuTemp.ToString()}c";
                }
                else
                {
                    mainTrayIcon.Text = $"Boost Disabled, Temp: {cpuTemp.ToString()}c";
                }
            }
        }