author | sandman <sandman> | 2002-12-10 22:15:30 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-12-10 22:15:30 (UTC) |
commit | 2173e57fcbd5d460fceb34a111845389dca56506 (patch) (side-by-side diff) | |
tree | 462b451f7c75e3960472468c55ae0037595d1af1 | |
parent | 51f44c6a22971d6f60f5e36523d9efbeaa8aec55 (diff) | |
download | opie-2173e57fcbd5d460fceb34a111845389dca56506.zip opie-2173e57fcbd5d460fceb34a111845389dca56506.tar.gz opie-2173e57fcbd5d460fceb34a111845389dca56506.tar.bz2 |
don't use atd anymore - our atd is only simple shell script, so better get
rid of it.
-rw-r--r-- | library/alarmserver.cpp | 519 |
1 files changed, 288 insertions, 231 deletions
diff --git a/library/alarmserver.cpp b/library/alarmserver.cpp index 5e4dd18..2ea4025 100644 --- a/library/alarmserver.cpp +++ b/library/alarmserver.cpp @@ -40,13 +40,18 @@ -struct timerEventItem { - time_t UTCtime; - QCString channel, message; - int data; - bool operator==( const timerEventItem &right ) const - { - return ( UTCtime == right.UTCtime - && channel == right.channel - && message == right.message - && data == right.data ); - } + +#undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume + + +struct timerEventItem +{ + time_t UTCtime; + QCString channel, message; + int data; + bool operator==( const timerEventItem &right ) const + { + return ( UTCtime == right.UTCtime + && channel == right.channel + && message == right.message + && data == right.data ); + } }; @@ -56,11 +61,16 @@ class TimerReceiverObject : public QObject public: - TimerReceiverObject() { } - ~TimerReceiverObject() { } - void resetTimer(); - void setTimerEventItem(); - void deleteTimer(); + TimerReceiverObject() + { } + ~TimerReceiverObject() + { } + void resetTimer(); + void setTimerEventItem(); + void deleteTimer(); protected: - void timerEvent( QTimerEvent *te ); + void timerEvent( QTimerEvent *te ); + +#ifdef USE_ATD private: - QString atfilename; + QString atfilename; +#endif }; @@ -75,13 +85,13 @@ void setNearestTimerEvent() { - nearestTimerEvent = NULL; - QListIterator<timerEventItem> it( timerEventList ); - if ( *it ) - nearestTimerEvent = *it; - for ( ; *it; ++it ) - if ( (*it)->UTCtime < nearestTimerEvent->UTCtime ) - nearestTimerEvent = *it; - if (nearestTimerEvent) - timerEventReceiver->resetTimer(); - else - timerEventReceiver->deleteTimer(); + nearestTimerEvent = NULL; + QListIterator<timerEventItem> it( timerEventList ); + if ( *it ) + nearestTimerEvent = *it; + for ( ; *it; ++it ) + if ( (*it)->UTCtime < nearestTimerEvent->UTCtime ) + nearestTimerEvent = *it; + if (nearestTimerEvent) + timerEventReceiver->resetTimer(); + else + timerEventReceiver->deleteTimer(); } @@ -94,28 +104,29 @@ static void saveState() { - QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); - if ( timerEventList.isEmpty() ) { - unlink( savefilename ); - return; - } + QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); + if ( timerEventList.isEmpty() ) { + unlink( savefilename ); + return ; + } - QFile savefile(savefilename+".new"); - if ( savefile.open(IO_WriteOnly) ) { - QDataStream ds( &savefile ); + QFile savefile(savefilename + ".new"); + if ( savefile.open(IO_WriteOnly) ) { + QDataStream ds( &savefile ); - //save + //save - QListIterator<timerEventItem> it( timerEventList ); - for ( ; *it; ++it ) { - ds << it.current()->UTCtime; - ds << it.current()->channel; - ds << it.current()->message; - ds << it.current()->data; - } + QListIterator<timerEventItem> it( timerEventList ); + for ( ; *it; ++it ) { + ds << it.current()->UTCtime; + ds << it.current()->channel; + ds << it.current()->message; + ds << it.current()->data; + } - savefile.close(); - unlink( savefilename ); - QDir d; d.rename(savefilename+".new",savefilename); + savefile.close(); + unlink( savefilename ); + QDir d; + d.rename(savefilename + ".new", savefilename); - } + } } @@ -127,22 +138,22 @@ void AlarmServer::initialize() { - //read autosave file and put events in timerEventList - - QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); - - QFile savefile(savefilename); - if ( savefile.open(IO_ReadOnly) ) { - QDataStream ds( &savefile ); - while ( !ds.atEnd() ) { - timerEventItem *newTimerEventItem = new timerEventItem; - ds >> newTimerEventItem->UTCtime; - ds >> newTimerEventItem->channel; - ds >> newTimerEventItem->message; - ds >> newTimerEventItem->data; - timerEventList.append( newTimerEventItem ); + //read autosave file and put events in timerEventList + + QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); + + QFile savefile(savefilename); + if ( savefile.open(IO_ReadOnly) ) { + QDataStream ds( &savefile ); + while ( !ds.atEnd() ) { + timerEventItem *newTimerEventItem = new timerEventItem; + ds >> newTimerEventItem->UTCtime; + ds >> newTimerEventItem->channel; + ds >> newTimerEventItem->message; + ds >> newTimerEventItem->data; + timerEventList.append( newTimerEventItem ); + } + savefile.close(); + if (!timerEventReceiver) + timerEventReceiver = new TimerReceiverObject; + setNearestTimerEvent(); } - savefile.close(); - if (!timerEventReceiver) - timerEventReceiver = new TimerReceiverObject; - setNearestTimerEvent(); - } } @@ -150,3 +161,3 @@ void AlarmServer::initialize() - +#ifdef USE_ATD @@ -156,23 +167,46 @@ static bool triggerAtd( bool writeHWClock = FALSE ) { - QFile trigger(QString(atdir) + "trigger"); - if ( trigger.open(IO_WriteOnly|IO_Raw) ) { - if ( trigger.writeBlock("\n",2) != 2 ) { - QMessageBox::critical( 0, QObject::tr( "Out of Space" ), - QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); - trigger.close(); - QFile::remove( trigger.name() ); - return FALSE; + QFile trigger(QString(atdir) + "trigger"); + if ( trigger.open(IO_WriteOnly | IO_Raw) ) { + if ( trigger.writeBlock("\n", 2) != 2 ) { + QMessageBox::critical( 0, QObject::tr( "Out of Space" ), + QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); + trigger.close(); + QFile::remove + ( trigger.name() ); + return FALSE; + } + return TRUE; + } + return FALSE; +} + +#else + +static bool writeResumeAt ( time_t wakeup ) +{ + FILE *fp = ::fopen ( "/var/run/resumeat", "w" ); + + if ( fp ) { + ::fprintf ( fp, "%d\n", (int) wakeup ); + ::fclose ( fp ); } - return TRUE; - } - return FALSE; + else + qWarning ( "Failed to write wakeup time to /var/run/resumeat" ); + + return ( fp ); } +#endif + void TimerReceiverObject::deleteTimer() { - if ( !atfilename.isEmpty() ) { - unlink( atfilename ); - atfilename = QString::null; - triggerAtd( FALSE ); - } +#ifdef USE_ATD + if ( !atfilename.isEmpty() ) { + unlink( atfilename ); + atfilename = QString::null; + triggerAtd( FALSE ); + } +#else + writeResumeAt ( 0 ); +#endif } @@ -181,50 +215,62 @@ void TimerReceiverObject::resetTimer() { - const int maxsecs = 2147000; - int total_written; - QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); - QDateTime now = QDateTime::currentDateTime(); - if ( nearest < now ) - nearest = now; - int secs = TimeConversion::secsTo( now, nearest ); - if ( secs > maxsecs ) { - // too far for millisecond timing - secs = maxsecs; - } - - // System timer (needed so that we wake from deep sleep), - // from the Epoch in seconds. - // - int at_secs = TimeConversion::toUTC(nearest); - // qDebug("reset timer to %d seconds from Epoch",at_secs); - QString fn = atdir + QString::number(at_secs) + "." - + QString::number(getpid()); - if ( fn != atfilename ) { - QFile atfile(fn+".new"); - if ( atfile.open(IO_WriteOnly|IO_Raw) ) { - // just wake up and delete the at file - QString cmd = "#!/bin/sh\nrm " + fn; - total_written = atfile.writeBlock(cmd.latin1(),cmd.length()); - if ( total_written != int(cmd.length()) ) { - QMessageBox::critical( 0, tr("Out of Space"), - tr("Unable to schedule alarm.\n" - "Please free up space and try again") ); - atfile.close(); - QFile::remove( atfile.name() ); - return; - } - atfile.close(); - unlink( atfilename ); - QDir d; d.rename(fn+".new",fn); - chmod(fn.latin1(),0755); - atfilename = fn; - triggerAtd( FALSE ); - } else { - qWarning("Cannot open atd file %s",fn.latin1()); + const int maxsecs = 2147000; + QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); + QDateTime now = QDateTime::currentDateTime(); + if ( nearest < now ) + nearest = now; + int secs = TimeConversion::secsTo( now, nearest ); + if ( secs > maxsecs ) { + // too far for millisecond timing + secs = maxsecs; + } + + // System timer (needed so that we wake from deep sleep), + // from the Epoch in seconds. + // + int at_secs = TimeConversion::toUTC(nearest); + // qDebug("reset timer to %d seconds from Epoch",at_secs); + +#ifdef USE_ATD + + QString fn = atdir + QString::number(at_secs) + "." + + QString::number(getpid()); + if ( fn != atfilename ) { + QFile atfile(fn + ".new"); + if ( atfile.open(IO_WriteOnly | IO_Raw) ) { + int total_written; + + // just wake up and delete the at file + QString cmd = "#!/bin/sh\nrm " + fn; + total_written = atfile.writeBlock(cmd.latin1(), cmd.length()); + if ( total_written != int(cmd.length()) ) { + QMessageBox::critical( 0, tr("Out of Space"), + tr("Unable to schedule alarm.\n" + "Please free up space and try again") ); + atfile.close(); + QFile::remove + ( atfile.name() ); + return ; + } + atfile.close(); + unlink( atfilename ); + QDir d; + d.rename(fn + ".new", fn); + chmod(fn.latin1(), 0755); + atfilename = fn; + triggerAtd( FALSE ); + } + else { + qWarning("Cannot open atd file %s", fn.latin1()); + } } - } - // Qt timers (does the actual alarm) - // from now in milliseconds - // - qDebug("AlarmServer waiting %d seconds",secs); - startTimer( 1000 * secs + 500 ); +#else + writeResumeAt ( at_secs ); + +#endif + + // Qt timers (does the actual alarm) + // from now in milliseconds + // + qDebug("AlarmServer waiting %d seconds", secs); + startTimer( 1000 * secs + 500 ); } @@ -233,22 +279,24 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) { - bool needSave = FALSE; - killTimers(); - if (nearestTimerEvent) { - if ( nearestTimerEvent->UTCtime - <= TimeConversion::toUTC(QDateTime::currentDateTime()) ) { + bool needSave = FALSE; + killTimers(); + if (nearestTimerEvent) { + if ( nearestTimerEvent->UTCtime + <= TimeConversion::toUTC(QDateTime::currentDateTime()) ) { #ifndef QT_NO_COP - QCopEnvelope e( nearestTimerEvent->channel, - nearestTimerEvent->message ); - e << TimeConversion::fromUTC( nearestTimerEvent->UTCtime ) - << nearestTimerEvent->data; + QCopEnvelope e( nearestTimerEvent->channel, + nearestTimerEvent->message ); + e << TimeConversion::fromUTC( nearestTimerEvent->UTCtime ) + << nearestTimerEvent->data; #endif - timerEventList.remove( nearestTimerEvent ); - needSave = TRUE; + + timerEventList.remove( nearestTimerEvent ); + needSave = TRUE; + } + setNearestTimerEvent(); } - setNearestTimerEvent(); - } else { - resetTimer(); - } - if ( needSave ) - saveState(); + else { + resetTimer(); + } + if ( needSave ) + saveState(); } @@ -258,3 +306,3 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) \brief The AlarmServer class allows alarms to be scheduled and unscheduled. - + Applications can schedule alarms with addAlarm() and can @@ -264,3 +312,3 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) additional data). - + Scheduling an alarm using this class is important (rather just using @@ -269,3 +317,3 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) battery usage while asleep. - + \ingroup qtopiaemb @@ -278,3 +326,3 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) be sent to \a channel, with \a data as a parameter. - + If this function is called with exactly the same data as a previous @@ -282,3 +330,3 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) with a given set of parameters. - + \sa deleteAlarm() @@ -286,43 +334,46 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) void AlarmServer::addAlarm ( QDateTime when, const QCString& channel, - const QCString& message, int data) + const QCString& message, int data) { - if ( qApp->type() == QApplication::GuiServer ) { - bool needSave = FALSE; - // Here we are the server so either it has been directly called from - // within the server or it has been sent to us from a client via QCop - if (!timerEventReceiver) - timerEventReceiver = new TimerReceiverObject; - - timerEventItem *newTimerEventItem = new timerEventItem; - newTimerEventItem->UTCtime = TimeConversion::toUTC( when ); - newTimerEventItem->channel = channel; - newTimerEventItem->message = message; - newTimerEventItem->data = data; - // explore the case of already having the event in here... - QListIterator<timerEventItem> it( timerEventList ); - for ( ; *it; ++it ) - if ( *(*it) == *newTimerEventItem ) - return; - // if we made it here, it is okay to add the item... - timerEventList.append( newTimerEventItem ); - needSave = TRUE; - // quicker than using setNearestTimerEvent() - if ( nearestTimerEvent ) { - if (newTimerEventItem->UTCtime < nearestTimerEvent->UTCtime) { - nearestTimerEvent = newTimerEventItem; - timerEventReceiver->killTimers(); - timerEventReceiver->resetTimer(); - } - } else { - nearestTimerEvent = newTimerEventItem; - timerEventReceiver->resetTimer(); + if ( qApp->type() == QApplication::GuiServer ) { + bool needSave = FALSE; + // Here we are the server so either it has been directly called from + // within the server or it has been sent to us from a client via QCop + if (!timerEventReceiver) + timerEventReceiver = new TimerReceiverObject; + + timerEventItem *newTimerEventItem = new timerEventItem; + newTimerEventItem->UTCtime = TimeConversion::toUTC( when ); + newTimerEventItem->channel = channel; + newTimerEventItem->message = message; + newTimerEventItem->data = data; + // explore the case of already having the event in here... + QListIterator<timerEventItem> it( timerEventList ); + for ( ; *it; ++it ) + if ( *(*it) == *newTimerEventItem ) + return ; + // if we made it here, it is okay to add the item... + timerEventList.append( newTimerEventItem ); + needSave = TRUE; + // quicker than using setNearestTimerEvent() + if ( nearestTimerEvent ) { + if (newTimerEventItem->UTCtime < nearestTimerEvent->UTCtime) { + nearestTimerEvent = newTimerEventItem; + timerEventReceiver->killTimers(); + timerEventReceiver->resetTimer(); + } + } + else { + nearestTimerEvent = newTimerEventItem; + timerEventReceiver->resetTimer(); + } + if ( needSave ) + saveState(); } - if ( needSave ) - saveState(); - } else { + else { #ifndef QT_NO_COP - QCopEnvelope e( "QPE/System", "addAlarm(QDateTime,QCString,QCString,int)" ); - e << when << channel << message << data; + QCopEnvelope e( "QPE/System", "addAlarm(QDateTime,QCString,QCString,int)" ); + e << when << channel << message << data; #endif - } + + } } @@ -332,3 +383,3 @@ void AlarmServer::addAlarm ( QDateTime when, const QCString& channel, \a message, and \a data. - + Passing null values for \a when, \a channel, or for the \link @@ -336,7 +387,7 @@ void AlarmServer::addAlarm ( QDateTime when, const QCString& channel, Similarly, passing -1 for \a data indicates "any". - + If there is no matching alarm, nothing happens. - + \sa addAlarm() - + */ @@ -344,38 +395,40 @@ void AlarmServer::deleteAlarm (QDateTime when, const QCString& channel, const QC { - if ( qApp->type() == QApplication::GuiServer) { - bool needSave = FALSE; - if ( timerEventReceiver != NULL ) { - timerEventReceiver->killTimers(); - - // iterate over the list of events - QListIterator<timerEventItem> it( timerEventList ); - time_t deleteTime = TimeConversion::toUTC( when ); - for ( ; *it; ++it ) { - // if its a match, delete it - if ( ( (*it)->UTCtime == deleteTime || when.isNull() ) - && ( channel.isNull() || (*it)->channel == channel ) - && ( message.isNull() || (*it)->message == message ) - && ( data==-1 || (*it)->data == data ) ) - { - // if it's first, then we need to update the timer - if ( (*it) == nearestTimerEvent ) { - timerEventList.remove(*it); - setNearestTimerEvent(); - } else { - timerEventList.remove(*it); - } - needSave = TRUE; + if ( qApp->type() == QApplication::GuiServer) { + bool needSave = FALSE; + if ( timerEventReceiver != NULL ) { + timerEventReceiver->killTimers(); + + // iterate over the list of events + QListIterator<timerEventItem> it( timerEventList ); + time_t deleteTime = TimeConversion::toUTC( when ); + for ( ; *it; ++it ) { + // if its a match, delete it + if ( ( (*it)->UTCtime == deleteTime || when.isNull() ) + && ( channel.isNull() || (*it)->channel == channel ) + && ( message.isNull() || (*it)->message == message ) + && ( data == -1 || (*it)->data == data ) ) { + // if it's first, then we need to update the timer + if ( (*it) == nearestTimerEvent ) { + timerEventList.remove(*it); + setNearestTimerEvent(); + } + else { + timerEventList.remove(*it); + } + needSave = TRUE; + } + } + if ( nearestTimerEvent ) + timerEventReceiver->resetTimer(); } - } - if ( nearestTimerEvent ) - timerEventReceiver->resetTimer(); + if ( needSave ) + saveState(); } - if ( needSave ) - saveState(); - } else { + else { #ifndef QT_NO_COP - QCopEnvelope e( "QPE/System", "deleteAlarm(QDateTime,QCString,QCString,int)" ); - e << when << channel << message << data; + QCopEnvelope e( "QPE/System", "deleteAlarm(QDateTime,QCString,QCString,int)" ); + e << when << channel << message << data; #endif - } + + } } @@ -387,6 +440,10 @@ void Global::writeHWClock() { - if ( !triggerAtd( TRUE ) ) { - // atd not running? set it ourselves - system("/sbin/hwclock --systohc"); // ##### UTC? - } +#ifdef USE_ATD + if ( !triggerAtd( TRUE ) ) { + // atd not running? set it ourselves + system("/sbin/hwclock --systohc"); // ##### UTC? + } +#else + // hwclock is written on suspend +#endif } |