change winamp song with your cellphone
(Updated 13 October 2004 - now possible to see what song is playing.)
Another winamp thingie I made: my winamp
toplist.
I'm often too tired to go to the laptop to change song when a song which sucks starts to play, so I coded a few progs to avoid this three meters journey. Now I can change song with my cellphone. I have a server which is connected to the internet, and then an internal network between the server and my laptop. The server runs linux, and I wrote this CGI-script (m.cgi):
#! /usr/bin/perl
use IO::Socket;
my $sock=new IO::Socket::INET(PeerAddr => "192.168.0.128", PeerPort=>'999',Proto=>'tcp');
recv($sock, $song, 1024, 0);
if($song ne ""){
$msg="bytt låt! Nu spelas \"$song\".";
}else{
$msg="hmm, kunde inte byta låt.";
}
$link="m.cgi?".(int rand 1000);
print << "_END_";
Cache-Control: no-cache, must-revalidate
Pragma: No-Cache
Content-Type: text/vnd.wap.wml
$msg Igen
_END_
And on my computer, I have this small prog running in the background all the time:
#include
#include
#include
#include
#include
using namespace std;
char title[1024];
void getSong(){
char buf[1024], *ptr;
HWND winamp=FindWindow("Winamp v1.x",NULL);
title[0]=0;
if(!winamp)
return;
GetWindowText(winamp,buf,1024);
if(!strncmp(buf, "Winamp", 6))
return;
*(strstr(buf, " - Winamp"))=0;
ptr=buf;
while(*ptr>='0' && *ptr<='9')
ptr++;
ptr+=2;
strcpy(title, ptr);
}
void switchSong(){
#define WINAMP_NEXT 40048
HWND hwndWinamp=FindWindow("Winamp v1.x",NULL);
if(hwndWinamp!=0)
SendMessage(hwndWinamp, WM_COMMAND,WINAMP_NEXT,0);
}
void die(char *a){
printf("%s\n", a);
system("PAUSE");
exit(1);
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
WSADATA wsaData;
if( WSAStartup( MAKEWORD(2,2), &wsaData ) != NO_ERROR )
die("kunde inte initiera winsock");
SOCKET ListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if (ListenSocket == INVALID_SOCKET)
die("kunde inte skapa socket");
hostent* thisHost;
char* ip;
u_short port;
port = 999;
thisHost = gethostbyname("");
ip = inet_ntoa (*(struct in_addr *)*thisHost->h_addr_list);
sockaddr_in service, client;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(ip);
service.sin_port = htons(port);
if ( bind( ListenSocket,(SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
die("kunde inte binda");
if (listen(ListenSocket,1) == -1)
die("apa med listen");
while(1) {
int sin_size = sizeof(struct sockaddr_in);
SOCKET ClientSocket;
if ((ClientSocket = accept(ListenSocket, (struct sockaddr *)&client,&sin_size)) == -1)
die("cp med accept\n");
printf("Incoming connection from: %s \n", inet_ntoa(client.sin_addr) );
switchSong();
getSong();
send(ClientSocket,title,strlen(title), 0);
closesocket(ClientSocket);
}
return 0;
}
And that's it, it took me only an hour to make it all work. I stole code freely all around the net.
Erik Bernhardsson