Exemple #1
0
bool
Control::stop()
{
	EXIT_IDLE;

	pms->log(MSG_DEBUG, 0, "Stopping playback.\n");

	return mpd_run_stop(conn->h());
}
Exemple #2
0
static void stopSong()
{
    mpd_update();
    if (currentSong != NULL) {
	if ((!mpd_run_stop(conn))
	    || (!mpd_response_finish(conn))) {
	    mpd_printerror("run_stop");
	}
    }
}
Exemple #3
0
void Playback::setPlayback(Playback::playback_operation operation)
{
	switch (operation)
	{
	case Playback::PLAY: mpd_run_play(Connection::getConnection()); this->currentState = PLAY; break;
	case Playback::PLAYPAUSE: mpd_run_toggle_pause(Connection::getConnection()); this->currentState = PLAYPAUSE; break;
	case Playback::STOP: mpd_run_stop(Connection::getConnection()); this->currentState = STOP; break;
	case Playback::NEXT: mpd_run_next(Connection::getConnection()); this->currentState = PLAY; break;
	case Playback::PREVIOUS: mpd_run_previous(Connection::getConnection()); this->currentState = PLAY; break;
	default: break;
	}
}
Exemple #4
0
void mpd_stop(void)
{
	if (!mpd.conn) {
		irc_say("Not connected to MPD");
		return;
	}

	mpd_run_noidle(mpd.conn);
	mpd_run_stop(mpd.conn);
	if (!mpd_response_finish(mpd.conn)) {
		mpd_report_error();
		return;
	}
	mpd_send_idle_mask(mpd.conn, MPD_IDLE_PLAYER);
}
Exemple #5
0
int callback_mpd(struct mg_connection *c)
{
    enum mpd_cmd_ids cmd_id = get_cmd_id(c->content);
    size_t n = 0;
    unsigned int uint_buf, uint_buf_2;
    int int_buf;
    char *p_charbuf = NULL, *token;

    if(cmd_id == -1)
        return MG_TRUE;

    if(mpd.conn_state != MPD_CONNECTED && cmd_id != MPD_API_SET_MPDHOST &&
        cmd_id != MPD_API_GET_MPDHOST && cmd_id != MPD_API_SET_MPDPASS &&
        cmd_id != MPD_API_GET_DIRBLEAPITOKEN)
        return MG_TRUE;

    switch(cmd_id)
    {
        case MPD_API_UPDATE_DB:
            mpd_run_update(mpd.conn, NULL);
            break;
        case MPD_API_SET_PAUSE:
            mpd_run_toggle_pause(mpd.conn);
            break;
        case MPD_API_SET_PREV:
            mpd_run_previous(mpd.conn);
            break;
        case MPD_API_SET_NEXT:
            mpd_run_next(mpd.conn);
            break;
        case MPD_API_SET_PLAY:
            mpd_run_play(mpd.conn);
            break;
        case MPD_API_SET_STOP:
            mpd_run_stop(mpd.conn);
            break;
        case MPD_API_RM_ALL:
            mpd_run_clear(mpd.conn);
            break;
        case MPD_API_RM_TRACK:
            if(sscanf(c->content, "MPD_API_RM_TRACK,%u", &uint_buf))
                mpd_run_delete_id(mpd.conn, uint_buf);
            break;
        case MPD_API_PLAY_TRACK:
            if(sscanf(c->content, "MPD_API_PLAY_TRACK,%u", &uint_buf))
                mpd_run_play_id(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_RANDOM:
            if(sscanf(c->content, "MPD_API_TOGGLE_RANDOM,%u", &uint_buf))
                mpd_run_random(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_REPEAT:
            if(sscanf(c->content, "MPD_API_TOGGLE_REPEAT,%u", &uint_buf))
                mpd_run_repeat(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_CONSUME:
            if(sscanf(c->content, "MPD_API_TOGGLE_CONSUME,%u", &uint_buf))
                mpd_run_consume(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_SINGLE:
            if(sscanf(c->content, "MPD_API_TOGGLE_SINGLE,%u", &uint_buf))
                mpd_run_single(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_CROSSFADE:
            if(sscanf(c->content, "MPD_API_TOGGLE_CROSSFADE,%u", &uint_buf))
                mpd_run_crossfade(mpd.conn, uint_buf);
            break;
        case MPD_API_GET_OUTPUTS:
            mpd.buf_size = mpd_put_outputs(mpd.buf, 1);
            c->callback_param = NULL;
            mpd_notify_callback(c, MG_POLL);
            break;
        case MPD_API_TOGGLE_OUTPUT:
            if (sscanf(c->content, "MPD_API_TOGGLE_OUTPUT,%u,%u", &uint_buf, &uint_buf_2)) {
                if (uint_buf_2)
                    mpd_run_enable_output(mpd.conn, uint_buf);
                else
                    mpd_run_disable_output(mpd.conn, uint_buf);
            }
            break;
        case MPD_API_SET_VOLUME:
            if(sscanf(c->content, "MPD_API_SET_VOLUME,%ud", &uint_buf) && uint_buf <= 100)
                mpd_run_set_volume(mpd.conn, uint_buf);
            break;
        case MPD_API_SET_SEEK:
            if(sscanf(c->content, "MPD_API_SET_SEEK,%u,%u", &uint_buf, &uint_buf_2))
                mpd_run_seek_id(mpd.conn, uint_buf, uint_buf_2);
            break;
        case MPD_API_GET_QUEUE:
            if(sscanf(c->content, "MPD_API_GET_QUEUE,%u", &uint_buf))
                n = mpd_put_queue(mpd.buf, uint_buf);
            break;
        case MPD_API_GET_BROWSE:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_BROWSE"))
                goto out_browse;

            uint_buf = strtoul(strtok(NULL, ","), NULL, 10);
            if((token = strtok(NULL, ",")) == NULL)
                goto out_browse;

			free(p_charbuf);
            p_charbuf = strdup(c->content);
            n = mpd_put_browse(mpd.buf, get_arg2(p_charbuf), uint_buf);
out_browse:
			free(p_charbuf);
            break;
        case MPD_API_ADD_TRACK:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_TRACK"))
                goto out_add_track;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_add_track;

			free(p_charbuf);
            p_charbuf = strdup(c->content);
            mpd_run_add(mpd.conn, get_arg1(p_charbuf));
out_add_track:
            free(p_charbuf);
            break;
        case MPD_API_ADD_PLAY_TRACK:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_PLAY_TRACK"))
                goto out_play_track;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_play_track;

			free(p_charbuf);
            p_charbuf = strdup(c->content);
            int_buf = mpd_run_add_id(mpd.conn, get_arg1(p_charbuf));
            if(int_buf != -1)
                mpd_run_play_id(mpd.conn, int_buf);
out_play_track:
            free(p_charbuf);
            break;
        case MPD_API_ADD_PLAYLIST:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_PLAYLIST"))
                goto out_playlist;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_playlist;

			free(p_charbuf);
            p_charbuf = strdup(c->content);
            mpd_run_load(mpd.conn, get_arg1(p_charbuf));
out_playlist:
            free(p_charbuf);
            break;
        case MPD_API_SAVE_QUEUE:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_SAVE_QUEUE"))
                goto out_save_queue;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_save_queue;

			free(p_charbuf);
            p_charbuf = strdup(c->content);
            mpd_run_save(mpd.conn, get_arg1(p_charbuf));
out_save_queue:
            free(p_charbuf);
            break;
        case MPD_API_SEARCH:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH"))
				goto out_search;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_search;

			free(p_charbuf);
            p_charbuf = strdup(c->content);
            n = mpd_search(mpd.buf, get_arg1(p_charbuf));
out_search:
            free(p_charbuf);
            break;
#ifdef WITH_MPD_HOST_CHANGE
        /* Commands allowed when disconnected from MPD server */
        case MPD_API_SET_MPDHOST:
            int_buf = 0;
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_SET_MPDHOST"))
                goto out_host_change;

            if((int_buf = strtol(strtok(NULL, ","), NULL, 10)) <= 0)
                goto out_host_change;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_host_change;

            strncpy(mpd.host, token, sizeof(mpd.host));
            mpd.port = int_buf;
            mpd.conn_state = MPD_RECONNECT;
            free(p_charbuf);
            return MG_TRUE;
out_host_change:
            free(p_charbuf);
            break;
        case MPD_API_GET_MPDHOST:
            n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"mpdhost\", \"data\": "
                "{\"host\" : \"%s\", \"port\": \"%d\", \"passwort_set\": %s}"
                "}", mpd.host, mpd.port, mpd.password ? "true" : "false");
            break;
        case MPD_API_GET_DIRBLEAPITOKEN:
            n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"dirbleapitoken\", \""
                "data\": \"%s\"}", dirble_api_token);
            break;
        case MPD_API_SET_MPDPASS:
            p_charbuf = strdup(c->content);
            if(strcmp(strtok(p_charbuf, ","), "MPD_API_SET_MPDPASS"))
                goto out_set_pass;

            if((token = strtok(NULL, ",")) == NULL)
                goto out_set_pass;

            if(mpd.password)
                free(mpd.password);

            mpd.password = strdup(token);
            mpd.conn_state = MPD_RECONNECT;
            free(p_charbuf);
            return MG_TRUE;
out_set_pass:
            free(p_charbuf);
            break;
#endif
    }

    if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS)
    {
        n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}", 
            mpd_connection_get_error_message(mpd.conn));

        /* Try to recover error */
        if (!mpd_connection_clear_error(mpd.conn))
            mpd.conn_state = MPD_FAILURE;
    }

    if(n > 0)
        mg_websocket_write(c, 1, mpd.buf, n);

    return MG_TRUE;
}
Exemple #6
0
bool
handle_player_command(struct mpdclient *c, command_t cmd)
{
	struct mpd_connection *connection;

	if (!mpdclient_is_connected(c) || c->status == NULL)
		return false;

	cancel_seek_timer();

	switch(cmd) {
		/*
	case CMD_PLAY:
		mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
		break;
		*/
	case CMD_PAUSE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_pause(connection,
				   mpd_status_get_state(c->status) != MPD_STATE_PAUSE))
			mpdclient_handle_error(c);
		break;
	case CMD_STOP:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_stop(connection))
			mpdclient_handle_error(c);
		break;
	case CMD_CROP:
		mpdclient_cmd_crop(c);
		break;
	case CMD_SEEK_FORWARD:
		if (!setup_seek(c))
			break;

		seek_target_time += options.seek_time;
		if (seek_target_time > (int)mpd_status_get_total_time(c->status))
			seek_target_time = mpd_status_get_total_time(c->status);
		break;

	case CMD_TRACK_NEXT:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_next(connection))
			mpdclient_handle_error(c);
		break;
	case CMD_SEEK_BACKWARD:
		if (!setup_seek(c))
			break;

		seek_target_time -= options.seek_time;
		if (seek_target_time < 0)
			seek_target_time = 0;
		break;

	case CMD_TRACK_PREVIOUS:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_previous(connection))
			mpdclient_handle_error(c);
		break;
	case CMD_SHUFFLE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (mpd_run_shuffle(connection))
			screen_status_message(_("Shuffled playlist"));
		else
			mpdclient_handle_error(c);
		break;
	case CMD_CLEAR:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (mpdclient_cmd_clear(c))
			screen_status_message(_("Cleared playlist"));
		break;
	case CMD_REPEAT:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_repeat(connection,
				    !mpd_status_get_repeat(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_RANDOM:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_random(connection,
				    !mpd_status_get_random(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_SINGLE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_single(connection,
				    !mpd_status_get_single(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_CONSUME:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_consume(connection,
				     !mpd_status_get_consume(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_CROSSFADE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_crossfade(connection,
				       mpd_status_get_crossfade(c->status) > 0
				       ? 0 : options.crossfade_time))
			mpdclient_handle_error(c);
		break;
	case CMD_DB_UPDATE:
		screen_database_update(c, NULL);
		break;
	case CMD_VOLUME_UP:
		mpdclient_cmd_volume_up(c);
		break;
	case CMD_VOLUME_DOWN:
		mpdclient_cmd_volume_down(c);
		break;

	default:
		return false;
	}

	return true;
}
/*btn3_short_press*/
void btn3_short_press(void)
{
        int res = json_type_null;
        json_object * pjson_obj_read = NULL;
        struct mpd_status *status = NULL;

        ncurrt_time = 0;
        system("killall aplay");
        system("killall xfchat");

        pthread_rwlock_wrlock(&json_rwlock_voice);
        pjson_obj_read = json_object_from_file(PLAY_VOICE_JSON_PATH);
        pthread_rwlock_unlock(&json_rwlock_voice);
        res = json_object_get_type(pjson_obj_read);
        printf("json_type: %u \n", json_object_get_type(pjson_obj_read));
//        printf("json_length: %u \n", json_object_array_length(pjson_obj_read));

        json_object_put(pjson_obj_read);
        if ((json_type_array == res) && (0 < json_object_array_length(pjson_obj_read)) && (!bplay_audio))
        {
                printf("nd btn3_short_press ret: %d, bplay_audio: %d\n", res, bplay_audio);
                my_mpd_run_pause();
                bplay_audio = true;
        }
        else
        {
                int quere_len = 0;
                int online_size = 0;
                bplay_audio = false;

                online_size = get_file_size(ONLINE_LIST_PATH);
                if (online_size > 0)
                {
                        quere_len = get_mpc_quere_len(conn);

                        pthread_mutex_lock(&mutex);
                        if (quere_len > 0)
                        {
                                mpd_run_stop(conn);
                                mpd_run_clear(conn);
                        }

                        bool mpc_load = mpd_run_load(conn, "online.lst");
                        bool mpc_list_clear = mpd_run_playlist_clear(conn, "online.lst");
                        printf("nd btn3_short_press mpc_load: %d, mpc_list_clear: %d\n", mpc_load, mpc_list_clear);
                        pthread_mutex_unlock(&mutex);

                        res = i2c_smbus_write_byte_data(i2c_file, 0x00, BLUE_OFF);
                        if (res < 0)
                        {
                                printf("nd btn3_short_press i2c_smbus_write_byte_data BLUE_OFF failed, ret: %d\n", res);
                        }
                }

                quere_len = get_mpc_quere_len(conn);
                if (quere_len > 0)
                {
                        printf("nd btn3_short_press get_mpc_quere_len quere_len: %d\n", quere_len);
                        pthread_mutex_lock(&mutex);
                        status = mpd_run_status(conn);
                        if (!status)
                        {
                                printf("nd btn3_short_press mpd_run_status2 %s \n", mpd_connection_get_error_message(conn));
                        }
                        else
                        {
                                if (mpd_status_get_state(status) == MPD_STATE_PLAY)
                                {
                                        printf("nd btn3_short_press mpd_status_get_state MPD_STATE_PLAY\n");
                                        mpd_run_pause(conn, true);
                                }
                                else
                                {
                                        printf("nd btn3_short_press mpd_status_get_state other state\n");
                                        mpd_run_play(conn);
                                }

                                mpd_status_free(status);
                        }
                        pthread_mutex_unlock(&mutex);
                }
  }
}
Exemple #8
0
void Connection::Stop()
{
	prechecksNoCommandsList();
	mpd_run_stop(m_connection.get());
	checkErrors();
}
Exemple #9
0
int callback_mpd(struct mg_connection *c)
{
    enum mpd_cmd_ids cmd_id = get_cmd_id(c->content);
    size_t n = 0;
    unsigned int uint_buf, uint_buf_2;
    int int_buf;
    char *p_charbuf = NULL;

    if(cmd_id == -1)
        return MG_CLIENT_CONTINUE;

    if(mpd.conn_state != MPD_CONNECTED && cmd_id != MPD_API_SET_MPDHOST &&
        cmd_id != MPD_API_GET_MPDHOST && cmd_id != MPD_API_SET_MPDPASS)
        return MG_CLIENT_CONTINUE;

    mpd_connection_set_timeout(mpd.conn, 10000);
    switch(cmd_id)
    {
        case MPD_API_UPDATE_DB:
            mpd_run_update(mpd.conn, NULL);
            break;
        case MPD_API_SET_PAUSE:
            mpd_run_toggle_pause(mpd.conn);
            break;
        case MPD_API_SET_PREV:
            mpd_run_previous(mpd.conn);
            break;
        case MPD_API_SET_NEXT:
            mpd_run_next(mpd.conn);
            break;
        case MPD_API_SET_PLAY:
            mpd_run_play(mpd.conn);
            break;
        case MPD_API_SET_STOP:
            mpd_run_stop(mpd.conn);
            break;
        case MPD_API_RM_ALL:
            mpd_run_clear(mpd.conn);
            break;
        case MPD_API_RM_TRACK:
            if(sscanf(c->content, "MPD_API_RM_TRACK,%u", &uint_buf))
                mpd_run_delete_id(mpd.conn, uint_buf);
            break;
        case MPD_API_PLAY_TRACK:
            if(sscanf(c->content, "MPD_API_PLAY_TRACK,%u", &uint_buf))
                mpd_run_play_id(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_RANDOM:
            if(sscanf(c->content, "MPD_API_TOGGLE_RANDOM,%u", &uint_buf))
                mpd_run_random(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_REPEAT:
            if(sscanf(c->content, "MPD_API_TOGGLE_REPEAT,%u", &uint_buf))
                mpd_run_repeat(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_CONSUME:
            if(sscanf(c->content, "MPD_API_TOGGLE_CONSUME,%u", &uint_buf))
                mpd_run_consume(mpd.conn, uint_buf);
            break;
        case MPD_API_TOGGLE_SINGLE:
            if(sscanf(c->content, "MPD_API_TOGGLE_SINGLE,%u", &uint_buf))
                mpd_run_single(mpd.conn, uint_buf);
            break;
        case MPD_API_SET_VOLUME:
            if(sscanf(c->content, "MPD_API_SET_VOLUME,%ud", &uint_buf) && uint_buf <= 100)
                mpd_run_set_volume(mpd.conn, uint_buf);
            break;
        case MPD_API_SET_SEEK:
            if(sscanf(c->content, "MPD_API_SET_SEEK,%u,%u", &uint_buf, &uint_buf_2))
                mpd_run_seek_id(mpd.conn, uint_buf, uint_buf_2);
            break;
        case MPD_API_GET_QUEUE:
            if(sscanf(c->content, "MPD_API_GET_QUEUE,%u", &uint_buf))
                n = mpd_put_queue(mpd.buf, uint_buf);
            break;
        case MPD_API_GET_BROWSE:
            if(sscanf(c->content, "MPD_API_GET_BROWSE,%u,%m[^\t\n]", &uint_buf, &p_charbuf) && p_charbuf != NULL)
            {
                n = mpd_put_browse(mpd.buf, p_charbuf, uint_buf);
                free(p_charbuf);
            }
            break;
        case MPD_API_ADD_TRACK:
            if(sscanf(c->content, "MPD_API_ADD_TRACK,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
            {
                mpd_run_add(mpd.conn, p_charbuf);
                free(p_charbuf);
            }
            break;
        case MPD_API_ADD_PLAY_TRACK:
            if(sscanf(c->content, "MPD_API_ADD_PLAY_TRACK,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
            {
                int_buf = mpd_run_add_id(mpd.conn, p_charbuf);
                if(int_buf != -1)
                    mpd_run_play_id(mpd.conn, int_buf);
                free(p_charbuf);
            }
            break;
        case MPD_API_ADD_PLAYLIST:
            if(sscanf(c->content, "MPD_API_ADD_PLAYLIST,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
            {
                mpd_run_load(mpd.conn, p_charbuf);
                free(p_charbuf);
            }
            break;
        case MPD_API_SEARCH:
            if(sscanf(c->content, "MPD_API_SEARCH,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL)
            {
                n = mpd_search(mpd.buf, p_charbuf);
                free(p_charbuf);
            }
            break;
#ifdef WITH_MPD_HOST_CHANGE
        /* Commands allowed when disconnected from MPD server */
        case MPD_API_SET_MPDHOST:
            int_buf = 0;
            if(sscanf(c->content, "MPD_API_SET_MPDHOST,%d,%m[^\t\n ]", &int_buf, &p_charbuf) && 
                p_charbuf != NULL && int_buf > 0)
            {
                strncpy(mpd.host, p_charbuf, sizeof(mpd.host));
                free(p_charbuf);
                mpd.port = int_buf;
                mpd.conn_state = MPD_RECONNECT;
                return MG_CLIENT_CONTINUE;
            }
            break;
        case MPD_API_GET_MPDHOST:
            n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"mpdhost\", \"data\": "
                "{\"host\" : \"%s\", \"port\": \"%d\", \"passwort_set\": %s}"
                "}", mpd.host, mpd.port, mpd.password ? "true" : "false");
            break;
        case MPD_API_SET_MPDPASS:
            if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf))
            {
                if(mpd.password)
                    free(mpd.password);

                mpd.password = p_charbuf;
                mpd.conn_state = MPD_RECONNECT;
                return MG_CLIENT_CONTINUE;
            }
            break;
#endif
    }

    if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS)
    {
        n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}", 
            mpd_connection_get_error_message(mpd.conn));

        /* Try to recover error */
        if (!mpd_connection_clear_error(mpd.conn))
            mpd.conn_state = MPD_FAILURE;
    }

    if(n > 0)
        mg_websocket_write(c, 1, mpd.buf, n);

    return MG_CLIENT_CONTINUE;
}
Exemple #10
0
int main(int argc, char const *argv[])
{
  if (argc < 5) {
    printf("Usage: evmpd HOSTNAME PORT DEVICE HALT_COMMAND\n");
    return EXIT_FAILURE;
  }

  struct mpd_connection *client = connect(argv[1], strtol(argv[2], NULL, 10));

  struct libevdev *dev = NULL;
  int fd;
  int rc = 1;

  fd = open(argv[3], O_RDONLY | O_NONBLOCK);
  rc = libevdev_new_from_fd(fd, &dev);
  if (rc < 0) {
    fprintf(stderr, "Failed to init libevdev (%d)\n", strerror(-rc));
    return EXIT_FAILURE;
  }
  printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
  printf("Input device ID: bus %#x vendor %#x product %#x\n",
         libevdev_get_id_bustype(dev),
         libevdev_get_id_vendor(dev), libevdev_get_id_product(dev));

  do {
    struct input_event ev;

    rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);

    if (rc == 0 && ev.value == 0) {
      if (libevdev_event_is_code(&ev, EV_KEY, KEY_F4)) {
        system(argv[4]);
        printf("shutdown\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_PLAYPAUSE)) {
        struct mpd_status *status = mpd_run_status(client);
        enum mpd_state state = mpd_status_get_state(status);

        if (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE) {
          mpd_run_play(client);
          printf("play\n");
        } else {
          mpd_run_toggle_pause(client);
          printf("toggle-pause\n");
        }

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_STOPCD)) {
        mpd_run_stop(client);
        printf("stop\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_PREVIOUSSONG)) {
        mpd_run_previous(client);
        printf("previous\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_NEXTSONG)) {
        mpd_run_next(client);
        printf("next\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_VOLUMEUP)) {
        struct mpd_status *status = mpd_run_status(client);
        int volume = mpd_status_get_volume(status);

        volume += 5;
        if (volume > 100) {
          volume = 100;
        }

        mpd_run_set_volume(client, volume);
        printf("set-volume %d\n", volume);

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_VOLUMEDOWN)) {
        struct mpd_status *status = mpd_run_status(client);
        int volume = mpd_status_get_volume(status);

        volume -= 5;
        if (volume < 0) {
          volume = 0;
        }

        mpd_run_set_volume(client, volume);
        printf("set-volume %d\n", volume);
      } else {
        printf("Event: %s %s %d\n",
               libevdev_event_type_get_name(ev.type),
               libevdev_event_code_get_name(ev.type, ev.code),
               ev.value);
      }
    }

  }
  while (rc == LIBEVDEV_READ_STATUS_SUCCESS ||
         rc == LIBEVDEV_READ_STATUS_SYNC ||
         rc == -EAGAIN);

  printf("rc: %d\n", rc);

  if (client != NULL) {
    mpd_connection_free(client);
  }
  return EXIT_SUCCESS;
}