-rw-r--r-- | library/alarmserver.cpp | 91 |
1 files changed, 74 insertions, 17 deletions
diff --git a/library/alarmserver.cpp b/library/alarmserver.cpp index 5e4dd18..2ea4025 100644 --- a/library/alarmserver.cpp +++ b/library/alarmserver.cpp @@ -38,7 +38,12 @@ #include <stdlib.h> #include <unistd.h> -struct timerEventItem { + +#undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume + + +struct timerEventItem +{ time_t UTCtime; QCString channel, message; int data; @@ -54,15 +59,20 @@ struct timerEventItem { class TimerReceiverObject : public QObject { public: - TimerReceiverObject() { } - ~TimerReceiverObject() { } + TimerReceiverObject() + { } + ~TimerReceiverObject() + { } void resetTimer(); void setTimerEventItem(); void deleteTimer(); protected: void timerEvent( QTimerEvent *te ); + +#ifdef USE_ATD private: QString atfilename; +#endif }; TimerReceiverObject *timerEventReceiver = NULL; @@ -115,7 +125,8 @@ static void saveState() savefile.close(); unlink( savefilename ); - QDir d; d.rename(savefilename+".new",savefilename); + QDir d; + d.rename(savefilename + ".new", savefilename); } } @@ -148,7 +159,7 @@ void AlarmServer::initialize() } - +#ifdef USE_ATD static const char* atdir = "/var/spool/at/"; @@ -160,7 +171,8 @@ static bool triggerAtd( bool writeHWClock = FALSE ) 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() ); + QFile::remove + ( trigger.name() ); return FALSE; } return TRUE; @@ -168,19 +180,40 @@ static bool triggerAtd( bool writeHWClock = FALSE ) 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 ); + } + else + qWarning ( "Failed to write wakeup time to /var/run/resumeat" ); + + return ( fp ); +} + +#endif + void TimerReceiverObject::deleteTimer() { +#ifdef USE_ATD if ( !atfilename.isEmpty() ) { unlink( atfilename ); atfilename = QString::null; triggerAtd( FALSE ); } +#else + writeResumeAt ( 0 ); +#endif } void TimerReceiverObject::resetTimer() { const int maxsecs = 2147000; - int total_written; QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); QDateTime now = QDateTime::currentDateTime(); if ( nearest < now ) @@ -196,11 +229,16 @@ void TimerReceiverObject::resetTimer() // 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()); @@ -209,19 +247,27 @@ void TimerReceiverObject::resetTimer() tr("Unable to schedule alarm.\n" "Please free up space and try again") ); atfile.close(); - QFile::remove( atfile.name() ); + QFile::remove + ( atfile.name() ); return; } atfile.close(); unlink( atfilename ); - QDir d; d.rename(fn+".new",fn); + QDir d; + d.rename(fn + ".new", fn); chmod(fn.latin1(),0755); atfilename = fn; triggerAtd( FALSE ); - } else { + } + else { qWarning("Cannot open atd file %s",fn.latin1()); } } +#else + writeResumeAt ( at_secs ); + +#endif + // Qt timers (does the actual alarm) // from now in milliseconds // @@ -242,11 +288,13 @@ void TimerReceiverObject::timerEvent( QTimerEvent * ) e << TimeConversion::fromUTC( nearestTimerEvent->UTCtime ) << nearestTimerEvent->data; #endif + timerEventList.remove( nearestTimerEvent ); needSave = TRUE; } setNearestTimerEvent(); - } else { + } + else { resetTimer(); } if ( needSave ) @@ -313,17 +361,20 @@ void AlarmServer::addAlarm ( QDateTime when, const QCString& channel, timerEventReceiver->killTimers(); timerEventReceiver->resetTimer(); } - } else { + } + else { nearestTimerEvent = newTimerEventItem; timerEventReceiver->resetTimer(); } if ( needSave ) saveState(); - } else { + } + else { #ifndef QT_NO_COP QCopEnvelope e( "QPE/System", "addAlarm(QDateTime,QCString,QCString,int)" ); e << when << channel << message << data; #endif + } } @@ -355,13 +406,13 @@ void AlarmServer::deleteAlarm (QDateTime when, const QCString& channel, const QC if ( ( (*it)->UTCtime == deleteTime || when.isNull() ) && ( channel.isNull() || (*it)->channel == channel ) && ( message.isNull() || (*it)->message == message ) - && ( data==-1 || (*it)->data == data ) ) - { + && ( data == -1 || (*it)->data == data ) ) { // if it's first, then we need to update the timer if ( (*it) == nearestTimerEvent ) { timerEventList.remove(*it); setNearestTimerEvent(); - } else { + } + else { timerEventList.remove(*it); } needSave = TRUE; @@ -372,11 +423,13 @@ void AlarmServer::deleteAlarm (QDateTime when, const QCString& channel, const QC } if ( needSave ) saveState(); - } else { + } + else { #ifndef QT_NO_COP QCopEnvelope e( "QPE/System", "deleteAlarm(QDateTime,QCString,QCString,int)" ); e << when << channel << message << data; #endif + } } @@ -385,8 +438,12 @@ void AlarmServer::deleteAlarm (QDateTime when, const QCString& channel, const QC */ void Global::writeHWClock() { +#ifdef USE_ATD if ( !triggerAtd( TRUE ) ) { // atd not running? set it ourselves system("/sbin/hwclock --systohc"); // ##### UTC? } +#else + // hwclock is written on suspend +#endif } |