2019-04-19 11:32:13 -05:00
// 19 april 2019
2019-04-21 14:08:09 -05:00
# include <stdarg.h>
2019-04-21 13:49:16 -05:00
# include <stdio.h>
2019-04-19 11:32:13 -05:00
# include <string.h>
# include "ui.h"
# include "uipriv.h"
2019-05-30 00:39:43 -05:00
// TODO rename this file to main.c
2019-05-29 20:10:44 -05:00
static bool initialized = false ;
2019-04-19 11:32:13 -05:00
2019-05-29 20:10:44 -05:00
bool uiInit ( void * options , uiInitError * err )
2019-04-19 11:32:13 -05:00
{
if ( err = = NULL )
2019-05-29 20:10:44 -05:00
return false ;
2019-04-19 11:32:13 -05:00
if ( err - > Size ! = sizeof ( uiInitError ) )
2019-05-29 20:10:44 -05:00
return false ;
2019-04-19 11:32:13 -05:00
if ( initialized )
2019-05-30 00:16:33 -05:00
return uiprivInitReturnErrorf ( err , " libui already initialized " ) ;
2019-04-19 11:32:13 -05:00
if ( options ! = NULL )
2019-05-30 00:16:33 -05:00
return uiprivInitReturnErrorf ( err , " options parameter to uiInit() must be NULL " ) ;
2019-04-19 11:32:13 -05:00
2019-05-09 11:07:28 -05:00
if ( ! uiprivSysInit ( options , err ) )
2019-05-29 20:10:44 -05:00
return false ;
initialized = true ;
return true ;
2019-04-19 11:32:13 -05:00
}
2019-05-30 00:16:33 -05:00
// TODO rename all msgs to fmt
2019-05-29 20:10:44 -05:00
bool uiprivInitReturnErrorf ( uiInitError * err , const char * msg , . . . )
2019-04-21 13:49:16 -05:00
{
2019-05-30 00:16:33 -05:00
int n ;
2019-04-21 13:49:16 -05:00
va_list ap ;
va_start ( ap , msg ) ;
2019-05-30 00:16:33 -05:00
n = vsnprintf ( err - > Message , 256 , msg , ap ) ;
2019-04-21 13:49:16 -05:00
va_end ( ap ) ;
2019-05-30 00:16:33 -05:00
if ( n < 0 )
uiprivInternalError ( " encoding error returning initialization error; this means something is very very wrong with libui itself " ) ;
if ( n > = 256 ) {
// the formatted message is too long; truncate it
err - > Message [ 252 ] = ' . ' ;
err - > Message [ 253 ] = ' . ' ;
err - > Message [ 254 ] = ' . ' ;
err - > Message [ 255 ] = ' \0 ' ;
}
2019-05-29 20:10:44 -05:00
return false ;
2019-04-21 13:49:16 -05:00
}
2019-05-28 20:54:13 -05:00
2019-05-30 00:39:43 -05:00
void uiQueueMain ( void ( * f ) ( void * data ) , void * data )
{
if ( ! initialized ) {
uiprivProgrammerError ( uiprivProgrammerErrorNotInitialized , uiprivFunc ) ;
return ;
}
uiprivSysQueueMain ( f , data ) ;
}
2019-05-28 20:54:13 -05:00
bool uiprivCheckInitializedAndThreadImpl ( const char * func )
{
// While it does seem risky to not lock this, if this changes during the execution of this function it means only that it was changed from a different thread, and since it can only change from false to true, an error will be reported anyway.
if ( ! initialized ) {
uiprivProgrammerError ( uiprivProgrammerErrorNotInitialized , func ) ;
return false ;
}
if ( ! uiprivSysCheckThread ( ) ) {
uiprivProgrammerError ( uiprivProgrammerErrorWrongThread , func ) ;
return false ;
}
return true ;
}