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