Index: playlist.c
===================================================================
--- playlist.c	(revision 3645)
+++ playlist.c	(working copy)
@@ -79,6 +79,10 @@
 } Playlist;
 
 static Playlist playlist;
+
+//WHATAH
+extern Queue *q;
+/* //END */
 static int playlist_state = PLAYLIST_STATE_STOP;
 static int playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH;
 static int playlist_stopOnError;
@@ -127,6 +131,12 @@
 static void incrPlaylistCurrent() {
 	if(playlist.current < 0) return;
 
+        //WHATAH
+        if (is_empty_q(q) > 0) {
+            playlist.current = pop_next_in_q(q);
+            return;
+        }
+        //END
 	if(playlist.current >= playlist.length-1) {
 		if(playlist.repeat) playlist.current = 0;
 		else playlist.current = -1;
@@ -134,11 +144,18 @@
 	else playlist.current++;
 }
 
+//WHATAH
+void initQueue() {
+    q = malloc(sizeof(*q));
+    init_queue(q);
+}
+//END
+
 void initPlaylist() {
 	char * test;
 	int i;
 	ConfigParam * param;
-
+        
 	playlist.length = 0;
 	playlist.repeat = 0;
 	playlist.version = 1;
@@ -524,7 +541,24 @@
 }
 
 void queueNextSongInPlaylist() {
-	if(playlist.current<playlist.length-1) {
+        // WHATAH
+        if (is_empty_q(q) > 0) {
+            
+            playlist.queued = next_in_q(q);
+            DEBUG("playlist: queue song %i:\"%s\"\n",
+                            playlist.queued,
+                            getSongUrl(playlist.songs[playlist.queued]));
+            if (queueSong(playlist.songs[playlist.queued]) < 0) {
+                playlist.queued = -1;
+                playlist_queueError = 1;
+            }
+
+            return;
+
+        } 
+        //END
+
+        if(playlist.current<playlist.length-1) {
 		playlist.queued = playlist.current+1;
 		DEBUG("playlist: queue song %i:\"%s\"\n",
 				playlist.queued,
@@ -558,16 +592,32 @@
 void syncPlaylistWithQueue(int queue) {
 	if(queue && getPlayerQueueState()==PLAYER_QUEUE_BLANK) {
 		queueNextSongInPlaylist();
+                DEBUG("queue && QUEUE_BLANK\n");
 	}
 	else if(getPlayerQueueState()==PLAYER_QUEUE_DECODE) {
 		if(playlist.queued!=-1) setQueueState(PLAYER_QUEUE_PLAY);
 		else setQueueState(PLAYER_QUEUE_STOP);
+                DEBUG("QUEUE_DECODE\n");
 	}
 	else if(getPlayerQueueState()==PLAYER_QUEUE_EMPTY) {
+                DEBUG("QUEUE_EMPTY\n");
 		setQueueState(PLAYER_QUEUE_BLANK);
 		if(playlist.queued>=0) {
 			DEBUG("playlist: now playing queued song\n");
-			playlist.current = playlist.queued;
+                        // WHATAH
+                        if (is_empty_q(q) > 0) {
+                            pop_next_in_q(q);
+                            playlist.current = playlist.queued;
+                            if (playlist.random) {
+                                int i;
+                                for(i=0;playlist.order[i]!=playlist.queued;i++);                            
+                                playlist.current = i;
+                            }
+                        } else {
+                            playlist.current = playlist.queued;
+                        }
+                        // END
+
 		}
 		playlist.queued = -1;
 		if(queue) queueNextSongInPlaylist();
@@ -588,6 +638,7 @@
 
 void clearPlayerQueue() {
 	playlist.queued = -1;
+        DEBUG("Clearing queue\n");
 	switch(getPlayerQueueState()) {
 	case PLAYER_QUEUE_FULL:
 		DEBUG("playlist: dequeue song\n");
@@ -778,6 +829,10 @@
 	/* now take care of other misc stuff */
 	playlist.songs[playlist.length-1] = NULL;
 	playlist.length--;
+        // WHATAH
+        /* Remove the song from the queue */
+        remove_q(q, song);
+        //END
 
 	incrPlaylistVersion();
 
@@ -946,6 +1001,13 @@
 
 	playlist_stopOnError = 0;
 
+        //WHATAH
+//        if (playlist.current == next_in_q(q)) pop_next_in_q(q);
+        if (is_empty_q(q) > 0) {
+            int i = pop_next_in_q(q);
+            return playPlaylistOrderNumber(fp, playlist.order[i]);
+        }
+        //END
 	syncPlaylistWithQueue(0);
 
 	if(playlist.current>= 0 && playlist.current<playlist.length) {
@@ -962,8 +1024,14 @@
 	syncPlaylistWithQueue(0);
 	
 	playlist_stopOnError = 0;
-
-	if(playlist.current<playlist.length-1) {
+        
+        // WHATAH
+        if (is_empty_q(q) > 0) {
+            pop_next_in_q(q);
+            return playPlaylist(fp, playlist.queued, 0);
+        } 
+        if(playlist.current<playlist.length-1) {
+        // END
 		return playPlaylistOrderNumber(fp,playlist.current+1);
 	}
 	else if(playlist.length && playlist.repeat) {
Index: command.c
===================================================================
--- command.c	(revision 3645)
+++ command.c	(working copy)
@@ -33,6 +33,10 @@
 #include "log.h"
 #include "dbUtils.h"
 
+//WHATAH
+Queue *q;
+//END
+
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -89,6 +93,12 @@
 #define COMMAND_COMMANDS	"commands"
 #define COMMAND_NOTCOMMANDS	"notcommands"
 
+//WHATAH
+#define COMMAND_QUEUE           "queue"
+#define COMMAND_DEQUEUE         "dequeue"
+#define COMMAND_GETQUEUE        "getqueue"
+//END
+
 #define COMMAND_STATUS_VOLUME           "volume"
 #define COMMAND_STATUS_STATE            "state"
 #define COMMAND_STATUS_REPEAT           "repeat"
@@ -598,6 +608,57 @@
         return setPlaylistRandomStatus(fp,status);
 }
 
+// WHATAH
+int handleQueue(FILE *fp, unsigned int *permission, int argArrayLength,
+                char ** argArray)
+{
+    int status;
+    char *test;
+    status = strtol(argArray[1], &test, 10);
+    if (*test != '\0') {
+        commandError(fp, ACK_ERROR_ARG, "need an integer", NULL);
+        return -1;
+    }
+    insert_q(q, status);
+    clearPlayerQueue();
+    return 0;
+}
+
+int handleDequeue(FILE *fp, unsigned int *permission, int argArrayLength,
+                char ** argArray)
+{
+    int status;
+    char *test;
+    status = strtol(argArray[1], &test, 10);
+    if (*test != '\0') {
+        commandError(fp, ACK_ERROR_ARG, "need an integer", NULL);
+        return -1;
+    }
+
+    remove_q(q, status);
+    return 0;
+}
+int handleGetqueue(FILE *fp, unsigned int *permission, int argArrayLength,
+                char ** argArray)
+{
+    int **contents = malloc(sizeof(contents));
+    if (q->size <= 0) {
+        return 0;
+    }
+    view_q(q, contents);
+    int *citer = *contents;
+    int i = 1;
+    while (*citer != -1) {
+/*         myfprintf(fp, "%i:%i\n", i, *citer); */
+        playlistInfo(fp, *citer);
+        citer++;
+        i++;
+    }
+    
+    return 0;
+}
+// END
+
 int handleStats(FILE * fp, unsigned int * permission, int argArrayLength, 
 		char ** argArray) 
 {
@@ -963,7 +1024,11 @@
         addCommand(COMMAND_DEVICES     ,PERMISSION_ADMIN,   0, 0,handleDevices,NULL);
         addCommand(COMMAND_COMMANDS    ,0,                  0, 0,handleCommands,NULL);
         addCommand(COMMAND_NOTCOMMANDS ,0,                  0, 0,handleNotcommands,NULL);
-
+        //WHATAH
+        addCommand(COMMAND_QUEUE       ,PERMISSION_CONTROL, 1, 1, handleQueue, NULL);
+        addCommand(COMMAND_DEQUEUE     ,PERMISSION_CONTROL, 1, 1, handleDequeue, NULL);
+        addCommand(COMMAND_GETQUEUE    ,PERMISSION_READ,    0, 0, handleGetqueue, NULL);
+        //END
         sortList(commandList);
 }
 
Index: command.h
===================================================================
--- command.h	(revision 3645)
+++ command.h	(working copy)
@@ -26,6 +26,10 @@
 #include "log.h"
 #include "ack.h"
 
+//WHATAH
+#include "queue.h"
+//END
+
 #include <stdio.h>
 
 #define COMMAND_RETURN_KILL	10
@@ -34,6 +38,10 @@
 extern char * current_command;
 extern int command_listNum;
 
+//WHATAH
+//extern Queue *q;
+//END
+
 int proccessListOfCommands(FILE * fp, int * permission, int * expired, 
 		int listOK, List * list);
 
Index: main.c
===================================================================
--- main.c	(revision 3645)
+++ main.c	(working copy)
@@ -467,6 +467,9 @@
         initReplayGainState();
 
         initPlaylist();
+        // WHATAH
+        initQueue();
+        // END
         initInputPlugins();
 
         openDB(&options, argv[0]);
Index: Makefile.am
===================================================================
--- Makefile.am	(revision 3645)
+++ Makefile.am	(working copy)
@@ -50,6 +50,7 @@
 	player.h \
 	playerData.h \
 	playlist.h \
+	queue.h \
 	replayGain.h \
 	signal_check.h \
 	sig_handlers.h \
@@ -94,6 +95,7 @@
 	player.c \
 	playerData.c \
 	playlist.c \
+	queue.c \
 	replayGain.c \
 	sig_handlers.c \
 	signal_check.c \

