32x: drc: handlers wip
[libpicofe.git] / linux / gp2x.c
index bcb3ee3..6eb252b 100644 (file)
@@ -1,10 +1,10 @@
-/* faking/emulating gp2x.c by using gtk */
+/* faking/emulating gp2x by using xlib */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 #include <pthread.h>
 #include <semaphore.h>
-#include <gtk/gtk.h>
 
 #include <unistd.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <errno.h>
 
-#include "../gp2x/emu.h"
-#include "../gp2x/gp2x.h"
 #include "../gp2x/version.h"
+#include "../common/emu.h"
+#include "../common/menu.h"
+#include "../common/readpng.h"
 #include "sndout_oss.h"
-#include "usbjoy.h"
 
 #include "log_io.h"
 
-void *gp2x_screen;
+unsigned long current_keys = 0;
 static int current_bpp = 8;
 static int current_pal[256];
-static unsigned long current_keys = 0;
 static const char *verstring = "PicoDrive " VERSION;
+static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
+void *gp2x_screens[4];
 
 // dummies
-char *ext_menu = 0, *ext_state = 0;
 int mix_32_to_16l_level;
+int crashed_940 = 0;
+int default_cpu_clock = 123;
+void *gp2x_memregs = NULL;
 
-/* gtk */
-struct gtk_global_struct
-{
-        GtkWidget *window;
-        GtkWidget *pixmap1;
-} gtk_items;
+/* ifndef is for qemu build without video out */
+#ifndef ARM
 
+/* faking GP2X pad */
+enum  { GP2X_UP=0x1,       GP2X_LEFT=0x4,       GP2X_DOWN=0x10,  GP2X_RIGHT=0x40,
+        GP2X_START=1<<8,   GP2X_SELECT=1<<9,    GP2X_L=1<<10,    GP2X_R=1<<11,
+        GP2X_A=1<<12,      GP2X_B=1<<13,        GP2X_X=1<<14,    GP2X_Y=1<<15,
+        GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
 
-static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
+static void key_press_event(int keycode)
 {
-       return FALSE;
-}
-
-static void destroy (GtkWidget *widget, gpointer data)
-{
-       gtk_main_quit ();
-}
-
-static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
-{
-       switch (event->hardware_keycode)
+       switch (keycode)
        {
+               case 111:
                case 0x62: current_keys |= GP2X_UP;    break;
+               case 116:
                case 0x68: current_keys |= GP2X_DOWN;  break;
+               case 113:
                case 0x64: current_keys |= GP2X_LEFT;  break;
+               case 114:
                case 0x66: current_keys |= GP2X_RIGHT; break;
                case 0x24: current_keys |= GP2X_START; break; // enter
                case 0x23: current_keys |= GP2X_SELECT;break; // ]
@@ -75,17 +73,19 @@ static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
                        break;
                }
        }
-
-       return 0;
 }
 
-static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
+static void key_release_event(int keycode)
 {
-       switch (event->hardware_keycode)
+       switch (keycode)
        {
+               case 111:
                case 0x62: current_keys &= ~GP2X_UP;    break;
+               case 116:
                case 0x68: current_keys &= ~GP2X_DOWN;  break;
+               case 113:
                case 0x64: current_keys &= ~GP2X_LEFT;  break;
+               case 114:
                case 0x66: current_keys &= ~GP2X_RIGHT; break;
                case 0x24: current_keys &= ~GP2X_START; break; // enter
                case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
@@ -99,291 +99,376 @@ static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
                case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
                case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
        }
-
-       return 0;
 }
 
-static void *gtk_threadf(void *targ)
-{
-       int argc = 0;
-       char *argv[] = { "" };
-       GtkWidget *box;
-       sem_t *sem = targ;
-
-       g_thread_init (NULL);
-       gdk_threads_init ();
-       gdk_set_locale ();
-       gtk_init (&argc, (char ***) &argv);
-
-       /* create new window */
-       gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-       g_signal_connect (G_OBJECT (gtk_items.window), "delete_event",
-                       G_CALLBACK (delete_event), NULL);
+/* --- */
 
-       g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
-                       G_CALLBACK (destroy), NULL);
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
 
-       g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
-                       G_CALLBACK (key_press_event), NULL);
+static Display *xlib_display;
+static Window xlib_window;
+static XImage *ximage;
 
-       g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
-                       G_CALLBACK (key_release_event), NULL);
+static void ximage_realloc(Display *display, Visual *visual)
+{
+       void *xlib_screen;
 
-       gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 2);
-       gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
+       XLockDisplay(xlib_display);
 
-       box = gtk_hbox_new(FALSE, 0);
-       gtk_widget_show(box);
-       gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
+       if (ximage != NULL)
+               XDestroyImage(ximage);
+       ximage = NULL;
 
