main.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "api.h"
  2. #include "../nu/ServiceWatcher.h"
  3. #include "../Agave/Component/ifc_wa5component.h"
  4. #include <api/service/waServiceFactory.h>
  5. #include "Factory.h"
  6. #include "ScriptFactory.h"
  7. Factory factory;
  8. static ScriptFactory scriptFactory;
  9. api_config *WASABI_API_CONFIG = 0;
  10. api_syscb *WASABI_API_SYSCB = 0;
  11. api_service *WASABI_API_SVC = 0;
  12. api_maki *WASABI_API_MAKI = 0;
  13. static ServiceWatcher serviceWatcher;
  14. class TimerComponent : public ifc_wa5component
  15. {
  16. public:
  17. void RegisterServices(api_service *service);
  18. int RegisterServicesSafeModeOk();
  19. void DeregisterServices(api_service *service);
  20. protected:
  21. RECVS_DISPATCH;
  22. };
  23. template <class api_t>
  24. api_t *GetService(GUID serviceGUID)
  25. {
  26. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(serviceGUID);
  27. if (sf)
  28. return (api_t *)sf->getInterface();
  29. else
  30. return 0;
  31. }
  32. inline void ReleaseService(GUID serviceGUID, void *service)
  33. {
  34. if (service)
  35. {
  36. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(serviceGUID);
  37. if (sf)
  38. sf->releaseInterface(service);
  39. }
  40. }
  41. void TimerComponent::RegisterServices(api_service *service)
  42. {
  43. WASABI_API_SVC = service;
  44. WASABI_API_SYSCB = GetService<api_syscb>(syscbApiServiceGuid);
  45. serviceWatcher.WatchWith(WASABI_API_SVC);
  46. serviceWatcher.WatchFor(&WASABI_API_CONFIG, configApiServiceGuid);
  47. serviceWatcher.WatchFor(&WASABI_API_MAKI, makiApiServiceGuid);
  48. service->service_register(&factory);
  49. service->service_register(&scriptFactory);
  50. // register for service callbacks in case any of these don't exist yet
  51. WASABI_API_SYSCB->syscb_registerCallback(&serviceWatcher);
  52. }
  53. int TimerComponent::RegisterServicesSafeModeOk()
  54. {
  55. return 1;
  56. }
  57. void TimerComponent::DeregisterServices(api_service *service)
  58. {
  59. service->service_deregister(&factory);
  60. service->service_deregister(&scriptFactory);
  61. serviceWatcher.StopWatching();
  62. serviceWatcher.Clear();
  63. ReleaseService(configApiServiceGuid, WASABI_API_CONFIG);
  64. ReleaseService(syscbApiServiceGuid, WASABI_API_SYSCB);
  65. ReleaseService(makiApiServiceGuid, WASABI_API_MAKI);
  66. factory.Stop();
  67. }
  68. TimerComponent timerComponent;
  69. extern "C" __declspec(dllexport) ifc_wa5component *GetWinamp5SystemComponent()
  70. {
  71. return &timerComponent;
  72. }
  73. #define CBCLASS TimerComponent
  74. START_DISPATCH;
  75. VCB(API_WA5COMPONENT_REGISTERSERVICES, RegisterServices)
  76. CB(15, RegisterServicesSafeModeOk)
  77. VCB(API_WA5COMPONENT_DEREEGISTERSERVICES, DeregisterServices)
  78. END_DISPATCH;
  79. #undef CBCLASS