/********************************************************************************* * BLURSOFT 2004 * * MODULE: Swimp * * DESCRIPTION: Swimp is the Synchronized WInaMP controller. Originally implemented * in C back in 2002, this is the next-gen version. * * REVISION HISTORY: * 23 Sep 04 - SJR - baseline established * * CREDITS: I used the code provided by Magnus Lindberg in * "Using DllImport for Windows Interop" to get this going *********************************************************************************/ using System; using System.Diagnostics; using System.Threading; using System.Runtime.InteropServices; public class WinampController { private int WINDOW_HANDLE; // // The following defines are pulled from the Winamp 5 SDK, though they should be // compatible with Winamp 2.x. The file they came from was "wa_ipc.h" // private const int WM_COMMAND = 0x111; // defined in windows.h or winuser.h private const int WM_USER = 0x0400; // defined in windows.h or winuser.h private const int WINAMP_BUTTON2 = 40045; // the Play button private const int WINAMP_BUTTON1 = 40044; // the Previous button private const int WINAMP_BUTTON4 = 40047; // the Stop button private const int WINAMP_BUTTON4_SHIFT = 40147; // the Fadeout Stop button (stops with a fade) private const int WINAMP_BUTTON5 = 40048; // the Next button private const int IPC_GETOUTPUTTIME = 105; // get song position in millisecs private const int IPC_JUMPTOTIME = 106; // jump to a position in millisecs private const int IPC_SETVOLUME = 122; // set volume (0-255) [DllImport("user32.dll",EntryPoint="SendMessage")] private static extern int SendMessage(int _WindowHandler, int _WM_USER, int _data, int _id); [DllImport("user32.dll",EntryPoint="FindWindow")] private static extern int FindWindow(string _ClassName, string _WindowName); //----------------------------------------------------------------------- // Initialize the WinampController. If winamp isn't running start it // (wait for it to boot up), then re-find its window. //----------------------------------------------------------------------- public WinampController() { WINDOW_HANDLE = FindWindow("Winamp v1.x",null); if(WINDOW_HANDLE == 0) { StartWinampProcess( ); WINDOW_HANDLE = FindWindow("Winamp v1.x",null); } } //----------------------------------------------------------------------- // If winamp isn't running this function can start it (if it's installed // to its default location). To make sure the window is fully created // there is a built-in wait of 2 seconds. //----------------------------------------------------------------------- private void StartWinampProcess( ) { Process winamp = null; ProcessStartInfo psi = new ProcessStartInfo( ); psi.FileName = "c:\\Program Files\\Winamp\\winamp.exe"; winamp = Process.Start( psi ); //winamp.WaitForInputIdle( ); //this doesn't seem to slow winamp down, so do a manual Thread.Sleep Thread.Sleep( 2000 ); //wait 2 seconds to make sure window is up (*risky*, perhaps) } public void Play( ) { SendMessage( WINDOW_HANDLE , WM_COMMAND , WINAMP_BUTTON2 , 0 ); } public void Stop( ) { SendMessage( WINDOW_HANDLE , WM_COMMAND , WINAMP_BUTTON4 , 0 ); } public void Next( ) { SendMessage( WINDOW_HANDLE , WM_COMMAND , WINAMP_BUTTON5 , 0 ); } public void Previous( ) { SendMessage( WINDOW_HANDLE , WM_COMMAND , WINAMP_BUTTON1 , 0 ); } public void FadeoutStop( ) { SendMessage( WINDOW_HANDLE , WM_COMMAND , WINAMP_BUTTON4_SHIFT , 0 ); } public int GetOutputTime( ) { return SendMessage( WINDOW_HANDLE , WM_USER , 0 , IPC_GETOUTPUTTIME ); } public int JumpToTime( int time ) { return SendMessage( WINDOW_HANDLE , WM_USER , time , IPC_JUMPTOTIME ); } public int SetVolume( int volume ) { return SendMessage( WINDOW_HANDLE , WM_USER , volume , IPC_SETVOLUME ); } }//WinampController class Driver { public static void Main() { try { int time = 0; WinampController winamp = new WinampController(); string command; while( true ) { command = Console.ReadLine( ); switch( command ) { case "play": winamp.Play(); break; case "stop": winamp.Stop(); break; case "next": winamp.Next(); break; case "previous": winamp.Previous(); break; case "fadeout-stop": winamp.FadeoutStop(); break; case "get-output-time": time = winamp.GetOutputTime(); Console.WriteLine( time ); break; case "jump-to-time": Console.WriteLine("enter time to jump to (eg, 1234)"); int j = Convert.ToInt32( Console.ReadLine() ); winamp.JumpToTime( j ); break; case "set-volume": Console.WriteLine("enter volume (0-255)"); int v = Convert.ToInt32( Console.ReadLine() ); winamp.SetVolume( v ); break; case "quit": return; default: break; } } } catch(Exception e) { Console.WriteLine(e.Message); } } }