From c36a5fe78ffbc90bc83fff53ed502f11776f70cf Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 6 Jul 2012 12:14:29 +0000 Subject: [PATCH] ps3: add pad support for the PS3, without plug/unplug detection for now. --- src/platform/ps3/ps3input.cpp | 89 +++++++++++++++++++++++++---------- src/platform/ps3/ps3input.h | 2 +- 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/platform/ps3/ps3input.cpp b/src/platform/ps3/ps3input.cpp index 56fe24a8..88e4f5ca 100644 --- a/src/platform/ps3/ps3input.cpp +++ b/src/platform/ps3/ps3input.cpp @@ -39,6 +39,8 @@ class Ps3InputData friend class Ps3Input; #if defined __CELLOS_LV2__ + Array m_joysticks; + vec2 mousepos; ivec3 mousebuttons; @@ -53,7 +55,7 @@ class Ps3InputData */ Ps3Input::Ps3Input() - : data(new Ps3InputData()) + : m_data(new Ps3InputData()) { #if defined __CELLOS_LV2__ int32_t ret = cellPadInit(NUM_PADS); @@ -66,16 +68,33 @@ Ps3Input::Ps3Input() int tmp; ret = cellSysutilGetSystemParamInt( CELL_SYSUTIL_SYSTEMPARAM_ID_ENTER_BUTTON_ASSIGN, &tmp); - data->circle_validates = + m_data->circle_validates = (ret == CELL_OK && tmp == CELL_SYSUTIL_ENTER_BUTTON_ASSIGN_CIRCLE); for (int i = 0; i < NUM_PADS; i++) for (int j = 0; j < 4; j++) - cellPadFilterIIRInit(&data->filter_sos[i][j], + cellPadFilterIIRInit(&m_data->filter_sos[i][j], CELL_PADFILTER_IIR_CUTOFF_2ND_LPF_BT_010); - data->mousepos = vec2(320.0f, 240.0f); - data->mousebuttons = ivec3(0, 0, 0); + /* Try to detect connected pads. We should do this each frame. */ + CellPadInfo2 pad_info2; + ret = cellPadGetInfo2(&pad_info2); + if (ret == CELL_PAD_OK) + { + for (int i = 0; i < NUM_PADS; i++) + { + if (!(pad_info2.port_status[i] & CELL_PAD_STATUS_CONNECTED)) + continue; + + Stick *stick = Input::CreateStick(); + stick->SetAxisCount(4); + stick->SetButtonCount(16); + m_data->m_joysticks.Push(i, stick); + } + } + + m_data->mousepos = vec2(320.0f, 240.0f); + m_data->mousebuttons = ivec3(0, 0, 0); m_gamegroup = GAMEGROUP_BEFORE; #endif @@ -91,49 +110,71 @@ void Ps3Input::TickGame(float seconds) if (ret != CELL_PAD_OK) return; - for (int i = 0; i < NUM_PADS; i++) + for (int i = 0; i < m_data->m_joysticks.Count(); i++) { - if (!(pad_info2.port_status[i] & CELL_PAD_STATUS_CONNECTED)) + int j = m_data->m_joysticks[i].m1; + + if (!(pad_info2.port_status[j] & CELL_PAD_STATUS_CONNECTED)) continue; + /* Propagate all buttons */ + uint16_t bitmap1 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL1]; + for (int k = 0; k < 8; k++) + m_data->m_joysticks[i].m2->SetButton(k, bitmap1 & (1 << k)); + uint16_t bitmap2 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL2]; + for (int k = 0; k < 8; k++) + m_data->m_joysticks[i].m2->SetButton(8 + k, bitmap2 & (1 << k)); + /* Get Pad status. If the data hasn't changed since the last call, - * data->pad[i].len will be 0 but we carry on anyway. */ - ret = cellPadGetData(i, &data->pad_data[i]); + * data->pad[j].len will be 0 but we carry on anyway. */ + ret = cellPadGetData(j, &m_data->pad_data[j]); if (ret != CELL_PAD_OK) continue; - /* Right stick moves the mouse */ if (!(pad_info2.system_info & CELL_PAD_INFO_INTERCEPTED)) { - int x = data->pad_data[i].button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X]; - int y = data->pad_data[i].button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X + 1]; + int x = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X]; + int y = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X + 1]; + int x2 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X]; + int y2 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X + 1]; + + /* Propagate all axes */ + m_data->m_joysticks[i].m2->SetAxis(0, (x - 127) / 127.0f); + m_data->m_joysticks[i].m2->SetAxis(1, (y - 127) / 127.0f); + m_data->m_joysticks[i].m2->SetAxis(2, (x2 - 127) / 127.0f); + m_data->m_joysticks[i].m2->SetAxis(3, (y2 - 127) / 127.0f); + + /* Right stick moves the mouse FIXME: deprecated */ vec2 delta(4.f * (abs(x - 127) < 16 ? 0 : x - 127), -4.f * (abs(y - 127) < 16 ? 0 : y - 127)); - data->mousepos += delta * seconds; - Input::SetMousePos((ivec2)data->mousepos); + m_data->mousepos += delta * seconds; + Input::SetMousePos((ivec2)m_data->mousepos); } - /* L1 or R1 for mouse button */ - int but = (data->pad_data[i].button[CELL_PAD_BTN_OFFSET_DIGITAL2] + /* L1 or R1 for mouse button FIXME: deprecated */ + int but = (m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_PAD_CTRL_L1) - || (data->pad_data[i].button[CELL_PAD_BTN_OFFSET_DIGITAL2] + || (m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_PAD_CTRL_R1); - if (but && !data->mousebuttons.x) + if (but && !m_data->mousebuttons.x) Input::SetMouseButton(0); - else if (!but && data->mousebuttons.x) + else if (!but && m_data->mousebuttons.x) Input::UnsetMouseButton(0); - data->mousebuttons.x = but; - - /* Only handle the first pad we meet */ - break; + m_data->mousebuttons.x = but; } #endif } Ps3Input::~Ps3Input() { - delete data; +#if defined __CELLOS_LV2__ + for (int i = 0; i < m_data->m_joysticks.Count(); ++i) + { + Input::DestroyStick(m_data->m_joysticks[0].m2); + } +#endif + delete m_data; } } /* namespace lol */ diff --git a/src/platform/ps3/ps3input.h b/src/platform/ps3/ps3input.h index e1d368f2..ea8ca5bc 100644 --- a/src/platform/ps3/ps3input.h +++ b/src/platform/ps3/ps3input.h @@ -33,7 +33,7 @@ protected: virtual void TickGame(float seconds); private: - Ps3InputData *data; + Ps3InputData *m_data; }; } /* namespace lol */