1 /* 2 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import java.util.StringTokenizer; 29 30 31 32 /** 33 * Audio configuration class for exposing attributes specific to the platform or system. 34 * 35 * @author Kara Kytle 36 * @author Florian Bomers 37 */ 38 class Platform { 39 40 41 // STATIC FINAL CHARACTERISTICS 42 43 // native library we need to load 44 private static final String libNameMain = "jsound"; 45 private static final String libNameALSA = "jsoundalsa"; 46 private static final String libNameDSound = "jsoundds"; 47 48 // extra libs handling: bit flags for each different library 49 public static final int LIB_MAIN = 1; 50 public static final int LIB_ALSA = 2; 51 public static final int LIB_DSOUND = 4; 52 53 // bit field of the constants above. Willbe set in loadLibraries 54 private static int loadedLibs = 0; 55 56 // features: the main native library jsound reports which feature is 57 // contained in which lib 58 public static final int FEATURE_MIDIIO = 1; 59 public static final int FEATURE_PORTS = 2; 60 public static final int FEATURE_DIRECT_AUDIO = 3; 61 62 // SYSTEM CHARACTERISTICS 63 // vary according to hardware architecture 64 65 // signed8 (use signed 8-bit values) is true for everything we support except for 66 // the solaris sbpro card. 67 // we'll leave it here as a variable; in the future we may need this in java. 68 // wait, is that true? i'm not sure. i think solaris takes unsigned data? 69 // $$kk: 03.11.99: i think solaris takes unsigned 8-bit or signed 16-bit data.... 70 private static boolean signed8; 71 72 // intel is little-endian. sparc is big-endian. 73 private static boolean bigEndian; 74 75 // this is the value of the "java.home" system property. i am looking it up here 76 // for use when trying to load the soundbank, just so 77 // that all the privileged code is localized in this file.... 78 private static String javahome; 79 80 // this is the value of the "java.class.path" system property 81 private static String classpath; 82 83 84 85 86 static { 87 if(Printer.trace)Printer.trace(">> Platform.java: static"); 88 89 loadLibraries(); 90 readProperties(); 91 } 92 93 94 /** 95 * Private constructor. 96 */ 97 private Platform() { 98 } 99 100 101 // METHODS FOR INTERNAL IMPLEMENTATION USE 102 103 104 /** 105 * Dummy method for forcing initialization. 106 */ 107 static void initialize() { 108 109 if(Printer.trace)Printer.trace("Platform: initialize()"); 110 } 111 112 113 /** 114 * Determine whether the system is big-endian. 115 */ 116 static boolean isBigEndian() { 117 118 return bigEndian; 119 } 120 121 122 /** 123 * Determine whether the system takes signed 8-bit data. 124 */ 125 static boolean isSigned8() { 126 127 return signed8; 128 } 129 130 131 /** 132 * Obtain javahome. 133 * $$kk: 04.16.99: this is *bad*!! 134 */ 135 static String getJavahome() { 136 137 return javahome; 138 } 139 140 /** 141 * Obtain classpath. 142 * $$jb: 04.21.99: this is *bad* too!! 143 */ 144 static String getClasspath() { 145 146 return classpath; 147 } 148 149 150 // PRIVATE METHODS 151 152 /** 153 * Load the native library or libraries. 154 */ 155 private static void loadLibraries() { 156 if(Printer.trace)Printer.trace(">>Platform.loadLibraries"); 157 158 try { 159 // load the main library 160 JSSecurityManager.loadLibrary(libNameMain); 161 // just for the heck of it... 162 loadedLibs |= LIB_MAIN; 163 } catch (SecurityException e) { 164 if(Printer.err)Printer.err("Security exception loading main native library. JavaSound requires access to these resources."); 165 throw(e); 166 } 167 168 // now try to load extra libs. They are defined at compile time in the Makefile 169 // with the define EXTRA_SOUND_JNI_LIBS 170 String extraLibs = nGetExtraLibraries(); 171 // the string is the libraries, separated by white space 172 StringTokenizer st = new StringTokenizer(extraLibs); 173 while (st.hasMoreTokens()) { 174 String lib = st.nextToken(); 175 try { 176 JSSecurityManager.loadLibrary(lib); 177 if (lib.equals(libNameALSA)) { 178 loadedLibs |= LIB_ALSA; 179 if (Printer.debug) Printer.debug("Loaded ALSA lib successfully."); 180 } else if (lib.equals(libNameDSound)) { 181 loadedLibs |= LIB_DSOUND; 182 if (Printer.debug) Printer.debug("Loaded DirectSound lib successfully."); 183 } else { 184 if (Printer.err) Printer.err("Loaded unknown lib '"+lib+"' successfully."); 185 } 186 } catch (Throwable t) { 187 if (Printer.err) Printer.err("Couldn't load library "+lib+": "+t.toString()); 188 } 189 } 190 } 191 192 193 static boolean isMidiIOEnabled() { 194 return isFeatureLibLoaded(FEATURE_MIDIIO); 195 } 196 197 static boolean isPortsEnabled() { 198 return isFeatureLibLoaded(FEATURE_PORTS); 199 } 200 201 static boolean isDirectAudioEnabled() { 202 return isFeatureLibLoaded(FEATURE_DIRECT_AUDIO); 203 } 204 205 private static boolean isFeatureLibLoaded(int feature) { 206 if (Printer.debug) Printer.debug("Platform: Checking for feature "+feature+"..."); 207 int requiredLib = nGetLibraryForFeature(feature); 208 boolean isLoaded = (requiredLib != 0) && ((loadedLibs & requiredLib) == requiredLib); 209 if (Printer.debug) Printer.debug(" ...needs library "+requiredLib+". Result is loaded="+isLoaded); 210 return isLoaded; 211 } 212 213 // the following native methods are implemented in Platform.c 214 private native static boolean nIsBigEndian(); 215 private native static boolean nIsSigned8(); 216 private native static String nGetExtraLibraries(); 217 private native static int nGetLibraryForFeature(int feature); 218 219 220 /** 221 * Read the required system properties. 222 */ 223 private static void readProperties() { 224 // $$fb 2002-03-06: implement check for endianness in native. Facilitates porting ! 225 bigEndian = nIsBigEndian(); 226 signed8 = nIsSigned8(); // Solaris on Sparc: signed, all others unsigned 227 javahome = JSSecurityManager.getProperty("java.home"); 228 classpath = JSSecurityManager.getProperty("java.class.path"); 229 } 230 }