]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VLCVoutWindowController.m
Use "activity" instead of "item-current" variable
[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
61     // TODO: make lion fullscreen compatible with video-wallpaper and !embedded-video
62     if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
63         // b_video_wallpaper is priorized over !b_video_deco
64
65         msg_Dbg(VLCIntf, "Creating background / blank window");
66         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
67         if (!screen)
68             screen = [[VLCMainWindow sharedInstance] screen];
69
70         NSRect window_rect;
71         if (b_video_wallpaper)
72             window_rect = [screen frame];
73         else
74             window_rect = [[VLCMainWindow sharedInstance] frame];
75
76         NSUInteger mask = NSBorderlessWindowMask;
77         if (!OSX_SNOW_LEOPARD && !b_video_deco)
78             mask |= NSResizableWindowMask;
79
80         BOOL b_no_video_deco_only = !b_video_wallpaper;
81         o_new_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
82         [o_new_video_window setDelegate:o_new_video_window];
83
84         if (b_video_wallpaper)
85             [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
86
87         [o_new_video_window setBackgroundColor: [NSColor blackColor]];
88         [o_new_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
89         [o_new_video_window setCanBecomeMainWindow: !b_video_wallpaper];
90         [o_new_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
91         [o_new_video_window setMovableByWindowBackground: !b_video_wallpaper];
92         [o_new_video_window useOptimizedDrawing: YES];
93
94         o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_new_video_window contentView] bounds]];
95         [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
96         [[o_new_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
97         [o_new_video_window setVideoView:o_vout_view];
98
99
100         if (b_video_wallpaper)
101             [o_new_video_window orderBack:nil];
102         else {
103             [o_new_video_window center];
104             [o_new_video_window setFrameAutosaveName:@"extra-videowindow"];
105             [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
106         }
107
108         [[VLCMainWindow sharedInstance] setNonembedded:YES];
109         b_nonembedded = YES;
110     } else {
111         if ((var_InheritBool(VLCIntf, "embedded-video") && !b_multiple_vout_windows) || b_nativeFullscreenMode) {
112             o_vout_view = [[[VLCMainWindow sharedInstance] videoView] retain];
113             o_new_video_window = [[VLCMainWindow sharedInstance] retain];
114             b_nonembedded = NO;
115         } else {
116             NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
117             [o_controller loadWindow];
118             o_new_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
119             [o_controller release];
120
121             [o_new_video_window setDelegate: o_new_video_window];
122             [o_new_video_window setLevel:NSNormalWindowLevel];
123             [o_new_video_window useOptimizedDrawing: YES];
124             o_vout_view = [[o_new_video_window videoView] retain];
125             b_nonembedded = YES;
126         }
127     }
128
129     if (!b_video_wallpaper) {
130         // set window size
131         NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
132
133         if (b_nonembedded) {
134             NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
135             [o_new_video_window setFrame:window_rect display:YES];
136         }
137         [o_new_video_window setNativeVideoSize:videoViewSize];
138
139         [o_new_video_window makeKeyAndOrderFront: self];
140
141         vout_thread_t *p_vout = getVout();
142         if (p_vout) {
143             if (var_GetBool(p_vout, "video-on-top"))
144                 [o_new_video_window setLevel: NSStatusWindowLevel];
145             else
146                 [o_new_video_window setLevel: NSNormalWindowLevel];
147             vlc_object_release(p_vout);
148         }
149     }
150
151     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
152
153     if (!b_multiple_vout_windows)
154         [[VLCMainWindow sharedInstance] setNonembedded:b_nonembedded];
155
156     [o_vout_view setVoutThread:(vout_thread_t *)p_wnd->p_parent];
157     [o_vout_dict setObject:[o_new_video_window autorelease] forKey:[NSValue valueWithPointer:p_wnd]];
158
159     if (b_nonembedded) {
160         // event occurs before window is created, so call again
161         [[VLCMain sharedInstance] playlistUpdated];
162     }
163
164     return [o_vout_view autorelease];
165 }
166
167 - (void)removeVoutforDisplay:(NSValue *)o_key
168 {
169     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
170     if (!o_window) {
171         msg_Err(VLCIntf, "Cannot close nonexisting window");
172         return;
173     }
174
175     if ([[VLCMainWindow sharedInstance] fullscreen] && ![[VLCMainWindow sharedInstance] nativeFullscreenMode])
176         [o_window leaveFullscreen];
177
178     if (![NSStringFromClass([o_window class]) isEqualToString:@"VLCMainWindow"]) {
179         [o_window orderOut:self];
180     }
181
182     [[o_window videoView] releaseVoutThread];
183     [o_vout_dict removeObjectForKey:o_key];
184
185     if ([o_vout_dict count] == 0)
186         [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
187 }
188
189 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
190 {
191     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
192         if ([obj respondsToSelector:@selector(controlsBar)]) {
193             id o_controlsBar = [obj controlsBar];
194             if (o_controlsBar)
195                 [o_controlsBar performSelector:aSel];
196         }
197     }];
198 }
199
200 - (void)updateWindow:(vout_window_t *)p_wnd withSelector:(SEL)aSel;
201 {
202     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
203     if (!o_window) {
204         msg_Err(VLCIntf, "Cannot call selector for nonexisting window");
205         return;
206     }
207
208     [o_window performSelector:aSel];
209 }
210
211 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
212 {
213     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
214         if ([obj isKindOfClass: [NSWindow class]])
215             windowUpdater(obj);
216     }];
217 }
218
219 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
220 {
221     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
222     if (!o_window) {
223         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
224         return;
225     }
226
227     [o_window setNativeVideoSize:size];
228 }
229
230 @end