1
0

main.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "../../Library/ml_pmp/pmp.h"
  2. #include "../Winamp/wa_ipc.h"
  3. #include "device.h"
  4. #include "api.h"
  5. #include "main.h"
  6. #include "nu/ns_wc.h"
  7. #include "resource.h"
  8. #include <shlwapi.h>
  9. #include <strsafe.h>
  10. #define PLUGIN_VERSION L"1.56"
  11. int winampVersion = 0;
  12. ifc_devicesupportedcommandenum *command_enum=0;
  13. ifc_devicesupportedcommandstore *command_store=0;
  14. ifc_deviceeventmanager *device_event_manager;
  15. char winamp_name[260] = {0};
  16. char winamp_id_str[40] = {0};
  17. wchar_t inifile[MAX_PATH] = {0};
  18. GUID winamp_id = GUID_NULL;
  19. static int Init();
  20. static void Quit();
  21. static intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3);
  22. PMPDevicePlugin plugin = {PMPHDR_VER,0,Init,Quit,MessageProc};
  23. void StartListenServer();
  24. BOOL FormatResProtocol(const wchar_t *resourceName, const wchar_t *resourceType, wchar_t *buffer, size_t bufferMax)
  25. {
  26. unsigned long filenameLength;
  27. if (NULL == resourceName)
  28. return FALSE;
  29. if (FAILED(StringCchCopyExW(buffer, bufferMax, L"res://", &buffer, &bufferMax, 0)))
  30. return FALSE;
  31. filenameLength = GetModuleFileNameW(plugin.hDllInstance, buffer, (DWORD)bufferMax);
  32. if (0 == filenameLength || bufferMax == filenameLength)
  33. return FALSE;
  34. buffer += filenameLength;
  35. bufferMax -= filenameLength;
  36. if (NULL != resourceType)
  37. {
  38. if (FALSE != IS_INTRESOURCE(resourceType))
  39. {
  40. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/#%d", (int)(INT_PTR)resourceType)))
  41. return FALSE;
  42. }
  43. else
  44. {
  45. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/%s", resourceType)))
  46. return FALSE;
  47. }
  48. }
  49. if (FALSE != IS_INTRESOURCE(resourceName))
  50. {
  51. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/#%d", (int)(INT_PTR)resourceName)))
  52. return FALSE;
  53. }
  54. else
  55. {
  56. if (FAILED(StringCchPrintfExW(buffer, bufferMax, &buffer, &bufferMax, 0, L"/%s", resourceName)))
  57. return FALSE;
  58. }
  59. return TRUE;
  60. }
  61. class WifiDeviceConnection : public ifc_deviceconnection
  62. {
  63. public:
  64. WifiDeviceConnection()
  65. {
  66. }
  67. const char *GetName()
  68. {
  69. return "wifi";
  70. }
  71. HRESULT GetIcon(wchar_t *buffer, size_t bufferMax, int width, int height)
  72. {
  73. if(FALSE == FormatResProtocol(MAKEINTRESOURCE(IDB_WIFI), L"PNG", buffer, bufferMax))
  74. return E_FAIL;
  75. return S_OK;
  76. }
  77. HRESULT GetDisplayName(wchar_t *buffer, size_t bufferMax)
  78. {
  79. if (NULL == buffer)
  80. return E_POINTER;
  81. WASABI_API_LNGSTRINGW_BUF(IDS_DEVICE_CONNECTION_WIFI, buffer, bufferMax);
  82. return S_OK;
  83. }
  84. protected:
  85. #define CBCLASS WifiDeviceConnection
  86. START_DISPATCH_INLINE;
  87. CB(API_GETNAME, GetName);
  88. CB(API_GETICON, GetIcon);
  89. CB(API_GETDISPLAYNAME, GetDisplayName);
  90. END_DISPATCH;
  91. #undef CBCLASS
  92. };
  93. class AttachCommand : public ifc_devicecommand
  94. {
  95. public:
  96. const char *GetName()
  97. {
  98. return "attach";
  99. }
  100. HRESULT GetIcon(wchar_t *buffer, size_t bufferMax, int width, int height)
  101. {
  102. int resourceId;
  103. if (width <= 16 && height <= 16)
  104. resourceId = IDB_ATTACH_16;
  105. else
  106. resourceId = IDB_ATTACH;
  107. if(FALSE == FormatResProtocol(MAKEINTRESOURCE(resourceId), L"PNG", buffer, bufferMax))
  108. return E_FAIL;
  109. return S_OK;
  110. }
  111. HRESULT GetDisplayName(wchar_t *buffer, size_t bufferMax)
  112. {
  113. if (NULL == buffer)
  114. return E_POINTER;
  115. WASABI_API_LNGSTRINGW_BUF(IDS_DEVICE_CMD_ATTACH, buffer, bufferMax);
  116. return S_OK;
  117. }
  118. HRESULT GetDescription(wchar_t *buffer, size_t bufferMax)
  119. {
  120. if (NULL == buffer)
  121. return E_POINTER;
  122. WASABI_API_LNGSTRINGW_BUF(IDS_DEVICE_CMD_ATTACH_DESC, buffer, bufferMax);
  123. return S_OK;
  124. }
  125. #define CBCLASS AttachCommand
  126. START_DISPATCH_INLINE;
  127. CB(API_GETNAME, GetName);
  128. CB(API_GETICON, GetIcon);
  129. CB(API_GETDISPLAYNAME, GetDisplayName);
  130. CB(API_GETDESCRIPTION, GetDescription);
  131. END_DISPATCH;
  132. #undef CBCLASS
  133. };
  134. class DeviceCommand : public Countable<ifc_devicesupportedcommand>
  135. {
  136. public:
  137. DeviceCommand(const char *name, DeviceCommandFlags flags);
  138. public:
  139. const char *GetName();
  140. HRESULT GetFlags(DeviceCommandFlags *flags);
  141. REFERENCE_COUNT_IMPLEMENTATION;
  142. public:
  143. const char *name;
  144. DeviceCommandFlags flags;
  145. RECVS_DISPATCH;
  146. };
  147. static AttachCommand attach_command;
  148. static WifiDeviceConnection wifi_connection;
  149. static int Init()
  150. {
  151. winampVersion = (int)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETVERSION);
  152. WasabiInit();
  153. if (!AGAVE_API_DEVICEMANAGER)
  154. return 1;
  155. WASABI_API_APP->GetUserID(&winamp_id);
  156. StringCbPrintfA(winamp_id_str, sizeof(winamp_id_str), "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", (int)winamp_id.Data1, (int)winamp_id.Data2, (int)winamp_id.Data3, (int)winamp_id.Data4[0], (int)winamp_id.Data4[1], (int)winamp_id.Data4[2], (int)winamp_id.Data4[3], (int)winamp_id.Data4[4], (int)winamp_id.Data4[5], (int)winamp_id.Data4[6], (int)winamp_id.Data4[7] );
  157. wchar_t user_name[128] = {0};
  158. wchar_t computer_name[128] = {0};
  159. DWORD buffer_size_user = 128, buffer_size_computer=128;
  160. if (GetUserNameW(user_name, &buffer_size_user) && GetComputerNameW(computer_name, &buffer_size_computer))
  161. {
  162. wchar_t winamp_name_utf16[260] = {0};
  163. StringCbPrintfW(winamp_name_utf16, sizeof(winamp_name_utf16), L"%s (%s)", user_name, computer_name);
  164. WideCharToMultiByteSZ(CP_UTF8, 0, winamp_name_utf16, -1, winamp_name, sizeof(winamp_name), 0, 0);
  165. }
  166. else
  167. StringCbCopyA(winamp_name, sizeof(winamp_name), "Winamp");
  168. const wchar_t *settings_path = WASABI_API_APP->path_getUserSettingsPath();
  169. PathCombineW(inifile, settings_path, L"Plugins\\ml\\pmp_wifi.ini");
  170. // need to have this initialized before we try to do anything with localization features
  171. WASABI_API_START_LANG(plugin.hDllInstance,PmpWifiLangGUID);
  172. static wchar_t szDescription[256];
  173. StringCbPrintfW(szDescription, sizeof(szDescription),
  174. WASABI_API_LNGSTRINGW(IDS_NULLSOFT_WIFI_DEVICE_PLUGIN), PLUGIN_VERSION);
  175. plugin.description = szDescription;
  176. if (AGAVE_API_DEVICEMANAGER)
  177. {
  178. ifc_devicecommand *command = &attach_command;
  179. AGAVE_API_DEVICEMANAGER->CommandRegister(&command, 1);
  180. ifc_deviceconnection *connection = &wifi_connection;
  181. AGAVE_API_DEVICEMANAGER->ConnectionRegister(&connection, 1);
  182. AGAVE_API_DEVICEMANAGER->CreateSupportedCommandStore(&command_store);
  183. command_store->Add("attach", DeviceCommandFlag_Primary);
  184. AGAVE_API_DEVICEMANAGER->CreateDeviceEventManager(&device_event_manager);
  185. }
  186. //AGAVE_API_DEVICEMANAGER->CreateSupportedCommandEnum(&command, 1, &command_enum);
  187. /* TODO: Use this if your device shows up as a normal drive
  188. SendMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(WPARAM)autoDetectCallback,PMP_IPC_ENUM_ACTIVE_DRIVES);
  189. */
  190. StartListenServer();
  191. return 0;
  192. }
  193. static void Quit()
  194. {
  195. StopListenServer();
  196. WasabiQuit();
  197. }
  198. static intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3)
  199. {
  200. switch(msg) {
  201. case PMP_DEVICECHANGE:
  202. // TODO: Implement
  203. return 0;
  204. case PMP_NO_CONFIG:
  205. return TRUE;
  206. case PMP_CONFIG:
  207. // TODO: Implement (Egg: changed from 1 to 0, for now)
  208. return 0;
  209. }
  210. return 0;
  211. }
  212. extern "C" __declspec(dllexport) PMPDevicePlugin *winampGetPMPDevicePlugin()
  213. {
  214. return &plugin;
  215. }