PlaylistScriptObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // -----------------------------------------------------------------------------------------------------
  2. // Playlist Script Object
  3. //
  4. // functions for <pledit.mi>
  5. // -----------------------------------------------------------------------------------------------------
  6. #include <precomp.h>
  7. #include "main.h"
  8. #include "PlaylistScriptObject.h"
  9. #include <bfc/wasabi_std.h>
  10. #include <api/script/script.h>
  11. #include <api/script/scriptmgr.h>
  12. #include <api/script/scriptobj.h>
  13. #include <api/script/scriptguid.h>
  14. #ifdef WASABI_COMPILE_MEDIACORE
  15. #include <api/service/svcs/svc_player.h>
  16. #include <api/core/buttons.h>
  17. #include <api/core/api_core.h>
  18. #include <api/core/corehandle.h> // safe to include even if core isn't there
  19. #endif
  20. static PlaylistScriptController _playlistController;
  21. ScriptObjectController *playlistController = &_playlistController;
  22. #ifndef _WASABIRUNTIME
  23. BEGIN_SERVICES(PlaylistObject_Svc);
  24. DECLARE_SERVICE(ScriptObjectCreator<PlaylistScriptObjectSvc>);
  25. END_SERVICES(PlaylistObject_Svc, _PlaylistObject_Svc);
  26. #ifdef _X86_
  27. extern "C" { int _link_PlaylistObjectSvc; }
  28. #else
  29. extern "C" { int __link_PlaylistObjectSvc; }
  30. #endif
  31. #endif
  32. // -----------------------------------------------------------------------------------------------------
  33. // Functions Table
  34. function_descriptor_struct PlaylistScriptController::exportedFunction[] =
  35. {
  36. {L"showCurrentlyPlayingTrack", 0, (void*)SPlaylist::script_vcpu_showCurrentlyPlayingEntry},
  37. {L"showTrack", 1, (void*)SPlaylist::script_vcpu_showEntry},
  38. {L"getNumTracks", 0, (void*)SPlaylist::script_vcpu_getNumItems},
  39. {L"getCurrentIndex", 0, (void*)SPlaylist::script_vcpu_getCurrentIndex},
  40. {L"getRating", 1, (void*)SPlaylist::script_vcpu_getTrackRating},
  41. {L"setRating", 2, (void*)SPlaylist::script_vcpu_setTrackRating},
  42. {L"enqueueFile", 1, (void*)SPlaylist::script_vcpu_enqueueFile },
  43. {L"clear", 0, (void*)SPlaylist::script_vcpu_clear },
  44. {L"removeTrack", 1, (void*)SPlaylist::script_vcpu_removeTrack },
  45. {L"swapTracks", 2, (void*)SPlaylist::script_vcpu_swapTrack },
  46. {L"moveUp", 1, (void*)SPlaylist::script_vcpu_moveUp },
  47. {L"moveDown", 1, (void*)SPlaylist::script_vcpu_moveDown },
  48. {L"moveTo", 2, (void*)SPlaylist::script_vcpu_moveTo },
  49. {L"getTitle", 1, (void*)SPlaylist::script_vcpu_getTitle },
  50. {L"getLength", 1, (void*)SPlaylist::script_vcpu_getLength },
  51. {L"getMetaData", 2, (void*)SPlaylist::script_vcpu_getExtendedInfo },
  52. {L"getNumSelectedTracks", 0, (void*)SPlaylist::script_vcpu_getNumSelectedItems },
  53. {L"getNextSelectedTrack", 1, (void*)SPlaylist::script_vcpu_getNextSelectedItem },
  54. {L"getFileName", 1, (void*)SPlaylist::script_vcpu_getFileName },
  55. {L"playTrack", 1, (void*)SPlaylist::script_vcpu_playTrack },
  56. {L"onPleditModified", 0, (void*)SPlaylist::script_vcpu_onPleditModified },
  57. };
  58. ScriptObjectController *PlaylistScriptObjectSvc::getController(int n)
  59. {
  60. switch (n) {
  61. case 0:
  62. return playlistController;
  63. }
  64. return NULL;
  65. }
  66. ScriptObject *PlaylistScriptController::instantiate()
  67. {
  68. SPlaylist *c = new SPlaylist;
  69. if (!c) return NULL;
  70. return c->getScriptObject();
  71. }
  72. void PlaylistScriptController::destroy(ScriptObject *o)
  73. {
  74. SPlaylist *obj = static_cast<SPlaylist *>(o->vcpu_getInterface(playlistScriptObjectGUID));
  75. ASSERT(obj != NULL);
  76. delete obj;
  77. }
  78. void *PlaylistScriptController::encapsulate(ScriptObject *o)
  79. {
  80. return NULL;
  81. }
  82. void PlaylistScriptController::deencapsulate(void *o)
  83. {
  84. }
  85. int PlaylistScriptController::getNumFunctions()
  86. {
  87. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  88. }
  89. SPlaylist::SPlaylist()
  90. {
  91. getScriptObject()->vcpu_setInterface(playlistScriptObjectGUID, (void *)static_cast<SPlaylist *>(this));
  92. getScriptObject()->vcpu_setClassName(L"PlEdit");
  93. getScriptObject()->vcpu_setController(playlistController);
  94. SOList.addItem(this);
  95. }
  96. SPlaylist::~SPlaylist()
  97. {
  98. SOList.removeItem(this);
  99. }
  100. // Script helper functions
  101. void SPlaylist::onPleditModified ()
  102. {
  103. //This one is not working
  104. for (int i=0; i < SOList.getNumItems(); i++)
  105. {
  106. //script_vcpu_onPleditModified(SCRIPT_CALL, getScriptObject());
  107. script_vcpu_onPleditModified(SCRIPT_CALL, SOList.enumItem(i)->getScriptObject());
  108. //Std::messageBox(SOList.enumItem(i)->getClassName(), t, 0);
  109. }
  110. }
  111. void SPlaylist::showEntry( int i)
  112. {
  113. HWND hPeWindow = wa2.getWnd(IPC_GETWND_PE);
  114. SendMessageW(hPeWindow, WM_USER, 666, i);
  115. }
  116. void SPlaylist::swap(int track1, int track2)
  117. {
  118. HWND hPeWindow = wa2.getWnd(IPC_GETWND_PE);
  119. int i = wa2.PE_getCurrentIndex();
  120. int param = ((track1) << 16) | (track2);
  121. SendMessageW(hPeWindow, WM_USER, IPC_PE_SWAPINDEX, param);
  122. //Swap our currently playing item as well
  123. if (i == track1)
  124. {
  125. wa2.PE_setCurrentIndex(track2);
  126. }
  127. else if (i == track2)
  128. {
  129. wa2.PE_setCurrentIndex(track1);
  130. }
  131. }
  132. fileinfoW *SPlaylist::getFileInfoStructW1 (int item)
  133. {
  134. static fileinfoW fi;
  135. fi.index = item;
  136. *(fi.file) = 0;
  137. HWND hPeWindow = wa2.getWnd(IPC_GETWND_PE);
  138. SendMessageW(hPeWindow, WM_USER, IPC_PE_GETINDEXINFOW_INPROC, (LPARAM)&fi);
  139. return &fi;
  140. }
  141. // Script Calls
  142. scriptVar SPlaylist::script_vcpu_showCurrentlyPlayingEntry(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  143. {
  144. SCRIPT_FUNCTION_INIT
  145. showEntry (wa2.PE_getCurrentIndex());
  146. RETURN_SCRIPT_VOID;
  147. }
  148. scriptVar SPlaylist::script_vcpu_showEntry(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar i)
  149. {
  150. SCRIPT_FUNCTION_INIT
  151. showEntry (GET_SCRIPT_INT(i));
  152. RETURN_SCRIPT_VOID;
  153. }
  154. scriptVar SPlaylist::script_vcpu_getNumItems(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  155. {
  156. SCRIPT_FUNCTION_INIT
  157. int i = wa2.PE_getNumItems();
  158. return MAKE_SCRIPT_INT(i);
  159. }
  160. scriptVar SPlaylist::script_vcpu_getCurrentIndex(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  161. {
  162. SCRIPT_FUNCTION_INIT
  163. int i = wa2.PE_getCurrentIndex();
  164. return MAKE_SCRIPT_INT(i);
  165. }
  166. scriptVar SPlaylist::script_vcpu_setTrackRating(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar i, scriptVar rating)
  167. {
  168. SCRIPT_FUNCTION_INIT
  169. HWND hwnd_winamp = wa2.getMainWindow();
  170. int cur_pos = wa2.PE_getCurrentIndex();
  171. wa2.PE_setCurrentIndex(GET_SCRIPT_INT(i));
  172. SendMessageW(hwnd_winamp, WM_WA_IPC, GET_SCRIPT_INT(rating), IPC_SETRATING);
  173. wa2.PE_setCurrentIndex(cur_pos);
  174. RETURN_SCRIPT_VOID;
  175. }
  176. scriptVar SPlaylist::script_vcpu_getTrackRating(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar i)
  177. {
  178. SCRIPT_FUNCTION_INIT
  179. HWND hwnd_winamp = wa2.getMainWindow();
  180. int cur_pos = wa2.PE_getCurrentIndex();
  181. wa2.PE_setCurrentIndex(GET_SCRIPT_INT(i));
  182. int r = 0;
  183. r = SendMessageW(hwnd_winamp, WM_WA_IPC, GET_SCRIPT_INT(i), IPC_GETRATING);
  184. wa2.PE_setCurrentIndex(cur_pos);
  185. return MAKE_SCRIPT_INT(r);
  186. }
  187. scriptVar SPlaylist::script_vcpu_enqueueFile(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar file)
  188. {
  189. SCRIPT_FUNCTION_INIT;
  190. wa2.enqueueFile (GET_SCRIPT_STRING(file));
  191. RETURN_SCRIPT_VOID;
  192. }
  193. scriptVar SPlaylist::script_vcpu_clear(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  194. {
  195. SCRIPT_FUNCTION_INIT;
  196. wa2.clearPlaylist();
  197. RETURN_SCRIPT_VOID;
  198. }
  199. scriptVar SPlaylist::script_vcpu_removeTrack(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar i)
  200. {
  201. SCRIPT_FUNCTION_INIT;
  202. HWND hPeWindow = wa2.getWnd(IPC_GETWND_PE);
  203. SendMessageW(hPeWindow, WM_USER, IPC_PE_DELETEINDEX, GET_SCRIPT_INT(i));
  204. RETURN_SCRIPT_VOID;
  205. }
  206. scriptVar SPlaylist::script_vcpu_swapTrack(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track1, scriptVar _track2)
  207. {
  208. SCRIPT_FUNCTION_INIT;
  209. int track1 = GET_SCRIPT_INT(_track1);
  210. int track2 = GET_SCRIPT_INT(_track2);
  211. if (track1 >= wa2.PE_getNumItems() || track1 < 0 || track2 >= wa2.PE_getNumItems() || track2 < 0) RETURN_SCRIPT_VOID;
  212. swap ((track1), (track2));
  213. RETURN_SCRIPT_VOID;
  214. }
  215. scriptVar SPlaylist::script_vcpu_moveUp(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  216. {
  217. SCRIPT_FUNCTION_INIT;
  218. int track = GET_SCRIPT_INT(_track);
  219. if (track < wa2.PE_getNumItems()-1) swap (track, track+1);
  220. RETURN_SCRIPT_VOID;
  221. }
  222. scriptVar SPlaylist::script_vcpu_moveDown(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  223. {
  224. SCRIPT_FUNCTION_INIT;
  225. int track = GET_SCRIPT_INT(_track);
  226. if (track > 0) swap (track, track-1);
  227. RETURN_SCRIPT_VOID;
  228. }
  229. scriptVar SPlaylist::script_vcpu_moveTo(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track, scriptVar _pos)
  230. {
  231. SCRIPT_FUNCTION_INIT;
  232. int track = GET_SCRIPT_INT(_track);
  233. int pos = GET_SCRIPT_INT(_pos);
  234. if (track == pos || pos >= wa2.PE_getNumItems() || pos < 0 || track >= wa2.PE_getNumItems() || track < 0)
  235. {
  236. RETURN_SCRIPT_VOID;
  237. }
  238. // Martin> This can be done much faster :P
  239. /*if (track > pos)
  240. {
  241. for (int i = track; i > pos; i--)
  242. {
  243. swap (i, i-1);
  244. }
  245. }
  246. else
  247. {
  248. for (int i = track; i < pos; i++)
  249. {
  250. swap (i, i+1);
  251. }
  252. }*/
  253. HWND hPeWindow = wa2.getWnd(IPC_GETWND_PE);
  254. fileinfoW *fi = getFileInfoStructW1(track);
  255. SendMessageW(hPeWindow, WM_USER, IPC_PE_DELETEINDEX, fi->index);
  256. fi->index = pos;
  257. static COPYDATASTRUCT cds;
  258. cds.dwData = IPC_PE_INSERTFILENAMEW;
  259. cds.lpData = fi;
  260. SendMessageW(hPeWindow, WM_COPYDATA, NULL, (LPARAM)&cds);
  261. RETURN_SCRIPT_VOID;
  262. }
  263. scriptVar SPlaylist::script_vcpu_getTitle(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  264. {
  265. SCRIPT_FUNCTION_INIT;
  266. int track = GET_SCRIPT_INT(_track);
  267. // Martin> There might be a better way to convert this;)
  268. // benski> yes, there is :) see below. There's a unicode equivalent API for this, just wasn't enabled in wa2frontend object
  269. // so I added it
  270. fileinfo2W *fi = wa2.PE_getFileTitleW(track);
  271. if (fi)
  272. return MAKE_SCRIPT_STRING(fi->filetitle);
  273. else
  274. return MAKE_SCRIPT_STRING(L"");
  275. }
  276. scriptVar SPlaylist::script_vcpu_getLength(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  277. {
  278. SCRIPT_FUNCTION_INIT;
  279. int track = GET_SCRIPT_INT(_track);
  280. fileinfo2W *fi = wa2.PE_getFileTitleW(track);
  281. if (fi)
  282. return MAKE_SCRIPT_STRING((fi->filelength));
  283. else
  284. return MAKE_SCRIPT_STRING(L"");
  285. }
  286. scriptVar SPlaylist::script_vcpu_getFileName(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  287. {
  288. SCRIPT_FUNCTION_INIT;
  289. int track = GET_SCRIPT_INT(_track);
  290. fileinfoW *fi = getFileInfoStructW1(track);
  291. return MAKE_SCRIPT_STRING(fi->file);
  292. }
  293. scriptVar SPlaylist::script_vcpu_getExtendedInfo(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track, scriptVar _name)
  294. {
  295. SCRIPT_FUNCTION_INIT;
  296. int track = GET_SCRIPT_INT(_track);
  297. const wchar_t *name = GET_SCRIPT_STRING(_name);
  298. fileinfoW *fi = getFileInfoStructW1(track);
  299. wa2.getMetaData((fi->file), name, staticStr, 4096);
  300. return MAKE_SCRIPT_STRING(staticStr);
  301. }
  302. scriptVar SPlaylist::script_vcpu_getNumSelectedItems(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  303. {
  304. SCRIPT_FUNCTION_INIT;
  305. HWND hPeWindow = wa2.getWnd(IPC_GETWND_PE);
  306. int ret = SendMessageW(hPeWindow, WM_USER, IPC_PE_GETSELECTEDCOUNT, 0);
  307. return MAKE_SCRIPT_INT(ret);
  308. }
  309. scriptVar SPlaylist::script_vcpu_getNextSelectedItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  310. {
  311. SCRIPT_FUNCTION_INIT;
  312. int track = GET_SCRIPT_INT(_track);
  313. track = SendMessageW(wa2.getMainWindow(), WM_USER, track, IPC_PLAYLIST_GET_NEXT_SELECTED);
  314. return MAKE_SCRIPT_INT(track);
  315. }
  316. scriptVar SPlaylist::script_vcpu_playTrack(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar _track)
  317. {
  318. SCRIPT_FUNCTION_INIT;
  319. #ifdef WASABI_COMPILE_MEDIACORE
  320. int track = GET_SCRIPT_INT(_track);
  321. WASABI_API_MEDIACORE->core_userButton(0, UserButton::STOP);
  322. wa2.PE_setCurrentIndex(track);
  323. WASABI_API_MEDIACORE->core_userButton(0, UserButton::PLAY);
  324. return MAKE_SCRIPT_VOID();
  325. #else
  326. RETURN_SCRIPT_VOID;
  327. #endif
  328. }
  329. scriptVar SPlaylist::script_vcpu_onPleditModified(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  330. {
  331. SCRIPT_FUNCTION_INIT;
  332. PROCESS_HOOKS0(o, playlistController);
  333. SCRIPT_FUNCTION_CHECKABORTEVENT;
  334. SCRIPT_EXEC_EVENT0(o);
  335. }
  336. wchar_t SPlaylist::staticStr[4096] = {0};
  337. PtrList < SPlaylist > SPlaylist::SOList;