/* ----------------------------------------------------------------- */
        /// LoadProfile
        /* ----------------------------------------------------------------- */
        private void LoadProfile(PowerSchemeItem src)
        {
            if (src == null) return;

            bool prev = this._EnableComboEvents;
            this._EnableComboEvents = false;
            this.MonitorComboBox.SelectedIndex = Translator.ExpireTypeToIndex(
                Translator.SecondToExpireType(src.Policy.user.VideoTimeoutAc));
            this.DiskComboBox.SelectedIndex = Translator.ExpireTypeToIndex(
                Translator.SecondToExpireType(src.Policy.user.SpindownTimeoutAc));
            this.StandByComboBox.SelectedIndex = Translator.ExpireTypeToIndex(
                Translator.SecondToExpireType(src.Policy.user.IdleTimeoutAc));
            this.HibernationComboBox.SelectedIndex = Translator.ExpireTypeToIndex(
                Translator.SecondToExpireType(src.Policy.mach.DozeS4TimeoutAc));
            this.DimComboBox.SelectedIndex = Translator.ExpireTypeToIndex(
                Translator.SecondToExpireType(src.DimTimeout));
            this.BrightnessNumericUpDown.Value = src.Brightness;
            this.DimBrightnessNumericUpDown.Value = src.DimBrightness;

            PowerThrottlePolicy processor = (PowerThrottlePolicy)src.Policy.user.ThrottlePolicyAc;
            this.PowerThrottleComboBox.SelectedIndex = Translator.PowerThrottlePolicyToIndex(processor);
            switch (processor) {
            case PowerThrottlePolicy.PO_THROTTLE_NONE:
                this.MinPowerThrottleNumericUpDown.Value = 100;
                this.MaxPowerThrottleNumericUpDown.Value = 100;
                break;
            case PowerThrottlePolicy.PO_THROTTLE_CONSTANT:
                this.MinPowerThrottleNumericUpDown.Value = 5;
                this.MaxPowerThrottleNumericUpDown.Value = 50;
                break;
            default:
                this.MinPowerThrottleNumericUpDown.Value = 5;
                this.MaxPowerThrottleNumericUpDown.Value = 100;
                break;
            }

            this._EnableComboEvents = prev;

            this._DetailGroupBox.Text = "[" + src.Name + "] の電源設定";
        }
        /* ----------------------------------------------------------------- */
        /// Update
        /* ----------------------------------------------------------------- */
        public bool Update(PowerSchemeItem item)
        {
            if (this.Find(item.Name) == null) return this.Add(item);
            foreach (KeyValuePair<Guid, PowerSchemeItem> elem in this._elements) {
                if (elem.Value.Name == item.Name) {
                    bool status = true;
                    Guid scheme = elem.Key;
                    status &= this.SetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_TIMEOUT, item.Policy.user.VideoTimeoutAc);
                    status &= this.SetACValue(scheme, GUID_DISK_SUBGROUP, GUID_DISK_TIMEOUT, item.Policy.user.SpindownTimeoutAc);
                    status &= this.SetACValue(scheme, GUID_SLEEP_SUBGROUP, GUID_STANDBY_TIMEOUT, item.Policy.user.IdleTimeoutAc);
                    status &= this.SetACValue(scheme, GUID_SLEEP_SUBGROUP, GUID_HIBERNATION_TIMEOUT, item.Policy.mach.DozeS4TimeoutAc);
                    status &= this.SetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_DIM_TIMEOUT, item.DimTimeout);
                    status &= this.SetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_BRIGHTNESS, item.Brightness);
                    status &= this.SetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_DIM_BRIGHTNESS, item.DimBrightness);
                    status &= this.SetACValue(scheme, GUID_PROCESSOR_SUBGROUP, GUID_PROCESSOR_MIN, item.MinThrottle);
                    status &= this.SetACValue(scheme, GUID_PROCESSOR_SUBGROUP, GUID_PROCESSOR_MAX, item.MaxThrottle);

                    return status;
                }
            }
            return false;
        }
        /* ----------------------------------------------------------------- */
        /// GetAllElements
        /* ----------------------------------------------------------------- */
        private void GetAllElements()
        {
            Guid scheme = Guid.Empty;
            uint index = 0;
            uint size = (uint)Marshal.SizeOf(scheme);

            while (NativeMethods.PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, NativeMethods.POWER_DATA_ACCESSOR.ACCESS_SCHEME, index, ref scheme, ref size) == 0) {
                PowerSchemeItem item = new PowerSchemeItem();
                item.Name = this.GetProfileName(scheme);

                POWER_POLICY policy = item.Policy;
                policy.user.VideoTimeoutAc = this.GetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_TIMEOUT);
                policy.user.SpindownTimeoutAc = this.GetACValue(scheme, GUID_DISK_SUBGROUP, GUID_DISK_TIMEOUT);
                policy.user.IdleTimeoutAc = this.GetACValue(scheme, GUID_SLEEP_SUBGROUP, GUID_STANDBY_TIMEOUT);
                policy.mach.DozeS4TimeoutAc = this.GetACValue(scheme, GUID_SLEEP_SUBGROUP, GUID_HIBERNATION_TIMEOUT);

                // プロセッサ調整
                item.MinThrottle = this.GetACValue(scheme, GUID_PROCESSOR_SUBGROUP, GUID_PROCESSOR_MIN);
                item.MaxThrottle = this.GetACValue(scheme, GUID_PROCESSOR_SUBGROUP, GUID_PROCESSOR_MAX);
                if (item.MinThrottle == 100 && item.MaxThrottle == 100) policy.user.ThrottlePolicyAc = (byte)PowerThrottlePolicy.PO_THROTTLE_NONE;
                else if (item.MaxThrottle == 100) policy.user.ThrottlePolicyAc = (byte)PowerThrottlePolicy.PO_THROTTLE_ADAPTIVE;
                else policy.user.ThrottlePolicyAc = (byte)PowerThrottlePolicy.PO_THROTTLE_CONSTANT;

                item.Policy = policy;

                item.DimTimeout = this.GetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_DIM_TIMEOUT);
                item.Brightness = this.GetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_BRIGHTNESS);
                item.DimBrightness = this.GetACValue(scheme, GUID_VIDEO_SUBGROUP, GUID_VIDEO_DIM_BRIGHTNESS);

                this._elements.Add(scheme, item);
                index++;
            }
        }
        /* ----------------------------------------------------------------- */
        /// Add
        /* ----------------------------------------------------------------- */
        public bool Add(PowerSchemeItem item)
        {
            if (item == null) return false;

            Guid active = Guid.Empty;
            Guid dest = Guid.Empty;
            IntPtr active_handle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
            IntPtr dest_handle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
            try {
                if (NativeMethods.PowerGetActiveScheme(IntPtr.Zero, out active_handle) != 0) return false;
                active = (Guid)Marshal.PtrToStructure(active_handle, typeof(Guid));
                if (NativeMethods.PowerDuplicateScheme(IntPtr.Zero, ref active, out dest_handle) != 0) return false;
                dest = (Guid)Marshal.PtrToStructure(dest_handle, typeof(Guid));

                bool status = true;
                status &= this.SetProfileName(dest, item.Name);
                status &= this.SetACValue(dest, GUID_VIDEO_SUBGROUP, GUID_VIDEO_TIMEOUT, item.Policy.user.VideoTimeoutAc);
                status &= this.SetACValue(dest, GUID_DISK_SUBGROUP, GUID_DISK_TIMEOUT, item.Policy.user.SpindownTimeoutAc);
                status &= this.SetACValue(dest, GUID_SLEEP_SUBGROUP, GUID_STANDBY_TIMEOUT, item.Policy.user.IdleTimeoutAc);
                status &= this.SetACValue(dest, GUID_SLEEP_SUBGROUP, GUID_HIBERNATION_TIMEOUT, item.Policy.mach.DozeS4TimeoutAc);
                status &= this.SetACValue(dest, GUID_VIDEO_SUBGROUP, GUID_VIDEO_DIM_TIMEOUT, item.DimTimeout);
                status &= this.SetACValue(dest, GUID_VIDEO_SUBGROUP, GUID_VIDEO_BRIGHTNESS, item.Brightness);
                status &= this.SetACValue(dest, GUID_VIDEO_SUBGROUP, GUID_VIDEO_DIM_BRIGHTNESS, item.DimBrightness);
                status &= this.SetACValue(dest, GUID_PROCESSOR_SUBGROUP, GUID_PROCESSOR_MIN, item.MinThrottle);
                status &= this.SetACValue(dest, GUID_PROCESSOR_SUBGROUP, GUID_PROCESSOR_MAX, item.MaxThrottle);

                if (!status) {
                    NativeMethods.PowerDeleteScheme(IntPtr.Zero, ref dest);
                    return false;
                }

                this._elements.Add(dest, item);
                return status;
            }
            finally {
                if (active_handle != null) Marshal.FreeHGlobal(active_handle);
                if (dest_handle != null) Marshal.FreeHGlobal(dest_handle);
            }
        }
