summaryrefslogtreecommitdiff
path: root/frontend/audio_hotplug.h
blob: db4eb59e8ccbe57c8b1e10fb9435c169afdc5e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef CHECK_AUDIO_H
#define CHECK_AUDIO_H

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

/* Only check once every 60 calls, so this can be called every frame
 * without much penalty */
#define AUDIO_CHECK_THROTTLE 60

bool found_usb_audio = false;
bool has_usb_audio = false;

/* Every AUDIO_CHECK_THROTTLE calls, check to see if the USB audio
 * device has been plugged or unplugged. When the status changes,
 * calls `reinit_callback` with the new status. */
void audio_hotplug_check(void (*reinit_callback)(bool has_usb_audio)) {
  static int count;

  if (++count < AUDIO_CHECK_THROTTLE) return;

  count = 0;

  if (access("/dev/dsp1", R_OK | W_OK) == 0) {
    found_usb_audio = true;
  } else {
    found_usb_audio = false;
  }

  if (!has_usb_audio && found_usb_audio) {
    has_usb_audio = true;
    reinit_callback(true);
  } else if (has_usb_audio && !found_usb_audio) {
    has_usb_audio = false;
    reinit_callback(false);
  }
}

/* For SDL apps, set audio to the correct device. */
void audio_hotplug_set_device(bool has_usb_audio) {
  if (has_usb_audio) {
    SDL_putenv("AUDIODEV=/dev/dsp1");
  } else {
    SDL_putenv("AUDIODEV=/dev/dsp");
  }
}

#endif