Update comments, cleanup warning messages.
[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  *                 1999 Eric Pouech
7  *
8  * NOTES
9  *   I will only improve this control once in a while.
10  *     Eric <ekohl@abo.rhein-zeitung.de>
11  *
12  * TODO:
13  *   - check for the 'rec ' list in some AVI files
14  *   - concurrent access to infoPtr
15  */
16
17
18 #include "winbase.h"
19 #include "commctrl.h"
20 #include "vfw.h"
21 #include "mmsystem.h"
22 #include "services.h"
23 #include "debugtools.h"
24
25 DEFAULT_DEBUG_CHANNEL(animate);
26
27 typedef struct
28 {
29    /* pointer to msvideo functions. it's easier to put them here.
30     * to be correct, they should be defined on a per process basis, but
31     * this would required a per process storage. We're using a per object
32     * storage instead, which is not efficient on memory usage, but
33     * will lead to less bugs in the future
34     */
35    HIC          WINAPI  (*fnICOpen)(DWORD, DWORD, UINT);
36    LRESULT      WINAPI  (*fnICClose)(HIC);
37    LRESULT      WINAPI  (*fnICSendMessage)(HIC, UINT, DWORD, DWORD);
38    DWORD        WINAPIV (*fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
39
40     HMMIO WINAPI (*fnmmioOpenA)(LPSTR,MMIOINFO*,DWORD);
41     MMRESULT WINAPI (*fnmmioClose)(HMMIO,UINT);
42     UINT  WINAPI (*fnmmioAscend)(HMMIO,MMCKINFO*,UINT);
43     UINT  WINAPI (*fnmmioDescend)(HMMIO,MMCKINFO*,const MMCKINFO*,UINT);
44     LONG  WINAPI (*fnmmioSeek)(HMMIO,LONG,INT);
45     LONG  WINAPI (*fnmmioRead)(HMMIO,HPSTR,LONG);
46
47    /* reference to input stream (file or resource) */
48    HGLOBAL              hRes;
49    HMMIO                        hMMio;  /* handle to mmio stream */
50    HWND                 hWnd;
51    /* information on the loaded AVI file */
52    MainAVIHeader        mah;
53    AVIStreamHeader      ash;
54    LPBITMAPINFOHEADER   inbih;
55    LPDWORD              lpIndex;
56    /* data for the decompressor */
57    HIC                  hic;
58    LPBITMAPINFOHEADER   outbih;
59    LPVOID               indata;
60    LPVOID               outdata;
61    /* data for the background mechanism */
62    CRITICAL_SECTION     cs;
63    HANDLE               hThread;
64    UINT                 uTimer;
65    /* data for playing the file */
66    int                  nFromFrame;
67    int                  nToFrame;
68    int                  nLoop;
69    int                  currFrame;
70    /* tranparency info*/
71    COLORREF             transparentColor;   
72    HBRUSH               hbrushBG;
73    HBITMAP              hbmPrevFrame;
74 } ANIMATE_INFO;
75
76 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongA(hWnd, 0))
77 #define ANIMATE_COLOR_NONE      0xffffffff
78
79 HMODULE hModWinmm;
80
81 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
82 {
83     SendMessageA(GetParent(infoPtr->hWnd), WM_COMMAND, 
84                  MAKEWPARAM(GetDlgCtrlID(infoPtr->hWnd), notif), 
85                  (LPARAM)infoPtr->hWnd);
86 }
87
88 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
89 {
90     HRSRC       hrsrc;
91     MMIOINFO    mminfo;
92     LPVOID      lpAvi;
93     
94     hrsrc = FindResourceA(hInst, lpName, "AVI");
95     if (!hrsrc)
96         return FALSE;
97     
98     infoPtr->hRes = LoadResource(hInst, hrsrc);
99     if (!infoPtr->hRes)
100         return FALSE;
101     
102     lpAvi = LockResource(infoPtr->hRes);
103     if (!lpAvi)
104         return FALSE;
105     
106     memset(&mminfo, 0, sizeof(mminfo));
107     mminfo.fccIOProc = FOURCC_MEM;
108     mminfo.pchBuffer = (LPSTR)lpAvi;
109     mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
110     infoPtr->hMMio = infoPtr->fnmmioOpenA(NULL, &mminfo, MMIO_READ);
111     
112     if (!infoPtr->hMMio) {
113         GlobalFree((HGLOBAL)lpAvi);
114         return FALSE;
115     }
116
117     return TRUE;
118 }
119
120
121 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
122 {
123     infoPtr->hMMio = infoPtr->fnmmioOpenA((LPSTR)lpName, NULL,
124                                MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
125     
126     if (!infoPtr->hMMio)
127         return FALSE;
128     
129     return TRUE;
130 }
131
132
133 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
134 {
135     EnterCriticalSection(&infoPtr->cs);
136
137     /* should stop playing */
138     if (infoPtr->hThread) 
139     {
140         if (!TerminateThread(infoPtr->hThread,0))
141             WARN("could not destroy animation thread!\n");
142             infoPtr->hThread = 0;
143     }
144     if (infoPtr->uTimer) {
145         KillTimer(infoPtr->hWnd, infoPtr->uTimer);
146         infoPtr->uTimer = 0;
147     }
148
149     LeaveCriticalSection(&infoPtr->cs);
150
151     ANIMATE_Notify(infoPtr, ACN_STOP);
152
153     return TRUE;
154 }
155
156
157 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
158 {
159     if (infoPtr->hMMio) {
160         ANIMATE_DoStop(infoPtr);
161         infoPtr->fnmmioClose(infoPtr->hMMio, 0);
162         if (infoPtr->hRes) {
163             FreeResource(infoPtr->hRes);
164             infoPtr->hRes = 0;
165         }
166         if (infoPtr->lpIndex) {
167             HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
168             infoPtr->lpIndex = NULL;
169         }
170         if (infoPtr->hic) {
171             (infoPtr->fnICClose)(infoPtr->hic);
172             infoPtr->hic = 0;
173         }
174         if (infoPtr->inbih) {
175             HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
176             infoPtr->inbih = NULL;
177         }
178         if (infoPtr->outbih) {
179             HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
180             infoPtr->outbih = NULL;
181         }
182         if( infoPtr->indata )
183         {
184         HeapFree(GetProcessHeap(), 0, infoPtr->indata);
185             infoPtr->indata = NULL;
186         }
187         if( infoPtr->outdata )
188         {
189         HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
190             infoPtr->outdata = NULL;
191         }
192         if( infoPtr->hbmPrevFrame )
193         {
194             DeleteObject(infoPtr->hbmPrevFrame);
195             infoPtr->hbmPrevFrame = 0;
196         }
197         infoPtr->indata = infoPtr->outdata = NULL;
198         infoPtr->hWnd = 0;
199         infoPtr->hMMio = 0;
200         
201         memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
202         memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
203         infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
204     }
205     infoPtr->transparentColor = ANIMATE_COLOR_NONE;    
206 }
207
208 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
209 {       
210     HDC hdcMask;
211     HBITMAP hbmMask;
212     HBITMAP hbmOld;            
213     
214     /* create a transparency mask */
215     hdcMask = CreateCompatibleDC(hdcDest);
216     hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);   
217     hbmOld = SelectObject(hdcMask, hbmMask);             
218
219     SetBkColor(hdcSource,infoPtr->transparentColor);
220     BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
221             
222     /* mask the source bitmap */
223     SetBkColor(hdcSource, RGB(0,0,0));          
224     SetTextColor(hdcSource, RGB(255,255,255)); 
225     BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
226
227     /* mask the destination bitmap */
228     SetBkColor(hdcDest, RGB(255,255,255));   
229     SetTextColor(hdcDest, RGB(0,0,0));         
230     BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
231
232     /* combine source and destination */
233     BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
234
235     SelectObject(hdcMask, hbmOld);
236     DeleteObject(hbmMask);
237     DeleteDC(hdcMask);
238 }
239
240 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
241 {
242     void* pBitmapData = NULL;
243     LPBITMAPINFO pBitmapInfo = NULL;
244
245     HDC hdcMem;
246     HBITMAP hbmOld;
247
248     int nOffsetX = 0;
249     int nOffsetY = 0;
250
251     int nWidth;
252     int nHeight;
253
254     if (!hDC || !infoPtr->inbih)
255         return TRUE;
256
257     if (infoPtr->hic )
258     {
259         pBitmapData = infoPtr->outdata;
260         pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
261
262         nWidth = infoPtr->outbih->biWidth;
263         nHeight = infoPtr->outbih->biHeight;  
264     } else
265     {  
266         pBitmapData = infoPtr->indata;
267         pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
268
269         nWidth = infoPtr->inbih->biWidth;
270         nHeight = infoPtr->inbih->biHeight;  
271     }  
272
273     if(!infoPtr->hbmPrevFrame)
274     {
275         infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
276     }
277
278     SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS); 
279     
280     hdcMem = CreateCompatibleDC(hDC);
281     hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
282
283     /* 
284      * we need to get the transparent color even without ACS_TRANSPARENT, 
285      * because the style can be changed later on and the color should always
286      * be obtained in the first frame 
287      */
288     if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
289     {
290         infoPtr->transparentColor = GetPixel(hdcMem,0,0);
291     } 
292
293     if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT) 
294     { 
295         HDC hdcFinal = CreateCompatibleDC(hDC);
296         HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
297         HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
298         RECT rect;
299         
300         rect.left = 0;
301         rect.top = 0;
302         rect.right = nWidth;
303         rect.bottom = nHeight;
304         
305         if(!infoPtr->hbrushBG)
306             infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
307
308         FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
309         ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
310
311         SelectObject(hdcFinal, hbmOld2);
312         SelectObject(hdcMem, hbmFinal);
313         DeleteDC(hdcFinal);
314         DeleteObject(infoPtr->hbmPrevFrame);
315         infoPtr->hbmPrevFrame = hbmFinal;
316          }
317     
318     if (GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_CENTER) 
319     {
320        RECT rect;   
321
322        GetWindowRect(infoPtr->hWnd, &rect);
323        nOffsetX = ((rect.right - rect.left) - nWidth)/2; 
324        nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;  
325     }
326     BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);    
327
328     SelectObject(hdcMem, hbmOld);
329     DeleteDC(hdcMem);
330     return TRUE;
331 }
332
333 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
334 {
335     HDC         hDC;
336
337     TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
338
339     EnterCriticalSection(&infoPtr->cs);
340
341     infoPtr->fnmmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
342     infoPtr->fnmmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
343     
344     if (infoPtr->hic &&
345         (infoPtr->fnICDecompress)(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata, 
346                                   infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
347         LeaveCriticalSection(&infoPtr->cs);
348         WARN("Decompression error\n");
349         return FALSE;
350     }
351
352     if ((hDC = GetDC(infoPtr->hWnd)) != 0) {
353         ANIMATE_PaintFrame(infoPtr, hDC);
354         ReleaseDC(infoPtr->hWnd, hDC);
355     }
356
357     if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
358         infoPtr->currFrame = infoPtr->nFromFrame;
359         if (infoPtr->nLoop != -1) {
360             if (--infoPtr->nLoop == 0) {
361                 ANIMATE_DoStop(infoPtr);
362             }
363         }
364     }
365     LeaveCriticalSection(&infoPtr->cs);
366
367     return TRUE;
368 }
369
370 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
371 {
372     ANIMATE_INFO*       infoPtr = (ANIMATE_INFO*)ptr_;
373     HDC hDC;
374     
375     if(!infoPtr)
376     {
377         WARN("animation structure undefined!\n");
378         return FALSE;
379     }
380
381     while(1)
382     {    
383         if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT) 
384         {
385             hDC = GetDC(infoPtr->hWnd);
386             infoPtr->hbrushBG = SendMessageA(GetParent(infoPtr->hWnd),WM_CTLCOLORSTATIC,hDC, infoPtr->hWnd);
387             ReleaseDC(infoPtr->hWnd,hDC);
388         }
389         
390     EnterCriticalSection(&infoPtr->cs);
391     ANIMATE_DrawFrame(infoPtr);
392     LeaveCriticalSection(&infoPtr->cs);
393     
394         /* time is in microseconds, we should convert it to milliseconds */
395         Sleep((infoPtr->mah.dwMicroSecPerFrame+500)/1000);
396 }
397     return TRUE;
398 }
399
400 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
401 {
402     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
403
404     /* nothing opened */
405     if (!infoPtr->hMMio)
406         return FALSE;
407
408     if (infoPtr->hThread || infoPtr->uTimer) {
409         FIXME("Already playing ? what should I do ??\n");
410         ANIMATE_DoStop(infoPtr);
411     }
412
413     infoPtr->nFromFrame = (INT)LOWORD(lParam);
414     infoPtr->nToFrame   = (INT)HIWORD(lParam);
415     infoPtr->nLoop      = (INT)wParam;
416
417     if (infoPtr->nToFrame == 0xFFFF)
418         infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
419
420     TRACE("(repeat=%d from=%d to=%d);\n", 
421           infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
422
423     if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
424         infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
425         return FALSE;
426
427     infoPtr->currFrame = infoPtr->nFromFrame;
428
429     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
430         TRACE("Using a timer\n");
431         /* create a timer to display AVI */
432         infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
433     } else {
434         DWORD threadID;
435
436         TRACE("Using an animation thread\n");
437         infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr,0,0 &threadID);
438         if(!infoPtr->hThread)
439         {
440            ERR("Could not create animation thread!\n");
441            return FALSE;
442     }
443         
444     }
445         
446     ANIMATE_Notify(infoPtr, ACN_START);
447
448     return TRUE;
449 }
450
451
452 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
453 {
454     MMCKINFO            ckMainRIFF;
455     MMCKINFO            mmckHead;
456     MMCKINFO            mmckList;
457     MMCKINFO            mmckInfo;
458     DWORD               numFrame;
459     DWORD               insize;
460
461     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
462         WARN("Can't find 'RIFF' chunk\n");
463         return FALSE;
464     }
465
466     if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
467         (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
468         WARN("Can't find 'AVI ' chunk\n");
469         return FALSE;
470     }
471
472     mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
473     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
474         WARN("Can't find 'hdrl' list\n");
475         return FALSE;
476     }
477
478     mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
479     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
480         WARN("Can't find 'avih' chunk\n");
481         return FALSE;
482     }
483
484     infoPtr->fnmmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
485     TRACE("mah.dwMicroSecPerFrame=%ld\n",       infoPtr->mah.dwMicroSecPerFrame);
486     TRACE("mah.dwMaxBytesPerSec=%ld\n",         infoPtr->mah.dwMaxBytesPerSec);
487     TRACE("mah.dwPaddingGranularity=%ld\n",     infoPtr->mah.dwPaddingGranularity);
488     TRACE("mah.dwFlags=%ld\n",                  infoPtr->mah.dwFlags);
489     TRACE("mah.dwTotalFrames=%ld\n",            infoPtr->mah.dwTotalFrames);
490     TRACE("mah.dwInitialFrames=%ld\n",          infoPtr->mah.dwInitialFrames);
491     TRACE("mah.dwStreams=%ld\n",                infoPtr->mah.dwStreams);
492     TRACE("mah.dwSuggestedBufferSize=%ld\n",    infoPtr->mah.dwSuggestedBufferSize);
493     TRACE("mah.dwWidth=%ld\n",                  infoPtr->mah.dwWidth);
494     TRACE("mah.dwHeight=%ld\n",                 infoPtr->mah.dwHeight);
495     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
496
497     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
498     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
499         WARN("Can't find 'strl' list\n");
500         return FALSE;
501     }
502
503     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
504     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
505         WARN("Can't find 'strh' chunk\n");
506         return FALSE;
507     }
508
509     infoPtr->fnmmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
510     TRACE("ash.fccType='%c%c%c%c'\n",           LOBYTE(LOWORD(infoPtr->ash.fccType)), 
511                                                 HIBYTE(LOWORD(infoPtr->ash.fccType)), 
512                                                 LOBYTE(HIWORD(infoPtr->ash.fccType)), 
513                                                 HIBYTE(HIWORD(infoPtr->ash.fccType)));
514     TRACE("ash.fccHandler='%c%c%c%c'\n",        LOBYTE(LOWORD(infoPtr->ash.fccHandler)), 
515                                                 HIBYTE(LOWORD(infoPtr->ash.fccHandler)), 
516                                                 LOBYTE(HIWORD(infoPtr->ash.fccHandler)), 
517                                                 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
518     TRACE("ash.dwFlags=%ld\n",                  infoPtr->ash.dwFlags);
519     TRACE("ash.wPriority=%d\n",                 infoPtr->ash.wPriority);
520     TRACE("ash.wLanguage=%d\n",                 infoPtr->ash.wLanguage);
521     TRACE("ash.dwInitialFrames=%ld\n",          infoPtr->ash.dwInitialFrames);
522     TRACE("ash.dwScale=%ld\n",                  infoPtr->ash.dwScale);
523     TRACE("ash.dwRate=%ld\n",                   infoPtr->ash.dwRate);
524     TRACE("ash.dwStart=%ld\n",                  infoPtr->ash.dwStart);
525     TRACE("ash.dwLength=%ld\n",                 infoPtr->ash.dwLength);
526     TRACE("ash.dwSuggestedBufferSize=%ld\n",    infoPtr->ash.dwSuggestedBufferSize);
527     TRACE("ash.dwQuality=%ld\n",                infoPtr->ash.dwQuality);
528     TRACE("ash.dwSampleSize=%ld\n",             infoPtr->ash.dwSampleSize);
529     TRACE("ash.rcFrame=(%d,%d,%d,%d)\n",        infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left, 
530           infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
531     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
532
533     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
534     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
535         WARN("Can't find 'strh' chunk\n");
536         return FALSE;
537     }
538
539     infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
540     if (!infoPtr->inbih) {
541         WARN("Can't alloc input BIH\n");
542         return FALSE;
543     }
544
545     infoPtr->fnmmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
546     TRACE("bih.biSize=%ld\n",           infoPtr->inbih->biSize);
547     TRACE("bih.biWidth=%ld\n",          infoPtr->inbih->biWidth);
548     TRACE("bih.biHeight=%ld\n",         infoPtr->inbih->biHeight);
549     TRACE("bih.biPlanes=%d\n",          infoPtr->inbih->biPlanes);
550     TRACE("bih.biBitCount=%d\n",        infoPtr->inbih->biBitCount);
551     TRACE("bih.biCompression=%ld\n",    infoPtr->inbih->biCompression);
552     TRACE("bih.biSizeImage=%ld\n",      infoPtr->inbih->biSizeImage);
553     TRACE("bih.biXPelsPerMeter=%ld\n",  infoPtr->inbih->biXPelsPerMeter);
554     TRACE("bih.biYPelsPerMeter=%ld\n",  infoPtr->inbih->biYPelsPerMeter);
555     TRACE("bih.biClrUsed=%ld\n",        infoPtr->inbih->biClrUsed);
556     TRACE("bih.biClrImportant=%ld\n",   infoPtr->inbih->biClrImportant);
557     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
558
559     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckList, 0);
560     
561 #if 0
562     /* an AVI has 0 or 1 video stream, and to be animated should not contain
563      * an audio stream, so only one strl is allowed 
564      */
565     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
566     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
567         WARN("There should be a single 'strl' list\n");
568         return FALSE;
569     }
570 #endif
571
572     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckHead, 0);
573
574     /* no need to read optional JUNK chunk */
575
576     mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
577     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
578         WARN("Can't find 'movi' list\n");
579         return FALSE;
580     }
581
582     /* FIXME: should handle the 'rec ' LIST when present */
583
584     infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
585                                  infoPtr->mah.dwTotalFrames * sizeof(DWORD));
586     if (!infoPtr->lpIndex) {
587         WARN("Can't alloc index array\n");
588         return FALSE;
589     }
590
591     numFrame = insize = 0;
592     while (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 && 
593            numFrame < infoPtr->mah.dwTotalFrames) {
594         infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
595         if (insize < mmckInfo.cksize)
596             insize = mmckInfo.cksize;
597         numFrame++;
598         infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
599     }
600     if (numFrame != infoPtr->mah.dwTotalFrames) {
601         WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
602         return FALSE;
603     }
604     if (insize > infoPtr->ash.dwSuggestedBufferSize) {
605         WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
606         infoPtr->ash.dwSuggestedBufferSize = insize;
607     }
608
609     infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
610     if (!infoPtr->indata) {
611         WARN("Can't alloc input buffer\n");
612         return FALSE;
613     }
614
615     return TRUE;
616 }
617
618
619 static BOOL    ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
620 {
621     DWORD       outSize;
622
623     /* check uncompressed AVI */
624     if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
625        (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')))
626     {
627         infoPtr->hic = 0;             
628         return TRUE;
629     }
630
631     /* try to get a decompressor for that type */
632     infoPtr->hic = (infoPtr->fnICOpen)(ICTYPE_VIDEO, 
633                                        infoPtr->ash.fccHandler, 
634                                        ICMODE_DECOMPRESS);
635     if (!infoPtr->hic) {
636         WARN("Can't load codec for the file\n");
637         return FALSE;
638     }
639     
640     outSize = (infoPtr->fnICSendMessage)(infoPtr->hic, 
641                                          ICM_DECOMPRESS_GET_FORMAT, 
642                                          (DWORD)infoPtr->inbih, 0L);
643     infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
644     if (!infoPtr->outbih) {
645         WARN("Can't alloc output BIH\n");
646         return FALSE;
647     }
648
649     if ((infoPtr->fnICSendMessage)(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT, 
650                                    (DWORD)infoPtr->inbih, 
651                                    (DWORD)infoPtr->outbih) != ICERR_OK) {
652         WARN("Can't get output BIH\n");
653         return FALSE;
654     }
655
656     infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
657     if (!infoPtr->outdata) {
658         WARN("Can't alloc output buffer\n");
659         return FALSE;
660     }
661
662     if ((infoPtr->fnICSendMessage)(infoPtr->hic, ICM_DECOMPRESS_BEGIN, 
663                                    (DWORD)infoPtr->inbih, 
664                                    (DWORD)infoPtr->outbih) != ICERR_OK) {
665         WARN("Can't begin decompression\n");
666         return FALSE;
667     }
668
669     return TRUE;
670 }
671
672 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
673 {
674     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
675     HINSTANCE hInstance = (HINSTANCE)wParam;
676
677     ANIMATE_Free(infoPtr);
678
679     if (!lParam) {
680         TRACE("Closing avi!\n");
681         return TRUE;
682     }
683     
684     if (!hInstance)
685        hInstance = GetWindowLongA(hWnd, GWL_HINSTANCE);
686
687     if (HIWORD(lParam)) {
688         TRACE("(\"%s\");\n", (LPSTR)lParam);
689
690         if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
691             TRACE("No AVI resource found!\n");
692             if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
693                 WARN("No AVI file found!\n");
694                 return FALSE;
695             }
696         }
697     } else {
698         TRACE("(%u);\n", (WORD)LOWORD(lParam));
699
700         if (!ANIMATE_LoadResA(infoPtr, hInstance,
701                               MAKEINTRESOURCEA((INT)lParam))) {
702             WARN("No AVI resource found!\n");
703             return FALSE;
704         }
705     }
706
707     if (!ANIMATE_GetAviInfo(infoPtr)) {
708         WARN("Can't get AVI information\n");
709         ANIMATE_Free(infoPtr);
710         return FALSE;
711     }
712
713     if (!ANIMATE_GetAviCodec(infoPtr)) {
714         WARN("Can't get AVI Codec\n");
715         ANIMATE_Free(infoPtr);
716         return FALSE;
717     }
718
719     if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
720         SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
721                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
722     }
723
724     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
725         return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
726     }
727
728     return TRUE;
729 }
730
731
732 /* << ANIMATE_Open32W >> */
733
734 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
735 {
736     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
737
738     /* nothing opened */
739     if (!infoPtr->hMMio)
740         return FALSE;
741
742     ANIMATE_DoStop(infoPtr);
743     return TRUE;
744 }
745
746
747 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
748 {
749     ANIMATE_INFO*       infoPtr;
750     HMODULE             hModule = LoadLibraryA("msvfw32.dll");
751
752     if (!hModule)
753         return FALSE;
754
755     /* allocate memory for info structure */
756     infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc(sizeof(ANIMATE_INFO));
757     if (!infoPtr) {
758         ERR("could not allocate info memory!\n");
759         return 0;
760     }
761
762     /* Temporary hack until we get dllglue up and running */
763     infoPtr->fnICOpen        = (void*)GetProcAddress(hModule, "ICOpen");
764     infoPtr->fnICClose       = (void*)GetProcAddress(hModule, "ICClose");
765     infoPtr->fnICSendMessage = (void*)GetProcAddress(hModule, "ICSendMessage");
766     infoPtr->fnICDecompress  = (void*)GetProcAddress(hModule, "ICDecompress");
767
768     TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)GetParent(hWnd));
769
770     /* store crossref hWnd <-> info structure */
771     SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
772     infoPtr->hWnd = hWnd;
773     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
774     infoPtr->hbmPrevFrame = 0;
775     hModWinmm = LoadLibraryA("WINMM");
776
777     infoPtr->fnmmioOpenA = (void*)GetProcAddress(hModWinmm, "mmioOpenA");
778     infoPtr->fnmmioClose = (void*)GetProcAddress(hModWinmm, "mmioClose");
779     infoPtr->fnmmioAscend = (void*)GetProcAddress(hModWinmm, "mmioAscend");
780     infoPtr->fnmmioDescend = (void*)GetProcAddress(hModWinmm, "mmioDescend");
781     infoPtr->fnmmioSeek = (void*)GetProcAddress(hModWinmm, "mmioSeek");
782     infoPtr->fnmmioRead = (void*)GetProcAddress(hModWinmm, "mmioRead");
783
784     InitializeCriticalSection(&infoPtr->cs);
785     
786     return 0;
787 }
788
789
790 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
791 {
792     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
793
794
795     /* free avi data */
796     ANIMATE_Free(infoPtr);
797
798     /* free animate info data */
799     COMCTL32_Free(infoPtr);
800     SetWindowLongA(hWnd, 0, 0);
801
802     FreeLibrary(hModWinmm);
803     return 0;
804 }
805
806
807 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
808 {
809     RECT rect;
810     HBRUSH hBrush = 0;       
811
812     if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT) 
813     {
814         hBrush = SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,(HDC)wParam, hWnd);    
815     }
816
817     GetClientRect(hWnd, &rect);
818     FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
819
820     return TRUE;
821 }
822
823 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
824 {
825     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
826         InvalidateRect(hWnd, NULL, TRUE);
827     }
828     return TRUE;
829 }
830
831 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
832 {
833     TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
834     if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
835         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
836     switch (uMsg)
837     {
838     case ACM_OPENA:
839         return ANIMATE_OpenA(hWnd, wParam, lParam);
840         
841         /*      case ACM_OPEN32W: FIXME!! */
842         /*          return ANIMATE_Open32W(hWnd, wParam, lParam); */
843         
844     case ACM_PLAY:
845         return ANIMATE_Play(hWnd, wParam, lParam);
846         
847     case ACM_STOP:
848         return ANIMATE_Stop(hWnd, wParam, lParam);
849         
850     case WM_NCCREATE:
851         ANIMATE_Create(hWnd, wParam, lParam);
852         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
853         
854     case WM_NCHITTEST:
855         return HTTRANSPARENT;
856
857     case WM_DESTROY:
858         ANIMATE_Destroy(hWnd, wParam, lParam);
859         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
860         
861     case WM_ERASEBKGND:
862         ANIMATE_EraseBackground(hWnd, wParam, lParam);
863         break;
864
865     /*  case WM_STYLECHANGED: FIXME shall we do something ?? */
866
867     case WM_TIMER:
868         if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
869         {
870             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
871             infoPtr->hbrushBG = SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,(HDC)wParam, hWnd);
872         }
873         return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
874         
875     case WM_CLOSE:
876         ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
877         return TRUE;
878
879     case WM_PAINT:
880         {
881             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
882                 
883             /* the animation isn't playing, don't paint */
884             if(!infoPtr->uTimer && !infoPtr->hThread)
885                 /* default paint handling */
886                 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
887             
888             if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
889                 infoPtr->hbrushBG = SendMessageA(GetParent(hWnd), WM_CTLCOLORSTATIC,
890                                                  (HDC)wParam, hWnd);
891     
892             if (wParam)
893             {
894                 EnterCriticalSection(&infoPtr->cs);
895                 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
896                 LeaveCriticalSection(&infoPtr->cs);
897             }
898             else
899             {
900                 PAINTSTRUCT ps;
901                 HDC hDC = BeginPaint(hWnd, &ps);
902
903                 EnterCriticalSection(&infoPtr->cs);
904                 ANIMATE_PaintFrame(infoPtr, hDC);
905                 LeaveCriticalSection(&infoPtr->cs);
906     
907                 EndPaint(hWnd, &ps);
908             }
909         }
910         break;
911
912     case WM_SIZE:
913         ANIMATE_Size(hWnd, wParam, lParam);
914         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
915
916     default:
917         if (uMsg >= WM_USER)
918             ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
919         
920         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
921     }
922     return 0;
923 }
924
925
926 void ANIMATE_Register(void)
927 {
928     WNDCLASSA wndClass;
929
930     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
931     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
932     wndClass.lpfnWndProc   = (WNDPROC)ANIMATE_WindowProc;
933     wndClass.cbClsExtra    = 0;
934     wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
935     wndClass.hCursor       = LoadCursorA(0, IDC_ARROWA);
936     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
937     wndClass.lpszClassName = ANIMATE_CLASSA;
938  
939     RegisterClassA(&wndClass);
940 }
941
942
943 void ANIMATE_Unregister(void)
944 {
945     UnregisterClassA(ANIMATE_CLASSA, (HINSTANCE)NULL);
946 }
947