1 /* faking/emulating gp2x by using gtk */
10 #include <sys/types.h>
15 #include "../gp2x/version.h"
16 #include "../common/emu.h"
17 #include "../common/menu.h"
18 #include "../common/readpng.h"
19 #include "sndout_oss.h"
23 unsigned long current_keys = 0;
24 static int current_bpp = 8;
25 static int current_pal[256];
26 static const char *verstring = "PicoDrive " VERSION;
27 static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
28 void *gp2x_screens[4];
31 int mix_32_to_16l_level;
33 int default_cpu_clock = 123;
34 void *gp2x_memregs = NULL;
37 struct gtk_global_struct
44 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
49 static void destroy (GtkWidget *widget, gpointer data)
55 enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40,
56 GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11,
57 GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15,
58 GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
60 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
62 switch (event->hardware_keycode)
65 case 0x62: current_keys |= GP2X_UP; break;
67 case 0x68: current_keys |= GP2X_DOWN; break;
69 case 0x64: current_keys |= GP2X_LEFT; break;
71 case 0x66: current_keys |= GP2X_RIGHT; break;
72 case 0x24: current_keys |= GP2X_START; break; // enter
73 case 0x23: current_keys |= GP2X_SELECT;break; // ]
74 case 0x34: current_keys |= GP2X_A; break; // z
75 case 0x35: current_keys |= GP2X_X; break; // x
76 case 0x36: current_keys |= GP2X_B; break; // c
77 case 0x37: current_keys |= GP2X_Y; break; // v
78 case 0x27: current_keys |= GP2X_L; break; // s
79 case 0x28: current_keys |= GP2X_R; break; // d
80 case 0x29: current_keys |= GP2X_PUSH; break; // f
81 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
82 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
83 case 0x2d: log_io_clear(); break; // k
84 case 0x2e: log_io_dump(); break; // l
86 extern int PicoReset(void);
95 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
97 switch (event->hardware_keycode)
100 case 0x62: current_keys &= ~GP2X_UP; break;
102 case 0x68: current_keys &= ~GP2X_DOWN; break;
104 case 0x64: current_keys &= ~GP2X_LEFT; break;
106 case 0x66: current_keys &= ~GP2X_RIGHT; break;
107 case 0x24: current_keys &= ~GP2X_START; break; // enter
108 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
109 case 0x34: current_keys &= ~GP2X_A; break; // z
110 case 0x35: current_keys &= ~GP2X_X; break; // x
111 case 0x36: current_keys &= ~GP2X_B; break; // c
112 case 0x37: current_keys &= ~GP2X_Y; break; // v
113 case 0x27: current_keys &= ~GP2X_L; break; // s
114 case 0x28: current_keys &= ~GP2X_R; break; // d
115 case 0x29: current_keys &= ~GP2X_PUSH; break; // f
116 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
117 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
123 static void size_allocate_event(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
125 // printf("%dx%d\n", allocation->width, allocation->height);
126 if (scr_w != allocation->width - 2 || scr_h != allocation->height - 2) {
127 scr_w = allocation->width - 2;
128 scr_h = allocation->height - 2;
133 static void *gtk_threadf(void *targ)
136 char *argv[] = { "" };
140 g_thread_init (NULL);
143 gtk_init (&argc, (char ***) &argv);
145 /* create new window */
146 gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
147 g_signal_connect (G_OBJECT (gtk_items.window), "delete_event",
148 G_CALLBACK (delete_event), NULL);
150 g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
151 G_CALLBACK (destroy), NULL);
153 g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
154 G_CALLBACK (key_press_event), NULL);
156 g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
157 G_CALLBACK (key_release_event), NULL);
159 g_signal_connect (G_OBJECT (gtk_items.window), "size_allocate",
160 G_CALLBACK (size_allocate_event), NULL);
162 gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 1);
163 gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
165 box = gtk_hbox_new(FALSE, 0);
166 gtk_widget_show(box);
167 gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
170 gtk_items.pixmap1 = gtk_image_new ();
171 gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1);
172 gtk_widget_show (gtk_items.pixmap1);
173 gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240);
175 gtk_widget_show (gtk_items.window);
181 printf("linux: gtk thread finishing\n");
187 static void gtk_initf(void)
189 pthread_t gtk_thread;
191 sem_init(&sem, 0, 0);
193 pthread_create(>k_thread, NULL, gtk_threadf, &sem);
194 pthread_detach(gtk_thread);
200 void finalize_image(guchar *pixels, gpointer data)
207 static void realloc_screen(void)
209 void *old = g_screen_ptr;
211 g_screen_width = scr_w;
212 g_screen_height = scr_h;
213 g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
217 for (i = 0; i < 4; i++)
218 gp2x_screens[i] = g_screen_ptr;
221 /* gp2x/emu.c stuff, most to be rm'd */
222 static void gp2x_video_flip_(void)
225 unsigned char *image;
228 pixel_count = g_screen_width * g_screen_height;
232 image = malloc(pixel_count * 3);
239 if (current_bpp == 8)
241 unsigned char *pixels = g_screen_ptr;
244 for (i = 0; i < pixel_count; i++)
246 pix = current_pal[pixels[i]];
247 image[3 * i + 0] = pix >> 16;
248 image[3 * i + 1] = pix >> 8;
249 image[3 * i + 2] = pix;
254 unsigned short *pixels = g_screen_ptr;
256 for (i = 0; i < pixel_count; i++)
258 /* in: rrrr rggg gggb bbbb */
259 /* out: rrrr r000 gggg gg00 bbbb b000 */
260 image[3 * i + 0] = (pixels[i] >> 8) & 0xf8;
261 image[3 * i + 1] = (pixels[i] >> 3) & 0xfc;
262 image[3 * i + 2] = (pixels[i] << 3);
266 pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
267 FALSE, 8, g_screen_width, g_screen_height,
268 g_screen_width * 3, finalize_image, NULL);
269 gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf);
270 g_object_unref (pixbuf);
278 static void gp2x_video_changemode_ll_(int bpp)
283 static void gp2x_video_setpalette_(int *pal, int len)
285 memcpy(current_pal, pal, len*4);
288 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
292 void gp2x_memset_all_buffers(int offset, int byte, int len)
294 memset((char *)g_screen_ptr + offset, byte, len);
297 void gp2x_video_changemode(int bpp)
299 gp2x_video_changemode_ll_(bpp);
302 void gp2x_make_fb_bufferable(int yes)
312 static char menu_bg_buffer[320*240*2];
313 char cpu_clk_name[16] = "GP2X CPU clocks";
315 void plat_video_menu_enter(int is_rom_loaded)
319 // darken the active framebuffer
320 memset(g_screen_ptr, 0, 320*8*2);
321 menu_darken_bg((char *)g_screen_ptr + 320*8*2, 320*224, 1);
322 memset((char *)g_screen_ptr + 320*232*2, 0, 320*8*2);
328 // should really only happen once, on startup..
329 emu_make_path(buff, "skin/background.png", sizeof(buff));
330 if (readpng(g_screen_ptr, buff, READPNG_BG) < 0)
331 memset(g_screen_ptr, 0, 320*240*2);
334 memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2);
337 gp2x_video_changemode_ll_(16);
341 void plat_video_menu_begin(void)
343 memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
346 void plat_video_menu_end(void)
351 void plat_validate_config(void)
353 // PicoOpt &= ~POPT_EXT_FM;
356 void plat_early_init(void)
363 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
371 void plat_finish(void)
378 static void do_nothing()
382 void *gp2x_video_flip = gp2x_video_flip_;
383 void *gp2x_video_flip2 = gp2x_video_flip_;
384 void *gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
385 void *gp2x_video_setpalette = gp2x_video_setpalette_;
387 void *gp2x_video_RGB_setscaling = do_nothing;
388 void *gp2x_video_wait_vsync = do_nothing;
389 void *gp2x_set_cpuclk = do_nothing;
390 void *set_lcd_custom_rate = do_nothing;
391 void *unset_lcd_custom_rate = do_nothing;
392 void *set_lcd_gamma = do_nothing;
393 void *set_ram_timings = do_nothing;
394 void *unset_ram_timings = do_nothing;
397 int gp2x_touchpad_read(int *x, int *y)
403 void spend_cycles(int c)
408 int mp3_get_bitrate(FILE *f, int size)
413 void mp3_start_play(FILE *f, int pos)
417 void mp3_update(int *buffer, int length, int stereo)
422 void lprintf(const char *fmt, ...)