summaryrefslogtreecommitdiff
path: root/plugins/ListeningTo/src/players/generic.cpp
blob: 875fe6a916244cd972835b35021f95c856a55105 (plain)
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
/*
Copyright (C) 2005-2009 Ricardo Pescuma Domenecci

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#include "../stdafx.h"

static UINT hTimer = NULL;
static HANDLE hLog = nullptr;

GenericPlayer *singleton = nullptr;

void m_log(const wchar_t *function, const wchar_t *fmt, ...)
{
	if (hLog == nullptr) {
		hLog = mir_createLog(MODULENAME, L"ListeningTo log", L"c:\\temp\\listeningto.txt", 0);
		if (hLog == nullptr)
			return;
	}

	mir_writeLogW(hLog, L"%s: ", function);

	va_list args;
	va_start(args, fmt);
	mir_writeLogVW(hLog, fmt, args);
}

static LRESULT CALLBACK ReceiverWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
	case WM_COPYDATA:
		if (loaded) {
			COPYDATASTRUCT *pData = (PCOPYDATASTRUCT)lParam;
			if (pData == nullptr || pData->dwData != MIRANDA_DW_PROTECTION || pData->cbData == 0 || pData->lpData == nullptr)
				return FALSE;

			if (singleton != nullptr)
				singleton->NewData((wchar_t *)pData->lpData, pData->cbData / 2);

			return TRUE;
		}
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

GenericPlayer::GenericPlayer()
{
	name = L"GenericPlayer";

	enabled = TRUE;
	received[0] = L'\0';
	singleton = this;

	WNDCLASS wc = {};
	wc.lpfnWndProc = ReceiverWndProc;
	wc.hInstance = g_plugin.getInst();
	wc.lpszClassName = MIRANDA_WINDOWCLASS;
	RegisterClass(&wc);

	hWnd = CreateWindow(MIRANDA_WINDOWCLASS, LPGENW("Miranda ListeningTo receiver"), 0, 0, 0, 0, 0, nullptr, nullptr, g_plugin.getInst(), nullptr);
}

GenericPlayer::~GenericPlayer()
{
	if (hTimer != NULL) {
		KillTimer(nullptr, hTimer);
		hTimer = NULL;
	}

	DestroyWindow(hWnd);
	hWnd = nullptr;

	UnregisterClass(MIRANDA_WINDOWCLASS, g_plugin.getInst());
	singleton = nullptr;
}

void GenericPlayer::ProcessReceived()
{
	mir_cslockfull lck(cs);

	// Do the processing
	// L"<Status 0-stoped 1-playing>\\0<Player>\\0<Type>\\0<Title>\\0<Artist>\\0<Album>\\0<Track>\\0<Year>\\0<Genre>\\0<Length (secs)>\\0\\0"

	wchar_t *p1 = wcsstr(received, L"\\0");
	if (IsEmpty(received) || p1 == nullptr)
		return;

	// Process string
	wchar_t *parts[11] = {};
	int pCount = 0;
	wchar_t *p = received;
	do {
		*p1 = '\0';
		parts[pCount] = p;
		pCount++;
		p = p1 + 2;
		p1 = wcsstr(p, L"\\0");
	} while (p1 != nullptr && pCount < 10);
	if (p1 != nullptr)
		*p1 = '\0';
	parts[pCount] = p;

	if (pCount < 5)
		return;

	// See if player is enabled
	Player *player = this;
	for (int i = FIRST_PLAYER; i < NUM_PLAYERS; i++) {

		wchar_t *player_name = players[i]->name;

		if (_wcsicmp(parts[1], player_name) == 0) {
			player = players[i];
			break;
		}
	}

	player->FreeData();

	if (wcscmp(L"1", parts[0]) != 0 || IsEmpty(parts[1]) || (IsEmpty(parts[3]) && IsEmpty(parts[4]))) {
		// Stoped playing or not enought info
	}
	else {
		mir_cslock plck(player->GetLock());
		LISTENINGTOINFO *li = player->GetInfo();

		li->cbSize = sizeof(listening_info);
		li->dwFlags = LTI_TCHAR;
		li->ptszType = U2TD(parts[2], L"Music");
		li->ptszTitle = U2T(parts[3]);
		li->ptszArtist = U2T(parts[4]);
		li->ptszAlbum = U2T(parts[5]);
		li->ptszTrack = U2T(parts[6]);
		li->ptszYear = U2T(parts[7]);
		li->ptszGenre = U2T(parts[8]);

		if (player == this)
			li->ptszPlayer = mir_wstrdup(parts[1]);
		else
			li->ptszPlayer = mir_wstrdup(player->name);

		if (parts[9] != nullptr) {
			long length = _wtoi(parts[9]);
			if (length > 0) {
				li->ptszLength = (wchar_t*)mir_alloc(10 * sizeof(wchar_t));

				int s = length % 60;
				int m = (length / 60) % 60;
				int h = (length / 60) / 60;

				if (h > 0)
					mir_snwprintf(li->ptszLength, 9, L"%d:%02d:%02d", h, m, s);
				else
					mir_snwprintf(li->ptszLength, 9, L"%d:%02d", m, s);
			}
		}
	}

	// Put back the '\\'s
	for (int i = 1; i <= pCount; i++)
		*(parts[i] - 2) = L'\\';
	if (p1 != nullptr)
		*p1 = L'\\';

	wcscpy(last_received, received);
	lck.unlock();

	NotifyInfoChanged();
}


static VOID CALLBACK SendTimerProc(HWND, UINT, UINT_PTR, DWORD)
{
	KillTimer(nullptr, hTimer);
	hTimer = NULL;

	if (!loaded)
		return;

	if (singleton != nullptr)
		singleton->ProcessReceived();
}


void GenericPlayer::NewData(const wchar_t *data, size_t len)
{
	if (data[0] == '\0')
		return;

	mir_cslock lck(cs);

	len = min(len, 1023);
	if (wcsncmp(received, data, len) != 0) {
		wcsncpy(received, data, len);
		received[len] = L'\0';

		if (hTimer)
			KillTimer(nullptr, hTimer);
		hTimer = SetTimer(nullptr, NULL, 300, SendTimerProc); // Do the processing after we return true
	}
}