-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathAppDelegate.m
490 lines (370 loc) · 15.8 KB
/
AppDelegate.m
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#import "AppDelegate.h"
#import "TouchBar.h"
#import <ServiceManagement/ServiceManagement.h>
#import "TouchButton.h"
#import "TouchDelegate.h"
#import <Cocoa/Cocoa.h>
#import <MASShortcut/Shortcut.h>
#import <CoreAudio/CoreAudio.h>
#import <AudioToolbox/AudioServices.h>
#import "ViewController.h"
static const NSTouchBarItemIdentifier muteIdentifier = @"pp.mute";
static NSString *const MASCustomShortcutKey = @"customShortcut";
@interface AppDelegate () <TouchDelegate>
@end
@implementation AppDelegate
@synthesize statusBar;
TouchButton *touchBarButton;
NSString *STATUS_ICON_BLACK = @"tray-unactive-black";
NSString *STATUS_ICON_RED = @"tray-active";
NSString *STATUS_ICON_WHITE = @"tray-unactive-white";
NSString *STATUS_ICON_OFF = @"micOff";
NSString *STATUS_ICON_ON = @"micOn";
AudioDeviceID currentAudioDeviceID = kAudioObjectUnknown;
AudioObjectPropertyListenerBlock onDefaultInputDeviceChange = NULL;
AudioObjectPropertyListenerBlock onAudioDeviceMuteChange = NULL;
- (void) awakeFromNib {
BOOL hideStatusBar = NO;
BOOL statusBarButtonToggle = NO;
BOOL useAlternateStatusBarIcons = NO;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"hide_status_bar"] != nil) {
hideStatusBar = [[NSUserDefaults standardUserDefaults] boolForKey:@"hide_status_bar"];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_button_toggle"] != nil) {
statusBarButtonToggle = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_button_toggle"];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) {
useAlternateStatusBarIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
}
[[NSUserDefaults standardUserDefaults] setBool:hideStatusBar forKey:@"hide_status_bar"];
[[NSUserDefaults standardUserDefaults] setBool:statusBarButtonToggle forKey:@"status_bar_button_toggle"];
[[NSUserDefaults standardUserDefaults] setBool:useAlternateStatusBarIcons forKey:@"status_bar_alternate_icons"];
if (!hideStatusBar) {
[self setupStatusBarItem];
}
// masshortcut
[self setShortcutKey];
}
- (void) setupStatusBarItem {
BOOL statusBarButtonToggle = NO;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_button_toggle"] != nil) {
statusBarButtonToggle = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_button_toggle"];
}
if (statusBarButtonToggle) {
NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
NSStatusBarButton *statusButton = statusItem.button;
statusButton.target = self;
statusButton.action = @selector(handleStatusButtonAction);
[statusButton sendActionOn:NSEventMaskLeftMouseUp|NSEventMaskRightMouseUp];
self.statusBar = statusItem;
} else {
self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
self.statusBar.menu = self.statusMenu;
}
NSImage* statusImage = [self getStatusBarImage];
statusImage.size = NSMakeSize(18, 18);
// allows cocoa to change the background of the icon
[statusImage setTemplate:YES];
self.statusBar.image = statusImage;
self.statusBar.highlightMode = YES;
self.statusBar.enabled = YES;
[self updateMenuItemIcon];
}
- (void) setShortcutKey {
// default shortcut is "Shift Command 0"
MASShortcut *firstLaunchShortcut = [MASShortcut shortcutWithKeyCode:kVK_ANSI_0 modifierFlags:NSEventModifierFlagCommand | NSEventModifierFlagShift];
NSData *firstLaunchShortcutData = [NSKeyedArchiver archivedDataWithRootObject:firstLaunchShortcut];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:@{ MASCustomShortcutKey : firstLaunchShortcutData }];
[defaults synchronize];
[[MASShortcutMonitor sharedMonitor] registerShortcut:firstLaunchShortcut withAction:^{
[self shortCutKeyPressed];
}];
}
- (void) hideMenuBar: (BOOL) enableState {
if (!enableState) {
[self setupStatusBarItem];
} else {
self.statusBar = nil;
}
}
- (void) shortCutKeyPressed {
[self toggleMute];
}
- (void) showMenu {
[self.statusBar popUpStatusItemMenu:self.statusMenu];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
DFRSystemModalShowsCloseBoxWhenFrontMost(YES);
NSCustomTouchBarItem *mute = [[NSCustomTouchBarItem alloc] initWithIdentifier:muteIdentifier];
NSImage *muteImage = [NSImage imageNamed:NSImageNameTouchBarAudioInputMuteTemplate];
TouchButton *button = [TouchButton buttonWithImage: muteImage target:nil action:nil];
[button setDelegate: self];
mute.view = button;
touchBarButton = button;
[NSTouchBarItem addSystemTrayItem:mute];
DFRElementSetControlStripPresenceForIdentifier(muteIdentifier, YES);
onDefaultInputDeviceChange = ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress * _Nonnull inAddresses) {
BOOL muted = false;
BOOL setMuted = false;
if (currentAudioDeviceID != kAudioObjectUnknown) {
setMuted = true;
muted = [self getInputDeviceMute:currentAudioDeviceID];
[self unlistenInputDevice:currentAudioDeviceID];
}
[self setDefaultInputDeviceAsCurrentAndListen];
if (setMuted) {
[self setInputDevice:currentAudioDeviceID mute:muted];
}
};
onAudioDeviceMuteChange = ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress * _Nonnull inAddresses) {
dispatch_async(dispatch_get_main_queue(), ^{
[self updatePresentation];
});
};
[self enableLoginAutostart];
[self listenInputDevices];
// fires if we enter / exit dark mode
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
}
- (void) unlistenCurrentDevice {
if (currentAudioDeviceID != kAudioObjectUnknown) {
[self unlistenInputDevice:currentAudioDeviceID];
}
}
- (void) setDefaultInputDeviceAsCurrentAndListen {
currentAudioDeviceID = [self getDefaultInputDevice];
if (currentAudioDeviceID != kAudioObjectUnknown) {
OSStatus error = [self listenInputDevice:currentAudioDeviceID];
if (error != noErr) {
currentAudioDeviceID = kAudioObjectUnknown;
} else {
NSLog(@"Listen device: 0x%0x", currentAudioDeviceID);
}
}
}
- (void) listenInputDevices {
AudioObjectPropertyAddress propertyAddress = [self defaultInputDevicePropertyAddress];
OSStatus error = AudioObjectAddPropertyListenerBlock(kAudioObjectSystemObject, &propertyAddress, NULL, onDefaultInputDeviceChange);
if (error != noErr) {
NSLog(@"Can't listen change of default device. Error: %d", error);
}
[self setDefaultInputDeviceAsCurrentAndListen];
}
- (OSStatus) listenInputDevice:(AudioDeviceID)deviceID {
AudioObjectPropertyAddress muteAddress = [self mutePropertyAddress];
OSStatus error = AudioObjectAddPropertyListenerBlock(deviceID, &muteAddress, NULL, onAudioDeviceMuteChange);
if (error != noErr) {
NSLog(@"Can't listen mute change of device: 0x%0x. Error: %d", deviceID, error);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self updatePresentation];
});
return error;
}
- (void) unlistenInputDevice:(AudioDeviceID)deviceID {
AudioObjectPropertyAddress muteAddress = [self mutePropertyAddress];
OSStatus error = AudioObjectRemovePropertyListenerBlock(deviceID, &muteAddress, NULL, onAudioDeviceMuteChange);
if (error != noErr) {
NSLog(@"Can't unlisten mute change of device: 0x%0x. Error: %d", deviceID, error);
}
[self setInputDevice:deviceID mute:false];
}
- (void) updatePresentation {
if (currentAudioDeviceID == kAudioObjectUnknown) {
return;
}
BOOL muted = [self getInputDeviceMute:currentAudioDeviceID];
[self updateTouchBarButtonWithMuted:muted];
[self updateStatusBarIconWithMuted:muted];
}
- (void) updateTouchBarButtonWithMuted:(BOOL)muted {
[touchBarButton setBezelColor: [self colorForMuted: muted]];
[touchBarButton layout];
}
- (void) updateStatusBarIconWithMuted:(BOOL)muted {
[self setStatusBarImgRed: muted];
}
-(void)darkModeChanged:(NSNotification *)notif {
[self updatePresentation];
}
- (NSImage*) getStatusBarImage {
BOOL useAlternateIcons = NO;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) {
useAlternateIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
}
NSImage* statusImage;
if (useAlternateIcons) {
statusImage = [NSImage imageNamed:STATUS_ICON_ON];
} else {
statusImage = [NSImage imageNamed:STATUS_ICON_BLACK];
// see https://stackoverflow.com/questions/25379525/how-to-detect-dark-mode-in-yosemite-to-change-the-status-bar-menu-icon
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:@"AppleInterfaceStyle"];
BOOL darkModeOn = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );
if (darkModeOn) {
statusImage = [NSImage imageNamed:STATUS_ICON_WHITE];
}
}
return statusImage;
}
- (void) setStatusBarImgRed:(BOOL) shouldBeRed {
NSImage* statusImage = [self getStatusBarImage];
if (shouldBeRed) {
BOOL useAlternateIcons = NO;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) {
useAlternateIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
}
if (useAlternateIcons) {
statusImage = [NSImage imageNamed:STATUS_ICON_OFF];
[statusImage setTemplate:YES];
} else {
statusImage = [NSImage imageNamed:STATUS_ICON_RED];
[statusImage setTemplate:!shouldBeRed];
}
}
statusImage.size = NSMakeSize(18, 18);
self.statusBar.image = statusImage;
}
- (BOOL) setAudioObject:(AudioObjectID)audioObjectID
property:(AudioObjectPropertyAddress)propertyAddress
dataSize:(UInt32)dataSize
data:(const void *)data
{
if (!AudioObjectHasProperty(audioObjectID, &propertyAddress) ) {
NSLog(@"No property for audioObject 0x%0x", audioObjectID);
return false;
}
Boolean settable;
OSStatus theError = AudioObjectIsPropertySettable(audioObjectID, &propertyAddress, &settable);
if (theError != noErr || !settable) {
NSLog(@"The property of audioObject 0x%0x cannot be set", audioObjectID);
return false;
}
//now read the property and correct it, if outside [0...1]
theError = AudioObjectSetPropertyData(audioObjectID, &propertyAddress, 0, NULL, dataSize, data);
if (theError != noErr) {
NSLog(@"Unable to set property for audioObject 0x%0x", audioObjectID);
return false;
}
return true;
}
- (BOOL) getAudioObject:(AudioObjectID)audioObjectID
property:(AudioObjectPropertyAddress)propertyAddress
dataSize:(UInt32)dataSize
data:(void *)data
{
if (!AudioObjectHasProperty(audioObjectID, &propertyAddress) ) {
NSLog(@"No property for audioObject 0x%0x", audioObjectID);
return false;
}
OSStatus theError = AudioObjectGetPropertyData(audioObjectID, &propertyAddress, 0, NULL, &dataSize, data);
if (theError != noErr) {
NSLog(@"Unable to read property for audioObject 0x%0x", audioObjectID);
return false;
}
return true;
}
- (AudioObjectPropertyAddress) mutePropertyAddress {
AudioObjectPropertyAddress propertyAddress = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster
};
return propertyAddress;
}
- (AudioObjectPropertyAddress) defaultInputDevicePropertyAddress {
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
return propertyAddress;
}
- (BOOL) getInputDeviceMute:(AudioDeviceID)deviceID {
AudioObjectPropertyAddress propertyAddress = [self mutePropertyAddress];
UInt32 data;
BOOL success = [self getAudioObject:deviceID property:propertyAddress dataSize:sizeof(data) data:&data];
return success ? data == 1 : false;
}
- (void) setInputDevice:(AudioDeviceID)deviceID mute:(BOOL)mute {
UInt32 value = mute ? 1 : 0;
AudioObjectPropertyAddress propertyAddress = [self mutePropertyAddress];
BOOL s = [self setAudioObject:deviceID property:propertyAddress dataSize:sizeof(value) data:&value];
if (s) {
printf("123");
AudioObjectShow(deviceID);
NSLog(@"Set property for audioObject 0x%0x to %d", deviceID, value);
}
}
-(void) enableLoginAutostart {
// on the first run this should be nil. So don't setup auto run
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] == nil) {
return;
}
BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"auto_login"];
if(!SMLoginItemSetEnabled((__bridge CFStringRef)@"Pixel-Point.Mute-Me-Now-Launcher", !state)) {
NSLog(@"The login was not succesfull");
}
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
[self unlistenCurrentDevice];
}
- (AudioDeviceID) getDefaultInputDevice {
AudioDeviceID defaultDevice = kAudioObjectUnknown;
AudioObjectPropertyAddress address = [self defaultInputDevicePropertyAddress];
[self getAudioObject:kAudioObjectSystemObject property:address dataSize:sizeof(AudioDeviceID) data:&defaultDevice];
return defaultDevice;
}
-(NSColor *)colorForMuted:(BOOL)muted {
if(muted) {
return NSColor.redColor;
} else {
return NSColor.clearColor;
}
}
- (void)onPressed:(TouchButton*)sender {
[self toggleMute];
}
- (void) toggleMute {
if (currentAudioDeviceID == kAudioObjectUnknown) {
NSLog(@"Can't toggle mute. No audio device.");
return;
}
BOOL isMuted = [self getInputDeviceMute:currentAudioDeviceID];
[self setInputDevice:currentAudioDeviceID mute:!isMuted];
}
- (void) openPrefsWindow {
NSStoryboard *mainStoryboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
NSWindowController *prefsWindowController = [mainStoryboard instantiateControllerWithIdentifier:@"prefsWindowController"];
[prefsWindowController showWindow:self];
[[NSApplication sharedApplication] activateIgnoringOtherApps:true];
}
- (void)onLongPressed:(TouchButton*)sender {
[self openPrefsWindow];
}
- (IBAction)muteMenuItemAction:(id)sender {
[self toggleMute];
}
- (IBAction)prefsMenuItemAction:(id)sender {
[self openPrefsWindow];
}
- (IBAction)quitMenuItemAction:(id)sender {
[NSApp terminate:nil];
}
- (void) updateMenuItemIcon {
if (self.muteMenuItem.state == NSOnState) {
[self setStatusBarImgRed:YES];
} else {
[self setStatusBarImgRed:NO];
}
}
- (void) handleStatusButtonAction {
NSEvent *event = [[NSApplication sharedApplication] currentEvent];
if ((event.modifierFlags & NSEventModifierFlagControl) || (event.modifierFlags & NSEventModifierFlagOption) || (event.type == NSEventTypeRightMouseUp)) {
[self showMenu];
return;
}
[self toggleMute];
}
@end