-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnm.cpp
More file actions
339 lines (256 loc) · 6.97 KB
/
nm.cpp
File metadata and controls
339 lines (256 loc) · 6.97 KB
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
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include "counters.h"
typedef unsigned long uint32;
typedef long int32;
// ================================================================================================
// Makes UI and bitmaps and stuff
// ================================================================================================
class NetMeter {
// minimal UI bits
HWND hwnd;
HBITMAP hBitmap;
HFONT font;
long width, height;
unsigned long *rawBits;
// everything else
PDH psend;
PDH precv;
ATOM Register(HINSTANCE hinst) {
WNDCLASS wc;
// register the main window class
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)wproc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinst;
wc.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "NM";
return RegisterClass(&wc);
}
static void CALLBACK tproc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
NetMeter *nm = (NetMeter*)idEvent;
nm->Timer();
}
int Initialize(int w, int h) {
//SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
width = w;
height = h;
HINSTANCE hi = GetModuleHandle(NULL);
ATOM netclass = Register(hi);
hwnd = ::CreateWindowEx(WS_EX_TOPMOST | WS_EX_TOOLWINDOW, "NM", "", WS_POPUP,
256, 16, width, height,
(HWND)NULL, (HMENU)NULL, hi, LPVOID(NULL));
int err =GetLastError();
if (!hwnd) return -1;
::SetWindowLong(hwnd, GWL_USERDATA, (long)this);
::SetWindowLong(hwnd, GWL_WNDPROC, (long)wproc);
{
BITMAPINFO bmi;
int bmihsize = sizeof(BITMAPINFOHEADER);
memset(&bmi, 0, bmihsize);
BITMAPINFOHEADER &h = bmi.bmiHeader;
h.biSize = bmihsize;
h.biWidth = width;
h.biHeight = -height;
h.biPlanes = 1;
h.biBitCount = 32;
h.biCompression = BI_RGB;
hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&rawBits, NULL, 0);
if (!hBitmap) return -1;
}
{
// make font
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
strcpy(lf.lfFaceName, "Arial");
lf.lfHeight = 14;
font = ::CreateFontIndirect(&lf);
}
::ShowWindow((HWND)hwnd, SW_SHOWNORMAL);
::SetTimer(hwnd, (long)this, 1000, tproc);
return 0;
}
void Smooth(long *v, int32 ct) {
int32 i;
const float soft = 0.1f;
const float isoft = 1.f - soft;
float weight = 0;
float value = 0;
for (i = 0; i < ct; i++) {
weight = weight * isoft + soft;
value = value * isoft + soft * v[i];
v[i] = value / weight;
}
weight = value = 0;
for (i = ct - 1; i >= 0; i--) {
weight = weight * isoft + soft;
value = value * isoft + soft * v[i];
v[i] = value / weight;
}
}
int Redraw() {
int32 i, x, y;
const uint32 ocol = 0x45433C;
const uint32 dcol = 0xD6CFBB;
const uint32 upcol = 0xF90505;
const uint32 dncol = 0x19469E;
if (!rawBits) return -1;
int32 clipheight = height - 20;
// plot history from pdh
for (i = 0; i < width * height; i++) {
rawBits[i] = 0xEEE7D0;
}
long *vs = (long*)alloca(width * 4);
long *vr = (long*)alloca(width * 4);
long maxval = 0;
long maxr = 0;
long maxs = 0;
for (i = 0; i < width; i++) {
vs[i] = psend.Sum(i);
vr[i] = precv.Sum(i);
maxs = max(vs[i], maxs);
maxr = max(vr[i], maxr);
maxval = max(vs[i], maxval);
maxval = max(vr[i], maxval);
}
long midmax = min(maxs, maxr);
{
int32 v1 = midmax * clipheight / maxval;
v1 = clipheight - v1 - 1;
for (x = 1; x < width - 1; x++) {
rawBits[x + width * v1] = dcol;
}
}
//for (int smooth = 0; smooth < 2; smooth ++) {
for (x = 0; x < width; x++) {
int32 i = (width - x - 1);
int32 sc1 = vs[i] * clipheight / maxval;
int32 sc2 = vr[i] * clipheight / maxval;
sc1 = clipheight - sc1 - 1;
sc2 = clipheight - sc2 - 1;
/*
if (smooth) {
if (vs[i] || vr[i]) {
uint32 *pixel1 = rawBits + x + width * sc1;
uint32 *pixel2 = rawBits + x + width * sc2;
if (sc1 < sc2) {
pixel1[0] = 0;
} else {
pixel2[0] = 0;
}
}
} */
for (int32 y = (clipheight - 1); y >= 0; y--) {
uint32 *pixel = rawBits + x + y * width;
if (y < sc1 && y < sc2) break;
/*if (smooth) {
if (y > sc2 || y > sc1) pixel[0] = (pixel[0] >> 1 & 0x7F7F7F) + 0x7f7f7f;
} else {*/
if (y > sc2) pixel[0] = dncol;
if (y > sc1) pixel[0] = upcol;
//}
}
}
//Smooth(vs, width);
//Smooth(vr, width);
//}
// outline thing
for (y = 0; y < height; y++) {
rawBits[0 + y * width] = ocol;
rawBits[(y + 1) * width - 1] = ocol;
}
for (x = 1; x < width - 1; x++) {
rawBits[x] = ocol;
rawBits[x + width * (height - 1)] = ocol;
rawBits[x + width * (clipheight)] = dcol;
}
{
HDC hdc = (HDC)CreateCompatibleDC(NULL);
::SelectObject(hdc, hBitmap);
::SetTextAlign(hdc, TA_BOTTOM | TA_CENTER);
::SetBkMode(hdc, TRANSPARENT);
::SelectObject(hdc, font);
char temp[200];
sprintf(temp, "Down: %dKB/s Up: %dKB/s Max: %dKB/s", vr[0] / 1024, vs[0] / 1024, maxval / 1024);
TextOut(hdc, width / 2, height - 4, temp, strlen(temp));
::DeleteDC(hdc);
}
return Draw();
}
int Draw(HDC hdc = NULL) {
HDC hcdc = CreateCompatibleDC(NULL);
HDC dc = hdc;
if (!dc) dc = (HDC)GetDC(hwnd);
HBITMAP oldbitmap = (HBITMAP)SelectObject((HDC)hcdc, hBitmap);
::BitBlt(dc, 0, 0, width, height, (HDC)hcdc, 0, 0, SRCCOPY);
int e = GetLastError();
SelectObject((HDC)hcdc, oldbitmap);
DeleteDC(hcdc);
DeleteDC(dc);
::GdiFlush();
return 0;
}
static int __stdcall wproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
NetMeter *nm = (NetMeter*)::GetWindowLong(hwnd, GWL_USERDATA);
long rv = nm->WindowProc(uMsg, wParam, lParam);
if (rv != -1) return rv;
// -1 => default
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
public:
NetMeter() : rawBits(NULL), hwnd(NULL), hBitmap(NULL) {
psend.AddObjects("Network Interface", "Bytes Sent");
precv.AddObjects("Network Interface", "Bytes Received");
Initialize(256, 96);
}
int Timer() {
psend.Tick();
precv.Tick();
return Redraw();
}
int WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (uMsg == WM_LBUTTONDOWN) {
// drag hack
::SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE | 0x02, 0);
}
if (uMsg == WM_TIMER) {
Timer();
}
if (uMsg == WM_ERASEBKGND) {
return 0;
}
if (uMsg == WM_PAINT) {
// stupid paint
PAINTSTRUCT ps;
HDC hdc = ::BeginPaint(hwnd, &ps);
if(hdc != NULL) {
Draw(hdc);
::EndPaint(hwnd, &ps);
}
}
if (uMsg == WM_CLOSE) {
::PostQuitMessage(0);
}
return -1;
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
NetMeter nm;
MSG msg;
while (::GetMessage(&msg, (HWND)NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}