+#define _BSD_SOURCE
+
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
mpd_Connection *mpd;
int car2pc_fd;
int hupped;
-int disc = 1;
+int disc;
void handle_hup(int sig)
{
mpd->errorStr);
}
}
+
void change_disc(int direction)
{
char buf[6];
}
}
-
int mainloop(void)
{
int track = -1, plid = -1, last_time = -1;
tcgetattr(car2pc_fd, &tio);
cfsetospeed(&tio, B9600);
cfsetispeed(&tio, B9600);
+ cfmakeraw(&tio);
tio.c_cflag &= ~PARENB;
tcsetattr(car2pc_fd, TCSANOW, &tio);
}
+struct file_list {
+ struct file_list *next;
+ char file[];
+};
+
+void startup_disc(void)
+{
+ struct file_list *pl_files = NULL, **last_file = &pl_files;
+ mpd_InfoEntity *e;
+ int i;
+
+ mpd_sendPlaylistInfoCommand(mpd, -1);
+
+ while ((e = mpd_getNextInfoEntity(mpd))) {
+ mpd_Song *s = e->info.song;
+
+ if (e->type == MPD_INFO_ENTITY_TYPE_SONG) {
+ struct file_list *new = calloc(1, sizeof(struct file_list) + strlen(s->file) + 1);
+ if (!new)
+ continue;
+ new->next = NULL;
+ strcpy(new->file, s->file);
+ *last_file = new;
+ last_file = &new->next;
+ }
+ mpd_freeInfoEntity(e);
+ }
+
+ for (i = 0; i < CAR2PC_MAX_DISC; i++) {
+ struct file_list *tmp = pl_files;
+ int matched = 1;
+ char buf[6];
+
+ sprintf(buf, "disc%d", i);
+
+ mpd_sendListPlaylistCommand(mpd, buf);
+ while ((e = mpd_getNextInfoEntity(mpd))) {
+ mpd_Song *s = e->info.song;
+
+ if (e->type == MPD_INFO_ENTITY_TYPE_SONG) {
+ if (matched && tmp && !strcmp(s->file, tmp->file))
+ tmp = tmp->next;
+ else
+ matched = 0;
+ }
+ mpd_freeInfoEntity(e);
+ }
+ if (tmp)
+ matched = 0;
+ if (matched) {
+ syslog(LOG_NOTICE, "Matched disc %d on startup\n", i);
+ disc = i;
+ break;
+ }
+ }
+ while (pl_files) {
+ struct file_list *tmp = pl_files;
+ pl_files = tmp->next;
+ free(tmp);
+ }
+}
+
void connect_mpd(void)
{
const char *host = getenv("MPD_HOST");
syslog(LOG_NOTICE, "mpc-car2pc starting up on port %s\n", argv[1]);
+ startup_disc();
+
+ if (!disc) {
+ disc = CAR2PC_MAX_DISC;
+ change_disc(1);
+ } else
+ send_car2pc_command("DS%03d", disc);
+
+
mainloop();
syslog(LOG_NOTICE, "mpc-car2pc exiting\n");