mp3splt-gtk
snackamp_control.c
Go to the documentation of this file.
1 /**********************************************************
2  *
3  * mp3splt-gtk -- utility based on mp3splt,
4  * for mp3/ogg splitting without decoding
5  *
6  * Copyright: (C) 2005-2012 Alexandru Munteanu
7  * Contact: io_fx@yahoo.fr
8  *
9  * http://mp3splt.sourceforge.net/
10  *
11  *********************************************************/
12 
13 /**********************************************************
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28  * USA.
29  *
30  *********************************************************/
31 
32 /*!********************************************************
33  * \file
34  * Snackamp control
35  *
36  * this file contains functions to control the snackamp
37  * player
38  **********************************************************/
39 
40 #include "snackamp_control.h"
41 
46 gint connect_snackamp(gint port, ui_state *ui)
47 {
48  struct sockaddr_in host;
49  struct hostent *h;
50  gint return_err = 0;
51 
52 #ifdef __WIN32__
53  long winsockinit;
54  WSADATA winsock;
55  winsockinit = WSAStartup(0x0101,&winsock);
56 #endif
57 
58  if((h = gethostbyname("localhost"))==NULL)
59  {
60  return_err = 1;
61  }
62 
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);
66 
67  if (return_err == 0)
68  {
69  if ((ui->pi->socket_id = socket(AF_INET, SOCK_STREAM, 0))==-1)
70  {
71  return_err = 2;
72  }
73  }
74 
75  if (return_err == 0)
76  {
77  if ((connect(ui->pi->socket_id, (void *)&host, sizeof(host)))==-1)
78  {
79  return_err = 3;
80  }
81  }
82 
83  if (return_err == 0)
84  {
85 #ifdef __WIN32__
86 #else
87  if (NULL == (ui->pi->in = fdopen(ui->pi->socket_id, "r")) ||
88  NULL == (ui->pi->out = fdopen(ui->pi->socket_id, "w")))
89  {
90  return_err = 4;
91  }
92 #endif
93  }
94 
95  if (return_err == 0)
96  {
97  setvbuf(ui->pi->out, NULL, _IOLBF, 0);
98  ui->pi->connected = TRUE;
99  }
100 
101  if (return_err >= 2)
102  {
104  }
105 
106  return return_err;
107 }
108 
109 static gchar *cut_begin_end(gchar *result)
110 {
111  if (strchr(result,' ') != NULL)
112  {
113  gchar *test = strchr(result, ' ');
114  g_snprintf(result, strlen(result), "%s",test+1);
115  }
116 
117  //cut the \n at the end
118  gint result_str = strlen(result);
119  if (result_str >= 2)
120  {
121  result[result_str - 2] = '\0';
122  }
123 
124  return result;
125 }
126 
131 {
132  ui->pi->connected = FALSE;
133 #ifdef __WIN32__
134  return closesocket(ui->pi->socket_id);
135 #else
136  return close(ui->pi->socket_id);
137 #endif
138 }
139 
145 static gchar *snackamp_socket_send_message(gchar *message, ui_state *ui)
146 {
147  gchar *result = malloc(1024 * sizeof(gchar *));
148  strcpy(result,"disconnected");
149 
150 #ifdef __WIN32__
151  gboolean r = TRUE;
152 
153  gint err1 = send(ui->pi->socket_id, message, strlen(message), 0);
154  if (err1 <= 0)
155  {
157  }
158  else
159  {
160  gint err = recv(ui->pi->socket_id, result, 1024, 0);
161  if (err <= 0)
162  {
164  r = FALSE;
165  }
166  }
167 #else
168  fputs(message, ui->pi->out);
169  fgets(result, 1024, ui->pi->in);
170 #endif
171 
172  //if on win32 we put the \0 when we find \n because no line buffering
173 #ifdef __WIN32__
174  if (r)
175  {
176  if (strchr(result,'\n') != NULL)
177  {
178  gchar *line_end;
179  line_end = strchr(result,'\n') + 1;
180  *line_end = '\0';
181  }
182  }
183 #endif
184 
185  return result;
186 }
187 
189 static gint get_integer_from_string(gchar *result)
190 {
191  gint our_integer = 0;
192  gint i = 0;
193  gchar *number = NULL;
194  while ((isdigit(result[i])==0) && (result[i]!='\0') && (result[i]!='-'))
195  {
196  i++;
197  number = result + i;
198  }
199  if (! (number == (result + strlen(result))))
200  {
201  our_integer = atoi (number);
202  }
203 
204  return our_integer;
205 }
206 
208 static gboolean snackamp_is_connected(ui_state *ui)
209 {
210  return ui->pi->connected;
211 }
212 
214 void snackamp_get_song_infos(gchar *total_infos, ui_state *ui)
215 {
216  gchar *result = snackamp_socket_send_message("xmms_remote_get_info\n", ui);
217  result = cut_begin_end(result);
218 
219  //stereo/mono
220  char *a = strstr(result, " ");
221  if (a == NULL)
222  {
223  g_snprintf(total_infos, 512, "disconnected");
224  g_free(result);
225  return;
226  }
227 
228  gchar rate_str[32] = { '\0' };
229  gchar freq_str[32] = { '\0' };
230  gchar nch_str[32] = { '\0' };
231  gchar *ptr = NULL;
232 
233  if (strstr(a+1, " ") != NULL)
234  {
235  ptr = strstr(a+1, " ") + 1;
236  }
237 
238  //rate
239  gint i = 0;
240  while (result[i] != ' ' && isdigit(result[i]) && i < 16)
241  {
242  g_sprintf(rate_str, "%s%c",rate_str,result[i]);
243  i++;
244  }
245 
246  //cut the beginning
247  if (strchr(result, ' ') != NULL)
248  {
249  gchar *test = strchr(result,' ');
250  g_snprintf(result, strlen(result), "%s",test+1);
251  }
252 
253  //freq
254  i = 0;
255  while (result[i] != ' ' && isdigit(result[i]) && i < 16)
256  {
257  g_sprintf(freq_str, "%s%c",freq_str,result[i]);
258  i++;
259  }
260 
261  //channels int
262  gint nch = atoi(ptr);
263  if (nch == 2)
264  {
265  snprintf(nch_str, 32, "%s", _("stereo"));
266  }
267  else
268  {
269  snprintf(nch_str, 32, "%s", _("mono"));
270  }
271 
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);
275 
276  g_free(result);
277 }
278 
280 static gint snackamp_get_playlist_pos(ui_state *ui)
281 {
282  gchar *result = snackamp_socket_send_message("xmms_remote_get_playlist_pos\n", ui);
283  gint number = get_integer_from_string(result);
284  g_free(result);
285  return number;
286 }
287 
293 {
294  gint playlist_pos = snackamp_get_playlist_pos(ui);
295 
296  //we get the current file
297  gchar temp[100];
298  g_snprintf(temp, 100, "%s %d\n", "xmms_remote_get_playlist_file", playlist_pos);
299 
300  gchar *result = snackamp_socket_send_message(temp, ui);
301  result = cut_begin_end(result);
302 
303  return result;
304 }
305 
308 {
309  gchar *result = snackamp_socket_send_message("xmms_remote_stop\n", ui);
310  g_free(result);
311 }
312 
315 {
316  gchar *result = snackamp_socket_send_message("xmms_remote_get_playlist_length\n", ui);
317  gint number = get_integer_from_string(result);
318  g_free(result);
319 
320  if (number == -1)
321  {
322  snackamp_stop(ui);
323  }
324 
325  return number;
326 }
327 
333 {
334  gint playlist_pos = snackamp_get_playlist_pos(ui);
335 
336  gchar temp[100];
337  g_snprintf(temp, 100,"%s %d\n", "xmms_remote_get_playlist_title",playlist_pos);
338 
339  gchar *result = snackamp_socket_send_message(temp, ui);
340  result = cut_begin_end(result);
341 
342  return result;
343 }
344 
347 {
348  gchar *result = snackamp_socket_send_message("xmms_remote_get_output_time\n", ui);
349  gint pos = get_integer_from_string(result);
350  g_free(result);
351 
352  return pos;
353 }
354 
357 {
358  static gchar *exec_command = "snackAmp";
359  gchar *exec_this = g_strdup_printf("%s &", exec_command);
360  system(exec_this);
361 
362  time_t lt;
363  gint timer = time(&lt);
364  while ((!snackamp_is_running(ui)) && ((time(&lt) - timer) < 8))
365  {
366  usleep(0);
367  }
368 
369  g_free(exec_this);
370 }
371 
373 static void snackamp_set_playlist_pos(gint pos, ui_state *ui)
374 {
375  gchar temp[100];
376  g_snprintf(temp, 100, "%s %d\n", "xmms_remote_set_playlist_pos",pos);
377  gchar *result = snackamp_socket_send_message(temp, ui);
378  g_free(result);
379 }
380 
383 {
384  gint last_song = snackamp_get_playlist_number(ui);
385  snackamp_set_playlist_pos(last_song - 1, ui);
386 }
387 
390 {
391  gchar *result = snackamp_socket_send_message("xmms_remote_play\n", ui);
392  g_free(result);
393 }
394 
397 {
399  snackamp_play(ui);
400 }
401 
403 void snackamp_add_files(GList *list, ui_state *ui)
404 {
405  gint i = 0;
406  gchar *song = NULL;
407  while ((song = g_list_nth_data(list, i)) != NULL)
408  {
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);
412 
413  gchar *result = snackamp_socket_send_message(local, ui);
414  g_free(result);
415 
416  g_free(local);
417  i++;
418  }
419 }
420 
422 void snackamp_set_volume(gint volume, ui_state *ui)
423 {
424  gchar temp[100];
425  g_snprintf(temp, 100, "%s %d\n", "xmms_remote_set_main_volume", volume);
426  gchar *result = snackamp_socket_send_message(temp, ui);
427  g_free(result);
428 }
429 
432 {
433  gchar *result = snackamp_socket_send_message("xmms_remote_get_main_volume\n", ui);
434  gint vol = get_integer_from_string(result);
435  g_free(result);
436 
437  return vol;
438 }
439 
441 void snackamp_start_with_songs(GList *list, ui_state *ui)
442 {
443  snackamp_start(ui);
444  snackamp_add_files(list, ui);
445 }
446 
449 {
450  if (ui->pi->connected)
451  {
452  return TRUE;
453  }
454 
455  if (connect_snackamp(8775, ui) == 0)
456  {
457  return TRUE;
458  }
459 
460  return FALSE;
461 }
462 
465 {
466  gchar *result = snackamp_socket_send_message("xmms_remote_pause\n", ui);
467  g_free(result);
468 }
469 
472 {
473  gchar *result = snackamp_socket_send_message("xmms_remote_playlist_next\n", ui);
474  g_free(result);
475 }
476 
479 {
480  gint playlist_pos = snackamp_get_playlist_pos(ui);
481 
482  if (playlist_pos > 0)
483  {
484  gchar *result = snackamp_socket_send_message("xmms_remote_playlist_prev\n", ui);
485  g_free(result);
486  return;
487  }
488 
490 }
491 
493 void snackamp_jump(gint position, ui_state *ui)
494 {
495  gint hundr_secs_pos = position / 10;
496  gint hundr_secs = hundr_secs_pos % 100;
497  if (hundr_secs == 1)
498  {
499  hundr_secs = 0;
500  }
501 
502  gint secs = hundr_secs_pos / 100;
503  gfloat total_pos = hundr_secs + secs * 100;
504  total_pos /= 100;
505 
506  gchar temp[100];
507  g_snprintf(temp, 100, "%s %f\n", "xmms_remote_jump_to_time", total_pos);
508 
509  gchar *result = snackamp_socket_send_message(temp, ui);
510  g_free(result);
511 }
512 
515 {
516  gchar *result = snackamp_socket_send_message("xmms_remote_get_playlist_time\n", ui);
517  gint hundr_secs = get_integer_from_string(result) * 1000;
518  g_free(result);
519 
520  return hundr_secs;
521 }
522 
525 {
526  if (!snackamp_is_connected(ui))
527  {
528  return FALSE;
529  }
530 
531  gchar *result = snackamp_socket_send_message("xmms_remote_is_playing\n", ui);
532  gint i = atoi(result);
533  g_free(result);
534 
535  if (i == 0)
536  {
537  return FALSE;
538  }
539 
540  return TRUE;
541 }
542 
548 {
549  if (!snackamp_is_connected(ui))
550  {
551  return FALSE;
552  }
553 
554  gchar *result = snackamp_socket_send_message("xmms_remote_is_paused\n", ui);
555  result = cut_begin_end(result);
556 
557  gint i = atoi(result);
558  g_free(result);
559 
560  if (i == 1)
561  {
562  return TRUE;
563  }
564 
565  return FALSE;
566 }
567