From b09232ad48582c1a3ea089c8caeb84b23e62cda1 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 24 Dec 2021 10:46:49 +0100
Subject: [PATCH] FMT bis + delete deprecated functions

---
 src/module/module_qt_window.hh              |   4 +-
 src/module/qt_window/mainwindow.hh          |   3 +-
 src/module/qt_window/mpvwidget.cc           |  25 +--
 src/module/qt_window/mpvwidget.hh           |  15 +-
 src/module/qt_window/mpvwidget_interface.hh |   2 +-
 src/module/qt_window/qthelper.hh            | 226 ++++++++------------
 utils/scripts/style.bash                    |   4 +-
 7 files changed, 113 insertions(+), 166 deletions(-)

diff --git a/src/module/module_qt_window.hh b/src/module/module_qt_window.hh
index db40722f..98732907 100644
--- a/src/module/module_qt_window.hh
+++ b/src/module/module_qt_window.hh
@@ -7,8 +7,8 @@ class MainWindow;
 #include "thread.h"
 
 struct module_qt_window_s {
-    MainWindow* main_window;
-    MpvWidget* mpv_widget;
+    MainWindow *main_window;
+    MpvWidget *mpv_widget;
 
     /* Thread related */
     struct poller_thread self;
diff --git a/src/module/qt_window/mainwindow.hh b/src/module/qt_window/mainwindow.hh
index d7a3c412..245d7fb6 100644
--- a/src/module/qt_window/mainwindow.hh
+++ b/src/module/qt_window/mainwindow.hh
@@ -6,8 +6,7 @@
 #include "../module_qt_window.hh"
 #include "mpvwidget.hh"
 
-class MainWindow : public QMainWindow
-{
+class MainWindow : public QMainWindow {
     Q_OBJECT
 public:
     explicit MainWindow(struct module_qt_window_s *qt_window, QWidget *parent = nullptr);
diff --git a/src/module/qt_window/mpvwidget.cc b/src/module/qt_window/mpvwidget.cc
index 570896a6..8c695f5e 100644
--- a/src/module/qt_window/mpvwidget.cc
+++ b/src/module/qt_window/mpvwidget.cc
@@ -7,13 +7,13 @@
 #include "qthelper.hh"
 #include "../mpv.h"
 
-static void
+PRIVATE_FUNCTION void
 wakeup(void *ctx)
 {
     QMetaObject::invokeMethod(static_cast<MpvWidget *>(ctx), "on_mpv_events", Qt::QueuedConnection);
 }
 
-static void *
+PRIVATE_FUNCTION void *
 get_proc_address(void *ctx, const char *name)
 {
     Q_UNUSED(ctx);
@@ -44,7 +44,7 @@ MpvWidget::MpvWidget(struct queue *queue, lkt_db *db, QWidget *parent)
         throw std::runtime_error("could not initialize mpv context");
 
     // Request hw decoding, just for testing.
-    mpv::qt::set_option_variant(mpv, "hwdec", "auto");
+    mpv_set_option_string(mpv, "hwdec", "auto");
 
     mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE);
     mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE);
@@ -163,11 +163,9 @@ MpvWidget::handle_mpv_event(mpv_event *event)
     case MPV_EVENT_CHAPTER_CHANGE:
     case MPV_EVENT_PLAYBACK_RESTART:
     case MPV_EVENT_QUEUE_OVERFLOW:
-    case MPV_EVENT_HOOK: {
+    case MPV_EVENT_HOOK:
         break;
     }
-        // Ignore uninteresting or unknown events.
-    }
 }
 
 // Make Qt invoke mpv_render_context_render() to draw a new/updated video frame.
@@ -198,37 +196,32 @@ MpvWidget::on_update(void *ctx)
 }
 
 bool
-MpvWidget::get_elapsed(int *elapsed_sec)
+MpvWidget::get_elapsed(int UNUSED *elapsed_sec)
 {
-    (void)elapsed_sec;
     return true;
 }
 
 bool
