Prevent the animation thread from waiting on itself when it stops.
[wine/gsoc_dplay.git] / dlls / comctl32 / animate.c
blob374b3b1111c31f45fccd910cdb35717f0eeb3b96
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
5 * Copyright 1998, 1999 Eric Kohl
6 * 1999 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * NOTES
23 * I will only improve this control once in a while.
24 * Eric <ekohl@abo.rhein-zeitung.de>
26 * TODO:
27 * - check for the 'rec ' list in some AVI files
28 * - concurrent access to infoPtr
31 #define COM_NO_WINDOWS_H
32 #include <stdarg.h>
33 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winnls.h"
39 #include "commctrl.h"
40 #include "vfw.h"
41 #include "mmsystem.h"
42 #include "comctl32.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(animate);
47 static struct {
48 HMODULE hModule;
49 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
50 LRESULT (WINAPI *fnICClose)(HIC);
51 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
52 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
53 } fnIC;
55 typedef struct
57 /* reference to input stream (file or resource) */
58 HGLOBAL hRes;
59 HMMIO hMMio; /* handle to mmio stream */
60 HWND hwndSelf;
61 HWND hwndNotify;
62 /* information on the loaded AVI file */
63 MainAVIHeader mah;
64 AVIStreamHeader ash;
65 LPBITMAPINFOHEADER inbih;
66 LPDWORD lpIndex;
67 /* data for the decompressor */
68 HIC hic;
69 LPBITMAPINFOHEADER outbih;
70 LPVOID indata;
71 LPVOID outdata;
72 /* data for the background mechanism */
73 CRITICAL_SECTION cs;
74 HANDLE hStopEvent;
75 HANDLE hThread;
76 DWORD threadId;
77 UINT uTimer;
78 /* data for playing the file */
79 int nFromFrame;
80 int nToFrame;
81 int nLoop;
82 int currFrame;
83 /* tranparency info*/
84 COLORREF transparentColor;
85 HBRUSH hbrushBG;
86 HBITMAP hbmPrevFrame;
87 } ANIMATE_INFO;
89 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0))
90 #define ANIMATE_COLOR_NONE 0xffffffff
92 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
94 SendMessageA(infoPtr->hwndNotify, WM_COMMAND,
95 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
96 (LPARAM)infoPtr->hwndSelf);
99 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
101 HRSRC hrsrc;
102 MMIOINFO mminfo;
103 LPVOID lpAvi;
105 hrsrc = FindResourceA(hInst, lpName, "AVI");
106 if (!hrsrc)
107 return FALSE;
109 infoPtr->hRes = LoadResource(hInst, hrsrc);
110 if (!infoPtr->hRes)
111 return FALSE;
113 lpAvi = LockResource(infoPtr->hRes);
114 if (!lpAvi)
115 return FALSE;
117 memset(&mminfo, 0, sizeof(mminfo));
118 mminfo.fccIOProc = FOURCC_MEM;
119 mminfo.pchBuffer = (LPSTR)lpAvi;
120 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
121 infoPtr->hMMio = mmioOpenA(NULL, &mminfo, MMIO_READ);
122 if (!infoPtr->hMMio) {
123 FreeResource(infoPtr->hRes);
124 return FALSE;
127 return TRUE;
131 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
133 infoPtr->hMMio = mmioOpenA((LPSTR)lpName, NULL,
134 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
136 if (!infoPtr->hMMio)
137 return FALSE;
139 return TRUE;
143 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
145 EnterCriticalSection(&infoPtr->cs);
147 /* should stop playing */
148 if (infoPtr->hThread)
150 HANDLE handle = infoPtr->hThread;
152 TRACE("stopping animation thread\n");
153 infoPtr->hThread = 0;
154 SetEvent( infoPtr->hStopEvent );
156 if (infoPtr->threadId != GetCurrentThreadId())
158 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
159 WaitForSingleObject( handle, INFINITE );
160 TRACE("animation thread stopped\n");
161 EnterCriticalSection(&infoPtr->cs);
164 CloseHandle( handle );
165 CloseHandle( infoPtr->hStopEvent );
166 infoPtr->hStopEvent = 0;
168 if (infoPtr->uTimer) {
169 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
170 infoPtr->uTimer = 0;
173 LeaveCriticalSection(&infoPtr->cs);
175 ANIMATE_Notify(infoPtr, ACN_STOP);
177 return TRUE;
181 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
183 if (infoPtr->hMMio) {
184 ANIMATE_DoStop(infoPtr);
185 mmioClose(infoPtr->hMMio, 0);
186 if (infoPtr->hRes) {
187 FreeResource(infoPtr->hRes);
188 infoPtr->hRes = 0;
190 if (infoPtr->lpIndex) {
191 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
192 infoPtr->lpIndex = NULL;
194 if (infoPtr->hic) {
195 fnIC.fnICClose(infoPtr->hic);
196 infoPtr->hic = 0;
198 if (infoPtr->inbih) {
199 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
200 infoPtr->inbih = NULL;
202 if (infoPtr->outbih) {
203 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
204 infoPtr->outbih = NULL;
206 if( infoPtr->indata )
208 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
209 infoPtr->indata = NULL;
211 if( infoPtr->outdata )
213 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
214 infoPtr->outdata = NULL;
216 if( infoPtr->hbmPrevFrame )
218 DeleteObject(infoPtr->hbmPrevFrame);
219 infoPtr->hbmPrevFrame = 0;
221 infoPtr->indata = infoPtr->outdata = NULL;
222 infoPtr->hwndSelf = 0;
223 infoPtr->hMMio = 0;
225 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
226 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
227 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
229 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
232 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
234 HDC hdcMask;
235 HBITMAP hbmMask;
236 HBITMAP hbmOld;
238 /* create a transparency mask */
239 hdcMask = CreateCompatibleDC(hdcDest);
240 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
241 hbmOld = SelectObject(hdcMask, hbmMask);
243 SetBkColor(hdcSource,infoPtr->transparentColor);
244 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
246 /* mask the source bitmap */
247 SetBkColor(hdcSource, RGB(0,0,0));
248 SetTextColor(hdcSource, RGB(255,255,255));
249 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
251 /* mask the destination bitmap */
252 SetBkColor(hdcDest, RGB(255,255,255));
253 SetTextColor(hdcDest, RGB(0,0,0));
254 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
256 /* combine source and destination */
257 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
259 SelectObject(hdcMask, hbmOld);
260 DeleteObject(hbmMask);
261 DeleteDC(hdcMask);
264 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
266 void* pBitmapData = NULL;
267 LPBITMAPINFO pBitmapInfo = NULL;
269 HDC hdcMem;
270 HBITMAP hbmOld;
272 int nOffsetX = 0;
273 int nOffsetY = 0;
275 int nWidth;
276 int nHeight;
278 if (!hDC || !infoPtr->inbih)
279 return TRUE;
281 if (infoPtr->hic )
283 pBitmapData = infoPtr->outdata;
284 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
286 nWidth = infoPtr->outbih->biWidth;
287 nHeight = infoPtr->outbih->biHeight;
288 } else
290 pBitmapData = infoPtr->indata;
291 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
293 nWidth = infoPtr->inbih->biWidth;
294 nHeight = infoPtr->inbih->biHeight;
297 if(!infoPtr->hbmPrevFrame)
299 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
302 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
304 hdcMem = CreateCompatibleDC(hDC);
305 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
308 * we need to get the transparent color even without ACS_TRANSPARENT,
309 * because the style can be changed later on and the color should always
310 * be obtained in the first frame
312 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
314 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
317 if(GetWindowLongA(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
319 HDC hdcFinal = CreateCompatibleDC(hDC);
320 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
321 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
322 RECT rect;
324 rect.left = 0;
325 rect.top = 0;
326 rect.right = nWidth;
327 rect.bottom = nHeight;
329 if(!infoPtr->hbrushBG)
330 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
332 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
333 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
335 SelectObject(hdcFinal, hbmOld2);
336 SelectObject(hdcMem, hbmFinal);
337 DeleteDC(hdcFinal);
338 DeleteObject(infoPtr->hbmPrevFrame);
339 infoPtr->hbmPrevFrame = hbmFinal;
342 if (GetWindowLongA(infoPtr->hwndSelf, GWL_STYLE) & ACS_CENTER)
344 RECT rect;
346 GetWindowRect(infoPtr->hwndSelf, &rect);
347 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
348 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
350 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
352 SelectObject(hdcMem, hbmOld);
353 DeleteDC(hdcMem);
354 return TRUE;
357 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
359 HDC hDC;
361 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
363 EnterCriticalSection(&infoPtr->cs);
365 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
366 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
368 if (infoPtr->hic &&
369 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
370 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
371 LeaveCriticalSection(&infoPtr->cs);
372 WARN("Decompression error\n");
373 return FALSE;
376 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
377 ANIMATE_PaintFrame(infoPtr, hDC);
378 ReleaseDC(infoPtr->hwndSelf, hDC);
381 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
382 infoPtr->currFrame = infoPtr->nFromFrame;
383 if (infoPtr->nLoop != -1) {
384 if (--infoPtr->nLoop == 0) {
385 ANIMATE_DoStop(infoPtr);
389 LeaveCriticalSection(&infoPtr->cs);
391 return TRUE;
394 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
396 ANIMATE_INFO* infoPtr = (ANIMATE_INFO*)ptr_;
397 HANDLE event;
398 DWORD timeout;
400 while(1)
402 EnterCriticalSection(&infoPtr->cs);
403 ANIMATE_DrawFrame(infoPtr);
404 timeout = infoPtr->mah.dwMicroSecPerFrame;
405 event = infoPtr->hStopEvent;
406 LeaveCriticalSection(&infoPtr->cs);
408 /* time is in microseconds, we should convert it to milliseconds */
409 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
410 break;
412 return TRUE;
415 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
417 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
419 /* nothing opened */
420 if (!infoPtr->hMMio)
421 return FALSE;
423 if (infoPtr->hThread || infoPtr->uTimer) {
424 TRACE("Already playing\n");
425 return TRUE;
428 infoPtr->nFromFrame = (INT)LOWORD(lParam);
429 infoPtr->nToFrame = (INT)HIWORD(lParam);
430 infoPtr->nLoop = (INT)wParam;
432 if (infoPtr->nToFrame == 0xFFFF)
433 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
435 TRACE("(repeat=%d from=%d to=%d);\n",
436 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
438 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
439 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
440 return FALSE;
442 infoPtr->currFrame = infoPtr->nFromFrame;
444 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
445 TRACE("Using a timer\n");
446 /* create a timer to display AVI */
447 infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
448 } else {
449 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
451 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
452 WM_CTLCOLORSTATIC, 0, (LPARAM)hWnd);
455 TRACE("Using an animation thread\n");
456 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
457 infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr, 0, &infoPtr->threadId);
458 if(!infoPtr->hThread)
460 ERR("Could not create animation thread!\n");
461 return FALSE;
466 ANIMATE_Notify(infoPtr, ACN_START);
468 return TRUE;
472 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
474 MMCKINFO ckMainRIFF;
475 MMCKINFO mmckHead;
476 MMCKINFO mmckList;
477 MMCKINFO mmckInfo;
478 DWORD numFrame;
479 DWORD insize;
481 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
482 WARN("Can't find 'RIFF' chunk\n");
483 return FALSE;
486 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
487 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
488 WARN("Can't find 'AVI ' chunk\n");
489 return FALSE;
492 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
493 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
494 WARN("Can't find 'hdrl' list\n");
495 return FALSE;
498 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
499 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
500 WARN("Can't find 'avih' chunk\n");
501 return FALSE;
504 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
506 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
507 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
508 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
509 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
510 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
511 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
512 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
513 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
514 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
515 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
517 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
519 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
520 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
521 WARN("Can't find 'strl' list\n");
522 return FALSE;
525 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
526 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
527 WARN("Can't find 'strh' chunk\n");
528 return FALSE;
531 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
533 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
534 HIBYTE(LOWORD(infoPtr->ash.fccType)),
535 LOBYTE(HIWORD(infoPtr->ash.fccType)),
536 HIBYTE(HIWORD(infoPtr->ash.fccType)));
537 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
538 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
539 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
540 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
541 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
542 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
543 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
544 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
545 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
546 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
547 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
548 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
549 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
550 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
551 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
552 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
553 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
555 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
557 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
558 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
559 WARN("Can't find 'strh' chunk\n");
560 return FALSE;
563 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
564 if (!infoPtr->inbih) {
565 WARN("Can't alloc input BIH\n");
566 return FALSE;
569 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
571 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
572 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
573 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
574 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
575 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
576 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
577 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
578 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
579 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
580 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
581 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
583 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
585 mmioAscend(infoPtr->hMMio, &mmckList, 0);
587 #if 0
588 /* an AVI has 0 or 1 video stream, and to be animated should not contain
589 * an audio stream, so only one strl is allowed
591 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
592 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
593 WARN("There should be a single 'strl' list\n");
594 return FALSE;
596 #endif
598 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
600 /* no need to read optional JUNK chunk */
602 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
603 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
604 WARN("Can't find 'movi' list\n");
605 return FALSE;
608 /* FIXME: should handle the 'rec ' LIST when present */
610 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
611 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
612 if (!infoPtr->lpIndex) {
613 WARN("Can't alloc index array\n");
614 return FALSE;
617 numFrame = insize = 0;
618 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
619 numFrame < infoPtr->mah.dwTotalFrames) {
620 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
621 if (insize < mmckInfo.cksize)
622 insize = mmckInfo.cksize;
623 numFrame++;
624 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
626 if (numFrame != infoPtr->mah.dwTotalFrames) {
627 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
628 return FALSE;
630 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
631 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
632 infoPtr->ash.dwSuggestedBufferSize = insize;
635 infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
636 if (!infoPtr->indata) {
637 WARN("Can't alloc input buffer\n");
638 return FALSE;
641 return TRUE;
645 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
647 DWORD outSize;
649 /* check uncompressed AVI */
650 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
651 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
652 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
654 infoPtr->hic = 0;
655 return TRUE;
658 /* try to get a decompressor for that type */
659 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
660 if (!infoPtr->hic) {
661 WARN("Can't load codec for the file\n");
662 return FALSE;
665 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
666 (DWORD)infoPtr->inbih, 0L);
668 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
669 if (!infoPtr->outbih) {
670 WARN("Can't alloc output BIH\n");
671 return FALSE;
674 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
675 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize) {
676 WARN("Can't get output BIH\n");
677 return FALSE;
680 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
681 if (!infoPtr->outdata) {
682 WARN("Can't alloc output buffer\n");
683 return FALSE;
686 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
687 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
688 WARN("Can't begin decompression\n");
689 return FALSE;
692 return TRUE;
695 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
697 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
698 HINSTANCE hInstance = (HINSTANCE)wParam;
700 ANIMATE_Free(infoPtr);
701 infoPtr->hwndSelf = hWnd;
703 if (!lParam) {
704 TRACE("Closing avi!\n");
705 /* installer of thebat! v1.62 requires FALSE here */
706 return (infoPtr->hMMio != 0);
709 if (!hInstance)
710 hInstance = (HINSTANCE)GetWindowLongPtrW(hWnd, GWLP_HINSTANCE);
712 if (HIWORD(lParam)) {
713 TRACE("(\"%s\");\n", (LPSTR)lParam);
715 if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
716 TRACE("No AVI resource found!\n");
717 if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
718 WARN("No AVI file found!\n");
719 return FALSE;
722 } else {
723 TRACE("(%u);\n", (WORD)LOWORD(lParam));
725 if (!ANIMATE_LoadResA(infoPtr, hInstance,
726 MAKEINTRESOURCEA((INT)lParam))) {
727 WARN("No AVI resource found!\n");
728 return FALSE;
732 if (!ANIMATE_GetAviInfo(infoPtr)) {
733 WARN("Can't get AVI information\n");
734 ANIMATE_Free(infoPtr);
735 return FALSE;
738 if (!ANIMATE_GetAviCodec(infoPtr)) {
739 WARN("Can't get AVI Codec\n");
740 ANIMATE_Free(infoPtr);
741 return FALSE;
744 if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
745 SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
746 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
749 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
750 return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
753 return TRUE;
757 /* << ANIMATE_Open32W >> */
759 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
761 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
763 /* nothing opened */
764 if (!infoPtr->hMMio)
765 return FALSE;
767 ANIMATE_DoStop(infoPtr);
768 return TRUE;
772 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
774 ANIMATE_INFO* infoPtr;
776 if (!fnIC.hModule) /* FIXME: not thread safe */
778 /* since there's a circular dep between msvfw32 and comctl32, we could either:
779 * - fix the build chain to allow this circular dep
780 * - handle it by hand
781 * AJ wants the latter :-(
783 fnIC.hModule = LoadLibraryA("msvfw32.dll");
784 if (!fnIC.hModule) return FALSE;
786 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
787 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
788 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
789 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
792 /* allocate memory for info structure */
793 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
794 if (!infoPtr) {
795 ERR("could not allocate info memory!\n");
796 return 0;
799 /* store crossref hWnd <-> info structure */
800 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
801 infoPtr->hwndSelf = hWnd;
802 infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
803 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
804 infoPtr->hbmPrevFrame = 0;
806 TRACE("Animate style=0x%08lx, parent=%p\n", GetWindowLongA(hWnd, GWL_STYLE), infoPtr->hwndNotify);
808 InitializeCriticalSection(&infoPtr->cs);
810 return TRUE;
814 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
816 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
819 /* free avi data */
820 ANIMATE_Free(infoPtr);
822 /* free animate info data */
823 Free(infoPtr);
824 SetWindowLongPtrW(hWnd, 0, 0);
826 return 0;
830 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
832 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
833 RECT rect;
834 HBRUSH hBrush = 0;
836 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
838 hBrush = (HBRUSH)SendMessageA(infoPtr->hwndNotify,WM_CTLCOLORSTATIC,
839 wParam, (LPARAM)hWnd);
842 GetClientRect(hWnd, &rect);
843 FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
845 return TRUE;
848 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
850 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
851 InvalidateRect(hWnd, NULL, TRUE);
853 return TRUE;
856 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
858 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
859 if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
860 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
861 switch (uMsg)
863 case ACM_OPENA:
864 return ANIMATE_OpenA(hWnd, wParam, lParam);
866 case ACM_OPENW:
867 FIXME("ACM_OPENW: stub!\n");
868 /* return ANIMATE_Open32W(hWnd, wParam, lParam); */
869 return 0;
871 case ACM_PLAY:
872 return ANIMATE_Play(hWnd, wParam, lParam);
874 case ACM_STOP:
875 return ANIMATE_Stop(hWnd, wParam, lParam);
877 case WM_NCCREATE:
878 return ANIMATE_Create(hWnd, wParam, lParam);
880 case WM_NCHITTEST:
881 return HTTRANSPARENT;
883 case WM_DESTROY:
884 return ANIMATE_Destroy(hWnd, wParam, lParam);
886 case WM_ERASEBKGND:
887 return ANIMATE_EraseBackground(hWnd, wParam, lParam);
889 /* case WM_STYLECHANGED: FIXME shall we do something ?? */
891 case WM_TIMER:
892 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
894 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
895 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
896 WM_CTLCOLORSTATIC,
897 wParam, (LPARAM)hWnd);
899 return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
901 case WM_PAINT:
903 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
905 /* the animation isn't playing, or has not decompressed
906 * (and displayed) the first frame yet, don't paint
908 if ((!infoPtr->uTimer && !infoPtr->hThread) ||
909 !infoPtr->hbmPrevFrame)
911 /* default paint handling */
912 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
915 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
916 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
917 WM_CTLCOLORSTATIC,
918 wParam, (LPARAM)hWnd);
920 if (wParam)
922 EnterCriticalSection(&infoPtr->cs);
923 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
924 LeaveCriticalSection(&infoPtr->cs);
926 else
928 PAINTSTRUCT ps;
929 HDC hDC = BeginPaint(hWnd, &ps);
931 EnterCriticalSection(&infoPtr->cs);
932 ANIMATE_PaintFrame(infoPtr, hDC);
933 LeaveCriticalSection(&infoPtr->cs);
935 EndPaint(hWnd, &ps);
938 break;
940 case WM_SIZE:
941 return ANIMATE_Size(hWnd, wParam, lParam);
943 default:
944 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
945 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
947 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
949 return 0;
952 void ANIMATE_Register(void)
954 WNDCLASSA wndClass;
956 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
957 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
958 wndClass.lpfnWndProc = ANIMATE_WindowProc;
959 wndClass.cbClsExtra = 0;
960 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
961 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
962 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
963 wndClass.lpszClassName = ANIMATE_CLASSA;
965 RegisterClassA(&wndClass);
969 void ANIMATE_Unregister(void)
971 UnregisterClassA(ANIMATE_CLASSA, NULL);