From 1ee54cd70a92d083d61aa661585ef124a047ae3d Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Sun, 6 Feb 2011 14:43:47 -0500 Subject: [PATCH] Remove some debugging routines and add comments --- termemu.cpp | 55 +++++++++++++++++------------------- terminal.cpp | 69 --------------------------------------------- terminal.hpp | 2 -- terminaldisplay.cpp | 2 ++ 4 files changed, 28 insertions(+), 100 deletions(-) diff --git a/termemu.cpp b/termemu.cpp index 64e537b..4be7152 100644 --- a/termemu.cpp +++ b/termemu.cpp @@ -27,28 +27,14 @@ const size_t buf_size = 16384; -void emulate_terminal( int fd, int debug_fd ); +void emulate_terminal( int fd ); int copy( int src, int dest ); -int termemu( int host_fd, int src_fd, bool user, int debug_fd, +int termemu( int host_fd, int src_fd, bool user, Parser::UTF8Parser *parser, Terminal::Emulator *terminal ); -int main( int argc, char *argv[] ) +int main( void ) { - int debug_fd; - if ( argc == 1 ) { - debug_fd = -1; - } else if ( argc == 2 ) { - debug_fd = open( argv[ 1 ], O_WRONLY ); - if ( debug_fd < 0 ) { - perror( "open" ); - exit( 1 ); - } - } else { - fprintf( stderr, "Usage: %s [debugfd]\n", argv[ 0 ] ); - exit( 1 ); - } - int master; struct termios saved_termios, raw_termios, child_termios; @@ -122,7 +108,7 @@ int main( int argc, char *argv[] ) exit( 1 ); } - emulate_terminal( master, debug_fd ); + emulate_terminal( master ); if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) { perror( "tcsetattr" ); @@ -135,6 +121,7 @@ int main( int argc, char *argv[] ) return 0; } +/* Print a frame if the last frame was more than 1/50 seconds ago */ bool tick( Terminal::Emulator *e ) { static bool initialized = false; @@ -162,7 +149,24 @@ bool tick( Terminal::Emulator *e ) return false; } -void emulate_terminal( int fd, int debug_fd ) +/* This is the main loop. + + 1. New bytes from the user get applied to the terminal emulator + as "UserByte" actions. + + 2. New bytes from the host get sent to the Parser, and then + those actions are applied to the terminal. + + 3. Resize events (from a SIGWINCH signal) get turned into + "Resize" actions and applied to the terminal. + + At every event from poll(), we run the tick() function to + possibly print a new frame (if we haven't printed one in the + last 1/50 second). The new frames are "differential" -- they + assume the previous frame was sent to the real terminal. +*/ + +void emulate_terminal( int fd ) { /* establish WINCH fd and start listening for signal */ sigset_t signal_mask; @@ -218,11 +222,11 @@ void emulate_terminal( int fd, int debug_fd ) } if ( pollfds[ 0 ].revents & POLLIN ) { - if ( termemu( fd, STDIN_FILENO, true, debug_fd, &parser, &terminal ) < 0 ) { + if ( termemu( fd, STDIN_FILENO, true, &parser, &terminal ) < 0 ) { break; } } else if ( pollfds[ 1 ].revents & POLLIN ) { - if ( termemu( fd, fd, false, debug_fd, &parser, &terminal ) < 0 ) { + if ( termemu( fd, fd, false, &parser, &terminal ) < 0 ) { break; } } else if ( pollfds[ 2 ].revents & POLLIN ) { @@ -264,7 +268,7 @@ void emulate_terminal( int fd, int debug_fd ) swrite( STDOUT_FILENO, terminal.close().c_str() ); } -int termemu( int host_fd, int src_fd, bool user, int debug_fd, +int termemu( int host_fd, int src_fd, bool user, Parser::UTF8Parser *parser, Terminal::Emulator *terminal ) { @@ -296,13 +300,6 @@ int termemu( int host_fd, int src_fd, bool user, int debug_fd, /* apply action to terminal */ act->act_on_terminal( terminal ); - /* print out action for debugging */ - if ( (debug_fd > 0) && (!act->handled) ) { - char actsum[ 64 ]; - snprintf( actsum, 64, "%s ", act->str().c_str() ); - swrite( debug_fd, actsum ); - } - delete *i; } } diff --git a/terminal.cpp b/terminal.cpp index 1b07660..4054602 100644 --- a/terminal.cpp +++ b/terminal.cpp @@ -121,75 +121,6 @@ void Emulator::Esc_dispatch( Parser::Esc_Dispatch *act ) } } -void Emulator::debug_printout( int fd ) -{ - fb.back_color_erase(); - - std::string screen; - - /* set window title */ - screen.append( "\033]0;[rtm] " ); - std::vector window_title = fb.get_window_title(); - for ( std::vector::iterator i = window_title.begin(); - i != window_title.end(); - i++ ) { - char utf8[ 8 ]; - snprintf( utf8, 8, "%lc", *i ); - screen.append( utf8 ); - } - screen.append( "\033\\" ); - - /* set reverse video */ - char rev[ 8 ]; - snprintf( rev, 8, "\033[?5%c", (fb.ds.reverse_video ? 'h' : 'l') ); - screen.append( rev ); - - for ( int y = 0; y < fb.ds.get_height(); y++ ) { - for ( int x = 0; x < fb.ds.get_width(); /* let charwidth handle advance */ ) { - char curmove[ 32 ]; - snprintf( curmove, 32, "\033[%d;%dH", y + 1, x + 1 ); - screen.append( curmove ); - Cell *cell = fb.get_cell( y, x ); - - /* print renditions */ - screen.append( cell->renditions.sgr() ); - - /* clear cell */ - screen.append( "\033[X" ); - - /* print cell contents */ - - /* cells that begin with combining character get combiner attached to no-break space */ - if ( cell->fallback ) { - char utf8[ 8 ]; - snprintf( utf8, 8, "%lc", 0xA0 ); - screen.append( utf8 ); - } - - for ( std::vector::iterator i = cell->contents.begin(); - i != cell->contents.end(); - i++ ) { - char utf8[ 8 ]; - snprintf( utf8, 8, "%lc", *i ); - screen.append( utf8 ); - } - - x += cell->width; - } - } - - char curmove[ 32 ]; - if ( fb.ds.cursor_visible ) { - snprintf( curmove, 32, "\033[?25h\033[%d;%dH", fb.ds.get_cursor_row() + 1, - fb.ds.get_cursor_col() + 1 ); - } else { - snprintf( curmove, 32, "\033[?25l" ); - } - screen.append( curmove ); - - swrite( fd, screen.c_str() ); -} - std::string Emulator::open( void ) { char appmode[ 6 ] = { 0x1b, '[', '?', '1', 'h', 0 }; diff --git a/terminal.hpp b/terminal.hpp index 744be1f..e892f13 100644 --- a/terminal.hpp +++ b/terminal.hpp @@ -62,8 +62,6 @@ namespace Terminal { std::string read_octets_to_host( void ); - void debug_printout( int fd ); - std::string open( void ); /* put user cursor keys in application mode */ std::string close( void ); /* restore user cursor keys */ diff --git a/terminaldisplay.cpp b/terminaldisplay.cpp index 8209861..e7abf41 100644 --- a/terminaldisplay.cpp +++ b/terminaldisplay.cpp @@ -2,6 +2,8 @@ using namespace Terminal; +/* Print a new "frame" to the terminal, using ANSI/ECMA-48 escape codes. */ + std::string Display::new_frame( Framebuffer &f ) { f.back_color_erase();