-       /* live pixmap */
-       gtk_items.pixmap1 = gtk_image_new ();
-       gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1);
-       gtk_widget_show (gtk_items.pixmap1);
-       gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240);
+       xlib_screen = calloc(scr_w * scr_h, 4);
+       if (xlib_screen != NULL)
+               ximage = XCreateImage(display, visual, 24, ZPixmap, 0,
+                               xlib_screen, scr_w, scr_h, 32, 0);
+       if (ximage == NULL)
+               fprintf(stderr, "failed to alloc ximage\n");
 
-       gtk_widget_show  (gtk_items.window);
+       XUnlockDisplay(xlib_display);
+}
 
-       sem_post(sem);
+static void xlib_update(void)
+{
+       Status xstatus;
 
-       gtk_main();
+       XLockDisplay(xlib_display);
 
-       printf("linux: gtk thread finishing\n");
-       exit(1);
+       xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage,
+               0, 0, 0, 0, g_screen_width, g_screen_height);
+       if (xstatus != 0)
+               fprintf(stderr, "XPutImage %d\n", xstatus);
 
-       return NULL;
+       XUnlockDisplay(xlib_display);
 }
 
-static void gtk_initf(void)
+static void *xlib_threadf(void *targ)
 {
-       pthread_t gtk_thread;
-       sem_t sem;
-       sem_init(&sem, 0, 0);
+       unsigned int width, height, display_width, display_height;
+       sem_t *sem = targ;
+       XTextProperty windowName;
+       Window win;
+       XEvent report;
+       Display *display;
+       Visual *visual;
+       int screen;
 
-       pthread_create(&gtk_thread, NULL, gtk_threadf, &sem);
-       pthread_detach(gtk_thread);
+       XInitThreads();
 
-       sem_wait(&sem);
-       sem_close(&sem);
-}
+       xlib_display = display = XOpenDisplay(NULL);
+       if (display == NULL)
+       {
+               fprintf(stderr, "cannot connect to X server %s\n",
+                               XDisplayName(NULL));
+               sem_post(sem);
+               return NULL;
+       }
 
-void finalize_image(guchar *pixels, gpointer data)
-{
-       free(pixels);
-}
+       visual = DefaultVisual(display, 0);
+       if (visual->class != TrueColor)
+       {
+               fprintf(stderr, "cannot handle non true color visual\n");
+               XCloseDisplay(display);
+               sem_post(sem);
+               return NULL;
+       }
 
-/* --- */
+       printf("X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
+               VendorRelease(display), DisplayString(display), ProtocolVersion(display),
+               ProtocolRevision(display));
 
-void gp2x_init(void)
-{
-       printf("entering init()\n"); fflush(stdout);
+       screen = DefaultScreen(display);
 
-       gp2x_screen = malloc(320*240*2 + 320*2);
-       memset(gp2x_screen, 0, 320*240*2 + 320*2);
+       ximage_realloc(display, visual);
+       sem_post(sem);
 
-       // snd
-       sndout_oss_init();
+       display_width = DisplayWidth(display, screen);
+       display_height = DisplayHeight(display, screen);
+
+       xlib_window = win = XCreateSimpleWindow(display,
+                       RootWindow(display, screen),
+                       display_width / 2 - scr_w / 2,
+                       display_height / 2 - scr_h / 2,
+                       scr_w + 2, scr_h + 2, 1,
+                       BlackPixel(display, screen),
+                       BlackPixel(display, screen));
+
+       XStringListToTextProperty((char **)&verstring, 1, &windowName);
+       XSetWMName(display, win, &windowName);
 
-       gtk_initf();
+       XSelectInput(display, win, ExposureMask |
+                       KeyPressMask | KeyReleaseMask |
+                       StructureNotifyMask);
 
-       usbjoy_init();
+       XMapWindow(display, win);
 
-       printf("exitting init()\n"); fflush(stdout);
+       while (1)
+       {
+               XNextEvent(display, &report);
+               switch (report.type)
+               {
+                       case Expose:
+                               while (XCheckTypedEvent(display, Expose, &report))
+                                       ;
+                               xlib_update();
+                               break;
+
+                       case ConfigureNotify:
+                               width = report.xconfigure.width;
+                               height = report.xconfigure.height;
+                               if (scr_w != width - 2 || scr_h != height - 2) {
+                                       scr_w = width - 2;
+                                       scr_h = height - 2;
+                                       scr_changed = 1;
+                               }
+                               break;
+
+                       case ButtonPress:
+                               break;
+
+                       case KeyPress:
+                               key_press_event(report.xkey.keycode);
+                               break;
+
+                       case KeyRelease:
+                               key_release_event(report.xkey.keycode);
+                               break;
+
+                       default:
+                               break;
+               }
+       }
 }
 
