From f94bad462d864b90d277c88a0491b320c18ed15a Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 20 Apr 2020 16:28:19 +0200
Subject: [PATCH] A working state, must clean some things up

---
 src/module/module_sdl2.c | 167 +++++++++++++++++++--------------------
 1 file changed, 81 insertions(+), 86 deletions(-)

diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index 95a9dd5d..724a6914 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -41,6 +41,60 @@ struct module_sdl2_window {
     volatile int is_sdl_init;
 };
 
+/* Private functions. */
+
+static inline void *
+get_proc_address_mpv(void *fn_ctx, const char *name)
+{
+    (void) fn_ctx;
+    return SDL_GL_GetProcAddress(name);
+}
+
+static inline void
+on_mpv_events(void *ctx)
+{
+    (void) ctx;
+    SDL_Event event = { .type = wakeup_on_mpv_events };
+    SDL_PushEvent(&event);
+}
+
+static inline void
+on_mpv_render_update(void *ctx)
+{
+    (void) ctx;
+    SDL_Event event = { .type = wakeup_on_mpv_render_update };
+    SDL_PushEvent(&event);
+}
+
+static inline bool
+init_mpv__(mpv_handle **ctx)
+{
+    *ctx = lmpv_prepare();
+    int status;
+    RETURN_IF((status = mpv_initialize(*ctx)) < 0, mpv_error_string(status), 1);
+    RETURN_UNLESS(lmpv_observe_properties(*ctx), "Observe properties failed", 1);
+    return 0;
+}
+
+static inline bool
+init_mpv_gl__(mpv_handle *mpv, mpv_render_context **mpv_gl, mpv_render_param *params)
+{
+    int status;
+    RETURN_IF((status = mpv_render_context_create(mpv_gl, mpv, params)) < 0,
+              mpv_error_string(status), 1);
+
+    wakeup_on_mpv_render_update = SDL_RegisterEvents(1);
+    wakeup_on_mpv_events = SDL_RegisterEvents(1);
+    if (wakeup_on_mpv_render_update == (Uint32) - 1 ||
+        wakeup_on_mpv_events == (Uint32) - 1) {
+        fprintf(stderr, " . init_mpv_gl__: Failed to register events\n");
+        return 1;
+    }
+
+    mpv_set_wakeup_callback(mpv, on_mpv_events, NULL);
+    mpv_render_context_set_update_callback(*mpv_gl, on_mpv_render_update, NULL);
+    return 0;
+}
 /* Thread related functions */
 
 static void *
@@ -66,6 +120,33 @@ sdl_thread__(struct lkt_thread_arg *arg)
     RETURN_UNLESS(sdl2->glcontext, "Failed to create the SDL context", NULL);
     sdl2->is_sdl_init = 1;
 
+    /* Init mpv here */
+    RETURN_IF(init_mpv__((mpv_handle **) & sdl2->mpv),
+              "Failed to init mpv", false);
+
+    mpv_render_param params[] = {
+        { MPV_RENDER_PARAM_API_TYPE, MPV_RENDER_API_TYPE_OPENGL },
+        {
+            MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &(mpv_opengl_init_params)
+            {
+                .get_proc_address = get_proc_address_mpv,
+            }
+        },
+        {
+            // Can't use mpv_command with that thing, should change to mpv_command_async
+            MPV_RENDER_PARAM_ADVANCED_CONTROL, &(int)
+            {
+                1
+            }
+        },
+        {0}
+    };
+
+    /* Init mpv_gl here */
+    RETURN_IF(init_mpv_gl__((mpv_handle *) sdl2->mpv,
+                            (mpv_render_context **) &sdl2->mpv_gl, params),
+              "Failed to init mpv_gl", false);
+
     while (!sdl2->launched)
         sched_yield();
 
@@ -139,31 +220,6 @@ loop:
     goto loop;  /* A loop without indentation. */
 }
 
-/* Private functions. */
-
-static inline void *
-get_proc_address_mpv(void *fn_ctx, const char *name)
-{
-    (void) fn_ctx;
-    return SDL_GL_GetProcAddress(name);
-}
-
-static inline void
-on_mpv_events(void *ctx)
-{
-    (void) ctx;
-    SDL_Event event = { .type = wakeup_on_mpv_events };
-    SDL_PushEvent(&event);
-}
-
-static inline void
-on_mpv_render_update(void *ctx)
-{
-    (void) ctx;
-    SDL_Event event = { .type = wakeup_on_mpv_render_update };
-    SDL_PushEvent(&event);
-}
-
 /* Exported functions */
 
 extern int
@@ -186,40 +242,6 @@ module_set_function(void *arg__, void *handle__)
     return 0;
 }
 
-/* Private init functions */
-
-static inline bool
-init_mpv__(mpv_handle **ctx)
-{
-    *ctx = lmpv_prepare();
-    int status;
-    RETURN_IF((status = mpv_initialize(*ctx)) < 0, mpv_error_string(status), 1);
-    RETURN_UNLESS(lmpv_observe_properties(*ctx), "Observe properties failed", 1);
-    return 0;
-}
-
-static inline bool
-init_mpv_gl__(mpv_handle *mpv, mpv_render_context **mpv_gl, mpv_render_param *params)
-{
-    int status;
-    RETURN_IF((status = mpv_render_context_create(mpv_gl, mpv, params)) < 0,
-              mpv_error_string(status), 1);
-
-    wakeup_on_mpv_render_update = SDL_RegisterEvents(1);
-    wakeup_on_mpv_events = SDL_RegisterEvents(1);
-    if (wakeup_on_mpv_render_update == (Uint32) - 1 ||
-        wakeup_on_mpv_events == (Uint32) - 1) {
-        fprintf(stderr, " . init_mpv_gl__: Failed to register events\n");
-        return 1;
-    }
-
-    mpv_set_wakeup_callback(mpv, on_mpv_events, NULL);
-    mpv_render_context_set_update_callback(*mpv_gl, on_mpv_render_update, NULL);
-    return 0;
-}
-
-/* Exported functions */
-
 bool
 module_sdl2_new(struct lkt_win *const win)
 {
@@ -246,33 +268,6 @@ module_sdl2_new(struct lkt_win *const win)
     while (!((struct module_sdl2_window *) win->window)->is_sdl_init)
         sched_yield();
 
-    /* Init mpv here */
-    RETURN_IF(init_mpv__((mpv_handle **) & ((struct module_sdl2_window *) win->window)->mpv),
-              "Failed to init mpv", false);
-
-    mpv_render_param params[] = {
-        { MPV_RENDER_PARAM_API_TYPE, MPV_RENDER_API_TYPE_OPENGL },
-        {
-            MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &(mpv_opengl_init_params)
-            {
-                .get_proc_address = get_proc_address_mpv,
-            }
-        },
-        {
-            // Can't use mpv_command with that thing, should change to mpv_command_async
-            MPV_RENDER_PARAM_ADVANCED_CONTROL, &(int)
-            {
-                1
-            }
-        },
-        {0}
-    };
-
-    /* Init mpv_gl here */
-    RETURN_IF(init_mpv_gl__((mpv_handle *) ((struct module_sdl2_window *) win->window)->mpv,
-                            (mpv_render_context **) & ((struct module_sdl2_window *) win->window)->mpv_gl, params),
-              "Failed to init mpv_gl", false);
-
     /* Finish */
     SDL_SetWindowTitle((SDL_Window *) ((struct module_sdl2_window *) win->window)->window, "Lektord");
     SDL_DisableScreenSaver();
-- 
GitLab