| 1 | package com.mojang.blaze3d.audio;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.HexFormat;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import org.lwjgl.openal.ALC10;
|
| 8 | import org.lwjgl.openal.SOFTSystemEventProcI;
|
| 9 | import org.lwjgl.openal.SOFTSystemEvents;
|
| 10 | import org.lwjgl.system.MemoryUtil;
|
| 11 | import org.slf4j.Logger;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class CallbackDeviceTracker extends AbstractDeviceTracker {
|
| 15 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 16 | private volatile boolean updateRequested;
|
| 17 | private static final int[] SUBSCRIBED_EVENT_TYPES = new int[]{6614, 6615, 6616};
|
| 18 | public static final HexFormat HEX_FORMAT = HexFormat.of();
|
| 19 |
|
| 20 | public CallbackDeviceTracker(final DeviceList deviceList) {
|
| 21 | super(deviceList);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | protected boolean isUpdateRequested() {
|
| 26 | return this.updateRequested;
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | protected void discardUpdateRequest() {
|
| 31 | this.updateRequested = false;
|
| 32 | }
|
| 33 |
|
| 34 | public static boolean isSupported() {
|
| 35 | if (!ALC10.alcIsExtensionPresent(0L, "ALC_SOFT_system_events")) {
|
| 36 | return false;
|
| 37 | }
|
| 38 |
|
| 39 | for (int eventType : SUBSCRIBED_EVENT_TYPES) {
|
| 40 | if (!isSupportedForPlaybackDevice(eventType)) {
|
| 41 | return false;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | return true;
|
| 46 | }
|
| 47 |
|
| 48 | public static CallbackDeviceTracker createAndInstall(final DeviceList deviceList) {
|
| 49 | CallbackDeviceTracker result = new CallbackDeviceTracker(deviceList);
|
| 50 | SOFTSystemEvents.alcEventControlSOFT(SUBSCRIBED_EVENT_TYPES, true);
|
| 51 | SOFTSystemEvents.alcEventCallbackSOFT(result.createCallback(), 0L);
|
| 52 | return result;
|
| 53 | }
|
| 54 |
|
| 55 | private SOFTSystemEventProcI createCallback() {
|
| 56 | return (eventType, deviceType, device, messageLength, messagePtr, userParam) -> {
|
| 57 | String deviceTypeString = deviceTypeToString(deviceType);
|
| 58 | String message = MemoryUtil.memASCII(messagePtr, messageLength);
|
| 59 | switch (eventType) {
|
| 60 | case 6614:
|
| 61 | LOGGER.debug("Default {} device changed: {}", deviceTypeString, message);
|
| 62 | break;
|
| 63 | case 6615:
|
| 64 | LOGGER.debug("Added new {} device: {}", deviceTypeString, message);
|
| 65 | break;
|
| 66 | case 6616:
|
| 67 | LOGGER.debug("Removed {} device: {}", deviceTypeString, message);
|
| 68 | }
|
| 69 |
|
| 70 | if (deviceType == 6612) {
|
| 71 | this.updateRequested = true;
|
| 72 | }
|
| 73 | };
|
| 74 | }
|
| 75 |
|
| 76 | private static boolean isSupportedForPlaybackDevice(final int eventType) {
|
| 77 | int result = SOFTSystemEvents.alcEventIsSupportedSOFT(eventType, 6612);
|
| 78 | if (result == 0) {
|
| 79 | int error = ALC10.alcGetError(0L);
|
| 80 | LOGGER.warn("Failed to check event {}, error: {}", HEX_FORMAT.toHexDigits(eventType), HEX_FORMAT.toHexDigits(error));
|
| 81 | return false;
|
| 82 | } else {
|
| 83 | return result == 6617;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | private static String deviceTypeToString(final int deviceType) {
|
| 88 | return switch (deviceType) {
|
| 89 | case 6612 -> "playback";
|
| 90 | case 6613 -> "capture";
|
| 91 | default -> "unknown (0x" + HEX_FORMAT.toHexDigits(deviceType) + ")";
|
| 92 | };
|
| 93 | }
|
| 94 | }
|
| 95 |
|