Add debug header

This commit is contained in:
Phireh 2024-12-07 18:13:58 +01:00
commit e4111e7e95
2 changed files with 123 additions and 84 deletions

34
debug.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef DEBUG_H
#define DEBUG_H
// TODO: Maybe add a IME subsystem?
typedef enum {
MODULE_EGL = 1 << 0,
MODULE_KEYBOARD = 1 << 1,
MODULE_MOUSE = 1 << 2,
MODULE_WAYLAND = 1 << 3,
MODULE_ALL = ~0
} console_verbosity_t;
typedef enum {
ERROR_LEVEL_0, // do not print errors
ERROR_LEVEL_ERROR = 1,
ERROR_LEVEL_WARNING = 2,
ERROR_LEVEL_LOG = 3,
ERROR_LEVEL_DEBUG = 4,
} error_level_t;
extern console_verbosity_t console_verbosity;
extern error_level_t error_verbosity;
#define console_err(fmt, ...) if (error_verbosity >= ERROR_LEVEL_ERROR) fprintf(stderr, fmt, ##__VA_ARGS__)
#define console_warn(fmt, ...) if (error_verbosity >= ERROR_LEVEL_WARNING) fprintf(stderr, fmt, ##__VA_ARGS__)
#define console_log(fmt, ...) if (error_verbosity >= ERROR_LEVEL_LOG) fprintf(stdout, fmt, ##__VA_ARGS__)
#define console_debug(fmt, ...) if (error_verbosity >= ERROR_LEVEL_DEBUG) fprintf(stderr, fmt, ##__VA_ARGS__)
#define console_err_ss(subsystem, fmt, ...) if (console_verbosity & subsystem) console_err(fmt, ##__VA_ARGS__)
#define console_warn_ss(subsystem, fmt, ...) if (console_verbosity & subsystem) console_warn(fmt, ##__VA_ARGS__)
#define console_log_ss(subsystem, fmt, ...) if (console_verbosity & subsystem) console_log(fmt, ##__VA_ARGS__)
#define console_debug_ss(subsystem, fmt, ...) if (console_verbosity & subsystem) console_debug(fmt, ##__VA_ARGS__)
#endif

159
wayland.c
View file

