From eb4d8df5a6e96d18c38e510a334306b3a76c6fbb Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 15 Apr 2020 21:02:55 +0200
Subject: [PATCH] WIP

---
 inc/lektor/module/mpv.h  |  2 ++
 src/module/module_sdl2.c | 77 +++++++++++++++++++++++++++++++++++++++-
 src/module/mpv.c         |  2 +-
 3 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/inc/lektor/module/mpv.h b/inc/lektor/module/mpv.h
index 1626d47f..a31a373b 100644
--- a/inc/lektor/module/mpv.h
+++ b/inc/lektor/module/mpv.h
@@ -4,3 +4,5 @@
 
 void lmpv_free(mpv_handle **ctx);
 mpv_handle *lmpv_new(unsigned long int wid);
+int lmpv_observe_properties(mpv_handle *ctx);
+mpv_handle *lmpv_prepare(void);
diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index cdda96b2..40b620eb 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -20,6 +20,7 @@ struct module_sdl2_window {
 
     /* Mpv related */
     mpv_handle *mpv;
+    mpv_render_context *mpv_gl;
     volatile int mpv_time_pos;  // Don't write it in the database       //
     volatile int mpv_duration;  // Because don't need to be persistent  //
 };
@@ -265,6 +266,7 @@ module_sdl2_new(struct lkt_win *const win)
         return false;
 
     struct module_sdl2_window *sdl2 = win->window;
+    int status;
 
     if (sdl2 == NULL) {
         sdl2 = calloc(1, sizeof(struct module_sdl2_window));
@@ -276,18 +278,91 @@ module_sdl2_new(struct lkt_win *const win)
         }
 
         /* CREATE THE SDL2 WINDOW TODO */
+        SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "no"); /* It seems that sdl sucks. */
+        if (SDL_Init(SDL_INIT_VIDEO) < 0)
+            goto error;
+
+        sdl2->window = SDL_CreateWindow("lektord", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+                                        WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
+        if (!sdl2->window)
+            goto error;
+
+        sdl2->glcontext = SDL_GL_CreateContext(sdl2->window);
+        if (!sdl2->glcontext)
+            goto error;
+
+        /* Init mpv here */
+        sdl2->mpv = lmpv_prepare();
+
+        if ((status = mpv_initialize(sdl2->mpv)) < 0) {
+            fprintf(stderr, " ! lmpv_new: failed to initialize mpv: %s\n",
+                    mpv_error_string(status));
+            goto error;
+        }
+
+        if (!lmpv_observe_properties(sdl2->mpv)) {
+            fprintf(stderr, " * lmpv_new: failed to observe properties\n");
+            goto error;
+        }
+
+        /* Init opengl with mpv and sdl2 */
+
+        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,
+                }
+            },
+            {
+                MPV_RENDER_PARAM_ADVANCED_CONTROL, &(int)
+                {
+                    1
+                }
+            },
+            {0}
+        };
+
+        if (mpv_render_context_create(&sdl2->mpv_gl, sdl2->mpv, params) < 0)
+            goto error;
 
         fprintf(stderr, " . module_sdl2_new: successfully created the X11 window\n");
+
+        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)
+            goto error;
+
+        mpv_set_wakeup_callback(sdl2->mpv, on_mpv_events, NULL);
+        mpv_render_context_set_update_callback(sdl2->mpv_gl, on_mpv_render_update, NULL);
     }
 
     if (sdl2->mpv == NULL) {
-        sdl2->mpv = lmpv_new(0);
+        sdl2->mpv = lmpv_prepare();
+
+        if ((status = mpv_initialize(sdl2->mpv)) < 0) {
+            fprintf(stderr, " ! lmpv_new: failed to initialize mpv: %s\n",
+                    mpv_error_string(status));
+            goto error;
+        }
+
+        if (!lmpv_observe_properties(sdl2->mpv)) {
+            fprintf(stderr, " * lmpv_new: failed to observe properties\n");
+            goto error;
+        }
+
         if (sdl2->mpv == NULL)
             return false;
     }
 
+    if (sdl2->mpv_gl == NULL) {
+    }
+
     win->window = sdl2;
     return true;
+error:
+    return false;
 }
 
 void
diff --git a/src/module/mpv.c b/src/module/mpv.c
index b81c1c47..2b172bd5 100644
--- a/src/module/mpv.c
+++ b/src/module/mpv.c
@@ -16,7 +16,7 @@ lmpv_free(mpv_handle **ctx)
     fprintf(stderr, " * lmpv_free: mpv context destroyed\n");
 }
 
-void *
+mpv_handle *
 lmpv_prepare(void)
 {
     mpv_handle *ctx = mpv_create();
-- 
GitLab