1 /* faking/emulating gp2x.c by using gtk */
10 #include <sys/types.h>
15 #include "../gp2x/emu.h"
16 #include "../gp2x/gp2x.h"
17 #include "../gp2x/version.h"
18 #include "../common/emu.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;
30 char *ext_menu = 0, *ext_state = 0;
31 int mix_32_to_16l_level;
34 struct gtk_global_struct
41 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
46 static void destroy (GtkWidget *widget, gpointer data)
52 enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40,
53 GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11,
54 GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15,
55 GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
57 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
59 switch (event->hardware_keycode)
62 case 0x62: current_keys |= GP2X_UP; break;
64 case 0x68: current_keys |= GP2X_DOWN; break;
66 case 0x64: current_keys |= GP2X_LEFT; break;
68 case 0x66: current_keys |= GP2X_RIGHT; break;
69 case 0x24: current_keys |= GP2X_START; break; // enter
70 case 0x23: current_keys |= GP2X_SELECT;break; // ]
71 case 0x34: current_keys |= GP2X_A; break; // z
72 case 0x35: current_keys |= GP2X_X; break; // x
73 case 0x36: current_keys |= GP2X_B; break; // c
74 case 0x37: current_keys |= GP2X_Y; break; // v
75 case 0x27: current_keys |= GP2X_L; break; // s
76 case 0x28: current_keys |= GP2X_R; break; // d
77 case 0x29: current_keys |= GP2X_PUSH; break; // f
78 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
79 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
80 case 0x2d: log_io_clear(); break; // k
81 case 0x2e: log_io_dump(); break; // l
83 extern int PicoReset(void);
92 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
94 switch (event->hardware_keycode)
97 case 0x62: current_keys &= ~GP2X_UP; break;
99 case 0x68: current_keys &= ~GP2X_DOWN; break;
101 case 0x64: current_keys &= ~GP2X_LEFT; break;
103 case 0x66: current_keys &= ~GP2X_RIGHT; break;
104 case 0x24: current_keys &= ~GP2X_START; break; // enter
105 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
106 case 0x34: current_keys &= ~GP2X_A; break; // z
107 case 0x35: current_keys &= ~GP2X_X; break; // x
108 case 0x36: current_keys &= ~GP2X_B; break; // c
109 case 0x37: current_keys &= ~GP2X_Y; break; // v
110 case 0x27: current_keys &= ~GP2X_L; break; // s
111 case 0x28: current_keys &= ~GP2X_R; break; // d
112 case 0x29: current_keys &= ~GP2X_PUSH; break; // f
113 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
114 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
120 static void size_allocate_event(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
122 // printf("%dx%d\n", allocation->width, allocation->height);
123 if (scr_w != allocation->width - 2 || scr_h != allocation->height - 2) {
124 scr_w = allocation->width - 2;
125 scr_h = allocation->height - 2;
130 static void *gtk_threadf(void *targ)
133 char *argv[] = { "" };
137 g_thread_init (NULL);
140 gtk_init (&argc, (char ***) &argv);
142 /* create new window */
143 gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
144 g_signal_connect (G_OBJECT (gtk_items.window), "delete_event",
145 G_CALLBACK (delete_event), NULL);
147 g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
148 G_CALLBACK (destroy), NULL);
150 g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
151 G_CALLBACK (key_press_event), NULL);
153 g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
154 G_CALLBACK (key_release_event), NULL);
156 g_signal_connect (G_OBJECT (gtk_items.window), "size_allocate",
157 G_CALLBACK (size_allocate_event), NULL);
159 gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 1);
160 gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
162 box = gtk_hbox_new(FALSE, 0);
163 gtk_widget_show(box);
164 gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
167 gtk_items.pixmap1 = gtk_image_new ();
168 gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1);
169 gtk_widget_show (gtk_items.pixmap1);
170 gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240);
172 gtk_widget_show (gtk_items.window);
178 printf("linux: gtk thread finishing\n");
184 static void gtk_initf(void)
186 pthread_t gtk_thread;
188 sem_init(&sem, 0, 0);
190 pthread_create(>k_thread, NULL, gtk_threadf, &sem);
191 pthread_detach(gtk_thread);
197 void finalize_image(guchar *pixels, gpointer data)
204 static void realloc_screen(void)
206 void *old = g_screen_ptr;
207 g_screen_width = scr_w;
208 g_screen_height = scr_h;
209 g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
216 printf("entering init()\n"); fflush(stdout);
219 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
226 printf("exitting init()\n"); fflush(stdout);
229 void gp2x_deinit(void)
236 void gp2x_video_flip(void)
239 unsigned char *image;
242 pixel_count = g_screen_width * g_screen_height;
246 image = malloc(pixel_count * 3);
253 if (current_bpp == 8)
255 unsigned char *pixels = g_screen_ptr;
258 for (i = 0; i < pixel_count; i++)
260 pix = current_pal[pixels[i]];
261 image[3 * i + 0] = pix >> 16;
262 image[3 * i + 1] = pix >> 8;
263 image[3 * i + 2] = pix;
268 unsigned short *pixels = g_screen_ptr;
270 for (i = 0; i < pixel_count; i++)
272 /* in: rrrr rggg gggb bbbb */
273 /* out: rrrr r000 gggg gg00 bbbb b000 */
274 image[3 * i + 0] = (pixels[i] >> 8) & 0xf8;
275 image[3 * i + 1] = (pixels[i] >> 3) & 0xfc;
276 image[3 * i + 2] = (pixels[i] << 3);
280 pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
281 FALSE, 8, g_screen_width, g_screen_height,
282 g_screen_width * 3, finalize_image, NULL);
283 gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf);
284 g_object_unref (pixbuf);
292 void gp2x_video_flip2(void)
297 void gp2x_video_changemode(int bpp)
302 void gp2x_video_changemode2(int bpp)
307 void gp2x_video_setpalette(int *pal, int len)
309 memcpy(current_pal, pal, len*4);
312 void gp2x_video_flush_cache(void)
316 void gp2x_video_RGB_setscaling(int v_offs, int W, int H)
320 void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
322 if ((char *)g_screen_ptr + offset != data)
323 memcpy((char *)g_screen_ptr + offset, data, len);
326 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
328 memcpy((char *)g_screen_ptr + offset, data, len);
332 void gp2x_memset_all_buffers(int offset, int byte, int len)
334 memset((char *)g_screen_ptr + offset, byte, len);
337 void gp2x_pd_clone_buffer2(void)
339 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
343 int gp2x_touchpad_read(int *x, int *y)
350 void Pause940(int yes)
354 void Reset940(int yes, int bank)
358 /* faking gp2x cpuctrl.c */
359 void cpuctrl_init(void)
363 void cpuctrl_deinit(void)
367 void set_FCLK(unsigned MHZ)
371 void Disable_940(void)
375 void gp2x_video_wait_vsync(void)
379 void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
383 void set_gamma(int g100, int A_SNs_curve)
387 void set_LCD_custom_rate(int rate)
391 void unset_LCD_custom_rate(void)
409 void spend_cycles(int c)
415 void lprintf(const char *fmt, ...)