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