J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | //#define USE_TRACE |
| 27 | #define USE_ERROR |
| 28 | |
| 29 | |
| 30 | #include <jni.h> |
| 31 | #include "SoundDefs.h" |
| 32 | #include "Ports.h" |
| 33 | #include "Utilities.h" |
| 34 | #include "com_sun_media_sound_PortMixer.h" |
| 35 | |
| 36 | |
| 37 | //////////////////////////////////////////// PortMixer //////////////////////////////////////////// |
| 38 | |
| 39 | JNIEXPORT jlong JNICALL Java_com_sun_media_sound_PortMixer_nOpen |
| 40 | (JNIEnv *env, jclass cls, jint mixerIndex) { |
| 41 | |
| 42 | jlong ret = 0; |
| 43 | #if USE_PORTS == TRUE |
| 44 | ret = (jlong) (INT_PTR) PORT_Open(mixerIndex); |
| 45 | #endif |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nClose |
| 50 | (JNIEnv *env, jclass cls, jlong id) { |
| 51 | |
| 52 | #if USE_PORTS == TRUE |
| 53 | if (id != 0) { |
| 54 | PORT_Close((void*) (INT_PTR) id); |
| 55 | } |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | JNIEXPORT jint JNICALL Java_com_sun_media_sound_PortMixer_nGetPortCount |
| 60 | (JNIEnv *env, jclass cls, jlong id) { |
| 61 | |
| 62 | jint ret = 0; |
| 63 | #if USE_PORTS == TRUE |
| 64 | if (id != 0) { |
| 65 | ret = (jint) PORT_GetPortCount((void*) (INT_PTR) id); |
| 66 | } |
| 67 | #endif |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | JNIEXPORT jint JNICALL Java_com_sun_media_sound_PortMixer_nGetPortType |
| 73 | (JNIEnv *env, jclass cls, jlong id, jint portIndex) { |
| 74 | |
| 75 | jint ret = 0; |
| 76 | TRACE1("Java_com_sun_media_sound_PortMixer_nGetPortType(%d).\n", portIndex); |
| 77 | |
| 78 | #if USE_PORTS == TRUE |
| 79 | if (id != 0) { |
| 80 | ret = (jint) PORT_GetPortType((void*) (INT_PTR) id, portIndex); |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | TRACE1("Java_com_sun_media_sound_PortMixerProvider_nGetPortType returning %d.\n", ret); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | JNIEXPORT jstring JNICALL Java_com_sun_media_sound_PortMixer_nGetPortName |
| 89 | (JNIEnv *env, jclass cls, jlong id, jint portIndex) { |
| 90 | |
| 91 | char str[PORT_STRING_LENGTH]; |
| 92 | jstring jString = NULL; |
| 93 | TRACE1("Java_com_sun_media_sound_PortMixer_nGetPortName(%d).\n", portIndex); |
| 94 | |
| 95 | str[0] = 0; |
| 96 | #if USE_PORTS == TRUE |
| 97 | if (id != 0) { |
| 98 | PORT_GetPortName((void*) (INT_PTR) id, portIndex, str, PORT_STRING_LENGTH); |
| 99 | } |
| 100 | #endif |
| 101 | jString = (*env)->NewStringUTF(env, str); |
| 102 | |
| 103 | TRACE1("Java_com_sun_media_sound_PortMixerProvider_nGetName returning \"%s\".\n", str); |
| 104 | return jString; |
| 105 | } |
| 106 | |
| 107 | JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nControlSetIntValue |
| 108 | (JNIEnv *env, jclass cls, jlong controlID, jint value) { |
| 109 | #if USE_PORTS == TRUE |
| 110 | if (controlID != 0) { |
| 111 | PORT_SetIntValue((void*) (UINT_PTR) controlID, (INT32) value); |
| 112 | } |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | JNIEXPORT jint JNICALL Java_com_sun_media_sound_PortMixer_nControlGetIntValue |
| 117 | (JNIEnv *env, jclass cls, jlong controlID) { |
| 118 | INT32 ret = 0; |
| 119 | #if USE_PORTS == TRUE |
| 120 | if (controlID != 0) { |
| 121 | ret = PORT_GetIntValue((void*) (UINT_PTR) controlID); |
| 122 | } |
| 123 | #endif |
| 124 | return (jint) ret; |
| 125 | } |
| 126 | |
| 127 | JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nControlSetFloatValue |
| 128 | (JNIEnv *env, jclass cls, jlong controlID, jfloat value) { |
| 129 | #if USE_PORTS == TRUE |
| 130 | if (controlID != 0) { |
| 131 | PORT_SetFloatValue((void*) (UINT_PTR) controlID, (float) value); |
| 132 | } |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | JNIEXPORT jfloat JNICALL Java_com_sun_media_sound_PortMixer_nControlGetFloatValue |
| 137 | (JNIEnv *env, jclass cls, jlong controlID) { |
| 138 | float ret = 0; |
| 139 | #if USE_PORTS == TRUE |
| 140 | if (controlID != 0) { |
| 141 | ret = PORT_GetFloatValue((void*) (UINT_PTR) controlID); |
| 142 | } |
| 143 | #endif |
| 144 | return (jfloat) ret; |
| 145 | } |
| 146 | |
| 147 | /* ************************************** native control creation support ********************* */ |
| 148 | |
| 149 | // contains all the needed references so that the platform dependent code can call JNI wrapper functions |
| 150 | typedef struct tag_ControlCreatorJNI { |
| 151 | // this member is seen by the platform dependent code |
| 152 | PortControlCreator creator; |
| 153 | // general JNI variables |
| 154 | JNIEnv *env; |
| 155 | // the vector to be filled with controls (initialized before usage) |
| 156 | jobject vector; |
| 157 | jmethodID vectorAddElement; |
| 158 | // control specific constructors (initialized on demand) |
| 159 | jclass boolCtrlClass; |
| 160 | jmethodID boolCtrlConstructor; // signature (JLjava/lang/String;)V |
| 161 | jclass controlClass; // class of javax.sound.sampled.Control |
| 162 | jclass compCtrlClass; |
| 163 | jmethodID compCtrlConstructor; // signature (Ljava/lang/String;[Ljavax/sound/sampled/Control;)V |
| 164 | jclass floatCtrlClass; |
| 165 | jmethodID floatCtrlConstructor1; // signature (JLjava/lang/String;FFFLjava/lang/String;)V |
| 166 | jmethodID floatCtrlConstructor2; // signature (JIFFFLjava/lang/String;)V |
| 167 | } ControlCreatorJNI; |
| 168 | |
| 169 | |
| 170 | void* PORT_NewBooleanControl(void* creatorV, void* controlID, char* type) { |
| 171 | ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV; |
| 172 | jobject ctrl = NULL; |
| 173 | |
| 174 | #ifdef USE_TRACE |
| 175 | if (((UINT_PTR) type) <= CONTROL_TYPE_MAX) { |
| 176 | TRACE1("PORT_NewBooleanControl: creating '%d'\n", (int) (UINT_PTR) type); |
| 177 | } else { |
| 178 | TRACE1("PORT_NewBooleanControl: creating '%s'\n", type); |
| 179 | } |
| 180 | #endif |
| 181 | if (!creator->boolCtrlClass) { |
| 182 | // retrieve class and constructor of PortMixer.BoolCtrl |
| 183 | creator->boolCtrlClass = (*creator->env)->FindClass(creator->env, IMPLEMENTATION_PACKAGE_NAME"/PortMixer$BoolCtrl"); |
| 184 | if (!creator->boolCtrlClass) { |
| 185 | ERROR0("PORT_NewBooleanControl: boolCtrlClass is NULL\n"); |
| 186 | return NULL; |
| 187 | } |
| 188 | creator->boolCtrlConstructor = (*creator->env)->GetMethodID(creator->env, creator->boolCtrlClass, |
| 189 | "<init>", "(JLjava/lang/String;)V"); |
| 190 | if (!creator->boolCtrlConstructor) { |
| 191 | ERROR0("PORT_NewBooleanControl: boolCtrlConstructor is NULL\n"); |
| 192 | return NULL; |
| 193 | } |
| 194 | } |
| 195 | if (type == CONTROL_TYPE_MUTE) { |
| 196 | type = "Mute"; |
| 197 | } |
| 198 | else if (type == CONTROL_TYPE_SELECT) { |
| 199 | type = "Select"; |
| 200 | } |
| 201 | |
| 202 | ctrl = (*creator->env)->NewObject(creator->env, creator->boolCtrlClass, creator->boolCtrlConstructor, |
| 203 | (jlong) (UINT_PTR) controlID, (*creator->env)->NewStringUTF(creator->env, type)); |
| 204 | if (!ctrl) { |
| 205 | ERROR0("PORT_NewBooleanControl: ctrl is NULL\n"); |
| 206 | } |
| 207 | if ((*creator->env)->ExceptionOccurred(creator->env)) { |
| 208 | ERROR0("PORT_NewBooleanControl: ExceptionOccurred!\n"); |
| 209 | } |
| 210 | TRACE0("PORT_NewBooleanControl succeeded\n"); |
| 211 | return (void*) ctrl; |
| 212 | } |
| 213 | |
| 214 | void* PORT_NewCompoundControl(void* creatorV, char* type, void** controls, int controlCount) { |
| 215 | ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV; |
| 216 | jobject ctrl = NULL; |
| 217 | jobjectArray controlArray; |
| 218 | int i; |
| 219 | |
| 220 | TRACE2("PORT_NewCompoundControl: creating '%s' with %d controls\n", type, controlCount); |
| 221 | if (!creator->compCtrlClass) { |
| 222 | TRACE0("PORT_NewCompoundControl: retrieve method ids\n"); |
| 223 | // retrieve class and constructor of PortMixer.BoolCtrl |
| 224 | creator->compCtrlClass = (*creator->env)->FindClass(creator->env, IMPLEMENTATION_PACKAGE_NAME"/PortMixer$CompCtrl"); |
| 225 | if (!creator->compCtrlClass) { |
| 226 | ERROR0("PORT_NewCompoundControl: compCtrlClass is NULL\n"); |
| 227 | return NULL; |
| 228 | } |
| 229 | creator->compCtrlConstructor = (*creator->env)->GetMethodID(creator->env, creator->compCtrlClass, |
| 230 | "<init>", "(Ljava/lang/String;[Ljavax/sound/sampled/Control;)V"); |
| 231 | if (!creator->compCtrlConstructor) { |
| 232 | ERROR0("PORT_NewCompoundControl: compCtrlConstructor is NULL\n"); |
| 233 | return NULL; |
| 234 | } |
| 235 | creator->controlClass = (*creator->env)->FindClass(creator->env, JAVA_SAMPLED_PACKAGE_NAME"/Control"); |
| 236 | if (!creator->controlClass) { |
| 237 | ERROR0("PORT_NewCompoundControl: controlClass is NULL\n"); |
| 238 | return NULL; |
| 239 | } |
| 240 | } |
| 241 | TRACE0("PORT_NewCompoundControl: creating array\n"); |
| 242 | // create new array for the controls |
| 243 | controlArray = (*creator->env)->NewObjectArray(creator->env, controlCount, creator->controlClass, (jobject) NULL); |
| 244 | if (!controlArray) { |
| 245 | ERROR0("PORT_NewCompoundControl: controlArray is NULL\n"); |
| 246 | return NULL; |
| 247 | } |
| 248 | TRACE0("PORT_NewCompoundControl: setting array values\n"); |
| 249 | for (i = 0; i < controlCount; i++) { |
| 250 | (*creator->env)->SetObjectArrayElement(creator->env, controlArray, i, (jobject) controls[i]); |
| 251 | } |
| 252 | TRACE0("PORT_NewCompoundControl: creating compound control\n"); |
| 253 | ctrl = (*creator->env)->NewObject(creator->env, creator->compCtrlClass, creator->compCtrlConstructor, |
| 254 | (*creator->env)->NewStringUTF(creator->env, type), controlArray); |
| 255 | if (!ctrl) { |
| 256 | ERROR0("PORT_NewCompoundControl: ctrl is NULL\n"); |
| 257 | } |
| 258 | if ((*creator->env)->ExceptionOccurred(creator->env)) { |
| 259 | ERROR0("PORT_NewCompoundControl: ExceptionOccurred!\n"); |
| 260 | } |
| 261 | TRACE0("PORT_NewCompoundControl succeeded\n"); |
| 262 | return (void*) ctrl; |
| 263 | } |
| 264 | |
| 265 | void* PORT_NewFloatControl(void* creatorV, void* controlID, char* type, |
| 266 | float min, float max, float precision, char* units) { |
| 267 | ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV; |
| 268 | jobject ctrl = NULL; |
| 269 | |
| 270 | #ifdef USE_TRACE |
| 271 | if (((UINT_PTR) type) <= CONTROL_TYPE_MAX) { |
| 272 | TRACE1("PORT_NewFloatControl: creating '%d'\n", (int) (UINT_PTR) type); |
| 273 | } else { |
| 274 | TRACE1("PORT_NewFloatControl: creating '%s'\n", type); |
| 275 | } |
| 276 | #endif |
| 277 | if (!creator->floatCtrlClass) { |
| 278 | // retrieve class and constructor of PortMixer.BoolCtrl |
| 279 | creator->floatCtrlClass = (*creator->env)->FindClass(creator->env, IMPLEMENTATION_PACKAGE_NAME"/PortMixer$FloatCtrl"); |
| 280 | if (!creator->floatCtrlClass) { |
| 281 | ERROR0("PORT_NewFloatControl: floatCtrlClass is NULL\n"); |
| 282 | return NULL; |
| 283 | } |
| 284 | creator->floatCtrlConstructor1 = (*creator->env)->GetMethodID(creator->env, creator->floatCtrlClass, |
| 285 | "<init>", "(JLjava/lang/String;FFFLjava/lang/String;)V"); |
| 286 | if (!creator->floatCtrlConstructor1) { |
| 287 | ERROR0("PORT_NewFloatControl: floatCtrlConstructor1 is NULL\n"); |
| 288 | return NULL; |
| 289 | } |
| 290 | creator->floatCtrlConstructor2 = (*creator->env)->GetMethodID(creator->env, creator->floatCtrlClass, |
| 291 | "<init>", "(JIFFFLjava/lang/String;)V"); |
| 292 | if (!creator->floatCtrlConstructor2) { |
| 293 | ERROR0("PORT_NewFloatControl: floatCtrlConstructor2 is NULL\n"); |
| 294 | return NULL; |
| 295 | } |
| 296 | } |
| 297 | if (((UINT_PTR) type) <= CONTROL_TYPE_MAX) { |
| 298 | // constructor with int parameter |
| 299 | TRACE1("PORT_NewFloatControl: calling constructor2 with type %d\n", (int) (UINT_PTR) type); |
| 300 | ctrl = (*creator->env)->NewObject(creator->env, creator->floatCtrlClass, creator->floatCtrlConstructor2, |
| 301 | (jlong) (UINT_PTR) controlID, (jint) (UINT_PTR) type, |
| 302 | min, max, precision, (*creator->env)->NewStringUTF(creator->env, units)); |
| 303 | } else { |
| 304 | TRACE0("PORT_NewFloatControl: calling constructor1\n"); |
| 305 | // constructor with string parameter |
| 306 | ctrl = (*creator->env)->NewObject(creator->env, creator->floatCtrlClass, creator->floatCtrlConstructor1, |
| 307 | (jlong) (UINT_PTR) controlID, (*creator->env)->NewStringUTF(creator->env, type), |
| 308 | min, max, precision, (*creator->env)->NewStringUTF(creator->env, units)); |
| 309 | } |
| 310 | if (!ctrl) { |
| 311 | ERROR0("PORT_NewFloatControl: ctrl is NULL!\n"); |
| 312 | } |
| 313 | if ((*creator->env)->ExceptionOccurred(creator->env)) { |
| 314 | ERROR0("PORT_NewFloatControl: ExceptionOccurred!\n"); |
| 315 | } |
| 316 | TRACE1("PORT_NewFloatControl succeeded %p\n", (void*) ctrl); |
| 317 | return (void*) ctrl; |
| 318 | } |
| 319 | |
| 320 | int PORT_AddControl(void* creatorV, void* control) { |
| 321 | ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV; |
| 322 | |
| 323 | TRACE1("PORT_AddControl %p\n", (void*) control); |
| 324 | (*creator->env)->CallVoidMethod(creator->env, creator->vector, creator->vectorAddElement, (jobject) control); |
| 325 | if ((*creator->env)->ExceptionOccurred(creator->env)) { |
| 326 | ERROR0("PORT_AddControl: ExceptionOccurred!\n"); |
| 327 | } |
| 328 | TRACE0("PORT_AddControl succeeded\n"); |
| 329 | return TRUE; |
| 330 | } |
| 331 | |
| 332 | JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nGetControls |
| 333 | (JNIEnv *env, jclass cls, jlong id, jint portIndex, jobject vector) { |
| 334 | |
| 335 | ControlCreatorJNI creator; |
| 336 | jclass vectorClass; |
| 337 | |
| 338 | #if USE_PORTS == TRUE |
| 339 | if (id != 0) { |
| 340 | memset(&creator, 0, sizeof(ControlCreatorJNI)); |
| 341 | creator.creator.newBooleanControl = &PORT_NewBooleanControl; |
| 342 | creator.creator.newCompoundControl = &PORT_NewCompoundControl; |
| 343 | creator.creator.newFloatControl = &PORT_NewFloatControl; |
| 344 | creator.creator.addControl = &PORT_AddControl; |
| 345 | creator.env = env; |
| 346 | vectorClass = (*env)->GetObjectClass(env, vector); |
| 347 | if (vectorClass == NULL) { |
| 348 | ERROR0("Java_com_sun_media_sound_PortMixer_nGetControls: vectorClass is NULL\n"); |
| 349 | return; |
| 350 | } |
| 351 | creator.vector = vector; |
| 352 | creator.vectorAddElement = (*env)->GetMethodID(env, vectorClass, "addElement", "(Ljava/lang/Object;)V"); |
| 353 | if (creator.vectorAddElement == NULL) { |
| 354 | ERROR0("Java_com_sun_media_sound_PortMixer_nGetControls: addElementMethodID is NULL\n"); |
| 355 | return; |
| 356 | } |
| 357 | PORT_GetControls((void*) (UINT_PTR) id, (INT32) portIndex, (PortControlCreator*) &creator); |
| 358 | } |
| 359 | #endif |
| 360 | } |