diff options
author | waker <wakeroid@gmail.com> | 2012-11-05 19:46:55 +0100 |
---|---|---|
committer | waker <wakeroid@gmail.com> | 2012-11-05 19:46:55 +0100 |
commit | d4132fccbcd079c56b8109ecc15b011e5d1bdee1 (patch) | |
tree | 5c53c9efc9745e42b7c48a525a39334ebed7aaf8 | |
parent | a1d50122035b30e5735ca5db70593fe51028e628 (diff) |
new action API code fixes
-rw-r--r-- | plugins/converter/convgui.c | 54 | ||||
-rw-r--r-- | plugins/gtkui/actions.c | 5 | ||||
-rw-r--r-- | plugins/gtkui/ddbtabstrip.c | 18 | ||||
-rw-r--r-- | plugins/gtkui/hotkeys.c | 2 | ||||
-rw-r--r-- | plugins/gtkui/plcommon.c | 33 | ||||
-rw-r--r-- | plugins/hotkeys/hotkeys.c | 108 | ||||
-rw-r--r-- | plugins/hotkeys/hotkeys.h | 2 |
7 files changed, 168 insertions, 54 deletions
diff --git a/plugins/converter/convgui.c b/plugins/converter/convgui.c index 1c77c4ae..e9a22c28 100644 --- a/plugins/converter/convgui.c +++ b/plugins/converter/convgui.c @@ -332,31 +332,43 @@ on_write_to_source_folder_toggled (GtkToggleButton *togglebutton, gpointer user_data); static gboolean -converter_show_cb (void *ctx) { +converter_show_cb (void *data) { + int ctx = (int)data; converter_ctx_t *conv = malloc (sizeof (converter_ctx_t)); current_ctx = conv; memset (conv, 0, sizeof (converter_ctx_t)); deadbeef->pl_lock (); - // copy list - int nsel = deadbeef->pl_getselcount (); - conv->convert_items_count = nsel; - if (0 < nsel) { - conv->convert_items = malloc (sizeof (DB_playItem_t *) * nsel); - if (conv->convert_items) { - int n = 0; - DB_playItem_t *it = deadbeef->pl_get_first (PL_MAIN); - while (it) { - if (deadbeef->pl_is_selected (it)) { - assert (n < nsel); - deadbeef->pl_item_ref (it); - conv->convert_items[n++] = it; + switch (ctx) { + case DDB_ACTION_CTX_MAIN: + case DDB_ACTION_CTX_SELECTION: + { + // copy list + int nsel = deadbeef->pl_getselcount (); + conv->convert_items_count = nsel; + if (0 < nsel) { + conv->convert_items = malloc (sizeof (DB_playItem_t *) * nsel); + if (conv->convert_items) { + int n = 0; + DB_playItem_t *it = deadbeef->pl_get_first (PL_MAIN); + while (it) { + if (deadbeef->pl_is_selected (it)) { + assert (n < nsel); + deadbeef->pl_item_ref (it); + conv->convert_items[n++] = it; + } + DB_playItem_t *next = deadbeef->pl_get_next (it, PL_MAIN); + deadbeef->pl_item_unref (it); + it = next; + } } - DB_playItem_t *next = deadbeef->pl_get_next (it, PL_MAIN); - deadbeef->pl_item_unref (it); - it = next; } + break; } + case DDB_ACTION_CTX_PLAYLIST: + case DDB_ACTION_CTX_NOWPLAYING: + // TODO: other contexts + break; } deadbeef->pl_unlock (); @@ -433,7 +445,7 @@ converter_show_cb (void *ctx) { } static int -converter_show (DB_plugin_action_t *act, DB_playItem_t *it) { +converter_show (DB_plugin_action_t *act, int ctx) { if (converter_plugin->misc.plugin.version_minor >= 1) { // reload all presets converter_plugin->free_encoder_presets (); @@ -442,7 +454,7 @@ converter_show (DB_plugin_action_t *act, DB_playItem_t *it) { converter_plugin->load_dsp_presets (); } // this can be called from non-gtk thread - gdk_threads_add_idle (converter_show_cb, NULL); + gdk_threads_add_idle (converter_show_cb, (void *)ctx); return 0; } @@ -1424,9 +1436,9 @@ convgui_connect (void) { DB_misc_t plugin = { .plugin.api_vmajor = 1, - .plugin.api_vminor = 4, + .plugin.api_vminor = 5, .plugin.version_major = 1, - .plugin.version_minor = 1, + .plugin.version_minor = 2, .plugin.type = DB_PLUGIN_MISC, #if GTK_CHECK_VERSION(3,0,0) .plugin.name = "Converter GTK3 UI", diff --git a/plugins/gtkui/actions.c b/plugins/gtkui/actions.c index ac74111f..ee7831f6 100644 --- a/plugins/gtkui/actions.c +++ b/plugins/gtkui/actions.c @@ -36,7 +36,10 @@ static void on_actionitem_activate (GtkMenuItem *menuitem, DB_plugin_action_t *action) { - action->callback (action, NULL, DDB_ACTION_CTX_MAIN); + // these actions are always in the MAIN context, or they are coming from new + // plugins, so we don't have to care about the user data for <=1.4 plugins. + // aren't we?.. + action->callback (action, DDB_ACTION_CTX_MAIN); } void diff --git a/plugins/gtkui/ddbtabstrip.c b/plugins/gtkui/ddbtabstrip.c index ed91107e..2c62e0e9 100644 --- a/plugins/gtkui/ddbtabstrip.c +++ b/plugins/gtkui/ddbtabstrip.c @@ -886,12 +886,20 @@ static void on_actionitem_activate (GtkMenuItem *menuitem, DB_plugin_action_t *action) { - ddb_playlist_t *plt = NULL; - if (tab_clicked != -1) { - plt = deadbeef->plt_get_for_idx (tab_clicked); + if (action->flags & DB_ACTION_USING_API_14) { + ddb_playlist_t *plt = NULL; + if (tab_clicked != -1) { + plt = deadbeef->plt_get_for_idx (tab_clicked); + } + typedef int (*action_callback_14_t)(struct DB_plugin_action_s *action, void *userdata); + ((action_callback_14_t)action->callback) (action, plt); + if (plt) { + deadbeef->plt_unref (plt); + } + } + else { + action->callback (action, DDB_ACTION_CTX_PLAYLIST); } - action->callback (action, NULL, DDB_ACTION_CTX_PLAYLIST); - deadbeef->plt_unref (plt); } static GtkWidget* diff --git a/plugins/gtkui/hotkeys.c b/plugins/gtkui/hotkeys.c index 8c769e54..88ef6d27 100644 --- a/plugins/gtkui/hotkeys.c +++ b/plugins/gtkui/hotkeys.c @@ -302,7 +302,7 @@ prefwin_init_hotkeys (GtkWidget *_prefwin) { t = action_tree_append (title, actions_store, &action_main_iter, &iter); gtk_tree_store_set (actions_store, &iter, 0, t, 1, actions->name, 2, DDB_ACTION_CTX_MAIN, -1); } - if (actions->flags & (DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS)) { + if (actions->flags & (DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_CAN_MULTIPLE_TRACKS__DEPRECATED)) { t = action_tree_append (title, actions_store, &action_selection_iter, &iter); gtk_tree_store_set (actions_store, &iter, 0, t, 1, actions->name, 2, DDB_ACTION_CTX_SELECTION, -1); t = action_tree_append (title, actions_store, &action_playlist_iter, &iter); diff --git a/plugins/gtkui/plcommon.c b/plugins/gtkui/plcommon.c index 6f49cce7..1dd43308 100644 --- a/plugins/gtkui/plcommon.c +++ b/plugins/gtkui/plcommon.c @@ -376,7 +376,38 @@ void actionitem_activate (GtkMenuItem *menuitem, DB_plugin_action_t *action) { - action->callback (action, NULL, DDB_ACTION_CTX_SELECTION); + if (action->flags & DB_ACTION_USING_API_14) { + typedef int (*action_callback_14_t)(struct DB_plugin_action_s *action, void *userdata); + // Plugin can handle all tracks by itself + if (action->flags & DB_ACTION_CAN_MULTIPLE_TRACKS__DEPRECATED) + { + ((action_callback_14_t)action->callback) (action, NULL); + return; + } + + // For single-track actions just invoke it with first selected track + if (!(action->flags & DB_ACTION_MULTIPLE_TRACKS)) + { + DB_playItem_t *it = deadbeef->pl_get_for_idx_and_iter (clicked_idx, PL_MAIN); + ((action_callback_14_t)action->callback) (action, it); + deadbeef->pl_item_unref (it); + return; + } + + //We end up here if plugin won't traverse tracks and we have to do it ourselves + DB_playItem_t *it = deadbeef->pl_get_first (PL_MAIN); + while (it) { + if (deadbeef->pl_is_selected (it)) { + ((action_callback_14_t)action->callback) (action, it); + } + DB_playItem_t *next = deadbeef->pl_get_next (it, PL_MAIN); + deadbeef->pl_item_unref (it); + it = next; + } + } + else { + action->callback (action, DDB_ACTION_CTX_SELECTION); + } } #define HOOKUP_OBJECT(component,widget,name) \ diff --git a/plugins/hotkeys/hotkeys.c b/plugins/hotkeys/hotkeys.c index 6ad4e8bf..34ac1e1d 100644 --- a/plugins/hotkeys/hotkeys.c +++ b/plugins/hotkeys/hotkeys.c @@ -54,6 +54,7 @@ typedef struct command_s { int modifier; int ctx; int isglobal; + int is_14_action; // means action is coming from plugin using API 1.4 or less DB_plugin_action_t *action; } command_t; @@ -90,39 +91,95 @@ trim (char* s) return h; } -/* - FIXME: This function has many common code with plcommon.c - and it does full traverse of playlist twice -*/ +typedef int (*action_callback_14_t)(struct DB_plugin_action_s *action, void *userdata); + static void -cmd_invoke_plugin_command (DB_plugin_action_t *action, int ctx) +cmd_invoke_plugin_command (DB_plugin_action_t *action, int ctx, int is_14_action) { - action->callback (action, NULL, ctx); -} + if (is_14_action) { + if (ctx == DDB_ACTION_CTX_MAIN) { + // collect stuff for 1.4 user data -static DB_plugin_action_t * -get_action (const char* command) -{ - DB_plugin_t **plugins = deadbeef->plug_get_list (); - for (int i = 0; plugins[i]; i++) { - DB_plugin_t *p = plugins[i]; - if (p->get_actions) { - DB_plugin_action_t *actions = p->get_actions (NULL); - while (actions) { - if (actions->name && !strcasecmp (command, actions->name)) { - return actions; + // common action + if (action->flags & DB_ACTION_COMMON) + { + ((action_callback_14_t)action->callback) (action, NULL); + return; + } + + // playlist action + if (action->flags & DB_ACTION_PLAYLIST__DEPRECATED) + { + ddb_playlist_t *plt = deadbeef->plt_get_curr (); + if (plt) { + ((action_callback_14_t)action->callback) (action, plt); + deadbeef->plt_unref (plt); + } + return; + } + + int selected_count = 0; + DB_playItem_t *pit = deadbeef->pl_get_first (PL_MAIN); + DB_playItem_t *selected = NULL; + while (pit) { + if (deadbeef->pl_is_selected (pit)) + { + if (!selected) + selected = pit; + selected_count++; + } + DB_playItem_t *next = deadbeef->pl_get_next (pit, PL_MAIN); + deadbeef->pl_item_unref (pit); + pit = next; + } + + //Now we're checking if action is applicable: + + if (selected_count == 0) + { + trace ("No tracks selected\n"); + return; + } + if ((selected_count == 1) && (!(action->flags & DB_ACTION_SINGLE_TRACK))) + { + trace ("Hotkeys: action %s not allowed for single track\n", action->name); + return; + } + if ((selected_count > 1) && (!(action->flags & DB_ACTION_MULTIPLE_TRACKS))) + { + trace ("Hotkeys: action %s not allowed for multiple tracks\n", action->name); + return; + } + + //So, action is allowed, do it. + + if (action->flags & DB_ACTION_CAN_MULTIPLE_TRACKS__DEPRECATED) + { + ((action_callback_14_t)action->callback) (action, NULL); + } + else { + pit = deadbeef->pl_get_first (PL_MAIN); + while (pit) { + if (deadbeef->pl_is_selected (pit)) + { + ((action_callback_14_t)action->callback) (action, pit); + } + DB_playItem_t *next = deadbeef->pl_get_next (pit, PL_MAIN); + deadbeef->pl_item_unref (pit); + pit = next; } - actions = actions->next; } } } - - return NULL; + else { + action->callback (action, ctx); + } } static DB_plugin_action_t * -find_action_by_name (const char *command) { +find_action_by_name (const char *command, int *is_14_action) { // find action with this name, and add to list + *is_14_action = 0; DB_plugin_action_t *actions = NULL; DB_plugin_t **plugins = deadbeef->plug_get_list (); for (int i = 0; plugins[i]; i++) { @@ -131,6 +188,9 @@ find_action_by_name (const char *command) { actions = p->get_actions (NULL); while (actions) { if (actions->name && actions->title && !strcasecmp (actions->name, command)) { + if (p->api_vminor < 5) { + *is_14_action = 1; + } break; // found } actions = actions->next; @@ -189,7 +249,7 @@ read_config (Display *disp) trace ("hotkeys: unexpected eol (action)\n"); goto out; } - cmd_entry->action = find_action_by_name (token); + cmd_entry->action = find_action_by_name (token, &cmd_entry->is_14_action); if (!cmd_entry->action) { trace ("hotkeys: action not found %s\n", token); goto out; @@ -355,7 +415,7 @@ hotkeys_event_loop (void *unused) { (state == commands[ i ].modifier)) { trace ("matches to commands[%d]!\n", i); - cmd_invoke_plugin_command (commands[i].action, commands[i].ctx); + cmd_invoke_plugin_command (commands[i].action, commands[i].ctx, commands->is_14_action); break; } } diff --git a/plugins/hotkeys/hotkeys.h b/plugins/hotkeys/hotkeys.h index d7b56e67..4a26570f 100644 --- a/plugins/hotkeys/hotkeys.h +++ b/plugins/hotkeys/hotkeys.h @@ -25,7 +25,7 @@ typedef struct DB_hotkeys_plugin_s { DB_misc_t misc; const char *(*get_name_for_keycode) (int keycode); void (*reset) (void); - // since plugin version 2.0 + // since plugin version 1.1 DB_plugin_action_t* (*get_action_for_keycombo) (int key, int mods, int isglobal, int *ctx); } DB_hotkeys_plugin_t; |