MIDIPortParent.cpp (3424B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/MIDIPortParent.h" 8 9 #include "mozilla/dom/MIDIPlatformService.h" 10 #include "nsContentUtils.h" 11 12 // C++ file contents 13 namespace mozilla::dom { 14 15 // Keep an internal ID that we can use for passing information about specific 16 // MIDI ports back and forth to the Rust libraries. 17 static uint32_t gId = 0; 18 19 mozilla::ipc::IPCResult MIDIPortParent::RecvSend( 20 nsTArray<MIDIMessage>&& aMsgs) { 21 if (mConnectionState != MIDIPortConnectionState::Open) { 22 mMessageQueue.AppendElements(aMsgs); 23 if (MIDIPlatformService::IsRunning()) { 24 MIDIPlatformService::Get()->Open(this); 25 } 26 return IPC_OK(); 27 } 28 if (MIDIPlatformService::IsRunning()) { 29 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId, aMsgs); 30 } 31 return IPC_OK(); 32 } 33 34 mozilla::ipc::IPCResult MIDIPortParent::RecvOpen() { 35 if (MIDIPlatformService::IsRunning() && 36 mConnectionState == MIDIPortConnectionState::Closed) { 37 MIDIPlatformService::Get()->Open(this); 38 } 39 return IPC_OK(); 40 } 41 42 mozilla::ipc::IPCResult MIDIPortParent::RecvClose() { 43 if (mConnectionState != MIDIPortConnectionState::Closed) { 44 if (MIDIPlatformService::IsRunning()) { 45 MIDIPlatformService::Get()->Close(this); 46 } 47 } 48 return IPC_OK(); 49 } 50 51 mozilla::ipc::IPCResult MIDIPortParent::RecvClear() { 52 if (MIDIPlatformService::IsRunning()) { 53 MIDIPlatformService::Get()->Clear(this); 54 } 55 return IPC_OK(); 56 } 57 58 mozilla::ipc::IPCResult MIDIPortParent::RecvShutdown() { 59 if (mShuttingDown) { 60 return IPC_OK(); 61 } 62 Close(); 63 return IPC_OK(); 64 } 65 66 void MIDIPortParent::ActorDestroy(ActorDestroyReason) { 67 mMessageQueue.Clear(); 68 MIDIPortInterface::Shutdown(); 69 if (MIDIPlatformService::IsRunning()) { 70 MIDIPlatformService::Get()->RemovePort(this); 71 } 72 } 73 74 bool MIDIPortParent::SendUpdateStatus( 75 const MIDIPortDeviceState& aDeviceState, 76 const MIDIPortConnectionState& aConnectionState) { 77 if (mShuttingDown) { 78 return true; 79 } 80 mDeviceState = aDeviceState; 81 mConnectionState = aConnectionState; 82 if (aConnectionState == MIDIPortConnectionState::Open && 83 aDeviceState == MIDIPortDeviceState::Disconnected) { 84 mConnectionState = MIDIPortConnectionState::Pending; 85 } else if (aConnectionState == MIDIPortConnectionState::Open && 86 aDeviceState == MIDIPortDeviceState::Connected && 87 !mMessageQueue.IsEmpty()) { 88 if (MIDIPlatformService::IsRunning()) { 89 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId, 90 mMessageQueue); 91 } 92 mMessageQueue.Clear(); 93 } 94 return PMIDIPortParent::SendUpdateStatus( 95 static_cast<uint32_t>(mDeviceState), 96 static_cast<uint32_t>(mConnectionState)); 97 } 98 99 MIDIPortParent::MIDIPortParent(const MIDIPortInfo& aPortInfo, 100 const bool aSysexEnabled) 101 : MIDIPortInterface(aPortInfo, aSysexEnabled), mInternalId(++gId) { 102 MOZ_ASSERT(MIDIPlatformService::IsRunning(), 103 "Shouldn't be able to add MIDI port without MIDI service!"); 104 MIDIPlatformService::Get()->AddPort(this); 105 } 106 107 } // namespace mozilla::dom