Chinaunix首页 | 论坛 | 博客
  • 博客访问: 601507
  • 博文数量: 204
  • 博客积分: 5172
  • 博客等级: 上校
  • 技术积分: 2092
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 21:48
个人简介

一个毫无毅力之人的自勉

文章分类

全部博文(204)

文章存档

2014年(1)

2013年(54)

2012年(50)

2011年(94)

2010年(3)

2009年(3)

分类: LINUX

2011-11-09 17:22:46

#include
#include
#include
#include
#include
int set_newinput_mode( struct termios *org_opts )
{
        int ret;
        struct termios new_opts;
        /* store old settings */
        ret = tcgetattr( STDIN_FILENO, org_opts );
        if ( ret < 0 )
        {
                printf( "error: tcgetattr()" );
                return 1;
        }
        /* set new terminal parms */
        memcpy( &new_opts, org_opts, sizeof(new_opts) );
        new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
        ret = tcsetattr( STDIN_FILENO, TCSANOW, &new_opts );
        if ( ret < 0 )
        {
                printf( "error: tcsetattr()" );
                return 1;
        }
        return 0;
}
int restore_input_mode( struct termios *org_opts )
{
        int ret;
        ret = tcsetattr( STDIN_FILENO, TCSANOW, org_opts );
        if ( ret < 0 )
        {
                printf( "error: tcsetattr()" );
                return 1;
        }
        return 0;
}
int main( void )
{
        int ret,i;
        char c;
        fd_set read_fd_set;
        struct timeval timeout;
        int keyboard = 0;
        struct termios org_opts;
#if 1
        keyboard = open( "/dev/tty", O_RDONLY | O_NONBLOCK );
        if ( keyboard < 0 )
        {
                printf( "error: open /dev/tty\n" );
        }
#endif
        set_newinput_mode( &org_opts );
        while (1)
        {
                timeout.tv_sec = 1;
                timeout.tv_usec = 0;
                FD_ZERO( &read_fd_set );
                FD_SET( keyboard, &read_fd_set );
                ret = select( keyboard+1, &read_fd_set, NULL, NULL, &timeout );
                if ( ret < 0 )
                {
                        perror( "select()" );
                }
                else if ( ret == 0 )
                {
                        printf( "timeout\n" );
                }
                else
                {
                        printf( "ret = %d: the status of keyboard is changed\n.", ret );
                }
                if ( FD_ISSET( keyboard, &read_fd_set ) )
                {
                        i = read( keyboard, &c, 1 );
                        printf("you press: %c\n",c);
                        if ( 'q' == c || 'Q' == c )
                                break;
                }
        }
        restore_input_mode( &org_opts );
        return 0;
}
阅读(1387) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~