/* * (C) GraÅžvydas "notaz" Ignotas, 2006-2012 * (C) kub 2020 * * This work is licensed under the terms of any of these licenses * (at your option): * - GNU GPL, version 2 or later. * - GNU LGPL, version 2.1 or later. * - MAME license. * See the COPYING file in the top-level directory. */ #include #include #include #include #include #include #include #include "../libpicofe/input.h" #include "psp.h" #include "in_psp.h" #define IN_PSP_PREFIX "psp:" #define IN_PSP_NBUTTONS 32 /* note: in_psp handles combos (if 2 btns have the same bind, * both must be pressed for action to happen) */ static int in_psp_combo_keys = 0; static int in_psp_combo_acts = 0; static const char *in_psp_keys[IN_PSP_NBUTTONS] = { [0 ... IN_PSP_NBUTTONS-1] = NULL, }; /* credits to https://graphics.stanford.edu/~seander/bithacks.html */ static int lg2(unsigned v) { int r, s; r = (v > 0xFFFF) << 4; v >>= r; s = (v > 0xFF ) << 3; v >>= s; r |= s; s = (v > 0xF ) << 2; v >>= s; r |= s; s = (v > 0x3 ) << 1; v >>= s; r |= s; r |= (v >> 1); return r; } static int in_psp_get_bits(void) { return psp_pad_read(0); } static void in_psp_probe(const in_drv_t *drv) { in_register(IN_PSP_PREFIX "PSP pad", -1, NULL, IN_PSP_NBUTTONS, in_psp_keys, 1); } static void in_psp_free(void *drv_data) { } static const char * const * in_psp_get_key_names(const in_drv_t *drv, int *count) { *count = IN_PSP_NBUTTONS; return in_psp_keys; } /* ORs result with pressed buttons */ static int in_psp_update(void *drv_data, const int *binds, int *result) { int type_start = 0; int i, t, keys; keys = in_psp_get_bits(); if (keys & in_psp_combo_keys) { result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, IN_PSP_NBUTTONS, in_psp_combo_keys, in_psp_combo_acts); type_start = IN_BINDTYPE_PLAYER12; } for (i = 0; keys; i++, keys >>= 1) { if (!(keys & 1)) continue; for (t = type_start; t < IN_BINDTYPE_COUNT; t++) result[t] |= binds[IN_BIND_OFFS(i, t)]; } return 0; } int in_psp_update_keycode(void *data, int *is_down) { static int old_val = 0; int val, diff, i; val = in_psp_get_bits(); diff = val ^ old_val; if (diff == 0) return -1; /* take one bit only */ for (i = 0; i < sizeof(diff)*8; i++) if (diff & (1< kc */ keycode = -keycode; for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) if (key_pbtn_map[i].pbtn == keycode) return lg2(key_pbtn_map[i].key); } else { for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) if (key_pbtn_map[i].key == 1<