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