]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - winsrc/gui.cpp
3753a86e99a4a47aeb2f948ce2a65a10c1598de6
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 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
)
126 GetClientRect(GraphWindow
, &r
);
127 int zeroHeight
= (r
.top
+ r
.bottom
) >> 1;
129 // plot X and Y grid lines
130 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
131 for(i
= offset
; i
< r
.right
; i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
132 SelectObject(hdc
, GreyPenLite
);
133 MoveToEx(hdc
, r
.left
+ i
, r
.top
, NULL
);
134 LineTo(hdc
, r
.left
+ i
, r
.bottom
);
138 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
139 for(i
= 0; i
< ((r
.top
+ r
.bottom
)>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
140 SelectObject(hdc
, GreyPenLite
);
141 MoveToEx(hdc
, r
.left
, zeroHeight
+ i
, NULL
);
142 LineTo(hdc
, r
.right
, zeroHeight
+ i
);
143 MoveToEx(hdc
, r
.left
, zeroHeight
- i
, NULL
);
144 LineTo(hdc
, r
.right
, zeroHeight
- i
);
148 // print vertical separator white line on the left of the window
149 SelectObject(hdc
, WhitePen
);
150 MoveToEx(hdc
, r
.left
+ offset
, r
.top
, NULL
);
151 LineTo(hdc
, r
.left
+ offset
, r
.bottom
);
153 // print horizontal grey zero axis line
154 SelectObject(hdc
, GreyPen
);
155 MoveToEx(hdc
, r
.left
, zeroHeight
, NULL
);
156 LineTo(hdc
, r
.right
, zeroHeight
);
158 startMax
= (GraphTraceLen
- (int)((r
.right
- r
.left
- offset
) / GraphPixelsPerPoint
));
160 if(startMax
< 0) startMax
= 0;
161 if(GraphStart
> startMax
) GraphStart
= startMax
;
162 if(GraphStart
< 0) GraphStart
= 0;
165 SelectObject(hdc
, pen
);
167 // go over the portion of the graph to be displayed and find the largest
168 // absolute value which will be used to auto scale the graph when displayed
169 for(i
= GraphStart
; ; i
++) {
170 if(i
>= GraphTraceLen
) {
173 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
174 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
176 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
182 absYMax
= (int)(absYMax
*1.2 + 1);
183 SelectObject(hdc
, MyFixedFont
);
184 SetTextColor(hdc
, RGB(255, 255, 255));
185 SetBkColor(hdc
, RGB(0, 0, 0));
187 // number of points that will be plotted
188 double span
= (int)((r
.right
- r
.left
) / GraphPixelsPerPoint
);
190 // one label every offset pixels, let us say
191 int labels
= (r
.right
- r
.left
- offset
) / offset
;
192 if(labels
<= 0) labels
= 1;
193 // round to nearest power of 2
194 int pointsPerLabel
= (int)(log(span
/ labels
)/log(2.0));
195 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
196 pointsPerLabel
= (int)pow(2.0,pointsPerLabel
);
198 // go over the graph and plot samples and labels
199 for(i
= GraphStart
; ; i
++) {
200 if(i
>= GraphTraceLen
) {
203 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
204 if(x
> r
.right
+ GraphPixelsPerPoint
) {
208 int y
= GraphBuffer
[i
];
209 if(y
< yMin
) yMin
= y
;
210 if(y
> yMax
) yMax
= y
;
214 y
= (y
* (r
.top
- r
.bottom
) / (2*absYMax
)) + zeroHeight
;
215 if(i
== GraphStart
) {
216 MoveToEx(hdc
, x
, y
, NULL
);
221 if(GraphPixelsPerPoint
> 10) {
227 FillRect(hdc
, &f
, brush
);
231 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
232 SelectObject(hdc
, WhitePen
);
233 MoveToEx(hdc
, x
, zeroHeight
- 8, NULL
);
234 LineTo(hdc
, x
, zeroHeight
+ 8);
236 sprintf(str
, "+%d", i
);
238 GetTextExtentPoint32(hdc
, str
, strlen(str
), &size
);
239 TextOut(hdc
, x
- size
.cx
, zeroHeight
+ 8, str
, strlen(str
));
241 SelectObject(hdc
, pen
);
242 MoveToEx(hdc
, x
, y
, NULL
);
245 // plot measurement cursors
246 if(i
== CursorAPos
|| i
== CursorBPos
) {
247 if(i
== CursorAPos
) {
248 SelectObject(hdc
, CursorAPen
);
250 SelectObject(hdc
, CursorBPen
);
252 MoveToEx(hdc
, x
, r
.top
, NULL
);
253 LineTo(hdc
, x
, r
.bottom
);
255 SelectObject(hdc
, pen
);
256 MoveToEx(hdc
, x
, y
, NULL
);
264 // print misc information at bottom of graph window
265 sprintf(str
, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]",
266 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
267 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
, GraphPixelsPerPoint
,
268 CursorAPos
, GraphBuffer
[CursorAPos
], CursorBPos
, GraphBuffer
[CursorBPos
]);
269 TextOut(hdc
, 50, r
.bottom
- 20, str
, strlen(str
));
272 static LRESULT CALLBACK
273 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
279 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
282 RepaintGraphWindow();
287 HDC hdc
= BeginPaint(hwnd
, &ps
);
291 // This draws the trace.
299 if(GraphPixelsPerPoint
<= 8) {
300 GraphPixelsPerPoint
*= 2;
305 if(GraphPixelsPerPoint
>= 0.01) {
306 GraphPixelsPerPoint
/= 2;
311 if(GraphPixelsPerPoint
< 16) {
312 GraphStart
+= (int)(16 / GraphPixelsPerPoint
);
319 if(GraphPixelsPerPoint
< 16) {
320 GraphStart
-= (int)(16 / GraphPixelsPerPoint
);
329 RepaintGraphWindow();
334 case WM_RBUTTONDOWN
: {
335 int x
= LOWORD(lParam
);
337 x
= (int)(x
/ GraphPixelsPerPoint
);
340 if(msg
== WM_LBUTTONDOWN
) {
345 RepaintGraphWindow();
349 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
355 void PrintToScrollback(char *fmt
, ...)
361 vsprintf(str
+2, fmt
, f
);
363 static char TextBuf
[1024*32];
364 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
367 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
368 strcat(TextBuf
, str
);
370 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
373 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
374 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
377 void ShowGraphWindow(void)
379 if(GraphWindow
) return;
381 GraphWindow
= CreateWindowEx(0, "Graph", "graphed",
382 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
383 WS_SIZEBOX
| WS_VISIBLE
, 200, 150, 600, 500, NULL
, NULL
, NULL
,
385 if(!GraphWindow
) oops();
388 void HideGraphWindow(void)
391 DestroyWindow(GraphWindow
);
396 static void SetCommandEditTo(char *str
)
398 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
399 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
405 memset(&wc
, 0, sizeof(wc
));
406 wc
.cbSize
= sizeof(wc
);
408 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
409 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
411 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
412 wc
.lpszClassName
= "Command";
413 wc
.lpszMenuName
= NULL
;
414 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
416 if(!RegisterClassEx(&wc
)) oops();
418 wc
.lpszClassName
= "Graph";
419 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
420 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
422 if(!RegisterClassEx(&wc
)) oops();
424 CommandWindow
= CreateWindowEx(0, "Command", "prox",
425 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
426 WS_SIZEBOX
| WS_VISIBLE
, 20, 20, 500, 400, NULL
, NULL
, NULL
,
428 if(!CommandWindow
) oops();
430 ScrollbackEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
431 WS_CHILD
| WS_CLIPSIBLINGS
| WS_VISIBLE
| ES_MULTILINE
|
432 ES_AUTOVSCROLL
| WS_VSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
,
435 CommandEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
436 WS_CHILD
| WS_CLIPSIBLINGS
| WS_TABSTOP
| WS_VISIBLE
|
437 ES_AUTOHSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
, NULL
, NULL
);
439 MyFixedFont
= CreateFont(14, 0, 0, 0, FW_REGULAR
, FALSE
, FALSE
, FALSE
,
440 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
441 FF_DONTCARE
, "Lucida Console");
443 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
445 FixedFont(ScrollbackEdit
);
446 FixedFont(CommandEdit
);
448 ResizeCommandWindow();
449 SetFocus(CommandEdit
);
451 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
452 PrintToScrollback(">> Connected to device");
454 GreyPenLite
= CreatePen(PS_SOLID
, 1, RGB(50, 50, 50));
455 GreyPen
= CreatePen(PS_SOLID
, 1, RGB(100, 100, 100));
456 GreenPen
= CreatePen(PS_SOLID
, 1, RGB(100, 255, 100));
457 YellowPen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 0));
458 GreenBrush
= CreateSolidBrush(RGB(100, 255, 100));
459 YellowBrush
= CreateSolidBrush(RGB(255, 255, 0));
460 WhitePen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 255));
462 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
463 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
467 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
468 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
470 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
473 if(strcmp(got
, "cls")==0) {
474 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
476 CommandReceived(got
);
478 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
480 // Insert it into the command history, unless it is
481 // identical to the previous command in the history.
482 int prev
= CommandHistoryNext
- 1;
483 if(prev
< 0) prev
+= COMMAND_HISTORY_MAX
;
484 if(strcmp(CommandHistory
[prev
], got
) != 0) {
485 strcpy(CommandHistory
[CommandHistoryNext
], got
);
486 CommandHistoryNext
++;
487 if(CommandHistoryNext
== COMMAND_HISTORY_MAX
) {
488 CommandHistoryNext
= 0;
491 CommandHistoryPos
= -1;
492 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
493 msg
.hwnd
== CommandEdit
)
495 if(CommandHistoryPos
== -1) {
496 CommandHistoryPos
= CommandHistoryNext
;
499 if(CommandHistoryPos
< 0) {
500 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
502 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
503 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
504 msg
.hwnd
== CommandEdit
)
507 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
508 CommandHistoryPos
= 0;
510 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
511 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
512 msg
.hwnd
== CommandEdit
)
514 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
516 if(msg
.message
== WM_KEYDOWN
) {
517 CommandHistoryPos
= -1;
519 TranslateMessage(&msg
);
520 DispatchMessage(&msg
);
527 if(ReceiveCommandPoll(&c
))
528 UsbCommandReceived(&c
);