]> git.sesse.net Git - vlc/blob - modules/gui/macosx/controls.m
cab73ba172c5686b7a5bcb31820c12695fa4bd55
[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 <vlc_keys.h>
41
42 #pragma mark -
43 /*****************************************************************************
44  * VLCControls implementation
45  *****************************************************************************/
46 @implementation VLCControls
47
48 @synthesize jumpTimeValue;
49
50 - (void)awakeFromNib
51 {
52     [o_specificTime_mi setTitle: _NS("Jump To Time")];
53     [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
54     [o_specificTime_ok_btn setTitle: _NS("OK")];
55     [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
56     [o_specificTime_goTo_lbl setStringValue: _NS("Jump to time")];
57 }
58
59 - (void)dealloc
60 {
61     [[NSNotificationCenter defaultCenter] removeObserver: self];
62
63     [super dealloc];
64 }
65
66 - (IBAction)play:(id)sender
67 {
68     [[VLCCoreInteraction sharedInstance] playOrPause];
69 }
70
71 - (IBAction)stop:(id)sender
72 {
73     [[VLCCoreInteraction sharedInstance] stop];
74 }
75
76 - (IBAction)prev:(id)sender
77 {
78     [[VLCCoreInteraction sharedInstance] previous];
79 }
80
81 - (IBAction)next:(id)sender
82 {
83     [[VLCCoreInteraction sharedInstance] next];
84 }
85
86 - (IBAction)random:(id)sender
87 {
88     [[VLCCoreInteraction sharedInstance] shuffle];
89 }
90
91 - (IBAction)repeat:(id)sender
92 {
93     vlc_value_t val;
94     intf_thread_t * p_intf = VLCIntf;
95     playlist_t * p_playlist = pl_Get(p_intf);
96
97     var_Get(p_playlist, "repeat", &val);
98     if (! val.b_bool)
99         [[VLCCoreInteraction sharedInstance] repeatOne];
100     else
101         [[VLCCoreInteraction sharedInstance] repeatOff];
102 }
103
104 - (IBAction)loop:(id)sender
105 {
106     vlc_value_t val;
107     intf_thread_t * p_intf = VLCIntf;
108     playlist_t * p_playlist = pl_Get(p_intf);
109
110     var_Get(p_playlist, "loop", &val);
111     if (! val.b_bool)
112         [[VLCCoreInteraction sharedInstance] repeatAll];
113     else
114         [[VLCCoreInteraction sharedInstance] repeatOff];
115 }
116
117 - (IBAction)quitAfterPlayback:(id)sender
118 {
119     vlc_value_t val;
120     playlist_t * p_playlist = pl_Get(VLCIntf);
121     var_ToggleBool(p_playlist, "play-and-exit");
122 }
123
124 - (IBAction)forward:(id)sender
125 {
126     [[VLCCoreInteraction sharedInstance] forward];
127 }
128
129 - (IBAction)backward:(id)sender
130 {
131     [[VLCCoreInteraction sharedInstance] backward];
132 }
133
134 - (IBAction)volumeUp:(id)sender
135 {
136     [[VLCCoreInteraction sharedInstance] volumeUp];
137 }
138
139 - (IBAction)volumeDown:(id)sender
140 {
141     [[VLCCoreInteraction sharedInstance] volumeDown];
142 }
143
144 - (IBAction)mute:(id)sender
145 {
146     [[VLCCoreInteraction sharedInstance] setMute: YES];
147 }
148
149 - (IBAction)volumeSliderUpdated:(id)sender
150 {
151     [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
152 }
153
154 - (IBAction)showPosition: (id)sender
155 {
156     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
157     if (p_input != NULL) {
158         vout_thread_t *p_vout = input_GetVout(p_input);
159         if (p_vout != NULL) {
160             var_SetInteger(VLCIntf->p_libvlc, "key-action", ACTIONID_POSITION);
161             vlc_object_release(p_vout);
162         }
163         vlc_object_release(p_input);
164     }
165 }
166
167 - (IBAction)lockVideosAspectRatio:(id)sender
168 {
169     [[VLCCoreInteraction sharedInstance] setAspectRatioIsLocked: ![sender state]];
170     [sender setState: [[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]];
171 }
172
173 - (BOOL)keyEvent:(NSEvent *)o_event
174 {
175     BOOL eventHandled = NO;
176     NSString * characters = [o_event charactersIgnoringModifiers];
177     if ([characters length] > 0) {
178         unichar key = [characters characterAtIndex: 0];
179
180         if (key) {
181             input_thread_t * p_input = pl_CurrentInput(VLCIntf);
182             if (p_input != NULL) {
183                 vout_thread_t *p_vout = input_GetVout(p_input);
184
185                 if (p_vout != NULL) {
186                     /* Escape */
187                     if (key == (unichar) 0x1b) {
188                         if (var_GetBool(p_vout, "fullscreen")) {
189                             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
190                             eventHandled = YES;
191                         }
192                     }
193                     else if (key == ' ') {
194                         [self play:self];
195                         eventHandled = YES;
196                     }
197                     vlc_object_release(p_vout);
198                 }
199                 vlc_object_release(p_input);
200             }
201         }
202     }
203     return eventHandled;
204 }
205
206 - (IBAction)goToSpecificTime:(id)sender
207 {
208     if (sender == o_specificTime_cancel_btn)
209     {
210         [NSApp endSheet: o_specificTime_win];
211         [o_specificTime_win close];
212     } else if (sender == o_specificTime_ok_btn) {
213         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
214         if (p_input) {
215             int64_t timeInSec = 0;
216             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
217             if ([[fieldContent componentsSeparatedByString: @":"] count] > 1 &&
218                [[fieldContent componentsSeparatedByString: @":"] count] <= 3) {
219                 NSArray * ourTempArray = \
220                     [fieldContent componentsSeparatedByString: @":"];
221
222                 if ([[fieldContent componentsSeparatedByString: @":"] count] == 3) {
223                     timeInSec += ([ourTempArray[0] intValue] * 3600); //h
224                     timeInSec += ([ourTempArray[1] intValue] * 60); //m
225                     timeInSec += [ourTempArray[2] intValue];        //s
226                 } else {
227                     timeInSec += ([ourTempArray[0] intValue] * 60); //m
228                     timeInSec += [ourTempArray[1] intValue]; //s
229                 }
230             }
231             else
232                 timeInSec = [fieldContent intValue];
233
234             input_Control(p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
235             vlc_object_release(p_input);
236         }
237
238         [NSApp endSheet: o_specificTime_win];
239         [o_specificTime_win close];
240     } else {
241         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
242         if (p_input) {
243             /* we can obviously only do that if an input is available */
244             vlc_value_t pos, length;
245             var_Get(p_input, "length", &length);
246             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
247             var_Get(p_input, "time", &pos);
248             [self setJumpTimeValue: (pos.i_time / 1000000)];
249             [NSApp beginSheet: o_specificTime_win modalForWindow: \
250                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
251                 contextInfo: nil];
252             [o_specificTime_win makeKeyWindow];
253             vlc_object_release(p_input);
254         }
255     }
256 }
257
258 @end
259
260 @implementation VLCControls (NSMenuValidation)
261
262 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
263 {
264     return [[VLCMainMenu sharedInstance] validateMenuItem:o_mi];
265 }
266
267 @end