shitdrop.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <windows.h>
  2. #include <commctrl.h>
  3. #include <ddraw.h>
  4. #include <math.h>
  5. #include "../Winamp/wa_ipc.h"
  6. #include "resource.h"
  7. #include "../Agave/Language/api_language.h"
  8. #include <api/service/waServiceFactory.h>
  9. #include "dd.h"
  10. #include "../winamp/vis.h"
  11. int (*warand)(void)=0;
  12. extern wchar_t szAppName[];
  13. extern void init_inifile(struct winampVisModule *this_mod);
  14. extern wchar_t *ini_file;
  15. static int g_width=640, g_height=480;
  16. static int g_divx=16, g_divy=12, g_fadeout=2;
  17. static C_DD g_dd;
  18. extern HWND g_hwnd,hwndParent;
  19. static int running;
  20. int g_minflag;
  21. extern api_service *WASABI_API_SVC;
  22. void do_min(HWND hwnd)
  23. {
  24. g_minflag=!IsIconic(hwnd);
  25. if (g_minflag)
  26. {
  27. ShowWindow(hwnd,SW_MINIMIZE);
  28. Sleep(100);
  29. }
  30. }
  31. void do_unmin(HWND hwnd)
  32. {
  33. if (g_minflag)
  34. {
  35. g_minflag=0;
  36. ShowWindow(hwnd,SW_RESTORE);
  37. SetForegroundWindow(hwnd);
  38. }
  39. }
  40. static void readconfig(struct winampVisModule *this_mod)
  41. {
  42. init_inifile(this_mod);
  43. g_width=GetPrivateProfileIntW(szAppName, L"sdwidth", 640, ini_file);
  44. g_height=GetPrivateProfileIntW(szAppName, L"sdheight", 480, ini_file);
  45. g_divx=GetPrivateProfileIntW(szAppName, L"sdx", 16, ini_file);
  46. g_divy=GetPrivateProfileIntW(szAppName, L"sdy", 12, ini_file);
  47. g_fadeout=GetPrivateProfileIntW(szAppName, L"sdfadeout", 2, ini_file);
  48. }
  49. extern void drawscope(unsigned char *out, int w, int h, unsigned char *visdata);
  50. extern unsigned char *getnewpalette();
  51. extern void moveframe_init(int w, int h, int divx, int divy, int fadeval);
  52. extern void moveframe(unsigned char *in, unsigned char *out, unsigned char *visdata);
  53. extern void moveframe_quit();
  54. static void writeInt(wchar_t *name, int value)
  55. {
  56. wchar_t buf[32] = {0};
  57. wsprintfW(buf, L"%d", value);
  58. WritePrivateProfileStringW(szAppName, name, buf, ini_file);
  59. }
  60. static HRESULT WINAPI _cb(LPDDSURFACEDESC lpDDSurfaceDesc, LPVOID lpContext)
  61. {
  62. if (lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount==8)
  63. {
  64. wchar_t s[32] = {0};
  65. wsprintfW(s, L"%dx%d", lpDDSurfaceDesc->dwWidth, lpDDSurfaceDesc->dwHeight);
  66. LRESULT idx=SendMessageW((HWND)lpContext,CB_ADDSTRING,0,(LPARAM)s);
  67. SendMessage((HWND)lpContext,CB_SETITEMDATA,idx,MAKELONG(lpDDSurfaceDesc->dwWidth,lpDDSurfaceDesc->dwHeight));
  68. if ((int)lpDDSurfaceDesc->dwWidth == g_width && (int)lpDDSurfaceDesc->dwHeight == g_height)
  69. SendMessage((HWND)lpContext,CB_SETCURSEL,idx,0);
  70. }
  71. return DDENUMRET_OK;
  72. }
  73. BOOL IsDirectMouseWheelMessage(const UINT uMsg)
  74. {
  75. static UINT WINAMP_WM_DIRECT_MOUSE_WHEEL = WM_NULL;
  76. if (WM_NULL == WINAMP_WM_DIRECT_MOUSE_WHEEL)
  77. {
  78. WINAMP_WM_DIRECT_MOUSE_WHEEL = RegisterWindowMessageW(L"WINAMP_WM_DIRECT_MOUSE_WHEEL");
  79. if (WM_NULL == WINAMP_WM_DIRECT_MOUSE_WHEEL)
  80. return FALSE;
  81. }
  82. return (WINAMP_WM_DIRECT_MOUSE_WHEEL == uMsg);
  83. }
  84. HWND ActiveChildWindowFromPoint(HWND hwnd, POINTS cursor_s, const int *controls, size_t controlsCount)
  85. {
  86. POINT pt;
  87. POINTSTOPOINT(pt, cursor_s);
  88. while(controlsCount--)
  89. {
  90. RECT controlRect;
  91. HWND controlWindow = GetDlgItem(hwnd, controls[controlsCount]);
  92. if (NULL != controlWindow &&
  93. FALSE != GetClientRect(controlWindow, &controlRect))
  94. {
  95. MapWindowPoints(controlWindow, HWND_DESKTOP, (POINT*)&controlRect, 2);
  96. if (FALSE != PtInRect(&controlRect, pt))
  97. {
  98. unsigned long windowStyle;
  99. windowStyle = (unsigned long)GetWindowLongPtrW(controlWindow, GWL_STYLE);
  100. if(WS_VISIBLE == ((WS_VISIBLE | WS_DISABLED) & windowStyle))
  101. return controlWindow;
  102. break;
  103. }
  104. }
  105. }
  106. return NULL;
  107. }
  108. BOOL DirectMouseWheel_ProcessDialogMessage(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam)
  109. {
  110. if (FALSE != IsDirectMouseWheelMessage(uMsg))
  111. {
  112. const int controls[] =
  113. {
  114. IDC_BANDS,
  115. IDC_VUSE,
  116. IDC_FALLOFF,
  117. IDC_FALLOFF2,
  118. IDC_SCSCALE,
  119. IDC_SLIDER1,
  120. };
  121. HWND targetWindow = ActiveChildWindowFromPoint(hwnd, MAKEPOINTS(lParam), controls, ARRAYSIZE(controls));
  122. if (NULL != targetWindow)
  123. {
  124. SendMessage(targetWindow, WM_MOUSEWHEEL, wParam, lParam);
  125. SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (long)TRUE);
  126. return TRUE;
  127. }
  128. }
  129. return FALSE;
  130. }
  131. static INT_PTR CALLBACK dlgProc1(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  132. {
  133. if (uMsg == WM_INITDIALOG)
  134. {
  135. LPDIRECTDRAW dd;
  136. if (DirectDrawCreate(NULL,&dd,NULL) == DD_OK)
  137. {
  138. IDirectDraw_EnumDisplayModes(dd,0,NULL,GetDlgItem(hwndDlg,IDC_MODELIST),_cb);
  139. IDirectDraw_Release(dd);
  140. }
  141. SetDlgItemInt(hwndDlg,IDC_EDIT1,g_divx,0);
  142. SetDlgItemInt(hwndDlg,IDC_EDIT2,g_divy,0);
  143. SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETRANGE,0,MAKELONG(0,32));
  144. SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETPOS,1,g_fadeout);
  145. return 1;
  146. }
  147. if (uMsg == WM_CLOSE || (uMsg == WM_COMMAND && (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)))
  148. {
  149. BOOL t;
  150. g_divx=GetDlgItemInt(hwndDlg,IDC_EDIT1,&t,0);
  151. g_divy=GetDlgItemInt(hwndDlg,IDC_EDIT2,&t,0);
  152. g_fadeout = SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_GETPOS,0,0);
  153. EndDialog(hwndDlg,IDOK);
  154. }
  155. if (uMsg == WM_COMMAND)
  156. {
  157. if (LOWORD(wParam) == IDC_MODELIST && HIWORD(wParam) == CBN_SELCHANGE)
  158. {
  159. DWORD n=SendDlgItemMessage(hwndDlg,IDC_MODELIST,CB_GETCURSEL,0,0);
  160. if (n != CB_ERR)
  161. {
  162. n=SendDlgItemMessage(hwndDlg,IDC_MODELIST,CB_GETITEMDATA,n,0);
  163. if (n != CB_ERR)
  164. {
  165. g_width=LOWORD(n);
  166. g_height=HIWORD(n);
  167. }
  168. }
  169. }
  170. }
  171. if (FALSE != DirectMouseWheel_ProcessDialogMessage(hwndDlg, uMsg, wParam, lParam))
  172. {
  173. return TRUE;
  174. }
  175. return 0;
  176. }
  177. void sd_config(struct winampVisModule *this_mod)
  178. {
  179. if (running) return;
  180. running=1;
  181. readconfig(this_mod);
  182. // loader so that we can get the localisation service api for use
  183. WASABI_API_SVC = (api_service*)SendMessage(this_mod->hwndParent, WM_WA_IPC, 0, IPC_GET_API_SERVICE);
  184. if (WASABI_API_SVC == (api_service*)1) WASABI_API_SVC = NULL;
  185. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(languageApiGUID);
  186. if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());
  187. // need to have this initialised before we try to do anything with localisation features
  188. WASABI_API_START_LANG(this_mod->hDllInstance,VisNFSFLangGUID);
  189. WASABI_API_DIALOGBOXW(IDD_DIALOG2,this_mod->hwndParent,dlgProc1);
  190. writeInt(L"sdwidth", g_width);
  191. writeInt(L"sdheight", g_height);
  192. writeInt(L"sdx", g_divx);
  193. writeInt(L"sdy", g_divy);
  194. writeInt(L"sdfadeout", g_fadeout);
  195. running=0;
  196. }
  197. static int __inline is_mmx(void) {
  198. _asm {
  199. sub eax, eax
  200. sub edx, edx
  201. inc eax
  202. _emit 0x0f
  203. _emit 0xa2
  204. test eax, eax
  205. jz noMMX
  206. and edx, 0x800000
  207. mov eax, edx
  208. noMMX:
  209. sub ebx, ebx // make sure compiler knows ebx and ecx are blown.
  210. sub ecx, ecx
  211. }
  212. }
  213. int sd_init(struct winampVisModule *this_mod)
  214. {
  215. if (running) return 1;
  216. running=1;
  217. if (!is_mmx())
  218. {
  219. char err[16];
  220. running=0;
  221. MessageBox(this_mod->hwndParent,WASABI_API_LNGSTRING(IDS_MMX_NOT_FOUND),
  222. WASABI_API_LNGSTRING_BUF(IDS_ERROR,err,16),MB_OK);
  223. return 1;
  224. }
  225. readconfig(this_mod);
  226. warand = (int (*)())SendMessage(this_mod->hwndParent, WM_WA_IPC, 0, IPC_GET_RANDFUNC);
  227. extern void initwindow(struct winampVisModule *this_mod,int,int);
  228. initwindow(this_mod,g_width,g_height);
  229. if (!g_hwnd)
  230. {
  231. running=0;
  232. return 1;
  233. }
  234. do_min(this_mod->hwndParent);
  235. SetForegroundWindow(g_hwnd);
  236. char *t=g_dd.open(g_width,g_height,g_hwnd);
  237. if (t)
  238. {
  239. char error[32];
  240. DestroyWindow(g_hwnd);
  241. g_hwnd=NULL;
  242. do_unmin(this_mod->hwndParent);
  243. UnregisterClassW(szAppName,this_mod->hDllInstance);
  244. MessageBox(this_mod->hwndParent,t,WASABI_API_LNGSTRING_BUF(IDS_DIRECTDRAW_ERROR,error,32),MB_OK);
  245. running=0;
  246. return 1;
  247. }
  248. moveframe_init(g_width,g_height,g_divx,g_divy,g_fadeout);
  249. g_dd.setpalette(getnewpalette(),5000);
  250. return 0;
  251. }
  252. // render function. This draws a frame. Returns 0 if successful, 1 if visualization should end.
  253. int sd_render(struct winampVisModule *this_mod)
  254. {
  255. unsigned char *in, *out;
  256. if (g_dd.palette_fadeleft() < -500)
  257. {
  258. g_dd.setpalette(getnewpalette(),3500);
  259. }
  260. if (!g_dd.lock(&in,&out)) return 0;
  261. moveframe(in,out,this_mod->waveformData[0]);
  262. drawscope(out,g_width,g_height,this_mod->waveformData[0]);
  263. g_dd.unlock();
  264. return 0;
  265. }
  266. // cleanup (opposite of init()). Destroys the window, unregisters the window class
  267. void sd_quit(struct winampVisModule *this_mod)
  268. {
  269. g_dd.close();
  270. moveframe_quit();
  271. ShowCursor(TRUE);
  272. if (g_hwnd) DestroyWindow(g_hwnd);
  273. g_hwnd=0;
  274. UnregisterClassW(szAppName,this_mod->hDllInstance);
  275. running=0;
  276. do_unmin(this_mod->hwndParent);
  277. }