]>
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
];
36 int PlotGridX
, PlotGridY
;
38 HPEN GreyPenLite
, GreyPen
, GreenPen
, WhitePen
, YellowPen
;
39 HBRUSH GreenBrush
, YellowBrush
;
41 static int GraphStart
= 0;
42 static double GraphPixelsPerPoint
= 1;
44 static int PlotGridX
= 0, PlotGridY
= 0;
45 static int CursorAPos
;
46 static int CursorBPos
;
47 double CursorScaleFactor
= 1.0;
48 static HPEN CursorAPen
;
49 static HPEN CursorBPen
;
51 static HWND CommandWindow
;
52 static HWND GraphWindow
;
53 static HWND ScrollbackEdit
;
54 static HWND CommandEdit
;
56 #define COMMAND_HISTORY_MAX 16
57 static char CommandHistory
[COMMAND_HISTORY_MAX
][256];
58 static int CommandHistoryPos
= -1;
59 static int CommandHistoryNext
;
61 static HFONT MyFixedFont
;
62 #define FixedFont(x) SendMessage((x), WM_SETFONT, (WPARAM)MyFixedFont, TRUE)
64 void ExecCmd(char *cmd
)
71 static void ResizeCommandWindow(void)
75 GetClientRect(CommandWindow
, &r
);
78 MoveWindow(ScrollbackEdit
, 10, 10, w
- 20, h
- 50, TRUE
);
79 MoveWindow(CommandEdit
, 10, h
- 29, w
- 20, 22, TRUE
);
82 void RepaintGraphWindow(void)
84 InvalidateRect(GraphWindow
, NULL
, TRUE
);
87 static LRESULT CALLBACK
88 CommandWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
97 ResizeCommandWindow();
101 SetFocus(CommandEdit
);
105 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
111 static void PaintGraph(HDC hdc
)
127 GetClientRect(GraphWindow
, &r
);
128 int zeroHeight
= (r
.top
+ r
.bottom
) >> 1;
130 // plot X and Y grid lines
131 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
132 for(i
= offset
; i
< r
.right
; i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
133 SelectObject(hdc
, GreyPenLite
);
134 MoveToEx(hdc
, r
.left
+ i
, r
.top
, NULL
);
135 LineTo(hdc
, r
.left
+ i
, r
.bottom
);
139 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
140 for(i
= 0; i
< ((r
.top
+ r
.bottom
)>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
141 SelectObject(hdc
, GreyPenLite
);
142 MoveToEx(hdc
, r
.left
, zeroHeight
+ i
, NULL
);
143 LineTo(hdc
, r
.right
, zeroHeight
+ i
);
144 MoveToEx(hdc
, r
.left
, zeroHeight
- i
, NULL
);
145 LineTo(hdc
, r
.right
, zeroHeight
- i
);
149 // print vertical separator white line on the left of the window
150 SelectObject(hdc
, WhitePen
);
151 MoveToEx(hdc
, r
.left
+ offset
, r
.top
, NULL
);
152 LineTo(hdc
, r
.left
+ offset
, r
.bottom
);
154 // print horizontal grey zero axis line
155 SelectObject(hdc
, GreyPen
);
156 MoveToEx(hdc
, r
.left
, zeroHeight
, NULL
);
157 LineTo(hdc
, r
.right
, zeroHeight
);
159 startMax
= (GraphTraceLen
- (int)((r
.right
- r
.left
- offset
) / GraphPixelsPerPoint
));
161 if(startMax
< 0) startMax
= 0;
162 if(GraphStart
> startMax
) GraphStart
= startMax
;
163 if(GraphStart
< 0) GraphStart
= 0;
166 SelectObject(hdc
, pen
);
168 // go over the portion of the graph to be displayed and find the largest
169 // absolute value which will be used to auto scale the graph when displayed
170 for(i
= GraphStart
; ; i
++) {
171 if(i
>= GraphTraceLen
) {
174 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
175 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
177 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
183 absYMax
= (int)(absYMax
*1.2 + 1);
184 SelectObject(hdc
, MyFixedFont
);
185 SetTextColor(hdc
, RGB(255, 255, 255));
186 SetBkColor(hdc
, RGB(0, 0, 0));
188 // number of points that will be plotted
189 double span
= (int)((r
.right
- r
.left
) / GraphPixelsPerPoint
);
191 // one label every offset pixels, let us say
192 int labels
= (r
.right
- r
.left
- offset
) / offset
;
193 if(labels
<= 0) labels
= 1;
194 // round to nearest power of 2
195 int pointsPerLabel
= (int)(log(span
/ labels
)/log(2.0));
196 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
197 pointsPerLabel
= (int)pow(2.0,pointsPerLabel
);
199 // go over the graph and plot samples and labels
200 for(i
= GraphStart
; ; i
++) {
201 if(i
>= GraphTraceLen
) {
204 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
205 if(x
> r
.right
+ GraphPixelsPerPoint
) {
209 int y
= GraphBuffer
[i
];
210 if(y
< yMin
) yMin
= y
;
211 if(y
> yMax
) yMax
= y
;
215 y
= (y
* (r
.top
- r
.bottom
) / (2*absYMax
)) + zeroHeight
;
216 if(i
== GraphStart
) {
217 MoveToEx(hdc
, x
, y
, NULL
);
222 if(GraphPixelsPerPoint
> 10) {
228 FillRect(hdc
, &f
, brush
);
232 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
233 SelectObject(hdc
, WhitePen
);
234 MoveToEx(hdc
, x
, zeroHeight
- 8, NULL
);
235 LineTo(hdc
, x
, zeroHeight
+ 8);
237 sprintf(str
, "+%d", i
);
239 GetTextExtentPoint32(hdc
, str
, strlen(str
), &size
);
240 TextOut(hdc
, x
- size
.cx
, zeroHeight
+ 8, str
, strlen(str
));
242 SelectObject(hdc
, pen
);
243 MoveToEx(hdc
, x
, y
, NULL
);
246 // plot measurement cursors
247 if(i
== CursorAPos
|| i
== CursorBPos
) {
248 if(i
== CursorAPos
) {
249 SelectObject(hdc
, CursorAPen
);
251 SelectObject(hdc
, CursorBPen
);
253 MoveToEx(hdc
, x
, r
.top
, NULL
);
254 LineTo(hdc
, x
, r
.bottom
);
256 SelectObject(hdc
, pen
);
257 MoveToEx(hdc
, x
, y
, NULL
);
265 // print misc information at bottom of graph window
266 sprintf(str
, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]",
267 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
268 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
, GraphPixelsPerPoint
,
269 CursorAPos
, GraphBuffer
[CursorAPos
], CursorBPos
, GraphBuffer
[CursorBPos
]);
270 TextOut(hdc
, 50, r
.bottom
- 20, str
, strlen(str
));
273 static LRESULT CALLBACK
274 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
280 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
283 RepaintGraphWindow();
288 HDC hdc
= BeginPaint(hwnd
, &ps
);
292 // This draws the trace.
300 if(GraphPixelsPerPoint
<= 8) {
301 GraphPixelsPerPoint
*= 2;
306 if(GraphPixelsPerPoint
>= 0.01) {
307 GraphPixelsPerPoint
/= 2;
312 if(GraphPixelsPerPoint
< 16) {
313 GraphStart
+= (int)(16 / GraphPixelsPerPoint
);
320 if(GraphPixelsPerPoint
< 16) {
321 GraphStart
-= (int)(16 / GraphPixelsPerPoint
);
330 RepaintGraphWindow();
335 case WM_RBUTTONDOWN
: {
336 int x
= LOWORD(lParam
);
338 x
= (int)(x
/ GraphPixelsPerPoint
);
341 if(msg
== WM_LBUTTONDOWN
) {
346 RepaintGraphWindow();
350 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
356 void PrintToScrollback(char *fmt
, ...)
362 vsprintf(str
+2, fmt
, f
);
364 static char TextBuf
[1024*32];
365 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
368 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
369 strcat(TextBuf
, str
);
371 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
374 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
375 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
378 void ShowGraphWindow(void)
380 if(GraphWindow
) return;
382 GraphWindow
= CreateWindowEx(0, "Graph", "graphed",
383 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
384 WS_SIZEBOX
| WS_VISIBLE
, 200, 150, 600, 500, NULL
, NULL
, NULL
,
386 if(!GraphWindow
) oops();
389 void HideGraphWindow(void)
392 DestroyWindow(GraphWindow
);
397 static void SetCommandEditTo(char *str
)
399 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
400 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
406 memset(&wc
, 0, sizeof(wc
));
407 wc
.cbSize
= sizeof(wc
);
409 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
410 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
412 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
413 wc
.lpszClassName
= "Command";
414 wc
.lpszMenuName
= NULL
;
415 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
417 if(!RegisterClassEx(&wc
)) oops();
419 wc
.lpszClassName
= "Graph";
420 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
421 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
423 if(!RegisterClassEx(&wc
)) oops();
425 CommandWindow
= CreateWindowEx(0, "Command", "prox",
426 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
427 WS_SIZEBOX
| WS_VISIBLE
, 20, 20, 500, 400, NULL
, NULL
, NULL
,
429 if(!CommandWindow
) oops();
431 ScrollbackEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
432 WS_CHILD
| WS_CLIPSIBLINGS
| WS_VISIBLE
| ES_MULTILINE
|
433 ES_AUTOVSCROLL
| WS_VSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
,
436 CommandEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
437 WS_CHILD
| WS_CLIPSIBLINGS
| WS_TABSTOP
| WS_VISIBLE
|
438 ES_AUTOHSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
, NULL
, NULL
);
440 MyFixedFont
= CreateFont(14, 0, 0, 0, FW_REGULAR
, FALSE
, FALSE
, FALSE
,
441 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
442 FF_DONTCARE
, "Lucida Console");
444 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
446 FixedFont(ScrollbackEdit
);
447 FixedFont(CommandEdit
);
449 ResizeCommandWindow();
450 SetFocus(CommandEdit
);
452 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
453 PrintToScrollback(">> Connected to device");
455 GreyPenLite
= CreatePen(PS_SOLID
, 1, RGB(50, 50, 50));
456 GreyPen
= CreatePen(PS_SOLID
, 1, RGB(100, 100, 100));
457 GreenPen
= CreatePen(PS_SOLID
, 1, RGB(100, 255, 100));
458 YellowPen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 0));
459 GreenBrush
= CreateSolidBrush(RGB(100, 255, 100));
460 YellowBrush
= CreateSolidBrush(RGB(255, 255, 0));
461 WhitePen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 255));
463 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
464 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
468 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
469 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
471 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
474 if(strcmp(got
, "cls")==0) {
475 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
477 CommandReceived(got
);
479 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
481 // Insert it into the command history, unless it is
482 // identical to the previous command in the history.
483 int prev
= CommandHistoryNext
- 1;
484 if(prev
< 0) prev
+= COMMAND_HISTORY_MAX
;
485 if(strcmp(CommandHistory
[prev
], got
) != 0) {
486 strcpy(CommandHistory
[CommandHistoryNext
], got
);
487 CommandHistoryNext
++;
488 if(CommandHistoryNext
== COMMAND_HISTORY_MAX
) {
489 CommandHistoryNext
= 0;
492 CommandHistoryPos
= -1;
493 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
494 msg
.hwnd
== CommandEdit
)
496 if(CommandHistoryPos
== -1) {
497 CommandHistoryPos
= CommandHistoryNext
;
500 if(CommandHistoryPos
< 0) {
501 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
503 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
504 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
505 msg
.hwnd
== CommandEdit
)
508 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
509 CommandHistoryPos
= 0;
511 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
512 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
513 msg
.hwnd
== CommandEdit
)
515 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
517 if(msg
.message
== WM_KEYDOWN
) {
518 CommandHistoryPos
= -1;
520 TranslateMessage(&msg
);
521 DispatchMessage(&msg
);
528 if(ReceiveCommandPoll(&c
))
529 UsbCommandReceived(&c
);