-MpvWidget::get_duration(int *dur_sec)
+MpvWidget::get_duration(int UNUSED *dur_sec)
 {
-    (void)dur_sec;
     return true;
 }
 
 bool
-MpvWidget::set_paussed(int paused)
+MpvWidget::set_paussed(int UNUSED paused)
 {
-    (void)paused;
     return true;
 }
 
 bool
-MpvWidget::set_volume(int vol)
+MpvWidget::set_volume(int UNUSED vol)
 {
-    (void)vol;
     return true;
 }
 
 bool
-MpvWidget::set_position(int sec)
+MpvWidget::set_position(int UNUSED sec)
 {
-    (void)sec;
     return true;
 }
 
diff --git a/src/module/qt_window/mpvwidget.hh b/src/module/qt_window/mpvwidget.hh
index 1ac917de..f38d8b6d 100644
--- a/src/module/qt_window/mpvwidget.hh
+++ b/src/module/qt_window/mpvwidget.hh
@@ -6,25 +6,26 @@
 #include <QtGui>
 #include <lektor/common.h>
 
-class MpvWidget Q_DECL_FINAL: public QOpenGLWidget
-{
+class MpvWidget Q_DECL_FINAL : public QOpenGLWidget {
     Q_OBJECT
 public:
-    MpvWidget(struct queue *queue, lkt_db *db, QWidget* parent);
+    MpvWidget(struct queue *queue, lkt_db *db, QWidget *parent);
     virtual ~MpvWidget() override;
-    void command(const QVariant& params);
-    void setProperty(const QString& name, const QVariant& value);
-    QVariant getProperty(const QString& name) const;
-    QSize sizeHint() const { return QSize(480, 270);}
+    void command(const QVariant &params);
+    void setProperty(const QString &name, const QVariant &value);
+    QVariant getProperty(const QString &name) const;
+    QSize sizeHint() const { return QSize(480, 270); }
 Q_SIGNALS:
     void durationChanged(int value);
     void positionChanged(int value);
+
 protected:
     void initializeGL() Q_DECL_OVERRIDE;
     void paintGL() Q_DECL_OVERRIDE;
 private Q_SLOTS:
     void on_mpv_events();
     void maybeUpdate();
+
 public:
     void handle_mpv_event(mpv_event *event);
     static void on_update(void *ctx);
diff --git a/src/module/qt_window/mpvwidget_interface.hh b/src/module/qt_window/mpvwidget_interface.hh
index 89b6a42b..89b972d5 100644
--- a/src/module/qt_window/mpvwidget_interface.hh
+++ b/src/module/qt_window/mpvwidget_interface.hh
@@ -3,7 +3,7 @@
 #include <lektor/common.h>
 #include "../module_qt_window.hh"
 
-void * ___create_mpv_widget(struct poller_thread_arg *arg);
+void *___create_mpv_widget(struct poller_thread_arg *arg);
 
 bool ___module_qt_window_get_elapsed(struct module_qt_window_s *, int *);
 bool ___module_qt_window_get_duration(struct module_qt_window_s *, int *);
diff --git a/src/module/qt_window/qthelper.hh b/src/module/qt_window/qthelper.hh
index 8f1011b2..c5a8a999 100644
--- a/src/module/qt_window/qthelper.hh
+++ b/src/module/qt_window/qthelper.hh
@@ -1,6 +1,6 @@
-#ifndef LIBMPV_QTHELPER_H_
-#define LIBMPV_QTHELPER_H_
+#pragma once
 
+#include <lektor/common.h>
 #include <mpv/client.h>
 
 #include <cstring>
@@ -12,17 +12,21 @@
 #include <QSharedPointer>
 #include <QMetaType>
 
-namespace mpv::qt {
+namespace mpv::qt
+{
 
 // Wrapper around mpv_handle. Does refcounting under the hood.
-class Handle
-{
+class Handle {
     struct container {
-        container(mpv_handle *h) : mpv(h) {}
+        container(mpv_handle *h)
+            : mpv(h)
+        {
+        }
         ~container() { mpv_terminate_destroy(mpv); }
         mpv_handle *mpv;
     };
     QSharedPointer<container> sptr;
+
 public:
     // Construct a new Handle from a raw mpv_handle with refcount 1. If the
     // last Handle goes out of scope, the mpv_handle will be destroyed with
@@ -32,27 +36,26 @@ public:
     // destroying the mpv_handle.
     // Never create multiple wrappers from the same raw mpv_handle; copy the
     // wrapper instead (that's what it's for).
-    static Handle FromRawHandle(mpv_handle *handle) {
+    static Handle FromRawHandle(mpv_handle *handle)
+    {
         Handle h;
         h.sptr = QSharedPointer<container>(new container(handle));
         return h;
     }
 
     // Return the raw handle; for use with the libmpv C API.
-    operator mpv_handle*() const { return sptr ? (*sptr).mpv : nullptr; }
+    operator mpv_handle *() const { return sptr ? (*sptr).mpv : nullptr; }
 };
 
-static inline QVariant node_to_variant(const mpv_node *node)
+PRIVATE_FUNCTION QVariant
+node_to_variant(const mpv_node *node)
 {
     switch (node->format) {
-    case MPV_FORMAT_STRING:
-        return QVariant(QString::fromUtf8(node->u.string));
-    case MPV_FORMAT_FLAG:
-        return QVariant(static_cast<bool>(node->u.flag));
-    case MPV_FORMAT_INT64:
-        return QVariant(static_cast<qlonglong>(node->u.int64));
-    case MPV_FORMAT_DOUBLE:
-        return QVariant(node->u.double_);
+    case MPV_FORMAT_STRING: return QVariant(QString::fromUtf8(node->u.string));
+    case MPV_FORMAT_FLAG: return QVariant(static_cast<bool>(node->u.flag));
+    case MPV_FORMAT_INT64: return QVariant(static_cast<qlonglong>(node->u.int64));
+    case MPV_FORMAT_DOUBLE: return QVariant(node->u.double_);
+
     case MPV_FORMAT_NODE_ARRAY: {
         mpv_node_list *list = node->u.list;
         QVariantList qlist;
@@ -60,45 +63,44 @@ static inline QVariant node_to_variant(const mpv_node *node)
             qlist.append(node_to_variant(&list->values[n]));
         return QVariant(qlist);
     }
+
     case MPV_FORMAT_NODE_MAP: {
         mpv_node_list *list = node->u.list;
         QVariantMap qmap;
         for (int n = 0; n < list->num; n++) {
-            qmap.insert(QString::fromUtf8(list->keys[n]),
-                        node_to_variant(&list->values[n]));
+            qmap.insert(QString::fromUtf8(list->keys[n]), node_to_variant(&list->values[n]));
         }
         return QVariant(qmap);
     }
+
     case MPV_FORMAT_OSD_STRING:
     case MPV_FORMAT_NONE:
     case MPV_FORMAT_NODE:
-    case MPV_FORMAT_BYTE_ARRAY:
-        return QVariant();
+    case MPV_FORMAT_BYTE_ARRAY: return QVariant();
     }
 }
 
 struct node_builder {
-    node_builder(const QVariant& v) {
-        set(&node_, v);
-    }
-    ~node_builder() {
-        free_node(&node_);
-    }
+    node_builder(const QVariant &v) { set(&node_, v); }
+    ~node_builder() { free_node(&node_); }
     mpv_node *node() { return &node_; }
+
 private:
     Q_DISABLE_COPY(node_builder)
     mpv_node node_;
-    mpv_node_list *create_list(mpv_node *dst, bool is_map, int num) {
-        dst->format = is_map ? MPV_FORMAT_NODE_MAP : MPV_FORMAT_NODE_ARRAY;
+
+    mpv_node_list *create_list(mpv_node *dst, bool is_map, int num)
+    {
+        dst->format         = is_map ? MPV_FORMAT_NODE_MAP : MPV_FORMAT_NODE_ARRAY;
         mpv_node_list *list = new mpv_node_list();
-        dst->u.list = list;
+        dst->u.list         = list;
         if (!list)
             goto err;
         list->values = new mpv_node[static_cast<size_t>(num)]();
         if (!list->values)
             goto err;
         if (is_map) {
-            list->keys = new char*[static_cast<size_t>(num)]();
+            list->keys = new char *[static_cast<size_t>(num)]();
             if (!list->keys)
                 goto err;
         }
@@ -107,41 +109,44 @@ private:
         free_node(dst);
         return nullptr;
     }
-    char *dup_qstring(const QString &s) {
+
+    char *dup_qstring(const QString &s)
+    {
         QByteArray b = s.toUtf8();
-        char *r = new char[static_cast<size_t>(b.size() + 1)];
+        char *r      = new char[static_cast<size_t>(b.size() + 1)];
         if (r)
             std::memcpy(r, b.data(), static_cast<size_t>(b.size() + 1));
         return r;
     }
-    bool test_type(const QVariant &v, QMetaType::Type t) {
+
+    bool test_type(const QVariant &v, QMetaType::Type t)
+    {
         // The Qt docs say: "Although this function is declared as returning
         // "QVariant::Type(obsolete), the return value should be interpreted
         // as QMetaType::Type."
         // So a cast really seems to be needed to avoid warnings (urgh).
         return static_cast<int>(v.type()) == static_cast<int>(t);
     }
-    void set(mpv_node *dst, const QVariant &src) {
+
+    void set(mpv_node *dst, const QVariant &src)
+    {
         if (test_type(src, QMetaType::QString)) {
-            dst->format = MPV_FORMAT_STRING;
+            dst->format   = MPV_FORMAT_STRING;
             dst->u.string = dup_qstring(src.toString());
             if (!dst->u.string)
                 goto fail;
         } else if (test_type(src, QMetaType::Bool)) {
             dst->format = MPV_FORMAT_FLAG;
             dst->u.flag = src.toBool() ? 1 : 0;
-        } else if (test_type(src, QMetaType::Int) ||
-                   test_type(src, QMetaType::LongLong) ||
-                   test_type(src, QMetaType::UInt) ||
-                   test_type(src, QMetaType::ULongLong))
-        {
-            dst->format = MPV_FORMAT_INT64;
+        } else if (test_type(src, QMetaType::Int) || test_type(src, QMetaType::LongLong) ||
+                   test_type(src, QMetaType::UInt) || test_type(src, QMetaType::ULongLong)) {
+            dst->format  = MPV_FORMAT_INT64;
             dst->u.int64 = src.toLongLong();
         } else if (test_type(src, QMetaType::Double)) {
-            dst->format = MPV_FORMAT_DOUBLE;
+            dst->format    = MPV_FORMAT_DOUBLE;
             dst->u.double_ = src.toDouble();
         } else if (src.canConvert<QVariantList>()) {
-            QVariantList qlist = src.toList();
+            QVariantList qlist  = src.toList();
             mpv_node_list *list = create_list(dst, false, qlist.size());
             if (!list)
                 goto fail;
@@ -149,7 +154,7 @@ private:
             for (int n = 0; n < qlist.size(); n++)
                 set(&list->values[n], qlist[n]);
         } else if (src.canConvert<QVariantMap>()) {
-            QVariantMap qmap = src.toMap();
+            QVariantMap qmap    = src.toMap();
             mpv_node_list *list = create_list(dst, true, qmap.size());
             if (!list)
                 goto fail;
@@ -169,14 +174,16 @@ private:
     fail:
         dst->format = MPV_FORMAT_NONE;
     }
-    void free_node(mpv_node *dst) {
+
+    void free_node(mpv_node *dst)
+    {
+        mpv_node_list *list = nullptr;
         switch (dst->format) {
-        case MPV_FORMAT_STRING:
-            delete[] dst->u.string;
-            break;
+        case MPV_FORMAT_STRING: delete[] dst->u.string; break;
+
         case MPV_FORMAT_NODE_ARRAY:
-        case MPV_FORMAT_NODE_MAP: {
-            mpv_node_list *list = dst->u.list;
+        case MPV_FORMAT_NODE_MAP:
+            list = dst->u.list;
             if (list) {
                 for (int n = 0; n < list->num; n++) {
                     if (list->keys)
@@ -189,16 +196,16 @@ private:
             }
             delete list;
             break;
+
+        case MPV_FORMAT_OSD_STRING:
+        case MPV_FORMAT_NONE:
+        case MPV_FORMAT_NODE:
+        case MPV_FORMAT_FLAG:
+        case MPV_FORMAT_INT64:
+        case MPV_FORMAT_DOUBLE:
+        case MPV_FORMAT_BYTE_ARRAY: break;
         }
-    case MPV_FORMAT_OSD_STRING:
-    case MPV_FORMAT_NONE:
-    case MPV_FORMAT_NODE:
-    case MPV_FORMAT_FLAG:
-    case MPV_FORMAT_INT64:
-    case MPV_FORMAT_DOUBLE:
-    case MPV_FORMAT_BYTE_ARRAY:
-        break;
-        }
+
         dst->format = MPV_FORMAT_NONE;
     }
 };
@@ -208,73 +215,13 @@ private:
  */
 struct node_autofree {
     mpv_node *ptr;
-    node_autofree(mpv_node *a_ptr) : ptr(a_ptr) {}
+    node_autofree(mpv_node *a_ptr)
+        : ptr(a_ptr)
+    {
+    }
     ~node_autofree() { mpv_free_node_contents(ptr); }
 };
 
-/**
- * Return the given property as mpv_node converted to QVariant, or QVariant()
- * on error.
- *
- * @deprecated use get_property() instead
- *
- * @param name the property name
- */
-[[deprecated]]
-static inline QVariant get_property_variant(mpv_handle *ctx, const QString &name)
-{
-    mpv_node node;
-    if (mpv_get_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, &node) < 0)
-        return QVariant();
-    node_autofree f(&node);
-    return node_to_variant(&node);
-}
-
-/**
- * Set the given property as mpv_node converted from the QVariant argument.
-
- * @deprecated use set_property() instead
- */
- [[deprecated]]
-static inline int set_property_variant(mpv_handle *ctx, const QString &name,
-                                       const QVariant &v)
-{
-    node_builder node(v);
-    return mpv_set_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node());
-}
-
-/**
- * Set the given option as mpv_node converted from the QVariant argument.
- *
- * @ deprecated use set_property() instead
- *
- * @note Don't set as deprecated because `set_property` doesn't seem to do the
- * same thing...
- */
-static inline int set_option_variant(mpv_handle *ctx, const QString &name,
-                                     const QVariant &v)
-{
-    node_builder node(v);
-    return mpv_set_option(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node());
-}
-
-/**
- * mpv_command_node() equivalent. Returns QVariant() on error (and
- * unfortunately, the same on success).
- *
- * @deprecated use command() instead
- */
- [[deprecated]]
-static inline QVariant command_variant(mpv_handle *ctx, const QVariant &args)
-{
-    node_builder node(args);
-    mpv_node res;
-    if (mpv_command_node(ctx, node.node(), &res) < 0)
-        return QVariant();
-    node_autofree f(&res);
-    return node_to_variant(&res);
-}
-
 /**
  * This is used to return error codes wrapped in QVariant for functions which
  * return QVariant.
@@ -282,15 +229,20 @@ static inline QVariant command_variant(mpv_handle *ctx, const QVariant &args)
  * You can use get_error() or is_error() to extract the error status from a
  * QVariant value.
  */
-struct ErrorReturn
-{
+struct ErrorReturn {
     /**
      * enum mpv_error value (or a value outside of it if ABI was extended)
      */
     int error;
 
-    ErrorReturn() : error(0) {}
-    explicit ErrorReturn(int err) : error(err) {}
+    ErrorReturn()
+        : error(0)
+    {
+    }
+    explicit ErrorReturn(int err)
+        : error(err)
+    {
+    }
 };
 
 /**
@@ -299,7 +251,8 @@ struct ErrorReturn
  *
  * @return error code (<0) or success (>=0)
  */
-static inline int get_error(const QVariant &v)
+PRIVATE_FUNCTION int
+get_error(const QVariant &v)
 {
     if (!v.canConvert<ErrorReturn>())
         return 0;
@@ -309,7 +262,8 @@ static inline int get_error(const QVariant &v)
 /**
  * Return whether the QVariant carries a mpv error code.
  */
-static inline bool is_error(const QVariant &v)
+PRIVATE_FUNCTION bool
+is_error(const QVariant &v)
 {
     return get_error(v) < 0;
 }
@@ -321,7 +275,8 @@ static inline bool is_error(const QVariant &v)
  * @param name the property name
  * @return the property value, or an ErrorReturn with the error code
  */
-static inline QVariant get_property(mpv_handle *ctx, const QString &name)
+PRIVATE_FUNCTION QVariant
+get_property(mpv_handle *ctx, const QString &name)
 {
     mpv_node node;
     int err = mpv_get_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, &node);
@@ -336,8 +291,8 @@ static inline QVariant get_property(mpv_handle *ctx, const QString &name)
  *
  * @return mpv error code (<0 on error, >= 0 on success)
  */
-static inline int set_property(mpv_handle *ctx, const QString &name,
-                                       const QVariant &v)
+PRIVATE_FUNCTION int
+set_property(mpv_handle *ctx, const QString &name, const QVariant &v)
 {
     node_builder node(v);
     return mpv_set_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node());
@@ -349,7 +304,8 @@ static inline int set_property(mpv_handle *ctx, const QString &name,
  * @param args command arguments, with args[0] being the command name as string
  * @return the property value, or an ErrorReturn with the error code
  */
-static inline QVariant command(mpv_handle *ctx, const QVariant &args)
+PRIVATE_FUNCTION QVariant
+command(mpv_handle *ctx, const QVariant &args)
 {
     node_builder node(args);
     mpv_node res;
@@ -363,5 +319,3 @@ static inline QVariant command(mpv_handle *ctx, const QVariant &args)
 }
 
 Q_DECLARE_METATYPE(mpv::qt::ErrorReturn)
-
-#endif
diff --git a/utils/scripts/style.bash b/utils/scripts/style.bash
index 0b8e84d4..4fbad8dd 100755
--- a/utils/scripts/style.bash
+++ b/utils/scripts/style.bash
@@ -2,5 +2,5 @@
 set -e
 _ROOT="$(git rev-parse --show-toplevel)"
 cd "$_ROOT"
-find ./src \( -name '*.cpp' -o -name '*.cc' -o -name '*.c' -o -name '*.h' -o -name '*.hpp' \) -exec clang-format -i {} \;
-find ./inc \( -name '*.h' -o -name '*.hpp' \) -exec clang-format -i {} \;
+find ./src \( -name '*.cpp' -o -name '*.cc' -o -name '*.c' -o -name '*.h' -o -name '*.hh' -o -name '*.hpp' \) -exec clang-format -i {} \;
+find ./inc \( -name '*.h' -o -name '*.hh' -o -name '*.hpp' \) -exec clang-format -i {} \;
-- 
GitLab