]> git.sesse.net Git - vlc/blob - modules/gui/macosx/controls.m
macosx: avoid possible index out of range problem in bookmarks table view
[vlc] / modules / gui / macosx / controls.m
1 /*****************************************************************************
2  * controls.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Benjamin Pracht <bigben at videolan doit org>
9  *          Felix Paul Kühne <fkuehne at videolan dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32
33 #import "intf.h"
34 #import "VideoView.h"
35 #import "open.h"
36 #import "controls.h"
37 #import "playlist.h"
38 #import "MainMenu.h"
39 #import "CoreInteraction.h"
40 #import "misc.h"
41 #import <vlc_keys.h>
42
43 #pragma mark -
44 /*****************************************************************************
45  * VLCControls implementation
46  *****************************************************************************/
47 @implementation VLCControls
48
49 @synthesize jumpTimeValue;
50
51 - (void)awakeFromNib
52 {
53     [o_specificTime_mi setTitle: _NS("Jump to Time")];
54     [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
55     [o_specificTime_ok_btn setTitle: _NS("OK")];
56     [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
57     [o_specificTime_goTo_lbl setStringValue: _NS("Jump to Time")];
58
59     [o_specificTime_enter_fld setFormatter:[[[PositionFormatter alloc] init] autorelease]];
60 }
61
62 - (void)dealloc
63 {
64     [[NSNotificationCenter defaultCenter] removeObserver: self];
65
66     [super dealloc];
67 }
68
69 - (IBAction)play:(id)sender
70 {
71     [[VLCCoreInteraction sharedInstance] playOrPause];
72 }
73
74 - (IBAction)stop:(id)sender
75 {
76     [[VLCCoreInteraction sharedInstance] stop];
77 }
78
79 - (IBAction)prev:(id)sender
80 {
81     [[VLCCoreInteraction sharedInstance] previous];
82 }
83
84 - (IBAction)next:(id)sender
85 {
86     [[VLCCoreInteraction sharedInstance] next];
87 }
88
89 - (IBAction)random:(id)sender
90 {
91     [[VLCCoreInteraction sharedInstance] shuffle];
92 }
93
94 - (IBAction)repeat:(id)sender
95 {
96     vlc_value_t val;
97     intf_thread_t * p_intf = VLCIntf;
98     playlist_t * p_playlist = pl_Get(p_intf);
99
100     var_Get(p_playlist, "repeat", &val);
101     if (! val.b_bool)
102         [[VLCCoreInteraction sharedInstance] repeatOne];
103     else
104         [[VLCCoreInteraction sharedInstance] repeatOff];
105 }
106
107 - (IBAction)loop:(id)sender
108 {
109     vlc_value_t val;
110     intf_thread_t * p_intf = VLCIntf;
111     playlist_t * p_playlist = pl_Get(p_intf);
112
113     var_Get(p_playlist, "loop", &val);
114     if (! val.b_bool)
115         [[VLCCoreInteraction sharedInstance] repeatAll];
116     else
117         [[VLCCoreInteraction sharedInstance] repeatOff];
118 }
119
120 - (IBAction)forward:(id)sender
121 {
122     [[VLCCoreInteraction sharedInstance] forward];
123 }
124
125 - (IBAction)backward:(id)sender
126 {
127     [[VLCCoreInteraction sharedInstance] backward];
128 }
129
130 - (IBAction)volumeUp:(id)sender
131 {
132     [[VLCCoreInteraction sharedInstance] volumeUp];
133 }
134
135 - (IBAction)volumeDown:(id)sender
136 {
137     [[VLCCoreInteraction sharedInstance] volumeDown];
138 }
139
140 - (IBAction)mute:(id)sender
141 {
142     [[VLCCoreInteraction sharedInstance] toggleMute];
143 }
144
145 - (IBAction)volumeSliderUpdated:(id)sender
146 {
147     [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
148 }
149
150 - (IBAction)showPosition: (id)sender
151 {
152     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
153     if (p_input != NULL) {
154         vout_thread_t *p_vout = input_GetVout(p_input);
155         if (p_vout != NULL) {
156             var_SetInteger(VLCIntf->p_libvlc, "key-action", ACTIONID_POSITION);
157             vlc_object_release(p_vout);
158         }
159         vlc_object_release(p_input);
160     }
161 }
162
163 - (IBAction)lockVideosAspectRatio:(id)sender
164 {
165     [[VLCCoreInteraction sharedInstance] setAspectRatioIsLocked: ![sender state]];
166     [sender setState: [[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]];
167 }
168
169 - (BOOL)keyEvent:(NSEvent *)o_event
170 {
171     BOOL eventHandled = NO;
172     NSString * characters = [o_event charactersIgnoringModifiers];
173     if ([characters length] > 0) {
174         unichar key = [characters characterAtIndex: 0];
175
176         if (key) {
177             input_thread_t * p_input = pl_CurrentInput(VLCIntf);
178             if (p_input != NULL) {
179                 vout_thread_t *p_vout = input_GetVout(p_input);
180
181                 if (p_vout != NULL) {
182                     /* Escape */
183                     if (key == (unichar) 0x1b) {
184                         if (var_GetBool(p_vout, "fullscreen")) {
185                             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
186                             eventHandled = YES;
187                         }
188                     }
189                     vlc_object_release(p_vout);
190                 }
191                 vlc_object_release(p_input);
192             }
193         }
194     }
195     return eventHandled;
196 }
197
198 - (IBAction)goToSpecificTime:(id)sender
199 {
200     if (sender == o_specificTime_cancel_btn)
201     {
202         [NSApp endSheet: o_specificTime_win];
203         [o_specificTime_win close];
204     } else if (sender == o_specificTime_ok_btn) {
205         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
206         if (p_input) {
207             int64_t timeInSec = 0;
208             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
209             if ([[fieldContent componentsSeparatedByString: @":"] count] > 1 &&
210                [[fieldContent componentsSeparatedByString: @":"] count] <= 3) {
211                 NSArray * ourTempArray = \
212                     [fieldContent componentsSeparatedByString: @":"];
213
214                 if ([[fieldContent componentsSeparatedByString: @":"] count] == 3) {
215                     timeInSec += ([[ourTempArray objectAtIndex:0] intValue] * 3600); //h
216                     timeInSec += ([[ourTempArray objectAtIndex:1] intValue] * 60); //m
217                     timeInSec += [[ourTempArray objectAtIndex:2] intValue];        //s
218                 } else {
219                     timeInSec += ([[ourTempArray objectAtIndex:0] intValue] * 60); //m
220                     timeInSec += [[ourTempArray objectAtIndex:1] intValue]; //s
221                 }
222             }
223             else
224                 timeInSec = [fieldContent intValue];
225
226             input_Control(p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
227             vlc_object_release(p_input);
228         }
229
230         [NSApp endSheet: o_specificTime_win];
231         [o_specificTime_win close];
232     } else {
233         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
234         if (p_input) {
235             /* we can obviously only do that if an input is available */
236             vlc_value_t pos, length;
237             var_Get(p_input, "length", &length);
238             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
239             var_Get(p_input, "time", &pos);
240             [self setJumpTimeValue: (pos.i_time / 1000000)];
241             [NSApp beginSheet: o_specificTime_win modalForWindow: \
242                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
243                 contextInfo: nil];
244             [o_specificTime_win makeKeyWindow];
245             vlc_object_release(p_input);
246         }
247     }
248 }
249
250 @end
251
252 @implementation VLCControls (NSMenuValidation)
253
254 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
255 {
256     return [[VLCMainMenu sharedInstance] validateMenuItem:o_mi];
257 }
258
259 @end