summaryrefslogtreecommitdiff
path: root/plugins/Watrack/players/pl_aimp.pas
blob: 410a44d5f0b777a180bc4e1bf78f5db54ac04fa3 (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
{AIMP player}
unit pl_AIMP;
{$include compilers.inc}

interface

implementation
uses windows,messages,common,srv_player,wat_api,winampapi;

const
  WM_AIMP_COMMAND = WM_USER + $75;
  WM_AIMP_GET_VERSION = 4;
  WM_AIMP_STATUS_GET  = 1;
  WM_AIMP_STATUS_SET  = 2;
  WM_AIMP_CALLFUNC    = 3;
const
  AIMP_STS_Player = 4;
  AIMP_STS_VOLUME = 1;
  AIMP_STS_POS    = 31;
const
  AIMP_PLAY  = 15;
  AIMP_PAUSE = 16;
  AIMP_STOP  = 17;
  AIMP_NEXT  = 18;
  AIMP_PREV  = 19;

const
  AIMP2_RemoteClass:PAnsiChar = 'AIMP2_RemoteInfo';
const
  AIMP2_RemoteFileSize = 2048;

type
  PAIMP2FileInfo = ^TAIMP2FileInfo;
  TAIMP2FileInfo = packed record
    cbSizeOF    :dword;
    //
    nActive     :LongBool;
    nBitRate    :dword;
    nChannels   :dword;
    nDuration   :dword;
    nFileSize   :Int64;
    nRating     :dword;
    nSampleRate :dword;
    nTrackID    :dword;
    //
    nAlbumLen   :dword;
    nArtistLen  :dword;
    nDateLen    :dword;
    nFileNameLen:dword;
    nGenreLen   :dword;
    nTitleLen   :dword;
    //
    sAlbum      :dword; // size of pointer for 32 bit system
    sArtist     :dword;
    sDate       :dword;
    sFileName   :dword;
    sGenre      :dword;
    sTitle      :dword;
  end;

function Check(wnd:HWND;flags:integer):HWND;cdecl;
begin
  if wnd<>0 then
  begin
    result:=0;
    exit;
  end;
  result:=FindWindowA(AIMP2_RemoteClass,AIMP2_RemoteClass);
end;

function GetVersionText(ver:integer):pWideChar;
begin
  if (ver and $F00)<>0 then
  begin
    mGetMem(result,8*SizeOf(WideChar));
    result[0]:=WideChar((ver div 1000)+ORD('0'));
    ver:=ver mod 1000;
    result[1]:='.';
    result[2]:=WideChar((ver div 100)+ORD('0'));
    ver:=ver mod 100;
    result[3]:='.';
    result[4]:=WideChar((ver div 10)+ORD('0'));
    result[5]:='.';
    result[6]:=WideChar((ver mod 10)+ORD('0'));
    result[7]:=#0;
  end
  else
    result:=nil;
end;

function GetVersion(wnd:HWND):integer;
begin
  result:=SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_GET_VERSION,0);
end;

function GetStatus(wnd:HWND):integer; cdecl;
begin
  result:=SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_STATUS_GET,AIMP_STS_Player);
end;

function GetVolume(wnd:HWND):cardinal;
begin
  result:=SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_STATUS_GET,AIMP_STS_VOLUME);
  result:=(result shl 16)+round((result shl 4)/100);
end;

procedure SetVolume(wnd:HWND;value:cardinal);
begin
  SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_STATUS_SET,
      (AIMP_STS_VOLUME shl 16)+((value*100) shr 4));
end;

function VolDn(wnd:HWND):integer;
var
  val:dword;
begin
  result:=GetVolume(wnd);
  val:=loword(result);
  if val>0 then
    SetVolume(wnd,val-1);
end;

function VolUp(wnd:HWND):integer;
var
  val:dword;
begin
  result:=GetVolume(wnd);
  val:=loword(result);
  if val<16 then
    SetVolume(wnd,val+1);
end;

