diff --git a/QtScrcpy/CMakeLists.txt b/QtScrcpy/CMakeLists.txt index a9d1c64..fc3e916 100755 --- a/QtScrcpy/CMakeLists.txt +++ b/QtScrcpy/CMakeLists.txt @@ -75,18 +75,49 @@ endif() # Qt # +# Find Qt version +if (NOT QT_DESIRED_VERSION) + find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core) + message(" >>> Found Qt version: ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}") + set(QT_DESIRED_VERSION ${QT_VERSION_MAJOR}) +endif() + set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Network Multimedia REQUIRED) -find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Network Multimedia REQUIRED) -if(CMAKE_SYSTEM_NAME STREQUAL "Linux") - find_package(QT NAMES Qt6 Qt5 COMPONENTS X11Extras REQUIRED) - find_package(Qt${QT_VERSION_MAJOR} COMPONENTS X11Extras REQUIRED) +set(qt_required_components Widgets Network Multimedia) + +if (QT_DESIRED_VERSION MATCHES 6) + # list(APPEND qt_required_components Core5Compat) + list(APPEND qt_required_components OpenGL) + list(APPEND qt_required_components OpenGLWidgets) +else() + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + list(APPEND qt_required_components X11Extras ) + endif() endif() -message(STATUS "[${PROJECT_NAME}] Qt version is: ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}") +find_package(Qt${QT_DESIRED_VERSION} REQUIRED COMPONENTS ${qt_required_components}) + +set(LINK_LIBS + Qt${QT_DESIRED_VERSION}::Widgets + Qt${QT_DESIRED_VERSION}::Network + Qt${QT_DESIRED_VERSION}::Multimedia +) + +if (QT_DESIRED_VERSION MATCHES 6) + # list(APPEND LINK_LIBS Qt${QT_DESIRED_VERSION}::Core5Compat) + list(APPEND LINK_LIBS Qt${QT_DESIRED_VERSION}::GuiPrivate) + list(APPEND LINK_LIBS Qt${QT_DESIRED_VERSION}::OpenGL) + list(APPEND LINK_LIBS Qt${QT_DESIRED_VERSION}::OpenGLWidgets) +else() + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + list(APPEND LINK_LIBS Qt${QT_DESIRED_VERSION}::X11Extras) + endif() +endif() + +message(STATUS "[${PROJECT_NAME}] Qt version is: ${QT_DESIRED_VERSION}") # # Sources @@ -312,8 +343,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux") find_package(Threads REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE - # qx11 - Qt${QT_VERSION_MAJOR}::X11Extras # xcb https://doc.qt.io/qt-5/linux-requirements.html xcb # pthread @@ -331,8 +360,6 @@ add_subdirectory(QtScrcpyCore) # Qt target_link_libraries(${PROJECT_NAME} PRIVATE - Qt${QT_VERSION_MAJOR}::Widgets - Qt${QT_VERSION_MAJOR}::Network - Qt${QT_VERSION_MAJOR}::Multimedia + ${LINK_LIBS} QtScrcpyCore ) diff --git a/QtScrcpy/QtScrcpyCore b/QtScrcpy/QtScrcpyCore index 6beab5c..43b7bb8 160000 --- a/QtScrcpy/QtScrcpyCore +++ b/QtScrcpy/QtScrcpyCore @@ -1 +1 @@ -Subproject commit 6beab5c967f7f77ddcfabe79757dc01202653ab7 +Subproject commit 43b7bb8fa22221f492ac5072857c4b467629d71f diff --git a/QtScrcpy/audio/audiooutput.cpp b/QtScrcpy/audio/audiooutput.cpp index 34e00cd..033abc8 100644 --- a/QtScrcpy/audio/audiooutput.cpp +++ b/QtScrcpy/audio/audiooutput.cpp @@ -4,11 +4,23 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) +#include +#include +#include +#endif + #include "audiooutput.h" AudioOutput::AudioOutput(QObject *parent) : QObject(parent) { + m_running = false; +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + m_audioOutput = nullptr; +#else + m_audioSink = nullptr; +#endif connect(&m_sndcpy, &QProcess::readyReadStandardOutput, this, [this]() { qInfo() << QString("AudioOutput::") << QString(m_sndcpy.readAllStandardOutput()); }); @@ -69,15 +81,10 @@ bool AudioOutput::runSndcpyProcess(const QString &serial, int port, bool wait) } #ifdef Q_OS_WIN32 - QStringList params; - params << serial; - params << QString("%1").arg(port); + QStringList params{serial, QString::number(port)}; m_sndcpy.start("sndcpy.bat", params); #else - QStringList params; - params << "sndcpy.sh"; - params << serial; - params << QString("%1").arg(port); + QStringList params{"sndcpy.sh", serial, QString::number(port)}; m_sndcpy.start("bash", params); #endif @@ -86,11 +93,11 @@ bool AudioOutput::runSndcpyProcess(const QString &serial, int port, bool wait) } if (!m_sndcpy.waitForStarted()) { - qWarning() << "AudioOutput::start sndcpy.bat failed"; + qWarning() << "AudioOutput::start sndcpy process failed"; return false; } if (!m_sndcpy.waitForFinished()) { - qWarning() << "AudioOutput::sndcpy.bat crashed"; + qWarning() << "AudioOutput::sndcpy process crashed"; return false; } @@ -99,6 +106,7 @@ bool AudioOutput::runSndcpyProcess(const QString &serial, int port, bool wait) void AudioOutput::startAudioOutput() { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) if (m_audioOutput) { return; } @@ -110,8 +118,8 @@ void AudioOutput::startAudioOutput() format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::SignedInt); - QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); + if (!info.isFormatSupported(format)) { qWarning() << "AudioOutput::audio format not supported, cannot play audio."; return; @@ -123,17 +131,47 @@ void AudioOutput::startAudioOutput() }); m_audioOutput->setBufferSize(48000*2*15/1000 * 20); m_outputDevice = m_audioOutput->start(); +#else + if (m_audioSink) { + return; + } + + QAudioFormat format; + format.setSampleRate(48000); + format.setChannelCount(2); + format.setSampleFormat(QAudioFormat::Int16); + QAudioDevice defaultDevice = QMediaDevices::defaultAudioOutput(); + if (!defaultDevice.isFormatSupported(format)) { + qWarning() << "AudioOutput::audio format not supported, cannot play audio."; + return; + } + m_audioSink = new QAudioSink(defaultDevice, format, this); + m_outputDevice = m_audioSink->start(); + if (!m_outputDevice) { + qWarning() << "AudioOutput::audio output device not available, cannot play audio."; + delete m_audioSink; + m_audioSink = nullptr; + return; + } +#endif } void AudioOutput::stopAudioOutput() { - if (!m_audioOutput) { - return; +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + if (m_audioOutput) { + m_audioOutput->stop(); + delete m_audioOutput; + m_audioOutput = nullptr; } - - m_audioOutput->stop(); - delete m_audioOutput; - m_audioOutput = nullptr; +#else + if (m_audioSink) { + m_audioSink->stop(); + delete m_audioSink; + m_audioSink = nullptr; + } +#endif + m_outputDevice = nullptr; } void AudioOutput::startRecvData(int port) @@ -165,7 +203,7 @@ void AudioOutput::startRecvData(int port) m_buffer.reserve(recv); } - qint64 count = audioSocket->read(m_buffer.data(), audioSocket->bytesAvailable()); + qint64 count = audioSocket->read(m_buffer.data(), recv); m_outputDevice->write(m_buffer.data(), count); }); connect(audioSocket, &QTcpSocket::stateChanged, audioSocket, [](QAbstractSocket::SocketState state) { diff --git a/QtScrcpy/audio/audiooutput.h b/QtScrcpy/audio/audiooutput.h index 1690168..d9060c1 100644 --- a/QtScrcpy/audio/audiooutput.h +++ b/QtScrcpy/audio/audiooutput.h @@ -6,6 +6,7 @@ #include #include +class QAudioSink; class QAudioOutput; class QIODevice; class AudioOutput : public QObject @@ -30,12 +31,16 @@ signals: void connectTo(int port); private: - QAudioOutput* m_audioOutput = nullptr; QPointer m_outputDevice; QThread m_workerThread; QProcess m_sndcpy; QVector m_buffer; bool m_running = false; +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + QAudioOutput* m_audioOutput = nullptr; +#else + QAudioSink *m_audioSink = nullptr; +#endif }; #endif // AUDIOOUTPUT_H diff --git a/QtScrcpy/main.cpp b/QtScrcpy/main.cpp index a24ba60..eeaebe1 100644 --- a/QtScrcpy/main.cpp +++ b/QtScrcpy/main.cpp @@ -55,10 +55,12 @@ int main(int argc, char *argv[]) QApplication::setAttribute(Qt::AA_UseDesktopOpenGL); } +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #if (QT_VERSION >= QT_VERSION_CHECK(5,14,0)) QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); +#endif #endif QSurfaceFormat varFormat = QSurfaceFormat::defaultFormat(); @@ -142,7 +144,10 @@ void installTranslator() break; } - translator.load(languagePath); + auto loaded = translator.load(languagePath); + if (!loaded) { + qWarning() << "Failed to load translation file:" << languagePath; + } qApp->installTranslator(&translator); } diff --git a/QtScrcpy/ui/dialog.cpp b/QtScrcpy/ui/dialog.cpp index 493060c..1350fec 100644 --- a/QtScrcpy/ui/dialog.cpp +++ b/QtScrcpy/ui/dialog.cpp @@ -618,9 +618,18 @@ void Dialog::on_usbConnectBtn_clicked() int Dialog::findDeviceFromeSerialBox(bool wifi) { - QRegExp regIP("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])\\b"); + QString regStr = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])\\b"; +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + QRegExp regIP(regStr); +#else + QRegularExpression regIP(regStr); +#endif for (int i = 0; i < ui->serialBox->count(); ++i) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) bool isWifi = regIP.exactMatch(ui->serialBox->itemText(i)); +#else + bool isWifi = regIP.match(ui->serialBox->itemText(i)).hasMatch(); +#endif bool found = wifi ? isWifi : !isWifi; if (found) { return i; diff --git a/QtScrcpy/ui/toolform.cpp b/QtScrcpy/ui/toolform.cpp index 35614f7..4d5c0e5 100644 --- a/QtScrcpy/ui/toolform.cpp +++ b/QtScrcpy/ui/toolform.cpp @@ -68,7 +68,11 @@ void ToolForm::updateGroupControl() void ToolForm::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) m_dragPosition = event->globalPos() - frameGeometry().topLeft(); +#else + m_dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft(); +#endif event->accept(); } } @@ -81,7 +85,11 @@ void ToolForm::mouseReleaseEvent(QMouseEvent *event) void ToolForm::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) move(event->globalPos() - m_dragPosition); +#else + move(event->globalPosition().toPoint() - m_dragPosition); +#endif event->accept(); } } diff --git a/QtScrcpy/ui/videoform.cpp b/QtScrcpy/ui/videoform.cpp index 5806f1d..ecf12f5 100644 --- a/QtScrcpy/ui/videoform.cpp +++ b/QtScrcpy/ui/videoform.cpp @@ -1,4 +1,4 @@ -#include +// #include #include #include #include @@ -363,21 +363,18 @@ void VideoForm::installShortcut() QRect VideoForm::getScreenRect() { QRect screenRect; - QWidget *win = window(); - if (!win) { - return screenRect; - } - - QWindow *winHandle = win->windowHandle(); QScreen *screen = QGuiApplication::primaryScreen(); - if (winHandle) { - screen = winHandle->screen(); - } - if (!screen) { - return screenRect; + QWidget *win = window(); + if (win) { + QWindow *winHandle = win->windowHandle(); + if (winHandle) { + screen = winHandle->screen(); + } } - screenRect = screen->availableGeometry(); + if (screen) { + screenRect = screen->availableGeometry(); + } return screenRect; } @@ -572,23 +569,32 @@ void VideoForm::mousePressEvent(QMouseEvent *event) } } +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + QPointF localPos = event->localPos(); + QPointF globalPos = event->globalPos(); +#else + QPointF localPos = event->position(); + QPointF globalPos = event->globalPosition(); +#endif + if (m_videoWidget->geometry().contains(event->pos())) { if (!device) { return; } - event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); - emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); + QPointF mappedPos = m_videoWidget->mapFrom(this, localPos.toPoint()); + QMouseEvent newEvent(event->type(), mappedPos, globalPos, event->button(), event->buttons(), event->modifiers()); + emit device->mouseEvent(&newEvent, m_videoWidget->frameSize(), m_videoWidget->size()); // debug keymap pos if (event->button() == Qt::LeftButton) { - qreal x = event->localPos().x() / m_videoWidget->size().width(); - qreal y = event->localPos().y() / m_videoWidget->size().height(); + qreal x = localPos.x() / m_videoWidget->size().width(); + qreal y = localPos.y() / m_videoWidget->size().height(); QString posTip = QString(R"("pos": {"x": %1, "y": %2})").arg(x).arg(y); qInfo() << posTip.toStdString().c_str(); } } else { if (event->button() == Qt::LeftButton) { - m_dragPosition = event->globalPos() - frameGeometry().topLeft(); + m_dragPosition = globalPos.toPoint() - frameGeometry().topLeft(); event->accept(); } } @@ -601,9 +607,15 @@ void VideoForm::mouseReleaseEvent(QMouseEvent *event) if (!device) { return; } - event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + QPointF localPos = event->localPos(); + QPointF globalPos = event->globalPos(); +#else + QPointF localPos = event->position(); + QPointF globalPos = event->globalPosition(); +#endif // local check - QPointF local = event->localPos(); + QPointF local = m_videoWidget->mapFrom(this, localPos.toPoint()); if (local.x() < 0) { local.setX(0); } @@ -616,8 +628,8 @@ void VideoForm::mouseReleaseEvent(QMouseEvent *event) if (local.y() > m_videoWidget->height()) { local.setY(m_videoWidget->height()); } - event->setLocalPos(local); - emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); + QMouseEvent newEvent(event->type(), local, globalPos, event->button(), event->buttons(), event->modifiers()); + emit device->mouseEvent(&newEvent, m_videoWidget->frameSize(), m_videoWidget->size()); } else { m_dragPosition = QPoint(0, 0); } @@ -625,16 +637,24 @@ void VideoForm::mouseReleaseEvent(QMouseEvent *event) void VideoForm::mouseMoveEvent(QMouseEvent *event) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + QPointF localPos = event->localPos(); + QPointF globalPos = event->globalPos(); +#else + QPointF localPos = event->position(); + QPointF globalPos = event->globalPosition(); +#endif auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial); if (m_videoWidget->geometry().contains(event->pos())) { if (!device) { return; } - event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); - emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); + QPointF mappedPos = m_videoWidget->mapFrom(this, localPos.toPoint()); + QMouseEvent newEvent(event->type(), mappedPos, globalPos, event->button(), event->buttons(), event->modifiers()); + emit device->mouseEvent(&newEvent, m_videoWidget->frameSize(), m_videoWidget->size()); } else if (!m_dragPosition.isNull()) { if (event->buttons() & Qt::LeftButton) { - move(event->globalPos() - m_dragPosition); + move(globalPos.toPoint() - m_dragPosition); event->accept(); } } @@ -657,8 +677,16 @@ void VideoForm::mouseDoubleClickEvent(QMouseEvent *event) if (!device) { return; } - event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); - emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + QPointF localPos = event->localPos(); + QPointF globalPos = event->globalPos(); +#else + QPointF localPos = event->position(); + QPointF globalPos = event->globalPosition(); +#endif + QPointF mappedPos = m_videoWidget->mapFrom(this, localPos.toPoint()); + QMouseEvent newEvent(event->type(), mappedPos, globalPos, event->button(), event->buttons(), event->modifiers()); + emit device->mouseEvent(&newEvent, m_videoWidget->frameSize(), m_videoWidget->size()); } } @@ -714,7 +742,11 @@ void VideoForm::paintEvent(QPaintEvent *paint) { Q_UNUSED(paint) QStyleOption opt; +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) opt.init(this); +#else + opt.initFrom(this); +#endif QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } diff --git a/QtScrcpy/util/config.cpp b/QtScrcpy/util/config.cpp index 43bcee0..2290426 100644 --- a/QtScrcpy/util/config.cpp +++ b/QtScrcpy/util/config.cpp @@ -113,10 +113,11 @@ QString Config::s_configPath = ""; Config::Config(QObject *parent) : QObject(parent) { m_settings = new QSettings(getConfigPath() + "/config.ini", QSettings::IniFormat); - m_settings->setIniCodec("UTF-8"); - m_userData = new QSettings(getConfigPath() + "/userdata.ini", QSettings::IniFormat); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + m_settings->setIniCodec("UTF-8"); m_userData->setIniCodec("UTF-8"); +#endif qDebug()<childGroups(); } diff --git a/QtScrcpy/util/mousetap/xmousetap.cpp b/QtScrcpy/util/mousetap/xmousetap.cpp index 18ba226..7d52a74 100644 --- a/QtScrcpy/util/mousetap/xmousetap.cpp +++ b/QtScrcpy/util/mousetap/xmousetap.cpp @@ -1,4 +1,10 @@ -#include +#include + +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) +#include +#else +#include +#endif #include #include