summaryrefslogtreecommitdiff
path: root/plugins/Watrack/players/pl_foobar.pas
blob: 7802440ad613f84a9335a81c2ca459f78379f817 (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
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
525
526
527
528
529
530
531
532
533
534
{Foobar2000 player}
unit pl_Foobar;
{$include compilers.inc}

interface

implementation
uses {$IFDEF KOL_MCK}err,{$ENDIF}
   windows,common,syswin,wrapper,srv_player,messages,wat_api,winampapi
  {$IFDEF DELPHI_7_UP}
  ,variants
  {$ENDIF}
  {$IFDEF KOL_MCK}
  ,kolcomobj
  {$ELSE}
  ,mComObj
  {$ENDIF}
;

const
  COMName:PAnsiChar = 'Foobar2000.Application.0.7';
const
  dummywnd  = 'uninteresting';
const
  FooExe    = 'FOOBAR2000.EXE';
  FooPrefix = 'foobar2000 v';
const
  FooBarClassExt = '{97E27FAA-C0B3-4b8e-A693-ED7881E99FC1}';
  FooBarClassNew = '{DA7CD0DE-1602-45e6-89A1-C2CA151E008E}';
  FooBarClassAdd = '{E7076D1C-A7BF-4f39-B771-BCBE88F2A2A8}';
(*
  class1='{B73733CA-9B0A-4f53-93FA-AC95D4FF2166}';
  text1='Cthulhu fhtagn!';

  '{53229DFC-A273-45cd-A3A4-161FA9FC6414}';
  '{641C2469-355C-4d6f-9663-E714382DA462}';
*)
var
  WinampWindow:HWND;

function proc(awnd:hwnd;param:pdword):boolean; stdcall;
var
  s:array [0..255] of AnsiChar;
begin
  result:=true;
  if (awnd<>param^) and (GetClassNameA(awnd,s,255)>0) then
  begin
    s[Length(FooBarClassNew)]:=#0;
    if (StrCmp(s,FooBarClassExt)=0) or
       (StrCmp(s,FooBarClassNew)=0) or
       (StrCmp(s,FooBarClassAdd)=0) then
    begin
      GetWindowTextA(awnd,s,255);
      if StrCmp(s,dummywnd)<>0 then
        param^:=awnd;
//      if WinampWindow<>0 then
//        result:=false;
    end

    else if (WinampWindow=0) and (StrCmp(s,WinampClass)=0) then
    begin
      WinampWindow:=awnd;
    end;

  end;
end;

function Check(wnd:HWND;flags:integer):HWND;cdecl;
var
  tmp,EXEName:PAnsiChar;
  lwnd:HWND;
  ltmp:bool;
begin
  if wnd<>0 then
  begin
    result:=0;
    exit;
  end;
  lwnd:=0;
  repeat
    lwnd:=FindWindowEx(0,lwnd,nil,dummywnd);
    if lwnd=0 then
      break;
    tmp:=Extract(GetEXEByWnd(lwnd,EXEName),true);
    mFreeMem(EXEName);
    ltmp:=lstrcmpia(tmp,FooExe)=0;
    mFreeMem(tmp);
    if ltmp then
    begin
      WinampWindow:=0;
      EnumThreadWindows(GetWindowThreadProcessId(lwnd,nil),@proc,int_ptr(@lwnd));
      break;
    end;
  until false;
  result:=lwnd;
end;

function GetYear(const v:variant):PWideChar;
begin
  try
    StrDupW(result,PWideChar(WideString(v.Playback.FormatTitle('[%year%]'))));
  except
    result:=nil;
  end;
end;

function GetArtist(const v:variant):PWideChar;
begin
  try
    StrDupW(result,PWideChar(WideString(v.Playback.FormatTitle('[%artist%]'))));
  except
    result:=nil;
  end;
end;

function GetTitle(const v:variant):PWideChar;
begin
  try
    StrDupW(result,PWideChar(WideString(v.Playback.FormatTitle('[%title%]'))));
  except
    result:=nil;
  end;
end;

function GetAlbum(const v:variant):PWideChar;
begin
  try
    StrDupW(result,PWideChar(WideString(v.Playback.FormatTitle('[%album%]'))));
  except
    result:=nil;
  end;
end;

function GetGenre(const v:variant):PWideChar;
begin
  try
    StrDupW(result,PWideChar(WideString(v.Playback.FormatTitle('[%genre%]'))));
  except
    result:=nil;
  end;
end;

function GetBitrate(const v:variant):integer;
begin
  try
    result:=v.Playback.FormatTitle('%bitrate%');
  except
    result:=0;
  end;
end;

function GetSamplerate(const v:variant):integer;
begin
  try
    result:=v.Playback.FormatTitle('%samplerate%');
  except
    result:=0;
  end;
end;

function GetChannels(const v:variant):integer;
var
  s:WideString;
begin
  result:=0;
  try
    s:=v.Playback.FormatTitle('%channels%');
    if StrCmpW(pWideChar(s),'mono')=0 then
      result:=1
    else if StrCmpW(pWideChar(s),'Stereo')=0 then
      result:=2;
  except
  end;
end;

function GetCodec(const v:variant):integer;
var
  s:WideString;
  i:integer;
begin
  result:=0;
  try
    s:=v.Playback.FormatTitle('%codec%');
    i:=Length(s);
    if i>0 then result:=ORD(s[1]);
    if i>1 then result:=result+(ORD(s[2]) shl 8);
    if i>2 then result:=result+(ORD(s[3]) shl 16);
    if i>3 then result:=result+(ORD(s[4]) shl 24);
  except
  end;
end;

function SplitVersion(p:pWideChar):integer;
begin
  result:=StrToInt(p);
  while (p^>='0') and (p^<='9') do inc(p); inc(p);
  result:=result*16+StrToInt(p);
  while (p^>='0') and (p^<='9') do inc(p); inc(p);
  result:=result*16+StrToInt(p);
  while (p^>='0') and (p^<='9') do inc(p);
  if p^<>#0 then inc(p);
  if (p^>='0') and (p^<='9') then
  begin
    result:=result*16+StrToInt(p);
    while (p^>='0') and (p^<='9') do inc(p);
  end;
  if p^<>#0 then
  begin
    inc(p);
    while (p^<>#0) and (p^<>' ') do inc(p);
    if p^=' ' then
    begin
      inc(p);
      result:=result*16+StrToInt(p);
    end;
  end;
end;

function GetVersion(const ver:pWideChar):integer;
begin
  if (ver=nil) or (ver^=#0) then
    result:=0
  else
    result:=SplitVersion(ver);
end;

function GetVersionText(const v:variant):PWideChar;
begin
  try
    StrDupW(result,PWideChar(WideString(v.Name))+length(FooPrefix));
  except
    result:=nil;
  end;
end;

function GetTotalTime(const v:variant):integer;
begin
  try
    result:=v.Playback.Length;
  except
    result:=0;
  end;
end;

function GetElapsedTime(const v:variant):integer;
begin
  try
    result:=v.Playback.Position;
  except
    result:=0;
  end;
end;

function GetStatus(wnd:HWND):integer; cdecl;
var
  tmp:boolean;
  v:variant;
  winampwnd:HWND;
begin
  try
    result:=WAT_MES_STOPPED;
    v:=GetActiveOleObject(COMName);
    tmp:=v.Playback.IsPaused;
    if tmp then
      result:=WAT_MES_PAUSED
    else
    begin
      tmp:=v.Playback.IsPlaying;
      if tmp then
        result:=WAT_MES_PLAYING;
    end;
  except
    winampwnd:=WinampFindWindow(wnd);
    if winampwnd<>0 then
      result:=WinampGetStatus(winampwnd)
    else
      result:=WAT_MES_UNKNOWN;
  end;
  v:=null;
end;

function GetWndText(wnd:HWND):pWideChar;
var
  i:integer;
begin
  result:=GetDlgText(wnd);
  if result<>nil then
  begin
    i:=StrIndexW(result,'[foobar');
    if i<>0 then
    begin
      dec(i);
      repeat
        dec(i);
        if ord(result[i])>ord(' ') then break;
      until i<0;
      result[i+1]:=#0;//if at end
    end;
  end;
end;

function GetFileName(wnd:HWND;flags:integer):PWideChar;cdecl;
var
  v:variant;
begin
  try
    v:=GetActiveOleObject(COMName);
//    v:=CreateOleObject(COMName);
    StrDupW(result,PWideChar(WideString(v.Playback.FormatTitle('%path%'))));
  except
    result:=nil;
  end;
  v:=Null;
end;

function Play(const v:variant;fname:PWideChar=nil):integer;
begin
  try
    result:=v.Playback.Start(false);
  except
    result:=0;
  end;
end;

function Pause(const v:variant):integer;
begin
  try
    result:=v.Playback.Pause;
  except
    result:=0;
  end;
end;

function Stop(const v:variant):integer;
begin
  try
    result:=v.Playback.Stop;
  except
    result:=0;
  end;
end;

function Next(const v:variant):integer;
begin
  try
    result:=v.Playback.Next;
  except
    result:=0;
  end;
end;

function Prev(const v:variant):integer;
begin
  try
    result:=v.Playback.Previous;
  except
    result:=0;
  end;
end;

function GetVolume(const v:variant):cardinal;
begin
  try
    result:=v.Playback.Settings.Volume+100;
    result:=(result shl 16)+round((result shl 4) / 100);
  except
    result:=0;
  end;
end;

procedure SetVolume(const v:variant;value:cardinal);
begin
  try
    v.Playback.Settings.Volume:=integer(((loword(value)*100) shr 4)-100);
  except
  end;
end;

function VolDn(const v:variant):integer;
var
  val:integer;
begin
  result:=GetVolume(v);
  val:=loword(result);
  if val>0 then
    SetVolume(v,val-1);
end;

function VolUp(const v:variant):integer;
var
  val:integer;
begin
  result:=GetVolume(v);
  val:=loword(result);
  if val<16 then
    SetVolume(v,val+1);
end;

function Seek(const v:variant;value:integer):integer;
begin
  try
    result:=v.Playback.Position;
    if (value>0) and (v.Playback.CanSeek) and (value<v.Playback.Length) then
      v.Playback.Seek(value)
    else
      result:=0;
  except
    result:=0;
  end;
end;

function GetInfo(var SongInfo:tSongInfo;flags:integer):integer;cdecl;
var
  v:variant;
begin
  result:=0;
  with SongInfo do
  begin
    try
      v:=GetActiveOleObject(COMName);
//        v:=CreateOleObject(COMName);
      if (flags and WAT_OPT_PLAYERDATA)<>0 then
      begin
        if SongInfo.plyver=0 then
        begin
          SongInfo.txtver:=GetVersionText(v);
          SongInfo.plyver:=GetVersion(txtver);
        end;
      end
      else if (flags and WAT_OPT_CHANGES)<>0 then
      begin
        volume:=GetVolume(v);
        if status<>WAT_MES_STOPPED then
          time:=GetElapsedTime(v);
      end
      else
      begin
        if kbps    =0   then kbps    :=GetBitrate(v);
        if khz     =0   then khz     :=GetSamplerate(v);
        if channels=0   then channels:=GetChannels(v);
        if codec   =0   then codec   :=GetCodec(v);
        if total   =0   then total   :=GetTotalTime(v);
        if year    =NIL then year    :=GetYear(v);
        if artist  =NIL then artist  :=GetArtist(v);
        if title   =NIL then title   :=GetTitle(v);
        if album   =NIL then album   :=GetAlbum(v);
        if genre   =NIL then genre   :=GetGenre(v);
      end;
    except
      SongInfo.winampwnd:=WinampWindow;
      if SongInfo.winampwnd<>0 then
      begin
        result:=WinampGetInfo(int_ptr(@SongInfo),flags);
      end;
    end;
    v:=Null;
    if (flags and WAT_OPT_CHANGES)<>0 then
      wndtext:=GetWndText(SongInfo.plwnd);
  end;
end;

function Command(wnd:HWND;cmd:integer;value:int_ptr):integer;cdecl;
//var
//  c:integer;
var
  v:Variant;
begin
  result:=0;
  try
    v:=GetActiveOleObject(COMName);
    case cmd of
      WAT_CTRL_PREV : result:=Prev (v);
      WAT_CTRL_PLAY : result:=Play (v,pWideChar(value));
      WAT_CTRL_PAUSE: result:=Pause(v);
      WAT_CTRL_STOP : result:=Stop (v);
      WAT_CTRL_NEXT : result:=Next (v);
      WAT_CTRL_VOLDN: result:=VolDn(v);
      WAT_CTRL_VOLUP: result:=VolUp(v);
      WAT_CTRL_SEEK : result:=Seek (v,value);
    end;
  except
    if WinampWindow<>0 then
      result:=WinampCommand(WinampWindow,cmd+(value shl 16))
{
    else
    begin
      case cmd of
        WAT_CTRL_PREV : c:=ORD('B');
        WAT_CTRL_PLAY : c:=ORD('C');
        WAT_CTRL_PAUSE: c:=ORD('X');
        WAT_CTRL_STOP : c:=ORD('Z');
        WAT_CTRL_NEXT : c:=ORD('V');
        WAT_CTRL_VOLDN: c:=VK_SUBTRACT;
        WAT_CTRL_VOLUP: c:=VK_ADD;
        else
          exit;
      end;
      PostMessageW(wnd,WM_KEYDOWN,c,1);
    end;
}
  end;
  v:=Null;
end;

const
  plRec:tPlayerCell=(
    Desc     :'foobar2000';
    flags    :WAT_OPT_SINGLEINST or WAT_OPT_HASURL;
    Icon     :0;
    Init     :nil;
    DeInit   :nil;
    Check    :@Check;
    GetStatus:@GetStatus;
    GetName  :@GetFileName;
    GetInfo  :@GetInfo;
    Command  :@Command;
    URL      :'http://www.foobar2000.org/';
    Notes    :'For more full info WinampSpam or foo_comserver (more powerful) '#13#10+
              'components needs to be installed.');

var
  LocalPlayerLink:twPlayer;

procedure InitLink;
begin
  LocalPlayerLink.Next:=PlayerLink;
  LocalPlayerLink.This:=@plRec;
  PlayerLink          :=@LocalPlayerLink;
end;

initialization
//  ServicePlayer(WAT_ACT_REGISTER,dword(@plRec));
  InitLink;
end.