From c47f61387da7c85aff9d3db00081053965e2e0dc Mon Sep 17 00:00:00 2001 From: bakkeby Date: Sun, 4 Oct 2020 21:19:54 +0200 Subject: mpdcontrol: fixing typo, mpd (music player daemon), not mdp --- README.md | 4 +- config.def.h | 12 ++--- config.mk | 2 +- patch/include.c | 4 +- patch/include.h | 4 +- patch/mdpcontrol.c | 144 ----------------------------------------------------- patch/mdpcontrol.h | 2 - patch/mpdcontrol.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++ patch/mpdcontrol.h | 2 + patches.def.h | 2 +- 10 files changed, 160 insertions(+), 160 deletions(-) delete mode 100644 patch/mdpcontrol.c delete mode 100644 patch/mdpcontrol.h create mode 100644 patch/mpdcontrol.c create mode 100644 patch/mpdcontrol.h diff --git a/README.md b/README.md index b39109d..cf8d3f3 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ Refer to [https://dwm.suckless.org/](https://dwm.suckless.org/) for details on t 2019-10-11 - Added the patch to ignore Xft errors when drawing text in the status bar -2019-10-10 - Added mdpcontrol, scratchpad and spawn_cwd cpatches +2019-10-10 - Added mpdcontrol, scratchpad and spawn_cwd cpatches 2019-10-08 - Added columns layout and fakefullscreen patch @@ -395,7 +395,7 @@ Refer to [https://dwm.suckless.org/](https://dwm.suckless.org/) for details on t - [maximize](https://dwm.suckless.org/patches/maximize/) - adds helper functions for maximizing, horizontally and vertically, floating windows using keybindings - - [mdpcontrol](https://dwm.suckless.org/patches/mpdcontrol/) + - [mpdcontrol](https://dwm.suckless.org/patches/mpdcontrol/) - adds keyboard bindings to control MDP (Music Player Daemon) - [monitorrules](https://github.com/bakkeby/patches/wiki/monitorrules/) diff --git a/config.def.h b/config.def.h index 517b887..2b7194c 100644 --- a/config.def.h +++ b/config.def.h @@ -1078,11 +1078,11 @@ static Key keys[] = { { MODKEY|ControlMask, XK_comma, cyclelayout, {.i = -1 } }, { MODKEY|ControlMask, XK_period, cyclelayout, {.i = +1 } }, #endif // CYCLELAYOUTS_PATCH - #if MDPCONTROL_PATCH + #if MPDCONTROL_PATCH { MODKEY, XK_F1, mpdchange, {.i = -1} }, { MODKEY, XK_F2, mpdchange, {.i = +1} }, { MODKEY, XK_Escape, mpdcontrol, {0} }, - #endif // MDPCONTROL_PATCH + #endif // MPDCONTROL_PATCH TAGKEYS( XK_1, 0) TAGKEYS( XK_2, 1) TAGKEYS( XK_3, 2) @@ -1272,10 +1272,10 @@ static Signal signals[] = { #if CYCLELAYOUTS_PATCH { "cyclelayout", cyclelayout }, #endif // CYCLELAYOUTS_PATCH - #if MDPCONTROL_PATCH + #if MPDCONTROL_PATCH { "mpdchange", mpdchange }, { "mpdcontrol", mpdcontrol }, - #endif // MDPCONTROL_PATCH + #endif // MPDCONTROL_PATCH { "toggleviewex", toggleviewex }, { "tag", tag }, { "tagall", tagallex }, @@ -1410,10 +1410,10 @@ static IPCCommand ipccommands[] = { IPCCOMMAND( toggleverticalmax, 1, {ARG_TYPE_NONE} ), IPCCOMMAND( togglemax, 1, {ARG_TYPE_NONE} ), #endif // MAXIMIZE_PATCH - #if MDPCONTROL_PATCH + #if MPDCONTROL_PATCH IPCCOMMAND( mpdchange, 1, {ARG_TYPE_SINT} ), IPCCOMMAND( mpdcontrol, 1, {ARG_TYPE_NONE} ), - #endif // MDPCONTROL_PATCH + #endif // MPDCONTROL_PATCH #if MOVEPLACE_PATCH IPCCOMMAND( moveplace, 1, {ARG_TYPE_UINT} ), #endif // MOVEPLACE_PATCH diff --git a/config.mk b/config.mk index 1c54ec6..0ff4840 100644 --- a/config.mk +++ b/config.mk @@ -25,7 +25,7 @@ FREETYPEINC = /usr/include/freetype2 # Uncomment this for the alpha patch / BAR_ALPHA_PATCH #XRENDER = -lXrender -# Uncomment this for the mdpcontrol patch / MDPCONTROL_PATCH +# Uncomment this for the mpdcontrol patch / MPDCONTROL_PATCH #MPDCLIENT = -lmpdclient # Uncomment for the pango patch / BAR_PANGO_PATCH diff --git a/patch/include.c b/patch/include.c index 69092aa..27ff539 100644 --- a/patch/include.c +++ b/patch/include.c @@ -162,8 +162,8 @@ #if MAXIMIZE_PATCH #include "maximize.c" #endif -#if MDPCONTROL_PATCH -#include "mdpcontrol.c" +#if MPDCONTROL_PATCH +#include "mpdcontrol.c" #endif #if MOVEPLACE_PATCH #include "moveplace.c" diff --git a/patch/include.h b/patch/include.h index e7ba3bb..c13d73b 100644 --- a/patch/include.h +++ b/patch/include.h @@ -158,8 +158,8 @@ #if MAXIMIZE_PATCH #include "maximize.h" #endif -#if MDPCONTROL_PATCH -#include "mdpcontrol.h" +#if MPDCONTROL_PATCH +#include "mpdcontrol.h" #endif #if MOVEPLACE_PATCH #include "moveplace.h" diff --git a/patch/mdpcontrol.c b/patch/mdpcontrol.c deleted file mode 100644 index c7ff786..0000000 --- a/patch/mdpcontrol.c +++ /dev/null @@ -1,144 +0,0 @@ -#include -#include -#include -#include - -#include - -#define MPDHOST "localhost" -#define MPDPORT 6600 - -struct mpd_connection *get_conn() -{ - struct mpd_connection *conn; - - conn = mpd_connection_new(MPDHOST, MPDPORT, 1000); - - if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { - fprintf(stderr, "Could not connect to mpd: %s\n", mpd_connection_get_error_message(conn)); - - mpd_connection_free(conn); - return NULL; - } - - return conn; -} - -void mpdchange(const Arg *direction) -{ - struct mpd_connection *conn; - - conn = get_conn(); - - if (conn == NULL) { - return; - } - - if (direction->i > 0) { - mpd_run_next(conn); - } - else { - mpd_run_previous(conn); - } - - mpd_connection_free(conn); -} - -char *get_regerror(int errcode, regex_t *compiled) -{ - size_t length = regerror(errcode, compiled, NULL, 0); - char *buffer = malloc(length); - (void) regerror(errcode, compiled, buffer, length); - - return buffer; -} - -void mpdcontrol() -{ - 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(); - - if (conn == NULL) { - return; - } - - status = mpd_run_status(conn); - - if (status == NULL) { - 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); - - if (state == MPD_STATE_STOP || state == MPD_STATE_PAUSE) { - mpd_run_play(conn); - mpd_status_free(status); - mpd_connection_free(conn); - } - else if (state != MPD_STATE_UNKNOWN) { //playing some music - song = mpd_run_current_song(conn); - - if (song == NULL){ - 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); - if (errcode != 0) { - 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); - - if (matchcode == 0) { - if (strstr(filename, "file://") == filename) { //match just at the start of the filename - //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); - } - else { - mpd_run_stop(conn); - } - } - else if (matchcode == REG_NOMATCH) { - mpd_run_toggle_pause(conn); - } - else { - 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); - } -} \ No newline at end of file diff --git a/patch/mdpcontrol.h b/patch/mdpcontrol.h deleted file mode 100644 index b8825d4..0000000 --- a/patch/mdpcontrol.h +++ /dev/null @@ -1,2 +0,0 @@ -static void mpdchange(const Arg *direction); -static void mpdcontrol(); \ No newline at end of file diff --git a/patch/mpdcontrol.c b/patch/mpdcontrol.c new file mode 100644 index 0000000..c7ff786 --- /dev/null +++ b/patch/mpdcontrol.c @@ -0,0 +1,144 @@ +#include +#include +#include +#include + +#include + +#define MPDHOST "localhost" +#define MPDPORT 6600 + +struct mpd_connection *get_conn() +{ + struct mpd_connection *conn; + + conn = mpd_connection_new(MPDHOST, MPDPORT, 1000); + + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { + fprintf(stderr, "Could not connect to mpd: %s\n", mpd_connection_get_error_message(conn)); + + mpd_connection_free(conn); + return NULL; + } + + return conn; +} + +void mpdchange(const Arg *direction) +{ + struct mpd_connection *conn; + + conn = get_conn(); + + if (conn == NULL) { + return; + } + + if (direction->i > 0) { + mpd_run_next(conn); + } + else { + mpd_run_previous(conn); + } + + mpd_connection_free(conn); +} + +char *get_regerror(int errcode, regex_t *compiled) +{ + size_t length = regerror(errcode, compiled, NULL, 0); + char *buffer = malloc(length); + (void) regerror(errcode, compiled, buffer, length); + + return buffer; +} + +void mpdcontrol() +{ + 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(); + + if (conn == NULL) { + return; + } + + status = mpd_run_status(conn); + + if (status == NULL) { + 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); + + if (state == MPD_STATE_STOP || state == MPD_STATE_PAUSE) { + mpd_run_play(conn); + mpd_status_free(status); + mpd_connection_free(conn); + } + else if (state != MPD_STATE_UNKNOWN) { //playing some music + song = mpd_run_current_song(conn); + + if (song == NULL){ + 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); + if (errcode != 0) { + 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); + + if (matchcode == 0) { + if (strstr(filename, "file://") == filename) { //match just at the start of the filename + //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); + } + else { + mpd_run_stop(conn); + } + } + else if (matchcode == REG_NOMATCH) { + mpd_run_toggle_pause(conn); + } + else { + 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); + } +} \ No newline at end of file diff --git a/patch/mpdcontrol.h b/patch/mpdcontrol.h new file mode 100644 index 0000000..b8825d4 --- /dev/null +++ b/patch/mpdcontrol.h @@ -0,0 +1,2 @@ +static void mpdchange(const Arg *direction); +static void mpdcontrol(); \ No newline at end of file diff --git a/patches.def.h b/patches.def.h index 6911946..8ddcd23 100644 --- a/patches.def.h +++ b/patches.def.h @@ -611,7 +611,7 @@ * - libmpdclient * https://dwm.suckless.org/patches/mpdcontrol/ */ -#define MDPCONTROL_PATCH 0 +#define MPDCONTROL_PATCH 0 /* Adds rules per monitor, e.g. have default layouts per monitor. * The use case for this is if the second monitor is vertical (i.e. rotated) then -- cgit v1.2.3