entry_nacl.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bx/bx.h>
  6. #if BX_PLATFORM_NACL
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10. #include <ppapi/c/pp_errors.h>
  11. #include <ppapi/c/pp_module.h>
  12. #include <ppapi/c/ppb.h>
  13. #include <ppapi/c/ppb_graphics_3d.h>
  14. #include <ppapi/c/ppb_instance.h>
  15. #include <ppapi/c/ppp.h>
  16. #include <ppapi/c/ppp_instance.h>
  17. #include <ppapi/gles2/gl2ext_ppapi.h>
  18. #include <bgfxplatform.h>
  19. #include "dbg.h"
  20. #include "entry.h"
  21. namespace entry
  22. {
  23. Event::Enum poll()
  24. {
  25. return Event::Nop;
  26. }
  27. } // namespace entry
  28. extern int _main_(int _argc, char** _argv);
  29. static void* _entry_(void*)
  30. {
  31. const char* argv[1] = { "nacl.nexe" };
  32. _main_(1, const_cast<char**>(argv) );
  33. return NULL;
  34. }
  35. struct NaclContext
  36. {
  37. NaclContext()
  38. : m_thread(0)
  39. {
  40. }
  41. const PPB_Instance* m_instInterface;
  42. const PPB_Graphics3D* m_graphicsInterface;
  43. pthread_t m_thread;
  44. PP_Module m_module;
  45. PP_Instance m_instance;
  46. };
  47. static NaclContext s_ctx;
  48. static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t _argc, const char* _argn[], const char* _argv[])
  49. {
  50. s_ctx.m_instance = _instance;
  51. bgfx::naclSetIntefraces(s_ctx.m_instance, s_ctx.m_instInterface, s_ctx.m_graphicsInterface, NULL);
  52. pthread_create(&s_ctx.m_thread, NULL, _entry_, NULL);
  53. return PP_TRUE;
  54. }
  55. static void naclInstanceDidDestroy(PP_Instance _instance)
  56. {
  57. if (s_ctx.m_thread)
  58. {
  59. pthread_join(s_ctx.m_thread, NULL);
  60. }
  61. }
  62. static void naclInstanceDidChangeView(PP_Instance _instance, PP_Resource _view)
  63. {
  64. }
  65. static void naclInstanceDidChangeFocus(PP_Instance _instance, PP_Bool _focus)
  66. {
  67. }
  68. static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance _instance, PP_Resource _urlLoader)
  69. {
  70. return PP_FALSE;
  71. }
  72. PP_EXPORT const void* PPP_GetInterface(const char* _name)
  73. {
  74. if (0 == strcmp(_name, PPP_INSTANCE_INTERFACE) )
  75. {
  76. static PPP_Instance instanceInterface =
  77. {
  78. &naclInstanceDidCreate,
  79. &naclInstanceDidDestroy,
  80. &naclInstanceDidChangeView,
  81. &naclInstanceDidChangeFocus,
  82. &naclInstanceHandleDocumentLoad,
  83. };
  84. return &instanceInterface;
  85. }
  86. return NULL;
  87. }
  88. template<typename Type>
  89. bool initializeInterface(const Type*& _result, PPB_GetInterface _interface, const char* _name)
  90. {
  91. _result = reinterpret_cast<const Type*>(_interface(_name) );
  92. return NULL != _result;
  93. }
  94. PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface)
  95. {
  96. s_ctx.m_module = _module;
  97. bool result = true;
  98. result &= initializeInterface(s_ctx.m_instInterface, _interface, PPB_INSTANCE_INTERFACE);
  99. result &= initializeInterface(s_ctx.m_graphicsInterface, _interface, PPB_GRAPHICS_3D_INTERFACE);
  100. result &= glInitializePPAPI(_interface);
  101. return result ? PP_OK : PP_ERROR_NOINTERFACE;
  102. }
  103. PP_EXPORT void PPP_ShutdownModule()
  104. {
  105. }
  106. #endif // BX_PLATFROM_NACL