Пример #1
0
void get_fm_info(fm_app_t *app, char *output)
{
    fm_song_t *current;

    switch (app->player.status) {
        case FM_PLAYER_PLAY:
        case FM_PLAYER_PAUSE:
            current = fm_playlist_current(&app->playlist);
            char btitle[128], bart[128], balb[128], bcover[128], burl[128]; 
            sprintf(output, "{\"status\":\"%s\",\"kbps\":\"%s\",\"channel\":\"%s\",\"user\":\"%s\","
                    "\"title\":\"%s\",\"artist\":\"%s\", \"album\":\"%s\",\"year\":%d,"
                    "\"cover\":\"%s\",\"url\":\"%s\",\"sid\":%d,"
                    "\"like\":%d,\"pos\":%d,\"len\":%d}",
                    app->player.status == FM_PLAYER_PLAY? "play": "pause",
                    current->kbps,app->playlist.config.channel, app->playlist.config.uname,
                    escapejson(btitle, current->title), 
                    escapejson(bart, current->artist), 
                    escapejson(balb, current->album),
                    current->pubdate, 
                    escapejson(bcover, current->cover), 
                    escapejson(burl, current->url),
                    current->sid, current->like, fm_player_pos(&app->player),
                    fm_player_length(&app->player));
            break;
        case FM_PLAYER_STOP:
            sprintf(output, "{\"status\":\"stop\",\"kbps\":\"%s\",\"channel\":\"%s\",\"user\":\"%s\"}",
                    app->playlist.config.kbps,app->playlist.config.channel, app->playlist.config.uname);
            break;
        default:
            break;
    }
}
Пример #2
0
void app_client_handler(void *ptr, char *input, char *output)
{
    fm_app_t *app = (fm_app_t*) ptr;
    char *cmd = input;
    char *arg = split(input, ' ');

    if (strcmp(cmd, "play") == 0) {
        if (app->player.status != FM_PLAYER_STOP || fm_player_set_song(&app->player, fm_playlist_current(&app->playlist)) == 0) {
            fm_player_play(&app->player);
            get_fm_info(app, output);
        } else 
            sprintf(output, "{\"status\":\"error\",\"message\":\"Some errors occurred during the processing of the song\"}");
    }
    else if(strcmp(cmd, "stop") == 0) {
        fm_player_stop(&app->player);
        get_fm_info(app, output);
    }
    else if(strcmp(cmd, "pause") == 0) {
        fm_player_pause(&app->player);
        get_fm_info(app, output);
    }
    else if(strcmp(cmd, "toggle") == 0) {
        switch (app->player.status) {
            case FM_PLAYER_PLAY:
                fm_player_pause(&app->player);
                get_fm_info(app, output);
                break;
            case FM_PLAYER_PAUSE:
                fm_player_play(&app->player);
                get_fm_info(app, output);
                break;
            case FM_PLAYER_STOP:
                if (fm_player_set_song(&app->player, fm_playlist_current(&app->playlist)) == 0) {
                    fm_player_play(&app->player);
                } else {
                    sprintf(output, "{\"status\":\"error\",\"message\":\"Some errors occurred during the processing of the song\"}");
                }  
                break;
        }
    }
    else if(strcmp(cmd, "skip") == 0 || strcmp(cmd, "next") == 0) {
        if (fm_player_set_song(&app->player, fm_playlist_skip(&app->playlist, 0)) == 0) {
            fm_player_play(&app->player);
            get_fm_info(app, output);
        } else
            sprintf(output, "{\"status\":\"error\",\"message\":\"Some errors occurred during the processing of the song\"}");
    }
    else if(strcmp(cmd, "ban") == 0) {
        if (fm_player_set_song(&app->player, fm_playlist_ban(&app->playlist)) == 0) {
            fm_player_play(&app->player);
            get_fm_info(app, output);
        } else
            sprintf(output, "{\"status\":\"error\",\"message\":\"Some errors occurred during the processing of the song\"}");
    }
    else if(strcmp(cmd, "rate") == 0) {
        fm_playlist_rate(&app->playlist);
        get_fm_info(app, output);
    }
    else if(strcmp(cmd, "unrate") == 0) {
        fm_playlist_unrate(&app->playlist);
        get_fm_info(app, output);
    }
    else if(strcmp(cmd, "info") == 0) {
        get_fm_info(app, output);
    }
    else if(strcmp(cmd, "end") == 0) {
        app->server.should_quit = 1;
    }
    else if(strcmp(cmd, "setch") == 0) {
        if (arg == NULL) {
            sprintf(output, "{\"status\":\"error\",\"message\":\"Missing argument: %s\"}", input);
        }
        else {
            if (strcmp(arg, app->playlist.config.channel) != 0) {
                int ret = fm_playlist_update_mode(&app->playlist, arg);
                if (ret != 0) {
                    char *message = "";
                    switch (ret) {
                        case -2: message = "Unable to set local channel because music directory is not set."; break;
                        default: message = "Unable to set channel.";
                    }
                    sprintf(output, "{\"status\":\"error\",\"message\":\"%s\"}", message);
                } else if (fm_player_set_song(&app->player, fm_playlist_skip(&app->playlist, 1)) == 0) {
                    fm_player_play(&app->player);
                    get_fm_info(app, output);
                } else {
                    sprintf(output, "{\"status\":\"error\",\"message\":\"Some errors occurred during the processing of the song\"}");
                }
            }
        }
    }
    else if(strcmp(cmd, "kbps") == 0) {
        if (app->playlist.mode == plDouban) {
            if (arg == NULL) {
                sprintf(output, "{\"status\":\"error\",\"message\":\"Missing argument: %s\"}", input);
            }
            else if (strcmp(arg, "64") != 0 && strcmp(arg, "128") != 0 && strcmp(arg, "192") != 0) {
                sprintf(output, "{\"status\":\"error\",\"message\":\"Wrong argument: %s\"}", arg);
            }
            else {
                if (strcmp(arg, app->playlist.config.kbps) != 0) {
                    strcpy(app->playlist.config.kbps, arg);
                    if (fm_player_set_song(&app->player, fm_playlist_skip(&app->playlist, 0)) == 0) {
                        fm_player_play(&app->player);
                        get_fm_info(app, output);
                    } else 
                        sprintf(output, "{\"status\":\"error\",\"message\":\"Some errors occurred during the processing of the song\"}");
                } else
                    get_fm_info(app, output);
            }
        } else {
            sprintf(output, "{\"status\":\"error\",\"message\":\"Current channel does not support bitrate switch: %s\"}", input);
        }
    } else {
        sprintf(output, "{\"status\":\"error\",\"message\":\"Wrong command: %s\"}", input);
    }
}
Пример #3
0
Файл: app.c Проект: trazyn/fmd
void app_client_handler(void *ptr, char *input, char *output)
{
    	fm_app_t *app = (fm_app_t*) ptr;
	char *cmd = input;
	char *arg = split(input, ' ');

	if (strcmp(cmd, "play") == 0) 
	{
		if (app->player.status == FM_PLAYER_STOP) 
		{
			fm_player_set_url(&app->player, fm_playlist_current(&app->playlist)->audio);
		}

		fm_player_play(&app->player);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "stop") == 0) 
	{
		fm_player_stop(&app->player);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "pause") == 0) 
	{
		fm_player_pause(&app->player);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "toggle") == 0) 
	{
		fm_player_toggle(&app->player);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "skip") == 0) 
	{
		fm_player_set_url(&app->player, fm_playlist_skip(&app->playlist)->audio);
		fm_player_play(&app->player);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "ban") == 0) 
	{
		fm_player_set_url(&app->player, fm_playlist_ban(&app->playlist)->audio);
		fm_player_play(&app->player);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "rate") == 0) 
	{
		fm_playlist_rate(&app->playlist);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "unrate") == 0) 
	{
		fm_playlist_unrate(&app->playlist);
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "info") == 0) 
	{
		get_fm_info(app, output);
	}

	else if(strcmp(cmd, "end") == 0) 
	{
		app->server.should_quit = 1;
	}

	else if(strcmp(cmd, "setch") == 0) 
	{
		if (arg == NULL) 
		{
			sprintf(output, "{\"status\":\"error\",\"message\":\"Missing argument: %s\"}", input);
		}

		else 
		{
			int ch = atoi(arg);

			if (ch != app->playlist.config.channel) 
			{
				app->playlist.config.channel = ch;
				fm_player_set_url(&app->player, fm_playlist_skip(&app->playlist)->audio);
				fm_player_play(&app->player);
			}
			get_fm_info(app, output);
		}
	}

	else 
	{
		sprintf(output, "{\"status\":\"error\",\"message\":\"Wrong command: %s\"}", input);
	}
}