wasabi.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "main.h"
  2. #include "./wasabi.h"
  3. #include <api/service/waservicefactory.h>
  4. api_service *WASABI_API_SVC = NULL;
  5. api_application *WASABI_API_APP = NULL;
  6. api_language *WASABI_API_LNG = NULL;
  7. api_devicemanager *WASABI_API_DEVICES = NULL;
  8. HINSTANCE WASABI_API_LNG_HINST = NULL;
  9. HINSTANCE WASABI_API_ORIG_HINST = NULL;
  10. static unsigned long wasabiReference = 0;
  11. static BOOL defaultServicesLoaded = FALSE;
  12. static void
  13. Wasabi_Uninitialize()
  14. {
  15. if (NULL != WASABI_API_SVC)
  16. {
  17. Wasabi_ReleaseInterface(applicationApiServiceGuid, WASABI_API_APP);
  18. Wasabi_ReleaseInterface(languageApiGUID, WASABI_API_LNG);
  19. Wasabi_ReleaseInterface(DeviceManagerGUID, WASABI_API_DEVICES);
  20. }
  21. WASABI_API_SVC = NULL;
  22. WASABI_API_APP = NULL;
  23. WASABI_API_LNG = NULL;
  24. WASABI_API_DEVICES = NULL;
  25. defaultServicesLoaded = FALSE;
  26. }
  27. BOOL
  28. Wasabi_Initialize(HINSTANCE instance, api_service *serviceMngr)
  29. {
  30. if (NULL != WASABI_API_SVC)
  31. return FALSE;
  32. defaultServicesLoaded = FALSE;
  33. WASABI_API_SVC = serviceMngr;
  34. if ((api_service*)1 == WASABI_API_SVC)
  35. WASABI_API_SVC = NULL;
  36. if (NULL == WASABI_API_SVC)
  37. return FALSE;
  38. WASABI_API_APP = NULL;
  39. WASABI_API_DEVICES = NULL;
  40. WASABI_API_LNG = NULL;
  41. WASABI_API_ORIG_HINST = instance;
  42. WASABI_API_LNG_HINST = WASABI_API_ORIG_HINST;
  43. Wasabi_AddRef();
  44. return TRUE;
  45. }
  46. BOOL
  47. Wasabi_InitializeFromWinamp(HINSTANCE instance, HWND winampWindow)
  48. {
  49. api_service *serviceMngr;
  50. serviceMngr = (api_service*)SENDWAIPC(winampWindow, IPC_GET_API_SERVICE, 0);
  51. return Wasabi_Initialize(instance, serviceMngr);
  52. }
  53. BOOL
  54. Wasabi_LoadDefaultServices(void)
  55. {
  56. if (NULL == WASABI_API_SVC)
  57. return FALSE;
  58. if (FALSE != defaultServicesLoaded)
  59. return FALSE;
  60. WASABI_API_APP = Wasabi_QueryInterface(api_application, applicationApiServiceGuid);
  61. WASABI_API_DEVICES = Wasabi_QueryInterface(api_devicemanager, DeviceManagerGUID);
  62. WASABI_API_LNG = Wasabi_QueryInterface(api_language, languageApiGUID);
  63. if (NULL != WASABI_API_LNG)
  64. {
  65. WASABI_API_LNG_HINST = WASABI_API_LNG->StartLanguageSupport(WASABI_API_ORIG_HINST,
  66. PLUGIN_LANGUAGE_ID);
  67. }
  68. defaultServicesLoaded = TRUE;
  69. return TRUE;
  70. }
  71. unsigned long
  72. Wasabi_AddRef(void)
  73. {
  74. return InterlockedIncrement((LONG*)&wasabiReference);
  75. }
  76. unsigned long
  77. Wasabi_Release(void)
  78. {
  79. if (0 == wasabiReference)
  80. return wasabiReference;
  81. LONG r = InterlockedDecrement((LONG*)&wasabiReference);
  82. if (0 == r)
  83. {
  84. Wasabi_Uninitialize();
  85. }
  86. return r;
  87. }
  88. void *
  89. Wasabi_QueryInterface0(const GUID &interfaceGuid)
  90. {
  91. waServiceFactory *serviceFactory;
  92. if (NULL == WASABI_API_SVC)
  93. return NULL;
  94. serviceFactory = WASABI_API_SVC->service_getServiceByGuid(interfaceGuid);
  95. if (NULL == serviceFactory)
  96. return NULL;
  97. return serviceFactory->getInterface();
  98. }
  99. void
  100. Wasabi_ReleaseInterface0(const GUID &interfaceGuid, void *interfaceInstance)
  101. {
  102. waServiceFactory *serviceFactory;
  103. if (NULL == WASABI_API_SVC)
  104. return;
  105. serviceFactory = WASABI_API_SVC->service_getServiceByGuid(interfaceGuid);
  106. if (NULL == serviceFactory)
  107. return;
  108. serviceFactory->releaseInterface(interfaceInstance);
  109. }