Winamp Controller

  • 13 years ago
    Since i was unable to create a new article, i decided to post a small snippet here, on how to control your winamp.

    Hotkeys

    Predefined hotkeys. Usage:

    CTRL + ALT + F12 - Play or pause current song.
    CTRL + ALT + F11 - Stop playing current song.
    CTRL + ALT + F10 - Resume playing curret song.
    CTRL + ALT + F9  - Close Winamp player.
    CTRL + ALT + F8  - Skip to next item in playlist.
    CTRL + ALT + F7  - Toggle repeat in Winamp.
    CTRL + ALT + F6  - Toggle shuffle in Winamp.
    CTRL + ALT + ARROW UP - Increase volume in Winamp.
    CTRL + ALT + ARROW DOWN - Decrease volume in Winamp.
    CTRL + ALT + ARROW RIGHT - Fast forward 5 seconds.
    CTRL + ALT + ARROW DOWN - Fast rewind 5 seconds.

    Source
    Below you fill everything you need to build the controller.
    I used Dev-C++ 4.9.9.2 to compile this source.

    /**************************************************************/
    /* A small snippet which allows you to control winamp
    /* via hotkeys on your keyboard.
    /*
    /* If you wish to add more features you can go here:
    /* http://forums.winamp.com/showthread.php?s=&threadid=180297
    /**************************************************************/

    #include <iostream>
    #include <windows.h>

    using namespace std;

    // WM_COMMAND - Commands to send to Winamp Client.
    #define WINAMP_START 40045
    #define WINAMP_PLAY_OR_PAUSE 40046
    #define WINAMP_NEXT_TRACK 40048
    #define WINAMP_PREVIOUS_TRACK  40044
    #define WINAMP_CLOSE 40001
    #define WINAMP_STOP 40047
    #define WINAMP_RAISE_VOLUME 40058
    #define WINAMP_LOWER_VOLUME 40059
    #define WINAMP_TOGGLE_REPEAT 40022
    #define WINAMP_TOGGLE_SHUFFLE 40023
    #define WINAMP_WINDOW_HANDLE "Winamp v1.x"
    #define WINAMP_FAST_FORWARD 40148
    #define WINAMP_FAST_REWIND 40144

    // WM_USER
    #define WINAMP_CUSTOM_VOLUME 122

    // HOTKEYS
    #define CALL_CMD_PLAY_OR_PAUSE 101 // CTRL + ALT + F12
    #define CALL_CMD_STOP 102 // CTRL + ALT + F11
    #define CALL_CMD_START 105 // CTRL + ALT + F10
    #define CALL_CMD_CLOSE 106 // CTRL + ALT + F9
    #define CALL_CMD_NEXT_TRACK 107 // CTRL + ALT + F8
    #define CALL_CMD_REPEAT 108 // CTRL + ALT + F7
    #define CALL_CMD_SHUFFLE 109 // CTRL + ALT + F6
    #define CALL_CMD_RAISE_VOLUME 103 // CTRL + ALT + ARROW UP
    #define CALL_CMD_LOWER_VOLUME 104 // CTRL + ALT + ARROW DOWN
    #define CALL_CMD_FAST_FORWARD 110 // CTRL + ALT + ARROW RIGHT
    #define CALL_CMD_FAST_REWIND  111 // CTRL + ALT + ARROW LEFT

    class Controller
    {
          public:
                 void PlayOrPause();
                 void Next();
                 void Previous();
                 void Start();
                 void Stop();
                 void Close();
                 void RaiseVolume();
                 void LowerVolume();
                 void ToggleRepeat();
                 void ToggleShuffle();
                 void CustomVolume(int Volume);
                 void FastForward();
                 void FastRewind();
                 char * CurrentTrack();
                 //
                 void WCommand(int command);
                 void UCommand(int input, int command);
                 HWND WinampHandle();
          private:
                  char itsCurrentTrack[2048],*p;
    };

    HWND Controller::WinampHandle()
    {
         return FindWindow(WINAMP_WINDOW_HANDLE,NULL);
    }

    void Controller::WCommand(int command)
    {
     SendMessage(Controller::WinampHandle(),WM_COMMAND, command, 1);
    }

    void Controller::UCommand(int input, int command)
    {
     SendMessage(Controller::WinampHandle(),WM_USER, input, command);    
    }

    void Controller::PlayOrPause()           { Controller::WCommand(WINAMP_PLAY_OR_PAUSE);        }
    void Controller::Next()                  { Controller::WCommand(WINAMP_NEXT_TRACK);           }
    void Controller::Previous()              { Controller::WCommand(WINAMP_PREVIOUS_TRACK);       }
    void Controller::Close()                 { Controller::WCommand(WINAMP_CLOSE);                }
    void Controller::Stop()                  { Controller::WCommand(WINAMP_STOP);                 }
    void Controller::ToggleRepeat()          { Controller::WCommand(WINAMP_TOGGLE_REPEAT);        }
    void Controller::ToggleShuffle()         { Controller::WCommand(WINAMP_TOGGLE_SHUFFLE);       }
    void Controller::RaiseVolume()           { Controller::WCommand(WINAMP_RAISE_VOLUME);         }
    void Controller::LowerVolume()           { Controller::WCommand(WINAMP_LOWER_VOLUME);         }
    void Controller::Start()                 { Controller::WCommand(WINAMP_START);                }
    void Controller::CustomVolume(int Volume){ Controller::UCommand(Volume,WINAMP_CUSTOM_VOLUME); }
    void Controller::FastForward()           { Controller::WCommand(WINAMP_FAST_FORWARD);         }
    void Controller::FastRewind()            { Controller::WCommand(WINAMP_FAST_REWIND);          }

    char * Controller::CurrentTrack()
    {
     GetWindowText(Controller::WinampHandle(),itsCurrentTrack,sizeof(itsCurrentTrack));
     p = itsCurrentTrack+strlen(itsCurrentTrack)-8;
     while (p >= itsCurrentTrack)
     {
           if (!strnicmp(p,"- Winamp",8))
           {
              break;
              p--;
           }
     }
     if (p >= itsCurrentTrack)
     {
           p--;
     }
     while (p >= itsCurrentTrack && *p == ' ')
     {
           p--;
           *++p = 0;
     }
     return itsCurrentTrack;
    }

    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

    char szClassName[ ] = "Winamp Controller";

    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)

    {
        HWND hwnd;              
        MSG messages;           
        WNDCLASSEX wincl;       
       
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;   
        wincl.style = CS_DBLCLKS;    
        wincl.cbSize = sizeof (WNDCLASSEX);

        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;               
        wincl.cbClsExtra = 0;                     
        wincl.cbWndExtra = 0;                     

        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

        if (!RegisterClassEx (&wincl))
            return 0;

        hwnd = CreateWindowEx (
               0,                  
               szClassName,        
               "Winamp Controller Utility",      
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT,      
               CW_USEDEFAULT,      
               544,                
               375,                
               HWND_DESKTOP,       
               NULL,               
               hThisInstance,      
               NULL                
               );
       
        RegisterHotKey(hwnd, CALL_CMD_PLAY_OR_PAUSE, MOD_CONTROL|MOD_ALT, VK_F12);
        RegisterHotKey(hwnd, CALL_CMD_STOP,  MOD_CONTROL|MOD_ALT, VK_F11);
        RegisterHotKey(hwnd, CALL_CMD_START, MOD_CONTROL|MOD_ALT, VK_F10);
        RegisterHotKey(hwnd, CALL_CMD_CLOSE, MOD_CONTROL|MOD_ALT, VK_F9);
        RegisterHotKey(hwnd, CALL_CMD_NEXT_TRACK, MOD_CONTROL|MOD_ALT, VK_F8);
        RegisterHotKey(hwnd, CALL_CMD_REPEAT, MOD_CONTROL|MOD_ALT, VK_F7);
        RegisterHotKey(hwnd, CALL_CMD_SHUFFLE, MOD_CONTROL|MOD_ALT, VK_F6);
        RegisterHotKey(hwnd, CALL_CMD_RAISE_VOLUME, MOD_CONTROL|MOD_ALT, VK_UP);
        RegisterHotKey(hwnd, CALL_CMD_LOWER_VOLUME, MOD_CONTROL|MOD_ALT, VK_DOWN);
        RegisterHotKey(hwnd, CALL_CMD_FAST_FORWARD, MOD_CONTROL|MOD_ALT, VK_RIGHT);
        RegisterHotKey(hwnd, CALL_CMD_FAST_REWIND, MOD_CONTROL|MOD_ALT, VK_LEFT);

        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }

    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        Controller Winamp;
        switch (message)                
        {
            case WM_HOTKEY:
            switch(wParam)
            {
                  case CALL_CMD_PLAY_OR_PAUSE:
                       Winamp.PlayOrPause();
                  break;
                  case CALL_CMD_STOP:
                       Winamp.Stop();
                  break;
                  case CALL_CMD_START:
                       Winamp.Start();
                  break;
                  case CALL_CMD_CLOSE:
                       Winamp.Close();
                  break;
                  case CALL_CMD_NEXT_TRACK:
                       Winamp.Next();
                  break;
                  case CALL_CMD_REPEAT:
                       Winamp.ToggleRepeat();
                  break;
                  case CALL_CMD_SHUFFLE:
                       Winamp.ToggleShuffle();
                  break;
                  case CALL_CMD_RAISE_VOLUME:
                       Winamp.RaiseVolume();
                  break;
                  case CALL_CMD_LOWER_VOLUME:
                       Winamp.LowerVolume();
                  break;
                  case CALL_CMD_FAST_FORWARD:
                       Winamp.FastForward();
                  break;
                  case CALL_CMD_FAST_REWIND:
                       Winamp.FastRewind();
                  break;
            }
            break;
            case WM_DESTROY:
                PostQuitMessage (0);      
                break;
            default:                     
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }


















































































































































































































































































Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook