main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "api__tataki.h"
  2. #include <api/service/waservicefactory.h>
  3. #include <tataki/export.h>
  4. #include <tataki/canvas/bltcanvas.h>
  5. #include "../nu/ServiceWatcher.h"
  6. api_service *WASABI_API_SVC=0;
  7. api_application *WASABI_API_APP=0;
  8. api_syscb *WASABI_API_SYSCB=0;
  9. api_font *WASABI_API_FONT=0;
  10. wnd_api *WASABI_API_WND=0;
  11. api_config *WASABI_API_CONFIG=0;
  12. imgldr_api *WASABI_API_IMGLDR=0;
  13. api_memmgr *WASABI_API_MEMMGR=0;
  14. api_skin *WASABI_API_SKIN=0;
  15. api_random *AGAVE_API_RANDOM=0;
  16. api_palette *WASABI_API_PALETTE=0;
  17. DWORD bitmap_cache_tls=TLS_OUT_OF_INDEXES;
  18. ServiceWatcher serviceWatcher;
  19. template <class api_t>
  20. api_t *GetService(GUID serviceGUID)
  21. {
  22. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(serviceGUID);
  23. if (sf)
  24. return reinterpret_cast<api_t *>( sf->getInterface() );
  25. else
  26. return 0;
  27. }
  28. template <class api_t>
  29. inline void ReleaseService(GUID serviceGUID, api_t *&service)
  30. {
  31. if (service)
  32. {
  33. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(serviceGUID);
  34. if (sf)
  35. sf->releaseInterface(service);
  36. service=0;
  37. }
  38. }
  39. static size_t refCount=0;
  40. TATAKIAPI size_t Tataki::Init(api_service *_serviceApi)
  41. {
  42. refCount++;
  43. if (bitmap_cache_tls == TLS_OUT_OF_INDEXES)
  44. bitmap_cache_tls=TlsAlloc();
  45. if (!WASABI_API_SVC)
  46. {
  47. WASABI_API_SVC = _serviceApi;
  48. WASABI_API_SYSCB = GetService<api_syscb>(syscbApiServiceGuid);
  49. AGAVE_API_RANDOM = GetService<api_random>(randomApiGUID);
  50. WASABI_API_PALETTE = GetService<api_palette>(PaletteManagerGUID);
  51. serviceWatcher.WatchWith(WASABI_API_SVC);
  52. serviceWatcher.WatchFor(&WASABI_API_APP, applicationApiServiceGuid);
  53. serviceWatcher.WatchFor(&WASABI_API_FONT, fontApiServiceGuid);
  54. serviceWatcher.WatchFor(&WASABI_API_WND, wndApiServiceGuid);
  55. serviceWatcher.WatchFor(&WASABI_API_CONFIG, configApiServiceGuid);
  56. serviceWatcher.WatchFor(&WASABI_API_IMGLDR, imgLdrApiServiceGuid);
  57. serviceWatcher.WatchFor(&WASABI_API_MEMMGR, memMgrApiServiceGuid);
  58. serviceWatcher.WatchFor(&WASABI_API_SKIN, skinApiServiceGuid);
  59. // register for service callbacks in case any of these don't exist yet
  60. WASABI_API_SYSCB->syscb_registerCallback(&serviceWatcher);
  61. }
  62. return refCount;
  63. }
  64. TATAKIAPI size_t Tataki::Quit()
  65. {
  66. if (!--refCount)
  67. {
  68. serviceWatcher.StopWatching();
  69. serviceWatcher.Clear();
  70. ReleaseService(syscbApiServiceGuid, WASABI_API_SYSCB);
  71. ReleaseService(applicationApiServiceGuid, WASABI_API_APP);
  72. ReleaseService(fontApiServiceGuid,WASABI_API_FONT);
  73. ReleaseService(wndApiServiceGuid, WASABI_API_WND);
  74. ReleaseService(configApiServiceGuid, WASABI_API_CONFIG);
  75. ReleaseService(imgLdrApiServiceGuid, WASABI_API_IMGLDR);
  76. ReleaseService(memMgrApiServiceGuid, WASABI_API_MEMMGR);
  77. ReleaseService(skinApiServiceGuid, WASABI_API_SKIN);
  78. ReleaseService(randomApiGUID, AGAVE_API_RANDOM);
  79. ReleaseService(PaletteManagerGUID, WASABI_API_PALETTE);
  80. // unregister callbacks
  81. // release any services we have
  82. WASABI_API_SVC = 0;
  83. }
  84. return refCount;
  85. }
  86. extern "C" BOOL WINAPI DllMain(
  87. HINSTANCE hinstDLL,
  88. DWORD fdwReason,
  89. LPVOID lpvReserved
  90. )
  91. {
  92. Wasabi::Std::Initialize();
  93. if (fdwReason == DLL_PROCESS_DETACH && bitmap_cache_tls!=TLS_OUT_OF_INDEXES)
  94. {
  95. TlsFree(bitmap_cache_tls);
  96. bitmap_cache_tls=TLS_OUT_OF_INDEXES;
  97. }
  98. else if (fdwReason == DLL_THREAD_DETACH && bitmap_cache_tls!=TLS_OUT_OF_INDEXES)
  99. {
  100. BltCanvas *cache_canvas = (BltCanvas *)TlsGetValue(bitmap_cache_tls);
  101. if (cache_canvas)
  102. {
  103. delete cache_canvas;
  104. TlsSetValue(bitmap_cache_tls, 0); // this is probably unnecessary but just in case
  105. }
  106. }
  107. return TRUE;
  108. }