RetroArch/input/drivers/cocoa_input.c

475 lines
12 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2014-01-01 00:50:59 +00:00
* Copyright (C) 2013-2014 - Jason Fetters
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <unistd.h>
2013-06-22 17:09:38 +00:00
2015-01-12 05:16:52 +00:00
#include "../input_common.h"
#include "../input_joypad.h"
#include "../input_keymaps.h"
2015-04-19 15:59:45 +00:00
#include "cocoa_input.h"
2015-01-12 05:16:52 +00:00
#include "../../general.h"
#include "../../driver.h"
#include "apple_keycode.h"
2015-11-14 21:13:47 +00:00
struct apple_key_name_map_entry
{
const char* const keyname;
const uint32_t hid_id;
};
const struct apple_key_name_map_entry apple_key_name_map[] =
{
2014-10-03 14:01:29 +00:00
{ "left", KEY_Left },
{ "right", KEY_Right },
{ "up", KEY_Up },
{ "down", KEY_Down },
{ "enter", KEY_Enter },
{ "kp_enter", KP_Enter },
{ "space", KEY_Space },
{ "tab", KEY_Tab },
{ "shift", KEY_LeftShift },
{ "rshift", KEY_RightShift },
{ "ctrl", KEY_LeftControl },
{ "alt", KEY_LeftAlt },
{ "escape", KEY_Escape },
{ "backspace", KEY_DeleteForward },
{ "backquote", KEY_Grave },
{ "pause", KEY_Pause },
{ "f1", KEY_F1 },
{ "f2", KEY_F2 },
{ "f3", KEY_F3 },
{ "f4", KEY_F4 },
{ "f5", KEY_F5 },
{ "f6", KEY_F6 },
{ "f7", KEY_F7 },
{ "f8", KEY_F8 },
{ "f9", KEY_F9 },
{ "f10", KEY_F10 },
{ "f11", KEY_F11 },
{ "f12", KEY_F12 },
{ "num0", KEY_0 },
{ "num1", KEY_1 },
{ "num2", KEY_2 },
{ "num3", KEY_3 },
{ "num4", KEY_4 },
{ "num5", KEY_5 },
{ "num6", KEY_6 },
{ "num7", KEY_7 },
{ "num8", KEY_8 },
{ "num9", KEY_9 },
{ "insert", KEY_Insert },
{ "del", KEY_DeleteForward },
{ "home", KEY_Home },
{ "end", KEY_End },
{ "pageup", KEY_PageUp },
{ "pagedown", KEY_PageDown },
{ "add", KP_Add },
{ "subtract", KP_Subtract },
{ "multiply", KP_Multiply },
{ "divide", KP_Divide },
{ "keypad0", KP_0 },
{ "keypad1", KP_1 },
{ "keypad2", KP_2 },
{ "keypad3", KP_3 },
{ "keypad4", KP_4 },
{ "keypad5", KP_5 },
{ "keypad6", KP_6 },
{ "keypad7", KP_7 },
{ "keypad8", KP_8 },
{ "keypad9", KP_9 },
{ "period", KEY_Period },
{ "capslock", KEY_CapsLock },
{ "numlock", KP_NumLock },
{ "print_screen", KEY_PrintScreen },
{ "scroll_lock", KEY_ScrollLock },
2014-10-03 14:01:29 +00:00
{ "a", KEY_A },
{ "b", KEY_B },
{ "c", KEY_C },
{ "d", KEY_D },
{ "e", KEY_E },
{ "f", KEY_F },
{ "g", KEY_G },
{ "h", KEY_H },
{ "i", KEY_I },
{ "j", KEY_J },
{ "k", KEY_K },
{ "l", KEY_L },
{ "m", KEY_M },
{ "n", KEY_N },
{ "o", KEY_O },
{ "p", KEY_P },
{ "q", KEY_Q },
{ "r", KEY_R },
{ "s", KEY_S },
{ "t", KEY_T },
{ "u", KEY_U },
{ "v", KEY_V },
{ "w", KEY_W },
{ "x", KEY_X },
{ "y", KEY_Y },
{ "z", KEY_Z },
{ "nul", 0x00},
};
int32_t cocoa_input_find_any_key(void)
{
unsigned i;
2015-03-18 19:15:40 +00:00
driver_t *driver =driver_get_ptr();
cocoa_input_data_t *apple = (cocoa_input_data_t*)driver->input_data;
if (!apple)
return 0;
2014-10-04 14:34:52 +00:00
if (apple->joypad)
apple->joypad->poll();
for (i = 0; apple_key_name_map[i].hid_id; i++)
if (apple->key_state[apple_key_name_map[i].hid_id])
return apple_key_name_map[i].hid_id;
return 0;
}
2015-09-16 01:40:16 +00:00
static int cocoa_input_find_any_button_ret(cocoa_input_data_t *apple,
unsigned buttons, unsigned port)
2015-09-16 01:29:38 +00:00
{
unsigned i;
if (buttons)
for (i = 0; i < 32; i++)
if (buttons & (1 << i))
return i;
return -1;
}
int32_t cocoa_input_find_any_button(uint32_t port)
{
2015-09-16 01:29:38 +00:00
int ret;
2015-03-18 19:15:40 +00:00
driver_t *driver = driver_get_ptr();
cocoa_input_data_t *apple = (cocoa_input_data_t*)driver->input_data;
2014-09-30 14:34:40 +00:00
if (!apple)
return -1;
2014-10-04 14:34:52 +00:00
if (apple->joypad)
apple->joypad->poll();
2015-09-16 01:40:16 +00:00
ret = cocoa_input_find_any_button_ret(apple, apple->buttons[port], port);
2015-09-16 01:29:38 +00:00
if (ret != -1)
return ret;
#ifdef HAVE_MFI
2015-09-16 01:40:16 +00:00
ret = cocoa_input_find_any_button_ret(apple, apple->mfi_buttons[port], port);
2015-09-16 01:29:38 +00:00
if (ret != -1)
return ret;
#endif
return -1;
}
int32_t cocoa_input_find_any_axis(uint32_t port)
{
int i;
2015-03-18 19:15:40 +00:00
driver_t *driver = driver_get_ptr();
cocoa_input_data_t *apple = (cocoa_input_data_t*)driver->input_data;
2014-10-04 14:34:52 +00:00
if (apple && apple->joypad)
apple->joypad->poll();
for (i = 0; i < 6; i++)
{
int16_t value = apple->axes[port][i];
if (abs(value) > 0x4000)
return (value < 0) ? -(i + 1) : i + 1;
}
return 0;
}
static int16_t cocoa_input_is_pressed(cocoa_input_data_t *apple, unsigned port_num,
2014-09-30 14:34:40 +00:00
const struct retro_keybind *binds, unsigned id)
{
2014-09-30 14:34:40 +00:00
if (id < RARCH_BIND_LIST_END)
{
const struct retro_keybind *bind = &binds[id];
unsigned bit = input_keymaps_translate_rk_to_keysym(bind->key);
return bind->valid && apple->key_state[bit];
2014-09-30 14:34:40 +00:00
}
return 0;
}
static void *cocoa_input_init(void)
{
2015-03-20 21:32:09 +00:00
settings_t *settings = config_get_ptr();
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);
apple->joypad = input_joypad_init_driver(settings->input.joypad_driver, apple);
return apple;
}
static void cocoa_input_poll(void *data)
{
uint32_t i;
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
for (i = 0; i < apple->touch_count; i++)
2015-02-11 01:16:33 +00:00
input_translate_coord_viewport(
apple->touches[i].screen_x,
apple->touches[i].screen_y,
&apple->touches[i].fixed_x,
&apple->touches[i].fixed_y,
&apple->touches[i].full_x,
&apple->touches[i].full_y);
if (apple->joypad)
apple->joypad->poll();
apple->mouse_x_last = apple->mouse_rel_x;
apple->mouse_y_last = apple->mouse_rel_y;
}
static int16_t cocoa_mouse_state(cocoa_input_data_t *apple,
2014-09-30 14:34:40 +00:00
unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return apple->mouse_rel_x - apple->mouse_x_last;
2014-09-30 14:34:40 +00:00
case RETRO_DEVICE_ID_MOUSE_Y:
return apple->mouse_rel_y - apple->mouse_y_last;
2014-09-30 14:34:40 +00:00
case RETRO_DEVICE_ID_MOUSE_LEFT:
return apple->mouse_buttons & 1;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
return apple->mouse_buttons & 2;
2015-03-10 23:35:26 +00:00
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
return apple->mouse_wu;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
return apple->mouse_wd;
2014-09-30 14:34:40 +00:00
}
return 0;
}
static int16_t cocoa_mouse_state_screen(cocoa_input_data_t *apple,
unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return apple->window_pos_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return apple->window_pos_y;
default:
break;
}
return cocoa_mouse_state(apple, id);
}
static int16_t cocoa_pointer_state(cocoa_input_data_t *apple,
2014-10-20 18:31:00 +00:00
unsigned device, unsigned idx, unsigned id)
2014-09-30 14:34:40 +00:00
{
2014-09-30 14:35:49 +00:00
const bool want_full = (device == RARCH_DEVICE_POINTER_SCREEN);
2014-09-30 14:34:40 +00:00
2014-10-20 18:31:00 +00:00
if (idx < apple->touch_count && (idx < MAX_TOUCHES))
2014-09-30 14:34:40 +00:00
{
int16_t x, y;
const cocoa_touch_data_t *touch = (const cocoa_touch_data_t *)
2014-10-20 18:31:00 +00:00
&apple->touches[idx];
if (!touch)
return 0;
x = touch->fixed_x;
y = touch->fixed_y;
2015-02-11 01:16:33 +00:00
if (want_full)
{
x = touch->full_x;
y = touch->full_y;
}
2014-09-30 14:34:40 +00:00
switch (id)
{
case RETRO_DEVICE_ID_POINTER_PRESSED:
return (x != -0x8000) && (y != -0x8000);
case RETRO_DEVICE_ID_POINTER_X:
return x;
case RETRO_DEVICE_ID_POINTER_Y:
return y;
}
}
return 0;
}
static int16_t cocoa_keyboard_state(cocoa_input_data_t *apple, unsigned id)
2015-02-11 01:16:33 +00:00
{
unsigned bit = input_keymaps_translate_rk_to_keysym((enum retro_key)id);
return (id < RETROK_LAST) && apple->key_state[bit];
}
static int16_t cocoa_input_state(void *data,
2014-09-30 14:34:40 +00:00
const struct retro_keybind **binds, unsigned port,
2014-10-20 18:31:00 +00:00
unsigned device, unsigned idx, unsigned id)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
if (!apple || !apple->joypad)
return 0;
switch (device)
{
case RETRO_DEVICE_JOYPAD:
return cocoa_input_is_pressed(apple, port, binds[port], id) ||
input_joypad_pressed(apple->joypad, port, binds[port], id);
case RETRO_DEVICE_ANALOG:
2014-09-30 14:34:40 +00:00
return input_joypad_analog(apple->joypad, port,
2014-10-20 18:31:00 +00:00
idx, id, binds[port]);
2013-03-31 15:59:37 +00:00
case RETRO_DEVICE_KEYBOARD:
return cocoa_keyboard_state(apple, id);
case RETRO_DEVICE_MOUSE:
return cocoa_mouse_state(apple, id);
case RARCH_DEVICE_MOUSE_SCREEN:
return cocoa_mouse_state_screen(apple, id);
case RETRO_DEVICE_POINTER:
case RARCH_DEVICE_POINTER_SCREEN:
return cocoa_pointer_state(apple, device, idx, id);
}
2014-09-30 14:34:40 +00:00
return 0;
}
2015-11-07 19:59:12 +00:00
static bool cocoa_input_key_pressed(void *data, int key)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
settings_t *settings = config_get_ptr();
2015-10-23 06:34:15 +00:00
if (cocoa_input_is_pressed(apple, 0, settings->input.binds[0], key))
return true;
if (input_joypad_pressed(apple->joypad, 0, settings->input.binds[0], key))
return true;
2015-10-23 06:34:15 +00:00
return false;
}
2015-11-07 19:59:12 +00:00
static bool cocoa_input_meta_key_pressed(void *data, int key)
2015-07-17 01:31:51 +00:00
{
return false;
}
static void cocoa_input_free(void *data)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
2014-10-04 13:14:39 +00:00
if (!apple || !data)
return;
2014-05-13 11:49:29 +00:00
2014-10-04 13:14:39 +00:00
if (apple->joypad)
apple->joypad->destroy();
free(apple);
}
static bool cocoa_input_set_rumble(void *data,
unsigned port, enum retro_rumble_effect effect, uint16_t strength)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
if (apple && apple->joypad)
2014-09-30 14:34:40 +00:00
return input_joypad_set_rumble(apple->joypad,
port, effect, strength);
return false;
}
static uint64_t cocoa_input_get_capabilities(void *data)
{
(void)data;
2014-10-04 13:14:39 +00:00
return
(1 << RETRO_DEVICE_JOYPAD) |
(1 << RETRO_DEVICE_MOUSE) |
(1 << RETRO_DEVICE_KEYBOARD) |
(1 << RETRO_DEVICE_POINTER) |
(1 << RETRO_DEVICE_ANALOG);
}
static void cocoa_input_grab_mouse(void *data, bool state)
2014-10-04 13:14:39 +00:00
{
/* Dummy for now. Might be useful in the future. */
(void)data;
(void)state;
}
static const input_device_driver_t *cocoa_input_get_joypad_driver(void *data)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
if (apple && apple->joypad)
return apple->joypad;
return NULL;
}
static bool cocoa_input_keyboard_mapping_is_blocked(void *data)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
if (!apple)
return false;
return apple->blocked;
}
static void cocoa_input_keyboard_mapping_set_block(void *data, bool value)
{
cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
if (!apple)
return;
apple->blocked = value;
}
input_driver_t input_cocoa = {
cocoa_input_init,
cocoa_input_poll,
cocoa_input_state,
cocoa_input_key_pressed,
2015-07-17 01:31:51 +00:00
cocoa_input_meta_key_pressed,
cocoa_input_free,
NULL,
NULL,
cocoa_input_get_capabilities,
"cocoa",
cocoa_input_grab_mouse,
NULL,
cocoa_input_set_rumble,
2015-06-19 01:46:54 +00:00
cocoa_input_get_joypad_driver,
cocoa_input_keyboard_mapping_is_blocked,
cocoa_input_keyboard_mapping_set_block,
};