forked from masterzorag/ps3_vsh_menu
/
main.c
571 lines (464 loc) · 15.4 KB
/
main.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
#include <sys/prx.h>
#include <sys/ppu_thread.h>
#include <sys/event.h>
#include <sys/process.h>
#include <sys/event.h>
#include <sys/syscall.h>
#include <sys/memory.h>
#include <sys/types.h>
#include <sys/sys_time.h>
#include <sys/timer.h>
#include <cell/pad.h>
#include <cell/cell_fs.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include "inc/vsh_exports.h"
#include "misc.h"
#include "mem.h"
#include "blitting.h"
#ifdef DEBUG
#include "network.h"
#endif
SYS_MODULE_INFO (VSH_MENU, 0, 1, 0);
SYS_MODULE_START(vsh_menu_start);
SYS_MODULE_STOP (vsh_menu_stop);
#define THREAD_NAME "vsh_menu_thread"
#define STOP_THREAD_NAME "vsh_menu_stop_thread"
static sys_ppu_thread_t vsh_menu_tid = -1;
static int8_t done = 0;
int32_t vsh_menu_start(uint64_t arg);
int32_t vsh_menu_stop(void);
// a temporary color, to preview text in realtime
uint8_t a, r, g, b;
/***********************************************************************
* sys_ppu_thread_exit, direct over syscall
***********************************************************************/
static inline void _sys_ppu_thread_exit(uint64_t val)
{
system_call_1(41, val);
}
/***********************************************************************
*
***********************************************************************/
static inline sys_prx_id_t prx_get_module_id_by_address(void *addr)
{
system_call_1(461, (uint64_t)(uint32_t)addr);
return (int32_t)p1;
}
////////////////////////////////////////////////////////////////////////
// BLITTING
static int8_t menu_running = 0; // vsh menu off(0) or on(1)
static int8_t line = 0; // current line into menu, init 0 (entry 1:)
static int8_t view = 0; // menu view, init 0 (main view)
// max menu entries per view
static int8_t max_menu[] = {9, 7, 5};
static uint32_t bg_color_menu[] =
{
0x7F0000FF, // blue, semitransparent
0x7FFF0000, // red, semitransparent
0x7F00FF00 // green, semitransparent
};
// menu entry strings
const char *entry_str[3][9] = {
{
"1: Make a single beep",
"2: Make a double beep",
"3: Enter second menu view",
"4: Enter third menu view",
"5: Play trophy sound",
"6: Make screenshot",
"7: Make screenshot with Menu",
"8: Reset PS3",
"9: Shutdown PS3"
},
{ // red menu
"1: Back to main view",
"2: screenshot",
"3: Alpha",
"4: Red",
"5: Green",
"6: Blue",
"7: test"
},
{
"1: Back to main view",
"2: test string...",
"3: test string...",
"4: test string...",
"5: test string..."
}
};
/***********************************************************************
* draw a frame
***********************************************************************/
static void draw_frame(CellPadData *data)
{
int8_t i;
// all 32bit colors are ARGB, the framebuffer format
set_foreground_color(0xFFFFFFFF); // white, opac
// set the right background color for current view
set_background_color(bg_color_menu[view]);
draw_background();
#ifdef HAVE_STARFIELD
// first draw stars, keeping them under text lines
move_star();
#endif
// print headline string, coordinates in canvas
print_text(4, 4, "PS3 VSH Menu");
// print all menu entries for view, and the current selected entry in green
for(i = 0; i < max_menu[view]; i++)
{
i == line ? set_foreground_color(0xFF00FF00) : set_foreground_color(0xFFFFFFFF);
print_text(4, 8 + ((FONT_H +1) * (i + 1)), entry_str[view][i]);
}
// (re)set back after draw last line
set_foreground_color(0xFFFFFFFF);
// ...
// second menu, red one:
if(view == 1)
{
/*
hexdump pad data: store in text 8 buttons in a row
in "%.4x" format plus 1 for ':', last one will be
replaced by terminator, no need to add 1
*/
char templn[8 * (4 + 1)];
uint16_t tmp_x;
// test text alignment
sprintf(templn, "*%p, %d bytes;", data, data->len * sizeof(uint16_t));
tmp_x = get_aligned_x(templn, CENTER);
print_text(tmp_x, 180, templn);
// hexdump first 32 buttons
uint16_t x = 0, y = 200;
for(i = 0; i < 32; i++)
{
sprintf(&templn[x], "%.4x:", data->button[i]);
x += 5;
templn[x] = '\0';
if(x %8 == 0)
{
// overwrite last ':' with terminator
templn[x -1] = '\0';
tmp_x = get_aligned_x(templn, CENTER);
print_text(tmp_x, y, templn);
x = 0, y += (FONT_H +1); // additional px to next line
}
}
// update temp color
uint32_t tmp_c = ARGB(a, r, g, b);
// start draw text with updated color
set_foreground_color(tmp_c);
// print its value
sprintf(templn, "%.8x", tmp_c);
tmp_x = get_aligned_x(templn, RIGHT) -4; // additional px from R margin
print_text(tmp_x, 4, templn);
// testing sine
float amp = f_sinf(10);
sprintf(templn, "%.4f", amp);
tmp_x = get_aligned_x(templn, RIGHT) -4;
print_text(tmp_x, 4 + (FONT_H +1), templn);
read_meminfo(templn);
tmp_x = get_aligned_x(templn, RIGHT) -4;
print_text(tmp_x, CANVAS_H - (FONT_H +1) -4, templn); // additional px from lower border
}
// ...
}
/***********************************************************************
* stop VSH Menu
***********************************************************************/
static void stop_VSH_Menu(void)
{
// menu off
menu_running = 0;
// free heap memory
destroy_heap();
// continue rsx rendering
rsx_fifo_pause(0);
}
/***********************************************************************
* execute a menu action, based on line(current selected menu entry)
***********************************************************************/
static void do_leftright_action(uint16_t curpad)
{
if(view == 1) // only on second screen
{
switch(line)
{
case 2: // 3: Alpha
(curpad & PAD_LEFT) ? a-- : a++;
break;
case 3: // 4: Red
(curpad & PAD_LEFT) ? r-- : r++;
break;
case 4: // 5: Green
(curpad & PAD_LEFT) ? g-- : g++;
break;
case 5: // 6: Blue
(curpad & PAD_LEFT) ? b-- : b++;
break;
default: // do nothing
return;
}
// update second screen bg_color
bg_color_menu[view] = ARGB(a, r, g, b);
play_rco_sound("system_plugin", "snd_cursor");
}
}
/***********************************************************************
* execute a menu action, based on line(current selected menu entry)
***********************************************************************/
static void do_menu_action(void)
{
switch(view)
{
case 0: // main menu view
switch(line)
{
case 0: // "1: Make a single beep"
buzzer(1);
break;
case 1: // "2: Make a double beep"
buzzer(2);
break;
case 2: // "3: Enter second menu view"
view = 1; // change menu view
line = 0; // on start entry
break;
case 3: // "4: Enter third menu view"
view = 2; // change menu view
line = 0; // on start entry
break;
case 4: // "5: Play trophy sound"
play_rco_sound("system_plugin", "snd_trophy");
break;
case 5: // "6: Make screenshot"
screenshot(0); // xmb only
play_rco_sound("system_plugin", "snd_system_ok");
break;
case 6: // "7: Make screenshot with menu"
screenshot(1); //
play_rco_sound("system_plugin", "snd_system_ok");
break;
case 7: // "8: Reset PS3"
stop_VSH_Menu();
delete_turnoff_flag();
sys_timer_sleep(1);
shutdown_reset(2);
break;
case 8: // "9: Shutdown PS3"
stop_VSH_Menu();
delete_turnoff_flag();
sys_timer_sleep(1);
shutdown_reset(1);
break;
}
break;
case 1: // second menu view
switch(line)
{
case 0: // 1: Back to main view"
view = line = 0;
break;
case 1: // 2: screenshot
screenshot(1);
break;
case 2: // 3: Alpha
//...
break;
case 3: // 4: Red
//...
break;
case 4: // 5: Green
//...
break;
case 5: // 6: Blue
//...
break;
case 6: // 7: test
break;
}
break;
case 2: // third menu view
switch(line)
{
case 0: // "1: Back to main view"
view = line = 0;
break;
case 1: // "2: test string..."
//...
break;
case 2: // "3: test string..."
//...
break;
case 3: // "4: test string..."
//...
break;
case 4: // "5: test string..."
//...
break;
}
break;
}
}
/***********************************************************************
* plugin main ppu thread
***********************************************************************/
static void vsh_menu_thread(uint64_t arg)
{
#ifdef DEBUG
dbg_init();
dbg_printf("programstart:\n");
#endif
uint16_t oldpad = 0, curpad = 0;
CellPadData pdata;
// wait for XMB, feedback
sys_timer_sleep(13);
//vshtask_notify("sprx running...");
play_rco_sound("system_plugin", "snd_trophy");
#ifdef HAVE_STARFIELD
init_once(/* stars */);
#endif
// custom bg_color init
a = GET_A(bg_color_menu[1]);
r = GET_R(bg_color_menu[1]);
g = GET_G(bg_color_menu[1]);
b = GET_B(bg_color_menu[1]);
while(1)
{
// if VSH Menu is running, we get pad data over our MyPadGetData()
// else, we use the vsh pad_data struct
if(menu_running)
MyPadGetData(0, &pdata);
else
VSHPadGetData(&pdata);
// if pad_data and we are in XMB(vshmain_EB757101() == 0)
if((pdata.len > 0)
&& (vshmain_EB757101() == 0)
)
{
curpad = (pdata.button[2] | (pdata.button[3] << 8));
if((curpad & PAD_SELECT) && (curpad != oldpad))
{
switch(menu_running)
{
// VSH Menu not running, start VSH Menu
case 0:
// main view and start on first entry
view = line = 0;
//
pause_RSX_rendering();
// create VSH Menu heap memory from memory container 1("app")
create_heap(64); // 64 MB
// initialize VSH Menu graphic (init drawing context, alloc buffers, blah, blah, blah...)
init_graphic();
// stop vsh pad
start_stop_vsh_pad(0);
// set menu_running
menu_running = 1;
break;
// VSH Menu is running, stop VSH Menu
case 1:
stop_VSH_Menu();
// restart vsh pad
start_stop_vsh_pad(1);
break;
}
oldpad = 0;
sys_timer_usleep(300000);
}
// VSH Menu is running, draw menu / check pad
if(menu_running)
{
#ifdef DEBUG
dbg_printf("%p\n", pdata);
#endif
draw_frame(&pdata);
flip_frame();
if(curpad != oldpad)
{
if(curpad & PAD_UP)
{
if(line <= 0){
line = 0;
}else{
line--;
play_rco_sound("system_plugin", "snd_cursor");
}
}
if(curpad & PAD_DOWN)
{
if(line >= max_menu[view]-1){
line = max_menu[view]-1;
}else{
line++;
play_rco_sound("system_plugin", "snd_cursor");
}
}
if(curpad & PAD_LEFT
|| curpad & PAD_RIGHT) do_leftright_action(curpad);
if(curpad & PAD_CROSS) do_menu_action();
}
// ...
sys_timer_usleep(30);
} // end VSH Menu is running
oldpad = curpad;
}else{
oldpad = 0;
}
}
#ifdef DEBUG
dbg_fini();
#endif
sys_ppu_thread_exit(0);
}
/***********************************************************************
* start thread
***********************************************************************/
int32_t vsh_menu_start(uint64_t arg)
{
sys_ppu_thread_create(&vsh_menu_tid, vsh_menu_thread, 0, 3000, 0x4000, 1, THREAD_NAME);
_sys_ppu_thread_exit(0);
return SYS_PRX_RESIDENT;
}
/***********************************************************************
* stop thread
***********************************************************************/
static void vsh_menu_stop_thread(uint64_t arg)
{
done = 1;
if(vsh_menu_tid != (sys_ppu_thread_t)-1)
{
uint64_t exit_code;
sys_ppu_thread_join(vsh_menu_tid, &exit_code);
}
sys_ppu_thread_exit(0);
}
/***********************************************************************
*
***********************************************************************/
static void finalize_module(void)
{
uint64_t meminfo[5];
sys_prx_id_t prx = prx_get_module_id_by_address(finalize_module);
meminfo[0] = 0x28;
meminfo[1] = 2;
meminfo[3] = 0;
system_call_3(482, prx, 0, (uint64_t)(uint32_t)meminfo);
}
/***********************************************************************
*
***********************************************************************/
int vsh_menu_stop(void)
{
sys_ppu_thread_t t;
uint64_t exit_code;
sys_ppu_thread_create(&t, vsh_menu_stop_thread, 0, 0, 0x2000, 1, STOP_THREAD_NAME);
sys_ppu_thread_join(t, &exit_code);
finalize_module();
_sys_ppu_thread_exit(0);
return SYS_PRX_STOP_OK;
}