2 * Copyright (c) 2002, Jerome Duval (jerome.duval@free.fr)
3 * Distributed under the terms of the MIT License.
7 #include "MultiAudioAddOn.h"
13 #include <Directory.h>
15 #include <FindDirectory.h>
20 #include "MultiAudioNode.h"
21 #include "MultiAudioDevice.h"
26 const char* kSettingsName
= "Media/multi_audio_settings";
29 //! instantiation function
30 extern "C" BMediaAddOn
*
31 make_media_addon(image_id image
)
34 return new MultiAudioAddOn(image
);
41 MultiAudioAddOn::MultiAudioAddOn(image_id image
)
46 fInitStatus
= _RecursiveScan("/dev/audio/hmulti/");
47 if (fInitStatus
!= B_OK
)
55 MultiAudioAddOn::~MultiAudioAddOn()
60 for (int32 i
= 0; (device
= fDevices
.ItemAt(i
)); i
++)
61 delete (MultiAudioDevice
*)device
;
68 MultiAudioAddOn::InitCheck(const char** _failureText
)
76 MultiAudioAddOn::CountFlavors()
79 return fDevices
.CountItems();
84 MultiAudioAddOn::GetFlavorAt(int32 index
, const flavor_info
** _info
)
88 MultiAudioDevice
* device
= (MultiAudioDevice
*)fDevices
.ItemAt(index
);
92 flavor_info
* info
= new (std::nothrow
) flavor_info
;
96 MultiAudioNode::GetFlavor(info
, index
);
97 info
->name
= (char*)device
->Description().friendly_name
;
105 MultiAudioAddOn::InstantiateNodeFor(const flavor_info
* info
, BMessage
* config
,
110 MultiAudioDevice
* device
= (MultiAudioDevice
*)fDevices
.ItemAt(
112 if (device
== NULL
) {
118 if (fSettings
.FindMessage(device
->Description().friendly_name
, config
)
120 fSettings
.RemoveData(device
->Description().friendly_name
);
124 MultiAudioNode
* node
= new (std::nothrow
) MultiAudioNode(this,
125 device
->Description().friendly_name
, device
, info
->internal_id
, config
);
127 *_error
= B_NO_MEMORY
;
129 *_error
= node
->InitCheck();
136 MultiAudioAddOn::GetConfigurationFor(BMediaNode
* _node
, BMessage
* message
)
139 MultiAudioNode
* node
= dynamic_cast<MultiAudioNode
*>(_node
);
144 if (message
== NULL
) {
146 if (node
->GetConfigurationFor(&settings
) == B_OK
) {
147 fSettings
.AddMessage(node
->Name(), &settings
);
153 // currently never called by the media kit. Seems it is not implemented.
155 return node
->GetConfigurationFor(message
);
160 MultiAudioAddOn::WantsAutoStart()
168 MultiAudioAddOn::AutoStart(int count
, BMediaNode
** _node
, int32
* _internalID
,
177 MultiAudioAddOn::_RecursiveScan(const char* rootPath
, BEntry
* rootEntry
, uint32 depth
)
184 if (rootEntry
!= NULL
)
185 root
.SetTo(rootEntry
);
186 else if (rootPath
!= NULL
)
187 root
.SetTo(rootPath
);
189 PRINT(("Error in MultiAudioAddOn::RecursiveScan() null params\n"));
194 while (root
.GetNextEntry(&entry
) == B_OK
) {
195 if (entry
.IsDirectory()) {
196 _RecursiveScan(rootPath
, &entry
, depth
+ 1);
199 entry
.GetPath(&path
);
200 MultiAudioDevice
*device
=
201 new(std::nothrow
) MultiAudioDevice(path
.Path()
202 + strlen(rootPath
), path
.Path());
204 if (device
->InitCheck() == B_OK
)
205 fDevices
.AddItem(device
);
217 MultiAudioAddOn::_SaveSettings()
221 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) != B_OK
)
224 path
.Append(kSettingsName
);
226 BFile
file(path
.Path(), B_READ_WRITE
| B_CREATE_FILE
| B_ERASE_FILE
);
227 if (file
.InitCheck() == B_OK
)
228 fSettings
.Flatten(&file
);
233 MultiAudioAddOn::_LoadSettings()
236 fSettings
.MakeEmpty();
239 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) != B_OK
)
242 path
.Append(kSettingsName
);
244 BFile
file(path
.Path(), B_READ_ONLY
);
245 if (file
.InitCheck() == B_OK
&& fSettings
.Unflatten(&file
) == B_OK
) {
246 PRINT_OBJECT(fSettings
);
248 PRINT(("Error unflattening settings file %s\n", path
.Path()));