]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
3 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
4 | // | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // UI utilities | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | #include <stdbool.h> | |
13 | #ifndef EXTERNAL_PRINTANDLOG | |
14 | #include <stdlib.h> | |
15 | #include <stdio.h> | |
16 | #include <stdarg.h> | |
17 | #include <readline/readline.h> | |
18 | #include <pthread.h> | |
19 | #endif | |
20 | ||
21 | #include "ui.h" | |
22 | ||
23 | double CursorScaleFactor = 1; | |
24 | int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0; | |
25 | bool flushAfterWrite = false; //buzzy | |
26 | int GridOffset = 0; | |
27 | bool GridLocked = false; | |
28 | bool showDemod = true; | |
29 | ||
30 | static char *logfilename = "proxmark3.log"; | |
31 | ||
32 | #ifndef EXTERNAL_PRINTANDLOG | |
33 | static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER; | |
34 | ||
35 | void PrintAndLog(char *fmt, ...) | |
36 | { | |
37 | char *saved_line; | |
38 | int saved_point; | |
39 | va_list argptr, argptr2; | |
40 | static FILE *logfile = NULL; | |
41 | static int logging=1; | |
42 | ||
43 | // lock this section to avoid interlacing prints from different threads | |
44 | pthread_mutex_lock(&print_lock); | |
45 | ||
46 | if (logging && !logfile) { | |
47 | logfile=fopen(logfilename, "a"); | |
48 | if (!logfile) { | |
49 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
50 | logging=0; | |
51 | } | |
52 | } | |
53 | ||
54 | // If there is an incoming message from the hardware (eg: lf hid read) in | |
55 | // the background (while the prompt is displayed and accepting user input), | |
56 | // stash the prompt and bring it back later. | |
57 | #ifdef RL_STATE_READCMD | |
58 | // We are using GNU readline. libedit (OSX) doesn't support this flag. | |
59 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
60 | ||
61 | if (need_hack) { | |
62 | saved_point = rl_point; | |
63 | saved_line = rl_copy_text(0, rl_end); | |
64 | rl_save_prompt(); | |
65 | rl_replace_line("", 0); | |
66 | rl_redisplay(); | |
67 | } | |
68 | #endif | |
69 | ||
70 | va_start(argptr, fmt); | |
71 | va_copy(argptr2, argptr); | |
72 | vprintf(fmt, argptr); | |
73 | printf(" "); // cleaning prompt | |
74 | va_end(argptr); | |
75 | printf("\n"); | |
76 | ||
77 | #ifdef RL_STATE_READCMD | |
78 | // We are using GNU readline. libedit (OSX) doesn't support this flag. | |
79 | if (need_hack) { | |
80 | rl_restore_prompt(); | |
81 | rl_replace_line(saved_line, 0); | |
82 | rl_point = saved_point; | |
83 | rl_redisplay(); | |
84 | free(saved_line); | |
85 | } | |
86 | #endif | |
87 | ||
88 | if (logging && logfile) { | |
89 | vfprintf(logfile, fmt, argptr2); | |
90 | fprintf(logfile,"\n"); | |
91 | fflush(logfile); | |
92 | } | |
93 | va_end(argptr2); | |
94 | ||
95 | if (flushAfterWrite) //buzzy | |
96 | { | |
97 | fflush(NULL); | |
98 | } | |
99 | //release lock | |
100 | pthread_mutex_unlock(&print_lock); | |
101 | } | |
102 | #endif | |
103 | ||
104 | void SetLogFilename(char *fn) | |
105 | { | |
106 | logfilename = fn; | |
107 | } | |
108 | ||
109 | void SetFlushAfterWrite(bool flush_after_write) { | |
110 | flushAfterWrite = flush_after_write; | |
111 | } | |
112 |