1 //---------------------------------------------------------------------------
  2 
  3 #include <vcl.h>
  4 #pragma hdrstop
  5 
  6 #include "frontend.h"
  7 #include "wa_ipc.h"
  8 
  9 #include "Player.h"
 10 #include "crc32.h"
 11 #include "browseamp.h"
 12 
 13 #include "MyWndProc.h"
 14 
 15 
 16 //---------------------------------------------------------------------------
 17 #pragma package(smart_init)
 18 TPlayer *Player;
 19 
 20  LRESULT CALLBACK MyWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 21  {
 22  CallWindowProc (OldWndProc, hWnd, uMsg, wParam, lParam);
 23  if(uMsg >= WM_USER)
 24     {
 25 //    AnsiString tmp;
 26 //    tmp.printf("%ld, wParam=%ld, lParam=%ld",uMsg, wParam, lParam);
 27 //    ShowMessage(tmp);
 28 
 29     if(BrowseAmp->RemovePlayed)
 30         {
 31         if(uMsg == (WM_USER+2)) Player->DeleteIndex(Player->PrevIndex);
 32         }
 33      }
 34  return(true);
 35  }
 36 
 37 //---------------------------------------------------------------------------
 38 __fastcall TPlayer::TPlayer()
 39 {
 40  FSupportedExtensions = "";
 41  hwndPlayer = hwndWinampPlaylist = hwndEqualizer = NULL;
 42  hwndPlayer = FindWindow("Winamp v1.x",NULL);
 43 
 44  hwndWinampPlaylist = GetWindowHandle(IPC_GETWND_PE);
 45  hwndEqualizer = GetWindowHandle(IPC_GETWND_EQ);
 46 
 47  FInternalTrackIndex = CurrentIndex;
 48 /*
 49  #ifdef STRICT
 50   OldWndProc = (WNDPROC)GetWindowLong (hwndPlayer, GWL_WNDPROC);
 51  #else
 52   OldWndProc = (FARPROC)GetWindowLong (hwndPlayer, GWL_WNDPROC);
 53  #endif
 54  SetWindowLong (hwndPlayer, GWL_WNDPROC, (long) MyWndProc);
 55  */
 56 }
 57 
 58 
 59 
 60 __fastcall TPlayer::~TPlayer()
 61 {
 62 }
 63 
 64 
 65 // Player information  .........................................................
 66 //==============================================================================
 67 
 68 
 69 HWND __fastcall TPlayer::GetWindowHandle(int window)
 70  {
 71  return((HWND)SendMessage(hwndPlayer,WM_WA_IPC,window,IPC_GETWND));
 72  }
 73 
 74 
 75 long __fastcall TPlayer::FGetInternalTrackIndex()
 76  {
 77  return(FInternalTrackIndex);
 78  }
 79 
 80 void __fastcall TPlayer::FSetInternalTrackIndex(long Index)
 81  {
 82  FInternalTrackIndex = Index;
 83  }
 84 
 85 AnsiString __fastcall TPlayer::FPlayerVersion()
 86 {
 87 AnsiString tmp;
 88 if(hwndPlayer)
 89     {
 90     int version = SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GETVERSION);
 91     tmp.printf("%x",version);
 92     tmp[3]=tmp[2];
 93     tmp[2]='.';
 94     return tmp;
 95     }
 96 else return "";
 97 }
 98 
 99 
