[vlc-commits] macosx: added an option to let the Apple Remote control the system volume instead of VLC 's volume (close #1071)

Felix Paul Kühne git at videolan.org
Sun May 20 12:45:58 CEST 2012


vlc | branch: master | Felix Paul Kühne <fkuehne at videolan.org> | Sun May 20 12:45:27 2012 +0200| [dd0b62080541e5f4e4854c95280a0c81ba78628c] | committer: Felix Paul Kühne

macosx: added an option to let the Apple Remote control the system volume instead of VLC's volume (close #1071)

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=dd0b62080541e5f4e4854c95280a0c81ba78628c
---

 configure.ac                |    2 +-
 modules/gui/macosx/intf.m   |   10 ++++-
 modules/gui/macosx/macosx.m |    4 ++
 modules/gui/macosx/misc.h   |   18 +++++++-
 modules/gui/macosx/misc.m   |   96 ++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 125 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1ef2bb7..fb83def 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3767,7 +3767,7 @@ then
   VLC_ADD_OBJCFLAGS([macosx minimal_macosx], [-fobjc-exceptions] )
   VLC_ADD_PLUGIN([macosx minimal_macosx])
 
-  VLC_ADD_LIBS([macosx], [-Wl,-framework,QTKit -Wl,-framework,IOKit -Wl,-framework,AddressBook -Wl,-framework,WebKit])
+  VLC_ADD_LIBS([macosx], [-Wl,-framework,QTKit -Wl,-framework,IOKit -Wl,-framework,AddressBook -Wl,-framework,WebKit -Wl,-framework,CoreAudio])
 
   if test ! -d ${CONTRIB_DIR}/Sparkle.framework
   then
diff --git a/modules/gui/macosx/intf.m b/modules/gui/macosx/intf.m
index a997f5d..1161b7f 100644
--- a/modules/gui/macosx/intf.m
+++ b/modules/gui/macosx/intf.m
@@ -1003,10 +1003,16 @@ static VLCMain *_o_sharedMainInstance = nil;
             }
             break;
         case kRemoteButtonVolume_Plus:
-            [[VLCCoreInteraction sharedInstance] volumeUp];
+            if (config_GetInt( VLCIntf, "macosx-appleremote-sysvol"))
+                [NSSound increaseSystemVolume];
+            else
+                [[VLCCoreInteraction sharedInstance] volumeUp];
             break;
         case kRemoteButtonVolume_Minus:
-            [[VLCCoreInteraction sharedInstance] volumeDown];
+            if (config_GetInt( VLCIntf, "macosx-appleremote-sysvol"))
+                [NSSound decreaseSystemVolume];
+            else
+                [[VLCCoreInteraction sharedInstance] volumeDown];
             break;
         case kRemoteButtonRight:
             [[VLCCoreInteraction sharedInstance] next];
diff --git a/modules/gui/macosx/macosx.m b/modules/gui/macosx/macosx.m
index 427f99f..59f4615 100644
--- a/modules/gui/macosx/macosx.m
+++ b/modules/gui/macosx/macosx.m
@@ -80,6 +80,9 @@ void WindowClose  ( vout_window_t * );
 #define USE_APPLE_REMOTE_TEXT N_("Control playback with the Apple Remote")
 #define USE_APPLE_REMOTE_LONGTEXT N_("By default, VLC can be remotely controlled with the Apple Remote.")
 
+#define USE_APPLE_REMOTE_VOLUME_TEXT N_("Control system volume with the Apple Remote")
+#define USE_APPLE_REMOTE_VOLUME_LONGTEXT N_("By default, VLC will control its own volume with the Apple Remote. However, you can choose to control the global system volume instead.")
+
 #define USE_MEDIAKEYS_TEXT N_("Control playback with media keys")
 #define USE_MEDIAKEYS_LONGTEXT N_("By default, VLC can be controlled using the media keys on modern Apple " \
                                   "keyboards.")
@@ -124,6 +127,7 @@ vlc_module_begin ()
     add_bool( "macosx-recentitems", true, RECENT_ITEMS_TEXT, RECENT_ITEMS_LONGTEXT, false )
     add_bool( "macosx-fspanel", true, FSPANEL_TEXT, FSPANEL_LONGTEXT, false )
     add_bool( "macosx-appleremote", true, USE_APPLE_REMOTE_TEXT, USE_APPLE_REMOTE_LONGTEXT, false )
+    add_bool( "macosx-appleremote-sysvol", false, USE_APPLE_REMOTE_VOLUME_TEXT, USE_APPLE_REMOTE_VOLUME_LONGTEXT, false )
     add_bool( "macosx-mediakeys", true, USE_MEDIAKEYS_TEXT, USE_MEDIAKEYS_LONGTEXT, false )
     add_bool( "macosx-interfacestyle", false, INTERFACE_STYLE_TEXT, INTERFACE_STYLE_LONGTEXT, false )
     add_bool( "macosx-nativefullscreenmode", false, NATIVE_FULLSCREEN_MODE_ON_LION_TEXT, NATIVE_FULLSCREEN_MODE_ON_LION_LONGTEXT, false )
diff --git a/modules/gui/macosx/misc.h b/modules/gui/macosx/misc.h
index 91a8351..1703927 100644
--- a/modules/gui/macosx/misc.h
+++ b/modules/gui/macosx/misc.h
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * misc.h: code not specific to vlc
  *****************************************************************************
