-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoView.m
169 lines (152 loc) · 4.79 KB
/
VideoView.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
//
// VideoView.m
// msx1-osx
//
// Created by Yoji Suzuki on 2023/08/03.
//
#import "VideoLayer.h"
#import "VideoView.h"
#import "emu.h"
#import "msx1def.h"
#include <ctype.h>
// Comment out the following #define if you want to assign cursor, Z and X on the keyboard as the original key, not the joypad.
#define ASSIGN_KEYBOARD_TO_JOYPAD
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* context);
extern unsigned char emu_key;
extern unsigned char emu_keycode;
@interface VideoView ()
@property (nonatomic) VideoLayer* videoLayer;
@property CVDisplayLinkRef displayLink;
@property (nonatomic, readwrite) BOOL pausing;
@property (nonatomic, readwrite) BOOL paused;
@end
@implementation VideoView
+ (Class)layerClass
{
return [VideoLayer class];
}
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]) != nil) {
[self setWantsLayer:YES];
_videoLayer = [VideoLayer layer];
[self setLayer:_videoLayer];
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
CVDisplayLinkSetOutputCallback(_displayLink, MyDisplayLinkCallback, (__bridge void*)self);
CVDisplayLinkStart(_displayLink);
}
return self;
}
- (void)releaseDisplayLink
{
if (_displayLink) {
CVDisplayLinkStop(_displayLink);
CVDisplayLinkRelease(_displayLink);
_displayLink = nil;
}
}
- (void)vsync
{
if (!_pausing) {
_paused = NO;
emu_vsync();
[self.videoLayer drawVRAM];
} else {
_paused = YES;
}
}
- (void)pauseWithCompletionHandler:(void (^)(void))completionHandler
{
if (_pausing) {
completionHandler();
return;
}
__weak VideoView* wealSelf = self;
_paused = NO;
_pausing = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (NO == wealSelf.paused) usleep(100);
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler();
});
});
}
- (void)resumeWithCompletionHandler:(void (^)(void))completionHandler
{
if (!_pausing) {
completionHandler();
return;
}
__weak VideoView* wealSelf = self;
_paused = YES;
_pausing = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (YES == wealSelf.paused) usleep(100);
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler();
});
});
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (unichar)keyMapFrom:(unichar)c
{
switch (c) {
case '5': return 0x05;
default: return c;
}
}
- (void)keyDown:(NSEvent*)event
{
unichar c = [self keyMapFrom:[event.charactersIgnoringModifiers characterAtIndex:0]];
//NSLog(@"keyDown: %04X", tolower(c));
switch (tolower(c)) {
#ifdef ASSIGN_KEYBOARD_TO_JOYPAD
case 0xF703: emu_key |= MSX1_JOY_RI; break;
case 0xF702: emu_key |= MSX1_JOY_LE; break;
case 0xF701: emu_key |= MSX1_JOY_DW; break;
case 0xF700: emu_key |= MSX1_JOY_UP; break;
case 0x0078: emu_key |= MSX1_JOY_T2; break;
case 0x007A: emu_key |= MSX1_JOY_T1; break;
#else
case 0xF700: emu_keycode = 0xC0; break;
case 0xF701: emu_keycode = 0xC1; break;
case 0xF702: emu_keycode = 0xC2; break;
case 0xF703: emu_keycode = 0xC3; break;
#endif
case 0xF704: emu_keycode = 0xF1; break;
case 0xF705: emu_keycode = 0xF2; break;
case 0xF706: emu_keycode = 0xF3; break;
case 0xF707: emu_keycode = 0xF4; break;
case 0xF708: emu_keycode = 0xF5; break;
case 0xF709: emu_keycode = 0xF6; break;
case 0xF70A: emu_keycode = 0xF7; break;
case 0xF70B: emu_keycode = 0xF8; break;
case 0xF70C: emu_keycode = 0xF9; break;
case 0xF70D: emu_keycode = 0xFA; break;
default: emu_keycode = c;
}
}
- (void)keyUp:(NSEvent*)event
{
#ifdef ASSIGN_KEYBOARD_TO_JOYPAD
unichar c = [self keyMapFrom:[event.charactersIgnoringModifiers characterAtIndex:0]];
switch (tolower(c)) {
case 0xF703: emu_key &= ~MSX1_JOY_RI; break;
case 0xF702: emu_key &= ~MSX1_JOY_LE; break;
case 0xF701: emu_key &= ~MSX1_JOY_DW; break;
case 0xF700: emu_key &= ~MSX1_JOY_UP; break;
case 0x0078: emu_key &= ~MSX1_JOY_T2; break;
case 0x007A: emu_key &= ~MSX1_JOY_T1; break;
}
#endif
emu_keycode = 0;
}
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* context)
{
[(__bridge VideoLayer*)context performSelectorOnMainThread:@selector(vsync) withObject:nil waitUntilDone:NO];
return kCVReturnSuccess;
}
@end