cc68a136 |
1 | /* faking/emulating gp2x.c by using gtk */ |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | #include <pthread.h> |
6 | #include <gtk/gtk.h> |
7 | |
8 | #include <unistd.h> |
9 | #include <sys/types.h> |
10 | #include <sys/stat.h> |
11 | #include <sys/ioctl.h> |
12 | #include <sys/soundcard.h> |
13 | #include <fcntl.h> |
14 | #include <errno.h> |
15 | |
16 | #include "../gp2x/emu.h" |
17 | #include "../gp2x/gp2x.h" |
18 | #include "../gp2x/usbjoy.h" |
19 | #include "../gp2x/version.h" |
20 | |
21 | void *gp2x_screen; |
22 | static int current_bpp = 8; |
23 | static int current_pal[256]; |
24 | static unsigned long current_keys = 0; |
25 | static int sounddev = 0, mixerdev = 0; |
26 | static const char *verstring = "PicoDrive " VERSION; |
27 | |
28 | // dummies |
29 | char *ext_menu = 0, *ext_state = 0; |
30 | |
31 | /* gtk */ |
32 | struct gtk_global_struct |
33 | { |
34 | GtkWidget *window; |
35 | GtkWidget *pixmap1; |
36 | } gtk_items; |
37 | |
38 | |
39 | static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data) |
40 | { |
41 | return FALSE; |
42 | } |
43 | |
44 | static void destroy (GtkWidget *widget, gpointer data) |
45 | { |
46 | gtk_main_quit (); |
47 | } |
48 | |
49 | static gint key_press_event (GtkWidget *widget, GdkEventKey *event) |
50 | { |
51 | switch (event->hardware_keycode) |
52 | { |
53 | case 0x62: current_keys |= GP2X_UP; break; |
54 | case 0x68: current_keys |= GP2X_DOWN; break; |
55 | case 0x64: current_keys |= GP2X_LEFT; break; |
56 | case 0x66: current_keys |= GP2X_RIGHT; break; |
57 | case 0x24: current_keys |= GP2X_START; break; // enter |
58 | case 0x23: current_keys |= GP2X_SELECT;break; // ] |
59 | case 0x34: current_keys |= GP2X_A; break; // z |
60 | case 0x35: current_keys |= GP2X_X; break; // x |
61 | case 0x36: current_keys |= GP2X_B; break; // c |
62 | case 0x37: current_keys |= GP2X_Y; break; // v |
63 | case 0x27: current_keys |= GP2X_L; break; // s |
64 | case 0x28: current_keys |= GP2X_R; break; // d |
65 | case 0x29: current_keys |= GP2X_PUSH; break; // f |
66 | case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q |
67 | case 0x19: current_keys |= GP2X_VOL_UP;break; // w |
68 | } |
69 | |
70 | return 0; |
71 | } |
72 | |
73 | static gint key_release_event (GtkWidget *widget, GdkEventKey *event) |
74 | { |
75 | switch (event->hardware_keycode) |
76 | { |
77 | case 0x62: current_keys &= ~GP2X_UP; break; |
78 | case 0x68: current_keys &= ~GP2X_DOWN; break; |
79 | case 0x64: current_keys &= ~GP2X_LEFT; break; |
80 | case 0x66: current_keys &= ~GP2X_RIGHT; break; |
81 | case 0x24: current_keys &= ~GP2X_START; break; // enter |
82 | case 0x23: current_keys &= ~GP2X_SELECT;break; // ] |
83 | case 0x34: current_keys &= ~GP2X_A; break; // z |
84 | case 0x35: current_keys &= ~GP2X_X; break; // x |
85 | case 0x36: current_keys &= ~GP2X_B; break; // c |
86 | case 0x37: current_keys &= ~GP2X_Y; break; // v |
87 | case 0x27: current_keys &= ~GP2X_L; break; // s |
88 | case 0x28: current_keys &= ~GP2X_R; break; // d |
89 | case 0x29: current_keys &= ~GP2X_PUSH; break; // f |
90 | case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q |
91 | case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w |
92 | } |
93 | |
94 | return 0; |
95 | } |
96 | |
97 | static void *gtk_threadf(void *none) |
98 | { |
99 | gtk_main(); |
100 | |
101 | printf("linux: gtk thread finishing\n"); |
102 | engineState = PGS_Quit; |
103 | |
104 | return NULL; |
105 | } |
106 | |
107 | static void gtk_initf(void) |
108 | { |
109 | int argc = 0; |
110 | char *argv[] = { "" }; |
111 | GtkWidget *box; |
112 | pthread_t gtk_thread; |
113 | |
114 | g_thread_init (NULL); |
115 | gdk_threads_init (); |
116 | gdk_set_locale (); |
117 | gtk_init (&argc, (char ***) &argv); |
118 | |
119 | /* create new window */ |
120 | gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
121 | g_signal_connect (G_OBJECT (gtk_items.window), "delete_event", |
122 | G_CALLBACK (delete_event), NULL); |
123 | |
124 | g_signal_connect (G_OBJECT (gtk_items.window), "destroy", |
125 | G_CALLBACK (destroy), NULL); |
126 | |
127 | g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event", |
128 | G_CALLBACK (key_press_event), NULL); |
129 | |
130 | g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event", |
131 | G_CALLBACK (key_release_event), NULL); |
132 | |
133 | gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 2); |
134 | gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring); |
135 | |
136 | box = gtk_hbox_new(FALSE, 0); |
137 | gtk_widget_show(box); |
138 | gtk_container_add (GTK_CONTAINER (gtk_items.window), box); |
139 | |
140 | /* live pixmap */ |
141 | gtk_items.pixmap1 = gtk_image_new (); |
142 | gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1); |
143 | gtk_widget_show (gtk_items.pixmap1); |
144 | gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240); |
145 | |
146 | gtk_widget_show (gtk_items.window); |
147 | |
148 | // pthread_mutex_init (&thr_mutex, NULL); |
149 | // pthread_mutex_lock (&thr_mutex); |
150 | // pthread_mutex_init (&scanner_muttex, NULL); |
151 | |
152 | pthread_create(>k_thread, NULL, gtk_threadf, NULL); |
153 | } |
154 | |
155 | void finalize_image(guchar *pixels, gpointer data) |
156 | { |
157 | free(pixels); |
158 | } |
159 | |
160 | /* --- */ |
161 | |
162 | void gp2x_init(void) |
163 | { |
164 | printf("entering init()\n"); fflush(stdout); |
165 | |
166 | gp2x_screen = malloc(320*240*2 + 320*2); |
167 | |
168 | // snd |
169 | mixerdev = open("/dev/mixer", O_RDWR); |
170 | if (mixerdev == -1) |
171 | printf("open(\"/dev/mixer\") failed with %i\n", errno); |
172 | |
173 | gtk_initf(); |
174 | |
175 | gp2x_usbjoy_init(); |
176 | |
177 | printf("exitting init()\n"); fflush(stdout); |
178 | } |
179 | |
180 | void gp2x_deinit(void) |
181 | { |
182 | free(gp2x_screen); |
183 | if (sounddev > 0) close(sounddev); |
184 | close(mixerdev); |
185 | gp2x_usbjoy_deinit(); |
186 | } |
187 | |
188 | /* video */ |
189 | void gp2x_video_flip(void) |
190 | { |
191 | GdkPixbuf *pixbuf; |
192 | unsigned char *image; |
193 | int i; |
194 | |
195 | gdk_threads_enter(); |
196 | |
197 | image = malloc (320*240*3); |
198 | if (image == NULL) |
199 | { |
200 | gdk_threads_leave(); |
201 | return; |
202 | } |
203 | |
204 | if (current_bpp == 8) |
205 | { |
206 | unsigned char *pixels = gp2x_screen; |
207 | int pix; |
208 | |
209 | for (i = 0; i < 320*240; i++) |
210 | { |
211 | pix = current_pal[pixels[i]]; |
212 | image[3 * i + 0] = pix >> 16; |
213 | image[3 * i + 1] = pix >> 8; |
214 | image[3 * i + 2] = pix; |
215 | } |
216 | } |
217 | else |
218 | { |
219 | unsigned short *pixels = gp2x_screen; |
220 | |
221 | for (i = 0; i < 320*240; i++) |
222 | { |
223 | /* in: rrrr rggg gggb bbbb */ |
224 | /* out: rrrr r000 gggg gg00 bbbb b000 */ |
225 | image[3 * i + 0] = (pixels[i] >> 8) & 0xf8; |
226 | image[3 * i + 1] = (pixels[i] >> 3) & 0xfc; |
227 | image[3 * i + 2] = (pixels[i] << 3); |
228 | } |
229 | } |
230 | |
231 | pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB, |
232 | FALSE, 8, 320, 240, 320*3, finalize_image, NULL); |
233 | gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf); |
234 | g_object_unref (pixbuf); |
235 | |
236 | gdk_threads_leave(); |
237 | } |
238 | |
239 | void gp2x_video_changemode(int bpp) |
240 | { |
241 | current_bpp = bpp; |
242 | } |
243 | |
244 | void gp2x_video_setpalette(int *pal, int len) |
245 | { |
246 | memcpy(current_pal, pal, len*4); |
247 | } |
248 | |
249 | void gp2x_video_RGB_setscaling(int W, int H) |
250 | { |
251 | } |
252 | |
253 | void gp2x_memcpy_all_buffers(void *data, int offset, int len) |
254 | { |
255 | memcpy((char *)gp2x_screen + offset, data, len); |
256 | } |
257 | |
258 | |
259 | void gp2x_memset_all_buffers(int offset, int byte, int len) |
260 | { |
261 | memset((char *)gp2x_screen + offset, byte, len); |
262 | } |
263 | |
264 | |
265 | /* sound */ |
266 | static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0; |
267 | |
268 | void gp2x_start_sound(int rate, int bits, int stereo) |
269 | { |
270 | int frag = 0, bsize, buffers; |
271 | |
272 | // if no settings change, we don't need to do anything |
273 | if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return; |
274 | |
275 | if (sounddev > 0) close(sounddev); |
276 | sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC); |
277 | if (sounddev == -1) |
278 | printf("open(\"/dev/dsp\") failed with %i\n", errno); |
279 | |
280 | ioctl(sounddev, SNDCTL_DSP_SPEED, &rate); |
281 | ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits); |
282 | ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo); |
283 | // calculate buffer size |
284 | buffers = 16; |
285 | bsize = rate / 32; |
286 | if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding |
287 | while ((bsize>>=1)) frag++; |
288 | frag |= buffers<<16; // 16 buffers |
289 | ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag); |
290 | printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n", |
291 | rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff)); |
292 | |
293 | s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo; |
294 | } |
295 | |
296 | void gp2x_sound_write(void *buff, int len) |
297 | { |
298 | write(sounddev, buff, len); |
299 | } |
300 | |
cb0316e4 |
301 | void gp2x_sound_sync(void) |
302 | { |
303 | ioctl(sounddev, SOUND_PCM_SYNC, 0); |
304 | } |
305 | |
cc68a136 |
306 | void gp2x_sound_volume(int l, int r) |
307 | { |
308 | l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r; |
309 | l<<=8; l|=r; |
310 | ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/ |
311 | } |
312 | |
313 | /* joy */ |
314 | unsigned long gp2x_joystick_read(int allow_usb_joy) |
315 | { |
316 | unsigned long value = current_keys; |
317 | int i; |
318 | |
319 | if (allow_usb_joy && num_of_joys > 0) { |
320 | // check the usb joy as well.. |
321 | gp2x_usbjoy_update(); |
322 | for (i = 0; i < num_of_joys; i++) |
323 | value |= gp2x_usbjoy_check(i); |
324 | } |
325 | |
326 | return value; |
327 | } |
328 | |
329 | /* 940 */ |
330 | int crashed_940 = 0; |
331 | void Pause940(int yes) |
332 | { |
333 | } |
334 | |
b837b69b |
335 | void Reset940(int yes, int bank) |
cc68a136 |
336 | { |
337 | } |
338 | |
339 | /* faking gp2x cpuctrl.c */ |
340 | void cpuctrl_init(void) |
341 | { |
342 | } |
343 | |
344 | void cpuctrl_deinit(void) |
345 | { |
346 | } |
347 | |
348 | void set_FCLK(unsigned MHZ) |
349 | { |
350 | } |
351 | |
352 | void Disable_940(void) |
353 | { |
354 | } |
355 | |
356 | void gp2x_video_wait_vsync(void) |
357 | { |
358 | } |
359 | |
360 | void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD) |
361 | { |
362 | } |
363 | |
364 | void set_gamma(int g100) |
365 | { |
366 | } |
367 | |
368 | |
369 | /* squidgehack.c */ |
370 | int mmuhack(void) |
371 | { |
372 | return 0; |
373 | } |
374 | |
375 | |
376 | int mmuunhack(void) |
377 | { |
378 | return 0; |
379 | } |
380 | |
381 | |
382 | /* misc */ |
383 | void spend_cycles(int c) |
384 | { |
385 | usleep(c/*/200*/); |
386 | } |
387 | |
388 | |
389 | |