1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "debug_printf.h"
31 #include "libmpd-internal.h"
33 static void mpd_free_queue_ob(MpdObj
*mi
);
34 static void mpd_server_free_commands(MpdObj
*mi
);
37 char *libmpd_version
= LIBMPD_VERSION
;
40 * Not every platfarm has strndup, so here we have a nice little custom implementation
42 char * strndup(const char *s
, size_t n
)
51 /* nAvail = min( strlen(s)+1, n+1 ); */
52 nAvail
=((strlen(s
)+1) > (n
+1)) ? n
+1 : strlen(s
)+1;
53 if(!(p
=malloc(nAvail
))) {
62 * @param state a #MpdServerState to initialize
64 * Initialize #MpdServerState. To stop duplicating code.
66 static void mpd_init_MpdServerState(MpdServerState
*state
)
68 state
->playlistid
= -1;
69 state
->storedplaylistid
= -1;
73 state
->dbUpdateTime
= 0;
74 state
->updatingDb
= 0;
80 state
->elapsedtime
= 0;
82 state
->samplerate
= 0;
85 state
->playlistLength
= 0;
86 state
->error
[0] = '\0';
91 static MpdObj
* mpd_create()
93 MpdObj
* mi
= malloc(sizeof(*mi
));
96 /* should never happen on linux */
101 /* set default values */
102 /* we start not connected */
103 mi
->connected
= FALSE
;
104 /* port 6600 is the default mpd port */
107 mi
->hostname
= strdup("localhost");
110 /* 1 second timeout */
111 mi
->connection_timeout
= 1.0;
112 /* we have no connection pointer */
113 mi
->connection
= NULL
;
119 mi
->error_mpd_code
= 0;
120 mi
->error_msg
= NULL
;
121 mi
->CurrentSong
= NULL
;
123 mpd_init_MpdServerState(&(mi
->CurrentState
));
124 mpd_init_MpdServerState(&(mi
->OldState
));
127 * Set signals to NULL
129 /* connection changed signal */
130 mi
->the_connection_changed_callback
= NULL
;
131 mi
->the_connection_changed_signal_userdata
= NULL
;
134 mi
->the_status_changed_callback
= NULL
;
135 mi
->the_status_changed_signal_userdata
= NULL
;
138 mi
->the_error_callback
= NULL
;
139 mi
->the_error_signal_userdata
= NULL
;
140 /* connection is locked because where not connected */
141 mi
->connection_lock
= TRUE
;
143 /* set queue to NULL */
146 mi
->search_type
= MPD_SEARCH_TYPE_NONE
;
147 /* no need to initialize, but set it to anything anyway*/
148 mi
->search_field
= MPD_TAG_ITEM_ARTIST
;
152 mi
->output_states
= NULL
;
154 memset((mi
->supported_tags
), 0,sizeof(mi
->supported_tags
));
162 void mpd_free(MpdObj
*mi
)
164 debug_printf(DEBUG_INFO
, "destroying MpdObj object\n");
168 debug_printf(DEBUG_WARNING
, "Connection still running, disconnecting\n");
186 mpd_closeConnection(mi
->connection
);
190 mpd_freeStatus(mi
->status
);
194 mpd_freeStats(mi
->stats
);
198 mpd_freeSong(mi
->CurrentSong
);
200 mpd_free_queue_ob(mi
);
201 mpd_server_free_commands(mi
);
205 int mpd_check_error(MpdObj
*mi
)
209 debug_printf(DEBUG_ERROR
, "mi == NULL?");
210 return MPD_ARGS_ERROR
;
213 /* this shouldn't happen, ever */
214 if(mi
->connection
== NULL
)
216 debug_printf(DEBUG_ERROR
, "mi->connection == NULL?");
217 return MPD_FATAL_ERROR
;
220 /* TODO: map these errors in the future */
221 mi
->error
= mi
->connection
->error
;
222 mi
->error_mpd_code
= mi
->connection
->errorCode
;
223 /*TODO: do I need to strdup this? */
224 if(!g_utf8_validate(mi
->connection
->errorStr
, -1, NULL
)){
225 mi
->error_msg
= g_locale_to_utf8(mi
->connection
->errorStr
, -1, NULL
, NULL
,NULL
);
227 mi
->error_msg
= g_strdup(mi
->connection
->errorStr
);
230 if(mi
->error_msg
== NULL
) mi
->error_msg
= g_strdup("Failed to convert error message to utf-8");
232 /* Check for permission */
233 /* First check for an error reported by MPD
234 * Then check what type of error mpd reported
236 if(mi
->error
== MPD_ERROR_ACK
)
239 debug_printf(DEBUG_ERROR
,"clearing errors in mpd_Connection: %i-%s", mi
->connection
->errorCode
, mi
->connection
->errorStr
);
240 mpd_clearError(mi
->connection
);
241 if (mi
->the_error_callback
)
243 debug_printf(DEBUG_ERROR
, "Error callback 1 (ACK)");
244 if(mi
->the_error_callback(mi
, mi
->error_mpd_code
, mi
->error_msg
, mi
->the_error_signal_userdata
))
246 debug_printf(DEBUG_ERROR
, "Error callback told me to disconnect");
249 mi
->error_msg
= NULL
;
251 return MPD_SERVER_ERROR
;
255 mi
->error_msg
= NULL
;
261 debug_printf(DEBUG_ERROR
, "Following error occurred: %i: code: %i msg: %s", mi
->error
,mi
->connection
->errorCode
, mi
->error_msg
);
263 if (mi
->the_error_callback
)
265 debug_printf(DEBUG_ERROR
, "Error callback 2");
266 mi
->the_error_callback(mi
, mi
->error
, mi
->error_msg
, mi
->the_error_signal_userdata
);
270 mi
->error_msg
= NULL
;
272 return MPD_SERVER_ERROR
;
275 mi
->error_msg
= NULL
;
281 int mpd_lock_conn(MpdObj
*mi
)
284 if(mi
->connection_lock
)
286 debug_printf(DEBUG_WARNING
, "Failed to lock connection, already locked\n");
287 return MPD_LOCK_FAILED
;
289 mi
->connection_lock
= TRUE
;
293 int mpd_unlock_conn(MpdObj
*mi
)
295 if(!mi
->connection_lock
)
297 debug_printf(DEBUG_ERROR
, "Failed to unlock connection, already unlocked\n");
298 return MPD_LOCK_FAILED
;
301 mi
->connection_lock
= FALSE
;
303 return mpd_check_error(mi
);
306 MpdObj
* mpd_new_default()
308 debug_printf(DEBUG_INFO
, "creating a new mpdInt object\n");
312 MpdObj
*mpd_new(char *hostname
, int port
, char *password
)
314 MpdObj
*mi
= mpd_create();
321 mpd_set_hostname(mi
, hostname
);
325 mpd_set_port(mi
, port
);
329 mpd_set_password(mi
, password
);
335 const char * mpd_get_hostname(MpdObj
*mi
)
344 int mpd_set_hostname(MpdObj
*mi
, char *hostname
)
348 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
349 return MPD_ARGS_ERROR
;
352 if(mi
->hostname
!= NULL
)
356 /* possible location todo some post processing of hostname */
357 mi
->hostname
= strdup(hostname
);
361 int mpd_set_password(MpdObj
*mi
, const char *password
)
365 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
366 return MPD_ARGS_ERROR
;
369 if(mi
->password
!= NULL
)
373 /* possible location todo some post processing of password */
374 mi
->password
= strdup(password
);
379 int mpd_send_password(MpdObj
*mi
)
381 if(!mi
) return MPD_ARGS_ERROR
;
382 if(mi
->password
&& mpd_check_connected(mi
) && strlen(mi
->password
))
384 if(mpd_lock_conn(mi
))
386 debug_printf(DEBUG_WARNING
, "failed to lock connection");
387 return MPD_LOCK_FAILED
;
389 mpd_sendPasswordCommand(mi
->connection
, mi
->password
);
390 mpd_finishCommand(mi
->connection
);
391 if(mpd_unlock_conn(mi
))
393 debug_printf(DEBUG_ERROR
, "Failed to unlock connection\n");
394 return MPD_LOCK_FAILED
;
396 mpd_server_get_allowed_commands(mi
);
397 /*TODO: should I do it here, or in the
398 * mpd_server_get_allowed_command, so it also get's executed on
401 if((mi
->the_status_changed_callback
!= NULL
))
403 mi
->the_status_changed_callback( mi
,
404 MPD_CST_PERMISSION
, mi
->the_status_changed_signal_userdata
);
410 int mpd_set_port(MpdObj
*mi
, int port
)
414 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
415 return MPD_ARGS_ERROR
;
421 int mpd_set_connection_timeout(MpdObj
*mi
, float timeout
)
425 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
426 return MPD_ARGS_ERROR
;
428 mi
->connection_timeout
= timeout
;
429 if(mpd_check_connected(mi
))
431 /*TODO: set timeout */
432 if(mpd_lock_conn(mi
))
434 debug_printf(DEBUG_ERROR
,"lock failed\n");
435 return MPD_LOCK_FAILED
;
437 mpd_setConnectionTimeout(mi
->connection
, timeout
);
438 mpd_finishCommand(mi
->connection
);
446 static void mpd_server_free_commands(MpdObj
*mi
)
451 while(mi
->commands
[i
].command_name
)
453 free(mi
->commands
[i
].command_name
);
461 char *mpd_server_get_version(MpdObj
*mi
)
464 if(!mi
|| !mpd_check_connected(mi
))
466 retval
= malloc(10*sizeof(char));
467 snprintf(retval
,10,"%i.%i.%i", mi
->connection
->version
[0], mi
->connection
->version
[1], mi
->connection
->version
[2]);
468 /* always make sure the string is terminated */
473 int mpd_server_get_allowed_commands(MpdObj
*mi
)
476 int num_commands
= 0;
478 debug_printf(DEBUG_ERROR
, "mi != NULL failed\n");
479 return MPD_ARGS_ERROR
;
481 if(!mpd_check_connected(mi
)) {
482 debug_printf(DEBUG_WARNING
, "Not Connected");
483 return MPD_NOT_CONNECTED
;
485 if(!mpd_server_check_version(mi
,0,12,0)){
486 debug_printf(DEBUG_INFO
, "Not supported by mpd");
487 return MPD_SERVER_NOT_SUPPORTED
;
490 mpd_server_free_commands(mi
);
492 if(mpd_lock_conn(mi
))
494 debug_printf(DEBUG_ERROR
, "lock failed");
495 return MPD_LOCK_FAILED
;
497 mpd_sendCommandsCommand(mi
->connection
);
498 while((temp
= mpd_getNextCommand(mi
->connection
)))
501 mi
->commands
= realloc(mi
->commands
, (num_commands
+1)*sizeof(MpdCommand
));
502 mi
->commands
[num_commands
-1].command_name
= temp
;
503 mi
->commands
[num_commands
-1].enabled
= TRUE
;
504 mi
->commands
[num_commands
].command_name
= NULL
;
505 mi
->commands
[num_commands
].enabled
= FALSE
;
506 if(strcmp(mi
->commands
[num_commands
-1].command_name
, "idle") == 0) {
510 mpd_finishCommand(mi
->connection
);
511 mpd_sendNotCommandsCommand(mi
->connection
);
512 while((temp
= mpd_getNextCommand(mi
->connection
)))
515 mi
->commands
= realloc(mi
->commands
, (num_commands
+1)*sizeof(MpdCommand
));
516 mi
->commands
[num_commands
-1].command_name
= temp
;
517 mi
->commands
[num_commands
-1].enabled
= FALSE
;
518 mi
->commands
[num_commands
].command_name
= NULL
;
519 mi
->commands
[num_commands
].enabled
= FALSE
;
521 mpd_finishCommand(mi
->connection
);
523 if(mpd_unlock_conn(mi
))
525 return MPD_LOCK_FAILED
;
532 int mpd_disconnect(MpdObj
*mi
)
537 debug_printf(DEBUG_INFO
, "disconnecting\n");
541 mpd_closeConnection(mi
->connection
);
542 mi
->connection
= NULL
;
546 mpd_freeStatus(mi
->status
);
551 mpd_freeStats(mi
->stats
);
556 mpd_freeSong(mi
->CurrentSong
);
557 mi
->CurrentSong
= NULL
;
559 mi
->CurrentState
.playlistid
= -1;
560 mi
->CurrentState
.storedplaylistid
= -1;
561 mi
->CurrentState
.state
= -1;
562 mi
->CurrentState
.songid
= -1;
563 mi
->CurrentState
.songpos
= -1;
564 mi
->CurrentState
.dbUpdateTime
= 0;
565 mi
->CurrentState
.updatingDb
= 0;
566 mi
->CurrentState
.repeat
= -1;
567 mi
->CurrentState
.random
= -1;
568 mi
->CurrentState
.volume
= -2;
569 mi
->CurrentState
.xfade
= -1;
570 mi
->CurrentState
.totaltime
= 0;
571 mi
->CurrentState
.elapsedtime
= 0;
572 mi
->CurrentState
.bitrate
= 0;
573 mi
->CurrentState
.samplerate
= 0;
574 mi
->CurrentState
.channels
= 0;
575 mi
->CurrentState
.bits
= 0;
576 mi
->CurrentState
.playlistLength
= 0;
577 mi
->CurrentState
.error
[0] = '\0';
579 mi
->search_type
= MPD_SEARCH_TYPE_NONE
;
580 /* no need to initialize, but set it to anything anyway*/
581 mi
->search_field
= MPD_TAG_ITEM_ARTIST
;
584 if(mi
->output_states
)
585 g_free(mi
->output_states
);
586 mi
->output_states
= NULL
;
589 memset((mi
->supported_tags
), 0,sizeof(mi
->supported_tags
));
593 memcpy(&(mi
->OldState
), &(mi
->CurrentState
) , sizeof(MpdServerState
));
595 mpd_free_queue_ob(mi
);
596 mpd_server_free_commands(mi
);
597 /*don't reset errors */
598 /* Remove this signal, we don't actually disconnect */
601 /* set disconnect flag */
602 mi
->connected
= FALSE
;
604 if(mi
->the_connection_changed_callback
!= NULL
)
606 mi
->the_connection_changed_callback( mi
, FALSE
, mi
->the_connection_changed_signal_userdata
);
609 debug_printf(DEBUG_INFO
, "Disconnect completed\n");
612 int mpd_connect(MpdObj
*mi
)
614 return mpd_connect_real(mi
,NULL
);
616 int mpd_connect_real(MpdObj
*mi
,mpd_Connection
*connection
)
621 /* should return some spiffy error here */
622 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
623 return MPD_ARGS_ERROR
;
627 mi
->error_mpd_code
= 0;
628 if(mi
->error_msg
!= NULL
)
632 mi
->error_msg
= NULL
;
634 debug_printf(DEBUG_INFO
, "connecting\n");
635 mpd_init_MpdServerState(&(mi
->CurrentState
));
637 memcpy(&(mi
->OldState
), &(mi
->CurrentState
), sizeof(MpdServerState
));
645 if(mi
->hostname
== NULL
)
647 mpd_set_hostname(mi
, "localhost");
649 /* make sure this is locked */
650 if(!mi
->connection_lock
)
655 mi
->connection
= connection
;
657 /* make timeout configurable */
658 mi
->connection
= mpd_newConnection(mi
->hostname
,mi
->port
,mi
->connection_timeout
);
660 if(mi
->connection
== NULL
)
662 /* TODO: make seperate error message? */
663 return MPD_NOT_CONNECTED
;
665 if(mpd_check_error(mi
) != MPD_OK
)
667 /* TODO: make seperate error message? */
668 return MPD_NOT_CONNECTED
;
671 /* set connected state */
672 mi
->connected
= TRUE
;
673 if(mpd_unlock_conn(mi
))
675 return MPD_LOCK_FAILED
;
678 /* get the commands we are allowed to use */
679 retv
= mpd_server_get_allowed_commands(mi
);
686 retv = mpd_server_update_outputs(mi);
690 debug_printf(DEBUG_INFO
, "Propagating connection changed");
692 if(mi
->the_connection_changed_callback
!= NULL
)
694 mi
->the_connection_changed_callback( mi
, TRUE
, mi
->the_connection_changed_signal_userdata
);
696 retv
= mpd_server_update_outputs(mi
);
697 /** update the supported tags */
700 char **retv
= mpd_server_get_tag_types(mi
);
702 for(i
=0;i
<MPD_TAG_ITEM_ANY
;i
++)
705 for(j
=0;retv
[j
] && strcasecmp(retv
[j
],mpdTagItemKeys
[i
]); j
++);
706 if(retv
[j
]) mi
->supported_tags
[i
] = TRUE
;
707 else mi
->supported_tags
[i
] = FALSE
;
711 /* also always true */
712 mi
->supported_tags
[MPD_TAG_ITEM_FILENAME
] = TRUE
;
713 mi
->supported_tags
[MPD_TAG_ITEM_ANY
] = TRUE
;
714 printf("Supported tags by server\n");
715 for(i
=0;i
< MPD_TAG_NUM_OF_ITEM_TYPES
;i
++){
716 printf("%20s: %s\n", mpdTagItemKeys
[i
], (mi
->supported_tags
[i
])?"true":"false");
723 debug_printf(DEBUG_INFO
, "Connected to mpd");
727 int mpd_check_connected(MpdObj
*mi
)
733 return mi
->connected
;
738 void mpd_signal_connect_status_changed (MpdObj
*mi
, StatusChangedCallback status_changed
, void *userdata
)
742 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
745 mi
->the_status_changed_callback
= status_changed
;
746 mi
->the_status_changed_signal_userdata
= userdata
;
750 void mpd_signal_connect_error(MpdObj
*mi
, ErrorCallback error_callback
, void *userdata
)
754 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
757 mi
->the_error_callback
= error_callback
;
758 mi
->the_error_signal_userdata
= userdata
;
761 void mpd_signal_connect_connection_changed(MpdObj
*mi
, ConnectionChangedCallback connection_changed
, void *userdata
)
765 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
768 mi
->the_connection_changed_callback
= connection_changed
;
769 mi
->the_connection_changed_signal_userdata
= userdata
;
775 MpdData
*mpd_new_data_struct(void)
778 data
= g_slice_new(MpdData_real
);
784 data
->directory
= NULL
;
785 data
->playlist
= NULL
;
786 data
->output_dev
= NULL
;
791 data
->userdata
= NULL
;
792 data
->freefunc
= NULL
;
793 return (MpdData
*)data
;
796 MpdData
*mpd_new_data_struct_append(MpdData
* data
)
798 MpdData_real
*data_real
= (MpdData_real
*)data
;
799 if(data_real
== NULL
)
801 data_real
= (MpdData_real
*)mpd_new_data_struct();
802 data_real
->first
= data_real
;
806 data_real
->next
= (MpdData_real
*)mpd_new_data_struct();
807 data_real
->next
->prev
= data_real
;
808 data_real
= data_real
->next
;
809 data_real
->next
= NULL
;
810 data_real
->first
= data_real
->prev
->first
;
812 return (MpdData
*)data_real
;
815 MpdData
* mpd_data_get_first(MpdData
const * const data
)
817 MpdData_real
const * const data_real
= (MpdData_real
const * const)data
;
818 if(data_real
!= NULL
)
820 return (MpdData
*)data_real
->first
;
826 MpdData
* mpd_data_get_next(MpdData
* const data
)
828 return mpd_data_get_next_real(data
, TRUE
);
831 MpdData
* mpd_data_get_next_real(MpdData
* const data
, int kill_list
)
833 MpdData_real
*data_real
= (MpdData_real
*)data
;
834 if (data_real
!= NULL
)
836 if (data_real
->next
!= NULL
)
838 return (MpdData
*)data_real
->next
;
842 if (kill_list
) mpd_data_free((MpdData
*)data_real
);
846 return (MpdData
*)data_real
;
849 int mpd_data_is_last(MpdData
const * const data
)
851 MpdData_real
const * const data_real
= (MpdData_real
const * const)data
;
852 if(data_real
!= NULL
)
854 if (data_real
->next
== NULL
)
862 MpdData_head *mpd_data_get_head(MpdData const * const data) {
863 return ((MpdData_real*)data)->head;
866 MpdData
* mpd_data_concatenate( MpdData
* const first
, MpdData
* const second
)
868 MpdData_real
*first_real
= (MpdData_real
*)first
;
869 MpdData_real
*second_real
= (MpdData_real
*)second
;
870 MpdData_real
*first_head
= NULL
;
872 if ( first
== NULL
) {
873 if ( second
!= NULL
)
874 return (MpdData
*)second_real
;
878 if ( second
== NULL
)
879 return (MpdData
*)first_real
;
882 first_head
= (MpdData_real
*)mpd_data_get_first(first
);
884 /* find last element in first data list */
885 while (!mpd_data_is_last((MpdData
*)first_real
)) first_real
= (MpdData_real
*)mpd_data_get_next_real((MpdData
*)first_real
, FALSE
);
886 second_real
=(MpdData_real
*) mpd_data_get_first((MpdData
*)second_real
);
888 first_real
->next
= second_real
;
889 second_real
->prev
= first_real
;
891 /* I need to set all the -> first correct */
894 second_real
->first
= first_head
;
895 second_real
= (MpdData_real
*)mpd_data_get_next_real((MpdData
*)second_real
, FALSE
);
898 return (MpdData
*)first_head
;
901 * Deletes an item from the list. It returns the next item in the list.
902 * if that is not available, it will return the last item
904 MpdData
* mpd_data_delete_item(MpdData
*data
)
906 MpdData_real
*temp
= NULL
, *data_real
= (MpdData_real
*)data
;
907 if(data_real
== NULL
) return NULL
;
908 /* if there is a next item, fix the prev pointer of the next item */
911 data_real
->next
->prev
= data_real
->prev
;
912 temp
= data_real
->next
;
914 /* if there is a previous item, fix the next pointer of the previous item */
917 /* the next item of the previous is the next item of the current */
918 data_real
->prev
->next
= data_real
->next
;
919 /* temp is the previous item */
920 temp
= data_real
->prev
;
923 /* fix first, if removed item is the first */
924 if(temp
&& temp
->first
== data_real
)
926 MpdData_real
*first
,*node
= temp
;
928 for(;node
->prev
;node
= node
->prev
);
935 /* make the removed row a valid list, so I can use the default free function to free it */
936 data_real
->next
= NULL
;
937 data_real
->prev
= NULL
;
938 data_real
->first
= data_real
;
940 mpd_data_free((MpdData
*)data_real
);
942 return (MpdData
*)temp
;
945 void mpd_data_free(MpdData
*data
)
947 MpdData_real
*data_real
,*temp
;
950 debug_printf(DEBUG_ERROR
, "data != NULL Failed");
953 data_real
= (MpdData_real
*)mpd_data_get_first(data
);
956 if (data_real
->type
== MPD_DATA_TYPE_SONG
) {
957 if(data_real
->song
) mpd_freeSong(data_real
->song
);
958 } else if (data_real
->type
== MPD_DATA_TYPE_OUTPUT_DEV
) {
959 mpd_freeOutputElement(data_real
->output_dev
);
960 } else if(data_real
->type
== MPD_DATA_TYPE_DIRECTORY
) {
961 if(data_real
->directory
)free(data_real
->directory
);
962 } else if(data_real
->type
== MPD_DATA_TYPE_PLAYLIST
) {
963 if(data_real
->playlist
) mpd_freePlaylistFile(data_real
->playlist
);
965 free((void*)(data_real
->tag
));
967 if(data_real
->freefunc
)
969 if(data_real
->userdata
)
970 data_real
->freefunc(data_real
->userdata
);
972 data_real
= data_real
->next
;
973 g_slice_free1(sizeof(*temp
), temp
);
977 /* clean this up.. make one while loop */
978 static void mpd_free_queue_ob(MpdObj
*mi
)
980 MpdQueue
*temp
= NULL
;
983 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
986 if(mi
->queue
== NULL
)
988 debug_printf(DEBUG_INFO
, "mi->queue != NULL failed, nothing to clean.");
991 mi
->queue
= mi
->queue
->first
;
992 while(mi
->queue
!= NULL
)
994 temp
= mi
->queue
->next
;
996 if(mi
->queue
->path
!= NULL
)
998 free(mi
->queue
->path
);
1008 MpdQueue
*mpd_new_queue_struct()
1010 MpdQueue
* queue
= malloc(sizeof(MpdQueue
));
1020 void mpd_queue_get_next(MpdObj
*mi
)
1022 if(mi
->queue
!= NULL
&& mi
->queue
->next
!= NULL
)
1024 mi
->queue
= mi
->queue
->next
;
1026 else if(mi
->queue
->next
== NULL
)
1028 mpd_free_queue_ob(mi
);
1033 long unsigned mpd_server_get_database_update_time(MpdObj
*mi
)
1035 if(!mpd_check_connected(mi
))
1037 debug_printf(DEBUG_WARNING
,"not connected\n");
1038 return MPD_NOT_CONNECTED
;
1040 if(mpd_stats_check(mi
) != MPD_OK
)
1042 debug_printf(DEBUG_WARNING
,"Failed grabbing status\n");
1043 return MPD_STATS_FAILED
;
1045 return mi
->stats
->dbUpdateTime
;
1049 MpdData
* mpd_server_get_output_devices(MpdObj
*mi
)
1051 mpd_OutputEntity
*output
= NULL
;
1052 MpdData
*data
= NULL
;
1053 if(!mpd_check_connected(mi
))
1055 debug_printf(DEBUG_WARNING
,"not connected\n");
1058 /* TODO: Check version */
1059 if(mpd_lock_conn(mi
))
1061 debug_printf(DEBUG_ERROR
,"lock failed\n");
1065 mpd_sendOutputsCommand(mi
->connection
);
1066 while (( output
= mpd_getNextOutput(mi
->connection
)) != NULL
)
1068 data
= mpd_new_data_struct_append(data
);
1069 data
->type
= MPD_DATA_TYPE_OUTPUT_DEV
;
1070 data
->output_dev
= output
;
1072 mpd_finishCommand(mi
->connection
);
1075 mpd_unlock_conn(mi
);
1080 return mpd_data_get_first(data
);
1083 int mpd_server_set_output_device(MpdObj
*mi
,int device_id
,int state
)
1085 if(!mpd_check_connected(mi
))
1087 debug_printf(DEBUG_WARNING
,"not connected\n");
1088 return MPD_NOT_CONNECTED
;
1090 if(mpd_lock_conn(mi
))
1092 debug_printf(DEBUG_ERROR
,"lock failed\n");
1093 return MPD_LOCK_FAILED
;
1097 mpd_sendEnableOutputCommand(mi
->connection
, device_id
);
1101 mpd_sendDisableOutputCommand(mi
->connection
, device_id
);
1103 mpd_finishCommand(mi
->connection
);
1105 mpd_unlock_conn(mi
);
1106 mpd_status_queue_update(mi
);
1110 int mpd_server_check_version(MpdObj
*mi
, int major
, int minor
, int micro
)
1112 if(!mpd_check_connected(mi
))
1114 debug_printf(DEBUG_WARNING
,"not connected\n");
1117 if(major
> mi
->connection
->version
[0]) return FALSE
;
1118 if(mi
->connection
->version
[0] > major
) return TRUE
;
1119 if(minor
> mi
->connection
->version
[1]) return FALSE
;
1120 if(mi
->connection
->version
[1] > minor
) return TRUE
;
1121 if(micro
> mi
->connection
->version
[2]) return FALSE
;
1122 if(mi
->connection
->version
[2] > micro
) return TRUE
;
1126 int mpd_server_check_command_allowed(MpdObj
*mi
, const char *command
)
1129 if(!mi
|| !command
) return MPD_SERVER_COMMAND_ERROR
;
1130 /* when we are connected to a mpd server that doesn't support commands and not commands
1131 * feature. (like mpd 0.11.5) allow everything
1133 if(!mpd_server_check_version(mi
, 0,12,0)) return MPD_SERVER_COMMAND_ALLOWED
;
1135 * Also when somehow we failted to get commands
1137 if(mi
->commands
== NULL
) return MPD_SERVER_COMMAND_ALLOWED
;
1141 for(i
=0;mi
->commands
[i
].command_name
;i
++)
1143 if(!strcasecmp(mi
->commands
[i
].command_name
, command
))
1144 return mi
->commands
[i
].enabled
;
1146 return MPD_SERVER_COMMAND_NOT_SUPPORTED
;
1149 char ** mpd_server_get_url_handlers(MpdObj
*mi
)
1154 if(!mpd_check_connected(mi
))
1156 debug_printf(DEBUG_WARNING
,"not connected\n");
1159 if(mpd_lock_conn(mi
))
1161 debug_printf(DEBUG_ERROR
,"lock failed\n");
1164 mpd_sendUrlHandlersCommand(mi
->connection
);
1165 while((temp
= mpd_getNextHandler(mi
->connection
)) != NULL
)
1167 retv
= realloc(retv
,(i
+2)*sizeof(*retv
));
1172 mpd_finishCommand(mi
->connection
);
1175 mpd_unlock_conn(mi
);
1178 char ** mpd_server_get_tag_types(MpdObj
*mi
)
1183 if(!mpd_check_connected(mi
))
1185 debug_printf(DEBUG_WARNING
,"not connected\n");
1188 if(mpd_lock_conn(mi
))
1190 debug_printf(DEBUG_ERROR
,"lock failed\n");
1193 mpd_sendTagTypesCommand(mi
->connection
);
1194 while((temp
= mpd_getNextTagType(mi
->connection
)) != NULL
)
1196 retv
= realloc(retv
,(i
+2)*sizeof(*retv
));
1201 mpd_finishCommand(mi
->connection
);
1204 mpd_unlock_conn(mi
);
1208 int mpd_misc_get_tag_by_name(char *name
)
1213 return MPD_ARGS_ERROR
;
1215 for(i
=0; i
< MPD_TAG_NUM_OF_ITEM_TYPES
; i
++)
1217 if(!strcasecmp(mpdTagItemKeys
[i
], name
))
1222 return MPD_TAG_NOT_FOUND
;
1225 int mpd_server_update_outputs(MpdObj
*mi
)
1227 mpd_OutputEntity
*output
= NULL
;
1228 if(!mpd_check_connected(mi
))
1230 debug_printf(DEBUG_WARNING
,"not connected\n");
1231 return MPD_NOT_CONNECTED
;
1233 if(mpd_lock_conn(mi
))
1235 debug_printf(DEBUG_ERROR
,"lock failed\n");
1236 return MPD_LOCK_FAILED
;
1238 mpd_sendOutputsCommand(mi
->connection
);
1239 while (( output
= mpd_getNextOutput(mi
->connection
)) != NULL
)
1242 mi
->output_states
= g_realloc(mi
->output_states
,mi
->num_outputs
*sizeof(int));
1243 mi
->output_states
[mi
->num_outputs
-1] = FALSE
;/*output->enabled;*/
1244 mpd_freeOutputElement(output
);
1246 mpd_finishCommand(mi
->connection
);
1247 return mpd_unlock_conn(mi
);
1250 int mpd_server_has_idle(MpdObj
*mi
)
1252 return mi
->has_idle
;
1255 int mpd_server_tag_supported(MpdObj
*mi
, int tag
)
1257 if(!mi
) return FALSE
;
1258 if(tag
< 0 || tag
>= MPD_TAG_NUM_OF_ITEM_TYPES
) {
1261 return mi
->supported_tags
[tag
];