1#ifndef MAUSWIN_H
2#define MAUSWIN_H
3
4#include <stdbool.h>
5#include <stdio.h>
6#include <stdint.h>
7
8#include "maus_input.h"
9
10/* auto selection */
11#if (!defined(BACKEND_WIN) && !defined(BACKEND_MAC) && \
12 !defined(BACKEND_X11) && !defined(BACKEND_WAY)) || \
13 defined(BACKEND_AUTO)
14
15#ifdef BACKEND_AUTO
16#define MAUS_WARN_BACKEND_AUTO_SEL 0
17#else
18#define MAUS_WARN_BACKEND_AUTO_SEL 1
19#endif /* BACKEND_AUTO */
20
21 #if defined(_WIN32)
22 #define BACKEND_WIN
23
24 #elif defined(__APPLE__)
25 #define BACKEND_MAC
26
27 #else /* default to X */
28 #define BACKEND_X11
29
30 #endif
31#else
32#define MAUS_WARN_BACKEND_AUTO_SEL 0
33#endif /* auto selection */
34
35#if defined(BACKEND_X11)
36 #include "maus_x11.h"
37
38#elif defined(BACKEND_WAY)
39 /* ... */
40
41#elif defined(BACKEND_WIN)
42 #include "maus_win.h"
43
44#elif defined(BACKEND_MAC)
45 /* ... */
46#endif
47
48#define MAUS_KEYCODE_LAST (256)
49#define MAUS_COL_ARGB(a, r, g, b) (MausColor){a, r, g, b}
50#define MAUS_COL_RGBA(r, g, b, a) (MausColor){a, r, g, b}
51#define MAUS_UNPACK_COL(c) \
52 (((uint32_t)(c).a << 24) | \
53 ((uint32_t)(c).r << 16) | \
54 ((uint32_t)(c).g << 8) | \
55 ((uint32_t)(c).b))
56/* ... */
57#define MAUS_PIXEL_AT(mw, x, y) ((mw)->bfb[(y) * (mw)->stride + (x)])
58
59typedef enum {
60 MAUS_EV_NONE,
61 MAUS_EV_CLOSE,
62 MAUS_EV_KEY,
63 MAUS_EV_MOUSE_BUTTON,
64 MAUS_EV_MOUSE_MOTION,
65 MAUS_EV_RESIZE,
66 MAUS_EV_REDRAW, /* TODO: MAUS_EV_REDRAW for windows */
67} MausEventType;
68
69typedef struct {
70 uint8_t a;
71 uint8_t r;
72 uint8_t g;
73 uint8_t b;
74} MausColor;
75
76typedef struct {
77 MausEventType type;
78
79 union {
80 struct {
81 uint32_t code; /* raw, backend keycode */
82 MausKey key; /* logical, mapped key */
83 char text; /* translated text from key */
84 bool pressed;
85 } key;
86
87 struct {
88 struct {
89 MausMouseButton button;
90 bool pressed;
91 } button;
92
93 struct {
94 int32_t x;
95 int32_t y;
96 } motion;
97 } mouse;
98
99 struct {
100 uint32_t width;
101 uint32_t height;
102 } resize;
103 };
104} MausEvent;
105
106typedef struct {
107 MausBackend backend;
108 uint64_t frame_time_last;
109 char* clipboard;
110
111 uint32_t* fb; /* front frame buffer */
112 uint32_t* bfb; /* back frame buffer */
113 uint32_t stride;
114
115 const char* title;
116 uint32_t width;
117 uint32_t height;
118 int32_t x;
119 int32_t y;
120
121 bool key_codes[MAUS_KEYCODE_LAST]; /* physical keys */
122 bool key_syms[MAUS_KEY_LAST]; /* logical keys */
123
124 MausCursor cursor;
125 bool 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 false on fail */
138void maus_close(Maus* mw);
139
140/* close a window without the whole Maus. returns false on fail */
141bool maus_close_window(Maus* mw);
142
143/* create window from Maus. returns false on fail */
144bool 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 true if event polled, else false.
160 note: can burn cpu cycles */
161bool maus_event_poll(Maus* mw, MausEvent* ev);
162
163/* poll for events then fill `ev` with retrieved events. returns true if event
164 polled, else false. 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 true on resize success,
171 else false */
172bool 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