aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_wire/service.cpp
blob: 80d7b71c9ee0ece63dd0574703f24592cc508c21 (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
#include "main.h"
#include "./service.h"
#include "api__ml_wire.h"
#include "./util.h"
#include "./resource.h"
#include "./externalCOM.h"

#include "../winamp/wa_ipc.h"
#include <strsafe.h>

#define IS_INVALIDISPATCH(__disp) (((IDispatch *)1) == (__disp) || NULL == (__disp))

OmService::OmService( UINT nId ) : id( nId )
{}

OmService::~OmService()
{
	Plugin_FreeResString( name );
	Plugin_FreeResString( url );
	Plugin_FreeResString( icon );
}


HRESULT OmService::CreateRemote( UINT nId, LPCWSTR pszName, LPCWSTR pszIcon, LPCWSTR pszUrl, OmService **instance )
{
	if ( instance == NULL )
		return E_POINTER;

	*instance = NULL;
	
	if ( nId == 0 || pszName == NULL )
		return E_INVALIDARG;

	OmService *service = new OmService( nId );
	if ( service == NULL )
		return E_OUTOFMEMORY;

	service->SetName( pszName );
	service->SetIcon( pszIcon );
	service->SetUrl( pszUrl );
    	
	*instance = service;

	return S_OK;
}

HRESULT OmService::CreateLocal( UINT nId, LPCWSTR pszName, LPCWSTR pszIcon, SVCWNDCREATEPROC windowCreator, OmService **instance )
{
	if ( instance == NULL )
		return E_POINTER;

	*instance = NULL;
	
	if ( nId == 0 || pszName == NULL )
		return E_INVALIDARG;

	OmService *service = new OmService( nId );
	if ( service == NULL )
		return E_OUTOFMEMORY;

	service->SetFlags( flagLocal, flagLocal );
	service->SetName( pszName );
	service->SetIcon( pszIcon );
	service->SetWindowCreator( windowCreator );
    	
	*instance = service;

	return S_OK;
}


size_t OmService::AddRef()
{
	return _ref.fetch_add( 1 );
}

size_t OmService::Release()
{
	if ( _ref.load() == 0 )
		return _ref.load();
	
	LONG r = _ref.fetch_sub( 1 );
	if ( r == 0 )
		delete( this );
	
	return r;
}


int OmService::QueryInterface( GUID interface_guid, void **object )
{
	if ( object == NULL )
		return E_POINTER;

	if ( IsEqualIID( interface_guid, IFC_OmService ) )
		*object = static_cast<ifc_omservice *>( this );
	else
	{
		*object = NULL;

		return E_NOINTERFACE;
	}

	if ( *object == NULL )
		return E_UNEXPECTED;

	AddRef();
	
	return S_OK;
}


unsigned int OmService::GetId()
{
	return id;
}

HRESULT OmService::GetName( wchar_t *pszBuffer, int cchBufferMax )
{
	return Plugin_CopyResString( pszBuffer, cchBufferMax, name );
}

HRESULT OmService::GetUrl( wchar_t *pszBuffer, int cchBufferMax )
{
	return Plugin_CopyResString( pszBuffer, cchBufferMax, url );
}

HRESULT OmService::GetIcon( wchar_t *pszBuffer, int cchBufferMax )
{
	if ( icon != NULL && IS_INTRESOURCE( icon ) )
	{
		WCHAR szPath[ 2 * MAX_PATH ] = { 0 };
		if ( GetModuleFileName( plugin.hDllInstance, szPath, ARRAYSIZE( szPath ) ) == 0 )
			return E_FAIL;

		return StringCchPrintf( pszBuffer, cchBufferMax, L"res://%s/#%d/#%d", szPath, RT_RCDATA, icon );
	}

	return StringCchCopyEx( pszBuffer, cchBufferMax, icon, NULL, NULL, STRSAFE_IGNORE_NULLS );
}

HRESULT OmService::GetExternal( IDispatch **ppDispatch )
{
	if ( ppDispatch == NULL )
		return E_POINTER;
	
	*ppDispatch = NULL;

	HWND hWinamp = plugin.hwndWinampParent;
	if ( hWinamp == NULL )
		return E_UNEXPECTED;
		
	//////*ppDispatch = (IDispatch*)SENDWAIPC(hWinamp, IPC_GET_DISPATCH_OBJECT, 0);

	WCHAR szBuffer[ 64 ] = { 0 };
	if ( SUCCEEDED( StringCchPrintfW( szBuffer, ARRAYSIZE( szBuffer ), L"%u", id ) ) )
		*ppDispatch = (IDispatch *) SENDWAIPC( hWinamp, IPC_JSAPI2_GET_DISPATCH_OBJECT, (WPARAM) szBuffer );


	if (IS_INVALIDISPATCH(*ppDispatch) && FAILED(ExternalCOM::CreateInstance((ExternalCOM**)ppDispatch)))
	{
		*ppDispatch = NULL;
		return E_FAIL;
	}
	

	return S_OK;
}


HRESULT OmService::SetName( LPCWSTR pszName )
{
	Plugin_FreeResString( name );
	name = Plugin_DuplicateResString( pszName );

	return S_OK;
}

HRESULT OmService::SetUrl( LPCWSTR pszUrl )
{
	Plugin_FreeResString( url );
	url = Plugin_DuplicateResString( pszUrl );

	return S_OK;
}

HRESULT OmService::SetIcon( LPCWSTR pszIcon )
{
	Plugin_FreeResString( icon );
	icon = Plugin_DuplicateResString( pszIcon );

	return S_OK;
}

void OmService::SetFlags( UINT mask, UINT newFlags )
{
	flags = ( flags & ~mask ) | ( mask & newFlags );
}

UINT OmService::GetFlags( void )
{
	return flags;
}


HRESULT OmService::SetWindowCreator( SVCWNDCREATEPROC proc )
{
	windowCreator = proc;

	return S_OK;
}

HRESULT OmService::GetWindowCreator( SVCWNDCREATEPROC *proc )
{
	if ( proc == NULL )
		return E_INVALIDARG;

	*proc = windowCreator;

	return S_OK;
}


HRESULT OmService::CreateView( HWND hParent, HWND *hView )
{
	if ( hView == NULL )
		return E_POINTER;

	*hView = NULL;
	HRESULT hr = S_OK;

	if ( ( flagLocal & flags ) != 0 )
	{
		if ( windowCreator != NULL )
		{
			*hView = windowCreator( hParent, this );
			if ( *hView == NULL )
				hr = E_FAIL;
		}
		else
			hr = E_INVALIDARG;
	}
	else
	{
		if ( OMBROWSERMNGR != NULL )
		{
			hr = OMBROWSERMNGR->Initialize( NULL, plugin.hwndWinampParent );
			if ( SUCCEEDED( hr ) )
				hr = OMBROWSERMNGR->CreateView( this, hParent, NULL, 0, hView );
		}
		else
			hr = E_UNEXPECTED;

	}

	return hr;
}


#define CBCLASS OmService
START_DISPATCH;
CB( ADDREF,          AddRef )
CB( RELEASE,         Release )
CB( QUERYINTERFACE,  QueryInterface )
CB( API_GETID,       GetId )
CB( API_GETNAME,     GetName )
CB( API_GETURL,      GetUrl )
CB( API_GETICON,     GetIcon )
CB( API_GETEXTERNAL, GetExternal )
END_DISPATCH;
#undef CBCLASS