2013-02-21 21:07:31 -08:00
/*
2014-02-10 22:07:04 -08:00
* Copyright 2011 - 2014 Branimir Karadzic . All rights reserved .
2013-02-21 21:07:31 -08:00
* License : http : //www.opensource.org/licenses/BSD-2-Clause
*/
2013-08-14 21:08:46 -07:00
# include "entry_p.h"
2013-02-21 21:07:31 -08:00
2013-08-14 21:08:46 -07:00
# if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_NACL
2013-02-21 21:07:31 -08:00
2013-08-07 23:11:20 -07:00
# include <bgfxplatform.h>
2013-08-07 22:01:46 -07:00
2013-02-21 21:07:31 -08:00
# include <stdio.h>
# include <string.h>
# include <pthread.h>
2013-05-18 22:12:40 -07:00
# include <string>
2013-02-21 21:07:31 -08:00
# include <ppapi/c/pp_errors.h>
# include <ppapi/c/pp_module.h>
# include <ppapi/c/ppb.h>
2013-05-18 22:12:40 -07:00
# include <ppapi/c/ppb_core.h>
2013-02-21 21:07:31 -08:00
# include <ppapi/c/ppb_graphics_3d.h>
# include <ppapi/c/ppb_instance.h>
2013-05-18 22:12:40 -07:00
# include <ppapi/c/ppb_message_loop.h>
# include <ppapi/c/ppb_url_loader.h>
# include <ppapi/c/ppb_url_request_info.h>
# include <ppapi/c/ppb_url_response_info.h>
# include <ppapi/c/ppb_var.h>
2013-02-21 21:07:31 -08:00
# include <ppapi/c/ppp.h>
# include <ppapi/c/ppp_instance.h>
# include <ppapi/gles2/gl2ext_ppapi.h>
# include <bgfxplatform.h>
2013-05-18 22:12:40 -07:00
# include <bx/thread.h>
2013-02-21 21:07:31 -08:00
# include "entry.h"
namespace entry
{
2013-05-18 22:12:40 -07:00
const PPB_Core * g_coreInterface ;
const PPB_Instance * g_instInterface ;
const PPB_Graphics3D * g_graphicsInterface ;
const PPB_MessageLoop * g_messageLoopInterface ;
const PPB_URLLoader * g_urlLoaderInterface ;
const PPB_URLRequestInfo * g_urlRequestInterface ;
const PPB_URLResponseInfo * g_urlResponseInterface ;
const PPB_Var * g_varInterface ;
PP_Instance g_instance ;
2013-01-16 22:44:51 -08:00
const Event * poll ( )
{
return NULL ;
}
2014-09-22 19:34:10 -07:00
const Event * poll ( WindowHandle _handle )
{
BX_UNUSED ( _handle ) ;
return NULL ;
}
2013-01-16 22:44:51 -08:00
void release ( const Event * _event )
{
2013-11-29 22:23:04 -08:00
BX_UNUSED ( _event ) ;
2013-01-16 22:44:51 -08:00
}
2014-09-22 19:34:10 -07:00
WindowHandle createWindow ( int32_t _x , int32_t _y , uint32_t _width , uint32_t _height , uint32_t _flags , const char * _title )
{
BX_UNUSED ( _x , _y , _width , _height , _flags , _title ) ;
WindowHandle handle = { UINT16_MAX } ;
return handle ;
}
void destroyWindow ( WindowHandle _handle )
{
BX_UNUSED ( _handle ) ;
}
void setWindowPos ( WindowHandle _handle , int32_t _x , int32_t _y )
{
BX_UNUSED ( _handle , _x , _y ) ;
}
2014-09-18 22:32:33 -07:00
void setWindowSize ( WindowHandle _handle , uint32_t _width , uint32_t _height )
2013-02-21 21:07:31 -08:00
{
2014-09-18 22:32:33 -07:00
BX_UNUSED ( _handle , _width , _height ) ;
2013-01-16 22:44:51 -08:00
}
2014-09-18 22:32:33 -07:00
void setWindowTitle ( WindowHandle _handle , const char * _title )
2014-08-05 20:57:52 +01:00
{
2014-09-18 22:32:33 -07:00
BX_UNUSED ( _handle , _title ) ;
2014-08-05 20:57:52 +01:00
}
2014-09-18 22:32:33 -07:00
void toggleWindowFrame ( WindowHandle _handle )
2013-02-21 21:07:31 -08:00
{
2014-09-18 22:32:33 -07:00
BX_UNUSED ( _handle ) ;
2013-01-16 22:44:51 -08:00
}
2013-02-21 21:07:31 -08:00
2014-09-18 22:32:33 -07:00
void setMouseLock ( WindowHandle _handle , bool _lock )
2013-01-19 00:22:25 -08:00
{
2014-09-18 22:32:33 -07:00
BX_UNUSED ( _handle , _lock ) ;
2013-01-19 00:22:25 -08:00
}
2013-05-18 22:12:40 -07:00
template < typename Type >
bool initializeInterface ( PPB_GetInterface _interface , const char * _name , const Type * & _result )
{
_result = reinterpret_cast < const Type * > ( _interface ( _name ) ) ;
2014-01-19 22:34:58 -08:00
// DBG("%p %s", _result, _name);
2013-05-18 22:12:40 -07:00
return NULL ! = _result ;
}
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
struct MainThreadEntry
{
int m_argc ;
char * * m_argv ;
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
static int32_t threadFunc ( void * _userData ) ;
} ;
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
struct NaclContext
2013-02-21 21:07:31 -08:00
{
2013-05-18 22:12:40 -07:00
NaclContext ( )
{
2014-09-18 22:32:33 -07:00
static const char * argv [ 1 ] = { " nacl.nexe " } ;
2013-05-18 22:12:40 -07:00
m_mte . m_argc = 1 ;
m_mte . m_argv = const_cast < char * * > ( argv ) ;
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
m_thread . init ( MainThreadEntry : : threadFunc , & m_mte ) ;
}
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
~ NaclContext ( )
{
m_thread . shutdown ( ) ;
}
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
MainThreadEntry m_mte ;
bx : : Thread m_thread ;
} ;
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
static NaclContext * s_ctx ;
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
int32_t MainThreadEntry : : threadFunc ( void * _userData )
{
MainThreadEntry * self = ( MainThreadEntry * ) _userData ;
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
PP_Resource resource = g_messageLoopInterface - > Create ( g_instance ) ;
g_messageLoopInterface - > AttachToCurrentThread ( resource ) ;
2013-02-21 21:07:31 -08:00
2013-08-07 21:45:56 -07:00
int32_t result = main ( self - > m_argc , self - > m_argv ) ;
2013-05-18 22:12:40 -07:00
return result ;
}
static PP_Bool naclInstanceDidCreate ( PP_Instance _instance , uint32_t /*_argc*/ , const char * /*_argn*/ [ ] , const char * /*_argv*/ [ ] )
2013-02-21 21:07:31 -08:00
{
2013-05-18 22:12:40 -07:00
g_instance = _instance ; // one instance only!
2014-01-19 22:34:58 -08:00
if ( bgfx : : naclSetInterfaces ( g_instance , g_instInterface , g_graphicsInterface , NULL ) )
{
s_ctx = new NaclContext ;
return PP_TRUE ;
}
return PP_FALSE ;
2013-02-21 21:07:31 -08:00
}
2013-05-18 22:12:40 -07:00
static void naclInstanceDidDestroy ( PP_Instance _instance )
{
2013-11-29 22:23:04 -08:00
BX_UNUSED ( _instance ) ;
2013-05-18 22:12:40 -07:00
delete s_ctx ;
}
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
static void naclInstanceDidChangeView ( PP_Instance /*_instance*/ , PP_Resource /*_view*/ )
{
}
2013-02-21 21:07:31 -08:00
2013-05-18 22:12:40 -07:00
static void naclInstanceDidChangeFocus ( PP_Instance /*_instance*/ , PP_Bool /*_focus*/ )
{
}
static PP_Bool naclInstanceHandleDocumentLoad ( PP_Instance /*_instance*/ , PP_Resource /*_urlLoader*/ )
{
return PP_FALSE ;
}
} // namespace entry
using namespace entry ;
2013-02-21 21:07:31 -08:00
PP_EXPORT const void * PPP_GetInterface ( const char * _name )
{
if ( 0 = = strcmp ( _name , PPP_INSTANCE_INTERFACE ) )
{
static PPP_Instance instanceInterface =
{
& naclInstanceDidCreate ,
& naclInstanceDidDestroy ,
& naclInstanceDidChangeView ,
& naclInstanceDidChangeFocus ,
& naclInstanceHandleDocumentLoad ,
} ;
return & instanceInterface ;
}
return NULL ;
}
PP_EXPORT int32_t PPP_InitializeModule ( PP_Module _module , PPB_GetInterface _interface )
{
2014-06-25 21:17:21 -07:00
DBG ( " PPAPI version: %d " , PPAPI_RELEASE ) ;
2013-11-29 22:23:04 -08:00
BX_UNUSED ( _module ) ;
2013-02-21 21:07:31 -08:00
bool result = true ;
2013-05-18 22:12:40 -07:00
result & = initializeInterface ( _interface , PPB_CORE_INTERFACE , g_coreInterface ) ;
result & = initializeInterface ( _interface , PPB_GRAPHICS_3D_INTERFACE , g_graphicsInterface ) ;
result & = initializeInterface ( _interface , PPB_INSTANCE_INTERFACE , g_instInterface ) ;
result & = initializeInterface ( _interface , PPB_MESSAGELOOP_INTERFACE , g_messageLoopInterface ) ;
result & = initializeInterface ( _interface , PPB_URLLOADER_INTERFACE , g_urlLoaderInterface ) ;
result & = initializeInterface ( _interface , PPB_URLREQUESTINFO_INTERFACE , g_urlRequestInterface ) ;
result & = initializeInterface ( _interface , PPB_URLRESPONSEINFO_INTERFACE , g_urlResponseInterface ) ;
result & = initializeInterface ( _interface , PPB_VAR_INTERFACE , g_varInterface ) ;
2013-02-21 21:07:31 -08:00
result & = glInitializePPAPI ( _interface ) ;
return result ? PP_OK : PP_ERROR_NOINTERFACE ;
}
PP_EXPORT void PPP_ShutdownModule ( )
{
}
2013-08-14 21:08:46 -07:00
# endif // ENTRY_CONFIG_USE_NATIVE && BX_PLATFROM_NACL