Skip to content

Commit

Permalink
undo: renaming and gtkui work
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Feb 1, 2024
1 parent fca851f commit 66795ec
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 180 deletions.
2 changes: 1 addition & 1 deletion plugins/cocoaui/Undo/DdbUndoBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface DdbUndoBuffer : NSObject

- (instancetype)initWithUndoBuffer:(undobuffer_t *)buffer;
- (instancetype)initWithUndoBuffer:(ddb_undobuffer_t *)buffer;
- (void)apply;

@end
Expand Down
8 changes: 4 additions & 4 deletions plugins/cocoaui/Undo/DdbUndoBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@

@interface DdbUndoBuffer()

@property (nonatomic) undobuffer_t *buffer;
@property (nonatomic) ddb_undobuffer_t *buffer;

@end

@implementation DdbUndoBuffer

- (void)dealloc {
undobuffer_free (_buffer);
ddb_undobuffer_free (_buffer);
}

- (instancetype)initWithUndoBuffer:(undobuffer_t *)buffer {
- (instancetype)initWithUndoBuffer:(ddb_undobuffer_t *)buffer {
self = [super init];
_buffer = buffer;
return self;
}

- (void)apply {
undobuffer_execute(self.buffer, undomanager_get_buffer(undomanager_shared()));
ddb_undobuffer_execute(self.buffer, ddb_undomanager_get_buffer(ddb_undomanager_shared()));

deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, 0, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/cocoaui/Undo/NSUndoManager+DdbUndoBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ - (void)registerUndoBuffer:(DdbUndoBuffer *)undoBuffer {
[target apply];
[DdbUndoBufferRetainer.shared releaseBuffer:target];

undobuffer_t *undobuffer = undomanager_consume_buffer(undomanager_shared());
ddb_undobuffer_t *undobuffer = ddb_undomanager_consume_buffer(ddb_undomanager_shared());
DdbUndoBuffer *wrappedBuffer = [[DdbUndoBuffer alloc] initWithUndoBuffer:undobuffer];
[self registerUndoBuffer:wrappedBuffer];
}];
Expand Down
2 changes: 1 addition & 1 deletion plugins/cocoaui/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int cocoaui_command (int command, ...) {
// register undo
va_list args;
va_start(args, command);
undobuffer_t *undobuffer = va_arg(args, undobuffer_t *);
ddb_undobuffer_t *undobuffer = va_arg(args, ddb_undobuffer_t *);
const char *name = va_arg(args, const char *);
va_end(args);

Expand Down
86 changes: 52 additions & 34 deletions plugins/gtkui/fileman.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ gtkui_add_dirs (GSList *lst) {

gtkui_dispatch_on_main(^{
ddb_playItem_t *tail = deadbeef->plt_get_tail_item(plt_curr, PL_MAIN);
deadbeef->undo_set_action_name ("Add Folders");
deadbeef->undo_set_action_name (_("Add Folders"));
deadbeef->plt_move_all_items (plt_curr, plt, tail);
if (tail != NULL) {
deadbeef->pl_item_unref (tail);
Expand Down Expand Up @@ -122,7 +122,7 @@ gtkui_add_files (struct _GSList *lst) {
gtkpl_add_files (plt, lst);
gtkui_dispatch_on_main (^{
ddb_playItem_t *tail = deadbeef->plt_get_tail_item (plt_curr, PL_MAIN);
deadbeef->undo_set_action_name ("Add Files");
deadbeef->undo_set_action_name (_("Add Files"));
deadbeef->plt_move_all_items (plt_curr, plt, tail);
if (tail != NULL) {
deadbeef->pl_item_unref (tail);
Expand Down Expand Up @@ -194,13 +194,13 @@ gtkui_add_location (const char *path, const char *custom_title) {
}

ddb_playItem_t *tail = deadbeef->plt_get_tail_item (plt_curr, PL_MAIN);
deadbeef->undo_set_action_name ("Add Files");
deadbeef->plt_move_all_items (plt_curr, plt, tail);
if (tail != NULL) {
deadbeef->pl_item_unref (tail);
}

deadbeef->plt_save_config (plt_curr);
deadbeef->undo_set_action_name(_("Add Location"));
deadbeef->plt_add_files_end (plt, 0);

free (path_copy);
Expand Down Expand Up @@ -254,28 +254,18 @@ strcopy_special (char *dest, const char *src, int len) {
*dest = 0;
}

static gboolean
set_dnd_cursor_idle (gpointer data) {
if (!data) {
static void
set_dnd_cursor (ddb_playItem_t *first) {
if (first == NULL) {
deadbeef->pl_set_cursor (PL_MAIN, -1);
deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
return FALSE;
return;
}
int cursor = deadbeef->pl_get_idx_of (DB_PLAYITEM (data));
int cursor = deadbeef->pl_get_idx_of (first);
deadbeef->pl_set_cursor (PL_MAIN, cursor);
deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
return FALSE;
}

void
gtkpl_add_fm_dropped_files (DB_playItem_t *drop_before, char *ptr, int length) {
ddb_playlist_t *plt = deadbeef->plt_get_curr ();
if (deadbeef->plt_add_files_begin (plt, 0) < 0) {
free (ptr);
deadbeef->plt_unref (plt);
return;
}

static ddb_playItem_t *
gtkpl_add_fm_dropped_files (ddb_playlist_t *plt, DB_playItem_t *drop_before, const char *ptr, int length) {
DdbListviewIter first = NULL;
DdbListviewIter after = NULL;
if (drop_before) {
Expand All @@ -293,8 +283,6 @@ gtkpl_add_fm_dropped_files (DB_playItem_t *drop_before, char *ptr, int length) {
if (pe - p < 4096 && pe - p > 7) {
char fname[(int)(pe - p)+1];
strcopy_special (fname, (const char *)p, (int)(pe-p));
//strncpy (fname, p, pe - p);
//fname[pe - p] = 0;
int abort = 0;
DdbListviewIter inserted = deadbeef->plt_insert_dir2 (0, plt, after, fname, &abort, NULL, NULL);
if (!inserted && !abort) {
Expand Down Expand Up @@ -323,12 +311,11 @@ gtkpl_add_fm_dropped_files (DB_playItem_t *drop_before, char *ptr, int length) {
if (after) {
deadbeef->pl_item_unref (after);
}
free (ptr);

deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_save_config (plt);
deadbeef->plt_unref (plt);
g_idle_add (set_dnd_cursor_idle, first);
if (first != NULL) {
deadbeef->pl_item_ref (first);
}
return first;
}

struct fmdrop_data {
Expand All @@ -339,24 +326,55 @@ struct fmdrop_data {

void
gtkui_receive_fm_drop (DB_playItem_t *before, char *mem, int length) {
struct fmdrop_data *data = malloc (sizeof (struct fmdrop_data));
if (!data) {
fprintf (stderr, "gtkui_receive_fm_drop: malloc failed\n");
return;
}
struct fmdrop_data *data = calloc (1, sizeof (struct fmdrop_data));
data->mem = mem;
data->length = length;
if (before) {
deadbeef->pl_item_ref (before);
}
data->drop_before = before;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
gtkpl_add_fm_dropped_files (data->drop_before, data->mem, data->length);
ddb_playlist_t *plt = deadbeef->plt_alloc ("receive-drag-drop");
ddb_playlist_t *plt_curr = deadbeef->plt_get_curr ();

if (deadbeef->plt_add_files_begin (plt, 0) < 0) {
if (data->drop_before) {
deadbeef->pl_item_unref (data->drop_before);
}
free (mem);
free (data);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
return;
}


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
ddb_playItem_t *first = gtkpl_add_fm_dropped_files (plt, data->drop_before, data->mem, data->length);

gtkui_dispatch_on_main(^{
ddb_playItem_t *tail = deadbeef->plt_get_tail_item(plt_curr, PL_MAIN);
deadbeef->undo_set_action_name(_("Drag & Drop"));
deadbeef->plt_move_all_items (plt_curr, plt, tail);
if (tail != NULL) {
deadbeef->pl_item_unref (tail);
}

deadbeef->plt_save_config (plt_curr);
deadbeef->plt_add_files_end (plt, 0);
set_dnd_cursor (first);

if (first != NULL) {
deadbeef->pl_item_unref (first);
}
if (data->drop_before) {
deadbeef->pl_item_unref (data->drop_before);
}
free (mem);
free (data);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
});
});
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/gtkui/gtkui.c
Original file line number Diff line number Diff line change
Expand Up @@ -2314,10 +2314,10 @@ int _gtkui_command (int command, ...) {
if (command == 110) { // init with undomanager
va_list args;
va_start (args, command);
undomanager_t *undomanager = va_arg (args, undomanager_t *);
ddb_undomanager_t *undomanager = va_arg (args, ddb_undomanager_t *);
va_end (args);

undomanager_shared_init (undomanager);
ddb_undomanager_shared_init (undomanager);

return 0;
}
Expand All @@ -2326,7 +2326,7 @@ int _gtkui_command (int command, ...) {
// register undo buffer
va_list args;
va_start (args, command);
undobuffer_t *undobuffer = va_arg (args, undobuffer_t *);
ddb_undobuffer_t *undobuffer = va_arg (args, ddb_undobuffer_t *);
const char *name = va_arg (args, const char *);
va_end (args);

Expand Down
14 changes: 7 additions & 7 deletions plugins/gtkui/undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct undo_item_s undo_item_t;

struct undo_item_s {
char *action_name;
undobuffer_t *undobuffer;
ddb_undobuffer_t *undobuffer;
undo_item_t *prev;
undo_item_t *next;
};
Expand All @@ -60,7 +60,7 @@ static undo_state_t _state;

static void
_free_item (undo_item_t *item) {
undobuffer_free(item->undobuffer);
ddb_undobuffer_free(item->undobuffer);
free (item->action_name);
free (item);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ _pop_last (undo_item_t **head, undo_item_t **tail) {
}

void
gtkui_undo_append_buffer (undobuffer_t *undobuffer, const char *action_name) {
gtkui_undo_append_buffer (ddb_undobuffer_t *undobuffer, const char *action_name) {
if (_state.type == none) {
// discard all redo operations
_free_item_list (_state.redo_head);
Expand Down Expand Up @@ -147,15 +147,15 @@ _perform_undo_redo (undo_type_t type) {
return;
}

undomanager_t *undomanager = undomanager_shared ();
undobuffer_t *new_buffer = undomanager_get_buffer (undomanager);
ddb_undomanager_t *undomanager = ddb_undomanager_shared ();
ddb_undobuffer_t *new_buffer = ddb_undomanager_get_buffer (undomanager);

// pop last undo item and execute in undo mode
_pop_last (head, tail);

_state.type = type;
undobuffer_execute(item->undobuffer, new_buffer);
undomanager_set_action_name(undomanager, item->action_name);
ddb_undobuffer_execute(item->undobuffer, new_buffer);
ddb_undomanager_set_action_name(undomanager, item->action_name);
deadbeef->undo_process();
_free_item (item);
_state.type = none;
Expand Down
2 changes: 1 addition & 1 deletion plugins/gtkui/undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void
gtkui_undo_deinit (void);

void
gtkui_undo_append_buffer (undobuffer_t *undobuffer, const char *action_name);
gtkui_undo_append_buffer (ddb_undobuffer_t *undobuffer, const char *action_name);

void
gtkui_perform_undo (void);
Expand Down
42 changes: 21 additions & 21 deletions shared/undo/undobuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
#include <stdlib.h>
#include "undobuffer.h"

struct _undobuffer_s {
undo_operation_t *operations;
struct _ddb_undobuffer_s {
ddb_undo_operation_t *operations;
int enabled;
int grouping;
};

undobuffer_t *
undobuffer_alloc(void) {
undobuffer_t *undobuffer = calloc (1, sizeof (undobuffer_t));
ddb_undobuffer_t *
ddb_undobuffer_alloc(void) {
ddb_undobuffer_t *undobuffer = calloc (1, sizeof (ddb_undobuffer_t));
undobuffer->enabled = 1;
return undobuffer;
}

static void
_undobuffer_free_operations(undobuffer_t *undobuffer, undo_operation_t **operations) {
for (undo_operation_t *op = *operations; op;) {
undo_operation_t *next = op->next;
_undobuffer_free_operations(ddb_undobuffer_t *undobuffer, ddb_undo_operation_t **operations) {
for (ddb_undo_operation_t *op = *operations; op;) {
ddb_undo_operation_t *next = op->next;

if (op->deinit) {
op->deinit (op);
Expand All @@ -55,62 +55,62 @@ _undobuffer_free_operations(undobuffer_t *undobuffer, undo_operation_t **operati
}

void
undobuffer_free (undobuffer_t *undobuffer) {
ddb_undobuffer_free (ddb_undobuffer_t *undobuffer) {
_undobuffer_free_operations(undobuffer, &undobuffer->operations);
free (undobuffer);
}

void
undobuffer_set_enabled (undobuffer_t *undobuffer, int enabled) {
ddb_undobuffer_set_enabled (ddb_undobuffer_t *undobuffer, int enabled) {
undobuffer->enabled = enabled;
}

int
undobuffer_is_enabled (undobuffer_t *undobuffer) {
ddb_undobuffer_is_enabled (ddb_undobuffer_t *undobuffer) {
return undobuffer->enabled;
}

static void
_undobuffer_append_operation(undobuffer_t *undobuffer, undo_operation_t *op, undo_operation_t **operations) {
_undobuffer_append_operation(ddb_undobuffer_t *undobuffer, ddb_undo_operation_t *op, ddb_undo_operation_t **operations) {
op->next = *operations;
*operations = op;
}

void
undobuffer_append_operation (undobuffer_t *undobuffer, undo_operation_t *op) {
ddb_undobuffer_append_operation (ddb_undobuffer_t *undobuffer, ddb_undo_operation_t *op) {
_undobuffer_append_operation(undobuffer, op, &undobuffer->operations);
}

void
undobuffer_execute (undobuffer_t *undobuffer, undobuffer_t *current_undobuffer) {
for (undo_operation_t *op = undobuffer->operations; op != NULL; op = op->next) {
ddb_undobuffer_execute (ddb_undobuffer_t *undobuffer, ddb_undobuffer_t *current_undobuffer) {
for (ddb_undo_operation_t *op = undobuffer->operations; op != NULL; op = op->next) {
if (op->perform != NULL) {
op->perform(current_undobuffer, op);
}
}
}

int
undobuffer_has_operations(undobuffer_t *undobuffer) {
ddb_undobuffer_has_operations(ddb_undobuffer_t *undobuffer) {
return undobuffer->operations != NULL;
}

void
undobuffer_group_begin (undobuffer_t *undobuffer) {
ddb_undobuffer_group_begin (ddb_undobuffer_t *undobuffer) {
undobuffer->grouping = 1;
}

void
undobuffer_group_end (undobuffer_t *undobuffer) {
ddb_undobuffer_group_end (ddb_undobuffer_t *undobuffer) {
undobuffer->grouping = 0;
}

int
undobuffer_is_grouping (undobuffer_t *undobuffer) {
ddb_undobuffer_is_grouping (ddb_undobuffer_t *undobuffer) {
return undobuffer->grouping;
}

undo_operation_t *
undobuffer_get_current_operation (undobuffer_t *undobuffer) {
ddb_undo_operation_t *
ddb_undobuffer_get_current_operation (ddb_undobuffer_t *undobuffer) {
return undobuffer->grouping ? undobuffer->operations : NULL;
}
Loading

0 comments on commit 66795ec

Please sign in to comment.