1#ifndef MAUSWIN_H
2#define MAUSWIN_H
3
4#include <stdio.h>
5#include <stdint.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_win.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 (((uint32_t)(c).a << 24) | \
52 ((uint32_t)(c).r << 16) | \
53 ((uint32_t)(c).g << 8) | \
54 ((uint32_t)(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} MausEventType;
67
68typedef struct {
69 uint8_t a;
70 uint8_t r;
71 uint8_t g;
72 uint8_t b;
73} MausColor;
74
75typedef struct {
76 MausEventType type;
77
78 struct {
79 uint32_t code; /* raw, backend keycode */
80 MausKey key; /* logical, mapped key */
81 char text; /* translated text from key */
82 int8_t pressed;
83 } key;
84
85 struct {
86 struct {
87 MausMouseButton button;
88 int8_t pressed;
89 } button;
90
91 struct {
92 int32_t x;
93 int32_t y;
94 int32_t dx;
95 int32_t dy;
96 } motion;
97 } mouse;
98
99 struct {
100 uint32_t width;
101 uint32_t height;
102 } resize;
103} MausEvent;
104
105typedef struct {
106 MausBackend backend;
107 uint64_t frame_time_last;
108 char* clipboard;
109
110 uint32_t* fb; /* front frame buffer */
111 uint32_t* bfb; /* back frame buffer */
112 uint32_t stride;
113
114 const char* title;
115 uint32_t width;
116 uint32_t height;
117 int32_t x;
118 int32_t y;
119
120 int8_t key_codes[MAUS_KEYCODE_LAST]; /* physical keys */
121 int8_t key_syms[MAUS_KEY_LAST]; /* logical keys */
122
123 MausCursor cursor;
124 int8_t mouse_buttons[MAUS_MOUSE_BUTTON_LAST];
125} Maus;
126
127/* clear screen with color: `col` */
128void maus_clear(Maus* mw, MausColor col);
129
130/* set text to system clipboard */
131void maus_clipboard_set_text(Maus* mw, const char* text);
132
133/* get text from system clipboard */
134char* maus_clipboard_get_text(Maus* mw);
135
136/* close a Maus. returns 0 on fail
137 TODO also free mw */
138void maus_close(Maus* mw);
139
140/* close a window without the whole Maus. returns 0 on fail */
141int8_t maus_close_window(Maus* mw);
142
143/* create window from Maus. returns 0 on fail */
144int8_t maus_create_window(Maus* mw);
145
146/* log message to output `fd` and die */
147void maus_die(const char* fmt, ...);
148
149/* get time since arbitrary start point (ns) */
150uint64_t maus_get_time_ns(void);
151
152/* initialise and fills the Maus. returns NULL on fail */
153Maus* maus_init(const char* title, int x, int y, int width, int height);
154
155/* log message to output `fd` */
156void maus_log(FILE* fd, const char* fmt, ...);
157
158/* poll for events then fill `ev` with retrieved events.
159 returns 1 if event polled, else 0.
160 note: can burn cpu cycles */
161int8_t maus_event_poll(Maus* mw, MausEvent* ev);
162
163/* poll for events then fill `ev` with retrieved events. returns 1 if event
164 polled, else 0. note, thread goes to sleep until an event arrives */
165void maus_event_wait(Maus* mw, MausEvent* ev);
166
167/* present the pixelbuffer to the screen */
168void maus_present(Maus* mw);
169
170/* resize window to specified width and height. returns 1 on resize success,
171 else 0 */
172int8_t maus_resize(Maus* mw, uint32_t width, uint32_t height);
173
174/* cap framerate to targetted fps */
175void maus_target_fps(Maus* mw, uint32_t fps);
176
177/* change behavior of mouse based on state passed */
178void maus_cur_set_mode(Maus* mw, MausCursorState state);
179
180#endif /* MAUSWIN_H */
181