ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
padx.c File Reference
#include <tamtypes.h>
#include <kernel.h>
#include <loadfile.h>
#include <stdio.h>
#include <sifrpc.h>
#include <gs_privileged.h>
#include <libpad.h>
#include <string.h>
+ Include dependency graph for padx.c:

Go to the source code of this file.

Macros

#define DEBUG_BGCOLOR(col)   *((u64 *) 0x120000e0) = (u64) (col)
 

Functions

static void wait_vsync ()
 
static void loadmodules (int free)
 
static void padWait (int port)
 
static void padStartAct (int port, int act, int speed)
 
static void padStopAct (int port, int act)
 
int main (int argc, char **argv)
 

Variables

static char * padTypeStr []
 
static char * padBuf [2]
 
static u32 portConnected [2]
 
static u32 paddata [2]
 
static u32 old_pad [2]
 
static u32 new_pad [2]
 
static u8 actDirect [2][6] = { {0,0,0,0,0,0}, {0,0,0,0,0,0}}
 

Macro Definition Documentation

◆ DEBUG_BGCOLOR

#define DEBUG_BGCOLOR (   col)    *((u64 *) 0x120000e0) = (u64) (col)

Definition at line 10 of file padx.c.

Function Documentation

◆ loadmodules()

static void loadmodules ( int  free)
static

Definition at line 35 of file padx.c.

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 }
int SifLoadModule(const char *path, int arg_len, const char *args)
s32 SleepThread(void)
#define NULL
Definition: tamtypes.h:91
signed int s32
Definition: tamtypes.h:58

References NULL, SifLoadModule(), and SleepThread().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 96 of file padx.c.

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 }
#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 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 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
int padInfoPressMode(int port, int slot)
Definition: libpad.c:675
#define PAD_MMODE_LOCK
Definition: libpad.h:84
#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
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 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 int u32
Definition: tamtypes.h:30
unsigned char u8
Definition: tamtypes.h:23

References actAlign, padButtonStatus::btns, padButtonStatus::circle_p, padButtonStatus::cross_p, padButtonStatus::ljoy_h, padButtonStatus::ljoy_v, loadmodules(), new_pad, old_pad, PAD_CIRCLE, PAD_CROSS, PAD_DOWN, PAD_L1, PAD_L2, PAD_L3, PAD_LEFT, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK, PAD_MODECUREXID, PAD_MODECURID, PAD_MODECUROFFS, PAD_MODETABLE, PAD_R1, PAD_R2, PAD_R3, PAD_RIGHT, PAD_SELECT, PAD_SQUARE, PAD_START, PAD_STATE_DISCONN, PAD_STATE_STABLE, PAD_TRIANGLE, PAD_UP, padBuf, paddata, padEnterPressMode(), padGetState(), padInfoAct(), padInfoMode(), padInfoPressMode(), padInit(), padPortOpen(), padRead(), padSetActAlign(), padSetMainMode(), padStartAct(), padStopAct(), padTypeStr, padWait(), port, portConnected, padButtonStatus::rjoy_h, padButtonStatus::rjoy_v, SifInitRpc(), padButtonStatus::square_p, padButtonStatus::triangle_p, and wait_vsync().

◆ padStartAct()

static void padStartAct ( int  port,
int  act,
int  speed 
)
static

Definition at line 80 of file padx.c.

81 {
82  if(actDirect[port][act] != speed)
83  {
84  actDirect[port][act] = speed;
85 
87  padWait(port);
88  }
89 }
int padSetActDirect(int port, int slot, char act_align[6])
Definition: libpad.c:800
static u8 actDirect[2][6]
Definition: padx.c:21

References actDirect, padSetActDirect(), padWait(), and port.

Referenced by main(), and padStopAct().

◆ padStopAct()

static void padStopAct ( int  port,
int  act 
)
static

Definition at line 91 of file padx.c.

92 {
93  padStartAct(port, act, 0);
94 }

References padStartAct(), and port.

Referenced by main().

◆ padWait()

static void padWait ( int  port)
static

Definition at line 69 of file padx.c.

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 }
unsigned char padGetReqState(int port, int slot)
Definition: libpad.c:511
#define PAD_RSTAT_COMPLETE
Definition: libpad.h:53

References PAD_RSTAT_COMPLETE, PAD_STATE_STABLE, padGetReqState(), padGetState(), port, and wait_vsync().

Referenced by main(), and padStartAct().

◆ wait_vsync()

static void wait_vsync ( void  )
static

Definition at line 23 of file padx.c.

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 }
#define GS_REG_CSR
Definition: gs_privileged.h:45
#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

References GS_REG_CSR, and GS_SET_CSR.

Referenced by main(), and padWait().

Variable Documentation

◆ actDirect

u8 actDirect[2][6] = { {0,0,0,0,0,0}, {0,0,0,0,0,0}}
static

Definition at line 21 of file padx.c.

Referenced by padStartAct().

◆ new_pad

u32 new_pad[2]
static

Definition at line 20 of file padx.c.

Referenced by main().

◆ old_pad

u32 old_pad[2]
static

Definition at line 19 of file padx.c.

Referenced by main().

◆ padBuf

char* padBuf[2]
static

Definition at line 16 of file padx.c.

Referenced by main().

◆ paddata

u32 paddata[2]
static

Definition at line 18 of file padx.c.

Referenced by main().

◆ padTypeStr

char* padTypeStr[]
static
Initial value:
= { "Unsupported controller", "Mouse", "Nejicon",
"Konami Gun", "Digital", "Analog", "Namco Gun",
"DualShock"}

Definition at line 12 of file padx.c.

Referenced by main().

◆ portConnected

u32 portConnected[2]
static

Definition at line 17 of file padx.c.

Referenced by main().