100 AnsiString __fastcall TPlayer::FPlayerDirectory()
101 {
102  return(Application->ExeName.SubString(1,Application->ExeName.LastDelimiter("\\")));
103 }
104 
105 void __fastcall TPlayer::FStatusMessage(AnsiString Msg)
106 {
107 }
108 
109 int __fastcall TPlayer::FStatus(void)
110 {
111  return(SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_ISPLAYING));
112 }
113 
114 
115 
116 AnsiString __fastcall TPlayer::FGetSupportedExtensions()
117 {
118  if(!FSupportedExtensions.Length())
119     {
120     char *extlist = (char *)SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GET_EXTLIST);
121     for(int i=0;i<5000;i++)
122     if(extlist[i] == 0)
123         {
124         extlist[i] = ':';
125         if ((i >= 1) && (extlist[i-1]==':'))
126             {
127             extlist[i-1] = 0;
128             extlist[i] = 0;
129             break;
130             }
131         }
132     FSupportedExtensions.printf("%s",extlist);
133     GlobalFree(HGLOBAL(extlist)); // make sure to free it
134     }
135  return(FSupportedExtensions);
136 }
137 
138 
139 // global Player commands  .....................................................
140 //==============================================================================
141 int __fastcall TPlayer::FPlayerReadVolume()
142 {
143  return(FInternalVolume);
144 }
145 
146 void __fastcall TPlayer::FPlayerWriteVolume(int newvol)
147 {
148  FInternalVolume = newvol;
149  SendMessage(hwndPlayer,WM_WA_IPC,long((float)(FInternalVolume)*2.55),IPC_SETVOLUME);
150 }
151 
152 int __fastcall TPlayer::FPlayerReadBalance()
153 {
154  return(FInternalBalance);
155 }
156 
157 void __fastcall TPlayer::FPlayerWriteBalance(int newbal)
158 {
159  FInternalBalance = newbal;
160  SendMessage(hwndPlayer,WM_WA_IPC,long((float)(FInternalBalance+0)*1.27),IPC_SETPANNING);
161 }
162 
163 
164 void __fastcall TPlayer::Minimize(void)
165  {
166  SendMessage(hwndPlayer, WM_SYSCOMMAND, SC_MINIMIZE , 0);
167  }
168 
169 void __fastcall TPlayer::Close(void)
170  {
171  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_CLOSE, 0);
172  }
173 
174 void __fastcall TPlayer::Restart(void)
175  {
176  SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_RESTARTWINAMP);
177  }
178 
179 void __fastcall TPlayer::ExecutePlugin(void)
180  {
181  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_EXECUTE_VIS, 0);
182  }
183 
184 
185 // Playback Commands  ..........................................................
186 //==============================================================================
187 void __fastcall TPlayer::Play()
188 {
189  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_BUTTON2, 0);
190 }
191 
192 void __fastcall TPlayer::Pause()
193 {
194  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_BUTTON3, 0);
195 }
196 
197 void __fastcall TPlayer::Stop()
198 {
199  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_BUTTON4, 0);
200 }
201 
202 void __fastcall TPlayer::TrackFwd()
203 {
204  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_BUTTON5, 0);
205 }
206 
207 void __fastcall TPlayer::TrackBwd()
208 {
209  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_BUTTON1, 0);
210 }
211 
212 void __fastcall TPlayer::SeekFwd()
213 {
214  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_FFWD5S, 0);
215 }
216 
217 void __fastcall TPlayer::SeekBwd()
218 {
219  SendMessage(hwndPlayer,WM_COMMAND, WINAMP_REW5S, 0);
220 }
221 
222 void __fastcall TPlayer::PlayIndex(int num)
223 {
224  SendMessage(hwndPlayer,WM_WA_IPC, num, IPC_SETPLAYLISTPOS);
225  Play();
226 }
227 
228 
229 void __fastcall TPlayer::FRepeatTrack(bool state)
230 {
231  SendMessage(hwndPlayer,WM_WA_IPC,state,IPC_SET_REPEAT);
232 }
233 
234 void __fastcall TPlayer::FShuffle(bool state)
235 {
236  SendMessage(hwndPlayer,WM_WA_IPC,state,IPC_SET_SHUFFLE);
237 }
238 
239 
240 // current song information ....................................................
241 //==============================================================================
242 
243 AnsiString __fastcall TPlayer::FCurrentArtist()
244  {
245  return( GetMetaData( GetTrackFile( FGetCurrentIndex() ),"Artist" ) );
246  }
247 
248 
249 AnsiString __fastcall TPlayer::FCurrentTitle()
250  {
251  return(GetTrackName(FGetCurrentIndex()));
252  }
253 
254  /*
255 AnsiString __fastcall TPlayer::FCurrentTrackName()
256 {
257 
258  LPDWORD ProcessID, BytesRead;
259  HANDLE PlayerProcessHandle;
260  LPCVOID Pointer;
261 
262  if(hwndPlayer)
263     {
264     ProcessID = new(DWORD);
265     BytesRead = new(DWORD);
266 
267     char Data[1024];
268     int ListLength = SendMessage(hwndPlayer, WM_USER, 0, 124);
269     int TrackPos = SendMessage(hwndPlayer, WM_USER, 0, 125);
270 
271     if(ListLength>0)
272         {
273         Pointer = (LPCVOID)SendMessage(hwndPlayer, WM_USER, TrackPos, 212);
274         GetWindowThreadProcessId(hwndPlayer, ProcessID);
275         PlayerProcessHandle = OpenProcess(PROCESS_VM_READ, False, *ProcessID);
276         ReadProcessMemory(PlayerProcessHandle, Pointer, Data, 1024, BytesRead);
277         CloseHandle(PlayerProcessHandle);
278         }
279     else strcpy(Data,"---");
280     delete BytesRead;
281     delete ProcessID;
282     return AnsiString(Data);
283     }
284  else return "";
285 
286  }
287 */
288 
289 long __fastcall TPlayer::FGetSongPosition(void)
290  {
291  return( MAX( SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GETOUTPUTTIME),0 ) )/1000;
292  }
293 
294 void __fastcall TPlayer::FSetSongPosition(long pos)
295  {
296  SendMessage(hwndPlayer,WM_WA_IPC,pos,IPC_JUMPTOTIME);
297  }
298 
299 
300 long __fastcall TPlayer::FGetSongRemain(void)
301  {
302  return(GetTrackLength(-1) - FGetSongPosition());
303  }
304 
305 
306 int __fastcall TPlayer::GetTrackLength(int index)
307  {
308  if(index == -1)
309      return(MAX(SendMessage(hwndPlayer,WM_WA_IPC,1,IPC_GETOUTPUTTIME),0));
310  else
311     {
312 /*    basicFileInfoStruct *data = new basicFileInfoStruct;
313     data->filename = GetTrackFile(index).c_str();
314     data->quickCheck = 0;
315     if(SendMessage(hwndPlayer,WM_WA_IPC,(WPARAM)data,IPC_GET_BASIC_FILE_INFO))
316         {
317         int tmp = data->length;
318         delete data;
319         return(tmp);
320         }
321     delete data;
322 */    return(0);
323     }
324  }
325 
326 
327 long __fastcall TPlayer::GetAudioInfo(int par1)
328  {
329  return(SendMessage(hwndPlayer,WM_WA_IPC,par1,IPC_GETINFO));
330  }
331 
332 
333 long __fastcall TPlayer::FGetCurrentSamplerate(void)
334  {
335  return(GetAudioInfo(0));
336  }
337 
338 
339 long __fastcall TPlayer::FGetCurrentBitrate(void)
340  {
341  return(GetAudioInfo(1));
342  }
343 
344 
345 long __fastcall TPlayer::FGetCurrentChannels(void)
346  {
347  return(GetAudioInfo(2));
348  }
349 
350 
351 long __fastcall TPlayer::FGetCurrentIndex(void)
352 {
353  return(SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GETLISTPOS));
354 }
355 
356 bool __fastcall TPlayer::FShuffleState(void)
357 {
358  return(SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GET_SHUFFLE));
359 }
360 
361 bool __fastcall TPlayer::FRepeatState(void)
362 {
363  return(SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GET_REPEAT));
364 }
365 
366 
367 
368 // playlist management .........................................................
369 //==============================================================================
370 
371 void __fastcall TPlayer::AddFile(int Pos, AnsiString FileName)
372 {
373  if(Pos == -1)
374     {
375     COPYDATASTRUCT cds;
376     cds.dwData = IPC_PLAYFILE;
377     cds.lpData = FileName.c_str();
378     cds.cbData = strlen((char *) cds.lpData)+1; // include space for null char
379     SendMessage(hwndPlayer,WM_COPYDATA,(WPARAM)NULL,(LPARAM)&cds);
380     }
381  else
382     {
383     typedef struct {
384     char file[MAX_PATH];
385     int index;
386     } fileinfo;
387 
388     COPYDATASTRUCT cds;
389     fileinfo f;
390 
391     if(hwndWinampPlaylist && hwndPlayer)
392         {
393         cds.dwData = 106;
394         strcpy(f.file, FileName.c_str()); // Path to the new entry
395         f.index = FGetCurrentIndex() + 1;
396 
397         cds.lpData = (void *)&f;
398         cds.cbData = sizeof(fileinfo);
399         SendMessage(hwndWinampPlaylist ,WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds);
400         }
401     }
402 }
403 
404 void __fastcall TPlayer::ClearPlaylist()
405 {
406  SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_DELETE);
407 }
408 
409 
410 void __fastcall TPlayer::DeleteIndex(int Index)
411 {
412 SendMessage(hwndWinampPlaylist,WM_WA_IPC, IPC_PE_DELETEINDEX, Index);
413 }
414 
415 
416 long __fastcall TPlayer::FGetNumTracks()
417 {
418  return(SendMessage(hwndPlayer,WM_WA_IPC,0,IPC_GETLISTLENGTH));
419 }
420 
421 
422 void __fastcall TPlayer::FSetPlayNext(long Pos)
423 {
424 }
425 
426 
427 long __fastcall TPlayer::FGetPlayNext(void)
428 {
429  return(-1);
430 }
431 
432 
433 AnsiString __fastcall TPlayer::GetTrackFile(int Index)
434  {
435  char *tmp;
436  AnsiString Name;
437  tmp = (char*)SendMessage(hwndPlayer,WM_WA_IPC,Index,IPC_GETPLAYLISTFILE);
438  Name.printf("%s",tmp);
439  return(Name);
440  }
441 
442 
443 AnsiString __fastcall TPlayer::GetTrackName(int Index)
444  {
445  char *tmp;
446  AnsiString Name;
447  tmp = (char*)SendMessage(hwndPlayer,WM_WA_IPC,Index,IPC_GETPLAYLISTTITLE);
448  if(tmp)
449     {
450     Name.printf("%s",tmp);
451     return(Name);
452     }
453  else return("");
454  }
455 
456 
457 AnsiString __fastcall TPlayer::GetTitle(int Index)
458  {
459 /*
460  char buffer[4096];
461  extendedFileInfoStruct data;
462  data.filename = GetTrackFile(Index).c_str();
463  data.metadata = "Title";
464  data.ret = buffer;
465  data.retlen = 4096;
466  if(SendMessage(hwndPlayer,WM_WA_IPC,(WPARAM)&data,IPC_GET_EXTENDED_FILE_INFO))
467     {
468     return(AnsiString(buffer));
469     }
470  */
471  return("");
472  }
473 
474 AnsiString __fastcall TPlayer::GetArtistName(int Index)
475  {
476 /*
477  char buffer[4096];
478  extendedFileInfoStruct data;
479  data.filename = GetTrackFile(Index).c_str();
480  data.metadata = "Artist";
481  data.ret = buffer;
482  data.retlen = 4096;
483  if(SendMessage(hwndPlayer,WM_WA_IPC,(WPARAM)&data,IPC_GET_EXTENDED_FILE_INFO))
484     {
485     return(AnsiString(buffer));
486     }
487 
488  else*/ return("");
489  }
490 
491 AnsiString __fastcall TPlayer::GetMetaData(AnsiString Filename, AnsiString Item)
492  {
493  char buffer[512];
494  extendedFileInfoStruct data;
495  data.filename = Filename.c_str();
496  data.metadata = Item.c_str();
497  data.ret = buffer;
498  data.retlen = 512;
499  if(SendMessage(hwndPlayer,WM_WA_IPC,(WPARAM)&data,IPC_GET_EXTENDED_FILE_INFO))
500     {
501     return(AnsiString(buffer));
502     }
503 
504  else return("");
505  }
506 
507 
508 void __fastcall TPlayer::SavePlaylist()
509  {
510  SendMessage(hwndPlayer,WM_WA_IPC, 0, IPC_WRITEPLAYLIST);
511  }
512 
513 
514 void __fastcall TPlayer::SavePlaylistTo(AnsiString Path, AnsiString Filename)
515 {
516  SavePlaylist();
517  CopyFile((PlayerDirectory+"\\winamp.m3u").c_str(),(Path+"\\"+Filename+".m3u").c_str(),false);
518 }


syntax highlighted by Code2HTML, v. 0.9.1