PS2SDK
PS2 Homebrew Libraries
libmouse.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #include <stdio.h>
17 #include <tamtypes.h>
18 #include <sifrpc.h>
19 #include <kernel.h>
20 #include <string.h>
21 #include "libmouse.h"
22 
23 static SifRpcClientData_t mouseif __attribute__((aligned(64)));
24 static union {
25  char buffer[128];
26  u32 mode;
27  u32 accel;
28  u32 thres;
29  struct mbounds {
30  s32 minx, maxx;
31  s32 miny, maxy;
32  } bounds;
33  struct {
34  s32 x, y;
35  } pos;
36  u32 data;
37  u32 time;
38 } buffer __attribute__((aligned(64)));
39 static int mouse_init = 0;
40 
41 int PS2MouseInit(void)
42 
43 {
44  if(mouse_init)
45  {
46  printf("PS2Mouse Library already initialised\n");
47  return 0;
48  }
49 
50  mouseif.server = NULL;
51 
52  do {
53  if (sceSifBindRpc(&mouseif, PS2MOUSE_BIND_RPC_ID, 0) < 0) {
54  return -1;
55  }
56  nopdelay();
57  } while(!mouseif.server);
58 
59  mouse_init = 1;
60 
61  return 1;
62 }
63 
65 
66 {
67  u8* uncached = UNCACHED_SEG(buffer.buffer);
68 
69  if(!data)
70  {
71  return -1;
72  }
73 
74  if (sceSifCallRpc(&mouseif, PS2MOUSE_READ, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
75  return -1;
76 
77 
78  memcpy(data, uncached, sizeof(PS2MouseData));
79  // printf("MouseRead %d %d %d %d\n", data->x, data->y, data->wheel, data->buttons);
80 
81  return 1;
82 }
83 
84 int PS2MouseSetReadMode(u32 readMode)
85 
86 {
87  buffer.mode = readMode;
88  if (sceSifCallRpc(&mouseif, PS2MOUSE_SETREADMODE, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
89  return -1;
90 
91  return 1;
92 }
93 
95 
96 {
97  u32 *uncached = (u32 *) UNCACHED_SEG(&buffer.mode);
98 
99  if (sceSifCallRpc(&mouseif, PS2MOUSE_GETREADMODE, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
100  return 0xFF;
101 
102  return uncached[0];
103 }
104 
105 int PS2MouseSetThres(u32 thres)
106 
107 {
108  buffer.thres = thres;
109  if (sceSifCallRpc(&mouseif, PS2MOUSE_SETTHRES, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
110  return -1;
111 
112  return 1;
113 }
114 
115 u32 PS2MouseGetThres()
116 
117 {
118  u32* uncached = (u32 *) UNCACHED_SEG(&buffer.thres);
119  if (sceSifCallRpc(&mouseif, PS2MOUSE_GETTHRES, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
120  return 0;
121 
122  return uncached[0];
123 }
124 
125 int PS2MouseSetAccel(float accel)
126 
127 {
128  u32 accel_fixed;
129 
130  if(accel < 0) return -1;
131 
132  accel_fixed = (u32) (accel * 65536.0);
133  buffer.accel = accel_fixed;
134  if (sceSifCallRpc(&mouseif, PS2MOUSE_SETACCEL, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
135  return -1;
136 
137  return 1;
138 }
139 
140 float PS2MouseGetAccel()
141 
142 {
143  u32* uncached = (u32 *) UNCACHED_SEG(&buffer.accel);
144  u32 accel_fixed;
145 
146  if (sceSifCallRpc(&mouseif, PS2MOUSE_GETACCEL, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
147  return -1;
148 
149  accel_fixed = uncached[0];
150 
151  return ((float) accel_fixed) / 65536.0 ;
152 }
153 
154 int PS2MouseSetBoundary(int minx, int maxx, int miny, int maxy)
155 
156 {
157  buffer.bounds.minx = minx;
158  buffer.bounds.maxx = maxx;
159  buffer.bounds.miny = miny;
160  buffer.bounds.maxy = maxy;
161 
162  if (sceSifCallRpc(&mouseif, PS2MOUSE_SETBOUNDARY, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
163  return -1;
164 
165  return 1;
166 }
167 
168 int PS2MouseGetBoundary(int *minx, int *maxx, int *miny, int *maxy)
169 
170 {
171  struct mbounds* uncached = (struct mbounds *) UNCACHED_SEG(&buffer.bounds);
172 
173  if (sceSifCallRpc(&mouseif, PS2MOUSE_GETBOUNDARY, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
174  return -1;
175 
176  *minx = uncached->minx;
177  *maxx = uncached->maxx;
178  *miny = uncached->miny;
179  *maxy = uncached->maxy;
180 
181  return 1;
182 }
183 
184 int PS2MouseSetPosition(int x, int y)
185 
186 {
187  buffer.pos.x = x;
188  buffer.pos.y = y;
189 
190  if (sceSifCallRpc(&mouseif, PS2MOUSE_SETPOSITION, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
191  return -1;
192 
193  return 1;
194 }
195 
196 int PS2MouseReset()
197 
198 {
199  if (sceSifCallRpc(&mouseif, PS2MOUSE_RESET, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
200  return -1;
201 
202  return 1;
203 }
204 
205 u32 PS2MouseEnum()
206 
207 {
208  u32* uncached = (u32 *) UNCACHED_SEG(&buffer.data);
209 
210  if (sceSifCallRpc(&mouseif, PS2MOUSE_ENUM, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
211  return -1;
212 
213  return uncached[0];
214 }
215 
216 u32 PS2MouseGetVersion()
217 
218 {
219  u32* uncached = (u32 *) UNCACHED_SEG(&buffer.data);
220 
221  if (sceSifCallRpc(&mouseif, PS2MOUSE_GETVERSION, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
222  return -1;
223 
224  return uncached[0];
225 }
226 
227 int PS2MouseSetDblClickTime(u32 msec)
228 
229 {
230  buffer.time = msec;
231 
232  if (sceSifCallRpc(&mouseif, PS2MOUSE_SETDBLCLICKTIME, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
233  return -1;
234 
235  return 1;
236 }
237 
238 u32 PS2MouseGetDblClickTIme()
239 
240 {
241  u32* uncached = (u32 *) UNCACHED_SEG(&buffer.time);
242 
243  if (sceSifCallRpc(&mouseif, PS2MOUSE_GETDBLCLICKTIME, 0, &buffer, 128, &buffer, 128, NULL, NULL) < 0)
244  return -1;
245 
246  return uncached[0];
247 }
248 
kernel.h
PS2MouseSetReadMode
int PS2MouseSetReadMode(u32 readMode)
Definition: libmouse.c:84
PS2MouseRead
int PS2MouseRead(PS2MouseData *data)
Definition: libmouse.c:64
PS2MouseInit
int PS2MouseInit(void)
Definition: libmouse.c:41
PS2MouseGetReadMode
u32 PS2MouseGetReadMode()
Definition: libmouse.c:94
_mouse_data
Definition: ps2mouse.h:21
tamtypes.h
stdio.h
t_SifRpcClientData
Definition: sifrpc-common.h:134
__attribute__
Definition: gif_registers.h:38
mbounds
Definition: libmouse.c:28
libmouse.h