mciavi: MCI_OPEN @NNNN should use an open AVIFile/Stream handle.
[wine] / dlls / mciqtz32 / mciqtz.c
1 /*
2  * DirectShow MCI Driver
3  *
4  * Copyright 2009 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "mmddk.h"
26 #include "wine/debug.h"
27 #include "mciqtz_private.h"
28 #include "digitalv.h"
29 #include "wownt32.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(mciqtz);
32
33 static DWORD MCIQTZ_mciClose(UINT, DWORD, LPMCI_GENERIC_PARMS);
34 static DWORD MCIQTZ_mciStop(UINT, DWORD, LPMCI_GENERIC_PARMS);
35
36 /*======================================================================*
37  *                          MCI QTZ implementation                      *
38  *======================================================================*/
39
40 HINSTANCE MCIQTZ_hInstance = 0;
41
42 /***********************************************************************
43  *              DllMain (MCIQTZ.0)
44  */
45 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
46 {
47     switch (fdwReason) {
48     case DLL_PROCESS_ATTACH:
49         DisableThreadLibraryCalls(hInstDLL);
50         MCIQTZ_hInstance = hInstDLL;
51         break;
52     }
53     return TRUE;
54 }
55
56 /**************************************************************************
57  *                              MCIQTZ_mciGetOpenDev            [internal]
58  */
59 static WINE_MCIQTZ* MCIQTZ_mciGetOpenDev(UINT wDevID)
60 {
61     WINE_MCIQTZ* wma = (WINE_MCIQTZ*)mciGetDriverData(wDevID);
62
63     if (!wma) {
64         WARN("Invalid wDevID=%u\n", wDevID);
65         return NULL;
66     }
67     return wma;
68 }
69
70 /**************************************************************************
71  *                              MCIQTZ_drvOpen                  [internal]
72  */
73 static DWORD MCIQTZ_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
74 {
75     WINE_MCIQTZ* wma;
76     static const WCHAR mciAviWStr[] = {'M','C','I','A','V','I',0};
77
78     TRACE("(%s, %p)\n", debugstr_w(str), modp);
79
80     /* session instance */
81     if (!modp)
82         return 0xFFFFFFFF;
83
84     wma = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIQTZ));
85     if (!wma)
86         return 0;
87
88     modp->wType = MCI_DEVTYPE_DIGITAL_VIDEO;
89     wma->wDevID = modp->wDeviceID;
90     modp->wCustomCommandTable = wma->command_table = mciLoadCommandResource(MCIQTZ_hInstance, mciAviWStr, 0);
91     mciSetDriverData(wma->wDevID, (DWORD_PTR)wma);
92
93     return modp->wDeviceID;
94 }
95
96 /**************************************************************************
97  *                              MCIQTZ_drvClose         [internal]
98  */
99 static DWORD MCIQTZ_drvClose(DWORD dwDevID)
100 {
101     WINE_MCIQTZ* wma;
102
103     TRACE("(%04x)\n", dwDevID);
104
105     wma = MCIQTZ_mciGetOpenDev(dwDevID);
106
107     if (wma) {
108         /* finish all outstanding things */
109         MCIQTZ_mciClose(dwDevID, MCI_WAIT, NULL);
110
111         mciFreeCommandResource(wma->command_table);
112         mciSetDriverData(dwDevID, 0);
113         HeapFree(GetProcessHeap(), 0, wma);
114         return 1;
115     }
116
117     return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
118 }
119
120 /**************************************************************************
121  *                              MCIQTZ_drvConfigure             [internal]
122  */
123 static DWORD MCIQTZ_drvConfigure(DWORD dwDevID)
124 {
125     WINE_MCIQTZ* wma;
126
127     TRACE("(%04x)\n", dwDevID);
128
129     wma = MCIQTZ_mciGetOpenDev(dwDevID);
130     if (!wma)
131         return 0;
132
133     MCIQTZ_mciStop(dwDevID, MCI_WAIT, NULL);
134
135     MessageBoxA(0, "Sample QTZ Wine Driver !", "MM-Wine Driver", MB_OK);
136
137     return 1;
138 }
139
140 /***************************************************************************
141  *                              MCIQTZ_mciOpen                  [internal]
142  */
143 static DWORD MCIQTZ_mciOpen(UINT wDevID, DWORD dwFlags,
144                             LPMCI_DGV_OPEN_PARMSW lpOpenParms)
145 {
146     WINE_MCIQTZ* wma;
147     HRESULT hr;
148     DWORD style = 0;
149     RECT rc = { 0, 0, 0, 0 };
150
151     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpOpenParms);
152
153     if (!lpOpenParms)
154         return MCIERR_NULL_PARAMETER_BLOCK;
155
156     wma = MCIQTZ_mciGetOpenDev(wDevID);
157     if (!wma)
158         return MCIERR_INVALID_DEVICE_ID;
159
160     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
161
162     hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
163     wma->uninit = SUCCEEDED(hr);
164
165     hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&wma->pgraph);
166     if (FAILED(hr)) {
167         TRACE("Cannot create filtergraph (hr = %x)\n", hr);
168         goto err;
169     }
170
171     hr = IGraphBuilder_QueryInterface(wma->pgraph, &IID_IMediaControl, (LPVOID*)&wma->pmctrl);
172     if (FAILED(hr)) {
173         TRACE("Cannot get IMediaControl interface (hr = %x)\n", hr);
174         goto err;
175     }
176
177     hr = IGraphBuilder_QueryInterface(wma->pgraph, &IID_IMediaSeeking, (void**)&wma->seek);
178     if (FAILED(hr)) {
179         TRACE("Cannot get IMediaSeeking interface (hr = %x)\n", hr);
180         goto err;
181     }
182
183     hr = IGraphBuilder_QueryInterface(wma->pgraph, &IID_IVideoWindow, (void**)&wma->vidwin);
184     if (FAILED(hr)) {
185         TRACE("Cannot get IVideoWindow interface (hr = %x)\n", hr);
186         goto err;
187     }
188
189     hr = IGraphBuilder_QueryInterface(wma->pgraph, &IID_IBasicVideo, (void**)&wma->vidbasic);
190     if (FAILED(hr)) {
191         TRACE("Cannot get IBasicVideo interface (hr = %x)\n", hr);
192         goto err;
193     }
194
195     if (!(dwFlags & MCI_OPEN_ELEMENT) || (dwFlags & MCI_OPEN_ELEMENT_ID)) {
196         TRACE("Wrong dwFlags %x\n", dwFlags);
197         goto err;
198     }
199
200     if (!lpOpenParms->lpstrElementName || !lpOpenParms->lpstrElementName[0]) {
201         TRACE("Invalid filename specified\n");
202         goto err;
203     }
204
205     TRACE("Open file %s\n", debugstr_w(lpOpenParms->lpstrElementName));
206
207     hr = IGraphBuilder_RenderFile(wma->pgraph, lpOpenParms->lpstrElementName, NULL);
208     if (FAILED(hr)) {
209         TRACE("Cannot render file (hr = %x)\n", hr);
210         goto err;
211     }
212
213     IVideoWindow_put_AutoShow(wma->vidwin, OAFALSE);
214     IVideoWindow_put_Visible(wma->vidwin, OAFALSE);
215     if (dwFlags & MCI_DGV_OPEN_WS)
216         style = lpOpenParms->dwStyle;
217     if (dwFlags & MCI_DGV_OPEN_PARENT) {
218         IVideoWindow_put_MessageDrain(wma->vidwin, (OAHWND)lpOpenParms->hWndParent);
219         IVideoWindow_put_WindowState(wma->vidwin, SW_HIDE);
220         IVideoWindow_put_WindowStyle(wma->vidwin, style|WS_CHILD);
221         IVideoWindow_put_Owner(wma->vidwin, (OAHWND)lpOpenParms->hWndParent);
222         GetClientRect(lpOpenParms->hWndParent, &rc);
223         IVideoWindow_SetWindowPosition(wma->vidwin, rc.left, rc.top, rc.right - rc.top, rc.bottom - rc.top);
224         wma->parent = (HWND)lpOpenParms->hWndParent;
225     }
226     else if (style)
227         IVideoWindow_put_WindowStyle(wma->vidwin, style);
228     IBasicVideo_GetVideoSize(wma->vidbasic, &rc.right, &rc.bottom);
229     wma->opened = TRUE;
230
231     if (dwFlags & MCI_NOTIFY)
232         mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)), wDevID, MCI_NOTIFY_SUCCESSFUL);
233
234     return 0;
235
236 err:
237     if (wma->vidbasic)
238         IUnknown_Release(wma->vidbasic);
239     wma->vidbasic = NULL;
240     if (wma->seek)
241         IUnknown_Release(wma->seek);
242     wma->seek = NULL;
243     if (wma->vidwin)
244         IUnknown_Release(wma->vidwin);
245     wma->vidwin = NULL;
246     if (wma->pgraph)
247         IGraphBuilder_Release(wma->pgraph);
248     wma->pgraph = NULL;
249     if (wma->pmctrl)
250         IMediaControl_Release(wma->pmctrl);
251     wma->pmctrl = NULL;
252
253     if (wma->uninit)
254         CoUninitialize();
255     wma->uninit = 0;
256
257     return MCIERR_INTERNAL;
258 }
259
260 /***************************************************************************
261  *                              MCIQTZ_mciClose                 [internal]
262  */
263 static DWORD MCIQTZ_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
264 {
265     WINE_MCIQTZ* wma;
266
267     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
268
269     wma = MCIQTZ_mciGetOpenDev(wDevID);
270     if (!wma)
271         return MCIERR_INVALID_DEVICE_ID;
272
273     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
274
275     if (wma->opened) {
276         IUnknown_Release(wma->vidwin);
277         IUnknown_Release(wma->vidbasic);
278         IUnknown_Release(wma->seek);
279         IGraphBuilder_Release(wma->pgraph);
280         IMediaControl_Release(wma->pmctrl);
281         if (wma->uninit)
282             CoUninitialize();
283         wma->opened = FALSE;
284     }
285
286     return 0;
287 }
288
289 /***************************************************************************
290  *                              MCIQTZ_mciPlay                  [internal]
291  */
292 static DWORD MCIQTZ_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
293 {
294     WINE_MCIQTZ* wma;
295     HRESULT hr;
296     REFERENCE_TIME time1 = 0, time2 = 0;
297     GUID format;
298     DWORD pos1;
299
300     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
301
302     if (!lpParms)
303         return MCIERR_NULL_PARAMETER_BLOCK;
304
305     wma = MCIQTZ_mciGetOpenDev(wDevID);
306     if (!wma)
307         return MCIERR_INVALID_DEVICE_ID;
308
309     IMediaSeeking_GetTimeFormat(wma->seek, &format);
310     if (dwFlags & MCI_FROM) {
311         if (IsEqualGUID(&format, &TIME_FORMAT_MEDIA_TIME))
312             time1 = lpParms->dwFrom * 10000;
313         else
314             time1 = lpParms->dwFrom;
315         pos1 = AM_SEEKING_AbsolutePositioning;
316     } else
317         pos1 = AM_SEEKING_NoPositioning;
318     if (dwFlags & MCI_TO) {
319         if (IsEqualGUID(&format, &TIME_FORMAT_MEDIA_TIME))
320             time2 = lpParms->dwTo * 10000;
321         else
322             time2 = lpParms->dwTo;
323     } else
324         IMediaSeeking_GetDuration(wma->seek, &time2);
325     IMediaSeeking_SetPositions(wma->seek, &time1, pos1, &time2, AM_SEEKING_AbsolutePositioning);
326
327     hr = IMediaControl_Run(wma->pmctrl);
328     if (FAILED(hr)) {
329         TRACE("Cannot run filtergraph (hr = %x)\n", hr);
330         return MCIERR_INTERNAL;
331     }
332
333     IVideoWindow_put_Visible(wma->vidwin, OATRUE);
334
335     if (dwFlags & MCI_NOTIFY)
336         mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)), wDevID, MCI_NOTIFY_SUCCESSFUL);
337     return 0;
338 }
339
340 /***************************************************************************
341  *                              MCIQTZ_mciSeek                  [internal]
342  */
343 static DWORD MCIQTZ_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
344 {
345     WINE_MCIQTZ* wma;
346     HRESULT hr;
347     LONGLONG newpos;
348
349     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
350
351     if (!lpParms)
352         return MCIERR_NULL_PARAMETER_BLOCK;
353
354     wma = MCIQTZ_mciGetOpenDev(wDevID);
355     if (!wma)
356         return MCIERR_INVALID_DEVICE_ID;
357
358     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
359
360     if (dwFlags & MCI_SEEK_TO_START) {
361         newpos = 0;
362     } else if (dwFlags & MCI_SEEK_TO_END) {
363         FIXME("MCI_SEEK_TO_END not implemented yet\n");
364         return MCIERR_INTERNAL;
365     } else if (dwFlags & MCI_TO) {
366         FIXME("MCI_TO not implemented yet\n");
367         return MCIERR_INTERNAL;
368     } else {
369         WARN("dwFlag doesn't tell where to seek to...\n");
370         return MCIERR_MISSING_PARAMETER;
371     }
372
373     hr = IMediaSeeking_SetPositions(wma->seek, &newpos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
374     if (FAILED(hr)) {
375         FIXME("Cannot set position (hr = %x)\n", hr);
376         return MCIERR_INTERNAL;
377     }
378
379     if (dwFlags & MCI_NOTIFY)
380         mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)), wDevID, MCI_NOTIFY_SUCCESSFUL);
381
382     return 0;
383 }
384
385 /***************************************************************************
386  *                              MCIQTZ_mciStop                  [internal]
387  */
388 static DWORD MCIQTZ_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
389 {
390     WINE_MCIQTZ* wma;
391     HRESULT hr;
392
393     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
394
395     wma = MCIQTZ_mciGetOpenDev(wDevID);
396     if (!wma)
397         return MCIERR_INVALID_DEVICE_ID;
398
399     if (!wma->opened)
400         return 0;
401
402     hr = IMediaControl_Stop(wma->pmctrl);
403     if (FAILED(hr)) {
404         TRACE("Cannot stop filtergraph (hr = %x)\n", hr);
405         return MCIERR_INTERNAL;
406     }
407
408     if (!wma->parent)
409         IVideoWindow_put_Visible(wma->vidwin, OAFALSE);
410
411     return 0;
412 }
413
414 /***************************************************************************
415  *                              MCIQTZ_mciPause                 [internal]
416  */
417 static DWORD MCIQTZ_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
418 {
419     WINE_MCIQTZ* wma;
420     HRESULT hr;
421
422     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
423
424     wma = MCIQTZ_mciGetOpenDev(wDevID);
425     if (!wma)
426         return MCIERR_INVALID_DEVICE_ID;
427
428     hr = IMediaControl_Pause(wma->pmctrl);
429     if (FAILED(hr)) {
430         TRACE("Cannot pause filtergraph (hr = %x)\n", hr);
431         return MCIERR_INTERNAL;
432     }
433
434     return 0;
435 }
436
437 /***************************************************************************
438  *                              MCIQTZ_mciGetDevCaps            [internal]
439  */
440 static DWORD MCIQTZ_mciGetDevCaps(UINT wDevID, DWORD dwFlags, LPMCI_GETDEVCAPS_PARMS lpParms)
441 {
442     WINE_MCIQTZ* wma;
443
444     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
445
446     if (!lpParms)
447         return MCIERR_NULL_PARAMETER_BLOCK;
448
449     wma = MCIQTZ_mciGetOpenDev(wDevID);
450     if (!wma)
451         return MCIERR_INVALID_DEVICE_ID;
452
453     if (!(dwFlags & MCI_GETDEVCAPS_ITEM))
454         return MCIERR_MISSING_PARAMETER;
455
456     switch (lpParms->dwItem) {
457         case MCI_GETDEVCAPS_CAN_RECORD:
458             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
459             TRACE("MCI_GETDEVCAPS_CAN_RECORD = %08x\n", lpParms->dwReturn);
460             break;
461         case MCI_GETDEVCAPS_HAS_AUDIO:
462             lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
463             TRACE("MCI_GETDEVCAPS_HAS_AUDIO = %08x\n", lpParms->dwReturn);
464             break;
465         case MCI_GETDEVCAPS_HAS_VIDEO:
466             lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
467             TRACE("MCI_GETDEVCAPS_HAS_VIDEO = %08x\n", lpParms->dwReturn);
468             break;
469         case MCI_GETDEVCAPS_DEVICE_TYPE:
470             lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_DIGITAL_VIDEO, MCI_DEVTYPE_DIGITAL_VIDEO);
471             TRACE("MCI_GETDEVCAPS_DEVICE_TYPE = %08x\n", lpParms->dwReturn);
472             break;
473         case MCI_GETDEVCAPS_USES_FILES:
474             lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
475             TRACE("MCI_GETDEVCAPS_USES_FILES = %08x\n", lpParms->dwReturn);
476             break;
477         case MCI_GETDEVCAPS_COMPOUND_DEVICE:
478             lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
479             TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE = %08x\n", lpParms->dwReturn);
480             break;
481         case MCI_GETDEVCAPS_CAN_EJECT:
482             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
483             TRACE("MCI_GETDEVCAPS_EJECT = %08x\n", lpParms->dwReturn);
484             break;
485         case MCI_GETDEVCAPS_CAN_PLAY:
486             lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
487             TRACE("MCI_GETDEVCAPS_CAN_PLAY = %08x\n", lpParms->dwReturn);
488             break;
489         case MCI_GETDEVCAPS_CAN_SAVE:
490             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
491             TRACE("MCI_GETDEVCAPS_CAN_SAVE = %08x\n", lpParms->dwReturn);
492             break;
493         case MCI_DGV_GETDEVCAPS_CAN_REVERSE:
494             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
495             TRACE("MCI_DGV_GETDEVCAPS_CAN_REVERSE = %08x\n", lpParms->dwReturn);
496             break;
497         case MCI_DGV_GETDEVCAPS_CAN_STRETCH:
498             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE); /* FIXME */
499             TRACE("MCI_DGV_GETDEVCAPS_CAN_STRETCH = %08x\n", lpParms->dwReturn);
500             break;
501         case MCI_DGV_GETDEVCAPS_CAN_LOCK:
502             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
503             TRACE("MCI_DGV_GETDEVCAPS_CAN_LOCK = %08x\n", lpParms->dwReturn);
504             break;
505         case MCI_DGV_GETDEVCAPS_CAN_FREEZE:
506             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
507             TRACE("MCI_DGV_GETDEVCAPS_CAN_FREEZE = %08x\n", lpParms->dwReturn);
508             break;
509         case MCI_DGV_GETDEVCAPS_CAN_STR_IN:
510             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
511             TRACE("MCI_DGV_GETDEVCAPS_CAN_STRETCH_INPUT = %08x\n", lpParms->dwReturn);
512             break;
513         case MCI_DGV_GETDEVCAPS_HAS_STILL:
514             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
515             TRACE("MCI_DGV_GETDEVCAPS_HAS_STILL = %08x\n", lpParms->dwReturn);
516             break;
517         case MCI_DGV_GETDEVCAPS_CAN_TEST:
518             lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE); /* FIXME */
519             TRACE("MCI_DGV_GETDEVCAPS_CAN_TEST = %08x\n", lpParms->dwReturn);
520             break;
521         case MCI_DGV_GETDEVCAPS_MAX_WINDOWS:
522             lpParms->dwReturn = 1;
523             TRACE("MCI_DGV_GETDEVCAPS_MAX_WINDOWS = %u\n", lpParms->dwReturn);
524             return 0;
525         default:
526             WARN("Unknown capability %08x\n", lpParms->dwItem);
527             /* Fall through */
528         case MCI_DGV_GETDEVCAPS_MAXIMUM_RATE: /* unknown to w2k */
529         case MCI_DGV_GETDEVCAPS_MINIMUM_RATE: /* unknown to w2k */
530             return MCIERR_UNSUPPORTED_FUNCTION;
531     }
532
533     return MCI_RESOURCE_RETURNED;
534 }
535
536 /***************************************************************************
537  *                              MCIQTZ_mciSet                   [internal]
538  */
539 static DWORD MCIQTZ_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SET_PARMS lpParms)
540 {
541     WINE_MCIQTZ* wma;
542
543     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
544
545     if (!lpParms)
546         return MCIERR_NULL_PARAMETER_BLOCK;
547
548     wma = MCIQTZ_mciGetOpenDev(wDevID);
549     if (!wma)
550         return MCIERR_INVALID_DEVICE_ID;
551
552     if (dwFlags & MCI_SET_TIME_FORMAT) {
553         switch (lpParms->dwTimeFormat) {
554             case MCI_FORMAT_MILLISECONDS:
555                 TRACE("MCI_SET_TIME_FORMAT = MCI_FORMAT_MILLISECONDS\n");
556                 wma->time_format = MCI_FORMAT_MILLISECONDS;
557                 break;
558             case MCI_FORMAT_FRAMES:
559                 TRACE("MCI_SET_TIME_FORMAT = MCI_FORMAT_FRAMES\n");
560                 wma->time_format = MCI_FORMAT_FRAMES;
561                 break;
562             default:
563                 WARN("Bad time format %u\n", lpParms->dwTimeFormat);
564                 return MCIERR_BAD_TIME_FORMAT;
565         }
566     }
567
568     if (dwFlags & MCI_SET_DOOR_OPEN)
569         FIXME("MCI_SET_DOOR_OPEN not implemented yet\n");
570     if (dwFlags & MCI_SET_DOOR_CLOSED)
571         FIXME("MCI_SET_DOOR_CLOSED not implemented yet\n");
572     if (dwFlags & MCI_SET_AUDIO)
573         FIXME("MCI_SET_AUDIO not implemented yet\n");
574     if (dwFlags & MCI_SET_VIDEO)
575         FIXME("MCI_SET_VIDEO not implemented yet\n");
576     if (dwFlags & MCI_SET_ON)
577         FIXME("MCI_SET_ON not implemented yet\n");
578     if (dwFlags & MCI_SET_OFF)
579         FIXME("MCI_SET_OFF not implemented yet\n");
580     if (dwFlags & MCI_SET_AUDIO_LEFT)
581         FIXME("MCI_SET_AUDIO_LEFT not implemented yet\n");
582     if (dwFlags & MCI_SET_AUDIO_RIGHT)
583         FIXME("MCI_SET_AUDIO_RIGHT not implemented yet\n");
584
585     if (dwFlags & ~0x7f03 /* All MCI_SET flags mask */)
586         ERR("Unknown flags %08x\n", dwFlags & ~0x7f03);
587
588     return 0;
589 }
590
591 /***************************************************************************
592  *                              MCIQTZ_mciStatus                [internal]
593  */
594 static DWORD MCIQTZ_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_DGV_STATUS_PARMSW lpParms)
595 {
596     WINE_MCIQTZ* wma;
597     HRESULT hr;
598
599     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
600
601     if (!lpParms)
602         return MCIERR_NULL_PARAMETER_BLOCK;
603
604     wma = MCIQTZ_mciGetOpenDev(wDevID);
605     if (!wma)
606         return MCIERR_INVALID_DEVICE_ID;
607
608     if (!(dwFlags & MCI_STATUS_ITEM)) {
609         WARN("No status item specified\n");
610         return MCIERR_UNRECOGNIZED_COMMAND;
611     }
612
613     switch (lpParms->dwItem) {
614         case MCI_STATUS_LENGTH: {
615             LONGLONG duration = -1;
616             GUID format;
617             switch (wma->time_format) {
618                 case MCI_FORMAT_MILLISECONDS: format = TIME_FORMAT_MEDIA_TIME; break;
619                 case MCI_FORMAT_FRAMES: format = TIME_FORMAT_FRAME; break;
620                 default: ERR("Unhandled format %x\n", wma->time_format); break;
621             }
622             hr = IMediaSeeking_SetTimeFormat(wma->seek, &format);
623             if (FAILED(hr)) {
624                 FIXME("Cannot set time format (hr = %x)\n", hr);
625                 lpParms->dwReturn = 0;
626                 break;
627             }
628             hr = IMediaSeeking_GetDuration(wma->seek, &duration);
629             if (FAILED(hr) || duration < 0) {
630                 FIXME("Cannot read duration (hr = %x)\n", hr);
631                 lpParms->dwReturn = 0;
632             } else if (wma->time_format != MCI_FORMAT_MILLISECONDS)
633                 lpParms->dwReturn = duration;
634             else
635                 lpParms->dwReturn = duration / 10000;
636             break;
637         }
638         case MCI_STATUS_POSITION: {
639             REFERENCE_TIME curpos;
640
641             hr = IMediaSeeking_GetCurrentPosition(wma->seek, &curpos);
642             if (FAILED(hr)) {
643                 FIXME("Cannot get position (hr = %x)\n", hr);
644                 return MCIERR_INTERNAL;
645             }
646             lpParms->dwReturn = curpos / 10000;
647             break;
648         }
649         case MCI_STATUS_NUMBER_OF_TRACKS:
650             FIXME("MCI_STATUS_NUMBER_OF_TRACKS not implemented yet\n");
651             return MCIERR_UNRECOGNIZED_COMMAND;
652         case MCI_STATUS_MODE: {
653             LONG state = State_Stopped;
654             IMediaControl_GetState(wma->pmctrl, -1, &state);
655             if (state == State_Stopped)
656                 state = MCI_MODE_STOP;
657             else if (state == State_Running)
658                 state = MCI_MODE_PLAY;
659             else if (state == State_Paused)
660                 state = MCI_MODE_PAUSE;
661             break;
662         }
663         case MCI_STATUS_MEDIA_PRESENT:
664             FIXME("MCI_STATUS_MEDIA_PRESENT not implemented yet\n");
665             return MCIERR_UNRECOGNIZED_COMMAND;
666         case MCI_STATUS_TIME_FORMAT:
667             lpParms->dwReturn = wma->time_format;
668             break;
669         case MCI_STATUS_READY:
670             FIXME("MCI_STATUS_READY not implemented yet\n");
671             return MCIERR_UNRECOGNIZED_COMMAND;
672         case MCI_STATUS_CURRENT_TRACK:
673             FIXME("MCI_STATUS_CURRENT_TRACK not implemented yet\n");
674             return MCIERR_UNRECOGNIZED_COMMAND;
675         default:
676             FIXME("Unknown command %08X\n", lpParms->dwItem);
677             return MCIERR_UNRECOGNIZED_COMMAND;
678     }
679
680     if (dwFlags & MCI_NOTIFY)
681         mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)), wDevID, MCI_NOTIFY_SUCCESSFUL);
682
683     return 0;
684 }
685
686 /***************************************************************************
687  *                              MCIQTZ_mciWhere                 [internal]
688  */
689 static DWORD MCIQTZ_mciWhere(UINT wDevID, DWORD dwFlags, LPMCI_DGV_RECT_PARMS lpParms)
690 {
691     WINE_MCIQTZ* wma;
692     HRESULT hr;
693     HWND hWnd;
694     RECT rc;
695     DWORD ret = MCIERR_UNRECOGNIZED_COMMAND;
696
697     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
698
699     if (!lpParms)
700         return MCIERR_NULL_PARAMETER_BLOCK;
701
702     wma = MCIQTZ_mciGetOpenDev(wDevID);
703     if (!wma)
704         return MCIERR_INVALID_DEVICE_ID;
705
706     hr = IVideoWindow_get_Owner(wma->vidwin, (OAHWND*)&hWnd);
707     if (FAILED(hr)) {
708         TRACE("No video stream, returning no window error\n");
709         return MCIERR_NO_WINDOW;
710     }
711
712     if (dwFlags & MCI_DGV_WHERE_SOURCE) {
713         if (dwFlags & MCI_DGV_WHERE_MAX)
714             FIXME("MCI_DGV_WHERE_SOURCE_MAX stub %s\n", wine_dbgstr_rect(&rc));
715         IBasicVideo_GetSourcePosition(wma->vidbasic, &rc.left, &rc.top, &rc.right, &rc.bottom);
716         TRACE("MCI_DGV_WHERE_SOURCE %s\n", wine_dbgstr_rect(&rc));
717     }
718     if (dwFlags & MCI_DGV_WHERE_DESTINATION) {
719         if (dwFlags & MCI_DGV_WHERE_MAX)
720             FIXME("MCI_DGV_WHERE_DESTINATION_MAX stub %s\n", wine_dbgstr_rect(&rc));
721         IBasicVideo_GetDestinationPosition(wma->vidbasic, &rc.left, &rc.top, &rc.right, &rc.bottom);
722         TRACE("MCI_DGV_WHERE_DESTINATION %s\n", wine_dbgstr_rect(&rc));
723     }
724     if (dwFlags & MCI_DGV_WHERE_FRAME) {
725         if (dwFlags & MCI_DGV_WHERE_MAX)
726             FIXME("MCI_DGV_WHERE_FRAME_MAX not supported yet\n");
727         else
728             FIXME("MCI_DGV_WHERE_FRAME not supported yet\n");
729         goto out;
730     }
731     if (dwFlags & MCI_DGV_WHERE_VIDEO) {
732         if (dwFlags & MCI_DGV_WHERE_MAX)
733             FIXME("MCI_DGV_WHERE_VIDEO_MAX not supported yet\n");
734         else
735             FIXME("MCI_DGV_WHERE_VIDEO not supported yet\n");
736         goto out;
737     }
738     if (dwFlags & MCI_DGV_WHERE_WINDOW) {
739         if (dwFlags & MCI_DGV_WHERE_MAX) {
740             GetWindowRect(GetDesktopWindow(), &rc);
741             rc.right -= rc.left;
742             rc.bottom -= rc.top;
743             TRACE("MCI_DGV_WHERE_WINDOW_MAX %s\n", wine_dbgstr_rect(&rc));
744         } else {
745             IVideoWindow_GetWindowPosition(wma->vidwin, &rc.left, &rc.top, &rc.right, &rc.bottom);
746             TRACE("MCI_DGV_WHERE_WINDOW %s\n", wine_dbgstr_rect(&rc));
747         }
748     }
749     ret = 0;
750 out:
751     lpParms->rc = rc;
752     return ret;
753 }
754
755 /***************************************************************************
756  *                              MCIQTZ_mciWindow                [internal]
757  */
758 static DWORD MCIQTZ_mciWindow(UINT wDevID, DWORD dwFlags, LPMCI_DGV_WINDOW_PARMSW lpParms)
759 {
760     WINE_MCIQTZ *wma = MCIQTZ_mciGetOpenDev(wDevID);
761
762     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
763
764     if (!lpParms)
765         return MCIERR_NULL_PARAMETER_BLOCK;
766     if (!wma)
767         return MCIERR_INVALID_DEVICE_ID;
768     if (dwFlags & MCI_TEST)
769         return 0;
770
771     if (dwFlags & MCI_DGV_WINDOW_HWND && (IsWindow(lpParms->hWnd) || !lpParms->hWnd)) {
772         LONG visible = OATRUE;
773         LONG style = 0;
774         TRACE("Setting hWnd to %p\n", lpParms->hWnd);
775         IVideoWindow_get_Visible(wma->vidwin, &visible);
776         IVideoWindow_put_Visible(wma->vidwin, OAFALSE);
777         IVideoWindow_get_WindowStyle(wma->vidwin, &style);
778         style &= ~WS_CHILD;
779         if (lpParms->hWnd)
780             IVideoWindow_put_WindowStyle(wma->vidwin, style|WS_CHILD);
781         else
782             IVideoWindow_put_WindowStyle(wma->vidwin, style);
783         IVideoWindow_put_Owner(wma->vidwin, (OAHWND)lpParms->hWnd);
784         IVideoWindow_put_MessageDrain(wma->vidwin, (OAHWND)lpParms->hWnd);
785         IVideoWindow_put_Visible(wma->vidwin, visible);
786         wma->parent = lpParms->hWnd;
787     }
788     if (dwFlags & MCI_DGV_WINDOW_STATE) {
789         TRACE("Setting nCmdShow to %d\n", lpParms->nCmdShow);
790         IVideoWindow_put_WindowState(wma->vidwin, lpParms->nCmdShow);
791     }
792     if (dwFlags & MCI_DGV_WINDOW_TEXT) {
793         TRACE("Setting caption to %s\n", debugstr_w(lpParms->lpstrText));
794         IVideoWindow_put_Caption(wma->vidwin, lpParms->lpstrText);
795     }
796     return 0;
797 }
798
799 /******************************************************************************
800  *              MCIAVI_mciUpdate            [internal]
801  */
802 static DWORD MCIQTZ_mciUpdate(UINT wDevID, DWORD dwFlags, LPMCI_DGV_UPDATE_PARMS lpParms)
803 {
804     WINE_MCIQTZ *wma;
805     DWORD res = 0;
806
807     TRACE("%04x, %08x, %p\n", wDevID, dwFlags, lpParms);
808
809     if (!lpParms)
810         return MCIERR_NULL_PARAMETER_BLOCK;
811
812     wma = MCIQTZ_mciGetOpenDev(wDevID);
813     if (!wma)
814         return MCIERR_INVALID_DEVICE_ID;
815
816     if (dwFlags & MCI_DGV_UPDATE_HDC) {
817         LONG state, size;
818         BYTE *data;
819         BITMAPINFO *info;
820         HRESULT hr;
821         RECT src, dest;
822         LONG visible = OATRUE;
823
824         res = MCIERR_INTERNAL;
825         IMediaControl_GetState(wma->pmctrl, -1, &state);
826         if (state == State_Running)
827             return MCIERR_UNSUPPORTED_FUNCTION;
828         /* If in stopped state, nothing has been drawn to screen
829          * moving to pause, which is needed for the old dib renderer, will result
830          * in a single frame drawn, so hide the window here */
831         IVideoWindow_get_Visible(wma->vidwin, &visible);
832         if (wma->parent)
833             IVideoWindow_put_Visible(wma->vidwin, OAFALSE);
834         /* FIXME: Should we check the original state and restore it? */
835         IMediaControl_Pause(wma->pmctrl);
836         IMediaControl_GetState(wma->pmctrl, -1, &state);
837         if (FAILED(hr = IBasicVideo_GetCurrentImage(wma->vidbasic, &size, NULL))) {
838             WARN("Could not get image size (hr = %x)\n", hr);
839             goto out;
840         }
841         data = HeapAlloc(GetProcessHeap(), 0, size);
842         info = (BITMAPINFO*)data;
843         IBasicVideo_GetCurrentImage(wma->vidbasic, &size, (LONG*)data);
844         data += info->bmiHeader.biSize;
845
846         IBasicVideo_GetSourcePosition(wma->vidbasic, &src.left, &src.top, &src.right, &src.bottom);
847         IBasicVideo_GetDestinationPosition(wma->vidbasic, &dest.left, &dest.top, &dest.right, &dest.bottom);
848         StretchDIBits(lpParms->hDC,
849               dest.left, dest.top, dest.right + dest.left, dest.bottom + dest.top,
850               src.left, src.top, src.right + src.left, src.bottom + src.top,
851               data, info, DIB_RGB_COLORS, SRCCOPY);
852         HeapFree(GetProcessHeap(), 0, data);
853         res = 0;
854 out:
855         if (wma->parent)
856             IVideoWindow_put_Visible(wma->vidwin, visible);
857     }
858     else if (dwFlags)
859         FIXME("Unhandled flags %x\n", dwFlags);
860     return res;
861 }
862
863 /***************************************************************************
864  *                              MCIQTZ_mciSetAudio              [internal]
865  */
866 static DWORD MCIQTZ_mciSetAudio(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETAUDIO_PARMSW lpParms)
867 {
868     WINE_MCIQTZ *wma;
869
870     FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
871
872     if (!lpParms)
873         return MCIERR_NULL_PARAMETER_BLOCK;
874
875     wma = MCIQTZ_mciGetOpenDev(wDevID);
876     if (!wma)
877         return MCIERR_INVALID_DEVICE_ID;
878
879     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
880
881     return 0;
882 }
883
884 /*======================================================================*
885  *                          MCI QTZ entry points                        *
886  *======================================================================*/
887
888 /**************************************************************************
889  *                              DriverProc (MCIQTZ.@)
890  */
891 LRESULT CALLBACK MCIQTZ_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
892                                    LPARAM dwParam1, LPARAM dwParam2)
893 {
894     TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
895           dwDevID, hDriv, wMsg, dwParam1, dwParam2);
896
897     switch (wMsg) {
898         case DRV_LOAD:                  return 1;
899         case DRV_FREE:                  return 1;
900         case DRV_OPEN:                  return MCIQTZ_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
901         case DRV_CLOSE:                 return MCIQTZ_drvClose(dwDevID);
902         case DRV_ENABLE:                return 1;
903         case DRV_DISABLE:               return 1;
904         case DRV_QUERYCONFIGURE:        return 1;
905         case DRV_CONFIGURE:             return MCIQTZ_drvConfigure(dwDevID);
906         case DRV_INSTALL:               return DRVCNF_RESTART;
907         case DRV_REMOVE:                return DRVCNF_RESTART;
908     }
909
910     /* session instance */
911     if (dwDevID == 0xFFFFFFFF)
912         return 1;
913
914     switch (wMsg) {
915         case MCI_OPEN_DRIVER:   return MCIQTZ_mciOpen      (dwDevID, dwParam1, (LPMCI_DGV_OPEN_PARMSW)     dwParam2);
916         case MCI_CLOSE_DRIVER:  return MCIQTZ_mciClose     (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)       dwParam2);
917         case MCI_PLAY:          return MCIQTZ_mciPlay      (dwDevID, dwParam1, (LPMCI_PLAY_PARMS)          dwParam2);
918         case MCI_SEEK:          return MCIQTZ_mciSeek      (dwDevID, dwParam1, (LPMCI_SEEK_PARMS)          dwParam2);
919         case MCI_STOP:          return MCIQTZ_mciStop      (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)       dwParam2);
920         case MCI_PAUSE:         return MCIQTZ_mciPause     (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)       dwParam2);
921         case MCI_GETDEVCAPS:    return MCIQTZ_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)    dwParam2);
922         case MCI_SET:           return MCIQTZ_mciSet       (dwDevID, dwParam1, (LPMCI_DGV_SET_PARMS)       dwParam2);
923         case MCI_STATUS:        return MCIQTZ_mciStatus    (dwDevID, dwParam1, (LPMCI_DGV_STATUS_PARMSW)   dwParam2);
924         case MCI_WHERE:         return MCIQTZ_mciWhere     (dwDevID, dwParam1, (LPMCI_DGV_RECT_PARMS)      dwParam2);
925         /* Digital Video specific */
926         case MCI_SETAUDIO:      return MCIQTZ_mciSetAudio  (dwDevID, dwParam1, (LPMCI_DGV_SETAUDIO_PARMSW) dwParam2);
927         case MCI_UPDATE:
928             return MCIQTZ_mciUpdate(dwDevID, dwParam1, (LPMCI_DGV_UPDATE_PARMS)dwParam2);
929         case MCI_WINDOW:
930             return MCIQTZ_mciWindow(dwDevID, dwParam1, (LPMCI_DGV_WINDOW_PARMSW)dwParam2);
931         case MCI_PUT:
932         case MCI_RECORD:
933         case MCI_RESUME:
934         case MCI_INFO:
935         case MCI_LOAD:
936         case MCI_SAVE:
937         case MCI_FREEZE:
938         case MCI_REALIZE:
939         case MCI_UNFREEZE:
940         case MCI_STEP:
941         case MCI_COPY:
942         case MCI_CUT:
943         case MCI_DELETE:
944         case MCI_PASTE:
945         case MCI_CUE:
946         /* Digital Video specific */
947         case MCI_CAPTURE:
948         case MCI_MONITOR:
949         case MCI_RESERVE:
950         case MCI_SIGNAL:
951         case MCI_SETVIDEO:
952         case MCI_QUALITY:
953         case MCI_LIST:
954         case MCI_UNDO:
955         case MCI_CONFIGURE:
956         case MCI_RESTORE:
957             FIXME("Unimplemented command [%08X]\n", wMsg);
958             break;
959         case MCI_SPIN:
960         case MCI_ESCAPE:
961             WARN("Unsupported command [%08X]\n", wMsg);
962             break;
963         case MCI_OPEN:
964         case MCI_CLOSE:
965             FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
966             break;
967         default:
968             TRACE("Sending msg [%08X] to default driver proc\n", wMsg);
969             return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
970     }
971
972     return MCIERR_UNRECOGNIZED_COMMAND;
973 }