diff --git a/BlackHole.xcodeproj/project.pbxproj b/BlackHole.xcodeproj/project.pbxproj index 90aa7fe..a37e176 100644 --- a/BlackHole.xcodeproj/project.pbxproj +++ b/BlackHole.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 28B7C7582B314A2C00586BF6 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 28B7C7572B314A2C00586BF6 /* main.c */; }; + 28B7C7652B314ADB00586BF6 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 28B7C7572B314A2C00586BF6 /* main.c */; }; 3D083818284A7F1200C69403 /* VERSION in Resources */ = {isa = PBXBuildFile; fileRef = 3D89662F2849BF1A002AB3F0 /* VERSION */; }; 3D083819284A7F1500C69403 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 2DA8FA1515FEAAB000F04B50 /* README.md */; }; 3D88D48F285BBA5800629399 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 3D88D48E285BBA5800629399 /* main.c */; }; @@ -20,6 +22,15 @@ /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ + 28B7C75B2B314A6300586BF6 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 3D88D48A285BBA5800629399 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -35,6 +46,8 @@ 039FF2572341B6E800400D7A /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = ""; }; 03FDCAD9233F235500101681 /* BlackHole.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = BlackHole.icns; sourceTree = ""; }; 03FDCADB233FAE0500101681 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; + 28B7C7572B314A2C00586BF6 /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = ""; }; + 28B7C75D2B314A6300586BF6 /* bh-client */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "bh-client"; sourceTree = BUILT_PRODUCTS_DIR; }; 2D616EF215B8C82500D598BD /* BlackHole.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = BlackHole.c; sourceTree = ""; }; 2D7477AC1578168D00412279 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; }; 2D7477EC157823CF00412279 /* CoreAudio.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreAudio.framework; path = System/Library/Frameworks/CoreAudio.framework; sourceTree = SDKROOT; }; @@ -56,6 +69,13 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 28B7C75A2B314A6300586BF6 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 3D88D489285BBA5800629399 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -76,6 +96,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 28B7C7562B314A0000586BF6 /* BlackHoleClient */ = { + isa = PBXGroup; + children = ( + 28B7C7572B314A2C00586BF6 /* main.c */, + ); + path = BlackHoleClient; + sourceTree = ""; + }; 2D616EF015B8C82500D598BD /* BlackHole */ = { isa = PBXGroup; children = ( @@ -89,6 +117,7 @@ 2D74779B1578162B00412279 = { isa = PBXGroup; children = ( + 28B7C7562B314A0000586BF6 /* BlackHoleClient */, 3D083805284A7BB000C69403 /* Installer */, 3D89662F2849BF1A002AB3F0 /* VERSION */, 2DA8FA1515FEAAB000F04B50 /* README.md */, @@ -106,6 +135,7 @@ children = ( 3D89664D2849C318002AB3F0 /* BlackHole.driver */, 3D88D48C285BBA5800629399 /* BlackHoleTests */, + 28B7C75D2B314A6300586BF6 /* bh-client */, ); name = Products; sourceTree = ""; @@ -154,6 +184,23 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ + 28B7C75C2B314A6300586BF6 /* bh-client */ = { + isa = PBXNativeTarget; + buildConfigurationList = 28B7C7612B314A6300586BF6 /* Build configuration list for PBXNativeTarget "bh-client" */; + buildPhases = ( + 28B7C7592B314A6300586BF6 /* Sources */, + 28B7C75A2B314A6300586BF6 /* Frameworks */, + 28B7C75B2B314A6300586BF6 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "bh-client"; + productName = "bh-client"; + productReference = 28B7C75D2B314A6300586BF6 /* bh-client */; + productType = "com.apple.product-type.tool"; + }; 3D88D48B285BBA5800629399 /* BlackHoleTests */ = { isa = PBXNativeTarget; buildConfigurationList = 3D88D493285BBA5800629399 /* Build configuration list for PBXNativeTarget "BlackHoleTests" */; @@ -196,6 +243,10 @@ attributes = { LastUpgradeCheck = 1220; TargetAttributes = { + 28B7C75C2B314A6300586BF6 = { + CreatedOnToolsVersion = 15.0.1; + ProvisioningStyle = Manual; + }; 3D88D48B285BBA5800629399 = { CreatedOnToolsVersion = 13.4; DevelopmentTeam = Q5C99V536K; @@ -221,6 +272,7 @@ targets = ( 3D89663D2849C318002AB3F0 /* BlackHole */, 3D88D48B285BBA5800629399 /* BlackHoleTests */, + 28B7C75C2B314A6300586BF6 /* bh-client */, ); }; /* End PBXProject section */ @@ -241,10 +293,19 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ + 28B7C7592B314A6300586BF6 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 28B7C7652B314ADB00586BF6 /* main.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 3D88D488285BBA5800629399 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 28B7C7582B314A2C00586BF6 /* main.c in Sources */, 3D88D48F285BBA5800629399 /* main.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -260,6 +321,112 @@ /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ + 28B7C7622B314A6300586BF6 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Manual; + COPY_PHASE_STRIP = NO; + DEVELOPMENT_TEAM = ""; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + }; + name = Release; + }; + 28B7C7632B314A6300586BF6 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Manual; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + DEVELOPMENT_TEAM = ""; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + }; + name = Debug; + }; + 28B7C7642B314A6300586BF6 /* Debug-Opt */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Manual; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + DEVELOPMENT_TEAM = ""; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + }; + name = "Debug-Opt"; + }; 2D7477A21578162B00412279 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { @@ -580,6 +747,16 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + 28B7C7612B314A6300586BF6 /* Build configuration list for PBXNativeTarget "bh-client" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 28B7C7622B314A6300586BF6 /* Release */, + 28B7C7632B314A6300586BF6 /* Debug */, + 28B7C7642B314A6300586BF6 /* Debug-Opt */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 2D7477A01578162B00412279 /* Build configuration list for PBXProject "BlackHole" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/BlackHole.xcodeproj/xcshareddata/xcschemes/bh-client.xcscheme b/BlackHole.xcodeproj/xcshareddata/xcschemes/bh-client.xcscheme new file mode 100644 index 0000000..23876c0 --- /dev/null +++ b/BlackHole.xcodeproj/xcshareddata/xcschemes/bh-client.xcscheme @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BlackHoleClient/main.c b/BlackHoleClient/main.c new file mode 100644 index 0000000..6bd91b5 --- /dev/null +++ b/BlackHoleClient/main.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef kSeverPort +#define kEventSeverPort 25192 +#endif + +static unsigned long gDeviceId = 0; +static char* gAppName; +static char* gAppPath; +static char** gAppArgv; + +enum DeviceEvent +{ + DeviceEventNone = 0, // No event. Can be used for pings, etc. + DeviceEventStarted = 1, + DeviceEventStopped = 62, + DeviceEventMax = 63, // Maximum possible value of device event +}; + +static void BlackHole_listen(int socket) +{ + pid_t subprocessPID = -1; + uint8_t eventMessage = 0; + uint8_t eventMessageSize = sizeof(eventMessage); + size_t result = 0; + uint8_t deviceMask = 3 << 6; + uint8_t eventMask = ~deviceMask; + + while(1) { + result = read(socket, &eventMessage, eventMessageSize); + if (result < eventMessageSize) { + perror("Client: Socket read error"); + break; + } + + uint8_t deviceId = (eventMessage & deviceMask) >> 6; + uint8_t event = eventMessage & eventMask; + + printf("Client: Received event %i for device id %i\n", event, deviceId); + + if (deviceId != gDeviceId) { + continue; + } + + if (event == DeviceEventStarted) { + if (subprocessPID > 0) { + printf("Subprocess: Already running"); + continue; + } + if ((subprocessPID = fork()) == 0) { + if (execv(gAppPath, gAppArgv) < 0) { + perror("Subprocess: execv error "); + } + } else if (subprocessPID < 0) { + printf("Subprocess: fork error"); + } else { + printf("Subprocess: started"); + } + } else if (event == DeviceEventStopped) { + if (subprocessPID > 0) { + printf("Subprocess: closed"); + kill(subprocessPID, SIGTERM); + subprocessPID = -1; + } + } + + } + close(socket); +} + +static void print_help(char* errorMessage) +{ + if (errorMessage != NULL) { + printf("Error: %s\n\n", errorMessage); + } + + printf("BlackHole loopback driver client.\n\n"); + printf("Liestens to events of the specified BlackHole device.\n"); + printf("Starts the specified app when the device becomes active.\n"); + printf("Terminates the app when the device becomes inactive.\n"); + printf("\n"); + printf("Usage:\n"); + printf("%s (device id) (app path) [app arguments]\n\n", gAppName); + printf("device id: ID (either 1 or 2) of the BackHole device to subscribe to.\n"); + printf("app path: Path to the app to run when the device activates.\n"); + printf("app arguments: Arguments to pass to the app.\n"); +} + +int main(int argc, char *argv[]) +{ + gAppName = argv[0]; + char* searchResult = gAppName; + + while (searchResult != NULL) { + searchResult = strstr(searchResult + 1, "/"); + if (searchResult != NULL) { + gAppName = searchResult + 1; + } + } + + if (argc < 3) { + print_help("Too few arguments"); + return 0; + } + + int appPathIndex = 2; + size_t appArgvCount = argc - appPathIndex; + + gAppArgv = (char **) malloc(sizeof(char *) * (appPathIndex + 1)); + + int appArgvIndex = 0; + for (int i = appPathIndex; i < argc; i++) { + gAppArgv[appArgvIndex] = argv[i]; + appArgvIndex++; + } + + gAppArgv[appArgvCount] = NULL; + gDeviceId = strtoul(argv[1], NULL, 0); + gAppPath = argv[appPathIndex]; + + printf("DeviceId: %lu\n", gDeviceId); + printf("App to run: "); + + for (uint i = 0; i < appArgvCount; i++) { + if (gAppArgv[i] == NULL) { + break; + } + printf(" %s", gAppArgv[i]); + } + printf("\n\n"); + + while (1) { + int server_socket; + int result; + struct sockaddr_in server_addr; + server_socket = socket(AF_INET, SOCK_STREAM, 0); + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + server_addr.sin_port = htons(kEventSeverPort); + result = connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)); + + if (result == -1) { + perror("Error:"); + sleep(1); + printf("Reconnecting...\n"); + continue; + } + + BlackHole_listen(server_socket); + printf("Disconnected\n"); + sleep(1); + printf("Reconnecting...\n"); + } +}