diff options
author | 2012-04-30 23:32:13 -0400 | |
---|---|---|
committer | 2012-05-16 00:00:27 -0400 | |
commit | 043f9af260f87d5ffbff0dd1e139f4a146b98b23 (patch) | |
tree | d908bf85787eb499f16d0bb65601558177ff1700 /src/examples/termemu.cc | |
parent | 2112a3865c97f76c6e8bcbe38a9321a254b81829 (diff) |
Use Select in examples
Diffstat (limited to 'src/examples/termemu.cc')
-rw-r--r-- | src/examples/termemu.cc | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/src/examples/termemu.cc b/src/examples/termemu.cc index 8d3296c..55bf883 100644 --- a/src/examples/termemu.cc +++ b/src/examples/termemu.cc @@ -23,7 +23,6 @@ #include <stdlib.h> #include <signal.h> #include <errno.h> -#include <sys/poll.h> #include <string.h> #include <locale.h> #include <wchar.h> @@ -51,6 +50,7 @@ #include "fatal_assert.h" #include "locale_utils.h" #include "sigfd.h" +#include "select.h" /* For newer skalibs */ extern "C" { @@ -190,7 +190,7 @@ bool tick( Terminal::Framebuffer &state, Terminal::Framebuffer &new_frame, 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 + At every event from select(), 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. @@ -227,36 +227,30 @@ void emulate_terminal( int fd ) /* open display */ Terminal::Display display( true ); /* use TERM to initialize */ - struct pollfd pollfds[ 3 ]; - - pollfds[ 0 ].fd = STDIN_FILENO; - pollfds[ 0 ].events = POLLIN; - - pollfds[ 1 ].fd = fd; - pollfds[ 1 ].events = POLLIN; - - pollfds[ 2 ].fd = signal_fd; - pollfds[ 2 ].events = POLLIN; + Select sel; + sel.add_fd( STDIN_FILENO ); + sel.add_fd( fd ); + sel.add_fd( signal_fd ); swrite( STDOUT_FILENO, Terminal::Emulator::open().c_str() ); - int poll_timeout = -1; + int timeout = -1; while ( 1 ) { - int active_fds = poll( pollfds, 3, poll_timeout ); + int active_fds = sel.select( timeout ); if ( active_fds < 0 && errno == EINTR ) { continue; } else if ( active_fds < 0 ) { - perror( "poll" ); + perror( "select" ); break; } - if ( pollfds[ 0 ].revents & POLLIN ) { + if ( sel.read( STDIN_FILENO ) ) { /* input from user */ char buf[ buf_size ]; /* fill buffer if possible */ - ssize_t bytes_read = read( pollfds[ 0 ].fd, buf, buf_size ); + ssize_t bytes_read = read( STDIN_FILENO, buf, buf_size ); if ( bytes_read == 0 ) { /* EOF */ return; } else if ( bytes_read < 0 ) { @@ -274,12 +268,12 @@ void emulate_terminal( int fd ) if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) { break; } - } else if ( pollfds[ 1 ].revents & POLLIN ) { + } else if ( sel.read( fd ) ) { /* input from host */ char buf[ buf_size ]; /* fill buffer if possible */ - ssize_t bytes_read = read( pollfds[ 1 ].fd, buf, buf_size ); + ssize_t bytes_read = read( fd, buf, buf_size ); if ( bytes_read == 0 ) { /* EOF */ return; } else if ( bytes_read < 0 ) { @@ -291,7 +285,7 @@ void emulate_terminal( int fd ) if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) { break; } - } else if ( pollfds[ 2 ].revents & POLLIN ) { + } else if ( sel.read( signal_fd ) ) { /* resize */ fatal_assert( sigfd_read() == SIGWINCH ); @@ -310,17 +304,16 @@ void emulate_terminal( int fd ) perror( "ioctl TIOCSWINSZ" ); return; } - } else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents) - & (POLLERR | POLLHUP | POLLNVAL) ) { + } else if ( sel.error( STDIN_FILENO ) || sel.error( fd ) ) { break; } Terminal::Framebuffer new_frame( complete.get_fb() ); if ( tick( state, new_frame, display ) ) { /* there was a frame */ - poll_timeout = -1; + timeout = -1; } else { - poll_timeout = 20; + timeout = 20; } } |