entry_nacl.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry.h"
  6. #if BX_PLATFORM_NACL
  7. #include <bgfxplatform.h>
  8. #include "entry_p.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <pthread.h>
  12. #include <string>
  13. #include <ppapi/c/pp_errors.h>
  14. #include <ppapi/c/pp_module.h>
  15. #include <ppapi/c/ppb.h>
  16. #include <ppapi/c/ppb_core.h>
  17. #include <ppapi/c/ppb_graphics_3d.h>
  18. #include <ppapi/c/ppb_instance.h>
  19. #include <ppapi/c/ppb_message_loop.h>
  20. #include <ppapi/c/ppb_url_loader.h>
  21. #include <ppapi/c/ppb_url_request_info.h>
  22. #include <ppapi/c/ppb_url_response_info.h>
  23. #include <ppapi/c/ppb_var.h>
  24. #include <ppapi/c/ppp.h>
  25. #include <ppapi/c/ppp_instance.h>
  26. #include <ppapi/gles2/gl2ext_ppapi.h>
  27. #include <bgfxplatform.h>
  28. #include <bx/thread.h>
  29. #include "entry.h"
  30. namespace entry
  31. {
  32. const PPB_Core* g_coreInterface;
  33. const PPB_Instance* g_instInterface;
  34. const PPB_Graphics3D* g_graphicsInterface;
  35. const PPB_MessageLoop* g_messageLoopInterface;
  36. const PPB_URLLoader* g_urlLoaderInterface;
  37. const PPB_URLRequestInfo* g_urlRequestInterface;
  38. const PPB_URLResponseInfo* g_urlResponseInterface;
  39. const PPB_Var* g_varInterface;
  40. PP_Instance g_instance;
  41. const Event* poll()
  42. {
  43. return NULL;
  44. }
  45. void release(const Event* _event)
  46. {
  47. }
  48. void setWindowSize(uint32_t _width, uint32_t _height)
  49. {
  50. }
  51. void toggleWindowFrame()
  52. {
  53. }
  54. void setMouseLock(bool _lock)
  55. {
  56. }
  57. template<typename Type>
  58. bool initializeInterface(PPB_GetInterface _interface, const char* _name, const Type*& _result)
  59. {
  60. _result = reinterpret_cast<const Type*>(_interface(_name) );
  61. return NULL != _result;
  62. }
  63. struct MainThreadEntry
  64. {
  65. int m_argc;
  66. char** m_argv;
  67. static int32_t threadFunc(void* _userData);
  68. };
  69. struct NaclContext
  70. {
  71. NaclContext()
  72. {
  73. const char* argv[1] = { "nacl.nexe" };
  74. m_mte.m_argc = 1;
  75. m_mte.m_argv = const_cast<char**>(argv);
  76. bgfx::naclSetIntefraces(g_instance, g_instInterface, g_graphicsInterface, NULL);
  77. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  78. }
  79. ~NaclContext()
  80. {
  81. m_thread.shutdown();
  82. }
  83. MainThreadEntry m_mte;
  84. bx::Thread m_thread;
  85. };
  86. static NaclContext* s_ctx;
  87. int32_t MainThreadEntry::threadFunc(void* _userData)
  88. {
  89. MainThreadEntry* self = (MainThreadEntry*)_userData;
  90. PP_Resource resource = g_messageLoopInterface->Create(g_instance);
  91. g_messageLoopInterface->AttachToCurrentThread(resource);
  92. int32_t result = main(self->m_argc, self->m_argv);
  93. return result;
  94. }
  95. static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t /*_argc*/, const char* /*_argn*/[], const char* /*_argv*/[])
  96. {
  97. g_instance = _instance; // one instance only!
  98. s_ctx = new NaclContext;
  99. return PP_TRUE;
  100. }
  101. static void naclInstanceDidDestroy(PP_Instance _instance)
  102. {
  103. delete s_ctx;
  104. }
  105. static void naclInstanceDidChangeView(PP_Instance /*_instance*/, PP_Resource /*_view*/)
  106. {
  107. }
  108. static void naclInstanceDidChangeFocus(PP_Instance /*_instance*/, PP_Bool /*_focus*/)
  109. {
  110. }
  111. static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance /*_instance*/, PP_Resource /*_urlLoader*/)
  112. {
  113. return PP_FALSE;
  114. }
  115. } // namespace entry
  116. using namespace entry;
  117. PP_EXPORT const void* PPP_GetInterface(const char* _name)
  118. {
  119. if (0 == strcmp(_name, PPP_INSTANCE_INTERFACE) )
  120. {
  121. static PPP_Instance instanceInterface =
  122. {
  123. &naclInstanceDidCreate,
  124. &naclInstanceDidDestroy,
  125. &naclInstanceDidChangeView,
  126. &naclInstanceDidChangeFocus,
  127. &naclInstanceHandleDocumentLoad,
  128. };
  129. return &instanceInterface;
  130. }
  131. return NULL;
  132. }
  133. PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface)
  134. {
  135. bool result = true;
  136. result &= initializeInterface(_interface, PPB_CORE_INTERFACE, g_coreInterface);
  137. result &= initializeInterface(_interface, PPB_GRAPHICS_3D_INTERFACE, g_graphicsInterface);
  138. result &= initializeInterface(_interface, PPB_INSTANCE_INTERFACE, g_instInterface);
  139. result &= initializeInterface(_interface, PPB_MESSAGELOOP_INTERFACE, g_messageLoopInterface);
  140. result &= initializeInterface(_interface, PPB_URLLOADER_INTERFACE, g_urlLoaderInterface);
  141. result &= initializeInterface(_interface, PPB_URLREQUESTINFO_INTERFACE, g_urlRequestInterface);
  142. result &= initializeInterface(_interface, PPB_URLRESPONSEINFO_INTERFACE, g_urlResponseInterface);
  143. result &= initializeInterface(_interface, PPB_VAR_INTERFACE, g_varInterface);
  144. result &= glInitializePPAPI(_interface);
  145. return result ? PP_OK : PP_ERROR_NOINTERFACE;
  146. }
  147. PP_EXPORT void PPP_ShutdownModule()
  148. {
  149. }
  150. #endif // BX_PLATFROM_NACL