/*
 *  tiny-xmms-remote.c --- A small XMMS interface
 *
 *  Author: Mark Triggs <mark@dishevelled.net>
 *
 *  This file is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This file is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Emacs; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 *  Commentary:
 *
 *  A small trampoline used by tiny-xmms.el.  Should be compilable with
 *  something like:
 *
 *    gcc -g -o tiny-xmms-remote tiny-xmms-remote.c -lxmms \
 *      `glib-config --cflags`
 *
 *  Provided you have the appropriate libraries/headers installed.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <xmms/xmmsctrl.h>

void xmms_status ()
{
    int rate, frequency, channels;
    int time, mins, seconds;

    time = (xmms_remote_get_output_time (0) / 1000);
    mins = (time / 60);
    seconds = time % 60;

    xmms_remote_get_info(0, &rate, &frequency, &channels);

    printf ("[%.2d:%.2d] %d Kbps; %d hz; %d channels\n",
            mins, seconds, rate / 1000, frequency, channels);
}

int main (int argc, char **argv)
{
    if (argc == 2 || argc == 3) {
        if (strcmp (argv[1], "get") == 0) {
            int len = xmms_remote_get_playlist_length (0);
            char *names[len];
            int i;
            for (i = (len - 1); i >= 0; i--) {
                names[i] = xmms_remote_get_playlist_title (0, i);
            }
            for (i = 0; i < len; i++) {
                printf ("%s%c%d\n", names[i], 0, i);
                free (names[i]);
            }
        } else if (strcmp (argv[1], "skip") == 0) {
            int pos = atoi (argv[2]);
            xmms_remote_set_playlist_pos (0, pos);
            while (xmms_remote_get_playlist_pos (0) != pos)
                sleep (0);
        } else if (strcmp (argv[1], "next") == 0) {
            xmms_remote_playlist_next (0);
        } else if (strcmp (argv[1], "prev") == 0) {
            xmms_remote_playlist_prev (0);
        } else if (strcmp (argv[1], "length") == 0) {
            printf ("%d\n", xmms_remote_get_playlist_length (0));
        } else if (strcmp (argv[1], "state") == 0) {
            if (xmms_remote_is_paused (0)) {
                printf ("paused\n");
            } else if (xmms_remote_is_playing (0)) {
                printf ("playing\n");
            } else {
                printf ("stopped\n");
            }
        } else if (strcmp (argv[1], "playing") == 0) {
            int pos = xmms_remote_get_playlist_pos (0);
            char *title = xmms_remote_get_playlist_title (0, pos);
            printf ("%s\n%d\n", title, pos);
            free (title);
        } else if (strcmp (argv[1], "play") == 0) {
            xmms_remote_play (0);
            while (xmms_remote_is_paused (0) || ! xmms_remote_is_playing (0))
                sleep (0);
        } else if (strcmp (argv[1], "stop") == 0) {
            xmms_remote_stop (0);
            while (xmms_remote_is_playing (0))
                sleep (0);
        } else if (strcmp (argv[1], "pause") == 0) {
            xmms_remote_pause (0);
            while (! xmms_remote_is_paused (0))
                sleep (0);
        } else if (strcmp (argv[1], "status") == 0) {
            xmms_status ();
        } else if (strcmp (argv[1], "remove") == 0) {
            xmms_remote_playlist_delete (0, atoi (argv[2]));
        }  else if (strcmp (argv[1], "id") == 0) {
            /* display something that can be used as an identifier for the
             * current playlist.  For want of something better, I'm using the
             * duration of the first song in the playlist. */
            printf ("%d\n", xmms_remote_get_playlist_time (0, 0));
        }
    }
}
