[Visualization] Gate registered observers

Gate registering observers behind a check variable in the object state,
which will be zero initialized by the Objective-C runtime, and will
prevent the class from erroneously unregistering observers without them
being registered in the first place. Apparently, services cannot be
unregistered if they were never registered first, or else an exception
will be thrown upon attempting to unregister them. This fixes a crash on
startup in the now several times running failure to work properly on
legacy Macs without Metal graphics, now that the new visualization
system has been implemented.

It's probably not worth bringing back the previous Core Graphics based
visualizer method anyway, as on those same legacy machines, it was
causing out of control CPU usage by WindowServer. At least on modern
Macs, any amount of 60fps UI updating will cause WindowServer to use
about 45% of a core, regardless of how many apps are drawing that much
at once.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-05-30 21:38:29 -07:00
parent 94057cf467
commit aabd9431f1
1 changed files with 51 additions and 34 deletions

View File

@ -28,6 +28,7 @@ extern NSString *CogPlaybackDidStopNotficiation;
BOOL isListening; BOOL isListening;
BOOL bandsReset; BOOL bandsReset;
BOOL cameraControlEnabled; BOOL cameraControlEnabled;
BOOL observersRegistered;
NSColor *backgroundColor; NSColor *backgroundColor;
ddb_analyzer_t _analyzer; ddb_analyzer_t _analyzer;
@ -171,6 +172,11 @@ extern NSString *CogPlaybackDidStopNotficiation;
_analyzer.freq_is_log = 0; _analyzer.freq_is_log = 0;
_analyzer.mode = freqMode ? DDB_ANALYZER_MODE_FREQUENCIES : DDB_ANALYZER_MODE_OCTAVE_NOTE_BANDS; _analyzer.mode = freqMode ? DDB_ANALYZER_MODE_FREQUENCIES : DDB_ANALYZER_MODE_OCTAVE_NOTE_BANDS;
[self registerObservers];
}
- (void)registerObservers {
if(!observersRegistered) {
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumProjectionMode" options:0 context:kSpectrumViewContext]; [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumProjectionMode" options:0 context:kSpectrumViewContext];
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewContext]; [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewContext];
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewContext]; [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewContext];
@ -191,12 +197,20 @@ extern NSString *CogPlaybackDidStopNotficiation;
selector:@selector(playbackDidStop:) selector:@selector(playbackDidStop:)
name:CogPlaybackDidStopNotficiation name:CogPlaybackDidStopNotficiation
object:nil]; object:nil];
observersRegistered = YES;
}
} }
- (void)dealloc { - (void)dealloc {
ddb_analyzer_dealloc(&_analyzer); ddb_analyzer_dealloc(&_analyzer);
ddb_analyzer_draw_data_dealloc(&_draw_data); ddb_analyzer_draw_data_dealloc(&_draw_data);
[self removeObservers];
}
- (void)removeObservers {
if(observersRegistered) {
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumProjectionMode"]; [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumProjectionMode"];
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumBarColor"]; [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumBarColor"];
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumDotColor"]; [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumDotColor"];
@ -213,6 +227,9 @@ extern NSString *CogPlaybackDidStopNotficiation;
[[NSNotificationCenter defaultCenter] removeObserver:self [[NSNotificationCenter defaultCenter] removeObserver:self
name:CogPlaybackDidStopNotficiation name:CogPlaybackDidStopNotficiation
object:nil]; object:nil];
observersRegistered = NO;
}
} }
- (void)repaint { - (void)repaint {