]>
git.zerfleddert.de Git - proxmark3-svn/blob - winsrc/gui.cpp
cc8e17f9f499bac6a1fab30c8f01fc67e85930a7
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
];
36 int offline
; // Whether the GUI operates in Offline mode.
38 HPEN GreyPen
, GreenPen
, WhitePen
, YellowPen
;
39 HBRUSH GreenBrush
, YellowBrush
;
41 static int GraphStart
= 0;
42 static double GraphPixelsPerPoint
= 1;
44 static int CursorAPos
;
45 static int CursorBPos
;
46 double CursorScaleFactor
= 1.0;
47 static HPEN CursorAPen
;
48 static HPEN CursorBPen
;
50 static HWND CommandWindow
;
51 static HWND GraphWindow
;
52 static HWND ScrollbackEdit
;
53 static HWND CommandEdit
;
55 #define COMMAND_HISTORY_MAX 16
56 static char CommandHistory
[COMMAND_HISTORY_MAX
][256];
57 static int CommandHistoryPos
= -1;
58 static int CommandHistoryNext
;
60 static HFONT MyFixedFont
;
61 #define FixedFont(x) SendMessage((x), WM_SETFONT, (WPARAM)MyFixedFont, TRUE)
63 void ExecCmd(char *cmd
)
70 static void ResizeCommandWindow(void)
74 GetClientRect(CommandWindow
, &r
);
77 MoveWindow(ScrollbackEdit
, 10, 10, w
- 20, h
- 50, TRUE
);
78 MoveWindow(CommandEdit
, 10, h
- 29, w
- 20, 22, TRUE
);
81 void RepaintGraphWindow(void)
83 InvalidateRect(GraphWindow
, NULL
, TRUE
);
86 static LRESULT CALLBACK
87 CommandWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
96 ResizeCommandWindow();
100 SetFocus(CommandEdit
);
104 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
110 static void PaintGraph(HDC hdc
)
123 GetClientRect(GraphWindow
, &r
);
125 SelectObject(hdc
, WhitePen
);
127 MoveToEx(hdc
, r
.left
+ offset
, r
.top
, NULL
);
128 LineTo(hdc
, r
.left
+ offset
, r
.bottom
);
130 int zeroHeight
= r
.top
+ (r
.bottom
- r
.top
) / 2;
131 SelectObject(hdc
, GreyPen
);
132 MoveToEx(hdc
, r
.left
, zeroHeight
, NULL
);
133 LineTo(hdc
, r
.right
, zeroHeight
);
136 (GraphTraceLen
- (int)((r
.right
- r
.left
- offset
) / GraphPixelsPerPoint
));
140 if(GraphStart
> startMax
) {
141 GraphStart
= startMax
;
146 SelectObject(hdc
, pen
);
149 for(i
= GraphStart
; ; i
++) {
150 if(i
>= GraphTraceLen
) {
153 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
154 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
156 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
162 absYMax
= (int)(absYMax
*1.2 + 1);
163 SelectObject(hdc
, MyFixedFont
);
164 SetTextColor(hdc
, RGB(255, 255, 255));
165 SetBkColor(hdc
, RGB(0, 0, 0));
167 // number of points that will be plotted
168 double span
= (int)((r
.right
- r
.left
) / GraphPixelsPerPoint
);
170 // one label every offset pixels, let us say
171 int labels
= (r
.right
- r
.left
- offset
) / offset
;
172 if(labels
<= 0) labels
= 1;
173 // round to nearest power of 2
174 int pointsPerLabel
= (int)(log(span
/ labels
)/log(2.0));
175 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
176 pointsPerLabel
= (int)pow(2.0,pointsPerLabel
);
183 for(i
= GraphStart
; ; i
++) {
184 if(i
>= GraphTraceLen
) {
187 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
188 if(x
> r
.right
+ GraphPixelsPerPoint
) {
192 int y
= GraphBuffer
[i
];
202 y
= (y
* (r
.top
- r
.bottom
) / (2*absYMax
)) + zeroHeight
;
203 if(i
== GraphStart
) {
204 MoveToEx(hdc
, x
, y
, NULL
);
209 if(GraphPixelsPerPoint
> 10) {
215 FillRect(hdc
, &f
, brush
);
218 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
219 SelectObject(hdc
, WhitePen
);
220 MoveToEx(hdc
, x
, zeroHeight
- 8, NULL
);
221 LineTo(hdc
, x
, zeroHeight
+ 8);
224 sprintf(str
, "+%d", (i
- GraphStart
));
226 GetTextExtentPoint32(hdc
, str
, strlen(str
), &size
);
227 TextOut(hdc
, x
- size
.cx
, zeroHeight
+ 8, str
, strlen(str
));
229 SelectObject(hdc
, pen
);
230 MoveToEx(hdc
, x
, y
, NULL
);
233 if(i
== CursorAPos
|| i
== CursorBPos
) {
234 if(i
== CursorAPos
) {
235 SelectObject(hdc
, CursorAPen
);
237 SelectObject(hdc
, CursorBPen
);
239 MoveToEx(hdc
, x
, r
.top
, NULL
);
240 LineTo(hdc
, x
, r
.bottom
);
242 SelectObject(hdc
, pen
);
243 MoveToEx(hdc
, x
, y
, NULL
);
252 sprintf(str
, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f",
253 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
254 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
, GraphPixelsPerPoint
);
255 TextOut(hdc
, 50, r
.bottom
- 20, str
, strlen(str
));
258 static LRESULT CALLBACK
259 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
265 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
268 RepaintGraphWindow();
273 HDC hdc
= BeginPaint(hwnd
, &ps
);
277 // This draws the trace.
285 if(GraphPixelsPerPoint
<= 8) {
286 GraphPixelsPerPoint
*= 2;
291 if(GraphPixelsPerPoint
>= 0.01) {
292 GraphPixelsPerPoint
/= 2;
297 if(GraphPixelsPerPoint
< 16) {
298 GraphStart
+= (int)(16 / GraphPixelsPerPoint
);
305 if(GraphPixelsPerPoint
< 16) {
306 GraphStart
-= (int)(16 / GraphPixelsPerPoint
);
315 RepaintGraphWindow();
320 case WM_RBUTTONDOWN
: {
321 int x
= LOWORD(lParam
);
323 x
= (int)(x
/ GraphPixelsPerPoint
);
325 if(msg
== WM_LBUTTONDOWN
) {
330 RepaintGraphWindow();
334 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
340 void PrintToScrollback(char *fmt
, ...)
346 vsprintf(str
+2, fmt
, f
);
348 static char TextBuf
[1024*32];
349 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
352 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
353 strcat(TextBuf
, str
);
355 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
358 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
359 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
362 void ShowGraphWindow(void)
364 if(GraphWindow
) return;
366 GraphWindow
= CreateWindowEx(0, "Graph", "graphed",
367 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
368 WS_SIZEBOX
| WS_VISIBLE
, 200, 150, 600, 500, NULL
, NULL
, NULL
,
370 if(!GraphWindow
) oops();
373 void HideGraphWindow(void)
376 DestroyWindow(GraphWindow
);
381 static void SetCommandEditTo(char *str
)
383 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
384 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
390 memset(&wc
, 0, sizeof(wc
));
391 wc
.cbSize
= sizeof(wc
);
393 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
394 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
396 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
397 wc
.lpszClassName
= "Command";
398 wc
.lpszMenuName
= NULL
;
399 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
401 if(!RegisterClassEx(&wc
)) oops();
403 wc
.lpszClassName
= "Graph";
404 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
405 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
407 if(!RegisterClassEx(&wc
)) oops();
409 CommandWindow
= CreateWindowEx(0, "Command", "prox",
410 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
411 WS_SIZEBOX
| WS_VISIBLE
, 20, 20, 500, 400, NULL
, NULL
, NULL
,
413 if(!CommandWindow
) oops();
415 ScrollbackEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
416 WS_CHILD
| WS_CLIPSIBLINGS
| WS_VISIBLE
| ES_MULTILINE
|
417 ES_AUTOVSCROLL
| WS_VSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
,
420 CommandEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
421 WS_CHILD
| WS_CLIPSIBLINGS
| WS_TABSTOP
| WS_VISIBLE
|
422 ES_AUTOHSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
, NULL
, NULL
);
424 MyFixedFont
= CreateFont(14, 0, 0, 0, FW_REGULAR
, FALSE
, FALSE
, FALSE
,
425 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
426 FF_DONTCARE
, "Lucida Console");
428 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
430 FixedFont(ScrollbackEdit
);
431 FixedFont(CommandEdit
);
433 ResizeCommandWindow();
434 SetFocus(CommandEdit
);
436 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
437 PrintToScrollback(">> Connected to device");
439 GreyPen
= CreatePen(PS_SOLID
, 1, RGB(100, 100, 100));
440 GreenPen
= CreatePen(PS_SOLID
, 1, RGB(100, 255, 100));
441 YellowPen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 0));
442 GreenBrush
= CreateSolidBrush(RGB(100, 255, 100));
443 YellowBrush
= CreateSolidBrush(RGB(255, 255, 0));
444 WhitePen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 255));
446 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
447 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
451 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
452 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
454 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
457 if(strcmp(got
, "cls")==0) {
458 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
460 CommandReceived(got
);
462 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
464 // Insert it into the command history, unless it is
465 // identical to the previous command in the history.
466 int prev
= CommandHistoryNext
- 1;
467 if(prev
< 0) prev
+= COMMAND_HISTORY_MAX
;
468 if(strcmp(CommandHistory
[prev
], got
) != 0) {
469 strcpy(CommandHistory
[CommandHistoryNext
], got
);
470 CommandHistoryNext
++;
471 if(CommandHistoryNext
== COMMAND_HISTORY_MAX
) {
472 CommandHistoryNext
= 0;
475 CommandHistoryPos
= -1;
476 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
477 msg
.hwnd
== CommandEdit
)
479 if(CommandHistoryPos
== -1) {
480 CommandHistoryPos
= CommandHistoryNext
;
483 if(CommandHistoryPos
< 0) {
484 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
486 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
487 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
488 msg
.hwnd
== CommandEdit
)
491 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
492 CommandHistoryPos
= 0;
494 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
495 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
496 msg
.hwnd
== CommandEdit
)
498 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
500 if(msg
.message
== WM_KEYDOWN
) {
501 CommandHistoryPos
= -1;
503 TranslateMessage(&msg
);
504 DispatchMessage(&msg
);
509 if(ReceiveCommandPoll(&c
)) {
510 UsbCommandReceived(&c
);