Skip to content

Commit

Permalink
Upd: rework presets code
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Aug 4, 2023
1 parent 1d813fa commit 0536c86
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 72 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ https://github.com/jcorporation/myMPD/

Describe the release here.

### API changes

This release changes some API methods.

- merge MYMPD_API_QUEUE_SEARCH_ADV and MYMPD_API_QUEUE_LIST into MYMPD_API_QUEUE_SEARCH
- rename MYMPD_API_PRESET_LOAD to MYMPD_API_PRESET_APPLY

### Changelog

- Feat: move song fingerprint calculation in worker thread #1046
Expand All @@ -16,7 +23,6 @@ Describe the release here.
- Feat: add advanced search to more views #1048
- Feat: add more inline help text's #1072
- Upd: some code enhancements #1045
- Upd: merge MYMPD_API_QUEUE_SEARCH_ADV and MYMPD_API_QUEUE_LIST into MYMPD_API_QUEUE_SEARCH
- Upd: Bootstrap 5.3.1
- Upd: rework settings
- Fix: calculate correct position for jukebox list
Expand Down
4 changes: 2 additions & 2 deletions docs/references/translating_status.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- es-AR: 82 missing phrases
- es-ES: 736 missing phrases
- es-VE: 742 missing phrases
- fi-FI: 739 missing phrases
- es-VE: 741 missing phrases
- fi-FI: 738 missing phrases
- fr-FR: 82 missing phrases
- it-IT: 82 missing phrases
- ja-JP: 84 missing phrases
Expand Down
4 changes: 2 additions & 2 deletions htdocs/js/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,8 @@ const APImethods = {
"name": APIparams.preset
}
},
"MYMPD_API_PRESET_LOAD": {
"desc": "Loads a preset.",
"MYMPD_API_PRESET_APPLY": {
"desc": "Applies a preset.",
"params": {
"name": APIparams.preset
}
Expand Down
2 changes: 1 addition & 1 deletion htdocs/js/modalSettingsPlayback.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function initModalSettingsPlayback() {
event.preventDefault();
if (event.target.nodeName === 'TD') {
if (!event.target.parentNode.classList.contains('not-clickable')) {
loadPreset(getData(event.target.parentNode, 'name'));
applyPreset(getData(event.target.parentNode, 'name'));
}
}
else if (event.target.nodeName === 'A') {
Expand Down
14 changes: 7 additions & 7 deletions htdocs/js/presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function initPresets() {
document.getElementById('selectPresetDropdown' + i).addEventListener('click', function(event) {
event.preventDefault();
if (event.target.nodeName === 'BUTTON') {
loadPreset(event.target.textContent);
applyPreset(event.target.textContent);
const d = event.target.parentNode.parentNode.previousElementSibling;
BSN.Dropdown.getInstance(d).hide();
}
Expand All @@ -23,22 +23,22 @@ function initPresets() {
}

/**
* Loads a preset
* Applies a preset
* @param {string} name preset name to load
* @returns {void}
*/
function loadPreset(name) {
sendAPI("MYMPD_API_PRESET_LOAD", {
function applyPreset(name) {
sendAPI("MYMPD_API_PRESET_APPLY", {
"name": name
}, loadPresetCheckError, true);
}, applyPresetCheckError, true);
}

/**
* Handler for the MYMPD_API_PRESET_LOAD jsonrpc response
* Handler for the MYMPD_API_PRESET_APPLY jsonrpc response
* @param {object} obj jsonrpc response
* @returns {void}
*/
function loadPresetCheckError(obj) {
function applyPresetCheckError(obj) {
if (obj.error) {
if (getOpenModal() !== null) {
showModalAlert(obj);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
X(MYMPD_API_PLAYLIST_RM) \
X(MYMPD_API_PLAYLIST_RM_ALL) \
X(MYMPD_API_PRESET_RM) \
X(MYMPD_API_PRESET_LOAD) \
X(MYMPD_API_PRESET_APPLY) \
X(MYMPD_API_QUEUE_ADD_RANDOM) \
X(MYMPD_API_QUEUE_APPEND_PLAYLISTS) \
X(MYMPD_API_QUEUE_APPEND_SEARCH) \
Expand Down
8 changes: 4 additions & 4 deletions src/lib/mympd_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void mympd_state_save(struct t_mympd_state *mympd_state, bool free_data) {
struct t_partition_state *partition_state = mympd_state->partition_state;
while (partition_state != NULL) {
mympd_api_last_played_file_save(partition_state);
presets_save(partition_state);
preset_list_save(partition_state);
partition_state = partition_state->next;
}
if (free_data == true) {
Expand Down Expand Up @@ -309,8 +309,8 @@ void partition_state_default(struct t_partition_state *partition_state, const ch
partition_state->stream_uri = sdsempty();
//lists
list_init(&partition_state->last_played);
list_init(&partition_state->presets);
presets_load(partition_state);
list_init(&partition_state->preset_list);
preset_list_load(partition_state);
}

/**
Expand All @@ -331,7 +331,7 @@ void partition_state_free(struct t_partition_state *partition_state) {
list_clear(&partition_state->jukebox_queue_tmp);
//lists
list_clear(&partition_state->last_played);
list_clear(&partition_state->presets);
list_clear(&partition_state->preset_list);
//local playback
FREE_SDS(partition_state->stream_uri);
//struct itself
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mympd_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct t_partition_state {
sds stream_uri; //!< custom url for local playback
//lists
struct t_list last_played; //!< last_played list
struct t_list presets; //!< Playback presets
struct t_list preset_list; //!< Playback presets
};

/**
Expand Down
79 changes: 70 additions & 9 deletions src/mpd_client/presets.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,49 @@
#include "src/lib/list.h"
#include "src/lib/log.h"
#include "src/lib/sds_extras.h"
#include "src/mpd_client/jukebox.h"
#include "src/mympd_api/settings.h"

#include <errno.h>

/**
* Applies a preset
* @param partition_state pointer to partition state
* @param preset_name preset name
* @param error pointer to alreay allocated sds string to append the error message
* @return true on success, else false
*/
bool preset_apply(struct t_partition_state *partition_state, sds preset_name, sds *error) {
struct t_list_node *preset = list_get_node(&partition_state->preset_list, preset_name);
if (preset != NULL) {
struct t_jsonrpc_parse_error parse_error;
jsonrpc_parse_error_init(&parse_error);
if (json_iterate_object(preset->value_p, "$", mympd_api_settings_mpd_options_set, partition_state, NULL, 100, &parse_error) == true) {
if (partition_state->jukebox_mode != JUKEBOX_OFF) {
//clear and start jukebox
jukebox_clear_all(partition_state->mympd_state);
jukebox_run(partition_state);
}
jsonrpc_parse_error_clear(&parse_error);
return true;
}
*error = sdscat(*error, "Can't set playback options");
jsonrpc_parse_error_clear(&parse_error);
}
else {
*error = sdscat(*error, "Could not load preset");
}
return false;
}

/**
* Prints the list of presets as json array
* @param presets preset list
* @param buffer sds string to append the array
* @return sds pointer to buffer
*/
sds presets_list(struct t_list *presets, sds buffer) {
struct t_list_node *current = presets->head;
sds presets_list(struct t_list *preset_list, sds buffer) {
struct t_list_node *current = preset_list->head;
int i = 0;
while (current != NULL) {
if (i++) {
Expand All @@ -34,9 +66,38 @@ sds presets_list(struct t_list *presets, sds buffer) {
return buffer;
}

bool presets_delete(struct t_list *presets, const char *preset) {
int idx = list_get_node_idx(presets, preset);
return list_remove_node(presets, idx);
/**
* Saves a preset
* @param preset_list pointer to preset list
* @param preset_name preset name
* @param preset_value preset value (stringified json)
* @param error pointer to alreay allocated sds string to append the error message
* @return true on success, else false
*/
bool preset_save(struct t_list *preset_list, sds preset_name, sds preset_value, sds *error) {
if (preset_value == NULL) {
*error = sdscat(*error, "Can't save preset");
return false;
}

int idx = list_get_node_idx(preset_list, preset_name);
if (idx > -1) {
// update existing preset
return list_replace(preset_list, idx, preset_name, 0, preset_value, NULL);
}
// add new preset
return list_push(preset_list, preset_name, 0, preset_value, NULL);
}

/**
* Deletes a preset
* @param preset_list pointer to preset list
* @param preset_name preset name
* @return bool
*/
bool preset_delete(struct t_list *preset_list, const char *preset_name) {
int idx = list_get_node_idx(preset_list, preset_name);
return list_remove_node(preset_list, idx);
}

/**
Expand All @@ -54,10 +115,10 @@ static sds preset_to_line_cb(sds buffer, struct t_list_node *current) {
* @param partition_state pointer to partition state
* @return bool true on success, else false
*/
bool presets_save(struct t_partition_state *partition_state) {
bool preset_list_save(struct t_partition_state *partition_state) {
sds filepath = sdscatfmt(sdsempty(), "%S/%S/%s",
partition_state->mympd_state->config->workdir, partition_state->state_dir, FILENAME_PRESETS);
bool rc = list_write_to_disk(filepath, &partition_state->presets, preset_to_line_cb);
bool rc = list_write_to_disk(filepath, &partition_state->preset_list, preset_to_line_cb);
FREE_SDS(filepath);
return rc;
}
Expand All @@ -67,7 +128,7 @@ bool presets_save(struct t_partition_state *partition_state) {
* @param partition_state pointer to partition state
* @return bool true on success, else false
*/
bool presets_load(struct t_partition_state *partition_state) {
bool preset_list_load(struct t_partition_state *partition_state) {
sds filepath = sdscatfmt(sdsempty(), "%S/%S/%s",
partition_state->mympd_state->config->workdir, partition_state->state_dir, FILENAME_PRESETS);
errno = 0;
Expand All @@ -77,7 +138,7 @@ bool presets_load(struct t_partition_state *partition_state) {
while (sds_getline(&line, fp, LINE_LENGTH_MAX) >= 0) {
sds name = NULL;
if (json_get_string_max(line, "$.name", &name, vcb_isname, NULL) == true) {
list_push(&partition_state->presets, name, 0, line, NULL);
list_push(&partition_state->preset_list, name, 0, line, NULL);
}
else {
MYMPD_LOG_ERROR(partition_state->name, "Reading presets line failed");
Expand Down
10 changes: 6 additions & 4 deletions src/mpd_client/presets.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include "src/lib/list.h"
#include "src/lib/mympd_state.h"

sds presets_list(struct t_list *presets, sds buffer);
bool presets_delete(struct t_list *presets, const char *preset);
bool presets_save(struct t_partition_state *partition_state);
bool presets_load(struct t_partition_state *partition_state);
bool preset_apply(struct t_partition_state *partition_state, sds preset_name, sds *error);
sds presets_list(struct t_list *preset_list, sds buffer);
bool preset_save(struct t_list *preset_list, sds preset_name, sds preset_value, sds *error);
bool preset_delete(struct t_list *preset_list, const char *preset_name);
bool preset_list_save(struct t_partition_state *partition_state);
bool preset_list_load(struct t_partition_state *partition_state);

#endif
47 changes: 10 additions & 37 deletions src/mympd_api/mympd_api_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,55 +426,28 @@ void mympd_api_handler(struct t_partition_state *partition_state, struct t_work_
json_get_string(request->data, "$.params.name", 0, NAME_LEN_MAX, &sds_buf1, vcb_isname, &parse_error) == true &&
sdslen(sds_buf1) > 0)
{
sds params = json_get_key_as_sds(request->data, "$.params");
if (params != NULL) {
int idx = list_get_node_idx(&partition_state->presets, sds_buf1);
if (idx > -1) {
list_replace(&partition_state->presets, idx, sds_buf1, 0, params, NULL);
}
else {
list_push(&partition_state->presets, sds_buf1, 0, params, NULL);
}
FREE_SDS(params);
}
else {
response->data = jsonrpc_respond_message(response->data, request->cmd_id, request->id,
JSONRPC_FACILITY_MPD, JSONRPC_SEVERITY_ERROR, "Can't save preset");
}
sds_buf2 = json_get_key_as_sds(request->data, "$.params");
rc = preset_save(&partition_state->preset_list, sds_buf1, sds_buf2, &error);
response->data = jsonrpc_respond_with_ok_or_error(response->data, request->cmd_id, request->id, rc,
JSONRPC_FACILITY_MPD, error);
break;
}
response->data = jsonrpc_respond_ok(response->data, request->cmd_id, request->id, JSONRPC_FACILITY_MPD);
}
break;
}
case MYMPD_API_PRESET_RM:
if (json_get_string(request->data, "$.params.name", 1, NAME_LEN_MAX, &sds_buf1, vcb_isname, &parse_error) == true) {
rc = presets_delete(&partition_state->presets, sds_buf1);
rc = preset_delete(&partition_state->preset_list, sds_buf1);
response->data = jsonrpc_respond_with_ok_or_error(response->data, request->cmd_id, request->id, rc,
JSONRPC_FACILITY_MPD, "Could not delete preset");
}
break;
case MYMPD_API_PRESET_LOAD:
case MYMPD_API_PRESET_APPLY:
if (json_get_string(request->data, "$.params.name", 1, NAME_LEN_MAX, &sds_buf1, vcb_isname, &parse_error) == true) {
struct t_list_node *preset = list_get_node(&partition_state->presets, sds_buf1);
if (preset != NULL) {
if (json_iterate_object(preset->value_p, "$", mympd_api_settings_mpd_options_set, partition_state, NULL, 100, &parse_error) == true) {
if (partition_state->jukebox_mode != JUKEBOX_OFF) {
//clear and start jukebox
jukebox_clear_all(partition_state->mympd_state);
jukebox_run(partition_state);
}
//respond with ok
response->data = jsonrpc_respond_ok(response->data, request->cmd_id, request->id, JSONRPC_FACILITY_MPD);
}
else {
response->data = jsonrpc_respond_message(response->data, request->cmd_id, request->id,
JSONRPC_FACILITY_MPD, JSONRPC_SEVERITY_ERROR, "Can't set playback options");
}
}
else {
response->data = jsonrpc_respond_message(response->data, request->cmd_id, request->id,
JSONRPC_FACILITY_SCRIPT, JSONRPC_SEVERITY_ERROR, "Could not load preset");
}
rc = preset_apply(partition_state, sds_buf1,&error);
response->data = jsonrpc_respond_with_ok_or_error(response->data, request->cmd_id, request->id, rc,
JSONRPC_FACILITY_MPD, error);
}
break;
// mpd connection
Expand Down
2 changes: 1 addition & 1 deletion src/mympd_api/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ sds mympd_api_settings_get(struct t_partition_state *partition_state, sds buffer
buffer = tojson_uint(buffer, "mpdStreamPort", partition_state->mpd_stream_port, true);
buffer = tojson_char(buffer, "streamUri", partition_state->stream_uri, true);
buffer = sdscat(buffer, "\"presets\": [");
buffer = presets_list(&partition_state->presets, buffer);
buffer = presets_list(&partition_state->preset_list, buffer);
buffer = sdscatlen(buffer, "],", 2);
if (partition_state->conn_state == MPD_CONNECTED) {
//mpd options
Expand Down
4 changes: 2 additions & 2 deletions src/mympd_api/timer_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ bool mympd_api_timer_startplay(struct t_partition_state *partition_state,
enum jukebox_modes jukebox_mode = JUKEBOX_OFF;
if (sdslen(preset) > 0) {
//get the jukebox mode from the preset
struct t_list_node *preset_value = list_get_node(&partition_state->presets, preset);
struct t_list_node *preset_value = list_get_node(&partition_state->preset_list, preset);
if (preset_value != NULL) {
sds jukebox_mode_str = NULL;
if (json_get_string_max(preset_value->value_p, "$.jukeboxMode", &jukebox_mode_str, vcb_isname, NULL) == true) {
Expand Down Expand Up @@ -162,7 +162,7 @@ bool mympd_api_timer_startplay(struct t_partition_state *partition_state,

if (sdslen(preset) > 0) {
//load the preset
struct t_work_request *request = create_request(-1, 0, MYMPD_API_PRESET_LOAD, NULL, partition_state->name);
struct t_work_request *request = create_request(-1, 0, MYMPD_API_PRESET_APPLY, NULL, partition_state->name);
request->data = tojson_sds(request->data, "name", preset, false);
request->data = jsonrpc_end(request->data);
mympd_queue_push(mympd_api_queue, request, 0);
Expand Down

0 comments on commit 0536c86

Please sign in to comment.