]>
git.zerfleddert.de Git - proxmark3-svn/blob - winsrc/gui.cpp
da83c865e7e82108e01d70cf803c9fde7921100a
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
);
252 sprintf(str
, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]",
253 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
254 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
, GraphPixelsPerPoint
,
255 CursorAPos
, GraphBuffer
[CursorAPos
], CursorBPos
, GraphBuffer
[CursorBPos
]);
256 TextOut(hdc
, 50, r
.bottom
- 20, str
, strlen(str
));
259 static LRESULT CALLBACK
260 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
266 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
269 RepaintGraphWindow();
274 HDC hdc
= BeginPaint(hwnd
, &ps
);
278 // This draws the trace.
286 if(GraphPixelsPerPoint
<= 8) {
287 GraphPixelsPerPoint
*= 2;
292 if(GraphPixelsPerPoint
>= 0.01) {
293 GraphPixelsPerPoint
/= 2;
298 if(GraphPixelsPerPoint
< 16) {
299 GraphStart
+= (int)(16 / GraphPixelsPerPoint
);
306 if(GraphPixelsPerPoint
< 16) {
307 GraphStart
-= (int)(16 / GraphPixelsPerPoint
);
316 RepaintGraphWindow();
321 case WM_RBUTTONDOWN
: {
322 int x
= LOWORD(lParam
);
324 x
= (int)(x
/ GraphPixelsPerPoint
);
327 if(msg
== WM_LBUTTONDOWN
) {
332 RepaintGraphWindow();
336 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
342 void PrintToScrollback(char *fmt
, ...)
348 vsprintf(str
+2, fmt
, f
);
350 static char TextBuf
[1024*32];
351 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
354 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
355 strcat(TextBuf
, str
);
357 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
360 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
361 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
364 void ShowGraphWindow(void)
366 if(GraphWindow
) return;
368 GraphWindow
= CreateWindowEx(0, "Graph", "graphed",
369 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
370 WS_SIZEBOX
| WS_VISIBLE
, 200, 150, 600, 500, NULL
, NULL
, NULL
,
372 if(!GraphWindow
) oops();
375 void HideGraphWindow(void)
378 DestroyWindow(GraphWindow
);
383 static void SetCommandEditTo(char *str
)
385 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
386 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
392 memset(&wc
, 0, sizeof(wc
));
393 wc
.cbSize
= sizeof(wc
);
395 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
396 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
398 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
399 wc
.lpszClassName
= "Command";
400 wc
.lpszMenuName
= NULL
;
401 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
403 if(!RegisterClassEx(&wc
)) oops();
405 wc
.lpszClassName
= "Graph";
406 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
407 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
409 if(!RegisterClassEx(&wc
)) oops();
411 CommandWindow
= CreateWindowEx(0, "Command", "prox",
412 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
413 WS_SIZEBOX
| WS_VISIBLE
, 20, 20, 500, 400, NULL
, NULL
, NULL
,
415 if(!CommandWindow
) oops();
417 ScrollbackEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
418 WS_CHILD
| WS_CLIPSIBLINGS
| WS_VISIBLE
| ES_MULTILINE
|
419 ES_AUTOVSCROLL
| WS_VSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
,
422 CommandEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
423 WS_CHILD
| WS_CLIPSIBLINGS
| WS_TABSTOP
| WS_VISIBLE
|
424 ES_AUTOHSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
, NULL
, NULL
);
426 MyFixedFont
= CreateFont(14, 0, 0, 0, FW_REGULAR
, FALSE
, FALSE
, FALSE
,
427 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
428 FF_DONTCARE
, "Lucida Console");
430 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
432 FixedFont(ScrollbackEdit
);
433 FixedFont(CommandEdit
);
435 ResizeCommandWindow();
436 SetFocus(CommandEdit
);
438 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
439 PrintToScrollback(">> Connected to device");
441 GreyPen
= CreatePen(PS_SOLID
, 1, RGB(100, 100, 100));
442 GreenPen
= CreatePen(PS_SOLID
, 1, RGB(100, 255, 100));
443 YellowPen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 0));
444 GreenBrush
= CreateSolidBrush(RGB(100, 255, 100));
445 YellowBrush
= CreateSolidBrush(RGB(255, 255, 0));
446 WhitePen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 255));
448 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
449 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
453 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
454 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
456 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
459 if(strcmp(got
, "cls")==0) {
460 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
462 CommandReceived(got
);
464 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
466 // Insert it into the command history, unless it is
467 // identical to the previous command in the history.
468 int prev
= CommandHistoryNext
- 1;
469 if(prev
< 0) prev
+= COMMAND_HISTORY_MAX
;
470 if(strcmp(CommandHistory
[prev
], got
) != 0) {
471 strcpy(CommandHistory
[CommandHistoryNext
], got
);
472 CommandHistoryNext
++;
473 if(CommandHistoryNext
== COMMAND_HISTORY_MAX
) {
474 CommandHistoryNext
= 0;
477 CommandHistoryPos
= -1;
478 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
479 msg
.hwnd
== CommandEdit
)
481 if(CommandHistoryPos
== -1) {
482 CommandHistoryPos
= CommandHistoryNext
;
485 if(CommandHistoryPos
< 0) {
486 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
488 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
489 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
490 msg
.hwnd
== CommandEdit
)
493 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
494 CommandHistoryPos
= 0;
496 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
497 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
498 msg
.hwnd
== CommandEdit
)
500 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
502 if(msg
.message
== WM_KEYDOWN
) {
503 CommandHistoryPos
= -1;
505 TranslateMessage(&msg
);
506 DispatchMessage(&msg
);
513 if(ReceiveCommandPoll(&c
))
514 UsbCommandReceived(&c
);