extracted OSS code to sndout_oss_*, BTN->PBTN, refactoring
[libpicofe.git] / gp2x / gp2x.c
index 3ed64fb..89505a0 100644 (file)
 #include <sys/mman.h>\r
 #include <sys/types.h>\r
 #include <sys/stat.h>\r
-#include <sys/ioctl.h>\r
-#include <sys/soundcard.h>\r
 #include <fcntl.h>\r
 #include <errno.h>\r
 \r
 #include "gp2x.h"\r
-#include "usbjoy.h"\r
+#include "../linux/usbjoy.h"\r
+#include "../linux/sndout_oss.h"\r
 #include "../common/arm_utils.h"\r
+#include "../common/arm_linux.h"\r
 \r
 volatile unsigned short *gp2x_memregs;\r
 //static\r
@@ -44,7 +44,8 @@ static void *gp2x_screens[4];
 static int screensel = 0;\r
 //static\r
 int memdev = 0;\r
-static int sounddev = 0, mixerdev = 0;\r
+static int touchdev = -1;\r
+static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };\r
 \r
 void *gp2x_screen;\r
 \r
@@ -161,9 +162,7 @@ void gp2x_video_wait_vsync(void)
 void gp2x_video_flush_cache(void)\r
 {\r
        // since we are using the mmu hack, we must flush the cache first\r
-       // (the params are most likely wrong, but they seem to work somehow)\r
-       //flushcache(addr, addr + 320*240*2, 0);\r
-       flushcache(gp2x_screen, (char *)gp2x_screen + 320*240*2, 0);\r
+       cache_flush_d_inval_i(gp2x_screen, (char *)gp2x_screen + 320*240*2);\r
 }\r
 \r
 \r
@@ -201,70 +200,53 @@ void gp2x_pd_clone_buffer2(void)
 unsigned long gp2x_joystick_read(int allow_usb_joy)\r
 {\r
        int i;\r
-       unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);\r
+       unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF); // GPIO M\r
        if(value==0xFD) value=0xFA;\r
        if(value==0xF7) value=0xEB;\r
        if(value==0xDF) value=0xAF;\r
        if(value==0x7F) value=0xBE;\r
-       value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));\r
+       value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16)); // C D\r
 \r
        if (allow_usb_joy && num_of_joys > 0) {\r
                // check the usb joy as well..\r
-               gp2x_usbjoy_update();\r
+               usbjoy_update();\r
                for (i = 0; i < num_of_joys; i++)\r
-                       value |= gp2x_usbjoy_check(i);\r
+                       value |= usbjoy_check(i);\r
        }\r
 \r
        return value;\r
 }\r
 \r
-static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r
-\r
-void gp2x_start_sound(int rate, int bits, int stereo)\r
+typedef struct ucb1x00_ts_event\r
 {\r
-       int frag = 0, bsize, buffers;\r
-\r
-       // if no settings change, we don't need to do anything\r
-       if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;\r
-\r
-       if (sounddev > 0) close(sounddev);\r
-       sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);\r
-       if (sounddev == -1)\r
-               printf("open(\"/dev/dsp\") failed with %i\n", errno);\r
-\r
-       ioctl(sounddev, SNDCTL_DSP_SPEED,  &rate);\r
-       ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);\r
-       ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);\r
-       // calculate buffer size\r
-       buffers = 16;\r
-       bsize = rate / 32;\r
-       if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding\r
-       while ((bsize>>=1)) frag++;\r
-       frag |= buffers<<16; // 16 buffers\r
-       ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);\r
-       printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",\r
-               rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r
-\r
-       s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r
-       usleep(100000);\r
-}\r
+       unsigned short pressure;\r
+       unsigned short x;\r
+       unsigned short y;\r
+       unsigned short pad;\r
+       struct timeval stamp;\r
+} UCB1X00_TS_EVENT;\r
+\r
+int gp2x_touchpad_read(int *x, int *y)\r
+{\r
+       UCB1X00_TS_EVENT event;\r
+       static int zero_seen = 0;\r
+       int retval;\r
 \r
+       if (touchdev < 0) return -1;\r
 \r
-void gp2x_sound_write(void *buff, int len)\r
-{\r
-       write(sounddev, buff, len);\r
-}\r
+       retval = read(touchdev, &event, sizeof(event));\r
+       if (retval <= 0) {\r
+               printf("touch read failed %i %i\n", retval, errno);\r
+               return -1;\r
+       }\r
+       // this is to ignore the messed-up 4.1.x driver\r
+       if (event.pressure == 0) zero_seen = 1;\r
 \r
-void gp2x_sound_sync(void)\r
-{\r
-       ioctl(sounddev, SOUND_PCM_SYNC, 0);\r
-}\r
+       if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16;\r
+       if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16;\r
+       // printf("read %i %i %i\n", event.pressure, *x, *y);\r
 \r
-void gp2x_sound_volume(int l, int r)\r
-{\r
-       l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r
-       l<<=8; l|=r;\r
-       ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r
+       return zero_seen ? event.pressure : 0;\r
 }\r
 \r
 \r
@@ -283,6 +265,31 @@ void Reset940(int yes, int bank)
        gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);\r
 }\r
 \r