function GetFileName(wnd:HWND;flags:integer):pWideChar;cdecl;
var
  FFile:THANDLE;
  pStr:pointer;
  s:integer;
  p:PAnsiChar;
  pw,pw1:pWideChar;
begin
  result:=nil;
  s:=AIMP2_RemoteFileSize;
  p:=AIMP2_RemoteClass;
  FFile:=OpenFileMappingA(FILE_MAP_READ,True,p);
  if FFile<>0 then
  begin
    pStr:=MapViewOfFile(FFile,FILE_MAP_READ,0,0,s);
    if pStr<>nil then
    begin
      try
        with PAIMP2FileInfo(pStr)^ do
        begin
          StrDupW(result,
            pWideChar(PAnsiChar(pStr)+SizeOf(TAIMP2FileInfo)+
               (nAlbumLen+nArtistLen+nDateLen)*SizeOf(WideChar)),
            nFileNameLen);
            // Delete rest index (like "filename.cue:3")
            pw :=StrRScanW(result,':');
            if pw<>nil then
            begin
              pw1:=StrScanW (result,':');
              if pw<>pw1 then
                pw^:=#0;
            end;
        end;
      except
      end;
      UnmapViewOfFile(pStr);
    end;
    CloseHandle(FFile);
  end;
end;

procedure TranslateRadio(var SongInfo:tSongInfo);
var
  pc,pc1:pWideChar;
begin
{
  artist - album - title (radio)
}
  with SongInfo  do
  begin
    if (artist=nil) and (title<>nil) then
    begin
      // Radio title
      if (StrEndW(title)-1)^=')' then
      begin
        pc:=StrRScanW(title,'(');
        if (pc<>nil) and (pc>title) and ((pc-1)^=' ') then
        begin
           if comment=nil then
           begin
             StrDupW(comment,pc+1);
             (StrEndW(comment)-1)^:=#0;
           end;
           (pc-1)^:=#0;
        end;
      end;
      // artist - title
      pc:=StrPosW(title,' - ');
      if pc<>nil then
      begin
        if artist=nil then
        begin
          pc^:=#0;
          inc(pc,3);
          StrDupW(artist,title);
        end;
        // artist - album - title
        pc1:=StrPosW(pc,' - ');
        if pc1<>nil then
        begin
          if album=nil then
          begin
            pc1^:=#0;
            StrDupW(album,pc);
            pc:=pc1+3;
          end;
        end;
        pc1:=title;
        StrDupW(title,pc);
        mFreeMem(pc1);
      end;
    end;
  end;
end;

function GetInfo(var SongInfo:tSongInfo;flags:integer):integer;cdecl;
var
  FFile:THANDLE;
  s:integer;
  p:PAnsiChar;
  pStr:PAIMP2FileInfo;
