2013-09-11 00:21:48 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2015-01-07 16:46:50 +00:00
|
|
|
* Copyright (C) 2010-2015 - Michael Lelli
|
2013-09-11 00:21:48 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-01-12 05:16:52 +00:00
|
|
|
#include "../input_autodetect.h"
|
|
|
|
#include "../input_common.h"
|
|
|
|
#include "../input_keymaps.h"
|
2013-09-11 00:21:48 +00:00
|
|
|
|
2015-01-12 05:16:52 +00:00
|
|
|
#include "../../driver.h"
|
2013-09-11 00:21:48 +00:00
|
|
|
|
2015-04-19 20:01:42 +00:00
|
|
|
#include <stdlib.h>
|
2014-10-21 03:05:52 +00:00
|
|
|
#include <boolean.h>
|
2015-01-12 05:16:52 +00:00
|
|
|
#include "../../general.h"
|
|
|
|
#include "../keyboard_line.h"
|
2015-04-14 20:01:41 +00:00
|
|
|
#include "../input_joypad.h"
|
2013-09-11 00:21:48 +00:00
|
|
|
|
2015-04-19 20:01:42 +00:00
|
|
|
typedef struct rwebinput_state
|
|
|
|
{
|
|
|
|
uint8_t keys[32];
|
|
|
|
int mouse_x;
|
|
|
|
int mouse_y;
|
|
|
|
char mouse_l;
|
|
|
|
char mouse_r;
|
|
|
|
} rwebinput_state_t;
|
|
|
|
|
|
|
|
int RWebInputInit(void);
|
|
|
|
rwebinput_state_t *RWebInputPoll(int context);
|
|
|
|
void RWebInputDestroy(int context);
|
|
|
|
|
2013-09-11 00:21:48 +00:00
|
|
|
static bool uninited = false;
|
|
|
|
|
|
|
|
typedef struct rwebinput_input
|
|
|
|
{
|
|
|
|
rwebinput_state_t state;
|
|
|
|
int context;
|
|
|
|
} rwebinput_input_t;
|
|
|
|
|
|
|
|
static void *rwebinput_input_init(void)
|
|
|
|
{
|
|
|
|
rwebinput_input_t *rwebinput = (rwebinput_input_t*)calloc(1, sizeof(*rwebinput));
|
|
|
|
if (!rwebinput)
|
2015-04-19 20:03:51 +00:00
|
|
|
goto error;
|
2013-09-11 00:21:48 +00:00
|
|
|
|
|
|
|
rwebinput->context = RWebInputInit();
|
|
|
|
if (!rwebinput->context)
|
2015-04-19 20:03:51 +00:00
|
|
|
goto error;
|
2013-09-11 00:21:48 +00:00
|
|
|
|
2015-01-12 01:52:52 +00:00
|
|
|
input_keymaps_init_keyboard_lut(rarch_key_map_rwebinput);
|
2013-09-11 00:21:48 +00:00
|
|
|
|
|
|
|
return rwebinput;
|
2015-04-19 20:03:51 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (rwebinput)
|
|
|
|
free(rwebinput);
|
|
|
|
return NULL;
|
2013-09-11 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool rwebinput_key_pressed(rwebinput_input_t *rwebinput, int key)
|
|
|
|
{
|
2015-01-12 01:52:52 +00:00
|
|
|
unsigned sym;
|
|
|
|
bool ret;
|
|
|
|
|
2013-09-11 00:21:48 +00:00
|
|
|
if (key >= RETROK_LAST)
|
|
|
|
return false;
|
|
|
|
|
2015-01-12 01:52:52 +00:00
|
|
|
sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key);
|
|
|
|
ret = rwebinput->state.keys[sym >> 3] & (1 << (sym & 7));
|
|
|
|
|
2013-09-11 00:21:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool rwebinput_is_pressed(rwebinput_input_t *rwebinput, const struct retro_keybind *binds, unsigned id)
|
|
|
|
{
|
|
|
|
if (id < RARCH_BIND_LIST_END)
|
|
|
|
{
|
|
|
|
const struct retro_keybind *bind = &binds[id];
|
|
|
|
return bind->valid && rwebinput_key_pressed(rwebinput, binds[id].key);
|
|
|
|
}
|
2015-01-12 01:52:52 +00:00
|
|
|
|
|
|
|
return false;
|
2013-09-11 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool rwebinput_bind_button_pressed(void *data, int key)
|
|
|
|
{
|
|
|
|
rwebinput_input_t *rwebinput = (rwebinput_input_t*)data;
|
2015-03-20 21:32:09 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
return rwebinput_is_pressed(rwebinput, settings->input.binds[0], key);
|
2013-09-11 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int16_t rwebinput_mouse_state(rwebinput_input_t *rwebinput, unsigned id)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case RETRO_DEVICE_ID_MOUSE_X:
|
|
|
|
return (int16_t) rwebinput->state.mouse_x;
|
|
|
|
case RETRO_DEVICE_ID_MOUSE_Y:
|
|
|
|
return (int16_t) rwebinput->state.mouse_y;
|
|
|
|
case RETRO_DEVICE_ID_MOUSE_LEFT:
|
|
|
|
return rwebinput->state.mouse_l;
|
|
|
|
case RETRO_DEVICE_ID_MOUSE_RIGHT:
|
|
|
|
return rwebinput->state.mouse_r;
|
|
|
|
}
|
2015-01-12 01:52:52 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-09-11 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 18:31:00 +00:00
|
|
|
static int16_t rwebinput_analog_pressed(rwebinput_input_t *rwebinput,
|
|
|
|
const struct retro_keybind *binds, unsigned idx, unsigned id)
|
2014-07-07 00:02:44 +00:00
|
|
|
{
|
2015-01-12 01:52:52 +00:00
|
|
|
int16_t pressed_minus = 0, pressed_plus = 0;
|
2014-07-07 00:02:44 +00:00
|
|
|
unsigned id_minus = 0;
|
|
|
|
unsigned id_plus = 0;
|
2015-01-12 01:52:52 +00:00
|
|
|
|
2014-10-20 18:31:00 +00:00
|
|
|
input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus);
|
2014-07-07 00:02:44 +00:00
|
|
|
|
2015-01-12 01:52:52 +00:00
|
|
|
if (rwebinput_is_pressed(rwebinput, binds, id_minus))
|
|
|
|
pressed_minus = -0x7fff;
|
|
|
|
if (rwebinput_is_pressed(rwebinput, binds, id_plus))
|
|
|
|
pressed_plus = 0x7fff;
|
|
|
|
|
2014-07-07 00:02:44 +00:00
|
|
|
return pressed_plus + pressed_minus;
|
|
|
|
}
|
|
|
|
|
2014-10-20 18:31:00 +00:00
|
|
|
static int16_t rwebinput_input_state(void *data, const struct retro_keybind **binds,
|
|
|
|
unsigned port, unsigned device, unsigned idx, unsigned id)
|
2013-09-11 00:21:48 +00:00
|
|
|
{
|
|
|
|
rwebinput_input_t *rwebinput = (rwebinput_input_t*)data;
|
|
|
|
|
|
|
|
switch (device)
|
|
|
|
{
|
|
|
|
case RETRO_DEVICE_JOYPAD:
|
|
|
|
return rwebinput_is_pressed(rwebinput, binds[port], id);
|
|
|
|
|
2014-07-07 00:02:44 +00:00
|
|
|
case RETRO_DEVICE_ANALOG:
|
2014-10-20 18:31:00 +00:00
|
|
|
return rwebinput_analog_pressed(rwebinput, binds[port], idx, id);
|
2014-07-07 00:02:44 +00:00
|
|
|
|
2013-09-11 00:21:48 +00:00
|
|
|
case RETRO_DEVICE_KEYBOARD:
|
|
|
|
return rwebinput_key_pressed(rwebinput, id);
|
|
|
|
|
|
|
|
case RETRO_DEVICE_MOUSE:
|
|
|
|
return rwebinput_mouse_state(rwebinput, id);
|
|
|
|
}
|
2015-01-12 01:52:52 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-09-11 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rwebinput_input_free(void *data)
|
|
|
|
{
|
|
|
|
rwebinput_input_t *rwebinput = (rwebinput_input_t*)data;
|
|
|
|
uninited = true;
|
|
|
|
|
2014-09-15 12:17:16 +00:00
|
|
|
if (!rwebinput)
|
|
|
|
return;
|
|
|
|
|
2013-09-11 00:21:48 +00:00
|
|
|
RWebInputDestroy(rwebinput->context);
|
|
|
|
|
2014-09-15 12:17:16 +00:00
|
|
|
free(rwebinput);
|
2013-09-11 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rwebinput_input_poll(void *data)
|
|
|
|
{
|
2015-01-12 01:52:52 +00:00
|
|
|
unsigned i;
|
2013-09-11 00:21:48 +00:00
|
|
|
rwebinput_input_t *rwebinput = (rwebinput_input_t*)data;
|
2015-01-12 01:52:52 +00:00
|
|
|
rwebinput_state_t *state = RWebInputPoll(rwebinput->context);
|
2013-09-11 00:21:48 +00:00
|
|
|
|
2015-01-12 01:52:52 +00:00
|
|
|
/* Get new keys. */
|
|
|
|
for (i = 0; i < 32; i++)
|
2014-07-07 00:02:44 +00:00
|
|
|
{
|
2015-01-12 01:52:52 +00:00
|
|
|
unsigned k;
|
|
|
|
uint8_t diff;
|
|
|
|
|
|
|
|
if (state->keys[i] == rwebinput->state.keys[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
diff = state->keys[i] ^ rwebinput->state.keys[i];
|
|
|
|
|
|
|
|
for (k = 0; diff; diff >>= 1, k++)
|
2014-07-07 00:02:44 +00:00
|
|
|
{
|
2015-01-12 01:52:52 +00:00
|
|
|
if (diff & 1)
|
2015-01-29 06:03:20 +00:00
|
|
|
input_keyboard_event((state->keys[i] & (1 << k)) != 0,
|
2015-01-29 22:59:46 +00:00
|
|
|
input_keymaps_translate_keysym_to_rk(i * 8 + k), 0, 0,
|
|
|
|
RETRO_DEVICE_KEYBOARD);
|
2014-07-07 00:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:21:48 +00:00
|
|
|
memcpy(&rwebinput->state, state, sizeof(rwebinput->state));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rwebinput_grab_mouse(void *data, bool state)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)state;
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:51:50 +00:00
|
|
|
static bool rwebinput_set_rumble(void *data, unsigned port,
|
|
|
|
enum retro_rumble_effect effect, uint16_t strength)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)port;
|
|
|
|
(void)effect;
|
|
|
|
(void)strength;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-02 20:16:57 +00:00
|
|
|
static uint64_t rwebinput_get_capabilities(void *data)
|
|
|
|
{
|
|
|
|
uint64_t caps = 0;
|
|
|
|
|
|
|
|
caps |= (1 << RETRO_DEVICE_JOYPAD);
|
2014-07-07 00:02:44 +00:00
|
|
|
caps |= (1 << RETRO_DEVICE_ANALOG);
|
2013-11-02 20:16:57 +00:00
|
|
|
caps |= (1 << RETRO_DEVICE_KEYBOARD);
|
|
|
|
caps |= (1 << RETRO_DEVICE_MOUSE);
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2014-09-11 05:06:20 +00:00
|
|
|
input_driver_t input_rwebinput = {
|
2013-09-11 00:21:48 +00:00
|
|
|
rwebinput_input_init,
|
|
|
|
rwebinput_input_poll,
|
|
|
|
rwebinput_input_state,
|
|
|
|
rwebinput_bind_button_pressed,
|
|
|
|
rwebinput_input_free,
|
|
|
|
NULL,
|
2013-11-02 23:27:58 +00:00
|
|
|
NULL,
|
2013-11-02 20:16:57 +00:00
|
|
|
rwebinput_get_capabilities,
|
2013-09-11 00:21:48 +00:00
|
|
|
"rwebinput",
|
|
|
|
rwebinput_grab_mouse,
|
2015-03-24 06:51:50 +00:00
|
|
|
rwebinput_set_rumble,
|
2013-09-11 00:21:48 +00:00
|
|
|
};
|