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
26 #include "wine/winbase16.h"
28 #include "selectors.h"
31 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(mmio);
36 /**************************************************************************
37 * mmioDosIOProc [internal]
39 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
40 LPARAM lParam1, LPARAM lParam2)
42 LRESULT ret = MMSYSERR_NOERROR;
44 TRACE("(%p, %X, %ld, %ld);\n", lpmmioinfo, uMessage, lParam1, lParam2);
50 * lParam1 = szFileName parameter from mmioOpen
51 * lParam2 = reserved (we use it for 16-bitness)
52 * Returns: zero on success, error code on error
53 * NOTE: lDiskOffset automatically set to zero
56 LPCSTR szFileName = (LPCSTR)lParam1;
58 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
59 FIXME("MMIO_GETTEMP not implemented\n");
60 return MMIOERR_CANNOTOPEN;
63 /* if filename NULL, assume open file handle in adwInfo[0] */
66 lpmmioinfo->adwInfo[0] = FILE_GetHandle(lpmmioinfo->adwInfo[0]);
70 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs,
72 if (lpmmioinfo->adwInfo[0] == -1)
73 ret = MMIOERR_CANNOTOPEN;
79 * lParam1 = wFlags parameter from mmioClose
81 * Returns: zero on success, error code on error
83 if (!(lParam1 & MMIO_FHOPEN))
84 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
89 * lParam1 = huge pointer to read buffer
90 * lParam2 = number of bytes to read
91 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
94 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
96 lpmmioinfo->lDiskOffset += ret;
101 case MMIOM_WRITEFLUSH:
102 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
105 * lParam1 = huge pointer to write buffer
106 * lParam2 = number of bytes to write
107 * Returns: number of bytes written, -1 for error (error code in
110 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
112 lpmmioinfo->lDiskOffset += ret;
117 * lParam1 = new position
118 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
119 * Returns: new file postion, -1 on error
121 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
123 lpmmioinfo->lDiskOffset = ret;
130 * Returns: zero on success, non-zero on failure
132 FIXME("MMIOM_RENAME incomplete\n");
133 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
134 ret = MMIOERR_FILENOTFOUND;
138 FIXME("unexpected message %u\n", uMessage);
145 /**************************************************************************
146 * mmioMemIOProc [internal]
148 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
149 LPARAM lParam1, LPARAM lParam2)
151 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
157 * lParam1 = filename (must be NULL)
158 * lParam2 = reserved (we use it for 16-bitness)
159 * Returns: zero on success, error code on error
160 * NOTE: lDiskOffset automatically set to zero
162 /* FIXME: io proc shouldn't change it */
163 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
164 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
169 * lParam1 = wFlags parameter from mmioClose
171 * Returns: zero on success, error code on error
177 * lParam1 = huge pointer to read buffer
178 * lParam2 = number of bytes to read
179 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
181 * NOTE: lDiskOffset should be updated
183 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
187 case MMIOM_WRITEFLUSH:
188 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
191 * lParam1 = huge pointer to write buffer
192 * lParam2 = number of bytes to write
193 * Returns: number of bytes written, -1 for error (error code in
195 * NOTE: lDiskOffset should be updated
197 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
202 * lParam1 = new position
203 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
204 * Returns: new file postion, -1 on error
205 * NOTE: lDiskOffset should be updated
207 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
211 FIXME("unexpected message %u\n", uMessage);
219 enum mmioProcType {MMIO_PROC_16,MMIO_PROC_32A,MMIO_PROC_32W};
223 struct IOProcList*pNext; /* Next item in linked list */
224 FOURCC fourCC; /* four-character code identifying IOProc */
225 LPMMIOPROC pIOProc; /* pointer to IProc */
226 enum mmioProcType type; /* 16, 32A or 32W */
227 int count; /* number of objects linked to it */
230 /* This array will be the entire list for most apps */
232 static struct IOProcList defaultProcs[] = {
233 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
234 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
237 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
239 /****************************************************************
240 * MMIO_FindProcNode [INTERNAL]
242 * Finds the ProcList node associated with a given FOURCC code.
244 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
246 struct IOProcList* pListNode;
248 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
249 if (pListNode->fourCC == fccIOProc) {
256 /****************************************************************
257 * MMIO_InstallIOProc [INTERNAL]
259 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
260 DWORD dwFlags, enum mmioProcType type)
262 LPMMIOPROC lpProc = NULL;
263 struct IOProcList* pListNode;
264 struct IOProcList** ppListNode;
266 TRACE("(%ld, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
268 if (dwFlags & MMIO_GLOBALPROC)
269 FIXME("Global procedures not implemented\n");
271 /* just handle the known procedures for now */
272 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
273 case MMIO_INSTALLPROC:
274 /* Create new entry for the IOProc list */
275 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
277 /* Fill in this node */
278 pListNode->fourCC = fccIOProc;
279 pListNode->pIOProc = pIOProc;
280 pListNode->type = type;
281 pListNode->count = 0;
283 /* Stick it on the end of the list */
284 pListNode->pNext = pIOProcListAnchor;
285 pIOProcListAnchor = pListNode;
287 /* Return this IOProc - that's how the caller knows we succeeded */
292 case MMIO_REMOVEPROC:
294 * Search for the node that we're trying to remove - note
295 * that this method won't find the first item on the list, but
296 * since the first two items on this list are ones we won't
297 * let the user delete anyway, that's okay
299 ppListNode = &pIOProcListAnchor;
300 while ((*ppListNode) && (*ppListNode)->fourCC != fccIOProc)
301 ppListNode = &((*ppListNode)->pNext);
303 if (*ppListNode) { /* found it */
304 /* FIXME: what should be done if an open mmio object uses this proc ?
305 * shall we return an error, nuke the mmio object ?
307 if ((*ppListNode)->count) {
308 ERR("Cannot remove a mmIOProc while in use\n");
311 /* remove it, but only if it isn't builtin */
312 if ((*ppListNode) >= defaultProcs &&
313 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
314 WARN("Tried to remove built-in mmio proc. Skipping\n");
317 struct IOProcList* ptmpNode = *ppListNode;
318 lpProc = (*ppListNode)->pIOProc;
319 *ppListNode = (*ppListNode)->pNext;
320 HeapFree(GetProcessHeap(), 0, ptmpNode);
326 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
327 lpProc = pListNode->pIOProc;
335 /****************************************************************
336 * MMIO_Map32To16 [INTERNAL]
338 static LRESULT MMIO_Map32To16(DWORD wMsg, LPARAM* lp1, LPARAM* lp2)
343 void* lp = SEGPTR_ALLOC(strlen((LPSTR)*lp1) + 1);
344 if (!lp) return MMSYSERR_NOMEM;
346 strcpy((void*)SEGPTR_GET(lp), (LPSTR)*lp1);
356 case MMIOM_WRITEFLUSH:
358 void* lp = SEGPTR_ALLOC(*lp2);
359 if (!lp) return MMSYSERR_NOMEM;
361 if (wMsg != MMIOM_READ)
362 memcpy((void*)SEGPTR_GET(lp), (void*)*lp1, *lp2);
367 TRACE("Not a mappable message (%ld)\n", wMsg);
369 return MMSYSERR_NOERROR;
372 /****************************************************************
373 * MMIO_UnMap32To16 [INTERNAL]
375 static LRESULT MMIO_UnMap32To16(DWORD wMsg, LPARAM lParam1, LPARAM lParam2,
376 LPARAM lp1, LPARAM lp2)
380 if (!SEGPTR_FREE((void*)lp1)) {
381 FIXME("bad free line=%d\n", __LINE__);
389 memcpy((void*)lParam1, (void*)SEGPTR_GET((void*)lp1), lp2);
392 case MMIOM_WRITEFLUSH:
393 if (!SEGPTR_FREE((void*)lp1)) {
394 FIXME("bad free line=%d\n", __LINE__);
398 TRACE("Not a mappable message (%ld)\n", wMsg);
400 return MMSYSERR_NOERROR;
403 /****************************************************************
404 * MMIO_GenerateInfoForIOProc [INTERNAL]
406 static SEGPTR MMIO_GenerateInfoForIOProc(const WINE_MMIO* wm)
408 SEGPTR lp = (SEGPTR)SEGPTR_ALLOC(sizeof(MMIOINFO16));
409 LPMMIOINFO16 mmioInfo16 = (LPMMIOINFO16)SEGPTR_GET((void*)lp);
411 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];
422 /****************************************************************
423 * MMIO_UpdateInfoForIOProc [INTERNAL]
425 static LRESULT MMIO_UpdateInfoForIOProc(WINE_MMIO* wm, SEGPTR segmmioInfo16)
427 const MMIOINFO16* mmioInfo16;
429 mmioInfo16 = (const MMIOINFO16*)SEGPTR_GET((void*)segmmioInfo16);
431 wm->info.lDiskOffset = mmioInfo16->lDiskOffset;
432 wm->info.adwInfo[0] = mmioInfo16->adwInfo[0];
433 wm->info.adwInfo[1] = mmioInfo16->adwInfo[1];
434 wm->info.adwInfo[2] = mmioInfo16->adwInfo[2];
435 wm->info.adwInfo[3] = mmioInfo16->adwInfo[3];
437 if (!SEGPTR_FREE((void*)segmmioInfo16)) {
438 FIXME("bad free line=%d\n", __LINE__);
441 return MMSYSERR_NOERROR;
444 /****************************************************************
445 * MMIO_SendMessage [INTERNAL]
447 static LRESULT MMIO_SendMessage(LPWINE_MMIO wm, DWORD wMsg, LPARAM lParam1,
448 LPARAM lParam2, enum mmioProcType type)
451 SEGPTR segmmioInfo16;
452 LPARAM lp1 = lParam1, lp2 = lParam2;
454 if (!wm->ioProc || !wm->info.pIOProc) {
456 result = MMSYSERR_INVALPARAM;
459 switch (wm->ioProc->type) {
461 segmmioInfo16 = MMIO_GenerateInfoForIOProc(wm);
462 if (wm->ioProc->type != type) {
463 /* map (lParam1, lParam2) into (lp1, lp2) 32=>16 */
464 if ((result = MMIO_Map32To16(wMsg, &lp1, &lp2)) != MMSYSERR_NOERROR)
467 /* FIXME: is wm->info.pIOProc a segmented or a linear address ?
468 * sounds to me it's a segmented one, should use a thunk somewhere
470 result = ((LPMMIOPROC16)wm->info.pIOProc)((LPSTR)segmmioInfo16,
473 if (wm->ioProc->type != type) {
474 MMIO_UnMap32To16(wMsg, lParam1, lParam2, lp1, lp2);
476 MMIO_UpdateInfoForIOProc(wm, segmmioInfo16);
480 if (wm->ioProc->type != type) {
481 /* map (lParam1, lParam2) into (lp1, lp2) 16=>32 */
484 result = (wm->info.pIOProc)((LPSTR)&wm->info, wMsg, lp1, lp2);
487 if (wm->ioProc->type != type) {
488 /* unmap (lParam1, lParam2) into (lp1, lp2) 16=>32 */
493 FIXME("Internal error\n");
494 result = MMSYSERR_ERROR;
500 /**************************************************************************
501 * MMIO_ParseExt [internal]
503 * Parses a filename for the extension.
506 * The FOURCC code for the extension if found, else 0.
508 static FOURCC MMIO_ParseExt(LPCSTR szFileName)
510 /* Filenames are of the form file.ext+ABC
511 FIXME: What if a '+' is part of the file name?
512 For now, we take the last '+' present */
516 /* Note that ext{Start,End} point to the . and + respectively */
519 TRACE("(%s)\n",debugstr_a(szFileName));
521 extEnd = strrchr(szFileName,'+');
523 /* Need to parse to find the extension */
527 while (extStart > szFileName && extStart[0] != '.') {
531 if (extStart == szFileName) {
532 ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
536 if (extEnd - extStart - 1 > 4)
537 WARN("Extension length > 4\n");
538 lstrcpynA(ext,extStart + 1,min(extEnd-extStart,5));
539 TRACE("Got extension: %s\n", debugstr_a(ext));
540 /* FOURCC codes identifying file-extentions must be uppercase */
541 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
547 /**************************************************************************
548 * MMIO_Get [internal]
550 * Retirieves from current process the mmio object
552 static LPWINE_MMIO MMIO_Get(LPWINE_MM_IDATA iData, HMMIO h)
554 LPWINE_MMIO wm = NULL;
556 if (!iData) iData = MULTIMEDIA_GetIData();
558 EnterCriticalSection(&iData->cs);
559 for (wm = iData->lpMMIO; wm; wm = wm->lpNext) {
560 if (wm->info.hmmio == h)
563 LeaveCriticalSection(&iData->cs);
567 /**************************************************************************
568 * MMIO_Create [internal]
570 * Creates an internal representation for a mmio instance
572 static LPWINE_MMIO MMIO_Create(void)
574 static WORD MMIO_counter = 0;
576 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
578 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
580 EnterCriticalSection(&iData->cs);
581 while (MMIO_Get(iData, ++MMIO_counter));
582 wm->info.hmmio = MMIO_counter;
583 wm->lpNext = iData->lpMMIO;
585 LeaveCriticalSection(&iData->cs);
590 /**************************************************************************
591 * MMIO_Destroy [internal]
593 * Destroys an internal representation for a mmio instance
595 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
597 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
600 EnterCriticalSection(&iData->cs);
601 for (m = &(iData->lpMMIO); *m && *m != wm; m = &(*m)->lpNext);
604 HeapFree(GetProcessHeap(), 0, wm);
607 LeaveCriticalSection(&iData->cs);
608 return wm ? FALSE : TRUE;
611 /****************************************************************
612 * MMIO_Flush [INTERNAL]
614 static LRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
616 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
617 /* not quite sure what to do here, but I'll guess */
618 if (wm->info.dwFlags & MMIO_DIRTY) {
619 MMIO_SendMessage(wm, MMIOM_SEEK, wm->info.lDiskOffset,
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.pchBuffer;
627 wm->info.dwFlags &= ~MMIO_DIRTY;
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;
649 size = MMIO_SendMessage(wm, MMIOM_READ, (LPARAM)wm->info.pchBuffer,
650 size, MMIO_PROC_32A);
652 wm->info.pchEndRead += size;
654 wm->bBufferLoaded = TRUE;
658 /***************************************************************************
659 * MMIO_SetBuffer [INTERNAL]
661 static UINT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
662 UINT uFlags, BOOL bFrom32)
664 TRACE("(%p %p %ld %u %d)\n", wm, pchBuffer, cchBuffer, uFlags, bFrom32);
666 if (uFlags) return MMSYSERR_INVALPARAM;
667 if (cchBuffer > 0xFFFF)
668 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
670 if (MMIO_Flush(wm, MMIO_EMPTYBUF) != 0)
671 return MMIOERR_CANNOTWRITE;
673 if ((!cchBuffer || pchBuffer) && (wm->info.dwFlags & MMIO_ALLOCBUF)) {
674 GlobalUnlock16(wm->hMem);
675 GlobalFree16(wm->hMem);
676 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
680 wm->info.pchBuffer = pchBuffer;
683 wm->info.pchBuffer = PTR_SEG_TO_LIN(pchBuffer);
684 wm->buffer16 = (SEGPTR)pchBuffer;
687 } else if (cchBuffer && (wm->info.dwFlags & MMIO_ALLOCBUF)) {
689 GlobalUnlock16(wm->hMem);
690 hNewBuf = GlobalReAlloc16(wm->hMem, cchBuffer, 0);
692 /* FIXME: this assumes the memory block didn't move */
693 GlobalLock16(wm->hMem);
694 return MMIOERR_OUTOFMEMORY;
697 } else if (cchBuffer) {
698 if (!(wm->hMem = GlobalAlloc16(GMEM_MOVEABLE, cchBuffer)))
699 return MMIOERR_OUTOFMEMORY;
700 wm->info.dwFlags |= MMIO_ALLOCBUF;
702 wm->info.pchBuffer = NULL;
708 wm->buffer16 = WIN16_GlobalLock16(wm->hMem);
709 wm->info.pchBuffer = (void*)PTR_SEG_TO_LIN((void*)wm->buffer16);
712 wm->info.cchBuffer = cchBuffer;
713 wm->info.pchNext = wm->info.pchBuffer;
714 wm->info.pchEndRead = wm->info.pchBuffer;
715 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
716 wm->info.lBufOffset = 0;
717 wm->bBufferLoaded = FALSE;
722 /**************************************************************************
723 * MMIO_Open [internal]
725 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo,
726 DWORD dwOpenFlags, enum mmioProcType type)
730 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
732 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
733 char buffer[MAX_PATH];
735 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
736 return (HMMIO16)FALSE;
737 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == -1))
739 strcpy(szFileName, buffer);
743 if ((wm = MMIO_Create()) == NULL)
746 /* If both params are NULL, then parse the file name */
747 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
748 wm->info.fccIOProc = MMIO_ParseExt(szFileName);
749 /* Handle any unhandled/error case. Assume DOS file */
750 if (wm->info.fccIOProc == 0)
751 wm->info.fccIOProc = FOURCC_DOS;
752 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
753 wm->info.pIOProc = wm->ioProc->pIOProc;
754 wm->bTmpIOProc = FALSE;
756 /* if just the four character code is present, look up IO proc */
757 else if (refmminfo->pIOProc == NULL) {
758 wm->info.fccIOProc = refmminfo->fccIOProc;
759 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
760 wm->info.pIOProc = wm->ioProc->pIOProc;
761 wm->bTmpIOProc = FALSE;
763 /* if IO proc specified, use it and specified four character code */
765 wm->info.fccIOProc = refmminfo->fccIOProc;
766 wm->info.pIOProc = refmminfo->pIOProc;
767 MMIO_InstallIOProc(wm->info.fccIOProc, wm->info.pIOProc,
768 MMIO_INSTALLPROC, type);
769 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
770 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
771 wm->info.pIOProc = wm->ioProc->pIOProc;
772 wm->bTmpIOProc = TRUE;
775 wm->bBufferLoaded = FALSE;
778 if (dwOpenFlags & MMIO_ALLOCBUF) {
779 if ((refmminfo->wErrorRet = mmioSetBuffer(wm->info.hmmio, NULL,
780 MMIO_DEFAULTBUFFER, 0)))
782 } else if (wm->info.fccIOProc == FOURCC_MEM) {
783 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
784 refmminfo->cchBuffer, 0,
785 type != MMIO_PROC_16);
786 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
788 wm->bBufferLoaded = TRUE;
789 } /* else => unbuffered, wm->info.pchBuffer == NULL */
791 /* see mmioDosIOProc for that one */
792 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
793 wm->info.dwFlags = dwOpenFlags;
795 /* call IO proc to actually open file */
796 refmminfo->wErrorRet = MMIO_SendMessage(wm, MMIOM_OPEN, (LPARAM)szFileName,
797 type == MMIO_PROC_16, MMIO_PROC_32A);
799 if (refmminfo->wErrorRet == 0)
800 return wm->info.hmmio;
802 if (wm->ioProc) wm->ioProc->count--;
808 /**************************************************************************
809 * mmioOpenW [WINMM.123]
811 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
815 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
818 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
822 mmioinfo.fccIOProc = 0;
823 mmioinfo.pIOProc = NULL;
824 mmioinfo.pchBuffer = NULL;
825 mmioinfo.cchBuffer = 0;
827 ret = MMIO_Open(szFn, &mmioinfo, dwOpenFlags, MMIO_PROC_32W);
830 HeapFree(GetProcessHeap(), 0, szFn);
834 /**************************************************************************
835 * mmioOpenA [WINMM.122]
837 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
843 ret = MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
847 mmioinfo.fccIOProc = 0;
848 mmioinfo.pIOProc = NULL;
849 mmioinfo.pchBuffer = NULL;
850 mmioinfo.cchBuffer = 0;
852 ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_32A);
857 /**************************************************************************
858 * mmioOpen [MMSYSTEM.1210]
860 HMMIO16 WINAPI mmioOpen16(LPSTR szFileName, MMIOINFO16* lpmmioinfo16,
869 memset(&mmioinfo, 0, sizeof(mmioinfo));
871 mmioinfo.dwFlags = lpmmioinfo16->dwFlags;
872 mmioinfo.fccIOProc = lpmmioinfo16->fccIOProc;
873 mmioinfo.pIOProc = (LPMMIOPROC)lpmmioinfo16->pIOProc;
874 mmioinfo.cchBuffer = lpmmioinfo16->cchBuffer;
875 mmioinfo.pchBuffer = lpmmioinfo16->pchBuffer;
876 mmioinfo.adwInfo[0] = lpmmioinfo16->adwInfo[0];
877 mmioinfo.adwInfo[1] = lpmmioinfo16->adwInfo[1];
878 mmioinfo.adwInfo[2] = lpmmioinfo16->adwInfo[2];
879 mmioinfo.adwInfo[3] = lpmmioinfo16->adwInfo[3];
881 ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_16);
883 mmioGetInfo16(mmioinfo.hmmio, lpmmioinfo16, 0);
884 lpmmioinfo16->wErrorRet = ret;
888 mmio.pchBuffer = NULL;
890 ret = MMIO_Open(szFileName, &mmio, dwOpenFlags, FALSE);
896 /**************************************************************************
897 * mmioClose [WINMM.114]
899 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
904 TRACE("(%04X, %04X);\n", hmmio, uFlags);
906 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
907 return MMSYSERR_INVALHANDLE;
909 if ((result = MMIO_Flush(wm, MMIO_EMPTYBUF)) != 0)
912 result = MMIO_SendMessage(wm, MMIOM_CLOSE, uFlags, 0, MMIO_PROC_32A);
914 mmioSetBuffer(hmmio, NULL, 0, 0);
919 MMIO_InstallIOProc(wm->info.fccIOProc, NULL,
920 MMIO_REMOVEPROC, wm->ioProc->type);
927 /**************************************************************************
928 * mmioClose [MMSYSTEM.1211]
930 MMRESULT16 WINAPI mmioClose16(HMMIO16 hmmio, UINT16 uFlags)
932 return mmioClose(hmmio, uFlags);
935 /**************************************************************************
936 * mmioRead [WINMM.124]
938 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
943 TRACE("(%04X, %p, %ld);\n", hmmio, pch, cch);
945 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
948 /* unbuffered case first */
949 if (!wm->info.pchBuffer)
950 return MMIO_SendMessage(wm, MMIOM_READ, (LPARAM)pch, cch, MMIO_PROC_32A);
952 /* first try from current buffer */
953 if (wm->info.pchNext != wm->info.pchEndRead) {
954 count = wm->info.pchEndRead - wm->info.pchNext;
955 if (count > cch || count < 0) count = cch;
956 memcpy(pch, wm->info.pchNext, count);
957 wm->info.pchNext += count;
963 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
964 assert(wm->info.cchBuffer);
969 size = MMIO_GrabNextBuffer(wm, TRUE);
970 if (size <= 0) break;
971 if (size > cch) size = cch;
972 memcpy(pch, wm->info.pchBuffer, size);
973 wm->info.pchNext += size;
980 TRACE("count=%ld\n", count);
984 /**************************************************************************
985 * mmioRead [MMSYSTEM.1212]
987 LONG WINAPI mmioRead16(HMMIO16 hmmio, HPSTR pch, LONG cch)
989 return mmioRead(hmmio, pch, cch);
992 /**************************************************************************
993 * mmioWrite [WINMM.133]
995 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
1000 TRACE("(%04X, %p, %ld);\n", hmmio, pch, cch);
1002 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1005 if (wm->info.cchBuffer) {
1008 if (wm->info.pchNext != wm->info.pchEndWrite) {
1009 count = wm->info.pchEndWrite - wm->info.pchNext;
1010 if (count > cch || count < 0) count = cch;
1011 memcpy(wm->info.pchNext, pch, count);
1012 wm->info.pchNext += count;
1016 wm->info.dwFlags |= MMIO_DIRTY;
1018 if (wm->info.fccIOProc == FOURCC_MEM) {
1019 if (wm->info.adwInfo[0]) {
1020 /* from where would we get the memory handle? */
1021 FIXME("memory file expansion not implemented!\n");
1026 if (wm->info.pchNext == wm->info.pchEndWrite) MMIO_Flush(wm, MMIO_EMPTYBUF);
1030 bytesW = MMIO_SendMessage(wm, MMIOM_WRITE, (LPARAM)pch, cch, MMIO_PROC_32A);
1031 wm->info.lBufOffset = wm->info.lDiskOffset;
1034 TRACE("bytes written=%ld\n", bytesW);
1038 /**************************************************************************
1039 * mmioWrite [MMSYSTEM.1213]
1041 LONG WINAPI mmioWrite16(HMMIO16 hmmio, HPCSTR pch, LONG cch)
1043 return mmioWrite(hmmio,pch,cch);
1046 /**************************************************************************
1047 * mmioSeek [MMSYSTEM.1214]
1049 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
1054 TRACE("(%04X, %08lX, %d);\n", hmmio, lOffset, iOrigin);
1056 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1057 return MMSYSERR_INVALHANDLE;
1059 /* not buffered, direct seek on file */
1060 if (!wm->info.pchBuffer)
1061 return MMIO_SendMessage(wm, MMIOM_SEEK, lOffset, iOrigin, MMIO_PROC_32A);
1068 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
1071 if (wm->info.fccIOProc == FOURCC_MEM) {
1072 offset = wm->info.cchBuffer;
1074 assert(MMIO_SendMessage(wm, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A) == wm->info.lDiskOffset);
1075 offset = MMIO_SendMessage(wm, MMIOM_SEEK, 0, SEEK_END, MMIO_PROC_32A);
1076 MMIO_SendMessage(wm, MMIOM_SEEK, wm->info.lDiskOffset, SEEK_SET, MMIO_PROC_32A);
1084 /* stay in same buffer ? */
1085 /* some memory mapped buffers are defined with -1 as a size */
1086 if ((wm->info.cchBuffer > 0) &&
1087 ((offset < wm->info.lBufOffset) ||
1088 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
1089 !wm->bBufferLoaded)) {
1091 /* condition to change buffer */
1092 if ((wm->info.fccIOProc == FOURCC_MEM) ||
1093 MMIO_Flush(wm, MMIO_EMPTYBUF) ||
1094 /* this also sets the wm->info.lDiskOffset field */
1095 MMIO_SendMessage(wm, MMIOM_SEEK,
1096 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
1097 SEEK_SET, MMIO_PROC_32A) == -1)
1099 MMIO_GrabNextBuffer(wm, TRUE);
1102 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
1104 TRACE("=> %ld\n", offset);
1108 /**************************************************************************
1109 * mmioSeek [MMSYSTEM.1214]
1111 LONG WINAPI mmioSeek16(HMMIO16 hmmio, LONG lOffset, INT16 iOrigin)
1113 return mmioSeek(hmmio, lOffset, iOrigin);
1116 /**************************************************************************
1117 * mmioGetInfo [MMSYSTEM.1215]
1119 UINT16 WINAPI mmioGetInfo16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1123 TRACE("mmioGetInfo\n");
1125 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1126 return MMSYSERR_INVALHANDLE;
1129 return MMSYSERR_ERROR;
1131 lpmmioinfo->dwFlags = wm->info.dwFlags;
1132 lpmmioinfo->fccIOProc = wm->info.fccIOProc;
1133 lpmmioinfo->pIOProc = (LPMMIOPROC16)wm->info.pIOProc;
1134 lpmmioinfo->wErrorRet = wm->info.wErrorRet;
1135 lpmmioinfo->hTask = wm->info.hTask;
1136 lpmmioinfo->cchBuffer = wm->info.cchBuffer;
1137 lpmmioinfo->pchBuffer = (void*)wm->buffer16;
1138 lpmmioinfo->pchNext = (void*)(wm->buffer16 + (wm->info.pchNext - wm->info.pchBuffer));
1139 lpmmioinfo->pchEndRead = (void*)(wm->buffer16 + (wm->info.pchEndRead - wm->info.pchBuffer));
1140 lpmmioinfo->pchEndWrite = (void*)(wm->buffer16 + (wm->info.pchEndWrite - wm->info.pchBuffer));
1141 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1142 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1143 lpmmioinfo->adwInfo[0] = wm->info.adwInfo[0];
1144 lpmmioinfo->adwInfo[1] = wm->info.adwInfo[1];
1145 lpmmioinfo->adwInfo[2] = wm->info.adwInfo[2];
1146 lpmmioinfo->adwInfo[3] = wm->info.adwInfo[3];
1147 lpmmioinfo->dwReserved1 = 0;
1148 lpmmioinfo->dwReserved2 = 0;
1149 lpmmioinfo->hmmio = wm->info.hmmio;
1154 /**************************************************************************
1155 * mmioGetInfo [WINMM.118]
1157 UINT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1161 TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1163 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1164 return MMSYSERR_INVALHANDLE;
1166 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
1171 /**************************************************************************
1172 * mmioSetInfo16 [MMSYSTEM.1216]
1174 UINT16 WINAPI mmioSetInfo16(HMMIO16 hmmio, const MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1178 TRACE("mmioSetInfo\n");
1180 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1181 return MMSYSERR_INVALHANDLE;
1183 /* check if seg and lin buffers are the same */
1184 if (wm->info.cchBuffer != lpmmioinfo->cchBuffer ||
1185 wm->info.pchBuffer != PTR_SEG_TO_LIN((void*)wm->buffer16))
1186 return MMSYSERR_INVALPARAM;
1188 /* check pointers coherence */
1189 if (lpmmioinfo->pchNext < lpmmioinfo->pchBuffer ||
1190 lpmmioinfo->pchNext > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
1191 lpmmioinfo->pchEndRead < lpmmioinfo->pchBuffer ||
1192 lpmmioinfo->pchEndRead > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
1193 lpmmioinfo->pchEndWrite < lpmmioinfo->pchBuffer ||
1194 lpmmioinfo->pchEndWrite > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer)
1195 return MMSYSERR_INVALPARAM;
1197 wm->info.pchNext = wm->info.pchBuffer + (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer);
1198 wm->info.pchEndRead = wm->info.pchBuffer + (lpmmioinfo->pchEndRead - lpmmioinfo->pchBuffer);
1199 wm->info.pchEndWrite = wm->info.pchBuffer + (lpmmioinfo->pchEndWrite - lpmmioinfo->pchBuffer);
1204 /**************************************************************************
1205 * mmioSetInfo [WINMM.130]
1207 UINT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
1211 TRACE("mmioSetInfo\n");
1213 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1214 return MMSYSERR_INVALHANDLE;
1216 /* check pointers coherence */
1217 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
1218 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
1219 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
1220 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
1221 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
1222 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
1223 return MMSYSERR_INVALPARAM;
1225 wm->info.pchNext = lpmmioinfo->pchNext;
1226 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
1231 /**************************************************************************
1232 * mmioSetBuffer [WINMM.129]
1234 UINT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
1238 TRACE("(hmmio=%04x, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1239 hmmio, pchBuffer, cchBuffer, uFlags);
1241 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1242 return MMSYSERR_INVALHANDLE;
1244 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags, TRUE);
1247 /**************************************************************************
1248 * mmioSetBuffer [MMSYSTEM.1217]
1250 UINT16 WINAPI mmioSetBuffer16(HMMIO16 hmmio, LPSTR segpchBuffer,
1251 LONG cchBuffer, UINT16 uFlags)
1255 TRACE("(hmmio=%04x, segpchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1256 hmmio, segpchBuffer, cchBuffer, uFlags);
1258 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1259 return MMSYSERR_INVALHANDLE;
1261 return MMIO_SetBuffer(wm, segpchBuffer, cchBuffer, uFlags, FALSE);
1264 /**************************************************************************
1265 * mmioFlush [WINMM.117]
1267 UINT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
1271 TRACE("(%04X, %04X)\n", hmmio, uFlags);
1273 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1274 return MMSYSERR_INVALHANDLE;
1276 return MMIO_Flush(wm, uFlags);
1279 /**************************************************************************
1280 * mmioFlush [MMSYSTEM.1218]
1282 UINT16 WINAPI mmioFlush16(HMMIO16 hmmio, UINT16 uFlags)
1284 return mmioFlush(hmmio, uFlags);
1287 /**************************************************************************
1288 * mmioAdvance [MMSYSTEM.1219]
1290 UINT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1294 TRACE("hmmio=%04X, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1296 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1297 return MMSYSERR_INVALHANDLE;
1299 if (!wm->info.cchBuffer)
1300 return MMIOERR_UNBUFFERED;
1302 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1303 return MMSYSERR_INVALPARAM;
1305 if (MMIO_Flush(wm, MMIO_EMPTYBUF))
1306 return MMIOERR_CANNOTWRITE;
1308 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1310 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1311 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1312 (wm->info.pchEndRead - wm->info.pchBuffer);
1313 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1314 (wm->info.pchEndWrite - wm->info.pchBuffer);
1315 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1316 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1320 /***********************************************************************
1321 * mmioAdvance [MMSYSTEM.1219]
1323 UINT16 WINAPI mmioAdvance16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
1327 TRACE("mmioAdvance\n");
1329 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1330 return MMSYSERR_INVALHANDLE;
1332 if (!wm->info.cchBuffer)
1333 return MMIOERR_UNBUFFERED;
1335 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1336 return MMSYSERR_INVALPARAM;
1338 if (MMIO_Flush(wm, MMIO_EMPTYBUF))
1339 return MMIOERR_CANNOTWRITE;
1341 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1343 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1344 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1345 (wm->info.pchEndRead - wm->info.pchBuffer);
1346 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1347 (wm->info.pchEndWrite - wm->info.pchBuffer);
1348 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1349 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1354 /**************************************************************************
1355 * mmioStringToFOURCCA [WINMM.131]
1357 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1362 for (i = 0; i < 4 && sz[i]; i++) {
1363 if (uFlags & MMIO_TOUPPER) {
1364 cc[i] = toupper(sz[i]);
1370 /* Pad with spaces */
1376 TRACE("Got %c%c%c%c\n",cc[0],cc[1],cc[2],cc[3]);
1377 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1380 /**************************************************************************
1381 * mmioStringToFOURCCW [WINMM.132]
1383 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1385 LPSTR szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
1386 FOURCC ret = mmioStringToFOURCCA(szA,uFlags);
1388 HeapFree(GetProcessHeap(),0,szA);
1392 /**************************************************************************
1393 * mmioStringToFOURCC [MMSYSTEM.1220]
1395 FOURCC WINAPI mmioStringToFOURCC16(LPCSTR sz, UINT16 uFlags)
1397 return mmioStringToFOURCCA(sz, uFlags);
1400 /**************************************************************************
1401 * mmioInstallIOProc16 [MMSYSTEM.1221]
1403 LPMMIOPROC16 WINAPI mmioInstallIOProc16(FOURCC fccIOProc, LPMMIOPROC16 pIOProc,
1406 return (LPMMIOPROC16)MMIO_InstallIOProc(fccIOProc, (LPMMIOPROC)pIOProc,
1407 dwFlags, MMIO_PROC_16);
1410 /**************************************************************************
1411 * mmioInstallIOProcA [WINMM.120]
1413 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1414 LPMMIOPROC pIOProc, DWORD dwFlags)
1416 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1419 /**************************************************************************
1420 * mmioInstallIOProcW [WINMM.]
1422 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1423 LPMMIOPROC pIOProc, DWORD dwFlags)
1425 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1428 /**************************************************************************
1429 * mmioSendMessage16 [MMSYSTEM.1222]
1431 LRESULT WINAPI mmioSendMessage16(HMMIO16 hmmio, UINT16 uMessage,
1432 LPARAM lParam1, LPARAM lParam2)
1436 TRACE("(%04X, %u, %ld, %ld)\n", hmmio, uMessage, lParam1, lParam2);
1438 if (uMessage < MMIOM_USER)
1439 return MMSYSERR_INVALPARAM;
1441 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1442 return MMSYSERR_INVALHANDLE;
1444 return MMIO_SendMessage(wm, uMessage, lParam1, lParam2, MMIO_PROC_16);
1447 /**************************************************************************
1448 * mmioSendMessage [WINMM.]
1450 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1451 LPARAM lParam1, LPARAM lParam2)
1455 TRACE("(%04X, %u, %ld, %ld)\n", hmmio, uMessage, lParam1, lParam2);
1457 if (uMessage < MMIOM_USER)
1458 return MMSYSERR_INVALPARAM;
1460 if ((wm = MMIO_Get(NULL, hmmio)) == NULL)
1461 return MMSYSERR_INVALHANDLE;
1463 return MMIO_SendMessage(wm, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1466 /**************************************************************************
1467 * mmioDescend [MMSYSTEM.1223]
1469 UINT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1470 const MMCKINFO* lpckParent, UINT uFlags)
1477 TRACE("(%04X, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1480 return MMSYSERR_INVALPARAM;
1482 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1483 TRACE("dwOldPos=%ld\n", dwOldPos);
1485 if (lpckParent != NULL) {
1486 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1487 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1488 if (dwOldPos < lpckParent->dwDataOffset ||
1489 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1490 WARN("outside parent chunk\n");
1491 return MMIOERR_CHUNKNOTFOUND;
1495 /* The SDK docu says 'ckid' is used for all cases. Real World
1496 * examples disagree -Marcus,990216.
1500 /* find_chunk looks for 'ckid' */
1501 if (uFlags & MMIO_FINDCHUNK)
1502 srchCkId = lpck->ckid;
1503 /* find_riff and find_list look for 'fccType' */
1504 if (uFlags & MMIO_FINDLIST) {
1505 srchCkId = FOURCC_LIST;
1506 srchType = lpck->fccType;
1508 if (uFlags & MMIO_FINDRIFF) {
1509 srchCkId = FOURCC_RIFF;
1510 srchType = lpck->fccType;
1513 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1514 TRACE("searching for %.4s.%.4s\n",
1516 srchType?(LPSTR)&srchType:"any ");
1521 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1522 if (ix < 2*sizeof(DWORD)) {
1523 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1524 WARN("return ChunkNotFound\n");
1525 return MMIOERR_CHUNKNOTFOUND;
1527 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1528 if (ix < lpck->dwDataOffset - dwOldPos) {
1529 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1530 WARN("return ChunkNotFound\n");
1531 return MMIOERR_CHUNKNOTFOUND;
1533 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1535 srchType?(LPSTR)&lpck->fccType:"<na>",
1537 if ((srchCkId == lpck->ckid) &&
1538 (!srchType || (srchType == lpck->fccType))
1542 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1543 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1546 /* FIXME: unverified, does it do this? */
1547 /* NB: This part is used by WAVE_mciOpen, among others */
1548 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1549 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1550 WARN("return ChunkNotFound 2nd\n");
1551 return MMIOERR_CHUNKNOTFOUND;
1553 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1556 /* If we were looking for RIFF/LIST chunks, the final file position
1557 * is after the chunkid. If we were just looking for the chunk
1558 * it is after the cksize. So add 4 in RIFF/LIST case.
1560 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1561 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1563 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1564 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1565 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1566 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1570 /**************************************************************************
1571 * mmioDescend16 [MMSYSTEM.1223]
1573 UINT16 WINAPI mmioDescend16(HMMIO16 hmmio, LPMMCKINFO lpck,
1574 const MMCKINFO* lpckParent, UINT16 uFlags)
1576 return mmioDescend(hmmio, lpck, lpckParent, uFlags);
1579 /**************************************************************************
1580 * mmioAscend [WINMM.113]
1582 UINT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1584 TRACE("(%04X, %p, %04X);\n", hmmio, lpck, uFlags);
1586 if (lpck->dwFlags & MMIO_DIRTY) {
1587 DWORD dwOldPos, dwNewSize, dwSizePos;
1589 TRACE("chunk is marked MMIO_DIRTY, correcting chunk size\n");
1590 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1591 TRACE("dwOldPos=%ld\n", dwOldPos);
1592 dwNewSize = dwOldPos - lpck->dwDataOffset;
1593 if (dwNewSize != lpck->cksize) {
1594 TRACE("dwNewSize=%ld\n", dwNewSize);
1595 lpck->cksize = dwNewSize;
1597 dwSizePos = lpck->dwDataOffset - sizeof(DWORD);
1598 TRACE("dwSizePos=%ld\n", dwSizePos);
1600 mmioSeek(hmmio, dwSizePos, SEEK_SET);
1601 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1605 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1610 /**************************************************************************
1611 * mmioAscend [MMSYSTEM.1224]
1613 UINT16 WINAPI mmioAscend16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
1615 return mmioAscend(hmmio,lpck,uFlags);
1618 /**************************************************************************
1619 * mmioCreateChunk [MMSYSTEM.1225]
1621 UINT16 WINAPI mmioCreateChunk16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
1627 TRACE("(%04X, %p, %04X);\n", hmmio, lpck, uFlags);
1629 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1630 TRACE("dwOldPos=%ld\n", dwOldPos);
1632 if (uFlags == MMIO_CREATELIST)
1633 lpck->ckid = FOURCC_LIST;
1634 else if (uFlags == MMIO_CREATERIFF)
1635 lpck->ckid = FOURCC_RIFF;
1637 TRACE("ckid=%08lX\n", lpck->ckid);
1639 size = 2 * sizeof(DWORD);
1640 lpck->dwDataOffset = dwOldPos + size;
1642 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1643 size += sizeof(DWORD);
1644 lpck->dwFlags = MMIO_DIRTY;
1646 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1647 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1649 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1650 WARN("return CannotWrite\n");
1651 return MMIOERR_CANNOTWRITE;
1657 /**************************************************************************
1658 * mmioCreateChunk [WINMM.115]
1660 UINT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1662 return mmioCreateChunk16(hmmio, lpck, uFlags);
1665 /**************************************************************************
1666 * mmioRename [MMSYSTEM.1226]
1668 UINT16 WINAPI mmioRename16(LPCSTR szFileName, LPCSTR szNewFileName,
1669 MMIOINFO16* lpmmioinfo, DWORD dwRenameFlags)
1671 UINT16 result = MMSYSERR_ERROR;
1672 LPMMIOPROC16 ioProc;
1674 TRACE("('%s', '%s', %p, %08lX);\n",
1675 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1677 /* If both params are NULL, then parse the file name */
1678 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1679 lpmmioinfo->fccIOProc = MMIO_ParseExt(szFileName);
1681 /* Handle any unhandled/error case from above. Assume DOS file */
1682 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1683 ioProc = (LPMMIOPROC16)mmioDosIOProc;
1684 /* if just the four character code is present, look up IO proc */
1685 else if (lpmmioinfo->pIOProc == NULL)
1686 ioProc = mmioInstallIOProc16(lpmmioinfo->fccIOProc, NULL, MMIO_FINDPROC);
1688 ioProc = lpmmioinfo->pIOProc;
1690 /* FIXME: ioProc is likely a segmented address, thus needing a
1691 * thunk somewhere. The main issue is that Wine's current thunking
1692 * 32 to 16 only supports pascal calling convention
1695 result = (ioProc)(0, MMIOM_RENAME,
1696 (LPARAM)szFileName, (LPARAM)szNewFileName);
1701 /**************************************************************************
1702 * mmioRenameA [WINMM.125]
1704 UINT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1705 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1707 UINT result = MMSYSERR_ERROR;
1710 TRACE("('%s', '%s', %p, %08lX);\n",
1711 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1713 /* If both params are NULL, then parse the file name */
1714 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1715 lpmmioinfo->fccIOProc = MMIO_ParseExt(szFileName);
1717 /* Handle any unhandled/error case from above. Assume DOS file */
1718 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1719 ioProc = (LPMMIOPROC)mmioDosIOProc;
1720 /* if just the four character code is present, look up IO proc */
1721 else if (lpmmioinfo->pIOProc == NULL)
1722 ioProc = MMIO_InstallIOProc(lpmmioinfo->fccIOProc, NULL,
1723 MMIO_FINDPROC, MMIO_PROC_32A);
1724 else /* use relevant ioProc */
1725 ioProc = lpmmioinfo->pIOProc;
1728 result = (ioProc)(0, MMIOM_RENAME,
1729 (LPARAM)szFileName, (LPARAM)szNewFileName);
1734 /**************************************************************************
1735 * mmioRenameW [WINMM.126]
1737 UINT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1738 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1740 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1741 LPSTR sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1742 UINT ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwRenameFlags);
1744 HeapFree(GetProcessHeap(),0,szFn);
1745 HeapFree(GetProcessHeap(),0,sznFn);