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