main.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //#define PLUGIN_NAME "Nullsoft iPod Plug-in"
  2. #define PLUGIN_VERSION L"0.91"
  3. #include "iPodDevice.h"
  4. #include <Dbt.h>
  5. #include "..\..\General\gen_ml/itemlist.h"
  6. #include <api/service/waservicefactory.h>
  7. #include "api.h"
  8. #include "../nu/autoLock.h"
  9. #include "deviceprovider.h"
  10. #include <tataki/export.h>
  11. #include <shlwapi.h>
  12. int init();
  13. void quit();
  14. intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3);
  15. extern PMPDevicePlugin plugin = {PMPHDR_VER,0,init,quit,MessageProc};
  16. static DWORD WINAPI ThreadFunc_DeviceChange(LPVOID lpParam);
  17. bool g_detectAll=false;
  18. std::vector<iPodDevice*> iPods;
  19. api_config *AGAVE_API_CONFIG=0;
  20. api_memmgr *WASABI_API_MEMMGR=0;
  21. api_albumart *AGAVE_API_ALBUMART=0;
  22. api_threadpool *WASABI_API_THREADPOOL=0;
  23. api_application *WASABI_API_APP=0;
  24. api_devicemanager *AGAVE_API_DEVICEMANAGER = 0;
  25. // wasabi based services for localisation support
  26. api_language *WASABI_API_LNG = 0;
  27. HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
  28. static Nullsoft::Utility::LockGuard connect_guard;
  29. static DeviceProvider *deviceProvider = NULL;
  30. static bool loading_devices[26] = {0,};
  31. template <class api_T>
  32. void ServiceBuild(api_T *&api_t, GUID factoryGUID_t)
  33. {
  34. if (plugin.service)
  35. {
  36. waServiceFactory *factory = plugin.service->service_getServiceByGuid(factoryGUID_t);
  37. if (factory)
  38. api_t = reinterpret_cast<api_T *>( factory->getInterface() );
  39. }
  40. }
  41. template <class api_T>
  42. void ServiceRelease(api_T *api_t, GUID factoryGUID_t)
  43. {
  44. if (plugin.service && api_t)
  45. {
  46. waServiceFactory *factory = plugin.service->service_getServiceByGuid(factoryGUID_t);
  47. if (factory)
  48. factory->releaseInterface(api_t);
  49. }
  50. api_t = NULL;
  51. }
  52. static bool createBlankDatabase(wchar_t drive) {
  53. wchar_t ipodtest[]={drive,L":\\iPod_Control\\iTunes\\iTunesDB"};
  54. iPod_mhbd db;
  55. BYTE data[4096] = {0};
  56. long l = db.write(data,sizeof(data));
  57. if(l <= 0) return false;
  58. FILE* fh=_wfopen(ipodtest,L"wb");
  59. if(!fh) return false;
  60. if(fwrite(data,l,1,fh))
  61. {
  62. wchar_t ipodtest2[]={drive,L":\\iPod_Control\\iTunes\\firsttime"};
  63. _wunlink(ipodtest2);
  64. }
  65. fclose(fh);
  66. wchar_t music[] = {drive,L":\\iPod_Control\\Music"};
  67. CreateDirectoryW(music,NULL);
  68. return true;
  69. }
  70. bool ConnectDrive(wchar_t drive, bool connect)
  71. {
  72. bool result;
  73. size_t index;
  74. iPodDevice *device;
  75. Nullsoft::Utility::AutoLock connect_lock(connect_guard);
  76. // capitalize
  77. if (drive >= L'a' && drive <= L'z')
  78. drive = drive - 32;
  79. // reject invalid drive letters
  80. if (drive < L'A' || drive > L'Z')
  81. return false;
  82. if (false != loading_devices[drive-'A'])
  83. return false;
  84. loading_devices[drive-'A'] = true;
  85. if (false != connect)
  86. {
  87. result = true;
  88. char ipodtest[]= {(char)drive,":\\iPod_Control\\iTunes\\iTunesDB"};
  89. if (GetFileAttributes(ipodtest) == INVALID_FILE_ATTRIBUTES)
  90. {
  91. char ipodtest2[]={(char)drive,":\\iPod_Control\\iTunes\\firsttime"};
  92. if (GetFileAttributes(ipodtest2) == INVALID_FILE_ATTRIBUTES ||
  93. false == createBlankDatabase(drive))
  94. {
  95. result = false;
  96. }
  97. }
  98. if (false != result)
  99. {
  100. index = iPods.size();
  101. while(index--)
  102. {
  103. device = iPods[index];
  104. if(device->drive == drive)
  105. break;
  106. }
  107. if((size_t)-1 == index)
  108. {
  109. iPodDevice *d = new iPodDevice((char)drive);
  110. }
  111. else
  112. result = false;
  113. }
  114. }
  115. else
  116. {
  117. result = false;
  118. index = iPods.size();
  119. while(index--)
  120. {
  121. device = iPods.at(index);
  122. if (device->drive == drive)
  123. {
  124. SendNotifyMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(intptr_t)device,PMP_IPC_DEVICEDISCONNECTED);
  125. iPods.erase(iPods.begin() + index);
  126. delete device;
  127. result = true;
  128. break;
  129. }
  130. }
  131. }
  132. loading_devices[drive-'A'] = false;
  133. return result;
  134. }
  135. static void autoDetectCallback(wchar_t driveW, UINT type)
  136. {
  137. if(false == g_detectAll && DRIVE_REMOVABLE != type)
  138. return;
  139. ConnectDrive(driveW, true);
  140. }
  141. int init()
  142. {
  143. wchar_t mlipod[MAX_PATH] = {0};
  144. PathCombineW(mlipod, (LPCWSTR)SendMessage(plugin.hwndWinampParent,WM_WA_IPC,0,IPC_GETPLUGINDIRECTORYW), L"ml_ipod.dll");
  145. FILE * f = _wfopen(mlipod, L"rb");
  146. if (f) {
  147. fclose(f);
  148. return -1;
  149. }
  150. Tataki::Init(plugin.service);
  151. ServiceBuild(AGAVE_API_CONFIG, AgaveConfigGUID);
  152. ServiceBuild(WASABI_API_LNG, languageApiGUID);
  153. ServiceBuild(WASABI_API_MEMMGR, memMgrApiServiceGuid);
  154. ServiceBuild(AGAVE_API_ALBUMART, albumArtGUID);
  155. ServiceBuild(WASABI_API_THREADPOOL, ThreadPoolGUID);
  156. ServiceBuild(WASABI_API_APP, applicationApiServiceGuid);
  157. ServiceBuild(AGAVE_API_DEVICEMANAGER, DeviceManagerGUID);
  158. // need to have this initialised before we try to do anything with localisation features
  159. WASABI_API_START_LANG(plugin.hDllInstance,PmpIPODLangGUID);
  160. static wchar_t szDescription[256];
  161. swprintf(szDescription, ARRAYSIZE(szDescription),
  162. WASABI_API_LNGSTRINGW(IDS_NULLSOFT_IPOD_PLUGIN), PLUGIN_VERSION);
  163. plugin.description = szDescription;
  164. if (NULL != AGAVE_API_DEVICEMANAGER &&
  165. NULL == deviceProvider)
  166. {
  167. if (SUCCEEDED(DeviceProvider::CreateInstance(&deviceProvider)) &&
  168. FAILED(deviceProvider->Register(AGAVE_API_DEVICEMANAGER)))
  169. {
  170. deviceProvider->Release();
  171. deviceProvider = NULL;
  172. }
  173. }
  174. /* Our device shows up as a normal drive */
  175. if (NULL == deviceProvider ||
  176. FAILED(deviceProvider->BeginDiscovery(AGAVE_API_DEVICEMANAGER)))
  177. {
  178. SendMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(WPARAM)autoDetectCallback,PMP_IPC_ENUM_ACTIVE_DRIVES);
  179. }
  180. return 0;
  181. }
  182. void quit()
  183. {
  184. ServiceRelease(AGAVE_API_CONFIG, AgaveConfigGUID);
  185. ServiceRelease(WASABI_API_LNG, languageApiGUID);
  186. ServiceRelease(WASABI_API_MEMMGR, memMgrApiServiceGuid);
  187. ServiceRelease(AGAVE_API_ALBUMART, albumArtGUID);
  188. ServiceRelease(WASABI_API_THREADPOOL, ThreadPoolGUID);
  189. ServiceRelease(WASABI_API_APP, applicationApiServiceGuid);
  190. ServiceRelease(AGAVE_API_DEVICEMANAGER, DeviceManagerGUID);
  191. Tataki::Quit();
  192. }
  193. static char FirstDriveFromMask(ULONG unitmask) {
  194. char i;
  195. for(i=0; i<26; ++i) {
  196. if(unitmask & 0x1) break;
  197. unitmask = unitmask >> 1;
  198. }
  199. return (i+'A');
  200. }
  201. int wmDeviceChange(WPARAM wParam, LPARAM lParam)
  202. {
  203. if(wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE)
  204. {
  205. PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
  206. if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
  207. { // its a volume
  208. PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
  209. if(0 == ((DBTF_MEDIA | DBTF_NET) & lpdbv->dbcv_flags) || g_detectAll)
  210. { // its not a network drive or a CD/floppy, game on!
  211. unsigned long unitmask = lpdbv->dbcv_unitmask;
  212. for (int i = 0; i < 26; i++)
  213. {
  214. if (0 != (0x1 & unitmask))
  215. {
  216. int p = (int)('A' + i);
  217. if (DBT_DEVICEARRIVAL == wParam)
  218. p += 0x10000;
  219. ThreadFunc_DeviceChange((void*)(intptr_t)p);
  220. unitmask = unitmask >> 1;
  221. if (0 == unitmask)
  222. break;
  223. }
  224. else
  225. unitmask = unitmask >> 1;
  226. }
  227. }
  228. }
  229. }
  230. return 0;
  231. }
  232. static DWORD WINAPI ThreadFunc_DeviceChange(LPVOID lpParam)
  233. {
  234. int p = (int)lpParam;
  235. bool connect = p > 0x10000;
  236. if(connect)
  237. p -= 0x10000;
  238. char drive = (char)p;
  239. if(drive == 0)
  240. return 0;
  241. if(connect)
  242. { // something plugged in
  243. ConnectDrive(drive, connect);
  244. }
  245. else
  246. { //something removed
  247. size_t index;
  248. index = iPods.size();
  249. while(index--)
  250. {
  251. iPodDevice *device = iPods.at(index);
  252. if (device->drive == drive)
  253. {
  254. SendMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(intptr_t)device,PMP_IPC_DEVICEDISCONNECTED);
  255. iPods.erase(iPods.begin() + index);
  256. delete device;
  257. break;
  258. }
  259. }
  260. }
  261. return 0;
  262. }
  263. intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3) {
  264. switch(msg) {
  265. case PMP_DEVICECHANGE:
  266. return wmDeviceChange(param1,param2);
  267. case PMP_NO_CONFIG:
  268. return TRUE;
  269. case PMP_CONFIG:
  270. return 0;
  271. }
  272. return 0;
  273. }
  274. extern "C" {
  275. __declspec( dllexport ) PMPDevicePlugin * winampGetPMPDevicePlugin(){return &plugin;}
  276. __declspec( dllexport ) int winampUninstallPlugin(HINSTANCE hDllInst, HWND hwndDlg, int param) {
  277. int i = iPods.size();
  278. while(i-- > 0) iPods[i]->Close();
  279. return PMP_PLUGIN_UNINSTALL_NOW;
  280. }
  281. };