-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
439 lines (413 loc) · 13.9 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
/*
* prxshot module
*
* Copyright (C) 2011 Codestation
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <pspsdk.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspthreadman.h>
#include <pspctrl.h>
#include <pspinit.h>
#include <stdio.h>
#include <string.h>
#include "pspdefs.h"
#include "kalloc.h"
#include "bitmap.h"
#include "pbp.h"
#include "minIni.h"
#include "logger.h"
PSP_MODULE_INFO("prxshot", 0x1000, 0, 3);
PSP_MAIN_THREAD_ATTR(0);
PSP_HEAP_SIZE_KB(8);
#define PICTURE_DIR_MS "ms0:/PSP/SCREENSHOT"
#define PICTURE_DIR_GO "ef0:/PSP/SCREENSHOT"
#define GAMEID_DIR "disc0:/UMD_DATA.BIN"
#define SHOT_BLK_NAME "shot_blk"
#define MAX_IMAGES 10000
#define BMP_SIZE 391734
// file paths and files
char eboot_path[128];
char ini_path[128];
char screenshot_filename[64];
char screenshot_basedir[32];
char picture[32];
char gameid[12];
SceUID last_id = 0;
int game_found = 0;
int eboot_found = 0;
int directory_ready = 0;
STMOD_HANDLER previous = NULL;
int module_found = 0;
int umd_need_gameid = 0;
// prxshot.ini settings
int key_button;
int force_ms0;
int clear_cache;
unsigned int key_timeout;
unsigned int last_msecs = 0;
void *get_mem(SceSize size, int *id) {
void *mem = NULL;
int api = sceKernelInitKeyConfig();
if((game_found || api == PSP_INIT_KEYCONFIG_VSH) && sceKernelGetModel() >= PSP_MODEL_SLIM) {
// use the umd cache only if is a game and slim or superior
mem = kalloc(size, SHOT_BLK_NAME, id, PSP_MEMORY_PARTITION_UMDCACHE, PSP_SMEM_Low);
}
if(!mem) {
// else get the memory from kernel
mem = kalloc(size, SHOT_BLK_NAME, id, PSP_MEMORY_PARTITION_KERNEL, PSP_SMEM_Low);
if(!mem && api == PSP_INIT_KEYCONFIG_GAME) {
// as a last resort, use the volatile mem
mem = kalloc_volatile();
}
}
return mem;
}
int take_shot(const char *path) {
void *frame_addr;
SceUID block_id = -1;
int frame_width, pixel_format;
unsigned int ptr;
void *mem = get_mem(BMP_SIZE, &block_id);
if(mem) {
sceDisplayWaitVblankStart();
if(sceDisplayGetFrameBuf(&frame_addr, &frame_width, &pixel_format, PSP_DISPLAY_SETBUF_NEXTFRAME) >= 0 && frame_addr) {
ptr = (unsigned int)frame_addr;
ptr |= ptr & 0x80000000 ? 0xA0000000 : 0x40000000;
bitmapWrite((void *)ptr, mem, pixel_format, path);
block_id >= 0 ? kfree(block_id) : kfree_volatile();
return 0;
}
}
return -1;
}
int update_filename(const char *basedir, char *filename) {
int end = last_id;
do {
sprintf(filename, picture, basedir, last_id++);
if(last_id == MAX_IMAGES)
last_id = 0;
SceUID fd = sceIoOpen(filename, PSP_O_RDONLY, 0777);
if(fd < 0) {
return last_id;
}
sceIoClose(fd);
} while(end != last_id);
return -1;
}
int get_eboot_gameid(char *buffer) {
char gameid[12];
if(*eboot_path) {
kprintf("using %s as eboot path\n", eboot_path);
if(generate_gameid(eboot_path, gameid, sizeof(gameid))) {
kprintf("Got gameid: %s\n", gameid);
strcpy(buffer, gameid);
game_found = 1;
return 1;
}
}
return 0;
}
int get_umd_gameid(char *buffer) {
char gameid[12];
kprintf("Trying to open %s\n", GAMEID_DIR);
SceUID fd = sceIoOpen(GAMEID_DIR, PSP_O_RDONLY, 0777);
if(fd >= 0) {
kprintf("UMD/ISO found\n");
game_found = 1;
sceIoRead(fd, gameid, 10);
gameid[10] = 0;
sceIoClose(fd);
strcpy(buffer, gameid);
if(gameid[4] == '-')
strcpy(buffer + 4, gameid + 5);
return 1;
}
return 0;
}
int build_gamedir(char *dir, const char *argp) {
if(!directory_ready) {
directory_ready = 1;
int model = sceKernelGetModel();
kprintf("Directory doesn't exists or is outdated\n");
if(force_ms0)
model = PSP_MODEL_SLIM;
strcpy(dir, model == PSP_MODEL_GO ? PICTURE_DIR_GO : PICTURE_DIR_MS);
kprintf("Creating base directory %s\n", dir);
sceIoMkdir(dir, 0777);
strcat(dir,"/");
if(sceKernelInitKeyConfig() == PSP_INIT_KEYCONFIG_VSH) {
strcat(dir, "XMB");
} else if(*gameid) {
strcat(dir, gameid);
} else if(!get_eboot_gameid(dir + strlen(model == PSP_MODEL_GO ? PICTURE_DIR_GO : PICTURE_DIR_MS)+1)) {
strcat(dir, "Homebrew");
}
kprintf("Creating directory %s\n", dir);
sceIoMkdir(dir, 0777);
return 1;
}
return 0;
}
// this causes problems to game categories D:
void update_xmb_cache() {
if(clear_cache && sceKernelInitKeyConfig() == PSP_INIT_KEYCONFIG_VSH) {
kprintf("Refreshing the xmb cache\n");
sceIoDevctl("fatms0:", 0x0240D81E, NULL, 0, NULL, 0);
}
}
int pbp_thread_start(SceSize args, void *argp) {
kprintf("pbp_thread_start called\n");
write_pbp(screenshot_basedir, *eboot_path ? eboot_path : NULL, argp);
// refresh the cache after creating the PSCM.DAT
update_xmb_cache();
return 0;
}
int module_start_handler(SceModule2 *module) {
kprintf("Loading: %s\n", module->modname);
if(!module_found) {
const char *path = NULL;
if(!*eboot_path) {
path = sceKernelInitFileName();
if(path) {
kprintf("Got path: %s\n", path);
strcpy(eboot_path, path);
}
}
if(sceKernelInitKeyConfig() == PSP_INIT_KEYCONFIG_POPS) {
kprintf("POPS found: %s\n", path);
directory_ready = 0;
module_found = 1;
} else if(sceKernelInitKeyConfig() == PSP_INIT_KEYCONFIG_GAME) {
// user module found
if((module->text_addr & 0x80000000) != 0x80000000) {
kprintf("User module found\n");
if(!strcmp(module->modname, "aLoader") ||
// blacklist open idea loader
!strcmp(module->modname, "OpenIdeaController") ||
!strcmp(module->modname, "ISO Loader Eboot") ||
// blacklist the Prometheus iso loader
!strcmp(module->modname, "PLoaderGUI")) {
// loader found, so a ISO has been loaded
kprintf("Loader detected: %s\n", module->modname);
module_found = 1;
umd_need_gameid = 1;
directory_ready = 0;
} else if(strcmp(module->modname, "sceKernelLibrary")) {
// eboot found
kprintf("EBOOT detected\n");
module_found = 1;
eboot_found = 1;
directory_ready = 0;
}
}
} else {
kprintf("Unknown mode detected: %08X\n", sceKernelInitKeyConfig());
}
}
return previous ? previous(module) : 0;
}
#ifdef KPRINTF_ENABLED
inline void boot_info() {
int boot = sceKernelBootFrom();
int key = sceKernelInitKeyConfig();
switch(boot) {
case PSP_BOOT_MS:
kprintf("Booting from Memory Stick\n");
break;
case PSP_BOOT_DISC:
kprintf("Booting from UMD\n");
break;
case PSP_BOOT_FLASH:
kprintf("Booting from Flash\n");
break;
default:
kprintf("Booting from: %i\n", boot);
}
switch(key) {
case PSP_INIT_KEYCONFIG_GAME:
kprintf("Init mode: Game\n");
break;
case PSP_INIT_KEYCONFIG_VSH:
kprintf("Init mode: VSH\n");
break;
case PSP_INIT_KEYCONFIG_POPS:
kprintf("Init mode: POPS\n");
break;
default:
kprintf("Init mode: %i\n", key);
}
}
#endif
inline void read_settings(const char *argp) {
create_path(ini_path, argp, "prxshot.ini");
key_button = ini_getlhex("General", "ScreenshotKey", PSP_CTRL_NOTE, ini_path);
kprintf("Read ScreenshotKey: %08X\n", key_button);
key_timeout = ini_getl("General", "KeyTimeout", 0, ini_path);
kprintf("Read KeyTimeout: %08X\n", key_timeout);
ini_gets("General", "ScreenshotName", "%s/pic_%04d.bmp", picture, sizeof(picture), ini_path);
kprintf("Read ScreenshotName: %s\n", picture);
force_ms0 = ini_getbool("General", "PSPGoUseMS0", 0, ini_path);
clear_cache = ini_getbool("General", "XMBClearCache", 0, ini_path);
kprintf("Read XMBClearCache: %i\n", clear_cache);
if(force_ms0) {
kprintf("PSPGoUseMS0 enabled, forcing ms0\n");
//Make sure that the /PSP directory exists first
sceIoMkdir("ms0:/PSP", 0777);
}
}
inline int refresh_directory(const char *dir) {
if(sceKernelInitKeyConfig() == PSP_INIT_KEYCONFIG_VSH) {
SceUID dfd = sceIoDopen(dir);
if(dfd >= 0) {
sceIoDclose(dfd);
} else {
kprintf("Recreating %s\n", dir);
sceIoMkdir(dir, 0777);
return 1;
}
}
return 0;
}
unsigned int getMilliseconds() {
SceKernelSysClock clock;
sceKernelGetSystemTime(&clock);
unsigned int time = clock.low / 1000;
time += clock.hi * (0xFFFFFFFF / 1000);
return time;
}
int isButtonPressed(int buttons, int mask) {
if(key_timeout == 0) {
return (buttons & mask) == mask ? 1 : 0;
} else {
if((buttons & mask) == mask) {
if(last_msecs == 0) {
last_msecs = getMilliseconds();
} else {
if(getMilliseconds() - last_msecs > key_timeout) {
last_msecs = 0;
return 1;
}
}
} else {
last_msecs = 0;
}
}
return 0;
}
int thread_start(SceSize args, void *argp) {
// read config file
kprintf("PRXshot main thread started\n");
#ifdef KPRINTF_ENABLED
boot_info();
#endif
// get custom settings from prxshot.ini
read_settings(argp);
// clear buffer
memset(eboot_path, 0, sizeof(eboot_path));
if(sceKernelInitKeyConfig() != PSP_INIT_KEYCONFIG_VSH) {
if(sceKernelBootFrom() != PSP_BOOT_DISC) {
kprintf("Booting from Memory Stick/Internal Storage\n");
previous = sctrlHENSetStartModuleHandler(module_start_handler);
}else {
umd_need_gameid = 1;
}
}
// current screenshot number
int picture_id = 0;
// flag for checking if the pbp needs to be created
int pbp_created = 0;
kprintf("Entering screenshot loop\n");
while(picture_id >= 0) {
SceCtrlData pad;
sceCtrlPeekBufferPositive(&pad, 1);
if(pad.Buttons) {
if(umd_need_gameid) {
if(get_umd_gameid(gameid)) {
*eboot_path = '\0';
umd_need_gameid = 0;
directory_ready = 0;
// get a custom key press for this specific game
key_button = ini_getlhex("CustomKeys", gameid, key_button, ini_path);
key_timeout = ini_getl("CustomTimeout", gameid, key_timeout, ini_path);
kprintf("Got custom key button for %s: %08X\n", gameid, key_button);
kprintf("Got custom key timeout for %s: %u\n", gameid, key_timeout);
}
} else if(eboot_found) {
if(read_gameid(eboot_path, gameid, sizeof(gameid))) {
key_button = ini_getlhex("CustomKeys", gameid, key_button, ini_path);
key_timeout = ini_getl("CustomTimeout", gameid, key_timeout, ini_path);
kprintf("Got custom key button for %s: %08X\n", gameid, key_button);
kprintf("Got custom key timeout for %s: %u\n", gameid, key_timeout);
}
//FIXME: there isn't a way to migrate from the old generated
//gameid for game eboots so we tell that we haven't found it
*gameid = '\0';
eboot_found = 0;
}
// check if the button combination is correct
//if((pad.Buttons & key_button) == key_button) {
if(isButtonPressed(pad.Buttons, key_button)) {
// attempt to create a screenshot directory
if(build_gamedir(screenshot_basedir, argp)) {
// if it was susseful then reset the image counter
last_id = 0;
kprintf("New screenshot dir created, new pbp needed\n");
picture_id = update_filename(screenshot_basedir, screenshot_filename);
pbp_created = 0;
}
// recreate the XMB screenshot directory if is deleted
if(refresh_directory(screenshot_basedir)) {
last_id = 0;
pbp_created = 0;
}
kprintf("Taking shot: %s\n", screenshot_filename);
// take the screenshot
if(take_shot(screenshot_filename) == 0) {
kprintf("Screenshot OK\n");
} else {
kprintf("Screenshot fail\n");
}
//update the filename and wait for the next shot
picture_id = update_filename(screenshot_basedir, screenshot_filename);
//launch a thread after the first shot to create the PSCM.DAT
if(!pbp_created) {
SceUID thid = sceKernelCreateThread("pbp_thread", pbp_thread_start, 0x20, 4096, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, args, argp);
} else {
kprintf("Failed to start thread: %08X\n", thid);
}
pbp_created = 1;
} else {
update_xmb_cache();
}
}
}
sceKernelDelayThread(100000); //1.000.000 = 1 seg
}
return 0;
}
int module_start(SceSize argc, void *argp) {
kprintf(">>>>>>>>>>>>>>>>>\nPRXshot started\n");
SceUID thid = sceKernelCreateThread("prxshot", thread_start, 0x10, 4096, 0, 0);
if(thid >= 0)
sceKernelStartThread(thid, argc, argp);
return 0;
}
int module_stop(SceSize argc, void *argp) {
return 0;
}