| 1 |
/* |
| 2 |
* This file is part of Bino, a 3D video player. |
| 3 |
* |
| 4 |
* Copyright (C) 2022, 2023, 2024, 2025 |
| 5 |
* Martin Lambers <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <QCommandLineParser> |
| 22 |
#include <QMediaDevices> |
| 23 |
#include <QGuiApplication> |
| 24 |
#include <QWindowCapture> |
| 25 |
|
| 26 |
#include "commandinterpreter.hpp" |
| 27 |
#include "modes.hpp" |
| 28 |
#include "bino.hpp" |
| 29 |
#include "gui.hpp" |
| 30 |
#include "log.hpp" |
| 31 |
|
| 32 |
|
| 33 |
CommandInterpreter::CommandInterpreter() : |
| 34 |
_file(), |
| 35 |
_lineBuf(), |
| 36 |
_lineIndex(-1), |
| 37 |
_waitForStop(false) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
bool CommandInterpreter::init(const QString& fileName) |
| 42 |
{ |
| 43 |
_file.setFileName(fileName); |
| 44 |
if (!_file.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text)) { |
| 45 |
LOG_FATAL("%s", qPrintable(tr("Cannot open %1: %2").arg(fileName).arg(_file.errorString()))); |
| 46 |
return false; |
| 47 |
} |
| 48 |
_lineBuf.resize(2048); |
| 49 |
_lineIndex = 0; |
| 50 |
_waitForStop = false; |
| 51 |
connect(&_timer, SIGNAL(timeout()), this, SLOT(processNextCommand())); |
| 52 |
_waitTimer.setSingleShot(true); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
void CommandInterpreter::start() |
| 57 |
{ |
| 58 |
_timer.start(20); // every 20 msecs = 50 times per second |
| 59 |
} |
| 60 |
|
| 61 |
static int getOnOff(const QString& s) |
| 62 |
{ |
| 63 |
if (s == "on") |
| 64 |
return 1; |
| 65 |
else if (s == "off") |
| 66 |
return 0; |
| 67 |
else |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
void CommandInterpreter::processNextCommand() |
| 72 |
{ |
| 73 |
if (_waitTimer.isActive()) |
| 74 |
return; |
| 75 |
if (_waitForStop && !Bino::instance()->stopped()) |
| 76 |
return; |
| 77 |
_waitForStop = false; |
| 78 |
|
| 79 |
_file.startTransaction(); |
| 80 |
qint64 lineLen = _file.readLine(_lineBuf.data(), _lineBuf.size()); |
| 81 |
if (lineLen < 0 && _file.atEnd()) { |
| 82 |
// eof |
| 83 |
_timer.stop(); |
| 84 |
return; |
| 85 |
} |
| 86 |
if (lineLen < 0) { |
| 87 |
// input error |
| 88 |
_timer.stop(); |
| 89 |
LOG_FATAL("%s", qPrintable(tr("Cannot read command from %1").arg(_file.fileName()))); |
| 90 |
return; |
| 91 |
} |
| 92 |
if (lineLen == _lineBuf.size() - 1 && _lineBuf[lineLen - 1] != '\n') { |
| 93 |
// overflow of the line buffer |
| 94 |
_timer.stop(); |
| 95 |
LOG_FATAL("%s", qPrintable(tr("Cannot read command from %1").arg(_file.fileName()))); |
| 96 |
return; |
| 97 |
} |
| 98 |
if (lineLen > 0 && _lineBuf[lineLen - 1] != '\n') { |
| 99 |
// an incomplete line was read; roll back and try again |
| 100 |
// on the next call to this function |
| 101 |
_file.rollbackTransaction(); |
| 102 |
return; |
| 103 |
} |
| 104 |
_file.commitTransaction(); |
| 105 |
_lineIndex++; |
| 106 |
if (lineLen == 0) { |
| 107 |
return; |
| 108 |
} |
| 109 |
QString cmd = QString(_lineBuf.data()).simplified(); |
| 110 |
LOG_DEBUG("Command line %d: %s", _lineIndex, qPrintable(cmd)); |
| 111 |
|
| 112 |
// empty lines and comments |
| 113 |
if (cmd.length() == 0 || cmd[0] == '#') |
| 114 |
return; |
| 115 |
|
| 116 |
if (cmd == "quit") { |
| 117 |
Bino::instance()->quit(); |
| 118 |
} else if (cmd.startsWith("wait ")) { |
| 119 |
if (cmd.mid(5) == "stop") { |
| 120 |
_waitForStop = true; |
| 121 |
} else { |
| 122 |
bool ok; |
| 123 |
float val = cmd.mid(5).toFloat(&ok); |
| 124 |
if (!ok) { |
| 125 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 126 |
} else { |
| 127 |
_waitTimer.start(val * 1000); |
| 128 |
} |
| 129 |
} |
| 130 |
} else if (cmd.startsWith("open ")) { |
| 131 |
QCommandLineParser parser; |
| 132 |
parser.addPositionalArgument("URL", "x"); |
| 133 |
parser.addOption({ "input", "", "x" }); |
| 134 |
parser.addOption({ "surround", "", "x" }); |
| 135 |
parser.addOption({ "video-track", "", "x" }); |
| 136 |
parser.addOption({ "audio-track", "", "x" }); |
| 137 |
parser.addOption({ "subtitle-track", "", "x" }); |
| 138 |
if (!parser.parse(cmd.split(' '))) { |
| 139 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 140 |
} else if (parser.positionalArguments().length() == 0 || parser.positionalArguments().length() > 1) { |
| 141 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 142 |
} else { |
| 143 |
QUrl url = parser.positionalArguments()[0]; |
| 144 |
InputMode inputMode = Input_Unknown; |
| 145 |
SurroundMode surroundMode = Surround_Unknown; |
| 146 |
int videoTrack = PlaylistEntry::DefaultTrack; |
| 147 |
int audioTrack = PlaylistEntry::DefaultTrack; |
| 148 |
int subtitleTrack = PlaylistEntry::NoTrack; |
| 149 |
bool ok = true; |
| 150 |
if (parser.isSet("input")) { |
| 151 |
inputMode = inputModeFromString(parser.value("input"), &ok); |
| 152 |
if (!ok) |
| 153 |
LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--input"))); |
| 154 |
} |
| 155 |
if (parser.isSet("surround")) { |
| 156 |
surroundMode = surroundModeFromString(parser.value("surround"), &ok); |
| 157 |
if (!ok) |
| 158 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 159 |
} |
| 160 |
if (parser.isSet("video-track")) { |
| 161 |
int t = parser.value("video-track").toInt(&ok); |
| 162 |
if (ok && t >= 0) { |
| 163 |
videoTrack = t; |
| 164 |
} else { |
| 165 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 166 |
ok = false; |
| 167 |
} |
| 168 |
} |
| 169 |
if (parser.isSet("audio-track")) { |
| 170 |
int t = parser.value("audio-track").toInt(&ok); |
| 171 |
if (ok && t >= 0) { |
| 172 |
audioTrack = t; |
| 173 |
} else { |
| 174 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 175 |
ok = false; |
| 176 |
} |
| 177 |
} |
| 178 |
if (parser.isSet("subtitle-track") && parser.value("subtitle-track").length() > 0) { |
| 179 |
int t = parser.value("subtitle-track").toInt(&ok); |
| 180 |
if (ok && t >= 0) { |
| 181 |
subtitleTrack = t; |
| 182 |
} else { |
| 183 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 184 |
ok = false; |
| 185 |
} |
| 186 |
} |
| 187 |
if (ok) { |
| 188 |
Bino::instance()->startPlaylistMode(); |
| 189 |
Playlist::instance()->clear(); |
| 190 |
Playlist::instance()->append(PlaylistEntry(url, inputMode, surroundMode, videoTrack, audioTrack, subtitleTrack)); |
| 191 |
Playlist::instance()->start(); |
| 192 |
} |
| 193 |
} |
| 194 |
} else if (cmd.startsWith("capture") && (cmd[7] == '\0' || cmd[7] == ' ')) { |
| 195 |
QCommandLineParser parser; |
| 196 |
parser.addOption({ "input", "", "x" }); |
| 197 |
parser.addOption({ "audio-input", "", "x" }); |
| 198 |
parser.addOption({ "video-input", "", "x" }); |
| 199 |
parser.addOption({ "screen-input", "", "x" }); |
| 200 |
parser.addOption({ "window-input", "", "x" }); |
| 201 |
if (!parser.parse(cmd.split(' '))) { |
| 202 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 203 |
} else { |
| 204 |
bool ok; |
| 205 |
InputMode inputMode = Input_Unknown; |
| 206 |
QList<QAudioDevice> audioInputDevices; |
| 207 |
QList<QCameraDevice> videoInputDevices; |
| 208 |
QList<QScreen*> screenInputDevices; |
| 209 |
QList<QCapturableWindow> windowInputDevices; |
| 210 |
int audioInputDeviceIndex = -1; |
| 211 |
int videoInputDeviceIndex = -1; |
| 212 |
int screenInputDeviceIndex = -1; |
| 213 |
int windowInputDeviceIndex = -1; |
| 214 |
if (parser.isSet("input")) { |
| 215 |
inputMode = inputModeFromString(parser.value("input"), &ok); |
| 216 |
if (!ok) |
| 217 |
LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--input"))); |
| 218 |
} |
| 219 |
if (parser.isSet("audio-input")) { |
| 220 |
if (parser.value("audio-input").length() == 0) { |
| 221 |
audioInputDeviceIndex = -2; // this means no audio input |
| 222 |
} else { |
| 223 |
audioInputDevices = QMediaDevices::audioInputs(); |
| 224 |
int ai = parser.value("audio-input").toInt(&ok); |
| 225 |
if (ok && ai >= 0 && ai < audioInputDevices.size()) { |
| 226 |
audioInputDeviceIndex = ai; |
| 227 |
} else { |
| 228 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 229 |
ok = false; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
if (parser.isSet("video-input")) { |
| 234 |
videoInputDevices = QMediaDevices::videoInputs(); |
| 235 |
int vi = parser.value("video-input").toInt(&ok); |
| 236 |
if (ok && vi >= 0 && vi < videoInputDevices.size()) { |
| 237 |
videoInputDeviceIndex = vi; |
| 238 |
} else { |
| 239 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 240 |
ok = false; |
| 241 |
} |
| 242 |
} |
| 243 |
if (parser.isSet("screen-input")) { |
| 244 |
screenInputDevices = QGuiApplication::screens(); |
| 245 |
int vi = parser.value("screen-input").toInt(&ok); |
| 246 |
if (ok && vi >= 0 && vi < screenInputDevices.size()) { |
| 247 |
screenInputDeviceIndex = vi; |
| 248 |
} else { |
| 249 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 250 |
ok = false; |
| 251 |
} |
| 252 |
} |
| 253 |
if (parser.isSet("window-input")) { |
| 254 |
windowInputDevices = QWindowCapture::capturableWindows(); |
| 255 |
int vi = parser.value("window-input").toInt(&ok); |
| 256 |
if (ok && vi >= 0 && vi < windowInputDevices.size()) { |
| 257 |
windowInputDeviceIndex = vi; |
| 258 |
} else { |
| 259 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 260 |
ok = false; |
| 261 |
} |
| 262 |
} |
| 263 |
if (ok) { |
| 264 |
if (screenInputDeviceIndex < 0 && windowInputDeviceIndex < 0) { |
| 265 |
Bino::instance()->startCaptureModeCamera(audioInputDeviceIndex >= -1, |
| 266 |
audioInputDeviceIndex >= 0 |
| 267 |
? audioInputDevices[audioInputDeviceIndex] |
| 268 |
: QMediaDevices::defaultAudioInput(), |
| 269 |
videoInputDeviceIndex >= 0 |
| 270 |
? videoInputDevices[videoInputDeviceIndex] |
| 271 |
: QMediaDevices::defaultVideoInput(), |
| 272 |
inputMode); |
| 273 |
} else if (screenInputDeviceIndex >= 0) { |
| 274 |
Bino::instance()->startCaptureModeScreen(audioInputDeviceIndex >= -1, |
| 275 |
audioInputDeviceIndex >= 0 |
| 276 |
? audioInputDevices[audioInputDeviceIndex] |
| 277 |
: QMediaDevices::defaultAudioInput(), |
| 278 |
screenInputDevices[screenInputDeviceIndex], |
| 279 |
inputMode); |
| 280 |
} else { |
| 281 |
Bino::instance()->startCaptureModeWindow(audioInputDeviceIndex >= -1, |
| 282 |
audioInputDeviceIndex >= 0 |
| 283 |
? audioInputDevices[audioInputDeviceIndex] |
| 284 |
: QMediaDevices::defaultAudioInput(), |
| 285 |
windowInputDevices[windowInputDeviceIndex], |
| 286 |
inputMode); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} else if (cmd.startsWith("set-output-mode ")) { |
| 291 |
bool ok; |
| 292 |
OutputMode outputMode = outputModeFromString(cmd.mid(16), &ok); |
| 293 |
if (!ok) { |
| 294 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 295 |
} else { |
| 296 |
Gui* gui = Gui::instance(); |
| 297 |
if (gui) |
| 298 |
gui->setOutputMode(outputMode); |
| 299 |
} |
| 300 |
} else if (cmd.startsWith("set-surround-vfov ")) { |
| 301 |
bool ok; |
| 302 |
float surroundVerticalFOV = cmd.mid(18).toFloat(&ok); |
| 303 |
if (!ok || surroundVerticalFOV < 5.0f || surroundVerticalFOV > 115.0f) { |
| 304 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 305 |
} else { |
| 306 |
Gui* gui = Gui::instance(); |
| 307 |
if (gui) |
| 308 |
gui->setSurroundVerticalFieldOfView(surroundVerticalFOV); |
| 309 |
} |
| 310 |
} else if (cmd == "play") { |
| 311 |
Bino::instance()->play(); |
| 312 |
} else if (cmd == "stop") { |
| 313 |
Bino::instance()->stop(); |
| 314 |
} else if (cmd == "pause") { |
| 315 |
Bino::instance()->pause(); |
| 316 |
} else if (cmd == "toggle-pause") { |
| 317 |
Bino::instance()->togglePause(); |
| 318 |
} else if (cmd.startsWith("set-position ")) { |
| 319 |
bool ok; |
| 320 |
float val = cmd.mid(13).toFloat(&ok); |
| 321 |
if (!ok) { |
| 322 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 323 |
} else { |
| 324 |
Bino::instance()->setPosition(val); |
| 325 |
} |
| 326 |
} else if (cmd.startsWith("playlist-load ")) { |
| 327 |
QString name = cmd.mid(14); |
| 328 |
QString errStr; |
| 329 |
bool load(const QString& fileName, QString& errStr); |
| 330 |
if (!(Playlist::instance()->load(name, errStr))) { |
| 331 |
LOG_FATAL("%s", qPrintable(tr("%1: %2").arg(name).arg(errStr))); |
| 332 |
} |
| 333 |
} else if (cmd == "playlist-next") { |
| 334 |
Playlist::instance()->next(); |
| 335 |
} else if (cmd == "playlist-prev") { |
| 336 |
Playlist::instance()->prev(); |
| 337 |
} else if (cmd.startsWith("playlist-wait ")) { |
| 338 |
bool ok; |
| 339 |
WaitMode waitMode = waitModeFromString(cmd.mid(14), &ok); |
| 340 |
if (!ok) { |
| 341 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 342 |
} else { |
| 343 |
Playlist::instance()->setWaitMode(waitMode); |
| 344 |
} |
| 345 |
} else if (cmd.startsWith("playlist-loop ")) { |
| 346 |
bool ok; |
| 347 |
LoopMode loopMode = loopModeFromString(cmd.mid(14), &ok); |
| 348 |
if (!ok) { |
| 349 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 350 |
} else { |
| 351 |
Playlist::instance()->setLoopMode(loopMode); |
| 352 |
} |
| 353 |
} else if (cmd.startsWith("seek ")) { |
| 354 |
bool ok; |
| 355 |
float val = cmd.mid(5).toFloat(&ok); |
| 356 |
if (!ok) { |
| 357 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 358 |
} else { |
| 359 |
Bino::instance()->seek(val); |
| 360 |
} |
| 361 |
} else if (cmd.startsWith("set-swap-eyes ")) { |
| 362 |
int onoff = getOnOff(cmd.mid(14)); |
| 363 |
if (onoff < 0) { |
| 364 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 365 |
} else { |
| 366 |
Bino::instance()->setSwapEyes(onoff); |
| 367 |
} |
| 368 |
} else if (cmd == "toggle-swap-eyes") { |
| 369 |
Bino::instance()->toggleSwapEyes(); |
| 370 |
} else if (cmd.startsWith("set-fullscreen ")) { |
| 371 |
int onoff = getOnOff(cmd.mid(15)); |
| 372 |
if (onoff < 0) { |
| 373 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 374 |
} else { |
| 375 |
Gui* gui = Gui::instance(); |
| 376 |
if (gui) |
| 377 |
gui->setFullscreen(onoff); |
| 378 |
} |
| 379 |
} else if (cmd == "toggle-fullscreen") { |
| 380 |
Gui* gui = Gui::instance(); |
| 381 |
if (gui) |
| 382 |
gui->viewToggleFullscreen(); |
| 383 |
} else if (cmd.startsWith("set-mute ")) { |
| 384 |
int onoff = getOnOff(cmd.mid(9)); |
| 385 |
if (onoff < 0) { |
| 386 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 387 |
} else { |
| 388 |
Bino::instance()->setMute(onoff); |
| 389 |
} |
| 390 |
} else if (cmd == "toggle-mute") { |
| 391 |
Bino::instance()->toggleMute(); |
| 392 |
} else if (cmd.startsWith("set-volume ")) { |
| 393 |
bool ok; |
| 394 |
float val = cmd.mid(11).toFloat(&ok); |
| 395 |
if (!ok) { |
| 396 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 397 |
} else { |
| 398 |
Bino::instance()->setVolume(val); |
| 399 |
} |
| 400 |
} else if (cmd.startsWith("adjust-volume ")) { |
| 401 |
bool ok; |
| 402 |
float val = cmd.mid(14).toFloat(&ok); |
| 403 |
if (!ok) { |
| 404 |
LOG_FATAL("%s", qPrintable(tr("Invalid argument in %1 line %2").arg(_file.fileName()).arg(_lineIndex))); |
| 405 |
} else { |
| 406 |
Bino::instance()->changeVolume(val); |
| 407 |
} |
| 408 |
} else { |
| 409 |
LOG_FATAL("%s", qPrintable(tr("Invalid command %1 line %2").arg(cmd).arg(_lineIndex))); |
| 410 |
} |
| 411 |
} |