Refactor new macOS code to reduce #ifs, increase shared code, and fix warnings

This commit is contained in:
Cameron Gutman
2018-03-27 00:34:38 -07:00
parent 6cc165b589
commit e8832ed746
22 changed files with 73 additions and 293 deletions
+2 -10
View File
@@ -22,20 +22,12 @@
// HACK: Avoid calling [UIApplication delegate] off the UI thread to keep
// Main Thread Checker happy.
if ([NSThread isMainThread]) {
#if TARGET_OS_IPHONE
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
#else
_appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
#endif
_appDelegate = (AppDelegate *)[[OSApplication sharedApplication] delegate];
}
else {
dispatch_sync(dispatch_get_main_queue(), ^{
#if TARGET_OS_IPHONE
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
#else
_appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
#endif
_appDelegate = (AppDelegate *)[[OSApplication sharedApplication] delegate];
});
}
+1 -2
View File
@@ -10,8 +10,7 @@
// Swift
#import "Moonlight-Swift.h"
#if TARGET_OS_IPHONE
#else
#if !TARGET_OS_IPHONE
#import "Gamepad.h"
#endif
@class Controller;
+6 -28
View File
@@ -8,9 +8,8 @@
#import "ControllerSupport.h"
#if TARGET_OS_IPHONE
#import "OnScreenControls.h"
#else
#if !TARGET_OS_IPHONE
#import "Gamepad.h"
#import "Control.h"
#endif
@@ -28,7 +27,6 @@
NSLock *_controllerStreamLock;
NSMutableDictionary *_controllers;
#if TARGET_OS_IPHONE
OnScreenControls *_osc;
// This controller object is shared between on-screen controls
@@ -37,7 +35,6 @@
#define EMULATING_SELECT 0x1
#define EMULATING_SPECIAL 0x2
#endif
bool _oscEnabled;
char _controllerNumbers;
@@ -85,7 +82,6 @@
}
}
#if TARGET_OS_IPHONE
-(void) handleSpecialCombosReleased:(Controller*)controller releasedButtons:(int)releasedButtons
{
if ((controller.emulatingButtonFlags & EMULATING_SELECT) &&
@@ -120,7 +116,6 @@
}
}
#endif
-(void) updateButtonFlags:(Controller*)controller flags:(int)flags
{
@@ -129,14 +124,12 @@
// This must be called before handleSpecialCombosPressed
// because we clear the original button flags there
#if TARGET_OS_IPHONE
int releasedButtons = (controller.lastButtonFlags ^ flags) & ~flags;
int pressedButtons = (controller.lastButtonFlags ^ flags) & flags;
[self handleSpecialCombosReleased:controller releasedButtons:releasedButtons];
[self handleSpecialCombosPressed:controller pressedButtons:pressedButtons];
#endif
}
}
@@ -144,9 +137,7 @@
{
@synchronized(controller) {
controller.lastButtonFlags |= flags;
#if TARGET_OS_IPHONE
[self handleSpecialCombosPressed:controller pressedButtons:flags];
#endif
}
}
@@ -154,9 +145,7 @@
{
@synchronized(controller) {
controller.lastButtonFlags &= ~flags;
#if TARGET_OS_IPHONE
[self handleSpecialCombosReleased:controller releasedButtons:flags];
#endif
}
}
@@ -258,7 +247,6 @@
}
}
#if TARGET_OS_IPHONE
-(void) updateAutoOnScreenControlMode
{
// Auto on-screen control support may not be enabled
@@ -285,7 +273,9 @@
}
}
#if TARGET_OS_IPHONE
[_osc setLevel:level];
#endif
}
-(void) initAutoOnScreenControlMode:(OnScreenControls*)osc
@@ -294,7 +284,6 @@
[self updateAutoOnScreenControlMode];
}
#endif
-(void) assignController:(GCController*)controller {
for (int i = 0; i < 4; i++) {
@@ -303,7 +292,7 @@
controller.playerIndex = i;
Controller* limeController;
#if TARGET_OS_IPHONE
if (i == 0) {
// Player 0 shares a controller object with the on-screen controls
limeController = _player0osc;
@@ -311,10 +300,6 @@
limeController = [[Controller alloc] init];
limeController.playerIndex = i;
}
#else
limeController = [[Controller alloc] init];
limeController.playerIndex = i;
#endif
[_controllers setObject:limeController forKey:[NSNumber numberWithInteger:controller.playerIndex]];
@@ -387,10 +372,10 @@
_controllers = [[NSMutableDictionary alloc] init];
_controllerNumbers = 0;
#if TARGET_OS_IPHONE
_player0osc = [[Controller alloc] init];
_player0osc.playerIndex = 0;
#if TARGET_OS_IPHONE
DataManager* dataMan = [[DataManager alloc] init];
_oscEnabled = (OnScreenControlsLevel)[[dataMan getSettings].onscreenControls integerValue] != OnScreenControlsLevelOff;
#else
@@ -403,10 +388,7 @@
for (GCController* controller in [GCController controllers]) {
[self assignController:controller];
[self registerControllerCallbacks:controller];
#if TARGET_OS_IPHONE
[self updateAutoOnScreenControlMode];
#endif
}
self.connectObserver = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidConnectNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
@@ -418,10 +400,8 @@
// Register callbacks on the new controller
[self registerControllerCallbacks:controller];
#if TARGET_OS_IPHONE
// Re-evaluate the on-screen control mode
[self updateAutoOnScreenControlMode];
#endif
}];
self.disconnectObserver = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidDisconnectNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
Log(LOG_I, @"Controller disconnected!");
@@ -435,10 +415,8 @@
[self updateFinished:[_controllers objectForKey:[NSNumber numberWithInteger:controller.playerIndex]]];
[_controllers removeObjectForKey:[NSNumber numberWithInteger:controller.playerIndex]];
#if TARGET_OS_IPHONE
// Re-evaluate the on-screen control mode
[self updateAutoOnScreenControlMode];
#endif
}];
return self;
}
+2
View File
@@ -24,10 +24,12 @@ typedef NS_ENUM(NSInteger, OnScreenControlsLevel) {
OnScreenControlsLevelAutoGCExtendedGamepad,
};
#if TARGET_OS_IPHONE
- (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSupport swipeDelegate:(id<EdgeDetectionDelegate>)edgeDelegate;
- (BOOL) handleTouchDownEvent:(NSSet*)touches;
- (BOOL) handleTouchUpEvent:(NSSet*)touches;
- (BOOL) handleTouchMovedEvent:(NSSet*)touches;
- (void) setLevel:(OnScreenControlsLevel)level;
#endif
@end
+1 -3
View File
@@ -6,8 +6,6 @@
// Copyright (c) 2014 Moonlight Stream. All rights reserved.
//
// This is redundant, as it is part of the prefix header
//#import <UIKit/UIKit.h>
#import "ControllerSupport.h"
@protocol EdgeDetectionDelegate <NSObject>
@@ -16,7 +14,7 @@
@end
@interface StreamView : UIView
@interface StreamView : OSView
- (void) setupOnScreenControls:(ControllerSupport*)controllerSupport swipeDelegate:(id<EdgeDetectionDelegate>)swipeDelegate;
- (void) setMouseDeltaFactors:(float)x y:(float)y;
+4 -3
View File
@@ -18,7 +18,8 @@
#elif TARGET_OS_MAC
#import <AppKit/AppKit.h>
#endif
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Logger.h"
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Logger.h"
#include "OSPortabilityDefs.h"
#endif
+5 -8
View File
@@ -16,16 +16,13 @@
self.statusMessage = @"App asset has no status message";
self.statusCode = -1;
}
- (OSImage*) getImage {
#if TARGET_OS_IPHONE
- (UIImage*) getImage {
UIImage* appImage = [[UIImage alloc] initWithData:self.data];
OSImage* appImage = [[OSImage alloc] initWithData:self.data];
#else
OSImage* appImage = nil;
#endif
return appImage;
}
#else
- (NSImage*) getImage {
return nil;
}
#endif
@end
+1 -5
View File
@@ -19,11 +19,7 @@ static const int MAX_ATTEMPTS = 5;
- (void) main {
#if TARGET_OS_IPHONE
UIImage* appImage = nil;
#else
NSImage* appImage = nil;
#endif
OSImage* appImage = nil;
int attempts = 0;
while (![self isCancelled] && appImage == nil && attempts++ < MAX_ATTEMPTS) {
+24
View File
@@ -0,0 +1,24 @@
//
// OSPortabilityDefs.h
// Moonlight
//
// Created by Cameron Gutman on 3/26/18.
// Copyright © 2018 Moonlight Stream. All rights reserved.
//
#ifndef OSPortabilityDefs_h
#define OSPortabilityDefs_h
#if TARGET_OS_IPHONE
#define OSImage UIImage
#define OSColor UIColor
#define OSView UIView
#define OSApplication UIApplication
#else
#define OSImage NSImage
#define OSColor NSColor
#define OSView NSView
#define OSApplication NSApplication
#endif
#endif /* OSPortabilityDefs_h */
+2 -3
View File
@@ -98,7 +98,6 @@ int ArInit(int audioConfiguration, POPUS_MULTISTREAM_CONFIGURATION opusConfig, v
audioLock = [[NSLock alloc] init];
#if TARGET_OS_IPHONE
// Configure the audio session for our app
NSError *audioSessionError = nil;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
@@ -108,8 +107,8 @@ int ArInit(int audioConfiguration, POPUS_MULTISTREAM_CONFIGURATION opusConfig, v
[audioSession setPreferredOutputNumberOfChannels:opusConfig->channelCount error:&audioSessionError];
[audioSession setPreferredIOBufferDuration:0.005 error:&audioSessionError];
[audioSession setActive: YES error: &audioSessionError];
#endif
OSStatus status;
AudioComponentDescription audioDesc;
@@ -355,7 +354,7 @@ void ClLogMessage(const char* format, ...)
#else
if (@available(macOS 10.13, *)) {
if (VTIsHardwareDecodeSupported(kCMVideoCodecType_HEVC) || _streamConfig.streamingRemotely != 0)
_streamConfig.supportsHevc = true;
_streamConfig.supportsHevc = true;
}
#endif
+1 -5
View File
@@ -12,11 +12,7 @@
@interface StreamManager : NSOperation
#if TARGET_OS_IPHONE
- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionCallbacks:(id<ConnectionCallbacks>)callback;
#else
- (id) initWithConfig:(StreamConfiguration*)config renderView:(NSView*)view connectionCallbacks:(id<ConnectionCallbacks>)callback;
#endif
- (id) initWithConfig:(StreamConfiguration*)config renderView:(OSView*)view connectionCallbacks:(id<ConnectionCallbacks>)callback;
- (void) stopStream;
+2 -22
View File
@@ -11,10 +11,6 @@
#import "HttpManager.h"
#import "Utils.h"
#if TARGET_OS_IPHONE
#import "OnScreenControls.h"
#endif
#import "StreamView.h"
#import "ServerInfoResponse.h"
#import "HttpResponse.h"
@@ -24,17 +20,12 @@
@implementation StreamManager {
StreamConfiguration* _config;
#if TARGET_OS_IPHONE
UIView* _renderView;
#else
NSView* _renderView;
#endif
OSView* _renderView;
id<ConnectionCallbacks> _callbacks;
Connection* _connection;
}
#if TARGET_OS_IPHONE
- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionCallbacks:(id<ConnectionCallbacks>)callbacks {
- (id) initWithConfig:(StreamConfiguration*)config renderView:(OSView*)view connectionCallbacks:(id<ConnectionCallbacks>)callbacks {
self = [super init];
_config = config;
_renderView = view;
@@ -43,17 +34,6 @@
_config.riKeyId = arc4random();
return self;
}
#else
- (id) initWithConfig:(StreamConfiguration*)config renderView:(NSView*)view connectionCallbacks:(id<ConnectionCallbacks>)callbacks {
self = [super init];
_config = config;
_renderView = view;
_callbacks = callbacks;
_config.riKey = [Utils randomBytes:16];
_config.riKeyId = arc4random();
return self;
}
#endif
- (void)main {
[CryptoManager generateKeyPairUsingSSl];
+2 -5
View File
@@ -11,11 +11,8 @@
@import AVFoundation;
@interface VideoDecoderRenderer : NSObject
#if TARGET_OS_IPHONE
- (id)initWithView:(UIView*)view;
#else
- (id)initWithView:(NSView*)view;
#endif
- (id)initWithView:(OSView*)view;
- (void)setupWithVideoFormat:(int)videoFormat;
+5 -39
View File
@@ -11,11 +11,7 @@
#include "Limelight.h"
@implementation VideoDecoderRenderer {
#if TARGET_OS_IPHONE
UIView *_view;
#else
NSView *_view;
#endif
OSView *_view;
AVSampleBufferDisplayLayer* displayLayer;
Boolean waitingForSps, waitingForPps, waitingForVps;
@@ -31,11 +27,7 @@
displayLayer = [[AVSampleBufferDisplayLayer alloc] init];
displayLayer.bounds = _view.bounds;
#if TARGET_OS_IPHONE
displayLayer.backgroundColor = [UIColor blackColor].CGColor;
#else
displayLayer.backgroundColor = [NSColor blackColor].CGColor;
#endif
displayLayer.backgroundColor = [OSColor blackColor].CGColor;
displayLayer.position = CGPointMake(CGRectGetMidX(_view.bounds), CGRectGetMidY(_view.bounds));
displayLayer.videoGravity = AVLayerVideoGravityResizeAspect;
@@ -61,8 +53,8 @@
formatDesc = nil;
}
}
#if TARGET_OS_IPHONE
- (id)initWithView:(UIView*)view
- (id)initWithView:(OSView*)view
{
self = [super init];
@@ -72,20 +64,6 @@
return self;
}
#else
- (id)initWithView:(NSView*)view
{
self = [super init];
_view = view;
[self reinitializeDisplayLayer];
return self;
}
#endif
- (void)setupWithVideoFormat:(int)videoFormat
{
@@ -248,20 +226,9 @@
#if TARGET_OS_IPHONE
if (@available(iOS 11.0, *)) {
status = CMVideoFormatDescriptionCreateFromHEVCParameterSets(kCFAllocatorDefault,
3, /* count of parameter sets */
parameterSetPointers,
parameterSetSizes,
NAL_LENGTH_PREFIX_SIZE,
nil,
&formatDesc);
} else {
// This means Moonlight-common-c decided to give us an HEVC stream
// even though we said we couldn't support it. All we can do is abort().
abort();
}
#else
if (@available(macOS 10.13, *)) {
#endif
status = CMVideoFormatDescriptionCreateFromHEVCParameterSets(kCFAllocatorDefault,
3, /* count of parameter sets */
parameterSetPointers,
@@ -274,7 +241,6 @@
// even though we said we couldn't support it. All we can do is abort().
abort();
}
#endif
if (status != noErr) {
Log(LOG_E, @"Failed to create HEVC format description: %d", (int)status);