- * Copyright (C) 2003-2011 VLC authors and VideoLAN
+ * Copyright (C) 2003-2012 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl at nanocrew.net>
@@ -26,6 +26,22 @@
 #import "CompatibilityFixes.h"
 
 /*****************************************************************************
+ * NSSound (VLCAdditions)
+ *
+ * added code to change the system volume, needed for the apple remote code
+ * this is simplified code, which won't let you set the exact volume
+ * (that's what the audio output is for after all), but just the system volume
+ * in steps of 1/16 (matching the default AR or volume key implementation).
+ *****************************************************************************/
+
+ at interface NSSound (VLCAdditions)
++ (float)systemVolumeForChannel:(int)channel;
++ (bool)setSystemVolume:(float)volume forChannel:(int)channel;
++ (void)increaseSystemVolume;
++ (void)decreaseSystemVolume;
+ at end
+
+/*****************************************************************************
  * NSAnimation (VLCAddition)
  *****************************************************************************/
 
diff --git a/modules/gui/macosx/misc.m b/modules/gui/macosx/misc.m
index 6bcc488..fd4868a 100644
--- a/modules/gui/macosx/misc.m
+++ b/modules/gui/macosx/misc.m
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * misc.m: code not specific to vlc
  *****************************************************************************
- * Copyright (C) 2003-2011 VLC authors and VideoLAN
+ * Copyright (C) 2003-2012 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl at nanocrew.net>
@@ -27,6 +27,100 @@
 #import "MainWindow.h"
 #import "controls.h"
 #import "CoreInteraction.h"
+#import <CoreAudio/CoreAudio.h>
+
+/*****************************************************************************
+ * NSSound (VLCAdditions)
+ *
+ * added code to change the system volume, needed for the apple remote code
+ * this is simplified code, which won't let you set the exact volume
+ * (that's what the audio output is for after all), but just the system volume
+ * in steps of 1/16 (matching the default AR or volume key implementation).
+ *****************************************************************************/
+
+ at implementation NSSound (VLCAdditions)
+
++ (float)systemVolumeForChannel:(int)channel
+{
+    AudioDeviceID i_device;
+    float f_volume;
+    OSStatus err;
+    UInt32 i_size;
+
+    i_size = sizeof( i_device );
+    AudioObjectPropertyAddress deviceAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
+    err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &deviceAddress, 0, NULL, &i_size, &i_device );
+    if (err != noErr)
+    {
+        msg_Warn( VLCIntf, "couldn't get main audio output device" );
+        return .0;
+    }
+
+    AudioObjectPropertyAddress propertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, channel };
+    i_size = sizeof( f_volume );
+    err = AudioObjectGetPropertyData(i_device, &propertyAddress, 0, NULL, &i_size, &f_volume);
+    if (err != noErr)
+    {
+        msg_Warn( VLCIntf, "couldn't get volume value" );
+        return .0;
+    }
+
+    return f_volume;
+}
+
++ (bool)setSystemVolume:(float)f_volume forChannel:(int)i_channel
+{
+    /* the following code will fail on S/PDIF devices. there is an easy work-around, but we'd like to match the OS behavior */
+
+    AudioDeviceID i_device;
+    OSStatus err;
+    UInt32 i_size;
+    Boolean b_writeable;
+
+    i_size = sizeof( i_device );
+    AudioObjectPropertyAddress deviceAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
+    err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &deviceAddress, 0, NULL, &i_size, &i_device );
+    if (err != noErr)
+    {
+        msg_Warn( VLCIntf, "couldn't get main audio output device" );
+        return NO;
+    }
+
+    AudioObjectPropertyAddress propertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, i_channel };
+    i_size = sizeof( f_volume );
+    err = AudioObjectIsPropertySettable( i_device, &propertyAddress, &b_writeable );
+    if (err != noErr || !b_writeable ) {
+        msg_Warn( VLCIntf, "we can't set the main audio devices' volume" );
+        return NO;
+    }
+    err = AudioObjectSetPropertyData(i_device, &propertyAddress, 0, NULL, i_size, &f_volume);
+
+    return YES;
+}
+
++ (void)increaseSystemVolume
+{
+    float f_volume = [NSSound systemVolumeForChannel:1]; // we trust that mono is always available and that all channels got the same volume
+    f_volume += .0625; // 1/16 to match the OS
+    bool b_returned = YES;
+
+    /* since core audio doesn't provide a reasonable way to see how many channels we got, let's see how long we can do this */
+    for (NSUInteger x = 1; b_returned ; x++)
+        b_returned = [NSSound setSystemVolume: f_volume forChannel:x];
+}
+
++ (void)decreaseSystemVolume
+{
+    float f_volume = [NSSound systemVolumeForChannel:1]; // we trust that mono is always available and that all channels got the same volume
+    f_volume -= .0625; // 1/16 to match the OS
+    bool b_returned = YES;
+
+    /* since core audio doesn't provide a reasonable way to see how many channels we got, let's see how long we can do this */
+    for (NSUInteger x = 1; b_returned ; x++)
+        b_returned = [NSSound setSystemVolume: f_volume forChannel:x];
+}
+
+ at end
 
 /*****************************************************************************
  * NSAnimation (VLCAdditions)



More information about the vlc-commits mailing list