diff options
Diffstat (limited to 'platform/osx')
-rw-r--r-- | platform/osx/audio_driver_osx.cpp | 102 | ||||
-rw-r--r-- | platform/osx/audio_driver_osx.h | 4 | ||||
-rw-r--r-- | platform/osx/os_osx.h | 6 | ||||
-rw-r--r-- | platform/osx/os_osx.mm | 146 |
4 files changed, 211 insertions, 47 deletions
diff --git a/platform/osx/audio_driver_osx.cpp b/platform/osx/audio_driver_osx.cpp index d7a91b1653..da5cd8f65e 100644 --- a/platform/osx/audio_driver_osx.cpp +++ b/platform/osx/audio_driver_osx.cpp @@ -30,6 +30,10 @@ #ifdef OSX_ENABLED #include "audio_driver_osx.h" +#include "core/project_settings.h" +#include "os/os.h" + +#define kOutputBus 0 static OSStatus outputDeviceAddressCB(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *__nullable inClientData) { AudioDriverOSX *driver = (AudioDriverOSX *)inClientData; @@ -40,42 +44,69 @@ static OSStatus outputDeviceAddressCB(AudioObjectID inObjectID, UInt32 inNumberA } Error AudioDriverOSX::initDevice() { + AudioComponentDescription desc; + zeromem(&desc, sizeof(desc)); + desc.componentType = kAudioUnitType_Output; + desc.componentSubType = kAudioUnitSubType_HALOutput; + desc.componentManufacturer = kAudioUnitManufacturer_Apple; + + AudioComponent comp = AudioComponentFindNext(NULL, &desc); + ERR_FAIL_COND_V(comp == NULL, FAILED); + + OSStatus result = AudioComponentInstanceNew(comp, &audio_unit); + ERR_FAIL_COND_V(result != noErr, FAILED); + AudioStreamBasicDescription strdesc; + + // TODO: Implement this + /*zeromem(&strdesc, sizeof(strdesc)); + UInt32 size = sizeof(strdesc); + result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &strdesc, &size); + ERR_FAIL_COND_V(result != noErr, FAILED); + + switch (strdesc.mChannelsPerFrame) { + case 2: // Stereo + case 6: // Surround 5.1 + case 8: // Surround 7.1 + channels = strdesc.mChannelsPerFrame; + break; + + default: + // Unknown number of channels, default to stereo + channels = 2; + break; + }*/ + + mix_rate = GLOBAL_DEF("audio/mix_rate", 44100); + + zeromem(&strdesc, sizeof(strdesc)); strdesc.mFormatID = kAudioFormatLinearPCM; strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; strdesc.mChannelsPerFrame = channels; - strdesc.mSampleRate = 44100; + strdesc.mSampleRate = mix_rate; strdesc.mFramesPerPacket = 1; strdesc.mBitsPerChannel = 16; strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8; strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket; - OSStatus result; - AURenderCallbackStruct callback; - AudioComponentDescription desc; - AudioComponent comp = NULL; - const AudioUnitElement output_bus = 0; - const AudioUnitElement bus = output_bus; - const AudioUnitScope scope = kAudioUnitScope_Input; - - zeromem(&desc, sizeof(desc)); - desc.componentType = kAudioUnitType_Output; - desc.componentSubType = kAudioUnitSubType_HALOutput; - desc.componentManufacturer = kAudioUnitManufacturer_Apple; + result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); + ERR_FAIL_COND_V(result != noErr, FAILED); - comp = AudioComponentFindNext(NULL, &desc); - ERR_FAIL_COND_V(comp == NULL, FAILED); + int latency = GLOBAL_DEF("audio/output_latency", 25); + unsigned int buffer_size = closest_power_of_2(latency * mix_rate / 1000); - result = AudioComponentInstanceNew(comp, &audio_unit); - ERR_FAIL_COND_V(result != noErr, FAILED); + if (OS::get_singleton()->is_stdout_verbose()) { + print_line("audio buffer size: " + itos(buffer_size) + " calculated latency: " + itos(buffer_size * 1000 / mix_rate)); + } - result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, scope, bus, &strdesc, sizeof(strdesc)); - ERR_FAIL_COND_V(result != noErr, FAILED); + samples_in.resize(buffer_size); + buffer_frames = buffer_size / channels; + AURenderCallbackStruct callback; zeromem(&callback, sizeof(AURenderCallbackStruct)); callback.inputProc = &AudioDriverOSX::output_callback; callback.inputProcRefCon = this; - result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, scope, bus, &callback, sizeof(callback)); + result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); ERR_FAIL_COND_V(result != noErr, FAILED); result = AudioUnitInitialize(audio_unit); @@ -114,15 +145,10 @@ Error AudioDriverOSX::init() { result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this); ERR_FAIL_COND_V(result != noErr, FAILED); - const int samples = 1024; - samples_in = memnew_arr(int32_t, samples); // whatever - buffer_frames = samples / channels; - return initDevice(); }; Error AudioDriverOSX::reopen() { - Error err; bool restart = false; lock(); @@ -131,7 +157,7 @@ Error AudioDriverOSX::reopen() { restart = true; } - err = finishDevice(); + Error err = finishDevice(); if (err != OK) { ERR_PRINT("finishDevice failed"); unlock(); @@ -179,7 +205,7 @@ OSStatus AudioDriverOSX::output_callback(void *inRefCon, while (frames_left) { int frames = MIN(frames_left, ad->buffer_frames); - ad->audio_server_process(frames, ad->samples_in); + ad->audio_server_process(frames, ad->samples_in.ptr()); for (int j = 0; j < frames * ad->channels; j++) { @@ -232,29 +258,33 @@ bool AudioDriverOSX::try_lock() { } void AudioDriverOSX::finish() { - OSStatus result; - finishDevice(); - result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this); + OSStatus result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this); if (result != noErr) { ERR_PRINT("AudioObjectRemovePropertyListener failed"); } + AURenderCallbackStruct callback; + zeromem(&callback, sizeof(AURenderCallbackStruct)); + result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); + if (result != noErr) { + ERR_PRINT("AudioUnitSetProperty failed"); + } + if (mutex) { memdelete(mutex); mutex = NULL; } - - if (samples_in) { - memdelete_arr(samples_in); - samples_in = NULL; - } }; AudioDriverOSX::AudioDriverOSX() { + active = false; mutex = NULL; - samples_in = NULL; + + mix_rate = 44100; + channels = 2; + samples_in.clear(); }; AudioDriverOSX::~AudioDriverOSX(){}; diff --git a/platform/osx/audio_driver_osx.h b/platform/osx/audio_driver_osx.h index 287c9d6cbf..4576100690 100644 --- a/platform/osx/audio_driver_osx.h +++ b/platform/osx/audio_driver_osx.h @@ -44,10 +44,12 @@ class AudioDriverOSX : public AudioDriver { bool active; Mutex *mutex; + int mix_rate; int channels; - int32_t *samples_in; int buffer_frames; + Vector<int32_t> samples_in; + static OSStatus output_callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index 4b5682518f..56e6802eeb 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -104,6 +104,10 @@ public: Size2 window_size; Rect2 restore_rect; + Point2 im_position; + ImeCallback im_callback; + void *im_target; + power_osx *power_manager; float _mouse_scale(float p_scale) { @@ -203,6 +207,8 @@ public: virtual void set_borderless_window(int p_borderless); virtual bool get_borderless_window(); + virtual void set_ime_position(const Point2 &p_pos); + virtual void set_ime_intermediate_text_callback(ImeCallback p_callback, void *p_inp); virtual PowerState get_power_state(); virtual int get_power_seconds_left(); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index e884058052..6d8a6eca66 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -261,10 +261,12 @@ static bool mouse_down_control = false; @end -@interface GodotContentView : NSView { +@interface GodotContentView : NSView <NSTextInputClient> { NSTrackingArea *trackingArea; + NSMutableAttributedString *markedText; + bool imeMode; } - +- (void)cancelComposition; @end @implementation GodotContentView @@ -278,16 +280,128 @@ static bool mouse_down_control = false; - (id)init { self = [super init]; trackingArea = nil; + imeMode = false; [self updateTrackingAreas]; [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]]; + markedText = [[NSMutableAttributedString alloc] init]; return self; } - (void)dealloc { [trackingArea release]; + [markedText release]; [super dealloc]; } +static const NSRange kEmptyRange = { NSNotFound, 0 }; + +- (BOOL)hasMarkedText { + return (markedText.length > 0); +} + +- (NSRange)markedRange { + return (markedText.length > 0) ? NSMakeRange(0, markedText.length - 1) : kEmptyRange; +} + +- (NSRange)selectedRange { + return kEmptyRange; +} + +- (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange { + if ([aString isKindOfClass:[NSAttributedString class]]) { + [markedText initWithAttributedString:aString]; + } else { + [markedText initWithString:aString]; + } + if (OS_OSX::singleton->im_callback) { + imeMode = true; + String ret; + ret.parse_utf8([[markedText mutableString] UTF8String]); + OS_OSX::singleton->im_callback(OS_OSX::singleton->im_target, ret, Point2(selectedRange.location, selectedRange.length)); + } +} + +- (void)doCommandBySelector:(SEL)aSelector { + if ([self respondsToSelector:aSelector]) + [self performSelector:aSelector]; +} + +- (void)unmarkText { + imeMode = false; + [[markedText mutableString] setString:@""]; + if (OS_OSX::singleton->im_callback) + OS_OSX::singleton->im_callback(OS_OSX::singleton->im_target, "", Point2()); +} + +- (NSArray *)validAttributesForMarkedText { + return [NSArray array]; +} + +- (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { + return nil; +} + +- (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { + return 0; +} + +- (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { + const NSRect contentRect = [OS_OSX::singleton->window_view frame]; + NSRect pointInWindowRect = NSMakeRect(OS_OSX::singleton->im_position.x / OS_OSX::singleton->display_scale, contentRect.size.height - (OS_OSX::singleton->im_position.y / OS_OSX::singleton->display_scale) - 1, 0, 0); + NSPoint pointOnScreen = [[OS_OSX::singleton->window_view window] convertRectToScreen:pointInWindowRect].origin; + + return NSMakeRect(pointOnScreen.x, pointOnScreen.y, 0, 0); +} + +- (void)cancelComposition { + [self unmarkText]; + NSInputManager *currentInputManager = [NSInputManager currentInputManager]; + [currentInputManager markedTextAbandoned:self]; +} + +- (void)insertText:(id)aString { + [self insertText:aString replacementRange:NSMakeRange(0, 0)]; +} + +- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange { + NSEvent *event = [NSApp currentEvent]; + Ref<InputEventKey> k; + k.instance(); + + get_key_modifier_state([event modifierFlags], k); + k->set_pressed(true); + k->set_echo(false); + k->set_scancode(0); + + NSString *characters; + if ([aString isKindOfClass:[NSAttributedString class]]) { + characters = [aString string]; + } else { + characters = (NSString *)aString; + } + + NSUInteger i, length = [characters length]; + + NSCharacterSet *ctrlChars = [NSCharacterSet controlCharacterSet]; + NSCharacterSet *wsnlChars = [NSCharacterSet whitespaceAndNewlineCharacterSet]; + if ([characters rangeOfCharacterFromSet:ctrlChars].length && [characters rangeOfCharacterFromSet:wsnlChars].length == 0) { + NSInputManager *currentInputManager = [NSInputManager currentInputManager]; + [currentInputManager markedTextAbandoned:self]; + [self cancelComposition]; + return; + } + + for (i = 0; i < length; i++) { + const unichar codepoint = [characters characterAtIndex:i]; + if ((codepoint & 0xFF00) == 0xF700) + continue; + + k->set_unicode(codepoint); + OS_OSX::singleton->push_input(k); + } + [self cancelComposition]; +} + - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { return NSDragOperationCopy; } @@ -634,15 +748,12 @@ static int translateKey(unsigned int key) { NSString *characters = [event characters]; NSUInteger i, length = [characters length]; - if (length > 0 && keycode_has_unicode(k->get_scancode())) { - for (i = 0; i < length; i++) { - k->set_unicode([characters characterAtIndex:i]); - OS_OSX::singleton->push_input(k); - k->set_scancode(0); - } - } else { + //disable raw input in IME mode + if (!imeMode) OS_OSX::singleton->push_input(k); - } + + if ((OS_OSX::singleton->im_position.x != 0) && (OS_OSX::singleton->im_position.y != 0)) + [self interpretKeyEvents:[NSArray arrayWithObject:event]]; } - (void)flagsChanged:(NSEvent *)event { @@ -761,6 +872,18 @@ inline void sendScrollEvent(int button, double factor, int modifierFlags) { @end +void OS_OSX::set_ime_intermediate_text_callback(ImeCallback p_callback, void *p_inp) { + im_callback = p_callback; + im_target = p_inp; + if (!im_callback) { + [window_view cancelComposition]; + } +} + +void OS_OSX::set_ime_position(const Point2 &p_pos) { + im_position = p_pos; +} + int OS_OSX::get_video_driver_count() const { return 1; } @@ -1735,6 +1858,9 @@ OS_OSX::OS_OSX() { mouse_mode = OS::MOUSE_MODE_VISIBLE; main_loop = NULL; singleton = this; + im_position = Point2(); + im_callback = NULL; + im_target = NULL; autoreleasePool = [[NSAutoreleasePool alloc] init]; eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); |