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

kmilo

kvaio.cpp

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002 
00003    This file implements the KVaio class.
00004 
00005    $ Author: Mirko Boehm $
00006    $ Copyright: (C) 1996-2003, Mirko Boehm $
00007    $ Contact: mirko@kde.org
00008          http://www.kde.org
00009          http://www.hackerbuero.org $
00010    $ License: LGPL with the following explicit clarification:
00011          This code may be linked against any version of the Qt toolkit
00012          from Troll Tech, Norway. $
00013 
00014    $Id: kvaio.cpp 721930 2007-10-06 09:41:04Z apaku $
00015 
00016    * Portions of this code are
00017    * (C) 2001-2002 Stelian Pop <stelian@popies.net> and
00018    * (C) 2001-2002 Alcove <www.alcove.com>.
00019    * Thanks to Stelian for the implementation of the sonypi driver.
00020    
00021    * Modified by Toan Nguyen <nguyenthetoan@gmail.com> 
00022    * to include support for 
00023    *        Fn+F2,F3,F4 (Volume)
00024    *        Fn+F5,F6 (Brightness)
00025    *        Fn+F1 (blankscreen)
00026    *        Fn+F12 (suspend to disk)
00027 */
00028 
00029 #include <kconfig.h>
00030 
00031 #include "kvaio.h"
00032 #include "kmilointerface.h"
00033 
00034 #include <fixx11h.h>
00035 
00036 #include <QLabel>
00037 #include <qevent.h>
00038 #include <QDataStream>
00039 #include <QTextStream>
00040 #include <QDBusReply>
00041 #include <QDBusInterface>
00042 #include <kxmlguiwindow.h>
00043 #include <kconfiggroup.h>
00044 #include <klocale.h>
00045 #include <kdebug.h>
00046 #include <kapplication.h>
00047 #include <ktoolinvocation.h>
00048 
00049 extern "C" {
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 #include <fcntl.h>
00053 #include <unistd.h>
00054 #include <sys/ioctl.h>
00055 #include <getopt.h>
00056 #include <errno.h>
00057 
00058 #include "./sonypi.h"
00059 }
00060 
00061 
00062 KVaio::KVaio(KMiloKVaio *parent, const char* name)
00063     : QObject(parent),
00064       mDisp(0),
00065       mTimer (new QTimer (this) )
00066 {
00067     setObjectName(name);
00068     myparent = parent;
00069     
00070     mDriver = new KVaioDriverInterface(this);
00071 
00072     if(!mDriver->connectToDriver())
00073     {
00074         delete mDriver; mDriver = 0L;
00075         kDebug() << "KVaio: Cannot connect to driver." ;
00076     } else {
00077         kDebug() << "KVaio: Connected to SonyPI driver." ;
00078         connect(mDriver, SIGNAL(vaioEvent(int)), SLOT(slotVaioEvent(int)));
00079     connect (mTimer, SIGNAL (timeout ()), SLOT (slotTimeout() ) );
00080     mTimer->setSingleShot (true);
00081     mTimer->setInterval (10000);
00082     mTimer->start ();
00083     }
00084 
00085     mDisp = XOpenDisplay(NULL);
00086     if(!mDisp)
00087     {
00088         kDebug() << "KVaio ctor: Failed to open display. Very strange."
00089           << endl;
00090     }
00091 
00092     KConfig config("kmilodrc");
00093 
00094     loadConfiguration(&config);
00095 
00096     m_mute = false;
00097     m_progress = 0;
00098     m_minVolume = 0;
00099     m_maxVolume = 100;
00100     m_volume = 50;
00101 
00102     m_VolumeStep = 10;
00103     
00104     m_brightness = 128;
00105     m_minBright = 0;
00106     m_maxBright = 255;
00107     m_BrightnessStep = 16;    
00108     kmixAdaptor = new QDBusInterface("org.kde.kmix", "/Mixer0", "org.kde.KMix");
00109     kmixWindowAdaptor = new QDBusInterface("org.kde.kmix","/kmix/KMixWindow", "org.kde.kmix.KMixWindow");
00110 
00111 //    retrieveVolume();
00112 //    retrieveMute();
00113 
00114 }
00115 
00116 KVaio::~KVaio()
00117 {
00118     kDebug() << "KVaio dtor: shutting down." ;
00119     if(mDriver!=0)
00120     {
00121         mDriver->disconnectFromDriver();
00122     }
00123 }
00124 
00125 void KVaio::slotVaioEvent(int event)
00126 {
00127     QString text;
00128     QTextStream stream(&text, QIODevice::WriteOnly);
00129 
00130     switch(event)
00131     {
00132     case SONYPI_EVENT_FNKEY_RELEASED:
00133         break;
00134     case SONYPI_EVENT_FNKEY_F1:
00135         blankScreen();
00136         break;
00137     case SONYPI_EVENT_FNKEY_F2:
00138         mute();
00139             break;
00140         case SONYPI_EVENT_FNKEY_F3:
00141         VolumeDown(m_VolumeStep); 
00142             break;
00143         case SONYPI_EVENT_FNKEY_F4:
00144         VolumeUp(m_VolumeStep); 
00145             break;
00146         case SONYPI_EVENT_FNKEY_F5:
00147         BrightnessDown(m_BrightnessStep);
00148             break;
00149         case SONYPI_EVENT_FNKEY_F6:
00150         BrightnessUp(m_BrightnessStep);
00151         break;
00152     case SONYPI_EVENT_FNKEY_F12:
00153         suspendToDisk();
00154         break;
00155     case SONYPI_EVENT_MEMORYSTICK_INSERT:
00156         showTextMsg( i18n ("Memory Stick inserted") );
00157         break;
00158     case SONYPI_EVENT_MEMORYSTICK_EJECT:
00159         showTextMsg( i18n ("Memory Stick ejected") );
00160         break;
00161     case SONYPI_EVENT_BACK_PRESSED:
00162         if (mShowPowerStatusOnBackButton)
00163         {
00164         showBatteryStatus (true);
00165         }
00166         break;
00167     default:
00168         stream << i18n("Unhandled event: ") << event;
00169         if(mReportUnknownEvents) showTextMsg(text);
00170         kDebug() << "KVaio::slotVaioEvent: event not handled."
00171               << endl;
00172     }
00173 }
00174 
00175 bool KVaio::showTextMsg(const QString& msg)
00176 {
00177     return myparent->showTextMsg(msg);    
00178 }
00179 
00180 
00181 
00182 bool KVaio::showProgressMsg(const QString& msg, int value)
00183 {
00184     m_progress = value;
00185     return myparent->showProgressMsg(msg,value);
00186 }
00187 
00188 void KVaio::blankScreen()
00189 {
00190 #ifdef __GNUC__
00191 #warning port to DBUS
00192 #endif
00193 
00194 #if 0
00195     bool blankonly;
00196 
00197     if (isKScreensaverAvailable()) {
00198     
00199         QByteArray data, replyData;
00200         QDataStream arg( &data,IO_WriteOnly);
00201 
00202         arg.setVersion(QDataStream::Qt_3_1);
00203         DCOPCString replyType;
00204 
00205     /* Set the screensave to BlankOnly mode */
00206     blankonly = true;
00207         arg << blankonly;
00208     mClient.call("kdesktop","KScreensaverIface","setBlankOnly(bool)",
00209             data, replyType, replyData) ;
00210 
00211     /* Save the screen */
00212     if ( !mClient.call("kdesktop", "KScreensaverIface", "save()",
00213             data, replyType, replyData) )
00214         {
00215             kDebug() << "KVaio::blankScreen: there was some error "
00216                       << "using DCOP." << endl;
00217         }
00218 
00219     /* Set the screensave to its original mode */
00220     blankonly = false;
00221         arg << blankonly;
00222     mClient.call("kdesktop","KScreensaverIface","setBlankOnly(bool)",
00223             data, replyType, replyData) ;
00224 
00225     }
00226 #endif
00227 }
00228 
00229 void KVaio::suspendToDisk()
00230 {
00231 #ifdef __GNUC__
00232 #warning port to DBUS
00233 #endif
00234 
00235 #if 0
00236  
00237         QByteArray data, replyData;
00238         QDataStream arg( &data,IO_WriteOnly);
00239 
00240         arg.setVersion(QDataStream::Qt_3_1);
00241         DCOPCString replyType;
00242 
00243     mClient.call("kpowersave","KPowersaveIface","do_suspendToDisk()",
00244             data, replyType, replyData) ;
00245 #endif
00246 }
00247 
00248 
00249 bool KVaio::isKScreensaverAvailable()
00250 {
00251 #ifdef __GNUC__
00252 #warning port to DBUS
00253 #endif
00254 
00255 #if 0
00256     if(mClient.isAttached())
00257         {
00258         // kDebug() << "KVaio::showTextMsg: attached to DCOP server." ;
00259         if(mClient.isApplicationRegistered("kdesktop"))
00260         {
00261             DCOPCStringList objects;
00262 
00263             // kDebug() << "KVaio::showTextMsg: kded is registered at dcop server."
00264             //           << endl;
00265             objects = mClient.remoteObjects("kdesktop");
00266             if(objects.contains("KScreensaverIface"))
00267             {
00268                 // kDebug() << "KVaio::showTextMsg: kmilod is available at kded."
00269                 //           << endl;
00270                 return true;
00271             } else {
00272                 kDebug() << "KVaio::isKScreensaverAvailable: "
00273                           << "KScreensaverIface is NOT available at kdesktop." << endl;
00274             return false;
00275             }
00276         } else {
00277             kDebug() << "KVaio::isKScreensaverAvailable: "
00278                       << "kdesktop is NOT registered at dcop server." << endl;
00279             return false;
00280         }
00281     } else {
00282         kDebug() << "KVaio::isKScreensaverAvailable: "
00283                   << "kdesktop is NOT registered at dcop server." << endl;
00284         return false;
00285     }
00286 #endif
00287     return false;
00288 }
00289 
00290 bool KVaio::isKMiloDAvailable()
00291 {
00292 #ifdef __GNUC__
00293 #warning port to DBUS
00294 #endif
00295 
00296 #if 0
00297     if(mClient.isAttached())
00298     {
00299         // kDebug() << "KVaio::showTextMsg: attached to DCOP server." ;
00300         if(mClient.isApplicationRegistered("kded"))
00301         {
00302             DCOPCStringList objects;
00303 
00304             // kDebug() << "KVaio::showTextMsg: kded is registered at dcop server."
00305             //           << endl;
00306             objects = mClient.remoteObjects("kded");
00307             if(objects.contains("kmilod"))
00308             {
00309                 // kDebug() << "KVaio::showTextMsg: kmilod is available at kded."
00310                 //           << endl;
00311                 return true;
00312             } else {
00313                 kDebug() << "KVaio::isKMiloDAvailable: "
00314                           << "kmilod is NOT available at kded." << endl;
00315                 return false;
00316             }
00317         } else {
00318             kDebug() << "KVaio::isKMiloDAvailable: "
00319                       << "kded is NOT registered at dcop server." << endl;
00320             return false;
00321         }
00322     } else {
00323         kDebug() << "KVaio::isKMiloDAvailable: "
00324                   << "kded is NOT registered at dcop server." << endl;
00325         return false;
00326     }
00327 #endif
00328     return false;
00329 }
00330 
00331 void KVaio::loadConfiguration(KConfig *k)
00332 {
00333     KConfigGroup gr = k->group("KVaio");
00334 
00335     mReportUnknownEvents =
00336     gr.readEntry("Report_Unknown_Events", false);
00337     mReportPowerStatus =
00338     gr.readEntry("PeriodicallyReportPowerStatus", false);
00339     mShowPowerStatusOnBackButton =
00340     gr.readEntry("PowerStatusOnBackButton", true);
00341 
00342     kDebug() << "KVaio::loadConfiguration: " 
00343               << "       mReportUnknownEvents:      "
00344           << mReportUnknownEvents << endl
00345           << "       mReportPowerStatus:        "
00346           << mReportPowerStatus << endl
00347           << "mShowPowerStatusOnBackButton:     "
00348           << mShowPowerStatusOnBackButton << endl;
00349 }
00350 
00351 const KVaioDriverInterface* KVaio::driver()
00352 {
00353     return mDriver;
00354 }
00355 
00356 void KVaio::slotTimeout ()
00357 {
00358     showBatteryStatus ();
00359 
00360     mTimer->setInterval (4000);
00361     mTimer->start ();
00362 }
00363 
00364 bool KVaio::showBatteryStatus ( bool force )
00365 {
00366     static bool acConnectedCache  = false;
00367     static int previousChargeCache = -1;
00368     bool bat1Avail = false, bat2Avail = false, acConnected = false;
00369     int bat1Remaining = 0, bat1Max = 0, bat2Remaining = 0, bat2Max = 0;
00370     bool displayBatteryMsg = false;
00371     bool displayACStatus = false;
00372 
00373     QString text, acMsg;
00374     QTextStream stream(&text, QIODevice::WriteOnly);
00375 
00376     // -----
00377     // only display on startup if mReportPowerStatus is true:
00378     if (mReportPowerStatus==false &&  !force)
00379     {
00380         return true;
00381     }
00382 
00383     // query all necessary information:
00384     (void) mDriver->getBatteryStatus(bat1Avail, bat1Remaining, bat1Max,
00385                                  bat2Avail, bat2Remaining, bat2Max,
00386                                  acConnected);
00387 
00388     int remaining;
00389     if ( bat1Avail || bat2Avail )
00390         remaining = (int)(100.0*(bat1Remaining+bat2Remaining)
00391                                / (bat1Max+bat2Max));
00392     else
00393         remaining = -1; // no battery available
00394 
00395     if (acConnectedCache != acConnected || force)
00396     {
00397     displayACStatus = true;
00398     acConnectedCache = acConnected;
00399     }
00400 
00401     displayBatteryMsg = ( previousChargeCache * 100 / remaining > 1000 )
00402     || ( previousChargeCache * 100 / remaining > 200 && remaining < 10 )
00403     || force;
00404 
00405 
00406     if (displayBatteryMsg)
00407     {
00408     previousChargeCache = remaining;
00409     }
00410 
00411     // ----- prepare text messages
00412     if (displayACStatus || displayBatteryMsg)
00413     {
00414 
00415     if (displayACStatus)
00416     {
00417         acMsg = acConnected ? i18n ("AC Connected") : i18n ("AC Disconnected");
00418     }
00419 
00420     switch (remaining)
00421     {
00422         case 100:
00423         stream << i18n("Battery is Fully Charged. ");
00424         break;
00425         case 5:
00426         case 4:
00427         case 3:
00428         case 2:
00429         case 1:
00430         stream << i18n("Caution: Battery is Almost Empty (%1% remaining).", remaining);
00431         break;
00432         case 0:
00433         stream << i18n("Alert: Battery is Empty");
00434         break;
00435             case -1:
00436                 stream << i18n("No Battery Inserted.");
00437                 break;
00438         default:
00439         stream << i18n("Remaining Battery Capacity: %1%", remaining );
00440     };
00441 
00442     // show a message if the battery status changed by more then 10% or on startup
00443     if (displayACStatus)
00444     {
00445         stream << endl << acMsg;
00446     }
00447 
00448     return showTextMsg (text);
00449     } else {
00450     return true;
00451     }
00452 }
00453 
00454 void KVaio::BrightnessUp(int step)
00455 {
00456     m_brightness = mDriver->brightness();
00457     
00458     m_brightness += step;
00459     if(m_brightness > m_maxBright) {
00460         m_brightness = m_maxBright;
00461     }
00462     
00463     mDriver->setBrightness(m_brightness);
00464     showProgressMsg( i18n("Brightness"), m_brightness*100/255);
00465 }
00466 
00467 void KVaio::BrightnessDown(int step)
00468 {
00469     m_brightness = mDriver->brightness();
00470     
00471     m_brightness -= step;
00472     if(m_brightness < m_minBright) {
00473         m_brightness = m_minBright;
00474     }
00475     
00476     mDriver->setBrightness(m_brightness);
00477     showProgressMsg( i18n("Brightness"), m_brightness*100/255);
00478 }
00479 
00480 
00481 void KVaio::displayVolume()
00482 {
00483 //        _interface->displayProgress(i18n("Volume"), m_volume);
00484 
00485     showProgressMsg(i18n("Volume"), m_volume);
00486         // If we got this far, the DCOP communication with kmix works,
00487     // so we don't have to test the result.
00488         kmixAdaptor->call("setMasterVolume", m_volume);
00489         if (m_mute)
00490         {
00491                 m_mute = false;
00492                 kmixAdaptor->call("setMute",  QString(), m_mute);
00493         }
00494 }
00495 
00496 
00497 bool KVaio::retrieveVolume() {
00498        bool kmix_error = false;
00499 
00500         QDBusReply<int> reply = kmixAdaptor->call("masterVolume");
00501         if( reply.isValid())
00502              m_volume = reply;
00503         else
00504             kmix_error = true;
00505         if (kmix_error) { // maybe the error occurred because kmix wasn't running
00506            if (KToolInvocation::startServiceByDesktopName("kmix")==0) { // trying to start kmix
00507               // trying again
00508               reply = kmixAdaptor->call("masterVolume");
00509               if (reply.isValid())  {
00510                  m_volume = reply;
00511                  kmix_error = false;
00512                  kmixWindowAdaptor->call("minimize");
00513               }
00514            }
00515         }
00516        
00517         if (kmix_error)
00518         {
00519                 kDebug() << "KMilo: GenericMonitor could not access kmix/Mixer0 via dcop"
00520                                                         << endl;
00521                 //_interface->displayText
00522         showTextMsg(i18n("It seems that KMix is not running."));
00523 
00524                 return false;
00525         } else {
00526                 return true;
00527         }
00528 }
00529 
00530 void KVaio::VolumeUp(int step)
00531 {
00532         if (!retrieveVolume())
00533                 return;
00534 
00535         // FIXME if the mixer doesn't support steps of the specified size it
00536         // could get stuck at one position
00537         m_volume += step;
00538         if (m_volume > m_maxVolume)
00539                 m_volume = m_maxVolume;
00540 
00541         displayVolume();
00542 }
00543 
00544 void KVaio::VolumeDown(int step)
00545 {
00546         if (!retrieveVolume())
00547                 return;
00548 
00549         m_volume -= step;
00550         if (m_volume < m_minVolume)
00551                 m_volume = m_minVolume;
00552 
00553         displayVolume();
00554 }
00555 
00556 bool KVaio::retrieveMute() 
00557 {
00558         bool kmix_error = false;
00559         QDBusReply<bool> reply = kmixAdaptor->call("mute");
00560         if( reply.isValid())
00561              m_volume = reply;
00562         else
00563             kmix_error = true;
00564         if (kmix_error) { // maybe the error occurred because kmix wasn't running
00565            showTextMsg(i18n("Starting KMix..."));
00566            if (KToolInvocation::startServiceByDesktopName("kmix")==0) { // trying to start kmix
00567               // trying again
00568               reply = kmixAdaptor->call("mute",0);
00569               if (reply.isValid())  {
00570                  m_mute = reply;
00571                  kmix_error = false;
00572                  kmixWindowAdaptor->call("minimize");
00573               }
00574            }
00575            else
00576            {
00577                   kmixWindowAdaptor->call("minimize");
00578                   kmix_error = true;
00579            }
00580         }
00581 
00582         if (kmix_error)
00583         {
00584                 kDebug() << "KMilo: GenericMonitor could not access kmix/Mixer0 via dcop"
00585                                                         << endl;
00586                 //_interface->displayText
00587         showTextMsg(i18n("It seems that KMix is not running."));
00588 
00589                 return false;
00590         } else {
00591                 return true;
00592         }
00593 }
00594 
00595 void KVaio::mute() 
00596 {
00597     if (!retrieveMute())
00598     return;
00599 
00600     m_mute = !m_mute;
00601 
00602     int newVolume;
00603     QString muteText;
00604     if (m_mute)
00605     {
00606             m_oldVolume = m_volume;
00607             newVolume = 0;
00608             muteText = i18n("Mute on");
00609     } else {
00610             newVolume = m_oldVolume;
00611             muteText = i18n("Mute off");
00612     }
00613     
00614     kmixAdaptor->call("setMute", QString(),m_mute);
00615 
00616     //_interface->displayText(muteText);
00617     showTextMsg(muteText);
00618 
00619 }
00620 
00621 
00622 #include "kvaio.moc"

kmilo

Skip menu "kmilo"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • 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