pandora.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/soundcard.h>
  11. #include <linux/fb.h>
  12. #include <fcntl.h>
  13. #include <errno.h>
  14. #include "../gp2x/gp2x.h"
  15. #include "../gp2x/usbjoy.h"
  16. #include "../common/arm_utils.h"
  17. static volatile unsigned int *memregs = MAP_FAILED;
  18. //static
  19. int memdev = 0;
  20. static int fbdev = -1, sounddev = -1, mixerdev = -1, touchdev = -1;
  21. static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
  22. //#define SCREEN_MAP_SIZE (((800*(480+11)*2)+0xfff)&~0xfff)
  23. #define SCREEN_MAP_SIZE (800*480*2)
  24. static void *screen = MAP_FAILED;
  25. void *gp2x_screen;
  26. /* video stuff */
  27. void gp2x_video_flip(void)
  28. {
  29. }
  30. /* doulblebuffered flip */
  31. void gp2x_video_flip2(void)
  32. {
  33. }
  34. void gp2x_video_changemode2(int bpp)
  35. {
  36. }
  37. void gp2x_video_changemode(int bpp)
  38. {
  39. }
  40. void gp2x_video_setpalette(int *pal, int len)
  41. {
  42. }
  43. void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
  44. {
  45. }
  46. void gp2x_video_wait_vsync(void)
  47. {
  48. }
  49. void gp2x_video_flush_cache(void)
  50. {
  51. // flushcache(gp2x_screen, (char *)gp2x_screen + 320*240*2, 0);
  52. }
  53. void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
  54. {
  55. }
  56. void gp2x_memcpy_all_buffers(void *data, int offset, int len)
  57. {
  58. }
  59. void gp2x_memset_all_buffers(int offset, int byte, int len)
  60. {
  61. memset((char *)gp2x_screen + offset, byte, len);
  62. }
  63. void gp2x_pd_clone_buffer2(void)
  64. {
  65. }
  66. unsigned long gp2x_joystick_read(int allow_usb_joy)
  67. {
  68. unsigned long value = 0;
  69. int i;
  70. if (allow_usb_joy && num_of_joys > 0) {
  71. // check the usb joy as well..
  72. gp2x_usbjoy_update();
  73. for (i = 0; i < num_of_joys; i++)
  74. value |= gp2x_usbjoy_check(i);
  75. }
  76. return value;
  77. }
  78. typedef struct ucb1x00_ts_event
  79. {
  80. unsigned short pressure;
  81. unsigned short x;
  82. unsigned short y;
  83. unsigned short pad;
  84. struct timeval stamp;
  85. } UCB1X00_TS_EVENT;
  86. int gp2x_touchpad_read(int *x, int *y)
  87. {
  88. UCB1X00_TS_EVENT event;
  89. static int zero_seen = 0;
  90. int retval;
  91. if (touchdev < 0) return -1;
  92. retval = read(touchdev, &event, sizeof(event));
  93. if (retval <= 0) {
  94. printf("touch read failed %i %i\n", retval, errno);
  95. return -1;
  96. }
  97. // this is to ignore the messed-up 4.1.x driver
  98. if (event.pressure == 0) zero_seen = 1;
  99. if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16;
  100. if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16;
  101. // printf("read %i %i %i\n", event.pressure, *x, *y);
  102. return zero_seen ? event.pressure : 0;
  103. }
  104. //static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
  105. void gp2x_start_sound(int rate, int bits, int stereo)
  106. {
  107. #if 0
  108. int frag = 0, bsize, buffers;
  109. // if no settings change, we don't need to do anything
  110. if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
  111. if (sounddev > 0) close(sounddev);
  112. sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
  113. if (sounddev == -1)
  114. printf("open(\"/dev/dsp\") failed with %i\n", errno);
  115. ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
  116. ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
  117. ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
  118. // calculate buffer size
  119. buffers = 16;
  120. bsize = rate / 32;
  121. if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
  122. while ((bsize>>=1)) frag++;
  123. frag |= buffers<<16; // 16 buffers
  124. ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
  125. usleep(192*1024);
  126. printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
  127. rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
  128. s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
  129. #endif
  130. }
  131. void gp2x_sound_write(void *buff, int len)
  132. {
  133. // write(sounddev, buff, len);
  134. }
  135. void gp2x_sound_sync(void)
  136. {
  137. // ioctl(sounddev, SOUND_PCM_SYNC, 0);
  138. }
  139. void gp2x_sound_volume(int l, int r)
  140. {
  141. #if 0
  142. l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
  143. l<<=8; l|=r;
  144. ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
  145. #endif
  146. }
  147. /* common */
  148. void gp2x_init(void)
  149. {
  150. struct fb_fix_screeninfo fbfix;
  151. int ret;
  152. printf("entering init()\n"); fflush(stdout);
  153. memdev = open("/dev/mem", O_RDWR);
  154. if (memdev == -1)
  155. {
  156. printf("open(\"/dev/mem\") failed with %i\n", errno);
  157. exit(1);
  158. }
  159. /*
  160. memregs = mmap(0, 0x01000000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x48000000);
  161. if (memregs == MAP_FAILED)
  162. {
  163. printf("mmap(memregs) failed with %i\n", errno);
  164. exit(1);
  165. }
  166. */
  167. fbdev = open("/dev/fb0", O_RDWR);
  168. if (fbdev == -1)
  169. {
  170. printf("open(\"/dev/fb0\") failed with %i\n", errno);
  171. exit(1);
  172. }
  173. ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
  174. if (ret == -1)
  175. {
  176. printf("ioctl(fbdev) failed with %i\n", errno);
  177. exit(1);
  178. }
  179. // squidge hack
  180. if (fbfix.line_length != 800*2)
  181. {
  182. gp2x_screen = malloc(800*640*2);
  183. return;
  184. }
  185. screen = mmap(0, SCREEN_MAP_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0);
  186. if (screen == MAP_FAILED)
  187. {
  188. printf("mmap(fbptr) failed with %i\n", errno);
  189. exit(1);
  190. }
  191. printf("fbptr %p\n", screen);
  192. // gp2x_screen = (char *)screen + 800*10*2-64;
  193. gp2x_screen = screen;
  194. // snd
  195. mixerdev = open("/dev/mixer", O_RDWR);
  196. if (mixerdev == -1)
  197. printf("open(\"/dev/mixer\") failed with %i\n", errno);
  198. /* init usb joys -GnoStiC */
  199. gp2x_usbjoy_init();
  200. // touchscreen
  201. touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
  202. if (touchdev >= 0) {
  203. FILE *pcf = fopen("/etc/pointercal", "r");
  204. if (pcf) {
  205. fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1],
  206. &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]);
  207. fclose(pcf);
  208. }
  209. printf("found touchscreen/wm97xx\n");
  210. }
  211. printf("exitting init()\n"); fflush(stdout);
  212. }
  213. void gp2x_deinit(void)
  214. {
  215. //gp2x_video_changemode(15);
  216. if (screen != MAP_FAILED)
  217. munmap(screen, SCREEN_MAP_SIZE);
  218. if (memregs != MAP_FAILED)
  219. munmap((void *)memregs, 0x10000);
  220. close(memdev);
  221. if (fbdev >= 0) close(fbdev);
  222. if (mixerdev >= 0) close(mixerdev);
  223. if (sounddev >= 0) close(sounddev);
  224. if (touchdev >= 0) close(touchdev);
  225. gp2x_usbjoy_deinit();
  226. printf("all done");
  227. }
  228. /* lprintf */
  229. void lprintf(const char *fmt, ...)
  230. {
  231. va_list vl;
  232. va_start(vl, fmt);
  233. vprintf(fmt, vl);
  234. va_end(vl);
  235. }
  236. /* fake GP2X */
  237. int crashed_940 = 0;
  238. int readpng(void *dest, const char *fname, int what) { return -1; }
  239. void set_gamma(int g100, int A_SNs_curve) {}
  240. void set_FCLK(unsigned MHZ) {}
  241. void set_LCD_custom_rate(int rate) {}
  242. void unset_LCD_custom_rate(void) {}
  243. void Pause940(int yes) {}
  244. void Reset940(int yes, int bank) {}