]>
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
)
69 static void ResizeCommandWindow(void)
73 GetClientRect(CommandWindow
, &r
);
76 MoveWindow(ScrollbackEdit
, 10, 10, w
- 20, h
- 50, TRUE
);
77 MoveWindow(CommandEdit
, 10, h
- 29, w
- 20, 22, TRUE
);
80 void RepaintGraphWindow(void)
82 InvalidateRect(GraphWindow
, NULL
, TRUE
);
85 static LRESULT CALLBACK
86 CommandWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
95 ResizeCommandWindow();
99 SetFocus(CommandEdit
);
103 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
109 static void PaintGraph(HDC hdc
)
122 GetClientRect(GraphWindow
, &r
);
124 SelectObject(hdc
, WhitePen
);
126 MoveToEx(hdc
, r
.left
+ offset
, r
.top
, NULL
);
127 LineTo(hdc
, r
.left
+ offset
, r
.bottom
);
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
);
135 (GraphTraceLen
- (int)((r
.right
- r
.left
- offset
) / GraphPixelsPerPoint
));
139 if(GraphStart
> startMax
) {
140 GraphStart
= startMax
;
145 SelectObject(hdc
, pen
);
148 for(i
= GraphStart
; ; i
++) {
149 if(i
>= GraphTraceLen
) {
152 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
153 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
155 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
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));
166 // number of points that will be plotted
167 double span
= (int)((r
.right
- r
.left
) / GraphPixelsPerPoint
);
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
);
182 for(i
= GraphStart
; ; i
++) {
183 if(i
>= GraphTraceLen
) {
186 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
187 if(x
> r
.right
+ GraphPixelsPerPoint
) {
191 int y
= GraphBuffer
[i
];
201 y
= (y
* (r
.top
- r
.bottom
) / (2*absYMax
)) + zeroHeight
;
202 if(i
== GraphStart
) {
203 MoveToEx(hdc
, x
, y
, NULL
);
208 if(GraphPixelsPerPoint
> 10) {
214 FillRect(hdc
, &f
, brush
);
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);
223 sprintf(str
, "+%d", (i
- GraphStart
));
225 GetTextExtentPoint32(hdc
, str
, strlen(str
), &size
);
226 TextOut(hdc
, x
- size
.cx
, zeroHeight
+ 8, str
, strlen(str
));
228 SelectObject(hdc
, pen
);
229 MoveToEx(hdc
, x
, y
, NULL
);
232 if(i
== CursorAPos
|| i
== CursorBPos
) {
233 if(i
== CursorAPos
) {
234 SelectObject(hdc
, CursorAPen
);
236 SelectObject(hdc
, CursorBPen
);
238 MoveToEx(hdc
, x
, r
.top
, NULL
);
239 LineTo(hdc
, x
, r
.bottom
);
241 SelectObject(hdc
, pen
);
242 MoveToEx(hdc
, x
, y
, NULL
);
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
));
257 static LRESULT CALLBACK
258 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
264 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
267 RepaintGraphWindow();
272 HDC hdc
= BeginPaint(hwnd
, &ps
);
276 // This draws the trace.
284 if(GraphPixelsPerPoint
<= 8) {
285 GraphPixelsPerPoint
*= 2;
290 if(GraphPixelsPerPoint
>= 0.01) {
291 GraphPixelsPerPoint
/= 2;
296 if(GraphPixelsPerPoint
< 16) {
297 GraphStart
+= (int)(16 / GraphPixelsPerPoint
);
304 if(GraphPixelsPerPoint
< 16) {
305 GraphStart
-= (int)(16 / GraphPixelsPerPoint
);
314 RepaintGraphWindow();
319 case WM_RBUTTONDOWN
: {
320 int x
= LOWORD(lParam
);
322 x
= (int)(x
/ GraphPixelsPerPoint
);
324 if(msg
== WM_LBUTTONDOWN
) {
329 RepaintGraphWindow();
333 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
339 void PrintToScrollback(char *fmt
, ...)
345 vsprintf(str
+2, fmt
, f
);
347 static char TextBuf
[1024*32];
348 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
351 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
352 strcat(TextBuf
, str
);
354 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
357 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
358 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
361 void ShowGraphWindow(void)
363 if(GraphWindow
) return;
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
,
369 if(!GraphWindow
) oops();
372 void HideGraphWindow(void)
375 DestroyWindow(GraphWindow
);
380 static void SetCommandEditTo(char *str
)
382 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
383 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
389 memset(&wc
, 0, sizeof(wc
));
390 wc
.cbSize
= sizeof(wc
);
392 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
393 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
395 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
396 wc
.lpszClassName
= "Command";
397 wc
.lpszMenuName
= NULL
;
398 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
400 if(!RegisterClassEx(&wc
)) oops();
402 wc
.lpszClassName
= "Graph";
403 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
404 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
406 if(!RegisterClassEx(&wc
)) oops();
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
,
412 if(!CommandWindow
) oops();
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
,
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
);
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");
427 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
429 FixedFont(ScrollbackEdit
);
430 FixedFont(CommandEdit
);
432 ResizeCommandWindow();
433 SetFocus(CommandEdit
);
435 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
436 PrintToScrollback(">> Connected to device");
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));
445 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
446 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
450 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
451 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
453 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
456 if(strcmp(got
, "cls")==0) {
457 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
459 CommandReceived(got
);
461 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
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;
474 CommandHistoryPos
= -1;
475 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
476 msg
.hwnd
== CommandEdit
)
478 if(CommandHistoryPos
== -1) {
479 CommandHistoryPos
= CommandHistoryNext
;
482 if(CommandHistoryPos
< 0) {
483 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
485 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
486 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
487 msg
.hwnd
== CommandEdit
)
490 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
491 CommandHistoryPos
= 0;
493 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
494 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
495 msg
.hwnd
== CommandEdit
)
497 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
499 if(msg
.message
== WM_KEYDOWN
) {
500 CommandHistoryPos
= -1;
502 TranslateMessage(&msg
);
503 DispatchMessage(&msg
);
508 if(ReceiveCommandPoll(&c
)) {
509 UsbCommandReceived(&c
);