add (unused still) timers to dutil

I'll want timers and idle procs for device operations, not just games.
This commit is contained in:
Eric House 2024-04-24 09:29:36 -07:00
parent 1ea0c97115
commit cb540c23c0
7 changed files with 112 additions and 9 deletions

View file

@ -50,7 +50,7 @@ typedef struct _AndDUtil {
} AndDUtil;
typedef struct _TimerStorage {
XWTimerProc proc;
UtilTimerProc proc;
void* closure;
} TimerStorage;
@ -339,7 +339,7 @@ utilTimerFired( XW_UtilCtxt* uc, XWEnv xwe, XWTimerReason why, int handle )
AndUtil* util = (AndUtil*)uc;
TimerStorage* timerStorage = &util->timerStorage[why];
if ( handle == (int)timerStorage ) {
XWTimerProc proc = timerStorage->proc;
UtilTimerProc proc = timerStorage->proc;
if ( !!proc ) {
handled = (*proc)( timerStorage->closure, xwe, why );
} else {
@ -354,7 +354,7 @@ utilTimerFired( XW_UtilCtxt* uc, XWEnv xwe, XWTimerReason why, int handle )
static void
and_util_setTimer( XW_UtilCtxt* uc, XWEnv xwe, XWTimerReason why, XP_U16 when,
XWTimerProc proc, void* closure )
UtilTimerProc proc, void* closure )
{
UTIL_CBK_HEADER("setTimer", "(III)V" );

View file

@ -43,6 +43,9 @@ typedef struct _Md5SumBuf {
#define KEY_WILDCARD "*"
#ifdef DUTIL_TIMERS
typedef void (*DUtilTimerProc)( XWEnv xwe, void* closure, size_t closureSize );
#endif
typedef struct _DUtilVtable {
XP_U32 (*m_dutil_getCurSeconds)( XW_DUtilCtxt* duc, XWEnv xwe );
const XP_UCHAR* (*m_dutil_getUserString)( XW_DUtilCtxt* duc, XWEnv xwe,
@ -80,6 +83,11 @@ typedef struct _DUtilVtable {
const XP_UCHAR* idRelay );
#endif
#ifdef DUTIL_TIMERS
void (*m_dutil_setTimer)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U16 when, DUtilTimerProc proc,
void* closure, size_t closureSize );
void (*m_dutil_clearTimer)( XW_DUtilCtxt* duc, XWEnv xwe, DUtilTimerProc proc );
#endif
void (*m_dutil_md5sum)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_U8* ptr,
XP_U32 len, Md5SumBuf* sb );
void (*m_dutil_getUsername)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U16 num,
@ -159,6 +167,13 @@ void dutil_super_cleanup( XW_DUtilCtxt* dutil, XWEnv xwe );
(duc)->vtable.m_dutil_deviceRegistered( (duc), (e), (typ), (id) )
#endif
#ifdef DUTIL_TIMERS
# define dutil_setTimer( duc, xwe, when, proc, closure, siz ) \
(duc)->vtable.m_dutil_setTimer((duc), (xwe), (when), (proc), (closure), (siz))
# define dutil_clearTimer( duc, xwe, proc ) \
(duc)->vtable.m_dutil_clearTimer((duc), (xwe), (proc) )
#endif
# define dutil_md5sum( duc, e, p, l, b ) \
(duc)->vtable.m_dutil_md5sum((duc), (e), (p), (l), (b))

View file

@ -77,8 +77,8 @@ typedef struct _BadWordInfo {
const XP_UCHAR* words[MAX_TRAY_TILES+2]; /* can form in both directions */
} BadWordInfo;
/* XWTimerProc returns true if redraw was necessitated by what the proc did */
typedef XP_Bool (*XWTimerProc)( void* closure, XWEnv xwe, XWTimerReason why );
/* UtilTimerProc returns true if redraw was necessitated by what the proc did */
typedef XP_Bool (*UtilTimerProc)( void* closure, XWEnv xwe, XWTimerReason why );
/* Platform-specific utility functions that need to be
*/
@ -130,7 +130,7 @@ typedef struct UtilVtable {
XP_Bool (*m_util_engineProgressCallback)( XW_UtilCtxt* uc, XWEnv xwe );
void (*m_util_setTimer)( XW_UtilCtxt* uc, XWEnv xwe, XWTimerReason why, XP_U16 when,
XWTimerProc proc, void* closure );
UtilTimerProc proc, void* closure );
void (*m_util_clearTimer)( XW_UtilCtxt* uc, XWEnv xwe, XWTimerReason why );
void (*m_util_requestTime)( XW_UtilCtxt* uc, XWEnv xwe );

View file

@ -132,6 +132,7 @@ DEFINES += -DCOMMON_LAYOUT
DEFINES += -DNATIVE_NLI
DEFINES += -DXWFEATURE_COMMS_INVITE
DEFINES += -DXWFEATURE_RO_BYNAME
DEFINES += -DDUTIL_TIMERS
# DEFINES += -DRELAY_VIA_HTTP
# MAX_ROWS controls STREAM_VERS_BIGBOARD and with it move hashing

View file

@ -41,6 +41,9 @@
typedef struct _LinDUtilCtxt {
XW_DUtilCtxt super;
#ifdef DUTIL_TIMERS
GSList* timers;
#endif
} LinDUtilCtxt;
static XP_U32 linux_dutil_getCurSeconds( XW_DUtilCtxt* duc, XWEnv xwe );
@ -312,6 +315,86 @@ linux_dutil_getRegValues( XW_DUtilCtxt* duc, XWEnv xwe )
return results;
}
typedef struct _TimerClosure {
XW_DUtilCtxt* duc;
DUtilTimerProc proc;
void* closure;
size_t closureSize;
guint src;
} TimerClosure;
static gint
timer_proc( gpointer data )
{
TimerClosure* tc = (TimerClosure*)data;
XP_LOGFF( "calling timer proc" );
(*tc->proc)( NULL_XWE, tc->closure, tc->closureSize );
XP_LOGFF( "timer proc done" );
XP_FREEP( tc->duc->mpool, &tc->closure );
LinDUtilCtxt* lduc = (LinDUtilCtxt*)tc->duc;
GSList** timers = &lduc->timers;
*timers = g_slist_remove( *timers, tc );
XP_LOGFF( "after firing, length %d", g_slist_length(*timers) );
XP_FREEP( tc->duc->mpool, &tc );
return 0;
}
#ifdef DUTIL_TIMERS
static void
linux_dutil_setTimer( XW_DUtilCtxt* duc, XWEnv xwe, XP_U16 when,
DUtilTimerProc proc, void* closure, size_t closureSize )
{
XP_USE(xwe); /* I assume I'll need this on the Android side */
TimerClosure* tc = XP_MALLOC(duc->mpool, sizeof(*tc));
tc->duc = duc;
tc->closureSize = closureSize;
tc->closure = XP_MALLOC( duc->mpool, closureSize );
XP_MEMCPY( tc->closure, closure, closureSize );
tc->proc = proc;
tc->src = g_timeout_add( when, timer_proc, tc );
LinDUtilCtxt* lduc = (LinDUtilCtxt*)duc;
GSList** timers = &lduc->timers;
*timers = g_slist_append( *timers, tc );
XP_LOGFF( "after setting, length %d", g_slist_length(*timers) );
}
static gint
findByProc( gconstpointer elemData, gconstpointer proc )
{
TimerClosure* tc = (TimerClosure*)elemData;
return tc->proc == proc ? 0 : 1;
}
static void
linux_dutil_clearTimer( XW_DUtilCtxt* duc, XWEnv xwe, DUtilTimerProc proc )
{
/* Will probably want to store the TimerClosure instances above in a list
that can be searched here for its proc value, then its src field used
to cancel. */
XP_USE(xwe);
int count = 0;
LinDUtilCtxt* lduc = (LinDUtilCtxt*)duc;
GSList** timers = &lduc->timers;
while ( !!*timers ) {
GSList* elem = g_slist_find_custom( *timers, proc, findByProc );
if ( !elem ) {
break;
}
*timers = g_slist_remove( *timers, elem );
++count;
}
XP_LOGFF( "removed %d timers", count );
}
#endif
XW_DUtilCtxt*
linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure )
{
@ -355,6 +438,10 @@ linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure )
SET_PROC(onGameGoneReceived);
SET_PROC(sendViaWeb);
SET_PROC(getRegValues);
#ifdef DUTIL_TIMERS
SET_PROC(setTimer);
SET_PROC(clearTimer);
#endif
# undef SET_PROC

View file

@ -1721,7 +1721,7 @@ XP_Bool
linuxFireTimer( CommonGlobals* cGlobals, XWTimerReason why )
{
TimerInfo* tip = &cGlobals->timerInfo[why];
XWTimerProc proc = tip->proc;
UtilTimerProc proc = tip->proc;
XP_Bool draw = false;
tip->proc = NULL;
@ -2313,7 +2313,7 @@ slowrob_timer_func( gpointer data )
static void
linux_util_setTimer( XW_UtilCtxt* uc, XWEnv XP_UNUSED(xwe), XWTimerReason why,
XP_U16 when, XWTimerProc proc, void* closure )
XP_U16 when, UtilTimerProc proc, void* closure )
{
CommonGlobals* cGlobals = (CommonGlobals*)uc->closure;
guint newSrc;

View file

@ -220,7 +220,7 @@ typedef void (*AddAcceptorFunc)(int listener, Acceptor func,
CommonGlobals* globals, void** storage );
typedef struct _TimerInfo {
XWTimerProc proc;
UtilTimerProc proc;
void* closure;
#ifdef USE_GLIBLOOP
struct CommonGlobals* globals;