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