| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef LIBMPV_QTHELPER_H_ |
| 4 | #define LIBMPV_QTHELPER_H_ |
| 5 | |
| 6 | #include <cstring> |
| 7 | #include <mpv/client.h> |
| 8 | |
| 9 | #include <QHash> |
| 10 | #include <QList> |
| 11 | #include <QMetaType> |
| 12 | #include <QSharedPointer> |
| 13 | #include <QString> |
| 14 | #include <QVariant> |
| 15 | |
| 16 | namespace mpv { |
| 17 | namespace qt { |
| 18 | // Wrapper around mpv_handle. Does refcounting under the hood. |
| 19 | class Handle { |
| 20 | struct container { |
| 21 | container(mpv_handle* h) : mpv(h) {} |
| 22 | ~container() { |
| 23 | mpv_terminate_destroy(mpv); |
| 24 | } |
| 25 | mpv_handle* mpv; |
| 26 | }; |
| 27 | QSharedPointer<container> sptr; |
| 28 | |
| 29 | public: |
| 30 | // Construct a new Handle from a raw mpv_handle with refcount 1. If the |
| 31 | // last Handle goes out of scope, the mpv_handle will be destroyed with |
| 32 | // mpv_terminate_destroy(). |
| 33 | // Never destroy the mpv_handle manually when using this wrapper. You |
| 34 | // will create dangling pointers. Just let the wrapper take care of |
| 35 | // destroying the mpv_handle. |
| 36 | // Never create multiple wrappers from the same raw mpv_handle; copy the |
| 37 | // wrapper instead (that's what it's for). |
| 38 | static Handle FromRawHandle(mpv_handle* handle) { |
| 39 | Handle h; |
| 40 | h.sptr = QSharedPointer<container>(new container(handle)); |
| 41 | return h; |
| 42 | } |
| 43 | |
| 44 | // Return the raw handle; for use with the libmpv C API. |
| 45 | operator mpv_handle*() const { |
| 46 | return sptr ? (*sptr).mpv : 0; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | static inline QVariant node_to_variant(const mpv_node* node) { |
| 51 | switch (node->format) { |
| 52 | case MPV_FORMAT_STRING: |
| 53 | return QVariant(QString::fromUtf8(node->u.string)); |
| 54 | |
| 55 | case MPV_FORMAT_FLAG: |
| 56 | return QVariant(static_cast<bool>(node->u.flag)); |
| 57 | |
| 58 | case MPV_FORMAT_INT64: |
| 59 | return QVariant(static_cast<qlonglong>(node->u.int64)); |
| 60 | |
| 61 | case MPV_FORMAT_DOUBLE: |
| 62 | return QVariant(node->u.double_); |
| 63 | |
| 64 | case MPV_FORMAT_NODE_ARRAY: { |
| 65 | mpv_node_list* list = node->u.list; |
| 66 | QVariantList qlist; |
| 67 | |
| 68 | for (int n = 0; n < list->num; n++) { |
| 69 | qlist.append(node_to_variant(&list->values[n])); |
| 70 | } |
| 71 | |
| 72 | return QVariant(qlist); |
| 73 | } |
| 74 | case MPV_FORMAT_NODE_MAP: { |
| 75 | mpv_node_list* list = node->u.list; |
| 76 | QVariantMap qmap; |
| 77 | |
| 78 | for (int n = 0; n < list->num; n++) { |
| 79 | qmap.insert(QString::fromUtf8(list->keys[n]), node_to_variant(&list->values[n])); |
| 80 | } |
| 81 | |
| 82 | return QVariant(qmap); |
| 83 | } |
| 84 | default: // MPV_FORMAT_NONE, unknown values (e.g. future extensions) |
| 85 | return QVariant(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | struct node_builder { |
| 90 | node_builder(const QVariant& v) { |
| 91 | set(&node_, v); |
| 92 | } |
| 93 | |
| 94 | ~node_builder() { |
| 95 | free_node(&node_); |
| 96 | } |
| 97 | |
| 98 | mpv_node* node() { |
| 99 | return &node_; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | Q_DISABLE_COPY(node_builder) |
| 104 | mpv_node node_; |
| 105 | |
| 106 | mpv_node_list* create_list(mpv_node* dst, bool is_map, int num) { |
| 107 | dst->format = is_map ? MPV_FORMAT_NODE_MAP : MPV_FORMAT_NODE_ARRAY; |
| 108 | mpv_node_list* list = new mpv_node_list(); |
| 109 | dst->u.list = list; |
| 110 | |
| 111 | if (!list) { |
| 112 | goto err; |
| 113 | } |
| 114 | |
| 115 | list->values = new mpv_node[num](); |
| 116 | |
| 117 | if (!list->values) { |
| 118 | goto err; |
| 119 | } |
| 120 | |
| 121 | if (is_map) { |
| 122 | list->keys = new char*[num](); |
| 123 | |
| 124 | if (!list->keys) { |
| 125 | goto err; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return list; |
| 130 | |
| 131 | err: |
| 132 | free_node(dst); |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
| 136 | char* dup_qstring(const QString& s) { |
| 137 | QByteArray b = s.toUtf8(); |
| 138 | char* r = new char[b.size() + 1]; |
| 139 | |
| 140 | if (r) { |
| 141 | std::memcpy(r, b.data(), b.size() + 1); |
| 142 | } |
| 143 | |
| 144 | return r; |
| 145 | } |
| 146 | |
| 147 | bool test_type(const QVariant& v, QMetaType::Type t) { |
| 148 | // The Qt docs say: "Although this function is declared as returning |
| 149 | // "QVariant::Type(obsolete), the return value should be interpreted |
| 150 | // as QMetaType::Type." |
| 151 | // So a cast really seems to be needed to avoid warnings (urgh). |
| 152 | #if QT_VERSION_MAJOR == 5 |
| 153 | return v.type() == static_cast<int>(t); |
| 154 | #else |
| 155 | return v.typeId() == static_cast<int>(t); |
| 156 | #endif |
| 157 | } |
| 158 | void set(mpv_node* dst, const QVariant& src) { |
| 159 | if (test_type(src, QMetaType::QString)) { |
| 160 | dst->format = MPV_FORMAT_STRING; |
| 161 | dst->u.string = dup_qstring(src.toString()); |
| 162 | |
| 163 | if (!dst->u.string) { |
| 164 | goto fail; |
| 165 | } |
| 166 | } |
| 167 | else if (test_type(src, QMetaType::Bool)) { |
| 168 | dst->format = MPV_FORMAT_FLAG; |
| 169 | dst->u.flag = src.toBool() ? 1 : 0; |
| 170 | } |
| 171 | else if (test_type(src, QMetaType::Int) || test_type(src, QMetaType::LongLong) || |
| 172 | test_type(src, QMetaType::UInt) || test_type(src, QMetaType::ULongLong)) { |
| 173 | dst->format = MPV_FORMAT_INT64; |
| 174 | dst->u.int64 = src.toLongLong(); |
| 175 | } |
| 176 | else if (test_type(src, QMetaType::Double)) { |
| 177 | dst->format = MPV_FORMAT_DOUBLE; |
| 178 | dst->u.double_ = src.toDouble(); |
| 179 | } |
| 180 | else if (src.canConvert<QVariantList>()) { |
| 181 | QVariantList qlist = src.toList(); |
| 182 | mpv_node_list* list = create_list(dst, false, qlist.size()); |
| 183 | |
| 184 | if (!list) { |
| 185 | goto fail; |
| 186 | } |
| 187 | |
| 188 | list->num = qlist.size(); |
| 189 | |
| 190 | for (int n = 0; n < qlist.size(); n++) { |
| 191 | set(&list->values[n], qlist[n]); |
| 192 | } |
| 193 | } |
| 194 | else if (src.canConvert<QVariantMap>()) { |
| 195 | QVariantMap qmap = src.toMap(); |
| 196 | mpv_node_list* list = create_list(dst, true, qmap.size()); |
| 197 | |
| 198 | if (!list) { |
| 199 | goto fail; |
| 200 | } |
| 201 | |
| 202 | list->num = qmap.size(); |
| 203 | |
| 204 | for (int n = 0; n < qmap.size(); n++) { |
| 205 | list->keys[n] = dup_qstring(qmap.keys()[n]); |
| 206 | |
| 207 | if (!list->keys[n]) { |
| 208 | free_node(dst); |
| 209 | goto fail; |
| 210 | } |
| 211 | |
| 212 | set(&list->values[n], qmap.values()[n]); |
| 213 | } |
| 214 | } |
| 215 | else { |
| 216 | goto fail; |
| 217 | } |
| 218 | |
| 219 | return; |
| 220 | |
| 221 | fail: |
| 222 | dst->format = MPV_FORMAT_NONE; |
| 223 | } |
| 224 | |
| 225 | void free_node(mpv_node* dst) { |
| 226 | switch (dst->format) { |
| 227 | case MPV_FORMAT_STRING: |
| 228 | delete[] dst->u.string; |
| 229 | break; |
| 230 | |
| 231 | case MPV_FORMAT_NODE_ARRAY: |
| 232 | case MPV_FORMAT_NODE_MAP: { |
| 233 | mpv_node_list* list = dst->u.list; |
| 234 | |
| 235 | if (list) { |
| 236 | for (int n = 0; n < list->num; n++) { |
| 237 | if (list->keys) { |
| 238 | delete[] list->keys[n]; |
| 239 | } |
| 240 | |
| 241 | if (list->values) { |
| 242 | free_node(&list->values[n]); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | delete[] list->keys; |
| 247 | delete[] list->values; |
| 248 | } |
| 249 | |
| 250 | delete list; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | default:; |
| 255 | } |
| 256 | |
| 257 | dst->format = MPV_FORMAT_NONE; |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | /** |
| 262 | * RAII wrapper that calls mpv_free_node_contents() on the pointer. |
| 263 | */ |
| 264 | struct node_autofree { |
| 265 | mpv_node* ptr; |
| 266 | |
| 267 | node_autofree(mpv_node* a_ptr) : ptr(a_ptr) {} |
| 268 | |
| 269 | ~node_autofree() { |
| 270 | mpv_free_node_contents(ptr); |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | /** |
| 275 | * Return the given property as mpv_node converted to QVariant, or QVariant() |
| 276 | * on error. |
| 277 | * |
| 278 | * @deprecated use get_property() instead |
| 279 | * |
| 280 | * @param name the property name |
| 281 | */ |
| 282 | static inline QVariant get_property_variant(mpv_handle* ctx, const QString& name) { |
| 283 | mpv_node node; |
| 284 | |
| 285 | if (mpv_get_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, &node) < 0) { |
| 286 | return QVariant(); |
| 287 | } |
| 288 | |
| 289 | node_autofree f(&node); |
| 290 | return node_to_variant(&node); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Set the given property as mpv_node converted from the QVariant argument. |
| 295 | |
| 296 | * @deprecated use set_property() instead |
| 297 | */ |
| 298 | static inline int set_property_variant(mpv_handle* ctx, const QString& name, const QVariant& v) { |
| 299 | node_builder node(v); |
| 300 | |
| 301 | return mpv_set_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node()); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Set the given option as mpv_node converted from the QVariant argument. |
| 306 | * |
| 307 | * @deprecated use set_property() instead |
| 308 | */ |
| 309 | static inline int set_option_variant(mpv_handle* ctx, const QString& name, const QVariant& v) { |
| 310 | node_builder node(v); |
| 311 | |
| 312 | return mpv_set_option(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node()); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * mpv_command_node() equivalent. Returns QVariant() on error (and |
| 317 | * unfortunately, the same on success). |
| 318 | * |
| 319 | * @deprecated use command() instead |
| 320 | */ |
| 321 | static inline QVariant command_variant(mpv_handle* ctx, const QVariant& args) { |
| 322 | node_builder node(args); |
| 323 | mpv_node res; |
| 324 | |
| 325 | if (mpv_command_node(ctx, node.node(), &res) < 0) { |
| 326 | return QVariant(); |
| 327 | } |
| 328 | |
| 329 | node_autofree f(&res); |
| 330 | return node_to_variant(&res); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * This is used to return error codes wrapped in QVariant for functions which |
| 335 | * return QVariant. |
| 336 | * |
| 337 | * You can use get_error() or is_error() to extract the error status from a |
| 338 | * QVariant value. |
| 339 | */ |
| 340 | struct ErrorReturn { |
| 341 | /** |
| 342 | * enum mpv_error value (or a value outside of it if ABI was extended) |
| 343 | */ |
| 344 | int error; |
| 345 | |
| 346 | ErrorReturn() : error(0) {} |
| 347 | explicit ErrorReturn(int err) : error(err) {} |
| 348 | }; |
| 349 | |
| 350 | /** |
| 351 | * Return the mpv error code packed into a QVariant, or 0 (success) if it's not |
| 352 | * an error value. |
| 353 | * |
| 354 | * @return error code (<0) or success (>=0) |
| 355 | */ |
| 356 | static inline int get_error(const QVariant& v) { |
| 357 | if (!v.canConvert<ErrorReturn>()) { |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | return v.value<ErrorReturn>().error; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Return whether the QVariant carries a mpv error code. |
| 366 | */ |
| 367 | static inline bool is_error(const QVariant& v) { |
| 368 | return get_error(v) < 0; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Return the given property as mpv_node converted to QVariant, or QVariant() |
| 373 | * on error. |
| 374 | * |
| 375 | * @param name the property name |
| 376 | * @return the property value, or an ErrorReturn with the error code |
| 377 | */ |
| 378 | static inline QVariant get_property(mpv_handle* ctx, const QString& name) { |
| 379 | mpv_node node; |
| 380 | int err = mpv_get_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, &node); |
| 381 | |
| 382 | if (err < 0) { |
| 383 | return QVariant::fromValue(ErrorReturn(err)); |
| 384 | } |
| 385 | |
| 386 | node_autofree f(&node); |
| 387 | return node_to_variant(&node); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Set the given property as mpv_node converted from the QVariant argument. |
| 392 | * |
| 393 | * @return mpv error code (<0 on error, >= 0 on success) |
| 394 | */ |
| 395 | static inline int set_property(mpv_handle* ctx, const QString& name, const QVariant& v) { |
| 396 | node_builder node(v); |
| 397 | return mpv_set_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node()); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * mpv_command_node() equivalent. |
| 402 | * |
| 403 | * @param args command arguments, with args[0] being the command name as string |
| 404 | * @return the property value, or an ErrorReturn with the error code |
| 405 | */ |
| 406 | static inline QVariant command(mpv_handle* ctx, const QVariant& args) { |
| 407 | node_builder node(args); |
| 408 | mpv_node res; |
| 409 | int err = mpv_command_node(ctx, node.node(), &res); |
| 410 | |
| 411 | if (err < 0) { |
| 412 | return QVariant::fromValue(ErrorReturn(err)); |
| 413 | } |
| 414 | |
| 415 | node_autofree f(&res); |
| 416 | return node_to_variant(&res); |
| 417 | } |
| 418 | |
| 419 | } // namespace qt |
| 420 | } // namespace mpv |
| 421 | |
| 422 | Q_DECLARE_METATYPE(mpv::qt::ErrorReturn) |
| 423 | |
| 424 | #endif |