source: trunk/src/platform/ps3/ps3input.cpp @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16
17#if defined __CELLOS_LV2__
18#   include <cell/pad.h>
19#   include <cell/padfilter.h>
20#   include <sysutil/sysutil_sysparam.h>
21#endif
22
23#include "core.h"
24#include "ps3input.h"
25
26using namespace std;
27
28namespace lol
29{
30
31static int const NUM_PADS = 7; /* CellPadUtil also has 7 */
32
33/*
34 * PS3 Input implementation class
35 */
36
37class Ps3InputData
38{
39    friend class Ps3Input;
40
41#if defined __CELLOS_LV2__
42    Array<int, Stick *> m_joysticks;
43
44    vec2 mousepos;
45    ivec3 mousebuttons;
46
47    CellPadData pad_data[NUM_PADS];
48    CellPadFilterIIRSos filter_sos[NUM_PADS][4];
49    bool circle_validates;
50#endif
51};
52
53/*
54 * Public Ps3Input class
55 */
56
57Ps3Input::Ps3Input()
58  : m_data(new Ps3InputData())
59{
60#if defined __CELLOS_LV2__
61    int32_t ret = cellPadInit(NUM_PADS);
62    if (ret != CELL_OK && ret != CELL_PAD_ERROR_ALREADY_INITIALIZED)
63    {
64        Log::Error("could not initialise PS3 pad library\n");
65        exit(1);
66    }
67
68    int tmp;
69    ret = cellSysutilGetSystemParamInt(
70                        CELL_SYSUTIL_SYSTEMPARAM_ID_ENTER_BUTTON_ASSIGN, &tmp);
71    m_data->circle_validates =
72            (ret == CELL_OK && tmp == CELL_SYSUTIL_ENTER_BUTTON_ASSIGN_CIRCLE);
73
74    for (int i = 0; i < NUM_PADS; i++)
75        for (int j = 0; j < 4; j++)
76            cellPadFilterIIRInit(&m_data->filter_sos[i][j],
77                                 CELL_PADFILTER_IIR_CUTOFF_2ND_LPF_BT_010);
78
79    /* Try to detect connected pads. We should do this each frame. */
80    CellPadInfo2 pad_info2;
81    ret = cellPadGetInfo2(&pad_info2);
82    if (ret == CELL_PAD_OK)
83    {
84        for (int i = 0; i < NUM_PADS; i++)
85        {
86            if (!(pad_info2.port_status[i] & CELL_PAD_STATUS_CONNECTED))
87                continue;
88
89            Stick *stick = Input::CreateStick();
90            stick->SetAxisCount(4);
91            stick->SetButtonCount(16);
92            m_data->m_joysticks.Push(i, stick);
93        }
94    }
95
96    m_data->mousepos = vec2(320.0f, 240.0f);
97    m_data->mousebuttons = ivec3(0, 0, 0);
98
99    m_gamegroup = GAMEGROUP_BEFORE;
100#endif
101}
102
103void Ps3Input::TickGame(float seconds)
104{
105    Entity::TickGame(seconds);
106
107#if defined __CELLOS_LV2__
108    CellPadInfo2 pad_info2;
109    int32_t ret = cellPadGetInfo2(&pad_info2);
110    if (ret != CELL_PAD_OK)
111        return;
112
113    for (int i = 0; i < m_data->m_joysticks.Count(); i++)
114    {
115        int j = m_data->m_joysticks[i].m1;
116
117        if (!(pad_info2.port_status[j] & CELL_PAD_STATUS_CONNECTED))
118            continue;
119
120        /* Propagate all buttons */
121        uint16_t bitmap1 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL1];
122        for (int k = 0; k < 8; k++)
123            m_data->m_joysticks[i].m2->SetButton(k, bitmap1 & (1 << k));
124        uint16_t bitmap2 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL2];
125        for (int k = 0; k < 8; k++)
126            m_data->m_joysticks[i].m2->SetButton(8 + k, bitmap2 & (1 << k));
127
128        /* Get Pad status. If the data hasn't changed since the last call,
129         * data->pad[j].len will be 0 but we carry on anyway. */
130        ret = cellPadGetData(j, &m_data->pad_data[j]);
131        if (ret != CELL_PAD_OK)
132            continue;
133
134        if (!(pad_info2.system_info & CELL_PAD_INFO_INTERCEPTED))
135        {
136            int x = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X];
137            int y = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X + 1];
138            int x2 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X];
139            int y2 = m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X + 1];
140
141            /* Propagate all axes */
142            m_data->m_joysticks[i].m2->SetAxis(0, (x - 127) / 127.0f);
143            m_data->m_joysticks[i].m2->SetAxis(1, (y - 127) / 127.0f);
144            m_data->m_joysticks[i].m2->SetAxis(2, (x2 - 127) / 127.0f);
145            m_data->m_joysticks[i].m2->SetAxis(3, (y2 - 127) / 127.0f);
146
147            /* Right stick moves the mouse FIXME: deprecated */
148            vec2 delta(4.f * (abs(x - 127) < 16 ? 0 : x - 127),
149                       -4.f * (abs(y - 127) < 16 ? 0 : y - 127));
150            m_data->mousepos += delta * seconds;
151            Input::SetMousePos((ivec2)m_data->mousepos);
152        }
153
154        /* L1 or R1 for mouse button FIXME: deprecated */
155        int but = (m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL2]
156                                                          & CELL_PAD_CTRL_L1)
157               || (m_data->pad_data[j].button[CELL_PAD_BTN_OFFSET_DIGITAL2]
158                                                          & CELL_PAD_CTRL_R1);
159        if (but && !m_data->mousebuttons.x)
160            Input::SetMouseButton(0);
161        else if (!but && m_data->mousebuttons.x)
162            Input::UnsetMouseButton(0);
163
164        m_data->mousebuttons.x = but;
165    }
166#endif
167}
168
169Ps3Input::~Ps3Input()
170{
171#if defined __CELLOS_LV2__
172    for (int i = 0; i < m_data->m_joysticks.Count(); ++i)
173    {
174        Input::DestroyStick(m_data->m_joysticks[0].m2);
175    }
176#endif
177    delete m_data;
178}
179
180} /* namespace lol */
181
Note: See TracBrowser for help on using the repository browser.