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