File indexing completed on 2024-04-28 16:55:15

0001 /***************************************************************************
0002  *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "powerdevilprofilegenerator.h"
0021 
0022 #include <PowerDevilSettings.h>
0023 
0024 #include <Solid/Device>
0025 #include <Solid/Battery>
0026 
0027 #include <KConfigGroup>
0028 #include <KSharedConfig>
0029 
0030 namespace PowerDevil {
0031 
0032 void ProfileGenerator::generateProfiles(bool mobile, bool vm, bool toRam, bool toDisk)
0033 {
0034     // Change critical action if default (hibernate) is unavailable
0035     if (!toDisk) {
0036         if (!toRam) {
0037             PowerDevilSettings::setBatteryCriticalAction(0);
0038         } else {
0039             PowerDevilSettings::setBatteryCriticalAction(1);
0040         }
0041 
0042         PowerDevilSettings::self()->save();
0043     }
0044 
0045     // Ok, let's get our config file.
0046     KSharedConfigPtr profilesConfig = KSharedConfig::openConfig("powermanagementprofilesrc", KConfig::SimpleConfig);
0047 
0048     // And clear it
0049     const QStringList groupList = profilesConfig->groupList();
0050     for (const QString &group : groupList) {
0051         // Don't delete activity-specific settings
0052         if (group != "Activities") {
0053             profilesConfig->deleteGroup(group);
0054         }
0055     }
0056 
0057     // Let's start: AC profile before anything else
0058     KConfigGroup acProfile(profilesConfig, "AC");
0059     acProfile.writeEntry("icon", "battery-charging");
0060 
0061     // We want to dim the screen after a while, definitely
0062     {
0063         KConfigGroup dimDisplay(&acProfile, "DimDisplay");
0064         dimDisplay.writeEntry< int >("idleTime", 300000);
0065     }
0066 
0067     auto initLid = [vm, toRam, mobile](KConfigGroup &profile)
0068     {
0069         const Modes defaultPowerButtonAction = mobile ? ToggleScreenOnOffMode : LogoutDialogMode;
0070 
0071         KConfigGroup handleButtonEvents(&profile, "HandleButtonEvents");
0072         handleButtonEvents.writeEntry< uint >("powerButtonAction", defaultPowerButtonAction);
0073         handleButtonEvents.writeEntry< uint >("powerDownAction", LogoutDialogMode);
0074         if (vm) {
0075             handleButtonEvents.writeEntry< uint >("lidAction", NoneMode);
0076         }
0077         else if (toRam) {
0078             handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
0079         } else {
0080             handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
0081         }
0082     };
0083 
0084     // Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
0085     initLid(acProfile);
0086 
0087     // And we also want to turn off the screen after another while
0088     {
0089         // on mobile, 1 minute, on desktop 10 minutes
0090         auto timeout = mobile ? 60 : 600;
0091         KConfigGroup dpmsControl(&acProfile, "DPMSControl");
0092         dpmsControl.writeEntry< uint >("idleTime", timeout);
0093         dpmsControl.writeEntry< uint >("lockBeforeTurnOff", mobile);
0094     }
0095 
0096     // Even on AC power, suspend after a rather long period of inactivity. Energy
0097     // is precious! But not on VMs.
0098     if (toRam && !vm) {
0099         // on mobile, 7 minutes, on laptop 15 minutes
0100         auto timeout = mobile ? 420000 : 900000;
0101         KConfigGroup suspendSession(&acProfile, "SuspendSession");
0102         suspendSession.writeEntry< uint >("idleTime", timeout);
0103         suspendSession.writeEntry< uint >("suspendType", ToRamMode);
0104     }
0105 
0106     // Powersave
0107     KConfigGroup batteryProfile(profilesConfig, "Battery");
0108     batteryProfile.writeEntry("icon", "battery-060");
0109     // We want to dim the screen after a while, definitely
0110     {
0111         // on mobile 30 seconds, on desktop 2 minutes
0112         // config is in the miliseconds
0113         auto timeout = mobile ? 30000 : 120000;
0114         KConfigGroup dimDisplay(&batteryProfile, "DimDisplay");
0115         dimDisplay.writeEntry< int >("idleTime", timeout);
0116     }
0117     // Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
0118     initLid(batteryProfile);
0119 
0120     // We want to turn off the screen after another while
0121     {
0122         // on mobile, 1 minute, on laptop 5 minutes
0123         auto timeout = mobile ? 60 : 300;
0124         KConfigGroup dpmsControl(&batteryProfile, "DPMSControl");
0125         dpmsControl.writeEntry< uint >("idleTime", timeout);
0126         dpmsControl.writeEntry< uint >("lockBeforeTurnOff", mobile);
0127     }
0128 
0129     // Last but not least, we want to suspend after some inactivity
0130     if (toRam && !vm) {
0131         // on mobile, 5 minute, on laptop 10 minutes
0132         auto timeout = mobile ? 300000 : 600000;
0133         KConfigGroup suspendSession(&batteryProfile, "SuspendSession");
0134         suspendSession.writeEntry< uint >("idleTime", timeout);
0135         suspendSession.writeEntry< uint >("suspendType", ToRamMode);
0136     }
0137 
0138 
0139     // Ok, now for aggressive powersave
0140     KConfigGroup lowBatteryProfile(profilesConfig, "LowBattery");
0141     lowBatteryProfile.writeEntry("icon", "battery-low");
0142     // Less brightness.
0143     {
0144         KConfigGroup brightnessControl(&lowBatteryProfile, "BrightnessControl");
0145         brightnessControl.writeEntry< int >("value", 30);
0146     }
0147     // We want to dim the screen after a while, definitely
0148     {
0149         // on mobile 30 seconds, on desktop 1 minute
0150         // config is in the miliseconds
0151         auto timeout = mobile ? 30000 : 60000;
0152         KConfigGroup dimDisplay(&lowBatteryProfile, "DimDisplay");
0153         dimDisplay.writeEntry< int >("idleTime", timeout);
0154     }
0155     // Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
0156     initLid(lowBatteryProfile);
0157 
0158     // We want to turn off the screen after another while
0159     {
0160         // on mobile, half minute, on laptop 2 minutes
0161         auto timeout = mobile ? 30 : 120;
0162         KConfigGroup dpmsControl(&lowBatteryProfile, "DPMSControl");
0163         dpmsControl.writeEntry< uint >("idleTime", timeout);
0164         dpmsControl.writeEntry< uint >("lockBeforeTurnOff", mobile);
0165     }
0166 
0167     // Last but not least, we want to suspend after a rather long period of inactivity
0168     // on mobile by default never suspend, if device wants to suspend, it will enable
0169     // using configuration overlay
0170     if (toRam && !vm) {
0171         // config is in the miliseconds
0172         KConfigGroup suspendSession(&lowBatteryProfile, "SuspendSession");
0173         suspendSession.writeEntry< uint >("idleTime", 300000);
0174         suspendSession.writeEntry< uint >("suspendType", ToRamMode);
0175     }
0176 
0177     // Save and be happy
0178     profilesConfig->sync();
0179 }
0180 
0181 }