PS2GL
OpenGL*-like API for the PS2
Loading...
Searching...
No Matches
pads.cpp
1/* Copyright (C) 2000,2001,2002 Sony Computer Entertainment America
2
3 This file is subject to the terms and conditions of the GNU Lesser
4 General Public License Version 2.1. See the file "COPYING" in the
5 main directory of this archive for more details. */
6
7/********************************************
8 * includes
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "loadfile.h"
16#include "sifrpc.h"
17
18#include "pads.h"
19#include "ps2s/math.h"
20
21/********************************************
22 * constants
23 */
24
25#define kPad0 0
26#define kPad1 1
27
28#define kPort0 0
29#define kSlot0 0
30
31#define kPadModeStandard 0x4
32#define kPadModeAnalog 0x7
33
34#define kPadSetLockModeUnchanged 0
35#define kPadSetLockModeLock 3
36#define kPadSetLockModeUnlock 1
37
38#define kStickMaxRadius 120
39#define kStickDeadRadius 25
40
41/********************************************
42 * globals
43 */
44
45CPad Pad0(kPad0);
46CPad Pad1(kPad1);
47
48/********************************************
49 * Pads
50 */
51
52void Pads::Init(void)
53{
54 // open the pads.. this should be elsewhere..
55 SifInitRpc(0);
56
57 /* load sio2man.irx */
58 if (SifLoadModule("rom0:SIO2MAN", 0, NULL) < 0) {
59 printf("Can't load module sio2man\n");
60 exit(0);
61 }
62 /* load padman.irx */
63 if (SifLoadModule("rom0:PADMAN", 0, NULL) < 0) {
64 printf("Can't load module padman\n");
65 exit(0);
66 }
67
68 padInit(0); // "must be zero"
69
70 if (!Pad0.Open()) {
71 printf("Couldn't open Pad0.\n");
72 exit(-1);
73 }
74}
75
76void Pads::Read(void)
77{
78 Pad0.Read();
79}
80
81/********************************************
82 * CPad
83 */
84
85CPad::CPad(unsigned int port)
86 : uiPort(port)
87 , bPadModeSet(false)
88{
89 memset(&CurStatus, 0, sizeof(tPadStatus));
90 memset(&LastStatus, 0, sizeof(tPadStatus));
91
92 // All buttons released
93 CurStatus.buttons = 0xffff;
94 LastStatus.buttons = 0xffff;
95}
96
97bool CPad::Open(void)
98{
99 // slot is only for use with multitap
100 return padPortOpen(uiPort, kSlot0, DmaBuffer);
101}
102
103void CPad::Read(void)
104{
105 t32 padState = padGetState(kPort0, kSlot0);
106 if (padState != PAD_STATE_STABLE)
107 return;
108
109 if (!bPadModeSet) {
110 // who knows what the 1 parameter is.. a return val of 1 indicates that the request is
111 // being processed
112 if (padSetMainMode(uiPort, kSlot0, 1, kPadSetLockModeUnlock) == 1)
113 bPadModeSet = true;
114 } else {
115 tPadStatus padStatus;
116 padRead(uiPort, kSlot0, (padButtonStatus*)&padStatus);
117
118 if (padStatus.success == 0) { // 0 indicates success
119 LastStatus = CurStatus;
120 padStatus.rightStick = CurStatus.rightStick;
121 padStatus.leftStick = CurStatus.leftStick;
122 CurStatus = padStatus;
123
124 // t32 id = padInfoMode( uiPort, kSlot0, PAD_MODECURID, 0 );
125 // if ( id == kPadModeStandard || id == kPadModeAnalog ) {
126 // // flip the sense of the bit field (1 = pressed)
127 // CurStatus.buttons ^= 0xffff;
128 // }
129
130 // sticks
131 if (WasPushed(Pads::kRightStickButton)) {
132 CurStatus.leftStick.isCentered = false;
133 CurStatus.rightStick.isCentered = false;
134 }
135 CurStatus.leftStick.xVal = CurStatus.l3h;
136 CurStatus.leftStick.yVal = CurStatus.l3v;
137 CurStatus.rightStick.xVal = CurStatus.r3h;
138 CurStatus.rightStick.yVal = CurStatus.r3v;
139 UpdateStick(&CurStatus.leftStick, &LastStatus.leftStick);
140 UpdateStick(&CurStatus.rightStick, &LastStatus.rightStick);
141 }
142 }
143}
144
145bool CPad::UpdateStick(tStickData* stickCur, tStickData* stickLast)
146{
147 t8 temp;
148 bool isChanged = false;
149
150 using namespace Math;
151
152 if (!stickCur->isCentered) {
153 stickCur->xCenter = stickCur->xVal;
154 stickCur->yCenter = stickCur->yVal;
155 stickCur->xPos = 0.0f;
156 stickCur->yPos = 0.0f;
157 stickCur->isCentered = true;
158
159 isChanged = true;
160 } else {
161 if (!FuzzyEqualsi(stickCur->xVal, stickCur->xCenter, kStickDeadRadius)) {
162 // stick is not inside the dead zone
163 temp = ((stickCur->xVal > stickCur->xCenter) ? -kStickDeadRadius : kStickDeadRadius);
164 stickCur->xPos = (float)(stickCur->xVal - stickCur->xCenter + temp) / (float)kStickMaxRadius;
165 isChanged = true;
166 } else {
167 // stick is inside the dead zone
168 stickCur->xPos = 0.0f;
169 // if it just entered the dead zone, send out one last event
170 if (!FuzzyEqualsi(stickLast->xVal, stickCur->xCenter, kStickDeadRadius))
171 isChanged = true;
172 }
173 if (!FuzzyEqualsi(stickCur->yVal, stickCur->yCenter, kStickDeadRadius)) {
174 // stick is not inside the dead zone
175 temp = (stickCur->yVal > stickCur->yCenter) ? kStickDeadRadius : -kStickDeadRadius;
176 stickCur->yPos = (float)(stickCur->yCenter - stickCur->yVal + temp) / (float)kStickMaxRadius;
177 isChanged = true;
178 } else {
179 // stick is inside the dead zone
180 stickCur->yPos = 0.0f;
181 // if it just entered the dead zone, send out one last event
182 if (!FuzzyEqualsi(stickLast->yVal, stickCur->yCenter, kStickDeadRadius))
183 isChanged = true;
184 }
185
186 stickCur->xPos = Clamp(stickCur->xPos, -1.0f, 1.0f);
187 stickCur->yPos = Clamp(stickCur->yPos, -1.0f, 1.0f);
188 }
189
190 return isChanged;
191}
192
193bool CPad::IsDown(tPadStatus status, unsigned int button)
194{
195 return !IsUp(status, button);
196}
197
198bool CPad::IsUp(tPadStatus status, unsigned int button)
199{
200 return status.buttons & (1 << button);
201}
202
203bool CPad::IsDown(unsigned int button)
204{
205 return IsDown(CurStatus, button);
206}
207
208bool CPad::IsUp(unsigned int button)
209{
210 return IsUp(CurStatus, button);
211}
212
213bool CPad::WasPushed(unsigned int button)
214{
215 return IsUp(LastStatus, button) && IsDown(CurStatus, button);
216}
217
218bool CPad::WasReleased(unsigned int button)
219{
220 return IsDown(LastStatus, button) && IsUp(CurStatus, button);
221}
Definition pads.h:46