aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/mediaplayer/MpAmipInterface.cpp
blob: 2514ba52b1faad069c6da5a1ab47202351253d6b (about) (plain) (blame)
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
//=============================================================================
//
//   File : MpAmipInterface.cpp
//   Creation date : Sun 27 Mar 2005 16:55:09 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2005-2010 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 "MpAmipInterface.h"
#include "KviOptions.h"

#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)

#include <QTextCodec>

#include "KviLocale.h"
#include "KviModule.h"

enum ac_StartMode
{
	AC_START_ALL = 0,
	AC_START_CLIENT,
	AC_START_SERVER,
	AC_START_NONE
};

enum ac_ErrorCode
{
	AC_ERR_NOERROR = 0,
	AC_ERR_CLIENTISNULL,
	AC_ERR_EXCEPTION,
	AC_ERR_CONNECTIONFAILED,
	AC_ERR_SERVERNOTRUNNING
};

#define AC_BUFFER_SIZE 2048

static HINSTANCE amip_dll = nullptr;

#define MP_AC_DYNPTR(__rettype, __func, __args)        \
	typedef __rettype(CALLBACK * lp_##__func)(__args); \
	lp_##__func __func

#define MP_AC_FUNC(__func)                                   \
	__func = (lp_##__func)GetProcAddress(amip_dll, #__func); \
	if(!__func)                                              \
	{                                                        \
		FreeLibrary(amip_dll);                               \
		return false;                                        \
	}

#define COMMA() ,

MP_AC_DYNPTR(void, ac_init, int mode);
MP_AC_DYNPTR(void, ac_uninit, void);
MP_AC_DYNPTR(void, ac_getDestHost, char * out);
MP_AC_DYNPTR(int, ac_getDestPort, void);
MP_AC_DYNPTR(bool, ac_pingServer, const char * host COMMA() int port COMMA() int timeout);
MP_AC_DYNPTR(int, ac_exec, const char * cmd);
MP_AC_DYNPTR(int, ac_eval, const char * cmd COMMA() char * result);

static bool loadAmipDll()
{
	amip_dll = LoadLibrary(TEXT("ac.dll"));
	if(!amip_dll)
		return false;

	MP_AC_FUNC(ac_init);
	MP_AC_FUNC(ac_uninit);
	MP_AC_FUNC(ac_getDestHost);
	MP_AC_FUNC(ac_getDestPort);
	MP_AC_FUNC(ac_pingServer);
	MP_AC_FUNC(ac_eval);
	MP_AC_FUNC(ac_exec);

	return true;
};

static QTextCodec * mediaplayer_get_codec()
{
	QTextCodec * pCodec = nullptr;
	pCodec = QTextCodec::codecForName(KVI_OPTION_STRING(KviOption_stringWinampTextEncoding).toUtf8());

	if(!pCodec)
		pCodec = QTextCodec::codecForLocale();
	return pCodec;
}

MP_IMPLEMENT_DESCRIPTOR(
    MpAmipInterface,
    "amip",
    __tr2qs_ctx(
        "An interface for the AMIP plugin.\n"
        "You can download it from http://amip.tools-for.net\n"
        "To use this interface you must "
        "install AMIP plugin for your player.",
        "mediaplayer"))

MpAmipInterface::MpAmipInterface()
    : MpInterface()
{
	if(!amip_dll)
	{
		bool res = loadAmipDll();
		if(!res)
		{
			amip_dll = nullptr;
			return;
		}
		ac_init(AC_START_CLIENT);
	}
}

MpAmipInterface::~MpAmipInterface()
{
	if(!amip_dll)
		return;
	ac_uninit();
	FreeLibrary(amip_dll);
	amip_dll = nullptr;
}

int MpAmipInterface::detect(bool bStart)
{
	if(!amip_dll)
		return 0;
	char host[AC_BUFFER_SIZE];
	ac_getDestHost(host);
	if(ac_pingServer(host, ac_getDestPort(), 5000))
		return 99;
	return 1;
}

#define MP_AMIP_COMMAND(__cmdname, __acmd)          \
	bool MpAmipInterface::__cmdname()               \
	{                                               \
		return (ac_exec(__acmd) == AC_ERR_NOERROR); \
	}

MP_AMIP_COMMAND(play, "control play")
MP_AMIP_COMMAND(stop, "control stop")
MP_AMIP_COMMAND(next, "control >")
MP_AMIP_COMMAND(prev, "control <")
MP_AMIP_COMMAND(pause, "control pause")
MP_AMIP_COMMAND(quit, "control exit")

// helper function for evaluating variables returning integers
int eval_int(const char * var)
{
	if(!amip_dll)
		return -1;
	char buff[AC_BUFFER_SIZE];
	int res = -1;
	if(AC_ERR_NOERROR == ac_eval(var, buff))
		res = atoi(buff);
	return res;
}

QString eval_str(const char * var)
{
	QString res;
	if(!amip_dll)
		return res;
	char buff[AC_BUFFER_SIZE];
	if(AC_ERR_NOERROR == ac_eval(var, buff))
		res.append(buff);
	return res;
}

int MpAmipInterface::length()
{
	return eval_int("var_sl") * 1000;
}

int MpAmipInterface::position()
{
	return eval_int("var_psec") * 1000;
}

int MpAmipInterface::bitRate()
{
	return eval_int("var_br");
}

int MpAmipInterface::sampleRate()
{
	return eval_int("var_sr");
}

int MpAmipInterface::channels()
{
	int ret = -1;
	if(eval_str("var_typ").startsWith("Stereo"))
		ret = 2;
	else if(eval_str("var_typ").startsWith("Mono"))
		ret = 1;
	return ret;
}

MpInterface::PlayerStatus MpAmipInterface::status()
{
	int ret = eval_int("var_stat");
	switch(ret)
	{
		case 0:
			return MpInterface::Stopped;
			break;
		case 3:
			return MpInterface::Paused;
			break;
		case 1:
			return MpInterface::Playing;
			break;
		default:
			return MpInterface::Unknown;
			break;
	}
	return MpInterface::Unknown;
}

QString MpAmipInterface::mrl()
{
	QString szRet;
	QString szFn = eval_str("var_fn");
	QTextCodec * pCodec = mediaplayer_get_codec();
	if(pCodec)
	{
		szRet = pCodec->toUnicode(szFn.toUtf8());
	}
	else
	{
		szRet = szFn;
	}

	if(!szRet.startsWith("http://", Qt::CaseInsensitive))
		szRet.prepend("file://");

	return szRet;
}

QString getAmipString(const char * var)
{
	QString szRet;
	QString szString = eval_str(var);
	QTextCodec * pCodec = mediaplayer_get_codec();
	if(pCodec)
	{
		szRet = pCodec->toUnicode(szString.toUtf8());
	}
	else
	{
		szRet = szString;
	}
	return szRet;
}

QString MpAmipInterface::nowPlaying()
{
	return getAmipString("var_s");
}

QString MpAmipInterface::artist()
{
	return getAmipString("var_1");
}

QString MpAmipInterface::title()
{
	return getAmipString("var_2");
}

QString MpAmipInterface::album()
{
	return getAmipString("var_4");
}

QString MpAmipInterface::year()
{
	return getAmipString("var_5");
}

QString MpAmipInterface::comment()
{
	return getAmipString("var_6");
}

QString MpAmipInterface::genre()
{
	return getAmipString("var_7");
}

bool MpAmipInterface::setVol(kvs_int_t & iVol)
{
	if(!amip_dll)
		return false;
	char volcmd[AC_BUFFER_SIZE];
	sprintf(volcmd, "control vol %d", iVol);
	return (ac_exec(volcmd) == AC_ERR_NOERROR);
}

int MpAmipInterface::getVol()
{
	return eval_int("var_vol");
}

bool MpAmipInterface::jumpTo(kvs_int_t & iPos)
{
	if(!amip_dll)
		return false;
	char jmpcmd[AC_BUFFER_SIZE];
	sprintf(jmpcmd, "jumptotime %d", iPos / 1000);
	return (ac_exec(jmpcmd) == AC_ERR_NOERROR);
}
bool MpAmipInterface::hide()
{
	HWND hWinamp = (HWND)eval_int("var_phwnd");
	if(hWinamp && hWinamp != (HWND)-1)
	{
		ShowWindow(hWinamp, SW_HIDE);
		return true;
	}
	return false;
}

bool MpAmipInterface::show()
{
	HWND hWinamp = (HWND)eval_int("var_phwnd");
	if(hWinamp && hWinamp != (HWND)-1)
	{
		ShowWindow(hWinamp, SW_SHOW);
		return true;
	}
	return false;
}

bool MpAmipInterface::minimize()
{
	if(!amip_dll)
		return false;
	return (ac_exec("control mimimize") == AC_ERR_NOERROR);
}

bool MpAmipInterface::setPlayListPos(kvs_int_t & iPos)
{
	if(!amip_dll)
		return false;
	char jmpcmd[AC_BUFFER_SIZE];
	sprintf(jmpcmd, "setplpos %d", iPos + 1);
	return (ac_exec(jmpcmd) == AC_ERR_NOERROR);
}

int MpAmipInterface::getPlayListPos()
{
	return eval_int("var_pos");
}

int MpAmipInterface::getListLength()
{
	return eval_int("var_ll");
}

bool MpAmipInterface::getRepeat()
{
	return eval_str("var_repeat").startsWith("on");
}

bool MpAmipInterface::getShuffle()
{
	return eval_str("var_shuffle").startsWith("on");
}

bool MpAmipInterface::setShuffle(bool & bVal)
{
	if(!amip_dll)
		return false;
	bool res;
	if(bVal)
		res = (ac_exec("setshuffle on") == AC_ERR_NOERROR);
	else
		res = (ac_exec("setshuffle off") == AC_ERR_NOERROR);
	return res;
}

bool MpAmipInterface::setRepeat(bool & bVal)
{
	if(!amip_dll)
		return false;
	bool res;
	if(bVal)
		res = (ac_exec("setrepeat on") == AC_ERR_NOERROR);
	else
		res = (ac_exec("setrepeat off") == AC_ERR_NOERROR);
	return res;
}

bool MpAmipInterface::amipExec(const QString & cmd)
{
	if(!amip_dll)
		return false;
	QTextCodec * c = mediaplayer_get_codec();
	KviCString szCmd = c ? c->fromUnicode(cmd) : cmd.toUtf8();
	return (ac_exec(szCmd) == AC_ERR_NOERROR);
}

QString MpAmipInterface::amipEval(const QString & cmd)
{
	QString ret;
	if(!amip_dll)
		return ret;
	QTextCodec * c = mediaplayer_get_codec();
	KviCString szCmd = c ? c->fromUnicode(cmd) : cmd.toUtf8();
	char buff[AC_BUFFER_SIZE];
	if((ac_eval(szCmd, buff) == AC_ERR_NOERROR))
	{
		QString s = buff;
		QTextCodec * c = mediaplayer_get_codec();
		if(c)
			ret = c->toUnicode(s.toLatin1());
		else
			ret = s;
	}
	return ret;
}

#endif //COMPILE_ON_WINDOWS