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