From ae31efcdae8f00d5041d57983dca9432717e0317 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sat, 18 Apr 2020 13:53:12 +0200
Subject: [PATCH] Separing the mpv event handle

---
 inc/lektor/module/mpv.h |  4 +++
 src/module/module_x11.c | 79 +++--------------------------------------
 src/module/mpv.c        | 77 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 75 deletions(-)

diff --git a/inc/lektor/module/mpv.h b/inc/lektor/module/mpv.h
index 4f97e294..d1414557 100644
--- a/inc/lektor/module/mpv.h
+++ b/inc/lektor/module/mpv.h
@@ -1,6 +1,8 @@
 #pragma once
 
+#include <sqlite3.h>
 #include <mpv/client.h>
+#include <lektor/net.h>
 
 void lmpv_free(mpv_handle **ctx);
 mpv_handle *lmpv_new(unsigned long int wid);
@@ -11,3 +13,5 @@ int lmpv_is_paused(mpv_handle *ctx, int *ret);
 int lmpv_set_volume(mpv_handle *ctx, int vol);
 int lmpv_load_file(mpv_handle *ctx, const char *file);
 int lmpv_toggle_pause(mpv_handle *ctx, int *pause);
+int lmpv_handle(struct lkt_win *win, mpv_handle *ctx, sqlite3 *db, enum mpd_idle_flag *mpd_idle_flag,
+                int *time_pos, int *time_duration);
diff --git a/src/module/module_x11.c b/src/module/module_x11.c
index 1e847035..9fa1e238 100644
--- a/src/module/module_x11.c
+++ b/src/module/module_x11.c
@@ -212,80 +212,6 @@ lx11_handle(struct module_x11_window *win)
  *
  */
 
