You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kaffeine/kaffeine/src/player-parts/libmpv-part/libmpv_event.cpp

167 lines
6.0 KiB

/*
* Kaffeine libmpv part
* Copyright (C) 2023 Mavridis Philippe <mavridisf@gmail.com>
*
* Based on Kaffeine dummy part
* Copyright (C) 2004-2005 Jürgen Kofler <kaffeine@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
// TQt
#include <tqapplication.h>
#include <tqcursor.h>
#include <tqevent.h>
// TDE
#include <tdelocale.h>
#include <kdebug.h>
// Part
#include "libmpv_event.h"
#include "libmpv_part.h"
MpvEventThread::MpvEventThread(MpvPart *part) {
m_part = part;
initPropertyObservers();
}
void MpvEventThread::initPropertyObservers() {
mpv_observe_property(m_part->m_mpv, 0, "time-pos", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_part->m_mpv, 0, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_part->m_mpv, 0, "media-title", MPV_FORMAT_STRING);
// "The advantage over using this instead of calculating it out of other
// properties is that it properly falls back to estimating the playback
// position from the byte position, if the file duration is not known."
mpv_observe_property(m_part->m_mpv, 0, "percent-pos", MPV_FORMAT_DOUBLE);
//mpv_observe_property(m_mpv, 0, "track-list", MPV_FORMAT_NODE);
//mpv_observe_property(m_mpv, 0, "chapter-list", MPV_FORMAT_NODE);
}
void MpvEventThread::run() {
while (m_part->m_mpv) {
mpv_event *event = mpv_wait_event(m_part->m_mpv, 0);
if (event->event_id != MPV_EVENT_NONE) {
processEvent(event);
}
}
}
void MpvEventThread::processEvent(mpv_event *event) {
switch (event->event_id) {
case MPV_EVENT_PROPERTY_CHANGE: {
mpv_event_property *prop = (mpv_event_property *)event->data;
MpvPropertyChangeEvent *pe = new MpvPropertyChangeEvent(
prop->name, prop->format, prop->data);
TQApplication::postEvent(m_part, pe);
break;
}
case MPV_EVENT_LOG_MESSAGE: {
struct mpv_event_log_message *msg = (struct mpv_event_log_message *)event->data;
TQString text(msg->text);
text = text.remove("\n");
if (text.isEmpty()) break;
kdDebug() << "[mpv " << msg->prefix << "] <" << msg->level << ">: " << text << endl;
m_part->setStatusBarText(TQString("MPV %1 (%2): %3").arg(msg->prefix, msg->level, msg->text));
processMessage(msg->prefix, msg->level, msg->text);
break;
}
case MPV_EVENT_END_FILE: {
struct mpv_event_end_file *end = (struct mpv_event_end_file *)event->data;
if (end->reason != MPV_END_FILE_REASON_EOF &&
end->reason != MPV_END_FILE_REASON_ERROR) {
break;
}
// Track errors
TQString error;
if (end->reason == MPV_END_FILE_REASON_ERROR) {
error = TQString(mpv_error_string(end->error));
}
MpvEOFEvent *eofe = new MpvEOFEvent(end->reason, error);
TQApplication::postEvent(m_part, eofe);
}
default: break; // ignore other events
}
}
void MpvEventThread::processMessage(TQString prefix, TQString level, TQString text) {
// HACK: click-through for the context menu
// MPV does not yet natively support click-through and thus eats up all mouse
// events on its embedded window (https://github.com/mpv-player/mpv/issues/8938).
// Moreover, there seems to be no way to natively listen for keyboard/mouse events.
// This is a nasty workaround.
if (level == "warn" && prefix == "input" && text.contains("MBTN_")) {
int button = TQt::NoButton;
if (text.contains("MBTN_LEFT")) {
button = TQt::LeftButton;
}
else if (text.contains("MBTN_MID")) {
button = TQt::MidButton;
}
else if (text.contains("MBTN_RIGHT")) {
button = TQt::RightButton;
}
// Emulate mouse events
TQMouseEvent *me = new TQMouseEvent(TQEvent::MouseButtonPress, TQCursor::pos(), button, button);
TQApplication::postEvent(m_part->parent(), me);
me = new TQMouseEvent(TQEvent::MouseButtonRelease, TQCursor::pos(), button, button);
TQApplication::postEvent(m_part->parent(), me);
// Emulate doubleclicks
if (text.contains("DBL")) {
TQMouseEvent *me = new TQMouseEvent(TQEvent::MouseButtonDblClick, TQCursor::pos(), button, button);
TQApplication::postEvent(m_part->parent(), me);
}
// Context menu emulation
m_part->hideContextMenu();
TQContextMenuEvent *cme;
if (button == TQt::RightButton) {
cme = new TQContextMenuEvent(TQContextMenuEvent::Mouse, TQCursor::pos(), TQt::RightButton);
TQApplication::postEvent(m_part->m_player, cme);
}
}
else {
// Ignore X11 errors
if (level == "error" && prefix == "vo/gpu/x11") {
return;
}
// Log event
struct MpvEventData eventData = { TQDateTime::currentDateTime(), prefix, level, text };
m_part->logEvent(eventData);
// If it's an error, inform the user and optionally do stuff
if (level == "error" || level == "fatal") {
MpvErrorEvent *ee = new MpvErrorEvent(level == "fatal");
TQApplication::postEvent(m_part, ee);
if (prefix == "recorder") {
m_part->stopRecording();
}
}
}
}