126 lines5.0 KBC
1/*
2plat.h: what the app needs from the machine it runs on (src/ps3, PSL1GHT).
3*/
4#ifndef PLAT_H
5#define PLAT_H
6
7#include <stdint.h>
8#include <stddef.h>
9
10#define SCREEN_W 1280
11#define SCREEN_H 720
12
13// ---- log (USRDIR/ps3world_log.txt on the PS3) ----
14void plat_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
15
16// ---- time ----
17uint64_t plat_time_us(void);
18void plat_sleep_us(unsigned us);
19
20// ---- threads ----
21typedef void (*plat_thread_fn)(void *arg);
22// prio: PS3 style, 0 highest; the main thread is ~1000
23int plat_thread_start(plat_thread_fn fn, void *arg, int prio, int stack_kb, const char *name);
24
25// a blocking lock for worker threads (never taken on the audio thread)
26typedef struct plat_mutex plat_mutex_t;
27plat_mutex_t *plat_mutex_new(void);
28void plat_mutex_lock(plat_mutex_t *m);
29void plat_mutex_unlock(plat_mutex_t *m);
30
31// a spin flag the audio thread can try without blocking
32static inline int spin_trylock(volatile int *f)
33{
34 if (!__sync_bool_compare_and_swap(f, 0, 1))
35 return 0;
36 __sync_synchronize();
37 return 1;
38}
39static inline void spin_lock(volatile int *f)
40{
41 while (!spin_trylock(f))
42 plat_sleep_us(50);
43}
44static inline void spin_unlock(volatile int *f)
45{
46 __sync_synchronize();
47 *f = 0;
48}
49
50// ---- files ----
51const char *plat_data_dir(void); // bundled fonts etc. (read-only)
52const char *plat_save_dir(void); // settings, log (writable)
53int plat_system_lang(void); // the console's language, as sysutil numbers it (1: English)
54
55// ---- sound effects ----
56enum { SND_MOVE, SND_OK, SND_BACK, SND_COUNT };
57int plat_sound_init(void); // 0: no sound (the UI goes on without)
58void plat_sound(int id); // starts one (mixed with any playing); any thread
59void plat_sound_enable(int on);
60void plat_sound_quit(void);
61// reads a whole file; NULL if it isn't there. *len gets the size. Caller frees.
62unsigned char *plat_read_file(const char *path, size_t *len);
63int plat_write_file(const char *path, const void *data, size_t len);
64// downloads and install targets: big files are written with stdio (fopen/fwrite), these do the rest
65long long plat_file_size(const char *path); // -1 if it isn't there (or is a folder)
66long long plat_file_mtime(const char *path); // when it was last written (seconds since 1970); 0 if it isn't there
67int plat_dir_exists(const char *path);
68int plat_mkdirs(const char *path); // every missing folder on the way; 1 if it's there after
69long long plat_free_bytes(const char *path); // on the drive path is on; -1 if unknown
70// up to max names of the folders in path (each < 64 chars); returns how many
71int plat_list_dirs(const char *path, char (*names)[64], int max);
72
73// ---- screen ----
74// the canvas to draw the next frame into (SCREEN_W x SCREEN_H, 0xAARRGGBB)
75uint32_t *plat_canvas_next(void);
76
77// ---- on-screen keyboard (Search) ----
78int plat_osk_open(const char *title, const char *initial); // 1 if it's up
79int plat_osk_active(void);
80int plat_osk_poll(char *out, int size); // 1 text entered, -1 cancelled, 0 still open
81
82// ---- system ----
83int plat_circle_confirms(void); // the console uses O to confirm and X to go back (Japan, Asia)
84void plat_quit(void); // back to the XMB, as the PS button's Quit does (after this frame)
85void plat_random(void *buf, int n); // unpredictable bytes
86// the picture: drawn by the CPU (720p) at the next start, rather than the graphics chip
87int plat_renderer_cpu(void); // chosen for the next start
88void plat_renderer_set_cpu(int cpu);
89int plat_renderer_gpu_now(void); // the graphics chip draws this time
90
91// ---- HTTP (http_ps3.c; http_sim.c in the simulator) ----
92typedef struct {
93 const char *method; // "GET" (NULL) or "POST"
94 const char *url;
95 const char *headers; // extra header lines, each ending "\r\n" (or NULL)
96 const void *body;
97 int body_len;
98 volatile int *cancel; // set to stop a download
99 int timeout_s; // per read; 0 = 30
100 // streaming: called with each piece of the body; return 0 to stop
101 int (*sink)(void *ctx, const void *data, int len);
102 void *ctx;
103 int no_redirect; // a 3xx is the answer, not somewhere to go
104 int total_s; // the whole request, at most (0: no limit, for streams)
105} http_req_t;
106
107typedef struct {
108 int status; // HTTP status, 0 if no reply
109 long long length; // Content-Length, -1 if none
110 char content_type[64];
111 char err[160]; // why it failed, for the screen
112} http_res_t;
113
114// Any number of threads may make requests at once. Returns 1 when a reply
115// with any status came back in full; sink gets the body of every status.
116int http_request(const http_req_t *req, http_res_t *res);
117
118// GET/POST into memory (NUL-terminated). Returns the body (caller frees) or NULL.
119char *http_fetch(const char *method, const char *url, const char *headers, const char *body,
120 int timeout_s, volatile int *cancel, size_t *len_out, http_res_t *res);
121// the same, with HTTP_NO_REDIRECT (an identity check must ask the address itself)
122#define HTTP_NO_REDIRECT 1
123char *http_fetch_ex(const char *method, const char *url, const char *headers, const char *body,
124 int timeout_s, volatile int *cancel, size_t *len_out, http_res_t *res, int flags);
125
126#endif