| 1 | /* |
| 2 | plat.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) ---- |
| 14 | void plat_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); |
| 15 |
|
| 16 | // ---- time ---- |
| 17 | uint64_t plat_time_us(void); |
| 18 | void plat_sleep_us(unsigned us); |
| 19 |
|
| 20 | // ---- threads ---- |
| 21 | typedef void (*plat_thread_fn)(void *arg); |
| 22 | // prio: PS3 style, 0 highest; the main thread is ~1000 |
| 23 | int 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) |
| 26 | typedef struct plat_mutex plat_mutex_t; |
| 27 | plat_mutex_t *plat_mutex_new(void); |
| 28 | void plat_mutex_lock(plat_mutex_t *m); |
| 29 | void plat_mutex_unlock(plat_mutex_t *m); |
| 30 |
|
| 31 | // a spin flag the audio thread can try without blocking |
| 32 | static 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 | } |
| 39 | static inline void spin_lock(volatile int *f) |
| 40 | { |
| 41 | while (!spin_trylock(f)) |
| 42 | plat_sleep_us(50); |
| 43 | } |
| 44 | static inline void spin_unlock(volatile int *f) |
| 45 | { |
| 46 | __sync_synchronize(); |
| 47 | *f = 0; |
| 48 | } |
| 49 |
|
| 50 | // ---- files ---- |
| 51 | const char *plat_data_dir(void); // bundled fonts etc. (read-only) |
| 52 | const char *plat_save_dir(void); // settings, log (writable) |
| 53 | int plat_system_lang(void); // the console's language, as sysutil numbers it (1: English) |
| 54 |
|
| 55 | // ---- sound effects ---- |
| 56 | enum { SND_MOVE, SND_OK, SND_BACK, SND_COUNT }; |
| 57 | int plat_sound_init(void); // 0: no sound (the UI goes on without) |
| 58 | void plat_sound(int id); // starts one (mixed with any playing); any thread |
| 59 | void plat_sound_enable(int on); |
| 60 | void plat_sound_quit(void); |
| 61 | // reads a whole file; NULL if it isn't there. *len gets the size. Caller frees. |
| 62 | unsigned char *plat_read_file(const char *path, size_t *len); |
| 63 | int 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 |
| 65 | long long plat_file_size(const char *path); // -1 if it isn't there (or is a folder) |
| 66 | long long plat_file_mtime(const char *path); // when it was last written (seconds since 1970); 0 if it isn't there |
| 67 | int plat_dir_exists(const char *path); |
| 68 | int plat_mkdirs(const char *path); // every missing folder on the way; 1 if it's there after |
| 69 | long 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 |
| 71 | int 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) |
| 75 | uint32_t *plat_canvas_next(void); |
| 76 |
|
| 77 | // ---- on-screen keyboard (Search) ---- |
| 78 | int plat_osk_open(const char *title, const char *initial); // 1 if it's up |
| 79 | int plat_osk_active(void); |
| 80 | int plat_osk_poll(char *out, int size); // 1 text entered, -1 cancelled, 0 still open |
| 81 |
|
| 82 | // ---- system ---- |
| 83 | int plat_circle_confirms(void); // the console uses O to confirm and X to go back (Japan, Asia) |
| 84 | void plat_quit(void); // back to the XMB, as the PS button's Quit does (after this frame) |
| 85 | void 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 |
| 87 | int plat_renderer_cpu(void); // chosen for the next start |
| 88 | void plat_renderer_set_cpu(int cpu); |
| 89 | int plat_renderer_gpu_now(void); // the graphics chip draws this time |
| 90 |
|
| 91 | // ---- HTTP (http_ps3.c; http_sim.c in the simulator) ---- |
| 92 | typedef 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 |
|
| 107 | typedef 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. |
| 116 | int 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. |
| 119 | char *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 |
| 123 | char *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 |