summaryrefslogtreecommitdiff
path: root/plugins/StatusManager/src/AdvancedAutoAway/advancedautoaway.cpp
blob: 3f638f22dbb551626e7a52a3767b2077c7b83a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
	 AdvancedAutoAway Plugin for Miranda-IM (www.miranda-im.org)
	 Copyright 2003-2006 P. Boon

	 This program is free software; you can redistribute it and/or modify
	 it under the terms of the GNU General Public License as published by
	 the Free Software Foundation; either version 2 of the License, or
	 (at your option) any later version.

	 This program is distributed in the hope that it will be useful,
	 but WITHOUT ANY WARRANTY; without even the implied warranty of
	 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 GNU General Public License for more details.

	 You should have received a copy of the GNU General Public License
	 along with this program; if not, write to the Free Software
	 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


	Some code is copied from Miranda's AutoAway module
*/

#include "..\stdafx.h"

#ifdef _DEBUG
#define SECS_PER_MINUTE		20 /* speedup */
#else
#define SECS_PER_MINUTE		60 /* default I believe */
#endif

/////////////////////////////////////////////////////////////////////////////////////////

int AAALangPack;

static HANDLE hEvents[3];

static BOOL ignoreLockKeys = FALSE;
static BOOL ignoreSysKeys = FALSE;
static BOOL ignoreAltCombo = FALSE;
static BOOL monitorMouse = TRUE;
static BOOL monitorKeyboard = TRUE;
static HWND confirmDialog;
static int mouseStationaryTimer;
HHOOK hMirandaMouseHook = nullptr;
HHOOK hMirandaKeyBoardHook = nullptr;
#pragma data_seg("Shared")
DWORD lastInput = 0;
POINT lastMousePos = { 0 };
HHOOK hMouseHook = nullptr;
HHOOK hKeyBoardHook = nullptr;
#pragma data_seg()
#pragma comment(linker, "/section:Shared,rws")
DWORD lastMirandaInput = 0;
static UINT_PTR hAutoAwayTimer;
// prototypes
extern DWORD StatusModeToProtoFlag(int status);
static int HookWindowsHooks(int hookMiranda, int hookAll);
static int UnhookWindowsHooks();
static LRESULT CALLBACK MouseHookFunction(int code, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK KeyBoardHookFunction(int code, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK MirandaMouseHookFunction(int code, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK MirandaKeyBoardHookFunction(int code, WPARAM wParam, LPARAM lParam);
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);

static VOID CALLBACK AutoAwayTimer(HWND hwnd, UINT message, UINT_PTR idEvent, DWORD dwTime);
extern int AutoAwayOptInitialise(WPARAM wParam, LPARAM lParam);
extern char *StatusModeToDbSetting(int status, const char *suffix);

/////////////////////////////////////////////////////////////////////////////////////////
// Load from DB

void AAAUnloadOptions()
{
	UnhookWindowsHooks();
	if (hAutoAwayTimer != 0)
		KillTimer(nullptr, hAutoAwayTimer);
}

void AAALoadOptions()
{
	// if bOverride is enabled, samesettings will be ignored (for options loading)
	int monitorMiranda = FALSE; // use windows hooks?
	int monitorAll = FALSE; // use windows hooks?

	AAAUnloadOptions();

	ignoreLockKeys = db_get_b(0, AAAMODULENAME, SETTING_IGNLOCK, FALSE);
	ignoreSysKeys = db_get_b(0, AAAMODULENAME, SETTING_IGNSYSKEYS, FALSE);
	ignoreAltCombo = db_get_b(0, AAAMODULENAME, SETTING_IGNALTCOMBO, FALSE);
	monitorMouse = db_get_b(0, AAAMODULENAME, SETTING_MONITORMOUSE, TRUE);
	monitorKeyboard = db_get_b(0, AAAMODULENAME, SETTING_MONITORKEYBOARD, TRUE);
	lastInput = lastMirandaInput = GetTickCount();

	for (auto &it : protoList) {
		char* protoName;
		if ((db_get_b(0, AAAMODULENAME, SETTING_SAMESETTINGS, 0)))
			protoName = SETTING_ALL;
		else
			protoName = it->m_szName;
		LoadAutoAwaySetting(*it, protoName);

		if (it->optionFlags & FLAG_MONITORMIRANDA)
			monitorMiranda = TRUE;
		else if (ignoreLockKeys || ignoreSysKeys || ignoreAltCombo || (monitorMouse != monitorKeyboard))
			monitorAll = TRUE;
	}

	if (db_get_b(0, "Idle", "AAEnable", 0))
		return;

	HookWindowsHooks(monitorMiranda, monitorAll);
	hAutoAwayTimer = SetTimer(nullptr, 0, db_get_w(0, AAAMODULENAME, SETTING_AWAYCHECKTIMEINSECS, 5) * 1000, AutoAwayTimer);
}

int LoadAutoAwaySetting(SMProto &autoAwaySetting, char* protoName)
{
	char setting[128];
	mir_snprintf(setting, "%s_OptionFlags", protoName);
	autoAwaySetting.optionFlags = db_get_w(0, AAAMODULENAME, setting, FLAG_LV2ONINACTIVE | FLAG_RESET);
	mir_snprintf(setting, "%s_AwayTime", protoName);
	autoAwaySetting.awayTime = db_get_w(0, AAAMODULENAME, setting, SETTING_AWAYTIME_DEFAULT);
	mir_snprintf(setting, "%s_NATime", protoName);
	autoAwaySetting.naTime = db_get_w(0, AAAMODULENAME, setting, SETTING_NATIME_DEFAULT);
	mir_snprintf(setting, "%s_StatusFlags", protoName);
	autoAwaySetting.statusFlags = db_get_w(0, AAAMODULENAME, setting, StatusModeToProtoFlag(ID_STATUS_ONLINE) | StatusModeToProtoFlag(ID_STATUS_FREECHAT));

	int flags;
	if (db_get_b(0, AAAMODULENAME, SETTING_SAMESETTINGS, 0))
		flags = 0xFFFFFF;
	else
		flags = CallProtoService(protoName, PS_GETCAPS, PFLAGNUM_2, 0)&~CallProtoService(protoName, PS_GETCAPS, (WPARAM)PFLAGNUM_5, 0);
	mir_snprintf(setting, "%s_Lv1Status", protoName);
	autoAwaySetting.lv1Status = db_get_w(0, AAAMODULENAME, setting, (flags&StatusModeToProtoFlag(ID_STATUS_AWAY)) ? ID_STATUS_AWAY : ID_STATUS_OFFLINE);
	mir_snprintf(setting, "%s_Lv2Status", protoName);
	autoAwaySetting.lv2Status = db_get_w(0, AAAMODULENAME, setting, (flags&StatusModeToProtoFlag(ID_STATUS_NA)) ? ID_STATUS_NA : ID_STATUS_OFFLINE);

	return 0;
}

static int ProcessProtoAck(WPARAM, LPARAM lParam)
{
	ACKDATA *ack = (ACKDATA*)lParam;
	if (ack->type != ACKTYPE_STATUS || ack->result != ACKRESULT_SUCCESS)
		return 0;

	log_debugA("ProcessProtoAck: ack->szModule: %s", ack->szModule);
	for (int i = 0; i < protoList.getCount(); i++) {
		SMProto &p = protoList[i];
		log_debugA("chk: %s", p.m_szName);
		if (!mir_strcmp(p.m_szName, ack->szModule)) {
			log_debugA("ack->szModule: %s p.statusChanged: %d", ack->szModule, p.statusChanged);
			if (!p.statusChanged)
				p.mStatus = TRUE;

			p.statusChanged = FALSE;
		}
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Account control event

static char* status2descr(int status)
{
	switch (status) {
	case ACTIVE:        return "ACTIVE";
	case STATUS1_SET:   return "STATUS1_SET";
	case STATUS2_SET:   return "STATUS2_SET";
	case SET_ORGSTATUS: return "SET_ORGSTATUS";
	case HIDDEN_ACTIVE: return "HIDDEN_ACTIVE";
	}
	return "ERROR";
}

static int changeState(SMProto &setting, STATES newState)
{
	if (setting.curState == newState)
		return 0;

	setting.oldState = setting.curState;
	setting.curState = newState;

	log_debugA("%s state change: %s -> %s", setting.m_szName, status2descr(setting.oldState), status2descr(setting.curState));

	if (setting.curState != SET_ORGSTATUS && setting.curState != ACTIVE && setting.statusChanged) {
		/* change the awaymessage */
		if (setting.m_szMsg != nullptr) {
			mir_free(setting.m_szMsg);
			setting.m_szMsg = nullptr;
		}

		if (db_get_b(0, AAAMODULENAME, StatusModeToDbSetting(setting.m_status, SETTING_MSGCUSTOM), FALSE))
			setting.m_szMsg = db_get_wsa(0, AAAMODULENAME, StatusModeToDbSetting(setting.m_status, SETTING_STATUSMSG));
	}
	else if (setting.m_szMsg != nullptr) {
		mir_free(setting.m_szMsg);
		setting.m_szMsg = nullptr;
	}

	return 0;
}

static VOID CALLBACK AutoAwayTimer(HWND, UINT, UINT_PTR, DWORD)
{
	int statusChanged = FALSE;
	int confirm = FALSE;

	for (auto &it : protoList) {
		it->m_status = ID_STATUS_DISABLED;

		BOOL bTrigger = false;

		if (it->optionFlags & FLAG_MONITORMIRANDA)
			mouseStationaryTimer = (GetTickCount() - lastMirandaInput) / 1000;
		else {
			LASTINPUTINFO ii = { sizeof(ii) };
			GetLastInputInfo(&ii);
			mouseStationaryTimer = (GetTickCount() - ii.dwTime) / 1000;
		}

		int sts1Time = it->awayTime * SECS_PER_MINUTE;
		int sts2Time = it->naTime * SECS_PER_MINUTE;
		int sts1setTime = it->sts1setTimer == 0 ? 0 : (GetTickCount() - it->sts1setTimer) / 1000;
		int currentMode = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);

		if (it->optionFlags & FLAG_ONSAVER)
			bTrigger |= IsScreenSaverRunning();
		if (it->optionFlags & FLAG_ONLOCK)
			bTrigger |= IsWorkstationLocked();
		if (it->optionFlags & FLAG_FULLSCREEN)
			bTrigger |= IsFullScreen();

		/* check states */
		if (it->curState == ACTIVE) {
			if (((mouseStationaryTimer >= sts1Time && (it->optionFlags & FLAG_ONMOUSE)) || bTrigger) && currentMode != it->lv1Status && it->statusFlags&StatusModeToProtoFlag(currentMode)) {
				/* from ACTIVE to STATUS1_SET */
				it->m_lastStatus = it->originalStatusMode = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
				it->m_status = it->lv1Status;
				it->sts1setTimer = GetTickCount();
				sts1setTime = 0;
				it->statusChanged = statusChanged = TRUE;
				changeState(*it, STATUS1_SET);
			}
			else if (mouseStationaryTimer >= sts2Time && currentMode == it->lv1Status && currentMode != it->lv2Status && (it->optionFlags & FLAG_SETNA) && (it->statusFlags & StatusModeToProtoFlag(currentMode))) {
				/* from ACTIVE to STATUS2_SET */
				it->m_lastStatus = it->originalStatusMode = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
				it->m_status = it->lv2Status;
				it->statusChanged = statusChanged = TRUE;
				changeState(*it, STATUS2_SET);
			}
		}

		if (it->curState == STATUS1_SET) {
			if ((mouseStationaryTimer < sts1Time && !bTrigger) && !(it->optionFlags & FLAG_RESET)) {
				/* from STATUS1_SET to HIDDEN_ACTIVE */
				changeState(*it, HIDDEN_ACTIVE);
				it->m_lastStatus = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
			}
			else if (((mouseStationaryTimer < sts1Time) && !bTrigger) &&
				((it->optionFlags & FLAG_LV2ONINACTIVE) || (!(it->optionFlags&FLAG_SETNA))) &&
				(it->optionFlags & FLAG_RESET)) {
				/* from STATUS1_SET to SET_ORGSTATUS */
				changeState(*it, SET_ORGSTATUS);
			}
			else if ((it->optionFlags & FLAG_SETNA) && sts1setTime >= sts2Time) {
				/* when set STATUS2, currentMode doesn't have to be in the selected status list (statusFlags) */
				/* from STATUS1_SET to STATUS2_SET */
				it->m_lastStatus = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
				it->m_status = it->lv2Status;
				it->statusChanged = statusChanged = TRUE;
				changeState(*it, STATUS2_SET);
			}
		}

		if (it->curState == STATUS2_SET) {
			if (mouseStationaryTimer < sts2Time && !bTrigger && (it->optionFlags & FLAG_RESET)) {
				/* from STATUS2_SET to SET_ORGSTATUS */
				changeState(*it, SET_ORGSTATUS);
			}
			else if (mouseStationaryTimer < sts2Time && !bTrigger && !(it->optionFlags & FLAG_RESET)) {
				/* from STATUS2_SET to HIDDEN_ACTIVE */
				/* Remember: after status1 is set, and "only on inactive" is NOT set, it implies !reset. */
				changeState(*it, HIDDEN_ACTIVE);
				it->m_lastStatus = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
			}
		}

		if (it->curState == HIDDEN_ACTIVE) {
			if (it->mStatus) {
				/* HIDDEN_ACTIVE to ACTIVE */
				//it->statusChanged = FALSE;
				changeState(*it, ACTIVE);
				it->sts1setTimer = 0;
				it->mStatus = FALSE;
			}
			else if ((it->optionFlags & FLAG_SETNA) && currentMode == it->lv1Status &&
				currentMode != it->lv2Status && (it->statusFlags & StatusModeToProtoFlag(currentMode)) &&
				(mouseStationaryTimer >= sts2Time || (sts1setTime >= sts2Time && !(it->optionFlags & FLAG_LV2ONINACTIVE)))) {
				/* HIDDEN_ACTIVE to STATUS2_SET */
				it->m_lastStatus = it->originalStatusMode = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
				it->m_status = it->lv2Status;
				it->statusChanged = statusChanged = TRUE;
				changeState(*it, STATUS2_SET);
			}
		}
		if (it->curState == SET_ORGSTATUS) {
			/* SET_ORGSTATUS to ACTIVE */
			it->m_lastStatus = CallProtoService(it->m_szName, PS_GETSTATUS, 0, 0);
			it->m_status = it->originalStatusMode;
			confirm = (it->optionFlags & FLAG_CONFIRM) ? TRUE : confirm;
			it->statusChanged = statusChanged = TRUE;
			changeState(*it, ACTIVE);
			it->sts1setTimer = 0;
		}
		it->mStatus = FALSE;
	}

	if (confirm || statusChanged) {
		TProtoSettings ps = protoList;
		for (auto &it : ps)
			if (it->m_status == ID_STATUS_DISABLED)
				it->m_szName = "";

		if (confirm)
			confirmDialog = ShowConfirmDialogEx(&ps, db_get_w(0, AAAMODULENAME, SETTING_CONFIRMDELAY, 5));
		else if (statusChanged)
			CallService(MS_CS_SETSTATUSEX, (WPARAM)&ps, 0);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// Windows hooks 

static int HookWindowsHooks(int hookMiranda, int hookAll)
{
	if (hookMiranda) {
		if (monitorKeyboard && hMirandaKeyBoardHook == nullptr)
			hMirandaKeyBoardHook = SetWindowsHookEx(WH_KEYBOARD, MirandaKeyBoardHookFunction, nullptr, GetCurrentThreadId());
		if (monitorMouse && hMirandaMouseHook == nullptr)
			hMirandaMouseHook = SetWindowsHookEx(WH_MOUSE, MirandaMouseHookFunction, nullptr, GetCurrentThreadId());
	}
	if (hookAll) {
		if (monitorKeyboard && hKeyBoardHook == nullptr)
			hKeyBoardHook = SetWindowsHookEx(WH_KEYBOARD, KeyBoardHookFunction, nullptr, GetCurrentThreadId());
		if (monitorMouse && hMouseHook == nullptr)
			hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseHookFunction, nullptr, GetCurrentThreadId());
	}

	return 0;
}

static int UnhookWindowsHooks()
{
	UnhookWindowsHookEx(hMouseHook);
	UnhookWindowsHookEx(hKeyBoardHook);
	UnhookWindowsHookEx(hMirandaMouseHook);
	UnhookWindowsHookEx(hMirandaKeyBoardHook);

	hMouseHook = hKeyBoardHook = hMirandaMouseHook = hMirandaKeyBoardHook = nullptr;
	return 0;
}

static LRESULT CALLBACK MirandaMouseHookFunction(int code, WPARAM wParam, LPARAM lParam)
{
	if (code >= 0) {
		PMOUSEHOOKSTRUCT mouseInfo = (PMOUSEHOOKSTRUCT)lParam;
		POINT pt = mouseInfo->pt;

		/* TioDuke's KeyBoardNotifyExt: only update if a Miranda window is focused */
		DWORD pid;
		GetWindowThreadProcessId(GetForegroundWindow(), &pid);
		if (pid != GetCurrentProcessId())
			return CallNextHookEx(hMirandaMouseHook, code, wParam, lParam);

		if (pt.x != lastMousePos.x || pt.y != lastMousePos.y) {
			lastMousePos = pt;
			lastMirandaInput = GetTickCount();
		}
	}

	return CallNextHookEx(hMirandaMouseHook, code, wParam, lParam);
}

static LRESULT CALLBACK MirandaKeyBoardHookFunction(int code, WPARAM wParam, LPARAM lParam)
{
	if (code >= 0) {
		if (ignoreAltCombo) {
			if (((GetKeyState(VK_MENU) < 0) || (wParam == VK_MENU)) ||
				((GetKeyState(VK_TAB) < 0) || (wParam == VK_TAB)) ||
				((GetKeyState(VK_SHIFT) < 0) || (wParam == VK_SHIFT)) ||
				((GetKeyState(VK_CONTROL) < 0) || (wParam == VK_CONTROL)) ||
				((GetKeyState(VK_ESCAPE) < 0) || (wParam == VK_ESCAPE)) ||
				((GetKeyState(VK_LWIN) < 0) || (wParam == VK_LWIN)) ||
				((GetKeyState(VK_RWIN) < 0) || (wParam == VK_RWIN))) {
				return CallNextHookEx(hMirandaKeyBoardHook, code, wParam, lParam);
			}
		}

		switch (wParam) {
		case VK_NUMLOCK:
		case VK_CAPITAL:
		case VK_SCROLL:
			if (!ignoreLockKeys)
				lastMirandaInput = GetTickCount();
			break;

		case VK_TAB:
		case VK_SHIFT:
		case VK_CONTROL:
		case VK_MENU:
		case VK_ESCAPE:
		case VK_LWIN:
		case VK_RWIN:
			if (!ignoreSysKeys)
				lastMirandaInput = GetTickCount();
			break;

		default:
			lastMirandaInput = GetTickCount();
			break;
		}
	}

	return CallNextHookEx(hMirandaKeyBoardHook, code, wParam, lParam);
}

static LRESULT CALLBACK MouseHookFunction(int code, WPARAM wParam, LPARAM lParam)
{
	if (code >= 0) {
		PMOUSEHOOKSTRUCT mouseInfo = (PMOUSEHOOKSTRUCT)lParam;
		POINT pt = mouseInfo->pt;

		/* TioDuke's KeyBoardNotifyExt: also grab clicks */
		if ((wParam >= WM_NCLBUTTONDOWN && wParam <= WM_NCXBUTTONDBLCLK && wParam != 0x00AA) || (wParam >= WM_LBUTTONDOWN && wParam <= WM_XBUTTONDBLCLK))
			lastInput = GetTickCount();

		if (pt.x != lastMousePos.x || pt.y != lastMousePos.y) {
			lastMousePos = pt;
			lastInput = GetTickCount();
		}
	}

	return CallNextHookEx(hMouseHook, code, wParam, lParam);
}

static LRESULT CALLBACK KeyBoardHookFunction(int code, WPARAM wParam, LPARAM lParam)
{
	if (code >= 0) {
		if (ignoreAltCombo) {
			if (((GetKeyState(VK_MENU) < 0) || (wParam == VK_MENU)) ||
				((GetKeyState(VK_TAB) < 0) || (wParam == VK_TAB)) ||
				((GetKeyState(VK_SHIFT) < 0) || (wParam == VK_SHIFT)) ||
				((GetKeyState(VK_CONTROL) < 0) || (wParam == VK_CONTROL)) ||
				((GetKeyState(VK_ESCAPE) < 0) || (wParam == VK_ESCAPE)) ||
				((GetKeyState(VK_LWIN) < 0) || (wParam == VK_LWIN)) ||
				((GetKeyState(VK_RWIN) < 0) || (wParam == VK_RWIN))) {
				return CallNextHookEx(hKeyBoardHook, code, wParam, lParam);
			}
		}

		switch (wParam) {
		case VK_NUMLOCK:
		case VK_CAPITAL:
		case VK_SCROLL:
			if (!ignoreLockKeys)
				lastInput = GetTickCount();
			break;

		case VK_TAB:
		case VK_SHIFT:
		case VK_CONTROL:
		case VK_MENU:
		case VK_ESCAPE:
		case VK_LWIN:
		case VK_RWIN:
			if (!ignoreSysKeys)
				lastInput = GetTickCount();
			break;

		default:
			lastInput = GetTickCount();
			break;
		}
	}

	return CallNextHookEx(hKeyBoardHook, code, wParam, lParam);
}

/////////////////////////////////////////////////////////////////////////////////////////
// Inits & stuff

static int AutoAwayShutdown(WPARAM, LPARAM)
{
	AAAUnloadOptions();
	return 0;
}

int AAAModuleLoaded(WPARAM, LPARAM)
{
	hEvents[0] = HookEvent(ME_OPT_INITIALISE, AutoAwayOptInitialise);
	hEvents[1] = HookEvent(ME_SYSTEM_PRESHUTDOWN, AutoAwayShutdown);
	hEvents[2] = HookEvent(ME_PROTO_ACK, ProcessProtoAck);
	
	mouseStationaryTimer = 0;
	lastInput = lastMirandaInput = GetTickCount();

	////////////////////////////////////////////////////////////////////////////////////////

	AAALoadOptions();
	return 0;
}

void AdvancedAutoAwayLoad()
{
	AAALangPack = GetPluginLangId(MIID_LAST, 0);

	if (g_bMirandaLoaded)
		AAAModuleLoaded(0, 0);
	else
		HookEvent(ME_SYSTEM_MODULESLOADED, AAAModuleLoaded);
}

void AdvancedAutoAwayUnload()
{
	if (g_bMirandaLoaded)
		AutoAwayShutdown(0, 0);

	KillModuleOptions(AAALangPack);

	for (auto &it : hEvents) {
		UnhookEvent(it);
		it = nullptr;
	}

	protoList.destroy();
}