123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- /* ympd
- (c) 2013-2014 Andrew Karpow <andy@ndyk.de>
- This project's homepage is: http://www.ympd.org
-
- 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; version 2 of the License.
- 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, write to the Free Software Foundation, Inc.,
- Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <libgen.h>
- #include <mpd/client.h>
- #include "mpd_client.h"
- #include "config.h"
- const char * mpd_cmd_strs[] = {
- MPD_CMDS(GEN_STR)
- };
- static inline enum mpd_cmd_ids get_cmd_id(char *cmd)
- {
- for(int i = 0; i < sizeof(mpd_cmd_strs)/sizeof(mpd_cmd_strs[0]); i++)
- if(!strncmp(cmd, mpd_cmd_strs[i], strlen(mpd_cmd_strs[i])))
- return i;
- return -1;
- }
- int callback_mpd(struct mg_connection *c)
- {
- enum mpd_cmd_ids cmd_id = get_cmd_id(c->content);
- size_t n = 0;
- unsigned int uint_buf, uint_buf_2;
- int int_buf;
- char *p_charbuf;
- if(cmd_id == -1)
- return MG_CLIENT_CONTINUE;
- if(mpd.conn_state != MPD_CONNECTED && cmd_id != MPD_API_SET_MPDHOST &&
- cmd_id != MPD_API_GET_MPDHOST && cmd_id != MPD_API_SET_MPDPASS)
- return MG_CLIENT_CONTINUE;
- switch(cmd_id)
- {
- case MPD_API_UPDATE_DB:
- mpd_run_update(mpd.conn, NULL);
- break;
- case MPD_API_SET_PAUSE:
- mpd_run_toggle_pause(mpd.conn);
- break;
- case MPD_API_SET_PREV:
- mpd_run_previous(mpd.conn);
- break;
- case MPD_API_SET_NEXT:
- mpd_run_next(mpd.conn);
- break;
- case MPD_API_SET_PLAY:
- mpd_run_play(mpd.conn);
- break;
- case MPD_API_SET_STOP:
- mpd_run_stop(mpd.conn);
- break;
- case MPD_API_RM_ALL:
- mpd_run_clear(mpd.conn);
- break;
- case MPD_API_GET_QUEUE:
- n = mpd_put_queue(mpd.buf);
- break;
- case MPD_API_RM_TRACK:
- if(sscanf(c->content, "MPD_API_RM_TRACK,%u", &uint_buf))
- mpd_run_delete_id(mpd.conn, uint_buf);
- break;
- case MPD_API_PLAY_TRACK:
- if(sscanf(c->content, "MPD_API_PLAY_TRACK,%u", &uint_buf))
- mpd_run_play_id(mpd.conn, uint_buf);
- break;
- case MPD_API_TOGGLE_RANDOM:
- if(sscanf(c->content, "MPD_API_TOGGLE_RANDOM,%u", &uint_buf))
- mpd_run_random(mpd.conn, uint_buf);
- break;
- case MPD_API_TOGGLE_REPEAT:
- if(sscanf(c->content, "MPD_API_TOGGLE_REPEAT,%u", &uint_buf))
- mpd_run_repeat(mpd.conn, uint_buf);
- break;
- case MPD_API_TOGGLE_CONSUME:
- if(sscanf(c->content, "MPD_API_TOGGLE_CONSUME,%u", &uint_buf))
- mpd_run_consume(mpd.conn, uint_buf);
- break;
- case MPD_API_TOGGLE_SINGLE:
- if(sscanf(c->content, "MPD_API_TOGGLE_SINGLE,%u", &uint_buf))
- mpd_run_single(mpd.conn, uint_buf);
- break;
- case MPD_API_SET_VOLUME:
- if(sscanf(c->content, "MPD_API_SET_VOLUME,%ud", &uint_buf) && uint_buf <= 100)
- mpd_run_set_volume(mpd.conn, uint_buf);
- break;
- case MPD_API_SET_SEEK:
- if(sscanf(c->content, "MPD_API_SET_SEEK,%u,%u", &uint_buf, &uint_buf_2))
- mpd_run_seek_id(mpd.conn, uint_buf, uint_buf_2);
- break;
- case MPD_API_GET_BROWSE:
- if(sscanf(c->content, "MPD_API_GET_BROWSE,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
- n = mpd_put_browse(mpd.buf, p_charbuf);
- else
- n = mpd_put_browse(mpd.buf, "/");
- free(p_charbuf);
- break;
- case MPD_API_ADD_TRACK:
- if(sscanf(c->content, "MPD_API_ADD_TRACK,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
- {
- mpd_run_add(mpd.conn, p_charbuf);
- free(p_charbuf);
- }
- break;
- case MPD_API_ADD_PLAY_TRACK:
- if(sscanf(c->content, "MPD_API_ADD_PLAY_TRACK,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
- {
- int_buf = mpd_run_add_id(mpd.conn, p_charbuf);
- if(int_buf != -1)
- mpd_run_play_id(mpd.conn, int_buf);
- free(p_charbuf);
- }
- break;
- case MPD_API_ADD_PLAYLIST:
- if(sscanf(c->content, "MPD_API_ADD_PLAYLIST,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
- {
- mpd_run_load(mpd.conn, p_charbuf);
- free(p_charbuf);
- }
- break;
- case MPD_API_SEARCH:
- if(sscanf(c->content, "MPD_API_SEARCH,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
- {
- n = mpd_search(mpd.buf, p_charbuf);
- free(p_charbuf);
- }
- break;
- #ifdef WITH_MPD_HOST_CHANGE
- /* Commands allowed when disconnected from MPD server */
- case MPD_API_SET_MPDHOST:
- int_buf = 0;
- if(sscanf(c->content, "MPD_API_SET_MPDHOST,%d,%m[^\t\n ]", &int_buf, &p_charbuf) &&
- p_charbuf != NULL && int_buf > 0)
- {
- strncpy(mpd.host, p_charbuf, sizeof(mpd.host));
- free(p_charbuf);
- mpd.port = int_buf;
- mpd.conn_state = MPD_RECONNECT;
- return MG_CLIENT_CONTINUE;
- }
- break;
- case MPD_API_GET_MPDHOST:
- n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"mpdhost\", \"data\": "
- "{\"host\" : \"%s\", \"port\": \"%d\", \"passwort_set\": %s}"
- "}", mpd.host, mpd.port, mpd.password ? "true" : "false");
- break;
- case MPD_API_SET_MPDPASS:
- if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf) &&
- p_charbuf != NULL)
- {
- if(mpd.password)
- free(mpd.password);
- mpd.password = p_charbuf;
- mpd.conn_state = MPD_RECONNECT;
- printf("Got mpd pw %s\n", mpd.password);
- return MG_CLIENT_CONTINUE;
- }
- break;
- #endif
- }
- if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS)
- {
- n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
- mpd_connection_get_error_message(mpd.conn));
- /* Try to recover error */
- if (!mpd_connection_clear_error(mpd.conn))
- mpd.conn_state = MPD_FAILURE;
- }
- if(n > 0)
- mg_websocket_write(c, 1, mpd.buf, n);
- return MG_CLIENT_CONTINUE;
- }
- int mpd_close_handler(struct mg_connection *c)
- {
- /* Cleanup session data */
- if(c->connection_param)
- free(c->connection_param);
- return 0;
- }
- static int mpd_notify_callback(struct mg_connection *c) {
- size_t n;
- if(!c->is_websocket)
- return MG_REQUEST_PROCESSED;
- if(c->callback_param)
- {
- /* error message? */
- n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\",\"data\":\"%s\"}",
- (const char *)c->callback_param);
- mg_websocket_write(c, 1, mpd.buf, n);
- return MG_REQUEST_PROCESSED;
- }
- if(!c->connection_param)
- c->connection_param = calloc(1, sizeof(struct t_mpd_client_session));
- struct t_mpd_client_session *s = (struct t_mpd_client_session *)c->connection_param;
- if(mpd.conn_state != MPD_CONNECTED) {
- n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"disconnected\"}");
- mg_websocket_write(c, 1, mpd.buf, n);
- }
- else
- {
- mg_websocket_write(c, 1, mpd.buf, mpd.buf_size);
- if(s->song_id != mpd.song_id)
- {
- n = mpd_put_current_song(mpd.buf);
- mg_websocket_write(c, 1, mpd.buf, n);
- s->song_id = mpd.song_id;
- }
- if(s->queue_version != mpd.queue_version)
- {
- n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"update_queue\"}");
- mg_websocket_write(c, 1, mpd.buf, n);
- s->queue_version = mpd.queue_version;
- }
- }
- return MG_REQUEST_PROCESSED;
- }
- void mpd_poll(struct mg_server *s)
- {
- switch (mpd.conn_state) {
- case MPD_DISCONNECTED:
- /* Try to connect */
- fprintf(stdout, "MPD Connecting to %s:%d\n", mpd.host, mpd.port);
- mpd.conn = mpd_connection_new(mpd.host, mpd.port, 3000);
- if (mpd.conn == NULL) {
- fprintf(stderr, "Out of memory.");
- mpd.conn_state = MPD_FAILURE;
- return;
- }
- if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
- fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
- mg_iterate_over_connections(s, mpd_notify_callback,
- (void *)mpd_connection_get_error_message(mpd.conn));
- mpd.conn_state = MPD_FAILURE;
- return;
- }
- if(mpd.password && !mpd_run_password(mpd.conn, mpd.password))
- {
- fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
- mg_iterate_over_connections(s, mpd_notify_callback,
- (void *)mpd_connection_get_error_message(mpd.conn));
- mpd.conn_state = MPD_FAILURE;
- return;
- }
- fprintf(stderr, "MPD connected.\n");
- mpd.conn_state = MPD_CONNECTED;
- break;
- case MPD_FAILURE:
- fprintf(stderr, "MPD connection failed.\n");
- case MPD_DISCONNECT:
- case MPD_RECONNECT:
- if(mpd.conn != NULL)
- mpd_connection_free(mpd.conn);
- mpd.conn = NULL;
- mpd.conn_state = MPD_DISCONNECTED;
- break;
- case MPD_CONNECTED:
- mpd.buf_size = mpd_put_state(mpd.buf, &mpd.song_id, &mpd.queue_version);
- mg_iterate_over_connections(s, mpd_notify_callback, NULL);
- break;
- }
- }
- char* mpd_get_title(struct mpd_song const *song)
- {
- char *str, *ptr;
- str = (char *)mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
- if(str == NULL){
- str = basename((char *)mpd_song_get_uri(song));
- }
- if(str == NULL)
- return NULL;
- ptr = str;
- while(*ptr++ != '\0')
- if(*ptr=='"')
- *ptr='\'';
- return str;
- }
- int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version)
- {
- struct mpd_status *status;
- int len;
- status = mpd_run_status(mpd.conn);
- if (!status) {
- fprintf(stderr, "MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn));
- mpd.conn_state = MPD_FAILURE;
- return 0;
- }
- len = snprintf(buffer, MAX_SIZE,
- "{\"type\":\"state\", \"data\":{"
- " \"state\":%d, \"volume\":%d, \"repeat\":%d,"
- " \"single\":%d, \"consume\":%d, \"random\":%d, "
- " \"songpos\": %d, \"elapsedTime\": %d, \"totalTime\":%d, "
- " \"currentsongid\": %d"
- "}}",
- mpd_status_get_state(status),
- mpd_status_get_volume(status),
- mpd_status_get_repeat(status),
- mpd_status_get_single(status),
- mpd_status_get_consume(status),
- mpd_status_get_random(status),
- mpd_status_get_song_pos(status),
- mpd_status_get_elapsed_time(status),
- mpd_status_get_total_time(status),
- mpd_status_get_song_id(status));
- *current_song_id = mpd_status_get_song_id(status);
- *queue_version = mpd_status_get_queue_version(status);
- mpd_status_free(status);
- return len;
- }
- int mpd_put_current_song(char *buffer)
- {
- char *cur = buffer;
- const char *end = buffer + MAX_SIZE;
- struct mpd_song *song;
- song = mpd_run_current_song(mpd.conn);
- if(song == NULL)
- return 0;
- cur += snprintf(cur, end - cur, "{\"type\": \"song_change\", \"data\":"
- "{\"pos\":%d, \"title\":\"%s\"",
- mpd_song_get_pos(song),
- mpd_get_title(song));
- if(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) != NULL)
- cur += snprintf(cur, end - cur, ", \"artist\":\"%s\"",
- mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
- if(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0) != NULL)
- cur += snprintf(cur, end - cur, ", \"album\":\"%s\"",
- mpd_song_get_tag(song, MPD_TAG_ALBUM, 0));
- cur += snprintf(cur, end - cur, "}}");
- mpd_song_free(song);
- mpd_response_finish(mpd.conn);
- return cur - buffer;
- }
- int mpd_put_queue(char *buffer)
- {
- char *cur = buffer;
- const char *end = buffer + MAX_SIZE;
- struct mpd_entity *entity;
- if (!mpd_send_list_queue_meta(mpd.conn))
- RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta");
- cur += snprintf(cur, end - cur, "{\"type\": \"queue\", \"data\": [ ");
- while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
- const struct mpd_song *song;
- if(mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
- song = mpd_entity_get_song(entity);
- cur += snprintf(cur, end - cur,
- "{\"id\":%d, \"pos\":%d, \"duration\":%d, \"title\":\"%s\"},",
- mpd_song_get_id(song),
- mpd_song_get_pos(song),
- mpd_song_get_duration(song),
- mpd_get_title(song)
- );
- }
- mpd_entity_free(entity);
- }
- /* remove last ',' */
- cur--;
- cur += snprintf(cur, end - cur, "] }");
- return cur - buffer;
- }
- int mpd_put_browse(char *buffer, char *path)
- {
- char *cur = buffer;
- const char *end = buffer + MAX_SIZE;
- struct mpd_entity *entity;
- if (!mpd_send_list_meta(mpd.conn, path))
- RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
- cur += snprintf(cur, end - cur, "{\"type\":\"browse\",\"data\":[ ");
- while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
- const struct mpd_song *song;
- const struct mpd_directory *dir;
- const struct mpd_playlist *pl;
- switch (mpd_entity_get_type(entity)) {
- case MPD_ENTITY_TYPE_UNKNOWN:
- break;
- case MPD_ENTITY_TYPE_SONG:
- song = mpd_entity_get_song(entity);
- cur += snprintf(cur, end - cur,
- "{\"type\":\"song\",\"uri\":\"%s\",\"duration\":%d,\"title\":\"%s\"},",
- mpd_song_get_uri(song),
- mpd_song_get_duration(song),
- mpd_get_title(song)
- );
- break;
- case MPD_ENTITY_TYPE_DIRECTORY:
- dir = mpd_entity_get_directory(entity);
- cur += snprintf(cur, end - cur,
- "{\"type\":\"directory\",\"dir\":\"%s\", \"basename\":\"%s\"},",
- mpd_directory_get_path(dir),
- basename((char *)mpd_directory_get_path(dir))
- );
- break;
- case MPD_ENTITY_TYPE_PLAYLIST:
- pl = mpd_entity_get_playlist(entity);
- cur += snprintf(cur, end - cur,
- "{\"type\":\"playlist\",\"plist\":\"%s\"},",
- mpd_playlist_get_path(pl)
- );
- break;
- }
- mpd_entity_free(entity);
- }
- if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(mpd.conn)) {
- fprintf(stderr, "MPD mpd_send_list_meta: %s\n", mpd_connection_get_error_message(mpd.conn));
- mpd.conn_state = MPD_FAILURE;
- return 0;
- }
- /* remove last ',' */
- cur--;
- cur += snprintf(cur, end - cur, "] }");
- return cur - buffer;
- }
- int mpd_search(char *buffer, char *searchstr)
- {
- char *cur = buffer;
- const char *end = buffer + MAX_SIZE;
- struct mpd_song *song;
- if(mpd_search_db_songs(mpd.conn, false) == false)
- RETURN_ERROR_AND_RECOVER("mpd_search_db_songs");
- else if(mpd_search_add_any_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, searchstr) == false)
- RETURN_ERROR_AND_RECOVER("mpd_search_add_any_tag_constraint");
- else if(mpd_search_commit(mpd.conn) == false)
- RETURN_ERROR_AND_RECOVER("mpd_search_commit");
- else {
- cur += snprintf(cur, end - cur, "{\"type\": \"search\", \"data\": [ ");
- while((song = mpd_recv_song(mpd.conn)) != NULL) {
- cur += snprintf(cur, end - cur,
- "{\"type\":\"song\",\"uri\":\"%s\",\"duration\":%d,\"title\":\"%s\"},",
- mpd_song_get_uri(song),
- mpd_song_get_duration(song),
- mpd_get_title(song)
- );
- mpd_song_free(song);
- }
- /* remove last ',' */
- cur--;
- cur += snprintf(cur, end - cur, "] }");
- }
- return cur - buffer;
- }
- void mpd_disconnect()
- {
- mpd.conn_state = MPD_DISCONNECT;
- mpd_poll(NULL);
- }
|