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
|
//#define PLUGIN_NAME "Nullsoft ActiveSync Plug-in"
#define PLUGIN_VERSION L"0.25"
#include "ASDevice.h"
int init();
void quit();
intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3);
extern PMPDevicePlugin plugin = {PMPHDR_VER,0,init,quit,MessageProc};
static HANDLE killEvent=0, hThread=0;
static DWORD WINAPI ThreadFunc(LPVOID lpParam);
IRAPIDesktop *pIRapiDesktop = NULL;
// wasabi based services for localisation support
api_language *WASABI_API_LNG = 0;
HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
std::vector<ASDevice*> devices;
std::vector<ejectedDevice*> ejected;
static RAPIDEVICEID lastDevId;
class MyRAPISink : public IRAPISink {
public:
virtual HRESULT STDMETHODCALLTYPE OnDeviceConnected(IRAPIDevice *pIDevice) {
RAPI_DEVICEINFO devInfo;
if(!SUCCEEDED(pIDevice->GetDeviceInfo(&devInfo))) return S_OK;
SysFreeString(devInfo.bstrName);
SysFreeString(devInfo.bstrPlatform);
EnterCriticalSection(&cs);
lastDevId = devInfo.DeviceId;
for(unsigned int i=0; i<ejected.size(); i++) {
if(devInfo.DeviceId == ejected[i]->id) {
ejected[i]->marked=true;
LeaveCriticalSection(&cs);
return S_OK;
}
}
for(unsigned int i=0; i<devices.size(); i++) {
if(devInfo.DeviceId == devices[i]->devInfo.DeviceId || devices[i]->pIDevice == pIDevice) {
LeaveCriticalSection(&cs);
return S_OK;
}
}
IRAPISession *pISession = NULL;
if (SUCCEEDED(pIDevice->CreateSession(&pISession))) {
if (SUCCEEDED(pISession->CeRapiInit())) devices.push_back(new ASDevice(pIDevice,pISession));
else pISession->Release();
}
LeaveCriticalSection(&cs);
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnDeviceDisconnected(IRAPIDevice *pIDevice) {
EnterCriticalSection(&cs);
RAPI_DEVICEINFO devInfo;
if(!SUCCEEDED(pIDevice->GetDeviceInfo(&devInfo))) return S_OK;
for(unsigned int i=0; i<devices.size(); i++) {
if(devInfo.DeviceId == devices[i]->devInfo.DeviceId || devices[i]->pIDevice == pIDevice) {
SendMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(intptr_t)devices[i],PMP_IPC_DEVICEDISCONNECTED);
delete devices[i];
}
}
SysFreeString(devInfo.bstrName);
SysFreeString(devInfo.bstrPlatform);
LeaveCriticalSection(&cs);
return S_OK;
}
DWORD RAPISinkContext;
CRITICAL_SECTION cs;
ULONG refs;
MyRAPISink() {refs=1; InitializeCriticalSection(&cs);}
~MyRAPISink() { DeleteCriticalSection(&cs); }
#define IMPLEMENTS(ifc) if (riid == IID_ ## ifc) { ++refs; *ppvObject = static_cast<ifc *>(this); return S_OK; }
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,void __RPC_FAR *__RPC_FAR *ppvObject) {
IMPLEMENTS(IRAPISink);
IMPLEMENTS(IUnknown);
*ppvObject = NULL;
return E_NOINTERFACE;
}
virtual ULONG STDMETHODCALLTYPE AddRef() { return ++refs; }
virtual ULONG STDMETHODCALLTYPE Release() { int x = --refs; if(!x) delete this; return x; }
#undef IMPLEMENTS
};
MyRAPISink *pMyRapiSink=NULL;
int init() {
CoInitializeEx(NULL, COINIT_MULTITHREADED);
HRESULT hr = CoCreateInstance(CLSID_RAPI,NULL,CLSCTX_INPROC_SERVER,IID_IRAPIDesktop,(void**)&pIRapiDesktop);
if(!SUCCEEDED(hr) || !pIRapiDesktop) return -1; // no activesync on this computer!
// loader so that we can get the localisation service api for use
waServiceFactory *sf = plugin.service->service_getServiceByGuid(languageApiGUID);
if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());
// need to have this initialised before we try to do anything with localisation features
WASABI_API_START_LANG(plugin.hDllInstance,PmpACTIVESYNCLangGUID);
static wchar_t szDescription[256];
swprintf(szDescription, ARRAYSIZE(szDescription),
WASABI_API_LNGSTRINGW(IDS_NULLSOFT_ACTIVESYNC_PLUGIN), PLUGIN_VERSION);
plugin.description = szDescription;
killEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
DWORD dwThreadId;
hThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, &dwThreadId);
return 0;
}
static void enumDevices() {
// find all the currently connected devices
IRAPIEnumDevices* pIRapiEnumDevices = NULL;
HRESULT hr = pIRapiDesktop->EnumDevices(&pIRapiEnumDevices);
for (unsigned int i = 0; i < ejected.size(); i++) ejected[i]->marked = false;
while (SUCCEEDED(hr) && pIRapiEnumDevices) {
IRAPIDevice* pIRapiDevice = NULL;
hr = pIRapiEnumDevices->Next(&pIRapiDevice);
if (SUCCEEDED(hr) && pIRapiDevice) {
pMyRapiSink->OnDeviceConnected(pIRapiDevice);
pIRapiDevice->Release();
}
else {
pIRapiEnumDevices->Release();
pIRapiEnumDevices = NULL;
}
}
//for (unsigned int i = 0; i < ejected.size(); i++)
//{
// if (!ejected[i]->marked)
// {
// free(ejected[i]);
// ejected.eraseindex(i);
// }
//}
auto it = ejected.begin();
while (it != ejected.end())
{
ejectedDevice* dev = *it;
if (!dev->marked)
{
free(dev);
it = ejected.erase(it);
}
else
{
it++;
}
}
}
static void init2() {
// set up device connection/disconnection notifications
pMyRapiSink = new MyRAPISink();
pIRapiDesktop->Advise(pMyRapiSink,(DWORD_PTR*)&pMyRapiSink->RAPISinkContext);
// find currently attached devices
enumDevices();
}
void quit() {
SetEvent(killEvent);
if (hThread) {
for(;;) {
int val = WaitForSingleObjectEx(hThread,15000,TRUE);
if(val == WAIT_OBJECT_0) { CloseHandle(hThread); break; }
else if(val == WAIT_TIMEOUT) { TerminateThread(hThread, 0); break; }
else continue;
}
}
CloseHandle(killEvent);
pIRapiDesktop->UnAdvise(pMyRapiSink->RAPISinkContext);
pIRapiDesktop->Release();
pMyRapiSink->Release();
CoUninitialize();
for(unsigned int i=0; i<ejected.size(); i++) free(ejected[i]);
}
static DWORD WINAPI ThreadFunc(LPVOID lpParam) {
CoInitializeEx(0,COINIT_MULTITHREADED);
init2();
while (WaitForSingleObjectEx(killEvent,5000,TRUE) != WAIT_OBJECT_0) {
// FUCKO: For some reason I'm not getting the device connected notifications, so lets just enum for devices on a regular basis.
enumDevices();
}
CoUninitialize();
return 0;
}
intptr_t MessageProc(int msg, intptr_t param1, intptr_t param2, intptr_t param3) {
if (msg == PMP_NO_CONFIG)
return TRUE;
return FALSE;
}
extern "C" {
__declspec( dllexport ) PMPDevicePlugin * winampGetPMPDevicePlugin(){return &plugin;}
__declspec( dllexport ) int winampUninstallPlugin(HINSTANCE hDllInst, HWND hwndDlg, int param) {
int i = (int)devices.size();
while(i-- > 0) devices[i]->Close();
return PMP_PLUGIN_UNINSTALL_NOW;
}
};
|