source: trunk/src/ps3input.cpp @ 845

Last change on this file since 845 was 845, checked in by sam, 10 years ago

ps3: prevent compilation of Ps3Input on other platforms.

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