bin_to_cso_mp3 improved
[picodrive.git] / platform / linux / gp2x.c
1 /* faking/emulating gp2x.c 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 <sys/ioctl.h>
13 #include <sys/soundcard.h>
14 #include <fcntl.h>
15 #include <errno.h>
16
17 #include "../gp2x/emu.h"
18 #include "../gp2x/gp2x.h"
19 #include "../gp2x/usbjoy.h"
20 #include "../gp2x/version.h"
21
22 #include "log_io.h"
23
24 void *gp2x_screen;
25 static int current_bpp = 8;
26 static int current_pal[256];
27 static unsigned long current_keys = 0;
28 static int sounddev = 0, mixerdev = 0;
29 static const char *verstring = "PicoDrive " VERSION;
30
31 // dummies
32 char *ext_menu = 0, *ext_state = 0;
33 int mix_32_to_16l_level;
34
35 /* gtk */
36 struct gtk_global_struct
37 {
38         GtkWidget *window;
39         GtkWidget *pixmap1;
40 } gtk_items;
41
42
43 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
44 {
45         return FALSE;
46 }
47
48 static void destroy (GtkWidget *widget, gpointer data)
49 {
50         gtk_main_quit ();
51 }
52
53 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
54 {
55         switch (event->hardware_keycode)
56         {
57                 case 0x62: current_keys |= GP2X_UP;    break;
58                 case 0x68: current_keys |= GP2X_DOWN;  break;
59                 case 0x64: current_keys |= GP2X_LEFT;  break;
60                 case 0x66: current_keys |= GP2X_RIGHT; break;
61                 case 0x24: current_keys |= GP2X_START; break; // enter
62                 case 0x23: current_keys |= GP2X_SELECT;break; // ]
63                 case 0x34: current_keys |= GP2X_A;     break; // z
64                 case 0x35: current_keys |= GP2X_X;     break; // x
65                 case 0x36: current_keys |= GP2X_B;     break; // c
66                 case 0x37: current_keys |= GP2X_Y;     break; // v
67                 case 0x27: current_keys |= GP2X_L;     break; // s
68                 case 0x28: current_keys |= GP2X_R;     break; // d
69                 case 0x29: current_keys |= GP2X_PUSH;  break; // f
70                 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
71                 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
72                 case 0x2d: log_io_clear(); break; // k
73                 case 0x2e: log_io_dump();  break; // l
74                 case 0x17: { // tab
75                         extern int PicoReset(void);
76                         PicoReset();
77                         break;
78                 }
79         }
80
81         return 0;
82 }
83
84 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
85 {
86         switch (event->hardware_keycode)
87         {
88                 case 0x62: current_keys &= ~GP2X_UP;    break;
89                 case 0x68: current_keys &= ~GP2X_DOWN;  break;
90                 case 0x64: current_keys &= ~GP2X_LEFT;  break;
91                 case 0x66: current_keys &= ~GP2X_RIGHT; break;
92                 case 0x24: current_keys &= ~GP2X_START; break; // enter
93                 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
94                 case 0x34: current_keys &= ~GP2X_A;     break; // z
95                 case 0x35: current_keys &= ~GP2X_X;     break; // x
96                 case 0x36: current_keys &= ~GP2X_B;     break; // c
97                 case 0x37: current_keys &= ~GP2X_Y;     break; // v
98                 case 0x27: current_keys &= ~GP2X_L;     break; // s
99                 case 0x28: current_keys &= ~GP2X_R;     break; // d
100                 case 0x29: current_keys &= ~GP2X_PUSH;  break; // f
101                 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
102                 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
103         }
104
105         return 0;
106 }
107
108 static void *gtk_threadf(void *targ)
109 {
110         int argc = 0;
111         char *argv[] = { "" };
112         GtkWidget *box;
113         sem_t *sem = targ;
114
115         g_thread_init (NULL);
116         gdk_threads_init ();
117         gdk_set_locale ();
118         gtk_init (&argc, (char ***) &argv);
119
120         /* create new window */
121         gtk_items.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
122         g_signal_connect (G_OBJECT (gtk_items.window), "delete_event",
123                         G_CALLBACK (delete_event), NULL);
124
125         g_signal_connect (G_OBJECT (gtk_items.window), "destroy",
126                         G_CALLBACK (destroy), NULL);
127
128         g_signal_connect (G_OBJECT (gtk_items.window), "key_press_event",
129                         G_CALLBACK (key_press_event), NULL);
130
131         g_signal_connect (G_OBJECT (gtk_items.window), "key_release_event",
132                         G_CALLBACK (key_release_event), NULL);
133
134         gtk_container_set_border_width (GTK_CONTAINER (gtk_items.window), 2);
135         gtk_window_set_title ((GtkWindow *) gtk_items.window, verstring);
136
137         box = gtk_hbox_new(FALSE, 0);
138         gtk_widget_show(box);
139         gtk_container_add (GTK_CONTAINER (gtk_items.window), box);
140
141         /* live pixmap */
142         gtk_items.pixmap1 = gtk_image_new ();
143         gtk_container_add (GTK_CONTAINER (box), gtk_items.pixmap1);
144         gtk_widget_show (gtk_items.pixmap1);
145         gtk_widget_set_size_request (gtk_items.pixmap1, 320, 240);
146
147         gtk_widget_show  (gtk_items.window);
148
149         sem_post(sem);
150
151         gtk_main();
152
153         printf("linux: gtk thread finishing\n");
154         exit(1);
155
156         return NULL;
157 }
158
159 static void gtk_initf(void)
160 {
161         pthread_t gtk_thread;
162         sem_t sem;
163         sem_init(&sem, 0, 0);
164
165         pthread_create(&gtk_thread, NULL, gtk_threadf, &sem);
166         pthread_detach(gtk_thread);
167
168         sem_wait(&sem);
169         sem_close(&sem);
170 }
171
172 void finalize_image(guchar *pixels, gpointer data)
173 {
174         free(pixels);
175 }
176
177 /* --- */
178
179 void gp2x_init(void)
180 {
181         printf("entering init()\n"); fflush(stdout);
182
183         gp2x_screen = malloc(320*240*2 + 320*2);
184         memset(gp2x_screen, 0, 320*240*2 + 320*2);
185
186         // snd
187         mixerdev = open("/dev/mixer", O_RDWR);
188         if (mixerdev == -1)
189                 printf("open(\"/dev/mixer\") failed with %i\n", errno);
190
191         gtk_initf();
192
193         gp2x_usbjoy_init();
194
195         printf("exitting init()\n"); fflush(stdout);
196 }
197
198 void gp2x_deinit(void)
199 {
200         free(gp2x_screen);
201         if (sounddev > 0) close(sounddev);
202         close(mixerdev);
203         gp2x_usbjoy_deinit();
204 }
205
206 /* video */
207 void gp2x_video_flip(void)
208 {
209         GdkPixbuf       *pixbuf;
210         unsigned char   *image;
211         int             i;
212
213         gdk_threads_enter();
214
215         image = malloc (320*240*3);
216         if (image == NULL)
217         {
218                 gdk_threads_leave();
219                 return;
220         }
221
222         if (current_bpp == 8)
223         {
224                 unsigned char *pixels = gp2x_screen;
225                 int pix;
226
227                 for (i = 0; i < 320*240; i++)
228                 {
229                         pix = current_pal[pixels[i]];
230                         image[3 * i + 0] = pix >> 16;
231                         image[3 * i + 1] = pix >>  8;
232                         image[3 * i + 2] = pix;
233                 }
234         }
235         else
236         {
237                 unsigned short *pixels = gp2x_screen;
238
239                 for (i = 0; i < 320*240; i++)
240                 {
241                         /*  in:           rrrr rggg gggb bbbb */
242                         /* out: rrrr r000 gggg gg00 bbbb b000 */
243                         image[3 * i + 0] = (pixels[i] >> 8) & 0xf8;
244                         image[3 * i + 1] = (pixels[i] >> 3) & 0xfc;
245                         image[3 * i + 2] = (pixels[i] << 3);
246                 }
247         }
248
249         pixbuf = gdk_pixbuf_new_from_data (image, GDK_COLORSPACE_RGB,
250                         FALSE, 8, 320, 240, 320*3, finalize_image, NULL);
251         gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_items.pixmap1), pixbuf);
252         g_object_unref (pixbuf);
253
254         gdk_threads_leave();
255 }
256
257 void gp2x_video_flip2(void)
258 {
259         gp2x_video_flip();
260 }
261
262 void gp2x_video_changemode(int bpp)
263 {
264         current_bpp = bpp;
265 }
266
267 void gp2x_video_changemode2(int bpp)
268 {
269         current_bpp = bpp;
270 }
271
272 void gp2x_video_setpalette(int *pal, int len)
273 {
274         memcpy(current_pal, pal, len*4);
275 }
276
277 void gp2x_video_flush_cache(void)
278 {
279 }
280
281 void gp2x_video_RGB_setscaling(int v_offs, int W, int H)
282 {
283 }
284
285 void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
286 {
287         if ((char *)gp2x_screen + offset != data)
288                 memcpy((char *)gp2x_screen + offset, data, len);
289 }
290
291 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
292 {
293         memcpy((char *)gp2x_screen + offset, data, len);
294 }
295
296
297 void gp2x_memset_all_buffers(int offset, int byte, int len)
298 {
299         memset((char *)gp2x_screen + offset, byte, len);
300 }
301
302 void gp2x_pd_clone_buffer2(void)
303 {
304         memset(gp2x_screen, 0, 320*240*2);
305 }
306
307 /* sound */
308 static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
309
310 void gp2x_start_sound(int rate, int bits, int stereo)
311 {
312         int frag = 0, bsize, buffers;
313
314         // if no settings change, we don't need to do anything
315         if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
316
317         if (sounddev > 0) close(sounddev);
318         sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
319         if (sounddev == -1)
320                 printf("open(\"/dev/dsp\") failed with %i\n", errno);
321
322         ioctl(sounddev, SNDCTL_DSP_SPEED,  &rate);
323         ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
324         ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
325         // calculate buffer size
326         buffers = 16;
327         bsize = rate / 32;
328         if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
329         while ((bsize>>=1)) frag++;
330         frag |= buffers<<16; // 16 buffers
331         ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
332         printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
333                 rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
334
335         s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
336 }
337
338 void gp2x_sound_write(void *buff, int len)
339 {
340         write(sounddev, buff, len);
341 }
342
343 void gp2x_sound_sync(void)
344 {
345         ioctl(sounddev, SOUND_PCM_SYNC, 0);
346 }
347
348 void gp2x_sound_volume(int l, int r)
349 {
350         l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
351         l<<=8; l|=r;
352         ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
353 }
354
355 /* joy */
356 unsigned long gp2x_joystick_read(int allow_usb_joy)
357 {
358         unsigned long value = current_keys;
359         int i;
360
361         if (allow_usb_joy && num_of_joys > 0) {
362                 // check the usb joy as well..
363                 gp2x_usbjoy_update();
364                 for (i = 0; i < num_of_joys; i++)
365                         value |= gp2x_usbjoy_check(i);
366         }
367
368         return value;
369 }
370
371 int gp2x_touchpad_read(int *x, int *y)
372 {
373         return -1;
374 }
375
376 /* 940 */
377 int crashed_940 = 0;
378 void Pause940(int yes)
379 {
380 }
381
382 void Reset940(int yes, int bank)
383 {
384 }
385
386 /* faking gp2x cpuctrl.c */
387 void cpuctrl_init(void)
388 {
389 }
390
391 void cpuctrl_deinit(void)
392 {
393 }
394
395 void set_FCLK(unsigned MHZ)
396 {
397 }
398
399 void Disable_940(void)
400 {
401 }
402
403 void gp2x_video_wait_vsync(void)
404 {
405 }
406
407 void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
408 {
409 }
410
411 void set_gamma(int g100, int A_SNs_curve)
412 {
413 }
414
415 void set_LCD_custom_rate(int rate)
416 {
417 }
418
419 void unset_LCD_custom_rate(void)
420 {
421 }
422
423 /* squidgehack.c */
424 int mmuhack(void)
425 {
426         return 0;
427 }
428
429
430 int mmuunhack(void)
431 {
432         return 0;
433 }
434
435
436 /* misc */
437 void spend_cycles(int c)
438 {
439         usleep(c/200);
440 }
441
442 /* lprintf */
443 void lprintf(const char *fmt, ...)
444 {
445         va_list vl;
446
447         va_start(vl, fmt);
448         vprintf(fmt, vl);
449         va_end(vl);
450 }
451