mpd_client.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ympd
  2. (c) 2013-2014 Andrew Karpow <andy@ndyk.de>
  3. This project's homepage is: http://www.ympd.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. #ifndef __MPD_CLIENT_H__
  16. #define __MPD_CLIENT_H__
  17. #include "mongoose.h"
  18. #define RETURN_ERROR_AND_RECOVER(X) do { \
  19. fprintf(stderr, "MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \
  20. cur += snprintf(cur, end - cur, "{\"type\":\"error\",\"data\":\"%s\"}", \
  21. mpd_connection_get_error_message(mpd.conn)); \
  22. if (!mpd_connection_clear_error(mpd.conn)) \
  23. mpd.conn_state = MPD_FAILURE; \
  24. return cur - buffer; \
  25. } while(0)
  26. #define MAX_SIZE 1024 * 100
  27. #define MAX_ELEMENTS_PER_PAGE 512
  28. #define GEN_ENUM(X) X,
  29. #define GEN_STR(X) #X,
  30. #define MPD_CMDS(X) \
  31. X(MPD_API_GET_QUEUE) \
  32. X(MPD_API_GET_BROWSE) \
  33. X(MPD_API_GET_MPDHOST) \
  34. X(MPD_API_ADD_TRACK) \
  35. X(MPD_API_ADD_PLAY_TRACK) \
  36. X(MPD_API_ADD_PLAYLIST) \
  37. X(MPD_API_PLAY_TRACK) \
  38. X(MPD_API_RM_TRACK) \
  39. X(MPD_API_RM_ALL) \
  40. X(MPD_API_SEARCH) \
  41. X(MPD_API_SET_VOLUME) \
  42. X(MPD_API_SET_PAUSE) \
  43. X(MPD_API_SET_PLAY) \
  44. X(MPD_API_SET_STOP) \
  45. X(MPD_API_SET_SEEK) \
  46. X(MPD_API_SET_NEXT) \
  47. X(MPD_API_SET_PREV) \
  48. X(MPD_API_SET_MPDHOST) \
  49. X(MPD_API_SET_MPDPASS) \
  50. X(MPD_API_UPDATE_DB) \
  51. X(MPD_API_TOGGLE_RANDOM) \
  52. X(MPD_API_TOGGLE_CONSUME) \
  53. X(MPD_API_TOGGLE_SINGLE) \
  54. X(MPD_API_TOGGLE_REPEAT)
  55. enum mpd_cmd_ids {
  56. MPD_CMDS(GEN_ENUM)
  57. };
  58. enum mpd_conn_states {
  59. MPD_DISCONNECTED,
  60. MPD_FAILURE,
  61. MPD_CONNECTED,
  62. MPD_RECONNECT,
  63. MPD_DISCONNECT
  64. };
  65. struct t_mpd {
  66. int port;
  67. char host[128];
  68. char *password;
  69. struct mpd_connection *conn;
  70. enum mpd_conn_states conn_state;
  71. /* Reponse Buffer */
  72. char buf[MAX_SIZE];
  73. size_t buf_size;
  74. int song_id;
  75. unsigned queue_version;
  76. } mpd;
  77. struct t_mpd_client_session {
  78. int song_id;
  79. unsigned queue_version;
  80. };
  81. void mpd_poll(struct mg_server *s);
  82. int callback_mpd(struct mg_connection *c);
  83. int mpd_close_handler(struct mg_connection *c);
  84. int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version);
  85. int mpd_put_current_song(char *buffer);
  86. int mpd_put_queue(char *buffer, unsigned int offset);
  87. int mpd_put_browse(char *buffer, char *path, unsigned int offset);
  88. int mpd_search(char *buffer, char *searchstr);
  89. void mpd_disconnect();
  90. #endif