|
| 1 | +/* |
| 2 | + DeaDBeeF -- the music player |
| 3 | + Copyright (C) 2009-2026 Oleksiy Yakovenko and other contributors |
| 4 | +
|
| 5 | + This software is provided 'as-is', without any express or implied |
| 6 | + warranty. In no event will the authors be held liable for any damages |
| 7 | + arising from the use of this software. |
| 8 | +
|
| 9 | + Permission is granted to anyone to use this software for any purpose, |
| 10 | + including commercial applications, and to alter it and redistribute it |
| 11 | + freely, subject to the following restrictions: |
| 12 | +
|
| 13 | + 1. The origin of this software must not be misrepresented; you must not |
| 14 | + claim that you wrote the original software. If you use this software |
| 15 | + in a product, an acknowledgment in the product documentation would be |
| 16 | + appreciated but is not required. |
| 17 | +
|
| 18 | + 2. Altered source versions must be plainly marked as such, and must not be |
| 19 | + misrepresented as being the original software. |
| 20 | +
|
| 21 | + 3. This notice may not be removed or altered from any source distribution. |
| 22 | +*/ |
| 23 | + |
| 24 | +#include <stdio.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | +#include <deadbeef/deadbeef.h> |
| 28 | +#include "plugins.h" |
| 29 | +#include "actionhelp.h" |
| 30 | +#include "gettext.h" |
| 31 | + |
| 32 | +static int |
| 33 | +action_alphasort(const void *a, const void *b) |
| 34 | +{ |
| 35 | + const DB_plugin_action_t *A = *(DB_plugin_action_t **)a; |
| 36 | + const DB_plugin_action_t *B = *(DB_plugin_action_t **)b; |
| 37 | + return strcmp(A->title, B->title); |
| 38 | +} |
| 39 | + |
| 40 | +char * |
| 41 | +build_actions_string(void) { |
| 42 | + DB_plugin_t **plugins = plug_get_list(); |
| 43 | + |
| 44 | + const int MAX_DEPTH = 32; |
| 45 | + const int NAME_COLUMN = 40; |
| 46 | + const int INITIAL_BUF = 4096; |
| 47 | + |
| 48 | + /* ---------- dynamic buffer ---------- */ |
| 49 | + size_t buf_capacity = INITIAL_BUF; |
| 50 | + size_t buf_len = 0; |
| 51 | + char *buf = malloc(buf_capacity); |
| 52 | + if (!buf) { |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + buf[0] = 0; |
| 56 | + |
| 57 | + /* ---------- helper to append to buffer ---------- */ |
| 58 | +#define APPEND(...) do { \ |
| 59 | +int needed = snprintf(NULL, 0, __VA_ARGS__); \ |
| 60 | +if (buf_len + needed + 1 > buf_capacity) { \ |
| 61 | +buf_capacity = (buf_len + needed + 1) * 2; \ |
| 62 | +buf = realloc(buf, buf_capacity); \ |
| 63 | +} \ |
| 64 | +buf_len += snprintf(buf + buf_len, buf_capacity - buf_len, __VA_ARGS__); \ |
| 65 | +} while(0) |
| 66 | + |
| 67 | + /* ---------- header ---------- */ |
| 68 | + APPEND("\n"); |
| 69 | + APPEND(_("List of all actions.\n")); |
| 70 | + APPEND(_("Perform actions using \"--action=NAME\" command line argument.\n\n")); |
| 71 | + APPEND(_("Title")); |
| 72 | + int header_pos = 5; |
| 73 | + for (int i = header_pos; i < NAME_COLUMN; i++) { |
| 74 | + APPEND(" "); |
| 75 | + } |
| 76 | + APPEND("| "); |
| 77 | + APPEND(_("Name")); |
| 78 | + APPEND("\n"); |
| 79 | + |
| 80 | + /* ---------- full-width divider ---------- */ |
| 81 | + int total_width = NAME_COLUMN + 40; // approximate total width |
| 82 | + for (int i = 0; i < total_width; i++) { |
| 83 | + APPEND(i == NAME_COLUMN ? "|" : "-"); |
| 84 | + } |
| 85 | + APPEND("\n"); |
| 86 | + |
| 87 | + /* ---------- collect actions ---------- */ |
| 88 | + int capacity = 64, count = 0; |
| 89 | + DB_plugin_action_t **list = malloc(sizeof(DB_plugin_action_t *) * capacity); |
| 90 | + if (!list) { |
| 91 | + free(buf); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + for (int pi = 0; plugins[pi] != NULL; pi++) { |
| 96 | + DB_plugin_t *plugin = plugins[pi]; |
| 97 | + if (!plugin->get_actions) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + DB_plugin_action_t *action = plugin->get_actions(NULL); |
| 101 | + while (action) { |
| 102 | + if (count == capacity) { |
| 103 | + capacity *= 2; |
| 104 | + list = realloc(list, sizeof(DB_plugin_action_t*) * capacity); |
| 105 | + } |
| 106 | + list[count++] = action; |
| 107 | + action = action->next; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (count == 0) { |
| 112 | + free(list); |
| 113 | + return buf; |
| 114 | + } |
| 115 | + |
| 116 | + /* ---------- sort ---------- */ |
| 117 | + qsort(list, count, sizeof(DB_plugin_action_t *), action_alphasort); |
| 118 | + |
| 119 | + /* ---------- tree printing ---------- */ |
| 120 | + char prev_parts[MAX_DEPTH][256]; |
| 121 | + int prev_depth = 0; |
| 122 | + |
| 123 | + for (int ai = 0; ai < count; ai++) { |
| 124 | + const char *src = list[ai]->title; |
| 125 | + char parts[MAX_DEPTH][256]; |
| 126 | + int depth = 0; |
| 127 | + |
| 128 | + /* split path handling escaped \/ */ |
| 129 | + while (*src && depth < MAX_DEPTH) { |
| 130 | + int idx = 0; |
| 131 | + while (*src) { |
| 132 | + if (src[0] == '\\' && src[1] == '/') { |
| 133 | + parts[depth][idx++] = '/'; |
| 134 | + src += 2; |
| 135 | + continue; |
| 136 | + } |
| 137 | + if (*src == '/') { |
| 138 | + break; |
| 139 | + } |
| 140 | + parts[depth][idx++] = *src++; |
| 141 | + } |
| 142 | + parts[depth][idx] = 0; |
| 143 | + depth++; |
| 144 | + if (*src == '/') { |
| 145 | + src++; |
| 146 | + } |
| 147 | + else { |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + int shared = 0; |
| 153 | + while (shared < depth |
| 154 | + && shared < prev_depth |
| 155 | + && strcmp(parts[shared], prev_parts[shared]) == 0) { |
| 156 | + shared++; |
| 157 | + } |
| 158 | + |
| 159 | + for (int level = shared; level < depth; level++) { |
| 160 | + int indent = level * 4; |
| 161 | + for (int i = 0; i < indent; i++) { |
| 162 | + APPEND(" "); |
| 163 | + } |
| 164 | + APPEND("%s", parts[level]); |
| 165 | + int current_pos = indent + (int)strlen(parts[level]); |
| 166 | + if (level == depth - 1) { |
| 167 | + if (current_pos < NAME_COLUMN) { |
| 168 | + for (int i = current_pos; i < NAME_COLUMN; i++) { |
| 169 | + APPEND(" "); |
| 170 | + } |
| 171 | + } else { |
| 172 | + APPEND(" "); |
| 173 | + } |
| 174 | + APPEND("| %s", list[ai]->name); |
| 175 | + } |
| 176 | + APPEND("\n"); |
| 177 | + strcpy(prev_parts[level], parts[level]); |
| 178 | + } |
| 179 | + prev_depth = depth; |
| 180 | + } |
| 181 | + |
| 182 | + free(list); |
| 183 | + return buf; |
| 184 | + |
| 185 | +#undef APPEND |
| 186 | +} |
| 187 | + |
0 commit comments