forked from henrypp/simplewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
196 lines (149 loc) · 3.61 KB
/
timer.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
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
// simplewall
// Copyright (c) 2016-2023 Henry++
#include "global.h"
BOOLEAN _app_istimersactive ()
{
PITEM_APP ptr_app;
SIZE_T enum_key;
enum_key = 0;
_r_queuedlock_acquireshared (&lock_apps);
while (_r_obj_enumhashtablepointer (apps_table, &ptr_app, NULL, &enum_key))
{
if (_app_istimerset (ptr_app))
{
_r_queuedlock_releaseshared (&lock_apps);
return TRUE;
}
}
_r_queuedlock_releaseshared (&lock_apps);
return FALSE;
}
BOOLEAN _app_istimerset (
_In_ PITEM_APP ptr_app
)
{
if (!ptr_app->htimer)
return FALSE;
return !!IsThreadpoolTimerSet (ptr_app->htimer);
}
VOID _app_timer_set (
_In_opt_ HWND hwnd,
_Inout_ PITEM_APP ptr_app,
_In_ LONG64 seconds
)
{
PTP_TIMER htimer;
FILETIME file_time;
LONG64 current_time;
BOOLEAN is_created;
if (seconds <= 0)
{
_app_timer_reset (NULL, ptr_app);
}
else
{
current_time = _r_unixtime_now ();
is_created = FALSE;
_r_unixtime_to_filetime (current_time + seconds, &file_time);
if (ptr_app->htimer)
{
SetThreadpoolTimer (ptr_app->htimer, &file_time, 0, 0);
is_created = TRUE;
}
else
{
htimer = CreateThreadpoolTimer (&_app_timer_callback, (PVOID)ptr_app->app_hash, NULL);
if (htimer)
{
SetThreadpoolTimer (htimer, &file_time, 0, 0);
ptr_app->htimer = htimer;
is_created = TRUE;
}
}
if (is_created)
{
ptr_app->is_enabled = TRUE;
ptr_app->timer = current_time + seconds;
}
else
{
_app_timer_reset (NULL, ptr_app);
}
}
if (hwnd)
_app_listview_updateitemby_param (hwnd, ptr_app->app_hash, TRUE);
}
VOID _app_timer_reset (
_In_opt_ HWND hwnd,
_Inout_ PITEM_APP ptr_app
)
{
ptr_app->is_enabled = FALSE;
ptr_app->is_haveerrors = FALSE;
ptr_app->timer = 0;
if (_app_istimerset (ptr_app))
_app_timer_remove (ptr_app);
if (hwnd)
_app_listview_updateitemby_param (hwnd, ptr_app->app_hash, TRUE);
}
VOID _app_timer_remove (
_Inout_ PITEM_APP ptr_app
)
{
PTP_TIMER current_timer;
current_timer = ptr_app->htimer;
ptr_app->htimer = NULL;
if (current_timer)
CloseThreadpoolTimer (current_timer);
}
VOID CALLBACK _app_timer_callback (
_Inout_ PTP_CALLBACK_INSTANCE instance,
_Inout_opt_ PVOID context,
_Inout_ PTP_TIMER timer
)
{
HANDLE hengine;
HWND hwnd;
PITEM_APP ptr_app;
PR_LIST rules;
PR_STRING string;
WCHAR buffer[256];
ULONG icon_id;
HRESULT hr;
ptr_app = _app_getappitem ((ULONG_PTR)context);
if (!ptr_app)
return;
hr = CoInitializeEx (NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
hwnd = _r_app_gethwnd ();
_app_timer_reset (hwnd, ptr_app);
hengine = _wfp_getenginehandle ();
if (hengine)
{
rules = _r_obj_createlist (NULL);
_r_obj_addlistitem (rules, ptr_app);
_wfp_create3filters (hengine, rules, DBG_ARG, FALSE);
_r_obj_dereference (rules);
}
_app_listview_updateby_id (hwnd, ptr_app->type, PR_UPDATE_TYPE | PR_UPDATE_FORCE);
_r_obj_dereference (ptr_app);
_app_profile_save ();
if (_r_config_getboolean (L"IsNotificationsTimer", TRUE))
{
icon_id = NIIF_INFO;
if (!_r_config_getboolean (L"IsNotificationsSound", TRUE))
icon_id |= NIIF_NOSOUND;
string = _app_getappdisplayname (ptr_app, TRUE);
_r_str_printf (
buffer,
RTL_NUMBER_OF (buffer),
L"%s - %s",
_r_app_getname (),
_r_obj_getstringorempty (string)
);
_r_tray_popup (hwnd, &GUID_TrayIcon, icon_id, buffer, _r_locale_getstring (IDS_STATUS_TIMER_DONE));
if (string)
_r_obj_dereference (string);
}
if (hr == S_OK || hr == S_FALSE)
CoUninitialize ();
}