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