aboutsummaryrefslogtreecommitdiff
path: root/src/os/os.h
blob: 3c9de187d3b497b330613ae0f2db3ec8e192fcaf (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
#ifndef OS_H
#define OS_H

#include <basic/arena.h>
#include <basic/basic.h>
#include <openssl/rsa.h>

typedef struct {
    size_t size;
    u8 *p;
} OSMemory;

b32  os_memory_allocate(OSMemory *memory, size_t size);
void os_memory_free(OSMemory *memory);


typedef struct {
    i64 seconds;
    i64 nanoseconds;
} OSTime;

OSTime os_time_get_now(void);


typedef void OSLibrary;
OSLibrary* os_library_open(const char *path);
void       os_library_close(OSLibrary *lib);
void*      os_library_get_proc(OSLibrary *lib, const char *name);


char*   os_file_read_as_string(Arena *arena, char *path, size_t *outlen);


typedef enum {
    OS_EVENT_KEY_PRESS,
    OS_EVENT_KEY_RELEASE,
    OS_EVENT_WINDOW_RESIZE,
    OS_EVENT_WINDOW_DESTROYED,
} OSEventType;

typedef enum {
    OS_KEYCODE_LEFT,
    OS_KEYCODE_RIGHT,
    OS_KEYCODE_UP,
    OS_KEYCODE_DOWN,
} OSEventKeySpecial;

typedef struct {
    b32 is_unicode; // else it's "special", see OSKeySpecial
    u32 code;   // unicode character or some other keyboard keys
} OSEventKeyPress;

typedef struct {
    b32 is_special;
    u32 code;   // unicode character or some other keyboard keys
} OSEventKeyRelease;

typedef struct {
    i32 width;
    i32 height;
} OSEventResize;

typedef struct OSEvent {
    OSEventType type;
    union {
        OSEventKeyPress key_press;
        OSEventKeyRelease key_release;
        OSEventResize resize;
    } ev;
} OSEvent;



typedef struct {
    u8 red_shift;
    u8 green_shift;
    u8 blue_shift;
    u8 alpha_shift;
    i32 width;
    i32 height;
    u32 *pixels;
} OSOffscreenBuffer;

typedef struct OSWindow OSWindow;
OSWindow*          os_window_create(const char *name, i32 width, i32 height);
void               os_window_destroy(OSWindow *window);
b32                os_window_get_event(OSWindow *window, OSEvent *event);
OSOffscreenBuffer* os_window_get_offscreen_buffer(OSWindow *window);
void               os_window_swap_buffers(OSWindow *window, OSOffscreenBuffer *offscreen_buffer);



typedef enum {
    OS_NET_SECURE_STREAM_ERROR,
    OS_NET_SECURE_STREAM_DISCONNECTED,

    OS_NET_SECURE_STREAM_CONNECTED,
    OS_NET_SECURE_STREAM_HANDSHAKING,
} OSNetSecureStreamStatus;

#define OS_NET_SECURE_STREAM_ID_INVALID U32_MAX

void os_net_secure_streams_init(Arena *arena, size_t max_count);

u32  os_net_secure_stream_listen(u16 port, EVP_PKEY *server_rsa_pri);
u32  os_net_secure_stream_accept(u32 listener_id);
u32  os_net_secure_stream_connect(char *address, u16 port, EVP_PKEY *server_rsa_pub);
void os_net_secure_stream_close(u32 id);

OSNetSecureStreamStatus os_net_secure_stream_get_status(u32 id);
i64  os_net_secure_stream_error(u32 id); // 0 if no error, else tbd

i64  os_net_secure_stream_send(u32 id, u8 *buffer, size_t size);
i64  os_net_secure_stream_recv(u32 id, u8 *buffer, size_t size);
int  os_net_secure_stream_get_fd(u32 id);



typedef struct OSSoundPlayer OSSoundPlayer;

typedef struct {
    i32 play_cursor;
    i32 max_sample_count;
    i32 sample_count;
    i32 samples_per_second;
    i16 *samples;
} OSSoundBuffer;

// Todo: maybe change api by replacing get_buffer with play_buffer
OSSoundPlayer* os_sound_player_create(Arena *arena, i32 samples_per_second);
OSSoundBuffer* os_sound_player_get_buffer(OSSoundPlayer *player);
void           os_sound_player_close(OSSoundPlayer *player);



// Note: api unused and in progress

typedef struct {
    void (*fn_worker)(void *data);
    void *data;
} OSWork;

typedef struct OSThreadPool OSThreadPool;
typedef u32 OSThreadId;

OSThreadPool* os_thread_pool_create(Arena *arena, u32 thread_count, u32 work_queue_size);
OSThreadId  os_thread_pool_start(OSThreadPool *pool, OSWork work);
void        os_thread_pool_finish(OSThreadPool *pool, OSThreadId id);



#endif // OS_H