+static void proc_set(const char *path, const char *val)\r
+{\r
+       FILE *f;\r
+       char tmp[16];\r
+\r
+       f = fopen(path, "w");\r
+       if (f == NULL) {\r
+               printf("failed to open: %s\n", path);\r
+               return;\r
+       }\r
+\r
+       fprintf(f, "0\n");\r
+       fclose(f);\r
+\r
+       printf("\"%s\" is set to: ", path);\r
+       f = fopen(path, "r");\r
+       if (f == NULL) {\r
+               printf("(open failed)\n");\r
+               return;\r
+       }\r
+\r
+       fgets(tmp, sizeof(tmp), f);\r
+       printf("%s", tmp);\r
+       fclose(f);\r
+}\r
 \r
 \r
 /* common */\r
@@ -293,7 +300,7 @@ void gp2x_init(void)
        memdev = open("/dev/mem", O_RDWR);\r
        if (memdev == -1)\r
        {\r
-               printf("open(\"/dev/mem\") failed with %i\n", errno);\r
+               perror("open(\"/dev/mem\")");\r
                exit(1);\r
        }\r
 \r
@@ -301,15 +308,17 @@ void gp2x_init(void)
        printf("memregs are @ %p\n", gp2x_memregs);\r
        if(gp2x_memregs == MAP_FAILED)\r
        {\r
-               printf("mmap(memregs) failed with %i\n", errno);\r
+               perror("mmap(memregs)");\r
                exit(1);\r
        }\r
        gp2x_memregl = (unsigned long *) gp2x_memregs;\r
 \r
+       gp2x_memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers\r
+\r
        gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);\r
        if(gp2x_screens[0] == MAP_FAILED)\r
        {\r
-               printf("mmap(gp2x_screen) failed with %i\n", errno);\r
+               perror("mmap(gp2x_screen)");\r
                exit(1);\r
        }\r
        printf("framebuffers point to %p\n", gp2x_screens[0]);\r
@@ -329,12 +338,26 @@ void gp2x_init(void)
        gp2x_memset_all_buffers(0, 0, 320*240*2);\r
 \r
        // snd\r
-       mixerdev = open("/dev/mixer", O_RDWR);\r
-       if (mixerdev == -1)\r
-               printf("open(\"/dev/mixer\") failed with %i\n", errno);\r
+       sndout_oss_init();\r
 \r
        /* init usb joys -GnoStiC */\r
-       gp2x_usbjoy_init();\r
+       usbjoy_init();\r
+\r
+       // touchscreen\r
+       touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);\r
+       if (touchdev >= 0) {\r
+               FILE *pcf = fopen("/etc/pointercal", "r");\r
+               if (pcf) {\r
+                       fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1],\r
+                               &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]);\r
+                       fclose(pcf);\r
+               }\r
+               printf("found touchscreen/wm97xx\n");\r
+       }\r
+\r
+       /* disable Linux read-ahead */\r
+       proc_set("/proc/sys/vm/max-readahead", "0\n");\r
+       proc_set("/proc/sys/vm/min-readahead", "0\n");\r
 \r
        printf("exitting init()\n"); fflush(stdout);\r
 }\r
@@ -355,15 +378,15 @@ void gp2x_deinit(void)
        munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);\r
        munmap((void *)gp2x_memregs, 0x10000);\r
        close(memdev);\r
-       close(mixerdev);\r
-       if (sounddev > 0) close(sounddev);\r
+       if (touchdev >= 0) close(touchdev);\r
 \r
-       gp2x_usbjoy_deinit();\r
+       sndout_oss_exit();\r
+       usbjoy_deinit();\r
 \r
        printf("all done, running ");\r
 \r
        // Zaq121's alternative frontend support from MAME\r
-       if(ext_menu && ext_state) {\r
+       if (ext_menu && ext_state) {\r
                printf("%s -state %s\n", ext_menu, ext_state);\r
                execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r
        } else if(ext_menu) {\r