Add support for WM_WINDOWPOSCHANGED to save new origin of window.
[wine] / dlls / avifil32 / api.c
1 /*
2  * Copyright 1999 Marcus Meissner
3  * Copyright 2001 Hidenori TAKESHIMA <hidenori@a2.ctktv.ne.jp>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <assert.h>
23
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "mmsystem.h"
27 #include "winerror.h"
28 #include "ole2.h"
29 #include "vfw.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
33
34 #include "avifile_private.h"
35
36
37 /***********************************************************************
38  *              AVIFileInit (AVIFILE.100)
39  *              AVIFileInit (AVIFIL32.@)
40  */
41 void WINAPI AVIFileInit(void)
42 {
43         TRACE("()\n");
44         if ( AVIFILE_data.dwAVIFileRef == 0 )
45         {
46                 if ( FAILED(CoInitialize(NULL)) )
47                         AVIFILE_data.fInitCOM = FALSE;
48                 else
49                         AVIFILE_data.fInitCOM = TRUE;
50         }
51         AVIFILE_data.dwAVIFileRef ++;
52 }
53
54 /***********************************************************************
55  *              AVIFileExit (AVIFILE.101)
56  *              AVIFileExit (AVIFIL32.@)
57  */
58 void WINAPI AVIFileExit(void)
59 {
60         TRACE("()\n");
61         if ( AVIFILE_data.dwAVIFileRef == 0 )
62         {
63                 ERR( "unexpected AVIFileExit()\n" );
64                 return;
65         }
66
67         AVIFILE_data.dwAVIFileRef --;
68         if ( AVIFILE_data.dwAVIFileRef == 0 )
69         {
70                 if ( AVIFILE_data.fInitCOM )
71                 {
72                         CoUninitialize();
73                         AVIFILE_data.fInitCOM = FALSE;
74                 }
75         }
76 }
77
78 /***********************************************************************
79  *              AVIFileAddRef (AVIFIL32.@)
80  */
81 ULONG WINAPI AVIFileAddRef(PAVIFILE pfile)
82 {
83         return IAVIFile_AddRef( pfile );
84 }
85
86 /***********************************************************************
87  *              AVIFileRelease (AVIFILE.141)
88  *              AVIFileRelease (AVIFIL32.@)
89  */
90 ULONG WINAPI AVIFileRelease(PAVIFILE pfile)
91 {
92         return IAVIFile_Release( pfile );
93 }
94
95 /***********************************************************************
96  *              AVIFileOpen  (AVIFILE.102)
97  *              AVIFileOpenA (AVIFIL32.@)
98  */
99 HRESULT WINAPI AVIFileOpenA(
100         PAVIFILE* ppfile,LPCSTR szFile,UINT uMode,LPCLSID lpHandler )
101 {
102         WCHAR*  pwsz;
103         HRESULT hr;
104
105         TRACE("(%p,%p,%u,%p)\n",ppfile,szFile,uMode,lpHandler);
106         pwsz = AVIFILE_strdupAtoW( szFile );
107         if ( pwsz == NULL )
108                 return AVIERR_MEMORY;
109         hr = AVIFileOpenW(ppfile,pwsz,uMode,lpHandler);
110         HeapFree( AVIFILE_data.hHeap, 0, pwsz );
111         return hr;
112 }
113
114 /***********************************************************************
115  *              AVIFileOpenW (AVIFIL32.@)
116  */
117 HRESULT WINAPI AVIFileOpenW(
118         PAVIFILE* ppfile,LPCWSTR szFile,UINT uMode,LPCLSID lpHandler )
119 {
120         HRESULT hr;
121         IClassFactory*  pcf;
122         CLSID   clsRIFF;
123
124         TRACE("(%p,%p,%u,%p)\n",ppfile,szFile,uMode,lpHandler);
125         *ppfile = (PAVIFILE)NULL;
126
127         if ( lpHandler == NULL )
128         {
129                 /* FIXME - check RIFF type and get a handler from registry
130                  *         if IAVIFile::Open is worked...
131                  */
132                 memcpy( &clsRIFF, &CLSID_AVIFile, sizeof(CLSID) );
133                 lpHandler = &clsRIFF;
134         }
135
136         /*
137          * FIXME - MS says IAVIFile::Open will be called,
138          *         but no such method in vfw.h... why????
139          */
140         if ( !IsEqualGUID( lpHandler, &CLSID_AVIFile ) )
141                 return REGDB_E_CLASSNOTREG;
142
143         hr = AVIFILE_DllGetClassObject(&CLSID_AVIFile,
144                                        &IID_IClassFactory,(void**)&pcf);
145         if ( hr != S_OK )
146                 return hr;
147
148         hr = IClassFactory_CreateInstance( pcf, NULL, &IID_IAVIFile,
149                                            (void**)ppfile );
150         IClassFactory_Release( pcf );
151
152         if ( hr == S_OK )
153         {
154                 /* FIXME??? */
155                 hr = AVIFILE_IAVIFile_Open( *ppfile, szFile, uMode );
156                 if ( hr != S_OK )
157                 {
158                         IAVIFile_Release( (*ppfile) );
159                         *ppfile = NULL;
160                 }
161         }
162
163         return hr;
164 }
165
166 /***********************************************************************
167  *              AVIFileInfoW (AVIFIL32.@)
168  */
169 HRESULT WINAPI AVIFileInfoW(PAVIFILE pfile,AVIFILEINFOW* pfi,LONG lSize)
170 {
171         return IAVIFile_Info( pfile, pfi, lSize );
172 }
173
174 /***********************************************************************
175  *              AVIFileInfo  (AVIFIL32.@)
176  *              AVIFileInfoA (AVIFIL32.@)
177  */
178 HRESULT WINAPI AVIFileInfoA(PAVIFILE pfile,AVIFILEINFOA* pfi,LONG lSize)
179 {
180         AVIFILEINFOW    fiw;
181         HRESULT         hr;
182
183         if ( lSize < sizeof(AVIFILEINFOA) )
184                 return AVIERR_BADSIZE;
185         hr = AVIFileInfoW( pfile, &fiw, sizeof(AVIFILEINFOW) );
186         if ( hr != S_OK )
187                 return hr;
188         memcpy( pfi,&fiw,sizeof(AVIFILEINFOA) );
189         AVIFILE_strncpyWtoA( pfi->szFileType, fiw.szFileType,
190                              sizeof(pfi->szFileType) );
191         pfi->szFileType[sizeof(pfi->szFileType)-1] = 0;
192
193         return S_OK;
194 }
195
196 /***********************************************************************
197  *              AVIFileGetStream (AVIFILE.143)
198  *              AVIFileGetStream (AVIFIL32.@)
199  */
200 HRESULT WINAPI AVIFileGetStream(PAVIFILE pfile,PAVISTREAM* pas,DWORD fccType,LONG lParam)
201 {
202         return IAVIFile_GetStream(pfile,pas,fccType,lParam);
203 }
204
205 /***********************************************************************
206  *              AVIFileCreateStreamW (AVIFIL32.@)
207  */
208 HRESULT WINAPI AVIFileCreateStreamW(PAVIFILE pfile,PAVISTREAM* ppas,AVISTREAMINFOW* pasi)
209 {
210         return IAVIFile_CreateStream(pfile,ppas,pasi);
211 }
212
213 /***********************************************************************
214  *              AVIFileCreateStreamA (AVIFIL32.@)
215  */
216 HRESULT WINAPI AVIFileCreateStreamA(PAVIFILE pfile,PAVISTREAM* ppas,AVISTREAMINFOA* pasi)
217 {
218         AVISTREAMINFOW  siw;
219         HRESULT         hr;
220
221         memcpy( &siw,pasi,sizeof(AVISTREAMINFOA) );
222         AVIFILE_strncpyAtoW( siw.szName, pasi->szName,
223                 sizeof(siw.szName)/sizeof(siw.szName[0]) );
224         siw.szName[sizeof(siw.szName)/sizeof(siw.szName[0])-1] = 0;
225
226         hr = AVIFileCreateStreamW(pfile,ppas,&siw);
227
228         return hr;
229 }
230
231 /***********************************************************************
232  *              AVIFileWriteData (AVIFIL32.@)
233  */
234 HRESULT WINAPI AVIFileWriteData(
235         PAVIFILE pfile,DWORD dwChunkId,LPVOID lpvData,LONG cbData )
236 {
237         return IAVIFile_WriteData( pfile,dwChunkId,lpvData,cbData );
238 }
239
240 /***********************************************************************
241  *              AVIFileReadData (AVIFIL32.@)
242  */
243 HRESULT WINAPI AVIFileReadData(
244         PAVIFILE pfile,DWORD dwChunkId,LPVOID lpvData,LPLONG pcbData )
245 {
246         return IAVIFile_ReadData( pfile,dwChunkId,lpvData,pcbData );
247 }
248
249 /***********************************************************************
250  *              AVIFileEndRecord (AVIFIL32.@)
251  */
252 HRESULT WINAPI AVIFileEndRecord( PAVIFILE pfile )
253 {
254         return IAVIFile_EndRecord( pfile );
255 }
256
257 /***********************************************************************
258  *              AVIStreamAddRef (AVIFIL32.@)
259  */
260 ULONG WINAPI AVIStreamAddRef(PAVISTREAM pas)
261 {
262         return IAVIStream_Release(pas);
263 }
264
265 /***********************************************************************
266  *              AVIStreamRelease (AVIFIL32.@)
267  */
268 ULONG WINAPI AVIStreamRelease(PAVISTREAM pas)
269 {
270         return IAVIStream_Release(pas);
271 }
272
273 /***********************************************************************
274  *              AVIStreamInfoW (AVIFIL32.@)
275  */
276 HRESULT WINAPI AVIStreamInfoW(PAVISTREAM pas,AVISTREAMINFOW* psi,LONG lSize)
277 {
278         return IAVIStream_Info(pas,psi,lSize);
279 }
280
281 /***********************************************************************
282  *              AVIStreamInfo  (AVIFIL32.@)
283  *              AVIStreamInfoA (AVIFIL32.@)
284  */
285 HRESULT WINAPI AVIStreamInfoA(PAVISTREAM pas,AVISTREAMINFOA* psi,LONG lSize)
286 {
287         AVISTREAMINFOW  siw;
288         HRESULT         hr;
289
290         if (lSize < sizeof(AVISTREAMINFOA))
291                 return AVIERR_BADSIZE;
292         hr = AVIStreamInfoW(pas,&siw,sizeof(AVISTREAMINFOW));
293         if ( hr != S_OK )
294                 return hr;
295         memcpy( psi,&siw,sizeof(AVIFILEINFOA) );
296         AVIFILE_strncpyWtoA( psi->szName, siw.szName, sizeof(psi->szName) );
297         psi->szName[sizeof(psi->szName)-1] = 0;
298
299         return hr;
300 }
301
302 /***********************************************************************
303  *              AVIStreamFindSample (AVIFIL32.@)
304  */
305 LONG WINAPI AVIStreamFindSample(PAVISTREAM pas,LONG lPos,LONG lFlags)
306 {
307         return IAVIStream_FindSample(pas,lPos,lFlags);
308 }
309
310 /***********************************************************************
311  *              AVIStreamReadFormat (AVIFIL32.@)
312  */
313 HRESULT WINAPI AVIStreamReadFormat(PAVISTREAM pas,LONG pos,LPVOID format,LONG *formatsize) {
314         return IAVIStream_ReadFormat(pas,pos,format,formatsize);
315 }
316
317 /***********************************************************************
318  *              AVIStreamSetFormat (AVIFIL32.@)
319  */
320 HRESULT WINAPI AVIStreamSetFormat(PAVISTREAM pas,LONG pos,LPVOID format,LONG formatsize) {
321         return IAVIStream_SetFormat(pas,pos,format,formatsize);
322 }
323
324 /***********************************************************************
325  *              AVIStreamReadData (AVIFIL32.@)
326  */
327 HRESULT WINAPI AVIStreamReadData(PAVISTREAM pas,DWORD fcc,LPVOID lp,LONG *lpread) {
328         return IAVIStream_ReadData(pas,fcc,lp,lpread);
329 }
330
331 /***********************************************************************
332  *              AVIStreamWriteData (AVIFIL32.@)
333  */
334 HRESULT WINAPI AVIStreamWriteData(PAVISTREAM pas,DWORD fcc,LPVOID lp,LONG size) {
335         return IAVIStream_WriteData(pas,fcc,lp,size);
336 }
337
338 /***********************************************************************
339  *              AVIStreamRead (AVIFIL32.@)
340  */
341 HRESULT WINAPI AVIStreamRead(PAVISTREAM pas,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread)
342 {
343         return IAVIStream_Read(pas,start,samples,buffer,buffersize,bytesread,samplesread);
344 }
345
346 /***********************************************************************
347  *              AVIStreamWrite (AVIFIL32.@)
348  */
349 HRESULT WINAPI AVIStreamWrite(PAVISTREAM pas,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten) {
350         return IAVIStream_Write(pas,start,samples,buffer,buffersize,flags,sampwritten,byteswritten);
351 }
352
353
354 /***********************************************************************
355  *              AVIStreamStart (AVIFIL32.@)
356  */
357 LONG WINAPI AVIStreamStart(PAVISTREAM pas)
358 {
359         AVISTREAMINFOW  si;
360         HRESULT                 hr;
361
362         hr = IAVIStream_Info(pas,&si,sizeof(si));
363         if (hr != S_OK)
364                 return -1;
365         return (LONG)si.dwStart;
366 }
367
368 /***********************************************************************
369  *              AVIStreamLength (AVIFIL32.@)
370  */
371 LONG WINAPI AVIStreamLength(PAVISTREAM pas)
372 {
373         AVISTREAMINFOW  si;
374         HRESULT                 hr;
375
376         hr = IAVIStream_Info(pas,&si,sizeof(si));
377         if (hr != S_OK)
378                 return -1;
379         return (LONG)si.dwLength;
380 }
381
382 /***********************************************************************
383  *              AVIStreamTimeToSample (AVIFIL32.@)
384  */
385 LONG WINAPI AVIStreamTimeToSample(PAVISTREAM pas,LONG lTime)
386 {
387         AVISTREAMINFOW  si;
388         HRESULT                 hr;
389
390         hr = IAVIStream_Info(pas,&si,sizeof(si));
391         if (hr != S_OK)
392                 return -1;
393
394         /* I am too lazy... */
395         FIXME("(%p,%ld)",pas,lTime);
396         return (LONG)-1L;
397 }
398
399 /***********************************************************************
400  *              AVIStreamSampleToTime (AVIFIL32.@)
401  */
402 LONG WINAPI AVIStreamSampleToTime(PAVISTREAM pas,LONG lSample)
403 {
404         AVISTREAMINFOW  si;
405         HRESULT                 hr;
406
407         hr = IAVIStream_Info(pas,&si,sizeof(si));
408         if (hr != S_OK)
409                 return -1;
410
411         /* I am too lazy... */
412         FIXME("(%p,%ld)",pas,lSample);
413         return (LONG)-1L;
414 }
415
416 /***********************************************************************
417  *              AVIStreamBeginStreaming (AVIFIL32.@)
418  */
419 HRESULT WINAPI AVIStreamBeginStreaming(PAVISTREAM pas,LONG lStart,LONG lEnd,LONG lRate)
420 {
421         FIXME("(%p)->(%ld,%ld,%ld),stub!\n",pas,lStart,lEnd,lRate);
422         return E_FAIL;
423 }
424
425 /***********************************************************************
426  *              AVIStreamEndStreaming (AVIFIL32.@)
427  */
428 HRESULT WINAPI AVIStreamEndStreaming(PAVISTREAM pas)
429 {
430         FIXME("(%p)->(),stub!\n",pas);
431         return E_FAIL;
432 }
433
434 /***********************************************************************
435  *              AVIStreamGetFrameOpen (AVIFIL32.@)
436  */
437 PGETFRAME WINAPI AVIStreamGetFrameOpen(PAVISTREAM pas,LPBITMAPINFOHEADER pbi)
438 {
439         IGetFrame*      pgf;
440         HRESULT         hr;
441         AVISTREAMINFOW  si;
442
443         FIXME("(%p,%p)\n",pas,pbi);
444
445         hr = IAVIStream_Info(pas,&si,sizeof(si));
446         if (hr != S_OK)
447                 return NULL;
448
449         hr = AVIFILE_CreateIGetFrame((void**)&pgf,pas,pbi);
450         if ( hr != S_OK )
451                 return NULL;
452         hr = IGetFrame_Begin( pgf, si.dwStart, si.dwLength, 1000 );
453         if ( hr != S_OK )
454         {
455                 IGetFrame_Release( pgf );
456                 return NULL;
457         }
458
459         return pgf;
460 }
461
462 /***********************************************************************
463  *              AVIStreamGetFrame (AVIFIL32.@)
464  */
465 LPVOID WINAPI AVIStreamGetFrame(PGETFRAME pgf, LONG lPos)
466 {
467         return IGetFrame_GetFrame(pgf,lPos);
468 }
469
470 /***********************************************************************
471  *              AVIStreamGetFrameClose (AVIFIL32.@)
472  */
473 HRESULT WINAPI AVIStreamGetFrameClose(PGETFRAME pgf)
474 {
475         return IGetFrame_End(pgf);
476 }
477
478 /***********************************************************************
479  *              AVIStreamOpenFromFileA (AVIFIL32.@)
480  */
481 HRESULT WINAPI AVIStreamOpenFromFileA(PAVISTREAM* ppas, LPCSTR szFile, DWORD fccType, LONG lParam, UINT uMode, CLSID* lpHandler)
482 {
483         WCHAR*  pwsz;
484         HRESULT hr;
485
486         pwsz = AVIFILE_strdupAtoW( szFile );
487         if ( pwsz == NULL )
488                 return AVIERR_MEMORY;
489         hr = AVIStreamOpenFromFileW(ppas,pwsz,fccType,lParam,uMode,lpHandler);
490         HeapFree( AVIFILE_data.hHeap, 0, pwsz );
491         return hr;
492 }
493
494 /***********************************************************************
495  *              AVIStreamOpenFromFileW (AVIFIL32.@)
496  */
497 HRESULT WINAPI AVIStreamOpenFromFileW(PAVISTREAM* ppas, LPCWSTR szFile, DWORD fccType, LONG lParam, UINT uMode, CLSID* lpHandler)
498 {
499         HRESULT hr;
500         PAVIFILE        paf;
501         AVIFILEINFOW    fi;
502
503         *ppas = NULL;
504         hr = AVIFileOpenW(&paf,szFile,uMode,lpHandler);
505         if ( hr != S_OK )
506                 return hr;
507         hr = AVIFileInfoW(paf,&fi,sizeof(AVIFILEINFOW));
508         if ( hr == S_OK )
509                 hr = AVIFileGetStream(paf,ppas,fccType,lParam);
510
511         IAVIFile_Release(paf);
512
513         return hr;
514 }
515
516 /***********************************************************************
517  *              AVIStreamCreate (AVIFIL32.@)
518  */
519 HRESULT WINAPI AVIStreamCreate(PAVISTREAM* ppas, LONG lParam1, LONG lParam2, CLSID* lpHandler)
520 {
521         HRESULT hr;
522         IClassFactory*  pcf;
523
524         *ppas = NULL;
525
526         if ( lpHandler == NULL )
527         {
528                 hr = AVIFILE_DllGetClassObject(&CLSID_AVIFile,
529                                                &IID_IClassFactory,(void**)&pcf);
530         }
531         else
532         {
533                 if ( !AVIFILE_data.fInitCOM )
534                         return E_UNEXPECTED;
535                 hr = CoGetClassObject(lpHandler,CLSCTX_INPROC_SERVER,
536                                       NULL,&IID_IClassFactory,(void**)&pcf);
537         }
538         if ( hr != S_OK )
539                 return hr;
540
541         hr = IClassFactory_CreateInstance( pcf, NULL, &IID_IAVIStream,
542                                            (void**)ppas );
543         IClassFactory_Release( pcf );
544
545         if ( hr == S_OK )
546         {
547                 hr = IAVIStream_Create((*ppas),lParam1,lParam2);
548                 if ( hr != S_OK )
549                 {
550                         IAVIStream_Release((*ppas));
551                         *ppas = NULL;
552                 }
553         }
554
555         return hr;
556 }
557
558 /***********************************************************************
559  *              AVIMakeCompressedStream (AVIFIL32.@)
560  */
561 HRESULT WINAPI AVIMakeCompressedStream(PAVISTREAM *ppsCompressed,PAVISTREAM ppsSource,AVICOMPRESSOPTIONS *aco,CLSID *pclsidHandler)
562 {
563         FIXME("(%p,%p,%p,%p)\n",ppsCompressed,ppsSource,aco,pclsidHandler);
564         return E_FAIL;
565 }
566