From: David Woodhouse Date: Wed, 20 May 2009 23:11:36 +0000 (+0100) Subject: fix serial port initialisation, work out which disc is loaded at startup X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=b0247b27a9fb18354fe8d5bfb277a1ea07187244;p=users%2Fdwmw2%2Fmpc-car2pc.git fix serial port initialisation, work out which disc is loaded at startup --- diff --git a/mpc-car2pc.c b/mpc-car2pc.c index 3ae68c0..e745cd3 100644 --- a/mpc-car2pc.c +++ b/mpc-car2pc.c @@ -1,3 +1,5 @@ +#define _BSD_SOURCE + #include #include #include @@ -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");