Exemplo n.º 1
0
Arquivo: app.c Projeto: digideskio/RPD
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);
    }
}
Exemplo n.º 2
0
Arquivo: app.c Projeto: 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);
	}
}