1 /* faking/emulating gp2x.c by using gtk */
11 #include <sys/ioctl.h>
12 #include <sys/soundcard.h>
16 #include "../gp2x/emu.h"
17 #include "../gp2x/gp2x.h"
18 #include "../gp2x/usbjoy.h"
19 #include "../gp2x/version.h"
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;
29 char *ext_menu = 0, *ext_state = 0;
32 struct gtk_global_struct
39 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
44 static void destroy (GtkWidget *widget, gpointer data)
49 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
51 switch (event->hardware_keycode)
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
73 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
75 switch (event->hardware_keycode)
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
97 static void *gtk_threadf(void *none)
101 printf("linux: gtk thread finishing\n");
102 engineState = PGS_Quit;
107 static void gtk_initf(void)
110 char *argv[] = { "" };
112 pthread_t gtk_thread;
114 g_thread_init (NULL);
117 gtk_init (&argc, (char ***) &argv);
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);
124 g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
125 G_CALLBACK (destroy), NULL);
127 g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
128 G_CALLBACK (key_press_event), NULL);
130 g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
131 G_CALLBACK (key_release_event), NULL);
133 gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 2);
134 gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
136 box = gtk_hbox_new(FALSE, 0);
137 gtk_widget_show(box);
138 gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
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);
146 gtk_widget_show (gtk_items.window);
148 // pthread_mutex_init (&thr_mutex, NULL);
149 // pthread_mutex_lock (&thr_mutex);
150 // pthread_mutex_init (&scanner_muttex, NULL);
152 pthread_create(>k_thread, NULL, gtk_threadf, NULL);
155 void finalize_image(guchar *pixels, gpointer data)
164 printf("entering init()\n"); fflush(stdout);
166 gp2x_screen = malloc(320*240*2 + 320*2);
169 mixerdev = open("/dev/mixer", O_RDWR);
171 printf("open(\"/dev/mixer\") failed with %i\n", errno);
177 printf("exitting init()\n"); fflush(stdout);
180 void gp2x_deinit(void)
183 if (sounddev > 0) close(sounddev);
185 gp2x_usbjoy_deinit();
189 void gp2x_video_flip(void)
192 unsigned char *image;
197 image = malloc (320*240*3);
204 if (current_bpp == 8)
206 unsigned char *pixels = gp2x_screen;
209 for (i = 0; i < 320*240; i++)
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;
219 unsigned short *pixels = gp2x_screen;
221 for (i = 0; i < 320*240; i++)
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);
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);
239 void gp2x_video_changemode(int bpp)
244 void gp2x_video_setpalette(int *pal, int len)
246 memcpy(current_pal, pal, len*4);
249 void gp2x_video_RGB_setscaling(int W, int H)
253 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
255 memcpy((char *)gp2x_screen + offset, data, len);
259 void gp2x_memset_all_buffers(int offset, int byte, int len)
261 memset((char *)gp2x_screen + offset, byte, len);
266 static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
268 void gp2x_start_sound(int rate, int bits, int stereo)
270 int frag = 0, bsize, buffers;
272 // if no settings change, we don't need to do anything
273 if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
275 if (sounddev > 0) close(sounddev);
276 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
278 printf("open(\"/dev/dsp\") failed with %i\n", errno);
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
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));
293 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
296 void gp2x_sound_write(void *buff, int len)
298 write(sounddev, buff, len);
301 void gp2x_sound_sync(void)
303 ioctl(sounddev, SOUND_PCM_SYNC, 0);
306 void gp2x_sound_volume(int l, int r)
308 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
310 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
314 unsigned long gp2x_joystick_read(int allow_usb_joy)
316 unsigned long value = current_keys;
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);
331 void Pause940(int yes)
335 void Reset940(int yes, int bank)
339 /* faking gp2x cpuctrl.c */
340 void cpuctrl_init(void)
344 void cpuctrl_deinit(void)
348 void set_FCLK(unsigned MHZ)
352 void Disable_940(void)
356 void gp2x_video_wait_vsync(void)
360 void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
364 void set_gamma(int g100)
383 void spend_cycles(int c)