mciqtz32: Implement MCI_SEEK.
[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_drvOpen                  [internal]
58  */
59 static DWORD MCIQTZ_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
60 {
61     WINE_MCIQTZ* wma;
62
63     TRACE("%s, %p\n", debugstr_w(str), modp);
64
65     /* session instance */
66     if (!modp)
67         return 0xFFFFFFFF;
68
69     wma = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIQTZ));
70     if (!wma)
71         return 0;
72
73     wma->wDevID = modp->wDeviceID;
74     mciSetDriverData(wma->wDevID, (DWORD_PTR)wma);
75
76     return modp->wDeviceID;
77 }
78
79 /**************************************************************************
80  *                              MCIQTZ_drvClose         [internal]
81  */
82 static DWORD MCIQTZ_drvClose(DWORD dwDevID)
83 {
84     WINE_MCIQTZ* wma;
85
86     TRACE("%04x\n", dwDevID);
87
88     /* finish all outstanding things */
89     MCIQTZ_mciClose(dwDevID, MCI_WAIT, NULL);
90
91     wma = (WINE_MCIQTZ*)mciGetDriverData(dwDevID);
92
93     if (wma) {
94         HeapFree(GetProcessHeap(), 0, wma);
95         return 1;
96     }
97
98     return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
99 }
100
101 /**************************************************************************
102  *                              MCIQTZ_drvConfigure             [internal]
103  */
104 static DWORD MCIQTZ_drvConfigure(DWORD dwDevID)
105 {
106     WINE_MCIQTZ* wma;
107
108     TRACE("%04x\n", dwDevID);
109
110     MCIQTZ_mciStop(dwDevID, MCI_WAIT, NULL);
111
112     wma = (WINE_MCIQTZ*)mciGetDriverData(dwDevID);
113
114     if (wma) {
115         MessageBoxA(0, "Sample QTZ Wine Driver !", "MM-Wine Driver", MB_OK);
116         return 1;
117     }
118
119     return 0;
120 }
121
122 /**************************************************************************
123  *                              MCIQTZ_mciGetOpenDev            [internal]
124  */
125 static WINE_MCIQTZ* MCIQTZ_mciGetOpenDev(UINT wDevID)
126 {
127     WINE_MCIQTZ* wma = (WINE_MCIQTZ*)mciGetDriverData(wDevID);
128
129     if (!wma) {
130         WARN("Invalid wDevID=%u\n", wDevID);
131         return 0;
132     }
133     return wma;
134 }
135
136 /***************************************************************************
137  *                              MCIQTZ_mciOpen                  [internal]
138  */
139 static DWORD MCIQTZ_mciOpen(UINT wDevID, DWORD dwFlags,
140                             LPMCI_DGV_OPEN_PARMSW lpOpenParms)
141 {
142     WINE_MCIQTZ* wma;
143     HRESULT hr;
144
145     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpOpenParms);
146
147     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
148
149     if (!lpOpenParms)
150         return MCIERR_NULL_PARAMETER_BLOCK;
151
152     wma = (WINE_MCIQTZ*)mciGetDriverData(wDevID);
153     if (!wma)
154         return MCIERR_INVALID_DEVICE_ID;
155
156     CoInitializeEx(NULL, COINIT_MULTITHREADED);
157
158     hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&wma->pgraph);
159     if (FAILED(hr)) {
160         TRACE("Cannot create filtergraph (hr = %x)\n", hr);
161         goto err;
162     }
163
164     hr = IGraphBuilder_QueryInterface(wma->pgraph, &IID_IMediaControl, (LPVOID*)&wma->pmctrl);
165     if (FAILED(hr)) {
166         TRACE("Cannot get IMediaControl interface (hr = %x)\n", hr);
167         goto err;
168     }
169
170     if (!((dwFlags & MCI_OPEN_ELEMENT) && (dwFlags & MCI_OPEN_ELEMENT))) {
171         TRACE("Wrong dwFlags %x\n", dwFlags);
172         goto err;
173     }
174
175     if (!lpOpenParms->lpstrElementName && !lstrlenW(lpOpenParms->lpstrElementName)) {
176         TRACE("Invalid filename specified\n");
177         goto err;
178     }
179
180     TRACE("Open file %s\n", debugstr_w(lpOpenParms->lpstrElementName));
181
182     hr = IGraphBuilder_RenderFile(wma->pgraph, lpOpenParms->lpstrElementName, NULL);
183     if (FAILED(hr)) {
184         TRACE("Cannot render file (hr = %x)\n", hr);
185         goto err;
186     }
187
188     return 0;
189
190 err:
191     if (wma->pgraph)
192         IGraphBuilder_Release(wma->pgraph);
193     wma->pgraph = NULL;
194     if (wma->pmctrl)
195         IMediaControl_Release(wma->pmctrl);
196     wma->pmctrl = NULL;
197
198     CoUninitialize();
199
200     return MCIERR_INTERNAL;
201 }
202
203 /***************************************************************************
204  *                              MCIQTZ_mciClose                 [internal]
205  */
206 static DWORD MCIQTZ_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
207 {
208     WINE_MCIQTZ* wma;
209
210     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
211
212     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
213
214     wma = MCIQTZ_mciGetOpenDev(wDevID);
215     if (!wma)
216         return MCIERR_INVALID_DEVICE_ID;
217
218     if (wma->pgraph)
219         IGraphBuilder_Release(wma->pgraph);
220     wma->pgraph = NULL;
221     if (wma->pmctrl)
222         IMediaControl_Release(wma->pmctrl);
223     wma->pmctrl = NULL;
224
225     CoUninitialize();
226
227     return 0;
228 }
229
230 /***************************************************************************
231  *                              MCIQTZ_mciPlay                  [internal]
232  */
233 static DWORD MCIQTZ_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
234 {
235     WINE_MCIQTZ* wma;
236     HRESULT hr;
237
238     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
239
240     if (!lpParms)
241         return MCIERR_NULL_PARAMETER_BLOCK;
242
243     wma = MCIQTZ_mciGetOpenDev(wDevID);
244
245     hr = IMediaControl_Run(wma->pmctrl);
246     if (FAILED(hr)) {
247         TRACE("Cannot run filtergraph (hr = %x)\n", hr);
248         return MCIERR_INTERNAL;
249     }
250
251     wma->started = TRUE;
252
253     return 0;
254 }
255
256 /***************************************************************************
257  *                              MCIQTZ_mciSeek                  [internal]
258  */
259 static DWORD MCIQTZ_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
260 {
261     WINE_MCIQTZ* wma;
262     HRESULT hr;
263     IMediaPosition* pmpos;
264     LONGLONG newpos;
265
266     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
267
268     MCIQTZ_mciStop(wDevID, MCI_WAIT, NULL);
269
270     if (!lpParms)
271         return MCIERR_NULL_PARAMETER_BLOCK;
272
273     wma = MCIQTZ_mciGetOpenDev(wDevID);
274     if (!wma)
275         return MCIERR_INVALID_DEVICE_ID;
276
277     if (dwFlags & MCI_SEEK_TO_START) {
278         newpos = 0;
279     } else if (dwFlags & MCI_SEEK_TO_END) {
280         FIXME("MCI_SEEK_TO_END not implemented yet\n");
281         return MCIERR_INTERNAL;
282     } else if (dwFlags & MCI_TO) {
283         FIXME("MCI_TO not implemented yet\n");
284         return MCIERR_INTERNAL;
285     } else {
286         WARN("dwFlag doesn't tell where to seek to...\n");
287         return MCIERR_MISSING_PARAMETER;
288     }
289
290     hr = IGraphBuilder_QueryInterface(wma->pgraph, &IID_IMediaPosition, (LPVOID*)&pmpos);
291     if (FAILED(hr)) {
292         FIXME("Cannot get IMediaPostion interface (hr = %x)\n", hr);
293         return MCIERR_INTERNAL;
294     }
295
296     hr = IMediaPosition_put_CurrentPosition(pmpos, newpos);
297     if (FAILED(hr)) {
298         FIXME("Cannot set position (hr = %x)\n", hr);
299         IMediaPosition_Release(pmpos);
300         return MCIERR_INTERNAL;
301     }
302
303     IMediaPosition_Release(pmpos);
304
305     if (dwFlags & MCI_NOTIFY)
306         mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)), wDevID, MCI_NOTIFY_SUCCESSFUL);
307
308     return 0;
309 }
310
311 /***************************************************************************
312  *                              MCIQTZ_mciStop                  [internal]
313  */
314 static DWORD MCIQTZ_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
315 {
316     WINE_MCIQTZ* wma;
317     HRESULT hr;
318
319     TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
320
321     wma = MCIQTZ_mciGetOpenDev(wDevID);
322     if (!wma)
323         return MCIERR_INVALID_DEVICE_ID;
324
325     if (!wma->started)
326         return 0;
327
328     hr = IMediaControl_Stop(wma->pmctrl);
329     if (FAILED(hr)) {
330         TRACE("Cannot stop filtergraph (hr = %x)\n", hr);
331         return MCIERR_INTERNAL;
332     }
333
334     wma->started = FALSE;
335
336     return 0;
337 }
338
339 /*======================================================================*
340  *                          MCI QTZ entry points                        *
341  *======================================================================*/
342
343 /**************************************************************************
344  *                              DriverProc (MCIQTZ.@)
345  */
346 LRESULT CALLBACK MCIQTZ_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
347                                    LPARAM dwParam1, LPARAM dwParam2)
348 {
349     TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
350           dwDevID, hDriv, wMsg, dwParam1, dwParam2);
351
352     switch (wMsg) {
353         case DRV_LOAD:                  return 1;
354         case DRV_FREE:                  return 1;
355         case DRV_OPEN:                  return MCIQTZ_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
356         case DRV_CLOSE:                 return MCIQTZ_drvClose(dwDevID);
357         case DRV_ENABLE:                return 1;
358         case DRV_DISABLE:               return 1;
359         case DRV_QUERYCONFIGURE:        return 1;
360         case DRV_CONFIGURE:             return MCIQTZ_drvConfigure(dwDevID);
361         case DRV_INSTALL:               return DRVCNF_RESTART;
362         case DRV_REMOVE:                return DRVCNF_RESTART;
363     }
364
365     /* session instance */
366     if (dwDevID == 0xFFFFFFFF)
367         return 1;
368
369     switch (wMsg) {
370         case MCI_OPEN_DRIVER:   return MCIQTZ_mciOpen      (dwDevID, dwParam1, (LPMCI_DGV_OPEN_PARMSW)     dwParam2);
371         case MCI_CLOSE_DRIVER:  return MCIQTZ_mciClose     (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)       dwParam2);
372         case MCI_PLAY:          return MCIQTZ_mciPlay      (dwDevID, dwParam1, (LPMCI_PLAY_PARMS)          dwParam2);
373         case MCI_SEEK:          return MCIQTZ_mciSeek      (dwDevID, dwParam1, (LPMCI_SEEK_PARMS)          dwParam2);
374         case MCI_RECORD:
375         case MCI_STOP:
376         case MCI_SET:
377         case MCI_PAUSE:
378         case MCI_RESUME:
379         case MCI_STATUS:
380         case MCI_GETDEVCAPS:
381         case MCI_INFO:
382         case MCI_PUT:
383         case MCI_WINDOW:
384         case MCI_LOAD:
385         case MCI_SAVE:
386         case MCI_FREEZE:
387         case MCI_REALIZE:
388         case MCI_UNFREEZE:
389         case MCI_UPDATE:
390         case MCI_WHERE:
391         case MCI_STEP:
392         case MCI_COPY:
393         case MCI_CUT:
394         case MCI_DELETE:
395         case MCI_PASTE:
396         case MCI_CUE:
397         /* Digital Video specific */
398         case MCI_CAPTURE:
399         case MCI_MONITOR:
400         case MCI_RESERVE:
401         case MCI_SETAUDIO:
402         case MCI_SIGNAL:
403         case MCI_SETVIDEO:
404         case MCI_QUALITY:
405         case MCI_LIST:
406         case MCI_UNDO:
407         case MCI_CONFIGURE:
408         case MCI_RESTORE:
409             FIXME("Unimplemented command [%u]\n", wMsg);
410             break;
411         case MCI_SPIN:
412         case MCI_ESCAPE:
413             WARN("Unsupported command [%u]\n", wMsg);
414             break;
415         case MCI_OPEN:
416         case MCI_CLOSE:
417             FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
418             break;
419         default:
420             TRACE("Sending msg [%u] to default driver proc\n", wMsg);
421             return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
422     }
423
424     return MCIERR_UNRECOGNIZED_COMMAND;
425 }