]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VLCVoutWindowController.m
a23628f2b105ccd2685f68dc3f7fb36e8cebf397
[vlc] / modules / gui / macosx / VLCVoutWindowController.m
1 /*****************************************************************************
2  * VLCVoutWindowController.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2012 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCVoutWindowController.h"
26 #import "intf.h"
27 #import "MainWindow.h"
28 #import "VideoView.h"
29
30 #import "VideoEffects.h"
31 #import "AudioEffects.h"
32 #import "playlistinfo.h"
33 #import "bookmarks.h"
34 #import "TrackSynchronization.h"
35
36 @implementation VLCVoutWindowController
37
38 - (id)init
39 {
40     self = [super init];
41     o_vout_dict = [[NSMutableDictionary alloc] init];
42     i_currentWindowLevel = NSNormalWindowLevel;
43     return self;
44 }
45
46 - (void)dealloc
47 {
48     NSArray *keys = [o_vout_dict allKeys];
49     for (NSValue *key in keys)
50         [self removeVoutforDisplay:key];
51
52     [o_vout_dict release];
53     [super dealloc];
54 }
55
56
57 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
58 {
59     BOOL b_nonembedded = NO;
60     BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
61     BOOL b_video_deco = var_InheritBool(VLCIntf, "video-deco");
62     BOOL b_video_wallpaper = var_InheritBool(VLCIntf, "video-wallpaper");
63     BOOL b_multiple_vout_windows = [o_vout_dict count] > 0;
64     VLCVoutView *o_vout_view;
65     VLCVideoWindowCommon *o_new_video_window;
66
67     if (b_multiple_vout_windows && b_video_wallpaper)
68         b_video_wallpaper = false;
69
70     // TODO: make lion fullscreen compatible with video-wallpaper and !embedded-video
71     if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
72         // b_video_wallpaper is priorized over !b_video_deco
73
74         msg_Dbg(VLCIntf, "Creating background / blank window");
75         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
76         if (!screen)
77             screen = [[VLCMainWindow sharedInstance] screen];
78
79         NSRect window_rect;
80         if (b_video_wallpaper)
81             window_rect = [screen frame];
82         else
83             window_rect = [[VLCMainWindow sharedInstance] frame];
84
85         NSUInteger mask = NSBorderlessWindowMask;
86         if (!OSX_SNOW_LEOPARD && !b_video_deco)
87             mask |= NSResizableWindowMask;
88
89         BOOL b_no_video_deco_only = !b_video_wallpaper;
90         o_new_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
91         [o_new_video_window setDelegate:o_new_video_window];
92
93         if (b_video_wallpaper)
94             [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
95
96         [o_new_video_window setBackgroundColor: [NSColor blackColor]];
97         [o_new_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
98         [o_new_video_window setCanBecomeMainWindow: !b_video_wallpaper];
99         [o_new_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
100         [o_new_video_window setMovableByWindowBackground: !b_video_wallpaper];
101         [o_new_video_window useOptimizedDrawing: YES];
102
103         o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_new_video_window contentView] bounds]];
104         [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
105         [[o_new_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
106         [o_new_video_window setVideoView:o_vout_view];
107
108
109         if (b_video_wallpaper)
110             [o_new_video_window orderBack:nil];
111         else {
112             // no frame autosave for additional vout windows
113             if (!b_multiple_vout_windows) {
114                 // initial window position
115                 [o_new_video_window center];
116                 [o_new_video_window setFrameAutosaveName:@"extra-videowindow"];
117             }
118             
119             [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
120         }
121
122         [[VLCMainWindow sharedInstance] setNonembedded:YES];
123         b_nonembedded = YES;
124     } else {
125         if ((var_InheritBool(VLCIntf, "embedded-video") && !b_multiple_vout_windows)) {
126             // setup embedded video
127             o_vout_view = [[[VLCMainWindow sharedInstance] videoView] retain];
128             o_new_video_window = [[VLCMainWindow sharedInstance] retain];
129             b_nonembedded = NO;
130         } else {
131             // setup detached window with controls
132             NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
133             [o_controller loadWindow];
134             o_new_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
135             [o_controller release];
136
137             // no frame autosave for additional vout windows
138             if (b_multiple_vout_windows)
139                 [o_new_video_window setFrameAutosaveName:@""];
140
141             [o_new_video_window setDelegate: o_new_video_window];
142             [o_new_video_window setLevel:NSNormalWindowLevel];
143             [o_new_video_window useOptimizedDrawing: YES];
144             o_vout_view = [[o_new_video_window videoView] retain];
145             b_nonembedded = YES;
146         }
147     }
148
149     if (!b_video_wallpaper) {
150         // set window size
151         NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
152
153         if (b_nonembedded) {
154             NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
155             [o_new_video_window setFrame:window_rect display:YES];
156         }
157
158         // cascade windows if we have more than one vout
159         if (b_multiple_vout_windows) {
160             if ([o_vout_dict count] == 1) {
161                 NSWindow * o_first_window = [o_vout_dict objectForKey: [[o_vout_dict allKeys] objectAtIndex: 0]];
162
163                 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
164                 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
165             }
166
167             top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
168             [o_new_video_window setFrameTopLeftPoint: top_left_point];
169         }
170         
171         [o_new_video_window setNativeVideoSize:videoViewSize];
172
173         [o_new_video_window makeKeyAndOrderFront: self];
174     }
175
176     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
177
178     if (!b_multiple_vout_windows)
179         [[VLCMainWindow sharedInstance] setNonembedded:b_nonembedded];
180
181     [o_vout_view setVoutThread:(vout_thread_t *)p_wnd->p_parent];
182     [o_vout_dict setObject:[o_new_video_window autorelease] forKey:[NSValue valueWithPointer:p_wnd]];
183
184     if (b_nonembedded) {
185         // event occurs before window is created, so call again
186         [[VLCMain sharedInstance] playlistUpdated];
187     }
188
189     return [o_vout_view autorelease];
190 }
191
192 - (void)removeVoutforDisplay:(NSValue *)o_key
193 {
194     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
195     if (!o_window) {
196         msg_Err(VLCIntf, "Cannot close nonexisting window");
197         return;
198     }
199
200     if ([[VLCMainWindow sharedInstance] fullscreen] && ![[VLCMainWindow sharedInstance] nativeFullscreenMode])
201         [o_window leaveFullscreen];
202
203     if (![NSStringFromClass([o_window class]) isEqualToString:@"VLCMainWindow"]) {
204         [o_window orderOut:self];
205     }
206
207     [[o_window videoView] releaseVoutThread];
208     [o_vout_dict removeObjectForKey:o_key];
209
210     if ([o_vout_dict count] == 0)
211         [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
212 }
213
214 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
215 {
216     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
217         if ([obj respondsToSelector:@selector(controlsBar)]) {
218             id o_controlsBar = [obj controlsBar];
219             if (o_controlsBar)
220                 [o_controlsBar performSelector:aSel];
221         }
222     }];
223 }
224
225 - (void)updateWindow:(vout_window_t *)p_wnd withSelector:(SEL)aSel;
226 {
227     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
228     if (!o_window) {
229         msg_Err(VLCIntf, "Cannot call selector for nonexisting window");
230         return;
231     }
232
233     [o_window performSelector:aSel];
234 }
235
236 - (VLCVideoWindowCommon *)getWindow:(vout_window_t *)p_wnd
237 {
238     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
239     assert(o_window);
240     return o_window;
241
242 }
243
244 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
245 {
246     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
247         if ([obj isKindOfClass: [NSWindow class]])
248             windowUpdater(obj);
249     }];
250 }
251
252 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
253 {
254     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
255     if (!o_window) {
256         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
257         return;
258     }
259
260     [o_window setNativeVideoSize:size];
261 }
262
263 - (void)setWindowLevel:(NSInteger)i_level forWindow:(vout_window_t *)p_wnd
264 {
265     // only set level for helper windows to normal if no status vout window exist anymore
266     if(i_level == NSStatusWindowLevel) {
267         i_statusLevelWindowCounter++;
268         [self updateWindowLevelForHelperWindows:i_level];
269     } else {
270         i_statusLevelWindowCounter--;
271         if (i_statusLevelWindowCounter == 0) {
272             [self updateWindowLevelForHelperWindows:i_level];
273         }
274     }
275
276     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
277     if (!o_window) {
278         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
279         return;
280     }
281
282     [o_window setWindowLevel:i_level];
283 }
284
285 - (void)updateWindowLevelForHelperWindows:(NSInteger)i_level
286 {
287     if (var_InheritBool(VLCIntf, "video-wallpaper"))
288         return;
289
290     i_currentWindowLevel = i_level;
291
292     [[VLCMainWindow sharedInstance] setWindowLevel:i_level];
293     [[VLCVideoEffects sharedInstance] updateCocoaWindowLevel:i_level];
294     [[VLCAudioEffects sharedInstance] updateCocoaWindowLevel:i_level];
295     [[[VLCMain sharedInstance] info] updateCocoaWindowLevel:i_level];
296     [[VLCBookmarks sharedInstance] updateCocoaWindowLevel:i_level];
297     [[VLCTrackSynchronization sharedInstance] updateCocoaWindowLevel:i_level];
298 }
299
300 @synthesize currentWindowLevel=i_currentWindowLevel;
301
302
303 @end