main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. //#define PLUGIN_NAME "Nullsoft PlaysForSure Plug-in"
  2. #define PLUGIN_VERSION L"0.99.1"
  3. //#define _WIN32_WINNT 0x0400
  4. #define _WIN32_DCOM
  5. #define INITGUID
  6. #include "P4SDevice.h"
  7. #include "..\..\General\gen_ml/itemlist.h"
  8. #include "key-sub-523.c" // This key is the authentication key.
  9. #include "mswmdm.h"
  10. #include "deviceprovider.h"
  11. #include "../nu/threadpool/api_threadpool.h"
  12. //#include "key.c"
  13. C_ItemList devices;
  14. // wasabi based services for localisation support
  15. api_language *WASABI_API_LNG = 0;
  16. HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
  17. api_memmgr *memoryManager=0;
  18. api_albumart *AGAVE_API_ALBUMART=0;
  19. api_threadpool *WASABI_API_THREADPOOL = 0;
  20. api_devicemanager *AGAVE_API_DEVICEMANAGER = 0;
  21. int init();
  22. void quit();
  23. intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3);
  24. PMPDevicePlugin plugin = {PMPHDR_VER,0,init,quit,MessageProc};
  25. void checkForDevices(BOOL *killSwitch);
  26. void gotDevice(IWMDMDevice* pIDevice);
  27. CRITICAL_SECTION csTransfers;
  28. static DeviceProvider *deviceProvider = NULL;
  29. static IWMDeviceManager3* pIdvMgr=NULL;
  30. static DWORD ConnectionNotificationCookie = 0;
  31. CSecureChannelClient SAC;
  32. IWMDRMDeviceApp * DRMDeviceApp = NULL;
  33. HANDLE killEvent = 0;
  34. static int ThreadInitFunc(HANDLE handle, void *user_data, intptr_t param);
  35. static int ThreadQuitFunc(HANDLE handle, void *user_data, intptr_t param);
  36. HANDLE hWinampThread=NULL;
  37. class MyNotification : public IWMDMNotification {
  38. public:
  39. virtual HRESULT STDMETHODCALLTYPE WMDMMessage(DWORD dwMessageType, LPCWSTR pwszCanonicalName) {
  40. switch(dwMessageType) {
  41. case WMDM_MSG_DEVICE_ARRIVAL: // WMDM device has been plugged in
  42. //OutputDebugString(L"Device arrived (IWMN)");
  43. if (NULL == deviceProvider ||
  44. FAILED(deviceProvider->BeginDiscovery(AGAVE_API_DEVICEMANAGER)))
  45. {
  46. checkForDevices(NULL);
  47. }
  48. break;
  49. case WMDM_MSG_DEVICE_REMOVAL: // WMDM device has been removed
  50. //OutputDebugString(L"Device removed (IWMN)");
  51. for(int i=0; i<devices.GetSize(); i++) {
  52. wchar_t devName[256] = {0};
  53. ((P4SDevice *)devices.Get(i))->WMDevice->GetCanonicalName(devName,256);
  54. if(wcscmp(pwszCanonicalName,devName) == 0) {
  55. ((P4SDevice *)devices.Get(i))->Close();
  56. }
  57. }
  58. break;
  59. case WMDM_MSG_MEDIA_ARRIVAL: // Media has been inserted in WMDM device
  60. WMDMMessage(WMDM_MSG_DEVICE_REMOVAL,pwszCanonicalName);
  61. WMDMMessage(WMDM_MSG_DEVICE_ARRIVAL,pwszCanonicalName);
  62. break;
  63. case WMDM_MSG_MEDIA_REMOVAL: // Media is removed from WMDM device
  64. WMDMMessage(WMDM_MSG_DEVICE_REMOVAL,pwszCanonicalName);
  65. WMDMMessage(WMDM_MSG_DEVICE_ARRIVAL,pwszCanonicalName);
  66. break;
  67. }
  68. return S_OK;
  69. }
  70. // COM shit
  71. ULONG refs;
  72. MyNotification() {refs=1;}
  73. #define IMPLEMENTS(ifc) if (riid == IID_ ## ifc) { ++refs; *ppvObject = static_cast<ifc *>(this); return S_OK; }
  74. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,void __RPC_FAR *__RPC_FAR *ppvObject) {
  75. IMPLEMENTS(IWMDMNotification);
  76. IMPLEMENTS(IUnknown);
  77. *ppvObject = NULL;
  78. return E_NOINTERFACE;
  79. }
  80. virtual ULONG STDMETHODCALLTYPE AddRef() { return ++refs; }
  81. virtual ULONG STDMETHODCALLTYPE Release() { int x = --refs; if(!x) delete this; return x; }
  82. #undef IMPLEMENTS
  83. };
  84. IWMDMNotification * myNotify=NULL;
  85. template <class api_T>
  86. void ServiceBuild(api_T *&api_t, GUID factoryGUID_t)
  87. {
  88. if (plugin.service)
  89. {
  90. waServiceFactory *factory = plugin.service->service_getServiceByGuid(factoryGUID_t);
  91. if (factory)
  92. api_t = reinterpret_cast<api_T *>( factory->getInterface() );
  93. }
  94. }
  95. template <class api_T>
  96. void ServiceRelease(api_T *api_t, GUID factoryGUID_t)
  97. {
  98. if (plugin.service && api_t)
  99. {
  100. waServiceFactory *factory = plugin.service->service_getServiceByGuid(factoryGUID_t);
  101. if (factory)
  102. factory->releaseInterface(api_t);
  103. }
  104. api_t = NULL;
  105. }
  106. BOOL
  107. QueueThreadFunction(api_threadpool::ThreadPoolFunc func, void *user, intptr_t id)
  108. {
  109. BOOL result;
  110. ThreadID *thread;
  111. if (NULL == WASABI_API_THREADPOOL)
  112. return FALSE;
  113. thread = WASABI_API_THREADPOOL->ReserveThread(api_threadpool::FLAG_REQUIRE_COM_MT);
  114. if (NULL == thread)
  115. return FALSE;
  116. result = (0 == WASABI_API_THREADPOOL->RunFunction(thread, func, user, id,
  117. api_threadpool::FLAG_REQUIRE_COM_MT));
  118. WASABI_API_THREADPOOL->ReleaseThread(thread);
  119. return result;
  120. }
  121. static int init2(BOOL runCheck)
  122. {
  123. HRESULT hr;
  124. IComponentAuthenticate* pICompAuth;
  125. DWORD dwNumProtCount;
  126. DWORD* pdwProt=NULL; // This will always be SAC_PROTOCOL_V1.
  127. hr = CoCreateInstance(CLSID_MediaDevMgr, NULL, CLSCTX_ALL, IID_IComponentAuthenticate, (void **)&pICompAuth);
  128. // After getting IComponentAuthenticate, the authentication progression follows.
  129. if (SUCCEEDED(hr))
  130. {
  131. hr = SAC.SetCertificate(SAC_CERT_V1, (BYTE*) abCert, sizeof(abCert), (BYTE*) abPVK, sizeof(abPVK));
  132. if (SUCCEEDED(hr))
  133. {
  134. // Set interface for Secure Authenticated Channel
  135. // operations. The return value of this function is void.
  136. SAC.SetInterface(pICompAuth);
  137. hr = pICompAuth->SACGetProtocols(&pdwProt, &dwNumProtCount);
  138. if (SUCCEEDED(hr))
  139. SAC.Authenticate(*pdwProt); //SAC_PROTOCOL_V1
  140. //if (SUCCEEDED(hr)) OutputDebugString(L"CSecureChannelClient.Authenticate succeeded\n");
  141. }
  142. if(pdwProt)
  143. CoTaskMemFree(pdwProt);
  144. // After authentication has succeeded, call QueryInterface to
  145. // get IID_IWMDeviceManager.
  146. if (FAILED(pICompAuth->QueryInterface(IID_IWMDeviceManager3, (void**)&pIdvMgr)))
  147. pIdvMgr = NULL;
  148. pICompAuth->Release();
  149. pICompAuth = NULL;
  150. if (NULL != pIdvMgr)
  151. {
  152. pIdvMgr->SetDeviceEnumPreference(ALLOW_OUTOFBAND_NOTIFICATION);
  153. IConnectionPointContainer *pIcpc = NULL;
  154. if (SUCCEEDED(pIdvMgr->QueryInterface(IID_IConnectionPointContainer,(void**)&pIcpc)))
  155. {
  156. IConnectionPoint * pICP=NULL;
  157. if (SUCCEEDED(pIcpc->FindConnectionPoint(IID_IWMDMNotification,&pICP)))
  158. {
  159. myNotify = new MyNotification;
  160. if (FAILED(pICP->Advise(myNotify, &ConnectionNotificationCookie)))
  161. ConnectionNotificationCookie = 0;
  162. pICP->Release();
  163. }
  164. pIcpc->Release();
  165. pIcpc=0;
  166. }
  167. }
  168. }
  169. if (FALSE != runCheck)
  170. checkForDevices(NULL);
  171. return 0;
  172. }
  173. static void quit2()
  174. {
  175. if(pIdvMgr)
  176. {
  177. if (0 != ConnectionNotificationCookie)
  178. {
  179. IConnectionPointContainer *pIcpc;
  180. if (SUCCEEDED(pIdvMgr->QueryInterface(IID_IConnectionPointContainer,(void**)&pIcpc)))
  181. {
  182. IConnectionPoint * pICP;
  183. if (SUCCEEDED(pIcpc->FindConnectionPoint(IID_IWMDMNotification,&pICP)))
  184. {
  185. pICP->Unadvise(ConnectionNotificationCookie);
  186. ConnectionNotificationCookie = 0;
  187. pICP->Release();
  188. }
  189. pIcpc->Release();
  190. }
  191. }
  192. pIdvMgr->Release();
  193. pIdvMgr = NULL;
  194. }
  195. if(myNotify)
  196. {
  197. myNotify->Release();
  198. myNotify=0;
  199. }
  200. if(DRMDeviceApp)
  201. {
  202. DRMDeviceApp->Release();
  203. DRMDeviceApp=0;
  204. }
  205. }
  206. int init()
  207. {
  208. CoInitialize(0);
  209. // check OS version
  210. OSVERSIONINFO osvi = {0};
  211. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  212. GetVersionEx(&osvi);
  213. if(osvi.dwMajorVersion < 5) return -1;
  214. if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion < 1) return -1;
  215. hWinampThread = GetCurrentThread();
  216. InitializeCriticalSection(&csTransfers);
  217. Tataki::Init(plugin.service);
  218. ServiceBuild(WASABI_API_LNG, languageApiGUID);
  219. ServiceBuild(WASABI_API_MEMMGR, memMgrApiServiceGuid);
  220. ServiceBuild(WASABI_API_THREADPOOL, ThreadPoolGUID);
  221. ServiceBuild(AGAVE_API_ALBUMART, albumArtGUID);
  222. ServiceBuild(AGAVE_API_DEVICEMANAGER, DeviceManagerGUID);
  223. // need to have this initialised before we try to do anything with localisation features
  224. WASABI_API_START_LANG(plugin.hDllInstance,PmpP4SLangGUID);
  225. static wchar_t szDescription[256];
  226. StringCchPrintfW(szDescription, ARRAYSIZE(szDescription),
  227. WASABI_API_LNGSTRINGW(IDS_NULLSOFT_P4S_PLUGIN), PLUGIN_VERSION);
  228. plugin.description = szDescription;
  229. if (NULL != AGAVE_API_DEVICEMANAGER &&
  230. NULL == deviceProvider)
  231. {
  232. if (SUCCEEDED(DeviceProvider::CreateInstance(&deviceProvider)) &&
  233. FAILED(deviceProvider->Register(AGAVE_API_DEVICEMANAGER)))
  234. {
  235. deviceProvider->Release();
  236. deviceProvider = NULL;
  237. }
  238. }
  239. // set up thread
  240. killEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  241. if (FALSE == QueueThreadFunction(ThreadInitFunc, NULL, (NULL == deviceProvider)))
  242. init2(NULL == deviceProvider);
  243. if (NULL != deviceProvider &&
  244. FAILED(deviceProvider->BeginDiscovery(AGAVE_API_DEVICEMANAGER)))
  245. {
  246. checkForDevices(NULL);
  247. }
  248. return 0;
  249. }
  250. void quit()
  251. {
  252. SetEvent(killEvent);
  253. if (NULL != deviceProvider)
  254. {
  255. deviceProvider->Unregister();
  256. deviceProvider->Release();
  257. deviceProvider = NULL;
  258. }
  259. HANDLE doneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  260. if (FALSE != QueueThreadFunction(ThreadQuitFunc, doneEvent, 0))
  261. WaitForSingleObject(doneEvent, INFINITE);
  262. else
  263. quit2();
  264. CloseHandle(doneEvent);
  265. CloseHandle(killEvent);
  266. DeleteCriticalSection(&csTransfers);
  267. Tataki::Quit();
  268. ServiceRelease(WASABI_API_LNG, languageApiGUID);
  269. ServiceRelease(WASABI_API_MEMMGR, memMgrApiServiceGuid);
  270. ServiceRelease(WASABI_API_THREADPOOL, ThreadPoolGUID);
  271. ServiceRelease(AGAVE_API_ALBUMART, albumArtGUID);
  272. ServiceRelease(AGAVE_API_DEVICEMANAGER, DeviceManagerGUID);
  273. CoUninitialize();
  274. }
  275. void newDeviceAvaliable(IWMDMDevice3* pIDevice)
  276. {
  277. HRESULT hr;
  278. DWORD dwTypeDev = 0;
  279. wchar_t buffer[1024] = {0};
  280. hr = pIDevice->GetType(&dwTypeDev);
  281. if (SUCCEEDED(hr))
  282. {
  283. if(0 == (WMDM_DEVICE_TYPE_PLAYBACK & dwTypeDev))
  284. {
  285. return;
  286. if (FAILED(pIDevice->GetName(buffer,ARRAYSIZE(buffer))))
  287. buffer[0] = L'\0';
  288. int l = wcslen(buffer);
  289. if(l > 5 &&
  290. buffer[l-5] == L' ' &&
  291. buffer[l-4] == L'(' &&
  292. buffer[l-2] == L':' &&
  293. buffer[l-1] == L')')
  294. {
  295. return;
  296. }
  297. }
  298. if(0 == (WMDM_DEVICE_TYPE_STORAGE & dwTypeDev))
  299. return;
  300. }
  301. else
  302. return;
  303. // make sure it's not just a simple mass storage device (we have a separate plugin for this!)
  304. PROPVARIANT protocol; // VARIANTs can kiss my ass
  305. hr = pIDevice->GetProperty(g_wszWMDMDeviceProtocol, &protocol);
  306. if (SUCCEEDED(hr))
  307. {
  308. bool isUsb = !!(*protocol.puuid == WMDM_DEVICE_PROTOCOL_MSC);
  309. PropVariantClear(&protocol);
  310. if (isUsb)
  311. return;
  312. }
  313. if (SUCCEEDED(pIDevice->GetCanonicalName(buffer, ARRAYSIZE(buffer))))
  314. {
  315. wchar_t buffer2[ARRAYSIZE(buffer)] = {0};
  316. for(int i=0; i < devices.GetSize(); i++)
  317. {
  318. P4SDevice *device = (P4SDevice*)devices.Get(i);
  319. if (NULL != device &&
  320. NULL != device->WMDevice &&
  321. SUCCEEDED(device->WMDevice->GetCanonicalName(buffer2, ARRAYSIZE(buffer2))))
  322. {
  323. if(wcscmp(buffer, buffer2) == 0)
  324. return;
  325. }
  326. }
  327. }
  328. if(!DRMDeviceApp)
  329. CoCreateInstance(CLSID_WMDRMDeviceApp, NULL, CLSCTX_ALL, IID_IWMDRMDeviceApp, (void **)&DRMDeviceApp);
  330. bool noMetadata;
  331. if (0 == (WMDM_DEVICE_TYPE_PLAYBACK & dwTypeDev))
  332. {
  333. noMetadata = true;
  334. }
  335. else if (SUCCEEDED(pIDevice->GetManufacturer(buffer, ARRAYSIZE(buffer))) &&
  336. NULL != wcsstr(buffer, L"Nokia"))
  337. {
  338. noMetadata = true;
  339. }
  340. else
  341. noMetadata = false;
  342. P4SDevice *dev = new P4SDevice(pIDevice,noMetadata);
  343. //return dev;
  344. }
  345. void checkForDevices(BOOL *killSwitch)
  346. {
  347. HRESULT hr = S_OK;
  348. IWMDMEnumDevice* pIEnumDev;
  349. IWMDMDevice* pIDeviceOld;
  350. IWMDMDevice3* pIDevice = NULL;
  351. unsigned long fetched;
  352. if (!pIdvMgr)
  353. return;
  354. hr = pIdvMgr->EnumDevices2(&pIEnumDev); // Query for device enumerator.
  355. if (SUCCEEDED(hr))
  356. {
  357. // If no device is found, S_FALSE is returned, which is still
  358. // a success case. Do not use the SUCCEEDED macro.
  359. while ((NULL == killSwitch || FALSE == *killSwitch) &&
  360. S_OK == pIEnumDev->Next(1, &pIDeviceOld, &fetched))
  361. {
  362. if(SUCCEEDED(pIDeviceOld->QueryInterface(IID_IWMDMDevice3,(void**)&pIDevice)))
  363. {
  364. newDeviceAvaliable(pIDevice);
  365. pIDevice->Release();
  366. }
  367. pIDeviceOld->Release();
  368. }
  369. // We should release this pointer but it appears to make winamp not shut down properly if we do.
  370. // This function will only be called a small number of times, so I propose that we don't free it
  371. // until this error can be better investigated. --will
  372. pIEnumDev->Release();
  373. }
  374. }
  375. static int ThreadInitFunc(HANDLE handle, void *user_data, intptr_t param)
  376. {
  377. init2((BOOL)param);
  378. return 0;
  379. }
  380. static int ThreadQuitFunc(HANDLE handle, void *user_data, intptr_t param)
  381. {
  382. quit2();
  383. if (NULL != user_data)
  384. SetEvent((HANDLE)user_data);
  385. return 0;
  386. }
  387. intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3) {
  388. switch(msg) {
  389. case PMP_DEVICECHANGE:
  390. return 0;
  391. case PMP_NO_CONFIG:
  392. return TRUE;
  393. case PMP_CONFIG:
  394. return 0;
  395. }
  396. return 0;
  397. }
  398. typedef struct { ULONG_PTR param; void * proc; int state; CRITICAL_SECTION lock;} spc ;
  399. static VOID CALLBACK spc_caller(ULONG_PTR dwParam) {
  400. spc * s = (spc*)dwParam;
  401. if(!s) return;
  402. EnterCriticalSection(&s->lock);
  403. if(s->state == -1) { LeaveCriticalSection(&s->lock); DeleteCriticalSection(&s->lock); free(s); return; }
  404. s->state = 2;
  405. void (CALLBACK *p)(ULONG_PTR dwParam);
  406. p = (void (CALLBACK *)(ULONG_PTR dwParam))s->proc;
  407. if(p) p(s->param);
  408. s->state=1;
  409. LeaveCriticalSection(&s->lock);
  410. }
  411. // p must be of type void (CALLBACK *)(ULONG_PTR dwParam). Returns 0 for success.
  412. int SynchronousProcedureCall(void * p, ULONG_PTR dwParam) {
  413. if(!p) return 1;
  414. spc * s = (spc*)calloc(1, sizeof(spc));
  415. InitializeCriticalSection(&s->lock);
  416. s->param = dwParam;
  417. s->proc = p;
  418. s->state = 0;
  419. if(!QueueUserAPC(spc_caller,hWinampThread,(ULONG_PTR)s)) { DeleteCriticalSection(&s->lock); free(s); return 1; } //failed
  420. int i=0, state;
  421. do {
  422. SleepEx(10,true);
  423. EnterCriticalSection(&s->lock);
  424. state = s->state;
  425. if(i++ == 100) {s->state = -1; return 1;}
  426. LeaveCriticalSection(&s->lock);
  427. }
  428. while(state!=1);
  429. DeleteCriticalSection(&s->lock);
  430. free(s);
  431. return 0;
  432. }
  433. BOOL FormatResProtocol(const wchar_t *resourceName, const wchar_t *resourceType, wchar_t *buffer, size_t bufferMax)
  434. {
  435. unsigned long filenameLength;
  436. if (NULL == resourceName)
  437. return FALSE;
  438. if (FAILED(StringCchCopyExW(buffer, bufferMax, L"res://", &buffer, &bufferMax, 0)))
  439. return FALSE;
  440. filenameLength = GetModuleFileNameW(plugin.hDllInstance, buffer, bufferMax);
  441. if (0 == filenameLength || bufferMax == filenameLength)
  442. return FALSE;
  443. buffer += filenameLength;
  444. bufferMax -= filenameLength;
  445. if (NULL != resourceType)
  446. {
  447. if (FALSE != IS_INTRESOURCE(resourceType))
  448. {
  449. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/#%d", (int)(INT_PTR)resourceType)))
  450. return FALSE;
  451. }
  452. else
  453. {
  454. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/%s", resourceType)))
  455. return FALSE;
  456. }
  457. }
  458. if (FALSE != IS_INTRESOURCE(resourceName))
  459. {
  460. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/#%d", (int)(INT_PTR)resourceName)))
  461. return FALSE;
  462. }
  463. else
  464. {
  465. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/%s", resourceName)))
  466. return FALSE;
  467. }
  468. return TRUE;
  469. }
  470. extern "C" {
  471. __declspec( dllexport ) PMPDevicePlugin * winampGetPMPDevicePlugin(){return &plugin;}
  472. __declspec( dllexport ) int winampUninstallPlugin(HINSTANCE hDllInst, HWND hwndDlg, int param) {
  473. int i = devices.GetSize();
  474. while(i-- > 0) ((Device*)devices.Get(i))->Close();
  475. return PMP_PLUGIN_UNINSTALL_NOW;
  476. }
  477. };