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