forked from meetpatty/basiliskii-vita
/
video_psp.cpp
1522 lines (1315 loc) · 40.9 KB
/
video_psp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* video_psp.cpp - Video/graphics emulation, PSP specific stuff
*
* Basilisk II (C) 1997-2005 Christian Bauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <psp2/ctrl.h>
#include <psp2/touch.h>
#include <psp2/hid.h>
#include <vita2d.h>
#include <malloc.h>
#include "sysdeps.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <vector>
#include "danzeff/danzeff.h"
#include "cpu_emulation.h"
#include "main.h"
#include "adb.h"
#include "macos_util.h"
#include "prefs.h"
#include "user_strings.h"
#include "video.h"
#include "video_defs.h"
#define DEBUG 0
#include "debug.h"
#define KERNELMODE 0 /* 1 is untested but required for some keyboards to change baudrate */
// Supported video modes
using std::vector;
static vector<video_mode> VideoModes;
// Constants
const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
const char QUAL_MSG[5][12] = {
"\0",
" CMD ",
" OPT ",
" CTRL ",
" CMD+OPT "
};
// Global variables
static unsigned int __attribute__((aligned(16))) clut256[256];
static unsigned int __attribute__((aligned(16))) clut0_15[256];
static unsigned int __attribute__((aligned(16))) clut1_15[256];
static unsigned int __attribute__((aligned(16))) clut2_15[256];
static unsigned int __attribute__((aligned(16))) clut0_24[256];
static unsigned int __attribute__((aligned(16))) clut1_24[256];
static unsigned int __attribute__((aligned(16))) clut2_24[256];
static unsigned int clut_15[256];
static uint8 __attribute__((aligned(64))) frame_buffer[768*576*4];
vita2d_texture *screen, *screen_16;
unsigned int *screen_data;
unsigned short *screen_16_data;
static int32 frame_skip; // Prefs items
bool refresh_okay = false;
static uint32 BUF_WIDTH = 0;
static uint32 SCR_WIDTH = 0;
static uint32 SCR_HEIGHT = 0;
#define PIXEL_SIZE (4) // using 8888 mode for screen
static uint32 FRAME_SIZE = 0;
static uint32 DISP_BUF = 0;
static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into)
static uint32 the_buffer_size; // Size of allocated the_buffer
static int32 psp_screen_x = 640;
static int32 psp_screen_y = 480;
static int32 psp_screen_d = VDEPTH_8BIT;
static int32 d_x, d_y, d_w, d_h;
double scale_x, scale_y;
static int psp_lcd_aspect = 0; // 4:3
extern bool emerg_quit; // Flag: emergency quit requested
extern int FONT_SIZE;
extern vita2d_font *font;
extern char psp_home[256];
extern char *psp_floppy_inserted; // String: filename of floppy inserted
extern char *psp_cdrom_inserted; // String: filename of cdrom inserted
extern bool psp_cdrom_locked; // Flag: cdrom lock
static int DANZEFF_SCALE_COUNT = 2;
static int danzeff_cur_scale = 0;
static float danzeff_scales[] = { 1.0, 2.0 };
// File handles are pointers to these structures
struct file_handle {
char *name; // Copy of device/file name
int fd;
bool is_file; // Flag: plain file or /dev/something?
bool is_floppy; // Flag: floppy device
bool is_cdrom; // Flag: CD-ROM device
bool read_only; // Copy of Sys_open() flag
loff_t start_byte; // Size of file header (if any)
loff_t file_size; // Size of file data (only valid if is_file is true)
int toc_fd; // filedesc for cdrom TOC file
};
extern file_handle *floppy_fh;
extern file_handle *cdrom_fh;
static bool psp_int_enable = false;
static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
static int keycode_table[256]; // X keycode -> Mac keycode translation table
SceHidKeyboardReport k_reports[SCE_HID_MAX_REPORT];
SceHidMouseReport m_reports[SCE_HID_MAX_REPORT];
static int keyboard_hid_handle = 0;
static int mouse_hid_handle = 0;
/*
* refresh subroutines
*/
static void refresh4(void)
{
int i, j;
for (j=0; j<psp_screen_y; j++)
{
for (i=0; i<psp_screen_x/2; i++)
{
screen_data[i*2+768*j] = clut256[the_buffer[i+768/2*j]>>4];
screen_data[i*2+768*j+1] = clut256[the_buffer[i+768/2*j]&0xF];
}
}
vita2d_draw_texture_scale(screen, d_x, d_y, scale_x, scale_y);
}
static void refresh8(void)
{
int i, j;
for (j=0; j<psp_screen_y; j++)
{
for (i=0; i<psp_screen_x; i++)
{
screen_data[i+768*j] = clut256[the_buffer[i+768*j]];
}
}
vita2d_draw_texture_scale(screen, d_x, d_y, scale_x, scale_y);
}
static void refresh15(void)
{
int i, j;
for (j=0; j<psp_screen_y; j++)
{
for (i=0; i<psp_screen_x; i++)
{
screen_data[i+768*j] = clut0_15[the_buffer[i*2+768*j*2]] | clut1_15[the_buffer[i*2+1+768*j*2]];
}
}
vita2d_draw_texture_scale(screen, d_x, d_y, scale_x, scale_y);
}
static void refresh24(void)
{
int i, j;
for (j=0; j<psp_screen_y; j++)
{
for (i=0; i<psp_screen_x; i++)
{
screen_data[i+768*j] = clut0_24[the_buffer[i*4+1+768*j*4]] | clut1_24[the_buffer[i*4+2+768*j*4]] | clut2_24[the_buffer[i*4+3+768*j*4]];
}
}
vita2d_draw_texture_scale(screen, d_x, d_y, scale_x, scale_y);
}
/*
* monitor_desc subclass for PSP display
*/
class PSP_monitor_desc : public monitor_desc {
public:
PSP_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
~PSP_monitor_desc() {}
virtual void switch_to_current_mode(void);
virtual void set_palette(uint8 *pal, int num);
virtual void set_interrupts_enable(bool enable);
bool video_open(void);
void video_close(void);
};
/*
* Utility functions
*/
// Return bytes per row for requested vdepth
static int PSPBytesPerRow(int width, int vdepth)
{
switch (vdepth) {
case VDEPTH_1BIT: return 768>>3;
case VDEPTH_2BIT: return 768>>2;
case VDEPTH_4BIT: return 768>>1;
case VDEPTH_8BIT: return 768;
case VDEPTH_16BIT: return 768<<1;
case VDEPTH_32BIT: return 768<<2;
}
return 768;
}
// Add mode to list of supported modes
static void add_mode(int width, int height, int resolution_id, int bytes_per_row, video_depth vdepth)
{
// Fill in VideoMode entry
video_mode mode;
mode.x = width;
mode.y = height;
mode.resolution_id = resolution_id;
mode.bytes_per_row = bytes_per_row;
mode.depth = vdepth;
VideoModes.push_back(mode);
}
// Add standard list of windowed modes for given color depth
static void add_video_modes(video_depth vdepth)
{
add_mode(512, 384, 0x80, PSPBytesPerRow(512, vdepth), vdepth);
add_mode(640, 360, 0x81, PSPBytesPerRow(640, vdepth), vdepth);
add_mode(640, 400, 0x82, PSPBytesPerRow(640, vdepth), vdepth);
add_mode(640, 480, 0x83, PSPBytesPerRow(640, vdepth), vdepth);
add_mode(726, 544, 0x84, PSPBytesPerRow(726, vdepth), vdepth);
add_mode(768, 432, 0x85, PSPBytesPerRow(768, vdepth), vdepth);
add_mode(768, 576, 0x86, PSPBytesPerRow(768, vdepth), vdepth);
}
// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
static void set_mac_frame_buffer(PSP_monitor_desc &monitor, int depth, bool native_byte_order)
{
#if !REAL_ADDRESSING && !DIRECT_ADDRESSING
int layout = FLAYOUT_DIRECT;
if (depth == VDEPTH_16BIT)
layout = FLAYOUT_HOST_565;
else if (depth == VDEPTH_32BIT)
layout = FLAYOUT_DIRECT;
if (native_byte_order)
MacFrameLayout = layout;
else
MacFrameLayout = FLAYOUT_DIRECT;
monitor.set_mac_frame_base(MacFrameBaseMac);
// Set variables used by UAE memory banking
const video_mode &mode = monitor.get_current_mode();
MacFrameBaseHost = the_buffer;
MacFrameSize = mode.bytes_per_row * mode.y;
InitFrameBufferMapping();
#else
monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
#endif
D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
}
/*
* Display "driver" classes
*/
class driver_base {
public:
driver_base(PSP_monitor_desc &m);
virtual ~driver_base();
public:
PSP_monitor_desc &monitor; // Associated video monitor
const video_mode &mode; // Video mode handled by the driver
bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
};
class driver_fullscreen : public driver_base {
public:
driver_fullscreen(PSP_monitor_desc &monitor);
~driver_fullscreen();
};
static driver_base *drv = NULL; // Pointer to currently used driver object
driver_base::driver_base(PSP_monitor_desc &m)
: monitor(m), mode(m.get_current_mode()), init_ok(false)
{
//the_buffer = NULL;
}
driver_base::~driver_base()
{
// if (the_buffer != NULL) {
// D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
// free(the_buffer);
// the_buffer = NULL;
// }
}
/*
* Full-screen display driver
*/
// Open display
driver_fullscreen::driver_fullscreen(PSP_monitor_desc &m)
: driver_base(m)
{
int width = mode.x, height = mode.y;
// Set relative mouse mode
ADBSetRelMouseMode(true);
// Allocate memory for frame buffer
the_buffer_size = 768*576*4; //height * PSPBytesPerRow(width, mode.depth);
//the_buffer = (uint8 *)((uint32)frame_buffer | 0x40000000); // ((uint32)memalign(64, the_buffer_size) | 0x40000000);
the_buffer = (uint8 *)((uint32)memalign(64, the_buffer_size));
//sceKernelDcacheWritebackInvalidateAll();
D(bug("the_buffer = %p\n", the_buffer));
psp_screen_x = width;
psp_screen_y = height;
psp_screen_d = mode.depth;
if (psp_lcd_aspect == 2 ) {
//fit: fill the screen vertically
//and adjust width by same scaling ratio to give square pixels
d_w = psp_screen_x*d_h*1.0/psp_screen_y;
d_x = (960-d_w)/2.0;
}
scale_x = d_w*1.0/psp_screen_x;
scale_y = d_h*1.0/psp_screen_y;
// Init blitting routines
// Set frame buffer base
set_mac_frame_buffer(monitor, mode.depth, true);
// Everything went well
init_ok = true;
}
// Close display
driver_fullscreen::~driver_fullscreen()
{
}
/*
* Initialization
*/
// Init keycode translation table
static void keycode_init(void)
{
bool use_kc = PrefsFindBool("keycodes");
if (use_kc) {
// Get keycode file path from preferences
const char *kc_path = PrefsFindString("keycodefile");
// Open keycode table
FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
if (f == NULL) {
//char str[256];
//sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
//WarningAlert(str);
return;
}
// Default translation table
for (int i=0; i<256; i++)
keycode_table[i] = -1;
char line[256];
int n_keys = 0;
while (fgets(line, sizeof(line) - 1, f)) {
// Read line
int len = strlen(line);
if (len == 0)
continue;
line[len-1] = 0;
// Comments begin with "#" or ";"
if (line[0] == '#' || line[0] == ';' || line[0] == 0)
continue;
// Read keycode
int x_code, mac_code;
if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
keycode_table[x_code & 0xff] = mac_code, n_keys++;
else
break;
}
// Keycode file completely read
fclose(f);
use_keycodes = n_keys > 0;
D(bug("Using keycodes table, %d key mappings\n", n_keys));
}
}
void psp_video_setup(void)
{
screen = vita2d_create_empty_texture_format(768, 576, SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR);
// set filters to improve image quality in case the mac pixel to screen pixel mapping is not 1:1
vita2d_texture_set_filters(screen, SCE_GXM_TEXTURE_FILTER_LINEAR, SCE_GXM_TEXTURE_FILTER_LINEAR);
screen_data = (unsigned int*)vita2d_texture_get_datap(screen);
vita2d_start_drawing();
vita2d_clear_screen();
vita2d_end_drawing();
vita2d_swap_buffers();
vita2d_start_drawing();
vita2d_clear_screen();
vita2d_end_drawing();
vita2d_swap_buffers();
refresh_okay = true;
}
// Open display for current mode
bool PSP_monitor_desc::video_open(void)
{
D(bug("video_open()\n"));
#if DEBUG
const video_mode &mode = get_current_mode();
D(bug("Current video mode:\n"));
D(bug(" %dx%d (ID %02x), %d bpp\n", mode.x, mode.y, mode.resolution_id, 1 << (mode.depth & 0x0f)));
#endif
if (drv)
delete drv;
drv = new driver_fullscreen(*this);
if (drv == NULL)
return false;
if (!drv->init_ok) {
delete drv;
drv = NULL;
return false;
}
psp_video_setup();
return true;
}
bool VideoInit(bool classic)
{
if (classic)
return false; // not supported by PSP
sceHidKeyboardEnumerate(&keyboard_hid_handle, 1);
sceHidMouseEnumerate(&mouse_hid_handle, 1);
// Init keycode translation
keycode_init();
// Read prefs
frame_skip = PrefsFindInt32("frameskip");
psp_lcd_aspect = PrefsFindInt32("pspdar");
// set PSP screen variables
BUF_WIDTH = 768;
SCR_WIDTH = 960;
SCR_HEIGHT = 544;
DISP_BUF = 0;
switch (psp_lcd_aspect)
{
case 1:
//16:9
d_w = 960;
d_h = 544;
d_x = 0;
d_y = 0;
break;
case 2:
//fit: d_w will be adjusted later to get square pixels
//and d_x will be adjusted later to center the image
d_w = 960;
d_h = 544;
d_x = 0;
d_y = 0;
break;
default:
//4:3
d_w = 726;
d_h = 544;
d_x = (960-726)/2;
d_y = 0;
}
// Get screen mode from preferences
const char *mode_str = NULL;
mode_str = PrefsFindString("screen");
// Determine display default dimensions
int default_width, default_height, default_depth;
default_width = default_height = default_depth = 0;
if (mode_str)
if (sscanf(mode_str, "%d/%d/%d", &default_width, &default_height, &default_depth) != 3)
default_width = default_height = default_depth = 0;
int default_mode;
if ((default_width == 512) && (default_height == 384))
default_mode = 0x80;
else if ((default_width == 640) && (default_height == 360))
default_mode = 0x81;
else if ((default_width == 640) && (default_height == 400))
default_mode = 0x82;
else if ((default_width == 640) && (default_height == 480))
default_mode = 0x83;
else if ((default_width == 726) && (default_height == 544))
default_mode = 0x84;
else if ((default_width == 768) && (default_height == 432))
default_mode = 0x85;
else if ((default_width == 768) && (default_height == 576))
default_mode = 0x86;
else {
default_width = 726;
default_height = 544;
default_depth = 8;
default_mode = 0x84;
}
video_depth default_vdepth;
switch (default_depth) {
case 4:
default_vdepth = VDEPTH_4BIT;
break;
case 8:
default_vdepth = VDEPTH_8BIT;
break;
case 15: case 16:
default_vdepth = VDEPTH_16BIT;
break;
case 24: case 32:
default_vdepth = VDEPTH_32BIT;
break;
default:
default_vdepth = VDEPTH_8BIT;
default_depth = 8;
break;
}
// Construct list of supported modes
add_video_modes(VDEPTH_4BIT);
add_video_modes(VDEPTH_8BIT);
add_video_modes(VDEPTH_16BIT);
add_video_modes(VDEPTH_32BIT);
#if DEBUG
D(bug("Available video modes:\n"));
std::vector<video_mode>::const_iterator i, end = VideoModes.end();
for (i = VideoModes.begin(); i != end; ++i) {
const video_mode &mode = (*i);
int bits = 1 << (mode.depth & 0x0f);
if (bits == 16)
bits = 15;
else if (bits == 32)
bits = 24;
D(bug(" %dx%d (ID %02x), %d colors\n", mode.x, mode.y, mode.resolution_id, 1 << bits));
}
#endif
// Create PSP_monitor_desc for this (the only) display
PSP_monitor_desc *monitor = new PSP_monitor_desc(VideoModes, default_vdepth, default_mode);
VideoMonitors.push_back(monitor);
// Open display
return monitor->video_open();
}
/*
* Deinitialization
*/
// Close display
void PSP_monitor_desc::video_close(void)
{
D(bug("video_close()\n"));
refresh_okay = false;
//sceGuTerm();
// Close display
// if (drv)
// {
// delete drv;
// drv = NULL;
// }
}
void VideoExit(void)
{
// crashes PSP, so we'll just skip it for now ;)
#if 0
// Close displays
vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
for (i = VideoMonitors.begin(); i != end; ++i)
dynamic_cast<PSP_monitor_desc *>(*i)->video_close();
#endif
}
/*
* Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
*/
void VideoQuitFullScreen(void)
{
D(bug("VideoQuitFullScreen()\n"));
}
// removeable media menu support
struct fileentries {
char filename[FILENAME_MAX];
char path[FILENAME_MAX];
int flags;
};
#define MAXCDROMS 64
#define MAXFLOPPIES 64
#define MAXIMAPS 64
static struct fileentries cdroms[MAXCDROMS];
static struct fileentries floppies[MAXFLOPPIES];
static struct fileentries imaps[MAXIMAPS];
static char* aspectratios[] = { "4:3", "16:9", "Fit" };
static int numcdroms = -1;
static int numfloppies = -1;
static int numimaps = -1;
static int numaspectratios = 3;
static bool caps_lock = false;
extern int parse_dir (char *path, struct fileentries *thefiles, int maxentries);
#define NUM_MAPS 64
static uint32 button_map[NUM_MAPS][7] = {
{ 0x0010, 0x0000, 62, 255, 255, 255, 0 },
{ 0x0020, 0x0000, 60, 255, 255, 255, 0 },
{ 0x0040, 0x0000, 61, 255, 255, 255, 0 },
{ 0x0080, 0x0000, 59, 255, 255, 255, 0 },
{ 0x0100, 0x0000, 58, 255, 255, 255, 0 },
{ 0x0200, 0x0000, 54, 255, 255, 255, 0 },
{ 0x1000, 0x0000, 55, 12, 255, 255, 0 },
{ 0x2000, 0x0000, 55, 13, 255, 255, 0 },
{ 0x4000, 0x0000, 256, 255, 255, 255, 0 },
{ 0x8000, 0x0000, 36, 255, 255, 255, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
void parse_imap(FILE *fd)
{
char temp[128];
uint32 i, j, k, l, m, n, o;
memset((void *)button_map, 0, NUM_MAPS*7*4);
for (i=0; i<NUM_MAPS; )
{
temp[0] = 0;
fgets(temp, 127, fd);
if (temp[0] == 0)
break;
if (temp[0] == '#')
continue; // skip comment line
if (sscanf(temp, "%x %x %u %u %u %u", &j, &k, &l, &m, &n, &o) != 6)
break;
button_map[i][0] = j; // pressed
button_map[i][1] = k; // not pressed
button_map[i][2] = l; // key 1
button_map[i][3] = m; // key 2
button_map[i][4] = n; // key 3
button_map[i][5] = o; // key 4
button_map[i][6] = 0; // flag = not down
i++;
}
}
void key_down(int i)
{
// look for mouse move first - it can be sent every call
if (button_map[i][2] & 0x200)
{
ADBMouseMoved((int32)((button_map[i][2] & 0x0F0) >> 4) - 8, (int32)(button_map[i][2] & 0x00F) - 8);
return;
}
if (button_map[i][6])
return; // already sent key down
button_map[i][6] = 1;
if (button_map[i][2] == 57)
{
// caps lock
caps_lock = caps_lock ? false : true;
if (caps_lock)
ADBKeyDown(57);
else
ADBKeyUp(57);
return;
}
if (button_map[i][2] & 0x100)
ADBMouseDown(button_map[i][2] & 7);
else
ADBKeyDown(button_map[i][2]);
if (button_map[i][3] & 0x100)
ADBMouseDown(button_map[i][3] & 7);
else if (button_map[i][3] != 0xFF)
ADBKeyDown(button_map[i][3]);
if (button_map[i][4] & 0x100)
ADBMouseDown(button_map[i][4] & 7);
else if (button_map[i][4] != 0xFF)
ADBKeyDown(button_map[i][4]);
if (button_map[i][5] & 0x100)
ADBMouseDown(button_map[i][5] & 7);
else if (button_map[i][5] != 0xFF)
ADBKeyDown(button_map[i][5]);
}
void key_up(int i)
{
if (!button_map[i][6])
return; // already sent key up
button_map[i][6] = 0;
if (button_map[i][2] == 57)
return; // ignore caps lock key up
if (button_map[i][5] & 0x100)
ADBMouseUp(button_map[i][5] & 7);
else if (button_map[i][5] != 0xFF)
ADBKeyUp(button_map[i][5]);
if (button_map[i][4] & 0x100)
ADBMouseUp(button_map[i][4] & 7);
else if (button_map[i][4] != 0xFF)
ADBKeyUp(button_map[i][4]);
if (button_map[i][3] & 0x100)
ADBMouseUp(button_map[i][3] & 7);
else if (button_map[i][3] != 0xFF)
ADBKeyUp(button_map[i][3]);
if (button_map[i][2] & 0x100)
ADBMouseUp(button_map[i][2] & 7);
else
ADBKeyUp(button_map[i][2]);
}
void handle_keyboard(void)
{
static uint8 mac_key[0xE8] = {
255, 255, 255, 255, 0, 11, 8, 2,
14, 3, 5, 4, 34, 38, 40, 37,
46, 45, 31, 35, 12, 15, 1, 17,
32, 9, 13, 7, 16, 6, 18, 19,
20, 21, 23, 22, 26, 28, 25, 29,
36, 53, 51, 48, 49, 27, 24, 33,
30, 42, 255, 41, 39, 50, 43, 47,
44, 57, 122, 120, 99, 118, 96, 97,
98, 100, 101, 109, 103, 111, 105, 107,
113, 114, 115, 116, 117, 119, 121, 60,
59, 61, 62, 71, 75, 67, 78, 69,
76, 83, 84, 85, 86, 87, 88, 90,
91, 92, 82, 65, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255
};
static uint8 prev_keys[6] = {0};
static uint8 prev_modifiers = 0;
if (keyboard_hid_handle > 0)
{
int numReports = sceHidKeyboardRead(keyboard_hid_handle, (SceHidKeyboardReport**)&k_reports, SCE_HID_MAX_REPORT);
if (numReports < 0) {
keyboard_hid_handle = 0;
}
else if (numReports) {
if (k_reports[numReports-1].modifiers[1] & 0x2) //Caps Lock
{
if (!caps_lock) {
caps_lock = true;
ADBKeyDown(57);
}
}
else
{
if (caps_lock) {
caps_lock = false;
ADBKeyUp(57);
}
}
int l_ctrl_pressed = k_reports[numReports-1].modifiers[0] & 0x1 || prev_modifiers & 0x10;
int l_ctrl_prev_pressed = prev_modifiers & 0x1 || prev_modifiers & 0x10;
if (l_ctrl_pressed)
{
if (!l_ctrl_prev_pressed) {
ADBKeyDown(54);
}
}
else
{
if (l_ctrl_prev_pressed) {
ADBKeyUp(54);
}
}
int l_shift_pressed = k_reports[numReports-1].modifiers[0] & 0x2 || prev_modifiers & 0x20;
int l_shift_prev_pressed = prev_modifiers & 0x2 || prev_modifiers & 0x20;
if (l_shift_pressed)
{
if (!l_shift_prev_pressed) {
ADBKeyDown(56);
}
}
else
{
if (l_shift_prev_pressed) {
ADBKeyUp(56);
}
}
int l_alt_pressed = k_reports[numReports-1].modifiers[0] & 0x4 || prev_modifiers & 0x40;
int l_alt_prev_pressed = prev_modifiers & 0x4 || prev_modifiers & 0x4;
if (l_alt_pressed)
{
if (!l_alt_prev_pressed) {
ADBKeyDown(55);
}
}
else
{
if (l_alt_prev_pressed) {
ADBKeyUp(55);
}
}
int l_logo_pressed = k_reports[numReports-1].modifiers[0] & 0x8 || prev_modifiers & 0x80;
int l_logo_prev_pressed = prev_modifiers & 0x8 || prev_modifiers & 0x80;
if (l_logo_pressed)
{
if (!l_logo_prev_pressed) {
ADBKeyDown(58);
}
}
else
{
if (l_logo_prev_pressed) {
ADBKeyUp(58);
}
}
prev_modifiers = k_reports[numReports-1].modifiers[0];
int i, j;
for (i = 0; i < 6; i++) {
int keyCode = k_reports[numReports-1].keycodes[i];
if (keyCode != prev_keys[i]) {
ADBKeyUp(mac_key[prev_keys[i]]);
if (keyCode) {
ADBKeyDown(mac_key[keyCode]);
}
prev_keys[i] = keyCode;
}
}
}
}
}
void handle_menu(SceCtrlData pad)
{
uint32 buttons;
static uint32 oldButtons = 0;
static uint32 sel = 0;
static uint32 max = 0;
static uint32 idx = 0; // 0 = input maps, 1 = floppies, 2 = cdroms, 3 = aspect ratio
char temp[256];
uint32 fc = 0xFF8888FF;
if (numcdroms == -1)
{
strcpy(temp, psp_home);
strcat(temp, "cdroms");
numcdroms = parse_dir(temp, cdroms, MAXCDROMS);
}
if (numfloppies == -1)
{
strcpy(temp, psp_home);
strcat(temp, "disks");
numfloppies = parse_dir(temp, floppies, MAXFLOPPIES);
}
if (numimaps == -1)
{
strcpy(temp, psp_home);
strcat(temp, "imaps");
numimaps = parse_dir(temp, imaps, MAXIMAPS);
}
// take care of initially clear max
while (max == 0)
{
max = (idx == 0) ? numimaps : (idx == 1) ? numfloppies : (idx == 2) ? numcdroms : numaspectratios;
if (max)
break;
idx = idx<3 ? idx+1 : 0;
}
buttons = pad.buttons ^ oldButtons; // set if button changed
oldButtons = pad.buttons;
if (buttons & (SCE_CTRL_UP | SCE_CTRL_DOWN))
if (pad.buttons & SCE_CTRL_UP)
{
// dec index
idx = idx>0 ? idx-1 : 3;
sel = 0;
max = 0;
while (max == 0)
{
max = (idx == 0) ? numimaps : (idx == 1) ? numfloppies : (idx == 2) ? numcdroms : numaspectratios;
if (max)
break;
idx = idx>0 ? idx-1 : 3;
}
}
else if (pad.buttons & SCE_CTRL_DOWN)
{
// inc index
idx = idx<3 ? idx+1 : 0;