From 824f00be15287993e983dd28b9fcdf68347d4f8e Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 19 Jan 2015 05:01:31 +0100 Subject: [PATCH] (input/connect) Cleanups --- input/connect/connect_ps3.c | 40 ++++--- input/connect/connect_wii.c | 67 +++++------ input/connect/joypad_connection.c | 87 +++++++------- input/drivers_joypad/apple_joypad_hid.c | 143 ++++++++++++------------ input/drivers_joypad/apple_joypad_ios.c | 10 +- 5 files changed, 182 insertions(+), 165 deletions(-) diff --git a/input/connect/connect_ps3.c b/input/connect/connect_ps3.c index 2357ae951d..d7c076d608 100644 --- a/input/connect/connect_ps3.c +++ b/input/connect/connect_ps3.c @@ -57,6 +57,10 @@ static void hidpad_ps3_send_control(struct hidpad_ps3_data* device) static void* hidpad_ps3_init(void *data, uint32_t slot, send_control_t ptr) { +#ifdef IOS + /* Magic packet to start reports. */ + static uint8_t magic_data[] = {0x53, 0xF4, 0x42, 0x03, 0x00, 0x00}; +#endif struct pad_connection* connection = (struct pad_connection*)data; struct hidpad_ps3_data* device = (struct hidpad_ps3_data*) calloc(1, sizeof(struct hidpad_ps3_data)); @@ -75,8 +79,6 @@ static void* hidpad_ps3_init(void *data, uint32_t slot, send_control_t ptr) device->send_control = ptr; #ifdef IOS - /* Magic packet to start reports. */ - static uint8_t magic_data[] = {0x53, 0xF4, 0x42, 0x03, 0x00, 0x00}; device->send_control(device->connection, magic_data, 6); #endif @@ -97,23 +99,23 @@ static void hidpad_ps3_deinit(void *data) static uint32_t hidpad_ps3_get_buttons(void *data) { struct hidpad_ps3_data *device = (struct hidpad_ps3_data*)data; - if (device) - return device->buttons; - return 0; + if (!device) + return 0; + return device->buttons; } static int16_t hidpad_ps3_get_axis(void *data, unsigned axis) { + int val; struct hidpad_ps3_data *device = (struct hidpad_ps3_data*)data; - - if (device && (axis < 4)) - { - int val = device->data[7 + axis]; - val = (val << 8) - 0x8000; - return (abs(val) > 0x1000) ? val : 0; - } - return 0; + if (!device || axis >= 4) + return 0; + + val = device->data[7 + axis]; + val = (val << 8) - 0x8000; + + return (abs(val) > 0x1000) ? val : 0; } static void hidpad_ps3_packet_handler(void *data, uint8_t *packet, uint16_t size) @@ -168,11 +170,13 @@ static void hidpad_ps3_set_rumble(void *data, struct hidpad_ps3_data *device = (struct hidpad_ps3_data*)data; unsigned idx = (effect == RETRO_RUMBLE_STRONG) ? 0 : 1; - if (device && (device->motors[idx] != strength)) - { - device->motors[idx] = strength; - hidpad_ps3_send_control(device); - } + if (!device) + return; + if ((device->motors[idx] == strength)) + return; + + device->motors[idx] = strength; + hidpad_ps3_send_control(device); } pad_connection_interface_t pad_connection_ps3 = { diff --git a/input/connect/connect_wii.c b/input/connect/connect_wii.c index 612011ae3f..40281b404e 100644 --- a/input/connect/connect_wii.c +++ b/input/connect/connect_wii.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "connect_wii.h" #include "joypad_connection.h" @@ -29,8 +30,9 @@ */ static int wiimote_send(struct wiimote_t* wm, - byte report_type, byte* msg, int len) + byte report_type, byte* msg, int len) { + int x = 2; byte buf[32]; buf[0] = WM_SET_REPORT | WM_BT_OUTPUT; @@ -39,7 +41,6 @@ static int wiimote_send(struct wiimote_t* wm, memcpy(buf+2, msg, len); #ifdef WIIMOTE_DBG - int x = 2; printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]); for (; x < len+2; ++x) printf("%.2x ", buf[x]); @@ -55,7 +56,6 @@ static int wiimote_send(struct wiimote_t* wm, * * Controller status includes: battery level, LED status, expansions. */ - static void wiimote_status(struct wiimote_t* wm) { byte buf = 0; @@ -122,7 +122,7 @@ static void wiimote_pressed_buttons(struct wiimote_t* wm, byte* msg) } static int classic_ctrl_handshake(struct wiimote_t* wm, - struct classic_ctrl_t* cc, byte* data, unsigned short len) + struct classic_ctrl_t* cc, byte* data, unsigned short len) { memset(cc, 0, sizeof(*cc)); wm->exp.type = EXP_CLASSIC; @@ -131,7 +131,9 @@ static int classic_ctrl_handshake(struct wiimote_t* wm, static float normalize_and_interpolate(float min, float max, float t) { - return (min == max) ? 0.0f : (t - min) / (max - min); + if (min == max) + return 0.0f; + return (t - min) / (max - min); } static void process_axis(struct axis_t* axis, byte raw) @@ -192,6 +194,7 @@ static void wiimote_handle_expansion(struct wiimote_t* wm, byte* msg) static int wiimote_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte len) { + int i = 0; byte buf[21] = {0}; /* the payload is always 23 */ if (!wm || !WIIMOTE_IS_CONNECTED(wm)) @@ -201,8 +204,6 @@ static int wiimote_write_data(struct wiimote_t* wm, #ifdef WIIMOTE_DBG printf("Writing %i bytes to memory location 0x%x...\n", len, addr); - - int i = 0; printf("Write data is: "); for (; i < len; ++i) printf("%x ", data[i]); @@ -244,7 +245,7 @@ static int wiimote_read_data(struct wiimote_t* wm, unsigned int addr, return 0; /* the offset is in big endian */ - *(int*)(buf) = BIG_ENDIAN_LONG(addr); + *(int*)(buf) = BIG_ENDIAN_LONG(addr); /* the length is in big endian */ *(short*)(buf + 4) = BIG_ENDIAN_SHORT(len); @@ -342,12 +343,12 @@ static int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data, * 0x00 to 0x(4)A400FB. (support clones) */ buf = 0x55; wiimote_write_data(wm, 0x04A400F0, &buf, 1); - usleep(100000); + retro_sleep(100000); buf = 0x00; wiimote_write_data(wm, 0x04A400FB, &buf, 1); /* check extension type! */ - usleep(100000); + retro_sleep(100000); wiimote_read_data(wm, WM_EXP_MEM_CALIBR+220, 4); #if 0 wiimote_read_data(wm, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); @@ -399,32 +400,34 @@ static int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data, wm->handshake_state = 6; continue; case 4: - if(event != WM_RPT_READ) - return 0; + { + int id; + if(event != WM_RPT_READ) + return 0; - int id = BIG_ENDIAN_LONG(*(int*)(data)); + id = BIG_ENDIAN_LONG(*(int*)(data)); #ifdef WIIMOTE_DBG - printf("Expansion id=0x%04x\n",id); + printf("Expansion id=0x%04x\n",id); #endif - /* EXP_ID_CODE_CLASSIC_CONTROLLER */ + /* EXP_ID_CODE_CLASSIC_CONTROLLER */ - if(id != 0xa4200101) - { - wm->handshake_state = 2; + if(id != 0xa4200101) + { + wm->handshake_state = 2; #if 0 - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); #endif - continue; + continue; + } + else + { + retro_sleep(100000); + /* pedimos datos de calibracion del JOY! */ + wiimote_read_data(wm, WM_EXP_MEM_CALIBR, 16); + wm->handshake_state = 5; + } } - else - { - usleep(100000); - /* pedimos datos de calibracion del JOY! */ - wiimote_read_data(wm, WM_EXP_MEM_CALIBR, 16); - wm->handshake_state = 5; - } - return 0; case 5: if(event != WM_RPT_READ) @@ -458,7 +461,7 @@ static int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data, #endif static void* hidpad_wii_init(void *data, uint32_t slot, - send_control_t ptr) + send_control_t ptr) { struct pad_connection *connection = (struct pad_connection*)data; struct wiimote_t *device = (struct wiimote_t*) @@ -517,9 +520,9 @@ static int16_t hidpad_wii_get_axis(void *data, unsigned axis) static uint32_t hidpad_wii_get_buttons(void *data) { struct wiimote_t* device = (struct wiimote_t*)data; - if (device) - return device->btns | (device->exp.cc.classic.btns << 16); - return 0; + if (!device) + return 0; + return device->btns | (device->exp.cc.classic.btns << 16); } static void hidpad_wii_packet_handler(void *data, diff --git a/input/connect/joypad_connection.c b/input/connect/joypad_connection.c index be5202a823..cefced9d38 100644 --- a/input/connect/joypad_connection.c +++ b/input/connect/joypad_connection.c @@ -26,27 +26,30 @@ static int find_vacant_pad(joypad_connection_t *joyconn) if (conn && !conn->used) return i; } + return -1; } void *pad_connection_init(unsigned pads) { - int i; - joypad_connection_t *joyconn = (joypad_connection_t*) - calloc(pads, sizeof(*joyconn)); - if (!joyconn) - return NULL; - - for (i = 0; i < pads; i++) - { - joypad_connection_t *conn = (joypad_connection_t*)&joyconn[i]; - conn->used = false; - conn->iface = NULL; - conn->is_gcapi = false; - conn->data = NULL; - } - - return joyconn; + int i; + joypad_connection_t *joyconn = (joypad_connection_t*) + calloc(pads, sizeof(*joyconn)); + + if (!joyconn) + return NULL; + + for (i = 0; i < pads; i++) + { + joypad_connection_t *conn = (joypad_connection_t*)&joyconn[i]; + + conn->used = false; + conn->iface = NULL; + conn->is_gcapi = false; + conn->data = NULL; + } + + return joyconn; } int32_t pad_connection_pad_init(joypad_connection_t *joyconn, @@ -97,8 +100,8 @@ int32_t apple_joypad_connect_gcapi(joypad_connection_t *joyconn) if (s) { - s->used = true; - s->is_gcapi = true; + s->used = true; + s->is_gcapi = true; } } @@ -110,17 +113,17 @@ void pad_connection_pad_deinit(joypad_connection_t *s, uint32_t pad) if (!s || !s->used) return; - if (s->iface) - { - s->iface->set_rumble(s->data, RETRO_RUMBLE_STRONG, 0); - s->iface->set_rumble(s->data, RETRO_RUMBLE_WEAK, 0); - if (s->iface->deinit) - s->iface->deinit(s->data); - } - - s->iface = NULL; - s->used = false; - s->is_gcapi = false; + if (s->iface) + { + s->iface->set_rumble(s->data, RETRO_RUMBLE_STRONG, 0); + s->iface->set_rumble(s->data, RETRO_RUMBLE_WEAK, 0); + if (s->iface->deinit) + s->iface->deinit(s->data); + } + + s->iface = NULL; + s->used = false; + s->is_gcapi = false; } void pad_connection_packet(joypad_connection_t *s, uint32_t pad, @@ -135,17 +138,17 @@ void pad_connection_packet(joypad_connection_t *s, uint32_t pad, uint32_t pad_connection_get_buttons(joypad_connection_t *s, unsigned pad) { - if (s->iface) - return s->iface->get_buttons(s->data); - return 0; + if (!s->iface) + return 0; + return s->iface->get_buttons(s->data); } int16_t pad_connection_get_axis(joypad_connection_t *s, unsigned idx, unsigned i) { - if (s->iface) - return s->iface->get_axis(s->data, i); - return 0; + if (!s->iface) + return 0; + return s->iface->get_axis(s->data, i); } bool pad_connection_has_interface(joypad_connection_t *s, unsigned pad) @@ -169,11 +172,13 @@ void pad_connection_destroy(joypad_connection_t *joyconn) bool pad_connection_rumble(joypad_connection_t *s, unsigned pad, enum retro_rumble_effect effect, uint16_t strength) { - if (s->used && s->iface && s->iface->set_rumble) - { - s->iface->set_rumble(s->data, effect, strength); - return true; - } + if (!s->used) + return false; + if (!s->iface) + return false; + if (!s->iface->set_rumble) + return; - return false; + s->iface->set_rumble(s->data, effect, strength); + return true; } diff --git a/input/drivers_joypad/apple_joypad_hid.c b/input/drivers_joypad/apple_joypad_hid.c index dce1ab63cb..ad62c2bb1f 100644 --- a/input/drivers_joypad/apple_joypad_hid.c +++ b/input/drivers_joypad/apple_joypad_hid.c @@ -48,73 +48,71 @@ static void hid_pad_connection_send_control(void *data, uint8_t* data_buf, size_ static void hid_device_input_callback(void* context, IOReturn result, void* sender, IOHIDValueRef value) { - apple_input_data_t *apple = (apple_input_data_t*)driver.input_data; + apple_input_data_t *apple = (apple_input_data_t*)driver.input_data; struct pad_connection* connection = (struct pad_connection*)context; - IOHIDElementRef element = IOHIDValueGetElement(value); - uint32_t type = IOHIDElementGetType(element); - uint32_t page = IOHIDElementGetUsagePage(element); - uint32_t use = IOHIDElementGetUsage(element); + IOHIDElementRef element = IOHIDValueGetElement(value); + uint32_t type = IOHIDElementGetType(element); + uint32_t page = IOHIDElementGetUsagePage(element); + uint32_t use = IOHIDElementGetUsage(element); + + if (type != kIOHIDElementTypeInput_Misc) + if (type != kIOHIDElementTypeInput_Button) + if (type != kIOHIDElementTypeInput_Axis) + return; /* Joystick handler. * TODO: Can GamePad work the same? */ - if ( - (type == kIOHIDElementTypeInput_Misc) || - (type == kIOHIDElementTypeInput_Button) || - (type == kIOHIDElementTypeInput_Axis) - ) + switch (page) { - switch (page) - { - case kHIDPage_GenericDesktop: - switch (type) - { - case kIOHIDElementTypeInput_Misc: - switch (use) - { - case kHIDUsage_GD_Hatswitch: - break; - default: + case kHIDPage_GenericDesktop: + switch (type) + { + case kIOHIDElementTypeInput_Misc: + switch (use) + { + case kHIDUsage_GD_Hatswitch: + break; + default: + { + int i; + static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 }; + + for (i = 0; i < 4; i ++) { - static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 }; - int i; + CFIndex min = IOHIDElementGetPhysicalMin(element); + CFIndex max = IOHIDElementGetPhysicalMax(element) - min; + CFIndex state = IOHIDValueGetIntegerValue(value) - min; + float val = (float)state / (float)max; - for (i = 0; i < 4; i ++) - { - CFIndex min = IOHIDElementGetPhysicalMin(element); - CFIndex max = IOHIDElementGetPhysicalMax(element) - min; - CFIndex state = IOHIDValueGetIntegerValue(value) - min; - float val = (float)state / (float)max; + if (use != axis_use_ids[i]) + continue; - if (use != axis_use_ids[i]) - continue; - - apple->axes[connection->slot][i] = - ((val * 2.0f) - 1.0f) * 32767.0f; - } + apple->axes[connection->slot][i] = + ((val * 2.0f) - 1.0f) * 32767.0f; } - break; - } - break; - } - break; - case kHIDPage_Button: - switch (type) - { - case kIOHIDElementTypeInput_Button: - { - CFIndex state = IOHIDValueGetIntegerValue(value); - unsigned id = use - 1; + } + break; + } + break; + } + break; + case kHIDPage_Button: + switch (type) + { + case kIOHIDElementTypeInput_Button: + { + CFIndex state = IOHIDValueGetIntegerValue(value); + unsigned id = use - 1; - if (state) - BIT32_SET(apple->buttons[connection->slot], id); - else - BIT32_CLEAR(apple->buttons[connection->slot], id); - } - break; - } - break; - } + if (state) + BIT32_SET(apple->buttons[connection->slot], id); + else + BIT32_CLEAR(apple->buttons[connection->slot], id); + } + break; + } + break; } } @@ -125,7 +123,8 @@ static void remove_device(void* context, IOReturn result, void* sender) if (connection && connection->slot < MAX_USERS) { - char msg[512]; + char msg[PATH_MAX_LENGTH]; + snprintf(msg, sizeof(msg), "Joypad #%u (%s) disconnected.", connection->slot, "N/A"); msg_queue_push(g_extern.msg_queue, msg, 0, 60); @@ -196,25 +195,25 @@ static void add_device(void* context, IOReturn result, IOHIDDeviceRegisterInputValueCallback(device, hid_device_input_callback, connection); - if (device_name[0] != '\0') - { - strlcpy(g_settings.input.device_names[connection->slot], - device_name, sizeof(g_settings.input.device_names)); + if (device_name[0] == '\0') + return; - input_config_autoconfigure_joypad(connection->slot, - device_name, connection->v_id, connection->p_id, apple_hid_joypad.ident); - RARCH_LOG("Port %d: %s.\n", connection->slot, device_name); - } + strlcpy(g_settings.input.device_names[connection->slot], + device_name, sizeof(g_settings.input.device_names)); + + input_config_autoconfigure_joypad(connection->slot, + device_name, connection->v_id, connection->p_id, apple_hid_joypad.ident); + RARCH_LOG("Port %d: %s.\n", connection->slot, device_name); } static void append_matching_dictionary(CFMutableArrayRef array, uint32_t page, uint32_t use) { - CFNumberRef usen; + CFNumberRef usen, pagen; CFMutableDictionaryRef matcher = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - CFNumberRef pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page); + pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page); CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsagePageKey), pagen); CFRelease(pagen); @@ -285,7 +284,7 @@ static void apple_joypad_destroy(void) static bool apple_joypad_button(unsigned port, uint16_t joykey) { apple_input_data_t *apple = (apple_input_data_t*)driver.input_data; - uint32_t buttons = pad_connection_get_buttons(&slots[port], port); + uint32_t buttons = pad_connection_get_buttons(&slots[port], port); if (!apple || joykey == NO_BTN) return false; @@ -313,13 +312,17 @@ static int16_t apple_joypad_axis(unsigned port, uint32_t joyaxis) { val = apple->axes[port][AXIS_NEG_GET(joyaxis)]; val += pad_connection_get_axis(&slots[port], port, AXIS_NEG_GET(joyaxis)); - val = (val < 0) ? val : 0; + + if (val >= 0) + val = 0; } else if(AXIS_POS_GET(joyaxis) < 4) { val = apple->axes[port][AXIS_POS_GET(joyaxis)]; val += pad_connection_get_axis(&slots[port], port, AXIS_POS_GET(joyaxis)); - val = (val > 0) ? val : 0; + + if (val <= 0) + val = 0; } return val; diff --git a/input/drivers_joypad/apple_joypad_ios.c b/input/drivers_joypad/apple_joypad_ios.c index 1e156f6625..d38ee32cfa 100644 --- a/input/drivers_joypad/apple_joypad_ios.c +++ b/input/drivers_joypad/apple_joypad_ios.c @@ -53,10 +53,10 @@ static bool apple_joypad_button(unsigned port, uint16_t joykey) if (!apple || joykey == NO_BTN) return false; - // Check hat. + /* Check hat. */ if (GET_HAT_DIR(joykey)) return false; - // Check the button + /* Check the button */ if ((port < MAX_USERS) && (joykey < 32)) return ((apple->buttons[port] & (1 << joykey)) != 0) || ((buttons & (1 << joykey)) != 0); @@ -75,13 +75,15 @@ static int16_t apple_joypad_axis(unsigned port, uint32_t joyaxis) { val = apple->axes[port][AXIS_NEG_GET(joyaxis)]; val += pad_connection_get_axis(&slots[port], port, AXIS_NEG_GET(joyaxis)); - val = (val < 0) ? val : 0; + if (val >= 0) + val = 0; } else if(AXIS_POS_GET(joyaxis) < 4) { val = apple->axes[port][AXIS_POS_GET(joyaxis)]; val += pad_connection_get_axis(&slots[port], port, AXIS_POS_GET(joyaxis)); - val = (val > 0) ? val : 0; + if (val <= 0) + val = 0; } return val;