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
->nextsongpos
= -1;
74 state
->nextsongid
= -1;
75 state
->dbUpdateTime
= 0;
76 state
->updatingDb
= 0;
82 state
->elapsedtime
= 0;
84 state
->samplerate
= 0;
89 state
->playlistLength
= 0;
90 state
->error
[0] = '\0';
95 static MpdObj
* mpd_create()
97 MpdObj
* mi
= malloc(sizeof(*mi
));
100 /* should never happen on linux */
105 /* set default values */
106 /* we start not connected */
107 mi
->connected
= FALSE
;
108 /* port 6600 is the default mpd port */
111 mi
->hostname
= strdup("localhost");
114 /* 1 second timeout */
115 mi
->connection_timeout
= 1.0;
116 /* we have no connection pointer */
117 mi
->connection
= NULL
;
123 mi
->error_mpd_code
= 0;
124 mi
->error_msg
= NULL
;
125 mi
->CurrentSong
= NULL
;
127 mpd_init_MpdServerState(&(mi
->CurrentState
));
128 mpd_init_MpdServerState(&(mi
->OldState
));
131 * Set signals to NULL
133 /* connection changed signal */
134 mi
->the_connection_changed_callback
= NULL
;
135 mi
->the_connection_changed_signal_userdata
= NULL
;
138 mi
->the_status_changed_callback
= NULL
;
139 mi
->the_status_changed_signal_userdata
= NULL
;
142 mi
->the_error_callback
= NULL
;
143 mi
->the_error_signal_userdata
= NULL
;
144 /* connection is locked because where not connected */
145 mi
->connection_lock
= TRUE
;
147 /* set queue to NULL */
150 mi
->search_type
= MPD_SEARCH_TYPE_NONE
;
151 /* no need to initialize, but set it to anything anyway*/
152 mi
->search_field
= MPD_TAG_ITEM_ARTIST
;
156 mi
->output_states
= NULL
;
158 mi
->url_handlers
= NULL
;
160 memset((mi
->supported_tags
), 0,sizeof(mi
->supported_tags
));
168 void mpd_free(MpdObj
*mi
)
170 debug_printf(DEBUG_INFO
, "destroying MpdObj object\n");
174 debug_printf(DEBUG_WARNING
, "Connection still running, disconnecting\n");
192 mpd_closeConnection(mi
->connection
);
196 mpd_freeStatus(mi
->status
);
200 mpd_freeStats(mi
->stats
);
204 mpd_freeSong(mi
->CurrentSong
);
208 g_strfreev(mi
->url_handlers
);
209 mi
->url_handlers
= NULL
;
211 mpd_free_queue_ob(mi
);
212 mpd_server_free_commands(mi
);
216 int mpd_check_error(MpdObj
*mi
)
220 debug_printf(DEBUG_ERROR
, "mi == NULL?");
221 return MPD_ARGS_ERROR
;
224 /* this shouldn't happen, ever */
225 if(mi
->connection
== NULL
)
227 debug_printf(DEBUG_ERROR
, "mi->connection == NULL?");
228 return MPD_FATAL_ERROR
;
231 /* TODO: map these errors in the future */
232 mi
->error
= mi
->connection
->error
;
233 mi
->error_mpd_code
= mi
->connection
->errorCode
;
234 /*TODO: do I need to strdup this? */
235 if(!g_utf8_validate(mi
->connection
->errorStr
, -1, NULL
)){
236 mi
->error_msg
= g_locale_to_utf8(mi
->connection
->errorStr
, -1, NULL
, NULL
,NULL
);
238 mi
->error_msg
= g_strdup(mi
->connection
->errorStr
);
241 if(mi
->error_msg
== NULL
) mi
->error_msg
= g_strdup("Failed to convert error message to utf-8");
243 /* Check for permission */
244 /* First check for an error reported by MPD
245 * Then check what type of error mpd reported
247 if(mi
->error
== MPD_ERROR_ACK
)
250 debug_printf(DEBUG_ERROR
,"clearing errors in mpd_Connection: %i-%s", mi
->connection
->errorCode
, mi
->connection
->errorStr
);
251 mpd_clearError(mi
->connection
);
252 if (mi
->the_error_callback
)
254 debug_printf(DEBUG_ERROR
, "Error callback 1 (ACK)");
255 if(mi
->the_error_callback(mi
, mi
->error_mpd_code
, mi
->error_msg
, mi
->the_error_signal_userdata
))
257 debug_printf(DEBUG_ERROR
, "Error callback told me to disconnect");
260 mi
->error_msg
= NULL
;
262 return MPD_SERVER_ERROR
;
266 mi
->error_msg
= NULL
;
272 debug_printf(DEBUG_ERROR
, "Following error occurred: %i: code: %i msg: %s", mi
->error
,mi
->connection
->errorCode
, mi
->error_msg
);
274 if (mi
->the_error_callback
)
276 debug_printf(DEBUG_ERROR
, "Error callback 2");
277 mi
->the_error_callback(mi
, mi
->error
, mi
->error_msg
, mi
->the_error_signal_userdata
);
281 mi
->error_msg
= NULL
;
283 return MPD_SERVER_ERROR
;
286 mi
->error_msg
= NULL
;
292 int mpd_lock_conn(MpdObj
*mi
)
295 if(mi
->connection_lock
)
297 debug_printf(DEBUG_WARNING
, "Failed to lock connection, already locked\n");
298 return MPD_LOCK_FAILED
;
300 mi
->connection_lock
= TRUE
;
304 int mpd_unlock_conn(MpdObj
*mi
)
306 if(!mi
->connection_lock
)
308 debug_printf(DEBUG_ERROR
, "Failed to unlock connection, already unlocked\n");
309 return MPD_LOCK_FAILED
;
312 mi
->connection_lock
= FALSE
;
314 return mpd_check_error(mi
);
317 MpdObj
* mpd_new_default()
319 debug_printf(DEBUG_INFO
, "creating a new mpdInt object\n");
323 MpdObj
*mpd_new(char *hostname
, int port
, char *password
)
325 MpdObj
*mi
= mpd_create();
332 mpd_set_hostname(mi
, hostname
);
336 mpd_set_port(mi
, port
);
340 mpd_set_password(mi
, password
);
346 const char * mpd_get_hostname(MpdObj
*mi
)
355 int mpd_set_hostname(MpdObj
*mi
, char *hostname
)
359 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
360 return MPD_ARGS_ERROR
;
363 if(mi
->hostname
!= NULL
)
367 /* possible location todo some post processing of hostname */
368 mi
->hostname
= strdup(hostname
);
372 int mpd_set_password(MpdObj
*mi
, const char *password
)
376 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
377 return MPD_ARGS_ERROR
;
380 if(mi
->password
!= NULL
)
384 /* possible location todo some post processing of password */
385 mi
->password
= strdup(password
);
390 int mpd_send_password(MpdObj
*mi
)
392 if(!mi
) return MPD_ARGS_ERROR
;
393 if(mi
->password
&& mpd_check_connected(mi
) && strlen(mi
->password
))
395 if(mpd_lock_conn(mi
))
397 debug_printf(DEBUG_WARNING
, "failed to lock connection");
398 return MPD_LOCK_FAILED
;
400 mpd_sendPasswordCommand(mi
->connection
, mi
->password
);
401 mpd_finishCommand(mi
->connection
);
402 if(mpd_unlock_conn(mi
))
404 debug_printf(DEBUG_ERROR
, "Failed to unlock connection\n");
405 return MPD_LOCK_FAILED
;
407 mpd_server_get_allowed_commands(mi
);
408 /*TODO: should I do it here, or in the
409 * mpd_server_get_allowed_command, so it also get's executed on
412 if((mi
->the_status_changed_callback
!= NULL
))
414 /** update the supported tags */
417 char **retv
= mpd_server_get_tag_types(mi
);
419 for(i
=0;i
<MPD_TAG_ITEM_ANY
;i
++)
422 for(j
=0;retv
[j
] && strcasecmp(retv
[j
],mpdTagItemKeys
[i
]); j
++);
423 if(retv
[j
]) mi
->supported_tags
[i
] = TRUE
;
424 else mi
->supported_tags
[i
] = FALSE
;
428 /* also always true */
429 mi
->supported_tags
[MPD_TAG_ITEM_FILENAME
] = TRUE
;
430 mi
->supported_tags
[MPD_TAG_ITEM_ANY
] = TRUE
;
432 /* If permission updates, we should also call an output update, The data might be available now. */
433 mi
->the_status_changed_callback( mi
,
434 MPD_CST_PERMISSION
|MPD_CST_OUTPUT
, mi
->the_status_changed_signal_userdata
);
440 int mpd_set_port(MpdObj
*mi
, int port
)
444 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
445 return MPD_ARGS_ERROR
;
451 int mpd_set_connection_timeout(MpdObj
*mi
, float timeout
)
455 debug_printf(DEBUG_ERROR
, "mi == NULL\n");
456 return MPD_ARGS_ERROR
;
458 mi
->connection_timeout
= timeout
;
459 if(mpd_check_connected(mi
))
461 /*TODO: set timeout */
462 if(mpd_lock_conn(mi
))
464 debug_printf(DEBUG_ERROR
,"lock failed\n");
465 return MPD_LOCK_FAILED
;
467 mpd_setConnectionTimeout(mi
->connection
, timeout
);
468 mpd_finishCommand(mi
->connection
);
476 static void mpd_server_free_commands(MpdObj
*mi
)
481 while(mi
->commands
[i
].command_name
)
483 free(mi
->commands
[i
].command_name
);
491 char *mpd_server_get_version(MpdObj
*mi
)
494 if(!mi
|| !mpd_check_connected(mi
))
496 retval
= malloc(10*sizeof(char));
497 snprintf(retval
,10,"%i.%i.%i", mi
->connection
->version
[0], mi
->connection
->version
[1], mi
->connection
->version
[2]);
498 /* always make sure the string is terminated */
503 int mpd_server_get_allowed_commands(MpdObj
*mi
)
506 int num_commands
= 0;
508 debug_printf(DEBUG_ERROR
, "mi != NULL failed\n");
509 return MPD_ARGS_ERROR
;
511 if(!mpd_check_connected(mi
)) {
512 debug_printf(DEBUG_WARNING
, "Not Connected");
513 return MPD_NOT_CONNECTED
;
515 if(!mpd_server_check_version(mi
,0,12,0)){
516 debug_printf(DEBUG_INFO
, "Not supported by mpd");
517 return MPD_SERVER_NOT_SUPPORTED
;
520 mpd_server_free_commands(mi
);
522 if(mpd_lock_conn(mi
))
524 debug_printf(DEBUG_ERROR
, "lock failed");
525 return MPD_LOCK_FAILED
;
527 mpd_sendCommandsCommand(mi
->connection
);
528 while((temp
= mpd_getNextCommand(mi
->connection
)))
531 mi
->commands
= realloc(mi
->commands
, (num_commands
+1)*sizeof(MpdCommand
));
532 mi
->commands
[num_commands
-1].command_name
= temp
;
533 mi
->commands
[num_commands
-1].enabled
= TRUE
;
534 mi
->commands
[num_commands
].command_name
= NULL
;
535 mi
->commands
[num_commands
].enabled
= FALSE
;
536 if(strcmp(mi
->commands
[num_commands
-1].command_name
, "idle") == 0) {
540 mpd_finishCommand(mi
->connection
);
541 mpd_sendNotCommandsCommand(mi
->connection
);
542 while((temp
= mpd_getNextCommand(mi
->connection
)))
545 mi
->commands
= realloc(mi
->commands
, (num_commands
+1)*sizeof(MpdCommand
));
546 mi
->commands
[num_commands
-1].command_name
= temp
;
547 mi
->commands
[num_commands
-1].enabled
= FALSE
;
548 mi
->commands
[num_commands
].command_name
= NULL
;
549 mi
->commands
[num_commands
].enabled
= FALSE
;
551 mpd_finishCommand(mi
->connection
);
553 if(mpd_unlock_conn(mi
))
555 return MPD_LOCK_FAILED
;
562 int mpd_disconnect(MpdObj
*mi
)
567 debug_printf(DEBUG_INFO
, "disconnecting\n");
571 mpd_closeConnection(mi
->connection
);
572 mi
->connection
= NULL
;
576 mpd_freeStatus(mi
->status
);
581 mpd_freeStats(mi
->stats
);
586 mpd_freeSong(mi
->CurrentSong
);
587 mi
->CurrentSong
= NULL
;
591 g_strfreev(mi
->url_handlers
);
592 mi
->url_handlers
= NULL
;
594 mi
->CurrentState
.playlistid
= -1;
595 mi
->CurrentState
.storedplaylistid
= -1;
596 mi
->CurrentState
.state
= -1;
597 mi
->CurrentState
.songid
= -1;
598 mi
->CurrentState
.songpos
= -1;
599 mi
->CurrentState
.nextsongid
= -1;
600 mi
->CurrentState
.nextsongpos
= -1;
601 mi
->CurrentState
.dbUpdateTime
= 0;
602 mi
->CurrentState
.updatingDb
= 0;
603 mi
->CurrentState
.repeat
= -1;
604 mi
->CurrentState
.random
= -1;
605 mi
->CurrentState
.volume
= -2;
606 mi
->CurrentState
.xfade
= -1;
607 mi
->CurrentState
.totaltime
= 0;
608 mi
->CurrentState
.elapsedtime
= 0;
609 mi
->CurrentState
.bitrate
= 0;
610 mi
->CurrentState
.samplerate
= 0;
611 mi
->CurrentState
.channels
= 0;
612 mi
->CurrentState
.bits
= 0;
613 mi
->CurrentState
.playlistLength
= 0;
614 mi
->CurrentState
.error
[0] = '\0';
616 mi
->search_type
= MPD_SEARCH_TYPE_NONE
;
617 /* no need to initialize, but set it to anything anyway*/
618 mi
->search_field
= MPD_TAG_ITEM_ARTIST
;
621 if(mi
->output_states
)
622 g_free(mi
->output_states
);
623 mi
->output_states
= NULL
;
626 memset((mi
->supported_tags
), 0,sizeof(mi
->supported_tags
));
630 memcpy(&(mi
->OldState
), &(mi
->CurrentState
) , sizeof(MpdServerState
));
632 mpd_free_queue_ob(mi
);
633 mpd_server_free_commands(mi
);
634 /*don't reset errors */
635 /* Remove this signal, we don't actually disconnect */
638 /* set disconnect flag */
639 mi
->connected
= FALSE
;
641 if(mi
->the_connection_changed_callback
!= NULL
)
643 mi
->the_connection_changed_callback( mi
, FALSE
, mi
->the_connection_changed_signal_userdata
);
646 debug_printf(DEBUG_INFO
, "Disconnect completed\n");
649 int mpd_connect(MpdObj
*mi
)
651 return mpd_connect_real(mi
,NULL
);
653 int mpd_connect_real(MpdObj
*mi
,mpd_Connection
*connection
)
658 /* should return some spiffy error here */
659 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
660 return MPD_ARGS_ERROR
;
664 mi
->error_mpd_code
= 0;
665 if(mi
->error_msg
!= NULL
)
669 mi
->error_msg
= NULL
;
671 debug_printf(DEBUG_INFO
, "connecting\n");
672 mpd_init_MpdServerState(&(mi
->CurrentState
));
674 memcpy(&(mi
->OldState
), &(mi
->CurrentState
), sizeof(MpdServerState
));
682 if(mi
->hostname
== NULL
)
684 mpd_set_hostname(mi
, "localhost");
686 /* make sure this is locked */
687 if(!mi
->connection_lock
)
692 mi
->connection
= connection
;
694 /* make timeout configurable */
695 mi
->connection
= mpd_newConnection(mi
->hostname
,mi
->port
,mi
->connection_timeout
);
697 if(mi
->connection
== NULL
)
699 /* TODO: make seperate error message? */
700 return MPD_NOT_CONNECTED
;
702 if(mpd_check_error(mi
) != MPD_OK
)
704 /* TODO: make seperate error message? */
705 return MPD_NOT_CONNECTED
;
708 /* set connected state */
709 mi
->connected
= TRUE
;
710 if(mpd_unlock_conn(mi
))
712 return MPD_LOCK_FAILED
;
715 /* get the commands we are allowed to use */
716 retv
= mpd_server_get_allowed_commands(mi
);
721 if(mi
->password
&& strlen(mi
->password
) > 0)
723 mpd_send_password(mi
);
728 char **retv
= mpd_server_get_tag_types(mi
);
730 for(i
=0;i
<MPD_TAG_ITEM_ANY
;i
++)
733 for(j
=0;retv
[j
] && strcasecmp(retv
[j
],mpdTagItemKeys
[i
]); j
++);
734 if(retv
[j
]) mi
->supported_tags
[i
] = TRUE
;
735 else mi
->supported_tags
[i
] = FALSE
;
739 /* also always true */
740 mi
->supported_tags
[MPD_TAG_ITEM_FILENAME
] = TRUE
;
741 mi
->supported_tags
[MPD_TAG_ITEM_ANY
] = TRUE
;
745 retv = mpd_server_update_outputs(mi);
750 retv
= mpd_server_update_outputs(mi
);
751 /** update the supported tags */
752 debug_printf(DEBUG_INFO
, "Propagating connection changed");
754 if(mi
->the_connection_changed_callback
!= NULL
)
756 mi
->the_connection_changed_callback( mi
, TRUE
, mi
->the_connection_changed_signal_userdata
);
762 debug_printf(DEBUG_INFO
, "Connected to mpd");
766 int mpd_check_connected(MpdObj
*mi
)
772 return mi
->connected
;
777 void mpd_signal_connect_status_changed (MpdObj
*mi
, StatusChangedCallback status_changed
, void *userdata
)
781 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
784 mi
->the_status_changed_callback
= status_changed
;
785 mi
->the_status_changed_signal_userdata
= userdata
;
789 void mpd_signal_connect_error(MpdObj
*mi
, ErrorCallback error_callback
, void *userdata
)
793 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
796 mi
->the_error_callback
= error_callback
;
797 mi
->the_error_signal_userdata
= userdata
;
800 void mpd_signal_connect_connection_changed(MpdObj
*mi
, ConnectionChangedCallback connection_changed
, void *userdata
)
804 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
807 mi
->the_connection_changed_callback
= connection_changed
;
808 mi
->the_connection_changed_signal_userdata
= userdata
;
814 MpdData
*mpd_new_data_struct(void)
817 data
= g_slice_new(MpdData_real
);
823 data
->directory
= NULL
;
824 data
->playlist
= NULL
;
825 data
->output_dev
= NULL
;
830 data
->userdata
= NULL
;
831 data
->freefunc
= NULL
;
832 return (MpdData
*)data
;
835 MpdData
*mpd_new_data_struct_append(MpdData
* data
)
837 MpdData_real
*data_real
= (MpdData_real
*)data
;
838 if(data_real
== NULL
)
840 data_real
= (MpdData_real
*)mpd_new_data_struct();
841 data_real
->first
= data_real
;
845 data_real
->next
= (MpdData_real
*)mpd_new_data_struct();
846 data_real
->next
->prev
= data_real
;
847 data_real
= data_real
->next
;
848 data_real
->next
= NULL
;
849 data_real
->first
= data_real
->prev
->first
;
851 return (MpdData
*)data_real
;
854 MpdData
* mpd_data_get_first(MpdData
const * const data
)
856 MpdData_real
const * const data_real
= (MpdData_real
const * const)data
;
857 if(data_real
!= NULL
)
859 return (MpdData
*)data_real
->first
;
865 MpdData
* mpd_data_get_next(MpdData
* const data
)
867 return mpd_data_get_next_real(data
, TRUE
);
870 MpdData
* mpd_data_get_next_real(MpdData
* const data
, int kill_list
)
872 MpdData_real
*data_real
= (MpdData_real
*)data
;
873 if (data_real
!= NULL
)
875 if (data_real
->next
!= NULL
)
877 return (MpdData
*)data_real
->next
;
881 if (kill_list
) mpd_data_free((MpdData
*)data_real
);
885 return (MpdData
*)data_real
;
888 int mpd_data_is_last(MpdData
const * const data
)
890 MpdData_real
const * const data_real
= (MpdData_real
const * const)data
;
891 if(data_real
!= NULL
)
893 if (data_real
->next
== NULL
)
901 MpdData_head *mpd_data_get_head(MpdData const * const data) {
902 return ((MpdData_real*)data)->head;
905 MpdData
* mpd_data_concatenate( MpdData
* const first
, MpdData
* const second
)
907 MpdData_real
*first_real
= (MpdData_real
*)first
;
908 MpdData_real
*second_real
= (MpdData_real
*)second
;
909 MpdData_real
*first_head
= NULL
;
911 if ( first
== NULL
) {
912 if ( second
!= NULL
)
913 return (MpdData
*)second_real
;
917 if ( second
== NULL
)
918 return (MpdData
*)first_real
;
921 first_head
= (MpdData_real
*)mpd_data_get_first(first
);
923 /* find last element in first data list */
924 while (!mpd_data_is_last((MpdData
*)first_real
)) first_real
= (MpdData_real
*)mpd_data_get_next_real((MpdData
*)first_real
, FALSE
);
925 second_real
=(MpdData_real
*) mpd_data_get_first((MpdData
*)second_real
);
927 first_real
->next
= second_real
;
928 second_real
->prev
= first_real
;
930 /* I need to set all the -> first correct */
933 second_real
->first
= first_head
;
934 second_real
= (MpdData_real
*)mpd_data_get_next_real((MpdData
*)second_real
, FALSE
);
937 return (MpdData
*)first_head
;
940 * Deletes an item from the list. It returns the next item in the list.
941 * if that is not available, it will return the last item
943 MpdData
* mpd_data_delete_item(MpdData
*data
)
945 MpdData_real
*temp
= NULL
, *data_real
= (MpdData_real
*)data
;
946 if(data_real
== NULL
) return NULL
;
947 /* if there is a next item, fix the prev pointer of the next item */
950 data_real
->next
->prev
= data_real
->prev
;
951 temp
= data_real
->next
;
953 /* if there is a previous item, fix the next pointer of the previous item */
956 /* the next item of the previous is the next item of the current */
957 data_real
->prev
->next
= data_real
->next
;
958 /* temp is the previous item */
959 temp
= data_real
->prev
;
962 /* fix first, if removed item is the first */
963 if(temp
&& temp
->first
== data_real
)
965 MpdData_real
*first
,*node
= temp
;
967 for(;node
->prev
;node
= node
->prev
);
974 /* make the removed row a valid list, so I can use the default free function to free it */
975 data_real
->next
= NULL
;
976 data_real
->prev
= NULL
;
977 data_real
->first
= data_real
;
979 mpd_data_free((MpdData
*)data_real
);
981 return (MpdData
*)temp
;
984 void mpd_data_free(MpdData
*data
)
986 MpdData_real
*data_real
,*temp
;
989 debug_printf(DEBUG_ERROR
, "data != NULL Failed");
992 data_real
= (MpdData_real
*)mpd_data_get_first(data
);
995 if (data_real
->type
== MPD_DATA_TYPE_SONG
) {
996 if(data_real
->song
) mpd_freeSong(data_real
->song
);
997 } else if (data_real
->type
== MPD_DATA_TYPE_OUTPUT_DEV
) {
998 mpd_freeOutputElement(data_real
->output_dev
);
999 } else if(data_real
->type
== MPD_DATA_TYPE_DIRECTORY
) {
1000 if(data_real
->directory
)free(data_real
->directory
);
1001 } else if(data_real
->type
== MPD_DATA_TYPE_PLAYLIST
) {
1002 if(data_real
->playlist
) mpd_freePlaylistFile(data_real
->playlist
);
1004 free((void*)(data_real
->tag
));
1006 if(data_real
->freefunc
)
1008 if(data_real
->userdata
)
1009 data_real
->freefunc(data_real
->userdata
);
1011 data_real
= data_real
->next
;
1012 g_slice_free1(sizeof(*temp
), temp
);
1016 /* clean this up.. make one while loop */
1017 static void mpd_free_queue_ob(MpdObj
*mi
)
1019 MpdQueue
*temp
= NULL
;
1022 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
1025 if(mi
->queue
== NULL
)
1027 debug_printf(DEBUG_INFO
, "mi->queue != NULL failed, nothing to clean.");
1030 mi
->queue
= mi
->queue
->first
;
1031 while(mi
->queue
!= NULL
)
1033 temp
= mi
->queue
->next
;
1035 if(mi
->queue
->path
!= NULL
)
1037 free(mi
->queue
->path
);
1047 MpdQueue
*mpd_new_queue_struct()
1049 MpdQueue
* queue
= malloc(sizeof(MpdQueue
));
1059 void mpd_queue_get_next(MpdObj
*mi
)
1061 if(mi
->queue
!= NULL
&& mi
->queue
->next
!= NULL
)
1063 mi
->queue
= mi
->queue
->next
;
1065 else if(mi
->queue
->next
== NULL
)
1067 mpd_free_queue_ob(mi
);
1072 long unsigned mpd_server_get_database_update_time(MpdObj
*mi
)
1074 if(!mpd_check_connected(mi
))
1076 debug_printf(DEBUG_WARNING
,"not connected\n");
1077 return MPD_NOT_CONNECTED
;
1079 if(mpd_stats_check(mi
) != MPD_OK
)
1081 debug_printf(DEBUG_WARNING
,"Failed grabbing status\n");
1082 return MPD_STATS_FAILED
;
1084 return mi
->stats
->dbUpdateTime
;
1088 MpdData
* mpd_server_get_output_devices(MpdObj
*mi
)
1090 mpd_OutputEntity
*output
= NULL
;
1091 MpdData
*data
= NULL
;
1092 if(!mpd_check_connected(mi
))
1094 debug_printf(DEBUG_WARNING
,"not connected\n");
1097 /* TODO: Check version */
1098 if(mpd_lock_conn(mi
))
1100 debug_printf(DEBUG_ERROR
,"lock failed\n");
1104 mpd_sendOutputsCommand(mi
->connection
);
1105 while (( output
= mpd_getNextOutput(mi
->connection
)) != NULL
)
1107 data
= mpd_new_data_struct_append(data
);
1108 data
->type
= MPD_DATA_TYPE_OUTPUT_DEV
;
1109 data
->output_dev
= output
;
1111 mpd_finishCommand(mi
->connection
);
1114 if(mpd_unlock_conn(mi
) != MPD_OK
)
1116 if(data
)mpd_data_free(data
);
1123 return mpd_data_get_first(data
);
1126 int mpd_server_set_output_device(MpdObj
*mi
,int device_id
,int state
)
1128 if(!mpd_check_connected(mi
))
1130 debug_printf(DEBUG_WARNING
,"not connected\n");
1131 return MPD_NOT_CONNECTED
;
1133 if(mpd_lock_conn(mi
))
1135 debug_printf(DEBUG_ERROR
,"lock failed\n");
1136 return MPD_LOCK_FAILED
;
1140 mpd_sendEnableOutputCommand(mi
->connection
, device_id
);
1144 mpd_sendDisableOutputCommand(mi
->connection
, device_id
);
1146 mpd_finishCommand(mi
->connection
);
1148 mpd_unlock_conn(mi
);
1149 mpd_status_queue_update(mi
);
1153 int mpd_server_check_version(MpdObj
*mi
, int major
, int minor
, int micro
)
1155 if(!mpd_check_connected(mi
))
1157 debug_printf(DEBUG_WARNING
,"not connected\n");
1160 if(major
> mi
->connection
->version
[0]) return FALSE
;
1161 if(mi
->connection
->version
[0] > major
) return TRUE
;
1162 if(minor
> mi
->connection
->version
[1]) return FALSE
;
1163 if(mi
->connection
->version
[1] > minor
) return TRUE
;
1164 if(micro
> mi
->connection
->version
[2]) return FALSE
;
1165 if(mi
->connection
->version
[2] > micro
) return TRUE
;
1169 int mpd_server_check_command_allowed(MpdObj
*mi
, const char *command
)
1172 if(!mi
|| !command
) return MPD_SERVER_COMMAND_ERROR
;
1173 /* when we are connected to a mpd server that doesn't support commands and not commands
1174 * feature. (like mpd 0.11.5) allow everything
1176 if(!mpd_server_check_version(mi
, 0,12,0)) return MPD_SERVER_COMMAND_ALLOWED
;
1178 * Also when somehow we failted to get commands
1180 if(mi
->commands
== NULL
) return MPD_SERVER_COMMAND_ALLOWED
;
1184 for(i
=0;mi
->commands
[i
].command_name
;i
++)
1186 if(!strcasecmp(mi
->commands
[i
].command_name
, command
))
1187 return mi
->commands
[i
].enabled
;
1189 return MPD_SERVER_COMMAND_NOT_SUPPORTED
;
1192 char ** mpd_server_get_url_handlers(MpdObj
*mi
)
1197 if(!mpd_check_connected(mi
))
1199 debug_printf(DEBUG_WARNING
,"not connected\n");
1202 if(mi
->url_handlers
){
1203 return g_strdupv(mi
->url_handlers
);
1205 if(mpd_lock_conn(mi
))
1207 debug_printf(DEBUG_ERROR
,"lock failed\n");
1211 * Fetch url handlers and store them
1213 mpd_sendUrlHandlersCommand(mi
->connection
);
1214 while((temp
= mpd_getNextHandler(mi
->connection
)) != NULL
)
1216 mi
->url_handlers
= realloc(mi
->url_handlers
,(i
+2)*sizeof(*mi
->url_handlers
));
1217 mi
->url_handlers
[i
] = temp
;
1218 mi
->url_handlers
[i
+1] = NULL
;
1221 mpd_finishCommand(mi
->connection
);
1224 mpd_unlock_conn(mi
);
1226 return g_strdupv(mi
->url_handlers
);
1228 char ** mpd_server_get_tag_types(MpdObj
*mi
)
1233 if(!mpd_check_connected(mi
))
1235 debug_printf(DEBUG_WARNING
,"not connected\n");
1238 if(mpd_lock_conn(mi
))
1240 debug_printf(DEBUG_ERROR
,"lock failed\n");
1243 mpd_sendTagTypesCommand(mi
->connection
);
1244 while((temp
= mpd_getNextTagType(mi
->connection
)) != NULL
)
1246 retv
= realloc(retv
,(i
+2)*sizeof(*retv
));
1251 mpd_finishCommand(mi
->connection
);
1254 mpd_unlock_conn(mi
);
1258 int mpd_misc_get_tag_by_name(char *name
)
1263 return MPD_ARGS_ERROR
;
1265 for(i
=0; i
< MPD_TAG_NUM_OF_ITEM_TYPES
; i
++)
1267 if(!strcasecmp(mpdTagItemKeys
[i
], name
))
1272 return MPD_TAG_NOT_FOUND
;
1275 int mpd_server_update_outputs(MpdObj
*mi
)
1277 mpd_OutputEntity
*output
= NULL
;
1278 if(!mpd_check_connected(mi
))
1280 debug_printf(DEBUG_WARNING
,"not connected\n");
1281 return MPD_NOT_CONNECTED
;
1283 if(mpd_lock_conn(mi
))
1285 debug_printf(DEBUG_ERROR
,"lock failed\n");
1286 return MPD_LOCK_FAILED
;
1288 mpd_sendOutputsCommand(mi
->connection
);
1289 while (( output
= mpd_getNextOutput(mi
->connection
)) != NULL
)
1292 mi
->output_states
= realloc(mi
->output_states
,mi
->num_outputs
*sizeof(int));
1293 mi
->output_states
[mi
->num_outputs
-1] = FALSE
;/*output->enabled;*/
1294 mpd_freeOutputElement(output
);
1296 mpd_finishCommand(mi
->connection
);
1297 return mpd_unlock_conn(mi
);
1300 int mpd_server_has_idle(MpdObj
*mi
)
1302 return mi
->has_idle
;
1305 int mpd_server_tag_supported(MpdObj
*mi
, int tag
)
1307 if(!mi
) return FALSE
;
1308 if(tag
< 0 || tag
>= MPD_TAG_NUM_OF_ITEM_TYPES
) {
1311 return mi
->supported_tags
[tag
];