diff --git a/input/drivers/cocoa_input.h b/input/drivers/cocoa_input.h index 4267ab14e0..213d02054f 100644 --- a/input/drivers/cocoa_input.h +++ b/input/drivers/cocoa_input.h @@ -52,7 +52,6 @@ typedef struct int16_t mouse_wd; int16_t mouse_wl; int16_t mouse_wr; - bool mouse_grabbed; } cocoa_input_data_t; #endif diff --git a/input/drivers/cocoa_input.m b/input/drivers/cocoa_input.m index 264314d370..c670e5cbda 100644 --- a/input/drivers/cocoa_input.m +++ b/input/drivers/cocoa_input.m @@ -40,40 +40,14 @@ static CMMotionManager *motionManager; #import #endif -#if TARGET_OS_IPHONE -#define HIDKEY(X) X -#else -#define HIDKEY(X) (X < 128) ? MAC_NATIVE_TO_HID[X] : 0 -#endif - -#define MAX_ICADE_PROFILES 4 -#define MAX_ICADE_KEYS 0x100 - -typedef struct icade_map -{ - bool up; - enum retro_key key; -} icade_map_t; - /* TODO/FIXME - * fix game focus toggle */ -/* - * FORWARD DECLARATIONS - */ #ifdef OSX +/* Forward declaration */ float cocoa_screen_get_backing_scale_factor(void); #endif -#if TARGET_OS_IPHONE -/* TODO/FIXME - static globals */ -static bool small_keyboard_active = false; -static icade_map_t icade_maps[MAX_ICADE_PROFILES][MAX_ICADE_KEYS]; -#if TARGET_OS_IOS -static UISelectionFeedbackGenerator *feedbackGenerator; -#endif -#endif - static bool apple_key_state[MAX_KEYS]; /* Send keyboard inputs directly using RETROK_* codes @@ -88,8 +62,27 @@ void apple_direct_input_keyboard_event(bool down, character, (enum retro_mod)mod, device); } + #if TARGET_OS_IPHONE -static bool apple_input_handle_small_keyboard(unsigned* code, bool down) +/* TODO/FIXME - static globals */ +static bool small_keyboard_active = false; +#if TARGET_OS_IOS +static UISelectionFeedbackGenerator *feedbackGenerator; +#endif + +#define HIDKEY(X) X +#define MAX_ICADE_PROFILES 4 +#define MAX_ICADE_KEYS 0x100 + +typedef struct icade_map +{ + bool up; + enum retro_key key; +} icade_map_t; + +static icade_map_t icade_maps[MAX_ICADE_PROFILES][MAX_ICADE_KEYS]; + +static bool handle_small_keyboard(unsigned* code, bool down) { static uint8_t mapping[128]; static bool map_initialized; @@ -144,10 +137,12 @@ static bool apple_input_handle_small_keyboard(unsigned* code, bool down) return false; } -static bool apple_input_handle_icade_event(unsigned kb_type_idx, unsigned *code, bool *keydown) +static bool handle_icade_event(unsigned *code, bool *keydown) { static bool initialized = false; bool ret = false; + settings_t *settings = config_get_ptr(); + unsigned kb_type_idx = settings->uints.input_keyboard_gamepad_mapping_type; if (!initialized) { @@ -299,19 +294,18 @@ void apple_input_keyboard_event(bool down, settings_t *settings = config_get_ptr(); bool keyboard_gamepad_enable = settings->bools.input_keyboard_gamepad_enable; bool small_keyboard_enable = settings->bools.input_small_keyboard_enable; + code = HIDKEY(code); if (keyboard_gamepad_enable) { - if (apple_input_handle_icade_event( - settings->uints.input_keyboard_gamepad_mapping_type, - &code, &down)) + if (handle_icade_event(&code, &down)) character = 0; else code = 0; } else if (small_keyboard_enable) { - if (apple_input_handle_small_keyboard(&code, down)) + if (handle_small_keyboard(&code, down)) character = 0; } @@ -325,21 +319,24 @@ void apple_input_keyboard_event(bool down, character, (enum retro_mod)mod, device); } #else +/* Taken from https://github.com/depp/keycode, + * check keycode.h for license. */ +static const unsigned char MAC_NATIVE_TO_HID[128] = { + 4, 22, 7, 9, 11, 10, 29, 27, 6, 25,255, 5, 20, 26, 8, 21, + 28, 23, 30, 31, 32, 33, 35, 34, 46, 38, 36, 45, 37, 39, 48, 18, + 24, 47, 12, 19, 40, 15, 13, 52, 14, 51, 49, 54, 56, 17, 16, 55, + 43, 44, 53, 42,255, 41,231,227,225, 57,226,224,229,230,228,255, + 108, 99,255, 85,255, 87,255, 83,255,255,255, 84, 88,255, 86,109, + 110,103, 98, 89, 90, 91, 92, 93, 94, 95,111, 96, 97,255,255,255, + 62, 63, 64, 60, 65, 66,255, 68,255,104,107,105,255, 67,255, 69, + 255,106,117, 74, 75, 76, 61, 77, 59, 78, 58, 80, 79, 81, 82,255 +}; + +#define HIDKEY(X) (X < 128) ? MAC_NATIVE_TO_HID[X] : 0 + void apple_input_keyboard_event(bool down, unsigned code, uint32_t character, uint32_t mod, unsigned device) { - /* Taken from https://github.com/depp/keycode, - * check keycode.h for license. */ - static const unsigned char MAC_NATIVE_TO_HID[128] = { - 4, 22, 7, 9, 11, 10, 29, 27, 6, 25,255, 5, 20, 26, 8, 21, - 28, 23, 30, 31, 32, 33, 35, 34, 46, 38, 36, 45, 37, 39, 48, 18, - 24, 47, 12, 19, 40, 15, 13, 52, 14, 51, 49, 54, 56, 17, 16, 55, - 43, 44, 53, 42,255, 41,231,227,225, 57,226,224,229,230,228,255, - 108, 99,255, 85,255, 87,255, 83,255,255,255, 84, 88,255, 86,109, - 110,103, 98, 89, 90, 91, 92, 93, 94, 95,111, 96, 97,255,255,255, - 62, 63, 64, 60, 65, 66,255, 68,255,104,107,105,255, 67,255, 69, - 255,106,117, 74, 75, 76, 61, 77, 59, 78, 58, 80, 79, 81, 82,255 - }; code = HIDKEY(code); if (code == 0 || code >= MAX_KEYS) return; @@ -354,7 +351,6 @@ void apple_input_keyboard_event(bool down, static void *cocoa_input_init(const char *joypad_driver) { - cocoa_input_data_t *apple = NULL; #ifdef HAVE_COREMOTION if (@available(macOS 10.15, *)) if (!motionManager) @@ -367,9 +363,8 @@ static void *cocoa_input_init(const char *joypad_driver) [feedbackGenerator prepare]; #endif - /* TODO/FIXME - shouldn't we free the above in case this fails for - * TARGET_OS_IOS / HAVE_COREMOTION? */ - if (!(apple = (cocoa_input_data_t*)calloc(1, sizeof(*apple)))) + cocoa_input_data_t *apple = (cocoa_input_data_t*)calloc(1, sizeof(*apple)); + if (!apple) return NULL; input_keymaps_init_keyboard_lut(rarch_key_map_apple_hid); @@ -442,8 +437,7 @@ static int16_t cocoa_input_state( { for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++) { - if ((binds[port][i].key < RETROK_LAST) - && apple_key_state[rarch_keysym_lut[binds[port][i].key]]) + if ((binds[port][i].key < RETROK_LAST) && apple_key_state[rarch_keysym_lut[binds[port][i].key]]) ret |= (1 << i); } } @@ -576,16 +570,25 @@ static int16_t cocoa_input_state( if (touch) { + int16_t x, y; + const bool want_full = (device == RARCH_DEVICE_POINTER_SCREEN); + switch (id) { case RETRO_DEVICE_ID_POINTER_PRESSED: - if (device == RARCH_DEVICE_POINTER_SCREEN) - return (touch->full_x != -0x8000) && (touch->full_y != -0x8000); /* Inside? */ - return (touch->fixed_x != -0x8000) && (touch->fixed_y != -0x8000); /* Inside? */ + x = touch->fixed_x; + y = touch->fixed_y; + + if (want_full) + { + x = touch->full_x; + y = touch->full_y; + } + return (x != -0x8000) && (y != -0x8000); /* Inside? */ case RETRO_DEVICE_ID_POINTER_X: - return (device == RARCH_DEVICE_POINTER_SCREEN) ? touch->full_x : touch->fixed_x; + return want_full ? touch->full_x : touch->fixed_x; case RETRO_DEVICE_ID_POINTER_Y: - return (device == RARCH_DEVICE_POINTER_SCREEN) ? touch->full_y : touch->fixed_y; + return want_full ? touch->full_y : touch->fixed_y; case RETRO_DEVICE_ID_POINTER_COUNT: return apple->touch_count; } @@ -623,17 +626,16 @@ static uint64_t cocoa_input_get_capabilities(void *data) } static bool cocoa_input_set_sensor_state(void *data, unsigned port, - enum retro_sensor_action action, unsigned rate) + enum retro_sensor_action action, unsigned rate) { - if ( (action != RETRO_SENSOR_ACCELEROMETER_ENABLE) - && (action != RETRO_SENSOR_ACCELEROMETER_DISABLE) - && (action != RETRO_SENSOR_GYROSCOPE_ENABLE) - && (action != RETRO_SENSOR_GYROSCOPE_DISABLE)) + if (action != RETRO_SENSOR_ACCELEROMETER_ENABLE && + action != RETRO_SENSOR_ACCELEROMETER_DISABLE && + action != RETRO_SENSOR_GYROSCOPE_ENABLE && + action != RETRO_SENSOR_GYROSCOPE_DISABLE) return false; #ifdef HAVE_MFI - if (@available(iOS 14.0, macOS 11.0, *)) - { + if (@available(iOS 14.0, macOS 11.0, *)) { for (GCController *controller in [GCController controllers]) { if (!controller || controller.playerIndex != port) @@ -642,15 +644,13 @@ static bool cocoa_input_set_sensor_state(void *data, unsigned port, break; if (controller.motion.sensorsRequireManualActivation) { - /* This is a bug, we assume if you turn on/off either - * you want both on/off */ - if ( (action == RETRO_SENSOR_ACCELEROMETER_ENABLE) - || (action == RETRO_SENSOR_GYROSCOPE_ENABLE)) + // this is a bug, we assume if you turn on/off either you want both on/off + if (action == RETRO_SENSOR_ACCELEROMETER_ENABLE || action == RETRO_SENSOR_GYROSCOPE_ENABLE) controller.motion.sensorsActive = YES; else controller.motion.sensorsActive = NO; } - /* no such thing as update interval for GCController? */ + // no such thing as update interval for GCController? return true; } } @@ -663,8 +663,7 @@ static bool cocoa_input_set_sensor_state(void *data, unsigned port, if (!motionManager || !motionManager.deviceMotionAvailable) return false; - if ( (action == RETRO_SENSOR_ACCELEROMETER_ENABLE) - || (action == RETRO_SENSOR_GYROSCOPE_ENABLE)) + if (action == RETRO_SENSOR_ACCELEROMETER_ENABLE || action == RETRO_SENSOR_GYROSCOPE_ENABLE) { if (!motionManager.deviceMotionActive) [motionManager startDeviceMotionUpdates]; @@ -685,8 +684,7 @@ static bool cocoa_input_set_sensor_state(void *data, unsigned port, static float cocoa_input_get_sensor_input(void *data, unsigned port, unsigned id) { #ifdef HAVE_MFI - if (@available(iOS 14.0, *)) - { + if (@available(iOS 14.0, *)) { for (GCController *controller in [GCController controllers]) { if (!controller || controller.playerIndex != port) @@ -713,23 +711,26 @@ static float cocoa_input_get_sensor_input(void *data, unsigned port, unsigned id #endif #ifdef HAVE_COREMOTION - if (port == 0 && motionManager && motionManager.deviceMotionActive) + if (port != 0) + return 0.0f; + + if (!motionManager || !motionManager.deviceMotionActive) + return 0.0f; + + switch (id) { - switch (id) - { - case RETRO_SENSOR_ACCELEROMETER_X: - return motionManager.deviceMotion.userAcceleration.x; - case RETRO_SENSOR_ACCELEROMETER_Y: - return motionManager.deviceMotion.userAcceleration.y; - case RETRO_SENSOR_ACCELEROMETER_Z: - return motionManager.deviceMotion.userAcceleration.z; - case RETRO_SENSOR_GYROSCOPE_X: - return motionManager.deviceMotion.rotationRate.x; - case RETRO_SENSOR_GYROSCOPE_Y: - return motionManager.deviceMotion.rotationRate.y; - case RETRO_SENSOR_GYROSCOPE_Z: - return motionManager.deviceMotion.rotationRate.z; - } + case RETRO_SENSOR_ACCELEROMETER_X: + return motionManager.deviceMotion.userAcceleration.x; + case RETRO_SENSOR_ACCELEROMETER_Y: + return motionManager.deviceMotion.userAcceleration.y; + case RETRO_SENSOR_ACCELEROMETER_Z: + return motionManager.deviceMotion.userAcceleration.z; + case RETRO_SENSOR_GYROSCOPE_X: + return motionManager.deviceMotion.rotationRate.x; + case RETRO_SENSOR_GYROSCOPE_Y: + return motionManager.deviceMotion.rotationRate.y; + case RETRO_SENSOR_GYROSCOPE_Z: + return motionManager.deviceMotion.rotationRate.z; } #endif @@ -737,33 +738,13 @@ static float cocoa_input_get_sensor_input(void *data, unsigned port, unsigned id } #if TARGET_OS_IOS -static void cocoa_input_keypress_vibrate(void) +static void cocoa_input_keypress_vibrate() { [feedbackGenerator selectionChanged]; [feedbackGenerator prepare]; } #endif -#if defined(HAVE_COCOA) || defined(HAVE_COCOA_METAL) -static void cocoa_input_grab_mouse(void *data, bool state) -{ - cocoa_input_data_t *apple = (cocoa_input_data_t*)data; - - if (state) - { - NSWindow *window = (BRIDGE NSWindow*)ui_companion_cocoa.get_main_window(nil); - CGPoint window_pos = window.frame.origin; - CGSize window_size = window.frame.size; - CGPoint window_center = CGPointMake(window_pos.x + window_size.width / 2.0f, window_pos.y + window_size.height / 2.0f); - CGWarpMouseCursorPosition(window_center); - } - - CGAssociateMouseAndMouseCursorPosition(!state); - cocoa_show_mouse(nil, !state); - apple->mouse_grabbed = state; -} -#endif - input_driver_t input_cocoa = { cocoa_input_init, cocoa_input_poll, @@ -773,15 +754,11 @@ input_driver_t input_cocoa = { cocoa_input_get_sensor_input, cocoa_input_get_capabilities, "cocoa", -#if defined(HAVE_COCOA) || defined(HAVE_COCOA_METAL) - cocoa_input_grab_mouse, -#else NULL, /* grab_mouse */ -#endif NULL, /* grab_stdin */ #if TARGET_OS_IOS cocoa_input_keypress_vibrate #else - NULL /* vibrate */ + NULL #endif }; diff --git a/pkg/apple/RetroArch_Metal.xcodeproj/project.pbxproj b/pkg/apple/RetroArch_Metal.xcodeproj/project.pbxproj index 1816c415d7..44ff460c05 100644 --- a/pkg/apple/RetroArch_Metal.xcodeproj/project.pbxproj +++ b/pkg/apple/RetroArch_Metal.xcodeproj/project.pbxproj @@ -454,6 +454,7 @@ 05C5D53320E3DD0900654EE4 /* input_types.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = input_types.h; sourceTree = ""; }; 05C5D53820E3DD0900654EE4 /* cocoa_input.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cocoa_input.h; sourceTree = ""; }; 05C5D54120E3DD0900654EE4 /* sdl_input.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = sdl_input.c; sourceTree = ""; }; + 05C5D54220E3DD0900654EE4 /* cocoa_input.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = cocoa_input.c; sourceTree = ""; }; 05C5D54C20E3DD0900654EE4 /* input_keymaps.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = input_keymaps.h; sourceTree = ""; }; 05C5D54E20E3DD0900654EE4 /* blissbox.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = blissbox.h; sourceTree = ""; }; 05C5D55420E3DD0900654EE4 /* GCExtendedGamepadSnapshot.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GCExtendedGamepadSnapshot.h; sourceTree = ""; }; @@ -553,7 +554,6 @@ A9020FA64527ED74C836B41D /* cocoa_gl_ctx_metal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = cocoa_gl_ctx_metal.m; sourceTree = ""; }; D27C50872228360000113BC0 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; D27C50892228360D00113BC0 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; }; - E8EE1E942A2C6A26003C73F6 /* cocoa_input.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = cocoa_input.m; sourceTree = ""; }; F0B1238F270D73A90006E60F /* connect */ = {isa = PBXFileReference; lastKnownFileType = folder; path = connect; sourceTree = ""; }; /* End PBXFileReference section */ @@ -838,6 +838,7 @@ isa = PBXGroup; children = ( 05A8C55E20DB72F000FF7857 /* menu_osk_utf8_pages.h */, + 05A8C55F20DB72F000FF7857 /* menu_osk.h */, ); path = widgets; sourceTree = ""; @@ -1239,7 +1240,7 @@ 05C5D53520E3DD0900654EE4 /* drivers */ = { isa = PBXGroup; children = ( - E8EE1E942A2C6A26003C73F6 /* cocoa_input.m */, + 05C5D54220E3DD0900654EE4 /* cocoa_input.c */, 05C5D53820E3DD0900654EE4 /* cocoa_input.h */, 05C5D54120E3DD0900654EE4 /* sdl_input.c */, ); diff --git a/ui/drivers/ui_cocoa.m b/ui/drivers/ui_cocoa.m index 19a67a2e1d..b2041e9a0a 100644 --- a/ui/drivers/ui_cocoa.m +++ b/ui/drivers/ui_cocoa.m @@ -467,14 +467,8 @@ static ui_application_t ui_application_cocoa = { /* Absolute */ apple->touches[0].screen_x = (int16_t)pos.x; apple->touches[0].screen_y = (int16_t)pos.y; - - if (apple->mouse_grabbed) { - apple->window_pos_x += (int16_t)delta_x; - apple->window_pos_y += (int16_t)delta_y; - } else { - apple->window_pos_x = (int16_t)pos.x; - apple->window_pos_y = (int16_t)pos.y; - } + apple->window_pos_x = (int16_t)pos.x; + apple->window_pos_y = (int16_t)pos.y; } break; #if defined(HAVE_COCOA_METAL)