Skip to content
Extraits de code Groupes Projets
Vérifiée Valider b09232ad rédigé par Kubat's avatar Kubat Validation de Elliu
Parcourir les fichiers

FMT bis + delete deprecated functions

parent 7a939633
Branches
Étiquettes
1 requête de fusion!186Add the Qt window module as an alternative to the SDL2 module
......@@ -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);
......
......@@ -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;
}
......
......@@ -6,8 +6,7 @@
#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);
......@@ -19,12 +18,14 @@ public:
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);
......
#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,7 +36,8 @@ 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;
......@@ -42,17 +47,15 @@ public:
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,35 +63,34 @@ 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) {
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;
......@@ -107,21 +109,27 @@ 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)];
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->u.string = dup_qstring(src.toString());
......@@ -130,11 +138,8 @@ private:
} 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))
{
} 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)) {
......@@ -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_BYTE_ARRAY: break;
}
dst->format = MPV_FORMAT_NONE;
}
};
......@@ -208,72 +215,12 @@ private:
*/
struct node_autofree {
mpv_node *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_autofree(mpv_node *a_ptr)
: ptr(a_ptr)
{
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);
}
~node_autofree() { mpv_free_node_contents(ptr); }
};
/**
* This is used to return error codes wrapped in QVariant for functions which
......@@ -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
......@@ -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 {} \;
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter