Do not kill the animation thread with TerminateThread, let it finish
[wine.git] / dlls / comctl32 / animate.c
bloba7c47e14e525e1ff0dafe3a8a9131fad91e61c2a
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 UINT uTimer;
77 /* data for playing the file */
78 int nFromFrame;
79 int nToFrame;
80 int nLoop;
81 int currFrame;
82 /* tranparency info*/
83 COLORREF transparentColor;
84 HBRUSH hbrushBG;
85 HBITMAP hbmPrevFrame;
86 } ANIMATE_INFO;
88 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongA(hWnd, 0))
89 #define ANIMATE_COLOR_NONE 0xffffffff
91 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
93 SendMessageA(infoPtr->hwndNotify, WM_COMMAND,
94 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
95 (LPARAM)infoPtr->hwndSelf);
98 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
100 HRSRC hrsrc;
101 MMIOINFO mminfo;
102 LPVOID lpAvi;
104 hrsrc = FindResourceA(hInst, lpName, "AVI");
105 if (!hrsrc)
106 return FALSE;
108 infoPtr->hRes = LoadResource(hInst, hrsrc);
109 if (!infoPtr->hRes)
110 return FALSE;
112 lpAvi = LockResource(infoPtr->hRes);
113 if (!lpAvi)
114 return FALSE;
116 memset(&mminfo, 0, sizeof(mminfo));
117 mminfo.fccIOProc = FOURCC_MEM;
118 mminfo.pchBuffer = (LPSTR)lpAvi;
119 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
120 infoPtr->hMMio = mmioOpenA(NULL, &mminfo, MMIO_READ);
121 if (!infoPtr->hMMio) {
122 GlobalFree((HGLOBAL)lpAvi);
123 return FALSE;
126 return TRUE;
130 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
132 infoPtr->hMMio = mmioOpenA((LPSTR)lpName, NULL,
133 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
135 if (!infoPtr->hMMio)
136 return FALSE;
138 return TRUE;
142 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
144 EnterCriticalSection(&infoPtr->cs);
146 /* should stop playing */
147 if (infoPtr->hThread)
149 HANDLE handle = infoPtr->hThread;
151 TRACE("stopping animation thread\n");
152 SetEvent( infoPtr->hStopEvent );
153 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
154 WaitForSingleObject( handle, INFINITE );
155 TRACE("animation thread stopped\n");
156 EnterCriticalSection(&infoPtr->cs);
157 CloseHandle( infoPtr->hThread );
158 CloseHandle( infoPtr->hStopEvent );
159 infoPtr->hThread = 0;
161 if (infoPtr->uTimer) {
162 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
163 infoPtr->uTimer = 0;
166 LeaveCriticalSection(&infoPtr->cs);
168 ANIMATE_Notify(infoPtr, ACN_STOP);
170 return TRUE;
174 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
176 if (infoPtr->hMMio) {
177 ANIMATE_DoStop(infoPtr);
178 mmioClose(infoPtr->hMMio, 0);
179 if (infoPtr->hRes) {
180 FreeResource(infoPtr->hRes);
181 infoPtr->hRes = 0;
183 if (infoPtr->lpIndex) {
184 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
185 infoPtr->lpIndex = NULL;
187 if (infoPtr->hic) {
188 fnIC.fnICClose(infoPtr->hic);
189 infoPtr->hic = 0;
191 if (infoPtr->inbih) {
192 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
193 infoPtr->inbih = NULL;
195 if (infoPtr->outbih) {
196 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
197 infoPtr->outbih = NULL;
199 if( infoPtr->indata )
201 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
202 infoPtr->indata = NULL;
204 if( infoPtr->outdata )
206 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
207 infoPtr->outdata = NULL;
209 if( infoPtr->hbmPrevFrame )
211 DeleteObject(infoPtr->hbmPrevFrame);
212 infoPtr->hbmPrevFrame = 0;
214 infoPtr->indata = infoPtr->outdata = NULL;
215 infoPtr->hwndSelf = 0;
216 infoPtr->hMMio = 0;
218 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
219 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
220 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
222 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
225 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
227 HDC hdcMask;
228 HBITMAP hbmMask;
229 HBITMAP hbmOld;
231 /* create a transparency mask */
232 hdcMask = CreateCompatibleDC(hdcDest);
233 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
234 hbmOld = SelectObject(hdcMask, hbmMask);
236 SetBkColor(hdcSource,infoPtr->transparentColor);
237 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
239 /* mask the source bitmap */
240 SetBkColor(hdcSource, RGB(0,0,0));
241 SetTextColor(hdcSource, RGB(255,255,255));
242 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
244 /* mask the destination bitmap */
245 SetBkColor(hdcDest, RGB(255,255,255));
246 SetTextColor(hdcDest, RGB(0,0,0));
247 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
249 /* combine source and destination */
250 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
252 SelectObject(hdcMask, hbmOld);
253 DeleteObject(hbmMask);
254 DeleteDC(hdcMask);
257 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
259 void* pBitmapData = NULL;
260 LPBITMAPINFO pBitmapInfo = NULL;
262 HDC hdcMem;
263 HBITMAP hbmOld;
265 int nOffsetX = 0;
266 int nOffsetY = 0;
268 int nWidth;
269 int nHeight;
271 if (!hDC || !infoPtr->inbih)
272 return TRUE;
274 if (infoPtr->hic )
276 pBitmapData = infoPtr->outdata;
277 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
279 nWidth = infoPtr->outbih->biWidth;
280 nHeight = infoPtr->outbih->biHeight;
281 } else
283 pBitmapData = infoPtr->indata;
284 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
286 nWidth = infoPtr->inbih->biWidth;
287 nHeight = infoPtr->inbih->biHeight;
290 if(!infoPtr->hbmPrevFrame)
292 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
295 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
297 hdcMem = CreateCompatibleDC(hDC);
298 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
301 * we need to get the transparent color even without ACS_TRANSPARENT,
302 * because the style can be changed later on and the color should always
303 * be obtained in the first frame
305 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
307 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
310 if(GetWindowLongA(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
312 HDC hdcFinal = CreateCompatibleDC(hDC);
313 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
314 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
315 RECT rect;
317 rect.left = 0;
318 rect.top = 0;
319 rect.right = nWidth;
320 rect.bottom = nHeight;
322 if(!infoPtr->hbrushBG)
323 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
325 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
326 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
328 SelectObject(hdcFinal, hbmOld2);
329 SelectObject(hdcMem, hbmFinal);
330 DeleteDC(hdcFinal);
331 DeleteObject(infoPtr->hbmPrevFrame);
332 infoPtr->hbmPrevFrame = hbmFinal;
335 if (GetWindowLongA(infoPtr->hwndSelf, GWL_STYLE) & ACS_CENTER)
337 RECT rect;
339 GetWindowRect(infoPtr->hwndSelf, &rect);
340 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
341 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
343 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
345 SelectObject(hdcMem, hbmOld);
346 DeleteDC(hdcMem);
347 return TRUE;
350 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
352 HDC hDC;
354 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
356 EnterCriticalSection(&infoPtr->cs);
358 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
359 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
361 if (infoPtr->hic &&
362 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
363 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
364 LeaveCriticalSection(&infoPtr->cs);
365 WARN("Decompression error\n");
366 return FALSE;
369 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
370 ANIMATE_PaintFrame(infoPtr, hDC);
371 ReleaseDC(infoPtr->hwndSelf, hDC);
374 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
375 infoPtr->currFrame = infoPtr->nFromFrame;
376 if (infoPtr->nLoop != -1) {
377 if (--infoPtr->nLoop == 0) {
378 ANIMATE_DoStop(infoPtr);
382 LeaveCriticalSection(&infoPtr->cs);
384 return TRUE;
387 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
389 ANIMATE_INFO* infoPtr = (ANIMATE_INFO*)ptr_;
390 HANDLE event;
391 DWORD timeout;
393 while(1)
395 EnterCriticalSection(&infoPtr->cs);
396 ANIMATE_DrawFrame(infoPtr);
397 timeout = infoPtr->mah.dwMicroSecPerFrame;
398 event = infoPtr->hStopEvent;
399 LeaveCriticalSection(&infoPtr->cs);
401 /* time is in microseconds, we should convert it to milliseconds */
402 if (WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
403 break;
405 return TRUE;
408 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
410 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
412 /* nothing opened */
413 if (!infoPtr->hMMio)
414 return FALSE;
416 if (infoPtr->hThread || infoPtr->uTimer) {
417 FIXME("Already playing ? what should I do ??\n");
418 ANIMATE_DoStop(infoPtr);
421 infoPtr->nFromFrame = (INT)LOWORD(lParam);
422 infoPtr->nToFrame = (INT)HIWORD(lParam);
423 infoPtr->nLoop = (INT)wParam;
425 if (infoPtr->nToFrame == 0xFFFF)
426 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
428 TRACE("(repeat=%d from=%d to=%d);\n",
429 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
431 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
432 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
433 return FALSE;
435 infoPtr->currFrame = infoPtr->nFromFrame;
437 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
438 TRACE("Using a timer\n");
439 /* create a timer to display AVI */
440 infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
441 } else {
442 DWORD threadID;
444 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
446 HDC hDC = GetDC(hWnd);
447 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
448 WM_CTLCOLORSTATIC, 0, (LPARAM)hWnd);
449 ReleaseDC(hWnd,hDC);
452 TRACE("Using an animation thread\n");
453 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
454 infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr, 0, &threadID);
455 if(!infoPtr->hThread)
457 ERR("Could not create animation thread!\n");
458 return FALSE;
463 ANIMATE_Notify(infoPtr, ACN_START);
465 return TRUE;
469 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
471 MMCKINFO ckMainRIFF;
472 MMCKINFO mmckHead;
473 MMCKINFO mmckList;
474 MMCKINFO mmckInfo;
475 DWORD numFrame;
476 DWORD insize;
478 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
479 WARN("Can't find 'RIFF' chunk\n");
480 return FALSE;
483 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
484 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
485 WARN("Can't find 'AVI ' chunk\n");
486 return FALSE;
489 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
490 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
491 WARN("Can't find 'hdrl' list\n");
492 return FALSE;
495 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
496 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
497 WARN("Can't find 'avih' chunk\n");
498 return FALSE;
501 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
503 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
504 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
505 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
506 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
507 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
508 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
509 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
510 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
511 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
512 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
514 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
516 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
517 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
518 WARN("Can't find 'strl' list\n");
519 return FALSE;
522 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
523 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
524 WARN("Can't find 'strh' chunk\n");
525 return FALSE;
528 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
530 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
531 HIBYTE(LOWORD(infoPtr->ash.fccType)),
532 LOBYTE(HIWORD(infoPtr->ash.fccType)),
533 HIBYTE(HIWORD(infoPtr->ash.fccType)));
534 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
535 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
536 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
537 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
538 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
539 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
540 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
541 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
542 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
543 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
544 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
545 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
546 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
547 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
548 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
549 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
550 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
552 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
554 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
555 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
556 WARN("Can't find 'strh' chunk\n");
557 return FALSE;
560 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
561 if (!infoPtr->inbih) {
562 WARN("Can't alloc input BIH\n");
563 return FALSE;
566 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
568 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
569 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
570 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
571 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
572 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
573 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
574 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
575 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
576 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
577 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
578 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
580 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
582 mmioAscend(infoPtr->hMMio, &mmckList, 0);
584 #if 0
585 /* an AVI has 0 or 1 video stream, and to be animated should not contain
586 * an audio stream, so only one strl is allowed
588 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
589 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
590 WARN("There should be a single 'strl' list\n");
591 return FALSE;
593 #endif
595 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
597 /* no need to read optional JUNK chunk */
599 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
600 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
601 WARN("Can't find 'movi' list\n");
602 return FALSE;
605 /* FIXME: should handle the 'rec ' LIST when present */
607 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
608 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
609 if (!infoPtr->lpIndex) {
610 WARN("Can't alloc index array\n");
611 return FALSE;
614 numFrame = insize = 0;
615 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
616 numFrame < infoPtr->mah.dwTotalFrames) {
617 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
618 if (insize < mmckInfo.cksize)
619 insize = mmckInfo.cksize;
620 numFrame++;
621 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
623 if (numFrame != infoPtr->mah.dwTotalFrames) {
624 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
625 return FALSE;
627 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
628 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
629 infoPtr->ash.dwSuggestedBufferSize = insize;
632 infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
633 if (!infoPtr->indata) {
634 WARN("Can't alloc input buffer\n");
635 return FALSE;
638 return TRUE;
642 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
644 DWORD outSize;
646 /* check uncompressed AVI */
647 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
648 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
649 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
651 infoPtr->hic = 0;
652 return TRUE;
655 /* try to get a decompressor for that type */
656 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
657 if (!infoPtr->hic) {
658 WARN("Can't load codec for the file\n");
659 return FALSE;
662 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
663 (DWORD)infoPtr->inbih, 0L);
665 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
666 if (!infoPtr->outbih) {
667 WARN("Can't alloc output BIH\n");
668 return FALSE;
671 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
672 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize) {
673 WARN("Can't get output BIH\n");
674 return FALSE;
677 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
678 if (!infoPtr->outdata) {
679 WARN("Can't alloc output buffer\n");
680 return FALSE;
683 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
684 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
685 WARN("Can't begin decompression\n");
686 return FALSE;
689 return TRUE;
692 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
694 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
695 HINSTANCE hInstance = (HINSTANCE)wParam;
697 ANIMATE_Free(infoPtr);
698 infoPtr->hwndSelf = hWnd;
700 if (!lParam) {
701 TRACE("Closing avi!\n");
702 /* installer of thebat! v1.62 requires FALSE here */
703 return (infoPtr->hMMio != 0);
706 if (!hInstance)
707 hInstance = (HINSTANCE)GetWindowLongA(hWnd, GWL_HINSTANCE);
709 if (HIWORD(lParam)) {
710 TRACE("(\"%s\");\n", (LPSTR)lParam);
712 if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
713 TRACE("No AVI resource found!\n");
714 if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
715 WARN("No AVI file found!\n");
716 return FALSE;
719 } else {
720 TRACE("(%u);\n", (WORD)LOWORD(lParam));
722 if (!ANIMATE_LoadResA(infoPtr, hInstance,
723 MAKEINTRESOURCEA((INT)lParam))) {
724 WARN("No AVI resource found!\n");
725 return FALSE;
729 if (!ANIMATE_GetAviInfo(infoPtr)) {
730 WARN("Can't get AVI information\n");
731 ANIMATE_Free(infoPtr);
732 return FALSE;
735 if (!ANIMATE_GetAviCodec(infoPtr)) {
736 WARN("Can't get AVI Codec\n");
737 ANIMATE_Free(infoPtr);
738 return FALSE;
741 if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
742 SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
743 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
746 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
747 return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
750 return TRUE;
754 /* << ANIMATE_Open32W >> */
756 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
758 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
760 /* nothing opened */
761 if (!infoPtr->hMMio)
762 return FALSE;
764 ANIMATE_DoStop(infoPtr);
765 return TRUE;
769 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
771 ANIMATE_INFO* infoPtr;
773 if (!fnIC.hModule) /* FIXME: not thread safe */
775 /* since there's a circular dep between msvfw32 and comctl32, we could either:
776 * - fix the build chain to allow this circular dep
777 * - handle it by hand
778 * AJ wants the latter :-(
780 fnIC.hModule = LoadLibraryA("msvfw32.dll");
781 if (!fnIC.hModule) return FALSE;
783 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
784 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
785 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
786 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
789 /* allocate memory for info structure */
790 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
791 if (!infoPtr) {
792 ERR("could not allocate info memory!\n");
793 return 0;
796 /* store crossref hWnd <-> info structure */
797 SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
798 infoPtr->hwndSelf = hWnd;
799 infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
800 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
801 infoPtr->hbmPrevFrame = 0;
803 TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)infoPtr->hwndNotify);
805 InitializeCriticalSection(&infoPtr->cs);
807 return 0;
811 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
813 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
816 /* free avi data */
817 ANIMATE_Free(infoPtr);
819 /* free animate info data */
820 Free(infoPtr);
821 SetWindowLongA(hWnd, 0, 0);
823 return 0;
827 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
829 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
830 RECT rect;
831 HBRUSH hBrush = 0;
833 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
835 hBrush = (HBRUSH)SendMessageA(infoPtr->hwndNotify,WM_CTLCOLORSTATIC,
836 wParam, (LPARAM)hWnd);
839 GetClientRect(hWnd, &rect);
840 FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
842 return TRUE;
845 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
847 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
848 InvalidateRect(hWnd, NULL, TRUE);
850 return TRUE;
853 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
855 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
856 if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
857 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
858 switch (uMsg)
860 case ACM_OPENA:
861 return ANIMATE_OpenA(hWnd, wParam, lParam);
863 /* case ACM_OPEN32W: FIXME!! */
864 /* return ANIMATE_Open32W(hWnd, wParam, lParam); */
866 case ACM_PLAY:
867 return ANIMATE_Play(hWnd, wParam, lParam);
869 case ACM_STOP:
870 return ANIMATE_Stop(hWnd, wParam, lParam);
872 case WM_NCCREATE:
873 ANIMATE_Create(hWnd, wParam, lParam);
874 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
876 case WM_NCHITTEST:
877 return HTTRANSPARENT;
879 case WM_DESTROY:
880 ANIMATE_Destroy(hWnd, wParam, lParam);
881 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
883 case WM_ERASEBKGND:
884 ANIMATE_EraseBackground(hWnd, wParam, lParam);
885 break;
887 /* case WM_STYLECHANGED: FIXME shall we do something ?? */
889 case WM_TIMER:
890 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
892 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
893 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
894 WM_CTLCOLORSTATIC,
895 wParam, (LPARAM)hWnd);
897 return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
899 case WM_CLOSE:
900 ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
901 return TRUE;
903 case WM_PAINT:
905 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
907 /* the animation isn't playing, or has not decompressed
908 * (and displayed) the first frame yet, don't paint
910 if ((!infoPtr->uTimer && !infoPtr->hThread) ||
911 !infoPtr->hbmPrevFrame)
913 /* default paint handling */
914 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
917 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
918 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
919 WM_CTLCOLORSTATIC,
920 wParam, (LPARAM)hWnd);
922 if (wParam)
924 EnterCriticalSection(&infoPtr->cs);
925 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
926 LeaveCriticalSection(&infoPtr->cs);
928 else
930 PAINTSTRUCT ps;
931 HDC hDC = BeginPaint(hWnd, &ps);
933 EnterCriticalSection(&infoPtr->cs);
934 ANIMATE_PaintFrame(infoPtr, hDC);
935 LeaveCriticalSection(&infoPtr->cs);
937 EndPaint(hWnd, &ps);
940 break;
942 case WM_SIZE:
943 ANIMATE_Size(hWnd, wParam, lParam);
944 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
946 default:
947 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
948 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
950 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
952 return 0;
955 void ANIMATE_Register(void)
957 WNDCLASSA wndClass;
959 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
960 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
961 wndClass.lpfnWndProc = (WNDPROC)ANIMATE_WindowProc;
962 wndClass.cbClsExtra = 0;
963 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
964 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
965 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
966 wndClass.lpszClassName = ANIMATE_CLASSA;
968 RegisterClassA(&wndClass);
972 void ANIMATE_Unregister(void)
974 UnregisterClassA(ANIMATE_CLASSA, NULL);