refactoring for Wiz port; random cleanups
[libpicofe.git] / linux / gp2x.c
index d0efee8..a58b451 100644 (file)
@@ -1,32 +1,33 @@
-/* faking/emulating gp2x.c by using gtk */
+/* faking/emulating gp2x by using gtk */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
+#include <semaphore.h>
 #include <gtk/gtk.h>
 
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <sys/soundcard.h>
 #include <fcntl.h>
 #include <errno.h>
 
 #include "../gp2x/emu.h"
 #include "../gp2x/gp2x.h"
-#include "../gp2x/usbjoy.h"
 #include "../gp2x/version.h"
+#include "../common/emu.h"
+#include "sndout_oss.h"
 
-void *gp2x_screen;
+#include "log_io.h"
+
+unsigned long current_keys = 0;
 static int current_bpp = 8;
 static int current_pal[256];
-static unsigned long current_keys = 0;
-static int sounddev = 0, mixerdev = 0;
 static const char *verstring = "PicoDrive " VERSION;
+static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
 
 // dummies
-char *ext_menu = 0, *ext_state = 0;
+int mix_32_to_16l_level;
 
 /* gtk */
 struct gtk_global_struct
@@ -46,13 +47,23 @@ static void destroy (GtkWidget *widget, gpointer data)
        gtk_main_quit ();
 }
 
+/* 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 gint key_press_event (GtkWidget *widget, GdkEventKey *event)
 {
        switch (event->hardware_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; // ]
@@ -65,6 +76,13 @@ static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
                case 0x29: current_keys |= GP2X_PUSH;  break; // f
                case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
                case 0x19: current_keys |= GP2X_VOL_UP;break; // w
+               case 0x2d: log_io_clear(); break; // k
+               case 0x2e: log_io_dump();  break; // l
+               case 0x17: { // tab
+                       extern int PicoReset(void);
+                       PicoReset();
+                       break;
+               }
        }
 
        return 0;
@@ -74,9 +92,13 @@ static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
 {
        switch (event->hardware_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; // ]
@@ -94,22 +116,22 @@ static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
        return 0;
 }
 
-static void *gtk_threadf(void *none)
+static void size_allocate_event(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
 {
-       gtk_main();
-
-       printf("linux: gtk thread finishing\n");
-       engineState = PGS_Quit;
-
-       return NULL;
+       // printf("%dx%d\n", allocation->width, allocation->height);
+       if (scr_w != allocation->width - 2 || scr_h != allocation->height - 2) {
+               scr_w = allocation->width - 2;
+               scr_h = allocation->height - 2;
+               scr_changed = 1;
+       }
 }
 
-static void gtk_initf(void)
+static void *gtk_threadf(void *targ)
 {
        int argc = 0;
        char *argv[] = { "" };
        GtkWidget *box;
-       pthread_t gtk_thread;
+       sem_t *sem = targ;
 
        g_thread_init (NULL);
        gdk_threads_init ();
@@ -130,7 +152,10 @@ static void gtk_initf(void)
        g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
                        G_CALLBACK (key_release_event), NULL);
 
-       gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 2);
+       g_signal_connect (G_OBJECT (gtk_items.window), "size_allocate",
+                       G_CALLBACK (size_allocate_event), NULL);
+
+       gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 1);
        gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
 
        box = gtk_hbox_new(FALSE, 0);
@@ -145,11 +170,27 @@ static void gtk_initf(void)
 
        gtk_widget_show  (gtk_items.window);
 
-       // pthread_mutex_init (&thr_mutex, NULL);
-       // pthread_mutex_lock (&thr_mutex);
-       // pthread_mutex_init (&scanner_muttex, NULL);
+       sem_post(sem);
+
+       gtk_main();
+
+       printf("linux: gtk thread finishing\n");
+       exit(1);
 
-       pthread_create(&gtk_thread, NULL, gtk_threadf, NULL);
+       return NULL;
+}
+
+static void gtk_initf(void)
+{
+       pthread_t gtk_thread;
+       sem_t sem;
+       sem_init(&sem, 0, 0);
+
+       pthread_create(&gtk_thread, NULL, gtk_threadf, &sem);
+       pthread_detach(gtk_thread);
+
+       sem_wait(&sem);
+       sem_close(&sem);
 }
 
 void finalize_image(guchar *pixels, gpointer data)
@@ -159,30 +200,35 @@ void finalize_image(guchar *pixels, gpointer data)
 
 /* --- */
 
-void gp2x_init(void)
+static void realloc_screen(void)
+{
+       void *old = g_screen_ptr;
+       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;
+}
+
+void plat_init(void)
 {
        printf("entering init()\n"); fflush(stdout);
 
-       gp2x_screen = malloc(320*240*2 + 320*2);
+       realloc_screen();
+       memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
 
        // snd
-       mixerdev = open("/dev/mixer", O_RDWR);
-       if (mixerdev == -1)
-               printf("open(\"/dev/mixer\") failed with %i\n", errno);
+       sndout_oss_init();
 
        gtk_initf();
 
-       gp2x_usbjoy_init();
-
        printf("exitting init()\n"); fflush(stdout);
 }
 
