From f7513accd109be011e60a6827f7ab6207d352486 Mon Sep 17 00:00:00 2001
From: Allan Odgaard <git@abetarda.com>
Date: Fri, 14 Feb 2014 19:20:59 +0700
Subject: [PATCH] macosx: Pause Spotify when video starts

This mimics the current iTunes support and behavior is controlled by the same setting.
---
 modules/gui/macosx/Spotify.h      | 16 ++++++++++++
 modules/gui/macosx/intf.h         |  3 ++-
 modules/gui/macosx/intf.m         | 52 ++++++++++++++++++++++++++++-----------
 modules/gui/macosx/macosx.m       |  6 ++---
 modules/gui/macosx/simple_prefs.m |  2 +-
 5 files changed, 59 insertions(+), 20 deletions(-)
 create mode 100644 modules/gui/macosx/Spotify.h

diff --git modules/gui/macosx/Spotify.h modules/gui/macosx/Spotify.h
new file mode 100644
index 0000000..097163a
--- /dev/null
+++ modules/gui/macosx/Spotify.h
@@ -0,0 +1,16 @@
+/*
+ * Spotify.h
+ */
+
+#import <AppKit/AppKit.h>
+#import <ScriptingBridge/ScriptingBridge.h>
+
+OSType const kSpotifyPlayerStateStopped = 'kPSS';
+OSType const kSpotifyPlayerStatePlaying = 'kPSP';
+OSType const kSpotifyPlayerStatePaused  = 'kPSp';
+
+@interface SpotifyApplication : SBApplication
+@property (readonly) OSType playerState;  // is Spotify stopped, paused, or playing?
+- (void)play;
+- (void)pause;
+@end
diff --git modules/gui/macosx/intf.h modules/gui/macosx/intf.h
index 3fae0e9..bda8cf8 100644
--- modules/gui/macosx/intf.h
+++ modules/gui/macosx/intf.h
@@ -125,8 +125,9 @@ struct intf_sys_t
 
     VLCVoutWindowController *o_vout_controller;
 
-    /* iTunes play/pause support */
+    /* iTunes/Spotify play/pause support */
     BOOL b_has_itunes_paused;
+    BOOL b_has_spotify_paused;
     NSTimer *o_itunes_play_timer;
 
     BOOL b_playlist_updated_selector_in_queue;
diff --git modules/gui/macosx/intf.m modules/gui/macosx/intf.m
index 5ed847d..9a876044 100644
--- modules/gui/macosx/intf.m
+++ modules/gui/macosx/intf.m
@@ -72,6 +72,7 @@
 #import <Sparkle/Sparkle.h>                 /* we're the update delegate */
 
 #import "iTunes.h"
