1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
5 * Copyright 1998, 1999 Eric Kohl
6 * Copyright 1999 Eric Pouech
7 * Copyright 2005 Dimitrie O. Paun
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * I will only improve this control once in a while.
25 * Eric <ekohl@abo.rhein-zeitung.de>
28 * - check for the 'rec ' list in some AVI files
29 * - concurrent access to infoPtr
32 #define COM_NO_WINDOWS_H
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(animate);
50 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
51 LRESULT (WINAPI *fnICClose)(HIC);
52 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
53 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
58 /* reference to input stream (file or resource) */
60 HMMIO hMMio; /* handle to mmio stream */
63 /* information on the loaded AVI file */
66 LPBITMAPINFOHEADER inbih;
68 /* data for the decompressor */
70 LPBITMAPINFOHEADER outbih;
73 /* data for the background mechanism */
79 /* data for playing the file */
85 COLORREF transparentColor;
90 #define ANIMATE_COLOR_NONE 0xffffffff
92 static void ANIMATE_Notify(ANIMATE_INFO *infoPtr, UINT notif)
94 SendMessageW(infoPtr->hwndNotify, WM_COMMAND,
95 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
96 (LPARAM)infoPtr->hwndSelf);
99 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPWSTR lpName)
101 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
106 hrsrc = FindResourceW(hInst, lpName, aviW);
110 infoPtr->hRes = LoadResource(hInst, hrsrc);
114 lpAvi = LockResource(infoPtr->hRes);
118 memset(&mminfo, 0, sizeof(mminfo));
119 mminfo.fccIOProc = FOURCC_MEM;
120 mminfo.pchBuffer = (LPSTR)lpAvi;
121 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
122 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
125 FreeResource(infoPtr->hRes);
133 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
135 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
137 return (BOOL)infoPtr->hMMio;
141 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
143 EnterCriticalSection(&infoPtr->cs);
145 /* should stop playing */
146 if (infoPtr->hThread)
148 HANDLE handle = infoPtr->hThread;
150 TRACE("stopping animation thread\n");
151 infoPtr->hThread = 0;
152 SetEvent( infoPtr->hStopEvent );
154 if (infoPtr->threadId != GetCurrentThreadId())
156 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
157 WaitForSingleObject( handle, INFINITE );
158 TRACE("animation thread stopped\n");
159 EnterCriticalSection(&infoPtr->cs);
162 CloseHandle( handle );
163 CloseHandle( infoPtr->hStopEvent );
164 infoPtr->hStopEvent = 0;
166 if (infoPtr->uTimer) {
167 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
171 LeaveCriticalSection(&infoPtr->cs);
173 ANIMATE_Notify(infoPtr, ACN_STOP);
179 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
181 if (infoPtr->hMMio) {
182 ANIMATE_DoStop(infoPtr);
183 mmioClose(infoPtr->hMMio, 0);
185 FreeResource(infoPtr->hRes);
188 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
189 infoPtr->lpIndex = NULL;
191 fnIC.fnICClose(infoPtr->hic);
194 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
195 infoPtr->inbih = NULL;
196 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
197 infoPtr->outbih = NULL;
198 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
199 infoPtr->indata = NULL;
200 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
201 infoPtr->outdata = NULL;
202 if( infoPtr->hbmPrevFrame )
204 DeleteObject(infoPtr->hbmPrevFrame);
205 infoPtr->hbmPrevFrame = 0;
208 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
209 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
210 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
212 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
215 static void ANIMATE_TransparentBlt(ANIMATE_INFO *infoPtr, HDC hdcDest, HDC hdcSource)
221 /* create a transparency mask */
222 hdcMask = CreateCompatibleDC(hdcDest);
223 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
224 hbmOld = SelectObject(hdcMask, hbmMask);
226 SetBkColor(hdcSource,infoPtr->transparentColor);
227 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
229 /* mask the source bitmap */
230 SetBkColor(hdcSource, RGB(0,0,0));
231 SetTextColor(hdcSource, RGB(255,255,255));
232 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
234 /* mask the destination bitmap */
235 SetBkColor(hdcDest, RGB(255,255,255));
236 SetTextColor(hdcDest, RGB(0,0,0));
237 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
239 /* combine source and destination */
240 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
242 SelectObject(hdcMask, hbmOld);
243 DeleteObject(hbmMask);
247 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
249 void* pBitmapData = NULL;
250 LPBITMAPINFO pBitmapInfo = NULL;
261 if (!hDC || !infoPtr->inbih)
264 if (GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
265 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
266 (WPARAM)hDC, (LPARAM)infoPtr->hwndSelf);
270 pBitmapData = infoPtr->outdata;
271 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
273 nWidth = infoPtr->outbih->biWidth;
274 nHeight = infoPtr->outbih->biHeight;
278 pBitmapData = infoPtr->indata;
279 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
281 nWidth = infoPtr->inbih->biWidth;
282 nHeight = infoPtr->inbih->biHeight;
285 if(!infoPtr->hbmPrevFrame)
287 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
290 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
292 hdcMem = CreateCompatibleDC(hDC);
293 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
296 * we need to get the transparent color even without ACS_TRANSPARENT,
297 * because the style can be changed later on and the color should always
298 * be obtained in the first frame
300 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
302 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
305 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
307 HDC hdcFinal = CreateCompatibleDC(hDC);
308 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
309 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
315 rect.bottom = nHeight;
317 if(!infoPtr->hbrushBG)
318 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
320 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
321 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
323 SelectObject(hdcFinal, hbmOld2);
324 SelectObject(hdcMem, hbmFinal);
326 DeleteObject(infoPtr->hbmPrevFrame);
327 infoPtr->hbmPrevFrame = hbmFinal;
330 if (GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_CENTER)
334 GetWindowRect(infoPtr->hwndSelf, &rect);
335 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
336 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
338 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
340 SelectObject(hdcMem, hbmOld);
345 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
349 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
351 EnterCriticalSection(&infoPtr->cs);
353 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
354 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
357 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
358 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
359 LeaveCriticalSection(&infoPtr->cs);
360 WARN("Decompression error\n");
364 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
365 ANIMATE_PaintFrame(infoPtr, hDC);
366 ReleaseDC(infoPtr->hwndSelf, hDC);
369 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
370 infoPtr->currFrame = infoPtr->nFromFrame;
371 if (infoPtr->nLoop != -1) {
372 if (--infoPtr->nLoop == 0) {
373 ANIMATE_DoStop(infoPtr);
377 LeaveCriticalSection(&infoPtr->cs);
382 static LRESULT ANIMATE_Paint(ANIMATE_INFO *infoPtr, HDC hdc)
387 if (!hdc) hDC = BeginPaint(infoPtr->hwndSelf, &ps);
389 EnterCriticalSection(&infoPtr->cs);
390 ANIMATE_PaintFrame(infoPtr, hDC);
391 LeaveCriticalSection(&infoPtr->cs);
393 if (!hdc) EndPaint(infoPtr->hwndSelf, &ps);
398 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
400 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
406 EnterCriticalSection(&infoPtr->cs);
407 ANIMATE_DrawFrame(infoPtr);
408 timeout = infoPtr->mah.dwMicroSecPerFrame;
409 event = infoPtr->hStopEvent;
410 LeaveCriticalSection(&infoPtr->cs);
412 /* time is in microseconds, we should convert it to milliseconds */
413 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
419 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
425 if (infoPtr->hThread || infoPtr->uTimer) {
426 TRACE("Already playing\n");
430 infoPtr->nFromFrame = wFrom;
431 infoPtr->nToFrame = wTo;
432 infoPtr->nLoop = cRepeat;
434 if (infoPtr->nToFrame == 0xFFFF)
435 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
437 TRACE("(repeat=%d from=%d to=%d);\n",
438 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
440 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
441 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
444 infoPtr->currFrame = infoPtr->nFromFrame;
446 if (GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_TIMER)
448 TRACE("Using a timer\n");
449 /* create a timer to display AVI */
450 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
451 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
455 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
457 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
458 WM_CTLCOLORSTATIC, 0,
459 (LPARAM)infoPtr->hwndSelf);
462 TRACE("Using an animation thread\n");
463 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
464 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
465 (LPVOID)infoPtr, 0, &infoPtr->threadId);
466 if(!infoPtr->hThread)
468 ERR("Could not create animation thread!\n");
474 ANIMATE_Notify(infoPtr, ACN_START);
480 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
489 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
490 WARN("Can't find 'RIFF' chunk\n");
494 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
495 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
496 WARN("Can't find 'AVI ' chunk\n");
500 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
501 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
502 WARN("Can't find 'hdrl' list\n");
506 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
507 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
508 WARN("Can't find 'avih' chunk\n");
512 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
514 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
515 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
516 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
517 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
518 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
519 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
520 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
521 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
522 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
523 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
525 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
527 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
528 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
529 WARN("Can't find 'strl' list\n");
533 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
534 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
535 WARN("Can't find 'strh' chunk\n");
539 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
541 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
542 HIBYTE(LOWORD(infoPtr->ash.fccType)),
543 LOBYTE(HIWORD(infoPtr->ash.fccType)),
544 HIBYTE(HIWORD(infoPtr->ash.fccType)));
545 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
546 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
547 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
548 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
549 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
550 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
551 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
552 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
553 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
554 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
555 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
556 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
557 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
558 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
559 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
560 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
561 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
563 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
565 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
566 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
567 WARN("Can't find 'strh' chunk\n");
571 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
572 if (!infoPtr->inbih) {
573 WARN("Can't alloc input BIH\n");
577 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
579 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
580 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
581 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
582 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
583 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
584 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
585 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
586 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
587 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
588 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
589 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
591 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
593 mmioAscend(infoPtr->hMMio, &mmckList, 0);
596 /* an AVI has 0 or 1 video stream, and to be animated should not contain
597 * an audio stream, so only one strl is allowed
599 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
600 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
601 WARN("There should be a single 'strl' list\n");
606 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
608 /* no need to read optional JUNK chunk */
610 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
611 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
612 WARN("Can't find 'movi' list\n");
616 /* FIXME: should handle the 'rec ' LIST when present */
618 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
619 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
620 if (!infoPtr->lpIndex)
623 numFrame = insize = 0;
624 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
625 numFrame < infoPtr->mah.dwTotalFrames) {
626 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
627 if (insize < mmckInfo.cksize)
628 insize = mmckInfo.cksize;
630 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
632 if (numFrame != infoPtr->mah.dwTotalFrames) {
633 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
636 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
637 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
638 infoPtr->ash.dwSuggestedBufferSize = insize;
641 infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
642 if (!infoPtr->indata)
649 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
653 /* check uncompressed AVI */
654 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
655 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
656 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
662 /* try to get a decompressor for that type */
663 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
665 WARN("Can't load codec for the file\n");
669 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
670 (DWORD)infoPtr->inbih, 0L);
672 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
673 if (!infoPtr->outbih)
676 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
677 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize)
679 WARN("Can't get output BIH\n");
683 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
684 if (!infoPtr->outdata)
687 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
688 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
689 WARN("Can't begin decompression\n");
696 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
698 ANIMATE_Free(infoPtr);
702 TRACE("Closing avi!\n");
703 /* installer of thebat! v1.62 requires FALSE here */
704 return (infoPtr->hMMio != 0);
708 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
710 if (HIWORD(lpszName))
712 TRACE("(\"%s\");\n", debugstr_w(lpszName));
714 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
716 TRACE("No AVI resource found!\n");
717 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
719 WARN("No AVI file found!\n");
726 TRACE("(%u);\n", (WORD)(DWORD)lpszName);
728 if (!ANIMATE_LoadResW(infoPtr, hInstance, MAKEINTRESOURCEW((INT)lpszName)))
730 WARN("No AVI resource found!\n");
735 if (!ANIMATE_GetAviInfo(infoPtr))
737 WARN("Can't get AVI information\n");
738 ANIMATE_Free(infoPtr);
742 if (!ANIMATE_GetAviCodec(infoPtr))
744 WARN("Can't get AVI Codec\n");
745 ANIMATE_Free(infoPtr);
749 if (!GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_CENTER)
750 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
751 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
753 if (GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_AUTOPLAY)
754 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
760 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
766 if (!HIWORD(lpszName))
767 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
769 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
770 lpwszName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
771 if (!lpwszName) return FALSE;
772 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
774 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
775 HeapFree(GetProcessHeap(), 0, lpwszName);
780 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
786 ANIMATE_DoStop(infoPtr);
791 static BOOL ANIMATE_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
793 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
794 ANIMATE_INFO *infoPtr;
796 if (!fnIC.hModule) /* FIXME: not thread safe */
798 /* since there's a circular dep between msvfw32 and comctl32, we could either:
799 * - fix the build chain to allow this circular dep
800 * - handle it by hand
801 * AJ wants the latter :-(
803 fnIC.hModule = LoadLibraryW(msvfw32W);
804 if (!fnIC.hModule) return FALSE;
806 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
807 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
808 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
809 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
812 /* allocate memory for info structure */
813 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
814 if (!infoPtr) return FALSE;
816 /* store crossref hWnd <-> info structure */
817 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
818 infoPtr->hwndSelf = hWnd;
819 infoPtr->hwndNotify = lpcs->hwndParent;
820 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
821 infoPtr->hbmPrevFrame = 0;
823 TRACE("Animate style=0x%08lx, parent=%p\n", GetWindowLongW(hWnd, GWL_STYLE), infoPtr->hwndNotify);
825 InitializeCriticalSection(&infoPtr->cs);
831 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
834 ANIMATE_Free(infoPtr);
836 /* free animate info data */
837 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
844 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO *infoPtr, HDC hdc)
849 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
851 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
852 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
855 GetClientRect(infoPtr->hwndSelf, &rect);
856 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
861 static LRESULT ANIMATE_Size(ANIMATE_INFO *infoPtr, INT flags, WORD width, WORD height)
863 if (GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & ACS_CENTER)
865 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
870 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
872 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
874 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
875 if (!infoPtr && (uMsg != WM_NCCREATE))
876 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
880 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
883 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
886 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
889 return ANIMATE_Stop(infoPtr);
892 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
895 return HTTRANSPARENT;
898 return ANIMATE_Destroy(infoPtr);
901 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
903 /* case WM_STYLECHANGED: FIXME shall we do something ?? */
906 return ANIMATE_DrawFrame(infoPtr);
909 /* the animation isn't playing, or has not decompressed
910 * (and displayed) the first frame yet, do default painting
912 if ((!infoPtr->uTimer && !infoPtr->hThread) || !infoPtr->hbmPrevFrame)
913 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
915 return ANIMATE_Paint(infoPtr, (HDC)wParam);
918 return ANIMATE_Size(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
921 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
922 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
924 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
929 void ANIMATE_Register(void)
933 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
934 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
935 wndClass.lpfnWndProc = ANIMATE_WindowProc;
936 wndClass.cbClsExtra = 0;
937 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
938 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
939 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
940 wndClass.lpszClassName = ANIMATE_CLASSW;
942 RegisterClassW(&wndClass);
946 void ANIMATE_Unregister(void)
948 UnregisterClassW(ANIMATE_CLASSW, NULL);