| 1 | /* |
| 2 | * midisnoop - MIDI monitor and prober |
| 3 | * Copyright (C) 2012 Devin Anderson |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the Free |
| 7 | * Software Foundation; either version 2 of the License, or (at your option) |
| 8 | * any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 675 Mass |
| 17 | * Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <cassert> |
| 21 | |
| 22 | #include <QtCore/QDebug> |
| 23 | |
| 24 | #include "controller.h" |
| 25 | #include "error.h" |
| 26 | #include "util.h" |
| 27 | |
| 28 | // Static data |
| 29 | |
| 30 | const int statusLengths[0x80] = { |
| 31 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 32 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 33 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 34 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 35 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 36 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 37 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 38 | 0, 2, 3, 2, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1 |
| 39 | }; |
| 40 | |
| 41 | // Class definition |
| 42 | |
| 43 | Controller::Controller(Application &application, QObject *parent): |
| 44 | QObject(parent), |
| 45 | application(application) |
| 46 | { |
| 47 | // Setup about view |
| 48 | aboutView.setMajorVersion(MIDISNOOP_MAJOR_VERSION); |
| 49 | aboutView.setMinorVersion(MIDISNOOP_MINOR_VERSION); |
| 50 | aboutView.setRevision(MIDISNOOP_REVISION); |
| 51 | connect(&aboutView, SIGNAL(closeRequest()), |
| 52 | &aboutView, SLOT(hide())); |
| 53 | |
| 54 | // Setup configure view |
| 55 | int driverCount = engine.getDriverCount(); |
| 56 | if (! driverCount) { |
| 57 | throw Error(tr("no MIDI drivers found")); |
| 58 | } |
| 59 | for (int i = 0; i < driverCount; i++) { |
| 60 | configureView.addDriver(i, engine.getDriverName(i)); |
| 61 | } |
| 62 | int driver = engine.getDriver(); |
| 63 | int outputPort = engine.getOutputPort(); |
| 64 | configureView.setDriver(driver); |
| 65 | configureView.setInputPort(engine.getInputPort()); |
| 66 | configureView.setIgnoreActiveSensingEvents |
| 67 | (engine.getIgnoreActiveSensingEvents()); |
| 68 | configureView.setIgnoreSystemExclusiveEvents |
| 69 | (engine.getIgnoreSystemExclusiveEvents()); |
| 70 | configureView.setIgnoreTimeEvents(engine.getIgnoreTimeEvents()); |
| 71 | configureView.setOutputPort(outputPort); |
| 72 | connect(&configureView, SIGNAL(closeRequest()), |
| 73 | &configureView, SLOT(hide())); |
| 74 | connect(&configureView, SIGNAL(driverChangeRequest(int)), |
| 75 | &engine, SLOT(setDriver(int))); |
| 76 | connect(&configureView, |
| 77 | SIGNAL(ignoreActiveSensingEventsChangeRequest(bool)), |
| 78 | &engine, SLOT(setIgnoreActiveSensingEvents(bool))); |
| 79 | connect(&configureView, |
| 80 | SIGNAL(ignoreSystemExclusiveEventsChangeRequest(bool)), |
| 81 | &engine, SLOT(setIgnoreSystemExclusiveEvents(bool))); |
| 82 | connect(&configureView, SIGNAL(ignoreTimeEventsChangeRequest(bool)), |
| 83 | &engine, SLOT(setIgnoreTimeEvents(bool))); |
| 84 | connect(&configureView, SIGNAL(inputPortChangeRequest(int)), |
| 85 | &engine, SLOT(setInputPort(int))); |
| 86 | connect(&configureView, SIGNAL(outputPortChangeRequest(int)), |
| 87 | &engine, SLOT(setOutputPort(int))); |
| 88 | |
| 89 | // Setup error view |
| 90 | connect(&errorView, SIGNAL(closeRequest()), |
| 91 | &errorView, SLOT(hide())); |
| 92 | |
| 93 | // Setup main view |
| 94 | mainView.setMessageSendEnabled((driver != -1) && (outputPort != -1)); |
| 95 | connect(&mainView, SIGNAL(aboutRequest()), |
| 96 | &aboutView, SLOT(show())); |
| 97 | connect(&mainView, SIGNAL(addMessageRequest()), |
| 98 | &messageView, SLOT(show())); |
| 99 | connect(&mainView, SIGNAL(clearMessagesRequest()), |
| 100 | &mainView, SLOT(clearMessages())); |
| 101 | connect(&mainView, SIGNAL(configureRequest()), |
| 102 | &configureView, SLOT(show())); |
| 103 | connect(&mainView, SIGNAL(closeRequest()), |
| 104 | &application, SLOT(quit())); |
| 105 | |
| 106 | // Setup message view |
| 107 | connect(&messageView, SIGNAL(closeRequest()), |
| 108 | &messageView, SLOT(hide())); |
| 109 | connect(&messageView, SIGNAL(sendRequest(const QString &)), |
| 110 | &messageView, SLOT(hide())); |
| 111 | connect(&messageView, SIGNAL(sendRequest(const QString &)), |
| 112 | SLOT(handleMessageSend(const QString &))); |
| 113 | |
| 114 | // Setup engine |
| 115 | connect(&engine, SIGNAL(messageReceived(quint64, const QByteArray &)), |
| 116 | SLOT(handleReceivedMessage(quint64, const QByteArray &))); |
| 117 | connect(&engine, SIGNAL(driverChanged(int)), |
| 118 | &configureView, SLOT(setDriver(int))); |
| 119 | connect(&engine, SIGNAL(driverChanged(int)), |
| 120 | SLOT(handleDriverChange())); |
| 121 | connect(&engine, SIGNAL(ignoreActiveSensingEventsChanged(bool)), |
| 122 | &configureView, SLOT(setIgnoreActiveSensingEvents(bool))); |
| 123 | connect(&engine, SIGNAL(ignoreSystemExclusiveEventsChanged(bool)), |
| 124 | &configureView, SLOT(setIgnoreSystemExclusiveEvents(bool))); |
| 125 | connect(&engine, SIGNAL(ignoreTimeEventsChanged(bool)), |
| 126 | &configureView, SLOT(setIgnoreTimeEvents(bool))); |
| 127 | connect(&engine, SIGNAL(inputPortAdded(int, QString)), |
| 128 | &configureView, SLOT(addInputPort(int, QString))); |
| 129 | connect(&engine, SIGNAL(inputPortChanged(int)), |
| 130 | &configureView, SLOT(setInputPort(int))); |
| 131 | connect(&engine, SIGNAL(inputPortChanged(int)), |
| 132 | SLOT(handleDriverChange())); |
| 133 | connect(&engine, SIGNAL(inputPortRemoved(int)), |
| 134 | &configureView, SLOT(removeInputPort(int))); |
| 135 | connect(&engine, SIGNAL(outputPortAdded(int, QString)), |
| 136 | &configureView, SLOT(addOutputPort(int, QString))); |
| 137 | connect(&engine, SIGNAL(outputPortChanged(int)), |
| 138 | &configureView, SLOT(setOutputPort(int))); |
| 139 | connect(&engine, SIGNAL(outputPortChanged(int)), |
| 140 | SLOT(handleDriverChange())); |
| 141 | connect(&engine, SIGNAL(outputPortRemoved(int)), |
| 142 | &configureView, SLOT(removeOutputPort(int))); |
| 143 | |
| 144 | // Setup application |
| 145 | connect(&application, SIGNAL(eventError(QString)), |
| 146 | &errorView, SLOT(setMessage(QString))); |
| 147 | connect(&application, SIGNAL(eventError(QString)), |
| 148 | &errorView, SLOT(show())); |
| 149 | } |
| 150 | |
| 151 | Controller::~Controller() |
| 152 | { |
| 153 | // Disconnect engine signals handled by the controller before the engine is |
| 154 | // deleted. |
| 155 | disconnect(&engine, SIGNAL(messageReceived(quint64, const QByteArray &)), |
| 156 | this, SLOT(handleReceivedMessage(quint64, const QByteArray &))); |
| 157 | disconnect(&engine, SIGNAL(driverChanged(int)), |
| 158 | this, SLOT(handleDriverChange())); |
| 159 | disconnect(&engine, SIGNAL(inputPortChanged(int)), |
| 160 | this, SLOT(handleDriverChange())); |
| 161 | disconnect(&engine, SIGNAL(outputPortChanged(int)), |
| 162 | this, SLOT(handleDriverChange())); |
| 163 | } |
| 164 | |
| 165 | QString |
| 166 | Controller::getGenericDataDescription(const QByteArray &message, int lastIndex) |
| 167 | { |
| 168 | assert((lastIndex >= -1) && (lastIndex < message.count())); |
| 169 | if (lastIndex == -1) { |
| 170 | lastIndex = message.count() - 1; |
| 171 | } |
| 172 | |
| 173 | QStringList dataParts; |
| 174 | for (int i = 1; i <= lastIndex; i++) { |
| 175 | dataParts += QString("%1"). |
| 176 | arg(static_cast<uint>(message[i]), 2, 16, QChar('0')); |
| 177 | } |
| 178 | dataParts += tr("(%1 bytes)").arg(lastIndex); |
| 179 | return dataParts.join(" "); |
| 180 | } |
| 181 | |
| 182 | void |
| 183 | Controller::handleDriverChange() |
| 184 | { |
| 185 | mainView.setMessageSendEnabled((engine.getDriver() != -1) && |
| 186 | (engine.getOutputPort() != -1)); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | Controller::handleMessageSend(const QString &message) |
| 191 | { |
| 192 | // Convert the message to bytes. |
| 193 | QStringList bytes = message.split(' ', QString::SkipEmptyParts); |
| 194 | int count = bytes.count(); |
| 195 | QByteArray msg; |
| 196 | for (int i = 0; i < count; i++) { |
| 197 | QString byteStr = bytes[i]; |
| 198 | bool success; |
| 199 | uint value = byteStr.toUInt(&success, 16); |
| 200 | if ((! success) || (value > 0xff)) { |
| 201 | showError(tr("'%1' is not a valid hexadecimal MIDI byte"). |
| 202 | arg(byteStr)); |
| 203 | return; |
| 204 | } |
| 205 | msg.append(static_cast<char>(static_cast<quint8>(value))); |
| 206 | } |
| 207 | |
| 208 | // Make sure the bytes represent a valid MIDI message. |
| 209 | parseMessage(msg); |
| 210 | if (! valid) { |
| 211 | showError(tr("The given message is not a valid MIDI message.")); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // Send the message. |
| 216 | quint64 timeStamp = engine.sendMessage(msg); |
| 217 | mainView.addSentMessage(timeStamp, statusDescription, dataDescription, |
| 218 | valid); |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | Controller::handleReceivedMessage(quint64 timeStamp, const QByteArray &message) |
| 223 | { |
| 224 | parseMessage(message); |
| 225 | mainView.addReceivedMessage(timeStamp, statusDescription, dataDescription, |
| 226 | valid); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | Controller::parseMessage(const QByteArray &message) |
| 231 | { |
| 232 | // Make sure we have an actual message. |
| 233 | int length = message.count(); |
| 234 | if (! length) { |
| 235 | dataDescription = ""; |
| 236 | statusDescription = tr("empty message"); |
| 237 | valid = false; |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | // Validate status byte. |
| 242 | quint8 status = static_cast<quint8>(message[0]); |
| 243 | if (status < 0x80) { |
| 244 | dataDescription = getGenericDataDescription(message); |
| 245 | statusDescription = tr("%1 (invalid status)"). |
| 246 | arg(static_cast<uint>(status), 2, 16, QChar('0')); |
| 247 | valid = false; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // Validate length and data. |
| 252 | QString errorMessage; |
| 253 | int expectedLength = statusLengths[status - 0x80]; |
| 254 | int lastDataIndex; |
| 255 | switch (expectedLength) { |
| 256 | |
| 257 | case -1: |
| 258 | dataDescription = getGenericDataDescription(message); |
| 259 | statusDescription = tr("%1 (undefined status)"). |
| 260 | arg(static_cast<uint>(status), 2, 16, QChar('0')); |
| 261 | valid = false; |
| 262 | return; |
| 263 | |
| 264 | case 0: |
| 265 | if (length == 1) { |
| 266 | dataDescription = ""; |
| 267 | statusDescription = tr("System Exclusive (no data)"); |
| 268 | valid = false; |
| 269 | return; |
| 270 | } |
| 271 | if (static_cast<quint8>(message[length - 1]) != 0xf7) { |
| 272 | dataDescription = getGenericDataDescription(message); |
| 273 | statusDescription = tr("System Exclusive (end not found)"); |
| 274 | valid = false; |
| 275 | return; |
| 276 | } |
| 277 | lastDataIndex = length - 2; |
| 278 | break; |
| 279 | |
| 280 | default: |
| 281 | if (length != expectedLength) { |
| 282 | dataDescription = getGenericDataDescription(message); |
| 283 | statusDescription = tr("%1 (incorrect length)"). |
| 284 | arg(static_cast<uint>(status), 2, 16, QChar('0')); |
| 285 | valid = false; |
| 286 | return; |
| 287 | } |
| 288 | lastDataIndex = length - 1; |
| 289 | } |
| 290 | |
| 291 | // Validate data bytes. |
| 292 | for (int i = 1; i <= lastDataIndex; i++) { |
| 293 | if (static_cast<quint8>(message[i]) >= 0x80) { |
| 294 | dataDescription = getGenericDataDescription(message); |
| 295 | statusDescription = tr("%1 (invalid data)"). |
| 296 | arg(static_cast<uint>(status), 2, 16, QChar('0')); |
| 297 | valid = false; |
| 298 | return; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // Convert message to user-friendly strings. |
| 303 | QString s; |
| 304 | int value; |
| 305 | valid = true; |
| 306 | switch (status & 0xf0) { |
| 307 | |
| 308 | case 0x80: |
| 309 | dataDescription = tr("Note: %1, Velocity: %2"). |
| 310 | arg(getMIDINoteString(static_cast<quint8>(message[1]))). |
| 311 | arg(static_cast<quint8>(message[2])); |
| 312 | statusDescription = tr("Note Off, Channel %1").arg((status & 0xf) + 1); |
| 313 | break; |
| 314 | |
| 315 | case 0x90: |
| 316 | dataDescription = tr("Note: %1, Velocity: %2"). |
| 317 | arg(getMIDINoteString(static_cast<quint8>(message[1]))). |
| 318 | arg(static_cast<quint8>(message[2])); |
| 319 | statusDescription = tr("Note On, Channel %1").arg((status & 0xf) + 1); |
| 320 | break; |
| 321 | |
| 322 | case 0xa0: |
| 323 | dataDescription = tr("Note: %1, Pressure: %2"). |
| 324 | arg(getMIDINoteString(static_cast<quint8>(message[1]))). |
| 325 | arg(static_cast<quint8>(message[2])); |
| 326 | statusDescription = tr("Aftertouch, Channel %1"). |
| 327 | arg((status & 0xf) + 1); |
| 328 | break; |
| 329 | |
| 330 | case 0xb0: |
| 331 | dataDescription = tr("Controller: %1, Value: %2"). |
| 332 | arg(getMIDIControlString(static_cast<quint8>(message[1]))). |
| 333 | arg(static_cast<quint8>(message[2])); |
| 334 | statusDescription = tr("Controller, Channel %1"). |
| 335 | arg((status & 0xf) + 1); |
| 336 | break; |
| 337 | |
| 338 | case 0xc0: |
| 339 | dataDescription = tr("Number: %1").arg(static_cast<quint8>(message[1])); |
| 340 | statusDescription = tr("Program Change, Channel %1"). |
| 341 | arg((status & 0xf) + 1); |
| 342 | break; |
| 343 | |
| 344 | case 0xd0: |
| 345 | dataDescription = tr("Pressure: %1"). |
| 346 | arg(static_cast<quint8>(message[1])); |
| 347 | statusDescription = tr("Channel Pressure, Channel %1"). |
| 348 | arg((status & 0xf) + 1); |
| 349 | break; |
| 350 | |
| 351 | case 0xe0: |
| 352 | dataDescription = tr("Value: %1"). |
| 353 | arg((((static_cast<qint16>(message[2])) << 7) | |
| 354 | (static_cast<qint16>(message[1]))) - 0x2000); |
| 355 | statusDescription = tr("Pitch Wheel, Channel %1"). |
| 356 | arg((status & 0xf) + 1); |
| 357 | break; |
| 358 | |
| 359 | case 0xf0: |
| 360 | switch (status & 0xf) { |
| 361 | |
| 362 | case 0x0: |
| 363 | dataDescription = getGenericDataDescription(message, lastDataIndex); |
| 364 | statusDescription = tr("System Exclusive"); |
| 365 | break; |
| 366 | |
| 367 | case 0x1: |
| 368 | value = message[0] & 0xf; |
| 369 | switch (message[0] & 0x70) { |
| 370 | |
| 371 | case 0x00: |
| 372 | dataDescription = tr("Frames Low Nibble: %1").arg(value); |
| 373 | break; |
| 374 | |
| 375 | case 0x10: |
| 376 | dataDescription = tr("Frames High Nibble: %1").arg(value); |
| 377 | break; |
| 378 | |
| 379 | case 0x20: |
| 380 | dataDescription = tr("Seconds Low Nibble: %1").arg(value); |
| 381 | break; |
| 382 | |
| 383 | case 0x30: |
| 384 | dataDescription = tr("Seconds High Nibble: %1").arg(value); |
| 385 | break; |
| 386 | |
| 387 | case 0x40: |
| 388 | dataDescription = tr("Minutes Low Nibble: %1").arg(value); |
| 389 | break; |
| 390 | |
| 391 | case 0x50: |
| 392 | dataDescription = tr("Minutes High Nibble: %1").arg(value); |
| 393 | break; |
| 394 | |
| 395 | case 0x60: |
| 396 | dataDescription = tr("Hours Low Nibble: %1").arg(value); |
| 397 | break; |
| 398 | |
| 399 | case 0x70: |
| 400 | switch ((value & 0x6) >> 1) { |
| 401 | |
| 402 | case 0: |
| 403 | s = tr("24 fps"); |
| 404 | break; |
| 405 | |
| 406 | case 1: |
| 407 | s = tr("25 fps"); |
| 408 | break; |
| 409 | |
| 410 | case 2: |
| 411 | s = tr("30 fps (drop-frame)"); |
| 412 | break; |
| 413 | |
| 414 | case 3: |
| 415 | s = tr("30 fps"); |
| 416 | break; |
| 417 | |
| 418 | default: |
| 419 | // We shouldn't get here. |
| 420 | assert(false); |
| 421 | } |
| 422 | dataDescription = tr("Hours High Nibble: %1, SMPTE Type: %2"). |
| 423 | arg(value & 1).arg(s); |
| 424 | break; |
| 425 | |
| 426 | default: |
| 427 | // We shouldn't get here. |
| 428 | assert(false); |
| 429 | } |
| 430 | statusDescription = tr("MTC Quarter Frame"); |
| 431 | break; |
| 432 | |
| 433 | case 0x2: |
| 434 | dataDescription = tr("MIDI Beat: %1"). |
| 435 | arg(((static_cast<qint16>(message[2])) << 7) | |
| 436 | (static_cast<qint16>(message[1]))); |
| 437 | statusDescription = tr("Song Position Pointer"); |
| 438 | break; |
| 439 | |
| 440 | case 0x3: |
| 441 | dataDescription = tr("Song Number: %1"). |
| 442 | arg(static_cast<quint8>(message[1])); |
| 443 | statusDescription = tr("Song Select"); |
| 444 | break; |
| 445 | |
| 446 | case 0x6: |
| 447 | dataDescription = ""; |
| 448 | statusDescription = tr("Tune Request"); |
| 449 | break; |
| 450 | |
| 451 | case 0x8: |
| 452 | dataDescription = ""; |
| 453 | statusDescription = tr("MIDI Clock"); |
| 454 | break; |
| 455 | |
| 456 | case 0x9: |
| 457 | dataDescription = ""; |
| 458 | statusDescription = tr("MIDI Tick"); |
| 459 | break; |
| 460 | |
| 461 | case 0xa: |
| 462 | dataDescription = ""; |
| 463 | statusDescription = tr("MIDI Start"); |
| 464 | break; |
| 465 | |
| 466 | case 0xb: |
| 467 | dataDescription = ""; |
| 468 | statusDescription = tr("MIDI Continue"); |
| 469 | break; |
| 470 | |
| 471 | case 0xc: |
| 472 | dataDescription = ""; |
| 473 | statusDescription = tr("MIDI Stop"); |
| 474 | break; |
| 475 | |
| 476 | case 0xe: |
| 477 | dataDescription = ""; |
| 478 | statusDescription = tr("Active Sense"); |
| 479 | break; |
| 480 | |
| 481 | case 0xf: |
| 482 | dataDescription = ""; |
| 483 | statusDescription = tr("Reset"); |
| 484 | break; |
| 485 | |
| 486 | default: |
| 487 | // We shouldn't get here. |
| 488 | assert(false); |
| 489 | } |
| 490 | break; |
| 491 | |
| 492 | default: |
| 493 | // We shouldn't get here. |
| 494 | assert(false); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void |
| 499 | Controller::run() |
| 500 | { |
| 501 | mainView.show(); |
| 502 | application.exec(); |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | Controller::showError(const QString &message) |
| 507 | { |
| 508 | errorView.setMessage(message); |
| 509 | errorView.show(); |
| 510 | } |