aboutsummaryrefslogblamecommitdiff
path: root/mpd_trigger.c
blob: 6e13b79ec66e593eaebf0940ac299d900e8b7f02 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                                                           




                   
                   

                      




                       









                                                                   
                               
                         
                                

                                                                            

                                                                                                                                   



































                                                                    
             


























                                                                                     
                                                                                 


                              




                                                                        
                      


               







                                                         
                                                           




                                             
                                                       























                                                                  

                                              


                                      

               



                     
                                                           



                               
                                   


                                    

                                      
                 
                                                 
                                 



                                                             
                 
                                   

                               





                                     







                             
                                                       







                                        
                                                                       

                             
                                                      



























                                                                






                                                             
                                                            

                                                     


                                                                                 







                                                                   






                                                 























                                                        








                                                             




































                                                                                                 

            
                                                                       

                                                 
                                                
                              
     
             
 
/**
 *  mpd_trigger: Execute whatever you want when MPD (Music Player Daemon) changes its state
 *  Copyright (C) 2014 Ted Yin

 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/wait.h>
#include <sys/types.h>

#include <mpd/idle.h>
#include <mpd/tag.h>
#include <mpd/status.h>
#include <mpd/client.h>

struct mpd_connection *conn;
typedef struct mpd_song mpd_song_t;
typedef struct mpd_status mpd_status_t;

#define MAINLOOP_ERROR do { handle_error(conn); return; } while (0)
#define MAX_HASH 1021
#define MAX_INFO_BUFF 256
#define MAX_OUTPUT_BUFF 2048

const char *host = "localhost";
unsigned int port = 6600;
unsigned int reconnect_time = 3;
const char *state_name[4] = {"unknown", "stopped", "now playing", "paused"};
const char *shell = "bash";
const char *trigger_command = "terminal-notifier -title \"{title}: {state} ({elapsed_pct}%)\" "
                            "-subtitle \"{artist}\" -message \"{album} @ {track?{track}:unknown track}\" -sender com.apple.iTunes";

typedef const char *(*Hook_t)(mpd_status_t status, mpd_song_t song);
typedef struct Entry {
    const char *name;
    const char **content_ref;
    struct Entry *next;
} Entry;

typedef struct {
    Entry *head[MAX_HASH];
} HashTable;

HashTable *hash_table_create() {
    HashTable *ht = (HashTable *)malloc(sizeof(HashTable));
    memset(ht->head, sizeof(ht->head), 0);
    return ht;
}

unsigned hash_table_hash_func(const char *name) {
    unsigned int seed = 131, res = 0;
    while (*name)
        res = res * seed + (*name++ - 'a');
    return res % MAX_HASH;
}

void hash_table_destroy(HashTable *ht) {
    unsigned int i;
    for (i = 0; i < MAX_HASH; i++)
    {
        Entry *e, *ne;
        for (e = ht->head[i]; e; e = ne)
        {
            ne = e->next;
            free(e);
        }
    }
    free(ht);
}

void hash_table_register(HashTable *ht, const char *name, const char **content_ref) {
    unsigned int hv = hash_table_hash_func(name);
    Entry *e = (Entry *)malloc(sizeof(Entry));
    e->name = name;
    e->content_ref = content_ref;
    e->next = ht->head[hv];
    ht->head[hv] = e;
}

const char *hash_table_lookup(HashTable *ht, const char *name) {
    unsigned int hv = hash_table_hash_func(name);
    Entry *e;
    for (e = ht->head[hv]; e; e = e->next)
        if (!strcmp(e->name, name))
            return *(e->content_ref);
    return NULL;
}

char etime_buff[MAX_INFO_BUFF], ttime_buff[MAX_INFO_BUFF], epct_buff[MAX_INFO_BUFF];
const char *<