]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/wingui.c
6c7ef52906c90b19aef7728c34aab923f93ffb32
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 //-----------------------------------------------------------------------------
20 sprintf(line, "Internal error at line %d file '%s'", __LINE__, \
22 MessageBox(NULL, line, "Error", MB_ICONERROR); \
26 void dbp(char *str
, ...)
31 vsprintf(buf
, str
, f
);
32 OutputDebugString(buf
);
33 OutputDebugString("\n");
36 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
38 int PlotGridX
, PlotGridY
;
40 HPEN GreyPenLite
, GreyPen
, GreenPen
, WhitePen
, YellowPen
;
41 HBRUSH GreenBrush
, YellowBrush
;
43 static int GraphStart
= 0;
44 static double GraphPixelsPerPoint
= 1;
46 static int CursorAPos
;
47 static int CursorBPos
;
48 double CursorScaleFactor
= 1.0;
49 static HPEN CursorAPen
;
50 static HPEN CursorBPen
;
52 static HWND CommandWindow
;
53 static HWND GraphWindow
;
54 static HWND ScrollbackEdit
;
55 static HWND CommandEdit
;
57 #define COMMAND_HISTORY_MAX 16
58 static char CommandHistory
[COMMAND_HISTORY_MAX
][256];
59 static int CommandHistoryPos
= -1;
60 static int CommandHistoryNext
;
62 static HFONT MyFixedFont
;
63 #define FixedFont(x) SendMessage((x), WM_SETFONT, (WPARAM)MyFixedFont, TRUE)
65 void ExecCmd(char *cmd
)
72 static void ResizeCommandWindow(void)
76 GetClientRect(CommandWindow
, &r
);
79 MoveWindow(ScrollbackEdit
, 10, 10, w
- 20, h
- 50, TRUE
);
80 MoveWindow(CommandEdit
, 10, h
- 29, w
- 20, 22, TRUE
);
83 void RepaintGraphWindow(void)
85 InvalidateRect(GraphWindow
, NULL
, TRUE
);
88 static LRESULT CALLBACK
89 CommandWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
98 ResizeCommandWindow();
102 SetFocus(CommandEdit
);
106 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
112 static void PaintGraph(HDC hdc
)
128 GetClientRect(GraphWindow
, &r
);
129 int zeroHeight
= (r
.top
+ r
.bottom
) >> 1;
131 // plot X and Y grid lines
132 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
133 for(i
= offset
; i
< r
.right
; i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
134 SelectObject(hdc
, GreyPenLite
);
135 MoveToEx(hdc
, r
.left
+ i
, r
.top
, NULL
);
136 LineTo(hdc
, r
.left
+ i
, r
.bottom
);
140 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
141 for(i
= 0; i
< ((r
.top
+ r
.bottom
)>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
142 SelectObject(hdc
, GreyPenLite
);
143 MoveToEx(hdc
, r
.left
, zeroHeight
+ i
, NULL
);
144 LineTo(hdc
, r
.right
, zeroHeight
+ i
);
145 MoveToEx(hdc
, r
.left
, zeroHeight
- i
, NULL
);
146 LineTo(hdc
, r
.right
, zeroHeight
- i
);
150 // print vertical separator white line on the left of the window
151 SelectObject(hdc
, WhitePen
);
152 MoveToEx(hdc
, r
.left
+ offset
, r
.top
, NULL
);
153 LineTo(hdc
, r
.left
+ offset
, r
.bottom
);
155 // print horizontal grey zero axis line
156 SelectObject(hdc
, GreyPen
);
157 MoveToEx(hdc
, r
.left
, zeroHeight
, NULL
);
158 LineTo(hdc
, r
.right
, zeroHeight
);
160 startMax
= (GraphTraceLen
- (int)((r
.right
- r
.left
- offset
) / GraphPixelsPerPoint
));
162 if(startMax
< 0) startMax
= 0;
163 if(GraphStart
> startMax
) GraphStart
= startMax
;
164 if(GraphStart
< 0) GraphStart
= 0;
167 SelectObject(hdc
, pen
);
169 // go over the portion of the graph to be displayed and find the largest
170 // absolute value which will be used to auto scale the graph when displayed
171 for(i
= GraphStart
; ; i
++) {
172 if(i
>= GraphTraceLen
) {
175 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
176 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
178 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
184 absYMax
= (int)(absYMax
*1.2 + 1);
185 SelectObject(hdc
, MyFixedFont
);
186 SetTextColor(hdc
, RGB(255, 255, 255));
187 SetBkColor(hdc
, RGB(0, 0, 0));
189 // number of points that will be plotted
190 double span
= (int)((r
.right
- r
.left
) / GraphPixelsPerPoint
);
192 // one label every offset pixels, let us say
193 int labels
= (r
.right
- r
.left
- offset
) / offset
;
194 if(labels
<= 0) labels
= 1;
195 // round to nearest power of 2
196 int pointsPerLabel
= (int)(log(span
/ labels
)/log(2.0));
197 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
198 pointsPerLabel
= (int)pow(2.0,pointsPerLabel
);
200 // go over the graph and plot samples and labels
201 for(i
= GraphStart
; ; i
++) {
202 if(i
>= GraphTraceLen
) {
205 int x
= offset
+ (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
206 if(x
> r
.right
+ GraphPixelsPerPoint
) {
210 int y
= GraphBuffer
[i
];
211 if(y
< yMin
) yMin
= y
;
212 if(y
> yMax
) yMax
= y
;
216 y
= (y
* (r
.top
- r
.bottom
) / (2*absYMax
)) + zeroHeight
;
217 if(i
== GraphStart
) {
218 MoveToEx(hdc
, x
, y
, NULL
);
223 if(GraphPixelsPerPoint
> 10) {
229 FillRect(hdc
, &f
, brush
);
233 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
234 SelectObject(hdc
, WhitePen
);
235 MoveToEx(hdc
, x
, zeroHeight
- 8, NULL
);
236 LineTo(hdc
, x
, zeroHeight
+ 8);
238 sprintf(str
, "+%d", i
);
240 GetTextExtentPoint32(hdc
, str
, strlen(str
), &size
);
241 TextOut(hdc
, x
- size
.cx
, zeroHeight
+ 8, str
, strlen(str
));
243 SelectObject(hdc
, pen
);
244 MoveToEx(hdc
, x
, y
, NULL
);
247 // plot measurement cursors
248 if(i
== CursorAPos
|| i
== CursorBPos
) {
249 if(i
== CursorAPos
) {
250 SelectObject(hdc
, CursorAPen
);
252 SelectObject(hdc
, CursorBPen
);
254 MoveToEx(hdc
, x
, r
.top
, NULL
);
255 LineTo(hdc
, x
, r
.bottom
);
257 SelectObject(hdc
, pen
);
258 MoveToEx(hdc
, x
, y
, NULL
);
266 // print misc information at bottom of graph window
267 sprintf(str
, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]",
268 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
269 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
, GraphPixelsPerPoint
,
270 CursorAPos
, GraphBuffer
[CursorAPos
], CursorBPos
, GraphBuffer
[CursorBPos
]);
271 TextOut(hdc
, 50, r
.bottom
- 20, str
, strlen(str
));
274 static LRESULT CALLBACK
275 GraphWindowProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
281 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
284 RepaintGraphWindow();
289 HDC hdc
= BeginPaint(hwnd
, &ps
);
293 // This draws the trace.
301 if(GraphPixelsPerPoint
<= 8) {
302 GraphPixelsPerPoint
*= 2;
307 if(GraphPixelsPerPoint
>= 0.01) {
308 GraphPixelsPerPoint
/= 2;
313 if(GraphPixelsPerPoint
< 16) {
314 GraphStart
+= (int)(16 / GraphPixelsPerPoint
);
321 if(GraphPixelsPerPoint
< 16) {
322 GraphStart
-= (int)(16 / GraphPixelsPerPoint
);
331 RepaintGraphWindow();
336 case WM_RBUTTONDOWN
: {
337 int x
= LOWORD(lParam
);
339 x
= (int)(x
/ GraphPixelsPerPoint
);
342 if(msg
== WM_LBUTTONDOWN
) {
347 RepaintGraphWindow();
351 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
357 void PrintToScrollback(char *fmt
, ...)
363 vsprintf(str
+2, fmt
, f
);
365 static char TextBuf
[1024*32];
366 SendMessage(ScrollbackEdit
, WM_GETTEXT
, (WPARAM
)sizeof(TextBuf
),
369 if(strlen(TextBuf
) + strlen(str
) + 1 <= sizeof(TextBuf
)) {
370 strcat(TextBuf
, str
);
372 lstrcpyn(TextBuf
, str
, sizeof(TextBuf
));
375 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)TextBuf
);
376 SendMessage(ScrollbackEdit
, EM_LINESCROLL
, 0, (LPARAM
)INT_MAX
);
379 void ShowGraphWindow(void)
381 if(GraphWindow
) return;
383 GraphWindow
= CreateWindowEx(0, "Graph", "graphed",
384 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
385 WS_SIZEBOX
| WS_VISIBLE
, 200, 150, 600, 500, NULL
, NULL
, NULL
,
387 if(!GraphWindow
) oops();
390 void HideGraphWindow(void)
393 DestroyWindow(GraphWindow
);
398 static void SetCommandEditTo(char *str
)
400 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)str
);
401 SendMessage(CommandEdit
, EM_SETSEL
, strlen(str
), strlen(str
));
407 memset(&wc
, 0, sizeof(wc
));
408 wc
.cbSize
= sizeof(wc
);
410 wc
.style
= CS_BYTEALIGNCLIENT
| CS_BYTEALIGNWINDOW
| CS_OWNDC
;
411 wc
.lpfnWndProc
= (WNDPROC
)CommandWindowProc
;
413 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNSHADOW
);
414 wc
.lpszClassName
= "Command";
415 wc
.lpszMenuName
= NULL
;
416 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
418 if(!RegisterClassEx(&wc
)) oops();
420 wc
.lpszClassName
= "Graph";
421 wc
.lpfnWndProc
= (WNDPROC
)GraphWindowProc
;
422 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
424 if(!RegisterClassEx(&wc
)) oops();
426 CommandWindow
= CreateWindowEx(0, "Command", "prox",
427 WS_OVERLAPPED
| WS_BORDER
| WS_MINIMIZEBOX
| WS_SYSMENU
|
428 WS_SIZEBOX
| WS_VISIBLE
, 20, 20, 500, 400, NULL
, NULL
, NULL
,
430 if(!CommandWindow
) oops();
432 ScrollbackEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
433 WS_CHILD
| WS_CLIPSIBLINGS
| WS_VISIBLE
| ES_MULTILINE
|
434 ES_AUTOVSCROLL
| WS_VSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
,
437 CommandEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, "edit", "",
438 WS_CHILD
| WS_CLIPSIBLINGS
| WS_TABSTOP
| WS_VISIBLE
|
439 ES_AUTOHSCROLL
, 0, 0, 0, 0, CommandWindow
, NULL
, NULL
, NULL
);
441 MyFixedFont
= CreateFont(14, 0, 0, 0, FW_REGULAR
, FALSE
, FALSE
, FALSE
,
442 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
443 FF_DONTCARE
, "Lucida Console");
445 MyFixedFont
= (HFONT
)GetStockObject(SYSTEM_FONT
);
447 FixedFont(ScrollbackEdit
);
448 FixedFont(CommandEdit
);
450 ResizeCommandWindow();
451 SetFocus(CommandEdit
);
453 PrintToScrollback(">> Started prox, built " __DATE__
" " __TIME__
);
454 PrintToScrollback(">> Connected to device");
456 GreyPenLite
= CreatePen(PS_SOLID
, 1, RGB(50, 50, 50));
457 GreyPen
= CreatePen(PS_SOLID
, 1, RGB(100, 100, 100));
458 GreenPen
= CreatePen(PS_SOLID
, 1, RGB(100, 255, 100));
459 YellowPen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 0));
460 GreenBrush
= CreateSolidBrush(RGB(100, 255, 100));
461 YellowBrush
= CreateSolidBrush(RGB(255, 255, 0));
462 WhitePen
= CreatePen(PS_SOLID
, 1, RGB(255, 255, 255));
464 CursorAPen
= CreatePen(PS_DASH
, 1, RGB(255, 255, 0));
465 CursorBPen
= CreatePen(PS_DASH
, 1, RGB(255, 0, 255));
469 if(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
470 if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_RETURN
) {
472 SendMessage(CommandEdit
, WM_GETTEXT
, (WPARAM
)sizeof(got
),
475 if(strcmp(got
, "cls")==0) {
476 SendMessage(ScrollbackEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
478 CommandReceived(got
);
480 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
482 // Insert it into the command history, unless it is
483 // identical to the previous command in the history.
484 int prev
= CommandHistoryNext
- 1;
485 if(prev
< 0) prev
+= COMMAND_HISTORY_MAX
;
486 if(strcmp(CommandHistory
[prev
], got
) != 0) {
487 strcpy(CommandHistory
[CommandHistoryNext
], got
);
488 CommandHistoryNext
++;
489 if(CommandHistoryNext
== COMMAND_HISTORY_MAX
) {
490 CommandHistoryNext
= 0;
493 CommandHistoryPos
= -1;
494 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_UP
&&
495 msg
.hwnd
== CommandEdit
)
497 if(CommandHistoryPos
== -1) {
498 CommandHistoryPos
= CommandHistoryNext
;
501 if(CommandHistoryPos
< 0) {
502 CommandHistoryPos
= COMMAND_HISTORY_MAX
-1;
504 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
505 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_DOWN
&&
506 msg
.hwnd
== CommandEdit
)
509 if(CommandHistoryPos
>= COMMAND_HISTORY_MAX
) {
510 CommandHistoryPos
= 0;
512 SetCommandEditTo(CommandHistory
[CommandHistoryPos
]);
513 } else if(msg
.message
== WM_KEYDOWN
&& msg
.wParam
== VK_ESCAPE
&&
514 msg
.hwnd
== CommandEdit
)
516 SendMessage(CommandEdit
, WM_SETTEXT
, 0, (LPARAM
)"");
518 if(msg
.message
== WM_KEYDOWN
) {
519 CommandHistoryPos
= -1;
521 TranslateMessage(&msg
);
522 DispatchMessage(&msg
);
529 if(ReceiveCommandPoll(&c
))
530 UsbCommandReceived(&c
);