aboutsummaryrefslogtreecommitdiffstats
path: root/src/platform_posix.c
blob: 7c2fb6cdb6d22ac04d8d6984b6502dff5a9d4e13 (plain)
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
#define _DEFAULT_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "app.h"

//
// PlatformAPI Implementation.
//

size_t
platform_read_file(const char *path, char *memory) {
    size_t file_size = 0;
    FILE *fp = fopen(path, "rb");
    if (!fp) {
        return 0;
    }
    fseek(fp, 0, SEEK_END);
    file_size = ftell(fp);
    rewind(fp);
    fread(memory, 1, file_size, fp);
    fclose(fp);
    memory[file_size] = '\0';
    return file_size;
}

void
platform_log(const char *format, ...) {
    // Print date.
    time_t raw_time = time(NULL);
    struct tm *tm = localtime(&raw_time);
    char date[64];
    strftime(date, sizeof(date), "%Y-%M-%d | %H:%M:%S", tm);
    printf("%s | ", date);

    // Print message.
    va_list args;
    va_start(args, format);
    vprintf(format, args);

    printf("\n");
    va_end(args);
}

void
platform_sleep(size_t microseconds) {
    usleep(microseconds);
}

#if defined(LIB_NAME) && defined(LIB_DIR)

//
// Dynamic linking with hot code reload.
//

#include <dlfcn.h>
#include <sys/stat.h>
#include <sys/types.h>

static ino_t lib_id;
static void * lib_handle;

static const char *lib_path = LIB_DIR "/" LIB_NAME;

static bool
load_lib(AppAPI *api) {
    struct stat st;
    if (stat(lib_path, &st) != 0) {
        return false;
    }

    if (lib_id != st.st_ino) {
        void *handle = dlopen(lib_path, RTLD_NOW);
        if (!handle) {
            lib_handle = NULL;
            lib_id = 0;
            return false;
        }
        lib_handle = handle;
        lib_id = st.st_ino;

        const AppAPI *app_api = dlsym(lib_handle, "APP_API");
        if (app_api == NULL) {
            dlclose(lib_handle);
            lib_handle = NULL;
            lib_id = 0;
            return false;
        }
        *api = *app_api;
    }

    return true;
}

static bool
_app_reload(AppAPI *api, AppState *state, PlatformAPI platform) {
    struct stat st;
    if (stat(lib_path, &st) == 0 && lib_id != st.st_ino) {
        if (lib_handle) {
            api->unload(state, platform);
            dlclose(lib_handle);
        }

        if (!load_lib(api)) {
            return false;
        }
        api->reload(state, platform);
    }

    return true;
}

static bool
_app_init(AppAPI *api, AppState *state, PlatformAPI platform) {
    if (!load_lib(api)) {
        fprintf(stderr, "error: can't open app library file: %s\n", lib_path);
        return false;
    }
    api->init(state, platform);
    return true;
}

static void
_app_destroy(AppAPI *api, AppState *state, PlatformAPI platform) {
    api->destroy(state, platform);
    if (lib_handle) {
        dlclose(lib_handle);
        lib_handle = NULL;
        lib_id = 0;
    }
}

#else

//
// Static linking of app code.
//

extern const AppAPI APP_API;

static bool
_app_reload() {
    return true;
}

static bool
_app_init(AppAPI *api, AppState *state, PlatformAPI platform) {
    *api = APP_API;
    api->init(state, platform);
    return true;
}

static void
_app_destroy(AppAPI *api, AppState *state, PlatformAPI platform) {
    api->destroy(state, platform);
}

#endif

const PlatformAPI PLATFORM_API = {
    .read_file = platform_read_file,
    .malloc = malloc,
    .free = free,
    .calloc = calloc,
    .realloc = realloc,
    .log = platform_log,
    .sleep = platform_sleep,
};