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
|
#include "main.h"
#include "./service.h"
#include "./wasabi.h"
#include "./resource.h"
#include "../replicant/nu/Autowide.h"
#include "../winamp/wa_ipc.h"
#include <strsafe.h>
#define IS_INVALIDISPATCH(__disp) (((IDispatch *)1) == (__disp) || NULL == (__disp))
OmService::OmService(UINT nId)
: ref(1), id(nId), name(NULL), url(NULL), icon(NULL)
{
}
OmService::~OmService()
{
Plugin_FreeResString(name);
Plugin_FreeString(url);
Plugin_FreeResString(icon);
}
HRESULT OmService::CreateInstance(OmService **instance)
{
if (NULL == instance) return E_POINTER;
*instance = NULL;
OmService *service = new OmService(SERVICE_ID);
if (NULL == service) return E_OUTOFMEMORY;
wchar_t nowplayingurl[1024] = {0};
lstrcpynW(nowplayingurl, AutoWide(g_config->ReadString("nowplayingurl","")), ARRAYSIZE(nowplayingurl));
service->SetName(MAKEINTRESOURCE(IDS_SERVICE_NAME));
service->SetUrl((nowplayingurl[0] ? nowplayingurl : SERVICE_HOMEURL));
service->SetIcon(MAKEINTRESOURCE(IDR_SERVICE_ICON));
*instance = service;
return S_OK;
}
size_t OmService::AddRef()
{
return InterlockedIncrement((LONG*)&ref);
}
size_t OmService::Release()
{
if (0 == ref)
return ref;
LONG r = InterlockedDecrement((LONG*)&ref);
if (0 == r)
delete(this);
return r;
}
int OmService::QueryInterface(GUID interface_guid, void **object)
{
if (NULL == object) return E_POINTER;
if (IsEqualIID(interface_guid, IFC_OmService))
*object = static_cast<ifc_omservice*>(this);
else
{
*object = NULL;
return E_NOINTERFACE;
}
if (NULL == *object)
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 StringCchCopyEx(pszBuffer, cchBufferMax, url, NULL, NULL, STRSAFE_IGNORE_NULLS);
}
HRESULT OmService::GetIcon(wchar_t *pszBuffer, int cchBufferMax)
{
if (NULL != icon && IS_INTRESOURCE(icon))
{
WCHAR szPath[2*MAX_PATH] = {0};
if (0 == GetModuleFileName(Plugin_GetInstance(), szPath, ARRAYSIZE(szPath)))
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 (NULL == ppDispatch)
return E_POINTER;
*ppDispatch = NULL;
HWND hWinamp = Plugin_GetWinamp();
if (NULL == hWinamp)
return E_UNEXPECTED;
// So far we do not use JSAPI2 in nowplaying
// // try JSAPI2 first
// 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))
{ // try JSAPI1
*ppDispatch = (IDispatch*)SENDWAIPC(hWinamp, IPC_GET_DISPATCH_OBJECT, 0);
if (IS_INVALIDISPATCH(*ppDispatch))
{ // Fail
*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_FreeString(url);
url = Plugin_CopyString(pszUrl);
return S_OK;
}
HRESULT OmService::SetIcon(LPCWSTR pszIcon)
{
Plugin_FreeResString(icon);
icon = Plugin_DuplicateResString(pszIcon);
return S_OK;
}
#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
|