1
0

main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "api__enc_flac.h"
  2. #include "../Winamp/wa_ipc.h"
  3. #include "../nsv/enc_if.h"
  4. #include "../nu/AutoWideFn.h"
  5. #include "AudioCoderFlac.h"
  6. #include "resource.h"
  7. #include <commctrl.h>
  8. #include <windows.h>
  9. #include <Uxtheme.h>
  10. #include <api/service/waservicefactory.h>
  11. #include <api/application/api_application.h>
  12. #include <mmsystem.h>
  13. #include <strsafe.h>
  14. #define ENC_VERSION "2.46"
  15. HWND winampwnd = 0;
  16. int isthemethere = 0;
  17. api_service *WASABI_API_SVC = 0;
  18. api_language *WASABI_API_LNG = 0;
  19. api_application *WASABI_API_APP = 0;
  20. HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
  21. typedef struct
  22. {
  23. configtype cfg;
  24. char configfile[MAX_PATH];
  25. }
  26. configwndrec;
  27. void readconfig(const char *configfile, configtype *cfg)
  28. {
  29. if (configfile)
  30. {
  31. cfg->compression = GetPrivateProfileIntA("audio_flac", "compression", 5, configfile);
  32. }
  33. else
  34. {
  35. cfg->compression = 5;
  36. }
  37. }
  38. void writeconfig(const char *configfile, configtype *cfg)
  39. {
  40. if (configfile)
  41. {
  42. char str[64] = {0};
  43. StringCchPrintfA(str, 64, "%u", cfg->compression);
  44. WritePrivateProfileStringA("audio_flac", "compression", str, configfile);
  45. }
  46. }
  47. static HINSTANCE GetMyInstance()
  48. {
  49. MEMORY_BASIC_INFORMATION mbi = {0};
  50. if(VirtualQuery(GetMyInstance, &mbi, sizeof(mbi)))
  51. return (HINSTANCE)mbi.AllocationBase;
  52. return NULL;
  53. }
  54. void GetLocalisationApiService(void)
  55. {
  56. if(!WASABI_API_LNG)
  57. {
  58. // loader so that we can get the localisation service api for use
  59. if(!WASABI_API_SVC)
  60. {
  61. WASABI_API_SVC = (api_service*)SendMessage(winampwnd, WM_WA_IPC, 0, IPC_GET_API_SERVICE);
  62. if (WASABI_API_SVC == (api_service*)1)
  63. {
  64. WASABI_API_SVC = NULL;
  65. return;
  66. }
  67. }
  68. if(!WASABI_API_APP)
  69. {
  70. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(applicationApiServiceGuid);
  71. if (sf) WASABI_API_APP = reinterpret_cast<api_application*>(sf->getInterface());
  72. }
  73. if(!WASABI_API_LNG)
  74. {
  75. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(languageApiGUID);
  76. if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());
  77. }
  78. // need to have this initialised before we try to do anything with localisation features
  79. WASABI_API_START_LANG(GetMyInstance(),EncFlacLangGUID);
  80. }
  81. }
  82. static const char *GetFLACVersion()
  83. {
  84. return "1.4.2";
  85. }
  86. static HCURSOR link_hand_cursor;
  87. LRESULT link_handlecursor(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  88. {
  89. LRESULT ret = CallWindowProcW((WNDPROC)GetPropW(hwndDlg, L"link_proc"), hwndDlg, uMsg, wParam, lParam);
  90. // override the normal cursor behaviour so we have a hand to show it is a link
  91. if(uMsg == WM_SETCURSOR)
  92. {
  93. if((HWND)wParam == hwndDlg)
  94. {
  95. if(!link_hand_cursor)
  96. {
  97. link_hand_cursor = LoadCursor(NULL, IDC_HAND);
  98. }
  99. SetCursor(link_hand_cursor);
  100. return TRUE;
  101. }
  102. }
  103. return ret;
  104. }
  105. void link_handledraw(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  106. {
  107. if (uMsg == WM_DRAWITEM)
  108. {
  109. DRAWITEMSTRUCT *di = (DRAWITEMSTRUCT *)lParam;
  110. if (di->CtlType == ODT_BUTTON)
  111. {
  112. wchar_t wt[123] = {0};
  113. int y;
  114. RECT r;
  115. HPEN hPen, hOldPen;
  116. GetDlgItemTextW(hwndDlg, (int)wParam, wt, sizeof(wt)/sizeof(wt[0]));
  117. // due to the fun of theming and owner drawing we have to get the background colour
  118. if(isthemethere){
  119. HTHEME hTheme = OpenThemeData(hwndDlg, L"Tab");
  120. if (hTheme) {
  121. DrawThemeParentBackground(di->hwndItem, di->hDC, &di->rcItem);
  122. CloseThemeData(hTheme);
  123. }
  124. }
  125. // draw text
  126. SetTextColor(di->hDC, (di->itemState & ODS_SELECTED) ? RGB(220, 0, 0) : RGB(0, 0, 220));
  127. r = di->rcItem;
  128. r.left += 2;
  129. DrawTextW(di->hDC, wt, -1, &r, DT_VCENTER | DT_SINGLELINE);
  130. memset(&r, 0, sizeof(r));
  131. DrawTextW(di->hDC, wt, -1, &r, DT_SINGLELINE | DT_CALCRECT);
  132. // draw underline
  133. y = di->rcItem.bottom - ((di->rcItem.bottom - di->rcItem.top) - (r.bottom - r.top)) / 2 - 1;
  134. hPen = CreatePen(PS_SOLID, 0, (di->itemState & ODS_SELECTED) ? RGB(220, 0, 0) : RGB(0, 0, 220));
  135. hOldPen = (HPEN) SelectObject(di->hDC, hPen);
  136. MoveToEx(di->hDC, di->rcItem.left + 2, y, NULL);
  137. LineTo(di->hDC, di->rcItem.right + 2 - ((di->rcItem.right - di->rcItem.left) - (r.right - r.left)), y);
  138. SelectObject(di->hDC, hOldPen);
  139. DeleteObject(hPen);
  140. }
  141. }
  142. }
  143. void link_startsubclass(HWND hwndDlg, UINT id)
  144. {
  145. HWND ctrl = GetDlgItem(hwndDlg, id);
  146. if(!GetPropW(ctrl, L"link_proc"))
  147. {
  148. SetPropW(ctrl, L"link_proc",
  149. (HANDLE)SetWindowLongPtrW(ctrl, GWLP_WNDPROC, (LONG_PTR)link_handlecursor));
  150. }
  151. }
  152. BOOL CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  153. {
  154. static configwndrec *wr;
  155. switch(uMsg)
  156. {
  157. case WM_INITDIALOG:
  158. {
  159. wr = (configwndrec *)lParam;
  160. SendMessage(GetDlgItem(hwndDlg,IDC_COMPRESSIONSLIDER),TBM_SETRANGE,TRUE,MAKELONG(0,8));
  161. SendMessage(GetDlgItem(hwndDlg,IDC_COMPRESSIONSLIDER),TBM_SETPOS,TRUE,wr->cfg.compression);
  162. const char *libFlacVer = GetFLACVersion();
  163. if (libFlacVer && *libFlacVer)
  164. {
  165. char flac_string[1024] = {0};
  166. StringCchPrintfA(flac_string, 1024, "libFLAC v%s",libFlacVer);
  167. SetDlgItemTextA(hwndDlg, IDC_STATIC_FLAC_VER, flac_string);
  168. }
  169. link_startsubclass(hwndDlg, IDC_URL);
  170. }
  171. break;
  172. case WM_COMMAND:
  173. if(LOWORD(wParam) == IDC_URL)
  174. {
  175. SendMessage(winampwnd, WM_WA_IPC, (WPARAM)"http://flac.sf.net/", IPC_OPEN_URL);
  176. }
  177. break;
  178. case WM_NOTIFY:
  179. if(wParam == IDC_COMPRESSIONSLIDER)
  180. {
  181. LPNMHDR l = (LPNMHDR)lParam;
  182. if(l->idFrom == IDC_COMPRESSIONSLIDER)
  183. wr->cfg.compression = (unsigned int)SendMessage(GetDlgItem(hwndDlg,IDC_COMPRESSIONSLIDER),TBM_GETPOS,0,0);
  184. }
  185. break;
  186. case WM_DESTROY:
  187. writeconfig(wr->configfile,&wr->cfg);
  188. free(wr); wr=NULL;
  189. break;
  190. }
  191. const int controls[] =
  192. {
  193. IDC_COMPRESSIONSLIDER,
  194. };
  195. if (FALSE != WASABI_API_APP->DirectMouseWheel_ProcessDialogMessage(hwndDlg, uMsg, wParam, lParam, controls, ARRAYSIZE(controls)))
  196. {
  197. return TRUE;
  198. }
  199. link_handledraw(hwndDlg,uMsg,wParam,lParam);
  200. return 0;
  201. }
  202. extern "C"
  203. {
  204. unsigned int __declspec(dllexport) GetAudioTypes3(int idx, char *desc)
  205. {
  206. if (idx == 0)
  207. {
  208. GetLocalisationApiService();
  209. const char *libFlacVer = GetFLACVersion();
  210. StringCchPrintfA(desc, 1024, WASABI_API_LNGSTRING(IDS_ENC_FLAC_DESC), ENC_VERSION, libFlacVer);
  211. return mmioFOURCC('F', 'L', 'A', 'C');
  212. }
  213. return 0;
  214. }
  215. AudioCoder __declspec(dllexport) *CreateAudio3(int nch, int srate, int bps, unsigned int srct, unsigned int *outt, char *configfile)
  216. {
  217. if (srct == mmioFOURCC('P', 'C', 'M', ' ') && *outt == mmioFOURCC('F', 'L', 'A', 'C'))
  218. {
  219. configtype cfg;
  220. readconfig(configfile, &cfg);
  221. *outt = mmioFOURCC('F', 'L', 'A', 'C');
  222. AudioCoderFlac *t=new AudioCoderFlac(nch, bps, srate, cfg.compression);
  223. if (!t->OK())
  224. {
  225. delete t;
  226. return NULL;
  227. }
  228. return t;
  229. }
  230. return NULL;
  231. }
  232. void __declspec(dllexport) FinishAudio3(const char *filename, AudioCoder *coder)
  233. {
  234. ((AudioCoderFlac*)coder)->Finish(AutoWideFn(filename));
  235. }
  236. void __declspec(dllexport) FinishAudio3W(const wchar_t *filename, AudioCoder *coder)
  237. {
  238. ((AudioCoderFlac*)coder)->Finish(filename);
  239. }
  240. void __declspec(dllexport) PrepareToFinish(const char *filename, AudioCoder *coder)
  241. {
  242. ((AudioCoderFlac*)coder)->PrepareToFinish();
  243. }
  244. void __declspec(dllexport) PrepareToFinishW(const wchar_t *filename, AudioCoder *coder)
  245. {
  246. ((AudioCoderFlac*)coder)->PrepareToFinish();
  247. }
  248. HWND __declspec(dllexport) ConfigAudio3(HWND hwndParent, HINSTANCE hinst, unsigned int outt, char *configfile)
  249. {
  250. if (outt == mmioFOURCC('F', 'L', 'A', 'C'))
  251. {
  252. configwndrec *wr = (configwndrec*)malloc(sizeof(configwndrec));
  253. if (configfile) StringCchCopyA(wr->configfile, MAX_PATH, configfile);
  254. else wr->configfile[0] = 0;
  255. readconfig(configfile, &wr->cfg);
  256. GetLocalisationApiService();
  257. return WASABI_API_CREATEDIALOGPARAMW(IDD_CONFIG, hwndParent, DlgProc, (LPARAM)wr);
  258. }
  259. return NULL;
  260. }
  261. int __declspec(dllexport) SetConfigItem(unsigned int outt, char *item, char *data, char *configfile)
  262. {
  263. // nothing yet
  264. return 0;
  265. }
  266. int __declspec(dllexport) GetConfigItem(unsigned int outt, char *item, char *data, int len, char *configfile)
  267. {
  268. if (outt == mmioFOURCC('F', 'L', 'A', 'C'))
  269. {
  270. /*
  271. configtype cfg;
  272. readconfig(configfile, &cfg);
  273. */
  274. if (!_stricmp(item, "bitrate")) StringCchCopyA(data, len, "755"); // FUCKO: this is ment to be an estimate for approximations of output filesize (used by ml_pmp). Improve this.
  275. else if (!_stricmp(item,"extension")) StringCchCopyA(data, len, "flac");
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. void __declspec(dllexport) SetWinampHWND(HWND hwnd)
  281. {
  282. winampwnd = hwnd;
  283. isthemethere = !SendMessage(hwnd,WM_WA_IPC,IPC_ISWINTHEMEPRESENT,IPC_USE_UXTHEME_FUNC);
  284. }
  285. };