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/. */
7 #include "mozilla/dom/MIDIPortParent.h"
8 #include "mozilla/dom/MIDIPlatformService.h"
9 #include "nsContentUtils.h"
12 namespace mozilla::dom
{
14 // Keep an internal ID that we can use for passing information about specific
15 // MIDI ports back and forth to the Rust libraries.
16 static uint32_t gId
= 0;
18 mozilla::ipc::IPCResult
MIDIPortParent::RecvSend(
19 nsTArray
<MIDIMessage
>&& aMsgs
) {
20 if (mConnectionState
!= MIDIPortConnectionState::Open
) {
21 mMessageQueue
.AppendElements(aMsgs
);
22 if (MIDIPlatformService::IsRunning()) {
23 MIDIPlatformService::Get()->Open(this);
27 if (MIDIPlatformService::IsRunning()) {
28 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId
, aMsgs
);
33 mozilla::ipc::IPCResult
MIDIPortParent::RecvOpen() {
34 if (MIDIPlatformService::IsRunning()) {
35 MIDIPlatformService::Get()->Open(this);
40 mozilla::ipc::IPCResult
MIDIPortParent::RecvClose() {
41 if (mConnectionState
!= MIDIPortConnectionState::Closed
) {
42 if (MIDIPlatformService::IsRunning()) {
43 MIDIPlatformService::Get()->Close(this);
49 mozilla::ipc::IPCResult
MIDIPortParent::RecvClear() {
50 if (MIDIPlatformService::IsRunning()) {
51 MIDIPlatformService::Get()->Clear(this);
56 mozilla::ipc::IPCResult
MIDIPortParent::RecvShutdown() {
61 Unused
<< Send__delete__(this);
65 void MIDIPortParent::Teardown() {
66 mMessageQueue
.Clear();
67 MIDIPortInterface::Shutdown();
68 if (MIDIPlatformService::IsRunning()) {
69 MIDIPlatformService::Get()->RemovePort(this);
73 void MIDIPortParent::ActorDestroy(ActorDestroyReason
) {}
75 bool MIDIPortParent::SendUpdateStatus(
76 const MIDIPortDeviceState
& aDeviceState
,
77 const MIDIPortConnectionState
& aConnectionState
) {
81 mDeviceState
= aDeviceState
;
82 mConnectionState
= aConnectionState
;
83 if (aConnectionState
== MIDIPortConnectionState::Open
&&
84 aDeviceState
== MIDIPortDeviceState::Disconnected
) {
85 mConnectionState
= MIDIPortConnectionState::Pending
;
86 } else if (aConnectionState
== MIDIPortConnectionState::Open
&&
87 aDeviceState
== MIDIPortDeviceState::Connected
&&
88 !mMessageQueue
.IsEmpty()) {
89 if (MIDIPlatformService::IsRunning()) {
90 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId
,
93 mMessageQueue
.Clear();
95 return PMIDIPortParent::SendUpdateStatus(
96 static_cast<uint32_t>(mDeviceState
),
97 static_cast<uint32_t>(mConnectionState
));
100 MIDIPortParent::MIDIPortParent(const MIDIPortInfo
& aPortInfo
,
101 const bool aSysexEnabled
)
102 : MIDIPortInterface(aPortInfo
, aSysexEnabled
), mInternalId(++gId
) {
103 MOZ_ASSERT(MIDIPlatformService::IsRunning(),
104 "Shouldn't be able to add MIDI port without MIDI service!");
105 MIDIPlatformService::Get()->AddPort(this);
108 } // namespace mozilla::dom