void mplayer_volume_step(float percent) { static const char *cmd_fmt = "\npausing_keep volume %f\n"; char *cmd; if (!mplayer_state.playing) return; /* this is a hack, since mplayer's volume command doesn't seem to work * as documented. */ if (mplayer_state.volume > -1) { percent += mplayer_state.volume; mplayer_volume_set(percent); return; } if (asprintf(&cmd, cmd_fmt, percent) == -1) err(1, "%s: asprintf failed", __FUNCTION__); mplayer_send_cmd(cmd); free(cmd); mplayer_volume_query(); }
void mplayer_stop() { mplayer_send_cmd("\nstop\n"); mplayer_state.playing = false; mplayer_state.paused = false; }
void mplayer_pause() { if (!mplayer_state.playing) return; mplayer_send_cmd("\npause\n"); mplayer_state.paused = !mplayer_state.paused; }
void mplayer_volume_query() { static const char *cmd = "\npausing_keep get_property volume\n"; if (!mplayer_state.playing) return; mplayer_send_cmd(cmd); }
void mplayer_finish() { mplayer_send_cmd("\nquit\n"); close(mplayer_state.pipe_read); close(mplayer_state.pipe_write); waitpid(mplayer_state.pid, NULL, 0); }
/***************************************************************************** * Player monitor function, called repeatedly via the signal handler in the * vitunes main loop. * * This communicates with the child process periodically to accomplish the * following: * 1. If the player is currently playing a song, determine the position * (in seconds) into the playback * 2. When the player finishes playing a song, it starts playing the next * song, according to the current playmode. ****************************************************************************/ void mplayer_monitor() { static const char *query_cmd = "\nget_property time_pos\n"; static const char *answer_fail = "ANS_ERROR=PROPERTY_UNAVAILABLE"; static const char *answer_good = "ANS_time_pos"; static char response[1000]; /* mplayer can be noisy */ char *s; int nbytes; /* in this case, nothing to monitor */ if (!mplayer_state.playing || mplayer_state.paused) return; /* read any output from the player */ bzero(response, sizeof(response)); nbytes = read(mplayer_state.pipe_read, &response, sizeof(response)); if (nbytes == -1 && errno == EAGAIN) return; response[nbytes + 1] = '\0'; /* case: reached end of playback for a given file */ if (strstr(response, answer_fail) != NULL) { if (mplayer_callback_playnext != NULL) mplayer_callback_playnext(); return; } /* case: continue in playing current file. update position */ if ((s = strstr(response, answer_good)) != NULL) { while (strstr(s + 1, answer_good) != NULL) s = strstr(s + 1, answer_good); if (sscanf(s, "ANS_time_pos=%f", &mplayer_state.position) != 1) errx(1, "player_monitor: player child is misbehaving."); } mplayer_send_cmd(query_cmd); /* check for recent volume */ static const char *volume_good = "ANS_volume"; if ((s = strstr(response, volume_good)) != NULL) { while (strstr(s + 1, volume_good) != NULL) s = strstr(s + 1, volume_good); if (sscanf(s, "ANS_volume=%f", &mplayer_state.volume) != 1) errx(1, "player_monitor: player child is misbehaving."); } }
void mplayer_seek(int seconds) { static const char *cmd_fmt = "\nseek %i 0\nget_property time_pos\n"; char *cmd; if (!mplayer_state.playing) return; if (asprintf(&cmd, cmd_fmt, seconds) == -1) err(1, "%s: asprintf failed", __FUNCTION__); mplayer_send_cmd(cmd); free(cmd); if (mplayer_state.paused) mplayer_state.paused = false; }
void mplayer_volume_set(float percent) { static const char *cmd_fmt = "\npausing_keep set_property volume %f\n"; char *cmd; if (!mplayer_state.playing) return; if (percent > 100) percent = 100; if (percent < 0) percent = 0; if (asprintf(&cmd, cmd_fmt, percent) == -1) err(1, "%s: asprintf failed", __FUNCTION__); mplayer_send_cmd(cmd); free(cmd); mplayer_volume_query(); }
void mplayer_play(const char *file) { static const char *cmd_fmt = "\nloadfile \"%s\" 0\nget_property time_pos\n"; char *cmd; if (asprintf(&cmd, cmd_fmt, file) == -1) err(1, "%s: asprintf failed", __FUNCTION__); mplayer_send_cmd(cmd); free(cmd); mplayer_state.position = 0; mplayer_state.playing = true; mplayer_state.paused = false; mplayer_state.current_song = file; /* if we have a volume, reset it */ if (mplayer_state.volume > -1) mplayer_volume_set(mplayer_state.volume); }