+#import "Spotify.h"
 
 /*****************************************************************************
  * Local prototypes.
@@ -1411,18 +1412,28 @@ static VLCMain *_o_sharedMainInstance = nil;
 
 - (void)resumeItunesPlayback:(id)sender
 {
-    if (b_has_itunes_paused && var_InheritInteger(p_intf, "macosx-control-itunes") > 1) {
-        iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
-        if (iTunesApp && [iTunesApp isRunning]) {
-            if ([iTunesApp playerState] == iTunesEPlSPaused) {
-                msg_Dbg(p_intf, "Unpause iTunes...");
-                [iTunesApp playpause];
+    if (var_InheritInteger(p_intf, "macosx-control-itunes") > 1) {
+        if (b_has_itunes_paused) {
+            iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
+            if (iTunesApp && [iTunesApp isRunning]) {
+                if ([iTunesApp playerState] == iTunesEPlSPaused) {
+                    msg_Dbg(p_intf, "Unpause iTunes...");
+                    [iTunesApp playpause];
+                }
             }
         }
 
+        if (b_has_spotify_paused) {
+            SpotifyApplication *spotifyApp = [SBApplication applicationWithBundleIdentifier:@"com.spotify.client"];
+            if ([spotifyApp isRunning] && [spotifyApp playerState] == kSpotifyPlayerStatePaused) {
+                msg_Dbg(p_intf, "Unpause Spotify...");
+                [spotifyApp play];
+            }
+        }
     }
 
     b_has_itunes_paused = NO;
+    b_has_spotify_paused = NO;
     o_itunes_play_timer = nil;
 }
 
@@ -1443,18 +1454,29 @@ static VLCMain *_o_sharedMainInstance = nil;
     }
 
     if (state == PLAYING_S) {
-        // pause iTunes
-        if (i_control_itunes > 0 && !b_has_itunes_paused) {
-            iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
-            if (iTunesApp && [iTunesApp isRunning]) {
-                if ([iTunesApp playerState] == iTunesEPlSPlaying) {
-                    msg_Dbg(p_intf, "Pause iTunes...");
-                    [iTunesApp pause];
-                    b_has_itunes_paused = YES;
+        if (i_control_itunes > 0) {
+            // pause iTunes
+            if (!b_has_itunes_paused) {
+                iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
+                if (iTunesApp && [iTunesApp isRunning]) {
+                    if ([iTunesApp playerState] == iTunesEPlSPlaying) {
+                        msg_Dbg(p_intf, "Pause iTunes...");
+                        [iTunesApp pause];
+                        b_has_itunes_paused = YES;
+                    }
                 }
             }
-        }
 
+            // pause Spotify
+            if (!b_has_spotify_paused) {
+                SpotifyApplication *spotifyApp = [SBApplication applicationWithBundleIdentifier:@"com.spotify.client"];
+                if ([spotifyApp isRunning] && [spotifyApp playerState] == kSpotifyPlayerStatePlaying) {
+                    msg_Dbg(p_intf, "Pause Spotify...");
+                    [spotifyApp pause];
+                    b_has_spotify_paused = YES;
+                }
+            }
+        }
 
         /* Declare user activity.
          This wakes the display if it is off, and postpones display sleep according to the users system preferences
diff --git modules/gui/macosx/macosx.m modules/gui/macosx/macosx.m
index 5b9047c..8944d21 100644
--- modules/gui/macosx/macosx.m
+++ modules/gui/macosx/macosx.m
@@ -121,13 +121,13 @@ void WindowClose  (vout_window_t *);
 #define SIDEBAR_TEXT N_("Show Sidebar")
 #define SIDEBAR_LONGTEXT N_("Shows a sidebar in the main window listing media sources.")
 
-#define ITUNES_TEXT N_("Pause iTunes during VLC playback")
-#define ITUNES_LONGTEXT N_("Pauses iTunes playback when VLC playback starts. If selected, iTunes playback will be resumed again if VLC playback is finished.")
+#define ITUNES_TEXT N_("Control external music players")
+#define ITUNES_LONGTEXT N_("When a video starts playing, VLC will pause supported music players. If selected, external player will be resumed again when playback is finished.")
 
 static const int itunes_list[] =
     { 0, 1, 2 };
 static const char *const itunes_list_text[] = {
-    N_("Do nothing"), N_("Pause iTunes"), N_("Pause and resume iTunes")
+    N_("Do nothing"), N_("Pause iTunes / Spotify"), N_("Pause and resume iTunes / Spotify")
 };
 
 #define VOLUME_MAX_TEXT N_("Maximum Volume displayed")
diff --git modules/gui/macosx/simple_prefs.m modules/gui/macosx/simple_prefs.m
index 2f7f92e..6947af9 100644
--- modules/gui/macosx/simple_prefs.m
+++ modules/gui/macosx/simple_prefs.m
@@ -371,7 +371,7 @@ create_toolbar_item(NSString * o_itemIdent, NSString * o_name, NSString * o_desc
     [o_intf_pauseminimized_ckb setTitle: _NS("Pause the video playback when minimized")];
     [o_intf_luahttp_box setTitle:_NS("Lua HTTP")];
     [o_intf_luahttppwd_lbl setStringValue:_NS("Password")];
-    [o_intf_pauseitunes_lbl setStringValue:_NS("Control iTunes during playback")];
+    [o_intf_pauseitunes_lbl setStringValue:_NS("Control external music players")];
 
     /* Subtitles and OSD */
     [o_osd_encoding_txt setStringValue: _NS("Default Encoding")];
-- 
1.8.5.3

