00001 #include <unistd.h> 00002 #include <QCoreApplication> 00003 #include "mythmainwindow.h" 00004 #include "mythevent.h" 00005 #include "mythuistatetracker.h" 00006 #include "mythuiactions.h" 00007 00008 MythUIStateTracker* MythUIStateTracker::gUIState = NULL; 00009 QMutex* MythUIStateTracker::gUIStateLock = new QMutex(); 00010 00011 MythUIStateTracker* MythUIStateTracker::GetMythUIStateTracker(void) 00012 { 00013 gUIStateLock->lock(); 00014 if (!gUIState) 00015 gUIState = new MythUIStateTracker(); 00016 gUIStateLock->unlock(); 00017 return gUIState; 00018 } 00019 00020 void MythUIStateTracker::SetState(QVariantMap &newstate) 00021 { 00022 MythUIStateTracker* uistate = MythUIStateTracker::GetMythUIStateTracker(); 00023 gUIStateLock->lock(); 00024 uistate->m_state = newstate; 00025 uistate->m_state.detach(); 00026 uistate->m_lastUpdated = QTime::currentTime(); 00027 gUIStateLock->unlock(); 00028 } 00029 00030 void MythUIStateTracker::GetState(QVariantMap &state) 00031 { 00032 MythUIStateTracker* uistate = MythUIStateTracker::GetMythUIStateTracker(); 00033 gUIStateLock->lock(); 00034 state = uistate->m_state; 00035 state.detach(); 00036 gUIStateLock->unlock(); 00037 } 00038 00039 void MythUIStateTracker::GetFreshState(QVariantMap &state) 00040 { 00041 if (MythUIStateTracker::TimeSinceLastUpdate() < 500) 00042 { 00043 MythUIStateTracker::GetState(state); 00044 return; 00045 } 00046 00047 MythEvent *e = new MythEvent(ACTION_GETSTATUS); 00048 qApp->postEvent(GetMythMainWindow(), e); 00049 00050 int tries = 0; 00051 while ((tries++ < 100) && (MythUIStateTracker::TimeSinceLastUpdate() >= 500)) 00052 usleep(10000); 00053 00054 MythUIStateTracker::GetState(state); 00055 } 00056 00057 int MythUIStateTracker::TimeSinceLastUpdate(void) 00058 { 00059 MythUIStateTracker* state = MythUIStateTracker::GetMythUIStateTracker(); 00060 gUIStateLock->lock(); 00061 int age = state->m_lastUpdated.msecsTo(QTime::currentTime()); 00062 gUIStateLock->unlock(); 00063 return age < 0 ? 1000000 : age; 00064 } 00065
1.6.3