aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/ServiceManager.cpp
blob: 5206057e2b7ebe014bd9b31fc5d505d5e69afb42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/** (c) Nullsoft, Inc.         C O N F I D E N T I A L
 ** Filename: 
 ** Project:
 ** Description:
 ** Author: Ben Allison benski@nullsoft.com
 ** Created:
 **/
#include "main.h"
#include "ServiceManager.h"
#include <api.h>
#include <api/service/waservicefactory.h>
#include "../nu/AutoLock.h"
#include <api/syscb/callbacks/syscb.h>
#include <api/syscb/callbacks/svccb.h>

using namespace Nullsoft::Utility;


struct Counter
{
	Counter( waServiceFactory *_owner, void *_ptr ) : ptr( _ptr ), owner( _owner ) {}

	void             *ptr;
	waServiceFactory *owner;
};

ServiceManager::ServiceManager() : serviceGuard(512)
{}

int ServiceManager::service_register( waServiceFactory *svc )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_register" ) );
	FOURCC service_type = svc->getServiceType();

	// add the service to the master list
	services.push_back( svc );

	// add it to the by-type lookup
	ServiceList *&type_list = services_by_type[ service_type ];
	if ( !type_list )
		type_list = new ServiceList;

	type_list->push_back( svc );

	// send notifications
	svc->serviceNotify( SvcNotify::ONREGISTERED );
	WASABI_API_SYSCB->syscb_issueCallback( SysCallback::SERVICE, SvcCallback::ONREGISTER, (intptr_t)service_type, reinterpret_cast<intptr_t>( svc ) );

	return 1;
}

int ServiceManager::service_deregister( waServiceFactory *svc )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_deregister" ) );
	FOURCC service_type = svc->getServiceType();

	// remove it from the master list
	//services.eraseObject(svc);
	auto it = std::find( services.begin(), services.end(), svc );
	if ( it != services.end() )
		services.erase( it );

	// and from the type lookup map
	ServiceList *type_list = services_by_type[ service_type ];
	if ( type_list )
	{
		//type_list->eraseObject(svc);
		auto it = std::find( type_list->begin(), type_list->end(), svc );
		if ( it != type_list->end() )
			type_list->erase( it );
	}

	WASABI_API_SYSCB->syscb_issueCallback( SysCallback::SERVICE, SvcCallback::ONDEREGISTER, (intptr_t)service_type, reinterpret_cast<intptr_t>( svc ) );
	svc->serviceNotify( SvcNotify::ONDEREGISTERED );

	return 1;
}

size_t ServiceManager::service_getNumServices( FOURCC svc_type )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_getNumServices" ) );
	if ( svc_type )
	{
		ServiceList *type_list = services_by_type[ svc_type ];
		if ( type_list )
			return type_list->size();
		else
			return 0;
	}
	else
		return services.size();
}

waServiceFactory *ServiceManager::service_enumService( FOURCC svc_type, size_t n )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_enumService" ) );
	ServiceList *type_list = 0;
	if ( svc_type )
		type_list = services_by_type[ svc_type ];
	else
		type_list = &services;

	if ( type_list && n < type_list->size() )
		return type_list->at( n );
	else
		return 0;
}

waServiceFactory *ServiceManager::service_getServiceByGuid( GUID guid )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_getServiceByGuid" ) );
	for ( waServiceFactory *l_service : services )
	{
		if ( l_service->getGuid() == guid )
			return l_service;
	}

	return NULL;
}

int ServiceManager::service_lock( waServiceFactory *owner, void *svcptr )
{
	if ( owner == NULL || svcptr == NULL )
		return 0;

	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_lock" ) );
	locks.push_back( new Counter( owner, svcptr ) );

	return 1;
}

int ServiceManager::service_clientLock( void *svcptr )
{
	return 1;
}

int ServiceManager::service_release( void *svcptr )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_release" ) );
	for ( size_t i = 0; i != locks.size(); i++ )
	{
		if ( locks[ i ]->ptr == svcptr )
		{
			void             *ptr   = locks[ i ]->ptr;
			waServiceFactory *owner = locks[ i ]->owner;

			delete locks[ i ];

			locks.erase( locks.begin() + i );
			owner->releaseInterface( ptr );

			return 1;
		}
	}

	return 0;
}

