/* * Copyright (C) 2025 Volodymyr Atamanenko * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ #include "reimpl/controls.h" #include #include #include #include #include #include "utils/logger.h" #define LEFT_ANALOG_DEADZONE 0.16f #define RIGHT_ANALOG_DEADZONE 0.16f void coord_normalize(float * x, float * y, float deadzone) { float magnitude = sqrtf((*x * *x) + (*y * *y)); if (magnitude < deadzone) { *x = 0; *y = 0; return; } // normalize *x = *x / magnitude; *y = *y / magnitude; float multiplier = ((magnitude - deadzone) / (1 - deadzone)); *x = *x * multiplier; *y = *y * multiplier; } void controls_init() { // Enable analog sticks and touchscreen sceCtrlSetSamplingModeExt(SCE_CTRL_MODE_ANALOG_WIDE); sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START); SceTouchPanelInfo panel; int panel_result = sceTouchGetPanelInfo(SCE_TOUCH_PORT_FRONT, &panel); l_info("TOUCH init result=0x%08x area=%u,%u..%u,%u display=%u,%u..%u,%u", panel_result, panel.minAaX, panel.minAaY, panel.maxAaX, panel.maxAaY, panel.minDispX, panel.minDispY, panel.maxDispX, panel.maxDispY); // Enable accelerometer sceMotionStartSampling(); } void poll_touch(); void poll_pad(); void poll_accel(); void poll_stick(ControlsStickId which, float raw_x, float raw_y, float * readings_x, float * readings_y, float deadzone); void controls_poll() { poll_touch(); poll_pad(); //poll_accel(); } SceTouchData touch; SceTouchData touch_old; static SceTouchPanelInfo touch_panel; static int touch_panel_valid; static unsigned int touch_events; static float cursor_x = 480.0f; static float cursor_y = 272.0f; static int cursor_pressed = 0; static void touch_to_screen(uint16_t raw_x, uint16_t raw_y, float *x, float *y) { if (!touch_panel_valid) { touch_panel_valid = sceTouchGetPanelInfo(SCE_TOUCH_PORT_FRONT, &touch_panel) >= 0; } float min_x = touch_panel_valid ? (float)touch_panel.minAaX : 0.0f; float min_y = touch_panel_valid ? (float)touch_panel.minAaY : 0.0f; float span_x = touch_panel_valid ? (float)(touch_panel.maxAaX - touch_panel.minAaX) : 1919.0f; float span_y = touch_panel_valid ? (float)(touch_panel.maxAaY - touch_panel.minAaY) : 1087.0f; if (span_x < 1.0f) span_x = 1919.0f; if (span_y < 1.0f) span_y = 1087.0f; *x = ((float)raw_x - min_x) * 959.0f / span_x; *y = ((float)raw_y - min_y) * 543.0f / span_y; if (*x < 0.0f) *x = 0.0f; if (*x > 959.0f) *x = 959.0f; if (*y < 0.0f) *y = 0.0f; if (*y > 543.0f) *y = 543.0f; } void poll_touch() { sceTouchPeek(SCE_TOUCH_PORT_FRONT, &touch, 1); for (int i = 0; i < touch.reportNum; i++) { float x, y; touch_to_screen(touch.report[i].x, touch.report[i].y, &x, &y); // Check if the finger was down before to distinguish between the Move and Down events int finger_down = 0; if (touch_old.reportNum > 0) { for (int j = 0; j < touch_old.reportNum; j++) { if (touch.report[i].id == touch_old.report[j].id) { finger_down = 1; break; } } } if (!finger_down) { cursor_x = x; cursor_y = y; cursor_pressed = 1; if (touch_events++ < 24) l_info("TOUCH down id=%u raw=%u,%u screen=%.1f,%.1f", touch.report[i].id, touch.report[i].x, touch.report[i].y, x, y); controls_handler_touch(touch.report[i].id, x, y, CONTROLS_ACTION_DOWN); } else { cursor_x = x; cursor_y = y; if (touch_events++ < 24) l_info("TOUCH move id=%u raw=%u,%u screen=%.1f,%.1f", touch.report[i].id, touch.report[i].x, touch.report[i].y, x, y); controls_handler_touch(touch.report[i].id, x, y, CONTROLS_ACTION_MOVE); } } for (int i = 0; i < touch_old.reportNum; i++) { int finger_up = 1; for (int j = 0; j < touch.reportNum; j++) { if (touch.report[j].id == touch_old.report[i].id ) { finger_up = 0; break; } } if (finger_up == 1) { float x, y; touch_to_screen(touch_old.report[i].x, touch_old.report[i].y, &x, &y); if (touch_events++ < 24) l_info("TOUCH up id=%u raw=%u,%u screen=%.1f,%.1f", touch_old.report[i].id, touch_old.report[i].x, touch_old.report[i].y, x, y); controls_handler_touch(touch_old.report[i].id, x, y, CONTROLS_ACTION_UP); cursor_x = x; cursor_y = y; cursor_pressed = 0; } } sceClibMemcpy(&touch_old, &touch, sizeof(touch)); } static ButtonMapping mapping[] = { { SCE_CTRL_UP, AKEYCODE_DPAD_UP }, { SCE_CTRL_DOWN, AKEYCODE_DPAD_DOWN }, { SCE_CTRL_LEFT, AKEYCODE_DPAD_LEFT }, { SCE_CTRL_RIGHT, AKEYCODE_DPAD_RIGHT }, { SCE_CTRL_CIRCLE, AKEYCODE_BUTTON_B }, { SCE_CTRL_SQUARE, AKEYCODE_BUTTON_X }, { SCE_CTRL_TRIANGLE, AKEYCODE_BUTTON_Y }, { SCE_CTRL_L1, AKEYCODE_BUTTON_L1 }, { SCE_CTRL_R1, AKEYCODE_BUTTON_R1 }, { SCE_CTRL_START, AKEYCODE_BUTTON_START }, { SCE_CTRL_SELECT, AKEYCODE_BUTTON_SELECT }, }; uint32_t old_buttons = 0, current_buttons = 0, pressed_buttons = 0, released_buttons = 0; float analog_lx[3] = { 0 }; float analog_ly[3] = { 0 }; float analog_rx[3] = { 0 }; float analog_ry[3] = { 0 }; void controls_get_cursor(float *x, float *y, int *pressed) { if (x) *x = cursor_x; if (y) *y = cursor_y; if (pressed) *pressed = cursor_pressed; } uint32_t controls_get_pressed(void) { return pressed_buttons; } void poll_pad() { SceCtrlData pad; sceCtrlPeekBufferPositiveExt2(0, &pad, 1); // Gamepad buttons old_buttons = current_buttons; current_buttons = pad.buttons; pressed_buttons = current_buttons & ~old_buttons; released_buttons = ~current_buttons & old_buttons; float cursor_dx = ((float)pad.lx - 128.0f) / 128.0f; float cursor_dy = ((float)pad.ly - 128.0f) / 128.0f; coord_normalize(&cursor_dx, &cursor_dy, LEFT_ANALOG_DEADZONE); cursor_x += cursor_dx * 9.0f; cursor_y += cursor_dy * 9.0f; if (current_buttons & SCE_CTRL_LEFT) cursor_x -= 3.0f; if (current_buttons & SCE_CTRL_RIGHT) cursor_x += 3.0f; if (current_buttons & SCE_CTRL_UP) cursor_y -= 3.0f; if (current_buttons & SCE_CTRL_DOWN) cursor_y += 3.0f; if (cursor_x < 0.0f) cursor_x = 0.0f; if (cursor_x > 959.0f) cursor_x = 959.0f; if (cursor_y < 0.0f) cursor_y = 0.0f; if (cursor_y > 543.0f) cursor_y = 543.0f; if (cursor_pressed && (cursor_dx != 0.0f || cursor_dy != 0.0f || (current_buttons & (SCE_CTRL_LEFT | SCE_CTRL_RIGHT | SCE_CTRL_UP | SCE_CTRL_DOWN)))) controls_handler_touch(15, cursor_x, cursor_y, CONTROLS_ACTION_MOVE); if (pressed_buttons & SCE_CTRL_CROSS) { cursor_pressed = 1; controls_handler_touch(15, cursor_x, cursor_y, CONTROLS_ACTION_DOWN); } if (released_buttons & SCE_CTRL_CROSS) { controls_handler_touch(15, cursor_x, cursor_y, CONTROLS_ACTION_UP); cursor_pressed = 0; } for (int i = 0; i < sizeof(mapping) / sizeof(ButtonMapping); i++) { if (pressed_buttons & mapping[i].sce_button) { controls_handler_key(mapping[i].android_button, CONTROLS_ACTION_DOWN); } if (released_buttons & mapping[i].sce_button) { controls_handler_key(mapping[i].android_button, CONTROLS_ACTION_UP); } } // Analog sticks poll_stick(CONTROLS_STICK_LEFT, (float)pad.lx, (float)pad.ly, analog_lx, analog_ly, LEFT_ANALOG_DEADZONE); poll_stick(CONTROLS_STICK_RIGHT, (float)pad.rx, (float)pad.ry, analog_rx, analog_ry, RIGHT_ANALOG_DEADZONE); } void poll_stick(ControlsStickId which, float raw_x, float raw_y, float * readings_x, float * readings_y, float deadzone) { readings_x[0] = (raw_x - 128.0f) / 128.0f; readings_y[0] = (raw_y - 128.0f) / 128.0f; coord_normalize(&readings_x[0], &readings_y[0], deadzone); // Last two readings are 0, the one before that isn't ==> MOTION_ACTION_UP if ( (readings_x[0] == 0.f && readings_y[0] == 0.f) && (readings_x[1] == 0.f && readings_y[1] == 0.f) && (readings_x[2] != 0.f || readings_y[2] != 0.f) ) { controls_handler_analog(which, readings_x[0], readings_y[0], CONTROLS_ACTION_UP); } // Current reading isn't 0, the two before are ==> MOTION_ACTION_DOWN else if ( (readings_x[0] != 0.f || readings_y[0] != 0.f) && (readings_x[1] == 0.f && readings_y[1] == 0.f) && (readings_x[2] == 0.f && readings_y[2] == 0.f) ) { controls_handler_analog(which, readings_x[0], readings_y[0], CONTROLS_ACTION_DOWN); } // Other cases ==> MOTION_ACTION_MOVE else { controls_handler_analog(which, readings_x[0], readings_y[0], CONTROLS_ACTION_MOVE); } readings_x[2] = readings_x[1]; readings_y[2] = readings_y[1]; readings_x[1] = readings_x[0]; readings_y[1] = readings_y[0]; }