summaryrefslogtreecommitdiff
path: root/plugins/AutoShutdown/src/watcher.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2015-08-13 22:13:41 +0000
committerGeorge Hazan <george.hazan@gmail.com>2015-08-13 22:13:41 +0000
commit3ec93b6744328421a82b9fd39d711eeea0d9749a (patch)
treea9c454c592bcbe60090c39320811892d14ce9b87 /plugins/AutoShutdown/src/watcher.cpp
parentfb24858c747011487663c030ed45635bf057bd88 (diff)
- 64-bit compatibility;
- code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@14950 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/AutoShutdown/src/watcher.cpp')
-rw-r--r--plugins/AutoShutdown/src/watcher.cpp164
1 files changed, 82 insertions, 82 deletions
diff --git a/plugins/AutoShutdown/src/watcher.cpp b/plugins/AutoShutdown/src/watcher.cpp
index f8194f3394..32fba6793e 100644
--- a/plugins/AutoShutdown/src/watcher.cpp
+++ b/plugins/AutoShutdown/src/watcher.cpp
@@ -32,7 +32,7 @@ static HANDLE hHookSettingChanged;
/* Weather Shutdown */
static HANDLE hHookWeatherUpdated;
/* Services */
-static HANDLE hServiceStartWatcher,hServiceStopWatcher,hServiceIsEnabled;
+static HANDLE hServiceStartWatcher, hServiceStopWatcher, hServiceIsEnabled;
static HANDLE hEventWatcherChanged;
/* Misc */
static HANDLE hHookModulesLoaded;
@@ -44,18 +44,18 @@ static WORD currentWatcherType;
static void __stdcall MainThreadMapping(void *param)
{
HANDLE *phDoneEvent = (HANDLE*)param;
- ServiceShutdown(0,TRUE); /* ensure main thread (for cpu usage shutdown) */
- ServiceStopWatcher(0,0);
+ ServiceShutdown(0, TRUE); /* ensure main thread (for cpu usage shutdown) */
+ ServiceStopWatcher(0, 0);
if (*phDoneEvent != NULL)
SetEvent(*phDoneEvent);
}
static void __inline ShutdownAndStopWatcher(void)
{
- HANDLE hDoneEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
+ HANDLE hDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
CallFunctionAsync(MainThreadMapping, &hDoneEvent);
if (hDoneEvent != NULL) {
- WaitForSingleObject(hDoneEvent,INFINITE);
+ WaitForSingleObject(hDoneEvent, INFINITE);
CloseHandle(hDoneEvent);
}
}
@@ -63,29 +63,29 @@ static void __inline ShutdownAndStopWatcher(void)
/************************* Msg Shutdown *******************************/
// ppBlob might get reallocated, must have been allocated using mir_alloc()
-static TCHAR* GetMessageText(BYTE **ppBlob,DWORD *pcbBlob)
+static TCHAR* GetMessageText(BYTE **ppBlob, DWORD *pcbBlob)
{
- (*ppBlob)[*pcbBlob]=0;
+ (*ppBlob)[*pcbBlob] = 0;
size_t cb = mir_strlen((char*)*ppBlob);
/* use Unicode data if present */
- if (*pcbBlob>(cb+3)) {
- (*ppBlob)[*pcbBlob-1]=0;
+ if (*pcbBlob > (cb + 3)) {
+ (*ppBlob)[*pcbBlob - 1] = 0;
return (WCHAR*)&(*ppBlob)[cb];
}
/* no Unicode data present, convert from ANSI */
- int len = MultiByteToWideChar(CP_ACP,0,(char*)*ppBlob,-1,NULL,0);
+ int len = MultiByteToWideChar(CP_ACP, 0, (char*)*ppBlob, -1, NULL, 0);
if (!len)
return NULL;
- BYTE *buf=(BYTE*)mir_realloc(*ppBlob,(*pcbBlob)+(len*sizeof(WCHAR)));
+ BYTE *buf = (BYTE*)mir_realloc(*ppBlob, (*pcbBlob) + (len*sizeof(WCHAR)));
if (buf == NULL)
return NULL;
*pcbBlob += len*sizeof(WCHAR);
*ppBlob = buf;
buf = &(*ppBlob)[cb];
- MultiByteToWideChar(CP_ACP,0,(char*)*ppBlob,-1,(WCHAR*)buf,len);
- ((WCHAR*)buf)[len-1] = 0;
+ MultiByteToWideChar(CP_ACP, 0, (char*)*ppBlob, -1, (WCHAR*)buf, len);
+ ((WCHAR*)buf)[len - 1] = 0;
return (WCHAR*)buf;
}
@@ -94,16 +94,16 @@ static int MsgEventAdded(WPARAM, LPARAM hDbEvent)
if (currentWatcherType & SDWTF_MESSAGE) {
DBEVENTINFO dbe = { sizeof(dbe) };
dbe.cbBlob = db_event_getBlobSize(hDbEvent);
- dbe.pBlob = (BYTE*)mir_alloc(dbe.cbBlob+2); /* ensure term zero */
+ dbe.pBlob = (BYTE*)mir_alloc(dbe.cbBlob + 2); /* ensure term zero */
if (dbe.pBlob == NULL)
return 0;
if (!db_event_get(hDbEvent, &dbe))
if (dbe.eventType == EVENTTYPE_MESSAGE && !(dbe.flags & DBEF_SENT)) {
DBVARIANT dbv;
- if (!db_get_ts(NULL,"AutoShutdown","Message",&dbv)) {
+ if (!db_get_ts(NULL, "AutoShutdown", "Message", &dbv)) {
TrimString(dbv.ptszVal);
- TCHAR *pszMsg = GetMessageText(&dbe.pBlob,&dbe.cbBlob);
- if (pszMsg != NULL && _tcsstr(pszMsg,dbv.ptszVal) != NULL)
+ TCHAR *pszMsg = GetMessageText(&dbe.pBlob, &dbe.cbBlob);
+ if (pszMsg != NULL && _tcsstr(pszMsg, dbv.ptszVal) != NULL)
ShutdownAndStopWatcher(); /* msg with specified text recvd */
mir_free(dbv.ptszVal); /* does NULL check */
}
@@ -120,18 +120,18 @@ static int nTransfersCount;
static int ProtoAck(WPARAM, LPARAM lParam)
{
- ACKDATA *ack=(ACKDATA*)lParam;
+ ACKDATA *ack = (ACKDATA*)lParam;
if (ack->type != ACKTYPE_FILE)
return 0;
- switch(ack->result) {
+ switch (ack->result) {
case ACKRESULT_DATA:
{
- for(int i=0; i < nTransfersCount; ++i)
- if (transfers[i]==ack->hProcess)
+ for (int i = 0; i < nTransfersCount; ++i)
+ if (transfers[i] == ack->hProcess)
break; /* already in list */
/* insert into list */
- HANDLE *buf = (HANDLE*)mir_realloc(transfers,(nTransfersCount+1)*sizeof(HANDLE));
+ HANDLE *buf = (HANDLE*)mir_realloc(transfers, (nTransfersCount + 1)*sizeof(HANDLE));
if (buf != NULL) {
transfers = buf;
transfers[nTransfersCount] = ack->hProcess;
@@ -142,15 +142,15 @@ static int ProtoAck(WPARAM, LPARAM lParam)
case ACKRESULT_SUCCESS:
case ACKRESULT_FAILED:
case ACKRESULT_DENIED:
- for(int i=0;i<nTransfersCount;++i) {
- if (transfers[i]==ack->hProcess) {
+ for (int i = 0; i < nTransfersCount; ++i) {
+ if (transfers[i] == ack->hProcess) {
/* remove from list */
- if (i<(nTransfersCount-1))
- memmove(&transfers[i],&transfers[i+1],(nTransfersCount-i-1)*sizeof(HANDLE));
+ if (i < (nTransfersCount - 1))
+ memmove(&transfers[i], &transfers[i + 1], (nTransfersCount - i - 1)*sizeof(HANDLE));
--nTransfersCount;
- HANDLE *buf = (HANDLE*)mir_realloc(transfers,nTransfersCount*sizeof(HANDLE));
- if (buf != NULL) transfers=buf;
- else if (!nTransfersCount) transfers=NULL;
+ HANDLE *buf = (HANDLE*)mir_realloc(transfers, nTransfersCount*sizeof(HANDLE));
+ if (buf != NULL) transfers = buf;
+ else if (!nTransfersCount) transfers = NULL;
/* stop watcher */
if (!nTransfersCount && (currentWatcherType&SDWTF_FILETRANSFER))
ShutdownAndStopWatcher();
@@ -164,7 +164,7 @@ static int ProtoAck(WPARAM, LPARAM lParam)
/************************* Idle Shutdown ******************************/
-static int IdleChanged(WPARAM,LPARAM lParam)
+static int IdleChanged(WPARAM, LPARAM lParam)
{
if (currentWatcherType&SDWTF_IDLE && lParam&IDF_ISIDLE)
ShutdownAndStopWatcher();
@@ -175,18 +175,18 @@ static int IdleChanged(WPARAM,LPARAM lParam)
static BOOL CheckAllContactsOffline(void)
{
- BOOL fSmartCheck,fAllOffline=TRUE; /* tentatively */
- fSmartCheck=db_get_b(NULL,"AutoShutdown","SmartOfflineCheck",SETTING_SMARTOFFLINECHECK_DEFAULT);
+ BOOL fSmartCheck, fAllOffline = TRUE; /* tentatively */
+ fSmartCheck = db_get_b(NULL, "AutoShutdown", "SmartOfflineCheck", SETTING_SMARTOFFLINECHECK_DEFAULT);
for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
char *pszProto = GetContactProto(hContact);
if (pszProto != NULL && CallProtoService(pszProto, PS_GETSTATUS, 0, 0) != ID_STATUS_OFFLINE) {
- if (db_get_b(hContact,pszProto,"ChatRoom",0)) continue;
- if (db_get_w(hContact,pszProto,"Status",0) != ID_STATUS_OFFLINE) {
+ if (db_get_b(hContact, pszProto, "ChatRoom", 0)) continue;
+ if (db_get_w(hContact, pszProto, "Status", 0) != ID_STATUS_OFFLINE) {
if (fSmartCheck) {
- if (db_get_b(hContact,"CList","Hidden",0)) continue;
- if (db_get_b(hContact,"CList","NotOnList",0)) continue;
+ if (db_get_b(hContact, "CList", "Hidden", 0)) continue;
+ if (db_get_b(hContact, "CList", "NotOnList", 0)) continue;
}
- fAllOffline=FALSE;
+ fAllOffline = FALSE;
break;
}
}
@@ -194,13 +194,13 @@ static BOOL CheckAllContactsOffline(void)
return fAllOffline;
}
-static int StatusSettingChanged(WPARAM wParam,LPARAM lParam)
+static int StatusSettingChanged(WPARAM wParam, LPARAM lParam)
{
if (currentWatcherType&SDWTF_STATUS) {
- DBCONTACTWRITESETTING *dbcws=(DBCONTACTWRITESETTING*)lParam;
- if ((HANDLE)wParam != NULL && dbcws->value.wVal==ID_STATUS_OFFLINE && !mir_strcmp(dbcws->szSetting,"Status")) {
+ DBCONTACTWRITESETTING *dbcws = (DBCONTACTWRITESETTING*)lParam;
+ if ((HANDLE)wParam != NULL && dbcws->value.wVal == ID_STATUS_OFFLINE && !mir_strcmp(dbcws->szSetting, "Status")) {
char *pszProto = GetContactProto(wParam);
- if (pszProto != NULL && !mir_strcmp(dbcws->szModule,pszProto))
+ if (pszProto != NULL && !mir_strcmp(dbcws->szModule, pszProto))
if (CheckAllContactsOffline())
ShutdownAndStopWatcher();
}
@@ -212,19 +212,19 @@ static int StatusSettingChanged(WPARAM wParam,LPARAM lParam)
static DWORD idCpuUsageThread;
-static BOOL CALLBACK CpuUsageWatcherProc(BYTE nCpuUsage,LPARAM lParam)
+static BOOL CALLBACK CpuUsageWatcherProc(BYTE nCpuUsage, LPARAM lParam)
{
- static BYTE nTimesBelow=0; /* only one watcher thread */
+ static BYTE nTimesBelow = 0; /* only one watcher thread */
/* terminated? */
if (idCpuUsageThread != GetCurrentThreadId()) {
- nTimesBelow=0;
+ nTimesBelow = 0;
return FALSE; /* stop poll thread */
}
/* ignore random peaks */
- if (nCpuUsage<(BYTE)lParam) ++nTimesBelow;
- else nTimesBelow=0;
- if (nTimesBelow==3) {
- nTimesBelow=0;
+ if (nCpuUsage < (BYTE)lParam) ++nTimesBelow;
+ else nTimesBelow = 0;
+ if (nTimesBelow == 3) {
+ nTimesBelow = 0;
ShutdownAndStopWatcher();
return FALSE; /* stop poll thread */
}
@@ -233,12 +233,12 @@ static BOOL CALLBACK CpuUsageWatcherProc(BYTE nCpuUsage,LPARAM lParam)
/************************* Weather Shutdown ***************************/
-static int WeatherUpdated(WPARAM wParam,LPARAM lParam)
+static int WeatherUpdated(WPARAM wParam, LPARAM lParam)
{
char *pszProto = GetContactProto(wParam);
- if ((BOOL)lParam && pszProto != NULL && CallProtoService(pszProto,PS_GETSTATUS,0,0)==THUNDER)
- if (db_get_b(NULL,"AutoShutdown","WeatherShutdown",SETTING_WEATHERSHUTDOWN_DEFAULT))
- ServiceShutdown(SDSDT_SHUTDOWN,TRUE);
+ if ((BOOL)lParam && pszProto != NULL && CallProtoService(pszProto, PS_GETSTATUS, 0, 0) == THUNDER)
+ if (db_get_b(NULL, "AutoShutdown", "WeatherShutdown", SETTING_WEATHERSHUTDOWN_DEFAULT))
+ ServiceShutdown(SDSDT_SHUTDOWN, TRUE);
return 0;
}
@@ -247,8 +247,8 @@ static int WeatherUpdated(WPARAM wParam,LPARAM lParam)
INT_PTR ServiceStartWatcher(WPARAM, LPARAM lParam)
{
/* passing watcherType as lParam is only to be used internally, undocumented */
- if (lParam==0)
- lParam=(LPARAM)db_get_w(NULL,"AutoShutdown","WatcherFlags",0);
+ if (lParam == 0)
+ lParam = (LPARAM)db_get_w(NULL, "AutoShutdown", "WatcherFlags", 0);
/* invalid flags or empty? */
if (!(lParam&SDWTF_MASK))
@@ -258,26 +258,26 @@ INT_PTR ServiceStartWatcher(WPARAM, LPARAM lParam)
if (lParam&SDWTF_SPECIFICTIME && !(lParam&SDWTF_ST_MASK))
return 2;
- if (currentWatcherType==(WORD)lParam)
+ if (currentWatcherType == (WORD)lParam)
return 3;
if (currentWatcherType != 0) {
/* Time Shutdown */
CloseCountdownFrame(); /* fails if not opened */
/* Cpu Shutdown */
- idCpuUsageThread=0;
+ idCpuUsageThread = 0;
}
SetShutdownMenuItem(true);
SetShutdownToolbarButton(true);
- currentWatcherType=(WORD)lParam;
- NotifyEventHooks(hEventWatcherChanged,TRUE,0);
+ currentWatcherType = (WORD)lParam;
+ NotifyEventHooks(hEventWatcherChanged, TRUE, 0);
/* Time Shutdown */
if (currentWatcherType&SDWTF_SPECIFICTIME)
ShowCountdownFrame(currentWatcherType); /* after modules loaded */
/* Cpu Shutdown */
if (currentWatcherType&SDWTF_CPUUSAGE)
- idCpuUsageThread=PollCpuUsage(CpuUsageWatcherProc,(LPARAM)DBGetContactSettingRangedByte(NULL,"AutoShutdown","CpuUsageThreshold",SETTING_CPUUSAGETHRESHOLD_DEFAULT,1,100),1500);
+ idCpuUsageThread = PollCpuUsage(CpuUsageWatcherProc, (LPARAM)DBGetContactSettingRangedByte(NULL, "AutoShutdown", "CpuUsageThreshold", SETTING_CPUUSAGETHRESHOLD_DEFAULT, 1, 100), 1500);
/* Transfer Shutdown */
if (currentWatcherType&SDWTF_FILETRANSFER && !nTransfersCount)
ShutdownAndStopWatcher();
@@ -287,24 +287,24 @@ INT_PTR ServiceStartWatcher(WPARAM, LPARAM lParam)
return 0;
}
-INT_PTR ServiceStopWatcher(WPARAM,LPARAM)
+INT_PTR ServiceStopWatcher(WPARAM, LPARAM)
{
- if (currentWatcherType==0) return 1;
+ if (currentWatcherType == 0) return 1;
/* Time Shutdown */
if (currentWatcherType&SDWTF_SPECIFICTIME)
CloseCountdownFrame();
/* Cpu Shutdown */
- idCpuUsageThread=0;
+ idCpuUsageThread = 0;
- currentWatcherType=0;
+ currentWatcherType = 0;
SetShutdownMenuItem(FALSE);
SetShutdownToolbarButton(FALSE);
- NotifyEventHooks(hEventWatcherChanged,FALSE,0);
+ NotifyEventHooks(hEventWatcherChanged, FALSE, 0);
return 0;
}
-INT_PTR ServiceIsWatcherEnabled(WPARAM,LPARAM)
+INT_PTR ServiceIsWatcherEnabled(WPARAM, LPARAM)
{
return currentWatcherType != 0;
}
@@ -315,44 +315,44 @@ void WatcherModulesLoaded(void)
{
/* Weather Shutdown */
if (ServiceExists(MS_WEATHER_UPDATE))
- hHookWeatherUpdated=HookEvent(ME_WEATHER_UPDATED,WeatherUpdated);
+ hHookWeatherUpdated = HookEvent(ME_WEATHER_UPDATED, WeatherUpdated);
/* restore watcher if it was running on last exit */
- if (db_get_b(NULL,"AutoShutdown","RememberOnRestart",0)==SDROR_RUNNING) {
- db_set_b(NULL,"AutoShutdown","RememberOnRestart",1);
- ServiceStartWatcher(0,0); /* after modules loaded */
+ if (db_get_b(NULL, "AutoShutdown", "RememberOnRestart", 0) == SDROR_RUNNING) {
+ db_set_b(NULL, "AutoShutdown", "RememberOnRestart", 1);
+ ServiceStartWatcher(0, 0); /* after modules loaded */
}
}
void InitWatcher(void)
{
/* Shared */
- currentWatcherType=0;
+ currentWatcherType = 0;
/* Message Shutdown */
- hHookEventAdded=HookEvent(ME_DB_EVENT_ADDED,MsgEventAdded);
+ hHookEventAdded = HookEvent(ME_DB_EVENT_ADDED, MsgEventAdded);
/* Status Shutdown*/
- hHookSettingChanged=HookEvent(ME_DB_CONTACT_SETTINGCHANGED,StatusSettingChanged);
+ hHookSettingChanged = HookEvent(ME_DB_CONTACT_SETTINGCHANGED, StatusSettingChanged);
/* Idle Shutdown */
- hHookIdleChanged=HookEvent(ME_IDLE_CHANGED,IdleChanged);
+ hHookIdleChanged = HookEvent(ME_IDLE_CHANGED, IdleChanged);
/* Transfer Shutdown */
- transfers=NULL;
- nTransfersCount=0;
- hHookProtoAck=HookEvent(ME_PROTO_ACK,ProtoAck);
+ transfers = NULL;
+ nTransfersCount = 0;
+ hHookProtoAck = HookEvent(ME_PROTO_ACK, ProtoAck);
/* Weather Shutdown */
- hHookWeatherUpdated=NULL;
+ hHookWeatherUpdated = NULL;
/* Services */
- hEventWatcherChanged=CreateHookableEvent(ME_AUTOSHUTDOWN_WATCHERCHANGED);
+ hEventWatcherChanged = CreateHookableEvent(ME_AUTOSHUTDOWN_WATCHERCHANGED);
hServiceStartWatcher = CreateServiceFunction(MS_AUTOSHUTDOWN_STARTWATCHER, ServiceStartWatcher);
- hServiceStopWatcher = CreateServiceFunction(MS_AUTOSHUTDOWN_STOPWATCHER, ServiceStopWatcher);
+ hServiceStopWatcher = CreateServiceFunction(MS_AUTOSHUTDOWN_STOPWATCHER, ServiceStopWatcher);
hServiceIsEnabled = CreateServiceFunction(MS_AUTOSHUTDOWN_ISWATCHERENABLED, ServiceIsWatcherEnabled);
}
void UninitWatcher(void)
{
/* remember watcher if running */
- if (!ServiceStopWatcher(0,0))
- if (db_get_b(NULL,"AutoShutdown","RememberOnRestart",SETTING_REMEMBERONRESTART_DEFAULT))
- db_set_b(NULL,"AutoShutdown","RememberOnRestart",SDROR_RUNNING);
+ if (!ServiceStopWatcher(0, 0))
+ if (db_get_b(NULL, "AutoShutdown", "RememberOnRestart", SETTING_REMEMBERONRESTART_DEFAULT))
+ db_set_b(NULL, "AutoShutdown", "RememberOnRestart", SDROR_RUNNING);
/* Message Shutdown */
UnhookEvent(hHookEventAdded);