+/* When user click in the Dock icon our double click in the finder */
+- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)hasVisibleWindows
+{
+ if (!hasVisibleWindows)
+ [o_window makeKeyAndOrderFront:self];
+
+ return YES;
+}
+
+- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
+{
+ /* Check for update silently on startup */
+ if ( !nib_update_loaded )
+ nib_update_loaded = [NSBundle loadNibNamed:@"Update" owner:self];
+
+ if([o_update shouldCheckForUpdate])
+ [NSThread detachNewThreadSelector:@selector(checkForUpdate) toTarget:o_update withObject:NULL];
+
+ /* Handle sleep notification */
+ [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
+ name:NSWorkspaceWillSleepNotification object:nil];
+}
+
+/* Listen to the remote in exclusive mode, only when VLC is the active
+ application */
+- (void)applicationDidBecomeActive:(NSNotification *)aNotification
+{
+ [o_remote startListening: self];
+}
+- (void)applicationDidResignActive:(NSNotification *)aNotification
+{
+ [o_remote stopListening: self];
+}
+
+/* Triggered when the computer goes to sleep */
+- (void)computerWillSleep: (NSNotification *)notification
+{
+ /* Pause */
+ if ( p_intf->p_sys->i_play_status == PLAYING_S )
+ {
+ vlc_value_t val;
+ val.i_int = config_GetInt( p_intf, "key-play-pause" );
+ var_Set( p_intf->p_libvlc, "key-pressed", val );
+ }
+}
+
+/* Helper method for the remote control interface in order to trigger forward/backward and volume
+ increase/decrease as long as the user holds the left/right, plus/minus button */
+- (void) executeHoldActionForRemoteButton: (NSNumber*) buttonIdentifierNumber
+{
+ if (b_remote_button_hold)
+ {
+ switch([buttonIdentifierNumber intValue])
+ {
+ case kRemoteButtonRight_Hold:
+ [o_controls forward: self];
+ break;
+ case kRemoteButtonLeft_Hold:
+ [o_controls backward: self];
+ break;
+ case kRemoteButtonVolume_Plus_Hold:
+ [o_controls volumeUp: self];
+ break;
+ case kRemoteButtonVolume_Minus_Hold:
+ [o_controls volumeDown: self];
+ break;
+ }
+ if (b_remote_button_hold)
+ {
+ /* trigger event */
+ [self performSelector:@selector(executeHoldActionForRemoteButton:)
+ withObject:buttonIdentifierNumber
+ afterDelay:0.25];
+ }
+ }
+}
+
+/* Apple Remote callback */
+- (void) appleRemoteButton: (AppleRemoteEventIdentifier)buttonIdentifier
+ pressedDown: (BOOL) pressedDown
+ clickCount: (unsigned int) count
+{
+ switch( buttonIdentifier )
+ {
+ case kRemoteButtonPlay:
+ if (count >= 2) {
+ [o_controls toogleFullscreen:self];
+ } else {
+ [o_controls play: self];
+ }
+ break;
+ case kRemoteButtonVolume_Plus:
+ [o_controls volumeUp: self];
+ break;
+ case kRemoteButtonVolume_Minus:
+ [o_controls volumeDown: self];
+ break;
+ case kRemoteButtonRight:
+ [o_controls next: self];
+ break;
+ case kRemoteButtonLeft:
+ [o_controls prev: self];
+ break;
+ case kRemoteButtonRight_Hold:
+ case kRemoteButtonLeft_Hold:
+ case kRemoteButtonVolume_Plus_Hold:
+ case kRemoteButtonVolume_Minus_Hold:
+ /* simulate an event as long as the user holds the button */
+ b_remote_button_hold = pressedDown;
+ if( pressedDown )
+ {
+ NSNumber* buttonIdentifierNumber = [NSNumber numberWithInt: buttonIdentifier];
+ [self performSelector:@selector(executeHoldActionForRemoteButton:)
+ withObject:buttonIdentifierNumber];
+ }
+ break;
+ case kRemoteButtonMenu:
+ [o_controls showPosition: self];
+ break;
+ default:
+ /* Add here whatever you want other buttons to do */
+ break;
+ }
+}
+