• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

klaptopdaemon

daemon_state.cpp

Go to the documentation of this file.
00001 /*
00002  * daemon_state.cpp
00003  * Copyright (C) 1999, 2003 Paul Campbell <paul@taniwha.com>
00004  *
00005  * this replaces kcmlaptop - there are 2 parts - one is the panels that
00006  *  put the setup configuration in the "kcmlaptop" configm, the other
00007  *  is the laptop_daemon (this guy) who watches the battery state
00008  *  and does stuff as a result
00009  *
00010  * This file contains the implementation of the main laptop battery monitoring daemon
00011  *
00012  * $Id: daemon_state.cpp 735416 2007-11-11 17:05:26Z pino $
00013  *
00014  *  This program is free software; you can redistribute it and/or modify
00015  *  it under the terms of the GNU General Public License as published by
00016  *  the Free Software Foundation; either version 2 of the License, or
00017  *  (at your option) any later version.
00018  *
00019  *  This program is distributed in the hope that it will be useful,
00020  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  *  GNU General Public License for more details.
00023  *
00024  *  You should have received a copy of the GNU General Public License
00025  *  along with this program; if not, write to the Free Software
00026  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00027  */
00028 
00029 #include <stdlib.h>
00030 #include <fcntl.h>
00031 
00032 #include <QTimer>
00033 
00034 #include <kconfig.h>
00035 #include <klocale.h>
00036 #include <kcmdlineargs.h>
00037 #include "daemon_state.h"
00038 #include "portable.h"
00039 #include <kaboutdata.h>
00040 #include <sys/ioctl.h>
00041 #include <kmessagebox.h>
00042 #include <dcopclient.h>
00043 #include <qsocketnotifier.h>
00044 #include <qcursor.h>
00045 
00046 
00047 #include <unistd.h>
00048 #include <sys/time.h>
00049 
00050 daemon_state::daemon_state()
00051 {
00052     exists = laptop_portable::has_power_management();
00053     has_brightness = laptop_portable::has_brightness();
00054         QStringList profile_list;
00055         int current_profile;
00056     bool *active_list;
00057         has_performance = laptop_portable::get_system_performance(0, current_profile, profile_list, active_list);
00058         QStringList throttle_list;
00059         int current_throttle;
00060         has_throttle = laptop_portable::get_system_throttling(0, current_throttle, throttle_list, active_list);
00061 
00062 }
00063 
00064 daemon_state::~daemon_state()
00065 {
00066 }
00067 
00068 void daemon_state::load()
00069 {
00070     KConfig *config = new KConfig("kcmlaptoprc");
00071 
00072     if (config == 0) {
00073         ::fprintf(stderr, "laptop_daemon: can't open kcmlaptop config files\n");
00074         ::exit(2);
00075     }
00076 
00077     
00078     // acpi settings
00079         config->setGroup("AcpiDefault");
00080         bool enablestandby = config->readEntry("EnableStandby", false);
00081         bool enablesuspend = config->readEntry("EnableSuspend", false);
00082         bool enablehibernate = config->readEntry("EnableHibernate", false);
00083         bool enableperformance = config->readEntry("EnablePerformance", false);
00084         bool enablethrottle = config->readEntry("EnableThrottle", false);
00085     laptop_portable::acpi_set_mask(enablestandby, enablesuspend, enablehibernate, enableperformance, enablethrottle);
00086 
00087         config->setGroup("ApmDefault");
00088         enablestandby = config->readEntry("EnableStandby", false);
00089         enablesuspend = config->readEntry("EnableSuspend", false);
00090         laptop_portable::apm_set_mask(enablestandby, enablesuspend);
00091 
00092         config->setGroup("SoftwareSuspendDefault");
00093         enablehibernate = config->readEntry("EnableHibernate", false);
00094         laptop_portable::software_suspend_set_mask(enablehibernate);
00095 
00096     exists = laptop_portable::has_power_management();
00097     has_brightness = laptop_portable::has_brightness();
00098         QStringList profile_list;
00099         int current_profile;
00100     bool *active_list;
00101         has_performance = laptop_portable::get_system_performance(0, current_profile, profile_list, active_list);
00102         QStringList throttle_list;
00103         int current_throttle;
00104         has_throttle = laptop_portable::get_system_throttling(0, current_throttle, throttle_list, active_list);
00105 
00106     bool can_suspend = laptop_portable::has_suspend();
00107     bool can_hibernate = laptop_portable::has_hibernation();
00108     bool can_standby = laptop_portable::has_standby();
00109 
00110     // power control settings
00111     config->setGroup("LaptopPower");
00112     power_action[0] = config->readEntry("PowerSuspend", 0);
00113     switch (power_action[0]) {
00114     case 1: if (!can_standby)   power_action[0] = 0; break;
00115     case 2: if (!can_suspend)   power_action[0] = 0; break;
00116     case 3: if (!can_hibernate) power_action[0] = 0; break;
00117     }
00118     power_action[1] = config->readEntry("NoPowerSuspend", 1);
00119     switch (power_action[1]) {
00120     case 1: if (!can_standby)   power_action[1] = 0; break;
00121     case 2: if (!can_suspend)   power_action[1] = 0; break;
00122     case 3: if (!can_hibernate) power_action[1] = 0; break;
00123     }
00124     power_brightness_enabled[0] = config->readEntry("PowerBrightnessEnabled", 0)&has_brightness;
00125     power_brightness_enabled[1] = config->readEntry("NoPowerBrightnessEnabled", 0)&has_brightness;
00126     power_brightness[0] = config->readEntry("PowerBrightness", 255);
00127     power_brightness[1] = config->readEntry("NoPowerBrightness", 0);
00128     power_performance_enabled[0] = config->readEntry("PowerPerformanceEnabled", 0)&has_performance;
00129     power_performance_enabled[1] = config->readEntry("NoPowerPerformanceEnabled", 0)&has_performance;
00130     power_performance[0] = config->readEntry("PowerPerformance", "");
00131     power_performance[1] = config->readEntry("NoPowerPerformance", "");
00132     power_throttle_enabled[0] = config->readEntry("PowerThrottleEnabled", 0)&has_throttle;
00133     power_throttle_enabled[1] = config->readEntry("NoPowerThrottleEnabled", 0)&has_throttle;
00134     power_throttle[0] = config->readEntry("PowerThrottle", "");
00135     power_throttle[1] = config->readEntry("NoPowerThrottle", "");
00136     power_wait[0] = 60*config->readEntry("PowerWait", 20);
00137     power_wait[1] = 60*config->readEntry("NoPowerWait", 5);
00138     lav_enabled[0] = config->readEntry("LavEnabled", 0);
00139     lav_enabled[1] = config->readEntry("NoLavEnabled", 0);
00140     lav_val[0] = config->readEntry("PowerLav", -1.0);
00141     lav_val[1] = config->readEntry("NoPowerLav", -1.0);
00142 
00143     // General settings
00144     config->setGroup("BatteryDefault");
00145         poll = config->readEntry("Poll", 20);
00146         enabled = config->readEntry("Enable", true)&exists;
00147         notifyMeWhenFull = config->readEntry("NotifyMe", false)&exists;
00148     useBlankSaver = config->readEntry("BlankSaver", false);
00149         noBatteryIcon = config->readEntry("NoBatteryPixmap", "battery-missing");
00150         noChargeIcon = config->readEntry("NoChargePixmap", "laptop_nocharge");
00151         chargeIcon = config->readEntry("ChargePixmap", "battery-charging");
00152 
00153 
00154     config->setGroup("BatteryLow");
00155         time_based_action_low = config->readEntry("TimeBasedAction", true);
00156         low[0] = config->readEntry("LowValTime", 15);
00157         low[1] = config->readEntry("LowValPercent", 7);
00158         runCommand[0] = config->readEntry("RunCommand", false)&exists;
00159         runCommandPath[0] = config->readEntry("RunCommandPath");
00160         playSound[0] = config->readEntry("PlaySound", false)&exists;
00161         playSoundPath[0] = config->readEntry("PlaySoundPath");
00162         systemBeep[0] = config->readEntry("SystemBeep", true)&exists;
00163         logout[0] = config->readEntry("Logout", false)&exists;
00164         shutdown[0] = config->readEntry("Shutdown", false)&exists;
00165         notify[0] = config->readEntry("Notify", true)&exists;
00166         do_hibernate[0] = config->readEntry("Hibernate", false)&can_hibernate&exists;
00167         do_suspend[0] = config->readEntry("Suspend", false)&can_suspend&exists;
00168         do_standby[0] = config->readEntry("Standby", false)&can_standby&exists;
00169         do_brightness[0] = config->readEntry("Brightness", false)&has_brightness&exists;
00170         val_brightness[0] = config->readEntry("BrightnessValue", 0);
00171         do_performance[0] = config->readEntry("Performance", false)&has_performance&exists;
00172         val_performance[0] = config->readEntry("PerformanceValue", "");
00173         do_throttle[0] = config->readEntry("Throttle", false)&has_throttle&exists;
00174         val_throttle[0] = config->readEntry("ThrottleValue", "");
00175         have_time = config->readEntry("HaveTime", 2);
00176 
00177         if (!have_time && laptop_portable::has_battery_time())
00178             have_time = 1;
00179 
00180     config->setGroup("BatteryCritical");
00181         time_based_action_critical = config->readEntry("TimeBasedAction", true);
00182         low[2] = config->readEntry("CriticalValTime", 5);
00183         low[3] = config->readEntry("CriticalValPercent", 3);
00184         runCommand[1] = config->readEntry("RunCommand", false)&exists;
00185         runCommandPath[1] = config->readEntry("RunCommandPath");
00186         playSound[1] = config->readEntry("PlaySound", false)&exists;
00187         playSoundPath[1] = config->readEntry("PlaySoundPath");
00188         systemBeep[1] = config->readEntry("SystemBeep", true)&exists;
00189         logout[1] = config->readEntry("Logout", true)&exists;
00190         shutdown[1] = config->readEntry("Shutdown", true)&exists;
00191         notify[1] = config->readEntry("Notify", (can_suspend?false:true))&exists;
00192         do_hibernate[1] = config->readEntry("Hibernate", false)&can_hibernate&exists;
00193         do_suspend[1] = config->readEntry("Suspend", (can_suspend?true:false))&can_suspend&exists;
00194         do_standby[1] = config->readEntry("Standby", false)&can_standby&exists;
00195         do_brightness[1] = config->readEntry("Brightness", false)&has_brightness&exists;
00196         val_brightness[1] = config->readEntry("BrightnessValue", 0);
00197         do_performance[1] = config->readEntry("Performance", false)&has_performance&exists;
00198         val_performance[1] = config->readEntry("PerformanceValue", "");
00199         do_throttle[1] = config->readEntry("Throttle", false)&has_throttle&exists;
00200         val_throttle[1] = config->readEntry("ThrottleValue", "");
00201 
00202     config->setGroup("ProfileDefault");
00203     bright_pon = config->readEntry("EnableBrightnessOn", false)&has_brightness;
00204     bright_poff = config->readEntry("EnableBrightnessOff", false)&has_brightness;
00205     performance_pon = config->readEntry("EnablePerformanceOn", false)&has_performance;
00206     performance_poff = config->readEntry("EnablePerformanceOff", false)&has_performance;
00207     throttle_pon = config->readEntry("EnableThrottleOn", false)&has_throttle;
00208     throttle_poff = config->readEntry("EnableThrottleOff", false)&has_throttle;
00209     bright_son = config->readEntry("BrightnessOnLevel", 255);
00210     bright_soff = config->readEntry("BrightnessOffLevel", 160);
00211     performance_val_on = config->readEntry("PerformanceOnLevel", "");
00212     performance_val_off = config->readEntry("PerformanceOffLevel", "");
00213     throttle_val_on = config->readEntry("ThrottleOnLevel", "");
00214     throttle_val_off = config->readEntry("ThrottleOffLevel", "");
00215 
00216     config->setGroup("LaptopButtons");
00217     enable_lid_button = laptop_portable::has_button(laptop_portable::LidButton);
00218     enable_power_button = laptop_portable::has_button(laptop_portable::PowerButton);
00219     button_lid = (enable_lid_button?config->readEntry("LidSuspend", 0):0);
00220     if (!enable_lid_button) {
00221         button_lid = 0;
00222     } else
00223     switch (button_lid) {
00224     case 1: if (!can_standby)   button_lid = 0; break;
00225     case 2: if (!can_suspend)   button_lid = 0; break;
00226     case 3: if (!can_hibernate) button_lid = 0; break;
00227     }
00228     button_power = (enable_power_button?config->readEntry("PowerSuspend", 0):0);
00229     if (!enable_power_button) {
00230         button_power = 0;
00231     } else
00232     switch (button_power) {
00233     case 1: if (!can_standby)   button_power = 0; break;
00234     case 2: if (!can_suspend)   button_power = 0; break;
00235     case 3: if (!can_hibernate) button_power = 0; break;
00236     }
00237     button_lid_bright_enabled = config->readEntry("LidBrightnessEnabled", 0)&enable_lid_button&has_brightness;
00238     button_power_bright_enabled = config->readEntry("PowerBrightnessEnabled", 0)&enable_power_button&has_brightness;
00239     button_lid_bright_val = config->readEntry("LidBrightness", 0);
00240     button_power_bright_val = config->readEntry("PowerBrightness", 0);
00241     button_lid_performance_enabled = config->readEntry("LidPerformanceEnabled", 0)&enable_lid_button&has_performance;
00242     button_power_performance_enabled = config->readEntry("PowerPerformanceEnabled", 0)&enable_power_button&has_performance;
00243     button_lid_performance_val = config->readEntry("LidPerformance", "");
00244     button_power_performance_val = config->readEntry("PowerPerformance", "");
00245     button_lid_throttle_enabled = config->readEntry("LidThrottleEnabled", 0)&enable_lid_button&has_throttle;
00246     button_power_throttle_enabled = config->readEntry("PowerThrottleEnabled", 0)&enable_power_button&has_throttle;
00247     button_lid_throttle_val = config->readEntry("LidThrottle", "");
00248     button_power_throttle_val = config->readEntry("PowerThrottle", "");
00249     
00250     config->setGroup("SonyDefault");
00251     sony_enablescrollbar = config->readEntry("EnableScrollBar", false);
00252     sony_middleemulation = config->readEntry("EnableMiddleEmulation", false);
00253 
00254     delete config;
00255 }
00256 
00257 bool
00258 daemon_state::need_to_run()
00259 {
00260     //
00261     //  look for reasons NOT to run the daemon
00262     //
00263     if (!exists)
00264         return(0);
00265     if (!enabled && !notifyMeWhenFull &&
00266         !runCommand[0] && !playSound[0] && !systemBeep[0] && !notify[0] && !do_suspend[0] &&
00267         !do_standby[0] && !logout[0] && !shutdown[0] && !do_hibernate[0] &&
00268         !runCommand[1] && !playSound[1] && !systemBeep[1] && !notify[1] && !do_suspend[1] &&
00269         !do_standby[1] && !logout[1] && !shutdown[1] && !do_hibernate[0] &&
00270         !sony_enablescrollbar && !sony_middleemulation && !do_brightness[0] && !do_brightness[1] && 
00271         button_lid==0 && button_power==0 && !button_lid_bright_enabled && !button_power_bright_enabled &&
00272         !button_lid_performance_enabled && !button_power_performance_enabled && !button_lid_throttle_enabled && !button_power_throttle_enabled &&
00273         !do_performance[0] && !do_performance[1] && !do_throttle[0] && !do_throttle[1] &&
00274         !bright_pon && !bright_poff && !performance_pon && !performance_poff && !throttle_pon && !throttle_poff &&
00275         !power_brightness_enabled[0] && !power_brightness_enabled[0] &&
00276         !power_performance_enabled[0] && !power_performance_enabled[1] && !power_throttle_enabled[0] && !power_throttle_enabled[1] &&
00277         power_action[0] == 0 && power_action[1] == 0) // if no reason to be running quit
00278         return(0);
00279     return(1);
00280 }
00281 

klaptopdaemon

Skip menu "klaptopdaemon"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal