1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
5 * Copyright 1998 Andrew Taylor
6 * Copyright 1998 Ove Kåven
7 * Copyright 2000 Eric Pouech
11 * + correct handling of global/local IOProcs
12 * + mode of mmio objects is not used (read vs write vs readwrite)
13 * + optimization of internal buffers (seg / lin)
14 * + even in 32 bit only, a seg ptr IO buffer is allocated (after this is
15 * fixed, we'll have a proper 32/16 separation)
17 * + rename operation is broken
29 #include "wine/mmsystem16.h"
30 #include "wine/winbase16.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(mmio);
38 /**************************************************************************
39 * mmioDosIOProc [internal]
41 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
42 LPARAM lParam1, LPARAM lParam2)
44 LRESULT ret = MMSYSERR_NOERROR;
46 TRACE("(%p, %X, %ld, %ld);\n", lpmmioinfo, uMessage, lParam1, lParam2);
52 * lParam1 = szFileName parameter from mmioOpen
53 * lParam2 = reserved (we use it for 16-bitness)
54 * Returns: zero on success, error code on error
55 * NOTE: lDiskOffset automatically set to zero
58 LPCSTR szFileName = (LPCSTR)lParam1;
60 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
61 FIXME("MMIO_GETTEMP not implemented\n");
62 return MMIOERR_CANNOTOPEN;
65 /* if filename NULL, assume open file handle in adwInfo[0] */
68 lpmmioinfo->adwInfo[0] = DosFileHandleToWin32Handle(lpmmioinfo->adwInfo[0]);
72 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs,
74 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
75 ret = MMIOERR_CANNOTOPEN;
81 * lParam1 = wFlags parameter from mmioClose
83 * Returns: zero on success, error code on error
85 if (!(lParam1 & MMIO_FHOPEN))
86 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
91 * lParam1 = huge pointer to read buffer
92 * lParam2 = number of bytes to read
93 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
96 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
98 lpmmioinfo->lDiskOffset += ret;
103 case MMIOM_WRITEFLUSH:
104 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
107 * lParam1 = huge pointer to write buffer
108 * lParam2 = number of bytes to write
109 * Returns: number of bytes written, -1 for error (error code in
112 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
114 lpmmioinfo->lDiskOffset += ret;
119 * lParam1 = new position
120 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
121 * Returns: new file postion, -1 on error
123 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
125 lpmmioinfo->lDiskOffset = ret;
132 * Returns: zero on success, non-zero on failure
134 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
135 ret = MMIOERR_FILENOTFOUND;
139 FIXME("unexpected message %u\n", uMessage);
146 /**************************************************************************
147 * mmioMemIOProc [internal]
149 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
150 LPARAM lParam1, LPARAM lParam2)
152 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
158 * lParam1 = filename (must be NULL)
159 * lParam2 = reserved (we use it for 16-bitness)
160 * Returns: zero on success, error code on error
161 * NOTE: lDiskOffset automatically set to zero
163 /* FIXME: io proc shouldn't change it */
164 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
165 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
170 * lParam1 = wFlags parameter from mmioClose
172 * Returns: zero on success, error code on error
178 * lParam1 = huge pointer to read buffer
179 * lParam2 = number of bytes to read
180 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
182 * NOTE: lDiskOffset should be updated
184 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
188 case MMIOM_WRITEFLUSH:
189 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
192 * lParam1 = huge pointer to write buffer
193 * lParam2 = number of bytes to write
194 * Returns: number of bytes written, -1 for error (error code in
196 * NOTE: lDiskOffset should be updated
198 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
203 * lParam1 = new position
204 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
205 * Returns: new file postion, -1 on error
206 * NOTE: lDiskOffset should be updated
208 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
212 FIXME("unexpected message %u\n", uMessage);
220 enum mmioProcType {MMIO_PROC_16,MMIO_PROC_32A,MMIO_PROC_32W};
224 struct IOProcList*pNext; /* Next item in linked list */
225 FOURCC fourCC; /* four-character code identifying IOProc */
226 LPMMIOPROC pIOProc; /* pointer to IProc */
227 enum mmioProcType type; /* 16, 32A or 32W */
228 int count; /* number of objects linked to it */
231 /* This array will be the entire list for most apps */
233 static struct IOProcList defaultProcs[] = {
234 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
235 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
238 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
240 /****************************************************************
241 * MMIO_FindProcNode [INTERNAL]
243 * Finds the ProcList node associated with a given FOURCC code.
245 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
247 struct IOProcList* pListNode;
249 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
250 if (pListNode->fourCC == fccIOProc) {
257 /****************************************************************
258 * MMIO_InstallIOProc [INTERNAL]
260 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
261 DWORD dwFlags, enum mmioProcType type)
263 LPMMIOPROC lpProc = NULL;
264 struct IOProcList* pListNode;
265 struct IOProcList** ppListNode;
267 TRACE("(%ld, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
269 if (dwFlags & MMIO_GLOBALPROC)
270 FIXME("Global procedures not implemented\n");
272 /* just handle the known procedures for now */
273 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
274 case MMIO_INSTALLPROC:
275 /* Create new entry for the IOProc list */
276 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
278 /* Fill in this node */
279 pListNode->fourCC = fccIOProc;
280 pListNode->pIOProc = pIOProc;
281 pListNode->type = type;
282 pListNode->count = 0;
284 /* Stick it on the end of the list */
285 pListNode->pNext = pIOProcListAnchor;
286 pIOProcListAnchor = pListNode;
288 /* Return this IOProc - that's how the caller knows we succeeded */
293 case MMIO_REMOVEPROC:
295 * Search for the node that we're trying to remove - note
296 * that this method won't find the first item on the list, but
297 * since the first two items on this list are ones we won't
298 * let the user delete anyway, that's okay
300 ppListNode = &pIOProcListAnchor;
301 while ((*ppListNode) && (*ppListNode)->fourCC != fccIOProc)
302 ppListNode = &((*ppListNode)->pNext);
304 if (*ppListNode) { /* found it */
305 /* FIXME: what should be done if an open mmio object uses this proc ?
306 * shall we return an error, nuke the mmio object ?
308 if ((*ppListNode)->count) {
309 ERR("Cannot remove a mmIOProc while in use\n");
312 /* remove it, but only if it isn't builtin */
313 if ((*ppListNode) >= defaultProcs &&
314 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
315 WARN("Tried to remove built-in mmio proc. Skipping\n");
318 struct IOProcList* ptmpNode = *ppListNode;
319 lpProc = (*ppListNode)->pIOProc;
320 *ppListNode = (*ppListNode)->pNext;
321 HeapFree(GetProcessHeap(), 0, ptmpNode);
327 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
328 lpProc = pListNode->pIOProc;
336 /****************************************************************
337 * MMIO_Map32To16 [INTERNAL]
339 static LRESULT MMIO_Map32To16(DWORD wMsg, LPARAM* lp1, LPARAM* lp2)
344 char *lp = SEGPTR_STRDUP( (LPSTR)*lp1 );
345 if (!lp) return MMSYSERR_NOMEM;
346 *lp1 = SEGPTR_GET(lp);
355 case MMIOM_WRITEFLUSH:
357 void* lp = SEGPTR_ALLOC(*lp2);
358 if (!lp) return MMSYSERR_NOMEM;
360 if (wMsg != MMIOM_READ)
361 memcpy(lp, (void*)*lp1, *lp2);
362 *lp1 = SEGPTR_GET(lp);
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)
379 if (!SEGPTR_FREE((void*)lp1)) {
380 FIXME("bad free line=%d\n", __LINE__);
388 memcpy((void*)lParam1, MapSL(lp1), lp2);
391 case MMIOM_WRITEFLUSH:
392 if (!SEGPTR_FREE(MapSL(lp1))) {
393 FIXME("bad free line=%d\n", __LINE__);
397 TRACE("Not a mappable message (%ld)\n", wMsg);
399 return MMSYSERR_NOERROR;
402 /****************************************************************
403 * MMIO_GenerateInfoForIOProc [INTERNAL]
405 static SEGPTR MMIO_GenerateInfoForIOProc(const WINE_MMIO* wm)
407 LPMMIOINFO16 mmioInfo16 = SEGPTR_ALLOC(sizeof(MMIOINFO16));
409 memset(mmioInfo16, 0, sizeof(MMIOINFO16));
411 mmioInfo16->lDiskOffset = wm->info.lDiskOffset;
412 mmioInfo16->adwInfo[0] = wm->info.adwInfo[0];
413 mmioInfo16->adwInfo[1] = wm->info.adwInfo[1];
414 mmioInfo16->adwInfo[2] = wm->info.adwInfo[2];
415 mmioInfo16->adwInfo[3] = wm->info.adwInfo[3];
417 return SEGPTR_GET(mmioInfo16);
420 /****************************************************************
421 * MMIO_UpdateInfoForIOProc [INTERNAL]
423 static LRESULT MMIO_UpdateInfoForIOProc(WINE_MMIO* wm, SEGPTR segmmioInfo16)
425 MMIOINFO16* mmioInfo16 = MapSL(segmmioInfo16);
427 wm->info.lDiskOffset = mmioInfo16->lDiskOffset;
428 wm->info.adwInfo[0] = mmioInfo16->adwInfo[0];
429 wm->info.adwInfo[1] = mmioInfo16->adwInfo[1];
430 wm->info.adwInfo[2] = mmioInfo16->adwInfo[2];
431 wm->info.adwInfo[3] = mmioInfo16->adwInfo[3];
433 if (!SEGPTR_FREE(mmioInfo16)) FIXME("bad free\n");
435 return MMSYSERR_NOERROR;
438 /****************************************************************
439 * MMIO_SendMessage [INTERNAL]
441 static LRESULT MMIO_SendMessage(LPWINE_MMIO wm, DWORD wMsg, LPARAM lParam1,
442 LPARAM lParam2, enum mmioProcType type)
445 SEGPTR segmmioInfo16;
446 LPARAM lp1 = lParam1, lp2 = lParam2;
448 if (!wm->ioProc || !wm->info.pIOProc) {
450 result = MMSYSERR_INVALPARAM;
453 switch (wm->ioProc->type) {
455 segmmioInfo16 = MMIO_GenerateInfoForIOProc(wm);
456 if (wm->ioProc->type != type) {
457 /* map (lParam1, lParam2) into (lp1, lp2) 32=>16 */
458 if ((result = MMIO_Map32To16(wMsg, &lp1, &lp2)) != MMSYSERR_NOERROR)
461 /* FIXME: is wm->info.pIOProc a segmented or a linear address ?
462 * sounds to me it's a segmented one, should use a thunk somewhere
464 result = ((LPMMIOPROC16)wm->info.pIOProc)((LPSTR)segmmioInfo16,
467 if (wm->ioProc->type != type) {
468 MMIO_UnMap32To16(wMsg, lParam1, lParam2, lp1, lp2);
470 MMIO_UpdateInfoForIOProc(wm, segmmioInfo16);
474 if (wm->ioProc->type != type) {
475 /* map (lParam1, lParam2) into (lp1, lp2) 16=>32 */
478 result = (wm->info.pIOProc)((LPSTR)&wm->info, wMsg, lp1, lp2);
481 if (wm->ioProc->type != type) {
482 /* unmap (lParam1, lParam2) into (lp1, lp2) 16=>32 */
487 FIXME("Internal error\n");
488 result = MMSYSERR_ERROR;
494 /**************************************************************************
495 * MMIO_ParseExt [internal]
497 * Parses a filename for the extension.
500 * The FOURCC code for the extension if found, else 0.
502 static FOURCC MMIO_ParseExt(LPCSTR szFileName)
504 /* Filenames are of the form file.ext+ABC
505 FIXME: What if a '+' is part of the file name?
506 For now, we take the last '+' present */
510 /* Note that ext{Start,End} point to the . and + respectively */
513 TRACE("(%s)\n",debugstr_a(szFileName));
517 extEnd = strrchr(szFileName,'+');
519 /* Need to parse to find the extension */
523 while (extStart >= szFileName && extStart[0] != '.') {
527 if (extStart < szFileName) {
528 ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
532 if (extEnd - extStart - 1 > 4)
533 WARN("Extension length > 4\n");
534 lstrcpynA(ext,extStart + 1,min(extEnd-extStart,5));
535 TRACE("Got extension: %s\n", debugstr_a(ext));
536 /* FOURCC codes identifying file-extensions must be uppercase */
537 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
543 /**************************************************************************
544 * MMIO_Get [internal]
546 * Retrieves the mmio object from current process
548 static LPWINE_MMIO MMIO_Get(LPWINE_MM_IDATA iData, HMMIO h)
550 LPWINE_MMIO wm = NULL;
552 if (!iData) iData = MULTIMEDIA_GetIData();
554 EnterCriticalSection(&iData->cs);
555 for (wm = iData->lpMMIO; wm; wm = wm->lpNext) {
556 if (wm->info.hmmio == h)
559 LeaveCriticalSection(&iData->cs);
563 /**************************************************************************
564 * MMIO_Create [internal]
566 * Creates an internal representation for a mmio instance
568 static LPWINE_MMIO MMIO_Create(void)
570 static WORD MMIO_counter = 0;
572 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
574 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
576 EnterCriticalSection(&iData->cs);
577 while (MMIO_Get(iData, ++MMIO_counter));
578 wm->info.hmmio = MMIO_counter;
579 wm->lpNext = iData->lpMMIO;
581 LeaveCriticalSection(&iData->cs);
586 /**************************************************************************
587 * MMIO_Destroy [internal]
589 * Destroys an internal representation for a mmio instance
591 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
593 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
596 EnterCriticalSection(&iData->cs);
597 /* search for the matching one... */
598 m = &(iData->lpMMIO);
599 while (*m && *m != wm) m = &(*m)->lpNext;
603 HeapFree(GetProcessHeap(), 0, wm);
606 LeaveCriticalSection(&iData->cs);
607 return wm ? FALSE : TRUE;
610 /****************************************************************
611 * MMIO_Flush [INTERNAL]
613 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
615 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
616 /* not quite sure what to do here, but I'll guess */
617 if (wm->info.dwFlags & MMIO_DIRTY) {
618 /* FIXME: error handling */
619 MMIO_SendMessage(wm, MMIOM_SEEK, wm->info.lBufOffset,
620 SEEK_SET, MMIO_PROC_32A);
621 MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)wm->info.pchBuffer,
622 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
624 if (uFlags & MMIO_EMPTYBUF)
625 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
627 wm->info.dwFlags &= ~MMIO_DIRTY;
629 return MMSYSERR_NOERROR;
632 /***************************************************************************
633 * MMIO_GrabNextBuffer [INTERNAL]
635 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
637 LONG size = wm->info.cchBuffer;
639 TRACE("bo=%lx do=%lx of=%lx\n",
640 wm->info.lBufOffset, wm->info.lDiskOffset,
641 MMIO_SendMessage(wm, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
643 wm->info.lBufOffset = wm->info.lDiskOffset;
644 wm->info.pchNext = wm->info.pchBuffer;
645 wm->info.pchEndRead = wm->info.pchBuffer;
646 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
649 size = MMIO_SendMessage(wm, MMIOM_READ, (LPARAM)wm->info.pchBuffer,
650 size, MMIO_PROC_32A);
652 wm->info.pchEndRead += size;
655 wm->bBufferLoaded = TRUE;
659 /***************************************************************************
660 * MMIO_SetBuffer [INTERNAL]
662 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
663 UINT uFlags, BOOL bFrom32)
665 TRACE("(%p %p %ld %u %d)\n", wm, pchBuffer, cchBuffer, uFlags, bFrom32);
667 if (uFlags) return MMSYSERR_INVALPARAM;
668 if (cchBuffer > 0xFFFF)
669 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
671 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
672 return MMIOERR_CANNOTWRITE;
674 if (wm->hMem && (wm->info.dwFlags & MMIO_ALLOCBUF)) {
675 GlobalUnlock16(wm->hMem);
676 GlobalFree16(wm->hMem);
678 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
683 wm->info.pchBuffer = pchBuffer;
686 wm->info.pchBuffer = MapSL((SEGPTR)pchBuffer);
687 wm->buffer16 = (SEGPTR)pchBuffer;
690 } else if (cchBuffer) {
691 if (!(wm->hMem = GlobalAlloc16(GMEM_MOVEABLE, cchBuffer)))
692 return MMIOERR_OUTOFMEMORY;
693 wm->info.dwFlags |= MMIO_ALLOCBUF;
695 wm->info.pchBuffer = NULL;
701 wm->buffer16 = K32WOWGlobalLock16(wm->hMem);
702 wm->info.pchBuffer = MapSL(wm->buffer16);
705 wm->info.cchBuffer = cchBuffer;
706 wm->info.pchNext = wm->info.pchBuffer;
707 wm->info.pchEndRead = wm->info.pchBuffer;
708 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
709 wm->info.lBufOffset = 0;
710 wm->bBufferLoaded = FALSE;
712 return MMSYSERR_NOERROR;
715 /**************************************************************************
716 * MMIO_Open [internal]
718 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo,
719 DWORD dwOpenFlags, enum mmioProcType type)
723 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
725 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
726 char buffer[MAX_PATH];
728 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
729 return (HMMIO16)FALSE;
730 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == -1))
732 strcpy(szFileName, buffer);
736 if ((wm = MMIO_Create()) == NULL)
739 /* If both params are NULL, then parse the file name if available */
740 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
741 wm->info.fccIOProc = MMIO_ParseExt(szFileName);
742 /* Handle any unhandled/error case. Assume DOS file */
743 if (wm->info.fccIOProc == 0)
744 wm->info.fccIOProc = FOURCC_DOS;
745 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
746 wm->info.pIOProc = wm->ioProc->pIOProc;
747 wm->bTmpIOProc = FALSE;
749 /* if just the four character code is present, look up IO proc */
750 else if (refmminfo->pIOProc == NULL) {
751 wm->info.fccIOProc = refmminfo->fccIOProc;
752 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
753 wm->info.pIOProc = wm->ioProc->pIOProc;
754 wm->bTmpIOProc = FALSE;
756 /* if IO proc specified, use it and specified four character code */
758 wm->info.fccIOProc = refmminfo->fccIOProc;
759 wm->info.pIOProc = refmminfo->pIOProc;
760 MMIO_InstallIOProc(wm->info.fccIOProc, wm->info.pIOProc,
761 MMIO_INSTALLPROC, type);
762 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
763 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
764 wm->info.pIOProc = wm->ioProc->pIOProc;
765 wm->bTmpIOProc = TRUE;
768 wm->bBufferLoaded = FALSE;
771 if (dwOpenFlags & MMIO_ALLOCBUF) {
772 if ((refmminfo->wErrorRet = mmioSetBuffer(wm->info.hmmio, NULL,
773 MMIO_DEFAULTBUFFER, 0)))
775 } else if (wm->info.fccIOProc == FOURCC_MEM) {
776 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
777 refmminfo->cchBuffer, 0,
778 type != MMIO_PROC_16);
779 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
781 wm->bBufferLoaded = TRUE;
782 } /* else => unbuffered, wm->info.pchBuffer == NULL */
784 /* see mmioDosIOProc for that one */
785 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
786 wm->info.dwFlags = dwOpenFlags;
788 /* call IO proc to actually open file */
789 refmminfo->wErrorRet = MMIO_SendMessage(wm, MMIOM_OPEN, (LPARAM)szFileName,
790 type == MMIO_PROC_16, MMIO_PROC_32A);
792 if (refmminfo->wErrorRet == 0)
793 return wm->info.hmmio;
795 if (wm->ioProc) wm->ioProc->count--;
801 /**************************************************************************
802 * mmioOpenW [WINMM.@]
804 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
808 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
811 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
815 mmioinfo.fccIOProc = 0;
816 mmioinfo.pIOProc = NULL;
817 mmioinfo.pchBuffer = NULL;
818 mmioinfo.cchBuffer = 0;
820 ret = MMIO_Open(szFn, &mmioinfo, dwOpenFlags, MMIO_PROC_32W);
823 HeapFree(GetProcessHeap(), 0, szFn);
827 /**************************************************************************
828 * mmioOpenA [WINMM.@]
830 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
836 ret = MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
840 mmioinfo.fccIOProc = 0;
841 mmioinfo.pIOProc = NULL;
842 mmioinfo.pchBuffer = NULL;
843 mmioinfo.cchBuffer = 0;
845 ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_32A);
850 /**************************************************************************
851 * mmioOpen [MMSYSTEM.1210]
853 HMMIO16 WINAPI mmioOpen16(LPSTR szFileName, MMIOINFO16* lpmmioinfo16,
862 memset(&mmioinfo, 0, sizeof(mmioinfo));
864 mmioinfo.dwFlags = lpmmioinfo16->dwFlags;
865 mmioinfo.fccIOProc = lpmmioinfo16->fccIOProc;
866 mmioinfo.pIOProc = (LPMMIOPROC)lpmmioinfo16->pIOProc;
867 mmioinfo.cchBuffer = lpmmioinfo16->cchBuffer;
868 mmioinfo.pchBuffer = lpmmioinfo16->pchBuffer;
869 mmioinfo.adwInfo[0] = lpmmioinfo16->adwInfo[0];
870 mmioinfo.adwInfo[1] = lpmmioinfo16->adwInfo[1];
871 mmioinfo.adwInfo[2] = lpmmioinfo16->adwInfo[2];
872 mmioinfo.adwInfo[3] = lpmmioinfo16->adwInfo[3];
874 ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_16);
876 mmioGetInfo16(mmioinfo.hmmio, lpmmioinfo16, 0);
877 lpmmioinfo16->wErrorRet = ret;
881 mmio.pchBuffer = NULL;
883 ret = MMIO_Open(szFileName, &mmio, dwOpenFlags, FALSE);
889 /**************************************************************************
890 * mmioClose [WINMM.@]
892 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
897 TRACE("(%04X, %04X);\n", hmmio, uFlags);
899 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
900 return MMSYSERR_INVALHANDLE;
902 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
905 result = MMIO_SendMessage(wm, MMIOM_CLOSE, uFlags, 0, MMIO_PROC_32A);
907 mmioSetBuffer(hmmio, NULL, 0, 0);
912 MMIO_InstallIOProc(wm->info.fccIOProc, NULL,
913 MMIO_REMOVEPROC, wm->ioProc->type);
920 /**************************************************************************
921 * mmioClose [MMSYSTEM.1211]
923 MMRESULT16 WINAPI mmioClose16(HMMIO16 hmmio, UINT16 uFlags)
925 return mmioClose(hmmio, uFlags);
928 /**************************************************************************
931 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
936 TRACE("(%04X, %p, %ld);\n", hmmio, pch, cch);
938 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
941 /* unbuffered case first */
942 if (!wm->info.pchBuffer)
943 return MMIO_SendMessage(wm, MMIOM_READ, (LPARAM)pch, cch, MMIO_PROC_32A);
945 /* first try from current buffer */
946 if (wm->info.pchNext != wm->info.pchEndRead) {
947 count = wm->info.pchEndRead - wm->info.pchNext;
948 if (count > cch || count < 0) count = cch;
949 memcpy(pch, wm->info.pchNext, count);
950 wm->info.pchNext += count;
956 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
957 assert(wm->info.cchBuffer);
962 size = MMIO_GrabNextBuffer(wm, TRUE);
963 if (size <= 0) break;
964 if (size > cch) size = cch;
965 memcpy(pch, wm->info.pchBuffer, size);
966 wm->info.pchNext += size;
973 TRACE("count=%ld\n", count);
977 /**************************************************************************
978 * mmioRead [MMSYSTEM.1212]
980 LONG WINAPI mmioRead16(HMMIO16 hmmio, HPSTR pch, LONG cch)
982 return mmioRead(hmmio, pch, cch);
985 /**************************************************************************
986 * mmioWrite [WINMM.@]
988 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
993 TRACE("(%04X, %p, %ld);\n", hmmio, pch, cch);
995 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
998 if (wm->info.cchBuffer) {
1003 if (wm->info.pchNext != wm->info.pchEndWrite) {
1004 count = wm->info.pchEndWrite - wm->info.pchNext;
1005 if (count > cch || count < 0) count = cch;
1006 memcpy(wm->info.pchNext, pch, count);
1007 wm->info.pchNext += count;
1011 wm->info.dwFlags |= MMIO_DIRTY;
1013 if (wm->info.fccIOProc == FOURCC_MEM) {
1014 if (wm->info.adwInfo[0]) {
1015 /* from where would we get the memory handle? */
1016 FIXME("memory file expansion not implemented!\n");
1022 if (wm->info.pchNext == wm->info.pchEndWrite)
1024 MMIO_Flush(wm, MMIO_EMPTYBUF);
1025 MMIO_GrabNextBuffer(wm, FALSE);
1031 count = MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)pch, cch, MMIO_PROC_32A);
1032 wm->info.lBufOffset = wm->info.lDiskOffset;
1035 TRACE("bytes written=%ld\n", count);
1039 /**************************************************************************
1040 * mmioWrite [MMSYSTEM.1213]
1042 LONG WINAPI mmioWrite16(HMMIO16 hmmio, HPCSTR pch, LONG cch)
1044 return mmioWrite(hmmio,pch,cch);
1047 /**************************************************************************
1048 * mmioSeek [WINMM.@]
1050 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
1055 TRACE("(%04X, %08lX, %d);\n", hmmio, lOffset, iOrigin);
1057 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1058 return MMSYSERR_INVALHANDLE;
1060 /* not buffered, direct seek on file */
1061 if (!wm->info.pchBuffer)
1062 return MMIO_SendMessage(wm, MMIOM_SEEK, lOffset, iOrigin, MMIO_PROC_32A);
1069 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
1072 if (wm->info.fccIOProc == FOURCC_MEM) {
1073 offset = wm->info.cchBuffer;
1075 assert(MMIO_SendMessage(wm, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A) == wm->info.lDiskOffset);
1076 offset = MMIO_SendMessage(wm, MMIOM_SEEK, 0, SEEK_END, MMIO_PROC_32A);
1077 MMIO_SendMessage(wm, MMIOM_SEEK, wm->info.lDiskOffset, SEEK_SET, MMIO_PROC_32A);
1085 /* stay in same buffer ? */
1086 /* some memory mapped buffers are defined with -1 as a size */
1087 if ((wm->info.cchBuffer > 0) &&
1088 ((offset < wm->info.lBufOffset) ||
1089 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
1090 !wm->bBufferLoaded)) {
1092 /* condition to change buffer */
1093 if ((wm->info.fccIOProc == FOURCC_MEM) ||
1094 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
1095 /* this also sets the wm->info.lDiskOffset field */
1096 MMIO_SendMessage(wm, MMIOM_SEEK,
1097 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
1098 SEEK_SET, MMIO_PROC_32A) == -1)
1100 MMIO_GrabNextBuffer(wm, TRUE);
1103 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
1105 TRACE("=> %ld\n", offset);
1109 /**************************************************************************
1110 * mmioSeek [MMSYSTEM.1214]
1112 LONG WINAPI mmioSeek16(HMMIO16 hmmio, LONG lOffset, INT16 iOrigin)
1114 return mmioSeek(hmmio, lOffset, iOrigin);
1117 /**************************************************************************
1118 * mmioGetInfo [MMSYSTEM.1215]
1120 MMRESULT16 WINAPI mmioGetInfo16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1124 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1126 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1127 return MMSYSERR_INVALHANDLE;
1130 return MMSYSERR_ERROR;
1132 lpmmioinfo->dwFlags = wm->info.dwFlags;
1133 lpmmioinfo->fccIOProc = wm->info.fccIOProc;
1134 lpmmioinfo->pIOProc = (LPMMIOPROC16)wm->info.pIOProc;
1135 lpmmioinfo->wErrorRet = wm->info.wErrorRet;
1136 lpmmioinfo->hTask = wm->info.hTask;
1137 lpmmioinfo->cchBuffer = wm->info.cchBuffer;
1138 lpmmioinfo->pchBuffer = (void*)wm->buffer16;
1139 lpmmioinfo->pchNext = (void*)(wm->buffer16 + (wm->info.pchNext - wm->info.pchBuffer));
1140 lpmmioinfo->pchEndRead = (void*)(wm->buffer16 + (wm->info.pchEndRead - wm->info.pchBuffer));
1141 lpmmioinfo->pchEndWrite = (void*)(wm->buffer16 + (wm->info.pchEndWrite - wm->info.pchBuffer));
1142 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1143 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1144 lpmmioinfo->adwInfo[0] = wm->info.adwInfo[0];
1145 lpmmioinfo->adwInfo[1] = wm->info.adwInfo[1];
1146 lpmmioinfo->adwInfo[2] = wm->info.adwInfo[2];
1147 lpmmioinfo->adwInfo[3] = wm->info.adwInfo[3];
1148 lpmmioinfo->dwReserved1 = 0;
1149 lpmmioinfo->dwReserved2 = 0;
1150 lpmmioinfo->hmmio = wm->info.hmmio;
1152 return MMSYSERR_NOERROR;
1155 /**************************************************************************
1156 * mmioGetInfo [WINMM.@]
1158 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1162 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1164 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1165 return MMSYSERR_INVALHANDLE;
1167 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
1169 return MMSYSERR_NOERROR;
1172 /**************************************************************************
1173 * mmioSetInfo [MMSYSTEM.1216]
1175 MMRESULT16 WINAPI mmioSetInfo16(HMMIO16 hmmio, const MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1179 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1181 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1182 return MMSYSERR_INVALHANDLE;
1184 /* check if seg and lin buffers are the same */
1185 if (wm->info.cchBuffer != lpmmioinfo->cchBuffer ||
1186 wm->info.pchBuffer != MapSL(wm->buffer16))
1187 return MMSYSERR_INVALPARAM;
1189 /* check pointers coherence */
1190 if (lpmmioinfo->pchNext < lpmmioinfo->pchBuffer ||
1191 lpmmioinfo->pchNext > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
1192 lpmmioinfo->pchEndRead < lpmmioinfo->pchBuffer ||
1193 lpmmioinfo->pchEndRead > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
1194 lpmmioinfo->pchEndWrite < lpmmioinfo->pchBuffer ||
1195 lpmmioinfo->pchEndWrite > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer)
1196 return MMSYSERR_INVALPARAM;
1198 wm->info.pchNext = wm->info.pchBuffer + (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer);
1199 wm->info.pchEndRead = wm->info.pchBuffer + (lpmmioinfo->pchEndRead - lpmmioinfo->pchBuffer);
1200 wm->info.pchEndWrite = wm->info.pchBuffer + (lpmmioinfo->pchEndWrite - lpmmioinfo->pchBuffer);
1202 return MMSYSERR_NOERROR;
1205 /**************************************************************************
1206 * mmioSetInfo [WINMM.@]
1208 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
1212 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1214 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1215 return MMSYSERR_INVALHANDLE;
1217 /* check pointers coherence */
1218 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
1219 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
1220 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
1221 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
1222 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
1223 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
1224 return MMSYSERR_INVALPARAM;
1226 wm->info.pchNext = lpmmioinfo->pchNext;
1227 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
1229 return MMSYSERR_NOERROR;
1232 /**************************************************************************
1233 * mmioSetBuffer [WINMM.@]
1235 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
1239 TRACE("(hmmio=%04x, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1240 hmmio, pchBuffer, cchBuffer, uFlags);
1242 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1243 return MMSYSERR_INVALHANDLE;
1245 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags, TRUE);
1248 /**************************************************************************
1249 * mmioSetBuffer [MMSYSTEM.1217]
1251 MMRESULT16 WINAPI mmioSetBuffer16(HMMIO16 hmmio, LPSTR segpchBuffer,
1252 LONG cchBuffer, UINT16 uFlags)
1256 TRACE("(hmmio=%04x, segpchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1257 hmmio, segpchBuffer, cchBuffer, uFlags);
1259 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1260 return MMSYSERR_INVALHANDLE;
1262 return MMIO_SetBuffer(wm, segpchBuffer, cchBuffer, uFlags, FALSE);
1265 /**************************************************************************
1266 * mmioFlush [WINMM.@]
1268 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
1272 TRACE("(%04X, %04X)\n", hmmio, uFlags);
1274 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1275 return MMSYSERR_INVALHANDLE;
1277 return MMIO_Flush(wm, uFlags);
1280 /**************************************************************************
1281 * mmioFlush [MMSYSTEM.1218]
1283 MMRESULT16 WINAPI mmioFlush16(HMMIO16 hmmio, UINT16 uFlags)
1285 return mmioFlush(hmmio, uFlags);
1288 /**************************************************************************
1289 * mmioAdvance [WINMM.@]
1291 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1295 TRACE("hmmio=%04X, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1297 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1298 return MMSYSERR_INVALHANDLE;
1300 if (!wm->info.cchBuffer)
1301 return MMIOERR_UNBUFFERED;
1303 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1304 return MMSYSERR_INVALPARAM;
1306 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1308 FIXME("writing data to disk\n");
1309 MMIO_SendMessage(wm, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1310 MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1311 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1312 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1314 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1315 return MMIOERR_CANNOTWRITE;
1317 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1319 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1320 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1321 (wm->info.pchEndRead - wm->info.pchBuffer);
1322 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1323 (wm->info.pchEndWrite - wm->info.pchBuffer);
1324 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1325 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1326 return MMSYSERR_NOERROR;
1329 /***********************************************************************
1330 * mmioAdvance [MMSYSTEM.1219]
1332 MMRESULT16 WINAPI mmioAdvance16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1336 TRACE("hmmio=%04X, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1338 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1339 return MMSYSERR_INVALHANDLE;
1341 if (!wm->info.cchBuffer)
1342 return MMIOERR_UNBUFFERED;
1344 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1345 return MMSYSERR_INVALPARAM;
1347 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1348 return MMIOERR_CANNOTWRITE;
1350 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1352 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1353 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1354 (wm->info.pchEndRead - wm->info.pchBuffer);
1355 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1356 (wm->info.pchEndWrite - wm->info.pchBuffer);
1357 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1358 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1360 return MMSYSERR_NOERROR;
1363 /**************************************************************************
1364 * mmioStringToFOURCCA [WINMM.@]
1366 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1371 for (i = 0; i < 4 && sz[i]; i++) {
1372 if (uFlags & MMIO_TOUPPER) {
1373 cc[i] = toupper(sz[i]);
1379 /* Pad with spaces */
1380 while (i < 4) cc[i++] = ' ';
1382 TRACE("Got '%.4s'\n",cc);
1383 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1386 /**************************************************************************
1387 * mmioStringToFOURCCW [WINMM.@]
1389 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1391 LPSTR szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
1392 FOURCC ret = mmioStringToFOURCCA(szA,uFlags);
1394 HeapFree(GetProcessHeap(), 0, szA);
1398 /**************************************************************************
1399 * mmioStringToFOURCC [MMSYSTEM.1220]
1401 FOURCC WINAPI mmioStringToFOURCC16(LPCSTR sz, UINT16 uFlags)
1403 return mmioStringToFOURCCA(sz, uFlags);
1406 /**************************************************************************
1407 * mmioInstallIOProc [MMSYSTEM.1221]
1409 LPMMIOPROC16 WINAPI mmioInstallIOProc16(FOURCC fccIOProc, LPMMIOPROC16 pIOProc,
1412 return (LPMMIOPROC16)MMIO_InstallIOProc(fccIOProc, (LPMMIOPROC)pIOProc,
1413 dwFlags, MMIO_PROC_16);
1416 /**************************************************************************
1417 * mmioInstallIOProcA [WINMM.@]
1419 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1420 LPMMIOPROC pIOProc, DWORD dwFlags)
1422 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1425 /**************************************************************************
1426 * mmioInstallIOProcW [WINMM.@]
1428 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1429 LPMMIOPROC pIOProc, DWORD dwFlags)
1431 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1434 /**************************************************************************
1435 * mmioSendMessage [MMSYSTEM.1222]
1437 LRESULT WINAPI mmioSendMessage16(HMMIO16 hmmio, UINT16 uMessage,
1438 LPARAM lParam1, LPARAM lParam2)
1442 TRACE("(%04X, %u, %ld, %ld)\n", hmmio, uMessage, lParam1, lParam2);
1444 if (uMessage < MMIOM_USER)
1445 return MMSYSERR_INVALPARAM;
1447 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1448 return MMSYSERR_INVALHANDLE;
1450 return MMIO_SendMessage(wm, uMessage, lParam1, lParam2, MMIO_PROC_16);
1453 /**************************************************************************
1454 * mmioSendMessage [WINMM.@]
1456 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1457 LPARAM lParam1, LPARAM lParam2)
1461 TRACE("(%04X, %u, %ld, %ld)\n", hmmio, uMessage, lParam1, lParam2);
1463 if (uMessage < MMIOM_USER)
1464 return MMSYSERR_INVALPARAM;
1466 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1467 return MMSYSERR_INVALHANDLE;
1469 return MMIO_SendMessage(wm, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1472 /**************************************************************************
1473 * mmioDescend [WINMM.@]
1475 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1476 const MMCKINFO* lpckParent, UINT uFlags)
1483 TRACE("(%04X, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1486 return MMSYSERR_INVALPARAM;
1488 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1489 TRACE("dwOldPos=%ld\n", dwOldPos);
1491 if (lpckParent != NULL) {
1492 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1493 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1494 if (dwOldPos < lpckParent->dwDataOffset ||
1495 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1496 WARN("outside parent chunk\n");
1497 return MMIOERR_CHUNKNOTFOUND;
1501 /* The SDK docu says 'ckid' is used for all cases. Real World
1502 * examples disagree -Marcus,990216.
1506 /* find_chunk looks for 'ckid' */
1507 if (uFlags & MMIO_FINDCHUNK)
1508 srchCkId = lpck->ckid;
1509 /* find_riff and find_list look for 'fccType' */
1510 if (uFlags & MMIO_FINDLIST) {
1511 srchCkId = FOURCC_LIST;
1512 srchType = lpck->fccType;
1514 if (uFlags & MMIO_FINDRIFF) {
1515 srchCkId = FOURCC_RIFF;
1516 srchType = lpck->fccType;
1519 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1520 TRACE("searching for %.4s.%.4s\n",
1522 srchType?(LPSTR)&srchType:"any ");
1527 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1528 if (ix < 2*sizeof(DWORD)) {
1529 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1530 WARN("return ChunkNotFound\n");
1531 return MMIOERR_CHUNKNOTFOUND;
1533 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1534 if (ix < lpck->dwDataOffset - dwOldPos) {
1535 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1536 WARN("return ChunkNotFound\n");
1537 return MMIOERR_CHUNKNOTFOUND;
1539 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1541 srchType?(LPSTR)&lpck->fccType:"<na>",
1543 if ((srchCkId == lpck->ckid) &&
1544 (!srchType || (srchType == lpck->fccType))
1548 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1549 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1552 /* FIXME: unverified, does it do this? */
1553 /* NB: This part is used by WAVE_mciOpen, among others */
1554 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1555 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1556 WARN("return ChunkNotFound 2nd\n");
1557 return MMIOERR_CHUNKNOTFOUND;
1559 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1562 /* If we were looking for RIFF/LIST chunks, the final file position
1563 * is after the chunkid. If we were just looking for the chunk
1564 * it is after the cksize. So add 4 in RIFF/LIST case.
1566 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1567 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1569 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1570 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1571 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1572 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1573 return MMSYSERR_NOERROR;
1576 /**************************************************************************
1577 * mmioDescend [MMSYSTEM.1223]
1579 MMRESULT16 WINAPI mmioDescend16(HMMIO16 hmmio, LPMMCKINFO lpck,
1580 const MMCKINFO* lpckParent, UINT16 uFlags)
1582 return mmioDescend(hmmio, lpck, lpckParent, uFlags);
1585 /**************************************************************************
1586 * mmioAscend [WINMM.@]
1588 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1590 TRACE("(%04X, %p, %04X);\n", hmmio, lpck, uFlags);
1592 if (lpck->dwFlags & MMIO_DIRTY) {
1593 DWORD dwOldPos, dwNewSize;
1595 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1596 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1597 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1598 dwNewSize = dwOldPos - lpck->dwDataOffset;
1599 if (dwNewSize != lpck->cksize) {
1600 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1601 lpck->cksize = dwNewSize;
1603 /* pad odd size with 0 */
1604 if (dwNewSize & 1) {
1606 mmioWrite(hmmio, &ch, 1);
1608 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1609 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1614 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1616 return MMSYSERR_NOERROR;
1619 /**************************************************************************
1620 * mmioAscend [MMSYSTEM.1224]
1622 MMRESULT16 WINAPI mmioAscend16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
1624 return mmioAscend(hmmio,lpck,uFlags);
1627 /**************************************************************************
1628 * mmioCreateChunk [WINMM.@]
1630 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1636 TRACE("(%04X, %p, %04X);\n", hmmio, lpck, uFlags);
1638 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1639 TRACE("dwOldPos=%ld\n", dwOldPos);
1641 if (uFlags == MMIO_CREATELIST)
1642 lpck->ckid = FOURCC_LIST;
1643 else if (uFlags == MMIO_CREATERIFF)
1644 lpck->ckid = FOURCC_RIFF;
1646 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1648 size = 2 * sizeof(DWORD);
1649 lpck->dwDataOffset = dwOldPos + size;
1651 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1652 size += sizeof(DWORD);
1653 lpck->dwFlags = MMIO_DIRTY;
1655 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1656 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1658 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1659 WARN("return CannotWrite\n");
1660 return MMIOERR_CANNOTWRITE;
1663 return MMSYSERR_NOERROR;
1666 /**************************************************************************
1667 * mmioCreateChunk [MMSYSTEM.1225]
1669 MMRESULT16 WINAPI mmioCreateChunk16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
1671 return mmioCreateChunk(hmmio, lpck, uFlags);
1674 /**************************************************************************
1675 * mmioRename [MMSYSTEM.1226]
1677 MMRESULT16 WINAPI mmioRename16(LPCSTR szFileName, LPCSTR szNewFileName,
1678 MMIOINFO16* lpmmioinfo, DWORD dwRenameFlags)
1680 UINT16 result = MMSYSERR_ERROR;
1681 LPMMIOPROC16 ioProc;
1683 TRACE("('%s', '%s', %p, %08lX);\n",
1684 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1686 /* If both params are NULL, then parse the file name */
1687 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1688 lpmmioinfo->fccIOProc = MMIO_ParseExt(szFileName);
1690 /* Handle any unhandled/error case from above. Assume DOS file */
1691 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1692 ioProc = (LPMMIOPROC16)mmioDosIOProc;
1693 /* if just the four character code is present, look up IO proc */
1694 else if (lpmmioinfo->pIOProc == NULL)
1695 ioProc = mmioInstallIOProc16(lpmmioinfo->fccIOProc, NULL, MMIO_FINDPROC);
1697 ioProc = lpmmioinfo->pIOProc;
1699 /* FIXME: ioProc is likely a segmented address, thus needing a
1700 * thunk somewhere. The main issue is that Wine's current thunking
1701 * 32 to 16 only supports pascal calling convention
1704 result = (ioProc)(0, MMIOM_RENAME,
1705 (LPARAM)szFileName, (LPARAM)szNewFileName);
1710 /**************************************************************************
1711 * mmioRenameA [WINMM.@]
1713 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1714 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1716 UINT result = MMSYSERR_ERROR;
1719 TRACE("('%s', '%s', %p, %08lX);\n",
1720 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1722 /* If both params are NULL, then parse the file name */
1723 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1724 lpmmioinfo->fccIOProc = MMIO_ParseExt(szFileName);
1726 /* Handle any unhandled/error case from above. Assume DOS file */
1727 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1728 ioProc = (LPMMIOPROC)mmioDosIOProc;
1729 /* if just the four character code is present, look up IO proc */
1730 else if (lpmmioinfo->pIOProc == NULL)
1731 ioProc = MMIO_InstallIOProc(lpmmioinfo->fccIOProc, NULL,
1732 MMIO_FINDPROC, MMIO_PROC_32A);
1733 else /* use relevant ioProc */
1734 ioProc = lpmmioinfo->pIOProc;
1737 result = (ioProc)(0, MMIOM_RENAME,
1738 (LPARAM)szFileName, (LPARAM)szNewFileName);
1743 /**************************************************************************
1744 * mmioRenameW [WINMM.@]
1746 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1747 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1749 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1750 LPSTR sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1751 UINT ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwRenameFlags);
1753 HeapFree(GetProcessHeap(),0,szFn);
1754 HeapFree(GetProcessHeap(),0,sznFn);