]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/applescript.m
vlc.desktop: add missing --started-from-file option (fixes #8839)
[vlc] / modules / gui / macosx / applescript.m
index 6fc5528381a48fea09f5e0719af7b9e76407a8a1..4c58f8ddc106ddba32d8f1456955e0881cd6e572 100644 (file)
@@ -1,16 +1,17 @@
 /*****************************************************************************
  * applescript.m: MacOS X AppleScript support
  *****************************************************************************
- * Copyright (C) 2002-2003 VideoLAN
- * $Id: applescript.m,v 1.4 2003/07/23 01:13:47 gbazin Exp $
+ * Copyright (C) 2002-2013 VLC authors and VideoLAN
+ * $Id$
  *
  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
+ *          Felix Paul Kühne <fkuehne at videolan dot org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "intf.h"
-#include "applescript.h"
-#include "controls.h"
-#include "open.h"
-
+#import "intf.h"
+#import "applescript.h"
+#import "CoreInteraction.h"
+#import "playlist.h"
 
 /*****************************************************************************
- * VLGetURLScriptCommand implementation 
+ * VLGetURLScriptCommand implementation
  *****************************************************************************/
 @implementation VLGetURLScriptCommand
 
     NSString *o_command = [[self commandDescription] commandName];
     NSString *o_urlString = [self directParameter];
 
-    if ( [o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"] )
-    {
-        intf_thread_t * p_intf = [NSApp getIntf];
-        playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                                        FIND_ANYWHERE );
-        if( p_playlist == NULL )
-        {
-            return nil;
-        }
+    if ([o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"]) {
+        if (o_urlString) {
+            BOOL b_autoplay = config_GetInt(VLCIntf, "macosx-autoplay");
+            NSURL * o_url = [NSURL fileURLWithPath: o_urlString];
+            if (o_url != nil)
+                [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_url];
 
-        if ( o_urlString )
-        {
-            NSURL * o_url;
-    
-            int i_mode = PLAYLIST_INSERT | PLAYLIST_GO;
-            
-            playlist_Add( p_playlist, [o_urlString fileSystemRepresentation],
-                          0, 0, i_mode, PLAYLIST_END );
-
-            o_url = [NSURL fileURLWithPath: o_urlString];
-            if( o_url != nil )
-            { 
-                [[NSDocumentController sharedDocumentController]
-                    noteNewRecentDocumentURL: o_url]; 
-            }
+            NSArray *o_result = @[[NSDictionary dictionaryWithObject:o_urlString forKey: @"ITEM_URL"]];
+
+            if (b_autoplay)
+                [[[VLCMain sharedInstance] playlist] appendArray: o_result atPos: -1 enqueue: NO];
+            else
+                [[[VLCMain sharedInstance] playlist] appendArray: o_result atPos: -1 enqueue: YES];
         }
-        vlc_object_release( p_playlist );
     }
     return nil;
 }
@@ -74,7 +61,7 @@
 
 
 /*****************************************************************************
- * VLControlScriptCommand implementation 
+ * VLControlScriptCommand implementation
  *****************************************************************************/
 /*
  * This entire control command needs a better design. more object oriented.
 
 - (id)performDefaultImplementation {
     NSString *o_command = [[self commandDescription] commandName];
+    NSString *o_parameter = [self directParameter];
+
+    intf_thread_t * p_intf = VLCIntf;
+    playlist_t * p_playlist = pl_Get(p_intf);
 
-    intf_thread_t * p_intf = [NSApp getIntf];
-    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                                    FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return nil;
+    if ([o_command isEqualToString:@"play"])
+        [[VLCCoreInteraction sharedInstance] playOrPause];
+    else if ([o_command isEqualToString:@"stop"])
+        [[VLCCoreInteraction sharedInstance] stop];
+    else if ([o_command isEqualToString:@"previous"])
+        [[VLCCoreInteraction sharedInstance] previous];
+    else if ([o_command isEqualToString:@"next"])
+        [[VLCCoreInteraction sharedInstance] next];
+    else if ([o_command isEqualToString:@"fullscreen"])
+        [[VLCCoreInteraction sharedInstance] toggleFullscreen];
+    else if ([o_command isEqualToString:@"mute"])
+        [[VLCCoreInteraction sharedInstance] setMute: YES];
+    else if ([o_command isEqualToString:@"volumeUp"])
+        [[VLCCoreInteraction sharedInstance] volumeUp];
+    else if ([o_command isEqualToString:@"volumeDown"])
+        [[VLCCoreInteraction sharedInstance] volumeDown];
+    else if ([o_command isEqualToString:@"stepForward"]) {
+        //default: forwardShort
+        if (o_parameter) {
+            int i_parameter = [o_parameter intValue];
+            switch (i_parameter) {
+                case 1:
+                    [[VLCCoreInteraction sharedInstance] forwardExtraShort];
+                    break;
+                case 2:
+                    [[VLCCoreInteraction sharedInstance] forwardShort];
+                    break;
+                case 3:
+                    [[VLCCoreInteraction sharedInstance] forwardMedium];
+                    break;
+                case 4:
+                    [[VLCCoreInteraction sharedInstance] forwardLong];
+                    break;
+                default:
+                    [[VLCCoreInteraction sharedInstance] forwardShort];
+                    break;
+            }
+        } else
+            [[VLCCoreInteraction sharedInstance] forwardShort];
+    } else if ([o_command isEqualToString:@"stepBackward"]) {
+        //default: backwardShort
+        if (o_parameter) {
+            int i_parameter = [o_parameter intValue];
+            switch (i_parameter) {
+                case 1:
+                    [[VLCCoreInteraction sharedInstance] backwardExtraShort];
+                    break;
+                case 2:
+                    [[VLCCoreInteraction sharedInstance] backwardShort];
+                    break;
+                case 3:
+                    [[VLCCoreInteraction sharedInstance] backwardMedium];
+                    break;
+                case 4:
+                    [[VLCCoreInteraction sharedInstance] backwardLong];
+                    break;
+                default:
+                    [[VLCCoreInteraction sharedInstance] backwardShort];
+                    break;
+            }
+        } else
+            [[VLCCoreInteraction sharedInstance] backwardShort];
     }
-    
-    VLCControls * o_controls = (VLCControls *)[[NSApp delegate] getControls];
-    
-    if ( o_controls )
-    {
-        if ( [o_command isEqualToString:@"play"] )
-        {
-            [o_controls play:self];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"stop"] )
-        {
-            [o_controls stop:self];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"previous"] )
-        {
-            [o_controls prev:self];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"next"] )
-        {
-            [o_controls next:self];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"fullscreen"] )
-        {
-            NSMenuItem *o_mi = [[NSMenuItem alloc] initWithTitle: _NS("Fullscreen") action: nil keyEquivalent:@""];
-            [o_controls windowAction:[o_mi autorelease]];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"mute"] )
-        {
-            [o_controls mute:self];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"volumeUp"] )
-        {
-            [o_controls volumeUp:self];
-            return nil;
-        }
-        else if ( [o_command isEqualToString:@"volumeDown"] )
-        {
-            [o_controls volumeDown:self];
-            return nil;
-        }
+   return nil;
+}
+
+@end
+
+/*****************************************************************************
+ * Category that adds AppleScript support to NSApplication
+ *****************************************************************************/
+@implementation NSApplication(ScriptSupport)
+
+- (BOOL)scriptFullscreenMode {
+    vout_thread_t * p_vout = getVoutForActiveWindow();
+    if (!p_vout)
+        return NO;
+    BOOL b_value = var_GetBool(p_vout, "fullscreen");
+    vlc_object_release(p_vout);
+    return b_value;
+}
+
+- (void)setScriptFullscreenMode:(BOOL)mode {
+    vout_thread_t * p_vout = getVoutForActiveWindow();
+    if (!p_vout)
+        return;
+    if (var_GetBool(p_vout, "fullscreen") == mode) {
+        vlc_object_release(p_vout);
+        return;
     }
-    vlc_object_release( p_playlist );
-    return nil;
+    vlc_object_release(p_vout);
+    [[VLCCoreInteraction sharedInstance] toggleFullscreen];
+}
+
+- (BOOL) muted {
+    return [[VLCCoreInteraction sharedInstance] mute];
+}
+
+- (BOOL) playing {
+    intf_thread_t *p_intf = VLCIntf;
+    if (!p_intf)
+        return NO;
+
+    input_thread_t * p_input = pl_CurrentInput(p_intf);
+    if (!p_input)
+        return NO;
+
+    input_state_e i_state = ERROR_S;
+    input_Control(p_input, INPUT_GET_STATE, &i_state);
+    vlc_object_release(p_input);
+
+    return ((i_state == OPENING_S) || (i_state == PLAYING_S));
+}
+
+- (int) audioVolume {
+    return ([[VLCCoreInteraction sharedInstance] volume]);
+}
+
+- (void) setAudioVolume:(int)i_audioVolume {
+    [[VLCCoreInteraction sharedInstance] setVolume:(int)i_audioVolume];
+}
+
+- (int) currentTime {
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+    int64_t i_currentTime = -1;
+
+    if (!p_input)
+        return i_currentTime;
+
+    input_Control(p_input, INPUT_GET_TIME, &i_currentTime);
+    vlc_object_release(p_input);
+
+    return (int)(i_currentTime / 1000000);
+}
+
+- (void) setCurrentTime:(int)i_currentTime {
+    if (i_currentTime) {
+        int64_t i64_value = (int64_t)i_currentTime;
+        input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+
+        if (!p_input)
+            return;
+
+        input_Control(p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
+        vlc_object_release(p_input);
+    }
+}
+
+- (int) durationOfCurrentItem {
+    return [[VLCCoreInteraction sharedInstance] durationOfCurrentPlaylistItem];
+}
+
+- (NSString*) pathOfCurrentItem {
+    return [[[VLCCoreInteraction sharedInstance] URLOfCurrentPlaylistItem] path];
+}
+
+- (NSString*) nameOfCurrentItem {
+    return [[VLCCoreInteraction sharedInstance] nameOfCurrentPlaylistItem];
 }
 
 @end