summaryrefslogtreecommitdiff
path: root/plugins/Ping/src/pinggraph.cpp
blob: 1947ada7bfac3401391b3f8f152d6bacf7bce9dc (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
#include "common.h"

HistoryMap history_map;

#define ID_REPAINT_TIMER		10101

struct WindowData {
	DWORD item_id;
	HistoryList list;
	HWND hwnd_chk_grid;
	HWND hwnd_chk_stat;
	bool show_grid;
	bool show_stat;
};

#define WM_REBUILDLIST			(WM_USER + 5)

LRESULT CALLBACK GraphWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_REBUILDLIST:
	{
		WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);

		bool found = false;
		mir_cslock lck(data_list_cs);
		for (pinglist_it i = data_list.begin(); i != data_list.end(); ++i) {
			if (i->item_id == wd->item_id) {
				wd->list = history_map[wd->item_id];
				found = true;
				break;
			}
		}

		if (!found) {
			PostMessage(hwnd, WM_CLOSE, 0, 0);
			return TRUE;
		}

		InvalidateRect(hwnd, 0, FALSE);
	}
	return TRUE;
	case WM_SHOWWINDOW:
		if (wParam == TRUE && lParam == 0) {
			WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);

			if (wd->hwnd_chk_grid == 0) {
				wd->hwnd_chk_grid = CreateWindow(_T("BUTTON"), TranslateT("Show grid lines"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, 0, hInst, 0);
				SendMessage(wd->hwnd_chk_grid, BM_SETCHECK, wd->show_grid ? BST_CHECKED : BST_UNCHECKED, 0);
			}
			if (wd->hwnd_chk_stat == 0) {
				wd->hwnd_chk_stat = CreateWindow(_T("BUTTON"), TranslateT("Show stats"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, 0, hInst, 0);
				SendMessage(wd->hwnd_chk_stat, BM_SETCHECK, wd->show_stat ? BST_CHECKED : BST_UNCHECKED, 0);
			}
			KillTimer(hwnd, ID_REPAINT_TIMER);
#ifdef min
			SetTimer(hwnd, ID_REPAINT_TIMER, min(options.ping_period * 1000, 5000), 0);
#else
			SetTimer(hwnd, ID_REPAINT_TIMER, std::min(options.ping_period * 1000, 5000), 0);
#endif

			SendMessage(hwnd, WM_REBUILDLIST, 0, 0);
		}
		break;
	case WM_COMMAND:
		if (HIWORD(wParam) == BN_CLICKED) {
			WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
			if ((HWND)lParam == wd->hwnd_chk_grid) {
				wd->show_grid = (SendMessage(wd->hwnd_chk_grid, BM_GETCHECK, 0, 0) == BST_CHECKED);
			}
			else if ((HWND)lParam == wd->hwnd_chk_stat) {
				wd->show_stat = (SendMessage(wd->hwnd_chk_stat, BM_GETCHECK, 0, 0) == BST_CHECKED);
			}
			InvalidateRect(hwnd, 0, TRUE);
		}
		return TRUE;
	case WM_TIMER:
	{
		SendMessage(hwnd, WM_REBUILDLIST, 0, 0);
	}
	return TRUE;
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc;
		RECT r;
		WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		if (wd && (hdc = BeginPaint(hwnd, &ps)) != 0)
		{
			GetClientRect(hwnd, &r);
			FillRect(hdc, &r, GetSysColorBrush(COLOR_WINDOW));

			short max_value = -1, min_value = (short)0x7FFF,
				graph_height = 0; // this is minimum graph height, in ms

			double avg = 0;
			for (HistoryList::Iterator hli = wd->list.begin(); hli.has_val(); hli.next())
			{
				if (hli.val().first > max_value)
					max_value = hli.val().first;
				if (hli.val().first >= 0 && hli.val().first < min_value)
					min_value = hli.val().first;
				avg += hli.val().first;
			}

			if (wd->list.size())
				avg /= wd->list.size();

			graph_height = (int)(max_value * 1.2f);

			if (graph_height < MIN_GRAPH_HEIGHT)
				graph_height = MIN_GRAPH_HEIGHT;

#ifdef max
			float unit_width = (r.right - r.left) / (float)max((int)wd->list.size(), MIN_BARS), // space for at least MIN_BARS bars
#else
			float unit_width = (r.right - r.left) / (float)std::max((int)wd->list.size(), MIN_BARS), // space for at least MIN_BARS bars
#endif
				unit_height = (r.bottom - r.top) / (float)graph_height;

			time_t last_time = 0, time, start_time = 0;
			if (wd->list.size())
				start_time = wd->list.begin().val().second;

			RECT bar;
			bar.bottom = r.bottom;
			float x = r.right - (unit_width * wd->list.size() + 0.5f);
			bar.left = (int)(x + 0.5f);
			bar.right = (int)(x + unit_width + 0.5f);

			// set up pen for horiz (ping time) and vert (time covered by graph) lines
			HPEN hPenOld, hPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
			hPenOld = (HPEN)SelectObject(hdc, hPen);

			for (HistoryList::Iterator hi = wd->list.begin(); hi.has_val(); hi.next())
			{
				if (hi.val().first != -1)
				{
					bar.top = bar.bottom - (int)(hi.val().first * unit_height + 0.5f);
					FillRect(hdc, &bar, GetSysColorBrush(COLOR_HOTLIGHT));
				}

				time = hi.val().second - start_time;

				if (time / MARK_PERIOD != last_time / MARK_PERIOD)
				{ // new minute
					MoveToEx(hdc, bar.left, r.bottom, 0);
					LineTo(hdc, bar.left, r.top);
				}

				last_time = time;

				x += unit_width;
				bar.left = bar.right;
				bar.right = (int)(x + unit_width + 0.5f);
			}

			if (wd->show_grid)
			{
				// draw horizontal lines to mark every 100ms
				for (int li = 0; li < graph_height; li += MARK_TIME)
				{
					MoveToEx(hdc, r.left, r.bottom - (int)(li * unit_height + 0.5f), 0);
					LineTo(hdc, r.right, r.bottom - (int)(li * unit_height + 0.5f));
				}
			}

			HPEN hPen2 = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
			if (wd->show_stat)
			{
				SelectObject(hdc, hPen2);
				MoveToEx(hdc, r.left, r.bottom - (int)(avg * unit_height + 0.5f), 0);
				LineTo(hdc, r.right, r.bottom - (int)(avg * unit_height + 0.5f));
				if (max_value != avg)
				{
					MoveToEx(hdc, r.left, r.bottom - (int)(max_value * unit_height + 0.5f), 0);
					LineTo(hdc, r.right, r.bottom - (int)(max_value * unit_height + 0.5f));
					MoveToEx(hdc, r.left, r.bottom - (int)(min_value * unit_height + 0.5f), 0);
					LineTo(hdc, r.right, r.bottom - (int)(min_value * unit_height + 0.5f));
				}
			}

			// restore pen
			SelectObject(hdc, hPenOld);
			DeleteObject(hPen);
			DeleteObject(hPen2);

			SetBkMode(hdc, TRANSPARENT);
			SetTextColor(hdc, GetSysColor(COLOR_3DDKSHADOW));
			TCHAR buff[64];
			if (wd->show_grid)
			{
				mir_sntprintf(buff, TranslateT("%d ms"), MARK_TIME);
				TextOut(hdc, r.right - 100, r.bottom - (int)(unit_height * MARK_TIME + 0.5f), buff, (int)mir_tstrlen(buff));
			}

			if (wd->show_stat)
			{
				SetTextColor(hdc, RGB(255, 0, 0));
				mir_sntprintf(buff, TranslateT("AVG %.1lf ms"), avg);
				TextOut(hdc, r.left + 10, r.bottom - (int)(avg * unit_height + 0.5f), buff, (int)mir_tstrlen(buff));
				if (max_value != avg) {
					mir_sntprintf(buff, TranslateT("MAX %hd ms"), max_value);
					TextOut(hdc, r.left + 10, r.bottom - (int)(max_value * unit_height + 0.5f), buff, (int)mir_tstrlen(buff));
					mir_sntprintf(buff, TranslateT("MIN %hd ms"), min_value);
					TextOut(hdc, r.left + 10, r.bottom - (int)(min_value * unit_height + 0.5f), buff, (int)mir_tstrlen(buff));
				}
			}

			EndPaint(hwnd, &ps);
		}
	}
	return TRUE;

	case WM_ERASEBKGND:
		return TRUE;

	case WM_SIZE:
	{
		WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		RECT r;
		GetClientRect(hwnd, &r);
		if (wd->hwnd_chk_grid != 0) SetWindowPos(wd->hwnd_chk_grid, 0, r.right - 150, r.top + 10, 120, 20, SWP_NOZORDER | SWP_NOACTIVATE);
		if (wd->hwnd_chk_stat != 0) SetWindowPos(wd->hwnd_chk_stat, 0, r.right - 150, r.top + 30, 120, 20, SWP_NOZORDER | SWP_NOACTIVATE);
	}
	InvalidateRect(hwnd, 0, FALSE);
	break;
	case WM_CLOSE:
	{
		KillTimer(hwnd, ID_REPAINT_TIMER);
		WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		{
			char buff[30];
			mir_snprintf(buff, "pinggraphwnd%d", wd->item_id);
			Utils_SaveWindowPosition(hwnd, 0, PLUG, buff);
		}
	}
	break;
	case WM_DESTROY:
	{
		WindowData *wd = (WindowData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		{
			char buff[30];
			mir_snprintf(buff, "WindowHandle%d", wd->item_id);
			db_set_dw(0, PLUG, buff, 0);
		}
		delete wd;
	}
	// drop through
	};

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

