1 /* faking/emulating gp2x.c by using gtk */
10 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <sys/soundcard.h>
17 #include "../gp2x/emu.h"
18 #include "../gp2x/gp2x.h"
19 #include "../gp2x/usbjoy.h"
20 #include "../gp2x/version.h"
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;
32 char *ext_menu = 0, *ext_state = 0;
33 int mix_32_to_16l_level;
36 struct gtk_global_struct
43 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
48 static void destroy (GtkWidget *widget, gpointer data)
53 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
55 switch (event->hardware_keycode)
57 case 0x62: current_keys |= GP2X_UP; break;
58 case 0x68: current_keys |= GP2X_DOWN; break;
59 case 0x64: current_keys |= GP2X_LEFT; break;
60 case 0x66: current_keys |= GP2X_RIGHT; break;
61 case 0x24: current_keys |= GP2X_START; break; // enter
62 case 0x23: current_keys |= GP2X_SELECT;break; // ]
63 case 0x34: current_keys |= GP2X_A; break; // z
64 case 0x35: current_keys |= GP2X_X; break; // x
65 case 0x36: current_keys |= GP2X_B; break; // c
66 case 0x37: current_keys |= GP2X_Y; break; // v
67 case 0x27: current_keys |= GP2X_L; break; // s
68 case 0x28: current_keys |= GP2X_R; break; // d
69 case 0x29: current_keys |= GP2X_PUSH; break; // f
70 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
71 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
72 case 0x2d: log_io_clear(); break; // k
73 case 0x2e: log_io_dump(); break; // l
75 extern int PicoReset(void);
84 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
86 switch (event->hardware_keycode)
88 case 0x62: current_keys &= ~GP2X_UP; break;
89 case 0x68: current_keys &= ~GP2X_DOWN; break;
90 case 0x64: current_keys &= ~GP2X_LEFT; break;
91 case 0x66: current_keys &= ~GP2X_RIGHT; break;
92 case 0x24: current_keys &= ~GP2X_START; break; // enter
93 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
94 case 0x34: current_keys &= ~GP2X_A; break; // z
95 case 0x35: current_keys &= ~GP2X_X; break; // x
96 case 0x36: current_keys &= ~GP2X_B; break; // c
97 case 0x37: current_keys &= ~GP2X_Y; break; // v
98 case 0x27: current_keys &= ~GP2X_L; break; // s
99 case 0x28: current_keys &= ~GP2X_R; break; // d
100 case 0x29: current_keys &= ~GP2X_PUSH; break; // f
101 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
102 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
108 static void *gtk_threadf(void *targ)
111 char *argv[] = { "" };
115 g_thread_init (NULL);
118 gtk_init (&argc, (char ***) &argv);
120 /* create new window */
121 gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
122 g_signal_connect (G_OBJECT (gtk_items.window), "delete_event",
123 G_CALLBACK (delete_event), NULL);
125 g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
126 G_CALLBACK (destroy), NULL);
128 g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
129 G_CALLBACK (key_press_event), NULL);
131 g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
132 G_CALLBACK (key_release_event), NULL);
134 gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 2);
135 gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
137 box = gtk_hbox_new(FALSE, 0);
138 gtk_widget_show(box);
139 gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
142 gtk_items.pixmap1 = gtk_image_new ();
143 gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1);
144 gtk_widget_show (gtk_items.pixmap1);
145 gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240);
147 gtk_widget_show (gtk_items.window);
153 printf("linux: gtk thread finishing\n");
159 static void gtk_initf(void)
161 pthread_t gtk_thread;
163 sem_init(&sem, 0, 0);
165 pthread_create(>k_thread, NULL, gtk_threadf, &sem);
166 pthread_detach(gtk_thread);
172 void finalize_image(guchar *pixels, gpointer data)
181 printf("entering init()\n"); fflush(stdout);
183 gp2x_screen = malloc(320*240*2 + 320*2);
184 memset(gp2x_screen, 0, 320*240*2 + 320*2);
187 mixerdev = open("/dev/mixer", O_RDWR);
189 printf("open(\"/dev/mixer\") failed with %i\n", errno);
195 printf("exitting init()\n"); fflush(stdout);
198 void gp2x_deinit(void)
201 if (sounddev > 0) close(sounddev);
203 gp2x_usbjoy_deinit();
207 void gp2x_video_flip(void)
210 unsigned char *image;
215 image = malloc (320*240*3);
222 if (current_bpp == 8)
224 unsigned char *pixels = gp2x_screen;
227 for (i = 0; i < 320*240; i++)
229 pix = current_pal[pixels[i]];
230 image[3 * i + 0] = pix >> 16;
231 image[3 * i + 1] = pix >> 8;
232 image[3 * i + 2] = pix;
237 unsigned short *pixels = gp2x_screen;
239 for (i = 0; i < 320*240; i++)
241 /* in: rrrr rggg gggb bbbb */
242 /* out: rrrr r000 gggg gg00 bbbb b000 */
243 image[3 * i + 0] = (pixels[i] >> 8) & 0xf8;
244 image[3 * i + 1] = (pixels[i] >> 3) & 0xfc;
245 image[3 * i + 2] = (pixels[i] << 3);
249 pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
250 FALSE, 8, 320, 240, 320*3, finalize_image, NULL);
251 gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf);
252 g_object_unref (pixbuf);
257 void gp2x_video_flip2(void)
262 void gp2x_video_changemode(int bpp)
267 void gp2x_video_changemode2(int bpp)
272 void gp2x_video_setpalette(int *pal, int len)
274 memcpy(current_pal, pal, len*4);
277 void gp2x_video_flush_cache(void)
281 void gp2x_video_RGB_setscaling(int v_offs, int W, int H)
285 void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
287 if ((char *)gp2x_screen + offset != data)
288 memcpy((char *)gp2x_screen + offset, data, len);
291 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
293 memcpy((char *)gp2x_screen + offset, data, len);
297 void gp2x_memset_all_buffers(int offset, int byte, int len)
299 memset((char *)gp2x_screen + offset, byte, len);
302 void gp2x_pd_clone_buffer2(void)
304 memset(gp2x_screen, 0, 320*240*2);
308 static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
310 void gp2x_start_sound(int rate, int bits, int stereo)
312 int frag = 0, bsize, buffers;
314 // if no settings change, we don't need to do anything
315 if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
317 if (sounddev > 0) close(sounddev);
318 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
320 printf("open(\"/dev/dsp\") failed with %i\n", errno);
322 ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
323 ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
324 ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
325 // calculate buffer size
328 if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
329 while ((bsize>>=1)) frag++;
330 frag |= buffers<<16; // 16 buffers
331 ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
332 printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
333 rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
335 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
338 void gp2x_sound_write(void *buff, int len)
340 write(sounddev, buff, len);
343 void gp2x_sound_sync(void)
345 ioctl(sounddev, SOUND_PCM_SYNC, 0);
348 void gp2x_sound_volume(int l, int r)
350 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
352 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
356 unsigned long gp2x_joystick_read(int allow_usb_joy)
358 unsigned long value = current_keys;
361 if (allow_usb_joy && num_of_joys > 0) {
362 // check the usb joy as well..
363 gp2x_usbjoy_update();
364 for (i = 0; i < num_of_joys; i++)
365 value |= gp2x_usbjoy_check(i);
371 int gp2x_touchpad_read(int *x, int *y)
378 void Pause940(int yes)
382 void Reset940(int yes, int bank)
386 /* faking gp2x cpuctrl.c */
387 void cpuctrl_init(void)
391 void cpuctrl_deinit(void)
395 void set_FCLK(unsigned MHZ)
399 void Disable_940(void)
403 void gp2x_video_wait_vsync(void)
407 void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
411 void set_gamma(int g100, int A_SNs_curve)
415 void set_LCD_custom_rate(int rate)
419 void unset_LCD_custom_rate(void)
437 void spend_cycles(int c)
443 void lprintf(const char *fmt, ...)