-void gp2x_deinit(void)
+static void xlib_init(void)
 {
-       free(gp2x_screen);
-       sndout_oss_exit();
-       usbjoy_deinit();
+       pthread_t x_thread;
+       sem_t xlib_sem;
+
+       sem_init(&xlib_sem, 0, 0);
+
+       pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem);
+       pthread_detach(x_thread);
+
+       sem_wait(&xlib_sem);
+       sem_destroy(&xlib_sem);
 }
+#endif // !ARM
+
+/* --- */
 
-/* video */
-void gp2x_video_flip(void)
+static void realloc_screen(void)
 {
-       GdkPixbuf       *pixbuf;
-       unsigned char   *image;
-       int             i;
+       void *old = g_screen_ptr;
+       int i;
+       g_screen_width = scr_w;
+       g_screen_height = scr_h;
+       g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
+       free(old);
+       scr_changed = 0;
 
-       gdk_threads_enter();
+       for (i = 0; i < 4; i++)
+               gp2x_screens[i] = g_screen_ptr;
+}
 
-       image = malloc (320*240*3);
-       if (image == NULL)
-       {
-               gdk_threads_leave();
+/* gp2x/emu.c stuff, most to be rm'd */
+static void gp2x_video_flip_(void)
+{
+#ifndef ARM
+       unsigned int *image;
+       int pixel_count, i;
+
+       if (ximage == NULL)
                return;
-       }
+
+       pixel_count = g_screen_width * g_screen_height;
+       image = (void *)ximage->data;
 
        if (current_bpp == 8)
        {
-               unsigned char *pixels = gp2x_screen;
+               unsigned char *pixels = g_screen_ptr;
                int pix;
 
-               for (i = 0; i < 320*240; i++)
+               for (i = 0; i < pixel_count; i++)
                {
                        pix = current_pal[pixels[i]];
-                       image[3 * i + 0] = pix >> 16;
-                       image[3 * i + 1] = pix >>  8;
-                       image[3 * i + 2] = pix;
+                       image[i] = pix;
                }
        }
        else
        {
-               unsigned short *pixels = gp2x_screen;
+               unsigned short *pixels = g_screen_ptr;
 
-               for (i = 0; i < 320*240; i++)
+               for (i = 0; i < pixel_count; i++)
                {
                        /*  in:           rrrr rggg gggb bbbb */
                        /* out: rrrr r000 gggg gg00 bbbb b000 */
-                       image[3 * i + 0] = (pixels[i] >> 8) & 0xf8;
-                       image[3 * i + 1] = (pixels[i] >> 3) & 0xfc;
-                       image[3 * i + 2] = (pixels[i] << 3);
+                       image[i]  = (pixels[i] << 8) & 0xf80000;
+                       image[i] |= (pixels[i] << 5) & 0x00fc00;
+                       image[i] |= (pixels[i] << 3) & 0x0000f8;
                }
        }
+       xlib_update();
 
-       pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
-                       FALSE, 8, 320, 240, 320*3, finalize_image, NULL);
-       gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf);
-       g_object_unref (pixbuf);
-
-       gdk_threads_leave();
-}
-
-void gp2x_video_flip2(void)
-{
-       gp2x_video_flip();
-}
-
-void gp2x_video_changemode(int bpp)
-{
-       current_bpp = bpp;
+       if (scr_changed) {
+               realloc_screen();
+               ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
+       }
+#endif
 }
 
-void gp2x_video_changemode2(int bpp)
+static void gp2x_video_changemode_ll_(int bpp)
 {
        current_bpp = bpp;
 }
 
-void gp2x_video_setpalette(int *pal, int len)
+static void gp2x_video_setpalette_(int *pal, int len)
 {
        memcpy(current_pal, pal, len*4);
 }
 
-void gp2x_video_flush_cache(void)
+void gp2x_memcpy_all_buffers(void *data, int offset, int len)
 {
 }
 
-void gp2x_video_RGB_setscaling(int v_offs, int W, int H)
+void gp2x_memset_all_buffers(int offset, int byte, int len)
 {
+       memset((char *)g_screen_ptr + offset, byte, len);
 }
 
-void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
+void gp2x_video_changemode(int bpp)
 {
-       if ((char *)gp2x_screen + offset != data)
-               memcpy((char *)gp2x_screen + offset, data, len);
+       gp2x_video_changemode_ll_(bpp);
 }
 
-void gp2x_memcpy_all_buffers(void *data, int offset, int len)
+void gp2x_make_fb_bufferable(int yes)
 {
-       memcpy((char *)gp2x_screen + offset, data, len);
 }
 
