controls.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  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, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include "squeezelite.h"
  19. #include "audio_controls.h"
  20. static log_level loglevel = lINFO;
  21. static in_addr_t server_ip;
  22. static u16_t server_hport;
  23. static u16_t server_cport;
  24. static u8_t mac[6];
  25. static void (*chained_notify)(in_addr_t, u16_t, u16_t);
  26. static void cli_send_cmd(char *cmd);
  27. static void lms_volume_up(void) {
  28. cli_send_cmd("button volup");
  29. }
  30. static void lms_volume_down(void) {
  31. cli_send_cmd("button voldown");
  32. }
  33. static void lms_toggle(void) {
  34. cli_send_cmd("pause");
  35. }
  36. static void lms_pause(void) {
  37. cli_send_cmd("pause 1");
  38. }
  39. static void lms_play(void) {
  40. cli_send_cmd("button play.single");
  41. }
  42. static void lms_stop(void) {
  43. cli_send_cmd("button stop");
  44. }
  45. static void lms_rew(void) {
  46. cli_send_cmd("button rew.repeat");
  47. }
  48. static void lms_fwd(void) {
  49. cli_send_cmd("button fwd.repeat");
  50. }
  51. static void lms_prev(void) {
  52. cli_send_cmd("button rew");
  53. }
  54. static void lms_next(void) {
  55. cli_send_cmd("button fwd");
  56. }
  57. static void lms_up(void) {
  58. cli_send_cmd("button arrow_up");
  59. }
  60. static void lms_down(void) {
  61. cli_send_cmd("button arrow_down");
  62. }
  63. static void lms_left(void) {
  64. cli_send_cmd("button arrow_left");
  65. }
  66. static void lms_right(void) {
  67. cli_send_cmd("button arrow_right");
  68. }
  69. static void lms_knob_left(void) {
  70. cli_send_cmd("button knob_left");
  71. }
  72. static void lms_knob_right(void) {
  73. cli_send_cmd("button knob_right");
  74. }
  75. static void lms_knob_push(void) {
  76. cli_send_cmd("button knob_push");
  77. }
  78. const actrls_t LMS_controls = {
  79. lms_volume_up, lms_volume_down, // volume up, volume down
  80. lms_toggle, lms_play, // toggle, play
  81. lms_pause, lms_stop, // pause, stop
  82. lms_rew, lms_fwd, // rew, fwd
  83. lms_prev, lms_next, // prev, next
  84. lms_up, lms_down,
  85. lms_left, lms_right,
  86. lms_knob_left, lms_knob_right, lms_knob_push,
  87. };
  88. /****************************************************************************************
  89. *
  90. */
  91. static void cli_send_cmd(char *cmd) {
  92. char packet[64];
  93. struct sockaddr_in addr;
  94. socklen_t addrlen = sizeof(addr);
  95. int len, sock = socket(AF_INET, SOCK_STREAM, 0);
  96. addr.sin_family = AF_INET;
  97. addr.sin_addr.s_addr = server_ip;
  98. addr.sin_port = htons(server_cport);
  99. if (connect(sock, (struct sockaddr *) &addr, addrlen) < 0) {
  100. LOG_ERROR("unable to connect to server %s:%hu with cli", inet_ntoa(server_ip), server_cport);
  101. return;
  102. }
  103. len = sprintf(packet, "%02x:%02x:%02x:%02x:%02x:%02x %s\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], cmd);
  104. LOG_DEBUG("sending command %s at %s:%hu", packet, inet_ntoa(server_ip), server_cport);
  105. if (send(sock, packet, len, 0) < 0) {
  106. LOG_WARN("cannot send CLI %s", packet);
  107. }
  108. closesocket(sock);
  109. }
  110. /****************************************************************************************
  111. * Notification when server changes
  112. */
  113. static void notify(in_addr_t ip, u16_t hport, u16_t cport) {
  114. server_ip = ip;
  115. server_hport = hport;
  116. server_cport = cport;
  117. LOG_INFO("notified server %s hport %hu cport %hu", inet_ntoa(ip), hport, cport);
  118. if (chained_notify) (*chained_notify)(ip, hport, cport);
  119. }
  120. /****************************************************************************************
  121. * Initialize controls - shall be called once from output_init_embedded
  122. */
  123. void sb_controls_init(void) {
  124. LOG_INFO("initializing CLI controls");
  125. get_mac(mac);
  126. actrls_set_default(LMS_controls, NULL);
  127. chained_notify = server_notify;
  128. server_notify = notify;
  129. }