dwm

Kris's build of dwm
git clone git clone https://git.krisyotam.com/krisyotam/dwm.git
Log | Files | Refs | README | LICENSE

mpdcontrol.c (3420B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 #include <stdio.h>
      4 #include <regex.h>
      5 
      6 #include <mpd/client.h>
      7 
      8 #define MPDHOST "localhost"
      9 #define MPDPORT 6600
     10 
     11 struct mpd_connection *get_conn()
     12 {
     13     struct mpd_connection *conn;
     14 
     15     conn = mpd_connection_new(MPDHOST, MPDPORT, 1000);
     16 
     17     if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
     18         fprintf(stderr, "Could not connect to mpd: %s\n", mpd_connection_get_error_message(conn));
     19 
     20         mpd_connection_free(conn);
     21         return NULL;
     22     }
     23 
     24     return conn;
     25 }
     26 
     27 void mpdchange(const Arg *direction)
     28 {
     29     struct mpd_connection *conn;
     30 
     31     conn = get_conn();
     32 
     33     if (conn == NULL) {
     34         return;
     35     }
     36 
     37     if (direction->i > 0) {
     38         mpd_run_next(conn);
     39     }
     40     else {
     41         mpd_run_previous(conn);
     42     }
     43 
     44     mpd_connection_free(conn);
     45 }
     46 
     47 char *get_regerror(int errcode, regex_t *compiled)
     48 {
     49     size_t length = regerror(errcode, compiled, NULL, 0);
     50     char *buffer = malloc(length);
     51     (void) regerror(errcode, compiled, buffer, length);
     52 
     53     return buffer;
     54 }
     55 
     56 void mpdcontrol()
     57 {
     58     struct mpd_connection *conn;
     59     struct mpd_status *status;
     60     struct mpd_song *song;
     61     enum mpd_state state;
     62 
     63     const char *filename;
     64 
     65     regex_t expr;
     66 
     67     conn = get_conn();
     68 
     69     if (conn == NULL) {
     70         return;
     71     }
     72 
     73     status = mpd_run_status(conn);
     74 
     75     if (status == NULL) {
     76         fprintf(stderr, "Could not get mpd status: %s\n", mpd_status_get_error(status));
     77 
     78         mpd_status_free(status);
     79         mpd_connection_free(conn);
     80         return;
     81     }
     82 
     83     state = mpd_status_get_state(status);
     84 
     85     if (state == MPD_STATE_STOP || state == MPD_STATE_PAUSE) {
     86         mpd_run_play(conn);
     87         mpd_status_free(status);
     88         mpd_connection_free(conn);
     89     }
     90     else if (state != MPD_STATE_UNKNOWN) { //playing some music
     91         song = mpd_run_current_song(conn);
     92 
     93         if (song == NULL){
     94             fprintf(stderr, "Error fetching current song!\n");
     95 
     96             mpd_song_free(song);
     97             mpd_status_free(status);
     98             mpd_connection_free(conn);
     99             return;
    100         }
    101 
    102         filename = mpd_song_get_uri(song);
    103 
    104         int errcode = regcomp(&expr, "^[[:alnum:]]+://", REG_EXTENDED|REG_NOSUB);
    105         if (errcode != 0) {
    106             char *err = get_regerror(errcode, &expr);
    107             fprintf(stderr, "Could not compile regexp: %s\n", err);
    108 
    109             mpd_song_free(song);
    110             mpd_status_free(status);
    111             mpd_connection_free(conn);
    112             free(err);
    113             regfree(&expr);
    114             return;
    115         }
    116 
    117         int matchcode = regexec(&expr, filename, 0, NULL, 0);
    118 
    119         if (matchcode == 0) {
    120             if (strstr(filename, "file://") == filename) { //match just at the start of the filename
    121                 //this means that mpd is playing a file outside the music_dir,
    122                 //but on disk, so we can safely pause
    123                 mpd_run_toggle_pause(conn);
    124             }
    125             else {
    126                 mpd_run_stop(conn);
    127             }
    128         }
    129         else if (matchcode == REG_NOMATCH) {
    130             mpd_run_toggle_pause(conn);
    131         }
    132         else {
    133             char *err = get_regerror(matchcode, &expr);
    134             fprintf(stderr, "Error while matching regexp: %s\n", err);
    135 
    136             free(err);
    137         }
    138 
    139         regfree(&expr);
    140         mpd_song_free(song);
    141         mpd_status_free(status);
    142         mpd_connection_free(conn);
    143     }
    144 }
    145