Skip to content

Latest commit

 

History

History
348 lines (282 loc) · 14.2 KB

platform.h

File metadata and controls

348 lines (282 loc) · 14.2 KB
 
May 12, 2007
May 12, 2007
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
Mar 25, 2006
Mar 25, 2006
18
// this is called by your mainline.
Mar 25, 2006
Mar 25, 2006
19
int MojoSetup_main(int argc, char **argv);
Dec 9, 2006
Dec 9, 2006
21
// Caller must free returned string!
Dec 9, 2006
Dec 9, 2006
22
char *MojoPlatform_appBinaryPath(void);
Mar 25, 2006
Mar 25, 2006
23
May 20, 2007
May 20, 2007
24
25
26
// Caller must free returned string!
char *MojoPlatform_currentWorkingDir(void);
May 10, 2007
May 10, 2007
27
28
29
// Caller must free returned string!
char *MojoPlatform_homedir(void);
Dec 4, 2006
Dec 4, 2006
30
31
uint32 MojoPlatform_ticks(void);
Dec 9, 2006
Dec 9, 2006
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);
May 7, 2007
May 7, 2007
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.
Dec 9, 2006
Dec 9, 2006
41
42
boolean MojoPlatform_unlink(const char *fname);
Dec 14, 2006
Dec 14, 2006
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);
May 1, 2007
May 1, 2007
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);
Jul 2, 2007
Jul 2, 2007
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);
Sep 25, 2007
Sep 25, 2007
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.
May 18, 2007
May 18, 2007
61
boolean MojoPlatform_mkdir(const char *path, uint16 perms);
May 1, 2007
May 1, 2007
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);
Sep 25, 2007
Sep 25, 2007
69
70
71
72
73
74
75
// 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
// existance of the entry itself. (fname) may be NULL, in which case,
// (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.
Apr 22, 2007
Apr 22, 2007
76
77
boolean MojoPlatform_exists(const char *dir, const char *fname);
Sep 25, 2007
Sep 25, 2007
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.
May 10, 2007
May 10, 2007
83
84
boolean MojoPlatform_writable(const char *fname);
Sep 25, 2007
Sep 25, 2007
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.
May 10, 2007
May 10, 2007
87
88
boolean MojoPlatform_isdir(const char *dir);
Sep 25, 2007
Sep 25, 2007
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.
Sep 20, 2007
Sep 20, 2007
91
92
boolean MojoPlatform_issymlink(const char *fname);
Nov 21, 2007
Nov 21, 2007
93
94
95
// Returns true if stdin and stdout are connected to a tty.
boolean MojoPlatform_istty(void);
Sep 25, 2007
Sep 25, 2007
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.
Sep 20, 2007
Sep 20, 2007
98
99
boolean MojoPlatform_isfile(const char *fname);
Sep 25, 2007
Sep 25, 2007
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.
Sep 20, 2007
Sep 20, 2007
102
103
int64 MojoPlatform_filesize(const char *fname);
Sep 25, 2007
Sep 25, 2007
104
105
// !!! FIXME: we really can't do this in a 16-bit value...non-Unix platforms
// !!! FIXME: and Extended Attributes need more.
May 3, 2007
May 3, 2007
106
107
108
// !!! FIXME: comment me.
boolean MojoPlatform_perms(const char *fname, uint16 *p);
Sep 25, 2007
Sep 25, 2007
109
110
// !!! FIXME: we really can't do this in a 16-bit value...non-Unix platforms
// !!! FIXME: and Extended Attributes need more.
May 3, 2007
May 3, 2007
111
112
113
// !!! FIXME: comment me.
boolean MojoPlatform_chmod(const char *fname, uint16 p);
Sep 25, 2007
Sep 25, 2007
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!
Apr 22, 2007
Apr 22, 2007
121
122
char *MojoPlatform_findMedia(const char *uniquefile);
Sep 20, 2007
Sep 20, 2007
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;
Sep 25, 2007
Sep 25, 2007
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.
Sep 20, 2007
Sep 20, 2007
148
void *MojoPlatform_open(const char *fname, uint32 flags, uint16 mode);
Sep 25, 2007
Sep 25, 2007
149
Jan 14, 2008
Jan 14, 2008
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);
Sep 25, 2007
Sep 25, 2007
156
157
// Read (bytes) bytes from (fd) into (buf). This wraps the Unix read() syscall.
// Returns number of bytes read, -1 on error.
Sep 20, 2007
Sep 20, 2007
158
int64 MojoPlatform_read(void *fd, void *buf, uint32 bytes);
Sep 25, 2007
Sep 25, 2007
159
160
161
// Write (bytes) bytes from (buf) into (fd). This wraps the Unix write()
// syscall. Returns number of bytes read, -1 on error.
Sep 20, 2007
Sep 20, 2007
162
int64 MojoPlatform_write(void *fd, const void *buf, uint32 bytes);
Sep 25, 2007
Sep 25, 2007
163
164
// Reports byte offset of file pointer in (fd), or -1 on error.
Sep 20, 2007
Sep 20, 2007
165
int64 MojoPlatform_tell(void *fd);
Sep 25, 2007
Sep 25, 2007
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.
Sep 20, 2007
Sep 20, 2007
170
int64 MojoPlatform_seek(void *fd, int64 offset, MojoFileSeek whence);
Sep 25, 2007
Sep 25, 2007
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.
Sep 20, 2007
Sep 20, 2007
174
int64 MojoPlatform_flen(void *fd);
Sep 25, 2007
Sep 25, 2007
175
176
177
// Force any pending data to disk, returns true on success, false if there
// was an i/o error.
Sep 20, 2007
Sep 20, 2007
178
179
boolean MojoPlatform_flush(void *fd);
Sep 25, 2007
Sep 25, 2007
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);
Sep 20, 2007
Sep 20, 2007
185
Sep 20, 2007
Sep 20, 2007
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);
May 18, 2007
May 18, 2007
204
// Convert a string into a permissions bitmask. On Unix, this is currently
Sep 20, 2007
Sep 20, 2007
205
// expected to be an octal string like "0755", but may expect other forms
May 18, 2007
May 18, 2007
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);
Dec 9, 2006
Dec 9, 2006
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);
Dec 9, 2006
Dec 9, 2006
225
226
227
void *MojoPlatform_dlsym(void *lib, const char *sym);
void MojoPlatform_dlclose(void *lib);
Jan 25, 2008
Jan 25, 2008
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);
Feb 13, 2008
Feb 13, 2008
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);
May 20, 2007
May 20, 2007
248
249
250
251
252
253
#if !SUPPORT_MULTIARCH
#define MojoPlatform_switchBin(img, len)
#else
void MojoPlatform_switchBin(const uint8 *img, size_t len);
#endif
Nov 21, 2007
Nov 21, 2007
254
255
256
257
// Try to spawn a terminal, and relaunch MojoSetup within it.
// Does not return on success (process replaces itself).
void MojoPlatform_spawnTerminal(void);
May 7, 2007
May 7, 2007
258
259
260
261
// 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);
Apr 22, 2007
Apr 22, 2007
262
Dec 10, 2006
Dec 10, 2006
263
264
265
266
267
// Put a line of text to the the system log, whatever that might be on a
// 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);
Nov 24, 2007
Nov 24, 2007
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 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);
Dec 3, 2006
Dec 3, 2006
282
283
284
// 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.
Jan 20, 2008
Jan 20, 2008
285
286
287
288
289
290
291
292
293
294
295
// 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);
// !!! FIXME: document me.
// Caller must free() the returned pointer!
char *MojoPlatform_osVersion(void);
Dec 3, 2006
Dec 3, 2006
296
Jan 20, 2008
Jan 20, 2008
297
298
299
300
301
302
303
304
305
// !!! FIXME: document me.
uint64 MojoPlatform_getuid(void);
// !!! FIXME: document me.
uint64 MojoPlatform_geteuid(void);
// !!! FIXME: document me.
uint64 MojoPlatform_getgid(void);
Dec 4, 2006
Dec 4, 2006
306
307
308
309
310
311
312
313
// Basic platform detection.
#if PLATFORM_WINDOWS
#define PLATFORM_NAME "windows"
#elif PLATFORM_MACOSX
#define PLATFORM_NAME "macosx"
#elif PLATFORM_UNIX
#define PLATFORM_NAME "unix"
Dec 13, 2006
Dec 13, 2006
314
315
#elif PLATFORM_BEOS
#define PLATFORM_NAME "beos"
Dec 4, 2006
Dec 4, 2006
316
#else
Dec 9, 2006
Dec 9, 2006
317
#error Unknown platform.
Dec 4, 2006
Dec 4, 2006
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#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
Sep 20, 2007
Sep 20, 2007
334
335
336
337
338
339
340
// Other basic truths...
#if PLATFORM_WINDOWS
#define MOJOPLATFORM_ENDLINE "\r\n"
#else
#define MOJOPLATFORM_ENDLINE "\n"
#endif
341
342
343
344
345
346
#ifdef __cplusplus
}
#endif
#endif
Mar 25, 2006
Mar 25, 2006
347
// end of platform.h ...