Misc.mm 2.92 KB
Newer Older
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Dummy Keymap functions that don't actually map anything,
// just to fix the SDL build.

#include <string.h>
#include "../Misc.h"

#include <unistd.h>

#include <sys/param.h> // For MAXPATHLEN
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface MiscCocoa : NSObject

@end

@implementation MiscCocoa

+ (void)requestMicrophoneAccess {
  switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio])
  {
    case AVAuthorizationStatusAuthorized:
    {
      fprintf(stdout, "Microphone access already granted\n");
      break;
    }
    case AVAuthorizationStatusNotDetermined:
    {
      [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        if (granted) {
            fprintf(stdout, "Microphone access granted\n");
        } else {
            fprintf(stderr, "Microphone access denied. No FFT support.\n");
        }
      }];
      return;
    }
    case AVAuthorizationStatusDenied:
    {
      fprintf(stderr, "Microphone access previously denied by user. Asking again.\n");
      [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        if (granted) {
          fprintf(stdout, "Microphone access granted\n");
        } else {
          fprintf(stderr, "Microphone access denied. No FFT support.\n");
        }
      }];
      return;
    }
    case AVAuthorizationStatusRestricted:
    {
      fprintf(stderr, "The user can't grant microphone access due to restrictions.\n");
    }
  }
}

@end

void Misc::PlatformStartup()
{
  char appPath[MAXPATHLEN];
  CFBundleRef bundle = CFBundleGetMainBundle();
  if (!bundle) return;

  CFURLRef bundleURL = CFBundleCopyBundleURL(bundle);
  CFURLRef pathURL = CFURLCreateCopyDeletingLastPathComponent(NULL, bundleURL);
  if (!CFURLGetFileSystemRepresentation(pathURL, true, (UInt8*)appPath, MAXPATHLEN))
  {
    CFRelease(bundleURL);
    CFRelease(pathURL);
    return;
  }
  CFRelease(bundleURL);
  CFRelease(pathURL);

  chdir(appPath);
  
  // Check if the user has granted microphone access to Bonzomatic. Else request it.
  if (@available(macOS 10.14, *)) {
    [MiscCocoa requestMicrophoneAccess];
  }
}

void Misc::PlatformShutdown()
{
}

void Misc::InitKeymaps()
{
}

void Misc::GetKeymapName(char* sz)
{
	strncpy(sz,"<native>",7);
}

bool Misc::ExecuteCommand( const char * cmd, const char * param )
{
	if (cmd && *cmd) // param is always set
	{
		char command[512];
		sprintf(command, "%s %s", cmd, param);
		system(command);
		return true;
	}
	return false;
}

bool Misc::FileExists(const char * path)
{
  return access(path, R_OK) != -1;
}

const char * Misc::GetDefaultFontPath()
{
  // Linux case
  // TODO: use fonts.conf(5) or X resources or something like that
  const char* fontPaths[] =
  {
    "/Library/Fonts/Courier New.ttf",
    NULL
  };
  for (int i = 0; fontPaths[i]; ++i)
  {
    if (FileExists(fontPaths[i]))
    {
      return fontPaths[i];
    }
  }
  return NULL;
}