sram bugfix + savestate refactoring
[picodrive.git] / platform / linux / gp2x.c
1 /* faking/emulating gp2x by using gtk */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <pthread.h>
6 #include <semaphore.h>
7 #include <gtk/gtk.h>
8
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
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"
20
21 #include "log_io.h"
22
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];
29
30 // dummies
31 int mix_32_to_16l_level;
32 int crashed_940 = 0;
33 int default_cpu_clock = 123;
34 void *gp2x_memregs = NULL;
35
36 /* gtk */
37 struct gtk_global_struct
38 {
39         GtkWidget *window;
40         GtkWidget *pixmap1;
41 } gtk_items;
42
43
44 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
45 {
46         return FALSE;
47 }
48
49 static void destroy (GtkWidget *widget, gpointer data)
50 {
51         gtk_main_quit ();
52 }
53
54 /* faking GP2X pad */
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 };
59
60 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
61 {
62         switch (event->hardware_keycode)
63         {
64                 case 111:
65                 case 0x62: current_keys |= GP2X_UP;    break;
66                 case 116:
67                 case 0x68: current_keys |= GP2X_DOWN;  break;
68                 case 113:
69                 case 0x64: current_keys |= GP2X_LEFT;  break;
70                 case 114:
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
85                 case 0x17: { // tab
86                         extern int PicoReset(void);
87                         PicoReset();
88                         break;
89                 }
90         }
91
92         return 0;
93 }
94
95 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
96 {
97         switch (event->hardware_keycode)
98         {
99                 case 111:
100                 case 0x62: current_keys &= ~GP2X_UP;    break;
101                 case 116:
102                 case 0x68: current_keys &= ~GP2X_DOWN;  break;
103                 case 113:
104                 case 0x64: current_keys &= ~GP2X_LEFT;  break;
105                 case 114:
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
118         }
119
120         return 0;
121 }
122
123 static void size_allocate_event(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
124 {
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;
129                 scr_changed = 1;
130         }
131 }
132
133 static void *gtk_threadf(void *targ)
134 {
135         int argc = 0;
136         char *argv[] = { "" };
137         GtkWidget *box;
138         sem_t *sem = targ;
139
140         g_thread_init (NULL);
141         gdk_threads_init ();
142         gdk_set_locale ();
143         gtk_init (&argc, (char ***) &argv);
144
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);
149
150         g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
151                         G_CALLBACK (destroy), NULL);
152
153         g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
154                         G_CALLBACK (key_press_event), NULL);
155
156         g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
157                         G_CALLBACK (key_release_event), NULL);
158
159         g_signal_connect (G_OBJECT (gtk_items.window), "size_allocate",
160                         G_CALLBACK (size_allocate_event), NULL);
161
162         gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 1);
163         gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
164
165         box = gtk_hbox_new(FALSE, 0);
166         gtk_widget_show(box);
167         gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
168
169         /* live pixmap */
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);
174
175         gtk_widget_show  (gtk_items.window);
176
177         sem_post(sem);
178
179         gtk_main();
180
181         printf("linux: gtk thread finishing\n");
182         exit(1);
183
184         return NULL;
185 }
186
187 static void gtk_initf(void)
188 {
189         pthread_t gtk_thread;
190         sem_t sem;
191         sem_init(&sem, 0, 0);
192
193         pthread_create(&gtk_thread, NULL, gtk_threadf, &sem);
194         pthread_detach(gtk_thread);
195
196         sem_wait(&sem);
197         sem_close(&sem);
198 }
199
200 void finalize_image(guchar *pixels, gpointer data)
201 {
202         free(pixels);
203 }
204
205 /* --- */
206
207 static void realloc_screen(void)
208 {
209         void *old = g_screen_ptr;
210         int i;
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);
214         free(old);
215         scr_changed = 0;
216
217         for (i = 0; i < 4; i++)
218                 gp2x_screens[i] = g_screen_ptr;
219 }
220
221 /* gp2x/emu.c stuff, most to be rm'd */
222 static void gp2x_video_flip_(void)
223 {
224         GdkPixbuf       *pixbuf;
225         unsigned char   *image;
226         int             pixel_count, i;
227
228         pixel_count = g_screen_width * g_screen_height;
229
230         gdk_threads_enter();
231
232         image = malloc(pixel_count * 3);
233         if (image == NULL)
234         {
235                 gdk_threads_leave();
236                 return;
237         }
238
239         if (current_bpp == 8)
240         {
241                 unsigned char *pixels = g_screen_ptr;
242                 int pix;
243
244                 for (i = 0; i < pixel_count; i++)
245                 {
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;
250                 }
251         }
252         else
253         {
254                 unsigned short *pixels = g_screen_ptr;
255
256                 for (i = 0; i < pixel_count; i++)
257                 {
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);
263                 }
264         }
265
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);
271
272         gdk_threads_leave();
273
274         if (scr_changed)
275                 realloc_screen();
276 }
277
278 static void gp2x_video_changemode_ll_(int bpp)
279 {
280         current_bpp = bpp;
281 }
282
283 static void gp2x_video_setpalette_(int *pal, int len)
284 {
285         memcpy(current_pal, pal, len*4);
286 }
287
288 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
289 {
290 }
291
292 void gp2x_memset_all_buffers(int offset, int byte, int len)
293 {
294         memset((char *)g_screen_ptr + offset, byte, len);
295 }
296
297 void gp2x_video_changemode(int bpp)
298 {
299         gp2x_video_changemode_ll_(bpp);
300 }
301
302 void gp2x_make_fb_bufferable(int yes)
303 {
304 }
305
306 int soc_detect(void)
307 {
308         return 0;
309 }
310
311 /* plat */
312 static char menu_bg_buffer[320*240*2];
313 char cpu_clk_name[16] = "GP2X CPU clocks";
314
315 void plat_video_menu_enter(int is_rom_loaded)
316 {
317         if (is_rom_loaded)
318         {
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);
323         }
324         else
325         {
326                 char buff[256];
327
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);
332         }
333
334         memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2);
335
336         // switch to 16bpp
337         gp2x_video_changemode_ll_(16);
338         gp2x_video_flip_();
339 }
340
341 void plat_video_menu_begin(void)
342 {
343         memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
344 }
345
346 void plat_video_menu_end(void)
347 {
348         gp2x_video_flip_();
349 }
350
351 void plat_validate_config(void)
352 {
353 //      PicoOpt &= ~POPT_EXT_FM;
354 }
355
356 void plat_early_init(void)
357 {
358 }
359
360 void plat_init(void)
361 {
362         realloc_screen();
363         memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
364
365         // snd
366         sndout_oss_init();
367
368         gtk_initf();
369 }
370
371 void plat_finish(void)
372 {
373         free(g_screen_ptr);
374         sndout_oss_exit();
375 }
376
377 /* nasty */
378 static void do_nothing()
379 {
380 }
381
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_;
386
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;
395
396 /* joy */
397 int gp2x_touchpad_read(int *x, int *y)
398 {
399         return -1;
400 }
401
402 /* misc */
403 void spend_cycles(int c)
404 {
405         usleep(c/200);
406 }
407
408 int mp3_get_bitrate(FILE *f, int size)
409 {
410         return 128;
411 }
412
413 void mp3_start_play(FILE *f, int pos)
414 {
415 }
416
417 void mp3_update(int *buffer, int length, int stereo)
418 {
419 }
420
421 /* lprintf */
422 void lprintf(const char *fmt, ...)
423 {
424         va_list vl;
425
426         va_start(vl, fmt);
427         vprintf(fmt, vl);
428         va_end(vl);
429 }
430