@ -21,6 +21,7 @@
// common headers for platform layer and application layer // common headers for platform layer and application layer
#include "input.h" #include "input.h"
#include "arena.h" #include "arena.h"
#include "debug.h"
struct wl_compositor *compositor = NULL; struct wl_compositor *compositor = NULL;
struct wl_registry *registry = NULL; struct wl_registry *registry = NULL;
@ -50,6 +51,8 @@ EGLBoolean errcode = 0;
bool running = true; bool running = true;
input_t user_input = {}; input_t user_input = {};
arena_t main_arena = {}; arena_t main_arena = {};
console_verbosity_t console_verbosity = { MODULE_ALL };
error_level_t error_verbosity = { ERROR_LEVEL_DEBUG };
char *egl_s(int code) char *egl_s(int code)
{ {
@ -74,22 +77,22 @@ char *egl_s(int code)
void registry_handle_global( void *data, struct wl_registry *registry, void registry_handle_global( void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) uint32_t name, const char *interface, uint32_t version)
{ {
fprintf(stdout, "Wayland interface: '%s', version: %d, name: %d\n", interface, version, name); console_log_ss(MODULE_WAYLAND, "Wayland interface: '%s', version: %d, name: %d\n", interface, version, name);
if (!strcmp(interface, wl_compositor_interface.name)) if (!strcmp(interface, wl_compositor_interface.name))
{ {
fprintf(stdout, "Registering compositor...\n"); console_log_ss(MODULE_WAYLAND, "Registering compositor...\n");
compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1); compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
} }
if (!strcmp(interface, xdg_wm_base_interface.name)) if (!strcmp(interface, xdg_wm_base_interface.name))
{ {
fprintf(stdout, "Registering XDG WM base interface...\n"); console_log_ss(MODULE_WAYLAND, "Registering XDG WM base interface...\n");
wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
} }
/* NOTE: Since wl_seat v5, pointer events are grouped into "frames", which do not appear to be useful for non-touch /* NOTE: Since wl_seat v5, pointer events are grouped into "frames", which do not appear to be useful for non-touch
applications and also create more events which are more complicated to dispatch. */ applications and also create more events which are more complicated to dispatch. */
if (!strcmp(interface, wl_seat_interface.name)) if (!strcmp(interface, wl_seat_interface.name))
{ {
fprintf(stdout, "Registering WL seat interface...\n"); console_log_ss(MODULE_WAYLAND, "Registering WL seat interface...\n");
wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 8); wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 8);
} }
@ -101,7 +104,7 @@ void registry_handle_global( void *data, struct wl_registry *registry,
void zwp_text_input_v3_enter_callback(void *data, struct zwp_text_input_v3 *text_input, struct wl_surface *surface) void zwp_text_input_v3_enter_callback(void *data, struct zwp_text_input_v3 *text_input, struct wl_surface *surface)
{ {
fprintf(stdout, "ZWP text input v3 enter callback\n"); console_debug_ss(MODULE_KEYBOARD, "ZWP text input v3 enter callback\n");
zwp_text_input_v3 = text_input; zwp_text_input_v3 = text_input;
zwp_text_input_v3_enable(zwp_text_input_v3); zwp_text_input_v3_enable(zwp_text_input_v3);
zwp_text_input_v3_commit(zwp_text_input_v3); zwp_text_input_v3_commit(zwp_text_input_v3);
@ -109,20 +112,20 @@ void zwp_text_input_v3_enter_callback(void *data, struct zwp_text_input_v3 *text
void zwp_text_input_v3_leave_callback(void *data, struct zwp_text_input_v3 *text_input, struct wl_surface *surface) void zwp_text_input_v3_leave_callback(void *data, struct zwp_text_input_v3 *text_input, struct wl_surface *surface)
{ {
fprintf(stdout, "ZWP text input v3 leave callback\n"); console_debug_ss(MODULE_KEYBOARD, "ZWP text input v3 leave callback\n");
zwp_text_input_v3_disable(zwp_text_input_v3); zwp_text_input_v3_disable(zwp_text_input_v3);
zwp_text_input_v3_commit(zwp_text_input_v3); zwp_text_input_v3_commit(zwp_text_input_v3);
} }
void zwp_text_input_v3_preedit_string_callback(void *data, struct zwp_text_input_v3 *text_input, const char *text, int32_t cursor_begin, int32_t cursor_end) void zwp_text_input_v3_preedit_string_callback(void *data, struct zwp_text_input_v3 *text_input, const char *text, int32_t cursor_begin, int32_t cursor_end)
{ {
fprintf(stdout, "ZWP text input v3 preedit string callback with text %s\n", text); console_debug_ss(MODULE_KEYBOARD, "ZWP text input v3 preedit string callback with text %s\n", text);
user_input.keyboard_state &= KEYBOARD_STATE_IME; user_input.keyboard_state &= KEYBOARD_STATE_IME;
} }
void zwp_text_input_v3_commit_string_callback(void *data, struct zwp_text_input_v3 *text_input, const char *text) void zwp_text_input_v3_commit_string_callback(void *data, struct zwp_text_input_v3 *text_input, const char *text)
{ {
fprintf(stdout, "ZWP text input v3 commit string callback with text %s\n", text); console_debug_ss(MODULE_KEYBOARD, "ZWP text input v3 commit string callback with text %s\n", text);
user_input.keyboard_state &= ~KEYBOARD_STATE_IME; user_input.keyboard_state &= ~KEYBOARD_STATE_IME;
const char *s = text; const char *s = text;
@ -131,17 +134,17 @@ void zwp_text_input_v3_commit_string_callback(void *data, struct zwp_text_input_
user_input.ime_str[i] = *s++; user_input.ime_str[i] = *s++;
user_input.ime_str[i] = '\0'; user_input.ime_str[i] = '\0';
fprintf(stdout, "Copied string %s\n", text); console_debug_ss(MODULE_KEYBOARD, "Copied string %s\n", text);
} }
void zwp_text_input_v3_delete_surrounding_text_callback(void *data, struct zwp_text_input_v3 *text_input, uint32_t before_length, uint32_t after_length) void zwp_text_input_v3_delete_surrounding_text_callback(void *data, struct zwp_text_input_v3 *text_input, uint32_t before_length, uint32_t after_length)
{ {
fprintf(stdout, "ZWP text input v3 delete surrounding text callback\n"); console_debug_ss(MODULE_KEYBOARD, "ZWP text input v3 delete surrounding text callback\n");
} }
void zwp_text_input_v3_done_callback(void *data, struct zwp_text_input_v3 *text_input, uint32_t surface) void zwp_text_input_v3_done_callback(void *data, struct zwp_text_input_v3 *text_input, uint32_t surface)
{ {
fprintf(stdout, "ZWP text input v3 done callback\n"); console_debug_ss(MODULE_KEYBOARD, "ZWP text input v3 done callback\n");
} }
void registry_handle_global_remove(void *data, struct wl_registry *registry, void registry_handle_global_remove(void *data, struct wl_registry *registry,
@ -162,7 +165,7 @@ void xdg_toplevel_configure(void *data, struct xdg_toplevel *t, int32_t width, i
if (!width || !height) if (!width || !height)
return; // compositor is deferring to us return; // compositor is deferring to us
fprintf(stdout, "Trying to resize window to %d W %d H\n", width, height); console_debug_ss(MODULE_WAYLAND, "Trying to resize window to %d W %d H\n", width, height);
wl_egl_window_resize(window, width, height, 0, 0); wl_egl_window_resize(window, width, height, 0, 0);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
@ -176,32 +179,33 @@ void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
/* Basic wayland method to check if our program is responsive still */ /* Basic wayland method to check if our program is responsive still */
void wayland_ping(void *data, struct xdg_wm_base *base, uint32_t serial) void wayland_ping(void *data, struct xdg_wm_base *base, uint32_t serial)
{ {
console_debug_ss(MODULE_WAYLAND, "Responding to WL ping\n");
xdg_wm_base_pong(base, serial); xdg_wm_base_pong(base, serial);
} }
void wl_seat_capabilities_callback(void *data, struct wl_seat *wl_seat, uint capabilities) void wl_seat_capabilities_callback(void *data, struct wl_seat *wl_seat, uint capabilities)
{ {
fprintf(stdout, "Capabilities changed for WL seat\n"); console_debug_ss(MODULE_WAYLAND, "Capabilities changed for WL seat\n");
} }
void keymap_callback(void *data, struct wl_keyboard *kb, uint format, int fd, uint size) void keymap_callback(void *data, struct wl_keyboard *kb, uint format, int fd, uint size)
{ {
// TODO: Check for XKB compatibility and use xkbcommon to extract // TODO: Check for XKB compatibility and use xkbcommon to extract
fprintf(stdout, "KBMAP of size %d detected\n", size); console_debug_ss(MODULE_KEYBOARD, "KBMAP of size %d detected\n", size);
char *mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); char *mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
fprintf(stdout, "XKBMAP desc:\n\t%s\n", mem); console_debug_ss(MODULE_KEYBOARD, "XKBMAP desc:\n\t%s\n", mem);
xkb_keymap = xkb_keymap_new_from_string(xkb_context, mem, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); xkb_keymap = xkb_keymap_new_from_string(xkb_context, mem, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(mem, size); munmap(mem, size);
close(fd); close(fd);
if (!(xkb_state = xkb_state_new(xkb_keymap))) if (!(xkb_state = xkb_state_new(xkb_keymap)))
fprintf(stderr, "Could not initialize XKB state machine\n"); console_debug_ss(MODULE_KEYBOARD, "Could not initialize XKB state machine\n");
} }
void enter_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface, struct wl_array *arr) void enter_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface, struct wl_array *arr)
{ {
fprintf(stdout, "KBMAP with serial %d entered surface\n", serial); console_debug_ss(MODULE_KEYBOARD, "KBMAP with serial %d entered surface\n", serial);
user_input.keyboard_state = KEYBOARD_STATE_FOCUSED; user_input.keyboard_state = KEYBOARD_STATE_FOCUSED;
// NOTE: wl_array is a dynamic array as specified in wayland-util.h // NOTE: wl_array is a dynamic array as specified in wayland-util.h
@ -211,15 +215,17 @@ void enter_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_s
user_input.keys[i] = KEY_STATE_UNPRESSED; user_input.keys[i] = KEY_STATE_UNPRESSED;
wl_array_for_each(pos, arr) { wl_array_for_each(pos, arr) {
// TODO: For some reason, we can get here with a perfectly correct keymap but with a single bad key that libxkbcommon resolves as 65293
xkb_keysym_t sym = xkb_state_key_get_one_sym(xkb_state, (*pos) + 8); xkb_keysym_t sym = xkb_state_key_get_one_sym(xkb_state, (*pos) + 8);
// TODO: Decide if we should also set KEY_STATE_HELD // TODO: Decide if we should also set KEY_STATE_HELD
if (sym < KEYBOARD_KEY_TOTAL)
user_input.keys[sym] = KEY_STATE_PRESSED; user_input.keys[sym] = KEY_STATE_PRESSED;
} }
} }
void leave_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface) void leave_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface)
{ {
fprintf(stdout, "KBMAP with serial %d left surface\n", serial); console_debug_ss(MODULE_KEYBOARD, "KBMAP with serial %d left surface\n", serial);
user_input.keyboard_state = KEYBOARD_STATE_UNFOCUSED; user_input.keyboard_state = KEYBOARD_STATE_UNFOCUSED;
} }
@ -238,52 +244,52 @@ void key_callback(void *data, struct wl_keyboard *kb, uint serial, uint time, ui
key += 8; key += 8;
uint32_t layout = xkb_state_key_get_layout(xkb_state, key); uint32_t layout = xkb_state_key_get_layout(xkb_state, key);
if (layout >= xkb_keymap_num_layouts_for_key(xkb_keymap, key)) if (layout >= xkb_keymap_num_layouts_for_key(xkb_keymap, key))
fprintf(stderr, "Layout %d exceeds number of available key layouts\n", layout); console_debug_ss(MODULE_KEYBOARD, "Layout %d exceeds number of available key layouts\n", layout);
xkb_keysym_t sym = xkb_state_key_get_one_sym(xkb_state, key); xkb_keysym_t sym = xkb_state_key_get_one_sym(xkb_state, key);
char keyname[128]; char keyname[128];
xkb_keysym_get_name(sym, keyname, sizeof(keyname)); xkb_keysym_get_name(sym, keyname, sizeof(keyname));
char keyutf8[128]; char keyutf8[128];
xkb_state_key_get_utf8(xkb_state, key, keyutf8, sizeof(keyutf8)); xkb_state_key_get_utf8(xkb_state, key, keyutf8, sizeof(keyutf8));
fprintf(stdout, "%s key %d layout %d (KEYSYM %s) (UTF8 %s)\n", (state == WL_KEYBOARD_KEY_STATE_PRESSED ? "Pressed" : "Released"), key, layout, keyname, keyutf8); console_debug_ss(MODULE_KEYBOARD, "%s key %d layout %d (KEYSYM %s) (UTF8 %s)\n", (state == WL_KEYBOARD_KEY_STATE_PRESSED ? "Pressed" : "Released"), key, layout, keyname, keyutf8);
// TODO: Test this // TODO: Test this
user_input.keys[key] = (state == WL_KEYBOARD_KEY_STATE_PRESSED ? KEY_STATE_PRESSED : KEY_STATE_RELEASED); user_input.keys[key] = (state == WL_KEYBOARD_KEY_STATE_PRESSED ? KEY_STATE_PRESSED : KEY_STATE_RELEASED);
} }
void modifier_callback(void *data, struct wl_keyboard *kb, uint serial, uint mods_depressed, uint mods_latched, uint mods_locked, uint group) void modifier_callback(void *data, struct wl_keyboard *kb, uint serial, uint mods_depressed, uint mods_latched, uint mods_locked, uint group)
{ {
fprintf(stdout, "Keyboard modifier keys changed\n"); console_debug_ss(MODULE_KEYBOARD, "Keyboard modifier keys changed\n");
xkb_state_update_mask(xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group); xkb_state_update_mask(xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
user_input.modifiers = (mods_depressed | mods_latched | mods_locked); user_input.modifiers = (mods_depressed | mods_latched | mods_locked);
} }
void repeat_info_callback(void *data, struct wl_keyboard *kb, int rate, int delay) void repeat_info_callback(void *data, struct wl_keyboard *kb, int rate, int delay)
{ {
fprintf(stdout, "Keyboard repeat/delay rate changed\n"); console_debug_ss(MODULE_KEYBOARD, "Keyboard repeat/delay rate changed\n");
} }
void pointer_enter_callback(void *data, struct wl_pointer *pointer, uint serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) void pointer_enter_callback(void *data, struct wl_pointer *pointer, uint serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
{ {
//fprintf(stdout, "Pointer entered WL surface on coordinates %d %d\n", x, y); console_debug_ss(MODULE_MOUSE, "Pointer entered WL surface on coordinates %d %d\n", x, y);
user_input.mouse_state = MOUSE_STATE_FOCUSED; user_input.mouse_state = MOUSE_STATE_FOCUSED;
} }
void pointer_leave_callback(void *data, struct wl_pointer *pointer, uint serial, struct wl_surface *surface) void pointer_leave_callback(void *data, struct wl_pointer *pointer, uint serial, struct wl_surface *surface)
{ {
//fprintf(stdout, "Pointer left WL surface\n"); console_debug_ss(MODULE_MOUSE, "Pointer left WL surface\n");
user_input.mouse_state = MOUSE_STATE_UNFOCUSED; user_input.mouse_state = MOUSE_STATE_UNFOCUSED;
} }
void pointer_motion_callback(void *data, struct wl_pointer *pointer, uint time, wl_fixed_t x, wl_fixed_t y) void pointer_motion_callback(void *data, struct wl_pointer *pointer, uint time, wl_fixed_t x, wl_fixed_t y)
{ {
//fprintf(stdout, "Pointer moved to %d %d\n", x, y); console_debug_ss(MODULE_MOUSE, "Pointer moved to %d %d\n", x, y);
user_input.mouse_x = x; user_input.mouse_x = x;
user_input.mouse_y = y; user_input.mouse_y = y;
} }
void pointer_button_callback(void *data, struct wl_pointer *pointer, uint serial, uint time, uint button, uint state) void pointer_button_callback(void *data, struct wl_pointer *pointer, uint serial, uint time, uint button, uint state)
{ {
fprintf(stdout, "Pointer button %d changed state\n", button); console_debug_ss(MODULE_MOUSE, "Pointer button %d changed state\n", button);
mouse_keysym_t idx = 0; mouse_keysym_t idx = 0;
switch (button) switch (button)
{ {
@ -312,42 +318,42 @@ void pointer_button_callback(void *data, struct wl_pointer *pointer, uint serial
void pointer_axis_callback(void *data, struct wl_pointer *pointer, uint time, uint axis, wl_fixed_t value) void pointer_axis_callback(void *data, struct wl_pointer *pointer, uint time, uint axis, wl_fixed_t value)
{ {
//fprintf(stdout, "Pointer scroll on axis %d value %d\n", axis, value); console_debug_ss(MODULE_MOUSE, "Pointer scroll on axis %d value %d\n", axis, value);
} }
/* NOTE: *some* events are logically grouped together. The frame event marks the boundary between groups of them. More info: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_pointer */ /* NOTE: *some* events are logically grouped together. The frame event marks the boundary between groups of them. More info: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_pointer */
void pointer_frame_callback(void *data, struct wl_pointer *frame) void pointer_frame_callback(void *data, struct wl_pointer *frame)
{ {
//fprintf(stdout, "Pointer frame ended\n"); console_debug_ss(MODULE_MOUSE, "Pointer frame ended\n");
} }
/* NOTE: This event is also used to group pointer events together. It specifies info about all events in its frame */ /* NOTE: This event is also used to group pointer events together. It specifies info about all events in its frame */
void pointer_axis_source_callback(void *data, struct wl_pointer *pointer, uint axis_source) void pointer_axis_source_callback(void *data, struct wl_pointer *pointer, uint axis_source)
{ {
//fprintf(stdout, "Pointer axis source: %d\n", axis_source); console_debug_ss(MODULE_MOUSE, "Pointer axis source: %d\n", axis_source);
} }
/* Optional event to implement kinetic scrolling */ /* Optional event to implement kinetic scrolling */
void pointer_axis_stop_callback(void *data, struct wl_pointer *pointer, uint time, uint axis) void pointer_axis_stop_callback(void *data, struct wl_pointer *pointer, uint time, uint axis)
{ {
//fprintf(stdout, "Pointer axis stopped: %d\n", axis); console_debug_ss(MODULE_MOUSE, "Pointer axis stopped: %d\n", axis);
} }
/* Optional event. Carries discrete scroll info after an 'axis' event */ /* Optional event. Carries discrete scroll info after an 'axis' event */
void pointer_axis_discrete_callback(void *data, struct wl_pointer *pointer, uint axis, int discrete) void pointer_axis_discrete_callback(void *data, struct wl_pointer *pointer, uint axis, int discrete)
{ {
//fprintf(stdout, "Pointer axis discrete step: %d\n", discrete); console_debug_ss(MODULE_MOUSE, "Pointer axis discrete step: %d\n", discrete);
} }
void pointer_axis_value120_callback(void *data, struct wl_pointer *pointer, uint axis, int value120) void pointer_axis_value120_callback(void *data, struct wl_pointer *pointer, uint axis, int value120)
{ {
fprintf(stdout, "Pointer axis value120: %d\n", value120); console_debug_ss(MODULE_MOUSE, "Pointer axis value120: %d\n", value120);
} }
void pointer_axis_relative_direction_callback(void *data, struct wl_pointer *pointer, uint axis, uint direction) void pointer_axis_relative_direction_callback(void *data, struct wl_pointer *pointer, uint axis, uint direction)
{ {
fprintf(stdout, "Pointer axis direction: %d\n", direction); console_debug_ss(MODULE_MOUSE, "Pointer axis direction: %d\n", direction);
} }
int main() int main()
@ -500,58 +506,57 @@ int main()
errcode = eglChooseConfig(egl_display, attr_list, configs, config_count * sizeof(EGLConfig), &chosen_config_count); errcode = eglChooseConfig(egl_display, attr_list, configs, config_count * sizeof(EGLConfig), &chosen_config_count);
if (errcode != EGL_TRUE) if (errcode != EGL_TRUE)
fprintf(stderr, "ERR %s: Could not choose EGL config parameters\n", egl_s(errcode)); console_err_ss(MODULE_EGL, "ERR %s: Could not choose EGL config parameters\n", egl_s(errcode));
int chosen_config_id = 0; int chosen_config_id = 0;
EGLConfig *chosen_config = configs[0]; EGLConfig *chosen_config = configs[0];
fprintf(stdout, "Found %d possible configurations:\n", chosen_config_count); console_log_ss(MODULE_EGL, "Found %d possible configurations:\n", chosen_config_count);
for (int i = 0; i < chosen_config_count; ++i) for (int i = 0; i < chosen_config_count; ++i)
{ {
fprintf(stdout, "\tCONFIG %d:\n", i); //fprintf(stdout, "\tCONFIG %d:\n", i);
int attr; int attr;
#define PRINT_ATTR(attr_name) \ #define GET_ATTR(attr_name) \
errcode = eglGetConfigAttrib(egl_display, configs[i], attr_name, &attr); \ errcode = eglGetConfigAttrib(egl_display, configs[i], attr_name, &attr); \
int attr_##attr_name = -1; \ int attr_##attr_name = -1; \
do { \ do { \
if (errcode == EGL_TRUE) { \ if (errcode == EGL_TRUE) { \
fprintf(stdout, "\t\t" #attr_name ": %d (%#0x)\n", attr, attr); \ console_debug_ss(MODULE_EGL, "\t\t" #attr_name ": %d (%#0x)\n", attr, attr); \
attr_##attr_name = attr; \ attr_##attr_name = attr; \
} \ } \
} while (0) } while (0)
GET_ATTR(EGL_ALPHA_SIZE);
PRINT_ATTR(EGL_ALPHA_SIZE); GET_ATTR(EGL_ALPHA_MASK_SIZE);
PRINT_ATTR(EGL_ALPHA_MASK_SIZE); GET_ATTR(EGL_BIND_TO_TEXTURE_RGB);
PRINT_ATTR(EGL_BIND_TO_TEXTURE_RGB); GET_ATTR(EGL_BIND_TO_TEXTURE_RGBA);
PRINT_ATTR(EGL_BIND_TO_TEXTURE_RGBA); GET_ATTR(EGL_BLUE_SIZE);
PRINT_ATTR(EGL_BLUE_SIZE); GET_ATTR(EGL_BUFFER_SIZE);
PRINT_ATTR(EGL_BUFFER_SIZE); GET_ATTR(EGL_COLOR_BUFFER_TYPE);
PRINT_ATTR(EGL_COLOR_BUFFER_TYPE); GET_ATTR(EGL_CONFIG_CAVEAT);
PRINT_ATTR(EGL_CONFIG_CAVEAT); GET_ATTR(EGL_CONFIG_ID);
PRINT_ATTR(EGL_CONFIG_ID); GET_ATTR(EGL_CONFORMANT);
PRINT_ATTR(EGL_CONFORMANT); GET_ATTR(EGL_DEPTH_SIZE);
PRINT_ATTR(EGL_DEPTH_SIZE); GET_ATTR(EGL_GREEN_SIZE);
PRINT_ATTR(EGL_GREEN_SIZE); GET_ATTR(EGL_LEVEL);
PRINT_ATTR(EGL_LEVEL); GET_ATTR(EGL_LUMINANCE_SIZE);
PRINT_ATTR(EGL_LUMINANCE_SIZE); GET_ATTR(EGL_MAX_PBUFFER_WIDTH);
PRINT_ATTR(EGL_MAX_PBUFFER_WIDTH); GET_ATTR(EGL_MAX_PBUFFER_HEIGHT);
PRINT_ATTR(EGL_MAX_PBUFFER_HEIGHT); GET_ATTR(EGL_MAX_PBUFFER_PIXELS);
PRINT_ATTR(EGL_MAX_PBUFFER_PIXELS); GET_ATTR(EGL_MAX_SWAP_INTERVAL);
PRINT_ATTR(EGL_MAX_SWAP_INTERVAL); GET_ATTR(EGL_NATIVE_RENDERABLE);
PRINT_ATTR(EGL_NATIVE_RENDERABLE); GET_ATTR(EGL_NATIVE_VISUAL_ID);
PRINT_ATTR(EGL_NATIVE_VISUAL_ID); GET_ATTR(EGL_NATIVE_VISUAL_TYPE);
PRINT_ATTR(EGL_NATIVE_VISUAL_TYPE); GET_ATTR(EGL_RED_SIZE);
PRINT_ATTR(EGL_RED_SIZE); GET_ATTR(EGL_RENDERABLE_TYPE);
PRINT_ATTR(EGL_RENDERABLE_TYPE); GET_ATTR(EGL_SAMPLE_BUFFERS);
PRINT_ATTR(EGL_SAMPLE_BUFFERS); GET_ATTR(EGL_SAMPLES);
PRINT_ATTR(EGL_SAMPLES); GET_ATTR(EGL_STENCIL_SIZE);
PRINT_ATTR(EGL_STENCIL_SIZE); GET_ATTR(EGL_SURFACE_TYPE);
PRINT_ATTR(EGL_SURFACE_TYPE); GET_ATTR(EGL_TRANSPARENT_TYPE);
PRINT_ATTR(EGL_TRANSPARENT_TYPE); GET_ATTR(EGL_TRANSPARENT_RED_VALUE);
PRINT_ATTR(EGL_TRANSPARENT_RED_VALUE); GET_ATTR(EGL_TRANSPARENT_GREEN_VALUE);
PRINT_ATTR(EGL_TRANSPARENT_GREEN_VALUE); GET_ATTR(EGL_TRANSPARENT_BLUE_VALUE);
PRINT_ATTR(EGL_TRANSPARENT_BLUE_VALUE);
// Try to at least choose an RGBA 32bit config // Try to at least choose an RGBA 32bit config
if (attr_EGL_ALPHA_SIZE == 8 && if (attr_EGL_ALPHA_SIZE == 8 &&
@ -565,10 +570,10 @@ int main()
chosen_config_id = i; chosen_config_id = i;
} }
} }
#undef PRINT_ATTR #undef GET_ATTR
chosen_config = configs[chosen_config_id]; chosen_config = configs[chosen_config_id];
fprintf(stdout, "Chosen config #%d\n", chosen_config_id); console_log_ss(MODULE_EGL, "Chosen config #%d\n", chosen_config_id);
EGLSurface egl_surface = eglCreateWindowSurface(egl_display, chosen_config, (EGLNativeWindowType)window, NULL); EGLSurface egl_surface = eglCreateWindowSurface(egl_display, chosen_config, (EGLNativeWindowType)window, NULL);
switch ((intptr_t)egl_surface) switch ((intptr_t)egl_surface)
@ -581,7 +586,7 @@ int main()
case EGL_BAD_ATTRIBUTE: case EGL_BAD_ATTRIBUTE:
case EGL_BAD_ALLOC: case EGL_BAD_ALLOC:
case EGL_BAD_MATCH: case EGL_BAD_MATCH:
fprintf(stderr, "ERR %s: Could not create EGL window surface\n", egl_s((intptr_t)egl_surface)); console_err_ss(MODULE_EGL, "ERR %s: Could not create EGL window surface\n", egl_s((intptr_t)egl_surface));
break; break;
} }
@ -596,20 +601,20 @@ int main()
/* Change from OpenGL ES to regular OpenGL */ /* Change from OpenGL ES to regular OpenGL */
errcode = eglBindAPI(EGL_OPENGL_API); errcode = eglBindAPI(EGL_OPENGL_API);
if (errcode != EGL_TRUE) if (errcode != EGL_TRUE)
fprintf(stderr, "ERR %s: Could not bind OpenGL API to EGL\n", egl_s(errcode)); console_err_ss(MODULE_EGL, "ERR %s: Could not bind OpenGL API to EGL\n", egl_s(errcode));
// Since we already bound desktop OGL, this is actually making a OGL context instead of an OGL ES one // Since we already bound desktop OGL, this is actually making a OGL context instead of an OGL ES one
EGLContext egl_context = eglCreateContext(egl_display, chosen_config, EGL_NO_CONTEXT, context_attribs); EGLContext egl_context = eglCreateContext(egl_display, chosen_config, EGL_NO_CONTEXT, context_attribs);
errcode = eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context); errcode = eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
if (errcode != EGL_TRUE) if (errcode != EGL_TRUE)
fprintf(stderr, "ERR %s: could not make EGL context current\n", egl_s(errcode)); console_err_ss(MODULE_EGL, "ERR %s: could not make EGL context current\n", egl_s(errcode));
/* NOTE: It would seem that having a non-zero swap interval can hang the Mesa driver forever, according to this SDL GH issue: /* NOTE: It would seem that having a non-zero swap interval can hang the Mesa driver forever, according to this SDL GH issue:
https://github.com/libsdl-org/SDL/issues/4335#issuecomment-829789881 https://github.com/libsdl-org/SDL/issues/4335#issuecomment-829789881
*/ */
if (eglSwapInterval(egl_display, 0) != EGL_TRUE) if (eglSwapInterval(egl_display, 0) != EGL_TRUE)
fprintf(stderr, "Could not set EGL swap interval\n"); console_err_ss(MODULE_EGL, "Could not set EGL swap interval\n");
// necessary before our first eglSwapBuffers // necessary before our first eglSwapBuffers
wl_surface_commit(surface); wl_surface_commit(surface);
@ -619,7 +624,7 @@ int main()
while (running) while (running)
{ {
wl_display_dispatch_pending(display); wl_display_dispatch_pending(display);
//fprintf(stdout, "Frame %d\n", framecount); //console_debug_ss(MODULE_WAYLAND, "Frame %d\n", framecount);
glClearColor(0.5, 0.3, 0.0, 1.0); glClearColor(0.5, 0.3, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glFlush(); glFlush();