const char *ServiceManager::service_getTypeName( FOURCC svc_type )
{
	return NULL;
}

int ServiceManager::service_unlock( void *svcptr )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_unlock" ) );
	for ( size_t i = 0; i != locks.size(); i++ )
	{
		if ( locks[ i ]->ptr == svcptr )
		{
			delete locks[ i ];
			locks.erase( locks.begin() + i );

			return 1;
		}
	}

	return 0;
}

int ServiceManager::service_isvalid( FOURCC svctype, waServiceFactory *service )
{
	AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_isvalid" ) );
	//return !!services.contains(service);
	return ( services.end() != std::find( services.begin(), services.end(), service ) );
}

int ServiceManager::service_compactDuplicates( waServiceFactory *me )
{
	// first, find 'me'
	GUID guid = me->getGuid();

	size_t me_index = 0;

	//bool found = services.findItem(me, &me_index);
	//if (!found)
	//	return 1;
	if ( services.end() == std::find( services.begin(), services.end(), me ) )
		return 1;

	ServiceList *type_list = services_by_type[ me->getServiceType() ];

	// go in reverse order because service to compact is likely at the end
	size_t n = services.size();
	while ( n-- ) // n is guaranteed to be >0 because otherwise the 'find' loop above would have failed
	{
		waServiceFactory *n_service = services[ n ];
		if ( n != me_index && n_service != me && n_service->getGuid() == guid )
		{
			services[ me_index ] = n_service;
			services.erase( services.begin() + n );

			// fix up our by-type cache
			if ( type_list )
			{
				size_t me_index = 0;
				size_t n_index = 0;

				//----------------------------------------------------
				// Replaced with code below this commented code
				//if (type_list->findItem_reverse(n_service, &n_index) && type_list->findItem(me, &me_index))
				//{
				//	type_list->at(me_index) = n_service;
				//	type_list->eraseindex(n_index);
				//}

				bool foundService = false;
				for ( int idx = type_list->size() - 1; idx >= 0; idx-- )
				{
					if ( type_list->at( idx ) == n_service )
					{
						foundService = true;
						n_index = idx;
						break;
					}
				}

				bool foundMe = false;
				for ( int idx2 = 0; idx2 < type_list->size(); idx2++ )
				{
					if ( type_list->at( idx2 ) == me )
					{
						foundMe = true;
						me_index = idx2;
						break;
					}
				}

				if ( foundService && foundMe )
				{
					type_list->at( me_index ) = n_service;
					type_list->erase( type_list->begin() + n_index );
				}
				//--------------------------------------------------
			}

			me->serviceNotify( SvcNotify::ONDEREGISTERED );

			return 0;
		}
	}

	return 1;
}

#ifdef CBCLASS
#undef CBCLASS
#endif

#define CBCLASS ServiceManager
START_DISPATCH;
CB( API_SERVICE_SERVICE_REGISTER,         service_register );
CB( API_SERVICE_SERVICE_DEREGISTER,       service_deregister );
CB( API_SERVICE_SERVICE_GETNUMSERVICES,   service_getNumServices );
CB( API_SERVICE_SERVICE_ENUMSERVICE,      service_enumService );
CB( API_SERVICE_SERVICE_GETSERVICEBYGUID, service_getServiceByGuid );
CB( API_SERVICE_SERVICE_LOCK,             service_lock );
CB( API_SERVICE_SERVICE_CLIENTLOCK,       service_clientLock );
CB( API_SERVICE_SERVICE_RELEASE,          service_release );
CB( API_SERVICE_SERVICE_GETTYPENAME,      service_getTypeName );
CB( API_SERVICE_SERVICE_UNLOCK,           service_unlock );
CB( API_SERVICE_ISVALID,                  service_isvalid );
CB( API_SERVICE_COMPACT_DUPLICATES,       service_compactDuplicates );
END_DISPATCH;
#undef CBCLASS