日期:2014-05-16 浏览次数:20946 次
#include <stdio.h>
#include <curses.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
/* some global settings main and the handler use */
#define MESSAGE "hello"
#define BLANK " "
int row; /* current row */
int col; /* current column */
int dir; /* where we are going */
int delay; /* bigger => slower */
int done;
void on_input(int); /* handler for keybd */
void enable_kbd_signals();
int main()
{
initscr();
crmode();
noecho();
clear();
signal(SIGIO,on_input); /* install a handler */
//enable_kbd_signals(); /* turn on kbd signals */ //为什么注释掉这一句后,字符串就可以自动移动??为什么有这句的时候,必须要按"上下左右"键才可以使得字符串进行移动?
row = 10; /* start here */
col = 0;
dir = 1; /* add 1 to col number */
delay = 200; /* 200ms = 0.2 seconds */
done = 0;
while(!done)
{
usleep(delay);
move_msg();
}
endwin();
return 0;
}
/*
* called after each sleep period
*/
move_msg()
{
move( row, col );
addstr( BLANK );
col += dir; /* move to new column */
move( row, col ); /* then set cursor */
addstr( MESSAGE ); /* redo message */
move(0,COLS-1);
refresh(); /* and show it */
/*
* now handle borders
*/
if ( dir == -1 && col <= 0 )
dir = 1;
else if ( dir == 1 && col+strlen(MESSAGE) >= COLS )
dir = -1;
}
/*
* called when a keystroke appears
*/
void on_input(int signum)
{
int c = getch(); /* grab the char */
switch ( c ){
case 'Q':
case EOF: done = 1;
break;
case ' ': dir = -dir;
break;
case 'f': if ( delay > 2 )
delay >>= 1;
break;
case 's': delay <<= 1;
break;
}
}
/*
* install a handler, tell kernel who to notify on input, enable signals
*/
void enable_kbd_signals()
{
int fd_flags;
fcntl(0, F_SETOWN, getpid());
fd_flags = fcntl(0, F_GETFL);
fcntl(0, F_SETFL, (fd_flags|O_ASYNC));
}