summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/egl_common.c36
-rw-r--r--src/egl_common.h2
-rw-r--r--src/gl_versions.h6
-rw-r--r--src/poll_common.c6
-rw-r--r--src/window.c4
-rw-r--r--src/window_glfm.c1
-rw-r--r--src/window_glfw.c6
-rw-r--r--src/window_kms.c1
-rw-r--r--src/window_wayland.c55
-rw-r--r--src/window_x11.c33
10 files changed, 97 insertions, 53 deletions
diff --git a/src/egl_common.c b/src/egl_common.c
index b0a38f2..818873f 100644
--- a/src/egl_common.c
+++ b/src/egl_common.c
@@ -30,12 +30,12 @@ bool stl_maybe_load_egl(void)
bool stl_load_egl(struct stl_egl_global *global)
{
- s32 minor, major;
+ EGLint major, minor;
if (!eglInitialize(global->display, &major, &minor)) {
al_log_error("egl", "Failed to initialize.");
return false;
}
- al_log_info("egl", "Initialized EGL version %i.%i.", major, minor);
+ al_log_info("egl", "Initialized EGL version %d.%d.", major, minor);
#ifdef STELA_USE_GLAD
s32 egl_version = gladLoaderLoadEGL(global->display);
@@ -59,45 +59,42 @@ void stl_terminate_egl(struct stl_egl_global *global)
}
// https://gitlab.freedesktop.org/mesa/kmscube/-/blob/master/common.c#L162
-static s32 match_config_to_visual(EGLDisplay display, EGLint visual_id, EGLConfig *configs, s32 count)
+static EGLint match_config_to_visual(EGLDisplay display, EGLint visual_id, EGLConfig *configs, EGLint count)
{
EGLint id;
- for (s32 i = 0; i < count; i++) {
+ for (EGLint i = 0; i < count; i++) {
if (!eglGetConfigAttrib(display, configs[i], EGL_NATIVE_VISUAL_ID, &id)) {
continue;
}
- if (id == visual_id) return i;
+ if (id == visual_id) {
+ return i;
+ }
}
return -1;
}
static bool egl_choose_config(EGLDisplay display, const EGLint *attribs, EGLint visual_id, EGLConfig *config)
{
- EGLint count = 0;
- EGLint matched = 0;
- EGLConfig *configs;
- s32 index = -1;
+ EGLint index = -1;
+ EGLint count = 0;
if (!eglGetConfigs(display, NULL, 0, &count) || count <= 0) {
al_log_error("egl", "No configs to choose from.");
return false;
}
- configs = (EGLConfig *)al_malloc(count * sizeof(*configs));
-
+ EGLint matched = 0;
+ EGLConfig *configs = (EGLConfig *)al_malloc(count * sizeof(EGLConfig));
if (!eglChooseConfig(display, attribs, configs, count, &matched) || !matched) {
al_log_error("egl", "No configs with appropriate attributes.");
goto out;
}
- if (!visual_id) index = 0;
-
- if (index == -1) {
- index = match_config_to_visual(display, visual_id, configs, matched);
+ index = visual_id ? match_config_to_visual(display, visual_id, config, matched) : 0;
+ if (index != -1) {
+ *config = configs[index];
}
- if (index != -1) *config = configs[index];
-
out:
al_free(configs);
@@ -122,7 +119,7 @@ bool stl_init_egl_context(struct stl_egl *egl, struct stl_egl_global *global, EG
EGL_NONE
};
- const EGLint *context_attr = (stl_gl_vers[i].api == STELA_GL_API) ?
+ const EGLint *context_attr = stl_gl_vers[i].api == STELA_GL_API ?
context_attr_gl : context_attr_gles;
eglBindAPI(stl_gl_vers[i].api);
@@ -163,7 +160,6 @@ bool stl_init_egl_context(struct stl_egl *egl, struct stl_egl_global *global, EG
}
egl->current = EGL_NO_CONTEXT;
-
egl->surface = EGL_NO_SURFACE;
return true;
@@ -184,7 +180,7 @@ void stl_egl_release_current(struct stl_egl *egl)
egl->current = EGL_NO_CONTEXT;
}
-void stl_egl_swap_interval(struct stl_egl *egl, s32 interval)
+void stl_egl_swap_interval(struct stl_egl *egl, EGLint interval)
{
eglSwapInterval(egl->display, interval);
}
diff --git a/src/egl_common.h b/src/egl_common.h
index e59aa5c..c836519 100644
--- a/src/egl_common.h
+++ b/src/egl_common.h
@@ -27,7 +27,7 @@ void stl_terminate_egl(struct stl_egl_global *global);
bool stl_init_egl_context(struct stl_egl *egl, struct stl_egl_global *global, EGLint visual_id, EGLConfig *config);
void stl_egl_make_current(struct stl_egl *egl);
void stl_egl_release_current(struct stl_egl *egl);
-void stl_egl_swap_interval(struct stl_egl *egl, s32 interval);
+void stl_egl_swap_interval(struct stl_egl *egl, EGLint interval);
void stl_egl_swap_buffers(struct stl_egl *egl);
bool stl_egl_is_ready(struct stl_egl *egl);
void stl_egl_destroy_surface(struct stl_egl *egl);
diff --git a/src/gl_versions.h b/src/gl_versions.h
index 2f45bbc..0fce4ee 100644
--- a/src/gl_versions.h
+++ b/src/gl_versions.h
@@ -12,7 +12,7 @@
{ STELA_GLES_API, STELA_GLES3_BIT, 3, 2, 320, 0 }, \
{ STELA_GLES_API, STELA_GLES3_BIT, 3, 1, 310, 0 }, \
{ STELA_GLES_API, STELA_GLES3_BIT, 3, 0, 300, 0 }, \
- { STELA_GLES_API, STELA_GLES2_BIT, 2, 0, 100, 0 }, \
+ { STELA_GLES_API, STELA_GLES2_BIT, 2, 0, 100, 0 } \
};
#else
#define PASTE_GL_VERSIONS() static struct { \
@@ -31,7 +31,7 @@
{ STELA_GL_API, STELA_GL_BIT, 3, 1, 140, 0 }, \
{ STELA_GL_API, STELA_GL_BIT, 3, 0, 130, 0 }, \
{ STELA_GL_API, STELA_GL_BIT, 2, 1, 120, 0 }, \
- { STELA_GL_API, STELA_GL_BIT, 2, 0, 110, 0 }, \
+ { STELA_GL_API, STELA_GL_BIT, 2, 0, 110, 0 } \
};
#endif
/*
@@ -55,6 +55,6 @@
{ STELA_GLES_API, STELA_GLES3_BIT, 3, 0, 300, 0 }, \
{ STELA_GLES_API, STELA_GLES2_BIT, 2, 0, 100, 0 }, \
{ STELA_GL_API, STELA_GL_BIT, 2, 1, 120, 0 }, \
- { STELA_GL_API, STELA_GL_BIT, 2, 0, 110, 0 }, \
+ { STELA_GL_API, STELA_GL_BIT, 2, 0, 110, 0 } \
};
*/
diff --git a/src/poll_common.c b/src/poll_common.c
index ebe234d..5e7f38a 100644
--- a/src/poll_common.c
+++ b/src/poll_common.c
@@ -16,11 +16,12 @@ bool stl_poll_common(struct nn_pollfd *fds, bool block)
#else
if (nn_poll_fds(fds, 1, (block) ? -1 : 0) < 0 && errno != EINTR) {
#endif
- al_log_error("poll_common", "poll() failed (%s).", errno, nn_strerror(errno));
+ al_log_error("poll_common", "poll() failed %s (%d).", nn_strerror(errno), errno);
return false;
}
- // This happens consistantly if a program is lagging too much. TODO: Recovery.
+ // This happens consistantly if a program is lagging too much.
+ // TODO: Recovery.
if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { // Fatal error.
al_log_error("poll_common", "poll() returned error in revents (%d).", fds[0].revents);
al_assert(false);
@@ -60,4 +61,3 @@ bool stl_process_events_common(struct nn_pollfd *fds)
#endif
return true;
}
-
diff --git a/src/window.c b/src/window.c
index 7dde823..762adce 100644
--- a/src/window.c
+++ b/src/window.c
@@ -83,6 +83,7 @@ void stl_window_send_key_event(struct stl_window *window, u8 state, u8 button)
if (window->key_immediate_callback) {
window->key_immediate_callback(window->userdata, state, button);
}
+
#ifdef STELA_EVENT_BUFFER
GET_EVENT_CHUNK(STELA_EVENT_KEY);
ptr[1] = state;
@@ -144,6 +145,7 @@ void stl_window_send_should_close_event(struct stl_window *window)
bool stl_window_read_events(struct stl_window *window)
{
u8 op = STELA_EVENT_NONE;
+
if (window->pending_resize) {
s32 width = window->width;
s32 height = window->height;
@@ -157,6 +159,7 @@ bool stl_window_read_events(struct stl_window *window)
window->pending_resize = false;
op = STELA_EVENT_RESIZE;
}
+
size_t size;
u8 *ptr = al_ring_buffer_read_chunk(&window->events, &size);
u8 *bc = ptr;
@@ -223,6 +226,7 @@ bool stl_window_read_events(struct stl_window *window)
}
al_assert((size_t)(bc - ptr) == size);
al_ring_buffer_consume(&window->events, ptr, size);
+
return op != STELA_EVENT_NONE;
}
#endif
diff --git a/src/window_glfm.c b/src/window_glfm.c
index 646b978..74310f7 100644
--- a/src/window_glfm.c
+++ b/src/window_glfm.c
@@ -152,5 +152,4 @@ struct stl_window *stl_window_create(void *context)
#elif defined STELA_API_DX11
#endif
return (struct stl_window *)glfm;
- return NULL;
}
diff --git a/src/window_glfw.c b/src/window_glfw.c
index ee85f81..01b8961 100644
--- a/src/window_glfw.c
+++ b/src/window_glfw.c
@@ -163,8 +163,10 @@ static bool window_glfw_create_window(struct stl_window *window, s32 width, s32
return false;
}
- // This doesn't work right, at least on wayland.
- if (!global.master_window) global.master_window = glfw->window;
+ if (!global.master_window) {
+ // This doesn't work right, at least on wayland.
+ global.master_window = glfw->window;
+ }
#ifdef STELA_API_OPENGL
glfwMakeContextCurrent(glfw->window);
diff --git a/src/window_kms.c b/src/window_kms.c
index 5fd24ce..cc35213 100644
--- a/src/window_kms.c
+++ b/src/window_kms.c
@@ -151,5 +151,4 @@ struct stl_window *stl_window_create(void *context)
#elif defined STELA_API_DX11
#endif
return (struct stl_window *)kms;
- return NULL;
}
diff --git a/src/window_wayland.c b/src/window_wayland.c
index 07a7067..c92549c 100644
--- a/src/window_wayland.c
+++ b/src/window_wayland.c
@@ -133,27 +133,35 @@ bool stl_global_init(bool skip_graphics)
return false;
}
global.display_fd = wl_display_get_fd(global.display);
+
if (!skip_graphics) {
#ifdef STELA_API_OPENGL
- if (!stl_maybe_load_egl()) return false;
+ if (!stl_maybe_load_egl()) {
+ return false;
+ }
global.egl.display = eglGetDisplay((EGLNativeDisplayType)global.display);
if (!global.egl.display) {
al_log_error("wayland", "Failed to get wayland-native EGL display.");
return false;
}
- if (!stl_load_egl(&global.egl)) return false;
+ if (!stl_load_egl(&global.egl)) {
+ return false;
+ }
#endif
} else {
#ifdef STELA_API_OPENGL
global.egl.display = EGL_NO_DISPLAY;
#endif
}
+
+ global.active_surface = NULL;
+
al_array_init(global.monitors);
global.registry = wl_display_get_registry(global.display);
wl_registry_add_listener(global.registry, &registry_listener, NULL);
wl_display_roundtrip(global.display);
wl_display_roundtrip(global.display);
- global.active_surface = NULL;
+
return true;
}
@@ -227,9 +235,13 @@ static void pointer_motion(void *data, struct wl_pointer *pointer,
u32 time, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
struct stl_window_wayland *wl = (struct stl_window_wayland *)data;
- if (wl->surface != global.active_surface) return;
(void)pointer;
(void)time;
+
+ if (wl->surface != global.active_surface) {
+ return;
+ }
+
wl->pointer_x += wl_fixed_to_double(surface_x) * wl->w.scale;
wl->pointer_y += wl_fixed_to_double(surface_y) * wl->w.scale;
}
@@ -240,7 +252,11 @@ static void pointer_button(void *data, struct wl_pointer *pointer,
struct stl_window_wayland *wl = (struct stl_window_wayland *)data;
(void)pointer;
(void)time;
- if (wl->surface != global.active_surface) return;
+
+ if (wl->surface != global.active_surface) {
+ return;
+ }
+
#define MOUSE1 0x110
#define MOUSE2 0x111
#define MOUSE3 0x112
@@ -275,7 +291,11 @@ static void pointer_axis(void *data, struct wl_pointer *pointer,
struct stl_window_wayland *wl = (struct stl_window_wayland *)data;
(void)pointer;
(void)time;
- if (wl->surface != global.active_surface) return;
+
+ if (wl->surface != global.active_surface) {
+ return;
+ }
+
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
wl->scroll_y += wl_fixed_to_double(value);
} else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
@@ -287,12 +307,17 @@ static void pointer_frame(void *data, struct wl_pointer *pointer)
{
struct stl_window_wayland *wl = (struct stl_window_wayland *)data;
(void)pointer;
- if (wl->surface != global.active_surface) return;
+
+ if (wl->surface != global.active_surface) {
+ return;
+ }
+
if (wl->pointer_x || wl->pointer_y) {
stl_window_send_pointer_pos_event(&wl->w, wl->pointer_x, wl->pointer_y);
wl->pointer_x = 0.0;
wl->pointer_y = 0.0;
}
+
if (wl->scroll_y) {
stl_window_send_scroll_event(&wl->w, wl->scroll_y);
wl->scroll_y = 0.0;
@@ -393,7 +418,11 @@ static void keyboard_key(void *data, struct wl_keyboard *keyboard,
(void)serial;
(void)keyboard;
(void)time;
- if (wl->surface != global.active_surface) return;
+
+ if (wl->surface != global.active_surface) {
+ return;
+ }
+
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
stl_window_send_key_event(&wl->w, STELA_BUTTON_PRESSED, key);
} else if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
@@ -694,7 +723,9 @@ static struct stl_monitor_wayland *monitor_from_name(const char *name)
{
for (u32 i = 0; i < global.monitors.size; i++) {
struct stl_monitor_wayland *monitor = (struct stl_monitor_wayland *)al_array_at(global.monitors, i);
- if (strcmp(monitor->m.name, name) == 0) return monitor;
+ if (strcmp(monitor->m.name, name) == 0) {
+ return monitor;
+ }
}
return NULL;
}
@@ -756,9 +787,11 @@ static bool window_wayland_create_window(struct stl_window *window, s32 width, s
{
struct stl_window_wayland *wl = (struct stl_window_wayland *)window;
- al_assert(width >= STELA_MIN_WIDTH && height >= STELA_MIN_HEIGHT);
+ if (!create_context_internal(wl)) {
+ goto err;
+ }
- if (!create_context_internal(wl)) goto err;
+ al_assert(width >= STELA_MIN_WIDTH && height >= STELA_MIN_HEIGHT);
struct stl_monitor_wayland *selected = NULL;
wl->w.width = width;
diff --git a/src/window_x11.c b/src/window_x11.c
index 04a60c2..528cfaf 100644
--- a/src/window_x11.c
+++ b/src/window_x11.c
@@ -22,21 +22,28 @@ void stl_set_output_discovered_callback(void (*output_discovered)(void *, struct
bool stl_global_init(bool skip_graphics)
{
- (void)skip_graphics;
global.x11_d = XOpenDisplay(NULL);
if (!global.x11_d) {
al_log_error("x11", "Couldn't open display.");
return false;
}
global.root = XRootWindow(global.x11_d, XDefaultScreen(global.x11_d));
+
#ifdef STELA_API_OPENGL
- if (!stl_maybe_load_egl()) return false;
- global.egl.display = eglGetDisplay((EGLNativeDisplayType)global.x11_d);
- if (!global.egl.display) {
- al_log_error("x11", "Failed to get x11 EGL display.");
+ if (!skip_graphics) {
+ if (!stl_maybe_load_egl()) {
+ return false;
+ }
+ global.egl.display = eglGetDisplay((EGLNativeDisplayType)global.x11_d);
+ if (!global.egl.display) {
+ al_log_error("x11", "Failed to get x11 EGL display.");
+ }
+ if (!stl_load_egl(&global.egl)) {
+ return false;
+ }
}
- if (!stl_load_egl(&global.egl)) return false;
#endif
+
s32 monitor_count;
XRRMonitorInfo *info = XRRGetMonitors(global.x11_d, global.root, 0, &monitor_count);
al_array_init(global.monitors);
@@ -60,6 +67,7 @@ bool stl_global_init(bool skip_graphics)
}
}
XRRFreeMonitors(info);
+
return true;
}
@@ -82,6 +90,7 @@ void stl_global_close(void)
XCloseDisplay(global.x11_d);
}
+#ifdef STELA_API_OPENGL
static bool x11_egl_init(struct stl_window_x11 *xw)
{
EGLConfig config;
@@ -89,9 +98,7 @@ static bool x11_egl_init(struct stl_window_x11 *xw)
return false;
}
-#ifdef STELA_API_OPENGL
xw->w.get_gl_proc_address = eglGetProcAddress;
-#endif
const EGLint surface_attr[] = {
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
@@ -102,6 +109,7 @@ static bool x11_egl_init(struct stl_window_x11 *xw)
return true;
}
+#endif
static bool window_x11_create_window(struct stl_window *window, s32 width, s32 height,
const char *monitor, const char *name, s32 flags)
@@ -134,8 +142,7 @@ static bool window_x11_create_window(struct stl_window *window, s32 width, s32 h
xw->w.width = gwa.width;
xw->w.height = gwa.height;
- if (flags & STELA_WINDOW_WALLPAPER)
- {
+ if (flags & STELA_WINDOW_WALLPAPER) {
Atom net_wm_type = XInternAtom(global.x11_d, "_NET_WM_WINDOW_TYPE", False);
if (net_wm_type) {
Atom desktop_type = XInternAtom(global.x11_d, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
@@ -158,7 +165,9 @@ static bool window_x11_create_window(struct stl_window *window, s32 width, s32 h
}
}
+#ifdef STELA_API_OPENGL
if (!x11_egl_init(xw)) return false;
+#endif
stl_egl_make_current(&xw->egl);
stl_egl_swap_interval(&xw->egl, (flags & STELA_WINDOW_VSYNC) ? 1 : 0);
@@ -266,7 +275,9 @@ static void window_x11_process_events(struct stl_window *window)
stl_process_events_common(xw->fds);
// It seems for some reason POLLIN doesn't get set even when there's events.
XPending(global.x11_d);
- while (QLength(global.x11_d) > 0) { handle_next_xevent(xw); }
+ while (QLength(global.x11_d) > 0) {
+ handle_next_xevent(xw);
+ }
}
#ifdef STELA_PAUSE