Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

suspendmonitor.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #include <qtopia/qcopenvelope_qws.h>
00021 #include <qtopia/qpeapplication.h>
00022 #include "suspendmonitor.h"
00023 
00024 
00025 #ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
00026 #define QTOPIA_MIN_SCREEN_DISABLE_TIME ((int) 300)  // min 5 minutes before forced suspend kicks in
00027 #endif
00028 
00029 
00030 TempScreenSaverMonitor::TempScreenSaverMonitor(QObject *parent, const char *name)
00031     : QObject(parent, name)
00032 {
00033     currentMode = QPEApplication::Enable;
00034     timerId = 0;
00035 }
00036 
00037 void TempScreenSaverMonitor::setTempMode(int mode, int pid)
00038 {
00039     removeOld(pid);
00040     switch(mode) {
00041         case QPEApplication::Disable:
00042             sStatus[0].append(pid);
00043             break;
00044         case QPEApplication::DisableLightOff:
00045             sStatus[1].append(pid);
00046             break;
00047         case QPEApplication::DisableSuspend:
00048             sStatus[2].append(pid);
00049             break;
00050         case QPEApplication::Enable:
00051             break;
00052         default:
00053             owarn << "Unrecognized temp power setting.  Ignored" << oendl; 
00054             return;
00055     }
00056     updateAll();
00057 }
00058 
00059 // Returns true if app had set a temp Mode earlier
00060 bool TempScreenSaverMonitor::removeOld(int pid)
00061 {
00062     QValueList<int>::Iterator it;
00063     for (int i = 0; i < 3; i++) {
00064         it = sStatus[i].find(pid);
00065         if ( it != sStatus[i].end() ) {
00066             sStatus[i].remove( it );
00067             return TRUE;
00068         }
00069     }
00070     return FALSE;
00071 }
00072 
00073 void TempScreenSaverMonitor::updateAll()
00074 {
00075     int mode = QPEApplication::Enable;
00076     if ( sStatus[0].count() ) {
00077         mode = QPEApplication::Disable;
00078     } else if ( sStatus[1].count() ) {
00079         mode = QPEApplication::DisableLightOff;
00080     } else if ( sStatus[2].count() ) {
00081         mode = QPEApplication::DisableSuspend;
00082     } 
00083 
00084     if ( mode != currentMode ) {
00085 #ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
00086         if ( currentMode == QPEApplication::Enable) {
00087             int tid = timerValue();
00088             if ( tid )
00089                 timerId = startTimer( tid * 1000 );
00090         } else if ( mode == QPEApplication::Enable ) {
00091             if ( timerId ) {
00092                 killTimer(timerId);
00093                 timerId = 0;
00094             }
00095         }
00096 #endif
00097         currentMode = mode;
00098         QCopEnvelope("QPE/System", "setScreenSaverMode(int)") << mode;
00099     }
00100 }
00101 
00102 void TempScreenSaverMonitor::applicationTerminated(int pid)
00103 {
00104     if ( removeOld(pid) )
00105         updateAll();
00106 }
00107 
00108 int TempScreenSaverMonitor::timerValue()
00109 {
00110     int tid = 0;
00111 #ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
00112     tid = QTOPIA_MAX_SCREEN_DISABLE_TIME;
00113 
00114     char *env = getenv("QTOPIA_DISABLED_APM_TIMEOUT");
00115     if ( !env ) 
00116         return tid;
00117 
00118     QString strEnv = env;
00119     bool ok = FALSE;
00120     int envTime = strEnv.toInt(&ok);
00121 
00122     if ( ok ) {
00123         if ( envTime < 0 )
00124             return 0;
00125         else if ( envTime <= QTOPIA_MIN_SCREEN_DISABLE_TIME )
00126             return tid;
00127         else 
00128             return envTime;
00129     }
00130 #endif
00131 
00132     return tid;
00133 }
00134 
00135 void TempScreenSaverMonitor::timerEvent(QTimerEvent *t)
00136 {
00137 #ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
00138     if ( timerId && (t->timerId() == timerId) ) {
00139         
00140         /*  Clean up    */
00141         killTimer(timerId);
00142         timerId = 0;
00143         currentMode = QPEApplication::Enable;
00144         QCopEnvelope("QPE/System", "setScreenSaverMode(int)") << currentMode;
00145         
00146         // signal starts on a merry-go-round, which ends up in Desktop::togglePower()
00147         emit forceSuspend();
00148         // if we have apm we are asleep at this point, next line will be executed when we
00149         // awake from suspend.
00150         if ( QFile::exists( "/proc/apm" ) ) {
00151             QTime t;
00152             t = t.addSecs( timerValue() );
00153             QString str =  tr("<qt>The running applications disabled the screen saver for more than the allowed time (%1).<p>The system was forced to suspend</p></qt>").arg( t.toString() );
00154             QMessageBox::information(0, tr("Forced suspend"), str);
00155         }
00156         
00157         // Reset all requests.
00158         for (int i = 0; i < 3; i++)
00159             sStatus[i].clear();
00160 
00161         updateAll();
00162     }
00163 #else
00164     Q_UNUSED(t);
00165 #endif
00166 }
00167 

Generated on Sat Nov 5 16:15:33 2005 for OPIE by  doxygen 1.4.2