| 1 | /* faking/emulating gp2x by using gtk */ |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <pthread.h> |
| 6 | #include <semaphore.h> |
| 7 | #include <gtk/gtk.h> |
| 8 | |
| 9 | #include <unistd.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <errno.h> |
| 14 | |
| 15 | #include "../gp2x/emu.h" |
| 16 | //#include "../gp2x/gp2x.h" |
| 17 | #include "../gp2x/version.h" |
| 18 | #include "../common/emu.h" |
| 19 | #include "sndout_oss.h" |
| 20 | |
| 21 | #include "log_io.h" |
| 22 | |
| 23 | unsigned long current_keys = 0; |
| 24 | static int current_bpp = 8; |
| 25 | static int current_pal[256]; |
| 26 | static const char *verstring = "PicoDrive " VERSION; |
| 27 | static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT; |
| 28 | |
| 29 | // dummies |
| 30 | int mix_32_to_16l_level; |
| 31 | |
| 32 | /* gtk */ |
| 33 | struct gtk_global_struct |
| 34 | { |
| 35 | GtkWidget *window; |
| 36 | GtkWidget *pixmap1; |
| 37 | } gtk_items; |
| 38 | |
| 39 | |
| 40 | static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data) |
| 41 | { |
| 42 | return FALSE; |
| 43 | } |
| 44 | |
| 45 | static void destroy (GtkWidget *widget, gpointer data) |
| 46 | { |
| 47 | gtk_main_quit (); |
| 48 | } |
| 49 | |
| 50 | /* faking GP2X pad */ |
| 51 | enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40, |
| 52 | GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11, |
| 53 | GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15, |
| 54 | GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 }; |
| 55 | |
| 56 | static gint key_press_event (GtkWidget *widget, GdkEventKey *event) |
| 57 | { |
| 58 | switch (event->hardware_keycode) |
| 59 | { |
| 60 | case 111: |
| 61 | case 0x62: current_keys |= GP2X_UP; break; |
| 62 | case 116: |
| 63 | case 0x68: current_keys |= GP2X_DOWN; break; |
| 64 | case 113: |
| 65 | case 0x64: current_keys |= GP2X_LEFT; break; |
| 66 | case 114: |
| 67 | case 0x66: current_keys |= GP2X_RIGHT; break; |
| 68 | case 0x24: current_keys |= GP2X_START; break; // enter |
| 69 | case 0x23: current_keys |= GP2X_SELECT;break; // ] |
| 70 | case 0x34: current_keys |= GP2X_A; break; // z |
| 71 | case 0x35: current_keys |= GP2X_X; break; // x |
| 72 | case 0x36: current_keys |= GP2X_B; break; // c |
| 73 | case 0x37: current_keys |= GP2X_Y; break; // v |
| 74 | case 0x27: current_keys |= GP2X_L; break; // s |
| 75 | case 0x28: current_keys |= GP2X_R; break; // d |
| 76 | case 0x29: current_keys |= GP2X_PUSH; break; // f |
| 77 | case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q |
| 78 | case 0x19: current_keys |= GP2X_VOL_UP;break; // w |
| 79 | case 0x2d: log_io_clear(); break; // k |
| 80 | case 0x2e: log_io_dump(); break; // l |
| 81 | case 0x17: { // tab |
| 82 | extern int PicoReset(void); |
| 83 | PicoReset(); |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static gint key_release_event (GtkWidget *widget, GdkEventKey *event) |
| 92 | { |
| 93 | switch (event->hardware_keycode) |
| 94 | { |
| 95 | case 111: |
| 96 | case 0x62: current_keys &= ~GP2X_UP; break; |
| 97 | case 116: |
| 98 | case 0x68: current_keys &= ~GP2X_DOWN; break; |
| 99 | case 113: |
| 100 | case 0x64: current_keys &= ~GP2X_LEFT; break; |
| 101 | case 114: |
| 102 | case 0x66: current_keys &= ~GP2X_RIGHT; break; |
| 103 | case 0x24: current_keys &= ~GP2X_START; break; // enter |
| 104 | case 0x23: current_keys &= ~GP2X_SELECT;break; // ] |
| 105 | case 0x34: current_keys &= ~GP2X_A; break; // z |
| 106 | case 0x35: current_keys &= ~GP2X_X; break; // x |
| 107 | case 0x36: current_keys &= ~GP2X_B; break; // c |
| 108 | case 0x37: current_keys &= ~GP2X_Y; break; // v |
| 109 | case 0x27: current_keys &= ~GP2X_L; break; // s |
| 110 | case 0x28: current_keys &= ~GP2X_R; break; // d |
| 111 | case 0x29: current_keys &= ~GP2X_PUSH; break; // f |
| 112 | case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q |
| 113 | case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static void size_allocate_event(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data) |
| 120 | { |
| 121 | // printf("%dx%d\n", allocation->width, allocation->height); |
| 122 | if (scr_w != allocation->width - 2 || scr_h != allocation->height - 2) { |
| 123 | scr_w = allocation->width - 2; |
| 124 | scr_h = allocation->height - 2; |
| 125 | scr_changed = 1; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void *gtk_threadf(void *targ) |
| 130 | { |
| 131 | int argc = 0; |
| 132 | char *argv[] = { "" }; |
| 133 | GtkWidget *box; |
| 134 | sem_t *sem = targ; |
| 135 | |
| 136 | g_thread_init (NULL); |
| 137 | gdk_threads_init (); |
| 138 | gdk_set_locale (); |
| 139 | gtk_init (&argc, (char ***) &argv); |
| 140 | |
| 141 | /* create new window */ |
| 142 | gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 143 | g_signal_connect (G_OBJECT (gtk_items.window), "delete_event", |
| 144 | G_CALLBACK (delete_event), NULL); |
| 145 | |
| 146 | g_signal_connect (G_OBJECT (gtk_items.window), "destroy", |
| 147 | G_CALLBACK (destroy), NULL); |
| 148 | |
| 149 | g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event", |
| 150 | G_CALLBACK (key_press_event), NULL); |
| 151 | |
| 152 | g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event", |
| 153 | G_CALLBACK (key_release_event), NULL); |
| 154 | |
| 155 | g_signal_connect (G_OBJECT (gtk_items.window), "size_allocate", |
| 156 | G_CALLBACK (size_allocate_event), NULL); |
| 157 | |
| 158 | gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 1); |
| 159 | gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring); |
| 160 | |
| 161 | box = gtk_hbox_new(FALSE, 0); |
| 162 | gtk_widget_show(box); |
| 163 | gtk_container_add (GTK_CONTAINER (gtk_items.window), box); |
| 164 | |
| 165 | /* live pixmap */ |
| 166 | gtk_items.pixmap1 = gtk_image_new (); |
| 167 | gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1); |
| 168 | gtk_widget_show (gtk_items.pixmap1); |
| 169 | gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240); |
| 170 | |
| 171 | gtk_widget_show (gtk_items.window); |
| 172 | |
| 173 | sem_post(sem); |
| 174 | |
| 175 | gtk_main(); |
| 176 | |
| 177 | printf("linux: gtk thread finishing\n"); |
| 178 | exit(1); |
| 179 | |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | static void gtk_initf(void) |
| 184 | { |
| 185 | pthread_t gtk_thread; |
| 186 | sem_t sem; |
| 187 | sem_init(&sem, 0, 0); |
| 188 | |
| 189 | pthread_create(>k_thread, NULL, gtk_threadf, &sem); |
| 190 | pthread_detach(gtk_thread); |
| 191 | |
| 192 | sem_wait(&sem); |
| 193 | sem_close(&sem); |
| 194 | } |
| 195 | |
| 196 | void finalize_image(guchar *pixels, gpointer data) |
| 197 | { |
| 198 | free(pixels); |
| 199 | } |
| 200 | |
| 201 | /* --- */ |
| 202 | |
| 203 | static void realloc_screen(void) |
| 204 | { |
| 205 | void *old = g_screen_ptr; |
| 206 | g_screen_width = scr_w; |
| 207 | g_screen_height = scr_h; |
| 208 | g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1); |
| 209 | free(old); |
| 210 | scr_changed = 0; |
| 211 | } |
| 212 | |
| 213 | void plat_init(void) |
| 214 | { |
| 215 | printf("entering init()\n"); fflush(stdout); |
| 216 | |
| 217 | realloc_screen(); |
| 218 | memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2); |
| 219 | |
| 220 | // snd |
| 221 | sndout_oss_init(); |
| 222 | |
| 223 | gtk_initf(); |
| 224 | |
| 225 | printf("exitting init()\n"); fflush(stdout); |
| 226 | } |
| 227 | |
| 228 | void plat_finish(void) |
| 229 | { |
| 230 | free(g_screen_ptr); |
| 231 | sndout_oss_exit(); |
| 232 | } |
| 233 | |
| 234 | /* video */ |
| 235 | void gp2x_video_flip(void) |
| 236 | { |
| 237 | GdkPixbuf *pixbuf; |
| 238 | unsigned char *image; |
| 239 | int pixel_count, i; |
| 240 | |
| 241 | pixel_count = g_screen_width * g_screen_height; |
| 242 | |
| 243 | gdk_threads_enter(); |
| 244 | |
| 245 | image = malloc(pixel_count * 3); |
| 246 | if (image == NULL) |
| 247 | { |
| 248 | gdk_threads_leave(); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | if (current_bpp == 8) |
| 253 | { |
| 254 | unsigned char *pixels = g_screen_ptr; |
| 255 | int pix; |
| 256 | |
| 257 | for (i = 0; i < pixel_count; i++) |
| 258 | { |
| 259 | pix = current_pal[pixels[i]]; |
| 260 | image[3 * i + 0] = pix >> 16; |
| 261 | image[3 * i + 1] = pix >> 8; |
| 262 | image[3 * i + 2] = pix; |
| 263 | } |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | unsigned short *pixels = g_screen_ptr; |
| 268 | |
| 269 | for (i = 0; i < pixel_count; i++) |
| 270 | { |
| 271 | /* in: rrrr rggg gggb bbbb */ |
| 272 | /* out: rrrr r000 gggg gg00 bbbb b000 */ |
| 273 | image[3 * i + 0] = (pixels[i] >> 8) & 0xf8; |
| 274 | image[3 * i + 1] = (pixels[i] >> 3) & 0xfc; |
| 275 | image[3 * i + 2] = (pixels[i] << 3); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB, |
| 280 | FALSE, 8, g_screen_width, g_screen_height, |
| 281 | g_screen_width * 3, finalize_image, NULL); |
| 282 | gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf); |
| 283 | g_object_unref (pixbuf); |
| 284 | |
| 285 | gdk_threads_leave(); |
| 286 | |
| 287 | if (scr_changed) |
| 288 | realloc_screen(); |
| 289 | } |
| 290 | |
| 291 | void gp2x_video_flip2(void) |
| 292 | { |
| 293 | gp2x_video_flip(); |
| 294 | } |
| 295 | |
| 296 | void gp2x_video_changemode(int bpp) |
| 297 | { |
| 298 | current_bpp = bpp; |
| 299 | } |
| 300 | |
| 301 | void gp2x_video_changemode2(int bpp) |
| 302 | { |
| 303 | current_bpp = bpp; |
| 304 | } |
| 305 | |
| 306 | void gp2x_video_setpalette(int *pal, int len) |
| 307 | { |
| 308 | memcpy(current_pal, pal, len*4); |
| 309 | } |
| 310 | |
| 311 | void gp2x_video_RGB_setscaling(int v_offs, int W, int H) |
| 312 | { |
| 313 | } |
| 314 | |
| 315 | void gp2x_memset_all_buffers(int offset, int byte, int len) |
| 316 | { |
| 317 | memset((char *)g_screen_ptr + offset, byte, len); |
| 318 | } |
| 319 | |
| 320 | /* joy */ |
| 321 | int gp2x_touchpad_read(int *x, int *y) |
| 322 | { |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | /* 940 */ |
| 327 | int crashed_940 = 0; |
| 328 | void pause940(int yes) |
| 329 | { |
| 330 | } |
| 331 | |
| 332 | void reset940(int yes, int bank) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | void gp2x_video_wait_vsync(void) |
| 337 | { |
| 338 | } |
| 339 | |
| 340 | void set_gamma(int g100, int A_SNs_curve) |
| 341 | { |
| 342 | } |
| 343 | |
| 344 | void set_lcd_custom_rate(int rate) |
| 345 | { |
| 346 | } |
| 347 | |
| 348 | void unset_lcd_custom_rate(void) |
| 349 | { |
| 350 | } |
| 351 | |
| 352 | /* misc */ |
| 353 | void spend_cycles(int c) |
| 354 | { |
| 355 | usleep(c/200); |
| 356 | } |
| 357 | |
| 358 | /* lprintf */ |
| 359 | void lprintf(const char *fmt, ...) |
| 360 | { |
| 361 | va_list vl; |
| 362 | |
| 363 | va_start(vl, fmt); |
| 364 | vprintf(fmt, vl); |
| 365 | va_end(vl); |
| 366 | } |
| 367 | |