ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
padx.c
Go to the documentation of this file.
1 #include <tamtypes.h>
2 #include <kernel.h>
3 #include <loadfile.h>
4 #include <stdio.h>
5 #include <sifrpc.h>
6 #include <gs_privileged.h>
7 #include <libpad.h>
8 #include <string.h>
9 
10 #define DEBUG_BGCOLOR(col) *((u64 *) 0x120000e0) = (u64) (col)
11 
12 static char* padTypeStr[] = { "Unsupported controller", "Mouse", "Nejicon",
13  "Konami Gun", "Digital", "Analog", "Namco Gun",
14  "DualShock"};
15 
16 static char *padBuf[2];
17 static u32 portConnected[2];
18 static u32 paddata[2];
19 static u32 old_pad[2];
20 static u32 new_pad[2];
21 static u8 actDirect[2][6] = { {0,0,0,0,0,0}, {0,0,0,0,0,0}};
22 
23 static void wait_vsync()
24 {
25  // Enable the vsync interrupt.
26  *GS_REG_CSR |= GS_SET_CSR(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0);
27 
28  // Wait for the vsync interrupt.
29  while (!(*GS_REG_CSR & (GS_SET_CSR(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0)))) { }
30 
31  // Disable the vsync interrupt.
32  *GS_REG_CSR &= ~GS_SET_CSR(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0);
33 }
34 
35 static void loadmodules(int free)
36 {
37  s32 ret;
38 
39  if(free == 1)
40  {
41  if((ret = SifLoadModule("host0:sio2man.irx", 0, NULL)) < 0)
42  {
43  printf("Failed to load sio2man.irx module (%d)\n", ret);
44  SleepThread();
45  }
46 
47  if((ret = SifLoadModule("host0:padman.irx", 0, NULL)) < 0)
48  {
49  printf("Failed to load padman.irx module (%d)\n", ret);
50  SleepThread();
51  }
52  }
53  else
54  {
55  if((ret = SifLoadModule("rom0:XSIO2MAN", 0, NULL)) < 0)
56  {
57  printf("Failed to load XSIO2MAN module (%d)\n", ret);
58  SleepThread();
59  }
60 
61  if((ret = SifLoadModule("rom0:XPADMAN", 0, NULL)) < 0)
62  {
63  printf("Failed to load XPADMAN module (%d)\n", ret);
64  SleepThread();
65  }
66  }
67 }
68 
69 static void padWait(int port)
70 {
71  /* Wait for request to complete. */
73  wait_vsync();
74 
75  /* Wait for pad to be stable. */
76  while(padGetState(port, 0) != PAD_STATE_STABLE)
77  wait_vsync();
78 }
79 
80 static void padStartAct(int port, int act, int speed)
81 {
82  if(actDirect[port][act] != speed)
83  {
84  actDirect[port][act] = speed;
85 
87  padWait(port);
88  }
89 }
90 
91 static void padStopAct(int port, int act)
92 {
93  padStartAct(port, act, 0);
94 }
95 
96 int main(int argc, char **argv)
97 {
98  u32 port;
99  struct padButtonStatus buttons;
100  int dualshock[2];
101  int acts[2];
102 
103  SifInitRpc(0);
104 
105  printf("libpadx sample");
106 
107  if((argc == 2) && (strncmp(argv[1], "free", 4) == 0))
108  {
109  printf(" - Using PS2SDK sio2man.irx and padman.irx modules.\n");
110  loadmodules(1);
111  }
112  else
113  {
114  printf(" - Using ROM XSIO2MAN and XPADMAN modules.\n");
115  printf("Start this sample with 'free' as an argument to load sio2man.irx and padman.irx\n");
116  printf("Example: ps2client execee host:padx_sample.elf free\n");
117  loadmodules(0);
118  }
119 
120 
121 
122 
123  padInit(0);
124 
125  padBuf[0] = memalign(64, 256);
126  padBuf[1] = memalign(64, 256);
127 
128  old_pad[0] = 0;
129  old_pad[1] = 0;
130 
131  portConnected[0] = 0;
132  portConnected[1] = 0;
133 
134  dualshock[0] = 0;
135  dualshock[1] = 0;
136 
137  acts[0] = 0;
138  acts[1] = 0;
139 
140  padPortOpen(0, 0, padBuf[0]);
141  padPortOpen(1, 0, padBuf[1]);
142 
143  while(1)
144  {
145  for(port=0; port < 2; port++)
146  {
147  s32 state = padGetState(port, 0);
148 
149  if((state == PAD_STATE_STABLE) && (portConnected[port] == 0))
150  {
151  u32 i;
152  u8 mTable[8];
153  u32 ModeCurId;
154  u32 ModeCurOffs;
155  u32 ModeCurExId;
156  u32 ModeTableNum = padInfoMode(port, 0, PAD_MODETABLE, -1);
157 
158  printf("Controller (%i) connected\n", port);
159 
160  /* Check if dualshock and if so, activate analog mode */
161  for(i = 0; i < ModeTableNum; i++)
162  mTable[i] = padInfoMode(port, 0, PAD_MODETABLE, i);
163 
164  /* Works for dualshock2 */
165  if((mTable[0] == 4) && (mTable[1] == 7) && (ModeTableNum == 2))
166  dualshock[port] = 1;
167 
168  /* Active and lock analog mode */
169  if(dualshock[port] == 1)
170  {
172  padWait(port);
173  }
174 
175  ModeCurId = padInfoMode(port, 0, PAD_MODECURID, 0);
176  ModeCurOffs = padInfoMode(port, 0, PAD_MODECUROFFS, 0);
177  ModeCurExId = padInfoMode(port, 0, PAD_MODECUREXID, 0);
178  ModeTableNum = padInfoMode(port, 0, PAD_MODETABLE, -1);
179  acts[port] = padInfoAct(port, 0, -1, 0);
180 
181  printf(" ModeCurId : %i (%s)\n", (int)ModeCurId, padTypeStr[ModeCurId]);
182  printf(" ModeCurExId : %i\n", (int)ModeCurExId);
183  printf(" ModeTable : ");
184 
185  for(i = 0; i < ModeTableNum; i++)
186  {
187  mTable[i] = padInfoMode(port, 0, PAD_MODETABLE, i);
188  printf("%i ", (int)mTable[i]);
189  }
190 
191  printf("\n");
192  printf(" ModeTableNum : %i\n", (int)ModeTableNum);
193  printf(" ModeCurOffs : %i\n", (int)ModeCurOffs);
194  printf(" NumOfAct : %i\n", (int)acts[port]);
195  printf(" PressMode : %i\n", (int)padInfoPressMode(port, 0));
196 
197 
198  if(acts[port] > 0)
199  {
200  u8 actAlign[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
201  u32 i;
202 
203  /* Set offsets for motor parameters for SetActDirect. */
204  for(i=0; i < acts[port]; i++)
205  actAlign[i] = i;
206 
208  padWait(port);
209  }
210 
211  printf(" EnterPressMode : %i\n", (int)padEnterPressMode(port, 0));
212  padWait(port);
213 
214  printf("Ready\n");
215 
216  portConnected[port] = 1;
217  }
218 
219  if((state == PAD_STATE_DISCONN) && (portConnected[port] == 1))
220  {
221  printf("Controller (%i) disconnected\n", port);
222  portConnected[port] = 0;
223  }
224 
225  if(portConnected[port] == 1)
226  {
227  s32 ret = padRead(port, 0, &buttons);
228 
229  if(ret != 0)
230  {
231  paddata[port] = 0xffff ^ buttons.btns;
232 
234  old_pad[port] = paddata[port];
235 
236  // Values 50 and 200 used because my controllers are worn out :-)
237  if((buttons.ljoy_h <= 50) || (buttons.ljoy_h >= 200)) printf("Left Analog X: %i\n", (int)buttons.ljoy_h);
238  if((buttons.ljoy_v <= 50) || (buttons.ljoy_v >= 200)) printf("Left Analog Y: %i\n", (int)buttons.ljoy_v);
239  if((buttons.rjoy_h <= 50) || (buttons.rjoy_h >= 200)) printf("Right Analog X: %i\n", (int)buttons.rjoy_h);
240  if((buttons.rjoy_v <= 50) || (buttons.rjoy_v >= 200)) printf("Right Analog Y: %i\n", (int)buttons.rjoy_v);
241 
242 
243  if(new_pad[port]) printf("Controller (%i) button(s) pressed: ", (int)port);
244  if(new_pad[port] & PAD_LEFT) printf("LEFT ");
245  if(new_pad[port] & PAD_RIGHT) printf("RIGHT ");
246  if(new_pad[port] & PAD_UP) printf("UP ");
247  if(new_pad[port] & PAD_DOWN) printf("DOWN ");
248  if(new_pad[port] & PAD_START) printf("START ");
249  if(new_pad[port] & PAD_SELECT) printf("SELECT ");
250  if(new_pad[port] & PAD_SQUARE) printf("SQUARE (Pressure: %i) ", (int)buttons.square_p);
251  if(new_pad[port] & PAD_TRIANGLE) printf("TRIANGLE (Pressure: %i) ", (int)buttons.triangle_p);
252  if(new_pad[port] & PAD_CIRCLE) printf("CIRCLE (Pressure: %i) ", (int)buttons.circle_p);
253  if(new_pad[port] & PAD_CROSS) printf("CROSS (Pressure: %i) ", (int)buttons.cross_p);
254  if(new_pad[port] & PAD_L1)
255  {
256  printf("L1 (Start Little Motor) ");
257  padStartAct(port, 0, 1);
258  }
259  if(new_pad[port] & PAD_L2)
260  {
261  printf("L2 (Stop Little Motor) ");
262  padStartAct(port, 0, 0);
263  }
264  if(new_pad[port] & PAD_L3) printf("L3 ");
265  if(new_pad[port] & PAD_R1)
266  {
267  printf("R1 (Start Big Motor) ");
268  padStartAct(port, 1, 255);
269  }
270  if(new_pad[port] & PAD_R2)
271  {
272  printf("R2 (Stop Big Motor) ");
273  padStopAct(port, 1);
274  }
275  if(new_pad[port] & PAD_R3) printf("R3 ");
276 
277  if(new_pad[port]) printf("\n");
278  }
279 
280  }
281  }
282  wait_vsync();
283  }
284 
285  return 0;
286 }
287 
int SifLoadModule(const char *path, int arg_len, const char *args)
#define GS_REG_CSR
Definition: gs_privileged.h:45
#define PAD_CROSS
Definition: input.h:26
#define PAD_SELECT
Definition: input.h:24
#define PAD_START
Definition: input.h:21
#define PAD_R3
Definition: input.h:22
#define PAD_CIRCLE
Definition: input.h:27
#define PAD_DOWN
Definition: input.h:18
#define PAD_RIGHT
Definition: input.h:19
#define PAD_SQUARE
Definition: input.h:25
#define PAD_TRIANGLE
Definition: input.h:28
#define PAD_L2
Definition: input.h:32
#define PAD_L3
Definition: input.h:23
#define PAD_UP
Definition: input.h:20
#define PAD_L1
Definition: input.h:30
#define PAD_LEFT
Definition: input.h:17
#define PAD_R2
Definition: input.h:31
#define PAD_R1
Definition: input.h:29
s32 SleepThread(void)
#define GS_SET_CSR(signal_evnt, finish_evnt, hsync_intrupt, vsync_intrupt, write_terminate, flush, reset, nfield, current_field, fifo_status, gs_rev_number, gs_id)
Definition: libgs.h:1249
s32 port
Definition: libpad.c:176
#define PAD_MODECUREXID
Definition: libpad.h:73
int padPortOpen(int port, int slot, void *padArea)
Definition: libpad.c:392
unsigned char padRead(int port, int slot, struct padButtonStatus *data)
Definition: libpad.c:470
int padInfoMode(int port, int slot, int infoMode, int index)
Definition: libpad.c:588
#define PAD_MODECUROFFS
Definition: libpad.h:74
#define PAD_MMODE_DUALSHOCK
Definition: libpad.h:81
int padSetActAlign(int port, int slot, char act_align[6])
Definition: libpad.c:777
int padSetActDirect(int port, int slot, char act_align[6])
Definition: libpad.c:800
int padInit(int mode)
Definition: libpad.c:297
#define PAD_STATE_STABLE
Definition: libpad.h:47
int padGetState(int port, int slot)
Definition: libpad.c:482
#define PAD_MODECURID
Definition: libpad.h:72
unsigned char padInfoAct(int port, int slot, int word, int byte)
Definition: libpad.c:738
unsigned char padGetReqState(int port, int slot)
Definition: libpad.c:511
int padInfoPressMode(int port, int slot)
Definition: libpad.c:675
#define PAD_MMODE_LOCK
Definition: libpad.h:84
#define PAD_RSTAT_COMPLETE
Definition: libpad.h:53
#define PAD_STATE_DISCONN
Definition: libpad.h:43
int padSetMainMode(int port, int slot, int mode, int lock)
Definition: libpad.c:656
int padEnterPressMode(int port, int slot)
Definition: libpad.c:690
#define PAD_MODETABLE
Definition: libpad.h:75
static char actAlign[6]
Definition: pad.c:30
static u32 new_pad[2]
Definition: padx.c:20
static void padStopAct(int port, int act)
Definition: padx.c:91
static char * padTypeStr[]
Definition: padx.c:12
static void padWait(int port)
Definition: padx.c:69
int main(int argc, char **argv)
Definition: padx.c:96
static u32 portConnected[2]
Definition: padx.c:17
static void wait_vsync()
Definition: padx.c:23
static char * padBuf[2]
Definition: padx.c:16
static u8 actDirect[2][6]
Definition: padx.c:21
static u32 old_pad[2]
Definition: padx.c:19
static void padStartAct(int port, int act, int speed)
Definition: padx.c:80
static void loadmodules(int free)
Definition: padx.c:35
static u32 paddata[2]
Definition: padx.c:18
void SifInitRpc(int mode)
unsigned short btns
Definition: libpad.h:99
unsigned char circle_p
Definition: libpad.h:111
unsigned char square_p
Definition: libpad.h:113
unsigned char rjoy_v
Definition: libpad.h:102
unsigned char triangle_p
Definition: libpad.h:110
unsigned char cross_p
Definition: libpad.h:112
unsigned char ljoy_h
Definition: libpad.h:103
unsigned char ljoy_v
Definition: libpad.h:104
unsigned char rjoy_h
Definition: libpad.h:101
#define NULL
Definition: tamtypes.h:91
signed int s32
Definition: tamtypes.h:58
unsigned int u32
Definition: tamtypes.h:30
unsigned char u8
Definition: tamtypes.h:23