]> www.infradead.org Git - users/dwmw2/mpc-car2pc.git/commitdiff
fix serial port initialisation, work out which disc is loaded at startup
authorDavid Woodhouse <David.Woodhouse@intel.com>
Wed, 20 May 2009 23:11:36 +0000 (00:11 +0100)
committerDavid Woodhouse <David.Woodhouse@intel.com>
Wed, 20 May 2009 23:12:10 +0000 (00:12 +0100)
mpc-car2pc.c

index 3ae68c0f70815f94a162b1765b8ac0fcea8b7771..e745cd35e49299021bc05ea9c1507109456f0bc6 100644 (file)
@@ -1,3 +1,5 @@
+#define _BSD_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -37,7 +39,7 @@
 mpd_Connection *mpd;
 int car2pc_fd;
 int hupped;
-int disc = 1;
+int disc;
 
 void handle_hup(int sig)
 {
@@ -155,6 +157,7 @@ void pause_playback(void)
                       mpd->errorStr);
        }
 }
+
 void change_disc(int direction)
 {
        char buf[6];
@@ -188,7 +191,6 @@ void change_disc(int direction)
        }
 }
 
-
 int mainloop(void)
 {
        int track = -1, plid = -1, last_time = -1;
@@ -296,10 +298,73 @@ void connect_car2pc(char *port)
        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");
@@ -391,6 +456,15 @@ int main(int argc, char **argv)
 
        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");