From fc52df9ea0941d21d20ce740eb81758641a46285 Mon Sep 17 00:00:00 2001
From: Allan Odgaard <git@abetarda.com>
Date: Fri, 14 Feb 2014 00:28:31 +0700
Subject: [PATCH 2/2] Pause Spotify when video playback starts

This mimics the current iTunes behavior. For resuming, we piggyback on the iTunes resume timer, it might make sense to rename this timer and associated trigger method to indicate that it is for multiple third party music players.
---
 modules/gui/macosx/Spotify.h | 16 ++++++++++++++++
 modules/gui/macosx/intf.h    |  3 ++-
 modules/gui/macosx/intf.m    | 22 +++++++++++++++++++++-
 modules/gui/macosx/macosx.m  | 11 +++++++++++
 4 files changed, 50 insertions(+), 2 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 0582a2e..bf0cc43 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.
@@ -1422,7 +1423,16 @@ static VLCMain *_o_sharedMainInstance = nil;
 
     }
 
+    if (b_has_spotify_paused && var_InheritInteger(p_intf, "macosx-control-spotify") > 1) {
+        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;
 }
 
@@ -1434,6 +1444,7 @@ static VLCMain *_o_sharedMainInstance = nil;
     }
 
     int i_control_itunes = var_InheritInteger(p_intf, "macosx-control-itunes");
+    int i_control_spotify = var_InheritInteger(p_intf, "macosx-control-spotify");
     // cancel itunes timer if next item starts playing
     if (state > -1 && state != END_S) {
         if (o_itunes_play_timer) {
@@ -1455,6 +1466,15 @@ static VLCMain *_o_sharedMainInstance = nil;
             }
         }
 
+        // pause Spotify
+        if (i_control_spotify > 0 && !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
@@ -1512,7 +1532,7 @@ static VLCMain *_o_sharedMainInstance = nil;
         }
 
         if (state == END_S || state == -1) {
-            if (i_control_itunes > 0) {
+            if (i_control_itunes > 0 || i_control_spotify > 0) {
                 if (o_itunes_play_timer) {
                     [o_itunes_play_timer invalidate];
                 }
diff --git modules/gui/macosx/macosx.m modules/gui/macosx/macosx.m
index 5b9047c..2e7cd84 100644
--- modules/gui/macosx/macosx.m
+++ modules/gui/macosx/macosx.m
@@ -130,6 +130,15 @@ static const char *const itunes_list_text[] = {
     N_("Do nothing"), N_("Pause iTunes"), N_("Pause and resume iTunes")
 };
 
+#define SPOTIFY_TEXT N_("Pause Spotify during VLC playback")
+#define SPOTIFY_LONGTEXT N_("Pauses Spotify playback when VLC playback starts. If selected, Spotify playback will be resumed again if VLC playback is finished.")
+
+static const int spotify_list[] =
+    { 0, 1, 2 };
+static const char *const spotify_list_text[] = {
+    N_("Do nothing"), N_("Pause Spotify"), N_("Pause and resume Spotify")
+};
+
 #define VOLUME_MAX_TEXT N_("Maximum Volume displayed")
 
 
@@ -162,6 +171,8 @@ vlc_module_begin()
         add_bool("macosx-lock-aspect-ratio", true, LOCK_ASPECT_RATIO_TEXT, LOCK_ASPECT_RATIO_TEXT, true)
         add_integer("macosx-control-itunes", 1, ITUNES_TEXT, ITUNES_LONGTEXT, false)
         change_integer_list(itunes_list, itunes_list_text)
+        add_integer("macosx-control-spotify", 1, SPOTIFY_TEXT, SPOTIFY_LONGTEXT, false)
+        change_integer_list(spotify_list, spotify_list_text)
 
     set_section(N_("Apple Remote and media keys"), 0)
         add_bool("macosx-appleremote", true, USE_APPLE_REMOTE_TEXT, USE_APPLE_REMOTE_LONGTEXT, false)
-- 
1.8.5.3

