/
platform.h
360 lines (291 loc) · 14.6 KB
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
9
10
11
12
13
14
15
16
17
#ifndef _INCL_PLATFORM_H_
#define _INCL_PLATFORM_H_
#include "universal.h"
#ifdef __cplusplus
extern "C" {
#endif
18
// this is called by your mainline.
19
int MojoSetup_main(int argc, char **argv);
20
21
// Caller must free returned string!
22
char *MojoPlatform_appBinaryPath(void);
23
24
25
26
// Caller must free returned string!
char *MojoPlatform_currentWorkingDir(void);
27
28
29
// Caller must free returned string!
char *MojoPlatform_homedir(void);
30
31
uint32 MojoPlatform_ticks(void);
32
33
34
35
36
37
38
// Make current process kill itself immediately, without any sort of internal
// cleanup, like atexit() handlers or static destructors...the OS will have
// to sort out the freeing of any resources, and no more code in this
// process than necessary should run. This function does not return. Try to
// avoid calling this.
void MojoPlatform_die(void);
39
40
// Delete a file from the physical filesystem. This should remove empty
// directories as well as files. Returns true on success, false on failure.
41
42
boolean MojoPlatform_unlink(const char *fname);
43
44
45
46
// Resolve symlinks, relative paths, etc. Caller free()'s buffer. Returns
// NULL if path couldn't be resolved.
char *MojoPlatform_realpath(const char *path);
47
48
49
50
51
// Create a symlink in the physical filesystem. (src) is the NAME OF THE LINK
// and (dst) is WHAT IT POINTS TO. This is backwards from the unix symlink()
// syscall! Returns true if link was created, false otherwise.
boolean MojoPlatform_symlink(const char *src, const char *dst);
52
53
54
55
56
// Read the destination from symlink (linkname). Returns a pointer
// allocated with xmalloc() containing the linkdest on success, and NULL
// on failure. The caller is responsible for freeing the returned pointer!
char *MojoPlatform_readlink(const char *linkname);
57
58
59
60
// !!! FIXME: we really can't do this in a 16-bit value...non-Unix platforms
// !!! FIXME: and Extended Attributes need more.
// Create a directory in the physical filesystem, with (perms) permissions.
// returns true if directory is created, false otherwise.
61
boolean MojoPlatform_mkdir(const char *path, uint16 perms);
62
63
64
65
66
67
68
// Move a file to a new name. This has to be a fast (if not atomic) operation,
// so if it would require a legitimate copy to another filesystem or device,
// this should fail, as the standard Unix rename() function does.
// Returns true on successful rename, false otherwise.
boolean MojoPlatform_rename(const char *src, const char *dst);
69
70
// Determine if dir/fname exists in the native filesystem. It doesn't matter
// if it's a directory, file, symlink, etc, we're just looking for the
71
// existence of the entry itself. (fname) may be NULL, in which case,
72
73
74
75
// (dir) contains the whole path, otherwise, the platform layer needs to
// build the path: (on Unix: dir/path, on Windows: dir\\path, etc).
// This is a convenience thing for the caller.
// Returns true if path in question exists, false otherwise.
76
77
boolean MojoPlatform_exists(const char *dir, const char *fname);
78
79
80
81
82
// Returns true if (fname) in the native filesystem is writable. If (fname)
// is a directory, this means that the contents of the directory can be
// added to (create files, delete files, etc). If (fname) is a file, this
// means that this process has write access to the file.
// Returns false if (fname) isn't writable.
83
84
boolean MojoPlatform_writable(const char *fname);
85
86
// Returns true if (dir) is a directory in the physical filesystem, false
// otherwise (including if (dir) doesn't exist). Don't follow symlinks.
87
88
boolean MojoPlatform_isdir(const char *dir);
89
90
// Returns true if (fname) is a symlink in the physical filesystem, false
// otherwise (including if (fname) doesn't exist). Don't follow symlinks.
91
92
boolean MojoPlatform_issymlink(const char *fname);
93
94
95
// Returns true if stdin and stdout are connected to a tty.
boolean MojoPlatform_istty(void);
96
97
// Returns true if (fname) is a regular file in the physical filesystem, false
// otherwise (including if (fname) doesn't exist). Don't follow symlinks.
98
99
boolean MojoPlatform_isfile(const char *fname);
100
101
// Returns size, in bytes, of the file (fname) in the physical filesystem.
// Return -1 if file is missing or not a file. Don't follow symlinks.
102
103
int64 MojoPlatform_filesize(const char *fname);
104
105
// !!! FIXME: we really can't do this in a 16-bit value...non-Unix platforms
// !!! FIXME: and Extended Attributes need more.
106
107
108
// !!! FIXME: comment me.
boolean MojoPlatform_perms(const char *fname, uint16 *p);
109
110
// !!! FIXME: we really can't do this in a 16-bit value...non-Unix platforms
// !!! FIXME: and Extended Attributes need more.
111
112
113
// !!! FIXME: comment me.
boolean MojoPlatform_chmod(const char *fname, uint16 p);
114
115
116
117
118
119
120
// Try to locate a specific piece of media (usually an inserted CD or DVD).
// (uniquefile) is a path that should exist on the media; its presence
// should uniquely identify the media.
// Returns the path of the media's mount point in the physical filesystem
// (something like "E:\\" on Windows or "/mnt/cdrom" on Unix), or NULL if
// the media isn't found (needed disc isn't inserted, etc).
// Caller must free return value!
121
122
char *MojoPlatform_findMedia(const char *uniquefile);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Flag values for MojoPlatform_fopen().
typedef enum
{
MOJOFILE_READ=(1<<0),
MOJOFILE_WRITE=(1<<1),
MOJOFILE_CREATE=(1<<2),
MOJOFILE_EXCLUSIVE=(1<<3),
MOJOFILE_TRUNCATE=(1<<4),
MOJOFILE_APPEND=(1<<5),
} MojoFileFlags;
typedef enum
{
MOJOSEEK_SET,
MOJOSEEK_CURRENT,
MOJOSEEK_END
} MojoFileSeek;
141
142
143
144
145
146
147
// !!! FIXME: we really can't do this in a 16-bit value...non-Unix platforms
// !!! FIXME: and Extended Attributes need more.
// Open file (fname). This wraps a subset of the Unix open() syscall. Use
// MojoFileFlags for (flags). If creating a file, use (mode) for the new
// file's permissions.
// Returns an opaque handle on success, NULL on error. Caller must free
// handle with MojoPlatform_close() when done with it.
148
void *MojoPlatform_open(const char *fname, uint32 flags, uint16 mode);
149
150
151
152
153
154
155
// Return a handle that's compatible with MojoPlatform_open()'s return values
// that represents stdout. May return NULL for platforms that don't support
// this concept. You need to make sure that stdout itself doesn't really
// close in MojoPlatform_close(), at least for now.
void *MojoPlatform_stdout(void);
156
157
// Read (bytes) bytes from (fd) into (buf). This wraps the Unix read() syscall.
// Returns number of bytes read, -1 on error.
158
int64 MojoPlatform_read(void *fd, void *buf, uint32 bytes);
159
160
161
// Write (bytes) bytes from (buf) into (fd). This wraps the Unix write()
// syscall. Returns number of bytes read, -1 on error.
162
int64 MojoPlatform_write(void *fd, const void *buf, uint32 bytes);
163
164
// Reports byte offset of file pointer in (fd), or -1 on error.
165
int64 MojoPlatform_tell(void *fd);
166
167
168
169
// Seek to (offset) byte offset of file pointer in (fd), relative to (whence).
// This wraps the Unix lseek() syscall. Returns byte offset from the start
// of the file, -1 on error.
170
int64 MojoPlatform_seek(void *fd, int64 offset, MojoFileSeek whence);
171
172
173
// Get the size, in bytes, of a file, referenced by its opaque handle.
// (This pulls the data through an fstat() on Unix.) Retuns -1 on error.
174
int64 MojoPlatform_flen(void *fd);
175
176
177
// Force any pending data to disk, returns true on success, false if there
// was an i/o error.
178
179
boolean MojoPlatform_flush(void *fd);
180
181
182
183
184
// Free any resources associated with (fd), flushing any pending data to disk,
// and closing the file. (fd) becomes invalid after this call returns
// successfully. This wraps the Unix close() syscall. Returns true on
// success, false on i/o error.
boolean MojoPlatform_close(void *fd);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Enumerate a directory. Returns an opaque pointer that can be used with
// repeated calls to MojoPlatform_readdir() to enumerate the names of
// directory entries. Returns NULL on error. Non-NULL values should be passed
// to MojoPlatform_closedir() for cleanup when you are done with them.
void *MojoPlatform_opendir(const char *dirname);
// Get the next entry in the directory. (dirhandle) is an opaque pointer
// returned by MojoPlatform_opendir(). Returns NULL if we're at the end of
// the directory, or a null-terminated UTF-8 string otherwise. The order of
// results are not guaranteed, and may change between two iterations.
// Caller must free returned string!
char *MojoPlatform_readdir(void *dirhandle);
// Clean up resources used by a directory enumeration. (dirhandle) is an
// opaque pointer returned by MojoPlatform_opendir(), and becomes invalid
// after this call.
void MojoPlatform_closedir(void *dirhandle);
204
// Convert a string into a permissions bitmask. On Unix, this is currently
205
// expected to be an octal string like "0755", but may expect other forms
206
207
208
209
210
211
212
213
214
215
216
217
// in the future, and other platforms may need to interpret permissions
// differently. (str) may be NULL for defaults, and is considered valid.
// If (str) is not valid, return a reasonable default and set (*valid) to
// false. Otherwise, set (*valid) to true and return the converted value.
uint16 MojoPlatform_makePermissions(const char *str, boolean *valid);
// Return a default, sane set of permissions for a newly-created file.
uint16 MojoPlatform_defaultFilePerms(void);
// Return a default, sane set of permissions for a newly-created directory.
uint16 MojoPlatform_defaultDirPerms(void);
218
219
220
221
222
223
224
// Wrappers for Unix dlopen/dlsym/dlclose, sort of. Instead of a filename,
// these take a memory buffer for the library. If you can't load this
// directly in RAM, the platform should write it to a temporary file first,
// and deal with cleanup in MojoPlatform_dlclose(). The memory buffer must be
// dereferenced in MojoPlatform_dlopen(), as the caller may free() it upon
// return. Everything else works like the usual Unix calls.
void *MojoPlatform_dlopen(const uint8 *img, size_t len);
225
226
227
void *MojoPlatform_dlsym(void *lib, const char *sym);
void MojoPlatform_dlclose(void *lib);
228
229
230
231
232
233
// Launch the user's preferred browser to view the URL (url).
// Returns true if the browser launched, false otherwise. We can't know
// if the URL actually loaded, just if the browser launched. The hope is that
// the browser will inform the user if there's a problem loading the URL.
boolean MojoPlatform_launchBrowser(const char *url);
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Add a menu item to the Application menu or Start bar or whatever.
// (data) is 100% platform dependent right now, and this interface will
// likely change as we come to understand various systems' needs better.
// On Unix, it expects this to be a path to a FreeDesktop .desktop file.
// Returns (true) on success and (false) on failure.
boolean MojoPlatform_installDesktopMenuItem(const char *data);
// Remove a menu item from the Application menu or Start bar or whatever.
// (data) is 100% platform dependent right now, and this interface will
// likely change as we come to understand various systems' needs better.
// On Unix, it expects this to be a path to a FreeDesktop .desktop file.
// Returns (true) on success and (false) on failure.
boolean MojoPlatform_uninstallDesktopMenuItem(const char *data);
248
249
250
// Run a script from the archive in the OS
int MojoPlatform_runScript(const char *script, boolean devnull, const char **argv);
251
252
253
// Exec a given process name
int MojoPlatform_exec(const char *cmd);
254
255
256
257
258
259
#if !SUPPORT_MULTIARCH
#define MojoPlatform_switchBin(img, len)
#else
void MojoPlatform_switchBin(const uint8 *img, size_t len);
#endif
260
261
262
263
264
// Try to spawn a terminal, and possibly relaunch MojoSetup within it.
// If we can attach to a terminal without relaunching, do so and
// return true. false for failure to attach/spawn.
// May not return on success (process replaces itself).
boolean MojoPlatform_spawnTerminal(void);
265
266
267
268
269
// Put the calling process to sleep for at least (ticks) milliseconds.
// This is meant to yield the CPU while spinning in a loop that is polling
// for input, etc. Pumping the GUI event queue happens elsewhere, not here.
void MojoPlatform_sleep(uint32 ticks);
270
271
// Put a line of text to the system log, whatever that might be on a
272
273
274
275
// given platform. (str) is a complete line, but won't end with any newline
// characters. You should supply if needed.
void MojoPlatform_log(const char *str);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// This tries to decode a graphic file in memory into an RGBA framebuffer.
// Most platforms return NULL here. No one should call this; use decodeImage()
// instead, which will try included platform-independent code if this fails.
// This function is just here to allow a platform with the appropriate
// functionality to work without compiling in stb_image.c, or supply more
// formats over the built-in code.
// (data) points to the compressed data, (size) is the number of bytes
// of compressed data. (*w) and (*h) will contain the images dimensions on
// return.
// Returns NULL on failure (unsupported, etc) and a pointer to the
// uncompressed data on success. Caller must free() the returned pointer!
uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
uint32 *w, uint32 *h);
290
291
292
// Get the current locale, in the format "xx_YY" where "xx" is the language
// (en, fr, de...) and "_YY" is the country. (_US, _CA, etc). The country
// can be omitted. Don't include encoding, it's always UTF-8 at this time.
293
294
295
296
297
298
299
300
// Returns locale string, or NULL if it couldn't be determined.
// Caller must free() the returned pointer!
char *MojoPlatform_locale(void);
// !!! FIXME: document me.
// Caller must free() the returned pointer!
char *MojoPlatform_osType(void);
301
302
303
304
// !!! FIXME: document me.
// Caller must free() the returned pointer!
char *MojoPlatform_osMachine(void);
305
306
307
// !!! FIXME: document me.
// Caller must free() the returned pointer!
char *MojoPlatform_osVersion(void);
308
309
310
311
312
313
314
315
316
317
// !!! FIXME: document me.
uint64 MojoPlatform_getuid(void);
// !!! FIXME: document me.
uint64 MojoPlatform_geteuid(void);
// !!! FIXME: document me.
uint64 MojoPlatform_getgid(void);
318
319
320
321
322
323
324
325
// Basic platform detection.
#if PLATFORM_WINDOWS
#define PLATFORM_NAME "windows"
#elif PLATFORM_MACOSX
#define PLATFORM_NAME "macosx"
#elif PLATFORM_UNIX
#define PLATFORM_NAME "unix"
326
327
#elif PLATFORM_BEOS
#define PLATFORM_NAME "beos"
328
#else
329
#error Unknown platform.
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#endif
// Basic architecture detection.
#if defined(__powerpc64__)
#define PLATFORM_ARCH "powerpc64"
#elif defined(__ppc__) || defined(__powerpc__) || defined(__POWERPC__)
#define PLATFORM_ARCH "powerpc"
#elif defined(__x86_64__) || defined(_M_X64)
#define PLATFORM_ARCH "x86-64"
#elif defined(__X86__) || defined(__i386__) || defined(i386) || defined (_M_IX86) || defined(__386__)
#define PLATFORM_ARCH "x86"
#else
#error Unknown processor architecture.
#endif
346
347
348
349
350
351
352
// Other basic truths...
#if PLATFORM_WINDOWS
#define MOJOPLATFORM_ENDLINE "\r\n"
#else
#define MOJOPLATFORM_ENDLINE "\n"
#endif
353
354
355
356
357
358
#ifdef __cplusplus
}
#endif
#endif
359
// end of platform.h ...