-static inline bool
-lmpv_handle(struct lkt_win *win, sqlite3 *db, enum mpd_idle_flag *mpd_idle_events)
-{
-    int ao_volume;
-    struct lkt_queue_state state;
-    bool sta = false;
-    struct module_x11_window *win_x11 = win->window;
-    mpv_event *event = NULL;
-    mpv_event_property *prop;
-
-    if (!win || !win_x11 || !win_x11->mpv)
-        goto end;
-
-    if (!database_queue_state(db, &state))
-        goto end;
-
-    for (;;) {
-        event = mpv_wait_event(win_x11->mpv, 0);
-
-        switch (event->event_id) {
-        case MPV_EVENT_PAUSE:
-        case MPV_EVENT_UNPAUSE:
-            database_queue_toggle_pause(db);
-            break;
-
-        case MPV_EVENT_SHUTDOWN:
-            database_queue_stop(db);
-            win->close(win);
-            goto end;
-
-        case MPV_EVENT_NONE:
-            goto end;
-
-        case MPV_EVENT_IDLE:
-            if (state.current > 0 && win_x11->mpv_time_pos > 0)
-                command_next(db, win, mpd_idle_events);
-            break;
-
-        case MPV_EVENT_PROPERTY_CHANGE:
-            prop = (mpv_event_property *) event->data;
-            if (prop->format == MPV_FORMAT_NONE)
-                break;
-            // MPV volume (BUG: The flag is not MPV_FORMAT_NONE only at the end of the song...) //
-            if (!strcmp(prop->name, "ao-volume")
-                && prop->format == MPV_FORMAT_INT64) {
-                ao_volume = *(int *) prop->data;
-                database_config_queue(db, "volume", ao_volume);
-            }
-            // File duration //
-            if (!strcmp(prop->name, "duration")
-                && prop->format == MPV_FORMAT_INT64) {
-                win_x11->mpv_duration = *(int *) prop->data;
-                database_config_queue(db, "duration", *(int *) prop->data);
-            }
-            if (!strcmp(prop->name, "time-pos")
-                && prop->format == MPV_FORMAT_INT64) {
-                win_x11->mpv_time_pos = *(int *) prop->data;
-                database_config_queue(db, "elapsed", *(int *) prop->data);
-            }
-            // Pause state //
-            if (!strcmp(prop->name, "pause")
-                && prop->format == MPV_FORMAT_FLAG)
-                database_queue_set_paused(db, *(bool *) prop->data);
-            break;
-        default:
-            fprintf(stderr, " ! Unhandled mpv event: %s\n", mpv_event_name(event->event_id));
-            break;
-        }
-    }
-
-    sta = true;
-end:
-    return sta;
-}
 
 /*
  *
@@ -424,5 +350,8 @@ module_x11_handle_events(struct lkt_win *const win, sqlite3 *db, enum mpd_idle_f
 {
     if (win == NULL || win->window == NULL)
         return false;
-    return lx11_handle(win->window) && lmpv_handle(win, db, mpd_idle_events);
+    struct module_x11_window *xwin = win->window;
+    return lx11_handle(xwin) &&
+           lmpv_handle(win, xwin->mpv, db, mpd_idle_events, (int *) &xwin->mpv_time_pos,
+                       (int *) &xwin->mpv_duration);
 }
diff --git a/src/module/mpv.c b/src/module/mpv.c
index 62cc17be..e1acfe7b 100644
--- a/src/module/mpv.c
+++ b/src/module/mpv.c
@@ -1,7 +1,10 @@
 #include <lektor/module/mpv.h>
+#include <lektor/commands.h>
+#include <lektor/database.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <sqlite3.h>
 
 void
 lmpv_free(mpv_handle **ctx)
@@ -196,3 +199,77 @@ lmpv_toggle_pause(mpv_handle *ctx, int *pause)
     }
     return lmpv_is_paused(ctx, pause);
 }
+
+int
+lmpv_handle(struct lkt_win *win, mpv_handle *ctx, sqlite3 *db, enum mpd_idle_flag *mpd_idle_events,
+            int *time_pos, int *time_duration)
+{
+    int ao_volume;
+    struct lkt_queue_state state;
+    bool sta = false;
+    mpv_event *event = NULL;
+    mpv_event_property *prop;
+
+    if (!ctx || !win)
+        goto end;
+
+    if (!database_queue_state(db, &state))
+        goto end;
+
+loop:
+    event = mpv_wait_event(ctx, 0);
+
+    switch (event->event_id) {
+    case MPV_EVENT_PAUSE:
+    case MPV_EVENT_UNPAUSE:
+        database_queue_toggle_pause(db);
+        break;
+
+    case MPV_EVENT_SHUTDOWN:
+        database_queue_stop(db);
+        win->close(win);
+        goto end;
+
+    case MPV_EVENT_NONE:
+        goto end;
+
+    case MPV_EVENT_IDLE:
+        if (state.current > 0 && *time_pos > 0)
+            command_next(db, win, mpd_idle_events);
+        break;
+
+    case MPV_EVENT_PROPERTY_CHANGE:
+        prop = (mpv_event_property *) event->data;
+        if (prop->format == MPV_FORMAT_NONE)
+            break;
+        // MPV volume (BUG: The flag is not MPV_FORMAT_NONE only at the end of the song...) //
+        if (!strcmp(prop->name, "ao-volume")
+            && prop->format == MPV_FORMAT_INT64) {
+            ao_volume = *(int *) prop->data;
+            database_config_queue(db, "volume", ao_volume);
+        }
+        // File duration //
+        if (!strcmp(prop->name, "duration")
+            && prop->format == MPV_FORMAT_INT64) {
+            *time_duration = *(int *) prop->data;
+            database_config_queue(db, "duration", *(int *) prop->data);
+        }
+        if (!strcmp(prop->name, "time-pos")
+            && prop->format == MPV_FORMAT_INT64) {
+            *time_pos = *(int *) prop->data;
+            database_config_queue(db, "elapsed", *(int *) prop->data);
+        }
+        // Pause state //
+        if (!strcmp(prop->name, "pause")
+            && prop->format == MPV_FORMAT_FLAG)
+            database_queue_set_paused(db, *(bool *) prop->data);
+        break;
+    default:
+        fprintf(stderr, " ! Unhandled mpv event: %s\n", mpv_event_name(event->event_id));
+        break;
+    }
+    goto loop; /* A loop without indentation. */
+    sta = true;
+end:
+    return sta;
+}
-- 
GitLab