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);
691 retv
= mpd_server_update_outputs(mi
);
692 /** update the supported tags */
695 char **retv
= mpd_server_get_tag_types(mi
);
697 for(i
=0;i
<MPD_TAG_ITEM_ANY
;i
++)
700 for(j
=0;retv
[j
] && strcasecmp(retv
[j
],mpdTagItemKeys
[i
]); j
++);
701 if(retv
[j
]) mi
->supported_tags
[i
] = TRUE
;
702 else mi
->supported_tags
[i
] = FALSE
;
706 /* also always true */
707 mi
->supported_tags
[MPD_TAG_ITEM_FILENAME
] = TRUE
;
708 mi
->supported_tags
[MPD_TAG_ITEM_ANY
] = TRUE
;
710 debug_printf(DEBUG_INFO
, "Propagating connection changed");
712 if(mi
->the_connection_changed_callback
!= NULL
)
714 mi
->the_connection_changed_callback( mi
, TRUE
, mi
->the_connection_changed_signal_userdata
);
720 debug_printf(DEBUG_INFO
, "Connected to mpd");
724 int mpd_check_connected(MpdObj
*mi
)
730 return mi
->connected
;
735 void mpd_signal_connect_status_changed (MpdObj
*mi
, StatusChangedCallback status_changed
, void *userdata
)
739 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
742 mi
->the_status_changed_callback
= status_changed
;
743 mi
->the_status_changed_signal_userdata
= userdata
;
747 void mpd_signal_connect_error(MpdObj
*mi
, ErrorCallback error_callback
, void *userdata
)
751 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
754 mi
->the_error_callback
= error_callback
;
755 mi
->the_error_signal_userdata
= userdata
;
758 void mpd_signal_connect_connection_changed(MpdObj
*mi
, ConnectionChangedCallback connection_changed
, void *userdata
)
762 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
765 mi
->the_connection_changed_callback
= connection_changed
;
766 mi
->the_connection_changed_signal_userdata
= userdata
;
772 MpdData
*mpd_new_data_struct(void)
775 data
= g_slice_new(MpdData_real
);
781 data
->directory
= NULL
;
782 data
->playlist
= NULL
;
783 data
->output_dev
= NULL
;
788 data
->userdata
= NULL
;
789 data
->freefunc
= NULL
;
790 return (MpdData
*)data
;
793 MpdData
*mpd_new_data_struct_append(MpdData
* data
)
795 MpdData_real
*data_real
= (MpdData_real
*)data
;
796 if(data_real
== NULL
)
798 data_real
= (MpdData_real
*)mpd_new_data_struct();
799 data_real
->first
= data_real
;
803 data_real
->next
= (MpdData_real
*)mpd_new_data_struct();
804 data_real
->next
->prev
= data_real
;
805 data_real
= data_real
->next
;
806 data_real
->next
= NULL
;
807 data_real
->first
= data_real
->prev
->first
;
809 return (MpdData
*)data_real
;
812 MpdData
* mpd_data_get_first(MpdData
const * const data
)
814 MpdData_real
const * const data_real
= (MpdData_real
const * const)data
;
815 if(data_real
!= NULL
)
817 return (MpdData
*)data_real
->first
;
823 MpdData
* mpd_data_get_next(MpdData
* const data
)
825 return mpd_data_get_next_real(data
, TRUE
);
828 MpdData
* mpd_data_get_next_real(MpdData
* const data
, int kill_list
)
830 MpdData_real
*data_real
= (MpdData_real
*)data
;
831 if (data_real
!= NULL
)
833 if (data_real
->next
!= NULL
)
835 return (MpdData
*)data_real
->next
;
839 if (kill_list
) mpd_data_free((MpdData
*)data_real
);
843 return (MpdData
*)data_real
;
846 int mpd_data_is_last(MpdData
const * const data
)
848 MpdData_real
const * const data_real
= (MpdData_real
const * const)data
;
849 if(data_real
!= NULL
)
851 if (data_real
->next
== NULL
)
859 MpdData_head *mpd_data_get_head(MpdData const * const data) {
860 return ((MpdData_real*)data)->head;
863 MpdData
* mpd_data_concatenate( MpdData
* const first
, MpdData
* const second
)
865 MpdData_real
*first_real
= (MpdData_real
*)first
;
866 MpdData_real
*second_real
= (MpdData_real
*)second
;
867 MpdData_real
*first_head
= NULL
;
869 if ( first
== NULL
) {
870 if ( second
!= NULL
)
871 return (MpdData
*)second_real
;
875 if ( second
== NULL
)
876 return (MpdData
*)first_real
;
879 first_head
= (MpdData_real
*)mpd_data_get_first(first
);
881 /* find last element in first data list */
882 while (!mpd_data_is_last((MpdData
*)first_real
)) first_real
= (MpdData_real
*)mpd_data_get_next_real((MpdData
*)first_real
, FALSE
);
883 second_real
=(MpdData_real
*) mpd_data_get_first((MpdData
*)second_real
);
885 first_real
->next
= second_real
;
886 second_real
->prev
= first_real
;
888 /* I need to set all the -> first correct */
891 second_real
->first
= first_head
;
892 second_real
= (MpdData_real
*)mpd_data_get_next_real((MpdData
*)second_real
, FALSE
);
895 return (MpdData
*)first_head
;
898 * Deletes an item from the list. It returns the next item in the list.
899 * if that is not available, it will return the last item
901 MpdData
* mpd_data_delete_item(MpdData
*data
)
903 MpdData_real
*temp
= NULL
, *data_real
= (MpdData_real
*)data
;
904 if(data_real
== NULL
) return NULL
;
905 /* if there is a next item, fix the prev pointer of the next item */
908 data_real
->next
->prev
= data_real
->prev
;
909 temp
= data_real
->next
;
911 /* if there is a previous item, fix the next pointer of the previous item */
914 /* the next item of the previous is the next item of the current */
915 data_real
->prev
->next
= data_real
->next
;
916 /* temp is the previous item */
917 temp
= data_real
->prev
;
920 /* fix first, if removed item is the first */
921 if(temp
&& temp
->first
== data_real
)
923 MpdData_real
*first
,*node
= temp
;
925 for(;node
->prev
;node
= node
->prev
);
932 /* make the removed row a valid list, so I can use the default free function to free it */
933 data_real
->next
= NULL
;
934 data_real
->prev
= NULL
;
935 data_real
->first
= data_real
;
937 mpd_data_free((MpdData
*)data_real
);
939 return (MpdData
*)temp
;
942 void mpd_data_free(MpdData
*data
)
944 MpdData_real
*data_real
,*temp
;
947 debug_printf(DEBUG_ERROR
, "data != NULL Failed");
950 data_real
= (MpdData_real
*)mpd_data_get_first(data
);
953 if (data_real
->type
== MPD_DATA_TYPE_SONG
) {
954 if(data_real
->song
) mpd_freeSong(data_real
->song
);
955 } else if (data_real
->type
== MPD_DATA_TYPE_OUTPUT_DEV
) {
956 mpd_freeOutputElement(data_real
->output_dev
);
957 } else if(data_real
->type
== MPD_DATA_TYPE_DIRECTORY
) {
958 if(data_real
->directory
)free(data_real
->directory
);
959 } else if(data_real
->type
== MPD_DATA_TYPE_PLAYLIST
) {
960 if(data_real
->playlist
) mpd_freePlaylistFile(data_real
->playlist
);
962 free((void*)(data_real
->tag
));
964 if(data_real
->freefunc
)
966 if(data_real
->userdata
)
967 data_real
->freefunc(data_real
->userdata
);
969 data_real
= data_real
->next
;
970 g_slice_free1(sizeof(*temp
), temp
);
974 /* clean this up.. make one while loop */
975 static void mpd_free_queue_ob(MpdObj
*mi
)
977 MpdQueue
*temp
= NULL
;
980 debug_printf(DEBUG_ERROR
, "mi != NULL failed");
983 if(mi
->queue
== NULL
)
985 debug_printf(DEBUG_INFO
, "mi->queue != NULL failed, nothing to clean.");
988 mi
->queue
= mi
->queue
->first
;
989 while(mi
->queue
!= NULL
)
991 temp
= mi
->queue
->next
;
993 if(mi
->queue
->path
!= NULL
)
995 free(mi
->queue
->path
);
1005 MpdQueue
*mpd_new_queue_struct()
1007 MpdQueue
* queue
= malloc(sizeof(MpdQueue
));
1017 void mpd_queue_get_next(MpdObj
*mi
)
1019 if(mi
->queue
!= NULL
&& mi
->queue
->next
!= NULL
)
1021 mi
->queue
= mi
->queue
->next
;
1023 else if(mi
->queue
->next
== NULL
)
1025 mpd_free_queue_ob(mi
);
1030 long unsigned mpd_server_get_database_update_time(MpdObj
*mi
)
1032 if(!mpd_check_connected(mi
))
1034 debug_printf(DEBUG_WARNING
,"not connected\n");
1035 return MPD_NOT_CONNECTED
;
1037 if(mpd_stats_check(mi
) != MPD_OK
)
1039 debug_printf(DEBUG_WARNING
,"Failed grabbing status\n");
1040 return MPD_STATS_FAILED
;
1042 return mi
->stats
->dbUpdateTime
;
1046 MpdData
* mpd_server_get_output_devices(MpdObj
*mi
)
1048 mpd_OutputEntity
*output
= NULL
;
1049 MpdData
*data
= NULL
;
1050 if(!mpd_check_connected(mi
))
1052 debug_printf(DEBUG_WARNING
,"not connected\n");
1055 /* TODO: Check version */
1056 if(mpd_lock_conn(mi
))
1058 debug_printf(DEBUG_ERROR
,"lock failed\n");
1062 mpd_sendOutputsCommand(mi
->connection
);
1063 while (( output
= mpd_getNextOutput(mi
->connection
)) != NULL
)
1065 data
= mpd_new_data_struct_append(data
);
1066 data
->type
= MPD_DATA_TYPE_OUTPUT_DEV
;
1067 data
->output_dev
= output
;
1069 mpd_finishCommand(mi
->connection
);
1072 mpd_unlock_conn(mi
);
1077 return mpd_data_get_first(data
);
1080 int mpd_server_set_output_device(MpdObj
*mi
,int device_id
,int state
)
1082 if(!mpd_check_connected(mi
))
1084 debug_printf(DEBUG_WARNING
,"not connected\n");
1085 return MPD_NOT_CONNECTED
;
1087 if(mpd_lock_conn(mi
))
1089 debug_printf(DEBUG_ERROR
,"lock failed\n");
1090 return MPD_LOCK_FAILED
;
1094 mpd_sendEnableOutputCommand(mi
->connection
, device_id
);
1098 mpd_sendDisableOutputCommand(mi
->connection
, device_id
);
1100 mpd_finishCommand(mi
->connection
);
1102 mpd_unlock_conn(mi
);
1103 mpd_status_queue_update(mi
);
1107 int mpd_server_check_version(MpdObj
*mi
, int major
, int minor
, int micro
)
1109 if(!mpd_check_connected(mi
))
1111 debug_printf(DEBUG_WARNING
,"not connected\n");
1114 if(major
> mi
->connection
->version
[0]) return FALSE
;
1115 if(mi
->connection
->version
[0] > major
) return TRUE
;
1116 if(minor
> mi
->connection
->version
[1]) return FALSE
;
1117 if(mi
->connection
->version
[1] > minor
) return TRUE
;
1118 if(micro
> mi
->connection
->version
[2]) return FALSE
;
1119 if(mi
->connection
->version
[2] > micro
) return TRUE
;
1123 int mpd_server_check_command_allowed(MpdObj
*mi
, const char *command
)
1126 if(!mi
|| !command
) return MPD_SERVER_COMMAND_ERROR
;
1127 /* when we are connected to a mpd server that doesn't support commands and not commands
1128 * feature. (like mpd 0.11.5) allow everything
1130 if(!mpd_server_check_version(mi
, 0,12,0)) return MPD_SERVER_COMMAND_ALLOWED
;
1132 * Also when somehow we failted to get commands
1134 if(mi
->commands
== NULL
) return MPD_SERVER_COMMAND_ALLOWED
;
1138 for(i
=0;mi
->commands
[i
].command_name
;i
++)
1140 if(!strcasecmp(mi
->commands
[i
].command_name
, command
))
1141 return mi
->commands
[i
].enabled
;
1143 return MPD_SERVER_COMMAND_NOT_SUPPORTED
;
1146 char ** mpd_server_get_url_handlers(MpdObj
*mi
)
1151 if(!mpd_check_connected(mi
))
1153 debug_printf(DEBUG_WARNING
,"not connected\n");
1156 if(mpd_lock_conn(mi
))
1158 debug_printf(DEBUG_ERROR
,"lock failed\n");
1161 mpd_sendUrlHandlersCommand(mi
->connection
);
1162 while((temp
= mpd_getNextHandler(mi
->connection
)) != NULL
)
1164 retv
= realloc(retv
,(i
+2)*sizeof(*retv
));
1169 mpd_finishCommand(mi
->connection
);
1172 mpd_unlock_conn(mi
);
1175 char ** mpd_server_get_tag_types(MpdObj
*mi
)
1180 if(!mpd_check_connected(mi
))
1182 debug_printf(DEBUG_WARNING
,"not connected\n");
1185 if(mpd_lock_conn(mi
))
1187 debug_printf(DEBUG_ERROR
,"lock failed\n");
1190 mpd_sendTagTypesCommand(mi
->connection
);
1191 while((temp
= mpd_getNextTagType(mi
->connection
)) != NULL
)
1193 retv
= realloc(retv
,(i
+2)*sizeof(*retv
));
1198 mpd_finishCommand(mi
->connection
);
1201 mpd_unlock_conn(mi
);
1205 int mpd_misc_get_tag_by_name(char *name
)
1210 return MPD_ARGS_ERROR
;
1212 for(i
=0; i
< MPD_TAG_NUM_OF_ITEM_TYPES
; i
++)
1214 if(!strcasecmp(mpdTagItemKeys
[i
], name
))
1219 return MPD_TAG_NOT_FOUND
;
1222 int mpd_server_update_outputs(MpdObj
*mi
)
1224 mpd_OutputEntity
*output
= NULL
;
1225 if(!mpd_check_connected(mi
))
1227 debug_printf(DEBUG_WARNING
,"not connected\n");
1228 return MPD_NOT_CONNECTED
;
1230 if(mpd_lock_conn(mi
))
1232 debug_printf(DEBUG_ERROR
,"lock failed\n");
1233 return MPD_LOCK_FAILED
;
1235 mpd_sendOutputsCommand(mi
->connection
);
1236 while (( output
= mpd_getNextOutput(mi
->connection
)) != NULL
)
1239 mi
->output_states
= g_realloc(mi
->output_states
,mi
->num_outputs
*sizeof(int));
1240 mi
->output_states
[mi
->num_outputs
-1] = FALSE
;/*output->enabled;*/
1241 mpd_freeOutputElement(output
);
1243 mpd_finishCommand(mi
->connection
);
1244 return mpd_unlock_conn(mi
);
1247 int mpd_server_has_idle(MpdObj
*mi
)
1249 return mi
->has_idle
;
1252 int mpd_server_tag_supported(MpdObj
*mi
, int tag
)
1254 if(!mi
) return FALSE
;
1255 if(tag
< 0 || tag
>= MPD_TAG_NUM_OF_ITEM_TYPES
) {
1258 return mi
->supported_tags
[tag
];