begin
  result:=0;
  if (flags and WAT_OPT_PLAYERDATA)<>0 then
  begin
    if SongInfo.plyver=0 then
    begin
      SongInfo.plyver:=GetVersion    (SongInfo.plwnd);
      SongInfo.txtver:=GetVersionText(SongInfo.plyver);
    end;
    if SongInfo.winampwnd=0 then
      SongInfo.winampwnd:=WinampFindWindow(SongInfo.plwnd);
    exit;
  end;

  if SongInfo.winampwnd<>0 then
    WinampGetInfo(int_ptr(@SongInfo),flags);
    
  if (flags and WAT_OPT_CHANGES)=0 then
  begin
    s:=AIMP2_RemoteFileSize;
    p:=AIMP2_RemoteClass;
    FFile:=OpenFileMappingA(FILE_MAP_READ,True,p);
    if FFile<>0 then
    begin
      pStr:=MapViewOfFile(FFile,FILE_MAP_READ,0,0,s);
      if pStr<>nil then
      begin
        try
          with SongInfo do
          begin
            with pStr^ do
            begin
              if channels=0 then channels:=nChannels;
              if kbps    =0 then kbps    :=nBitRate    div 1000;
              if khz     =0 then khz     :=nSampleRate div 1000;
              if total   =0 then total   :=nduration;
              if fsize   =0 then fsize   :=nFileSize;
              if track   =0 then track   :=nTrackID;

              with PAIMP2FileInfo(pStr)^ do
              begin
                if (artist=nil) and (nArtistLen>0) then
                begin
                  StrDupW(artist,
                    pWideChar(PAnsiChar(pStr)+SizeOf(TAIMP2FileInfo))+
                       nAlbumLen,nArtistLen);
                end
                else
                  artist:=StrEmpty();
                if (album=nil) and (nAlbumLen>0) then
                begin
                  StrDupW(album,
                    pWideChar(PAnsiChar(pStr)+SizeOf(TAIMP2FileInfo)),
                    nAlbumLen);
                end;
                if (title=nil) and (nTitleLen>0) then
                begin
                  StrDupW(title,
                    pWideChar(PAnsiChar(pStr)+SizeOf(TAIMP2FileInfo))+
                       nAlbumLen+nArtistLen+nDateLen+nFileNameLen+nGenreLen,
                    nTitleLen);
                end
                else
                  title:=StrEmpty();
                if (year=nil) and (nDateLen>0) then
                begin
                  StrDupW(year,
                    pWideChar(PAnsiChar(pStr)+SizeOf(TAIMP2FileInfo))+
                       nAlbumLen+nArtistLen,
                    nDateLen);
                end;
                if (genre=nil) and (nGenreLen>0) then
                begin
                  StrDupW(genre,
                    pWideChar(PAnsiChar(pStr)+SizeOf(TAIMP2FileInfo))+
                       nAlbumLen+nArtistLen+nDateLen+nFileNameLen,
                    nGenreLen);
                end;

                if StrPosW(mfile,'://')<>nil then
                  TranslateRadio(SongInfo);
              end;
            end;
          end;
        except
        end;
        UnmapViewOfFile(pStr);
      end;
      CloseHandle(FFile);
    end;
  end
  else // request AIMP changed data: volume
  begin
    SongInfo.time:=SendMessage(SongInfo.plwnd,WM_AIMP_COMMAND,WM_AIMP_STATUS_GET,AIMP_STS_POS);
    SongInfo.volume:=GetVolume(SongInfo.plwnd);
  end;
end;

function Command(wnd:HWND;cmd:integer;value:integer):integer;cdecl;
var
  WinampWindow:HWND;
begin
  WinampWindow:=WinampFindWindow(wnd);
  if WinampWindow<>0 then
    result:=WinampCommand(WinampWindow,cmd+(value shl 16))
  else
  begin
    result:=0;
    case cmd of
      WAT_CTRL_PREV : SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_CALLFUNC,AIMP_PREV);
      WAT_CTRL_PLAY : SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_CALLFUNC,AIMP_PLAY);
      WAT_CTRL_PAUSE: SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_CALLFUNC,AIMP_PAUSE);
      WAT_CTRL_STOP : SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_CALLFUNC,AIMP_STOP);
      WAT_CTRL_NEXT : SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_CALLFUNC,AIMP_NEXT);
      WAT_CTRL_VOLDN: result:=VolDn(wnd);
      WAT_CTRL_VOLUP: result:=VolUp(wnd);
      WAT_CTRL_SEEK : begin
        SendMessage(wnd,WM_AIMP_COMMAND,WM_AIMP_STATUS_SET,
                      (AIMP_STS_POS shl 16)+value);
      end;
    end;
  end;
end;

const
  plRec:tPlayerCell=(
    Desc     :'AIMP';
    flags    :WAT_OPT_APPCOMMAND or WAT_OPT_HASURL;
    Icon     :0;
    Init     :nil;
    DeInit   :nil;
    Check    :@Check;
    GetStatus:@GetStatus;
    GetName  :@GetFileName;
    GetInfo  :@GetInfo;
    Command  :@Command;
    URL      :'http://www.aimp.ru/';
    Notes    :nil);

var
  LocalPlayerLink:twPlayer;

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

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