Path: blob/master/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Util.c
41149 views
/*1* Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526#define USE_ERROR27//#define USE_TRACE2829#include "PLATFORM_API_WinOS_Util.h"3031#if (USE_PLATFORM_MIDI_IN == TRUE) || (USE_PLATFORM_MIDI_OUT == TRUE)3233/* set the startTime field in MidiDeviceHandle */34void MIDI_SetStartTime(MidiDeviceHandle* handle) {35if (handle != NULL) {36handle->startTime = (INT64) timeGetTime();37}38}394041/* return time stamp in microseconds */42INT64 MIDI_GetTimeStamp(MidiDeviceHandle* handle) {43INT64 res;44if (handle == NULL) {45return (INT64) -1;46}47res = ((INT64) timeGetTime()) - handle->startTime;48if (res < 0) {49res *= (INT64) -1000;50} else {51res *= (INT64) 1000;52}53return res;54}555657void* MIDI_CreateLock() {58CRITICAL_SECTION* lock = (CRITICAL_SECTION*) malloc(sizeof(CRITICAL_SECTION));59InitializeCriticalSection(lock);60TRACE0("MIDI_CreateLock\n");61return lock;62}6364void MIDI_DestroyLock(void* lock) {65if (lock) {66DeleteCriticalSection((CRITICAL_SECTION*) lock);67free(lock);68TRACE0("MIDI_DestroyLock\n");69}70}7172void MIDI_Lock(void* lock) {73if (lock) {74EnterCriticalSection((CRITICAL_SECTION*) lock);75}76}7778void MIDI_Unlock(void* lock) {79if (lock) {80LeaveCriticalSection((CRITICAL_SECTION*) lock);81}82}83int MIDI_WinCreateEmptyLongBufferQueue(MidiDeviceHandle* handle, int count) {84return MIDI_WinCreateLongBufferQueue(handle, count, 0, NULL);85}8687int MIDI_WinCreateLongBufferQueue(MidiDeviceHandle* handle, int count, int size, UBYTE* preAllocatedMem) {88SysExQueue* sysex;89int i;90UBYTE* dataPtr;91int structSize = sizeof(SysExQueue) + ((count - 1) * sizeof(MIDIHDR));9293sysex = (SysExQueue*) malloc(structSize);94if (!sysex) return FALSE;95memset(sysex, 0, structSize);96sysex->count = count;97sysex->size = size;9899// prepare memory block which will contain the actual data100if (!preAllocatedMem && size > 0) {101preAllocatedMem = (UBYTE*) malloc(count*size);102if (!preAllocatedMem) {103free(sysex);104return FALSE;105}106sysex->ownsLinearMem = 1;107}108sysex->linearMem = preAllocatedMem;109handle->longBuffers = sysex;110111// set up headers112dataPtr = preAllocatedMem;113for (i=0; i<count; i++) {114sysex->header[i].lpData = dataPtr;115sysex->header[i].dwBufferLength = size;116// user data is the index of the buffer117sysex->header[i].dwUser = (DWORD) i;118dataPtr += size;119}120return TRUE;121}122123void MIDI_WinDestroyLongBufferQueue(MidiDeviceHandle* handle) {124SysExQueue* sysex = (SysExQueue*) handle->longBuffers;125if (sysex) {126handle->longBuffers = NULL;127if (sysex->ownsLinearMem && sysex->linearMem) {128free(sysex->linearMem);129}130free(sysex);131}132}133134#endif // USE_PLATFORM_MIDI_IN || USE_PLATFORM_MIDI_OUT135136137