Skip to content

Commit

Permalink
undo: refactor the usage of deadbeef API calls from the undo system
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Feb 1, 2024
1 parent ffb6851 commit a2b9f7c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 47 deletions.
3 changes: 1 addition & 2 deletions include/deadbeef/deadbeef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions plugins/cocoaui/Undo/DdbUndoBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#import "DdbUndoBuffer.h"
#include "undomanager.h"
#include <deadbeef/deadbeef.h>

extern DB_functions_t *deadbeef;

@interface DdbUndoBuffer()

Expand All @@ -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
7 changes: 6 additions & 1 deletion plugins/gtkui/undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
3. This notice may not be removed or altered from any source distribution.
*/

#include <deadbeef/deadbeef.h>
#include <stdlib.h>
#include <string.h>
#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;

Expand Down Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion src/plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/undo/undo_playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions src/undo/undobuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <stdlib.h>
#include "undobuffer.h"

extern DB_functions_t *deadbeef;

struct _undobuffer_s {
undo_operation_t *operations;
int enabled;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/undo/undobuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#ifndef undobuffer_h
#define undobuffer_h

#include "../playlist.h"

struct _undobuffer_s;

typedef struct _undobuffer_s undobuffer_t;
Expand Down
32 changes: 0 additions & 32 deletions src/undo/undomanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <string.h>
#include "undomanager.h"

extern DB_functions_t *deadbeef;

struct undomanager_s {
undobuffer_t *buffer;
char *action_name;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
4 changes: 0 additions & 4 deletions src/undo/undomanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit a2b9f7c

Please sign in to comment.