Limit the frame rate

This commit is contained in:
Keith Winstein
2011-02-05 06:23:47 -05:00
parent eb9af5ebe7
commit 9b02204d72
+31 -6
View File
@@ -19,6 +19,7 @@
#include <termios.h> #include <termios.h>
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
#include <sys/time.h>
#include "parser.hpp" #include "parser.hpp"
#include "terminal.hpp" #include "terminal.hpp"
@@ -134,6 +135,29 @@ int main( int argc, char *argv[] )
return 0; return 0;
} }
void tick( Terminal::Emulator *e )
{
static bool initialized = false;
static struct timeval last_time;
struct timeval this_time;
if ( gettimeofday( &this_time, NULL ) < 0 ) {
perror( "gettimeofday" );
}
int diff = 1000000 * (this_time.tv_sec - last_time.tv_sec)
+ (this_time.tv_usec - last_time.tv_usec);
if ( (!initialized)
|| (diff >= 20000) ) {
std::string update = e->new_frame();
swrite( STDOUT_FILENO, update.c_str() );
initialized = true;
last_time = this_time;
}
}
void emulate_terminal( int fd, int debug_fd ) void emulate_terminal( int fd, int debug_fd )
{ {
/* establish WINCH fd and start listening for signal */ /* establish WINCH fd and start listening for signal */
@@ -181,8 +205,8 @@ void emulate_terminal( int fd, int debug_fd )
swrite( STDOUT_FILENO, terminal.open().c_str() ); swrite( STDOUT_FILENO, terminal.open().c_str() );
while ( 1 ) { while ( 1 ) {
int active_fds = poll( pollfds, 3, -1 ); int active_fds = poll( pollfds, 3, 0.02 );
if ( active_fds <= 0 ) { if ( active_fds < 0 ) {
perror( "poll" ); perror( "poll" );
break; break;
} }
@@ -195,8 +219,6 @@ void emulate_terminal( int fd, int debug_fd )
if ( termemu( fd, fd, false, debug_fd, &parser, &terminal ) < 0 ) { if ( termemu( fd, fd, false, debug_fd, &parser, &terminal ) < 0 ) {
break; break;
} }
std::string update = terminal.new_frame();
swrite( STDOUT_FILENO, update.c_str() );
} else if ( pollfds[ 2 ].revents & POLLIN ) { } else if ( pollfds[ 2 ].revents & POLLIN ) {
/* resize */ /* resize */
struct signalfd_siginfo info; struct signalfd_siginfo info;
@@ -221,11 +243,14 @@ void emulate_terminal( int fd, int debug_fd )
} else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents) } else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents)
& (POLLERR | POLLHUP | POLLNVAL) ) { & (POLLERR | POLLHUP | POLLNVAL) ) {
break; break;
} else {
fprintf( stderr, "poll mysteriously woken up\n" );
} }
tick( &terminal );
} }
std::string update = terminal.new_frame();
swrite( STDOUT_FILENO, update.c_str() );
swrite( STDOUT_FILENO, terminal.close().c_str() ); swrite( STDOUT_FILENO, terminal.close().c_str() );
} }