dmusic: Implement IDirectMusicBufferImpl_GetMaxBytes.
[wine] / dlls / dmusic / buffer.c
1 /* IDirectMusicBuffer Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program 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 program 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 program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "dmusic_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
23
24 static inline IDirectMusicBufferImpl *impl_from_IDirectMusicBuffer(IDirectMusicBuffer *iface)
25 {
26     return CONTAINING_RECORD(iface, IDirectMusicBufferImpl, IDirectMusicBuffer_iface);
27 }
28
29 /* IDirectMusicBufferImpl IUnknown part: */
30 static HRESULT WINAPI IDirectMusicBufferImpl_QueryInterface(LPDIRECTMUSICBUFFER iface, REFIID riid, LPVOID *ppobj)
31 {
32         IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
33         TRACE("(%p, (%s, %p)\n",This,debugstr_dmguid(riid),ppobj);
34         if (IsEqualIID (riid, &IID_IUnknown) 
35                 || IsEqualIID (riid, &IID_IDirectMusicBuffer)) {
36                 IUnknown_AddRef(iface);
37                 *ppobj = This;
38                 return S_OK;
39         }
40         WARN("(%p, (%s, %p): not found\n",This,debugstr_dmguid(riid),ppobj);
41         return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirectMusicBufferImpl_AddRef (LPDIRECTMUSICBUFFER iface)
45 {
46         IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
47         ULONG refCount = InterlockedIncrement(&This->ref);
48
49         TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
50
51         DMUSIC_LockModule();
52
53         return refCount;
54 }
55
56 static ULONG WINAPI IDirectMusicBufferImpl_Release(LPDIRECTMUSICBUFFER iface)
57 {
58         IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
59         ULONG refCount = InterlockedDecrement(&This->ref);
60
61         TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
62
63         if (!refCount) {
64                 HeapFree(GetProcessHeap(), 0, This->data);
65                 HeapFree(GetProcessHeap(), 0, This);
66         }
67
68         DMUSIC_UnlockModule();
69         
70         return refCount;
71 }
72
73 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
74 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
75 {
76     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
77
78     FIXME("(%p): stub\n", This);
79
80     return S_OK;
81 }
82
83 static HRESULT WINAPI IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prtTime)
84 {
85     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
86
87     FIXME("(%p, %p): stub\n", This, prtTime);
88
89     return S_OK;
90 }
91
92 static HRESULT WINAPI IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt, DWORD dwChannelGroup, DWORD dwChannelMessage)
93 {
94     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
95
96     FIXME("(%p, 0x%s, %d, %d): stub\n", This, wine_dbgstr_longlong(rt), dwChannelGroup, dwChannelMessage);
97
98     return S_OK;
99 }
100
101 static HRESULT WINAPI IDirectMusicBufferImpl_PackUnstructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt, DWORD dwChannelGroup, DWORD cb, LPBYTE lpb)
102 {
103     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
104
105     FIXME("(%p, 0x%s, %d, %d, %p): stub\n", This, wine_dbgstr_longlong(rt), dwChannelGroup, cb, lpb);
106
107     return S_OK;
108 }
109
110 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
111 {
112     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
113
114     FIXME("(%p): stub\n", This);
115
116     return S_OK;
117 }
118
119 static HRESULT WINAPI IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt, LPDWORD pdwChannelGroup, LPDWORD pdwLength, LPBYTE* ppData)
120 {
121     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
122
123     FIXME("(%p, %p, %p, %p, %p): stub\n", This, prt, pdwChannelGroup, pdwLength, ppData);
124
125     return S_OK;
126 }
127
128 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* ppData)
129 {
130     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
131
132     FIXME("(%p, %p): stub\n", This, ppData);
133
134     return S_OK;
135 }
136
137 static HRESULT WINAPI IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt)
138 {
139     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
140
141     FIXME("(%p, %p): stub\n", This, prt);
142
143     return S_OK;
144 }
145
146 static HRESULT WINAPI IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface, LPDWORD pcb)
147 {
148     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
149
150     FIXME("(%p, %p): stub\n", This, pcb);
151
152     return S_OK;
153 }
154
155 static HRESULT WINAPI IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface, LPDWORD max_bytes)
156 {
157     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
158
159     TRACE("(%p)->(%p)\n", iface, max_bytes);
160
161     if (!max_bytes)
162         return E_POINTER;
163
164     *max_bytes = This->size;
165
166     return S_OK;
167 }
168
169 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
170 {
171     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
172
173     TRACE("(%p)->(%p)\n", iface, format);
174
175     if (!format)
176         return E_POINTER;
177
178     *format = This->format;
179     return S_OK;
180 }
181
182 static HRESULT WINAPI IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt)
183 {
184     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
185
186     FIXME("(%p, 0x%s): stub\n", This, wine_dbgstr_longlong(rt));
187
188     return S_OK;
189 }
190
191 static HRESULT WINAPI IDirectMusicBufferImpl_SetUsedBytes (LPDIRECTMUSICBUFFER iface, DWORD cb)
192 {
193     IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
194
195     FIXME("(%p, %d): stub\n", This, cb);
196
197     return S_OK;
198 }
199
200 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl = {
201         IDirectMusicBufferImpl_QueryInterface,
202         IDirectMusicBufferImpl_AddRef,
203         IDirectMusicBufferImpl_Release,
204         IDirectMusicBufferImpl_Flush,
205         IDirectMusicBufferImpl_TotalTime,
206         IDirectMusicBufferImpl_PackStructured,
207         IDirectMusicBufferImpl_PackUnstructured,
208         IDirectMusicBufferImpl_ResetReadPtr,
209         IDirectMusicBufferImpl_GetNextEvent,
210         IDirectMusicBufferImpl_GetRawBufferPtr,
211         IDirectMusicBufferImpl_GetStartTime,
212         IDirectMusicBufferImpl_GetUsedBytes,
213         IDirectMusicBufferImpl_GetMaxBytes,
214         IDirectMusicBufferImpl_GetBufferFormat,
215         IDirectMusicBufferImpl_SetStartTime,
216         IDirectMusicBufferImpl_SetUsedBytes
217 };
218
219 HRESULT DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc, LPVOID* ret_iface)
220 {
221     IDirectMusicBufferImpl* dmbuffer;
222     HRESULT hr;
223
224     TRACE("(%p, %p)\n", desc, ret_iface);
225
226     *ret_iface = NULL;
227
228     dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
229     if (!dmbuffer)
230         return E_OUTOFMEMORY;
231
232     dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
233     dmbuffer->ref = 0; /* Will be inited by QueryInterface */
234
235     memcpy(&dmbuffer->format, &desc->guidBufferFormat, sizeof(GUID));
236     dmbuffer->size = (desc->cbBuffer + 3) & ~3; /* Buffer size must be multiple of 4 bytes */
237
238     dmbuffer->data = HeapAlloc(GetProcessHeap(), 0, dmbuffer->size);
239     if (!dmbuffer->data) {
240         HeapFree(GetProcessHeap(), 0, dmbuffer);
241         return E_OUTOFMEMORY;
242     }
243
244     hr = IDirectMusicBufferImpl_QueryInterface((LPDIRECTMUSICBUFFER)dmbuffer, &IID_IDirectMusicBuffer, ret_iface);
245     if (FAILED(hr))
246     {
247         HeapFree(GetProcessHeap(), 0, dmbuffer->data);
248         HeapFree(GetProcessHeap(), 0, dmbuffer);
249     }
250
251     return hr;
252 }