Commit 4876da8b authored by Alessandro Rubini's avatar Alessandro Rubini

userspace/tools/wr_mon: several changes

This fixes several minor issues with wr_mon. It changes the term.c interface,
bur wr_mon is the only user of it and term.h.

  - term: support b/w mode (no colors)
  - term: simplify termios setting, and don't do it on non-tty files
  - term: term_poll now does poll, so user input is handled quickly
  - term: exit on ctrl-C
  - term: don't fflush() on every cprintf
  - wr_mon: accept "-b" for "black and white"
  - wr_mon: fix buffering of stdout, fflush the whole screenshot
      (this results in much less flashing in the terminal window)
  - wr_mon: use \n where possible, not calculated positions
      (this simplified future addition of dynamic interface listings)
  - wr_mon: shrink output vertically, so in no-servo mode it fits 80x25
  - wr_mon: use term_poll, not explicit sleep
  - wr_mon: don't clear the screen at exit time
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent ea50dbec
......@@ -9,37 +9,38 @@
#include "term.h"
struct termios oldkey, newkey;
static struct termios oldkey, newkey;
static int term_usecolor;
void term_restore(void)
{
tcsetattr(STDIN_FILENO,TCSANOW,&oldkey);
}
void term_init(void)
void term_init(int usecolor)
{
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
term_usecolor = usecolor;
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
return;
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
tcgetattr(STDIN_FILENO,&oldkey);
memcpy(&newkey, &oldkey, sizeof(struct termios));
newkey.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
newkey.c_iflag = IGNPAR;
newkey.c_oflag = OPOST |ONLCR;
newkey.c_lflag = 0;
newkey.c_cc[VMIN]=1;
newkey.c_cc[VTIME]=0;
newkey = oldkey;
cfmakeraw(&newkey);
newkey.c_oflag |= OPOST; /* don't require \r in source files */
tcflush(STDIN_FILENO, TCIFLUSH);
tcsetattr(STDIN_FILENO,TCSANOW,&newkey);
atexit(term_restore);
}
int term_poll(void)
int term_poll(int msec_timeout)
{
struct pollfd pfd;
pfd.fd = STDIN_FILENO;
pfd.events = POLLIN | POLLPRI;
if(poll(&pfd,1,0)>0)return 1;
if(poll(&pfd, 1, msec_timeout) > 0) return 1;
return 0;
}
......@@ -55,29 +56,30 @@ int term_get(void)
q=c;
} else q=-1;
if (c == 3) /* ctrl-C */
exit(0);
return q;
}
void term_cprintf(int color, const char *fmt, ...)
{
va_list ap;
printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f);
// printf("\033[01;%dm",color);
if (term_usecolor)
printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fflush(stdout);
}
void term_pcprintf(int row, int col, int color, const char *fmt, ...)
{
va_list ap;
printf("\033[%d;%df", row, col);
printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f);
if (term_usecolor)
printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fflush(stdout);
}
void term_clear(void)
......
......@@ -11,8 +11,8 @@
#define C_BLUE 4
void term_restore(void);
void term_init(void);
int term_poll(void);
void term_init(int usecolor);
int term_poll(int msec_timeout);
int term_get(void);
void term_cprintf(int color, const char *fmt, ...);
void term_pcprintf(int row, int col, int color, const char *fmt, ...);
......
......@@ -14,7 +14,7 @@ hexp_port_list_t port_list;
static struct minipc_ch *ptp_ch;
void init()
void init(int usecolor)
{
halexp_client_init();
......@@ -27,14 +27,21 @@ void init()
}
term_init();
term_init(usecolor);
halexp_query_ports(&port_list);
}
void show_ports()
{
int i, j;
term_pcprintf(3, 1, C_BLUE, "Switch ports:");
time_t t;
struct tm *tm;
char datestr[32];
time(&t);
tm = localtime(&t);
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", tm);
term_pcprintf(3, 1, C_BLUE, "Switch ports at %s\n", datestr);
for(i=0; i<18;i++)
{
......@@ -50,7 +57,7 @@ void show_ports()
halexp_get_port_state(&state, if_name);
term_pcprintf(5+i, 1, C_WHITE, "%05s: ", if_name);
term_cprintf(C_WHITE, " %05s: ", if_name);
if(state.up)
term_cprintf(C_GREEN, "Link up ");
else
......@@ -74,9 +81,9 @@ void show_ports()
term_cprintf(C_RED, "NoLock ");
if(state.rx_calibrated && state.tx_calibrated)
term_cprintf(C_GREEN, "Calibrated ");
term_cprintf(C_GREEN, "Calibrated \n");
else
term_cprintf(C_RED, "Uncalibrated ");
term_cprintf(C_RED, "Uncalibrated \n");
}
}
......@@ -86,11 +93,11 @@ void show_servo()
minipc_call(ptp_ch, 2000, &__rpcdef_get_sync_state, &ss);
term_cprintf(C_BLUE, "\n\nSynchronization status:\n\n");
term_cprintf(C_BLUE, "Synchronization status:\n");
if(!ss.valid)
{
term_cprintf(C_RED, "Master mode or sync info not valid\n\n");
term_cprintf(C_RED, "Master mode or sync info not valid\n");
return;
}
......@@ -156,15 +163,21 @@ void show_screen()
show_ports();
show_servo();
fflush(stdout);
}
int main()
int main(int argc, char **argv)
{
init();
int usecolor = 1;
if (argc > 1 && !strcmp(argv[1], "-b"))
usecolor = 0;
init(usecolor);
setvbuf(stdout, NULL, _IOFBF, 4096);
for(;;)
{
if(term_poll())
if(term_poll(500))
{
int c = term_get();
......@@ -179,13 +192,10 @@ int main()
track_onoff);
}
}
show_screen();
usleep(500000);
}
term_cprintf(C_GREY,"bye...\n\n");
term_clear();
term_restore();
setlinebuf(stdout);
printf("\n");
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment