source: trunk/src/ps3input.cpp @ 839

Last change on this file since 839 was 839, checked in by sam, 8 years ago

ps3: get rid of the useless and convoluted padutil dependency; the
standard pad API is perfect for us.

File size: 3.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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://sam.zoy.org/projects/COPYING.WTFPL 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    vec2 mousepos;
42    vec3i mousebuttons;
43
44    CellPadFilterIIRSos filter_sos[NUM_PADS][4];
45    bool circle_validates;
46};
47
48/*
49 * Public Ps3Input class
50 */
51
52Ps3Input::Ps3Input()
53  : data(new Ps3InputData())
54{
55#if defined __CELLOS_LV2__
56    int32_t ret = cellPadInit(NUM_PADS);
57    if (ret != CELL_OK && ret != CELL_PAD_ERROR_ALREADY_INITIALIZED)
58    {
59        Log::Error("could not initialise PS3 pad library\n");
60        exit(1);
61    }
62
63    int tmp;
64    ret = cellSysutilGetSystemParamInt(
65                        CELL_SYSUTIL_SYSTEMPARAM_ID_ENTER_BUTTON_ASSIGN, &tmp);
66    data->circle_validates =
67            (ret == CELL_OK && tmp == CELL_SYSUTIL_ENTER_BUTTON_ASSIGN_CIRCLE);
68
69    for (int i = 0; i < NUM_PADS; i++)
70        for (int j = 0; j < 4; j++)
71            cellPadFilterIIRInit(&data->filter_sos[i][j],
72                                 CELL_PADFILTER_IIR_CUTOFF_2ND_LPF_BT_010);
73
74    data->mousepos = vec2(320.0f, 240.0f);
75    data->mousebuttons = vec3i(0, 0, 0);
76
77    gamegroup = GAMEGROUP_BEFORE;
78#endif
79}
80
81void Ps3Input::TickGame(float deltams)
82{
83    Entity::TickGame(deltams);
84
85#if defined __CELLOS_LV2__
86    CellPadInfo2 pad_info2;
87    int32_t ret = cellPadGetInfo2(&pad_info2);
88    if (ret != CELL_PAD_OK)
89        return;
90
91    for (int i = 0; i < NUM_PADS; i++)
92    {
93        if (!(pad_info2.port_status[i] & CELL_PAD_STATUS_CONNECTED))
94            continue;
95
96        CellPadData pad_data;
97        ret = cellPadGetData(i, &pad_data);
98        if (ret != CELL_PAD_OK || pad_data.len == 0)
99            continue;
100
101        /* L1 or R1 for mouse button */
102        int but = (pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2]
103                                                          == CELL_PAD_CTRL_L1)
104               || (pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2]
105                                                          == CELL_PAD_CTRL_R1);
106        if (but && !data->mousebuttons.x)
107            Input::SetMouseButton(0);
108        else if (!but && data->mousebuttons.x)
109            Input::UnsetMouseButton(0);
110
111        data->mousebuttons.x = but;
112
113        /* Right stick moves the mouse */
114        if (!(pad_info2.system_info & CELL_PAD_INFO_INTERCEPTED))
115        {
116            int x = pad_data.button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X];
117            int y = pad_data.button[CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X + 1];
118            vec2 delta(4e-3f * (abs(x - 127) < 16 ? 0 : x - 127),
119                       -4e-3f * (abs(y - 127) < 16 ? 0 : y - 127));
120            data->mousepos += delta * deltams;
121            Input::SetMousePos((vec2i)data->mousepos);
122        }
123
124        /* Only handle the first pad we meet */
125        break;
126    }
127#endif
128}
129
130Ps3Input::~Ps3Input()
131{
132    delete data;
133}
134
135} /* namespace lol */
136
Note: See TracBrowser for help on using the repository browser.