2019-10-10 21:09:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <regex.h>
|
|
|
|
|
|
|
|
#include <mpd/client.h>
|
|
|
|
|
|
|
|
#define MPDHOST "localhost"
|
|
|
|
#define MPDPORT 6600
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
struct mpd_connection *get_conn()
|
|
|
|
{
|
2019-10-10 21:09:07 +00:00
|
|
|
struct mpd_connection *conn;
|
|
|
|
|
|
|
|
conn = mpd_connection_new(MPDHOST, MPDPORT, 1000);
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
|
2019-10-10 21:09:07 +00:00
|
|
|
fprintf(stderr, "Could not connect to mpd: %s\n", mpd_connection_get_error_message(conn));
|
|
|
|
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
void mpdchange(const Arg *direction)
|
|
|
|
{
|
2019-10-10 21:09:07 +00:00
|
|
|
struct mpd_connection *conn;
|
|
|
|
|
|
|
|
conn = get_conn();
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (conn == NULL) {
|
2019-10-10 21:09:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (direction->i > 0) {
|
2019-10-10 21:09:07 +00:00
|
|
|
mpd_run_next(conn);
|
|
|
|
}
|
2019-11-21 11:09:05 +00:00
|
|
|
else {
|
2019-10-10 21:09:07 +00:00
|
|
|
mpd_run_previous(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
char *get_regerror(int errcode, regex_t *compiled)
|
|
|
|
{
|
2019-10-10 21:09:07 +00:00
|
|
|
size_t length = regerror(errcode, compiled, NULL, 0);
|
|
|
|
char *buffer = malloc(length);
|
|
|
|
(void) regerror(errcode, compiled, buffer, length);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
void mpdcontrol()
|
|
|
|
{
|
2019-10-10 21:09:07 +00:00
|
|
|
struct mpd_connection *conn;
|
|
|
|
struct mpd_status *status;
|
|
|
|
struct mpd_song *song;
|
|
|
|
enum mpd_state state;
|
|
|
|
|
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
regex_t expr;
|
|
|
|
|
|
|
|
conn = get_conn();
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (conn == NULL) {
|
2019-10-10 21:09:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = mpd_run_status(conn);
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (status == NULL) {
|
2019-10-10 21:09:07 +00:00
|
|
|
fprintf(stderr, "Could not get mpd status: %s\n", mpd_status_get_error(status));
|
|
|
|
|
|
|
|
mpd_status_free(status);
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = mpd_status_get_state(status);
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (state == MPD_STATE_STOP || state == MPD_STATE_PAUSE) {
|
2019-10-10 21:09:07 +00:00
|
|
|
mpd_run_play(conn);
|
|
|
|
mpd_status_free(status);
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
}
|
2019-11-21 11:09:05 +00:00
|
|
|
else if (state != MPD_STATE_UNKNOWN) { //playing some music
|
2019-10-10 21:09:07 +00:00
|
|
|
song = mpd_run_current_song(conn);
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (song == NULL){
|
2019-10-10 21:09:07 +00:00
|
|
|
fprintf(stderr, "Error fetching current song!\n");
|
|
|
|
|
|
|
|
mpd_song_free(song);
|
|
|
|
mpd_status_free(status);
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = mpd_song_get_uri(song);
|
|
|
|
|
|
|
|
int errcode = regcomp(&expr, "^[[:alnum:]]+://", REG_EXTENDED|REG_NOSUB);
|
2019-11-21 11:09:05 +00:00
|
|
|
if (errcode != 0) {
|
2019-10-10 21:09:07 +00:00
|
|
|
char *err = get_regerror(errcode, &expr);
|
|
|
|
fprintf(stderr, "Could not compile regexp: %s\n", err);
|
|
|
|
|
|
|
|
mpd_song_free(song);
|
|
|
|
mpd_status_free(status);
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
free(err);
|
|
|
|
regfree(&expr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int matchcode = regexec(&expr, filename, 0, NULL, 0);
|
|
|
|
|
2019-11-21 11:09:05 +00:00
|
|
|
if (matchcode == 0) {
|
|
|
|
if (strstr(filename, "file://") == filename) { //match just at the start of the filename
|
2019-10-10 21:09:07 +00:00
|
|
|
//this means that mpd is playing a file outside the music_dir,
|
|
|
|
//but on disk, so we can safely pause
|
|
|
|
mpd_run_toggle_pause(conn);
|
|
|
|
}
|
2019-11-21 11:09:05 +00:00
|
|
|
else {
|
2019-10-10 21:09:07 +00:00
|
|
|
mpd_run_stop(conn);
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 11:09:05 +00:00
|
|
|
else if (matchcode == REG_NOMATCH) {
|
2019-10-10 21:09:07 +00:00
|
|
|
mpd_run_toggle_pause(conn);
|
|
|
|
}
|
2019-11-21 11:09:05 +00:00
|
|
|
else {
|
2019-10-10 21:09:07 +00:00
|
|
|
char *err = get_regerror(matchcode, &expr);
|
|
|
|
fprintf(stderr, "Error while matching regexp: %s\n", err);
|
|
|
|
|
|
|
|
free(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
regfree(&expr);
|
|
|
|
mpd_song_free(song);
|
|
|
|
mpd_status_free(status);
|
|
|
|
mpd_connection_free(conn);
|
|
|
|
}
|
|
|
|
}
|