cleanup: unify gtk&curses; make duplicate mode work

This commit is contained in:
Eric House 2020-02-02 22:10:20 -08:00
parent 4c19a00263
commit 7076501710
7 changed files with 83 additions and 108 deletions

View file

@ -134,4 +134,15 @@ ca_inform( WINDOW* window, const char* message )
(void)cursesask( window, message, VSIZE(buttons), buttons );
}
void
ca_informf( WINDOW* window, const char* fmt, ... )
{
va_list args;
va_start( args, fmt );
gchar* msg = g_strdup_vprintf( fmt, args );
va_end( args );
ca_inform( window, msg );
g_free( msg );
}
#endif /* PLATFORM_NCURSES */

View file

@ -26,6 +26,6 @@ int cursesask( WINDOW* window, const char* question,
short numButtons, const char** buttons );
void ca_inform( WINDOW* window, const char* message );
void ca_informf( WINDOW* window, const char* fmt, ... );
#endif

View file

@ -672,8 +672,7 @@ cursesUserError( CursesBoardGlobals* bGlobals, const char* format, ... )
va_end(ap);
if ( !!bGlobals->boardWin ) {
const char* buttons[] = { "Ok" };
(void)cursesask( bGlobals->boardWin, buf, VSIZE(buttons), buttons );
(void)ca_inform( bGlobals->boardWin, buf );
} else {
XP_LOGF( "%s(msg=%s)", __func__, buf );
}
@ -705,6 +704,7 @@ curses_util_informNeedPickTiles( XW_UtilCtxt* XP_UNUSED(uc),
const XP_UCHAR** XP_UNUSED(faces),
const XP_U16* XP_UNUSED(counts) )
{
XP_ASSERT(0);
/* CursesBoardGlobals* globals = (CursesBoardGlobals*)uc->closure; */
/* char query[128]; */
/* XP_S16 index; */
@ -757,34 +757,40 @@ curses_util_informNeedPassword( XW_UtilCtxt* XP_UNUSED(uc),
const XP_UCHAR* XP_UNUSED_DBG(name) )
{
XP_WARNF( "curses_util_informNeedPassword(num=%d, name=%s", playerNum, name );
XP_ASSERT(0);
} /* curses_util_askPassword */
static void
curses_util_yOffsetChange( XW_UtilCtxt* XP_UNUSED(uc),
XP_U16 XP_UNUSED(maxOffset),
XP_U16 XP_UNUSED(oldOffset), XP_U16 XP_UNUSED(newOffset) )
curses_util_yOffsetChange( XW_UtilCtxt* uc, XP_U16 XP_UNUSED(maxOffset),
XP_U16 oldOffset, XP_U16 newOffset )
{
/* if ( oldOffset != newOffset ) { */
/* XP_WARNF( "curses_util_yOffsetChange(%d,%d,%d) not implemented", */
/* maxOffset, oldOffset, newOffset ); */
/* } */
if ( 0 != newOffset ) {
CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)uc->closure;
if ( !!bGlobals->boardWin ) {
ca_informf( bGlobals->boardWin, "%s(oldOffset=%d, newOffset=%d)", __func__,
oldOffset, newOffset );
}
}
} /* curses_util_yOffsetChange */
#ifdef XWFEATURE_TURNCHANGENOTIFY
static void
curses_util_turnChanged( XW_UtilCtxt* XP_UNUSED(uc), XP_S16 XP_UNUSED_DBG(newTurn) )
curses_util_turnChanged( XW_UtilCtxt* uc, XP_S16 XP_UNUSED_DBG(newTurn) )
{
XP_LOGF( "%s(turn=%d)", __func__, newTurn );
XP_LOGF( "%s(newTurn=%d)", __func__, newTurn );
CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)uc->closure;
linuxSaveGame( &bGlobals->cGlobals );
}
#endif
static void
curses_util_notifyIllegalWords( XW_UtilCtxt* XP_UNUSED(uc),
curses_util_notifyIllegalWords( XW_UtilCtxt* uc,
BadWordInfo* XP_UNUSED(bwi),
XP_U16 XP_UNUSED(player),
XP_Bool XP_UNUSED(turnLost) )
{
XP_WARNF( "curses_util_notifyIllegalWords not implemented" );
CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)uc->closure;
ca_informf( bGlobals->boardWin, "%s() not implemented", __func__ );
} /* curses_util_notifyIllegalWord */
/* this needs to change!!! */
@ -847,8 +853,7 @@ cursesShowFinalScores( CursesBoardGlobals* bGlobals )
text = strFromStream( stream );
const char* buttons[] = { "Ok" };
(void)cursesask( bGlobals->boardWin, text, VSIZE(buttons), buttons );
(void)ca_inform( bGlobals->boardWin, text );
free( text );
stream_destroy( stream );
@ -860,22 +865,25 @@ curses_util_informMove( XW_UtilCtxt* uc, XP_S16 XP_UNUSED(turn),
{
CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)uc->closure;
char* question = strFromStream( expl );
const char* buttons[] = { "Ok" };
(void)cursesask( bGlobals->boardWin, question, VSIZE(buttons), buttons );
(void)ca_inform( bGlobals->boardWin, question );
free( question );
}
static void
curses_util_notifyDupStatus( XW_UtilCtxt* XP_UNUSED(uc),
XP_Bool XP_UNUSED_DBG(amHost),
const XP_UCHAR* XP_UNUSED_DBG(msg) )
curses_util_notifyDupStatus( XW_UtilCtxt* uc,
XP_Bool XP_UNUSED(amHost),
const XP_UCHAR* msg )
{
XP_LOGF( "%s(amHost=%d, msg=%s)", __func__, amHost, msg );
CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)uc->closure;
if ( !!bGlobals->boardWin ) {
ca_inform( bGlobals->boardWin, msg );
}
}
static void
curses_util_informUndo( XW_UtilCtxt* XP_UNUSED(uc))
{
XP_ASSERT(0);
LOG_FUNC();
}
@ -947,43 +955,6 @@ curses_util_engineProgressCallback( XW_UtilCtxt* XP_UNUSED(uc) )
return XP_TRUE;
} /* curses_util_engineProgressCallback */
static void
curses_util_clearTimer( XW_UtilCtxt* uc, XWTimerReason why )
{
CursesBoardGlobals* globals = (CursesBoardGlobals*)uc->closure;
globals->cGlobals.timerInfo[why].proc = NULL;
}
#ifdef USE_GLIBLOOP
static gboolean
onetime_idle( gpointer data )
{
CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)data;
XP_LOGF( "%s(): passed bGlobals %p", __func__, bGlobals );
XWGame* game = &bGlobals->cGlobals.game;
if ( !!game->server && server_do( game->server ) ) {
if ( !!game->board ) {
board_draw( game->board );
}
linuxSaveGame( &bGlobals->cGlobals );
}
return FALSE;
}
#endif
static void
curses_util_requestTime( XW_UtilCtxt* uc )
{
CursesBoardGlobals* globals = (CursesBoardGlobals*)uc->closure;
#if 1
(void)ADD_ONETIME_IDLE( onetime_idle, globals );
#else
(void)g_timeout_add( 1,// interval,
onetime_idle, globals );
#endif
} /* curses_util_requestTime */
static XP_Bool
curses_util_altKeyDown( XW_UtilCtxt* XP_UNUSED(uc) )
{
@ -1004,8 +975,7 @@ curses_util_remSelected( XW_UtilCtxt* uc )
text = strFromStream( stream );
const char* buttons[] = { "Ok" };
(void)cursesask( bGlobals->boardWin, text, VSIZE(buttons), buttons );
(void)ca_inform( bGlobals->boardWin, text );
free( text );
}
@ -1036,8 +1006,7 @@ curses_util_playerScoreHeld( XW_UtilCtxt* uc, XP_U16 player )
player, &lmi ) ) {
XP_UCHAR buf[128];
formatLMI( &lmi, buf, VSIZE(buf) );
const char* buttons[] = { "Ok" };
(void)cursesask( bGlobals->boardWin, buf, VSIZE(buttons), buttons );
(void)ca_inform( bGlobals->boardWin, buf );
}
}
@ -1111,8 +1080,6 @@ setupCursesUtilCallbacks( CursesBoardGlobals* bGlobals, XW_UtilCtxt* util )
SET_PROC(hiliteCell);
#endif
SET_PROC(engineProgressCallback);
SET_PROC(clearTimer);
SET_PROC(requestTime);
SET_PROC(altKeyDown); /* ?? */
SET_PROC(notifyIllegalWords);
SET_PROC(remSelected);
@ -1437,8 +1404,7 @@ handleShowVals( void* closure, int XP_UNUSED(key) )
XP_MEMCPY( buf, data, len );
buf[len] = '\0';
const char* buttons[] = { "Ok" };
(void)cursesask( bGlobals->boardWin, buf, VSIZE(buttons), buttons );
(void)ca_inform( bGlobals->boardWin, buf );
stream_destroy( stream );
return XP_TRUE;