Beispiel #5
0
        private bool Update(ScheduleItem item)
        {
            PowerSchemeItem elem = new PowerSchemeItem();
            elem.Name = CUBEPOWER_PROFILENAME;
            elem.Description = CUBEPOWER_DESCRIPTION;
            POWER_POLICY policy = elem.Policy;
            policy.user.VideoTimeoutAc = item.ACValues.MonitorTimeout;
            policy.user.SpindownTimeoutAc = item.ACValues.DiskTimeout;
            policy.user.IdleTimeoutAc = item.ACValues.StandByTimeout;
            policy.mach.DozeS4TimeoutAc = item.ACValues.HibernationTimeout;
            policy.user.ThrottlePolicyAc = (byte)item.ACValues.ThrottlePolicy;
            elem.Policy = policy;
            elem.DimTimeout = item.ACValues.DimTimeout;
            elem.Brightness = item.ACValues.Brightness;
            elem.DimBrightness = item.ACValues.DimBrightness;
            elem.MinThrottle = item.ACValues.MinThrottle;
            elem.MaxThrottle = item.ACValues.MaxThrottle;

            bool status = true;
            if (this._setting.Scheme.Find(CUBEPOWER_PROFILENAME) == null) status &= this._setting.Scheme.Add(elem);
            else status &= this._setting.Scheme.Update(elem);
            status &= this._setting.Scheme.Activate(elem.Name);

            return status;
        }
 /* ----------------------------------------------------------------- */
 ///
 /// PwrSchemesEnumProcFunction
 /// 
 /// <summary>
 /// Callback function for the EnumPwrSchemes function.
 /// </summary>
 /// 
 /* ----------------------------------------------------------------- */
 private bool PwrSchemesEnumProcFunction(uint uiIndex, UInt32 dwName, [MarshalAs(UnmanagedType.LPWStr)] string sName, UInt32 dwDesc, [MarshalAs(UnmanagedType.LPWStr)] string sDesc, ref POWER_POLICY pp, int lParam)
 {
     _Count += 1;
     PowerSchemeItem elem = new PowerSchemeItem();
     elem.Name = sName;
     elem.Description = sDesc;
     elem.Policy = pp;
     _Elements.Add(uiIndex, elem);
     return true;
 }
        /* ----------------------------------------------------------------- */
        /// Update
        /* ----------------------------------------------------------------- */
        public bool Update(PowerSchemeItem item)
        {
            if (this.Find(item.Name) == null) return this.Add(item);

            bool status = true;
            foreach (KeyValuePair<uint, PowerSchemeItem> elem in _Elements) {
                if (elem.Value.Name == item.Name) {
                    POWER_POLICY policy = elem.Value.Policy;
                    policy.user.VideoTimeoutAc = item.Policy.user.VideoTimeoutAc;
                    policy.user.SpindownTimeoutAc = item.Policy.user.SpindownTimeoutAc;
                    policy.user.IdleTimeoutAc = item.Policy.user.IdleTimeoutAc;
                    policy.mach.DozeS4TimeoutAc = item.Policy.mach.DozeS4TimeoutAc;
                    policy.user.ThrottlePolicyAc = item.Policy.user.ThrottlePolicyAc;

                    uint index = elem.Key;
                    status = NativeMethods.WritePwrScheme(ref index, item.Name, item.Description, ref policy);
                    elem.Value.Policy = policy;

                    // TODO: powercfg ��g�p�����ɐݒ肷����@
                    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                    info.FileName = "powercfg";
                    info.Arguments = "/CHANGE \"" + item.Name + "\" /processor-throttle-ac ";
                    if (item.Policy.user.ThrottlePolicyAc == (byte)PowerThrottlePolicy.PO_THROTTLE_NONE) info.Arguments += "NONE";
                    else if (item.Policy.user.ThrottlePolicyAc == (byte)PowerThrottlePolicy.PO_THROTTLE_CONSTANT) info.Arguments += "CONSTANT";
                    else if (item.Policy.user.ThrottlePolicyAc == (byte)PowerThrottlePolicy.PO_THROTTLE_DEGRADE) info.Arguments += "DEGRADE";
                    else info.Arguments += "ADAPTIVE";
                    info.CreateNoWindow = true;
                    info.UseShellExecute = false;
                    System.Diagnostics.Process proc = new System.Diagnostics.Process();
                    proc.StartInfo = info;
                    proc.Start();
                    proc.WaitForExit();

                    break;
                }
            }

            return status;
        }
        /* ----------------------------------------------------------------- */
        /// Add
        /* ----------------------------------------------------------------- */
        public bool Add(PowerSchemeItem item)
        {
            if (item == null) return false;

            uint index = 0;
            while (this._Elements.ContainsKey(index)) index++;
            POWER_POLICY policy = item.Policy;
            bool status = NativeMethods.WritePwrScheme(ref index, item.Name, item.Description, ref policy);
            if (status) {
                this._Elements.Add(index, item);
                this._Count++;
            }
            return status;
        }