summaryrefslogtreecommitdiffstats
path: root/src/modules/mediaplayer/mp_amipinterface.cpp
blob: 3a363ed5094f8f1f10a87894706977877b79861d (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
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
#include "mp_amipinterface.h"
#include "kvi_options.h"

#ifdef COMPILE_ON_WINDOWS

#include <tqtextcodec.h>

#include "kvi_locale.h"
#include "kvi_module.h"

#include <windows.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 = NULL;

#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("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 TQTextCodec * mediaplayer_get_codec()
{
  TQTextCodec * c= TQTextCodec::codecForName(KVI_OPTION_STRING(KviOption_stringWinampTextEncoding)); 
  if(!c)c = TQTextCodec::codecForLocale(); 
  return c;
}

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



KviAmipInterface::KviAmipInterface()
: KviMediaPlayerInterface()
{
  if(!amip_dll) {
    bool res = loadAmipDll();
    if(!res) {
      amip_dll = NULL;
      return;
    }
    ac_init(AC_START_CLIENT);
  }
}

KviAmipInterface::~KviAmipInterface()
{
  if(!amip_dll) return;
  ac_uninit();
  FreeLibrary(amip_dll);
  amip_dll = NULL;
}


int KviAmipInterface::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 KviAmipInterface::__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;
}

TQString eval_str(const char *var)
{
  TQString 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 KviAmipInterface::length()
{
  return eval_int("var_sl") * 1000;
}

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

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

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

int KviAmipInterface::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;
}

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

TQString KviAmipInterface::mrl()
{
  TQString ret;
  TQString fn = eval_str("var_fn");
  TQTextCodec *c=mediaplayer_get_codec();
  if (c) ret = c->toUnicode(fn);
  else ret=fn;
  if(!ret.startsWith("http://",false))
    ret.prepend("file://");
  
  return ret;
}

TQString getAmipString(const char * var) {
  TQString ret;
  TQString s = eval_str(var);
  TQTextCodec *c=mediaplayer_get_codec();
  if (c) ret = c->toUnicode(s);
  else ret=s;
  return ret;
}

TQString KviAmipInterface::nowPlaying()
{
  return getAmipString("var_s");
}

TQString KviAmipInterface::artist()
{
  return getAmipString("var_1");
}

TQString KviAmipInterface::title()
{
  return getAmipString("var_2");
}

TQString KviAmipInterface::album()
{
  return getAmipString("var_4");
}

TQString KviAmipInterface::year()
{
  return getAmipString("var_5");
}

TQString KviAmipInterface::comment()
{
  return getAmipString("var_6");
}

TQString KviAmipInterface::genre()
{
  return getAmipString("var_7");
}

bool KviAmipInterface::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 KviAmipInterface::getVol()
{
  return eval_int("var_vol");
}

bool KviAmipInterface::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 KviAmipInterface::hide()
{
  HWND hWinamp = (HWND)eval_int("var_phwnd");
  if(hWinamp && hWinamp != (HWND)-1)
  {
    ShowWindow(hWinamp, SW_HIDE);
    return true;
  }
  return false;
}

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

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

bool KviAmipInterface::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 KviAmipInterface::getPlayListPos()
{
  return eval_int("var_pos");
}

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

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

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

bool KviAmipInterface::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 KviAmipInterface::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 KviAmipInterface::amipExec(const TQString &cmd)
{
  if(!amip_dll) return false;
  TQTextCodec *c=mediaplayer_get_codec();
  KviStr szCmd = c ? c->fromUnicode(cmd) : cmd.utf8();
  return (ac_exec(szCmd) == AC_ERR_NOERROR);
}

TQString KviAmipInterface::amipEval(const TQString &cmd)
{
  TQString ret;
  if(!amip_dll) return ret;
  TQTextCodec *c=mediaplayer_get_codec();
  KviStr szCmd = c ? c->fromUnicode(cmd) : cmd.utf8();
  char buff[AC_BUFFER_SIZE];
  if((ac_eval(szCmd, buff) == AC_ERR_NOERROR)) {
    TQString s = buff;
    TQTextCodec *c=mediaplayer_get_codec();
    if (c) ret = c->toUnicode(s);
    else ret=s;
  }
  return ret;
}

#endif //COMPILE_ON_WINDOWS