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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
#include "main.h"
#include "DeviceView.h"
#include "api__ml_pmp.h"
#include "../nu/refcount.h"
#include "..\..\General\gen_ml/ml_ipc_0313.h"
#include "DeviceCommands.h"
#include "resource1.h"
#include <strsafe.h>
// known commands
static const char *DEVICE_CMD_VIEW_OPEN = "view_open";
static const char *DEVICE_CMD_SYNC = "sync";
static const char *DEVICE_CMD_AUTOFILL = "autofill";
static const char *DEVICE_CMD_PLAYLIST_CREATE = "playlist_create";
static const char *DEVICE_CMD_RENAME = "rename";
static const char *DEVICE_CMD_PREFERENCES = "preferences";
static const char *DEVICE_CMD_EJECT = "eject";
static const char *DEVICE_CMD_REMOVE = "remove";
static const char *DEVICE_CMD_TRANSFER = "transfer";
static const char *DEVICE_CMD_HIDE = "hide";
extern void UpdateDevicesListView(bool softUpdate);
// we're going to share the command enum stuff for all devices
class PortableDeviceType : public ifc_devicetype
{
public:
PortableDeviceType()
{
}
const char *GetName()
{
return "portable";
}
HRESULT GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
{
return E_NOTIMPL;
}
HRESULT GetDisplayName(wchar_t *buffer, size_t bufferSize)
{
if (NULL == buffer)
return E_POINTER;
WASABI_API_LNGSTRINGW_BUF(IDS_PORTABLE_DEVICE_TYPE, buffer, bufferSize);
return S_OK;
}
protected:
#define CBCLASS PortableDeviceType
START_DISPATCH_INLINE;
CB(API_GETNAME, GetName);
CB(API_GETICON, GetIcon);
CB(API_GETDISPLAYNAME, GetDisplayName);
END_DISPATCH;
#undef CBCLASS
};
class USBDeviceConnection : public ifc_deviceconnection
{
public:
USBDeviceConnection()
{
}
const char *GetName()
{
return "usb";
}
HRESULT GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
{
if (FALSE == FormatResProtocol(MAKEINTRESOURCE(IDB_USB),
L"PNG", buffer, bufferSize))
{
return E_FAIL;
}
return S_OK;
}
HRESULT GetDisplayName(wchar_t *buffer, size_t bufferSize)
{
if (NULL == buffer)
return E_POINTER;
WASABI_API_LNGSTRINGW_BUF(IDS_DEVICE_CONNECTION_USB, buffer, bufferSize);
return S_OK;
}
protected:
#define CBCLASS USBDeviceConnection
START_DISPATCH_INLINE;
CB(API_GETNAME, GetName);
CB(API_GETICON, GetIcon);
CB(API_GETDISPLAYNAME, GetDisplayName);
END_DISPATCH;
#undef CBCLASS
};
static PortableDeviceType portable_device_type;
static USBDeviceConnection usb_connection;
static PortableCommand registered_commands[] =
{
PortableCommand(DEVICE_CMD_VIEW_OPEN, IDS_DEVICE_CMD_VIEW_OPEN, IDS_DEVICE_CMD_VIEW_OPEN_DESC),
PortableCommand(DEVICE_CMD_SYNC, IDS_DEVICE_CMD_SYNC, IDS_DEVICE_CMD_SYNC_DESC),
PortableCommand(DEVICE_CMD_TRANSFER, IDS_DEVICE_CMD_TRANSFER, IDS_DEVICE_CMD_TRANSFER_DESC),
PortableCommand(DEVICE_CMD_EJECT, IDS_DEVICE_CMD_EJECT, IDS_DEVICE_CMD_EJECT_DESC),
PortableCommand(DEVICE_CMD_REMOVE, IDS_DEVICE_CMD_REMOVE, IDS_DEVICE_CMD_REMOVE_DESC),
PortableCommand(DEVICE_CMD_RENAME, IDS_DEVICE_CMD_RENAME, IDS_DEVICE_CMD_RENAME_DESC),
PortableCommand(DEVICE_CMD_AUTOFILL, IDS_DEVICE_CMD_AUTOFILL, IDS_DEVICE_CMD_AUTOFILL_DESC),
PortableCommand(DEVICE_CMD_PLAYLIST_CREATE, IDS_DEVICE_CMD_PLAYLIST_CREATE, IDS_DEVICE_CMD_PLAYLIST_CREATE_DESC),
PortableCommand(DEVICE_CMD_PREFERENCES, IDS_DEVICE_CMD_PREFERENCES, IDS_DEVICE_CMD_PREFERENCES_DESC),
PortableCommand(DEVICE_CMD_HIDE, IDS_DEVICE_CMD_HIDE, IDS_DEVICE_CMD_HIDE),
};
static ifc_devicecommand * _cdecl
Devices_RegisterCommand(const char *name, void *user)
{
for(size_t i = 0; i < sizeof(registered_commands)/sizeof(*registered_commands); i++)
{
if (name == registered_commands[i].GetName())
{
return ®istered_commands[i];
}
}
return NULL;
}
void Devices_Init()
{
if (AGAVE_API_DEVICEMANAGER)
{
/* register 'portable' device type */
ifc_devicetype *type = &portable_device_type;
AGAVE_API_DEVICEMANAGER->TypeRegister(&type, 1);
/* register 'usb' connection type */
ifc_deviceconnection *connection = &usb_connection;
AGAVE_API_DEVICEMANAGER->ConnectionRegister(&connection, 1);
/* register commands */
const char *commands[sizeof(registered_commands)/sizeof(*registered_commands)];
for(size_t i = 0; i < sizeof(registered_commands)/sizeof(*registered_commands); i++)
{
commands[i] = registered_commands[i].GetName();
}
AGAVE_API_DEVICEMANAGER->CommandRegisterIndirect(commands, sizeof(registered_commands)/sizeof(*registered_commands), Devices_RegisterCommand, NULL);
}
}
int DeviceView::QueryInterface(GUID interface_guid, void **object)
{
if (interface_guid == IFC_Device)
{
AddRef();
*object = (ifc_device *)this;
return 0;
}
return 1;
}
const char *DeviceView::GetName()
{
return name;
}
HRESULT DeviceView::GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
{
buffer[0]=0;
dev->extraActions(DEVICE_GET_ICON, width, height, (intptr_t)buffer);
if (buffer[0] == 0)
return E_NOTIMPL;
else
return S_OK;
return E_NOTIMPL;
}
HRESULT DeviceView::GetDisplayName(wchar_t *buffer, size_t bufferSize)
{
// TODO sometimes this is erroring on loading
dev->getPlaylistName(0, buffer, bufferSize);
return S_OK;
}
const char *DeviceView::GetType()
{
return "portable";
}
const char *DeviceView::GetDisplayType()
{
return display_type;
}
const char *DeviceView::GetConnection()
{
return connection_type;
}
BOOL DeviceView::GetHidden()
{
return FALSE;
}
HRESULT DeviceView::GetTotalSpace(uint64_t *size)
{
UpdateSpaceInfo(FALSE, TRUE);
*size = dev->getDeviceCapacityTotal();
return S_OK;
}
HRESULT DeviceView::GetUsedSpace(uint64_t *size)
{
if (NULL == size)
return E_POINTER;
UpdateSpaceInfo(TRUE, TRUE);
*size = usedSpace;
return S_OK;
}
BOOL DeviceView::GetAttached()
{
return TRUE; // ml_pmp devices are by default attached
}
HRESULT DeviceView::Attach(HWND hostWindow)
{
return E_NOTIMPL;
}
HRESULT DeviceView::Detach(HWND hostWindow)
{
return E_NOTIMPL;
}
HRESULT DeviceView::EnumerateCommands(ifc_devicesupportedcommandenum **enumerator, DeviceCommandContext context)
{
DeviceCommandInfo commands[32];
size_t count;
if (NULL == enumerator)
return E_POINTER;
count = 0;
LinkedQueue * txQueue = getTransferQueue(this);
if (txQueue == NULL)
return E_POINTER;
// return E_NOTIMPL;
if (context == DeviceCommandContext_View)
{
if (0 == txQueue->GetSize())
{
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_SYNC, DeviceCommandFlag_Primary);
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_AUTOFILL, DeviceCommandFlag_None);
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_PLAYLIST_CREATE, DeviceCommandFlag_None);
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_PREFERENCES, DeviceCommandFlag_None);
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_EJECT, DeviceCommandFlag_None);
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_REMOVE, DeviceCommandFlag_None);
}
}
else
{
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_VIEW_OPEN, DeviceCommandFlag_Primary);
DeviceCommandFlags flags = DeviceCommandFlag_None;
if (0 != txQueue->GetSize())
flags |= DeviceCommandFlag_Disabled;
if (0 != dev->extraActions(DEVICE_SYNC_UNSUPPORTED,0,0,0))
flags |= DeviceCommandFlag_Disabled;
SetDeviceCommandInfo(&commands[count++], (!isCloudDevice ? DEVICE_CMD_SYNC : DEVICE_CMD_TRANSFER), flags | DeviceCommandFlag_Group);
if (!isCloudDevice) SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_AUTOFILL, flags);
flags = DeviceCommandFlag_None;
if (0 == dev->extraActions(DEVICE_PLAYLISTS_UNSUPPORTED,0,0,0))
{
// TODO remove once we've got cloud playlists implemented
if (!isCloudDevice)
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_PLAYLIST_CREATE, flags | DeviceCommandFlag_Group);
}
// adds a specific menu item to hide the 'local library' source
if (isCloudDevice)
{
char name[128] = {0};
if (dev->extraActions(DEVICE_GET_UNIQUE_ID, (intptr_t)name, sizeof(name), 0))
{
if (!strcmp(name, "local_desktop"))
{
flags = DeviceCommandFlag_None | DeviceCommandFlag_Group;
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_HIDE, flags);
}
}
}
bool has_rename = true;
flags = DeviceCommandFlag_None;
if (0 == dev->extraActions(DEVICE_CAN_RENAME_DEVICE,0,0,0))
has_rename = false;
else
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_RENAME, flags | DeviceCommandFlag_Group);
flags = (!has_rename ? DeviceCommandFlag_Group : DeviceCommandFlag_None);
SetDeviceCommandInfo(&commands[count++], DEVICE_CMD_PREFERENCES, flags);
if (!dev->extraActions(DEVICE_DOES_NOT_SUPPORT_REMOVE,0,0,0))
SetDeviceCommandInfo(&commands[count++], (!isCloudDevice ? DEVICE_CMD_EJECT : DEVICE_CMD_REMOVE), flags | DeviceCommandFlag_Group);
}
*enumerator = new DeviceCommandEnumerator(commands, count);
if (NULL == *enumerator)
return E_OUTOFMEMORY;
return S_OK;
}
HRESULT DeviceView::SendCommand(const char *command, HWND hostWindow, ULONG_PTR param)
{
if (!strcmp(command, DEVICE_CMD_EJECT) || !strcmp(command, DEVICE_CMD_REMOVE))
{
Eject();
return S_OK;
}
else if (!strcmp(command, DEVICE_CMD_SYNC) || !strcmp(command, DEVICE_CMD_TRANSFER))
{
if (!this->isCloudDevice) Sync();
else CloudSync();
return S_OK;
}
else if (!strcmp(command, DEVICE_CMD_AUTOFILL))
{
Autofill();
return S_OK;
}
else if (!strcmp(command, DEVICE_CMD_RENAME))
{
if (NULL != treeItem)
MLNavItem_EditTitle(plugin.hwndLibraryParent, treeItem);
else
RenamePlaylist(0);
return S_OK;
}
else if (!strcmp(command, DEVICE_CMD_PLAYLIST_CREATE))
{
CreatePlaylist();
return S_OK;
}
else if (!strcmp(command, DEVICE_CMD_PREFERENCES))
{
SENDWAIPC(plugin.hwndWinampParent, IPC_OPENPREFSTOPAGE,(WPARAM)&devPrefsPage);
return S_OK;
}
else if (!strcmp(command, DEVICE_CMD_HIDE))
{
static int IPC_CLOUD_HIDE_LOCAL = -1;
if (IPC_CLOUD_HIDE_LOCAL == -1)
IPC_CLOUD_HIDE_LOCAL = SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&"WinampCloudLocal", IPC_REGISTER_WINAMP_IPCMESSAGE);
SENDWAIPC(plugin.hwndWinampParent, IPC_CLOUD_HIDE_LOCAL, 0);
return S_OK;
}
return E_NOTIMPL;
}
HRESULT DeviceView::GetCommandFlags(const char *command, DeviceCommandFlags *flags)
{
return E_NOTIMPL;
}
HRESULT DeviceView::GetActivity(ifc_deviceactivity **activity)
{
LinkedQueue * txQueue = getTransferQueue();
if (txQueue == NULL || txQueue->GetSize() == 0)
{
*activity = 0;
return S_FALSE;
}
AddRef();
*activity = this;
return S_OK;
}
HRESULT DeviceView::Advise(ifc_deviceevent *handler)
{
event_handlers.push_back(handler);
return S_OK;
}
HRESULT DeviceView::Unadvise(ifc_deviceevent *handler)
{
//event_handlers.eraseObject(handler);
auto it = std::find(event_handlers.begin(), event_handlers.end(), handler);
if (it != event_handlers.end())
{
event_handlers.erase(it);
}
return S_OK;
}
extern C_ItemList devices;
extern void UpdateDevicesListView(bool softupdate);
void DeviceView::SetNavigationItem(void *navigationItem)
{
if (navigationItem)
RegisterViews((HNAVITEM)navigationItem);
}
BOOL DeviceView::GetActive()
{
LinkedQueue * txQueue = getTransferQueue();
if (txQueue == NULL || txQueue->GetSize() == 0)
return FALSE;
return TRUE;
}
BOOL DeviceView::GetCancelable()
{
return FALSE;
}
HRESULT DeviceView::GetProgress(unsigned int *percentCompleted)
{
LinkedQueue * txQueue = getTransferQueue();
LinkedQueue * finishedTX = getFinishedTransferQueue();
int txProgress = getTransferProgress();
int size = (txQueue ? txQueue->GetSize() : 0);
double num = (100.0 * (double)size) - (double)txProgress;
double total = (double)100 * size + 100 * (finishedTX ? finishedTX->GetSize() : 0);
double percent = (0 != total) ? (((total - num) * 100) / total) : 0;
*percentCompleted = (unsigned int)percent;
return S_OK;
}
HRESULT DeviceView::Activity_GetDisplayName(wchar_t *buffer, size_t bufferMax)
{
WASABI_API_LNGSTRINGW_BUF(IDS_TRANSFERRING, buffer, bufferMax);
return S_OK;
}
HRESULT DeviceView::Activity_GetStatus(wchar_t *buffer, size_t bufferMax)
{
WASABI_API_LNGSTRINGW_BUF(IDS_TRANSFERRING_DESC, buffer, bufferMax);
return S_OK;
}
HRESULT DeviceView::Cancel(HWND hostWindow)
{
// threadKillswitch = 1; // TODO: i think this is how to do it
//transferContext.WaitForKill();
return S_OK;
}
HRESULT DeviceView::GetDropSupported(unsigned int dataType)
{
if (dataType == ML_TYPE_ITEMRECORDLISTW
|| dataType == ML_TYPE_ITEMRECORDLIST
|| dataType == ML_TYPE_PLAYLIST
|| dataType == ML_TYPE_PLAYLISTS
|| dataType == ML_TYPE_FILENAMES
|| dataType == ML_TYPE_FILENAMESW)
return S_OK;
return E_FAIL;
}
HRESULT DeviceView::Drop(void *data, unsigned int dataType)
{
return (HRESULT)TransferFromML(dataType,data,E_FAIL,S_OK);
}
HRESULT DeviceView::SetDisplayName(const wchar_t *displayName, bool force = 0)
{
if((0 == force && 0 == dev->extraActions(DEVICE_CAN_RENAME_DEVICE,0,0,0)))
return E_FAIL;
dev->setPlaylistName(0, displayName);
free(devPrefsPage.name);
devPrefsPage.name = _wcsdup(displayName);
SENDWAIPC(plugin.hwndWinampParent, IPC_UPDATE_PREFS_DLGW, (WPARAM)&devPrefsPage);
DevicePropertiesChanges();
UpdateDevicesListView(false);
OnNameChanged(displayName);
return S_OK;
}
HRESULT DeviceView::GetModel(wchar_t *buffer, size_t bufferSize)
{
if (NULL == buffer)
return E_POINTER;
buffer[0] = L'\0';
if(0 == dev->extraActions(DEVICE_GET_MODEL, (intptr_t)buffer, bufferSize, 0))
return E_NOTIMPL;
return S_OK;
}
HRESULT DeviceView::GetStatus(wchar_t *buffer, size_t bufferSize)
{
return E_NOTIMPL;
}
#define CBCLASS DeviceView
START_MULTIPATCH;
START_PATCH(PATCH_IFC_DEVICE)
M_CB(PATCH_IFC_DEVICE, ifc_device, QUERYINTERFACE, QueryInterface);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETNAME, GetName);
M_CB(PATCH_IFC_DEVICE, ifc_device, ifc_deviceobject::API_GETDISPLAYNAME, GetDisplayName);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETICON, GetIcon);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETTYPE, GetType);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETDISPLAYTYPE, GetDisplayType);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETCONNECTION, GetConnection);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETTOTALSPACE, GetTotalSpace);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETUSEDSPACE, GetUsedSpace);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_ENUMERATECOMMANDS, EnumerateCommands);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_SENDCOMMAND, SendCommand);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETATTACHED, GetAttached);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_ATTACH, Attach);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_DETACH, Detach);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_ADVISE, Advise);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_UNADVISE, Unadvise);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_CREATEVIEW, CreateView);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETACTIVITY, GetActivity);
M_VCB(PATCH_IFC_DEVICE, ifc_device, API_SETNAVIGATIONITEM, SetNavigationItem);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETDROPSUPPORTED, GetDropSupported);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_DROP, Drop);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_SETDISPLAYNAME, SetDisplayName);
M_CB(PATCH_IFC_DEVICE, ifc_device, API_GETMODEL, GetModel);
M_CB(PATCH_IFC_DEVICE, ifc_device, ifc_device::API_GETSTATUS, GetStatus);
M_CB(PATCH_IFC_DEVICE, ifc_device, ADDREF, AddRef);
M_CB(PATCH_IFC_DEVICE, ifc_device, RELEASE, Release);
NEXT_PATCH(PATCH_IFC_DEVICEACTIVITY)
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, API_GETACTIVE, GetActive);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, API_GETCANCELABLE, GetCancelable);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, API_GETPROGRESS, GetProgress);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, ifc_deviceactivity::API_GETDISPLAYNAME, Activity_GetDisplayName);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, ifc_deviceactivity::API_GETSTATUS, Activity_GetStatus);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, API_CANCEL, Cancel);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, ADDREF, AddRef);
M_CB(PATCH_IFC_DEVICEACTIVITY, ifc_deviceactivity, RELEASE, Release);
END_PATCH
END_MULTIPATCH;
#undef CBCLASS
|