From a2b9f7c9903d2f0d22a5f77dc9b151313f12db7e Mon Sep 17 00:00:00 2001 From: Oleksiy Yakovenko Date: Thu, 1 Feb 2024 18:10:56 +0100 Subject: [PATCH] undo: refactor the usage of deadbeef API calls from the undo system --- include/deadbeef/deadbeef.h | 3 +-- plugins/cocoaui/Undo/DdbUndoBuffer.m | 5 +++++ plugins/gtkui/undo.c | 7 +++++- src/plugins.c | 30 +++++++++++++++++++++++++- src/undo/undo_playlist.h | 1 + src/undo/undobuffer.c | 5 ----- src/undo/undobuffer.h | 2 -- src/undo/undomanager.c | 32 ---------------------------- src/undo/undomanager.h | 4 ---- 9 files changed, 42 insertions(+), 47 deletions(-) diff --git a/include/deadbeef/deadbeef.h b/include/deadbeef/deadbeef.h index d941a46e1d..600204a595 100644 --- a/include/deadbeef/deadbeef.h +++ b/include/deadbeef/deadbeef.h @@ -1718,8 +1718,7 @@ typedef struct { /// If undo registration is enabled: required to be called on main thread. void (*plt_move_all_items) (ddb_playlist_t *to, ddb_playlist_t *from, ddb_playItem_t *insert_after); - /// Called to create an undo action from all actions - /// accumulated since the previous calls + /// Called to send the accumulated undo buffer to the active UI plugin. void (*undo_process)(void); /// Set the action name to be displayed in Undo/Redo menu item. diff --git a/plugins/cocoaui/Undo/DdbUndoBuffer.m b/plugins/cocoaui/Undo/DdbUndoBuffer.m index e54dfbe377..efe9a76364 100644 --- a/plugins/cocoaui/Undo/DdbUndoBuffer.m +++ b/plugins/cocoaui/Undo/DdbUndoBuffer.m @@ -23,6 +23,9 @@ #import "DdbUndoBuffer.h" #include "undomanager.h" +#include + +extern DB_functions_t *deadbeef; @interface DdbUndoBuffer() @@ -44,6 +47,8 @@ - (instancetype)initWithUndoBuffer:(undobuffer_t *)buffer { - (void)apply { undobuffer_execute(self.buffer, undomanager_get_buffer(undomanager_shared())); + + deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, 0, 0); } @end diff --git a/plugins/gtkui/undo.c b/plugins/gtkui/undo.c index 0cc4832bbf..d9535724d4 100644 --- a/plugins/gtkui/undo.c +++ b/plugins/gtkui/undo.c @@ -21,12 +21,15 @@ 3. This notice may not be removed or altered from any source distribution. */ +#include #include #include #include "undo.h" #include "undobuffer.h" #include "undomanager.h" +extern DB_functions_t *deadbeef; + struct undo_item_s; typedef struct undo_item_s undo_item_t; @@ -153,9 +156,11 @@ _perform_undo_redo (undo_type_t type) { _state.type = type; undobuffer_execute(item->undobuffer, new_buffer); undomanager_set_action_name(undomanager, item->action_name); - undomanager_flush (undomanager); + deadbeef->undo_process(); _free_item (item); _state.type = none; + + deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, 0, 0); } void diff --git a/src/plugins.c b/src/plugins.c index fdd76a7866..0567df85dd 100644 --- a/src/plugins.c +++ b/src/plugins.c @@ -142,9 +142,37 @@ static void _viz_spectrum_listen_stub (void *ctx, void (*callback)(void *ctx, const ddb_audio_data_t *data)) { } +static DB_plugin_t * +_plug_get_gui (void) { + struct DB_plugin_s **plugs = deadbeef->plug_get_list (); + for (int i = 0; plugs[i]; i++) { + if (plugs[i]->type == DB_PLUGIN_GUI) { + return plugs[i]; + } + } + return NULL; +} + static void _undo_process(void) { - undomanager_flush(undomanager_shared()); + undomanager_t *undomanager = undomanager_shared(); + + DB_plugin_t *ui_plugin = _plug_get_gui (); + undobuffer_t *undobuffer = undomanager_consume_buffer (undomanager); + + int has_ui_command = ui_plugin != NULL && ui_plugin->command != NULL; + + int res = -1; + if (undobuffer_has_operations (undobuffer) && has_ui_command) { + ui_plugin->command (110, undomanager); + res = ui_plugin->command (111, undobuffer, undomanager_get_action_name (undomanager)); + } + + if (res != 0) { + undobuffer_free (undobuffer); + } + + undomanager_set_action_name (undomanager, NULL); } static void diff --git a/src/undo/undo_playlist.h b/src/undo/undo_playlist.h index a2504e55b0..9c6c9c27e4 100644 --- a/src/undo/undo_playlist.h +++ b/src/undo/undo_playlist.h @@ -25,6 +25,7 @@ #define undo_playlist_h #include "undobuffer.h" +#include "../playlist.h" void undo_remove_items(undobuffer_t *undobuffer, playlist_t *plt, playItem_t **items, size_t count); diff --git a/src/undo/undobuffer.c b/src/undo/undobuffer.c index 6ce906e4d9..80402467ed 100644 --- a/src/undo/undobuffer.c +++ b/src/undo/undobuffer.c @@ -25,8 +25,6 @@ #include #include "undobuffer.h" -extern DB_functions_t *deadbeef; - struct _undobuffer_s { undo_operation_t *operations; int enabled; @@ -90,9 +88,6 @@ undobuffer_execute (undobuffer_t *undobuffer, undobuffer_t *current_undobuffer) op->perform(current_undobuffer, op); } } - if (undobuffer->operations != NULL) { - deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, 0, 0); - } } int diff --git a/src/undo/undobuffer.h b/src/undo/undobuffer.h index de511a343b..dd1cb6b480 100644 --- a/src/undo/undobuffer.h +++ b/src/undo/undobuffer.h @@ -26,8 +26,6 @@ #ifndef undobuffer_h #define undobuffer_h -#include "../playlist.h" - struct _undobuffer_s; typedef struct _undobuffer_s undobuffer_t; diff --git a/src/undo/undomanager.c b/src/undo/undomanager.c index bb8c330a0b..1cab4a7f05 100644 --- a/src/undo/undomanager.c +++ b/src/undo/undomanager.c @@ -25,8 +25,6 @@ #include #include "undomanager.h" -extern DB_functions_t *deadbeef; - struct undomanager_s { undobuffer_t *buffer; char *action_name; @@ -83,17 +81,6 @@ undomanager_consume_buffer (undomanager_t *undomanager) { return buffer; } -static DB_plugin_t * -_plug_get_gui (void) { - struct DB_plugin_s **plugs = deadbeef->plug_get_list (); - for (int i = 0; plugs[i]; i++) { - if (plugs[i]->type == DB_PLUGIN_GUI) { - return plugs[i]; - } - } - return NULL; -} - void undomanager_set_action_name (undomanager_t *undomanager, const char *name) { free (undomanager->action_name); @@ -105,22 +92,3 @@ undomanager_get_action_name (undomanager_t *undomanager) { return undomanager->action_name; } -void -undomanager_flush(undomanager_t *undomanager) { - DB_plugin_t *ui_plugin = _plug_get_gui (); - undobuffer_t *undobuffer = undomanager_consume_buffer (undomanager); - - int has_ui_command = ui_plugin != NULL && ui_plugin->command != NULL; - - int res = -1; - if (undobuffer_has_operations (undobuffer) && has_ui_command) { - ui_plugin->command (110, undomanager); - res = ui_plugin->command (111, undobuffer, undomanager_get_action_name (undomanager)); - } - - if (res != 0) { - undobuffer_free (undobuffer); - } - - undomanager_set_action_name (undomanager, NULL); -} diff --git a/src/undo/undomanager.h b/src/undo/undomanager.h index 109505b029..a368a57e5b 100644 --- a/src/undo/undomanager.h +++ b/src/undo/undomanager.h @@ -42,10 +42,6 @@ undomanager_get_buffer (undomanager_t *undomanager); undobuffer_t * undomanager_consume_buffer (undomanager_t *undomanager); -// Send the accumulated undo buffer to the UI for registration -void -undomanager_flush(undomanager_t *undomanager); - void undomanager_set_action_name (undomanager_t *undomanager, const char *name);