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 mConnectionState
== MIDIPortConnectionState::Closed
) {
36 MIDIPlatformService::Get()->Open(this);
41 mozilla::ipc::IPCResult
MIDIPortParent::RecvClose() {
42 if (mConnectionState
!= MIDIPortConnectionState::Closed
) {
43 if (MIDIPlatformService::IsRunning()) {
44 MIDIPlatformService::Get()->Close(this);
50 mozilla::ipc::IPCResult
MIDIPortParent::RecvClear() {
51 if (MIDIPlatformService::IsRunning()) {
52 MIDIPlatformService::Get()->Clear(this);
57 mozilla::ipc::IPCResult
MIDIPortParent::RecvShutdown() {
65 void MIDIPortParent::ActorDestroy(ActorDestroyReason
) {
66 mMessageQueue
.Clear();
67 MIDIPortInterface::Shutdown();
68 if (MIDIPlatformService::IsRunning()) {
69 MIDIPlatformService::Get()->RemovePort(this);
73 bool MIDIPortParent::SendUpdateStatus(
74 const MIDIPortDeviceState
& aDeviceState
,
75 const MIDIPortConnectionState
& aConnectionState
) {
79 mDeviceState
= aDeviceState
;
80 mConnectionState
= aConnectionState
;
81 if (aConnectionState
== MIDIPortConnectionState::Open
&&
82 aDeviceState
== MIDIPortDeviceState::Disconnected
) {
83 mConnectionState
= MIDIPortConnectionState::Pending
;
84 } else if (aConnectionState
== MIDIPortConnectionState::Open
&&
85 aDeviceState
== MIDIPortDeviceState::Connected
&&
86 !mMessageQueue
.IsEmpty()) {
87 if (MIDIPlatformService::IsRunning()) {
88 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId
,
91 mMessageQueue
.Clear();
93 return PMIDIPortParent::SendUpdateStatus(
94 static_cast<uint32_t>(mDeviceState
),
95 static_cast<uint32_t>(mConnectionState
));
98 MIDIPortParent::MIDIPortParent(const MIDIPortInfo
& aPortInfo
,
99 const bool aSysexEnabled
)
100 : MIDIPortInterface(aPortInfo
, aSysexEnabled
), mInternalId(++gId
) {
101 MOZ_ASSERT(MIDIPlatformService::IsRunning(),
102 "Shouldn't be able to add MIDI port without MIDI service!");
103 MIDIPlatformService::Get()->AddPort(this);
106 } // namespace mozilla::dom