⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bounce_async.c

📁 unix编程实践教程代码。适合初学者
💻 C
字号:
/* bounce_async.c *	purpose	animation with user control, using O_ASYNC on fd *	note	set_ticker() sends SIGALRM, handler does animation *		keyboard sends SIGIO, main only calls pause() *	compile	cc bounce_async.c set_ticker.c -lcurses -o bounce_async */#include	<stdio.h>#include	<curses.h>#include	<signal.h>#include	<fcntl.h>/* The state of the game */#define	MESSAGE	"hello"#define	BLANK	"     "int	row   = 10;	/* current row		*/int	col   =  0;	/* current column	*/int	dir   =  1;	/* where we are going	*/int	delay = 200;	/* how long to wait	*/int	done  = 0;main(){	void	on_alarm(int);	/* handler for alarm	*/	void	on_input(int);	/* handler for keybd    */	void	enable_kbd_signals();	initscr();		/* set up screen */	crmode();	noecho();	clear();	signal(SIGIO, on_input);          /* install a handler        */	enable_kbd_signals();             /* turn on kbd signals      */	signal(SIGALRM, on_alarm);        /* install alarm handler    */	set_ticker(delay);		  /* start ticking	      */	move(row,col);		          /* get into position	      */	addstr( MESSAGE );	          /* draw initial image       */	while( !done )			  /* the main loop */		pause();	endwin();}void on_input(int signum){	int	c = getch();		  /* grab the char */	if ( c == 'Q' || c == EOF )		done = 1;	else if ( c == ' ' )		dir = -dir;}void on_alarm(int signum){	signal(SIGALRM, on_alarm);	/* reset, just in case	*/	mvaddstr( row, col, BLANK );	/* note mvaddstr()	*/	col += dir;			/* move to new column	*/	mvaddstr( row, col, MESSAGE );	/* redo message		*/	refresh();			/* and show it		*/	/*	 * now handle borders	 */	if ( dir == -1 && col <= 0 )		dir = 1;	else if ( dir == 1 && col+strlen(MESSAGE) >= COLS )		dir = -1;}/* * 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));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -