/
winproc.cpp
290 lines (232 loc) · 7.54 KB
/
winproc.cpp
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
/*
* Collects SNMP statistics and updates the system tray icons.
*/
#include "StdAfx.h"
#include "NetPerSec.h"
#include "winproc.h"
#include "resource.h"
#include "hlp\helpids.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT TaskbarCallbackMsg = RegisterWindowMessage("NPSTaskbarMsg");
// Cwinproc
Cwinproc::Cwinproc() {
m_pPropertiesDlg = NULL;
ZeroMemory(&m_SystemTray, sizeof(m_SystemTray));
ResetData();
m_dwStartTime = GetTickCount();
}
Cwinproc::~Cwinproc() {}
// Cwinproc
void Cwinproc::OnClose() {
KillTimer(TIMER_ID_WINPROC);
if (m_SystemTray.hWnd != NULL)
Shell_NotifyIcon(NIM_DELETE, &m_SystemTray);
CWnd::OnClose();
}
// Cwinproc
BEGIN_MESSAGE_MAP(Cwinproc, CWnd)
//{{AFX_MSG_MAP(Cwinproc)
ON_WM_TIMER()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(TaskbarCallbackMsg, OnTaskbarNotify)
END_MESSAGE_MAP()
/* Cwinproc message handlers */
// Startup - called when the invisible window is created in netpersec.cpp.
// Initializes SNMP and the system tray icon
void Cwinproc::StartUp() {
if (!snmp.Init())
PostQuitMessage(0);
else {
SetTimer(TIMER_ID_WINPROC, g_nSampleRate, NULL);
// 1.1 init using NIF_ICON
HICON hIcon;
if (g_IconStyle == ICON_HISTOGRAM)
hIcon = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_HISTOGRAM), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
else
hIcon = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_BARGRAPH), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
m_SystemTray.cbSize = sizeof(NOTIFYICONDATA);
m_SystemTray.hWnd = GetSafeHwnd();
m_SystemTray.uID = 1;
m_SystemTray.hIcon = hIcon;
m_SystemTray.uFlags = NIF_MESSAGE | NIF_ICON;
m_SystemTray.uCallbackMessage = TaskbarCallbackMsg;
if (!Shell_NotifyIcon(NIM_ADD, &m_SystemTray))
AfxMessageBox("System tray error.");
}
}
void Cwinproc::CalcAverages(double dbTotal, DWORD dwTime, DWORD dwBps, STATS_STRUCT *pStats) {
ASSERT(g_nAveragingWindow <= MAX_SAMPLES);
// Write first few fields into array head element
pStats[0].Bps = dwBps;
pStats[0].total = dbTotal;
pStats[0].time = dwTime; // Current time interval (ms)
// The index in the array for calculating averages
int start = g_nAveragingWindow;
while (start > 0 && (start >= MAX_SAMPLES || pStats[start].total == 0))
start--;
// Total bytes received/sent in our sampling window
double dbSampleTotal = dbTotal - pStats[start].total;
// Elapsed time (ms)
DWORD dwElapsed = pStats[0].time - pStats[start].time;
// Calculate average
pStats[0].ave = dwElapsed > 0 ? (DWORD)(dbSampleTotal * 1000.0 / dwElapsed + 0.5) : 0;
}
DWORD Cwinproc::GetRecentMaximum(STATS_STRUCT *stats, int num, int type) {
ASSERT(num <= MAX_SAMPLES);
DWORD result = 1; // To prevent division by zero when using the maximum for scaling
if (type == 0) {
for (int i = 0; i < num; i++)
result = max(stats[i].Bps, result);
} else if (type == 1) {
for (int i = 0; i < num; i++)
result = max(stats[i].ave, result);
} else
ASSERT(false);
return result;
}
// Calculate samples
void Cwinproc::OnTimer(UINT /* nIDEvent */) {
// Retrieve the total number of bytes received and sent by all network adapters, modulo 2^32.
// Each network adapter counts from its own epoch, which is the last time the cable or wireless LAN was connected.
DWORD r = 0;
DWORD s = 0;
bool ok = snmp.GetReceivedAndSentOctets(r, s);
DWORD curRecv = 0;
DWORD curSent = 0;
if (ok) {
if (m_PrevBytesOk) {
curRecv = r - m_PrevBytesRecv; // With 32-bit unsigned wraparound
curSent = s - m_PrevBytesSent;
}
m_PrevBytesRecv = r;
m_PrevBytesSent = s;
}
m_TotalBytesRecv += curRecv;
m_TotalBytesSent += curSent;
m_PrevBytesOk = ok;
// Don't depend upon timing of WM_TIMER messages. Get the true number of milliseconds elapsed
DWORD dwTime = GetTickCount();
DWORD elapsed = dwTime - m_dwStartTime;
m_dwStartTime = dwTime;
// Calculate bits per second
DWORD dwRecv_bps = 0;
DWORD dwSent_bps = 0;
if (elapsed > 0) {
dwRecv_bps = (DWORD)(curRecv * 1000.0 / elapsed + 0.5);
dwSent_bps = (DWORD)(curSent * 1000.0 / elapsed + 0.5);
}
// Shift over previous data
for (int i = MAX_SAMPLES - 1; i >= 1; i--) {
RecvStats[i] = RecvStats[i - 1];
SentStats[i] = SentStats[i - 1];
}
// Calc the average bps and add new entry to array
CalcAverages(m_TotalBytesRecv, dwTime, dwRecv_bps, RecvStats);
CalcAverages(m_TotalBytesSent, dwTime, dwSent_bps, SentStats);
// Get the icon for the system tray
HICON hIcon = theApp.m_Icons.GetIcon(RecvStats, SentStats, g_IconStyle);
UpdateTrayIcon(hIcon);
DestroyIcon(hIcon);
}
void Cwinproc::ShowPropertiesDlg() {
if (m_pPropertiesDlg != NULL)
m_pPropertiesDlg->SetForegroundWindow();
else {
// Fake out MFC in order to receive the "minimize all windows" syscommand message
// The window is restored in initdialog
theApp.m_pMainWnd = NULL;
m_pPropertiesDlg = new DlgPropSheet(SZ_APPNAME, NULL);
m_pPropertiesDlg->m_psh.dwFlags |= PSH_NOAPPLYNOW | PSH_MODELESS;
if (m_pPropertiesDlg->DoModal() == IDOK)
SaveSettings();
else
ReadSettings();
delete m_pPropertiesDlg;
m_pPropertiesDlg = NULL;
SetTimer(TIMER_ID_WINPROC, g_nSampleRate, NULL);
}
}
LRESULT Cwinproc::OnTaskbarNotify(WPARAM wParam, LPARAM lParam) {
UNREFERENCED_PARAMETER(wParam);
switch (lParam) {
case WM_MOUSEMOVE:
{
CString s, sRecvBPS, sRecvAVE;
FormatBytes(RecvStats[0].Bps, sRecvBPS, true);
FormatBytes(RecvStats[0].ave, sRecvAVE, true);
s.Format("Current: %s Average: %s", sRecvBPS, sRecvAVE);
m_SystemTray.cbSize = sizeof(NOTIFYICONDATA);
m_SystemTray.hWnd = GetSafeHwnd();
m_SystemTray.uID = 1;
m_SystemTray.uFlags = NIF_TIP;
strcpy_s(m_SystemTray.szTip, s);
Shell_NotifyIcon(NIM_MODIFY, &m_SystemTray);
}
break;
case WM_LBUTTONDBLCLK:
ShowPropertiesDlg();
break;
case WM_RBUTTONUP:
{
CMenu menu;
POINT pt;
GetCursorPos(&pt);
menu.LoadMenu(IDR_MENU1);
menu.SetDefaultItem(0, TRUE);
CMenu &pMenu = *menu.GetSubMenu(0);
pMenu.SetDefaultItem(0, TRUE);
// See Q135788 "PRB: Menus for Notification Icons Do Not Work Correctly"
SetForegroundWindow();
int cmd = pMenu.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, pt.x, pt.y, this);
PostMessage(WM_NULL, 0, 0);
if (cmd == IDCLOSE) {
// Save any settings if the user closes the tray icon while the dialog is open
if (m_pPropertiesDlg != NULL) {
SaveSettings();
m_pPropertiesDlg->SendMessage(WM_CLOSE);
}
theApp.m_wnd.PostMessage(WM_CLOSE);
} else if (cmd == ID_PROPERTIES)
ShowPropertiesDlg();
}
break;
}
return 0;
}
void Cwinproc::UpdateTrayIcon(HICON hIcon) {
ASSERT(hIcon != 0);
if (m_SystemTray.hWnd != NULL && hIcon != NULL) {
m_SystemTray.cbSize = sizeof(NOTIFYICONDATA);
m_SystemTray.hWnd = GetSafeHwnd();
m_SystemTray.uID = 1;
m_SystemTray.hIcon = hIcon;
m_SystemTray.uFlags = NIF_ICON;
m_SystemTray.uCallbackMessage = TaskbarCallbackMsg;
Shell_NotifyIcon(NIM_MODIFY, &m_SystemTray);
}
}
// Init all arrays
void Cwinproc::ResetData() {
ZeroMemory(RecvStats, sizeof(RecvStats));
ZeroMemory(SentStats, sizeof(SentStats));
m_PrevBytesOk = false;
m_TotalBytesRecv = 0;
m_TotalBytesSent = 0;
m_PrevBytesRecv = 0;
m_PrevBytesSent = 0;
}
void Cwinproc::WinHelp(DWORD /*dwData*/, UINT /*nCmd*/) {
if (m_pPropertiesDlg != NULL) {
switch (m_pPropertiesDlg->GetActiveIndex()) {
case 0: CWnd::WinHelp(IDH_connection_tab); return;
case 1: CWnd::WinHelp(IDH_options_tab ); return;
case 2: CWnd::WinHelp(IDH_colors_tab ); return;
}
}
CWnd::WinHelp(0, HELP_CONTENTS);
}