From 5dace1cc7a7519e8a193996cce144423d0b6bc8c Mon Sep 17 00:00:00 2001 From: ehouse Date: Fri, 9 Sep 2005 03:14:11 +0000 Subject: [PATCH] add enough timer support for heartbeat; fix redraw bug --- xwords4/linux/cursesmain.c | 37 +++++++++++++++++++++++++++++++++---- xwords4/linux/cursesmain.h | 2 ++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/xwords4/linux/cursesmain.c b/xwords4/linux/cursesmain.c index ac3a77cad..3d8b52904 100644 --- a/xwords4/linux/cursesmain.c +++ b/xwords4/linux/cursesmain.c @@ -214,7 +214,13 @@ static void curses_util_setTimer( XW_UtilCtxt* uc, XWTimerReason why, XP_U16 when, TimerProc proc, void* closure ) { - XP_ASSERT( 0 ); /* no pen-down events..... */ + CursesAppGlobals* globals = (CursesAppGlobals*)uc->closure; + + XP_ASSERT( why == TIMER_HEARTBEAT ); + + globals->cGlobals.timerProcs[why] = proc; + globals->cGlobals.timerClosures[why] = closure; + globals->nextTimer = util_getCurSeconds(uc) + when; } /* curses_util_setTimer */ static void @@ -624,6 +630,24 @@ curses_socket_changed( void* closure, int oldSock, int newSock ) } } /* curses_socket_changed */ +static int +figureTimeout( CursesAppGlobals* globals ) +{ + int result = INFINITE_TIMEOUT; + + if ( globals->cGlobals.timerProcs[TIMER_HEARTBEAT] != 0 ) { + XP_U32 now = util_getCurSeconds( globals->cGlobals.params->util ); + XP_U32 then = globals->nextTimer; + if ( now >= then ) { + result = 0; + } else { + result = (then - now) * 1000; + } + } + + return result; +} /* figureTimeout */ + /* * Ok, so this doesn't block yet.... */ @@ -635,9 +659,14 @@ blocking_gotEvent( CursesAppGlobals* globals, int* ch ) short fdIndex; XP_Bool redraw = XP_FALSE; - numEvents = poll( globals->fdArray, globals->fdCount, INFINITE_TIMEOUT ); + int timeout = figureTimeout( globals ); + numEvents = poll( globals->fdArray, globals->fdCount, timeout ); - if ( numEvents > 0 ) { + if ( timeout != INFINITE_TIMEOUT && numEvents == 0 ) { + if ( !globals->cGlobals.params->noHeartbeat ) { + linuxFireTimer( &globals->cGlobals, TIMER_HEARTBEAT ); + } + } else if ( numEvents > 0 ) { /* stdin first */ if ( (globals->fdArray[FD_STDIN].revents & POLLIN) != 0 ) { @@ -670,7 +699,7 @@ blocking_gotEvent( CursesAppGlobals* globals, int* ch ) if ( nBytes != -1 ) { XWStreamCtxt* inboundS; - XP_Bool redraw = XP_FALSE; + redraw = XP_FALSE; XP_STATUSF( "linuxReceive=>%d", nBytes ); inboundS = stream_from_msgbuf( &globals->cGlobals, diff --git a/xwords4/linux/cursesmain.h b/xwords4/linux/cursesmain.h index c76f5e883..ac7e45274 100644 --- a/xwords4/linux/cursesmain.h +++ b/xwords4/linux/cursesmain.h @@ -97,6 +97,8 @@ struct CursesAppGlobals { struct pollfd fdArray[FD_MAX]; /* one for stdio, one for listening socket */ int timepipe[2]; /* for reading/writing "user events" */ + + XP_U32 nextTimer; };