40 #include "snackamp_control.h"
48 struct sockaddr_in host;
55 winsockinit = WSAStartup(0x0101,&winsock);
58 if((h = gethostbyname(
"localhost"))==NULL)
63 host.sin_family = AF_INET;
64 host.sin_addr.s_addr = ((
struct in_addr *) (h->h_addr)) ->s_addr;
65 host.sin_port=htons(port);
69 if ((ui->pi->socket_id = socket(AF_INET, SOCK_STREAM, 0))==-1)
77 if ((connect(ui->pi->socket_id, (
void *)&host,
sizeof(host)))==-1)
87 if (NULL == (ui->pi->in = fdopen(ui->pi->socket_id,
"r")) ||
88 NULL == (ui->pi->out = fdopen(ui->pi->socket_id,
"w")))
97 setvbuf(ui->pi->out, NULL, _IOLBF, 0);
98 ui->pi->connected = TRUE;
109 static gchar *cut_begin_end(gchar *result)
111 if (strchr(result,
' ') != NULL)
113 gchar *test = strchr(result,
' ');
114 g_snprintf(result, strlen(result),
"%s",test+1);
118 gint result_str = strlen(result);
121 result[result_str - 2] =
'\0';
132 ui->pi->connected = FALSE;
134 return closesocket(ui->pi->socket_id);
136 return close(ui->pi->socket_id);
145 static gchar *snackamp_socket_send_message(gchar *message,
ui_state *ui)
147 gchar *result = malloc(1024 *
sizeof(gchar *));
148 strcpy(result,
"disconnected");
153 gint err1 = send(ui->pi->socket_id, message, strlen(message), 0);
160 gint err = recv(ui->pi->socket_id, result, 1024, 0);
168 fputs(message, ui->pi->out);
169 fgets(result, 1024, ui->pi->in);
176 if (strchr(result,
'\n') != NULL)
179 line_end = strchr(result,
'\n') + 1;
189 static gint get_integer_from_string(gchar *result)
191 gint our_integer = 0;
193 gchar *number = NULL;
194 while ((isdigit(result[i])==0) && (result[i]!=
'\0') && (result[i]!=
'-'))
199 if (! (number == (result + strlen(result))))
201 our_integer = atoi (number);
208 static gboolean snackamp_is_connected(
ui_state *ui)
210 return ui->pi->connected;
216 gchar *result = snackamp_socket_send_message(
"xmms_remote_get_info\n", ui);
217 result = cut_begin_end(result);
220 char *a = strstr(result,
" ");
223 g_snprintf(total_infos, 512,
"disconnected");
228 gchar rate_str[32] = {
'\0' };
229 gchar freq_str[32] = {
'\0' };
230 gchar nch_str[32] = {
'\0' };
233 if (strstr(a+1,
" ") != NULL)
235 ptr = strstr(a+1,
" ") + 1;
240 while (result[i] !=
' ' && isdigit(result[i]) && i < 16)
242 g_sprintf(rate_str,
"%s%c",rate_str,result[i]);
247 if (strchr(result,
' ') != NULL)
249 gchar *test = strchr(result,
' ');
250 g_snprintf(result, strlen(result),
"%s",test+1);
255 while (result[i] !=
' ' && isdigit(result[i]) && i < 16)
257 g_sprintf(freq_str,
"%s%c",freq_str,result[i]);
262 gint nch = atoi(ptr);
265 snprintf(nch_str, 32,
"%s", _(
"stereo"));
269 snprintf(nch_str, 32,
"%s", _(
"mono"));
272 gchar *_Kbps = _(
"Kbps");
273 gchar *_Khz = _(
"Khz");
274 g_snprintf(total_infos, 512,
"%s %s %s %s %s", rate_str, _Kbps, freq_str, _Khz, nch_str);
280 static gint snackamp_get_playlist_pos(
ui_state *ui)
282 gchar *result = snackamp_socket_send_message(
"xmms_remote_get_playlist_pos\n", ui);
283 gint number = get_integer_from_string(result);
294 gint playlist_pos = snackamp_get_playlist_pos(ui);
298 g_snprintf(temp, 100,
"%s %d\n",
"xmms_remote_get_playlist_file", playlist_pos);
300 gchar *result = snackamp_socket_send_message(temp, ui);
301 result = cut_begin_end(result);
309 gchar *result = snackamp_socket_send_message(
"xmms_remote_stop\n", ui);
316 gchar *result = snackamp_socket_send_message(
"xmms_remote_get_playlist_length\n", ui);
317 gint number = get_integer_from_string(result);
334 gint playlist_pos = snackamp_get_playlist_pos(ui);
337 g_snprintf(temp, 100,
"%s %d\n",
"xmms_remote_get_playlist_title",playlist_pos);
339 gchar *result = snackamp_socket_send_message(temp, ui);
340 result = cut_begin_end(result);
348 gchar *result = snackamp_socket_send_message(
"xmms_remote_get_output_time\n", ui);
349 gint pos = get_integer_from_string(result);
358 static gchar *exec_command =
"snackAmp";
359 gchar *exec_this = g_strdup_printf(
"%s &", exec_command);
363 gint timer = time(<);
373 static void snackamp_set_playlist_pos(gint pos,
ui_state *ui)
376 g_snprintf(temp, 100,
"%s %d\n",
"xmms_remote_set_playlist_pos",pos);
377 gchar *result = snackamp_socket_send_message(temp, ui);
385 snackamp_set_playlist_pos(last_song - 1, ui);
391 gchar *result = snackamp_socket_send_message(
"xmms_remote_play\n", ui);
407 while ((song = g_list_nth_data(list, i)) != NULL)
409 gint malloc_int = strlen(song) + 30;
410 gchar *local = malloc(malloc_int *
sizeof(gchar *));
411 g_snprintf(local, malloc_int,
"%s {%s}\n",
"xmms_remote_playlist_add ", song);
413 gchar *result = snackamp_socket_send_message(local, ui);
425 g_snprintf(temp, 100,
"%s %d\n",
"xmms_remote_set_main_volume", volume);
426 gchar *result = snackamp_socket_send_message(temp, ui);
433 gchar *result = snackamp_socket_send_message(
"xmms_remote_get_main_volume\n", ui);
434 gint vol = get_integer_from_string(result);
450 if (ui->pi->connected)
466 gchar *result = snackamp_socket_send_message(
"xmms_remote_pause\n", ui);
473 gchar *result = snackamp_socket_send_message(
"xmms_remote_playlist_next\n", ui);
480 gint playlist_pos = snackamp_get_playlist_pos(ui);
482 if (playlist_pos > 0)
484 gchar *result = snackamp_socket_send_message(
"xmms_remote_playlist_prev\n", ui);
495 gint hundr_secs_pos = position / 10;
496 gint hundr_secs = hundr_secs_pos % 100;
502 gint secs = hundr_secs_pos / 100;
503 gfloat total_pos = hundr_secs + secs * 100;
507 g_snprintf(temp, 100,
"%s %f\n",
"xmms_remote_jump_to_time", total_pos);
509 gchar *result = snackamp_socket_send_message(temp, ui);
516 gchar *result = snackamp_socket_send_message(
"xmms_remote_get_playlist_time\n", ui);
517 gint hundr_secs = get_integer_from_string(result) * 1000;
526 if (!snackamp_is_connected(ui))
531 gchar *result = snackamp_socket_send_message(
"xmms_remote_is_playing\n", ui);
532 gint i = atoi(result);
549 if (!snackamp_is_connected(ui))
554 gchar *result = snackamp_socket_send_message(
"xmms_remote_is_paused\n", ui);
555 result = cut_begin_end(result);
557 gint i = atoi(result);