-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfoo_snarl.cpp
355 lines (312 loc) · 11 KB
/
foo_snarl.cpp
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
/*
Copyright (c) 2008-2015, Skyler Kehren
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
VALIDATE_COMPONENT_FILENAME("foo_snarl.dll");
DECLARE_COMPONENT_VERSION(
"FooSnarl",
"2.0 beta",
"Snarl notification interface for Foobar2000\n"
"Developed by: Skyler Kehren (Pyrodogg)\n"
"foosnarl at pyrodogg.com\n"
"Copyright (C) 2008-2015 Skyler Kehren\n"
"Released under BSD License\n"
"Contributions by: Max Battcher");
Snarl::V42::SnarlInterface sn42;
#pragma region Declarations
inline char base64_char(unsigned char in);
void base64_encode(pfc::string_base & out, const unsigned char * data, unsigned size);
#pragma endregion
#pragma region Callback Window
LRESULT CALLBACK WndProcFooSnarl(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message)
{
case WM_SHOWWINDOW:
if (wParam == TRUE) {
standard_commands::main_activate();
}
case WM_CLOSE:
DestroyWindow(hwnd);
case WM_USER:
switch (LOWORD(wParam))
{
case Snarl::V42::SnarlEnums::SnarlQuit:
{
foo_snarl.try_unregister();
return 0;
}
case Snarl::V42::SnarlEnums::NotifyAction:
int action = HIWORD(wParam);
switch (action) {
case 1:
//Back action
standard_commands::main_previous();
return 0;
case 2:
//Next action
standard_commands::main_next();
return 0;
case 3:
//Stop
standard_commands::main_stop();
return 0;
case 4:
//Play
standard_commands::main_play();
return 0;
case 5:
//Pause
standard_commands::main_pause();
return 0;
}
}
default:
if (message == Snarl::V42::SnarlInterface::Broadcast())
{
switch (wParam)
{
case Snarl::V42::SnarlEnums::SnarlLaunched:
case Snarl::V42::SnarlEnums::SnarlStarted:
foo_snarl.try_register();
return 0;
case Snarl::V42::SnarlEnums::SnarlQuit:
case Snarl::V42::SnarlEnums::SnarlStopped:
foo_snarl.try_unregister();
return 0;
}
}
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
#pragma endregion
namespace FooSnarl {
LPSTR FSClass(int intclass) {
switch (intclass) {
case MessageClass::Play:
return "Play";
break;
case MessageClass::Pause:
return "Pause";
break;
case MessageClass::Stop:
return "Stop";
break;
default:
return "";
break;
}
}
void FooSnarl::register_snarl_class(int intClass) {
LONG32 ret = sn42.AddClass(FSClass(intClass), FSClass(intClass),0,0,0,0,5);
if (ret < 0 && ret != -Snarl::V42::SnarlEnums::ErrorAlreadyRegistered)
{
console::formatter() << "[FooSnarl] Unable to register class " << intClass;
}
}
void FooSnarl::send_snarl_message(int pAlertClass, pfc::string pTitleFormat, pfc::string pBodyFormat, bool pEnableActions) {
static_api_ptr_t<playback_control> pc;
static_api_ptr_t<playlist_manager> pm;
service_ptr_t<titleformat_object> script;
metadb_handle_ptr handle;
pfc::string_formatter text_formatted;
pfc::string snarl_title;
pfc::string snarl_msg;
pfc::string snarl_icon;
pfc::string8 snarl_icon_data;
static metadb_handle_ptr handle_last_song;
static int FSLastMsgClass = 0;
//Get and store now playing. If false, then retrieve last playing.
if (pc->get_now_playing(handle)) {
handle_last_song.copy(handle);
}
else {
handle.copy(handle_last_song);
}
//If not playing and no stored handle, get focused playlist item.
if (handle.is_empty()) pm->activeplaylist_get_focus_item_handle(handle);
//If no handle is identified, then exit.
if (handle.is_empty()) return;
if (pAlertClass == MessageClass::Auto) {
if (pc->is_paused()) {
pAlertClass = MessageClass::Pause;
}
else if (pc->is_playing()) {
pAlertClass = MessageClass::Play;
}
else {
pAlertClass = MessageClass::Stop;
}
}
//Process title format string for message body
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(script, pBodyFormat.get_ptr(), "Invalid format script");
pc->playback_format_title_ex(handle, NULL, text_formatted, script, NULL, play_control::display_level_titles);
snarl_msg = text_formatted.toString();
//Process title format string for message title
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(script, pTitleFormat.get_ptr(), "Invalid format script");
pc->playback_format_title_ex(handle, NULL, text_formatted, script, NULL, play_control::display_level_titles);
snarl_title = text_formatted.toString();
metadb_handle_list handle_list;
pfc::list_t<GUID> guid_list;
handle_list.add_item(handle);
guid_list.add_item(album_art_ids::cover_front);
try
{
//Get front cover album art if available
abort_callback_impl moo;
album_art_extractor_instance_v2::ptr art_instance = static_api_ptr_t<album_art_manager_v2>()->open(handle_list, guid_list, moo);
album_art_data_ptr art = art_instance->query(album_art_ids::cover_front, moo);
if (art->get_size())
{
base64_encode(snarl_icon_data, art->get_ptr(), art->get_size());
snarl_icon = "";
}
}
catch (...)
{
//Use foobar2000 icon if album art isn't available.
snarl_icon = foobarIcon;
}
//Send Snarl Message
if (FSLastMsgClass != pAlertClass) {
sn42.Hide(sn42.GetLastMsgToken());
}
FSLastMsgClass = pAlertClass;
if (sn42.IsVisible(sn42.GetLastMsgToken()) == Snarl::V42::SnarlEnums::Success)
{
sn42.Update(sn42.GetLastMsgToken(), FSClass(pAlertClass), snarl_title.get_ptr(), snarl_msg.get_ptr(), -1, snarl_icon.get_ptr(), snarl_icon_data.get_ptr());
}
else
{
LONG32 ret = sn42.Notify(FSClass(pAlertClass), snarl_title.get_ptr(), snarl_msg.get_ptr(), -1, snarl_icon.get_ptr(), snarl_icon_data.get_ptr());
if (ret > 0 && pEnableActions)
{
switch (pAlertClass) {
case MessageClass::Play:
sn42.AddAction(ret, "Pause", "@5");
sn42.AddAction(ret, "Next", "@2");
sn42.AddAction(ret, "Previous", "@1");
break;
case MessageClass::Pause:
case MessageClass::Stop:
sn42.AddAction(ret, "Play", "@4");
sn42.AddAction(ret, "Next", "@2");
sn42.AddAction(ret, "Previous", "@1");
break;
}
}
}
}
void FooSnarl::on_playback_event(int alertClass) {
send_snarl_message(alertClass, Preferences::titleformat_data, Preferences::textformat_data, Preferences::enable_actions_data);
}
void FooSnarl::try_register()
{
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = (WNDPROC)WndProcFooSnarl;
wcex.hInstance = core_api::get_my_instance();
wcex.lpszClassName = _T("FooSnarlMsg");
RegisterClassEx(&wcex);
hwndFooSnarlMsg = CreateWindowEx(0, _T("FooSnarlMsg"), _T("FooSnarl Msg"), 0, 0, 0, 0, 0, GetDesktopWindow(), 0, 0, 0);
if (hwndFooSnarlMsg == NULL)
{
console::formatter() << "[FooSnarl] Unable to create message window (Error 0x" << pfc::format_int(GetLastError(), 0, 16) << ")";
}
uGetModuleFileName(NULL, foobarIcon);
foobarIcon += ",105";
//Register Foobar2000 with Snarl
pfc::string8 snarl_password;
service_ptr_t<genrand_service> g_rand = genrand_service::g_create();
g_rand->seed(time(NULL));
pfc::array_t<unsigned> junk;
junk.set_count(4);
for (unsigned i = 0; i < 4; i++) junk[i] = g_rand->genrand(~0);
base64_encode(snarl_password, junk.get_ptr(), 16);
LONG32 ret = sn42.Register("Foobar2000", "Foobar2000", foobarIcon, snarl_password.get_ptr(), hwndFooSnarlMsg, WM_USER);
if (ret > 0)
{
foo_snarl.register_snarl_class(MessageClass::Play);
foo_snarl.register_snarl_class(MessageClass::Pause);
foo_snarl.register_snarl_class(MessageClass::Stop);
}
else
{
if (ret != -Snarl::V42::SnarlEnums::ErrorNotRunning)
{
console::formatter() << "[FooSnarl] Unable to register with Snarl";
}
}
}
void FooSnarl::try_unregister()
{
//Unregister foosnarl
LONG32 ret = sn42.ClearClasses();
if (ret < 0 && ret != -Snarl::V42::SnarlEnums::ErrorNotRunning)
{
console::formatter() << "[FooSnarl] Failed to remove registered classes";
}
ret = sn42.Unregister("Foobar2000");
if (ret < 0 && ret != -Snarl::V42::SnarlEnums::ErrorNotRunning)
{
console::formatter() << "[FooSnarl] Failed to unregister with Snarl";
}
DestroyWindow(hwndFooSnarlMsg);
UnregisterClass(_T("FooSnarlMsg"), core_api::get_my_instance());
}
}
#pragma region Utility Functions
inline char base64_char(unsigned char in)
{
static const char base64_chars[64] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};
return base64_chars[in];
}
void base64_encode(pfc::string_base & out, const unsigned char * data, unsigned size)
{
out.reset();
while (size >= 3)
{
out.add_byte( base64_char( data[0] >> 2 ) );
out.add_byte( base64_char( ( ( data[0] & 3 ) << 4 ) + ( data[1] >> 4 ) ) );
out.add_byte( base64_char( ( ( data[1] & 15 ) << 2 ) + ( data[2] >> 6 ) ) );
out.add_byte( base64_char( data[2] & 63 ) );
data += 3;
size -= 3;
}
if (size == 2)
{
out.add_byte( base64_char( data[0] >> 2 ) );
out.add_byte( base64_char( ( ( data[0] & 3 ) << 4 ) + ( data[1] >> 4 ) ) );
out.add_byte( base64_char( ( data[1] & 15 ) << 2 ) );
out.add_byte( '%' );
}
else if (size == 1)
{
out.add_byte( base64_char( data[0] >> 2 ) );
out.add_byte( base64_char( ( data[0] & 3 ) << 4 ) );
out.add_byte( '%' );
out.add_byte( '%' );
}
}
#pragma endregion