1
0

main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //#define PLUGIN_NAME "Nullsoft Creative NJB Plug-in"
  2. #define PLUGIN_VERSION L"0.57"
  3. #include "NJBDevice.h"
  4. #include "../Winamp/wa_ipc.h"
  5. #define WM_PMP_NJB_DEVICE_CONNECTED (WM_USER+23)
  6. int init();
  7. void quit();
  8. intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3);
  9. PMPDevicePlugin plugin = {PMPHDR_VER, 0, init, quit, MessageProc};
  10. LRESULT CALLBACK CallbackWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  11. C_ItemList devices;
  12. // wasabi based services for localisation support
  13. api_language *WASABI_API_LNG = 0;
  14. HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
  15. ICTJukebox2 * m_pCTJukebox2 = NULL;
  16. HWND mainMessageWindow = 0;
  17. static bool classRegistered = 0;
  18. HWND CreateDummyWindow()
  19. {
  20. if (!classRegistered)
  21. {
  22. WNDCLASSW wc = {0, };
  23. wc.style = 0;
  24. wc.lpfnWndProc = CallbackWndProc;
  25. wc.hInstance = plugin.hDllInstance;
  26. wc.hIcon = 0;
  27. wc.hCursor = NULL;
  28. wc.lpszClassName = L"pmp_njb_window";
  29. if (!RegisterClassW(&wc))
  30. return 0;
  31. classRegistered = true;
  32. }
  33. HWND dummy = CreateWindowW(L"pmp_njb_window", L"pmp_njb_window", 0, 0, 0, 0, 0, NULL, NULL, plugin.hDllInstance, NULL);
  34. return dummy;
  35. }
  36. int init()
  37. {
  38. waServiceFactory *sf = plugin.service->service_getServiceByGuid(languageApiGUID);
  39. if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());
  40. // need to have this initialised before we try to do anything with localisation features
  41. WASABI_API_START_LANG(plugin.hDllInstance,PmpNJBLangGUID);
  42. static wchar_t szDescription[256];
  43. swprintf(szDescription, ARRAYSIZE(szDescription),
  44. WASABI_API_LNGSTRINGW(IDS_NULLSOFT_CREATIVE_NJB), PLUGIN_VERSION);
  45. plugin.description = szDescription;
  46. OleInitialize(NULL);
  47. HRESULT hr = CoCreateInstance(CLSID_CTJukeBox2, NULL, CLSCTX_ALL, IID_ICTJukebox2, (void**) & m_pCTJukebox2);
  48. if (hr != S_OK) return 0;
  49. hr = m_pCTJukebox2->Initialize2();
  50. if (hr != S_OK)
  51. {
  52. m_pCTJukebox2->Release();
  53. m_pCTJukebox2=0;
  54. return 0;
  55. }
  56. mainMessageWindow = CreateDummyWindow();
  57. m_pCTJukebox2->SetCallbackWindow2(0, (long)mainMessageWindow);
  58. long devCount = 0;
  59. m_pCTJukebox2->GetDeviceCount2(&devCount);
  60. for (long i = 1; i <= devCount; i++)
  61. PostMessage(mainMessageWindow, WM_PMP_NJB_DEVICE_CONNECTED, i, 0);
  62. return 0;
  63. }
  64. void quit()
  65. {
  66. if (m_pCTJukebox2)
  67. {
  68. m_pCTJukebox2->ShutDown2();
  69. m_pCTJukebox2->Release();
  70. m_pCTJukebox2 = 0;
  71. }
  72. DestroyWindow(mainMessageWindow);
  73. OleUninitialize();
  74. }
  75. LRESULT CALLBACK CallbackWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  76. {
  77. switch (uMsg)
  78. {
  79. case WM_PMP_NJB_DEVICE_CONNECTED:
  80. {
  81. new NJBDevice((int)wParam);
  82. }
  83. break;
  84. case WM_DAPSDK_JUKEBOX_REMOVAL:
  85. {
  86. long devCount = 0;
  87. if (m_pCTJukebox2->GetDeviceCount2(&devCount) == S_OK)
  88. {
  89. if (devCount == 0) // TODO benski> shouldn't we always register for messages?
  90. m_pCTJukebox2->SetCallbackWindow2(0, (long)mainMessageWindow);
  91. for (long i = 0; i < devices.GetSize(); /*i++*/)
  92. {
  93. NJBDevice * dev = (NJBDevice *)devices.Get(i);
  94. bool attached = false;
  95. for (long j = 1; j <= devCount; j++)
  96. {
  97. BYTE * ptr = NULL;
  98. if (m_pCTJukebox2->GetDeviceProperties(j, kDeviceSerialNumberValue, (IUnknown*)ptr) == S_OK)
  99. {
  100. if (memcmp(ptr, dev->serial, 16) == 0)
  101. {
  102. attached = true;
  103. dev->id = j;
  104. break;
  105. }
  106. //free(ptr);
  107. }
  108. }
  109. if (!attached)
  110. {
  111. SendMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(intptr_t)dev,PMP_IPC_DEVICEDISCONNECTED);
  112. delete dev;
  113. }
  114. else
  115. i++;
  116. }
  117. }
  118. }
  119. break;
  120. case WM_DAPSDK_JUKEBOX_ARRIVAL:
  121. {
  122. long devCount = 0;
  123. if (m_pCTJukebox2->GetDeviceCount2(&devCount) == S_OK)
  124. new NJBDevice(devCount);
  125. }
  126. break;
  127. default:
  128. {
  129. for (int i = 0; i < devices.GetSize(); i++)
  130. {
  131. NJBDevice * dev = (NJBDevice *)devices.Get(i);
  132. if (dev->messageWindow == hwnd)
  133. return dev->WindowMessage(hwnd, uMsg, wParam, lParam);
  134. }
  135. }
  136. }
  137. return DefWindowProc(hwnd, uMsg, wParam, lParam);;
  138. }
  139. intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3) {
  140. switch(msg) {
  141. case PMP_DEVICECHANGE:
  142. return 0;
  143. case PMP_NO_CONFIG:
  144. return TRUE;
  145. case PMP_CONFIG:
  146. return 0;
  147. }
  148. return 0;
  149. }
  150. extern "C" {
  151. __declspec( dllexport ) PMPDevicePlugin * winampGetPMPDevicePlugin(){return &plugin;}
  152. __declspec( dllexport ) int winampUninstallPlugin(HINSTANCE hDllInst, HWND hwndDlg, int param) {
  153. int i = devices.GetSize();
  154. while(i-- > 0) ((Device*)devices.Get(i))->Close();
  155. return PMP_PLUGIN_UNINSTALL_NOW;
  156. }
  157. };