|
| 1 | +package net.bjoernpetersen.volctl; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.channels.Channels; |
| 7 | +import java.nio.channels.FileChannel; |
| 8 | +import java.nio.channels.FileLock; |
| 9 | +import java.nio.channels.ReadableByteChannel; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.nio.file.StandardOpenOption; |
| 14 | + |
| 15 | +import static net.bjoernpetersen.volctl.StringUtils.substringAfterLast; |
| 16 | +import static net.bjoernpetersen.volctl.StringUtils.substringBeforeLast; |
| 17 | + |
| 18 | +/** |
| 19 | + * Allows master system audio volume access and control. |
| 20 | + * <p> |
| 21 | + * This implementation uses JNI to perform the required native system calls. To do that it has to |
| 22 | + * store an included library file outside of the .jar-file. The location and name of that file can |
| 23 | + * be customized using the constructor parameters. |
| 24 | + * |
| 25 | + * <h2>Usage with multiple class loaders</h2> |
| 26 | + * <p> |
| 27 | + * If you plan to use this class from multiple class loaders, you'll have to set |
| 28 | + * {@code supportMultipleClassLoaders} to {@code true}. If you do that, |
| 29 | + * <b>every instance will export its own library file</b>. These files won't have a fully |
| 30 | + * predictable filename and cannot be deleted by the same JVM that has loaded them. |
| 31 | + * This workaround is necessary because only one {@link ClassLoader} is allowed to load a |
| 32 | + * library file. |
| 33 | + * <p> |
| 34 | + * <b>Note:</b> You may use the {@link #newInstanceWithClassLoaderSupport} method if you |
| 35 | + * want to use the default location and name, but enable multiple class loader support. |
| 36 | + */ |
| 37 | +@SuppressWarnings({"unused", "WeakerAccess"}) |
| 38 | +public class VolumeControl { |
| 39 | + /** |
| 40 | + * The minimum value the volume may have. |
| 41 | + */ |
| 42 | + public static final int MIN_VOLUME = 0; |
| 43 | + /** |
| 44 | + * The maximum value the volume may have. |
| 45 | + */ |
| 46 | + public static final int MAX_VOLUME = 100; |
| 47 | + |
| 48 | + private static final String LIB_NAME = "volctl"; |
| 49 | + private static final String TMP_DIR_PROPERTY_NAME = "java.io.tmpdir"; |
| 50 | + |
| 51 | + /** |
| 52 | + * Constructor. |
| 53 | + * |
| 54 | + * @param dllLocation the directory to store the native access library in, |
| 55 | + * defaults to temp dir |
| 56 | + * @param dllName the name of the library file without file extension, |
| 57 | + * defaults to "volctl" |
| 58 | + * @param supportMultipleClassLoaders whether to create library files with different names for |
| 59 | + * each new instance (see {@link VolumeControl class docs}) |
| 60 | + * @throws IOException if the library file can't be exported or loaded |
| 61 | + */ |
| 62 | + public VolumeControl( |
| 63 | + @NotNull Path dllLocation, |
| 64 | + @NotNull String dllName, |
| 65 | + boolean supportMultipleClassLoaders |
| 66 | + ) throws IOException { |
| 67 | + String defaultLibFile = getDefaultLibFileName(); |
| 68 | + String extension = substringAfterLast(defaultLibFile, '.'); |
| 69 | + final Path path; |
| 70 | + if (supportMultipleClassLoaders) { |
| 71 | + // Every instance gets its own library file |
| 72 | + path = Files.createTempFile(dllLocation, dllName, '.' + extension); |
| 73 | + FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE); |
| 74 | + writeLibraryFile(channel); |
| 75 | + } else { |
| 76 | + path = dllLocation.resolve(dllName + '.' + extension); |
| 77 | + boolean writeFile = true; |
| 78 | + if (Files.isRegularFile(path)) { |
| 79 | + try { |
| 80 | + // Try to delete outdated library file |
| 81 | + Files.delete(path); |
| 82 | + } catch (IOException e) { |
| 83 | + // Errors are most likely caused by another instance using the existing file, |
| 84 | + // so we can just ignore it and use the file |
| 85 | + writeFile = false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (writeFile) { |
| 90 | + FileChannel channel = FileChannel |
| 91 | + .open(path, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); |
| 92 | + writeLibraryFile(channel); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + System.load(path.toAbsolutePath().toString()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Same as calling |
| 101 | + * {@link #VolumeControl(Path, String, boolean) VolumeControl(dllLocation, dllName, false)}. |
| 102 | + * |
| 103 | + * @param dllLocation the directory to store the native access library in, |
| 104 | + * defaults to temp dir |
| 105 | + * @param dllName the name of the library file without file extension, |
| 106 | + * defaults to "volctl" |
| 107 | + * @throws IOException if the library file can't be exported or loaded |
| 108 | + */ |
| 109 | + public VolumeControl(@NotNull Path dllLocation, @NotNull String dllName) throws IOException { |
| 110 | + this(dllLocation, dllName, false); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Same as calling |
| 115 | + * {@link #VolumeControl(Path, String) VolumeControl(dllLocation, getDefaultLibName())}. |
| 116 | + * |
| 117 | + * @param dllLocation the directory to store the native access library in, |
| 118 | + * defaults to temp dir |
| 119 | + * @throws IOException if the library file can't be exported or loaded |
| 120 | + */ |
| 121 | + public VolumeControl(@NotNull Path dllLocation) throws IOException { |
| 122 | + this(dllLocation, getDefaultLibName()); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Same as calling |
| 127 | + * {@link #VolumeControl(Path, String) VolumeControl(getTempDir(), dllName)}. |
| 128 | + * |
| 129 | + * @param dllName the name of the library file without file extension, |
| 130 | + * defaults to "volctl" |
| 131 | + * @throws IOException if the library file can't be exported or loaded |
| 132 | + */ |
| 133 | + public VolumeControl(@NotNull String dllName) throws IOException { |
| 134 | + this(getTempDir(), dllName); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Same as calling |
| 139 | + * {@link #VolumeControl(Path) VolumeControl(getTempDir())}. |
| 140 | + * |
| 141 | + * @throws IOException if the library file can't be exported or loaded |
| 142 | + */ |
| 143 | + public VolumeControl() throws IOException { |
| 144 | + this(getTempDir()); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Gets the current master audio volume level. |
| 149 | + * |
| 150 | + * @return a value between 0 and 100 (inclusively) |
| 151 | + */ |
| 152 | + public int getVolume() { |
| 153 | + return getVolumeNative(); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Sets the current master audio volume level. |
| 158 | + * |
| 159 | + * @param value a value between 0 and 100 (inclusively) |
| 160 | + */ |
| 161 | + public void setVolume(int value) { |
| 162 | + if (value < MIN_VOLUME) |
| 163 | + throw new IllegalStateException("Value must be positive, was " + value); |
| 164 | + if (value > MAX_VOLUME) |
| 165 | + throw new IllegalStateException("Value must be less than 100, was " + value); |
| 166 | + setVolumeNative(value); |
| 167 | + } |
| 168 | + |
| 169 | + private native int getVolumeNative(); |
| 170 | + |
| 171 | + private native void setVolumeNative(int value); |
| 172 | + |
| 173 | + /** |
| 174 | + * Creates a VolumeControl instance with defaults except for the |
| 175 | + * {@code supportMultipleClassLoaders} parameter being true. |
| 176 | + * |
| 177 | + * @return a new VolumeControl instance |
| 178 | + * @throws IOException if the library file couldn't be exported or loaded |
| 179 | + */ |
| 180 | + @NotNull |
| 181 | + public static VolumeControl newInstanceWithClassLoaderSupport() throws IOException { |
| 182 | + return new VolumeControl(getTempDir(), getDefaultLibName(), true); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Retrieves the directory for temporary files on the current system. |
| 187 | + * |
| 188 | + * @return the path to the tmp directory |
| 189 | + */ |
| 190 | + @NotNull |
| 191 | + public static Path getTempDir() { |
| 192 | + String path = System.getProperty(TMP_DIR_PROPERTY_NAME); |
| 193 | + return Paths.get(path); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @return the default library file name for the current OS, including extension |
| 198 | + */ |
| 199 | + @NotNull |
| 200 | + public static String getDefaultLibFileName() { |
| 201 | + return System.mapLibraryName(LIB_NAME); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @return the default library file name for the current OS, excluding extension |
| 206 | + */ |
| 207 | + @NotNull |
| 208 | + public static String getDefaultLibName() { |
| 209 | + return substringBeforeLast(getDefaultLibFileName(), '.'); |
| 210 | + } |
| 211 | + |
| 212 | + private static void writeLibraryFile(@NotNull FileChannel channel) throws IOException { |
| 213 | + try { |
| 214 | + FileLock lock = channel.lock(); |
| 215 | + try (ReadableByteChannel input = Channels.newChannel( |
| 216 | + VolumeControl.class |
| 217 | + .getResourceAsStream('/' + getDefaultLibFileName()))) { |
| 218 | + channel.transferFrom(input, 0, Long.MAX_VALUE); |
| 219 | + } finally { |
| 220 | + lock.release(); |
| 221 | + } |
| 222 | + } finally { |
| 223 | + channel.close(); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments