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