]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VLCVoutWindowController.m
macosx: fix avcodec-hw option implementation
[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 @implementation VLCVoutWindowController
31
32 - (id)init
33 {
34     self = [super init];
35     o_vout_dict = [[NSMutableDictionary alloc] init];
36     return self;
37 }
38
39 - (void)dealloc
40 {
41     NSArray *keys = [o_vout_dict allKeys];
42     for (NSValue *key in keys)
43         [self removeVoutforDisplay:key];
44
45     [o_vout_dict release];
46     [super dealloc];
47 }
48
49
50 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
51 {
52     BOOL b_nonembedded = NO;
53     BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
54     BOOL b_video_deco = var_InheritBool(VLCIntf, "video-deco");
55     BOOL b_video_wallpaper = var_InheritBool(VLCIntf, "video-wallpaper");
56     BOOL b_multiple_vout_windows = [o_vout_dict count] > 0;
57     VLCVoutView *o_vout_view;
58     VLCVideoWindowCommon *o_new_video_window;
59
60     if (b_multiple_vout_windows && b_video_wallpaper)
61         b_video_wallpaper = false;
62
63     // TODO: make lion fullscreen compatible with video-wallpaper and !embedded-video
64     if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
65         // b_video_wallpaper is priorized over !b_video_deco
66
67         msg_Dbg(VLCIntf, "Creating background / blank window");
68         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
69         if (!screen)
70             screen = [[VLCMainWindow sharedInstance] screen];
71
72         NSRect window_rect;
73         if (b_video_wallpaper)
74             window_rect = [screen frame];
75         else
76             window_rect = [[VLCMainWindow sharedInstance] frame];
77
78         NSUInteger mask = NSBorderlessWindowMask;
79         if (!OSX_SNOW_LEOPARD && !b_video_deco)
80             mask |= NSResizableWindowMask;
81
82         BOOL b_no_video_deco_only = !b_video_wallpaper;
83         o_new_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
84         [o_new_video_window setDelegate:o_new_video_window];
85
86         if (b_video_wallpaper)
87             [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
88
89         [o_new_video_window setBackgroundColor: [NSColor blackColor]];
90         [o_new_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
91         [o_new_video_window setCanBecomeMainWindow: !b_video_wallpaper];
92         [o_new_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
93         [o_new_video_window setMovableByWindowBackground: !b_video_wallpaper];
94         [o_new_video_window useOptimizedDrawing: YES];
95
96         o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_new_video_window contentView] bounds]];
97         [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
98         [[o_new_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
99         [o_new_video_window setVideoView:o_vout_view];
100
101
102         if (b_video_wallpaper)
103             [o_new_video_window orderBack:nil];
104         else {
105             // no frame autosave for additional vout windows
106             if (!b_multiple_vout_windows) {
107                 // initial window position
108                 [o_new_video_window center];
109                 [o_new_video_window setFrameAutosaveName:@"extra-videowindow"];
110             }
111             
112             [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
113         }
114
115         [[VLCMainWindow sharedInstance] setNonembedded:YES];
116         b_nonembedded = YES;
117     } else {
118         if ((var_InheritBool(VLCIntf, "embedded-video") && !b_multiple_vout_windows) || b_nativeFullscreenMode) {
119             // setup embedded video
120             o_vout_view = [[[VLCMainWindow sharedInstance] videoView] retain];
121             o_new_video_window = [[VLCMainWindow sharedInstance] retain];
122             b_nonembedded = NO;
123         } else {
124             // setup detached window with controls
125             NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
126             [o_controller loadWindow];
127             o_new_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
128             [o_controller release];
129
130             // no frame autosave for additional vout windows
131             if (b_multiple_vout_windows)
132                 [o_new_video_window setFrameAutosaveName:@""];
133
134             [o_new_video_window setDelegate: o_new_video_window];
135             [o_new_video_window setLevel:NSNormalWindowLevel];
136             [o_new_video_window useOptimizedDrawing: YES];
137             o_vout_view = [[o_new_video_window videoView] retain];
138             b_nonembedded = YES;
139         }
140     }
141
142     if (!b_video_wallpaper) {
143         // set window size
144         NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
145
146         if (b_nonembedded) {
147             NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
148             [o_new_video_window setFrame:window_rect display:YES];
149         }
150
151         // cascade windows if we have more than one vout
152         if (b_multiple_vout_windows) {
153             if ([o_vout_dict count] == 1) {
154                 NSWindow * o_first_window = [o_vout_dict objectForKey: [[o_vout_dict allKeys] objectAtIndex: 0]];
155
156                 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
157                 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
158             }
159
160             top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
161             [o_new_video_window setFrameTopLeftPoint: top_left_point];
162         }
163         
164         [o_new_video_window setNativeVideoSize:videoViewSize];
165
166         [o_new_video_window makeKeyAndOrderFront: self];
167
168         vout_thread_t *p_vout = getVout();
169         if (p_vout) {
170             if (var_GetBool(p_vout, "video-on-top"))
171                 [o_new_video_window setLevel: NSStatusWindowLevel];
172             else
173                 [o_new_video_window setLevel: NSNormalWindowLevel];
174             vlc_object_release(p_vout);
175         }
176     }
177
178     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
179
180     if (!b_multiple_vout_windows)
181         [[VLCMainWindow sharedInstance] setNonembedded:b_nonembedded];
182
183     [o_vout_view setVoutThread:(vout_thread_t *)p_wnd->p_parent];
184     [o_vout_dict setObject:[o_new_video_window autorelease] forKey:[NSValue valueWithPointer:p_wnd]];
185
186     if (b_nonembedded) {
187         // event occurs before window is created, so call again
188         [[VLCMain sharedInstance] playlistUpdated];
189     }
190
191     return [o_vout_view autorelease];
192 }
193
194 - (void)removeVoutforDisplay:(NSValue *)o_key
195 {
196     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
197     if (!o_window) {
198         msg_Err(VLCIntf, "Cannot close nonexisting window");
199         return;
200     }
201
202     if ([[VLCMainWindow sharedInstance] fullscreen] && ![[VLCMainWindow sharedInstance] nativeFullscreenMode])
203         [o_window leaveFullscreen];
204
205     if (![NSStringFromClass([o_window class]) isEqualToString:@"VLCMainWindow"]) {
206         [o_window orderOut:self];
207     }
208
209     [[o_window videoView] releaseVoutThread];
210     [o_vout_dict removeObjectForKey:o_key];
211
212     if ([o_vout_dict count] == 0)
213         [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
214 }
215
216 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
217 {
218     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
219         if ([obj respondsToSelector:@selector(controlsBar)]) {
220             id o_controlsBar = [obj controlsBar];
221             if (o_controlsBar)
222                 [o_controlsBar performSelector:aSel];
223         }
224     }];
225 }
226
227 - (void)updateWindow:(vout_window_t *)p_wnd withSelector:(SEL)aSel;
228 {
229     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
230     if (!o_window) {
231         msg_Err(VLCIntf, "Cannot call selector for nonexisting window");
232         return;
233     }
234
235     [o_window performSelector:aSel];
236 }
237
238 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
239 {
240     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
241         if ([obj isKindOfClass: [NSWindow class]])
242             windowUpdater(obj);
243     }];
244 }
245
246 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
247 {
248     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
249     if (!o_window) {
250         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
251         return;
252     }
253
254     [o_window setNativeVideoSize:size];
255 }
256
257 @end