Path: blob/master/src/java.desktop/share/native/libjsound/PlatformMidi.c
41149 views
/*1* Copyright (c) 2002, 2015, 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 "PlatformMidi.h"3031#include <stdlib.h>3233char* GetInternalErrorStr(INT32 err) {34switch (err) {35case MIDI_SUCCESS: return "";36case MIDI_NOT_SUPPORTED: return "feature not supported";37case MIDI_INVALID_DEVICEID: return "invalid device ID";38case MIDI_INVALID_HANDLE: return "internal error: invalid handle";39case MIDI_OUT_OF_MEMORY: return "out of memory";40}41return NULL;42}4344/*45* internal implementation for getting error string46*/47char* MIDI_IN_InternalGetErrorString(INT32 err) {48char* result = GetInternalErrorStr(err);4950#if USE_PLATFORM_MIDI_IN == TRUE51if (!result) {52result = MIDI_IN_GetErrorStr(err);53}54#endif55if (!result) {56result = GetInternalErrorStr(MIDI_NOT_SUPPORTED);57}58return result;59}6061/*62* internal implementation for getting error string63*/64char* MIDI_OUT_InternalGetErrorString(INT32 err) {65char* result = GetInternalErrorStr(err);6667#if USE_PLATFORM_MIDI_OUT == TRUE68if (!result) {69result = MIDI_OUT_GetErrorStr(err);70}71#endif72if (!result) {73result = GetInternalErrorStr(MIDI_NOT_SUPPORTED);74}75return result;76}777879#if USE_MIDI_QUEUE == TRUE8081// MessageQueue implementation8283MidiMessageQueue* MIDI_CreateQueue(int capacity) {84MidiMessageQueue* queue = (MidiMessageQueue*) malloc(sizeof(MidiMessageQueue) + ((capacity-1) * sizeof(MidiMessage)));85if (queue) {86TRACE0("MIDI_CreateQueue\n");87queue->lock = MIDI_CreateLock();88queue->capacity = capacity;89queue->size = 0;90queue->readIndex = 0;91queue->writeIndex = 0;92}93return queue;94}9596void MIDI_DestroyQueue(MidiMessageQueue* queue) {97if (queue) {98void* lock = queue->lock;99MIDI_Lock(lock);100free(queue);101MIDI_Unlock(lock);102MIDI_DestroyLock(lock);103TRACE0("MIDI_DestroyQueue\n");104}105}106107// if overwrite is true, oldest messages will be overwritten when the queue is full108// returns true, if message has been added109int MIDI_QueueAddShort(MidiMessageQueue* queue, UINT32 packedMsg, INT64 timestamp, int overwrite) {110if (queue) {111MIDI_Lock(queue->lock);112if (queue->size == queue->capacity) {113TRACE0("MIDI_QueueAddShort: overflow\n");114if (!overwrite || queue->queue[queue->writeIndex].locked) {115return FALSE; // failed116}117// adjust overwritten readIndex118queue->readIndex = (queue->readIndex+1) % queue->capacity;119} else {120queue->size++;121}122TRACE2("MIDI_QueueAddShort. index=%d, size=%d\n", queue->writeIndex, queue->size);123queue->queue[queue->writeIndex].type = SHORT_MESSAGE;124queue->queue[queue->writeIndex].data.s.packedMsg = packedMsg;125queue->queue[queue->writeIndex].timestamp = timestamp;126queue->writeIndex = (queue->writeIndex+1) % queue->capacity;127MIDI_Unlock(queue->lock);128return TRUE;129}130return FALSE;131}132133int MIDI_QueueAddLong(MidiMessageQueue* queue, UBYTE* data, UINT32 size,134INT32 sysexIndex, INT64 timestamp, int overwrite) {135if (queue) {136MIDI_Lock(queue->lock);137if (queue->size == queue->capacity) {138TRACE0("MIDI_QueueAddLong: overflow\n");139if (!overwrite || queue->queue[queue->writeIndex].locked) {140return FALSE; // failed141}142// adjust overwritten readIndex143queue->readIndex = (queue->readIndex+1) % queue->capacity;144} else {145queue->size++;146}147TRACE2("MIDI_QueueAddLong. index=%d, size=%d\n", queue->writeIndex, queue->size);148//fprintf(stdout, "MIDI_QueueAddLong sysex-index %d\n", sysexIndex); fflush(stdout);149queue->queue[queue->writeIndex].type = LONG_MESSAGE;150queue->queue[queue->writeIndex].data.l.size = size;151queue->queue[queue->writeIndex].data.l.data = data;152queue->queue[queue->writeIndex].data.l.index = sysexIndex;153queue->queue[queue->writeIndex].timestamp = timestamp;154queue->writeIndex = (queue->writeIndex+1) % queue->capacity;155MIDI_Unlock(queue->lock);156return TRUE;157}158return FALSE;159}160161// returns NULL if no messages in queue.162MidiMessage* MIDI_QueueRead(MidiMessageQueue* queue) {163MidiMessage* msg = NULL;164if (queue) {165MIDI_Lock(queue->lock);166if (queue->size > 0) {167msg = &(queue->queue[queue->readIndex]);168TRACE2("MIDI_QueueRead. index=%d, size=%d\n", queue->readIndex, queue->size);169msg->locked = TRUE;170}171MIDI_Unlock(queue->lock);172}173return msg;174}175176void MIDI_QueueRemove(MidiMessageQueue* queue, INT32 onlyLocked) {177if (queue) {178MIDI_Lock(queue->lock);179if (queue->size > 0) {180MidiMessage* msg = &(queue->queue[queue->readIndex]);181if (!onlyLocked || msg->locked) {182TRACE2("MIDI_QueueRemove. index=%d, size=%d\n", queue->readIndex, queue->size);183queue->readIndex = (queue->readIndex+1) % queue->capacity;184queue->size--;185}186msg->locked = FALSE;187}188MIDI_Unlock(queue->lock);189}190}191192void MIDI_QueueClear(MidiMessageQueue* queue) {193if (queue) {194MIDI_Lock(queue->lock);195queue->size = 0;196queue->readIndex = 0;197queue->writeIndex = 0;198MIDI_Unlock(queue->lock);199}200}201202#endif203204205