/
cad.c
524 lines (438 loc) · 12.9 KB
/
cad.c
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
/*****************************************************************************
* Copyright (C) 2013 Birunthan Mohanathas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "cad_sdk.h"
#include <Windows.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_interface.h>
#include <vlc_playlist.h>
#include <vlc_url.h>
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
static int Open(vlc_object_t* p_this);
static void Close(vlc_object_t* p_this);
// Module descriptor.
#define MODULE_STRING "cad"
vlc_module_begin()
set_shortname("CAD NowPlaying")
set_description("CAD NowPlaying")
set_category(CAT_INTERFACE)
set_subcategory(SUBCAT_INTERFACE_CONTROL)
set_capability("interface", 0)
set_callbacks(Open, Close)
vlc_module_end()
// Module instance data.
struct intf_sys_t
{
HWND window;
HWND cad_window;
vlc_thread_t thread;
vlc_mutex_t lock;
input_thread_t* p_input;
};
const int DATA_MAX_LENGTH = 1024;
static void* Thread(void* p_data);
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static int InputEvent(
vlc_object_t* p_this, const char* var, vlc_value_t oldval, vlc_value_t val, void* p_data);
static int PlaylistEvent(
vlc_object_t* p_this, const char* var, vlc_value_t oldval, vlc_value_t val, void* p_data);
// Initialize interface.
static int Open(vlc_object_t* p_this)
{
intf_thread_t* const p_intf = (intf_thread_t*)p_this;
// Limit to one instance.
if (FindWindow(L"CD Art Display IPC Class", L"VLC"))
{
p_intf->p_sys = NULL;
return VLC_SUCCESS;
}
OutputDebugString(L"VLC Open");
intf_sys_t* const p_sys = (intf_sys_t*)calloc(1, sizeof(intf_sys_t));
if (!p_sys)
{
return VLC_ENOMEM;
}
p_intf->p_sys = p_sys;
vlc_mutex_init(&p_sys->lock);
if (vlc_clone(&p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW))
{
vlc_mutex_destroy(&p_sys->lock);
free(p_sys);
p_intf->p_sys = NULL;
return VLC_ENOMEM;
}
var_AddCallback(pl_Get(p_intf->p_libvlc), "input-current", PlaylistEvent, p_intf);
return VLC_SUCCESS;
}
// Destroy interface.
static void Close(vlc_object_t* p_this)
{
intf_thread_t* p_intf = (intf_thread_t*)p_this;
intf_sys_t* p_sys = p_intf->p_sys;
if (!p_sys)
{
// This instance was never initialized in Open() as there was already a libcad instance open.
return;
}
if (p_sys->p_input)
{
vlc_object_release(p_sys->p_input);
p_sys->p_input = NULL;
}
var_DelCallback(pl_Get(p_intf->p_libvlc), "input-current", PlaylistEvent, p_intf);
if (p_sys->cad_window)
{
PostMessage(p_sys->cad_window, WM_USER, 0, IPC_SHUTDOWN_NOTIFICATION);
}
vlc_mutex_lock(&p_sys->lock);
if (p_sys->window)
{
PostMessage(p_sys->window, WM_CLOSE, 0, 0);
}
vlc_mutex_unlock(&p_sys->lock);
vlc_join(p_sys->thread, NULL);
vlc_mutex_destroy(&p_sys->lock);
free(p_intf->p_sys);
}
static int InputEvent(
vlc_object_t* p_this, const char* var, vlc_value_t oldval, vlc_value_t val, void* p_data)
{
input_thread_t* const p_input = (input_thread_t*)p_this;
intf_thread_t* const p_intf = (intf_thread_t*)p_data;
intf_sys_t* const p_sys = p_intf->p_sys;
if (!p_sys->cad_window) return VLC_SUCCESS;
switch (val.i_int)
{
case INPUT_EVENT_ITEM_META:
{
input_item_t* p_item = input_GetItem(p_input);
if (input_item_IsPreparsed(p_item))
{
PostMessage(p_sys->cad_window, WM_USER, 0, IPC_TRACK_CHANGED_NOTIFICATION);
}
break;
}
case INPUT_EVENT_STATE:
{
const int input_state = var_GetInteger(p_input, "state");
const int state =
input_state == PLAYING_S ? 1 :
input_state == PAUSE_S ? 2 :
0;
PostMessage(p_sys->cad_window, WM_USER, state, IPC_STATE_CHANGED_NOTIFICATION);
break;
}
}
return VLC_SUCCESS;
}
// Called when the playing item changes.
static int PlaylistEvent(
vlc_object_t* p_this, const char* var, vlc_value_t oldval, vlc_value_t val, void* p_data)
{
intf_thread_t* const p_intf = (intf_thread_t*)p_data;
intf_sys_t* const p_sys = p_intf->p_sys;
if (p_sys->p_input)
{
var_DelCallback(p_sys->p_input, "intf-event", InputEvent, p_intf);
vlc_object_release(p_sys->p_input);
p_sys->p_input = NULL;
}
// Add a callback for the input events so that we can receive a notification when the item
// metadata has been parsed and when the playlist state changes.
p_sys->p_input = (input_thread_t*)val.p_address;
var_AddCallback(p_sys->p_input, "intf-event", InputEvent, p_intf);
vlc_object_hold(p_sys->p_input);
return VLC_SUCCESS;
}
// Helper thread that creates the CAD window and processes messages.
static void* Thread(void* p_data)
{
intf_thread_t* const p_intf = (intf_thread_t*)p_data;
intf_sys_t* const p_sys = p_intf->p_sys;
const HMODULE module = (HINSTANCE)&__ImageBase;
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.hInstance = module;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"CD Art Display IPC Class";
RegisterClass(&wc);
HWND window = CreateWindow(
L"CD Art Display IPC Class", L"VLC", WS_DISABLED,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, module, NULL);
if (!window) return NULL;
vlc_mutex_lock(&p_sys->lock);
p_sys->window = window;
vlc_mutex_unlock(&p_sys->lock);
SetWindowLongPtr(p_sys->window, GWLP_USERDATA, (LONG_PTR)p_intf);
typedef BOOL (WINAPI * FPCHANGEWINDOWMESSAGEFILTER)(UINT message, DWORD dwFlag);
typedef BOOL (WINAPI * FPCHANGEWINDOWMESSAGEFILTEREX)(
HWND hWnd, UINT message, DWORD dwFlag, PCHANGEFILTERSTRUCT pChangeFilterStruct);
// WM_USER needs to be removed from the filtered messages. To do so, try
// ChangeWindowMessageFilterEx first (for Windows 7 and later).
HMODULE user32 = GetModuleHandle(L"user32.dll");
auto changeWindowMessageFilterEx =
(FPCHANGEWINDOWMESSAGEFILTEREX)GetProcAddress(user32, "ChangeWindowMessageFilterEx");
if (changeWindowMessageFilterEx)
{
changeWindowMessageFilterEx(p_sys->window, WM_USER, MSGFLT_ALLOW, NULL);
}
else
{
// Otherwise try ChangeWindowMessageFilter (for Windows Vista).
FPCHANGEWINDOWMESSAGEFILTER changeWindowMessageFilter =
(FPCHANGEWINDOWMESSAGEFILTER)GetProcAddress(user32, "ChangeWindowMessageFilter");
if (changeWindowMessageFilter)
{
changeWindowMessageFilter(WM_USER, MSGFLT_ALLOW);
}
}
p_sys->cad_window = FindWindow(NULL, L"CD Art Display 1.x Class");
if (p_sys->cad_window)
{
WCHAR filename[MAX_PATH];
GetModuleFileName(module, filename, MAX_PATH);
WCHAR buffer[DATA_MAX_LENGTH];
const int buffer_len = _snwprintf(
buffer, ARRAYSIZE(buffer), L"1\tCD Art Display IPC Class\tVLC\t%s\t", filename);
// Register with CAD.
COPYDATASTRUCT cds;
cds.dwData = IPC_REGISTER_NOTIFICATION;
cds.lpData = buffer;
cds.cbData = (buffer_len + 1) * sizeof(buffer[0]);
SendMessage(p_sys->cad_window, WM_COPYDATA, 0, (LPARAM)&cds);
}
// Standard message loop.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
vlc_mutex_lock(&p_sys->lock);
DestroyWindow(window);
UnregisterClass(L"CD Art Display IPC Class", module);
p_sys->window = NULL;
vlc_mutex_unlock(&p_sys->lock);
return 0;
}
static LRESULT HandleCadMessage(intf_thread_t* p_intf, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
intf_sys_t* const p_sys = p_intf->p_sys;
switch (lParam)
{
case IPC_PLAY:
{
playlist_Play(pl_Get(p_intf->p_libvlc));
return 1;
}
case IPC_PLAYPAUSE:
{
playlist_t* p_playlist = pl_Get(p_intf->p_libvlc);
const bool playing = playlist_Status(p_playlist) == PLAYLIST_RUNNING;
playlist_Control(p_playlist, playing ? PLAYLIST_PAUSE : PLAYLIST_PLAY, pl_Unlocked);
return 1;
}
case IPC_PAUSE:
{
playlist_Pause(pl_Get(p_intf->p_libvlc));
return 1;
}
case IPC_STOP:
{
playlist_Stop(pl_Get(p_intf->p_libvlc));
return 1;
}
case IPC_NEXT:
{
playlist_Next(pl_Get(p_intf->p_libvlc));
return 1;
}
case IPC_PREVIOUS:
{
playlist_Prev(pl_Get(p_intf->p_libvlc));
return 1;
}
case IPC_SET_VOLUME:
{
playlist_VolumeSet(pl_Get(p_intf->p_libvlc), (int)wParam / 100.0f);
return 1;
}
case IPC_GET_VOLUME:
{
// VLC can return a volume larger than 100% so we need to cap it to 100 here.
const float volume = playlist_VolumeGet(pl_Get(p_intf->p_libvlc)) * 100.0f;
return (LRESULT)min(volume, 100.0f);
}
case IPC_GET_DURATION:
{
unsigned int duration = 0;
if (p_sys->p_input)
{
input_item_t* const p_item = input_GetItem(p_sys->p_input);
duration = (unsigned int)(input_item_GetDuration(p_item) / 1000000);
}
return duration;
}
case IPC_GET_POSITION:
{
int pos = 0;
if (p_sys->p_input)
{
pos = (int)(var_GetTime(p_sys->p_input, "time") / CLOCK_FREQ);
}
return pos;
}
case IPC_SET_POSITION:
{
if (p_sys->p_input)
{
var_SetTime(p_sys->p_input, "time", (int64_t)wParam * CLOCK_FREQ);
}
return 0;
}
case IPC_GET_SHUFFLE:
{
return (int)var_GetBool(pl_Get(p_intf->p_libvlc), "random");
}
case IPC_SET_SHUFFLE:
{
return (int)var_SetBool(pl_Get(p_intf->p_libvlc), "random", (bool)wParam);
}
case IPC_GET_REPEAT:
{
return (int)var_GetBool(pl_Get(p_intf->p_libvlc), "repeat");
}
case IPC_SET_REPEAT:
{
return (int)var_SetBool(pl_Get(p_intf->p_libvlc), "repeat", (bool)wParam);
}
case IPC_SET_RATING:
{
// VLC does not support ratings so send back 0.
PostMessage(p_sys->cad_window, WM_USER, 0, IPC_RATING_CHANGED_NOTIFICATION);
return 0;
}
case IPC_SET_CALLBACK_HWND:
{
p_sys->cad_window = (HWND)wParam;
return 1;
}
case IPC_SHOW_WINDOW:
{
// TODO.
return 0;
}
case IPC_GET_STATE:
{
const int status = playlist_Status(pl_Get(p_intf->p_libvlc));
return
status == PLAYLIST_RUNNING ? 1 :
status == PLAYLIST_PAUSED ? 2 :
0;
}
case IPC_SHUTDOWN_NOTIFICATION:
{
p_sys->cad_window = NULL;
return 1;
}
case IPC_CLOSE:
{
libvlc_Quit(p_intf->p_libvlc);
return 1;
}
case IPC_GET_CURRENT_TRACK:
{
if (!p_sys->p_input) return 0;
input_item_t* p_item = input_GetItem(p_sys->p_input);
char buffer[DATA_MAX_LENGTH];
int buffer_len = 0;
// If the i sstarts with file://, we assume that it is a local file and detailed
// metadata is available. Otherwise, we assume it to be a network stream (i.e. radio)
// with limited info (only a title).
char* const file = decode_URI(input_item_GetURI(p_item));
if (strncmp(file, "file://", 7) == 0)
{
char* const title = input_item_GetTitleFbName(p_item);
char* const artist = input_item_GetArtist(p_item);
char* const album = input_item_GetAlbum(p_item);
char* const cover = decode_URI(input_item_GetArtworkURL(p_item));
const unsigned int duration = input_item_GetDuration(p_item) / 1000000U;
buffer_len = _snprintf(
buffer, ARRAYSIZE(buffer), "%s\t%s\t%s\t\t\t\t\t%u\t%s\t\t%s\t\t\t\t\t\t\t",
title ? title : "",
artist ? artist : "",
album ? album : "",
duration,
file ? &file[8] : "",
cover ? &cover[8] : ""); // Skip the "file://" part.
free(title);
free(artist);
free(album);
free(cover);
}
else if (char* now_playing = input_item_GetNowPlaying(p_item))
{
char* artist = NULL;
char* title = now_playing;
char* pos = strstr(now_playing, " - ");
if (pos)
{
pos[0] = '\0';
artist = title;
title = pos + 3; // Skip the " - "
}
buffer_len = _snprintf(
buffer, ARRAYSIZE(buffer), "%s\t%s\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t",
title ? title : "",
artist ? artist : "");
free(now_playing);
}
free(file);
if (buffer_len)
{
wchar_t buffer_w[DATA_MAX_LENGTH];
const int buffer_w_len = MultiByteToWideChar(
CP_UTF8, 0, buffer, buffer_len + 1, buffer_w, ARRAYSIZE(buffer_w));
COPYDATASTRUCT cds;
cds.dwData = IPC_CURRENT_TRACK_NOTIFICATION;
cds.lpData = &buffer_w;
cds.cbData = buffer_w_len * sizeof(buffer_w[0]);
SendMessage(
p_sys->cad_window, WM_COPYDATA, IPC_CURRENT_TRACK_NOTIFICATION, (LPARAM)&cds);
}
return 1;
}
}
return 0;
}
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
intf_thread_t* const p_intf = (intf_thread_t*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (p_intf)
{
switch (msg)
{
case WM_USER:
return HandleCadMessage(p_intf, hwnd, wParam, lParam);
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}