forked from bucanero/pkgi-ps3
/
pkgi_ps3.c
1596 lines (1305 loc) · 35.2 KB
/
pkgi_ps3.c
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
#include "pkgi.h"
#include "pkgi_style.h"
#include <sys/stat.h>
#include <sys/thread.h>
#include <sys/mutex.h>
#include <sys/memory.h>
#include <sys/process.h>
#include <sysutil/osk.h>
#include <io/pad.h>
#include <lv2/sysfs.h>
#include <lv2/process.h>
#include <net/net.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ya2d/ya2d.h>
#include <curl/curl.h>
#include "ttf_render.h"
#include <mikmod.h>
#include "mikmod_loader.h"
#define OSKDIALOG_FINISHED 0x503
#define OSKDIALOG_UNLOADED 0x504
#define OSKDIALOG_INPUT_ENTERED 0x505
#define OSKDIALOG_INPUT_CANCELED 0x506
#define PKGI_OSK_INPUT_LENGTH 128
#define SCE_IME_DIALOG_MAX_TITLE_LENGTH (128)
#define SCE_IME_DIALOG_MAX_TEXT_LENGTH (512)
#define ANALOG_CENTER 0x78
#define ANALOG_THRESHOLD 0x68
#define ANALOG_MIN (ANALOG_CENTER - ANALOG_THRESHOLD)
#define ANALOG_MAX (ANALOG_CENTER + ANALOG_THRESHOLD)
#define PKGI_USER_AGENT "Mozilla/5.0 (PLAYSTATION 3; 1.00)"
struct pkgi_http
{
int used;
uint64_t size;
uint64_t offset;
CURL *curl;
};
typedef struct
{
pkgi_texture circle;
pkgi_texture cross;
pkgi_texture triangle;
pkgi_texture square;
} t_tex_buttons;
typedef struct
{
char *memory;
size_t size;
} curl_memory_t;
static sys_mutex_t g_dialog_lock;
static uint32_t cpu_temp_c[2];
static int g_ok_button;
static int g_cancel_button;
static uint32_t g_button_frame_count;
static u64 g_time;
static int g_ime_active;
static int osk_action = 0;
static int osk_level = 0;
static sys_mem_container_t container_mem;
static oskCallbackReturnParam OutputReturnedParam;
volatile int osk_event = 0;
volatile int osk_unloaded = 0;
static uint16_t g_ime_title[SCE_IME_DIALOG_MAX_TITLE_LENGTH];
static uint16_t g_ime_text[SCE_IME_DIALOG_MAX_TEXT_LENGTH];
static uint16_t g_ime_input[SCE_IME_DIALOG_MAX_TEXT_LENGTH + 1];
static pkgi_http g_http[4];
static t_tex_buttons tex_buttons;
static MREADER *mem_reader;
static MODULE *module;
int pkgi_snprintf(char* buffer, uint32_t size, const char* msg, ...)
{
va_list args;
va_start(args, msg);
// TODO: why sceClibVsnprintf doesn't work here?
int len = vsnprintf(buffer, size - 1, msg, args);
va_end(args);
buffer[len] = 0;
return len;
}
void pkgi_vsnprintf(char* buffer, uint32_t size, const char* msg, va_list args)
{
// TODO: why sceClibVsnprintf doesn't work here?
int len = vsnprintf(buffer, size - 1, msg, args);
buffer[len] = 0;
}
char* pkgi_strstr(const char* str, const char* sub)
{
return strstr(str, sub);
}
int pkgi_stricontains(const char* str, const char* sub)
{
return strcasestr(str, sub) != NULL;
}
int pkgi_stricmp(const char* a, const char* b)
{
return strcasecmp(a, b);
}
void pkgi_strncpy(char* dst, uint32_t size, const char* src)
{
strncpy(dst, src, size);
}
char* pkgi_strrchr(const char* str, char ch)
{
return strrchr(str, ch);
}
uint32_t pkgi_strlen(const char *str)
{
return strlen(str);
}
int64_t pkgi_strtoll(const char* str)
{
int64_t res = 0;
const char* s = str;
if (*s && *s == '-')
{
s++;
}
while (*s)
{
res = res * 10 + (*s - '0');
s++;
}
return str[0] == '-' ? -res : res;
}
void *pkgi_malloc(uint32_t size)
{
return malloc(size);
}
void pkgi_free(void *ptr)
{
free(ptr);
}
void pkgi_memcpy(void* dst, const void* src, uint32_t size)
{
memcpy(dst, src, size);
}
void pkgi_memmove(void* dst, const void* src, uint32_t size)
{
memmove(dst, src, size);
}
int pkgi_memequ(const void* a, const void* b, uint32_t size)
{
return memcmp(a, b, size) == 0;
}
static void pkgi_start_debug_log(void)
{
#ifdef PKGI_ENABLE_LOGGING
dbglogger_init();
LOG("PKGi PS3 logging initialized");
dbglogger_failsafe("9999");
#endif
}
static void pkgi_stop_debug_log(void)
{
#ifdef PKGI_ENABLE_LOGGING
dbglogger_stop();
#endif
}
int pkgi_ok_button(void)
{
return g_ok_button;
}
int pkgi_cancel_button(void)
{
return g_cancel_button;
}
static void music_update_thread(void)
{
while (module)
{
MikMod_Update();
usleep(1000);
}
pkgi_thread_exit();
}
void init_music(void)
{
MikMod_InitThreads();
/* register the driver and S3M module loader */
MikMod_RegisterDriver(&drv_psl1ght);
MikMod_RegisterLoader(&load_s3m);
/* init the library */
md_mode |= DMODE_SOFT_MUSIC | DMODE_STEREO | DMODE_HQMIXER | DMODE_16BITS;
if (MikMod_Init("")) {
LOG("Could not initialize sound: %s", MikMod_strerror(MikMod_errno));
return;
}
LOG("Init %s", MikMod_InfoDriver());
LOG("Loader %s", MikMod_InfoLoader());
mem_reader = new_mikmod_mem_reader(haiku_s3m_bin, haiku_s3m_bin_size);
module = Player_LoadGeneric(mem_reader, 64, 0);
module->wrap = TRUE;
pkgi_start_thread("music_thread", &music_update_thread);
}
void pkgi_start_music(void)
{
if (module) {
/* start module */
LOG("Playing %s", module->songname);
Player_Start(module);
} else
LOG("Could not load module: %s", MikMod_strerror(MikMod_errno));
}
void pkgi_stop_music(void)
{
LOG("Stop music");
Player_Stop();
}
void end_music(void)
{
Player_Free(module);
delete_mikmod_mem_reader(mem_reader);
MikMod_Exit();
}
static int sys_game_get_temperature(int sel, u32 *temperature)
{
u32 temp;
lv2syscall2(383, (u64) sel, (u64) &temp);
*temperature = (temp >> 24);
return_to_user_prog(int);
}
int pkgi_dialog_lock(void)
{
int res = sysMutexLock(g_dialog_lock, 0);
if (res != 0)
{
LOG("dialog lock failed error=0x%08x", res);
}
return (res == 0);
}
int pkgi_dialog_unlock(void)
{
int res = sysMutexUnlock(g_dialog_lock);
if (res != 0)
{
LOG("dialog unlock failed error=0x%08x", res);
}
return (res == 0);
}
static int convert_to_utf16(const char* utf8, uint16_t* utf16, uint32_t available)
{
int count = 0;
while (*utf8)
{
uint8_t ch = (uint8_t)*utf8++;
uint32_t code;
uint32_t extra;
if (ch < 0x80)
{
code = ch;
extra = 0;
}
else if ((ch & 0xe0) == 0xc0)
{
code = ch & 31;
extra = 1;
}
else if ((ch & 0xf0) == 0xe0)
{
code = ch & 15;
extra = 2;
}
else
{
// TODO: this assumes there won't be invalid utf8 codepoints
code = ch & 7;
extra = 3;
}
for (uint32_t i=0; i<extra; i++)
{
uint8_t next = (uint8_t)*utf8++;
if (next == 0 || (next & 0xc0) != 0x80)
{
return count;
}
code = (code << 6) | (next & 0x3f);
}
if (code < 0xd800 || code >= 0xe000)
{
if (available < 1) return count;
utf16[count++] = (uint16_t)code;
available--;
}
else // surrogate pair
{
if (available < 2) return count;
code -= 0x10000;
utf16[count++] = 0xd800 | (code >> 10);
utf16[count++] = 0xdc00 | (code & 0x3ff);
available -= 2;
}
}
utf16[count]=0;
return count;
}
static int convert_from_utf16(const uint16_t* utf16, char* utf8, uint32_t size)
{
int count = 0;
while (*utf16)
{
uint32_t code;
uint16_t ch = *utf16++;
if (ch < 0xd800 || ch >= 0xe000)
{
code = ch;
}
else // surrogate pair
{
uint16_t ch2 = *utf16++;
if (ch < 0xdc00 || ch > 0xe000 || ch2 < 0xd800 || ch2 > 0xdc00)
{
return count;
}
code = 0x10000 + ((ch & 0x03FF) << 10) + (ch2 & 0x03FF);
}
if (code < 0x80)
{
if (size < 1) return count;
utf8[count++] = (char)code;
size--;
}
else if (code < 0x800)
{
if (size < 2) return count;
utf8[count++] = (char)(0xc0 | (code >> 6));
utf8[count++] = (char)(0x80 | (code & 0x3f));
size -= 2;
}
else if (code < 0x10000)
{
if (size < 3) return count;
utf8[count++] = (char)(0xe0 | (code >> 12));
utf8[count++] = (char)(0x80 | ((code >> 6) & 0x3f));
utf8[count++] = (char)(0x80 | (code & 0x3f));
size -= 3;
}
else
{
if (size < 4) return count;
utf8[count++] = (char)(0xf0 | (code >> 18));
utf8[count++] = (char)(0x80 | ((code >> 12) & 0x3f));
utf8[count++] = (char)(0x80 | ((code >> 6) & 0x3f));
utf8[count++] = (char)(0x80 | (code & 0x3f));
size -= 4;
}
}
utf8[count]=0;
return count;
}
static void osk_exit(void)
{
if(osk_level == 2) {
oskAbort();
oskUnloadAsync(&OutputReturnedParam);
osk_event = 0;
osk_action=-1;
}
if(osk_level >= 1) {
sysUtilUnregisterCallback(SYSUTIL_EVENT_SLOT0);
sysMemContainerDestroy(container_mem);
}
}
static void osk_event_handle(u64 status, u64 param, void * userdata)
{
switch((u32) status)
{
case OSKDIALOG_INPUT_CANCELED:
osk_event = OSKDIALOG_INPUT_CANCELED;
break;
case OSKDIALOG_UNLOADED:
osk_unloaded = 1;
break;
case OSKDIALOG_INPUT_ENTERED:
osk_event = OSKDIALOG_INPUT_ENTERED;
break;
case OSKDIALOG_FINISHED:
osk_event = OSKDIALOG_FINISHED;
break;
default:
break;
}
}
void pkgi_dialog_input_text(const char* title, const char* text)
{
oskParam DialogOskParam;
oskInputFieldInfo inputFieldInfo;
int ret = 0;
osk_level = 0;
if(sysMemContainerCreate(&container_mem, 8*1024*1024) < 0) {
ret = -1;
goto error_end;
}
osk_level = 1;
convert_to_utf16(title, g_ime_title, PKGI_COUNTOF(g_ime_title) - 1);
convert_to_utf16(text, g_ime_text, PKGI_COUNTOF(g_ime_text) - 1);
inputFieldInfo.message = g_ime_title;
inputFieldInfo.startText = g_ime_text;
inputFieldInfo.maxLength = PKGI_OSK_INPUT_LENGTH;
OutputReturnedParam.res = OSK_NO_TEXT;
OutputReturnedParam.len = PKGI_OSK_INPUT_LENGTH;
OutputReturnedParam.str = g_ime_input;
memset(g_ime_input, 0, sizeof(g_ime_input));
if(oskSetKeyLayoutOption (OSK_10KEY_PANEL | OSK_FULLKEY_PANEL) < 0) {
ret = -2;
goto error_end;
}
DialogOskParam.firstViewPanel = OSK_PANEL_TYPE_ALPHABET_FULL_WIDTH;
DialogOskParam.allowedPanels = (OSK_PANEL_TYPE_ALPHABET | OSK_PANEL_TYPE_NUMERAL | OSK_PANEL_TYPE_ENGLISH);
if(oskAddSupportLanguage (DialogOskParam.allowedPanels) < 0) {
ret = -3;
goto error_end;
}
if(oskSetLayoutMode( OSK_LAYOUTMODE_HORIZONTAL_ALIGN_CENTER | OSK_LAYOUTMODE_VERTICAL_ALIGN_CENTER ) < 0) {
ret = -4;
goto error_end;
}
oskPoint pos = {0.0, 0.0};
DialogOskParam.controlPoint = pos;
DialogOskParam.prohibitFlags = OSK_PROHIBIT_RETURN;
if(oskSetInitialInputDevice(OSK_DEVICE_PAD) < 0) {
ret = -5;
goto error_end;
}
sysUtilUnregisterCallback(SYSUTIL_EVENT_SLOT0);
sysUtilRegisterCallback(SYSUTIL_EVENT_SLOT0, osk_event_handle, NULL);
osk_action = 0;
osk_unloaded = 0;
if(oskLoadAsync(container_mem, (const void *) &DialogOskParam, (const void *) &inputFieldInfo) < 0) {
ret= -6;
goto error_end;
}
osk_level = 2;
if (ret == 0)
{
g_ime_active = 1;
return;
}
error_end:
LOG("Keyboard Init failed, error 0x%08x", ret);
osk_exit();
osk_level = 0;
}
int pkgi_dialog_input_update(void)
{
if (!g_ime_active)
{
return 0;
}
if (!osk_unloaded)
{
switch(osk_event)
{
case OSKDIALOG_INPUT_ENTERED:
oskGetInputText(&OutputReturnedParam);
osk_event = 0;
break;
case OSKDIALOG_INPUT_CANCELED:
oskAbort();
oskUnloadAsync(&OutputReturnedParam);
osk_event = 0;
osk_action = -1;
break;
case OSKDIALOG_FINISHED:
if (osk_action != -1) osk_action = 1;
oskUnloadAsync(&OutputReturnedParam);
osk_event = 0;
break;
default:
break;
}
}
else
{
g_ime_active = 0;
if ((OutputReturnedParam.res == OSK_OK) && (osk_action == 1))
{
osk_exit();
return 1;
}
osk_exit();
}
return 0;
}
void pkgi_dialog_input_get_text(char* text, uint32_t size)
{
convert_from_utf16(g_ime_input, text, size - 1);
LOG("input: %s", text);
}
void load_ttf_fonts()
{
LOG("loading TTF fonts");
TTFUnloadFont();
TTFLoadFont(0, "/dev_flash/data/font/SCE-PS3-RD-I-LATIN.TTF", NULL, 0);
TTFLoadFont(1, "/dev_flash/data/font/SCE-PS3-DH-R-CGB.TTF", NULL, 0);
TTFLoadFont(2, "/dev_flash/data/font/SCE-PS3-SR-R-JPN.TTF", NULL, 0);
TTFLoadFont(3, "/dev_flash/data/font/SCE-PS3-YG-R-KOR.TTF", NULL, 0);
ya2d_texturePointer = (u32*) init_ttf_table((u16*) ya2d_texturePointer);
}
static void sys_callback(uint64_t status, uint64_t param, void* userdata)
{
switch (status) {
case SYSUTIL_EXIT_GAME:
pkgi_end();
sysProcessExit(1);
break;
case SYSUTIL_MENU_OPEN:
case SYSUTIL_MENU_CLOSE:
break;
default:
break;
}
}
void pkgi_start(void)
{
pkgi_start_debug_log();
netInitialize();
LOG("initializing Network");
sysModuleLoad(SYSMODULE_NET);
sys_mutex_attr_t mutex_attr;
mutex_attr.attr_protocol = SYS_MUTEX_PROTOCOL_FIFO;
mutex_attr.attr_recursive = SYS_MUTEX_ATTR_NOT_RECURSIVE;
mutex_attr.attr_pshared = SYS_MUTEX_ATTR_NOT_PSHARED;
mutex_attr.attr_adaptive = SYS_MUTEX_ATTR_ADAPTIVE;
strcpy(mutex_attr.name, "dialog");
int ret = sysMutexCreate(&g_dialog_lock, &mutex_attr);
if (ret != 0) {
LOG("mutex create error (%x)", ret);
}
sysUtilGetSystemParamInt(SYSUTIL_SYSTEMPARAM_ID_ENTER_BUTTON_ASSIGN, &ret);
if (ret == 0)
{
g_ok_button = PKGI_BUTTON_O;
g_cancel_button = PKGI_BUTTON_X;
}
else
{
g_ok_button = PKGI_BUTTON_X;
g_cancel_button = PKGI_BUTTON_O;
}
ya2d_init();
ya2d_paddata[0].ANA_L_H = ANALOG_CENTER;
ya2d_paddata[0].ANA_L_V = ANALOG_CENTER;
tex_buttons.circle = pkgi_load_image_buffer(CIRCLE, png);
tex_buttons.cross = pkgi_load_image_buffer(CROSS, png);
tex_buttons.triangle = pkgi_load_image_buffer(TRIANGLE, png);
tex_buttons.square = pkgi_load_image_buffer(SQUARE, png);
SetFontSize(PKGI_FONT_WIDTH, PKGI_FONT_HEIGHT);
SetFontZ(PKGI_FONT_Z);
load_ttf_fonts();
pkgi_mkdirs(PKGI_TMP_FOLDER);
pkgi_mkdirs(PKGI_RAP_FOLDER);
init_music();
// register exit callback
sysUtilRegisterCallback(SYSUTIL_EVENT_SLOT0, sys_callback, NULL);
g_time = pkgi_time_msec();
}
int pkgi_update(pkgi_input* input)
{
ya2d_controlsRead();
uint32_t previous = input->down;
memcpy(&input->down, &ya2d_paddata[0].button[2], sizeof(uint32_t));
if (ya2d_paddata[0].ANA_L_V < ANALOG_MIN)
input->down |= PKGI_BUTTON_UP;
if (ya2d_paddata[0].ANA_L_V > ANALOG_MAX)
input->down |= PKGI_BUTTON_DOWN;
if (ya2d_paddata[0].ANA_L_H < ANALOG_MIN)
input->down |= PKGI_BUTTON_LEFT;
if (ya2d_paddata[0].ANA_L_H > ANALOG_MAX)
input->down |= PKGI_BUTTON_RIGHT;
input->pressed = input->down & ~previous;
input->active = input->pressed;
if (input->down == previous)
{
if (g_button_frame_count >= 10)
{
input->active = input->down;
}
g_button_frame_count++;
}
else
{
g_button_frame_count = 0;
}
#ifdef PKGI_ENABLE_LOGGING
if ((input->active & PKGI_BUTTON_RIGHT) && (input->active & PKGI_BUTTON_LEFT)) {
LOG("screenshot");
dbglogger_screenshot_tmp(0);
}
#endif
ya2d_screenClear();
ya2d_screenBeginDrawing();
reset_ttf_frame();
uint64_t time = pkgi_time_msec();
input->delta = time - g_time;
g_time = time;
return 1;
}
void pkgi_swap(void)
{
ya2d_screenFlip();
}
void pkgi_end(void)
{
if (module) end_music();
pkgi_stop_debug_log();
pkgi_free_texture(tex_buttons.circle);
pkgi_free_texture(tex_buttons.cross);
pkgi_free_texture(tex_buttons.triangle);
pkgi_free_texture(tex_buttons.square);
ya2d_deinit();
sysMutexDestroy(g_dialog_lock);
#ifdef PKGI_ENABLE_LOGGING
sysProcessExitSpawn2("/dev_hdd0/game/PSL145310/RELOAD.SELF", NULL, NULL, NULL, 0, 1001, SYS_PROCESS_SPAWN_STACK_SIZE_1M);
#endif
sysProcessExit(0);
}
int pkgi_get_temperature(uint8_t cpu)
{
static uint32_t t = 0;
if (t++ % 0x100 == 0)
{
sys_game_get_temperature(0, &cpu_temp_c[0]);
sys_game_get_temperature(1, &cpu_temp_c[1]);
}
return cpu_temp_c[cpu];
}
int pkgi_temperature_is_high(void)
{
return ((cpu_temp_c[0] >= 70 || cpu_temp_c[1] >= 70));
}
uint64_t pkgi_get_free_space(void)
{
u32 blockSize;
static uint32_t t = 0;
static uint64_t freeSize = 0;
if (t++ % 0x200 == 0)
{
sysFsGetFreeSize("/dev_hdd0/", &blockSize, &freeSize);
freeSize *= blockSize;
}
return (freeSize);
}
const char* pkgi_get_config_folder(void)
{
return PKGI_APP_FOLDER;
}
const char* pkgi_get_temp_folder(void)
{
return PKGI_TMP_FOLDER;
}
const char* pkgi_get_app_folder(void)
{
return PKGI_APP_FOLDER;
}
int pkgi_is_incomplete(const char* titleid)
{
char path[256];
pkgi_snprintf(path, sizeof(path), "%s/%s.resume", pkgi_get_temp_folder(), titleid);
struct stat st;
int res = stat(path, &st);
return (res == 0);
}
/*const char* dir_list(const char* path)
{
//bool ret = true;
//int i;
sysFSStat stat;
const char* subPath;
if( sysFsStat( path, &stat ) )
return "";
if( stat.st_mode & S_IFDIR )
{
s32 fd;
sysFSDirent entry;
u64 read;
while( !sysFsReaddir( fd, &entry , &read ) && read > 0 )
{
subPath = entry.d_name;
}
}
return(subPath);
}*/
int pkgi_dir_exists(const char* path)
{
// LOG("checking if folder %s exists", path);
struct stat sb;
if ((stat(path, &sb) == 0) && S_ISDIR(sb.st_mode)) {
return 1;
}
return 0;
}
bool pkgi_file_exists(const char* path)
{
sysFSStat entry;
return( sysFsStat( path, &entry ) == 0 );
}
int chk_act_dat()
{
char path[64];
int a=0;
for (int i = 1; i <= 0x200; i++)
{
sprintf(path, "/dev_hdd0/home/%08i/exdata", i);
if(pkgi_dir_exists(path))
{
snprintf(path, sizeof(path), "/dev_hdd0/home/%08d/exdata/act.dat", i);
if(pkgi_file_exists(path))
{
a=i;
break;
}
}
}
return a;
}
int pkgi_is_installed(const char* content)
{
char path[128];
//snprintf(path, sizeof(path), "/dev_hdd0/game/%.9s", content + 7); //времено убрано сравнение по title id
return (pkgi_dir_exists(path));
}
uint32_t pkgi_time_msec()
{
return ya2d_millis();
}
void pkgi_thread_exit()
{
sysThreadExit(0);
}
void pkgi_start_thread(const char* name, pkgi_thread_entry* start)
{
s32 ret;
sys_ppu_thread_t id;
ret = sysThreadCreate(&id, (void (*)(void *))start, NULL, 1500, 1024*1024, THREAD_JOINABLE, (char*)name);
LOG("sysThreadCreate: %s (0x%08x)",name, id);
if (ret != 0)
{
LOG("failed to start %s thread", name);
}
}
void pkgi_sleep(uint32_t msec)
{
usleep(msec * 1000);
}
int pkgi_load(const char* name, void* data, uint32_t max)
{
FILE* fd = fopen(name, "rb");
if (!fd)
{
return -1;
}
char* data8 = data;
int total = 0;
while (max != 0)
{
int read = fread(data8 + total, 1, max, fd);
if (read < 0)
{
total = -1;
break;
}
else if (read == 0)
{
break;
}
total += read;
max -= read;
}
fclose(fd);
return total;
}
int pkgi_save(const char* name, const void* data, uint32_t size)
{
FILE* fd = fopen(name, "wb");
if (!fd)
{
return 0;
}
int ret = 1;
const char* data8 = data;
while (size != 0)
{
int written = fwrite(data8, 1, size, fd);
if (written <= 0)
{
ret = 0;
break;
}
data8 += written;
size -= written;
}
fclose(fd);
return ret;
}
void pkgi_lock_process(void)
{
/*
if (__atomic_fetch_add(&g_power_lock, 1, __ATOMIC_SEQ_CST) == 0)
{
LOG("locking shell functionality");
if (sceShellUtilLock(SCE_SHELL_UTIL_LOCK_TYPE_PS_BTN) < 0)
{
LOG("sceShellUtilLock failed");
}
}
*/
}
void pkgi_unlock_process(void)
{
/*