-void gp2x_deinit(void)
+void plat_finish(void)
 {
-       free(gp2x_screen);
-       if (sounddev > 0) close(sounddev);
-       close(mixerdev);
-       gp2x_usbjoy_deinit();
+       free(g_screen_ptr);
+       sndout_oss_exit();
 }
 
 /* video */
@@ -190,11 +236,13 @@ void gp2x_video_flip(void)
 {
        GdkPixbuf       *pixbuf;
        unsigned char   *image;
-       int             i;
+       int             pixel_count, i;
+
+       pixel_count = g_screen_width * g_screen_height;
 
        gdk_threads_enter();
 
-       image = malloc (320*240*3);
+       image = malloc(pixel_count * 3);
        if (image == NULL)
        {
                gdk_threads_leave();
@@ -203,10 +251,10 @@ void gp2x_video_flip(void)
 
        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;
@@ -216,9 +264,9 @@ void gp2x_video_flip(void)
        }
        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 */
@@ -229,105 +277,59 @@ void gp2x_video_flip(void)
        }
 
        pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
-                       FALSE, 8, 320, 240, 320*3, finalize_image, NULL);
+                       FALSE, 8, g_screen_width, g_screen_height,
+                       g_screen_width * 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_changemode(int bpp)
-{
-       current_bpp = bpp;
-}
 
-void gp2x_video_setpalette(int *pal, int len)
-{
-       memcpy(current_pal, pal, len*4);
+       if (scr_changed)
+               realloc_screen();
 }
 
-void gp2x_video_RGB_setscaling(int W, int H)
+void gp2x_video_flip2(void)
 {
+       gp2x_video_flip();
 }
 
-void gp2x_memcpy_all_buffers(void *data, int offset, int len)
+void gp2x_video_changemode(int bpp)
 {
-       memcpy((char *)gp2x_screen + offset, data, len);
+       current_bpp = bpp;
 }
 
-
-void gp2x_memset_all_buffers(int offset, int byte, int len)
+void gp2x_video_changemode2(int bpp)
 {
-       memset((char *)gp2x_screen + offset, byte, len);
+       current_bpp = bpp;
 }
 
-
-/* sound */
-static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
-
-void gp2x_start_sound(int rate, int bits, int stereo)
+void gp2x_video_setpalette(int *pal, int len)
 {
-       int frag = 0, bsize, buffers;
-
-       // if no settings change, we don't need to do anything
-       if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
-
-       if (sounddev > 0) close(sounddev);
-       sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
-       if (sounddev == -1)
-               printf("open(\"/dev/dsp\") failed with %i\n", errno);
-
-       ioctl(sounddev, SNDCTL_DSP_SPEED,  &rate);
-       ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
-       ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
-       // calculate buffer size
-       buffers = 16;
-       bsize = rate / 32;
-       if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
-       while ((bsize>>=1)) frag++;
-       frag |= buffers<<16; // 16 buffers
-       ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
-       printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
-               rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
-
-       s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
+       memcpy(current_pal, pal, len*4);
 }
 
-void gp2x_sound_write(void *buff, int len)
+void gp2x_video_RGB_setscaling(int v_offs, int W, int H)
 {
-       write(sounddev, buff, len);
 }
 
-void gp2x_sound_volume(int l, int r)
+void gp2x_memset_all_buffers(int offset, int byte, int len)
 {
-       l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
-       l<<=8; l|=r;
-       ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
+       memset((char *)g_screen_ptr + offset, byte, len);
 }
 
 /* joy */
-unsigned long gp2x_joystick_read(int allow_usb_joy)
+int gp2x_touchpad_read(int *x, int *y)
 {
-       unsigned long value = current_keys;
-       int i;
-
-       if (allow_usb_joy && num_of_joys > 0) {
-               // check the usb joy as well..
-               gp2x_usbjoy_update();
-               for (i = 0; i < num_of_joys; i++)
-                       value |= gp2x_usbjoy_check(i);
-       }
-
-       return value;
+       return -1;
 }
 
 /* 940 */
 int crashed_940 = 0;
-void Pause940(int yes)
+void pause940(int yes)
 {
 }
 
-void Reset940(int yes, int bank)
+void reset940(int yes, int bank)
 {
 }
 
@@ -356,10 +358,17 @@ void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, in
 {
 }
 
-void set_gamma(int g100)
+void set_gamma(int g100, int A_SNs_curve)
 {
 }
 
+void set_LCD_custom_rate(int rate)
+{
+}
+
+void unset_LCD_custom_rate(void)
+{
+}
 
 /* squidgehack.c */
 int mmuhack(void)
@@ -377,8 +386,16 @@ int mmuunhack(void)
 /* misc */
 void spend_cycles(int c)
 {
-       usleep(c/*/200*/);
+       usleep(c/200);
 }
 
+/* lprintf */
+void lprintf(const char *fmt, ...)
+{
+       va_list vl;
 
+       va_start(vl, fmt);
+       vprintf(fmt, vl);
+       va_end(vl);
+}