Skip to content

Commit aa0feb8

Browse files
[#102] Allow file caching policies
1 parent a8a719e commit aa0feb8

24 files changed

Lines changed: 3357 additions & 2838 deletions

doc/CONFIGURATION.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ The `Bool` data type supports the following values: `on`, `1`, `true`, `off`, `0
2222
| device | | String | No | The default device name |
2323
| output | `[%n/%N] %d: %f [%i] (%t/%T) (%p)`| String | No | Defines the console output. Valid expansions are: `%n` (current track number), `%N` (total number of tracks), `%d` (device name), `%f` (file name), `%F` (full path of file), `%i` (file information), `%t` (current time), `%T` (total time), `%p` (percentage), `%b` (ringbuffer current size in Mb), `%B` (ringbuffer maximum size in Mb)|
2424
| volume | -1 | Int | No | The volume in percent. -1 means use current volume |
25-
| cache | 256Mb | Int | No | The cache size |
25+
| cache | 256Mb | Int | No | The cache size. `0` means no caching |
26+
| cache_files | `off` | String | No | File caching policy: `off` only caches the current file, `minimal` caches the previous and next files as well, and `all` caches all files in the playlist |
2627
| log_type | console | String | No | The logging type (console, file, syslog) |
2728
| log_level | info | String | No | The logging level, any of the (case insensitive) strings `FATAL`, `ERROR`, `WARN`, `INFO` and `DEBUG` (that can be more specific as `DEBUG1` thru `DEBUG5`). Debug level greater than 5 will be set to `DEBUG5`. Not recognized values will make the log_level be `INFO` |
2829
| log_path | hrmp.log | String | No | The log file location. Can be a strftime(3) compatible string. |

doc/man/hrmp.conf.5.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ volume
3535
The volume in percent. -1 means use current volume
3636

3737
cache
38-
The cache size. Default is 256Mb
38+
The cache size. 0 means no caching. Default is 256Mb
39+
40+
cache_files
41+
File caching policy: off only caches the current file, minimal caches the previous and next files as well, and all caches all files
42+
in the playlist
3943

4044
log_type
4145
The logging type (console, file, syslog). Default is console

doc/manual/03-configuration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ The `Bool` data type supports the following values: `on`, `yes`, `1`, `true`, `o
2626
| device | | String | No | The default device name |
2727
| output | `[%n/%N] %d: %f [%i] (%t/%T) (%p)`| String | No | Defines the console output. Valid expansions are: `%n` (current track number), `%N` (total number of tracks), `%d` (device name), `%f` (file name), `%F` (full path of file), `%i` (file information), `%t` (current time), `%T` (total time), `%p` (percentage), `%b` (ringbuffer current size in Mb), `%B` (ringbuffer maximum size in Mb)|
2828
| volume | -1 | Int | No | The volume in percent. -1 means use current volume |
29-
| cache | 256Mb | Int | No | The cache size |
29+
| cache | 256Mb | Int | No | The cache size. `0` means no caching |
30+
| cache_files | `off` | String | No | File caching policy: `off` only caches the current file, `minimal` caches the previous and next files as well, and `all` caches all files in the playlist |
3031
| log_type | console | String | No | The logging type (console, file, syslog) |
3132
| log_level | info | String | No | The logging level, any of the (case insensitive) strings `FATAL`, `ERROR`, `WARN`, `INFO` and `DEBUG` (that can be more specific as `DEBUG1` thru `DEBUG5`). Debug level greater than 5 will be set to `DEBUG5`. Not recognized values will make the log_level be `INFO` |
3233
| log_path | hrmp.log | String | No | The log file location. Can be a strftime(3) compatible string. |

src/include/hrmp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ extern "C" {
4343
#define UPDATE_PROCESS_TITLE_MINIMAL 2
4444
#define UPDATE_PROCESS_TITLE_VERBOSE 3
4545

46+
#define HRMP_CACHE_FILES_OFF 0
47+
#define HRMP_CACHE_FILES_MINIMAL 1
48+
#define HRMP_CACHE_FILES_ALL 2
49+
4650
#define DEFAULT_BUFFER_SIZE 131072
4751
#define ALIGNMENT_SIZE 512
4852

@@ -193,6 +197,7 @@ struct configuration
193197
bool is_muted; /**< Is muted */
194198

195199
size_t cache_size; /**< The cache size */
200+
int cache_files; /**< The cache files policy */
196201

197202
bool metadata; /**< Display metadata about files */
198203

src/include/list.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ extern "C" {
2828
#include <stddef.h>
2929

3030
/** @struct list_entry
31-
* Node of a singly linked list of strings
31+
* Node of a singly linked list
3232
*/
3333
struct list;
3434

3535
struct list_entry
3636
{
37-
char value[MAX_PATH]; /**< Stored string value (NUL-terminated) */
37+
void* value; /**< Stored value (owned by the list) */
3838
struct list_entry* next; /**< Pointer to the next entry */
3939
struct list* list; /**< Owning list */
4040
};
4141

4242
/** @struct list
43-
* Singly linked list of strings
43+
* Singly linked list
4444
*/
4545
struct list
4646
{
@@ -64,6 +64,14 @@ hrmp_list_create(struct list** list);
6464
void
6565
hrmp_list_destroy(struct list* list);
6666

67+
/**
68+
* Destroy a list and free all its entries using a custom free function
69+
* @param list The list
70+
* @param free_value Function to free each value (can be NULL)
71+
*/
72+
void
73+
hrmp_list_destroy_with(struct list* list, void (*free_value)(void*));
74+
6775
/**
6876
* Is the list empty
6977
* @param list The list
@@ -89,6 +97,15 @@ hrmp_list_size(const struct list* list);
8997
int
9098
hrmp_list_append(struct list* list, const char* value);
9199

100+
/**
101+
* Append a value to the end of the list (takes ownership)
102+
* @param list The list
103+
* @param value The value to append (owned by list)
104+
* @return 0 if success, otherwise 1
105+
*/
106+
int
107+
hrmp_list_append_owned(struct list* list, void* value);
108+
92109
/**
93110
* Prepend a value to the beginning of the list
94111
* @param list The list

src/include/playback.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,32 @@ struct playback
4848
};
4949

5050
/**
51-
* Play back a file
51+
* Initialize a playback
5252
* @param number The file number
5353
* @param total The total number of files
5454
* @param fm The file metadata
55+
* @param pb The playback
56+
* @return 0 upon success, otherwise 1
57+
*/
58+
int
59+
hrmp_playback_init(int number, int total, struct file_metadata* fm, struct playback** pb);
60+
61+
/**
62+
* Prepare a playback ringbuffer according to cache settings
63+
* @param pb The playback
64+
* @return 0 upon success, otherwise 1
65+
*/
66+
int
67+
hrmp_playback_prepare_ringbuffer(struct playback* pb);
68+
69+
/**
70+
* Play back a file
71+
* @param pb The playback
5572
* @param next Are going forward or backward
5673
* @return 0 upon success, otherwise 1
5774
*/
5875
int
59-
hrmp_playback(int number, int total, struct file_metadata* fm, bool* next);
76+
hrmp_playback(struct playback* pb, bool* next);
6077

6178
#ifdef __cplusplus
6279
}

src/libhrmp/alsa.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#include <stdio.h>
2727
#include <alsa/pcm.h>
2828

29+
static int
30+
find_best_format(struct file_metadata* fm, snd_pcm_format_t* format);
31+
32+
2933
#define MAX_BUFFER_SIZE 131072
3034

31-
static int find_best_format(struct file_metadata* fm, snd_pcm_format_t* format);
3235

3336
int
3437
hrmp_alsa_init_handle(struct file_metadata* fm, snd_pcm_t** handle)
@@ -511,6 +514,7 @@ hrmp_alsa_set_volume(int volume)
511514
return 1;
512515
}
513516

517+
514518
static int
515519
find_best_format(struct file_metadata* fm, snd_pcm_format_t* format)
516520
{
@@ -600,3 +604,4 @@ find_best_format(struct file_metadata* fm, snd_pcm_format_t* format)
600604

601605
return 1;
602606
}
607+

src/libhrmp/cmd.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,7 @@
2121
#include <stdlib.h>
2222
#include <string.h>
2323

24-
static bool
25-
option_requires_arg(char* option_name, cli_option* options, int num_options, bool is_long_option)
26-
{
27-
for (int j = 0; j < num_options; j++)
28-
{
29-
cli_option* option = &options[j];
30-
bool matches = false;
31-
32-
if (is_long_option)
33-
{
34-
matches = (strcmp(option_name, option->long_name) == 0);
35-
}
36-
else
37-
{
38-
matches = (strcmp(option_name, option->short_name) == 0);
39-
}
40-
41-
if (matches)
42-
{
43-
return option->requires_arg;
44-
}
45-
}
46-
return false;
47-
}
24+
static bool option_requires_arg(char* option_name, cli_option* options, int num_options, bool is_long_option);
4825

4926
int
5027
cmd_parse(
@@ -260,3 +237,30 @@ cmd_parse(
260237

261238
return result_count;
262239
}
240+
241+
242+
static bool
243+
option_requires_arg(char* option_name, cli_option* options, int num_options, bool is_long_option)
244+
{
245+
for (int j = 0; j < num_options; j++)
246+
{
247+
cli_option* option = &options[j];
248+
bool matches = false;
249+
250+
if (is_long_option)
251+
{
252+
matches = (strcmp(option_name, option->long_name) == 0);
253+
}
254+
else
255+
{
256+
matches = (strcmp(option_name, option->short_name) == 0);
257+
}
258+
259+
if (matches)
260+
{
261+
return option->requires_arg;
262+
}
263+
}
264+
return false;
265+
}
266+

0 commit comments

Comments
 (0)