INT_PTR ShowGraph(WPARAM wParam, LPARAM lParam) {
	char buff[30];
	mir_snprintf(buff, "WindowHandle%d", (DWORD)wParam);
	HWND hGraphWnd = (HWND)db_get_dw(0, PLUG, buff, 0);
	if (hGraphWnd) {
		ShowWindow(hGraphWnd, SW_SHOW);
		SetWindowPos(hGraphWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
		return 0;
	}

	WNDCLASS wndclass;
	wndclass.style = 0;
	wndclass.lpfnWndProc = GraphWindowProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInst;
	wndclass.hIcon = hIconResponding;
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = _T(PLUG) _T("GraphWindow");
	RegisterClass(&wndclass);

	TCHAR title[256];
	mir_tstrncpy(title, TranslateT("Ping Graph"), SIZEOF(title));
	if (lParam) {
		mir_tstrncat(title, _T(" - "), SIZEOF(title) - mir_tstrlen(title));
		mir_tstrncat(title, (TCHAR *)lParam, SIZEOF(title) - mir_tstrlen(title));
	}

	HWND parent = 0;
	hGraphWnd = CreateWindowEx(0, _T(PLUG) _T("GraphWindow"), title,
		(WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN) & ~CS_VREDRAW & ~CS_HREDRAW,
		0, 0, 800, 600, parent, NULL, hInst, NULL);

	WindowData *wd = new WindowData;
	wd->item_id = (DWORD)wParam; // wParam is destination id
	wd->hwnd_chk_grid = 0;
	wd->hwnd_chk_stat = 0;
	wd->show_grid = db_get_b(0, PLUG, "ShowGridLines", 0) ? true : false;
	wd->show_stat = db_get_b(0, PLUG, "ShowStats", 1) ? true : false;

	db_set_dw(0, PLUG, buff, (DWORD)hGraphWnd);

	SetWindowLongPtr(hGraphWnd, GWLP_USERDATA, (LONG_PTR)wd);

	mir_snprintf(buff, "pinggraphwnd%d", wd->item_id);
	Utils_RestoreWindowPosition(hGraphWnd, 0, PLUG, buff);

	if (!IsWindowVisible(hGraphWnd))
		ShowWindow(hGraphWnd, SW_SHOW);

	return 0;
}

// save window positions, close windows
void graphs_cleanup() {
	int list_size = GetListSize(0, 0);
	char buff[64];
	HWND hwnd;

	for (int i = 0; i < list_size; i++) {
		mir_snprintf(buff, "WindowHandle%d", i);
		if (hwnd = (HWND)db_get_dw(0, PLUG, buff, 0)) {
			DestroyWindow(hwnd);
			db_set_dw(0, PLUG, buff, 0);
			mir_snprintf(buff, "WindowWasOpen%d", i);
			db_set_b(0, PLUG, buff, 1);
		}
	}
}

// remove old data, possibly left from a crash
void graphs_init() {
	PINGLIST pl;
	char buff[64];
	CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
	for (pinglist_it i = pl.begin(); i != pl.end(); ++i) {
		mir_snprintf(buff, "WindowHandle%d", i->item_id); // clean up from possible crash
		db_set_dw(0, PLUG, buff, 0);
		mir_snprintf(buff, "WindowWasOpen%d", i->item_id); // restore windows that were open on shutdown
		if (db_get_b(0, PLUG, buff, 0)) {
			db_set_b(0, PLUG, buff, 0);
			ShowGraph((WPARAM)i->item_id, (LPARAM)i->pszLabel);
		}
	}
}