-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgp2x_psp.c
238 lines (211 loc) · 5.89 KB
/
gp2x_psp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_events.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "global.h"
#include "gp2x_psp.h"
/* For internal use only */
# define GP2X_CTRL_UPRIGHT 0x10000
# define GP2X_CTRL_UPLEFT 0x20000
# define GP2X_CTRL_DOWNRIGHT 0x40000
# define GP2X_CTRL_DOWNLEFT 0x80000
static int loc_Volume = 50;
static int loc_LastEventMask = 0;
static int loc_CurrEventMask = 0;
static int loc_CurrEventButtons = 0;
static u32 loc_LastTimeStamp = 0;
static u32 loc_CurrTimeStamp = 0;
static int loc_VolumeButtons = 0;
static int loc_VolumePress = 0;
static u32 loc_LastVolumeTimeStamp = 0;
# define GP2X_MIN_TIME_VOLUME 300000
# define GP2X_MIN_TIME_REPEAT 100000
static inline int
gp2xConvertMaskToButtons(int Mask)
{
int Buttons = Mask & GP2X_CTRL_MASK;
if (Mask & GP2X_CTRL_UPLEFT ) Buttons |= GP2X_CTRL_UP|GP2X_CTRL_LEFT;
if (Mask & GP2X_CTRL_UPRIGHT) Buttons |= GP2X_CTRL_UP|GP2X_CTRL_RIGHT;
if (Mask & GP2X_CTRL_DOWNLEFT ) Buttons |= GP2X_CTRL_DOWN|GP2X_CTRL_LEFT;
if (Mask & GP2X_CTRL_DOWNRIGHT) Buttons |= GP2X_CTRL_DOWN|GP2X_CTRL_RIGHT;
return Buttons;
}
void
gp2xTreatVolume(gp2xCtrlData* c)
{
if (c->Buttons & GP2X_CTRL_VOLDOWN) {
/* Already down ? */
if (! (loc_VolumeButtons & GP2X_CTRL_VOLDOWN)) {
loc_LastVolumeTimeStamp = loc_CurrTimeStamp;
loc_VolumeButtons |= GP2X_CTRL_VOLDOWN;
loc_VolumePress = 1;
gp2xDecreaseVolume();
} else
if ((((loc_CurrTimeStamp - loc_LastVolumeTimeStamp) > GP2X_MIN_TIME_VOLUME) && (loc_VolumePress == 1)) ||
(((loc_CurrTimeStamp - loc_LastVolumeTimeStamp) > GP2X_MIN_TIME_REPEAT) && (loc_VolumePress > 1))) {
loc_LastVolumeTimeStamp = loc_CurrTimeStamp;
loc_VolumePress++;
gp2xDecreaseVolume();
}
} else
if (c->Buttons & GP2X_CTRL_VOLUP) {
/* Already down ? */
if (! (loc_VolumeButtons & GP2X_CTRL_VOLUP)) {
loc_LastVolumeTimeStamp = loc_CurrTimeStamp;
loc_VolumeButtons |= GP2X_CTRL_VOLUP;
loc_VolumePress = 1;
gp2xIncreaseVolume();
} else
if ((((loc_CurrTimeStamp - loc_LastVolumeTimeStamp) > GP2X_MIN_TIME_VOLUME) && (loc_VolumePress == 1)) ||
(((loc_CurrTimeStamp - loc_LastVolumeTimeStamp) > GP2X_MIN_TIME_REPEAT) && (loc_VolumePress > 1))) {
loc_LastVolumeTimeStamp = loc_CurrTimeStamp;
loc_VolumePress++;
gp2xIncreaseVolume();
}
} else {
loc_VolumeButtons = 0;
}
}
int
gp2xCtrlPeekBufferPositive(gp2xCtrlData* c, int v)
{
SDL_Event SDLEvent;
int Event = 0;
int ButtonPress = 0;
int ButtonRelease = 0;
int Mask = 0;
memset(c, 0x0, sizeof(gp2xCtrlData));
loc_CurrTimeStamp = SDL_GetTicks() * 1000;
if (SDL_PollEvent(&SDLEvent)) {
#if defined(WIZ_MODE) || defined(GP2X_MODE)
Event = SDLEvent.jbutton.button;
if (SDLEvent.type==SDL_JOYBUTTONDOWN) ButtonPress = 1;
else
if (SDLEvent.type==SDL_JOYBUTTONUP) ButtonRelease = 1;
#else
Event=((SDL_KeyboardEvent*)(&SDLEvent))->keysym.scancode;
if (SDLEvent.type==SDL_KEYDOWN) {
ButtonPress = 1;
} else
if (SDLEvent.type==SDL_KEYUP) {
ButtonRelease = 1;
}
#endif
switch (Event) {
case GP2X_UP : Mask = GP2X_CTRL_UP;
break;
case GP2X_DOWN : Mask = GP2X_CTRL_DOWN;
break;
case GP2X_LEFT : Mask = GP2X_CTRL_LEFT;
break;
case GP2X_RIGHT : Mask = GP2X_CTRL_RIGHT;
break;
case GP2X_UPLEFT : Mask = GP2X_CTRL_UPLEFT;
break;
case GP2X_UPRIGHT : Mask = GP2X_CTRL_UPRIGHT;
break;
case GP2X_DOWNLEFT : Mask = GP2X_CTRL_DOWNLEFT;
break;
case GP2X_DOWNRIGHT : Mask = GP2X_CTRL_DOWNRIGHT;
break;
case GP2X_A : Mask = GP2X_CTRL_SQUARE;
break;
case GP2X_B : Mask = GP2X_CTRL_CIRCLE;
break;
case GP2X_X : Mask = GP2X_CTRL_CROSS;
break;
case GP2X_Y : Mask = GP2X_CTRL_TRIANGLE;
break;
case GP2X_L : Mask = GP2X_CTRL_LTRIGGER;
break;
case GP2X_R : Mask = GP2X_CTRL_RTRIGGER;
break;
case GP2X_FIRE : Mask = GP2X_CTRL_FIRE;
break;
case GP2X_START : Mask = GP2X_CTRL_START;
break;
case GP2X_SELECT : Mask = GP2X_CTRL_SELECT;
break;
case GP2X_VOLUP : Mask = GP2X_CTRL_VOLUP;
break;
case GP2X_VOLDOWN : Mask = GP2X_CTRL_VOLDOWN;
break;
}
loc_LastEventMask = loc_CurrEventMask;
if (ButtonPress) {
loc_CurrEventMask |= Mask;
} else
if (ButtonRelease) {
loc_CurrEventMask &= ~Mask;
}
loc_CurrEventButtons = gp2xConvertMaskToButtons(loc_CurrEventMask);
c->Buttons = loc_CurrEventButtons;
c->TimeStamp = loc_CurrTimeStamp;
loc_LastTimeStamp = loc_CurrTimeStamp;
} else {
c->Buttons = loc_CurrEventButtons;
c->TimeStamp = loc_CurrTimeStamp;
}
gp2xTreatVolume(c);
return (c->Buttons != 0);
}
int
gp2xCtrlReadBufferPositive(gp2xCtrlData* c, int v)
{
while (! gp2xCtrlPeekBufferPositive(c, v));
return 1;
}
extern void set_speed_clock(int freq);
void
gp2xPowerSetClockFrequency(int freq)
{
#if defined(WIZ_MODE) || defined(GP2X_MODE)
if ((freq >= 33) && (freq <= 266)) {
set_speed_clock(freq);
}
# endif
}
int
gp2xGetSoundVolume()
{
return loc_Volume;
}
void
gp2xDecreaseVolume()
{
loc_Volume -= 2;
if (loc_Volume < 0) loc_Volume = 0;
}
void
gp2xIncreaseVolume()
{
loc_Volume += 2;
if (loc_Volume > 100) loc_Volume = 100;
}
int
gp2xInsmodMMUhack(void)
{
# ifdef GP2X_MMU_HACK
int mmufd = open("/dev/mmuhack", O_RDWR);
if(mmufd < 0) {
system("/sbin/insmod ./drivers/mmuhack.o");
mmufd = open("/dev/mmuhack", O_RDWR);
}
if(mmufd < 0) return 0;
close(mmufd);
# endif
return 1;
}
int
gp2xRmmodMMUhack(void)
{
# ifdef GP2X_MMU_HACK
system("/sbin/rmmod mmuhack");
# endif
return 0;
}