1 /* faking/emulating gp2x 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 int mix_32_to_16l_level;
33 struct gtk_global_struct
40 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
45 static void destroy (GtkWidget *widget, gpointer data)
51 enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40,
52 GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11,
53 GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15,
54 GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
56 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
58 switch (event->hardware_keycode)
61 case 0x62: current_keys |= GP2X_UP; break;
63 case 0x68: current_keys |= GP2X_DOWN; break;
65 case 0x64: current_keys |= GP2X_LEFT; break;
67 case 0x66: current_keys |= GP2X_RIGHT; break;
68 case 0x24: current_keys |= GP2X_START; break; // enter
69 case 0x23: current_keys |= GP2X_SELECT;break; // ]
70 case 0x34: current_keys |= GP2X_A; break; // z
71 case 0x35: current_keys |= GP2X_X; break; // x
72 case 0x36: current_keys |= GP2X_B; break; // c
73 case 0x37: current_keys |= GP2X_Y; break; // v
74 case 0x27: current_keys |= GP2X_L; break; // s
75 case 0x28: current_keys |= GP2X_R; break; // d
76 case 0x29: current_keys |= GP2X_PUSH; break; // f
77 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
78 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
79 case 0x2d: log_io_clear(); break; // k
80 case 0x2e: log_io_dump(); break; // l
82 extern int PicoReset(void);
91 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
93 switch (event->hardware_keycode)
96 case 0x62: current_keys &= ~GP2X_UP; break;
98 case 0x68: current_keys &= ~GP2X_DOWN; break;
100 case 0x64: current_keys &= ~GP2X_LEFT; break;
102 case 0x66: current_keys &= ~GP2X_RIGHT; break;
103 case 0x24: current_keys &= ~GP2X_START; break; // enter
104 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
105 case 0x34: current_keys &= ~GP2X_A; break; // z
106 case 0x35: current_keys &= ~GP2X_X; break; // x
107 case 0x36: current_keys &= ~GP2X_B; break; // c
108 case 0x37: current_keys &= ~GP2X_Y; break; // v
109 case 0x27: current_keys &= ~GP2X_L; break; // s
110 case 0x28: current_keys &= ~GP2X_R; break; // d
111 case 0x29: current_keys &= ~GP2X_PUSH; break; // f
112 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
113 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
119 static void size_allocate_event(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
121 // printf("%dx%d\n", allocation->width, allocation->height);
122 if (scr_w != allocation->width - 2 || scr_h != allocation->height - 2) {
123 scr_w = allocation->width - 2;
124 scr_h = allocation->height - 2;
129 static void *gtk_threadf(void *targ)
132 char *argv[] = { "" };
136 g_thread_init (NULL);
139 gtk_init (&argc, (char ***) &argv);
141 /* create new window */
142 gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
143 g_signal_connect (G_OBJECT (gtk_items.window), "delete_event",
144 G_CALLBACK (delete_event), NULL);
146 g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
147 G_CALLBACK (destroy), NULL);
149 g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
150 G_CALLBACK (key_press_event), NULL);
152 g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
153 G_CALLBACK (key_release_event), NULL);
155 g_signal_connect (G_OBJECT (gtk_items.window), "size_allocate",
156 G_CALLBACK (size_allocate_event), NULL);
158 gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 1);
159 gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
161 box = gtk_hbox_new(FALSE, 0);
162 gtk_widget_show(box);
163 gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
166 gtk_items.pixmap1 = gtk_image_new ();
167 gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1);
168 gtk_widget_show (gtk_items.pixmap1);
169 gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240);
171 gtk_widget_show (gtk_items.window);
177 printf("linux: gtk thread finishing\n");
183 static void gtk_initf(void)
185 pthread_t gtk_thread;
187 sem_init(&sem, 0, 0);
189 pthread_create(>k_thread, NULL, gtk_threadf, &sem);
190 pthread_detach(gtk_thread);
196 void finalize_image(guchar *pixels, gpointer data)
203 static void realloc_screen(void)
205 void *old = g_screen_ptr;
206 g_screen_width = scr_w;
207 g_screen_height = scr_h;
208 g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
215 printf("entering init()\n"); fflush(stdout);
218 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
225 printf("exitting init()\n"); fflush(stdout);
228 void plat_finish(void)
235 void gp2x_video_flip(void)
238 unsigned char *image;
241 pixel_count = g_screen_width * g_screen_height;
245 image = malloc(pixel_count * 3);
252 if (current_bpp == 8)
254 unsigned char *pixels = g_screen_ptr;
257 for (i = 0; i < pixel_count; i++)
259 pix = current_pal[pixels[i]];
260 image[3 * i + 0] = pix >> 16;
261 image[3 * i + 1] = pix >> 8;
262 image[3 * i + 2] = pix;
267 unsigned short *pixels = g_screen_ptr;
269 for (i = 0; i < pixel_count; i++)
271 /* in: rrrr rggg gggb bbbb */
272 /* out: rrrr r000 gggg gg00 bbbb b000 */
273 image[3 * i + 0] = (pixels[i] >> 8) & 0xf8;
274 image[3 * i + 1] = (pixels[i] >> 3) & 0xfc;
275 image[3 * i + 2] = (pixels[i] << 3);
279 pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
280 FALSE, 8, g_screen_width, g_screen_height,
281 g_screen_width * 3, finalize_image, NULL);
282 gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf);
283 g_object_unref (pixbuf);
291 void gp2x_video_flip2(void)
296 void gp2x_video_changemode(int bpp)
301 void gp2x_video_changemode2(int bpp)
306 void gp2x_video_setpalette(int *pal, int len)
308 memcpy(current_pal, pal, len*4);
311 void gp2x_video_RGB_setscaling(int v_offs, int W, int H)
315 void gp2x_memset_all_buffers(int offset, int byte, int len)
317 memset((char *)g_screen_ptr + offset, byte, len);
321 int gp2x_touchpad_read(int *x, int *y)
328 void pause940(int yes)
332 void reset940(int yes, int bank)
336 void gp2x_video_wait_vsync(void)
340 void set_gamma(int g100, int A_SNs_curve)
344 void set_lcd_custom_rate(int rate)
348 void unset_lcd_custom_rate(void)
353 void spend_cycles(int c)
359 void lprintf(const char *fmt, ...)