Fix direct sound capabilities to match hardware.
[wine] / dlls / comctl32 / animate.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3  * Animation control
4  *
5  * Copyright 1998, 1999 Eric Kohl
6  * Copyright 1999 Eric Pouech
7  * Copyright 2005 Dimitrie O. Paun
8  *
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.
13  *
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.
18  *
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
22  *
23  * NOTES
24  *
25  * This code was audited for completeness against the documented features
26  * of Comctl32.dll version 6.0 on Mar. 15, 2005, by Dimitrie O. Paun.
27  * 
28  * Unless otherwise noted, we believe this code to be complete, as per
29  * the specification mentioned above.
30  * If you discover missing features, or bugs, please note them below.
31  * 
32  * TODO:
33  *   - check for the 'rec ' list in some AVI files
34  */
35
36 #define COM_NO_WINDOWS_H
37 #include <stdarg.h>
38 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "vfw.h"
46 #include "mmsystem.h"
47 #include "comctl32.h"
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(animate);
51
52 static struct {
53     HMODULE     hModule;
54     HIC         (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
55     LRESULT     (WINAPI *fnICClose)(HIC);
56     LRESULT     (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
57     DWORD       (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
58 } fnIC;
59
60 typedef struct
61 {
62    /* reference to input stream (file or resource) */
63    HGLOBAL              hRes;
64    HMMIO                hMMio;  /* handle to mmio stream */
65    HWND                 hwndSelf;
66    HWND                 hwndNotify;
67    DWORD                dwStyle;
68    /* information on the loaded AVI file */
69    MainAVIHeader        mah;
70    AVIStreamHeader      ash;
71    LPBITMAPINFOHEADER   inbih;
72    LPDWORD              lpIndex;
73    /* data for the decompressor */
74    HIC                  hic;
75    LPBITMAPINFOHEADER   outbih;
76    LPVOID               indata;
77    LPVOID               outdata;
78    /* data for the background mechanism */
79    CRITICAL_SECTION     cs;
80    HANDLE               hStopEvent;
81    HANDLE               hThread;
82    DWORD                threadId;
83    UINT                 uTimer;
84    /* data for playing the file */
85    int                  nFromFrame;
86    int                  nToFrame;
87    int                  nLoop;
88    int                  currFrame;
89    /* tranparency info*/
90    COLORREF             transparentColor;
91    HBRUSH               hbrushBG;
92    HBITMAP              hbmPrevFrame;
93 } ANIMATE_INFO;
94
95 #define ANIMATE_COLOR_NONE      0xffffffff
96
97 static void ANIMATE_Notify(ANIMATE_INFO *infoPtr, UINT notif)
98 {
99     SendMessageW(infoPtr->hwndNotify, WM_COMMAND,
100                  MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
101                  (LPARAM)infoPtr->hwndSelf);
102 }
103
104 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPWSTR lpName)
105 {
106     static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
107     HRSRC       hrsrc;
108     MMIOINFO    mminfo;
109     LPVOID      lpAvi;
110
111     hrsrc = FindResourceW(hInst, lpName, aviW);
112     if (!hrsrc)
113         return FALSE;
114
115     infoPtr->hRes = LoadResource(hInst, hrsrc);
116     if (!infoPtr->hRes)
117         return FALSE;
118
119     lpAvi = LockResource(infoPtr->hRes);
120     if (!lpAvi)
121         return FALSE;
122
123     memset(&mminfo, 0, sizeof(mminfo));
124     mminfo.fccIOProc = FOURCC_MEM;
125     mminfo.pchBuffer = (LPSTR)lpAvi;
126     mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
127     infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
128     if (!infoPtr->hMMio) 
129     {
130         FreeResource(infoPtr->hRes);
131         return FALSE;
132     }
133
134     return TRUE;
135 }
136
137
138 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
139 {
140     infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
141
142     return (BOOL)infoPtr->hMMio;
143 }
144
145
146 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
147 {
148     EnterCriticalSection(&infoPtr->cs);
149
150     /* should stop playing */
151     if (infoPtr->hThread)
152     {
153         HANDLE handle = infoPtr->hThread;
154
155         TRACE("stopping animation thread\n");
156         infoPtr->hThread = 0;
157         SetEvent( infoPtr->hStopEvent );
158
159         if (infoPtr->threadId != GetCurrentThreadId())
160         {
161             LeaveCriticalSection(&infoPtr->cs);  /* leave it a chance to run */
162             WaitForSingleObject( handle, INFINITE );
163             TRACE("animation thread stopped\n");
164             EnterCriticalSection(&infoPtr->cs);
165         }
166
167         CloseHandle( handle );
168         CloseHandle( infoPtr->hStopEvent );
169         infoPtr->hStopEvent = 0;
170     }
171     if (infoPtr->uTimer) {
172         KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
173         infoPtr->uTimer = 0;
174     }
175
176     LeaveCriticalSection(&infoPtr->cs);
177
178     ANIMATE_Notify(infoPtr, ACN_STOP);
179
180     return TRUE;
181 }
182
183
184 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
185 {
186     if (infoPtr->hMMio) {
187         ANIMATE_DoStop(infoPtr);
188         mmioClose(infoPtr->hMMio, 0);
189         if (infoPtr->hRes) {
190             FreeResource(infoPtr->hRes);
191             infoPtr->hRes = 0;
192         }
193         HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
194         infoPtr->lpIndex = NULL;
195         if (infoPtr->hic) {
196             fnIC.fnICClose(infoPtr->hic);
197             infoPtr->hic = 0;
198         }
199         HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
200         infoPtr->inbih = NULL;
201         HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
202         infoPtr->outbih = NULL;
203         HeapFree(GetProcessHeap(), 0, infoPtr->indata);
204         infoPtr->indata = NULL;
205         HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
206         infoPtr->outdata = NULL;
207         if( infoPtr->hbmPrevFrame )
208         {
209             DeleteObject(infoPtr->hbmPrevFrame);
210             infoPtr->hbmPrevFrame = 0;
211         }
212
213         memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
214         memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
215         infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
216     }
217     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
218 }
219
220 static void ANIMATE_TransparentBlt(ANIMATE_INFO *infoPtr, HDC hdcDest, HDC hdcSource)
221 {
222     HDC hdcMask;
223     HBITMAP hbmMask;
224     HBITMAP hbmOld;
225
226     /* create a transparency mask */
227     hdcMask = CreateCompatibleDC(hdcDest);
228     hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
229     hbmOld = SelectObject(hdcMask, hbmMask);
230
231     SetBkColor(hdcSource,infoPtr->transparentColor);
232     BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
233
234     /* mask the source bitmap */
235     SetBkColor(hdcSource, RGB(0,0,0));
236     SetTextColor(hdcSource, RGB(255,255,255));
237     BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
238
239     /* mask the destination bitmap */
240     SetBkColor(hdcDest, RGB(255,255,255));
241     SetTextColor(hdcDest, RGB(0,0,0));
242     BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
243
244     /* combine source and destination */
245     BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
246
247     SelectObject(hdcMask, hbmOld);
248     DeleteObject(hbmMask);
249     DeleteDC(hdcMask);
250 }
251
252 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
253 {
254     void* pBitmapData = NULL;
255     LPBITMAPINFO pBitmapInfo = NULL;
256
257     HDC hdcMem;
258     HBITMAP hbmOld;
259
260     int nOffsetX = 0;
261     int nOffsetY = 0;
262
263     int nWidth;
264     int nHeight;
265
266     if (!hDC || !infoPtr->inbih)
267         return TRUE;
268
269     if (infoPtr->hic )
270     {
271         pBitmapData = infoPtr->outdata;
272         pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
273
274         nWidth = infoPtr->outbih->biWidth;
275         nHeight = infoPtr->outbih->biHeight;
276     } 
277     else
278     {
279         pBitmapData = infoPtr->indata;
280         pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
281
282         nWidth = infoPtr->inbih->biWidth;
283         nHeight = infoPtr->inbih->biHeight;
284     }
285
286     if(!infoPtr->hbmPrevFrame)
287     {
288         infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
289     }
290
291     SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
292
293     hdcMem = CreateCompatibleDC(hDC);
294     hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
295
296     /*
297      * we need to get the transparent color even without ACS_TRANSPARENT,
298      * because the style can be changed later on and the color should always
299      * be obtained in the first frame
300      */
301     if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
302     {
303         infoPtr->transparentColor = GetPixel(hdcMem,0,0);
304     }
305
306     if(infoPtr->dwStyle & ACS_TRANSPARENT)
307     {
308         HDC hdcFinal = CreateCompatibleDC(hDC);
309         HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
310         HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
311         RECT rect;
312
313         rect.left = 0;
314         rect.top = 0;
315         rect.right = nWidth;
316         rect.bottom = nHeight;
317
318         if(!infoPtr->hbrushBG)
319             infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
320
321         FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
322         ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
323
324         SelectObject(hdcFinal, hbmOld2);
325         SelectObject(hdcMem, hbmFinal);
326         DeleteDC(hdcFinal);
327         DeleteObject(infoPtr->hbmPrevFrame);
328         infoPtr->hbmPrevFrame = hbmFinal;
329     }
330
331     if (infoPtr->dwStyle & ACS_CENTER)
332     {
333         RECT rect;
334
335         GetWindowRect(infoPtr->hwndSelf, &rect);
336         nOffsetX = ((rect.right - rect.left) - nWidth)/2;
337         nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
338     }
339     BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
340
341     SelectObject(hdcMem, hbmOld);
342     DeleteDC(hdcMem);
343     return TRUE;
344 }
345
346 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
347 {
348     HDC         hDC;
349
350     TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
351
352     mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
353     mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
354
355     if (infoPtr->hic &&
356         fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
357                      infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
358         WARN("Decompression error\n");
359         return FALSE;
360     }
361
362     if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
363         ANIMATE_PaintFrame(infoPtr, hDC);
364         ReleaseDC(infoPtr->hwndSelf, hDC);
365     }
366
367     if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
368         infoPtr->currFrame = infoPtr->nFromFrame;
369         if (infoPtr->nLoop != -1) {
370             if (--infoPtr->nLoop == 0) {
371                 ANIMATE_DoStop(infoPtr);
372             }
373         }
374     }
375
376     return TRUE;
377 }
378
379 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
380 {
381    /* FIXME: we should pass the hDC instead of 0 to WM_CTLCOLORSTATIC */
382    if (infoPtr->dwStyle & ACS_TRANSPARENT)
383         infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
384                                                  WM_CTLCOLORSTATIC,
385                                                  0, (LPARAM)infoPtr->hwndSelf);
386     EnterCriticalSection(&infoPtr->cs);
387     ANIMATE_DrawFrame(infoPtr);
388     LeaveCriticalSection(&infoPtr->cs);
389
390     return 0;
391 }
392
393 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
394 {
395     ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
396     HANDLE event;
397     DWORD timeout;
398
399     while(1)
400     {
401         EnterCriticalSection(&infoPtr->cs);
402         ANIMATE_DrawFrame(infoPtr);
403         timeout = infoPtr->mah.dwMicroSecPerFrame;
404         event = infoPtr->hStopEvent;
405         LeaveCriticalSection(&infoPtr->cs);
406
407         /* time is in microseconds, we should convert it to milliseconds */
408         if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
409             break;
410     }
411     return TRUE;
412 }
413
414 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
415 {
416     /* nothing opened */
417     if (!infoPtr->hMMio)
418         return FALSE;
419
420     if (infoPtr->hThread || infoPtr->uTimer) {
421         TRACE("Already playing\n");
422         return TRUE;
423     }
424
425     infoPtr->nFromFrame = wFrom;
426     infoPtr->nToFrame   = wTo;
427     infoPtr->nLoop      = cRepeat;
428
429     if (infoPtr->nToFrame == 0xFFFF)
430         infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
431
432     TRACE("(repeat=%d from=%d to=%d);\n",
433           infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
434
435     if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
436         infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
437         return FALSE;
438
439     infoPtr->currFrame = infoPtr->nFromFrame;
440
441     if (infoPtr->dwStyle & ACS_TIMER) 
442     {
443         TRACE("Using a timer\n");
444         /* create a timer to display AVI */
445         infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1, 
446                                    infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
447     } 
448     else 
449     {
450         if(infoPtr->dwStyle & ACS_TRANSPARENT)
451             infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
452                                                      WM_CTLCOLORSTATIC, 0,
453                                                      (LPARAM)infoPtr->hwndSelf);
454
455         TRACE("Using an animation thread\n");
456         infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
457         infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
458                                         (LPVOID)infoPtr, 0, &infoPtr->threadId);
459         if(!infoPtr->hThread) return FALSE;
460
461     }
462
463     ANIMATE_Notify(infoPtr, ACN_START);
464
465     return TRUE;
466 }
467
468
469 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
470 {
471     MMCKINFO            ckMainRIFF;
472     MMCKINFO            mmckHead;
473     MMCKINFO            mmckList;
474     MMCKINFO            mmckInfo;
475     DWORD               numFrame;
476     DWORD               insize;
477
478     if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
479         WARN("Can't find 'RIFF' chunk\n");
480         return FALSE;
481     }
482
483     if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
484         (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
485         WARN("Can't find 'AVI ' chunk\n");
486         return FALSE;
487     }
488
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;
493     }
494
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;
499     }
500
501     mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
502
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);
513
514     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
515
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;
520     }
521
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;
526     }
527
528     mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
529
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);
551
552     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
553
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;
558     }
559
560     infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
561     if (!infoPtr->inbih) {
562         WARN("Can't alloc input BIH\n");
563         return FALSE;
564     }
565
566     mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
567
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);
579
580     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
581
582     mmioAscend(infoPtr->hMMio, &mmckList, 0);
583
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
587      */
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;
592     }
593 #endif
594
595     mmioAscend(infoPtr->hMMio, &mmckHead, 0);
596
597     /* no need to read optional JUNK chunk */
598
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;
603     }
604
605     /* FIXME: should handle the 'rec ' LIST when present */
606
607     infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
608                                  infoPtr->mah.dwTotalFrames * sizeof(DWORD));
609     if (!infoPtr->lpIndex) 
610         return FALSE;
611
612     numFrame = insize = 0;
613     while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
614            numFrame < infoPtr->mah.dwTotalFrames) {
615         infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
616         if (insize < mmckInfo.cksize)
617             insize = mmckInfo.cksize;
618         numFrame++;
619         mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
620     }
621     if (numFrame != infoPtr->mah.dwTotalFrames) {
622         WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
623         return FALSE;
624     }
625     if (insize > infoPtr->ash.dwSuggestedBufferSize) {
626         WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
627         infoPtr->ash.dwSuggestedBufferSize = insize;
628     }
629
630     infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
631     if (!infoPtr->indata) 
632         return FALSE;
633
634     return TRUE;
635 }
636
637
638 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
639 {
640     DWORD       outSize;
641
642     /* check uncompressed AVI */
643     if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
644        (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
645        (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
646     {
647         infoPtr->hic = 0;
648         return TRUE;
649     }
650
651     /* try to get a decompressor for that type */
652     infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
653     if (!infoPtr->hic) {
654         WARN("Can't load codec for the file\n");
655         return FALSE;
656     }
657
658     outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
659                             (DWORD)infoPtr->inbih, 0L);
660
661     infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
662     if (!infoPtr->outbih)
663         return FALSE;
664
665     if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
666                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize) 
667     {
668         WARN("Can't get output BIH\n");
669         return FALSE;
670     }
671
672     infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
673     if (!infoPtr->outdata) 
674         return FALSE;
675
676     if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
677                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
678         WARN("Can't begin decompression\n");
679         return FALSE;
680     }
681
682     return TRUE;
683 }
684
685
686 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
687 {
688     ANIMATE_Free(infoPtr);
689
690     if (!lpszName) 
691     {
692         TRACE("Closing avi!\n");
693         /* installer of thebat! v1.62 requires FALSE here */
694         return (infoPtr->hMMio != 0);
695     }
696
697     if (!hInstance)
698         hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
699
700     if (HIWORD(lpszName)) 
701     {
702         TRACE("(\"%s\");\n", debugstr_w(lpszName));
703
704         if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName)) 
705         {
706             TRACE("No AVI resource found!\n");
707             if (!ANIMATE_LoadFileW(infoPtr, lpszName)) 
708             {
709                 WARN("No AVI file found!\n");
710                 return FALSE;
711             }
712         }
713     } 
714     else 
715     {
716         TRACE("(%u);\n", (WORD)(DWORD)lpszName);
717
718         if (!ANIMATE_LoadResW(infoPtr, hInstance, MAKEINTRESOURCEW((INT)lpszName))) 
719         {
720             WARN("No AVI resource found!\n");
721             return FALSE;
722         }
723     }
724
725     if (!ANIMATE_GetAviInfo(infoPtr)) 
726     {
727         WARN("Can't get AVI information\n");
728         ANIMATE_Free(infoPtr);
729         return FALSE;
730     }
731
732     if (!ANIMATE_GetAviCodec(infoPtr)) 
733     {
734         WARN("Can't get AVI Codec\n");
735         ANIMATE_Free(infoPtr);
736         return FALSE;
737     }
738
739     if (!(infoPtr->dwStyle & ACS_CENTER))
740         SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
741                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
742
743     if (infoPtr->dwStyle & ACS_AUTOPLAY) 
744         return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
745
746     return TRUE;
747 }
748
749
750 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
751 {
752     LPWSTR lpwszName;
753     LRESULT result;
754     INT len;
755
756     if (!HIWORD(lpszName))
757         return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
758
759     len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
760     lpwszName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
761     if (!lpwszName) return FALSE;
762     MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
763
764     result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
765     HeapFree(GetProcessHeap(), 0, lpwszName);
766     return result;
767 }
768
769
770 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
771 {
772     /* nothing opened */
773     if (!infoPtr->hMMio)
774         return FALSE;
775
776     ANIMATE_DoStop(infoPtr);
777     return TRUE;
778 }
779
780
781 static BOOL ANIMATE_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
782 {
783     static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
784     ANIMATE_INFO *infoPtr;
785
786     if (!fnIC.hModule)
787     {
788         fnIC.hModule = LoadLibraryW(msvfw32W);
789         if (!fnIC.hModule) return FALSE;
790
791         fnIC.fnICOpen        = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
792         fnIC.fnICClose       = (void*)GetProcAddress(fnIC.hModule, "ICClose");
793         fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
794         fnIC.fnICDecompress  = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
795     }
796
797     /* allocate memory for info structure */
798     infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
799     if (!infoPtr) return FALSE;
800
801     /* store crossref hWnd <-> info structure */
802     SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
803     infoPtr->hwndSelf = hWnd;
804     infoPtr->hwndNotify = lpcs->hwndParent;
805     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
806     infoPtr->hbmPrevFrame = 0;
807     infoPtr->dwStyle = lpcs->style;
808
809     TRACE("Animate style=0x%08lx, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
810
811     InitializeCriticalSection(&infoPtr->cs);
812
813     return TRUE;
814 }
815
816
817 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
818 {
819     /* free avi data */
820     ANIMATE_Free(infoPtr);
821
822     /* free animate info data */
823     SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
824
825     DeleteCriticalSection(&infoPtr->cs);
826     Free(infoPtr);
827
828     return 0;
829 }
830
831
832 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO *infoPtr, HDC hdc)
833 {
834     RECT rect;
835     HBRUSH hBrush = 0;
836
837     if(infoPtr->dwStyle & ACS_TRANSPARENT)
838     {
839         hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
840                                       (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
841     }
842
843     GetClientRect(infoPtr->hwndSelf, &rect);
844     FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
845
846     return TRUE;
847 }
848
849
850 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, LPSTYLESTRUCT lpss)
851 {
852     TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
853           wStyleType, lpss->styleOld, lpss->styleNew);
854
855     if (wStyleType != GWL_STYLE) return 0;
856   
857     infoPtr->dwStyle = lpss->styleNew;
858
859     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
860     return 0;
861 }
862
863
864 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
865 {
866     ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
867
868     TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
869     if (!infoPtr && (uMsg != WM_NCCREATE))
870         return DefWindowProcW(hWnd, uMsg, wParam, lParam);
871     switch (uMsg)
872     {
873     case ACM_OPENA:
874         return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
875
876     case ACM_OPENW:
877         return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
878
879     case ACM_PLAY:
880         return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
881
882     case ACM_STOP:
883         return ANIMATE_Stop(infoPtr);
884
885     case WM_CLOSE:
886         ANIMATE_Free(infoPtr);
887         return 0;
888
889     case WM_NCCREATE:
890         return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
891
892     case WM_NCHITTEST:
893         return HTTRANSPARENT;
894
895     case WM_DESTROY:
896         return ANIMATE_Destroy(infoPtr);
897
898     case WM_ERASEBKGND:
899         return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
900
901     case WM_STYLECHANGED:
902         return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
903
904     case WM_TIMER:
905         return ANIMATE_Timer(infoPtr);
906
907     case WM_PAINT:
908         {
909             /* the animation isn't playing, or has not decompressed
910              * (and displayed) the first frame yet, don't paint
911              */
912             if ((!infoPtr->uTimer && !infoPtr->hThread) ||
913                 !infoPtr->hbmPrevFrame)
914             {
915                 /* default paint handling */
916                 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
917             }
918
919             if (infoPtr->dwStyle & ACS_TRANSPARENT)
920                 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
921                                                          WM_CTLCOLORSTATIC,
922                                                          wParam, (LPARAM)infoPtr->hwndSelf);
923
924             if (wParam)
925             {
926                 EnterCriticalSection(&infoPtr->cs);
927                 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
928                 LeaveCriticalSection(&infoPtr->cs);
929             }
930             else
931             {
932                 PAINTSTRUCT ps;
933                 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
934
935                 EnterCriticalSection(&infoPtr->cs);
936                 ANIMATE_PaintFrame(infoPtr, hDC);
937                 LeaveCriticalSection(&infoPtr->cs);
938
939                 EndPaint(infoPtr->hwndSelf, &ps);
940             }
941         }
942         break;
943
944     case WM_SIZE:
945         if (infoPtr->dwStyle & ACS_CENTER) 
946             InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
947         return DefWindowProcW(hWnd, uMsg, wParam, lParam);
948
949     default:
950         if ((uMsg >= WM_USER) && (uMsg < WM_APP))
951             ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
952
953         return DefWindowProcW(hWnd, uMsg, wParam, lParam);
954     }
955     return 0;
956 }
957
958 void ANIMATE_Register(void)
959 {
960     WNDCLASSW wndClass;
961
962     ZeroMemory(&wndClass, sizeof(WNDCLASSW));
963     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
964     wndClass.lpfnWndProc   = ANIMATE_WindowProc;
965     wndClass.cbClsExtra    = 0;
966     wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
967     wndClass.hCursor       = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
968     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
969     wndClass.lpszClassName = ANIMATE_CLASSW;
970
971     RegisterClassW(&wndClass);
972 }
973
974
975 void ANIMATE_Unregister(void)
976 {
977     UnregisterClassW(ANIMATE_CLASSW, NULL);
978 }