1
0

service.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "main.h"
  2. #include "./service.h"
  3. #include "./wasabi.h"
  4. #include "./resource.h"
  5. #include "../replicant/nu/Autowide.h"
  6. #include "../winamp/wa_ipc.h"
  7. #include <strsafe.h>
  8. #define IS_INVALIDISPATCH(__disp) (((IDispatch *)1) == (__disp) || NULL == (__disp))
  9. OmService::OmService(UINT nId)
  10. : ref(1), id(nId), name(NULL), url(NULL), icon(NULL)
  11. {
  12. }
  13. OmService::~OmService()
  14. {
  15. Plugin_FreeResString(name);
  16. Plugin_FreeString(url);
  17. Plugin_FreeResString(icon);
  18. }
  19. HRESULT OmService::CreateInstance(OmService **instance)
  20. {
  21. if (NULL == instance) return E_POINTER;
  22. *instance = NULL;
  23. OmService *service = new OmService(SERVICE_ID);
  24. if (NULL == service) return E_OUTOFMEMORY;
  25. wchar_t nowplayingurl[1024] = {0};
  26. lstrcpynW(nowplayingurl, AutoWide(g_config->ReadString("nowplayingurl","")), ARRAYSIZE(nowplayingurl));
  27. service->SetName(MAKEINTRESOURCE(IDS_SERVICE_NAME));
  28. service->SetUrl((nowplayingurl[0] ? nowplayingurl : SERVICE_HOMEURL));
  29. service->SetIcon(MAKEINTRESOURCE(IDR_SERVICE_ICON));
  30. *instance = service;
  31. return S_OK;
  32. }
  33. size_t OmService::AddRef()
  34. {
  35. return InterlockedIncrement((LONG*)&ref);
  36. }
  37. size_t OmService::Release()
  38. {
  39. if (0 == ref)
  40. return ref;
  41. LONG r = InterlockedDecrement((LONG*)&ref);
  42. if (0 == r)
  43. delete(this);
  44. return r;
  45. }
  46. int OmService::QueryInterface(GUID interface_guid, void **object)
  47. {
  48. if (NULL == object) return E_POINTER;
  49. if (IsEqualIID(interface_guid, IFC_OmService))
  50. *object = static_cast<ifc_omservice*>(this);
  51. else
  52. {
  53. *object = NULL;
  54. return E_NOINTERFACE;
  55. }
  56. if (NULL == *object)
  57. return E_UNEXPECTED;
  58. AddRef();
  59. return S_OK;
  60. }
  61. unsigned int OmService::GetId()
  62. {
  63. return id;
  64. }
  65. HRESULT OmService::GetName(wchar_t *pszBuffer, int cchBufferMax)
  66. {
  67. return Plugin_CopyResString(pszBuffer, cchBufferMax, name);
  68. }
  69. HRESULT OmService::GetUrl(wchar_t *pszBuffer, int cchBufferMax)
  70. {
  71. return StringCchCopyEx(pszBuffer, cchBufferMax, url, NULL, NULL, STRSAFE_IGNORE_NULLS);
  72. }
  73. HRESULT OmService::GetIcon(wchar_t *pszBuffer, int cchBufferMax)
  74. {
  75. if (NULL != icon && IS_INTRESOURCE(icon))
  76. {
  77. WCHAR szPath[2*MAX_PATH] = {0};
  78. if (0 == GetModuleFileName(Plugin_GetInstance(), szPath, ARRAYSIZE(szPath)))
  79. return E_FAIL;
  80. return StringCchPrintf(pszBuffer, cchBufferMax, L"res://%s/#%d/#%d", szPath, RT_RCDATA, icon);
  81. }
  82. return StringCchCopyEx(pszBuffer, cchBufferMax, icon, NULL, NULL, STRSAFE_IGNORE_NULLS);
  83. }
  84. HRESULT OmService::GetExternal(IDispatch **ppDispatch)
  85. {
  86. if (NULL == ppDispatch)
  87. return E_POINTER;
  88. *ppDispatch = NULL;
  89. HWND hWinamp = Plugin_GetWinamp();
  90. if (NULL == hWinamp)
  91. return E_UNEXPECTED;
  92. // So far we do not use JSAPI2 in nowplaying
  93. // // try JSAPI2 first
  94. // WCHAR szBuffer[64] = {0};
  95. // if (SUCCEEDED(StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%u", id)))
  96. // *ppDispatch = (IDispatch*)SENDWAIPC(hWinamp, IPC_JSAPI2_GET_DISPATCH_OBJECT, (WPARAM)szBuffer);
  97. if (IS_INVALIDISPATCH(*ppDispatch))
  98. { // try JSAPI1
  99. *ppDispatch = (IDispatch*)SENDWAIPC(hWinamp, IPC_GET_DISPATCH_OBJECT, 0);
  100. if (IS_INVALIDISPATCH(*ppDispatch))
  101. { // Fail
  102. *ppDispatch = NULL;
  103. return E_FAIL;
  104. }
  105. }
  106. return S_OK;
  107. }
  108. HRESULT OmService::SetName(LPCWSTR pszName)
  109. {
  110. Plugin_FreeResString(name);
  111. name = Plugin_DuplicateResString(pszName);
  112. return S_OK;
  113. }
  114. HRESULT OmService::SetUrl(LPCWSTR pszUrl)
  115. {
  116. Plugin_FreeString(url);
  117. url = Plugin_CopyString(pszUrl);
  118. return S_OK;
  119. }
  120. HRESULT OmService::SetIcon(LPCWSTR pszIcon)
  121. {
  122. Plugin_FreeResString(icon);
  123. icon = Plugin_DuplicateResString(pszIcon);
  124. return S_OK;
  125. }
  126. #define CBCLASS OmService
  127. START_DISPATCH;
  128. CB(ADDREF, AddRef)
  129. CB(RELEASE, Release)
  130. CB(QUERYINTERFACE, QueryInterface)
  131. CB(API_GETID, GetId)
  132. CB(API_GETNAME, GetName)
  133. CB(API_GETURL, GetUrl)
  134. CB(API_GETICON, GetIcon)
  135. CB(API_GETEXTERNAL, GetExternal)
  136. END_DISPATCH;
  137. #undef CBCLASS