]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - 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 //-----------------------------------------------------------------------------
18 sprintf(line, "Internal error at line %d file '%s'", __LINE__, \
20 MessageBox(NULL, line, "Error", MB_ICONERROR); \
24 void dbp(char *str
, ...)
29 vsprintf(buf
, str
, f
);
30 OutputDebugString(buf
);
31 OutputDebugString("\n");
34 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
37 HPEN GreyPen
, GreenPen
, WhitePen
, YellowPen
;
38 HBRUSH GreenBrush
, YellowBrush
;
40 static int GraphStart
= 0;
41 static double GraphPixelsPerPoint
= 1;
43 static int CursorAPos
;
44 static int CursorBPos
;
45 double CursorScaleFactor
= 1.0;
46 static HPEN CursorAPen
;
47 static HPEN CursorBPen
;
49 static HWND CommandWindow
;
50 static HWND GraphWindow
;
51 static HWND ScrollbackEdit
;
52 static HWND CommandEdit
;
54 #define COMMAND_HISTORY_MAX 16
55 static char CommandHistory
[COMMAND_HISTORY_MAX
][256];
56 static int CommandHistoryPos
= -1;
57 static int CommandHistoryNext
;
59 static HFONT MyFixedFont
;
60 #define FixedFont(x) SendMessage((x), WM_SETFONT, (WPARAM)MyFixedFont, TRUE)
62 void ExecCmd(char *cmd
)
68 static void ResizeCommandWindow(void)
72 GetClientRect(CommandWindow
, &r
);
75 MoveWindow(ScrollbackEdit
, 10, 10, w
- 20, h
- 50, TRUE
);
76 MoveWindow(CommandEdit
, 10, h
- 29, w
- 20, 22, TRUE
);
79 void RepaintGraphWindow(void)
81 InvalidateRect(GraphWindow
, NULL
, TRUE
);
84 static LRESULT CALLBACK
85 CommandWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
94 ResizeCommandWindow();
98 SetFocus(CommandEdit
);
102 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
108 static void PaintGraph(HDC hdc
)
121 GetClientRect(GraphWindow
, &r
);
123 SelectObject(hdc
, WhitePen
);
125 MoveToEx(hdc
, r
.left
+ 40, r
.top
, NULL
);
126 LineTo(hdc
, r
.left
+ 40, r
.bottom
);
128 int zeroHeight
= r
.top
+ (r
.bottom
- r
.top
) / 2;
129 SelectObject(hdc
, GreyPen
);
130 MoveToEx(hdc
, r
.left
, zeroHeight
, NULL
);
131 LineTo(hdc
, r
.right
, zeroHeight
);
134 (GraphTraceLen
- (int)((r
.right
- r
.left
- 40) / GraphPixelsPerPoint
));
138 if(GraphStart
> startMax
) {
139 GraphStart
= startMax
;
144 SelectObject(hdc
, pen
);
147 for(i
= GraphStart
; ; i
++) {
148 if(i
>= GraphTraceLen
) {
151 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
152 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
154 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
160 absYMax
= (int)(absYMax
*1.2 + 1);
161 SelectObject(hdc
, MyFixedFont
);
162 SetTextColor(hdc
, RGB(255, 255, 255));
163 SetBkColor(hdc
, RGB(0, 0, 0));
165 // number of points that will be plotted
166 int span
= (int)((r
.right
- r
.left
) / GraphPixelsPerPoint
);
167 // one label every 100 pixels, let us say
168 int labels
= (r
.right
- r
.left
- 40) / 100;
169 if(labels
<= 0) labels
= 1;
170 int pointsPerLabel
= span
/ labels
;
171 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
178 for(i
= GraphStart
; ; i
++) {
179 if(i
>= GraphTraceLen
) {
182 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
183 if(x
> r
.right
+ GraphPixelsPerPoint
) {
187 int y
= GraphBuffer
[i
];
197 y
= (y
* (r
.top
- r
.bottom
) / (2*absYMax
)) + zeroHeight
;
198 if(i
== GraphStart
) {
199 MoveToEx(hdc
, x
, y
, NULL
);
204 if(GraphPixelsPerPoint
> 10) {
210 FillRect(hdc
, &f
, brush
);
213 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
214 SelectObject(hdc
, WhitePen
);
215 MoveToEx(hdc
, x
, zeroHeight
- 3, NULL
);
216 LineTo(hdc
, x
, zeroHeight
+ 3);
219 sprintf(str
, "+%d", (i
- GraphStart
));
221 GetTextExtentPoint32(hdc
, str
, strlen(str
), &size
);
222 TextOut(hdc
, x
- size
.cx
, zeroHeight
+ 8, str
, strlen(str
));
224 SelectObject(hdc
, pen
);
225 MoveToEx(hdc
, x
, y
, NULL
);
228 if(i
== CursorAPos
|| i
== CursorBPos
) {
229 if(i
== CursorAPos
) {
230 SelectObject(hdc
, CursorAPen
);
232 SelectObject(hdc
, CursorBPen
);
234 MoveToEx(hdc
, x
, r
.top
, NULL
);
235 LineTo(hdc
, x
, r
.bottom
);
237 SelectObject(hdc
, pen
);
238 MoveToEx(hdc
, x
, y
, NULL
);
247 sprintf(str
, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f]",
248 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
249 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
);
250 TextOut(hdc
, 50, r
.bottom
- 20, str
, strlen(str
));
253 static LRESULT CALLBACK
254 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
260 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
263 RepaintGraphWindow();
268 HDC hdc
= BeginPaint(hwnd
, &ps
);
272 // This draws the trace.
280 if(GraphPixelsPerPoint
<= 50) {
281 GraphPixelsPerPoint
*= 2;
286 if(GraphPixelsPerPoint
>= 0.02) {
287 GraphPixelsPerPoint
/= 2;
292 if(GraphPixelsPerPoint
< 20) {
293 GraphStart
+= (int)(20 / GraphPixelsPerPoint
);
300 if(GraphPixelsPerPoint
< 20) {
301 GraphStart
-= (int)(20 / GraphPixelsPerPoint
);
310 RepaintGraphWindow();
315 case WM_RBUTTONDOWN
: {
316 int x
= LOWORD(lParam
);
318 x
= (int)(x
/ GraphPixelsPerPoint
);
320 if(msg
== WM_LBUTTONDOWN
) {
325 RepaintGraphWindow();
329 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
335 void PrintToScrollback(char *fmt
, ...)
341 vsprintf(str
+2, fmt
, f
);
343 static char TextBuf
[1024*32];
344 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
347 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
348 strcat(TextBuf
, str
);
350 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
353 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
354 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
357 void ShowGraphWindow(void)
359 if(GraphWindow
) return;
361 GraphWindow
= CreateWindowEx(0, "Graph", "graphed",
362 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
363 WS_SIZEBOX
| WS_VISIBLE
, 200, 150, 600, 500, NULL
, NULL
, NULL
,
365 if(!GraphWindow
) oops();
368 void HideGraphWindow(void)
371 DestroyWindow(GraphWindow
);
376 static void SetCommandEditTo(char *str
)
378 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
379 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
385 memset(&wc
, 0, sizeof(wc
));
386 wc
.cbSize
= sizeof(wc
);
388 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
389 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
391 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
392 wc
.lpszClassName
= "Command";
393 wc
.lpszMenuName
= NULL
;
394 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
396 if(!RegisterClassEx(&wc
)) oops();
398 wc
.lpszClassName
= "Graph";
399 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
400 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
402 if(!RegisterClassEx(&wc
)) oops();
404 CommandWindow
= CreateWindowEx(0, "Command", "prox",
405 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
406 WS_SIZEBOX
| WS_VISIBLE
, 20, 20, 500, 400, NULL
, NULL
, NULL
,
408 if(!CommandWindow
) oops();
410 ScrollbackEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
411 WS_CHILD
| WS_CLIPSIBLINGS
| WS_VISIBLE
| ES_MULTILINE
|
412 ES_AUTOVSCROLL
| WS_VSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
,
415 CommandEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
416 WS_CHILD
| WS_CLIPSIBLINGS
| WS_TABSTOP
| WS_VISIBLE
|
417 ES_AUTOHSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
, NULL
, NULL
);
419 MyFixedFont
= CreateFont(14, 0, 0, 0, FW_REGULAR
, FALSE
, FALSE
, FALSE
,
420 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
421 FF_DONTCARE
, "Lucida Console");
423 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
425 FixedFont(ScrollbackEdit
);
426 FixedFont(CommandEdit
);
428 ResizeCommandWindow();
429 SetFocus(CommandEdit
);
431 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
432 PrintToScrollback(">> Connected to device");
434 GreyPen
= CreatePen(PS_SOLID
, 1, RGB(100, 100, 100));
435 GreenPen
= CreatePen(PS_SOLID
, 1, RGB(100, 255, 100));
436 YellowPen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 0));
437 GreenBrush
= CreateSolidBrush(RGB(100, 255, 100));
438 YellowBrush
= CreateSolidBrush(RGB(255, 255, 0));
439 WhitePen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 255));
441 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
442 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
446 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
447 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
449 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
452 if(strcmp(got
, "cls")==0) {
453 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
455 CommandReceived(got
);
457 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
459 // Insert it into the command history, unless it is
460 // identical to the previous command in the history.
461 int prev
= CommandHistoryNext
- 1;
462 if(prev
< 0) prev
+= COMMAND_HISTORY_MAX
;
463 if(strcmp(CommandHistory
[prev
], got
) != 0) {
464 strcpy(CommandHistory
[CommandHistoryNext
], got
);
465 CommandHistoryNext
++;
466 if(CommandHistoryNext
== COMMAND_HISTORY_MAX
) {
467 CommandHistoryNext
= 0;
470 CommandHistoryPos
= -1;
471 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
472 msg
.hwnd
== CommandEdit
)
474 if(CommandHistoryPos
== -1) {
475 CommandHistoryPos
= CommandHistoryNext
;
478 if(CommandHistoryPos
< 0) {
479 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
481 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
482 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
483 msg
.hwnd
== CommandEdit
)
486 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
487 CommandHistoryPos
= 0;
489 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
490 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
491 msg
.hwnd
== CommandEdit
)
493 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
495 if(msg
.message
== WM_KEYDOWN
) {
496 CommandHistoryPos
= -1;
498 TranslateMessage(&msg
);
499 DispatchMessage(&msg
);
504 if(ReceiveCommandPoll(&c
)) {
505 UsbCommandReceived(&c
);