int cmd_crop(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn) { struct mpd_status *status = getStatus( conn ); int length = mpd_status_get_queue_length(status) - 1; if (length < 0) { mpd_status_free(status); DIE( "A playlist longer than 1 song in length is required to crop.\n" ); } else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); while( length >= 0 ) { if (length != mpd_status_get_song_pos(status)) { mpd_send_delete(conn, length); } length--; } mpd_status_free(status); if (!mpd_command_list_end(conn) || !mpd_response_finish(conn)) printErrorAndExit(conn); return ( 0 ); } else { mpd_status_free(status); DIE( "You need to be playing to crop the playlist\n" ); } }
/* * Functions: selectRunMode * Let user to select the program mode. Return 1 is the user chose * "run mode". */ int selectRunMode(){ printf("There are two modes to select, please pick one.\n"); printf("[1] Run the script in the code.\n"); printf("[2] Run the interactive mode to control smiley.\n"); printf(">> "); int select=0; if(scanf("%d",&select)!=1){ printErrorAndExit(INVALID_INPUT); } return (select==1)?1:0; }
int cmd_load ( int argc, char ** argv, struct mpd_connection *conn ) { if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); for (int i = 0; i < argc; ++i) { printf("loading: %s\n",argv[i]); mpd_send_load(conn, charset_to_utf8(argv[i])); } mpd_command_list_end(conn); my_finishCommand(conn); return 0; }
int cmd_current(gcc_unused int argc, gcc_unused char **argv, struct mpd_connection *conn) { if (options.wait) wait_current(conn); if (!mpd_command_list_begin(conn, true) || !mpd_send_status(conn) || !mpd_send_current_song(conn) || !mpd_command_list_end(conn)) printErrorAndExit(conn); struct mpd_status *status = mpd_recv_status(conn); if (status == NULL) printErrorAndExit(conn); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_response_next(conn)) printErrorAndExit(conn); struct mpd_song *song = mpd_recv_song(conn); if (song != NULL) { pretty_print_song(song); printf("\n"); mpd_song_free(song); } if (!mpd_response_finish(conn)) printErrorAndExit(conn); } mpd_status_free(status); return 0; }
void addUserToEndOfList(User **headPtr,const char *inputName, double inBalance) { //create struct. User *UserList; UserList = malloc(sizeof(User)); if (UserList == NULL) printErrorAndExit("malloc in addToEndOfList error"); //insert name. size_t inputName_length = strlen(inputName); UserList->name = malloc(inputName_length + 1); //+1 allow for null-space character if (UserList->name == NULL) printErrorAndExit("mylist->name malloc error"); strncpy(UserList->name, inputName, inputName_length); UserList->name[inputName_length] = '\0'; //ensure null char is at the end of the string. UserList->balance = inBalance; //next node does not exist. UserList->next = NULL; //case 1: Linked list is empty. Create first node, if (*headPtr == NULL) //if given list is empty *headPtr = UserList; else //case 2: Linked list NOT empty, find the last node, append to it. { //find last node. User *curr = *headPtr; for(;curr->next != NULL; curr = curr->next); //append new node to the last one. curr->next = UserList; } return; }
int cmd_current(mpd_unused int argc, mpd_unused char ** argv, mpd_Connection *conn) { mpd_Status * status; mpd_InfoEntity * entity; mpd_sendCommandListOkBegin(conn); printErrorAndExit(conn); mpd_sendStatusCommand(conn); printErrorAndExit(conn); mpd_sendCurrentSongCommand(conn); printErrorAndExit(conn); mpd_sendCommandListEnd(conn); printErrorAndExit(conn); status = mpd_getStatus(conn); printErrorAndExit(conn); if (status->state == MPD_STATUS_STATE_PLAY || status->state == MPD_STATUS_STATE_PAUSE) { mpd_nextListOkCommand(conn); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { struct mpd_song *song = entity->info.song; if(entity->type!=MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } pretty_print_song(song); printf("\n"); mpd_freeInfoEntity(entity); break; } printErrorAndExit(conn); mpd_finishCommand(conn); printErrorAndExit(conn); } mpd_freeStatus(status); return 0; }
int cmd_repeat ( int argc, char ** argv, mpd_Connection * conn ) { int mode; if(argc==1) { mode = get_boolean(argv[0]); if (mode < 0) return -1; } else { mpd_Status * status; status = getStatus(conn); mode = !status->repeat; mpd_freeStatus(status); } mpd_sendRepeatCommand(conn,mode); printErrorAndExit(conn); my_finishCommand(conn); printErrorAndExit(conn); return 1; }
// Given an address string, determine if it's a dotted-quad IP address // or a domain address. If the latter, ask DNS to resolve it. In // either case, return resolved a IP address. On failure report the // error and exit u_long lookupAddress(const char * host) { u_long nRemoteAddr = inet_addr(host); if (nRemoteAddr == INADDR_NONE) { // host isn't a dotted IP, so resolve it through DNS hostent * pHE = gethostbyname(host); if (pHE == 0) { const char * format = "lookupAddress failed gethostbyname(%s)"; char * szErr = (char *) malloc(strlen(format) + strlen(host) + 1); sprintf(szErr, format, host); printErrorAndExit(szErr); } nRemoteAddr = *((u_long*)pHE->h_addr_list[0]); } return nRemoteAddr; }
static int do_search(int argc, char ** argv, struct mpd_connection *conn, bool exact) { mpd_search_db_songs(conn, exact); if (!add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); print_entity_list(conn, MPD_ENTITY_TYPE_SONG); my_finishCommand(conn); return 0; }
static int do_search ( int argc, char ** argv, struct mpd_connection *conn, int exact ) { mpd_search_db_songs(conn, exact); if (!add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); print_filenames(conn); my_finishCommand(conn); return 0; }
SOCKET socketBind(const char * host, int port) { SOCKET sd = socket(AF_INET, SOCK_STREAM, 0); if (sd != INVALID_SOCKET) { sockaddr_in sinInterface; sinInterface.sin_family = AF_INET; sinInterface.sin_addr.s_addr = lookupAddress(host); sinInterface.sin_port = htons(port); if (bind(sd, (sockaddr*)&sinInterface, sizeof(sockaddr_in)) != SOCKET_ERROR) { listen(sd, SOMAXCONN); return sd; } } printErrorAndExit("socketBind"); return 0; }
int main(int argc, char ** argv) { struct mpd_connection *conn = setup_connection(); int id = -1; if (!mpd_send_list_all_meta(conn, "radio")) printErrorAndExit(conn); print_entity_list(conn, MPD_ENTITY_TYPE_SONG);//MPD_ENTITY_TYPE_UNKNOWN); id = find_songname_id(conn, "artist Radiohead"); printf("id = %i\n", id); mpd_connection_free(conn); return 0; }
int cmd_cdprev(gcc_unused int argc, gcc_unused char **argv, struct mpd_connection *conn) { struct mpd_status *status = getStatus(conn); /* go to previous track if mpd is playing first 3 seconds of current track otherwise seek to beginning of current track */ if (mpd_status_get_elapsed_time(status) < 3) { cmd_prev(0, NULL, conn); } else { if (!mpd_run_seek_id(conn, mpd_status_get_song_id(status), 0)) printErrorAndExit(conn); } return 1; }
int cmd_playlist(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn) { struct mpd_song *song; if (!mpd_send_list_queue_meta(conn)) printErrorAndExit(conn); while ((song = mpd_recv_song(conn)) != NULL) { pretty_print_song(song); mpd_song_free(song); printf("\n"); } my_finishCommand(conn); return 0; }
static int ls_entity(int argc, char **argv, struct mpd_connection *conn, enum mpd_entity_type type) { const char *ls = ""; int i = 0; if (argc > 0) ls = charset_to_utf8(argv[i]); do { if (!mpd_send_list_meta(conn, ls)) printErrorAndExit(conn); print_entity_list(conn, type); my_finishCommand(conn); } while (++i < argc && (ls = charset_to_utf8(argv[i])) != NULL); return 0; }
/** * Wait until the song changes or until playback is started/stopped. */ static void wait_current(struct mpd_connection *c) { struct mpd_status *status = getStatus(c); const int old_song = get_active_song(status); mpd_status_free(status); int new_song; do { enum mpd_idle idle = mpd_run_idle_mask(c, MPD_IDLE_PLAYER); if (idle == 0) printErrorAndExit(c); status = getStatus(c); new_song = get_active_song(status); mpd_status_free(status); } while (new_song == old_song); }
int cmd_volume ( int argc, char ** argv, struct mpd_connection *conn ) { struct int_value_change ch; struct mpd_status *status; if(argc==1) { if(!parse_int_value_change(argv[0], &ch)) DIE("\"%s\" is not an integer\n", argv[0]); } else { status = getStatus(conn); if (mpd_status_get_volume(status) >= 0) printf("volume:%3i%c\n", mpd_status_get_volume(status), '%'); else printf("volume: n/a\n"); mpd_status_free(status); return 0; } if (ch.is_relative) { int old_volume; status = getStatus(conn); old_volume = mpd_status_get_volume(status); mpd_status_free(status); ch.value += old_volume; if (ch.value < 0) ch.value = 0; else if (ch.value > 100) ch.value = 100; if (ch.value == old_volume) return 1; } if (!mpd_run_set_volume(conn, ch.value)) printErrorAndExit(conn); return 1; }
int cmd_move(gcc_unused int argc, char **argv, struct mpd_connection *conn) { int from; if (!parse_int(argv[0], &from) || from <= 0) DIE("\"%s\" is not a positive integer\n", argv[0]); int to; if (!parse_int(argv[1], &to) || to <= 0) DIE("\"%s\" is not a positive integer\n", argv[1]); /* users type in 1-based numbers, mpd uses 0-based */ --from; --to; if (!mpd_run_move(conn, from, to)) printErrorAndExit(conn); return 0; }
int cmd_crossfade ( int argc, char ** argv, struct mpd_connection *conn ) { if(argc==1) { int seconds; if(!parse_int(argv[0], &seconds) || seconds<0) DIE("\"%s\" is not 0 or positive integer\n",argv[0]); if (!mpd_run_crossfade(conn, seconds)) printErrorAndExit(conn); } else { struct mpd_status *status; status = getStatus(conn); printf("crossfade: %i\n", mpd_status_get_crossfade(status)); mpd_status_free(status); } return 0; }
int cmd_listall ( int argc, char ** argv, mpd_Connection * conn ) { const char * listall = ""; int i=0; if (argc > 0) listall = charset_to_utf8(argv[i]); do { mpd_sendListallCommand(conn,listall); printErrorAndExit(conn); print_filenames(conn); my_finishCommand(conn); } while (++i < argc && (listall = charset_to_utf8(argv[i])) != NULL); return 0; }
static void *handleClientConnection(void *data) { char buffer[COPY_BUFFER_SIZE]; int client_socket = (int) data; int read; while ((read = recv(client_socket, buffer, COPY_BUFFER_SIZE, 0 /*no flags*/)) > 0) { Monitor_AddBuffer(&monitor, buffer, read); } if (read < 0) { printErrorAndExit("relay: recv"); } Monitor_CloseBuffer(&monitor); close(client_socket); return NULL; }
int cmd_crossfade ( int argc, char ** argv, mpd_Connection * conn ) { int seconds; if(argc==1) { if(!parse_int(argv[0], &seconds) || seconds<0) DIE("\"%s\" is not 0 or positive integer\n",argv[0]); mpd_sendCrossfadeCommand(conn,seconds); my_finishCommand(conn); } else { mpd_Status * status; status = getStatus(conn); printf("crossfade: %i\n",status->crossfade); mpd_freeStatus(status); printErrorAndExit(conn); } return 0; }
int cmd_stats(gcc_unused int argc, gcc_unused char **argv, struct mpd_connection *conn) { struct mpd_stats *stats = mpd_run_stats(conn); if (stats == NULL) printErrorAndExit(conn); printf("Artists: %6d\n", mpd_stats_get_number_of_artists(stats)); printf("Albums: %6d\n", mpd_stats_get_number_of_albums(stats)); printf("Songs: %6d\n", mpd_stats_get_number_of_songs(stats)); printf("\n"); printf("Play Time: %s\n", DHMS(mpd_stats_get_play_time(stats))); printf("Uptime: %s\n", DHMS(mpd_stats_get_uptime(stats))); time_t t = mpd_stats_get_db_update_time(stats); printf("DB Updated: %s", ctime(&t)); /* no \n needed */ printf("DB Play Time: %s\n", DHMS(mpd_stats_get_db_play_time(stats))); mpd_stats_free(stats); return 0; }
/** * Wait until the song changes or until playback is started/stopped. */ static void wait_current(struct mpd_connection *c) { if (mpd_connection_cmp_server_version(c, 0, 14, 0) < 0) fprintf(stderr, "warning: MPD 0.14 required for this command\n"); struct mpd_status *status = getStatus(c); const int old_song = get_active_song(status); mpd_status_free(status); int new_song; do { enum mpd_idle idle = mpd_run_idle_mask(c, MPD_IDLE_PLAYER); if (idle == 0) printErrorAndExit(c); status = getStatus(c); new_song = get_active_song(status); mpd_status_free(status); } while (new_song == old_song); }
// generic routine to create a thread that maintains a connection // for receiving server messages void initClientThread(struct socketDescriptor * sd) { int connected; if ((connected=establishConnection(sd)) || !sd->exit) { // start rx thread if connected or if thread should retry (not exit) sd->exit=FALSE; sd->hThread = CreateThread( NULL, // default security 0, // default stack size (LPTHREAD_START_ROUTINE)&rxClientThread, sd, // thread parameter 0, // run thread immediately NULL); // ignore thread id } if ((!connected && sd->exit) || !sd->hThread) { const char * init = "initClientThread "; if (!sd->description) sd->description = sd->host; char * description = (char *) malloc(strlen(init) + strlen(sd->description)+1); printErrorAndExit((const char *)strcat(strcpy(description,init),sd->description)); } }
// create the server thread that listens on a bound socket // n.b. current design only supports a single server instance HANDLE createServerListenThread(SOCKET listeningSocket) { static HANDLE serverThread=NULL; if (serverThread) return serverThread; if (!serverList) serverList = new LinkedList(); if ((serverThread=CreateThread(NULL, // default security 0, // default stack size (LPTHREAD_START_ROUTINE)&serverListenThread, (LPVOID)listeningSocket, // thread parameter 0, // run thread immediately NULL)) // ignore thread id == NULL) { printErrorAndExit("createServerListenThread"); } return serverThread; }
int cmd_loadtab ( int argc, char ** argv, struct mpd_connection *conn ) { struct mpd_playlist *pl; if (argc != 1) return 0; if (!mpd_send_list_meta(conn, NULL)) printErrorAndExit(conn); while ((pl = mpd_recv_playlist(conn)) != NULL) { if (strncmp(mpd_playlist_get_path(pl), argv[0], strlen(argv[0])) == 0) printf("%s\n", charset_from_utf8(mpd_playlist_get_path(pl))); mpd_playlist_free(pl); } my_finishCommand(conn); return 0; }
int cmd_lstab ( int argc, char ** argv, struct mpd_connection *conn ) { struct mpd_directory *dir; if (argc != 1) return 0; if (!mpd_send_list_all(conn, NULL)) printErrorAndExit(conn); while ((dir = mpd_recv_directory(conn)) != NULL) { if (strncmp(mpd_directory_get_path(dir), argv[0], strlen(argv[0])) == 0) printf("%s\n", charset_from_utf8(mpd_directory_get_path(dir))); mpd_directory_free(dir); } my_finishCommand(conn); return 0; }
static int bool_cmd(int argc, char **argv, struct mpd_connection *conn, bool (*get_mode)(const struct mpd_status *status), bool (*run_set_mode)(struct mpd_connection *conn, bool mode)) { bool mode; if (argc == 1) { mode = get_boolean(argv[0]); if (mode < 0) return -1; } else { struct mpd_status *status; status = getStatus(conn); mode = !get_mode(status); mpd_status_free(status); } if (!run_set_mode(conn, mode)) printErrorAndExit(conn); return 1; }
int cmd_play(int argc, char **argv, struct mpd_connection *conn) { assert(argc < 2); bool success; if (argc > 0) { const char *string = argv[0]; int song; if (!parse_songnum(string, &song)) DIE("error parsing song numbers from: %s\n", string); song--; success = mpd_run_play_pos(conn, song); } else success = mpd_run_play(conn); if (!success) printErrorAndExit(conn); return 1; }