ympd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <getopt.h>
  20. #include <sys/time.h>
  21. #include <pthread.h>
  22. #include "mongoose.h"
  23. #include "http_server.h"
  24. #include "mpd_client.h"
  25. #include "config.h"
  26. extern char *optarg;
  27. int force_exit = 0;
  28. void bye()
  29. {
  30. force_exit = 1;
  31. }
  32. static int server_callback(struct mg_connection *c) {
  33. if (c->is_websocket)
  34. {
  35. c->content[c->content_len] = '\0';
  36. if(c->content_len)
  37. return callback_mpd(c);
  38. else
  39. return MG_CLIENT_CONTINUE;
  40. }
  41. else
  42. return callback_http(c);
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. int n, option_index = 0;
  47. struct mg_server *server = mg_create_server(NULL);
  48. unsigned int current_timer = 0, last_timer = 0;
  49. atexit(bye);
  50. mg_set_option(server, "listening_port", "8080");
  51. mpd.port = 6600;
  52. strcpy(mpd.host, "127.0.0.1");
  53. static struct option long_options[] = {
  54. {"host", required_argument, 0, 'h'},
  55. {"port", required_argument, 0, 'p'},
  56. {"webport", required_argument, 0, 'w'},
  57. {"user", required_argument, 0, 'u'},
  58. {"version", no_argument, 0, 'V'},
  59. {"help", no_argument, 0, 0 },
  60. {0, 0, 0, 0 }
  61. };
  62. while((n = getopt_long(argc, argv, "h:p:w:u::V",
  63. long_options, &option_index)) != -1) {
  64. switch (n) {
  65. case 'h':
  66. strncpy(mpd.host, optarg, sizeof(mpd.host));
  67. break;
  68. case 'p':
  69. mpd.port = atoi(optarg);
  70. case 'w':
  71. mg_set_option(server, "listening_port", optarg);
  72. break;
  73. case 'u':
  74. mg_set_option(server, "run_as_user", optarg);
  75. break;
  76. case 'V':
  77. fprintf(stdout, "ympd %d.%d.%d\n"
  78. "Copyright (C) 2014 Andrew Karpow <andy@ndyk.de>\n"
  79. "built " __DATE__ " "__TIME__ " ("__VERSION__")\n",
  80. YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH);
  81. return EXIT_SUCCESS;
  82. break;
  83. default:
  84. fprintf(stderr, "Usage: %s [OPTION]...\n\n"
  85. "\t-h, --host <host>\t\tconnect to mpd at host [localhost]\n"
  86. "\t-p, --port <port>\t\tconnect to mpd at port [6600]\n"
  87. "\t-w, --webport [ip:]<port>\t\tlisten interface/port for webserver [8080]\n"
  88. "\t-u, --user <username>\t\t\tdrop priviliges to user after socket bind\n"
  89. "\t-V, --version\t\t\tget version\n"
  90. "\t--help\t\t\t\tthis help\n"
  91. , argv[0]);
  92. return EXIT_FAILURE;
  93. }
  94. }
  95. mg_set_http_close_handler(server, mpd_close_handler);
  96. mg_set_request_handler(server, server_callback);
  97. while (!force_exit) {
  98. current_timer = mg_poll_server(server, 200);
  99. if(current_timer - last_timer)
  100. {
  101. last_timer = current_timer;
  102. mpd_poll(server);
  103. }
  104. }
  105. mpd_disconnect();
  106. mg_destroy_server(&server);
  107. return EXIT_SUCCESS;
  108. }