Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / dlls / winmm / mmio.c
blobd69dec2292d32a458d1d3fe96155262a035bd83d
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * MMIO functions
5 * Copyright 1998 Andrew Taylor
6 * Copyright 1998 Ove Kåven
7 * Copyright 2000 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* Still to be done:
25 * + correct handling of global/local IOProcs
26 * + mode of mmio objects is not used (read vs write vs readwrite)
27 * + optimization of internal buffers (seg / lin)
28 * + even in 32 bit only, a seg ptr IO buffer is allocated (after this is
29 * fixed, we'll have a proper 32/16 separation)
30 * + thread safeness
31 * + rename operation is broken
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <assert.h>
40 #include "mmsystem.h"
41 #include "windef.h"
43 #include "wine/mmsystem16.h"
44 #include "wine/winbase16.h"
45 #include "heap.h"
46 #include "winemm.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
52 /**************************************************************************
53 * mmioDosIOProc [internal]
55 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
56 LPARAM lParam1, LPARAM lParam2)
58 LRESULT ret = MMSYSERR_NOERROR;
60 TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
62 switch (uMessage) {
63 case MMIOM_OPEN:
65 /* Parameters:
66 * lParam1 = szFileName parameter from mmioOpen
67 * lParam2 = reserved (we use it for 16-bitness)
68 * Returns: zero on success, error code on error
69 * NOTE: lDiskOffset automatically set to zero
71 OFSTRUCT ofs;
72 LPCSTR szFileName = (LPCSTR)lParam1;
74 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
75 FIXME("MMIO_GETTEMP not implemented\n");
76 return MMIOERR_CANNOTOPEN;
79 /* if filename NULL, assume open file handle in adwInfo[0] */
80 if (!szFileName) {
81 if (lParam2)
82 lpmmioinfo->adwInfo[0] = DosFileHandleToWin32Handle(lpmmioinfo->adwInfo[0]);
83 break;
85 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
86 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
87 ret = MMIOERR_CANNOTOPEN;
89 break;
91 case MMIOM_CLOSE:
92 /* Parameters:
93 * lParam1 = wFlags parameter from mmioClose
94 * lParam2 = unused
95 * Returns: zero on success, error code on error
97 if (!(lParam1 & MMIO_FHOPEN))
98 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
99 break;
101 case MMIOM_READ:
102 /* Parameters:
103 * lParam1 = huge pointer to read buffer
104 * lParam2 = number of bytes to read
105 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
106 * in wErrorRet)
108 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
109 if (ret != -1)
110 lpmmioinfo->lDiskOffset += ret;
112 break;
114 case MMIOM_WRITE:
115 case MMIOM_WRITEFLUSH:
116 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
118 /* Parameters:
119 * lParam1 = huge pointer to write buffer
120 * lParam2 = number of bytes to write
121 * Returns: number of bytes written, -1 for error (error code in
122 * wErrorRet)
124 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
125 if (ret != -1)
126 lpmmioinfo->lDiskOffset += ret;
127 break;
129 case MMIOM_SEEK:
130 /* Parameters:
131 * lParam1 = new position
132 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
133 * Returns: new file postion, -1 on error
135 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
136 if (ret != -1)
137 lpmmioinfo->lDiskOffset = ret;
138 return ret;
140 case MMIOM_RENAME:
141 /* Parameters:
142 * lParam1 = old name
143 * lParam2 = new name
144 * Returns: zero on success, non-zero on failure
146 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
147 ret = MMIOERR_FILENOTFOUND;
148 break;
150 default:
151 FIXME("unexpected message %u\n", uMessage);
152 return 0;
155 return ret;
158 /**************************************************************************
159 * mmioMemIOProc [internal]
161 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
162 LPARAM lParam1, LPARAM lParam2)
164 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
166 switch (uMessage) {
168 case MMIOM_OPEN:
169 /* Parameters:
170 * lParam1 = filename (must be NULL)
171 * lParam2 = reserved (we use it for 16-bitness)
172 * Returns: zero on success, error code on error
173 * NOTE: lDiskOffset automatically set to zero
175 /* FIXME: io proc shouldn't change it */
176 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
177 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
178 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
179 return 0;
181 case MMIOM_CLOSE:
182 /* Parameters:
183 * lParam1 = wFlags parameter from mmioClose
184 * lParam2 = unused
185 * Returns: zero on success, error code on error
187 return 0;
189 case MMIOM_READ:
190 /* Parameters:
191 * lParam1 = huge pointer to read buffer
192 * lParam2 = number of bytes to read
193 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
194 * in wErrorRet)
195 * NOTE: lDiskOffset should be updated
197 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
198 return 0;
200 case MMIOM_WRITE:
201 case MMIOM_WRITEFLUSH:
202 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
204 /* Parameters:
205 * lParam1 = huge pointer to write buffer
206 * lParam2 = number of bytes to write
207 * Returns: number of bytes written, -1 for error (error code in
208 * wErrorRet)
209 * NOTE: lDiskOffset should be updated
211 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
212 return 0;
214 case MMIOM_SEEK:
215 /* Parameters:
216 * lParam1 = new position
217 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
218 * Returns: new file postion, -1 on error
219 * NOTE: lDiskOffset should be updated
221 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
222 return -1;
224 default:
225 FIXME("unexpected message %u\n", uMessage);
226 return 0;
229 return 0;
233 enum mmioProcType {MMIO_PROC_16,MMIO_PROC_32A,MMIO_PROC_32W};
235 struct IOProcList
237 struct IOProcList*pNext; /* Next item in linked list */
238 FOURCC fourCC; /* four-character code identifying IOProc */
239 LPMMIOPROC pIOProc; /* pointer to IProc */
240 enum mmioProcType type; /* 16, 32A or 32W */
241 int count; /* number of objects linked to it */
244 /* This array will be the entire list for most apps */
246 static struct IOProcList defaultProcs[] = {
247 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
248 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
251 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
253 /****************************************************************
254 * MMIO_FindProcNode [INTERNAL]
256 * Finds the ProcList node associated with a given FOURCC code.
258 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
260 struct IOProcList* pListNode;
262 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
263 if (pListNode->fourCC == fccIOProc) {
264 return pListNode;
267 return NULL;
270 /****************************************************************
271 * MMIO_InstallIOProc [INTERNAL]
273 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
274 DWORD dwFlags, enum mmioProcType type)
276 LPMMIOPROC lpProc = NULL;
277 struct IOProcList* pListNode;
278 struct IOProcList** ppListNode;
280 TRACE("(%ld, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
282 if (dwFlags & MMIO_GLOBALPROC)
283 FIXME("Global procedures not implemented\n");
285 /* just handle the known procedures for now */
286 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
287 case MMIO_INSTALLPROC:
288 /* Create new entry for the IOProc list */
289 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
290 if (pListNode) {
291 /* Fill in this node */
292 pListNode->fourCC = fccIOProc;
293 pListNode->pIOProc = pIOProc;
294 pListNode->type = type;
295 pListNode->count = 0;
297 /* Stick it on the end of the list */
298 pListNode->pNext = pIOProcListAnchor;
299 pIOProcListAnchor = pListNode;
301 /* Return this IOProc - that's how the caller knows we succeeded */
302 lpProc = pIOProc;
304 break;
306 case MMIO_REMOVEPROC:
308 * Search for the node that we're trying to remove - note
309 * that this method won't find the first item on the list, but
310 * since the first two items on this list are ones we won't
311 * let the user delete anyway, that's okay
313 ppListNode = &pIOProcListAnchor;
314 while ((*ppListNode) && (*ppListNode)->fourCC != fccIOProc)
315 ppListNode = &((*ppListNode)->pNext);
317 if (*ppListNode) { /* found it */
318 /* FIXME: what should be done if an open mmio object uses this proc ?
319 * shall we return an error, nuke the mmio object ?
321 if ((*ppListNode)->count) {
322 ERR("Cannot remove a mmIOProc while in use\n");
323 break;
325 /* remove it, but only if it isn't builtin */
326 if ((*ppListNode) >= defaultProcs &&
327 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
328 WARN("Tried to remove built-in mmio proc. Skipping\n");
329 } else {
330 /* Okay, nuke it */
331 struct IOProcList* ptmpNode = *ppListNode;
332 lpProc = (*ppListNode)->pIOProc;
333 *ppListNode = (*ppListNode)->pNext;
334 HeapFree(GetProcessHeap(), 0, ptmpNode);
337 break;
339 case MMIO_FINDPROC:
340 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
341 lpProc = pListNode->pIOProc;
343 break;
346 return lpProc;
349 /****************************************************************
350 * MMIO_Map32To16 [INTERNAL]
352 static LRESULT MMIO_Map32To16(DWORD wMsg, LPARAM* lp1, LPARAM* lp2)
354 switch (wMsg) {
355 case MMIOM_CLOSE:
356 case MMIOM_SEEK:
357 /* nothing to do */
358 break;
359 case MMIOM_OPEN:
360 case MMIOM_READ:
361 case MMIOM_WRITE:
362 case MMIOM_WRITEFLUSH:
363 *lp1 = MapLS( (void *)*lp1 );
364 break;
365 default:
366 TRACE("Not a mappable message (%ld)\n", wMsg);
368 return MMSYSERR_NOERROR;
371 /****************************************************************
372 * MMIO_UnMap32To16 [INTERNAL]
374 static LRESULT MMIO_UnMap32To16(DWORD wMsg, LPARAM lParam1, LPARAM lParam2,
375 LPARAM lp1, LPARAM lp2)
377 switch (wMsg) {
378 case MMIOM_CLOSE:
379 case MMIOM_SEEK:
380 /* nothing to do */
381 break;
382 case MMIOM_OPEN:
383 case MMIOM_READ:
384 case MMIOM_WRITE:
385 case MMIOM_WRITEFLUSH:
386 UnMapLS( lp1 );
387 break;
388 default:
389 TRACE("Not a mappable message (%ld)\n", wMsg);
391 return MMSYSERR_NOERROR;
394 /****************************************************************
395 * MMIO_SendMessage [INTERNAL]
397 static LRESULT MMIO_SendMessage(LPWINE_MMIO wm, DWORD wMsg, LPARAM lParam1,
398 LPARAM lParam2, enum mmioProcType type)
400 MMIOINFO16 mmioInfo16;
401 LRESULT result;
402 SEGPTR segmmioInfo16;
403 LPARAM lp1 = lParam1, lp2 = lParam2;
405 if (!wm->ioProc || !wm->info.pIOProc) {
406 ERR("brrr\n");
407 result = MMSYSERR_INVALPARAM;
410 switch (wm->ioProc->type) {
411 case MMIO_PROC_16:
412 memset( &mmioInfo16, 0, sizeof(MMIOINFO16));
413 mmioInfo16.lDiskOffset = wm->info.lDiskOffset;
414 mmioInfo16.adwInfo[0] = wm->info.adwInfo[0];
415 mmioInfo16.adwInfo[1] = wm->info.adwInfo[1];
416 mmioInfo16.adwInfo[2] = wm->info.adwInfo[2];
417 mmioInfo16.adwInfo[3] = wm->info.adwInfo[3];
418 if (wm->ioProc->type != type) {
419 /* map (lParam1, lParam2) into (lp1, lp2) 32=>16 */
420 if ((result = MMIO_Map32To16(wMsg, &lp1, &lp2)) != MMSYSERR_NOERROR)
421 return result;
423 /* FIXME: is wm->info.pIOProc a segmented or a linear address ?
424 * sounds to me it's a segmented one, should use a thunk somewhere
426 segmmioInfo16 = MapLS( &mmioInfo16 );
427 result = ((LPMMIOPROC16)wm->info.pIOProc)((LPSTR)segmmioInfo16, wMsg, lp1, lp2);
428 UnMapLS( segmmioInfo16 );
429 if (wm->ioProc->type != type) {
430 MMIO_UnMap32To16(wMsg, lParam1, lParam2, lp1, lp2);
432 wm->info.lDiskOffset = mmioInfo16.lDiskOffset;
433 wm->info.adwInfo[0] = mmioInfo16.adwInfo[0];
434 wm->info.adwInfo[1] = mmioInfo16.adwInfo[1];
435 wm->info.adwInfo[2] = mmioInfo16.adwInfo[2];
436 wm->info.adwInfo[3] = mmioInfo16.adwInfo[3];
437 break;
438 case MMIO_PROC_32A:
439 case MMIO_PROC_32W:
440 if (wm->ioProc->type != type) {
441 /* map (lParam1, lParam2) into (lp1, lp2) 16=>32 */
442 WARN("NIY\n");
444 result = (wm->info.pIOProc)((LPSTR)&wm->info, wMsg, lp1, lp2);
446 #if 0
447 if (wm->ioProc->type != type) {
448 /* unmap (lParam1, lParam2) into (lp1, lp2) 16=>32 */
450 #endif
451 break;
452 default:
453 FIXME("Internal error\n");
454 result = MMSYSERR_ERROR;
457 return result;
460 /**************************************************************************
461 * MMIO_ParseExt [internal]
463 * Parses a filename for the extension.
465 * RETURNS
466 * The FOURCC code for the extension if found, else 0.
468 static FOURCC MMIO_ParseExt(LPCSTR szFileName)
470 /* Filenames are of the form file.ext+ABC
471 FIXME: What if a '+' is part of the file name?
472 For now, we take the last '+' present */
474 FOURCC ret = 0;
476 /* Note that ext{Start,End} point to the . and + respectively */
477 LPSTR extEnd;
479 TRACE("(%s)\n",debugstr_a(szFileName));
481 if (!szFileName)
482 return ret;
483 extEnd = strrchr(szFileName,'+');
484 if (extEnd) {
485 /* Need to parse to find the extension */
486 LPSTR extStart;
488 extStart = extEnd;
489 while (extStart >= szFileName && extStart[0] != '.') {
490 extStart--;
493 if (extStart < szFileName) {
494 ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
495 } else {
496 CHAR ext[5];
498 if (extEnd - extStart - 1 > 4)
499 WARN("Extension length > 4\n");
500 lstrcpynA(ext,extStart + 1,min(extEnd-extStart,5));
501 TRACE("Got extension: %s\n", debugstr_a(ext));
502 /* FOURCC codes identifying file-extensions must be uppercase */
503 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
506 return ret;
509 /**************************************************************************
510 * MMIO_Get [internal]
512 * Retrieves the mmio object from current process
514 static LPWINE_MMIO MMIO_Get(LPWINE_MM_IDATA iData, HMMIO h)
516 LPWINE_MMIO wm = NULL;
518 if (!iData) iData = MULTIMEDIA_GetIData();
520 EnterCriticalSection(&iData->cs);
521 for (wm = iData->lpMMIO; wm; wm = wm->lpNext) {
522 if (wm->info.hmmio == h)
523 break;
525 LeaveCriticalSection(&iData->cs);
526 return wm;
529 /**************************************************************************
530 * MMIO_Create [internal]
532 * Creates an internal representation for a mmio instance
534 static LPWINE_MMIO MMIO_Create(void)
536 static WORD MMIO_counter = 0;
537 LPWINE_MMIO wm;
538 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
540 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
541 if (wm) {
542 EnterCriticalSection(&iData->cs);
543 while (MMIO_Get(iData, ++MMIO_counter));
544 wm->info.hmmio = MMIO_counter;
545 wm->lpNext = iData->lpMMIO;
546 iData->lpMMIO = wm;
547 LeaveCriticalSection(&iData->cs);
549 return wm;
552 /**************************************************************************
553 * MMIO_Destroy [internal]
555 * Destroys an internal representation for a mmio instance
557 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
559 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
560 LPWINE_MMIO* m;
562 EnterCriticalSection(&iData->cs);
563 /* search for the matching one... */
564 m = &(iData->lpMMIO);
565 while (*m && *m != wm) m = &(*m)->lpNext;
566 /* ...and destroy */
567 if (*m) {
568 *m = (*m)->lpNext;
569 HeapFree(GetProcessHeap(), 0, wm);
570 wm = NULL;
572 LeaveCriticalSection(&iData->cs);
573 return wm ? FALSE : TRUE;
576 /****************************************************************
577 * MMIO_Flush [INTERNAL]
579 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
581 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
582 /* not quite sure what to do here, but I'll guess */
583 if (wm->info.dwFlags & MMIO_DIRTY) {
584 /* FIXME: error handling */
585 MMIO_SendMessage(wm, MMIOM_SEEK, wm->info.lBufOffset,
586 SEEK_SET, MMIO_PROC_32A);
587 MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)wm->info.pchBuffer,
588 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
590 if (uFlags & MMIO_EMPTYBUF)
591 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
593 wm->info.dwFlags &= ~MMIO_DIRTY;
595 return MMSYSERR_NOERROR;
598 /***************************************************************************
599 * MMIO_GrabNextBuffer [INTERNAL]
601 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
603 LONG size = wm->info.cchBuffer;
605 TRACE("bo=%lx do=%lx of=%lx\n",
606 wm->info.lBufOffset, wm->info.lDiskOffset,
607 MMIO_SendMessage(wm, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
609 wm->info.lBufOffset = wm->info.lDiskOffset;
610 wm->info.pchNext = wm->info.pchBuffer;
611 wm->info.pchEndRead = wm->info.pchBuffer;
612 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
614 if (for_read) {
615 size = MMIO_SendMessage(wm, MMIOM_READ, (LPARAM)wm->info.pchBuffer,
616 size, MMIO_PROC_32A);
617 if (size > 0)
618 wm->info.pchEndRead += size;
621 wm->bBufferLoaded = TRUE;
622 return size;
625 /***************************************************************************
626 * MMIO_SetBuffer [INTERNAL]
628 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
629 UINT uFlags, BOOL bFrom32)
631 TRACE("(%p %p %ld %u %d)\n", wm, pchBuffer, cchBuffer, uFlags, bFrom32);
633 if (uFlags) return MMSYSERR_INVALPARAM;
634 if (cchBuffer > 0xFFFF)
635 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
637 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
638 return MMIOERR_CANNOTWRITE;
640 if (wm->hMem && (wm->info.dwFlags & MMIO_ALLOCBUF)) {
641 GlobalUnlock16(wm->hMem);
642 GlobalFree16(wm->hMem);
643 wm->hMem = 0;
644 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
647 if (pchBuffer) {
648 if (bFrom32) {
649 wm->info.pchBuffer = pchBuffer;
650 wm->buffer16 = 0;
651 } else {
652 wm->info.pchBuffer = MapSL((SEGPTR)pchBuffer);
653 wm->buffer16 = (SEGPTR)pchBuffer;
655 wm->hMem = 0;
656 } else if (cchBuffer) {
657 if (!(wm->hMem = GlobalAlloc16(GMEM_MOVEABLE, cchBuffer)))
658 return MMIOERR_OUTOFMEMORY;
659 wm->info.dwFlags |= MMIO_ALLOCBUF;
660 } else {
661 wm->info.pchBuffer = NULL;
662 wm->hMem = 0;
663 wm->buffer16 = 0;
666 if (wm->hMem) {
667 wm->buffer16 = K32WOWGlobalLock16(wm->hMem);
668 wm->info.pchBuffer = MapSL(wm->buffer16);
671 wm->info.cchBuffer = cchBuffer;
672 wm->info.pchNext = wm->info.pchBuffer;
673 wm->info.pchEndRead = wm->info.pchBuffer;
674 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
675 wm->info.lBufOffset = 0;
676 wm->bBufferLoaded = FALSE;
678 return MMSYSERR_NOERROR;
681 /**************************************************************************
682 * MMIO_Open [internal]
684 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo,
685 DWORD dwOpenFlags, enum mmioProcType type)
687 LPWINE_MMIO wm;
689 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
691 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
692 char buffer[MAX_PATH];
694 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
695 return (HMMIO16)FALSE;
696 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == -1))
697 return (HMMIO)FALSE;
698 strcpy(szFileName, buffer);
699 return (HMMIO)TRUE;
702 if ((wm = MMIO_Create()) == NULL)
703 return 0;
705 /* If both params are NULL, then parse the file name if available */
706 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
707 wm->info.fccIOProc = MMIO_ParseExt(szFileName);
708 /* Handle any unhandled/error case. Assume DOS file */
709 if (wm->info.fccIOProc == 0)
710 wm->info.fccIOProc = FOURCC_DOS;
711 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
712 wm->info.pIOProc = wm->ioProc->pIOProc;
713 wm->bTmpIOProc = FALSE;
715 /* if just the four character code is present, look up IO proc */
716 else if (refmminfo->pIOProc == NULL) {
717 wm->info.fccIOProc = refmminfo->fccIOProc;
718 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
719 wm->info.pIOProc = wm->ioProc->pIOProc;
720 wm->bTmpIOProc = FALSE;
722 /* if IO proc specified, use it and specified four character code */
723 else {
724 wm->info.fccIOProc = refmminfo->fccIOProc;
725 wm->info.pIOProc = refmminfo->pIOProc;
726 MMIO_InstallIOProc(wm->info.fccIOProc, wm->info.pIOProc,
727 MMIO_INSTALLPROC, type);
728 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
729 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
730 wm->info.pIOProc = wm->ioProc->pIOProc;
731 wm->bTmpIOProc = TRUE;
734 wm->bBufferLoaded = FALSE;
735 wm->ioProc->count++;
737 if (dwOpenFlags & MMIO_ALLOCBUF) {
738 if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0,
739 type != MMIO_PROC_16)))
740 goto error1;
741 } else if (wm->info.fccIOProc == FOURCC_MEM) {
742 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
743 refmminfo->cchBuffer, 0,
744 type != MMIO_PROC_16);
745 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
746 goto error1;
747 wm->bBufferLoaded = TRUE;
748 } /* else => unbuffered, wm->info.pchBuffer == NULL */
750 /* see mmioDosIOProc for that one */
751 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
752 wm->info.dwFlags = dwOpenFlags;
754 /* call IO proc to actually open file */
755 refmminfo->wErrorRet = MMIO_SendMessage(wm, MMIOM_OPEN, (LPARAM)szFileName,
756 type == MMIO_PROC_16, MMIO_PROC_32A);
758 /* grab file size, when possible */
759 wm->dwFileSize = GetFileSize(wm->info.adwInfo[0], NULL);
761 if (refmminfo->wErrorRet == 0)
762 return wm->info.hmmio;
763 error1:
764 if (wm->ioProc) wm->ioProc->count--;
765 error2:
766 MMIO_Destroy(wm);
767 return 0;
770 /**************************************************************************
771 * mmioOpenW [WINMM.@]
773 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
774 DWORD dwOpenFlags)
776 HMMIO ret;
777 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
779 if (lpmmioinfo) {
780 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
781 } else {
782 MMIOINFO mmioinfo;
784 mmioinfo.fccIOProc = 0;
785 mmioinfo.pIOProc = NULL;
786 mmioinfo.pchBuffer = NULL;
787 mmioinfo.cchBuffer = 0;
789 ret = MMIO_Open(szFn, &mmioinfo, dwOpenFlags, MMIO_PROC_32W);
792 HeapFree(GetProcessHeap(), 0, szFn);
793 return ret;
796 /**************************************************************************
797 * mmioOpenA [WINMM.@]
799 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
800 DWORD dwOpenFlags)
802 HMMIO ret;
804 if (lpmmioinfo) {
805 ret = MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
806 } else {
807 MMIOINFO mmioinfo;
809 mmioinfo.fccIOProc = 0;
810 mmioinfo.pIOProc = NULL;
811 mmioinfo.pchBuffer = NULL;
812 mmioinfo.cchBuffer = 0;
814 ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_32A);
816 return ret;
819 /**************************************************************************
820 * mmioOpen [MMSYSTEM.1210]
822 HMMIO16 WINAPI mmioOpen16(LPSTR szFileName, MMIOINFO16* lpmmioinfo16,
823 DWORD dwOpenFlags)
825 HMMIO ret;
826 MMIOINFO mmio;
828 if (lpmmioinfo16) {
829 MMIOINFO mmioinfo;
831 memset(&mmioinfo, 0, sizeof(mmioinfo));
833 mmioinfo.dwFlags = lpmmioinfo16->dwFlags;
834 mmioinfo.fccIOProc = lpmmioinfo16->fccIOProc;
835 mmioinfo.pIOProc = (LPMMIOPROC)lpmmioinfo16->pIOProc;
836 mmioinfo.cchBuffer = lpmmioinfo16->cchBuffer;
837 mmioinfo.pchBuffer = lpmmioinfo16->pchBuffer;
838 mmioinfo.adwInfo[0] = lpmmioinfo16->adwInfo[0];
839 mmioinfo.adwInfo[1] = lpmmioinfo16->adwInfo[1];
840 mmioinfo.adwInfo[2] = lpmmioinfo16->adwInfo[2];
841 mmioinfo.adwInfo[3] = lpmmioinfo16->adwInfo[3];
843 ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_16);
845 mmioGetInfo16(mmioinfo.hmmio, lpmmioinfo16, 0);
846 lpmmioinfo16->wErrorRet = ret;
847 } else {
848 mmio.fccIOProc = 0;
849 mmio.pIOProc = NULL;
850 mmio.pchBuffer = NULL;
851 mmio.cchBuffer = 0;
852 ret = MMIO_Open(szFileName, &mmio, dwOpenFlags, FALSE);
854 return ret;
858 /**************************************************************************
859 * mmioClose [WINMM.@]
861 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
863 LPWINE_MMIO wm;
864 MMRESULT result;
866 TRACE("(%04X, %04X);\n", hmmio, uFlags);
868 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
869 return MMSYSERR_INVALHANDLE;
871 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
872 return result;
874 result = MMIO_SendMessage(wm, MMIOM_CLOSE, uFlags, 0, MMIO_PROC_32A);
876 MMIO_SetBuffer(wm, NULL, 0, 0, TRUE);
878 wm->ioProc->count--;
880 if (wm->bTmpIOProc)
881 MMIO_InstallIOProc(wm->info.fccIOProc, NULL,
882 MMIO_REMOVEPROC, wm->ioProc->type);
884 MMIO_Destroy(wm);
886 return result;
889 /**************************************************************************
890 * mmioClose [MMSYSTEM.1211]
892 MMRESULT16 WINAPI mmioClose16(HMMIO16 hmmio, UINT16 uFlags)
894 return mmioClose(hmmio, uFlags);
897 /**************************************************************************
898 * mmioRead [WINMM.@]
900 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
902 LPWINE_MMIO wm;
903 LONG count;
905 TRACE("(%04X, %p, %ld);\n", hmmio, pch, cch);
907 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
908 return -1;
910 /* unbuffered case first */
911 if (!wm->info.pchBuffer)
912 return MMIO_SendMessage(wm, MMIOM_READ, (LPARAM)pch, cch, MMIO_PROC_32A);
914 /* first try from current buffer */
915 if (wm->info.pchNext != wm->info.pchEndRead) {
916 count = wm->info.pchEndRead - wm->info.pchNext;
917 if (count > cch || count < 0) count = cch;
918 memcpy(pch, wm->info.pchNext, count);
919 wm->info.pchNext += count;
920 pch += count;
921 cch -= count;
922 } else
923 count = 0;
925 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
926 assert(wm->info.cchBuffer);
928 while (cch) {
929 LONG size;
931 size = MMIO_GrabNextBuffer(wm, TRUE);
932 if (size <= 0) break;
933 if (size > cch) size = cch;
934 memcpy(pch, wm->info.pchBuffer, size);
935 wm->info.pchNext += size;
936 pch += size;
937 cch -= size;
938 count += size;
942 TRACE("count=%ld\n", count);
943 return count;
946 /**************************************************************************
947 * mmioRead [MMSYSTEM.1212]
949 LONG WINAPI mmioRead16(HMMIO16 hmmio, HPSTR pch, LONG cch)
951 return mmioRead(hmmio, pch, cch);
954 /**************************************************************************
955 * mmioWrite [WINMM.@]
957 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
959 LPWINE_MMIO wm;
960 LONG count;
962 TRACE("(%04X, %p, %ld);\n", hmmio, pch, cch);
964 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
965 return -1;
967 if (wm->info.cchBuffer) {
968 LONG bytesW = 0;
970 count = 0;
971 while (cch) {
972 if (wm->info.pchNext != wm->info.pchEndWrite) {
973 count = wm->info.pchEndWrite - wm->info.pchNext;
974 if (count > cch || count < 0) count = cch;
975 memcpy(wm->info.pchNext, pch, count);
976 wm->info.pchNext += count;
977 pch += count;
978 cch -= count;
979 bytesW += count;
980 wm->info.dwFlags |= MMIO_DIRTY;
981 } else {
982 if (wm->info.fccIOProc == FOURCC_MEM) {
983 if (wm->info.adwInfo[0]) {
984 /* from where would we get the memory handle? */
985 FIXME("memory file expansion not implemented!\n");
986 break;
987 } else break;
991 if (wm->info.pchNext == wm->info.pchEndWrite)
993 MMIO_Flush(wm, MMIO_EMPTYBUF);
994 MMIO_GrabNextBuffer(wm, FALSE);
996 else break;
998 count = bytesW;
999 } else {
1000 count = MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)pch, cch, MMIO_PROC_32A);
1001 wm->info.lBufOffset = wm->info.lDiskOffset;
1004 TRACE("bytes written=%ld\n", count);
1005 return count;
1008 /**************************************************************************
1009 * mmioWrite [MMSYSTEM.1213]
1011 LONG WINAPI mmioWrite16(HMMIO16 hmmio, HPCSTR pch, LONG cch)
1013 return mmioWrite(hmmio,pch,cch);
1016 /**************************************************************************
1017 * mmioSeek [WINMM.@]
1019 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
1021 LPWINE_MMIO wm;
1022 LONG offset;
1024 TRACE("(%04X, %08lX, %d);\n", hmmio, lOffset, iOrigin);
1026 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1027 return MMSYSERR_INVALHANDLE;
1029 /* not buffered, direct seek on file */
1030 if (!wm->info.pchBuffer)
1031 return MMIO_SendMessage(wm, MMIOM_SEEK, lOffset, iOrigin, MMIO_PROC_32A);
1033 switch (iOrigin) {
1034 case SEEK_SET:
1035 offset = lOffset;
1036 break;
1037 case SEEK_CUR:
1038 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
1039 break;
1040 case SEEK_END:
1041 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
1042 break;
1043 default:
1044 return -1;
1047 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
1048 /* should check that write mode exists */
1049 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1050 return -1;
1051 wm->info.lBufOffset = offset;
1052 wm->info.pchEndRead = wm->info.pchBuffer;
1053 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
1054 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
1055 wm->info.lDiskOffset = wm->dwFileSize;
1057 } else if ((wm->info.cchBuffer > 0) &&
1058 ((offset < wm->info.lBufOffset) ||
1059 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
1060 !wm->bBufferLoaded)) {
1061 /* stay in same buffer ? */
1062 /* some memory mapped buffers are defined with -1 as a size */
1064 /* condition to change buffer */
1065 if ((wm->info.fccIOProc == FOURCC_MEM) ||
1066 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
1067 /* this also sets the wm->info.lDiskOffset field */
1068 MMIO_SendMessage(wm, MMIOM_SEEK,
1069 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
1070 SEEK_SET, MMIO_PROC_32A) == -1)
1071 return -1;
1072 MMIO_GrabNextBuffer(wm, TRUE);
1075 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
1077 TRACE("=> %ld\n", offset);
1078 return offset;
1081 /**************************************************************************
1082 * mmioSeek [MMSYSTEM.1214]
1084 LONG WINAPI mmioSeek16(HMMIO16 hmmio, LONG lOffset, INT16 iOrigin)
1086 return mmioSeek(hmmio, lOffset, iOrigin);
1089 /**************************************************************************
1090 * mmioGetInfo [MMSYSTEM.1215]
1092 MMRESULT16 WINAPI mmioGetInfo16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1094 LPWINE_MMIO wm;
1096 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1098 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1099 return MMSYSERR_INVALHANDLE;
1101 if (!wm->buffer16)
1102 return MMSYSERR_ERROR;
1104 lpmmioinfo->dwFlags = wm->info.dwFlags;
1105 lpmmioinfo->fccIOProc = wm->info.fccIOProc;
1106 lpmmioinfo->pIOProc = (LPMMIOPROC16)wm->info.pIOProc;
1107 lpmmioinfo->wErrorRet = wm->info.wErrorRet;
1108 lpmmioinfo->hTask = wm->info.hTask;
1109 lpmmioinfo->cchBuffer = wm->info.cchBuffer;
1110 lpmmioinfo->pchBuffer = (void*)wm->buffer16;
1111 lpmmioinfo->pchNext = (void*)(wm->buffer16 + (wm->info.pchNext - wm->info.pchBuffer));
1112 lpmmioinfo->pchEndRead = (void*)(wm->buffer16 + (wm->info.pchEndRead - wm->info.pchBuffer));
1113 lpmmioinfo->pchEndWrite = (void*)(wm->buffer16 + (wm->info.pchEndWrite - wm->info.pchBuffer));
1114 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1115 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1116 lpmmioinfo->adwInfo[0] = wm->info.adwInfo[0];
1117 lpmmioinfo->adwInfo[1] = wm->info.adwInfo[1];
1118 lpmmioinfo->adwInfo[2] = wm->info.adwInfo[2];
1119 lpmmioinfo->adwInfo[3] = wm->info.adwInfo[3];
1120 lpmmioinfo->dwReserved1 = 0;
1121 lpmmioinfo->dwReserved2 = 0;
1122 lpmmioinfo->hmmio = wm->info.hmmio;
1124 return MMSYSERR_NOERROR;
1127 /**************************************************************************
1128 * mmioGetInfo [WINMM.@]
1130 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1132 LPWINE_MMIO wm;
1134 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1136 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1137 return MMSYSERR_INVALHANDLE;
1139 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
1141 return MMSYSERR_NOERROR;
1144 /**************************************************************************
1145 * mmioSetInfo [MMSYSTEM.1216]
1147 MMRESULT16 WINAPI mmioSetInfo16(HMMIO16 hmmio, const MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1149 LPWINE_MMIO wm;
1151 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1153 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1154 return MMSYSERR_INVALHANDLE;
1156 /* check if seg and lin buffers are the same */
1157 if (wm->info.cchBuffer != lpmmioinfo->cchBuffer ||
1158 wm->info.pchBuffer != MapSL(wm->buffer16))
1159 return MMSYSERR_INVALPARAM;
1161 /* check pointers coherence */
1162 if (lpmmioinfo->pchNext < lpmmioinfo->pchBuffer ||
1163 lpmmioinfo->pchNext > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
1164 lpmmioinfo->pchEndRead < lpmmioinfo->pchBuffer ||
1165 lpmmioinfo->pchEndRead > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
1166 lpmmioinfo->pchEndWrite < lpmmioinfo->pchBuffer ||
1167 lpmmioinfo->pchEndWrite > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer)
1168 return MMSYSERR_INVALPARAM;
1170 wm->info.pchNext = wm->info.pchBuffer + (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer);
1171 wm->info.pchEndRead = wm->info.pchBuffer + (lpmmioinfo->pchEndRead - lpmmioinfo->pchBuffer);
1172 wm->info.pchEndWrite = wm->info.pchBuffer + (lpmmioinfo->pchEndWrite - lpmmioinfo->pchBuffer);
1174 return MMSYSERR_NOERROR;
1177 /**************************************************************************
1178 * mmioSetInfo [WINMM.@]
1180 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
1182 LPWINE_MMIO wm;
1184 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1186 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1187 return MMSYSERR_INVALHANDLE;
1189 /* check pointers coherence */
1190 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
1191 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
1192 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
1193 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
1194 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
1195 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
1196 return MMSYSERR_INVALPARAM;
1198 wm->info.pchNext = lpmmioinfo->pchNext;
1199 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
1201 return MMSYSERR_NOERROR;
1204 /**************************************************************************
1205 * mmioSetBuffer [WINMM.@]
1207 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
1209 LPWINE_MMIO wm;
1211 TRACE("(hmmio=%04x, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1212 hmmio, pchBuffer, cchBuffer, uFlags);
1214 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1215 return MMSYSERR_INVALHANDLE;
1217 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags, TRUE);
1220 /**************************************************************************
1221 * mmioSetBuffer [MMSYSTEM.1217]
1223 MMRESULT16 WINAPI mmioSetBuffer16(HMMIO16 hmmio, LPSTR segpchBuffer,
1224 LONG cchBuffer, UINT16 uFlags)
1226 LPWINE_MMIO wm;
1228 TRACE("(hmmio=%04x, segpchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1229 hmmio, segpchBuffer, cchBuffer, uFlags);
1231 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1232 return MMSYSERR_INVALHANDLE;
1234 return MMIO_SetBuffer(wm, segpchBuffer, cchBuffer, uFlags, FALSE);
1237 /**************************************************************************
1238 * mmioFlush [WINMM.@]
1240 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
1242 LPWINE_MMIO wm;
1244 TRACE("(%04X, %04X)\n", hmmio, uFlags);
1246 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1247 return MMSYSERR_INVALHANDLE;
1249 return MMIO_Flush(wm, uFlags);
1252 /**************************************************************************
1253 * mmioFlush [MMSYSTEM.1218]
1255 MMRESULT16 WINAPI mmioFlush16(HMMIO16 hmmio, UINT16 uFlags)
1257 return mmioFlush(hmmio, uFlags);
1260 /**************************************************************************
1261 * mmioAdvance [WINMM.@]
1263 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1265 LPWINE_MMIO wm;
1267 TRACE("hmmio=%04X, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1269 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1270 return MMSYSERR_INVALHANDLE;
1272 if (!wm->info.cchBuffer)
1273 return MMIOERR_UNBUFFERED;
1275 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1276 return MMSYSERR_INVALPARAM;
1278 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1280 MMIO_SendMessage(wm, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1281 MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1282 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1283 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1285 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1286 return MMIOERR_CANNOTWRITE;
1287 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset + (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1289 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1291 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1292 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1293 (wm->info.pchEndRead - wm->info.pchBuffer);
1294 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1295 (wm->info.pchEndWrite - wm->info.pchBuffer);
1296 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1297 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1298 return MMSYSERR_NOERROR;
1301 /***********************************************************************
1302 * mmioAdvance [MMSYSTEM.1219]
1304 MMRESULT16 WINAPI mmioAdvance16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1306 LPWINE_MMIO wm;
1308 TRACE("hmmio=%04X, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1310 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1311 return MMSYSERR_INVALHANDLE;
1313 if (!wm->info.cchBuffer)
1314 return MMIOERR_UNBUFFERED;
1316 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1317 return MMSYSERR_INVALPARAM;
1319 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1320 return MMIOERR_CANNOTWRITE;
1322 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1324 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1325 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1326 (wm->info.pchEndRead - wm->info.pchBuffer);
1327 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1328 (wm->info.pchEndWrite - wm->info.pchBuffer);
1329 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1330 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1332 return MMSYSERR_NOERROR;
1335 /**************************************************************************
1336 * mmioStringToFOURCCA [WINMM.@]
1338 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1340 CHAR cc[4];
1341 int i = 0;
1343 for (i = 0; i < 4 && sz[i]; i++) {
1344 if (uFlags & MMIO_TOUPPER) {
1345 cc[i] = toupper(sz[i]);
1346 } else {
1347 cc[i] = sz[i];
1351 /* Pad with spaces */
1352 while (i < 4) cc[i++] = ' ';
1354 TRACE("Got '%.4s'\n",cc);
1355 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1358 /**************************************************************************
1359 * mmioStringToFOURCCW [WINMM.@]
1361 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1363 LPSTR szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
1364 FOURCC ret = mmioStringToFOURCCA(szA,uFlags);
1366 HeapFree(GetProcessHeap(), 0, szA);
1367 return ret;
1370 /**************************************************************************
1371 * mmioStringToFOURCC [MMSYSTEM.1220]
1373 FOURCC WINAPI mmioStringToFOURCC16(LPCSTR sz, UINT16 uFlags)
1375 return mmioStringToFOURCCA(sz, uFlags);
1378 /**************************************************************************
1379 * mmioInstallIOProc [MMSYSTEM.1221]
1381 LPMMIOPROC16 WINAPI mmioInstallIOProc16(FOURCC fccIOProc, LPMMIOPROC16 pIOProc,
1382 DWORD dwFlags)
1384 return (LPMMIOPROC16)MMIO_InstallIOProc(fccIOProc, (LPMMIOPROC)pIOProc,
1385 dwFlags, MMIO_PROC_16);
1388 /**************************************************************************
1389 * mmioInstallIOProcA [WINMM.@]
1391 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1392 LPMMIOPROC pIOProc, DWORD dwFlags)
1394 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1397 /**************************************************************************
1398 * mmioInstallIOProcW [WINMM.@]
1400 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1401 LPMMIOPROC pIOProc, DWORD dwFlags)
1403 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1406 /**************************************************************************
1407 * mmioSendMessage [MMSYSTEM.1222]
1409 LRESULT WINAPI mmioSendMessage16(HMMIO16 hmmio, UINT16 uMessage,
1410 LPARAM lParam1, LPARAM lParam2)
1412 LPWINE_MMIO wm;
1414 TRACE("(%04X, %u, %ld, %ld)\n", hmmio, uMessage, lParam1, lParam2);
1416 if (uMessage < MMIOM_USER)
1417 return MMSYSERR_INVALPARAM;
1419 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1420 return MMSYSERR_INVALHANDLE;
1422 return MMIO_SendMessage(wm, uMessage, lParam1, lParam2, MMIO_PROC_16);
1425 /**************************************************************************
1426 * mmioSendMessage [WINMM.@]
1428 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1429 LPARAM lParam1, LPARAM lParam2)
1431 LPWINE_MMIO wm;
1433 TRACE("(%04X, %u, %ld, %ld)\n", hmmio, uMessage, lParam1, lParam2);
1435 if (uMessage < MMIOM_USER)
1436 return MMSYSERR_INVALPARAM;
1438 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1439 return MMSYSERR_INVALHANDLE;
1441 return MMIO_SendMessage(wm, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1444 /**************************************************************************
1445 * mmioDescend [WINMM.@]
1447 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1448 const MMCKINFO* lpckParent, UINT uFlags)
1450 DWORD dwOldPos;
1451 FOURCC srchCkId;
1452 FOURCC srchType;
1455 TRACE("(%04X, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1457 if (lpck == NULL)
1458 return MMSYSERR_INVALPARAM;
1460 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1461 TRACE("dwOldPos=%ld\n", dwOldPos);
1463 if (lpckParent != NULL) {
1464 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1465 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1466 if (dwOldPos < lpckParent->dwDataOffset ||
1467 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1468 WARN("outside parent chunk\n");
1469 return MMIOERR_CHUNKNOTFOUND;
1473 /* The SDK docu says 'ckid' is used for all cases. Real World
1474 * examples disagree -Marcus,990216.
1477 srchType = 0;
1478 /* find_chunk looks for 'ckid' */
1479 if (uFlags & MMIO_FINDCHUNK)
1480 srchCkId = lpck->ckid;
1481 /* find_riff and find_list look for 'fccType' */
1482 if (uFlags & MMIO_FINDLIST) {
1483 srchCkId = FOURCC_LIST;
1484 srchType = lpck->fccType;
1486 if (uFlags & MMIO_FINDRIFF) {
1487 srchCkId = FOURCC_RIFF;
1488 srchType = lpck->fccType;
1491 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1492 TRACE("searching for %.4s.%.4s\n",
1493 (LPSTR)&srchCkId,
1494 srchType?(LPSTR)&srchType:"any ");
1496 while (TRUE) {
1497 LONG ix;
1499 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1500 if (ix < 2*sizeof(DWORD)) {
1501 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1502 WARN("return ChunkNotFound\n");
1503 return MMIOERR_CHUNKNOTFOUND;
1505 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1506 if (ix < lpck->dwDataOffset - dwOldPos) {
1507 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1508 WARN("return ChunkNotFound\n");
1509 return MMIOERR_CHUNKNOTFOUND;
1511 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1512 (LPSTR)&lpck->ckid,
1513 srchType?(LPSTR)&lpck->fccType:"<na>",
1514 lpck->cksize);
1515 if ((srchCkId == lpck->ckid) &&
1516 (!srchType || (srchType == lpck->fccType))
1518 break;
1520 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1521 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1523 } else {
1524 /* FIXME: unverified, does it do this? */
1525 /* NB: This part is used by WAVE_mciOpen, among others */
1526 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1527 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1528 WARN("return ChunkNotFound 2nd\n");
1529 return MMIOERR_CHUNKNOTFOUND;
1531 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1533 lpck->dwFlags = 0;
1534 /* If we were looking for RIFF/LIST chunks, the final file position
1535 * is after the chunkid. If we were just looking for the chunk
1536 * it is after the cksize. So add 4 in RIFF/LIST case.
1538 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1539 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1540 else
1541 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1542 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1543 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1544 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1545 return MMSYSERR_NOERROR;
1548 /**************************************************************************
1549 * mmioDescend [MMSYSTEM.1223]
1551 MMRESULT16 WINAPI mmioDescend16(HMMIO16 hmmio, LPMMCKINFO lpck,
1552 const MMCKINFO* lpckParent, UINT16 uFlags)
1554 return mmioDescend(hmmio, lpck, lpckParent, uFlags);
1557 /**************************************************************************
1558 * mmioAscend [WINMM.@]
1560 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1562 TRACE("(%04X, %p, %04X);\n", hmmio, lpck, uFlags);
1564 if (lpck->dwFlags & MMIO_DIRTY) {
1565 DWORD dwOldPos, dwNewSize;
1567 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1568 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1569 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1570 dwNewSize = dwOldPos - lpck->dwDataOffset;
1571 if (dwNewSize != lpck->cksize) {
1572 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1573 lpck->cksize = dwNewSize;
1575 /* pad odd size with 0 */
1576 if (dwNewSize & 1) {
1577 char ch = 0;
1578 mmioWrite(hmmio, &ch, 1);
1580 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1581 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1583 lpck->dwFlags = 0;
1586 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1588 return MMSYSERR_NOERROR;
1591 /**************************************************************************
1592 * mmioAscend [MMSYSTEM.1224]
1594 MMRESULT16 WINAPI mmioAscend16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
1596 return mmioAscend(hmmio,lpck,uFlags);
1599 /**************************************************************************
1600 * mmioCreateChunk [WINMM.@]
1602 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1604 DWORD dwOldPos;
1605 LONG size;
1606 LONG ix;
1608 TRACE("(%04X, %p, %04X);\n", hmmio, lpck, uFlags);
1610 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1611 TRACE("dwOldPos=%ld\n", dwOldPos);
1613 if (uFlags == MMIO_CREATELIST)
1614 lpck->ckid = FOURCC_LIST;
1615 else if (uFlags == MMIO_CREATERIFF)
1616 lpck->ckid = FOURCC_RIFF;
1618 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1620 size = 2 * sizeof(DWORD);
1621 lpck->dwDataOffset = dwOldPos + size;
1623 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1624 size += sizeof(DWORD);
1625 lpck->dwFlags = MMIO_DIRTY;
1627 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1628 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1629 if (ix < size) {
1630 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1631 WARN("return CannotWrite\n");
1632 return MMIOERR_CANNOTWRITE;
1635 return MMSYSERR_NOERROR;
1638 /**************************************************************************
1639 * mmioCreateChunk [MMSYSTEM.1225]
1641 MMRESULT16 WINAPI mmioCreateChunk16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
1643 return mmioCreateChunk(hmmio, lpck, uFlags);
1646 /**************************************************************************
1647 * mmioRename [MMSYSTEM.1226]
1649 MMRESULT16 WINAPI mmioRename16(LPCSTR szFileName, LPCSTR szNewFileName,
1650 MMIOINFO16* lpmmioinfo, DWORD dwRenameFlags)
1652 UINT16 result = MMSYSERR_ERROR;
1653 LPMMIOPROC16 ioProc;
1655 TRACE("('%s', '%s', %p, %08lX);\n",
1656 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1658 /* If both params are NULL, then parse the file name */
1659 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1660 lpmmioinfo->fccIOProc = MMIO_ParseExt(szFileName);
1662 /* Handle any unhandled/error case from above. Assume DOS file */
1663 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1664 ioProc = (LPMMIOPROC16)mmioDosIOProc;
1665 /* if just the four character code is present, look up IO proc */
1666 else if (lpmmioinfo->pIOProc == NULL)
1667 ioProc = mmioInstallIOProc16(lpmmioinfo->fccIOProc, NULL, MMIO_FINDPROC);
1668 else
1669 ioProc = lpmmioinfo->pIOProc;
1671 /* FIXME: ioProc is likely a segmented address, thus needing a
1672 * thunk somewhere. The main issue is that Wine's current thunking
1673 * 32 to 16 only supports pascal calling convention
1675 if (ioProc)
1676 result = (ioProc)(0, MMIOM_RENAME,
1677 (LPARAM)szFileName, (LPARAM)szNewFileName);
1679 return result;
1682 /**************************************************************************
1683 * mmioRenameA [WINMM.@]
1685 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1686 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1688 UINT result = MMSYSERR_ERROR;
1689 LPMMIOPROC ioProc;
1691 TRACE("('%s', '%s', %p, %08lX);\n",
1692 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1694 /* If both params are NULL, then parse the file name */
1695 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1696 lpmmioinfo->fccIOProc = MMIO_ParseExt(szFileName);
1698 /* Handle any unhandled/error case from above. Assume DOS file */
1699 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1700 ioProc = (LPMMIOPROC)mmioDosIOProc;
1701 /* if just the four character code is present, look up IO proc */
1702 else if (lpmmioinfo->pIOProc == NULL)
1703 ioProc = MMIO_InstallIOProc(lpmmioinfo->fccIOProc, NULL,
1704 MMIO_FINDPROC, MMIO_PROC_32A);
1705 else /* use relevant ioProc */
1706 ioProc = lpmmioinfo->pIOProc;
1708 if (ioProc)
1709 result = (ioProc)(0, MMIOM_RENAME,
1710 (LPARAM)szFileName, (LPARAM)szNewFileName);
1712 return result;
1715 /**************************************************************************
1716 * mmioRenameW [WINMM.@]
1718 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1719 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1721 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1722 LPSTR sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1723 UINT ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwRenameFlags);
1725 HeapFree(GetProcessHeap(),0,szFn);
1726 HeapFree(GetProcessHeap(),0,sznFn);
1727 return ret;