2016-06-08 21:37:00 +00:00
|
|
|
#import <Carbon/Carbon.h>
|
2016-03-30 20:07:55 +00:00
|
|
|
#import "GBView.h"
|
2018-06-11 17:23:51 +00:00
|
|
|
#import "GBViewGL.h"
|
2018-06-15 09:58:33 +00:00
|
|
|
#import "GBViewMetal.h"
|
2016-04-13 19:43:16 +00:00
|
|
|
#import "GBButtons.h"
|
|
|
|
#import "NSString+StringForKey.h"
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2018-06-26 16:36:14 +00:00
|
|
|
#define JOYSTICK_HIGH 0x4000
|
|
|
|
#define JOYSTICK_LOW 0x3800
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
@implementation GBView
|
|
|
|
{
|
|
|
|
uint32_t *image_buffers[3];
|
|
|
|
unsigned char current_buffer;
|
2016-07-05 20:34:33 +00:00
|
|
|
BOOL mouse_hidden;
|
|
|
|
NSTrackingArea *tracking_area;
|
|
|
|
BOOL _mouseHidingEnabled;
|
2018-06-26 16:36:14 +00:00
|
|
|
bool axisActive[2];
|
2018-02-10 21:30:30 +00:00
|
|
|
bool underclockKeyDown;
|
|
|
|
double clockMultiplier;
|
|
|
|
NSEventModifierFlags previousModifiers;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 17:23:51 +00:00
|
|
|
+ (instancetype)alloc
|
2016-06-14 11:11:37 +00:00
|
|
|
{
|
2018-06-15 09:58:33 +00:00
|
|
|
return [self allocWithZone:NULL];
|
2018-06-11 17:23:51 +00:00
|
|
|
}
|
2016-06-14 11:11:37 +00:00
|
|
|
|
2018-06-11 17:23:51 +00:00
|
|
|
+ (instancetype)allocWithZone:(struct _NSZone *)zone
|
|
|
|
{
|
|
|
|
if (self == [GBView class]) {
|
2018-06-15 17:03:59 +00:00
|
|
|
if ([GBViewMetal isSupported]) {
|
|
|
|
return [GBViewMetal allocWithZone: zone];
|
|
|
|
}
|
|
|
|
return [GBViewGL allocWithZone: zone];
|
2018-06-11 17:23:51 +00:00
|
|
|
}
|
|
|
|
return [super allocWithZone:zone];
|
2016-06-14 11:11:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 17:23:51 +00:00
|
|
|
- (void) createInternalView
|
|
|
|
{
|
|
|
|
assert(false && "createInternalView must not be inherited");
|
|
|
|
}
|
2016-06-14 11:11:37 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
- (void) _init
|
|
|
|
{
|
|
|
|
image_buffers[0] = malloc(160 * 144 * 4);
|
|
|
|
image_buffers[1] = malloc(160 * 144 * 4);
|
|
|
|
image_buffers[2] = malloc(160 * 144 * 4);
|
|
|
|
_shouldBlendFrameWithPrevious = 1;
|
2016-07-05 18:23:55 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
|
2016-07-05 20:34:33 +00:00
|
|
|
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
|
|
|
|
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
|
|
|
|
owner:self
|
|
|
|
userInfo:nil];
|
|
|
|
[self addTrackingArea:tracking_area];
|
2018-02-10 21:30:30 +00:00
|
|
|
clockMultiplier = 1.0;
|
2018-06-11 17:23:51 +00:00
|
|
|
[self createInternalView];
|
|
|
|
[self addSubview:self.internalView];
|
|
|
|
self.internalView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 18:23:55 +00:00
|
|
|
- (void) ratioKeepingChanged
|
|
|
|
{
|
|
|
|
[self setFrame:self.superview.frame];
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:06:55 +00:00
|
|
|
- (void) setShouldBlendFrameWithPrevious:(BOOL)shouldBlendFrameWithPrevious
|
|
|
|
{
|
|
|
|
_shouldBlendFrameWithPrevious = shouldBlendFrameWithPrevious;
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
- (unsigned char) numberOfBuffers
|
|
|
|
{
|
|
|
|
return _shouldBlendFrameWithPrevious? 3 : 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
free(image_buffers[0]);
|
|
|
|
free(image_buffers[1]);
|
|
|
|
free(image_buffers[2]);
|
2016-07-05 20:34:33 +00:00
|
|
|
if (mouse_hidden) {
|
|
|
|
mouse_hidden = false;
|
|
|
|
[NSCursor unhide];
|
|
|
|
}
|
2016-04-28 20:07:05 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)coder
|
|
|
|
{
|
|
|
|
if (!(self = [super initWithCoder:coder]))
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
[self _init];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(NSRect)frameRect
|
|
|
|
{
|
|
|
|
if (!(self = [super initWithFrame:frameRect]))
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
[self _init];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:58:25 +00:00
|
|
|
- (void)setFrame:(NSRect)frame
|
|
|
|
{
|
|
|
|
frame = self.superview.frame;
|
2016-07-05 18:23:55 +00:00
|
|
|
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAspectRatioUnkept"]) {
|
|
|
|
double ratio = frame.size.width / frame.size.height;
|
|
|
|
if (ratio >= 160.0/144.0) {
|
|
|
|
double new_width = round(frame.size.height / 144.0 * 160.0);
|
|
|
|
frame.origin.x = floor((frame.size.width - new_width) / 2);
|
|
|
|
frame.size.width = new_width;
|
|
|
|
frame.origin.y = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
double new_height = round(frame.size.width / 160.0 * 144.0);
|
|
|
|
frame.origin.y = floor((frame.size.height - new_height) / 2);
|
|
|
|
frame.size.height = new_height;
|
|
|
|
frame.origin.x = 0;
|
|
|
|
}
|
2016-07-03 17:58:25 +00:00
|
|
|
}
|
2016-07-05 20:34:33 +00:00
|
|
|
|
2016-07-03 17:58:25 +00:00
|
|
|
[super setFrame:frame];
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
- (void) flip
|
|
|
|
{
|
2018-02-10 21:30:30 +00:00
|
|
|
if (underclockKeyDown && clockMultiplier > 0.5) {
|
|
|
|
clockMultiplier -= 0.1;
|
|
|
|
GB_set_clock_multiplier(_gb, clockMultiplier);
|
|
|
|
}
|
|
|
|
if (!underclockKeyDown && clockMultiplier < 1.0) {
|
|
|
|
clockMultiplier += 0.1;
|
|
|
|
GB_set_clock_multiplier(_gb, clockMultiplier);
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
current_buffer = (current_buffer + 1) % self.numberOfBuffers;
|
2018-03-02 17:42:02 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
});
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (uint32_t *) pixels
|
|
|
|
{
|
|
|
|
return image_buffers[(current_buffer + 1) % self.numberOfBuffers];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)keyDown:(NSEvent *)theEvent
|
|
|
|
{
|
2017-01-24 19:00:56 +00:00
|
|
|
unsigned short keyCode = theEvent.keyCode;
|
2016-04-13 19:43:16 +00:00
|
|
|
bool handled = false;
|
|
|
|
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
for (GBButton i = 0; i < GBButtonCount; i++) {
|
2017-01-24 19:00:56 +00:00
|
|
|
if ([defaults integerForKey:button_to_preference_name(i)] == keyCode) {
|
2016-04-13 19:43:16 +00:00
|
|
|
handled = true;
|
|
|
|
switch (i) {
|
|
|
|
case GBTurbo:
|
2018-02-10 12:42:14 +00:00
|
|
|
GB_set_turbo_mode(_gb, true, self.isRewinding);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GBRewind:
|
|
|
|
self.isRewinding = true;
|
|
|
|
GB_set_turbo_mode(_gb, false, false);
|
2016-04-13 19:43:16 +00:00
|
|
|
break;
|
2018-02-10 21:30:30 +00:00
|
|
|
|
|
|
|
case GBUnderclock:
|
|
|
|
underclockKeyDown = true;
|
|
|
|
break;
|
2016-04-13 19:43:16 +00:00
|
|
|
|
|
|
|
default:
|
2017-04-17 17:16:17 +00:00
|
|
|
GB_set_key_state(_gb, (GB_key_t)i, true);
|
2016-04-13 19:43:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:37:40 +00:00
|
|
|
if (!handled && [theEvent type] != NSEventTypeFlagsChanged) {
|
2016-04-13 19:43:16 +00:00
|
|
|
[super keyDown:theEvent];
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)keyUp:(NSEvent *)theEvent
|
|
|
|
{
|
2017-01-24 19:00:56 +00:00
|
|
|
unsigned short keyCode = theEvent.keyCode;
|
2016-04-13 19:43:16 +00:00
|
|
|
bool handled = false;
|
|
|
|
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
for (GBButton i = 0; i < GBButtonCount; i++) {
|
2017-01-24 19:00:56 +00:00
|
|
|
if ([defaults integerForKey:button_to_preference_name(i)] == keyCode) {
|
2016-04-13 19:43:16 +00:00
|
|
|
handled = true;
|
|
|
|
switch (i) {
|
|
|
|
case GBTurbo:
|
2017-04-19 18:55:58 +00:00
|
|
|
GB_set_turbo_mode(_gb, false, false);
|
2016-04-13 19:43:16 +00:00
|
|
|
break;
|
2018-02-10 12:42:14 +00:00
|
|
|
|
|
|
|
case GBRewind:
|
|
|
|
self.isRewinding = false;
|
|
|
|
break;
|
2018-02-10 21:30:30 +00:00
|
|
|
|
|
|
|
case GBUnderclock:
|
|
|
|
underclockKeyDown = false;
|
|
|
|
break;
|
2016-04-13 19:43:16 +00:00
|
|
|
|
|
|
|
default:
|
2017-04-17 17:16:17 +00:00
|
|
|
GB_set_key_state(_gb, (GB_key_t)i, false);
|
2016-04-13 19:43:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:37:40 +00:00
|
|
|
if (!handled && [theEvent type] != NSEventTypeFlagsChanged) {
|
2016-04-13 19:43:16 +00:00
|
|
|
[super keyUp:theEvent];
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 14:23:17 +00:00
|
|
|
- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state
|
|
|
|
{
|
2018-02-02 17:22:08 +00:00
|
|
|
UpdateSystemActivity(UsrActivity);
|
2017-12-30 14:23:17 +00:00
|
|
|
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"][joystick_name];
|
|
|
|
|
|
|
|
for (GBButton i = 0; i < GBButtonCount; i++) {
|
|
|
|
NSNumber *mapped_button = [mapping objectForKey:GBButtonNames[i]];
|
|
|
|
if (mapped_button && [mapped_button integerValue] == button) {
|
|
|
|
switch (i) {
|
|
|
|
case GBTurbo:
|
2018-02-10 12:42:14 +00:00
|
|
|
GB_set_turbo_mode(_gb, state, state && self.isRewinding);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GBRewind:
|
|
|
|
self.isRewinding = state;
|
|
|
|
if (state) {
|
|
|
|
GB_set_turbo_mode(_gb, false, false);
|
|
|
|
}
|
2017-12-30 14:23:17 +00:00
|
|
|
break;
|
2018-02-10 21:30:30 +00:00
|
|
|
|
|
|
|
case GBUnderclock:
|
|
|
|
underclockKeyDown = state;
|
|
|
|
break;
|
2017-12-30 14:23:17 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
GB_set_key_state(_gb, (GB_key_t)i, state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value
|
|
|
|
{
|
2018-02-02 17:22:08 +00:00
|
|
|
UpdateSystemActivity(UsrActivity);
|
2017-12-30 14:23:17 +00:00
|
|
|
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"][joystick_name];
|
|
|
|
NSNumber *x_axis = [mapping objectForKey:@"XAxis"];
|
|
|
|
NSNumber *y_axis = [mapping objectForKey:@"YAxis"];
|
|
|
|
|
2018-06-26 16:36:14 +00:00
|
|
|
if (axis == [x_axis integerValue]) {
|
|
|
|
if (value > JOYSTICK_HIGH) {
|
|
|
|
axisActive[0] = true;
|
|
|
|
GB_set_key_state(_gb, GB_KEY_RIGHT, true);
|
|
|
|
GB_set_key_state(_gb, GB_KEY_LEFT, false);
|
|
|
|
}
|
|
|
|
else if (value < -JOYSTICK_HIGH) {
|
|
|
|
axisActive[0] = true;
|
|
|
|
GB_set_key_state(_gb, GB_KEY_RIGHT, false);
|
|
|
|
GB_set_key_state(_gb, GB_KEY_LEFT, true);
|
|
|
|
}
|
|
|
|
else if (axisActive[0] && value < JOYSTICK_LOW && value > -JOYSTICK_LOW) {
|
|
|
|
axisActive[0] = false;
|
|
|
|
GB_set_key_state(_gb, GB_KEY_RIGHT, false);
|
|
|
|
GB_set_key_state(_gb, GB_KEY_LEFT, false);
|
|
|
|
}
|
2017-12-30 14:23:17 +00:00
|
|
|
}
|
2018-06-26 16:36:14 +00:00
|
|
|
else if (axis == [y_axis integerValue]) {
|
|
|
|
if (value > JOYSTICK_HIGH) {
|
|
|
|
axisActive[1] = true;
|
|
|
|
GB_set_key_state(_gb, GB_KEY_DOWN, true);
|
|
|
|
GB_set_key_state(_gb, GB_KEY_UP, false);
|
|
|
|
}
|
|
|
|
else if (value < -JOYSTICK_HIGH) {
|
|
|
|
axisActive[1] = true;
|
|
|
|
GB_set_key_state(_gb, GB_KEY_DOWN, false);
|
|
|
|
GB_set_key_state(_gb, GB_KEY_UP, true);
|
|
|
|
}
|
|
|
|
else if (axisActive[1] && value < JOYSTICK_LOW && value > -JOYSTICK_LOW) {
|
|
|
|
axisActive[1] = false;
|
|
|
|
GB_set_key_state(_gb, GB_KEY_DOWN, false);
|
|
|
|
GB_set_key_state(_gb, GB_KEY_UP, false);
|
|
|
|
}
|
2017-12-30 14:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2016-07-05 20:34:33 +00:00
|
|
|
|
|
|
|
- (void)mouseEntered:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
if (!mouse_hidden) {
|
|
|
|
mouse_hidden = true;
|
|
|
|
if (_mouseHidingEnabled) {
|
|
|
|
[NSCursor hide];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[super mouseEntered:theEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseExited:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
if (mouse_hidden) {
|
|
|
|
mouse_hidden = false;
|
|
|
|
if (_mouseHidingEnabled) {
|
|
|
|
[NSCursor unhide];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[super mouseExited:theEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMouseHidingEnabled:(BOOL)mouseHidingEnabled
|
|
|
|
{
|
|
|
|
if (mouseHidingEnabled == _mouseHidingEnabled) return;
|
|
|
|
|
|
|
|
_mouseHidingEnabled = mouseHidingEnabled;
|
|
|
|
|
|
|
|
if (mouse_hidden && _mouseHidingEnabled) {
|
|
|
|
[NSCursor hide];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse_hidden && !_mouseHidingEnabled) {
|
|
|
|
[NSCursor unhide];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isMouseHidingEnabled
|
|
|
|
{
|
|
|
|
return _mouseHidingEnabled;
|
|
|
|
}
|
2017-12-30 14:23:17 +00:00
|
|
|
|
2018-02-10 21:30:30 +00:00
|
|
|
- (void) flagsChanged:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if (event.modifierFlags > previousModifiers) {
|
|
|
|
[self keyDown:event];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
[self keyUp:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
previousModifiers = event.modifierFlags;
|
|
|
|
}
|
|
|
|
|
2018-06-11 17:23:51 +00:00
|
|
|
- (uint32_t *)currentBuffer
|
|
|
|
{
|
|
|
|
return image_buffers[current_buffer];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (uint32_t *)previousBuffer
|
|
|
|
{
|
|
|
|
return image_buffers[(current_buffer + 2) % self.numberOfBuffers];
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
@end
|