-
-void gp2x_memset_all_buffers(int offset, int byte, int len)
+int soc_detect(void)
 {
-       memset((char *)gp2x_screen + offset, byte, len);
+       return 0;
 }
 
-void gp2x_pd_clone_buffer2(void)
-{
-       memset(gp2x_screen, 0, 320*240*2);
-}
+/* plat */
+static char menu_bg_buffer[320*240*2];
+char cpu_clk_name[16] = "GP2X CPU clocks";
 
-/* joy */
-unsigned long gp2x_joystick_read(int allow_usb_joy)
+void plat_video_menu_enter(int is_rom_loaded)
 {
-       unsigned long value = current_keys;
-       int i;
+       if (is_rom_loaded)
+       {
+               // darken the active framebuffer
+               memset(g_screen_ptr, 0, 320*8*2);
+               menu_darken_bg((char *)g_screen_ptr + 320*8*2, 320*224, 1);
+               memset((char *)g_screen_ptr + 320*232*2, 0, 320*8*2);
+       }
+       else
+       {
+               char buff[256];
 
-       if (allow_usb_joy && num_of_joys > 0) {
-               // check the usb joy as well..
-               usbjoy_update();
-               for (i = 0; i < num_of_joys; i++)
-                       value |= usbjoy_check(i);
+               // should really only happen once, on startup..
+               emu_make_path(buff, "skin/background.png", sizeof(buff));
+               if (readpng(g_screen_ptr, buff, READPNG_BG) < 0)
+                       memset(g_screen_ptr, 0, 320*240*2);
        }
 
-       return value;
-}
+       memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2);
 
-int gp2x_touchpad_read(int *x, int *y)
-{
-       return -1;
+       // switch to 16bpp
+       gp2x_video_changemode_ll_(16);
+       gp2x_video_flip_();
 }
 
-/* 940 */
-int crashed_940 = 0;
-void Pause940(int yes)
+void plat_video_menu_begin(void)
 {
+       memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
 }
 
-void Reset940(int yes, int bank)
+void plat_video_menu_end(void)
 {
+       gp2x_video_flip_();
 }
 
-/* faking gp2x cpuctrl.c */
-void cpuctrl_init(void)
+void plat_validate_config(void)
 {
+       extern int PicoOpt;
+//     PicoOpt &= ~POPT_EXT_FM;
+       PicoOpt &= ~(1<<17); // POPT_EN_SVP_DRC
 }
 
-void cpuctrl_deinit(void)
+void plat_early_init(void)
 {
 }
 
-void set_FCLK(unsigned MHZ)
+void plat_init(void)
 {
-}
+       realloc_screen();
+       memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
 
-void Disable_940(void)
-{
+       // snd
+       sndout_oss_init();
+
+#ifndef ARM
+       xlib_init();
+#endif
 }
 
-void gp2x_video_wait_vsync(void)
+void plat_finish(void)
 {
+       free(g_screen_ptr);
+       sndout_oss_exit();
 }
 
-void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
+/* nasty */
+static void do_nothing()
 {
 }
 
-void set_gamma(int g100, int A_SNs_curve)
+void *gp2x_video_flip = gp2x_video_flip_;
+void *gp2x_video_flip2 = gp2x_video_flip_;
+void *gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
+void *gp2x_video_setpalette = gp2x_video_setpalette_;
+
+void *gp2x_video_RGB_setscaling = do_nothing;
+void *gp2x_video_wait_vsync = do_nothing;
+void *gp2x_set_cpuclk = do_nothing;
+void *gp2x_read_battery = do_nothing;
+void *set_lcd_custom_rate = do_nothing;
+void *unset_lcd_custom_rate = do_nothing;
+void *set_lcd_gamma = do_nothing;
+void *set_ram_timings = do_nothing;
+void *unset_ram_timings = do_nothing;
+
+/* joy */
+int gp2x_touchpad_read(int *x, int *y)
 {
+       return -1;
 }
 
-void set_LCD_custom_rate(int rate)
+/* misc */
+void spend_cycles(int c)
 {
+       usleep(c/200);
 }
 
-void unset_LCD_custom_rate(void)
+int mp3_get_bitrate(FILE *f, int size)
 {
+       return 128;
 }
 
-/* squidgehack.c */
-int mmuhack(void)
+void mp3_start_play(FILE *f, int pos)
 {
-       return 0;
 }
 
-
-int mmuunhack(void)
+void mp3_update(int *buffer, int length, int stereo)
 {
-       return 0;
 }
 
-
-/* misc */
-void spend_cycles(int c)
+void cache_flush_d_inval_i()
 {
-       usleep(c/200);
 }
 
 /* lprintf */