- first attempt to remove QT
[profile/ivi/audiomanager.git] / AudioManagerDaemon / HookEngine.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * AudioManangerDeamon
5  *
6  * \file HookEngine.cpp
7  *
8  * \date 20.05.2011
9  * \author Christian Müller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG – Christian Müller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  *
24  */
25
26 #include "HookEngine.h"
27
28 BaseHook::BaseHook() {
29 }
30
31 BaseHook::~BaseHook() {
32 }
33
34 HookHandler::HookHandler() {
35 }
36
37 HookHandler::~HookHandler() {
38         for (std::list<BaseHook*>::iterator hook = m_listofPlugins.begin(); hook != m_listofPlugins.end(); hook++) {
39                 (*hook)->DeinitHook();
40         }
41 }
42
43 void BaseHook::registerAudioManagerCore(AudioManagerCore* core) {
44         m_core = core;
45 }
46
47 genError_t BaseHook::registerHookEngine(HookHandler* engine) {
48         m_hookhandler = engine;
49         return GEN_OK;
50 }
51
52 genError_t HookHandler::registerHook(hookprio_t prio, genHook_t hookType, BaseHook* hookClass) {
53         prioList newEntry;
54         std::list<prioList>* list;
55
56         if (prio < 0 || prio > 100) {
57                 DLT_LOG( AudioManager, DLT_LOG_WARN, DLT_STRING("Register Hook: Priority out of range: "), DLT_INT(prio));
58                 return GEN_OUTOFRANGE;
59         }
60
61         newEntry.prio = prio;
62         newEntry.hook = hookClass;
63
64         switch (hookType) {
65         case HOOK_DOMAIN_REGISTER:
66                 list = &m_domainRegisterList;
67                 break;
68         case HOOK_DOMAIN_DEREGISTER:
69                 list = &m_domainDeregisterList;
70                 break;
71         case HOOK_SINK_REGISTER:
72                 list = &m_sinkRegisterList;
73                 break;
74         case HOOK_SINK_DEREGISTER:
75                 list = &m_sinkDeregisterList;
76                 break;
77         case HOOK_SOURCE_REGISTER:
78                 list = &m_sourceRegisterList;
79                 break;
80         case HOOK_SOURCE_DEREGISTER:
81                 list = &m_sourceDeregisterList;
82                 break;
83         case HOOK_GATEWAY_REGISTER:
84                 list = &m_gatewayRegisterList;
85                 break;
86         case HOOK_GATEWAY_DERGISTER:
87                 list = &m_gatewayDeregisterList;
88                 break;
89         case HOOK_ROUTING_REQUEST:
90                 list = &m_routingRequestList;
91                 break;
92         case HOOK_ROUTING_COMPLETE:
93                 list = &m_routingCompleteList;
94                 break;
95         case HOOK_SYSTEM_READY:
96                 list = &m_systemReadyList;
97                 break;
98         case HOOK_SYSTEM_DOWN:
99                 list = &m_systemDownList;
100                 break;
101         case HOOK_VOLUME_CHANGE:
102                 list = &m_volumeChangeList;
103                 break;
104         case HOOK_MUTE_SOURCE:
105                 list = &m_muteSourceList;
106                 break;
107         case HOOK_UNMUTE_SOURCE:
108                 list = &m_unmuteSourceList;
109                 break;
110         case HOOK_MUTE_SINK:
111                 list = &m_muteSinkList;
112                 break;
113         case HOOK_UNMUTE_SINK:
114                 list = &m_unmuteSinkList;
115                 break;
116         case HOOK_USER_CONNECTION_REQUEST:
117                 list = &m_userConnectionRequestList;
118                 break;
119         case HOOK_USER_DISCONNECTION_REQUEST:
120                 list = &m_userDisconnectionReuestList;
121                 break;
122         case HOOK_CONNECTION_REQUEST:
123                 list = &m_connectionRequestList;
124                 break;
125         case HOOK_DISCONNECTION_REQUEST:
126                 list = &m_disconnectionReuestList;
127                 break;
128         case HOOK_INTERRUPT_REQUEST:
129                 list = &m_interruptRequestList;
130                 break;
131         default:
132                 DLT_LOG(AudioManager, DLT_LOG_WARN, DLT_STRING("Trying to register unknown Hook "));
133                 return GEN_OUTOFRANGE;
134         }
135
136         int index = 0;
137         std::list<prioList>::iterator l;
138         for (l = list->begin(); l != list->end(); l++) {
139                 if (l->prio > prio) {
140                         index++;
141                 }
142         }
143         std::advance(l, index);
144         list->insert(l, newEntry);
145         //TODO test the sorting of the hooks with more than one plugin
146
147         return GEN_OK;
148 }
149
150 genError_t HookHandler::fireHookDomainRegister(char* Name, domain_t ID) {
151         for (std::list<prioList>::iterator hook = m_domainRegisterList.begin(); hook != m_domainDeregisterList.end(); hook++) {
152                 if (hook->hook->hookDomainRegister(Name, ID) == HOOK_STOP)
153                         break;
154         }
155         return GEN_OK;
156 }
157
158 genError_t HookHandler::fireHookDomainDeregister(domain_t ID) {
159         for (std::list<prioList>::iterator hook = m_domainDeregisterList.begin(); hook != m_domainDeregisterList.end(); hook++) {
160                 if (hook->hook->hookDomainDeregister(ID) == HOOK_STOP)
161                         break;
162         }
163         return GEN_OK;
164 }
165
166 genError_t HookHandler::fireHookSinkRegister(char* Name, sink_t ID) {
167         for (std::list<prioList>::iterator hook = m_sinkRegisterList.begin(); hook != m_sinkRegisterList.end(); hook++) {
168                 if (hook->hook->hookSinkRegister(Name, ID) == HOOK_STOP)
169                         break;
170         }
171         return GEN_OK;
172 }
173
174 genError_t HookHandler::fireHookSinkDeregister(sink_t ID) {
175         for (std::list<prioList>::iterator hook = m_sinkDeregisterList.begin(); hook != m_sinkDeregisterList.end(); hook++) {
176                 if (hook->hook->hookSinkDeregister(ID) == HOOK_STOP)
177                         break;
178         }
179         return GEN_OK;
180 }
181
182 genError_t HookHandler::fireHookSourceRegister(char* Name, source_t ID) {
183         for (std::list<prioList>::iterator hook = m_sourceRegisterList.begin(); hook != m_sourceRegisterList.end(); hook++) {
184                 if (hook->hook->hookSourceRegister(Name, ID) == HOOK_STOP)
185                         break;
186         }
187         return GEN_OK;
188 }
189
190 genError_t HookHandler::fireHookSourceDeregister(source_t ID) {
191         for (std::list<prioList>::iterator hook = m_sourceDeregisterList.begin(); hook != m_sourceDeregisterList.end(); hook++) {
192                 if (hook->hook->hookSourceDeregister(ID) == HOOK_STOP)
193                         break;
194         }
195         return GEN_OK;
196 }
197
198 genError_t HookHandler::fireHookGatewayRegister(char* Name, gateway_t ID) {
199         for (std::list<prioList>::iterator hook = m_gatewayRegisterList.begin(); hook != m_gatewayRegisterList.end(); hook++) {
200                 if (hook->hook->hookGatewayRegister(Name, ID) == HOOK_STOP)
201                         break;
202         }
203         return GEN_OK;
204 }
205
206 genError_t HookHandler::fireHookGatewayDeregister(gateway_t ID) {
207         for (std::list<prioList>::iterator hook = m_gatewayDeregisterList.begin(); hook != m_gatewayDeregisterList.end(); hook++) {
208                 if (hook->hook->hookGatewayDeregister(ID) == HOOK_STOP)
209                 break;
210         }
211         return GEN_OK;
212 }
213
214 genError_t HookHandler::fireHookSystemReady(void) {
215         for (std::list<prioList>::iterator hook = m_systemReadyList.begin(); hook != m_systemReadyList.end(); hook++) {
216                 if (hook->hook->hookSystemReady() == HOOK_STOP)
217                 break;
218         }
219         return GEN_OK;
220 }
221
222 genError_t HookHandler::fireHookSystemDown(void) {
223         for (std::list<prioList>::iterator hook = m_systemDownList.begin(); hook != m_systemDownList.end(); hook++) {
224                 if (hook->hook->hookSystemDown() == HOOK_STOP)
225                 break;
226         }
227         return GEN_OK;
228 }
229
230 genError_t HookHandler::fireHookConnectionRequest(source_t SourceID, sink_t SinkID) {
231         for (std::list<prioList>::iterator hook = m_connectionRequestList.begin(); hook != m_connectionRequestList.end(); hook++) {
232                 if (hook->hook->hookConnectionRequest(SourceID, SinkID) == HOOK_STOP)
233                 break;
234         }
235         return GEN_OK;
236 }
237
238 genError_t HookHandler::fireHookDisconnectionRequest(connection_t ID) {
239         for (std::list<prioList>::iterator hook = m_disconnectionReuestList.begin(); hook != m_disconnectionReuestList.end(); hook++) {
240                 if (hook->hook->hookDisconnectionRequest(ID) == HOOK_STOP)
241                 break;
242         }
243         return GEN_OK;
244 }
245
246 genError_t HookHandler::fireHookUserConnectionRequest(Queue* queue, source_t SourceID, sink_t SinkID) {
247         for (std::list<prioList>::iterator hook = m_userConnectionRequestList.begin(); hook != m_userConnectionRequestList.end(); hook++) {
248                 if (hook->hook->hookUserConnectionRequest(queue, SourceID, SinkID) == HOOK_STOP)
249                 break;
250         }
251         return GEN_OK;
252 }
253
254 genError_t HookHandler::fireHookUserDisconnectionRequest(Queue* queue, connection_t connID) {
255         for (std::list<prioList>::iterator hook = m_userDisconnectionReuestList.begin(); hook != m_userDisconnectionReuestList.end(); hook++) {
256                 if (hook->hook->hookUserDisconnectionRequest(queue, connID) == HOOK_STOP)
257                 break;
258         }
259         return GEN_OK;
260 }
261
262 genError_t HookHandler::fireHookRoutingRequest(bool onlyfree, source_t source, sink_t sink, std::list<genRoute_t>* ReturnList) {
263         for (std::list<prioList>::iterator hook = m_routingRequestList.begin(); hook != m_routingRequestList.end(); hook++) {
264                 if (hook->hook->hookRoutingRequest(onlyfree, source, sink, ReturnList) == HOOK_STOP)
265                 break;
266         }
267         return GEN_OK;
268 }
269
270 //todo change type
271 genError_t HookHandler::fireHookRoutingComplete(genRoute_t route) {
272         for (std::list<prioList>::iterator hook = m_routingCompleteList.begin(); hook != m_routingCompleteList.end(); hook++) {
273                 if (hook->hook->hookRoutingComplete(route) == HOOK_STOP)
274                 break;
275         }
276         return GEN_OK;
277 }
278
279 genError_t HookHandler::fireHookVolumeChange(volume_t newVolume, sink_t SinkID) {
280         for (std::list<prioList>::iterator hook = m_volumeChangeList.begin(); hook != m_volumeChangeList.end(); hook++) {
281                 if (hook->hook->hookVolumeChange(newVolume, SinkID) == HOOK_STOP)
282                 break;
283         }
284         return GEN_OK;
285 }
286
287 genError_t HookHandler::fireHookMuteSource(source_t ID) {
288         for (std::list<prioList>::iterator hook = m_muteSourceList.begin(); hook != m_muteSourceList.end(); hook++) {
289                 if (hook->hook->hookMuteSource(ID) == HOOK_STOP)
290                 break;
291         }
292         return GEN_OK;
293 }
294
295 genError_t HookHandler::fireHookUnmuteSource(source_t ID) {
296         for (std::list<prioList>::iterator hook = m_unmuteSourceList.begin(); hook != m_unmuteSourceList.end(); hook++) {
297                 if (hook->hook->hookUnmuteSource(ID) == HOOK_STOP)
298                 break;
299         }
300         return GEN_OK;
301 }
302
303 genError_t HookHandler::fireHookMuteSink(sink_t ID) {
304         for (std::list<prioList>::iterator hook = m_muteSinkList.begin(); hook != m_muteSinkList.end(); hook++) {
305                 if (hook->hook->hookMuteSink(ID) == HOOK_STOP)
306                 break;
307         }
308         return GEN_OK;
309 }
310
311 genError_t HookHandler::fireHookUnmuteSink(sink_t ID) {
312         for (std::list<prioList>::iterator hook = m_unmuteSinkList.begin(); hook != m_unmuteSinkList.end(); hook++) {
313                 if (hook->hook->hookUnmuteSink(ID) == HOOK_STOP)
314                 break;
315         }
316         return GEN_OK;
317 }
318
319 genError_t HookHandler::fireHookInterruptRequest(Queue* queue, source_t interruptSource, sink_t sink, genInt_t* interruptID) {
320         for (std::list<prioList>::iterator hook = m_interruptRequestList.begin(); hook != m_interruptRequestList.end(); hook++) {
321                 if (hook->hook->hookInterruptRequest(queue, interruptSource, sink, interruptID) == HOOK_STOP)
322                 break;
323         }
324         return GEN_OK;
325 }
326
327 void HookHandler::registerAudioManagerCore(AudioManagerCore* core) {
328         m_core = core;
329 }
330
331 void HookHandler::loadHookPlugins() {
332         BaseHook *b = NULL;
333 //      foreach(QObject * plugin, QPluginLoader::staticInstances())
334 //      {
335 //              HookPluginFactory* HookPluginFactory_ = qobject_cast<HookPluginFactory *>(plugin);
336 //              if (HookPluginFactory_) {
337 //                      b = HookPluginFactory_->returnInstance();
338 //                      b->registerHookEngine(this);
339 //                      b->registerAudioManagerCore(m_core);
340 //                      b->InitHook();
341 //                      char pName[40];
342 //                      if (b->returnPluginName(pName) == GEN_OK) {
343 //                              DLT_LOG( AudioManager, DLT_LOG_INFO, DLT_STRING("Registered Hook Plugin:"), DLT_STRING(pName));
344 //                              m_listofPlugins.append(b);
345 //                      }
346 //              }
347 //      }
348 }
349