forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMovieView.m
301 lines (253 loc) · 9.37 KB
/
MMovieView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <[email protected]>
//
// This file is part of Movist.
//
// Movist is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Movist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "MMovieView.h"
#import "MMovie.h"
#import "MMovie_QuickTime.h"
#import "MMovie_FFMPEG.h"
#import "MMovieLayer_FFMPEG.h"
#import "MMovieLayer_AVFoundation.h"
#import "MSubtitle.h"
#import "MMovieViewLayer.h"
#import "MMovieOSDLayer.h"
#import "MMovieOSD.h"
#import "AppController.h" // for NSApp's delegate
@implementation MMovieView
- (void)awakeFromNib
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
if (![self initOSD]) {
// FIXME: alert...
return;
}
// etc. options
_fullScreenFill = FS_FILL_NEVER;
_fullScreenUnderScan = 0.0;
_viewDragAction = VIEW_DRAG_ACTION_NONE;
_captureFormat = CAPTURE_FORMAT_PNG;
_includeLetterBoxOnCapture = TRUE;
// drag-and-drop
[self registerForDraggedTypes:MOVIST_DRAG_TYPES];
// notifications
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(windowMoved:)
name:NSWindowDidMoveNotification object:[self window]];
[nc addObserver:self selector:@selector(frameResized:)
name:NSViewFrameDidChangeNotification object:self];
// add layer
_rootLayer = [MMovieViewLayer layer];
[self setLayer:_rootLayer];
[self setWantsLayer:YES];
if([self respondsToSelector:@selector(setLayerUsesCoreImageFilters:)])
{
// OS X 10.9 and later only
[self setLayerUsesCoreImageFilters:YES];
}
[self initCoreImage];
CALayer* iconOSDLayer = [CALayer layer];
iconOSDLayer.zPosition = 1.0;
CGImageRef iconImageRef = [[NSImage imageNamed:@"Movist"] CGImageForProposedRect:NULL context:nil hints:nil];
iconOSDLayer.bounds = (CGRect){
.origin = CGPointZero,
.size = (CGSize){.width = CGImageGetWidth(iconImageRef), .height = CGImageGetHeight(iconImageRef)}
};
iconOSDLayer.contents = (id)iconImageRef;
_rootLayer.icon = iconOSDLayer;
MMovieOSDLayer* messageLayer = [MMovieOSDLayer layer];
messageLayer.hidden = YES;
messageLayer.horizontalPlacement = OSD_HPOSITION_LEFT;
messageLayer.verticalPlacement = OSD_VPOSITION_TOP;
_rootLayer.message = messageLayer;
MMovieOSDLayer* errorLayer = [MMovieOSDLayer layer];
errorLayer.hidden = YES;
errorLayer.horizontalPlacement = OSD_HPOSITION_CENTER;
errorLayer.verticalPlacement = OSD_HPOSITION_CENTER;
_rootLayer.error = errorLayer;
}
- (void)subtitleShutdown
{
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:MMovieCurrentTimeNotification];
}
- (void)updateSubtitle
{
if(_subtitleVisible)
{
[self updateSubtitleOSD];
[_rootLayer.subtitle setTextImage:[_subtitleOSD texImage]];
}
}
- (void)subtitleTick:(NSNotification*)theNotification
{
[self updateSubtitle];
}
- (void)dealloc
{
[self subtitleShutdown];
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self invalidateMessageHideTimer];
[self cleanupOSD];
[_subtitle release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (void)redisplay { [self setNeedsDisplay:TRUE]; }
// TODO: remove these. They're just here to ease the CoreAnimation conversion process
- (NSOpenGLContext*)openGLContext
{
if([_rootLayer.movie isKindOfClass:[CAOpenGLLayer class]])
return [(NSOpenGLLayer*)_rootLayer.movie openGLContext];
return nil;
}
- (NSOpenGLPixelFormat*)pixelFormat
{
if([_rootLayer.movie isKindOfClass:[CAOpenGLLayer class]])
return [(NSOpenGLLayer*)_rootLayer.movie openGLPixelFormat];
return nil;
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark movie
- (MMovie*)movie { return _movie; }
- (float)currentFps { return _currentFps; }
- (void)setMovie:(MMovie*)movie
{
//TRACE(@"%s %@", __PRETTY_FUNCTION__, movie);
CALayer<MMovieLayer>* movieLayer = nil;
Class c = [movie class];
if(c == [MMovie_QuickTime class])
{
movieLayer = [MMovieLayer_AVFoundation layer];
}
else if(c == [MMovie_FFmpeg class])
{
movieLayer = [MMovieLayer_FFMPEG layer];
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MMovieCurrentTimeNotification object:_movie];
_movie = movie;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subtitleTick:) name:MMovieCurrentTimeNotification object:_movie];
[movieLayer setMovie:movie];
movieLayer.name = @"Movie";
_rootLayer.movie = movieLayer;
[self removeAllSubtitles];
[self clearOSD];
[self updateOSDImageBaseWidth];
[self updateLetterBoxHeight];
[self updateMovieRect:TRUE];
_lastFpsCheckTime = 0.0;
_fpsElapsedTime = 0.0;
_fpsFrameCount = 0;
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark event-handling
- (BOOL)acceptsFirstResponder { return TRUE; }
- (void)keyDown:(NSEvent*)event
{
//TRACE(@"%s \"%@\" (modifierFlags=%u)", __PRETTY_FUNCTION__,
// [event characters], [event modifierFlags]);
unichar key = [[event characters] characterAtIndex:0];
BOOL shiftPressed = ([event modifierFlags] & NSShiftKeyMask) ? TRUE : FALSE;
switch (key) {
case ' ' : // space: toggle play/pause
[[NSApp delegate] playAction:self];
break;
case NSCarriageReturnCharacter : // return : toggle full-screen
case NSEnterCharacter : // enter (in keypad)
if (_movie) {
[[NSApp delegate] fullScreenAction:self];
}
break;
case 27 : // escape
if ([[NSApp delegate] isFullScreen]) {
[[NSApp delegate] fullScreenAction:self];
}
else if ([[NSApp delegate] isDesktopBackground]) {
[[NSApp delegate] desktopBackgroundAction:self];
}
else if (_movie) {
[[NSApp delegate] closeMovie];
[self showLogo];
}
else {
[[NSApp mainWindow] performClose:self];
}
break;
case 'n' : case 'N' : [[NSApp delegate] fullNavigationAction:self]; break;
case '[' : case '{' : [[NSApp delegate] stepBackward]; break;
case ']' : case '}' : [[NSApp delegate] stepForward]; break;
case 'c' : case 'C' : [[NSApp delegate] changePlayRate:+1]; break;
case 'x' : case 'X' : [[NSApp delegate] changePlayRate:-1]; break;
case 'z' : case 'Z' : [[NSApp delegate] changePlayRate: 0]; break;
case 'v' : case 'V' : [[NSApp delegate] changeSubtitleVisible]; break;
case 's' : case 'S' : [[NSApp delegate] changeSubtitleLanguage:-1]; break;
case 'l' : case 'L' : [[NSApp delegate] changeLetterBoxHeight]; break;
case 'p' : case 'P' : [[NSApp delegate] changeSubtitlePosition]; break;
case ',' : case '<' : [[NSApp delegate] changeSubtitleSync:-1]; break;
case '.' : case '>' : [[NSApp delegate] changeSubtitleSync:+1]; break;
case '/' : case '?' : [[NSApp delegate] changeSubtitleSync: 0]; break;
case 'm' : case 'M' : [[NSApp delegate] setMuted:![_movie muted]]; break;
case 'i' : case 'I' : [self saveCurrentImage:shiftPressed]; break;
}
}
- (void)mouseDown:(NSEvent*)event
{
//TRACE(@"%s clickCount=%d", __PRETTY_FUNCTION__, [event clickCount]);
if (2 <= [event clickCount]) {
[[NSApp delegate] fullScreenAction:self];
}
else {
int action = [self viewDragActionWithModifierFlags:[event modifierFlags]];
if (action == VIEW_DRAG_ACTION_MOVE_WINDOW) {
if (![[NSApp delegate] isFullScreen]) {
[[self window] mouseDown:event];
}
}
}
}
- (void)scrollWheel:(NSEvent*)event
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[[NSApp delegate] scrollWheelAction:event];
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark notification
- (void)windowMoved:(NSNotification*)aNotification
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
NSDictionary* deviceDesc = [[[self window] screen] deviceDescription];
NSNumber* screenNumber = [deviceDesc objectForKey:@"NSScreenNumber"];
CGDirectDisplayID displayID = (CGDirectDisplayID)[screenNumber intValue];
if (displayID && displayID != _displayID) {
_displayID = displayID;
TRACE(@"main window moved: display changed");
[self updateLetterBoxHeight];
[[NSApp delegate] updateLetterBoxHeightMenuItems];
}
}
- (void)frameResized:(NSNotification*)notification
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self updateMovieRect:FALSE];
}
@end