-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwkb-proc-inc.c
125 lines (100 loc) · 2.94 KB
/
wkb-proc-inc.c
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
/*
Copyright (C) 2016-2020 Dimitar Toshkov Zhekov <[email protected]>
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
static UINT getTimerInterval(void)
{
SYSTEM_POWER_STATUS powerStat;
UINT interval = 70;
if (GetSystemPowerStatus(&powerStat))
{
if (powerStat.ACLineStatus == 1)
interval = 40;
else if (powerStat.ACLineStatus == 0)
interval = 100;
}
return interval;
}
static UINT_PTR timerId = 0;
static void startTimer(void)
{
checkFunc("SetTimer", (timerId = SetTimer(NULL, 0, getTimerInterval(), NULL)) != 0);
}
static LRESULT CALLBACK wkbWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_POWERBROADCAST)
{
if (wParam == PBT_APMRESUMEAUTOMATIC || wParam == PBT_APMPOWERSTATUSCHANGE)
{
timerDelay = 0;
startTimer();
}
else if (wParam == PBT_APMSUSPEND && timerId)
{
KillTimer(NULL, timerId);
timerId = 0;
}
}
else if (uMsg == WM_CLOSE) // destroy is used in reconnect
PostQuitMessage(0);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
static BOOL standardMainProc(MSG *msg)
{
if (msg->message != WM_TIMER)
{
TranslateMessage(msg);
DispatchMessage(msg);
return TRUE;
}
if (!timerId)
return TRUE;
if (timerDelay > 0)
{
timerDelay--;
return TRUE;
}
return FALSE;
}
static void getDesktopName(HDESK desktop, char *buffer, size_t size)
{
DWORD needed;
if (!GetUserObjectInformation(desktop, UOI_NAME, buffer, size, &needed))
*buffer = '\0';
}
static HWND getFocus(DWORD desktopAccess)
{
HWND focus = GetForegroundWindow();
if (!focus)
{
HDESK desktop = OpenInputDesktop(0, FALSE, desktopAccess);
if (desktop)
{
char inputDesktopName[0x100];
char threadDesktopName[0x100];
getDesktopName(desktop, inputDesktopName, sizeof inputDesktopName);
getDesktopName(GetThreadDesktop(GetCurrentThreadId()), threadDesktopName, sizeof threadDesktopName);
if (*inputDesktopName == '\0' || strcmp(inputDesktopName, threadDesktopName))
{
disconnectHooks();
checkFunc("DestroyWindow", DestroyWindow(wkbWindow));
if (!SetThreadDesktop(desktop))
timerDelay = 5;
connectLock();
}
CloseHandle(desktop);
focus = GetForegroundWindow();
}
}
return focus;
}