]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/ui.c
Merge pull request #658 from grauerfuchs/master
[proxmark3-svn] / client / ui.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
a553f267 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
3775e9e8
MF
12#include <stdbool.h>
13#ifndef EXTERNAL_PRINTANDLOG
51969283 14#include <stdlib.h>
7fe9b0b7 15#include <stdio.h>
3775e9e8 16#include <stdarg.h>
51969283 17#include <readline/readline.h>
9492e0b0 18#include <pthread.h>
3775e9e8 19#endif
7fe9b0b7 20
21#include "ui.h"
22
3fe71039 23double CursorScaleFactor = 1;
b8fdac9e 24int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0;
61aaee35 25bool flushAfterWrite = false; //buzzy
0f321d63 26int GridOffset = 0;
3fe71039 27bool GridLocked = false;
b8fdac9e 28bool showDemod = true;
0f321d63 29
7fe9b0b7 30static char *logfilename = "proxmark3.log";
31
3775e9e8 32#ifndef EXTERNAL_PRINTANDLOG
f5ecd97b 33static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
3775e9e8 34
7fe9b0b7 35void PrintAndLog(char *fmt, ...)
36{
51969283
M
37 char *saved_line;
38 int saved_point;
9492e0b0 39 va_list argptr, argptr2;
40 static FILE *logfile = NULL;
41 static int logging=1;
7fe9b0b7 42
acf0582d 43 // lock this section to avoid interlacing prints from different threads
9492e0b0 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 }
ed50f7f3 53
a5a83016
MF
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.
ed50f7f3 57#ifdef RL_STATE_READCMD
a5a83016 58 // We are using GNU readline. libedit (OSX) doesn't support this flag.
51969283 59 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 60
51969283
M
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 }
ed50f7f3 68#endif
51969283 69
9492e0b0 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");
51969283 76
a5a83016
MF
77#ifdef RL_STATE_READCMD
78 // We are using GNU readline. libedit (OSX) doesn't support this flag.
51969283
M
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 }
a5a83016 86#endif
51969283 87
9492e0b0 88 if (logging && logfile) {
89 vfprintf(logfile, fmt, argptr2);
90 fprintf(logfile,"\n");
91 fflush(logfile);
92 }
93 va_end(argptr2);
94
61aaee35 95 if (flushAfterWrite) //buzzy
ed77aabe 96 {
97 fflush(NULL);
98 }
9492e0b0 99 //release lock
100 pthread_mutex_unlock(&print_lock);
7fe9b0b7 101}
3775e9e8 102#endif
9492e0b0 103
7fe9b0b7 104void SetLogFilename(char *fn)
105{
106 logfilename = fn;
107}
61aaee35 108
109void SetFlushAfterWrite(bool flush_after_write) {
110 flushAfterWrite = flush_after_write;
111}
112
Impressum, Datenschutz