diff options
Diffstat (limited to 'src/hardware.cpp')
-rw-r--r-- | src/hardware.cpp | 248 |
1 files changed, 102 insertions, 146 deletions
diff --git a/src/hardware.cpp b/src/hardware.cpp index f4efedd..7def545 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -24,10 +24,15 @@ * \date 2006-2007 */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + // include global header #include <fcntl.h> +#include <math.h> -// include QT header +// include TQt header #include <tqtimer.h> #include <tqdir.h> @@ -54,10 +59,10 @@ HardwareInfo::HardwareInfo() { // initialize connection to the TDE hardware library m_hwdevices = TDEGlobal::hardwareDevices(); - connect(m_hwdevices, TQT_SIGNAL(hardwareUpdated(TDEGenericDevice*)), this, TQT_SLOT(processHardwareChangedEvent(TDEGenericDevice*))); + connect(m_hwdevices, TQ_SIGNAL(hardwareUpdated(TDEGenericDevice*)), this, TQ_SLOT(processHardwareChangedEvent(TDEGenericDevice*))); #ifdef USE_EVENT_DEVICES_DIRECTLY - connect(m_hwdevices, TQT_SIGNAL(eventDeviceKeyPressed(unsigned int, TDEEventDevice*)), this, TQT_SLOT(processKeyPressEvent(unsigned int, TDEEventDevice*))); + connect(m_hwdevices, TQ_SIGNAL(eventDeviceKeyPressed(unsigned int, TDEEventDevice*)), this, TQ_SLOT(processKeyPressEvent(unsigned int, TDEEventDevice*))); #endif // update everything the first time @@ -80,7 +85,7 @@ HardwareInfo::HardwareInfo() { // connect to D-Bus dbus_iface = new dbusInterface(); sessionIsActive = dbus_iface->checkActiveSession(); - connect(dbus_iface, TQT_SIGNAL(activeSessionChanged(bool)), this, TQT_SLOT(handleSessionState(bool))); + connect(dbus_iface, TQ_SIGNAL(activeSessionChanged(bool)), this, TQ_SLOT(handleSessionState(bool))); checkPowermanagement(); checkIsLaptop(); @@ -142,13 +147,13 @@ bool HardwareInfo::reinitHardwareInfos () { /*! - * \b TQT_SLOT called if the state of the current session change + * \b Slot called if the state of the current session change * \param state boolean represent the state of the session */ void HardwareInfo::handleSessionState( bool state ) { if( state != sessionIsActive ) { sessionIsActive = state; - TQTimer::singleShot(50, this, TQT_SLOT(emitSessionActiveState())); + TQTimer::singleShot(50, this, TQ_SLOT(emitSessionActiveState())); } } @@ -161,7 +166,7 @@ void HardwareInfo::processHardwareChangedEvent (TDEGenericDevice* device) { if (allUDIs.contains( device->uniqueID() )) { if (device->type() == TDEGenericDeviceType::PowerSupply) { - TQTimer::singleShot(50, this, TQT_SLOT(checkACAdapterState())); + TQTimer::singleShot(50, this, TQ_SLOT(checkACAdapterState())); } else if (device->type() == TDEGenericDeviceType::Battery) { // this is a battery event updateBatteryValues(device); @@ -169,11 +174,11 @@ void HardwareInfo::processHardwareChangedEvent (TDEGenericDevice* device) { TDEEventDevice* edevice = dynamic_cast<TDEEventDevice*>(device); if (edevice) { if (edevice->eventType() == TDEEventDeviceType::ACPILidSwitch) { - TQTimer::singleShot(50, this, TQT_SLOT(checkLidcloseState())); + TQTimer::singleShot(50, this, TQ_SLOT(checkLidcloseState())); } } } else if (device->type() == TDEGenericDeviceType::Backlight) { - TQTimer::singleShot(50, this, TQT_SLOT(checkBrightness())); + TQTimer::singleShot(50, this, TQ_SLOT(checkBrightness())); } // TODO: add needed code } else { @@ -193,34 +198,23 @@ void HardwareInfo::processKeyPressEvent(unsigned int keycode, TDEEventDevice* ed // FIXME // How can I get specific button press/release information (instead of just "something happened to the button") from the TDE hardware library? - // TODO: Check if we really need to monitor this events. We get maybe also - // HAL_PROPERTY_CHANGED event for the key -// if (message.startsWith("ButtonPressed")) { kdDebug() << "ButtonPressed event from TDE HW library " << endl; if (((edevice->eventType() == TDEEventDeviceType::ACPIPowerButton) || (edevice->eventType() == TDEEventDeviceType::ACPIOtherInput)) && (keycode == KEY_POWER)) { - TQTimer::singleShot(50, this, TQT_SLOT(emitPowerButtonPressed())); + TQTimer::singleShot(50, this, TQ_SLOT(emitPowerButtonPressed())); } else if (((edevice->eventType() == TDEEventDeviceType::ACPISuspendButton) || (edevice->eventType() == TDEEventDeviceType::ACPIOtherInput)) && (keycode == KEY_SLEEP)) { - TQTimer::singleShot(50, this, TQT_SLOT(emitSuspendButtonPressed())); + TQTimer::singleShot(50, this, TQ_SLOT(emitSuspendButtonPressed())); } else if (((edevice->eventType() == TDEEventDeviceType::ACPISuspendButton) || (edevice->eventType() == TDEEventDeviceType::ACPIOtherInput)) && (keycode == KEY_SUSPEND)) { - TQTimer::singleShot(50, this, TQT_SLOT(emitS2diskButtonPressed())); -// } else if (value.startsWith("brightness-")) { -// if (!brightness_in_hardware && value.endsWith("-up")) -// TQTimer::singleShot(50, this, TQT_SLOT(brightnessUpPressed())); -// else if (!brightness_in_hardware && value.endsWith("-down")) -// TQTimer::singleShot(50, this, TQT_SLOT(brightnessDownPressed())); + TQTimer::singleShot(50, this, TQ_SLOT(emitS2diskButtonPressed())); } -// } else { -// kdDebug() << "Unmonitored HAL_CONDITION: " << message << " : " << value << endl; -// } kdDebugFuncOut(trace); } /*! - * This TQT_SLOT is used to fetch the resume signal and multiplex. If needed some + * This slot is used to fetch the resume signal and multiplex. If needed some * actions after resume, do this here. * \param result integer with the result of the resume/suspend */ @@ -611,7 +605,7 @@ bool HardwareInfo::intialiseHWInfo() { if (!allUDIs.contains( hwdevice->uniqueID() )) { allUDIs.append( hwdevice->uniqueID() ); } - connect(edevice, TQT_SIGNAL(switchChanged()), this, TQT_SLOT(checkLidcloseState())); + connect(edevice, TQ_SIGNAL(switchChanged()), this, TQ_SLOT(checkLidcloseState())); checkLidcloseState(); } } @@ -628,7 +622,7 @@ bool HardwareInfo::intialiseHWInfo() { Battery *bat; for (bat = BatteryList.first(); bat; bat = BatteryList.next() ) { if (bat->getType() == BAT_PRIMARY) { - connect(bat, TQT_SIGNAL(changedBattery()),this, TQT_SLOT(updatePrimaryBatteries())); + connect(bat, TQ_SIGNAL(changedBattery()),this, TQ_SLOT(updatePrimaryBatteries())); } } @@ -637,7 +631,7 @@ bool HardwareInfo::intialiseHWInfo() { } /*! - * The function/TQT_SLOT checks the state of the AC adapter. + * The function/slot checks the state of the AC adapter. */ void HardwareInfo::checkACAdapterState() { kdDebugFuncIn(trace); @@ -728,10 +722,10 @@ void HardwareInfo::updatePrimaryBatteries () { if (primaryBatteries->getNumBatteries() < 1) { setPrimaryBatteriesWarningLevel(); primaryBatteries->refreshInfo( BatteryList ); - connect(primaryBatteries, TQT_SIGNAL(batteryChanged()), this, - TQT_SLOT(setPrimaryBatteriesChanges())); - connect(primaryBatteries, TQT_SIGNAL(batteryWarnState(int,int)), this, - TQT_SLOT(emitBatteryWARNState(int,int))); + connect(primaryBatteries, TQ_SIGNAL(batteryChanged()), this, + TQ_SLOT(setPrimaryBatteriesChanges())); + connect(primaryBatteries, TQ_SIGNAL(batteryWarnState(int,int)), this, + TQ_SLOT(emitBatteryWARNState(int,int))); } else { setPrimaryBatteriesWarningLevel(); primaryBatteries->refreshInfo( BatteryList ); @@ -900,31 +894,70 @@ bool HardwareInfo::suspend( suspend_type suspend ) { } /*! - * Function to set brightness via TDE hardware library (if supported by hardware) + * Function to set brightness level via TDE hardware library (if supported by hardware) * \param level Integer with the level to set, (range: 0 - \ref availableBrightnessLevels ) - * \param percent Integer with the brightness percentage to set * \return boolean with result of the operation * \retval true if successful * \retval false else, if a error occurs */ -bool HardwareInfo::setBrightness ( int level, int percent ){ - if (trace) kdDebug() << funcinfo << "IN: " << "level: " << level << " percent: " << percent << endl; +bool HardwareInfo::setBrightnessLevel(int level) +{ + if (trace) kdDebug() << funcinfo << "IN: " << "level: " << level << endl; bool retval = false; + int maxBrightness = getMaxBrightnessLevel(); + + // Use the first backlight in the list + TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::Backlight); + TDEGenericDevice *hwdevice; + hwdevice = hwlist.first(); + TDEBacklightDevice *backlightdevice = static_cast<TDEBacklightDevice*>(hwdevice); - if ((level == -1) && (percent >= 0)) { - if (percent == 0) { + if (backlightdevice) { + if (!brightness) { + checkBrightness(); + } + + // Make sure level is within valid range + if (level < 0) { level = 0; - } else if (percent >= 98) { - level = (availableBrightnessLevels - 1); + } + else if (level > maxBrightness) { + level = maxBrightness; + } + + if (!brightness) { + kdError() << "Change brightness not supported " << endl; } else { - level = (int)((float)availableBrightnessLevels * ((float)percent/100.0)); - if (level > (availableBrightnessLevels -1)) - level = availableBrightnessLevels -1; - kdDebug() << "percentage mapped to new level: " << level << endl; + if (currentBrightnessLevel == level) { + kdDebug() << "Brightness level not changed, requested level == current level" << endl; + retval = true; + } else { + backlightdevice->setRawBrightness(level); + retval = true; + } } } + // check for actual brightness level to be sure everything was set correct + checkCurrentBrightness(); + kdDebugFuncOut(trace); + return retval; +} + +/*! + * Function to set brightness percentage via TDE hardware library (if supported by hardware) + * \param percent Integer with the brightness percentage to set + * \return boolean with result of the operation + * \retval true if successful + * \retval false else, if a error occurs + */ +bool HardwareInfo::setBrightnessPercentage(int percent) +{ + if (trace) kdDebug() << funcinfo << "IN: " << " percent: " << percent << endl; + + bool retval = false; + // Use the first backlight in the list TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::Backlight); TDEGenericDevice *hwdevice; @@ -936,13 +969,24 @@ bool HardwareInfo::setBrightness ( int level, int percent ){ checkBrightness(); } - if (!brightness || (level < 0 ) || (level >= availableBrightnessLevels)) { - kdError() << "Change brightness or requested level not supported " << endl; - } else { + if (!brightness) { + kdError() << "Change brightness not supported " << endl; + } + else { + // Make sure percentage is within valid range + if (percent < 0) { + percent = 0; + } + else if (percent > 100) { + percent = 100; + } + + int level = (int)round(getMaxBrightnessLevel() * percent / 100.0); if (currentBrightnessLevel == level) { kdDebug() << "Brightness level not changed, requested level == current level" << endl; retval = true; - } else { + } + else { backlightdevice->setRawBrightness(level); retval = true; } @@ -1082,9 +1126,9 @@ bool HardwareInfo::setCPUFreqGovernor( const char *governor ) { // --> TDE hardware library method call (trigger actions) section -- END <--- // --> private helper functions/slots to forward/handle events -- START <-- -// need this functions to make events from HAL/D-Bus independent -// from QT event loop and to allow QT3 D-Bus bindings to get not -// blocked by normal KDE/QT (GUI) calls +// need this functions to make events independent +// from TQt event loop and to allow TQt3 D-Bus bindings to get not +// blocked by normal TDE/TQt (GUI) calls /*! * Function to emit the signal for the Power button. */ @@ -1139,35 +1183,10 @@ bool HardwareInfo::setBrightnessUp(int percentageStep) { checkCurrentBrightness(); - if (supportBrightness() && (getCurrentBrightnessLevel() >= 0) && - (getCurrentBrightnessLevel() != (getMaxBrightnessLevel()-1))) { - int setTo = 0; - int minPercStep = 10; - int currentPerc = (int)(((float)getCurrentBrightnessLevel()/(float)(getMaxBrightnessLevel()-1))*100.0); - - if (percentageStep > 0 && (percentageStep <= (100-currentPerc))) { - minPercStep = percentageStep; - } - - if ((currentPerc + minPercStep) > 100) { - // set to 100 % - setTo = getMaxBrightnessLevel() -1; - } else { - setTo = (int)(((float)(getMaxBrightnessLevel()-1))*(((float)(currentPerc + minPercStep))/100.0)); - if ((setTo == getCurrentBrightnessLevel()) && (setTo < (getMaxBrightnessLevel() -1))) { - setTo++; - } - } - - if (trace) { - kdDebug() << "Max: " << getMaxBrightnessLevel() - << " Current: " << getCurrentBrightnessLevel() - << " minPercStep: " << minPercStep - << " currentPerc: " << currentPerc - << " setTo: " << setTo << endl; - } - - retval = setBrightness(setTo, -1); + if (supportBrightness() && percentageStep > 0) + { + int currentPerc = (int)round(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); + retval = setBrightnessPercentage(currentPerc + percentageStep); } kdDebugFuncOut(trace); @@ -1188,78 +1207,15 @@ bool HardwareInfo::setBrightnessDown(int percentageStep) { checkCurrentBrightness(); - if (supportBrightness() && (getCurrentBrightnessLevel() > 0)) { - int setTo = 0; - int minPercStep = 10; - int currentPerc = (int)(((float)getCurrentBrightnessLevel()/(float)(getMaxBrightnessLevel()-1))*100.0); - - if (percentageStep > 0 && (percentageStep < currentPerc)) { - minPercStep = percentageStep; - } - - if ((currentPerc - minPercStep) < 0) { - setTo = 0; - } else { - setTo = (int)(((float)(getMaxBrightnessLevel()-1))*(((float)(currentPerc - minPercStep))/100.0)); - if ((setTo == getCurrentBrightnessLevel()) && (setTo > 0)) { - setTo--; - } - } - - if (trace) { - kdDebug() << "Max: " << getMaxBrightnessLevel() - << " Current: " << getCurrentBrightnessLevel() - << " minPercStep: " << minPercStep - << " currentPerc: " << currentPerc - << " setTo: " << setTo << endl; - } - - retval = setBrightness(setTo, -1); + if (supportBrightness() && percentageStep > 0) { + int currentPerc = (int)round(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); + retval = setBrightnessPercentage(currentPerc - percentageStep); } kdDebugFuncOut(trace); return retval; } -/*! - * Function to handle the signal for the brightness up button/key - */ -void HardwareInfo::brightnessUpPressed() { - kdDebugFuncIn(trace); - - if (brightness) { - if (!sessionIsActive) { - kdWarning() << "Session is not active, don't react on brightness up key event!" << endl; - } else { - if (currentBrightnessLevel < availableBrightnessLevels) { - setBrightnessUp(); - } else { - kdWarning() << "Could not set brightness to higher level, it's already set to max." << endl; - } - } - } - kdDebugFuncOut(trace); -} - -/*! - * Function to handle the signal for the brightness down button/key - */ -void HardwareInfo::brightnessDownPressed() { - kdDebugFuncIn(trace); - - if (brightness) { - if (!sessionIsActive) { - kdWarning() << "Session is not active, don't react on brightness down key event!" << endl; - } else { - if (currentBrightnessLevel > 0) { - setBrightnessDown(); - } else { - kdWarning() << "Could not set brightness to lower level, it's already set to min." << endl; - } - } - } -} - // --> private helper slots to forward/handle events -- END <-- // --> get private members section -- START <--- @@ -1290,7 +1246,7 @@ bool HardwareInfo::getLidclose() const { */ int HardwareInfo::getMaxBrightnessLevel() const { if (brightness) - return availableBrightnessLevels; + return availableBrightnessLevels - 1; else return -1; } |