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