1#ifndef MAUS_H
2#define MAUS_H
3
4#include <stdio.h>
5#include "uletypes.h"
6
7#include "maus_input.h"
8
9/* auto selection */
10#if (!defined(BACKEND_WIN) && !defined(BACKEND_MAC) && \
11 !defined(BACKEND_X11) && !defined(BACKEND_WAY)) || \
12 defined(BACKEND_AUTO)
13
14#ifdef BACKEND_AUTO
15#define MAUS_WARN_BACKEND_AUTO_SEL 0
16#else
17#define MAUS_WARN_BACKEND_AUTO_SEL 1
18#endif /* BACKEND_AUTO */
19
20 #if defined(_WIN32)
21 #define BACKEND_WIN
22
23 #elif defined(__APPLE__)
24 #define BACKEND_MAC
25
26 #else /* default to X */
27 #define BACKEND_X11
28
29 #endif
30#else
31#define MAUS_WARN_BACKEND_AUTO_SEL 0
32#endif /* auto selection */
33
34#if defined(BACKEND_X11)
35 #include "maus_x11.h"
36
37#elif defined(BACKEND_WAY)
38 #include "maus_wayland.h"
39
40#elif defined(BACKEND_WIN)
41 #include "maus_windows.h"
42
43#elif defined(BACKEND_MAC)
44 /* ... */
45#endif
46
47#define MAUS_KEYCODE_LAST (256)
48#define MAUS_COL_ARGB(a, r, g, b) (MausColor){a, r, g, b}
49#define MAUS_COL_RGBA(r, g, b, a) (MausColor){a, r, g, b}
50#define MAUS_UNPACK_COL(c) \
51 (((ule_u32)(c).a << 24) | \
52 ((ule_u32)(c).r << 16) | \
53 ((ule_u32)(c).g << 8) | \
54 ((ule_u32)(c).b))
55
56#define MAUS_PIXEL_AT(mw, x, y) ((mw)->bfb[(y) * (mw)->stride + (x)])
57
58typedef enum {
59 MAUS_EV_NONE,
60 MAUS_EV_CLOSE,
61 MAUS_EV_KEY,
62 MAUS_EV_MOUSE_BUTTON,
63 MAUS_EV_MOUSE_MOTION,
64 MAUS_EV_RESIZE,
65 MAUS_EV_REDRAW, /* TODO: MAUS_EV_REDRAW for windows */
66 MAUS_EV_LAST
67} MausEventType;
68
69typedef struct {
70 ule_u8 a;
71 ule_u8 r;
72 ule_u8 g;
73 ule_u8 b;
74} MausColor;
75
76typedef struct {
77 MausEventType type;
78
79 struct {
80 ule_u32 code; /* raw, backend keycode */
81 MausKey key; /* logical, mapped key */
82 char text; /* translated text from key */
83 ule_i8 pressed;
84 } key;
85
86 struct {
87 struct {
88 MausMouseButton button;
89 ule_i8 pressed;
90 } button;
91
92 struct {
93 ule_i32 x;
94 ule_i32 y;
95 ule_i32 dx;
96 ule_i32 dy;
97 } motion;
98 } mouse;
99
100 struct {
101 ule_u32 width;
102 ule_u32 height;
103 } resize;
104} MausEvent;
105
106typedef struct {
107 MausBackend backend;
108 ule_u64 frame_time_last;
109 char* clipboard;
110
111 ule_u32* fb; /* front frame buffer */
112 ule_u32* bfb; /* back frame buffer */
113 ule_u32 stride;
114
115 const char* title;
116 ule_u32 width;
117 ule_u32 height;
118 ule_i32 x;
119 ule_i32 y;
120
121 ule_i8 key_codes[MAUS_KEYCODE_LAST]; /* physical keys */
122 ule_i8 key_syms[MAUS_KEY_LAST]; /* logical keys */
123
124 MausCursor cursor;
125 ule_i8 mouse_buttons[MAUS_MOUSE_BUTTON_LAST];
126} Maus;
127
128/* clear screen with color: `col` */
129void maus_clear(Maus* mw, MausColor col);
130
131/* set text to system clipboard */
132void maus_clipboard_set_text(Maus* mw, const char* text);
133
134/* get text from system clipboard */
135char* maus_clipboard_get_text(Maus* mw);
136
137/* close a Maus. returns 0 on fail
138 TODO also free mw */
139void maus_close(Maus* mw);
140
141/* close a window without the whole Maus. returns 0 on fail */
142ule_i8 maus_close_window(Maus* mw);
143
144/* create window from Maus. returns 0 on fail */
145ule_i8 maus_create_window(Maus* mw);
146
147/* log message to output `fd` and die */
148void maus_die(const char* fmt, ...);
149
150/* get time since arbitrary start point (ns) */
151ule_u64 maus_get_time_ns(void);
152
153/* initialise and fills the Maus. returns NULL on fail */
154Maus* maus_init(const char* title, int x, int y, int width, int height);
155
156/* log message to output `fd` */
157void maus_log(FILE* fd, const char* fmt, ...);
158
159/* poll for events then fill `ev` with retrieved events.
160 returns 1 if event polled, else 0.
161 note: can burn cpu cycles */
162ule_i8 maus_event_poll(Maus* mw, MausEvent* ev);
163
164/* poll for events then fill `ev` with retrieved events. returns 1 if event
165 polled, else 0. note, thread goes to sleep until an event arrives */
166void maus_event_wait(Maus* mw, MausEvent* ev);
167
168/* present the pixelbuffer to the screen */
169void maus_present(Maus* mw);
170
171/* resize window to specified width and height. returns 1 on resize success,
172 else 0 */
173ule_i8 maus_resize(Maus* mw, ule_u32 width, ule_u32 height);
174
175/* sleep for `ms` milliseconds */
176void maus_sleep(ule_u32 ms);
177
178/* cap framerate to targetted fps */
179void maus_target_fps(Maus* mw, ule_u32 fps);
180
181/* change behavior of mouse based on state passed */
182void maus_cur_set_mode(Maus* mw, MausCursorState state);
183
184#endif /* MAUS */
185