]> cvs.zerfleddert.de Git - proxmark3-svn/blame - winsrc/gui.cpp
show samples values at cursor A & B in plots
[proxmark3-svn] / winsrc / gui.cpp
CommitLineData
6658905f 1//-----------------------------------------------------------------------------\r
2// Routines for the user interface when doing interactive things with prox\r
3// cards; this is basically a command line thing, in one window, and then\r
4// another window to do the graphs.\r
5// Jonathan Westhues, Sept 2005\r
6//-----------------------------------------------------------------------------\r
7#include <windows.h>\r
8#include <limits.h>\r
9#include <commctrl.h>\r
10#include <stdlib.h>\r
11#include <stdio.h>\r
12#include <math.h>\r
13\r
14#include "prox.h"\r
15\r
16#define oops() do { \\r
17 char line[100]; \\r
18 sprintf(line, "Internal error at line %d file '%s'", __LINE__, \\r
19 __FILE__); \\r
20 MessageBox(NULL, line, "Error", MB_ICONERROR); \\r
21 exit(-1); \\r
22} while(0)\r
23\r
24void dbp(char *str, ...)\r
25{\r
26 va_list f;\r
27 char buf[1024];\r
28 va_start(f, str);\r
29 vsprintf(buf, str, f);\r
30 OutputDebugString(buf);\r
31 OutputDebugString("\n");\r
32}\r
33\r
34int GraphBuffer[MAX_GRAPH_TRACE_LEN];\r
35int GraphTraceLen;\r
36\r
37HPEN GreyPen, GreenPen, WhitePen, YellowPen;\r
38HBRUSH GreenBrush, YellowBrush;\r
39\r
40static int GraphStart = 0;\r
41static double GraphPixelsPerPoint = 1;\r
42\r
43static int CursorAPos;\r
44static int CursorBPos;\r
45double CursorScaleFactor = 1.0;\r
46static HPEN CursorAPen;\r
47static HPEN CursorBPen;\r
48\r
49static HWND CommandWindow;\r
50static HWND GraphWindow;\r
51static HWND ScrollbackEdit;\r
52static HWND CommandEdit;\r
53\r
54#define COMMAND_HISTORY_MAX 16\r
55static char CommandHistory[COMMAND_HISTORY_MAX][256];\r
56static int CommandHistoryPos = -1;\r
57static int CommandHistoryNext;\r
58\r
59static HFONT MyFixedFont;\r
60#define FixedFont(x) SendMessage((x), WM_SETFONT, (WPARAM)MyFixedFont, TRUE)\r
61\r
62void ExecCmd(char *cmd)\r
63{\r
64\r
65}\r
66int CommandFinished;\r
30f2a7d3 67int offset = 64;\r
6658905f 68\r
69static void ResizeCommandWindow(void)\r
70{\r
71 int w, h;\r
72 RECT r;\r
73 GetClientRect(CommandWindow, &r);\r
74 w = r.right - r.left;\r
75 h = r.bottom - r.top;\r
76 MoveWindow(ScrollbackEdit, 10, 10, w - 20, h - 50, TRUE);\r
77 MoveWindow(CommandEdit, 10, h - 29, w - 20, 22, TRUE);\r
78}\r
79\r
80void RepaintGraphWindow(void)\r
81{\r
82 InvalidateRect(GraphWindow, NULL, TRUE);\r
83}\r
84\r
85static LRESULT CALLBACK\r
86 CommandWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\r
87{\r
88 switch (msg) {\r
89 case WM_DESTROY:\r
90 case WM_QUIT:\r
91 exit(0);\r
92 return 0;\r
93\r
94 case WM_SIZE:\r
95 ResizeCommandWindow();\r
96 return 0;\r
97\r
98 case WM_SETFOCUS:\r
99 SetFocus(CommandEdit);\r
100 break;\r
101\r
102 default:\r
103 return DefWindowProc(hwnd, msg, wParam, lParam);\r
104 }\r
105\r
106 return 1;\r
107}\r
108\r
109static void PaintGraph(HDC hdc)\r
110{\r
111 HBRUSH brush;\r
112 HPEN pen;\r
113\r
114 brush = GreenBrush;\r
115 pen = GreenPen;\r
116\r
117 if(GraphStart < 0) {\r
118 GraphStart = 0;\r
119 }\r
120\r
121 RECT r;\r
122 GetClientRect(GraphWindow, &r);\r
123\r
124 SelectObject(hdc, WhitePen);\r
125\r
30f2a7d3 126 MoveToEx(hdc, r.left + offset, r.top, NULL);\r
127 LineTo(hdc, r.left + offset, r.bottom);\r
6658905f 128\r
129 int zeroHeight = r.top + (r.bottom - r.top) / 2;\r
130 SelectObject(hdc, GreyPen);\r
131 MoveToEx(hdc, r.left, zeroHeight, NULL);\r
132 LineTo(hdc, r.right, zeroHeight);\r
133\r
134 int startMax =\r
30f2a7d3 135 (GraphTraceLen - (int)((r.right - r.left - offset) / GraphPixelsPerPoint));\r
6658905f 136 if(startMax < 0) {\r
137 startMax = 0;\r
138 }\r
139 if(GraphStart > startMax) {\r
140 GraphStart = startMax;\r
141 }\r
142\r
143 int absYMax = 1;\r
144\r
145 SelectObject(hdc, pen);\r
146\r
147 int i;\r
148 for(i = GraphStart; ; i++) {\r
149 if(i >= GraphTraceLen) {\r
150 break;\r
151 }\r
152 if(fabs((double)GraphBuffer[i]) > absYMax) {\r
153 absYMax = (int)fabs((double)GraphBuffer[i]);\r
154 }\r
30f2a7d3 155 int x = offset + (int)((i - GraphStart)*GraphPixelsPerPoint);\r
6658905f 156 if(x > r.right) {\r
157 break;\r
158 }\r
159 }\r
160\r
161 absYMax = (int)(absYMax*1.2 + 1);\r
162 SelectObject(hdc, MyFixedFont);\r
163 SetTextColor(hdc, RGB(255, 255, 255));\r
164 SetBkColor(hdc, RGB(0, 0, 0));\r
165\r
166 // number of points that will be plotted\r
30f2a7d3 167 double span = (int)((r.right - r.left) / GraphPixelsPerPoint);\r
168\r
169 // one label every offset pixels, let us say\r
170 int labels = (r.right - r.left - offset) / offset;\r
6658905f 171 if(labels <= 0) labels = 1;\r
30f2a7d3 172 // round to nearest power of 2\r
173 int pointsPerLabel = (int)(log(span / labels)/log(2.0));\r
6658905f 174 if(pointsPerLabel <= 0) pointsPerLabel = 1;\r
30f2a7d3 175 pointsPerLabel = (int)pow(2.0,pointsPerLabel);\r
6658905f 176\r
177 int yMin = INT_MAX;\r
178 int yMax = INT_MIN;\r
179 int yMean = 0;\r
180 int n = 0;\r
181\r
182 for(i = GraphStart; ; i++) {\r
183 if(i >= GraphTraceLen) {\r
184 break;\r
185 }\r
30f2a7d3 186 int x = offset + (int)((i - GraphStart)*GraphPixelsPerPoint);\r
6658905f 187 if(x > r.right + GraphPixelsPerPoint) {\r
188 break;\r
189 }\r
190\r
191 int y = GraphBuffer[i];\r
192 if(y < yMin) {\r
193 yMin = y;\r
194 }\r
195 if(y > yMax) {\r
196 yMax = y;\r
197 }\r
198 yMean += y;\r
199 n++;\r
200\r
201 y = (y * (r.top - r.bottom) / (2*absYMax)) + zeroHeight;\r
202 if(i == GraphStart) {\r
203 MoveToEx(hdc, x, y, NULL);\r
204 } else {\r
205 LineTo(hdc, x, y);\r
206 }\r
207\r
208 if(GraphPixelsPerPoint > 10) {\r
209 RECT f;\r
210 f.left = x - 3;\r
211 f.top = y - 3;\r
212 f.right = x + 3;\r
213 f.bottom = y + 3;\r
214 FillRect(hdc, &f, brush);\r
215 }\r
216\r
217 if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) {\r
218 SelectObject(hdc, WhitePen);\r
30f2a7d3 219 MoveToEx(hdc, x, zeroHeight - 8, NULL);\r
220 LineTo(hdc, x, zeroHeight + 8);\r
6658905f 221\r
222 char str[100];\r
223 sprintf(str, "+%d", (i - GraphStart));\r
224 SIZE size;\r
225 GetTextExtentPoint32(hdc, str, strlen(str), &size);\r
226 TextOut(hdc, x - size.cx, zeroHeight + 8, str, strlen(str));\r
227\r
228 SelectObject(hdc, pen);\r
229 MoveToEx(hdc, x, y, NULL);\r
230 }\r
231\r
232 if(i == CursorAPos || i == CursorBPos) {\r
233 if(i == CursorAPos) {\r
234 SelectObject(hdc, CursorAPen);\r
235 } else {\r
236 SelectObject(hdc, CursorBPen);\r
237 }\r
238 MoveToEx(hdc, x, r.top, NULL);\r
239 LineTo(hdc, x, r.bottom);\r
240\r
241 SelectObject(hdc, pen);\r
242 MoveToEx(hdc, x, y, NULL);\r
243 }\r
244 }\r
245\r
246 if(n != 0) {\r
247 yMean /= n;\r
248 }\r
249\r
250 char str[100];\r
af2d53cb 251 sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d (%d) CursorB=%d (%d)",\r
6658905f 252 GraphStart, yMax, yMin, yMean, n, GraphTraceLen,\r
af2d53cb 253 CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor, GraphPixelsPerPoint, CursorAPos, GraphBuffer[CursorAPos], CursorBPos, GraphBuffer[CursorBPos]);\r
6658905f 254 TextOut(hdc, 50, r.bottom - 20, str, strlen(str));\r
255}\r
256\r
257static LRESULT CALLBACK\r
258 GraphWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\r
259{\r
260 switch (msg) {\r
261 case WM_DESTROY:\r
262 case WM_QUIT:\r
263 GraphWindow = NULL;\r
264 return DefWindowProc(hwnd, msg, wParam, lParam);\r
265\r
266 case WM_SIZE:\r
267 RepaintGraphWindow();\r
268 return 0;\r
269\r
270 case WM_PAINT: {\r
271 PAINTSTRUCT ps;\r
272 HDC hdc = BeginPaint(hwnd, &ps);\r
273 if(GraphStart < 0) {\r
274 GraphStart = 0;\r
275 }\r
276 // This draws the trace.\r
277 PaintGraph(hdc);\r
278 EndPaint(hwnd, &ps);\r
279 break;\r
280 }\r
281 case WM_KEYDOWN:\r
282 switch(wParam) {\r
283 case VK_DOWN:\r
30f2a7d3 284 if(GraphPixelsPerPoint <= 8) {\r
6658905f 285 GraphPixelsPerPoint *= 2;\r
286 }\r
287 break;\r
288\r
289 case VK_UP:\r
30f2a7d3 290 if(GraphPixelsPerPoint >= 0.01) {\r
6658905f 291 GraphPixelsPerPoint /= 2;\r
292 }\r
293 break;\r
294\r
295 case VK_RIGHT:\r
30f2a7d3 296 if(GraphPixelsPerPoint < 16) {\r
297 GraphStart += (int)(16 / GraphPixelsPerPoint);\r
6658905f 298 } else {\r
299 GraphStart++;\r
300 }\r
301 break;\r
302\r
303 case VK_LEFT:\r
30f2a7d3 304 if(GraphPixelsPerPoint < 16) {\r
305 GraphStart -= (int)(16 / GraphPixelsPerPoint);\r
6658905f 306 } else {\r
307 GraphStart--;\r
308 }\r
309 break;\r
310\r
311 default:\r
312 goto nopaint;\r
313 }\r
314 RepaintGraphWindow();\r
315nopaint:\r
316 break;\r
317\r
318 case WM_LBUTTONDOWN:\r
319 case WM_RBUTTONDOWN: {\r
320 int x = LOWORD(lParam);\r
30f2a7d3 321 x -= offset;\r
6658905f 322 x = (int)(x / GraphPixelsPerPoint);\r
323 x += GraphStart;\r
324 if(msg == WM_LBUTTONDOWN) {\r
325 CursorAPos = x;\r
326 } else {\r
327 CursorBPos = x;\r
328 }\r
329 RepaintGraphWindow();\r
330 break;\r
331 }\r
332 default:\r
333 return DefWindowProc(hwnd, msg, wParam, lParam);\r
334 }\r
335\r
336 return 1;\r
337}\r
338\r
339void PrintToScrollback(char *fmt, ...)\r
340{\r
341 va_list f;\r
342 char str[1024];\r
343 strcpy(str, "\r\n");\r
344 va_start(f, fmt);\r
345 vsprintf(str+2, fmt, f);\r
346\r
347 static char TextBuf[1024*32];\r
348 SendMessage(ScrollbackEdit, WM_GETTEXT, (WPARAM)sizeof(TextBuf),\r
349 (LPARAM)TextBuf);\r
350\r
351 if(strlen(TextBuf) + strlen(str) + 1 <= sizeof(TextBuf)) {\r
352 strcat(TextBuf, str);\r
353 } else {\r
354 lstrcpyn(TextBuf, str, sizeof(TextBuf));\r
355 }\r
356\r
357 SendMessage(ScrollbackEdit, WM_SETTEXT, 0, (LPARAM)TextBuf);\r
358 SendMessage(ScrollbackEdit, EM_LINESCROLL, 0, (LPARAM)INT_MAX);\r
359}\r
360\r
361void ShowGraphWindow(void)\r
362{\r
363 if(GraphWindow) return;\r
364\r
365 GraphWindow = CreateWindowEx(0, "Graph", "graphed",\r
366 WS_OVERLAPPED | WS_BORDER | WS_MINIMIZEBOX | WS_SYSMENU |\r
367 WS_SIZEBOX | WS_VISIBLE, 200, 150, 600, 500, NULL, NULL, NULL,\r
368 NULL);\r
369 if(!GraphWindow) oops();\r
370}\r
371\r
372void HideGraphWindow(void)\r
373{\r
374 if(GraphWindow) {\r
375 DestroyWindow(GraphWindow);\r
376 GraphWindow = NULL;\r
377 }\r
378}\r
379\r
380static void SetCommandEditTo(char *str)\r
381{\r
382 SendMessage(CommandEdit, WM_SETTEXT, 0, (LPARAM)str);\r
383 SendMessage(CommandEdit, EM_SETSEL, strlen(str), strlen(str));\r
384}\r
385\r
9760414b 386void ShowGui()\r
6658905f 387{\r
388 WNDCLASSEX wc;\r
389 memset(&wc, 0, sizeof(wc));\r
390 wc.cbSize = sizeof(wc);\r
391\r
392 wc.style = CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW | CS_OWNDC;\r
393 wc.lpfnWndProc = (WNDPROC)CommandWindowProc;\r
394 wc.hInstance = NULL;\r
395 wc.hbrBackground = (HBRUSH)(COLOR_BTNSHADOW);\r
396 wc.lpszClassName = "Command";\r
397 wc.lpszMenuName = NULL;\r
398 wc.hCursor = LoadCursor(NULL, IDC_ARROW);\r
399\r
400 if(!RegisterClassEx(&wc)) oops();\r
401\r
402 wc.lpszClassName = "Graph";\r
403 wc.lpfnWndProc = (WNDPROC)GraphWindowProc;\r
404 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);\r
405\r
406 if(!RegisterClassEx(&wc)) oops();\r
407\r
408 CommandWindow = CreateWindowEx(0, "Command", "prox",\r
409 WS_OVERLAPPED | WS_BORDER | WS_MINIMIZEBOX | WS_SYSMENU |\r
410 WS_SIZEBOX | WS_VISIBLE, 20, 20, 500, 400, NULL, NULL, NULL,\r
411 NULL);\r
412 if(!CommandWindow) oops();\r
413\r
414 ScrollbackEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "",\r
415 WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | ES_MULTILINE |\r
416 ES_AUTOVSCROLL | WS_VSCROLL, 0, 0, 0, 0, CommandWindow, NULL,\r
417 NULL, NULL);\r
418\r
419 CommandEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "",\r
420 WS_CHILD | WS_CLIPSIBLINGS | WS_TABSTOP | WS_VISIBLE |\r
421 ES_AUTOHSCROLL, 0, 0, 0, 0, CommandWindow, NULL, NULL, NULL);\r
422\r
423 MyFixedFont = CreateFont(14, 0, 0, 0, FW_REGULAR, FALSE, FALSE, FALSE,\r
424 ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,\r
425 FF_DONTCARE, "Lucida Console");\r
426 if(!MyFixedFont)\r
427 MyFixedFont = (HFONT)GetStockObject(SYSTEM_FONT);\r
428\r
429 FixedFont(ScrollbackEdit);\r
430 FixedFont(CommandEdit);\r
431\r
432 ResizeCommandWindow();\r
433 SetFocus(CommandEdit);\r
434\r
435 PrintToScrollback(">> Started prox, built " __DATE__ " " __TIME__);\r
436 PrintToScrollback(">> Connected to device");\r
437\r
438 GreyPen = CreatePen(PS_SOLID, 1, RGB(100, 100, 100));\r
439 GreenPen = CreatePen(PS_SOLID, 1, RGB(100, 255, 100));\r
440 YellowPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 0));\r
441 GreenBrush = CreateSolidBrush(RGB(100, 255, 100));\r
442 YellowBrush = CreateSolidBrush(RGB(255, 255, 0));\r
443 WhitePen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));\r
444\r
445 CursorAPen = CreatePen(PS_DASH, 1, RGB(255, 255, 0));\r
446 CursorBPen = CreatePen(PS_DASH, 1, RGB(255, 0, 255));\r
447\r
448 MSG msg;\r
449 for(;;) {\r
450 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {\r
451 if(msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN) {\r
452 char got[1024];\r
453 SendMessage(CommandEdit, WM_GETTEXT, (WPARAM)sizeof(got),\r
454 (LPARAM)got);\r
455\r
456 if(strcmp(got, "cls")==0) {\r
457 SendMessage(ScrollbackEdit, WM_SETTEXT, 0, (LPARAM)"");\r
458 } else {\r
459 CommandReceived(got);\r
460 }\r
461 SendMessage(CommandEdit, WM_SETTEXT, 0, (LPARAM)"");\r
462\r
463 // Insert it into the command history, unless it is\r
464 // identical to the previous command in the history.\r
465 int prev = CommandHistoryNext - 1;\r
466 if(prev < 0) prev += COMMAND_HISTORY_MAX;\r
467 if(strcmp(CommandHistory[prev], got) != 0) {\r
468 strcpy(CommandHistory[CommandHistoryNext], got);\r
469 CommandHistoryNext++;\r
470 if(CommandHistoryNext == COMMAND_HISTORY_MAX) {\r
471 CommandHistoryNext = 0;\r
472 }\r
473 }\r
474 CommandHistoryPos = -1;\r
475 } else if(msg.message == WM_KEYDOWN && msg.wParam == VK_UP &&\r
476 msg.hwnd == CommandEdit)\r
477 {\r
478 if(CommandHistoryPos == -1) {\r
479 CommandHistoryPos = CommandHistoryNext;\r
480 }\r
481 CommandHistoryPos--;\r
482 if(CommandHistoryPos < 0) {\r
483 CommandHistoryPos = COMMAND_HISTORY_MAX-1;\r
484 }\r
485 SetCommandEditTo(CommandHistory[CommandHistoryPos]);\r
486 } else if(msg.message == WM_KEYDOWN && msg.wParam == VK_DOWN &&\r
487 msg.hwnd == CommandEdit)\r
488 {\r
489 CommandHistoryPos++;\r
490 if(CommandHistoryPos >= COMMAND_HISTORY_MAX) {\r
491 CommandHistoryPos = 0;\r
492 }\r
493 SetCommandEditTo(CommandHistory[CommandHistoryPos]);\r
494 } else if(msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE &&\r
495 msg.hwnd == CommandEdit)\r
496 {\r
497 SendMessage(CommandEdit, WM_SETTEXT, 0, (LPARAM)"");\r
498 } else {\r
499 if(msg.message == WM_KEYDOWN) {\r
500 CommandHistoryPos = -1;\r
501 }\r
502 TranslateMessage(&msg);\r
503 DispatchMessage(&msg);\r
504 }\r
505 }\r
506\r
9760414b 507 if (!offline)\r
508 {\r
509 UsbCommand c;\r
510 if(ReceiveCommandPoll(&c))\r
511 UsbCommandReceived(&c);\r
6658905f 512 }\r
513\r
514 Sleep(10);\r
515 }\r
516}\r
Impressum, Datenschutz