1
0

JSAPI2_PlayerAPI.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "JSAPI2_PlayerAPI.h"
  2. #include "JSAPI2_Security.h"
  3. #include "main.h"
  4. #include "JSAPI.h"
  5. #include "ipc_pe.h"
  6. #include <strsafe.h>
  7. JSAPI2::PlayerAPI::PlayerAPI(const wchar_t *_key, JSAPI::ifc_info *_info)
  8. {
  9. info = _info;
  10. key = _key;
  11. refCount = 1;
  12. }
  13. enum
  14. {
  15. DISPID_PLAYERAPI_PLAY, // start playing a file immediately (and clear the old playlist)
  16. DISPID_PLAYERAPI_ENQUEUE, // insert a URL at the end of the playlist
  17. DISPID_PLAYERAPI_INSERT, // insert a URL at a particular position in the playlist
  18. DISPID_PLAYERAPI_CLEARQUEUE, // clear the playlist
  19. DISPID_PLAYERAPI_GETURL, // get the URL (or filename) for an item in the playlist
  20. DISPID_PLAYERAPI_GETTITLE, // get title for an item in the playlist
  21. DISPID_PLAYERAPI_GETMETADATA, // get a metadata string for an item in the playlist
  22. // properties
  23. DISPID_PLAYERAPI_LENGTH, // length of the playlist
  24. DISPID_PLAYERAPI_POSITION, // position in the playlist
  25. };
  26. #define DISP_TABLE \
  27. CHECK_ID(Play, DISPID_PLAYERAPI_PLAY)\
  28. CHECK_ID(Enqueue, DISPID_PLAYERAPI_ENQUEUE)\
  29. CHECK_ID(Insert, DISPID_PLAYERAPI_INSERT)\
  30. CHECK_ID(ClearQueue, DISPID_PLAYERAPI_CLEARQUEUE)\
  31. CHECK_ID(GetURL, DISPID_PLAYERAPI_GETURL)\
  32. CHECK_ID(GetTitle, DISPID_PLAYERAPI_GETTITLE)\
  33. CHECK_ID(GetMetadata, DISPID_PLAYERAPI_GETMETADATA)\
  34. CHECK_ID(length, DISPID_PLAYERAPI_LENGTH)\
  35. CHECK_ID(cursor, DISPID_PLAYERAPI_POSITION)\
  36. #define CHECK_ID(str, id)\
  37. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, rgszNames[i], -1, L## #str, -1))\
  38. { rgdispid[i] = id; continue; }
  39. HRESULT JSAPI2::PlayerAPI::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  40. {
  41. bool unknowns = false;
  42. for (unsigned int i = 0;i != cNames;i++)
  43. {
  44. DISP_TABLE
  45. rgdispid[i] = DISPID_UNKNOWN;
  46. unknowns = true;
  47. }
  48. if (unknowns)
  49. return DISP_E_UNKNOWNNAME;
  50. else
  51. return S_OK;
  52. }
  53. HRESULT JSAPI2::PlayerAPI::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  54. {
  55. return E_NOTIMPL;
  56. }
  57. HRESULT JSAPI2::PlayerAPI::GetTypeInfoCount(unsigned int FAR * pctinfo)
  58. {
  59. return E_NOTIMPL;
  60. }
  61. HRESULT JSAPI2::PlayerAPI::Play(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  62. {
  63. JSAPI_VERIFY_METHOD(wFlags);
  64. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 3);
  65. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  66. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 2, VT_BSTR, puArgErr);
  67. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 3, VT_I4, puArgErr);
  68. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  69. if (security.GetActionAuthorization(L"player", L"playlist", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  70. {
  71. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  72. enqueueFileWithMetaStructW s = {0,0,0,0};
  73. SendMessageW(hMainWindow, WM_WA_IPC, 0, IPC_DELETE);
  74. //PlayList_delete();
  75. s.filename = JSAPI_PARAM(pdispparams, 1).bstrVal;
  76. s.title = JSAPI_PARAM_OPTIONAL(pdispparams, 2, bstrVal, 0);
  77. s.ext = NULL;
  78. s.length = JSAPI_PARAM_OPTIONAL(pdispparams, 3, lVal, 0);
  79. /*if (title)
  80. PlayList_append_withinfo(filename, title, length);
  81. else
  82. PlayList_appendthing(filename, 0);
  83. plEditRefresh();
  84. */
  85. SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)&s, IPC_PLAYFILEW);
  86. SendMessageW(hMainWindow, WM_WA_IPC, 0, IPC_STARTPLAY);
  87. }
  88. else
  89. {
  90. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  91. }
  92. return S_OK;
  93. }
  94. HRESULT JSAPI2::PlayerAPI::Enqueue(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  95. {
  96. JSAPI_VERIFY_METHOD(wFlags);
  97. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 3);
  98. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  99. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 2, VT_BSTR, puArgErr);
  100. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 3, VT_I4, puArgErr);
  101. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  102. if (security.GetActionAuthorization(L"player", L"playlist", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  103. {
  104. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  105. enqueueFileWithMetaStructW s = {0,0,0,0};
  106. //PlayList_delete();
  107. s.filename = JSAPI_PARAM(pdispparams, 1).bstrVal;
  108. s.title = JSAPI_PARAM_OPTIONAL(pdispparams, 2, bstrVal, 0);
  109. s.ext = NULL;
  110. s.length = JSAPI_PARAM_OPTIONAL(pdispparams, 3, lVal, 0);
  111. /*if (title)
  112. PlayList_append_withinfo(filename, title, length);
  113. else
  114. PlayList_appendthing(filename, 0);
  115. plEditRefresh();
  116. */
  117. SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)&s, IPC_PLAYFILEW);
  118. }
  119. else
  120. {
  121. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  122. }
  123. return S_OK;
  124. }
  125. HRESULT JSAPI2::PlayerAPI::Insert(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  126. {
  127. JSAPI_VERIFY_METHOD(wFlags);
  128. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 2, 4);
  129. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  130. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  131. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 3, VT_BSTR, puArgErr);
  132. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 4, VT_I4, puArgErr);
  133. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  134. if (security.GetActionAuthorization(L"player", L"playlist", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  135. {
  136. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  137. fileinfoW info;
  138. COPYDATASTRUCT cds;
  139. cds.dwData = IPC_PE_INSERTFILENAMEW;
  140. cds.lpData = &info;
  141. cds.cbData = sizeof(info);
  142. StringCbCopyW(info.file, sizeof(info.file), JSAPI_PARAM(pdispparams, 2).bstrVal);
  143. info.index = JSAPI_PARAM(pdispparams, 1).lVal;
  144. // benski> TODO const wchar_t *title = JSAPI_PARAM_OPTIONAL(pdispparams, 3, bstrVal, 0);
  145. // benski> TODO int length = JSAPI_PARAM_OPTIONAL(pdispparams, 4, lVal, 0);
  146. SendMessageW(hPLWindow, WM_COPYDATA, 0, (LPARAM)&cds);
  147. }
  148. else
  149. {
  150. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  151. }
  152. return S_OK;
  153. }
  154. HRESULT JSAPI2::PlayerAPI::ClearQueue(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  155. {
  156. JSAPI_VERIFY_METHOD(wFlags);
  157. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  158. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  159. if (security.GetActionAuthorization(L"player", L"playlist", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  160. {
  161. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  162. SendMessageW(hMainWindow, WM_WA_IPC, 0, IPC_DELETE);
  163. }
  164. else
  165. {
  166. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  167. }
  168. return S_OK;
  169. }
  170. HRESULT JSAPI2::PlayerAPI::GetURL(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  171. {
  172. JSAPI_VERIFY_METHOD(wFlags);
  173. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  174. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  175. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  176. if (security.GetActionAuthorization(L"player", L"metadata", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  177. {
  178. int position = JSAPI_PARAM(pdispparams, 1).lVal;
  179. wchar_t filename[FILENAME_SIZE] = {0};
  180. if (PlayList_getitem2W(position, filename, 0) == 0)
  181. {
  182. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(filename));
  183. }
  184. else
  185. {
  186. JSAPI_EMPTY_RESULT(pvarResult);
  187. }
  188. }
  189. else
  190. {
  191. JSAPI_EMPTY_RESULT(pvarResult);
  192. }
  193. return S_OK;
  194. }
  195. HRESULT JSAPI2::PlayerAPI::GetTitle(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  196. {
  197. JSAPI_VERIFY_METHOD(wFlags);
  198. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  199. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  200. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  201. if (security.GetActionAuthorization(L"player", L"metadata", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  202. {
  203. int position = JSAPI_PARAM(pdispparams, 1).lVal;
  204. wchar_t filetitle[FILETITLE_SIZE] = {0};
  205. if (PlayList_getitem2W(position, 0, filetitle) == 0)
  206. {
  207. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(filetitle));
  208. }
  209. else
  210. {
  211. JSAPI_EMPTY_RESULT(pvarResult);
  212. }
  213. }
  214. else
  215. {
  216. JSAPI_EMPTY_RESULT(pvarResult);
  217. }
  218. return S_OK;
  219. }
  220. HRESULT JSAPI2::PlayerAPI::GetMetadata(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  221. {
  222. JSAPI_VERIFY_METHOD(wFlags);
  223. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  224. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  225. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  226. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  227. if (security.GetActionAuthorization(L"player", L"metadata", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  228. {
  229. int position = JSAPI_PARAM(pdispparams, 1).lVal;
  230. wchar_t filename[FILENAME_SIZE] = {0};
  231. if (PlayList_getitem2W(position, filename, 0) == 0)
  232. {
  233. wchar_t buffer[4096] = {0};
  234. extendedFileInfoStructW info;
  235. info.filename = filename;
  236. info.metadata = JSAPI_PARAM(pdispparams, 2).bstrVal;
  237. info.ret = buffer;
  238. info.retlen = sizeof(buffer)/sizeof(*buffer);
  239. if (NULL != info.filename &&
  240. NULL != info.metadata)
  241. {
  242. if (0 == SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)&info, IPC_GET_EXTENDED_FILE_INFOW_HOOKABLE))
  243. info.ret = NULL;
  244. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(info.ret));
  245. }
  246. else
  247. JSAPI_EMPTY_RESULT(pvarResult);
  248. }
  249. else
  250. {
  251. JSAPI_EMPTY_RESULT(pvarResult);
  252. }
  253. }
  254. else
  255. {
  256. JSAPI_EMPTY_RESULT(pvarResult);
  257. }
  258. return S_OK;
  259. }
  260. HRESULT JSAPI2::PlayerAPI::length(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  261. {
  262. if (wFlags & DISPATCH_PROPERTYGET)
  263. {
  264. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  265. VariantInit(pvarResult);
  266. V_VT(pvarResult) = VT_I4;
  267. V_I4(pvarResult) = PlayList_getlength();
  268. return S_OK;
  269. }
  270. return DISP_E_MEMBERNOTFOUND;
  271. }
  272. HRESULT JSAPI2::PlayerAPI::cursor(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  273. {
  274. if (wFlags & DISPATCH_PROPERTYPUT)
  275. {
  276. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  277. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  278. if (security.GetActionAuthorization(L"player", L"playlist", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  279. {
  280. PlayList_setposition(JSAPI_PARAM(pdispparams, 1).lVal);
  281. plEditRefresh();
  282. }
  283. return S_OK;
  284. }
  285. else if (wFlags & DISPATCH_PROPERTYGET)
  286. {
  287. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  288. VariantInit(pvarResult);
  289. V_VT(pvarResult) = VT_I4;
  290. V_I4(pvarResult) = PlayList_getPosition();
  291. return S_OK;
  292. }
  293. return DISP_E_MEMBERNOTFOUND;
  294. }
  295. #undef CHECK_ID
  296. #define CHECK_ID(str, id) case id: return str(wFlags, pdispparams, pvarResult, puArgErr);
  297. HRESULT JSAPI2::PlayerAPI::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  298. {
  299. switch (dispid)
  300. {
  301. DISP_TABLE
  302. }
  303. return DISP_E_MEMBERNOTFOUND;
  304. }
  305. STDMETHODIMP JSAPI2::PlayerAPI::QueryInterface(REFIID riid, PVOID *ppvObject)
  306. {
  307. if (!ppvObject)
  308. return E_POINTER;
  309. else if (IsEqualIID(riid, IID_IDispatch))
  310. *ppvObject = (IDispatch *)this;
  311. else if (IsEqualIID(riid, IID_IUnknown))
  312. *ppvObject = this;
  313. else
  314. {
  315. *ppvObject = NULL;
  316. return E_NOINTERFACE;
  317. }
  318. AddRef();
  319. return S_OK;
  320. }
  321. ULONG JSAPI2::PlayerAPI::AddRef(void)
  322. {
  323. return InterlockedIncrement(&refCount);
  324. }
  325. ULONG JSAPI2::PlayerAPI::Release(void)
  326. {
  327. LONG lRef = InterlockedDecrement(&refCount);
  328. if (lRef == 0) delete this;
  329. return lRef;
  330. }