master uint/maus / include / maus.h
  1#ifndef MAUS_H
  2#define MAUS_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_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	(((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	MAUS_EV_LAST
 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	struct {
 80		uint32_t code; /* raw, backend keycode */
 81		MausKey  key;  /* logical, mapped key */
 82		char     text; /* translated text from key */
 83		int8_t     pressed;
 84	} key;
 85
 86	struct {
 87		struct {
 88			MausMouseButton button;
 89			int8_t            pressed;
 90		} button;
 91
 92		struct {
 93			int32_t x;
 94			int32_t y;
 95			int32_t dx;
 96			int32_t dy;
 97		} motion;
 98	} mouse;
 99
100	struct {
101		uint32_t width;
102		uint32_t height;
103	} resize;
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	int8_t key_codes[MAUS_KEYCODE_LAST]; /* physical keys */
122	int8_t key_syms[MAUS_KEY_LAST];      /* logical keys */
123
124	MausCursor cursor;
125	int8_t       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 */
142int8_t maus_close_window(Maus* mw);
143
144/* create window from Maus. returns 0 on fail */
145int8_t 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) */
151uint64_t 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 */
162int8_t 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 */
173int8_t maus_resize(Maus* mw, uint32_t width, uint32_t height);
174
175/* sleep for `ms` milliseconds */
176void maus_sleep(uint32_t ms);
177
178/* cap framerate to targetted fps */
179void maus_target_fps(Maus* mw, uint32_t 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