View file

@ -689,8 +689,8 @@ cleanup( GtkGameGlobals* globals )
{
CommonGlobals* cGlobals = &globals->cGlobals;
linuxSaveGame( cGlobals );
if ( 0 < globals->idleID ) {
g_source_remove( globals->idleID );
if ( 0 < cGlobals->idleID ) {
g_source_remove( cGlobals->idleID );
}
cancelTimers( cGlobals );
@ -1861,41 +1861,6 @@ gtk_util_engineProgressCallback( XW_UtilCtxt* XP_UNUSED(uc) )
#endif
} /* gtk_util_engineProgressCallback */
static void
gtk_util_clearTimer( XW_UtilCtxt* uc, XWTimerReason why )
{
GtkGameGlobals* globals = (GtkGameGlobals*)uc->closure;
globals->cGlobals.timerInfo[why].proc = NULL;
}
static gint
idle_func( gpointer data )
{
GtkGameGlobals* globals = (GtkGameGlobals*)data;
/* XP_DEBUGF( "idle_func called\n" ); */
/* remove before calling server_do. If server_do puts up a dialog that
calls gtk_main, then this idle proc will also apply to that event loop
and bad things can happen. So kill the idle proc asap. */
g_source_remove( globals->idleID );
globals->idleID = 0; /* 0 is illegal event source ID */
ServerCtxt* server = globals->cGlobals.game.server;
if ( !!server && server_do( server ) ) {
if ( !!globals->cGlobals.game.board ) {
board_draw( globals->cGlobals.game.board );
}
}
return 0; /* 0 will stop it from being called again */
} /* idle_func */
static void
gtk_util_requestTime( XW_UtilCtxt* uc )
{
GtkGameGlobals* globals = (GtkGameGlobals*)uc->closure;
globals->idleID = g_idle_add( idle_func, globals );
} /* gtk_util_requestTime */
static gint
ask_bad_words( gpointer data )
{
@ -2258,8 +2223,6 @@ setupGtkUtilCallbacks( GtkGameGlobals* globals, XW_UtilCtxt* util )
#endif
SET_PROC(altKeyDown);
SET_PROC(engineProgressCallback);
SET_PROC(clearTimer);
SET_PROC(requestTime);
SET_PROC(notifyIllegalWords);
SET_PROC(remSelected);
SET_PROC(timerSelected);

View file

@ -121,8 +121,6 @@ typedef struct GtkGameGlobals {
EngineCtxt* engine;
guint idleID;
GtkAdjustment* adjustment;
ClientStreamRec clientRecs[MAX_NUM_PLAYERS];

View file

@ -2309,6 +2309,40 @@ linux_util_setTimer( XW_UtilCtxt* uc, XWTimerReason why,
cGlobals->timerSources[why-1] = newSrc;
} /* linux_util_setTimer */
static void
linux_util_clearTimer( XW_UtilCtxt* uc, XWTimerReason why )
{
CommonGlobals* cGlobals = (CommonGlobals*)uc->closure;
cGlobals->timerInfo[why].proc = NULL;
}
static gint
idle_func( gpointer data )
{
CommonGlobals* cGlobals = (CommonGlobals*)data;
/* remove before calling server_do. If server_do puts up a dialog that
calls gtk_main, then this idle proc will also apply to that event loop
and bad things can happen. So kill the idle proc asap. */
g_source_remove( cGlobals->idleID );
cGlobals->idleID = 0; /* 0 is illegal event source ID */
ServerCtxt* server = cGlobals->game.server;
if ( !!server && server_do( server ) ) {
if ( !!cGlobals->game.board ) {
board_draw( cGlobals->game.board );
}
}
return 0; /* 0 will stop it from being called again */
} /* idle_func */
static void
linux_util_requestTime( XW_UtilCtxt* uc )
{
CommonGlobals* cGlobals = (CommonGlobals*)uc->closure;
cGlobals->idleID = g_idle_add( idle_func, cGlobals );
} /* gtk_util_requestTime */
void
setupLinuxUtilCallbacks( XW_UtilCtxt* util )
{
@ -2320,6 +2354,8 @@ setupLinuxUtilCallbacks( XW_UtilCtxt* util )
#endif
SET_PROC(formatPauseHistory);
SET_PROC(setTimer);
SET_PROC(clearTimer);
SET_PROC(requestTime);
#undef SET_PROC
}

View file

@ -218,6 +218,7 @@ struct CommonGlobals {
guint timerSources[NUM_TIMERS_PLUS_ONE - 1];
XP_U32 scoreTimerInterval;
struct timeval scoreTv; /* for timer */
guint idleID;
CommsRelayState state;