| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <pthread.h> |
| 6 | #include <semaphore.h> |
| 7 | |
| 8 | #include <unistd.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <fcntl.h> |
| 12 | |
| 13 | #include "../common/emu.h" |
| 14 | #include "../common/menu.h" |
| 15 | #include "sndout_oss.h" |
| 16 | #include "version.h" |
| 17 | |
| 18 | #include "log_io.h" |
| 19 | |
| 20 | unsigned long current_keys = 0; |
| 21 | static int current_bpp = 16; |
| 22 | static int current_pal[256]; |
| 23 | static const char *verstring = "PicoDrive " VERSION; |
| 24 | static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT; |
| 25 | |
| 26 | /* ifndef is for qemu build without video out */ |
| 27 | #ifndef ARM |
| 28 | |
| 29 | /* faking GP2X pad */ |
| 30 | enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40, |
| 31 | GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11, |
| 32 | GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15, |
| 33 | GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 }; |
| 34 | |
| 35 | static void key_press_event(int keycode) |
| 36 | { |
| 37 | switch (keycode) |
| 38 | { |
| 39 | case 111: |
| 40 | case 0x62: current_keys |= GP2X_UP; break; |
| 41 | case 116: |
| 42 | case 0x68: current_keys |= GP2X_DOWN; break; |
| 43 | case 113: |
| 44 | case 0x64: current_keys |= GP2X_LEFT; break; |
| 45 | case 114: |
| 46 | case 0x66: current_keys |= GP2X_RIGHT; break; |
| 47 | case 0x24: current_keys |= GP2X_START; break; // enter |
| 48 | case 0x23: current_keys |= GP2X_SELECT;break; // ] |
| 49 | case 0x34: current_keys |= GP2X_A; break; // z |
| 50 | case 0x35: current_keys |= GP2X_X; break; // x |
| 51 | case 0x36: current_keys |= GP2X_B; break; // c |
| 52 | case 0x37: current_keys |= GP2X_Y; break; // v |
| 53 | case 0x27: current_keys |= GP2X_L; break; // s |
| 54 | case 0x28: current_keys |= GP2X_R; break; // d |
| 55 | case 0x29: current_keys |= GP2X_PUSH; break; // f |
| 56 | case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q |
| 57 | case 0x19: current_keys |= GP2X_VOL_UP;break; // w |
| 58 | case 0x2d: log_io_clear(); break; // k |
| 59 | case 0x2e: log_io_dump(); break; // l |
| 60 | case 0x17: { // tab |
| 61 | extern int PicoReset(void); |
| 62 | PicoReset(); |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void key_release_event(int keycode) |
| 69 | { |
| 70 | switch (keycode) |
| 71 | { |
| 72 | case 111: |
| 73 | case 0x62: current_keys &= ~GP2X_UP; break; |
| 74 | case 116: |
| 75 | case 0x68: current_keys &= ~GP2X_DOWN; break; |
| 76 | case 113: |
| 77 | case 0x64: current_keys &= ~GP2X_LEFT; break; |
| 78 | case 114: |
| 79 | case 0x66: current_keys &= ~GP2X_RIGHT; break; |
| 80 | case 0x24: current_keys &= ~GP2X_START; break; // enter |
| 81 | case 0x23: current_keys &= ~GP2X_SELECT;break; // ] |
| 82 | case 0x34: current_keys &= ~GP2X_A; break; // z |
| 83 | case 0x35: current_keys &= ~GP2X_X; break; // x |
| 84 | case 0x36: current_keys &= ~GP2X_B; break; // c |
| 85 | case 0x37: current_keys &= ~GP2X_Y; break; // v |
| 86 | case 0x27: current_keys &= ~GP2X_L; break; // s |
| 87 | case 0x28: current_keys &= ~GP2X_R; break; // d |
| 88 | case 0x29: current_keys &= ~GP2X_PUSH; break; // f |
| 89 | case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q |
| 90 | case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* --- */ |
| 95 | |
| 96 | #include <X11/Xlib.h> |
| 97 | #include <X11/Xutil.h> |
| 98 | |
| 99 | static Display *xlib_display; |
| 100 | static Window xlib_window; |
| 101 | static XImage *ximage; |
| 102 | |
| 103 | static void ximage_realloc(Display *display, Visual *visual) |
| 104 | { |
| 105 | void *xlib_screen; |
| 106 | |
| 107 | XLockDisplay(xlib_display); |
| 108 | |
| 109 | if (ximage != NULL) |
| 110 | XDestroyImage(ximage); |
| 111 | ximage = NULL; |
| 112 | |
| 113 | xlib_screen = calloc(scr_w * scr_h, 4); |
| 114 | if (xlib_screen != NULL) |
| 115 | ximage = XCreateImage(display, visual, 24, ZPixmap, 0, |
| 116 | xlib_screen, scr_w, scr_h, 32, 0); |
| 117 | if (ximage == NULL) |
| 118 | fprintf(stderr, "failed to alloc ximage\n"); |
| 119 | |
| 120 | XUnlockDisplay(xlib_display); |
| 121 | } |
| 122 | |
| 123 | static void xlib_update(void) |
| 124 | { |
| 125 | Status xstatus; |
| 126 | |
| 127 | XLockDisplay(xlib_display); |
| 128 | |
| 129 | xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage, |
| 130 | 0, 0, 0, 0, g_screen_width, g_screen_height); |
| 131 | if (xstatus != 0) |
| 132 | fprintf(stderr, "XPutImage %d\n", xstatus); |
| 133 | |
| 134 | XUnlockDisplay(xlib_display); |
| 135 | } |
| 136 | |
| 137 | static void *xlib_threadf(void *targ) |
| 138 | { |
| 139 | unsigned int width, height, display_width, display_height; |
| 140 | sem_t *sem = targ; |
| 141 | XTextProperty windowName; |
| 142 | Window win; |
| 143 | XEvent report; |
| 144 | Display *display; |
| 145 | Visual *visual; |
| 146 | int screen; |
| 147 | |
| 148 | XInitThreads(); |
| 149 | |
| 150 | xlib_display = display = XOpenDisplay(NULL); |
| 151 | if (display == NULL) |
| 152 | { |
| 153 | fprintf(stderr, "cannot connect to X server %s\n", |
| 154 | XDisplayName(NULL)); |
| 155 | sem_post(sem); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | visual = DefaultVisual(display, 0); |
| 160 | if (visual->class != TrueColor) |
| 161 | { |
| 162 | fprintf(stderr, "cannot handle non true color visual\n"); |
| 163 | XCloseDisplay(display); |
| 164 | sem_post(sem); |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | printf("X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display), |
| 169 | VendorRelease(display), DisplayString(display), ProtocolVersion(display), |
| 170 | ProtocolRevision(display)); |
| 171 | |
| 172 | screen = DefaultScreen(display); |
| 173 | |
| 174 | ximage_realloc(display, visual); |
| 175 | sem_post(sem); |
| 176 | |
| 177 | display_width = DisplayWidth(display, screen); |
| 178 | display_height = DisplayHeight(display, screen); |
| 179 | |
| 180 | xlib_window = win = XCreateSimpleWindow(display, |
| 181 | RootWindow(display, screen), |
| 182 | display_width / 2 - scr_w / 2, |
| 183 | display_height / 2 - scr_h / 2, |
| 184 | scr_w + 2, scr_h + 2, 1, |
| 185 | BlackPixel(display, screen), |
| 186 | BlackPixel(display, screen)); |
| 187 | |
| 188 | XStringListToTextProperty((char **)&verstring, 1, &windowName); |
| 189 | XSetWMName(display, win, &windowName); |
| 190 | |
| 191 | XSelectInput(display, win, ExposureMask | |
| 192 | KeyPressMask | KeyReleaseMask | |
| 193 | StructureNotifyMask); |
| 194 | |
| 195 | XMapWindow(display, win); |
| 196 | |
| 197 | while (1) |
| 198 | { |
| 199 | XNextEvent(display, &report); |
| 200 | switch (report.type) |
| 201 | { |
| 202 | case Expose: |
| 203 | while (XCheckTypedEvent(display, Expose, &report)) |
| 204 | ; |
| 205 | xlib_update(); |
| 206 | break; |
| 207 | |
| 208 | case ConfigureNotify: |
| 209 | width = report.xconfigure.width; |
| 210 | height = report.xconfigure.height; |
| 211 | if (scr_w != width - 2 || scr_h != height - 2) { |
| 212 | scr_w = width - 2; |
| 213 | scr_h = height - 2; |
| 214 | scr_changed = 1; |
| 215 | } |
| 216 | break; |
| 217 | |
| 218 | case ButtonPress: |
| 219 | break; |
| 220 | |
| 221 | case KeyPress: |
| 222 | key_press_event(report.xkey.keycode); |
| 223 | break; |
| 224 | |
| 225 | case KeyRelease: |
| 226 | key_release_event(report.xkey.keycode); |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static void xlib_init(void) |
| 236 | { |
| 237 | pthread_t x_thread; |
| 238 | sem_t xlib_sem; |
| 239 | |
| 240 | sem_init(&xlib_sem, 0, 0); |
| 241 | |
| 242 | pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem); |
| 243 | pthread_detach(x_thread); |
| 244 | |
| 245 | sem_wait(&xlib_sem); |
| 246 | sem_destroy(&xlib_sem); |
| 247 | } |
| 248 | #endif // !ARM |
| 249 | |
| 250 | /* --- */ |
| 251 | |
| 252 | static void realloc_screen(void) |
| 253 | { |
| 254 | void *old = g_screen_ptr; |
| 255 | g_screen_width = scr_w; |
| 256 | g_screen_height = scr_h; |
| 257 | g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1); |
| 258 | free(old); |
| 259 | scr_changed = 0; |
| 260 | } |
| 261 | |
| 262 | void update_screen(void) |
| 263 | { |
| 264 | #ifndef ARM |
| 265 | unsigned int *image; |
| 266 | int pixel_count, i; |
| 267 | |
| 268 | if (ximage == NULL) |
| 269 | return; |
| 270 | |
| 271 | pixel_count = g_screen_width * g_screen_height; |
| 272 | image = (void *)ximage->data; |
| 273 | |
| 274 | if (current_bpp == 8) |
| 275 | { |
| 276 | unsigned char *pixels = g_screen_ptr; |
| 277 | int pix; |
| 278 | |
| 279 | for (i = 0; i < pixel_count; i++) |
| 280 | { |
| 281 | pix = current_pal[pixels[i]]; |
| 282 | image[i] = pix; |
| 283 | } |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | unsigned short *pixels = g_screen_ptr; |
| 288 | |
| 289 | for (i = 0; i < pixel_count; i++) |
| 290 | { |
| 291 | /* in: rrrr rggg gggb bbbb */ |
| 292 | /* out: rrrr r000 gggg gg00 bbbb b000 */ |
| 293 | image[i] = (pixels[i] << 8) & 0xf80000; |
| 294 | image[i] |= (pixels[i] << 5) & 0x00fc00; |
| 295 | image[i] |= (pixels[i] << 3) & 0x0000f8; |
| 296 | } |
| 297 | } |
| 298 | xlib_update(); |
| 299 | |
| 300 | if (scr_changed) { |
| 301 | realloc_screen(); |
| 302 | ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0)); |
| 303 | } |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | void plat_early_init(void) |
| 308 | { |
| 309 | } |
| 310 | |
| 311 | void plat_init(void) |
| 312 | { |
| 313 | realloc_screen(); |
| 314 | memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2); |
| 315 | |
| 316 | // snd |
| 317 | sndout_oss_init(); |
| 318 | |
| 319 | #ifndef ARM |
| 320 | xlib_init(); |
| 321 | #endif |
| 322 | } |
| 323 | |
| 324 | void plat_finish(void) |
| 325 | { |
| 326 | free(g_screen_ptr); |
| 327 | sndout_oss_exit(); |
| 328 | } |
| 329 | |
| 330 | /* misc */ |
| 331 | int mp3_get_bitrate(FILE *f, int size) |
| 332 | { |
| 333 | return 128; |
| 334 | } |
| 335 | |
| 336 | void mp3_start_play(FILE *f, int pos) |
| 337 | { |
| 338 | } |
| 339 | |
| 340 | void mp3_update(int *buffer, int length, int stereo) |
| 341 | { |
| 342 | } |
| 343 | |
| 344 | /* lprintf */ |
| 345 | void lprintf(const char *fmt, ...) |
| 346 | { |
| 347 | va_list vl; |
| 348 | |
| 349 | va_start(vl, fmt); |
| 350 | vprintf(fmt, vl); |
| 351 | va_end(vl); |
| 352 | } |
| 353 | |