chore: reduce debt with const values

This commit is contained in:
2025-11-10 16:29:11 +01:00
parent 1c737f4ac6
commit 5532cbf53e
20 changed files with 123 additions and 104 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ static void parse_config_file_line(const ConfigFile *config, char *line) {
hashmap_set(config->map, &item);
}
void config_file_read(ConfigFile *config, char *path) {
void config_file_read(ConfigFile *config, const char *path) {
File file;
char *line;
char *rest;
+1 -1
View File
@@ -3,7 +3,7 @@
#ifndef CONFIG_FILE_H
#define CONFIG_FILE_H
void config_file_read(ConfigFile *config, char *path);
void config_file_read(ConfigFile *config, const char *path);
const char *config_file_get_str(const ConfigFile *config, const char *key,
const char *default_value);
+6 -7
View File
@@ -12,7 +12,7 @@
#include "file.h"
#include "string.h"
static time_t get_file_time(File *file) {
static time_t get_file_time(const File *file) {
struct stat attr;
if (stat(file->path, &attr) == 0) {
@@ -22,7 +22,7 @@ static time_t get_file_time(File *file) {
return 0;
}
bool file_should_update(File *file) {
bool file_should_update(const File *file) {
return file->last_write != get_file_time(file);
}
@@ -70,7 +70,7 @@ bool file_update(File *file) {
return true;
}
void file_read(File *file, char *path) {
void file_read(File *file, const char *path) {
strlcpy(file->path, path, STR_LEN);
file->content = NULL;
file->error = false;
@@ -79,7 +79,7 @@ void file_read(File *file, char *path) {
file_update(file);
}
void file_dump(char *path, char *content) {
void file_dump(const char *path, const char *content) {
FILE *file_pointer;
log_info("Dumping %s...", path);
@@ -98,7 +98,7 @@ void file_dump(char *path, char *content) {
fclose(file_pointer);
}
void file_write(char *path, StringArray *lines) {
void file_write(const char *path, const StringArray *lines) {
FILE *file_pointer;
log_info("Writing %s...", path);
@@ -119,9 +119,8 @@ void file_write(char *path, StringArray *lines) {
fclose(file_pointer);
}
void file_free(File *file) {
void file_free(const File *file) {
if (!file->error) {
free(file->content);
file->error = true;
}
}
+5 -5
View File
@@ -3,16 +3,16 @@
#ifndef FILE_H
#define FILE_H
void file_read(File *file, char *path);
void file_read(File *file, const char *path);
bool file_should_update(File *file);
bool file_should_update(const File *file);
bool file_update(File *file);
void file_write(char *path, StringArray *lines);
void file_write(const char *path, const StringArray *lines);
void file_dump(char *path, char *content);
void file_dump(const char *path, const char *content);
void file_free(File *file);
void file_free(const File *file);
#endif /* FILE_H */
+4 -4
View File
@@ -57,7 +57,7 @@ static void compute_fps(bool trace_fps) {
}
}
static void init_context(Parameters *params, unsigned int in_count) {
static void init_context(const Parameters *params, unsigned int in_count) {
state_init(context, &project.state_config, params->demo, params->auto_random,
params->base_tempo, params->state_file, params->load_state);
@@ -82,7 +82,7 @@ static void reload_shader(unsigned int i) {
shaders_update(&program, &project.fragment_shaders[i][0], i);
}
static void init_inputs(StringArray *video_in, unsigned int video_size) {
static void init_inputs(const StringArray *video_in, unsigned int video_size) {
inputs.length = video_in->length;
for (unsigned int i = 0; i < video_in->length; i++) {
@@ -173,7 +173,7 @@ static void loop(bool hr, bool trace_fps) {
window_events();
}
void forge_run(Parameters *params) {
void forge_run(const Parameters *params) {
context = shared_init_context("/" PACKAGE "_context");
context->stop = false;
@@ -195,7 +195,7 @@ void forge_run(Parameters *params) {
midi_open(&midi, config_file_get_str(&project.config, "MIDI_HW", "hw"));
if (midi.error) {
params->demo = true;
context->demo = true;
} else {
trace_midi = params->trace_midi;
+1 -1
View File
@@ -3,6 +3,6 @@
#ifndef FORGE_H
#define FORGE_H
void forge_run(Parameters *params);
void forge_run(const Parameters *params);
#endif /* FORGE_H */
+4 -2
View File
@@ -19,7 +19,8 @@ void midi_open(MidiDevice *device, const char *name) {
log_info("(%s) MIDI open", name);
}
void midi_write(MidiDevice *device, unsigned char code, unsigned char value) {
void midi_write(const MidiDevice *device, unsigned char code,
unsigned char value) {
unsigned char buffer[3];
buffer[0] = 0xB0;
@@ -29,7 +30,8 @@ void midi_write(MidiDevice *device, unsigned char code, unsigned char value) {
snd_rawmidi_write(device->output, buffer, 3);
}
bool midi_background_listen(MidiDevice *device, SharedContext *context,
bool midi_background_listen(const MidiDevice *device,
const SharedContext *context,
void (*event_callback)(unsigned char code,
unsigned char value)) {
pid_t pid;
+4 -2
View File
@@ -4,8 +4,10 @@
#define MIDI_H
void midi_open(MidiDevice *device, const char *name);
void midi_write(MidiDevice *device, unsigned char code, unsigned char value);
bool midi_background_listen(MidiDevice *device, SharedContext *context,
void midi_write(const MidiDevice *device, unsigned char code,
unsigned char value);
bool midi_background_listen(const MidiDevice *device,
const SharedContext *context,
void (*event_callback)(unsigned char code,
unsigned char value));
+3 -2
View File
@@ -76,7 +76,8 @@ static bool read_fragment_shader_file(Project *project, const char *frag_prefix,
return parse_fragment_shader_file(project, i);
}
void project_init(Project *project, char *project_path, char *config_file) {
void project_init(Project *project, const char *project_path,
const char *config_file) {
char config_path[STR_LEN];
const char *frag_prefix;
@@ -134,7 +135,7 @@ void project_reload(Project *project, void (*reload_callback)(unsigned int)) {
}
}
void project_free(Project *project) {
void project_free(const Project *project) {
for (unsigned int i = 0; i < project->frag_count; i++) {
file_free(&project->fragment_shaders[i][0]);
}
+3 -2
View File
@@ -3,10 +3,11 @@
#ifndef PROJECT_H
#define PROJECT_H
void project_init(Project *project, char *project_path, char *config_file);
void project_init(Project *project, const char *project_path,
const char *config_file);
void project_reload(Project *project, void (*reload_callback)(unsigned int));
void project_free(Project *project);
void project_free(const Project *project);
#endif /* PROJECT_H */
+28 -20
View File
@@ -35,7 +35,8 @@ static void init_gl(ShaderProgram *program) {
gladLoadEGL(program->egl_display, glfwGetProcAddress);
}
static void init_textures(ShaderProgram *program, SharedContext *context) {
static void init_textures(ShaderProgram *program,
const SharedContext *context) {
glGenTextures(program->tex_count, program->textures);
for (unsigned int i = 0; i < program->tex_count; i++) {
@@ -110,7 +111,7 @@ static void link_input_to_texture(ShaderProgram *program, VideoCapture *input,
log_info("Texture %d linked to %s", texture_index, input->name);
}
static void init_input(ShaderProgram *program, ConfigFile *config,
static void init_input(ShaderProgram *program, const ConfigFile *config,
VideoCaptureArray *inputs) {
unsigned int tex_i;
char name[STR_LEN];
@@ -126,7 +127,8 @@ static void init_input(ShaderProgram *program, ConfigFile *config,
}
}
static void init_framebuffers(ShaderProgram *program, ConfigFile *config) {
static void init_framebuffers(ShaderProgram *program,
const ConfigFile *config) {
unsigned int tex_i;
char name[STR_LEN];
@@ -182,7 +184,8 @@ static void bind_vertices(ShaderProgram *program, unsigned int index) {
}
}
static bool compile_shader(GLuint shader_id, char *name, char *source_code) {
static bool compile_shader(GLuint shader_id, const char *name,
const char *source_code) {
GLint status_params;
char log[STR_LEN];
@@ -208,7 +211,7 @@ static bool compile_shader(GLuint shader_id, char *name, char *source_code) {
return status_params == GL_TRUE;
}
static void init_shaders(ShaderProgram *program, Project *project) {
static void init_shaders(ShaderProgram *program, const Project *project) {
// compile vertex shader
program->vertex_shader = glCreateShader(GL_VERTEX_SHADER);
program->error |= !compile_shader(
@@ -228,7 +231,8 @@ static void init_shaders(ShaderProgram *program, Project *project) {
}
static void init_single_program(ShaderProgram *program, unsigned int i,
ConfigFile *config, StateConfig *state_config) {
const ConfigFile *config,
const StateConfig *state_config) {
unsigned int index1;
unsigned int index2;
char name[STR_LEN];
@@ -365,15 +369,15 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
log_info("Program %d initialized", i + 1);
}
static void init_programs(ShaderProgram *program, ConfigFile *config,
StateConfig *state_config) {
static void init_programs(ShaderProgram *program, const ConfigFile *config,
const StateConfig *state_config) {
for (unsigned int i = 0; i < program->frag_count; i++) {
init_single_program(program, i, config, state_config);
}
}
void shaders_init(ShaderProgram *program, Project *project,
SharedContext *context, VideoCaptureArray *inputs,
void shaders_init(ShaderProgram *program, const Project *project,
const SharedContext *context, VideoCaptureArray *inputs,
bool rebind) {
if (!rebind) {
program->error = false;
@@ -418,7 +422,7 @@ void shaders_init(ShaderProgram *program, Project *project,
;
}
void shaders_update(ShaderProgram *program, File *fragment_shader,
void shaders_update(ShaderProgram *program, const File *fragment_shader,
unsigned int i) {
bool result;
@@ -432,7 +436,8 @@ void shaders_update(ShaderProgram *program, File *fragment_shader,
}
}
static void update_viewport(ShaderProgram *program, SharedContext *context) {
static void update_viewport(ShaderProgram *program,
const SharedContext *context) {
// viewport changed
if (context->resolution[0] != program->last_resolution[0] ||
context->resolution[1] != program->last_resolution[1]) {
@@ -442,6 +447,8 @@ static void update_viewport(ShaderProgram *program, SharedContext *context) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0],
context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
}
program->last_resolution[0] = context->resolution[0];
program->last_resolution[1] = context->resolution[1];
}
}
@@ -457,21 +464,21 @@ static void write_uniform_1i(GLuint location, unsigned int value) {
}
}
static void write_uniform_2f(GLuint location, vec2 *value) {
static void write_uniform_2f(GLuint location, const vec2 *value) {
if (location != unused_uniform) {
glUniform2fv(location, 1, (const GLfloat *)value);
}
}
static void write_uniform_multi_3f(GLuint location, unsigned int count,
vec3 *value) {
const vec3 *value) {
if (location != unused_uniform) {
glUniform3fv(location, count, (const GLfloat *)value);
}
}
static void use_program(ShaderProgram *program, int i, bool output,
SharedContext *context) {
static void use_program(const ShaderProgram *program, int i, bool output,
const SharedContext *context) {
unsigned int k;
unsigned int offset;
unsigned int subcount;
@@ -569,7 +576,7 @@ static void use_program(ShaderProgram *program, int i, bool output,
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void shaders_compute(ShaderProgram *program, SharedContext *context,
void shaders_compute(ShaderProgram *program, const SharedContext *context,
bool monitor, bool output_only) {
if (!output_only) {
glBindVertexArray(program->vertex_array[0]);
@@ -593,7 +600,7 @@ void shaders_compute(ShaderProgram *program, SharedContext *context,
true, context);
}
void shaders_free(ShaderProgram *program) {
void shaders_free(const ShaderProgram *program) {
for (unsigned int i = 0; i < program->frag_count; i++) {
glDeleteProgram(program->programs[i]);
}
@@ -603,11 +610,12 @@ void shaders_free(ShaderProgram *program) {
glDeleteBuffers(1, &program->vertex_buffer);
}
void shaders_free_window(ShaderProgram *program, bool secondary) {
void shaders_free_window(const ShaderProgram *program, bool secondary) {
glDeleteVertexArrays(1, &program->vertex_array[secondary ? 1 : 0]);
}
void shaders_free_input(ShaderProgram *program, VideoCapture *input) {
void shaders_free_input(const ShaderProgram *program,
const VideoCapture *input) {
if (!input->error && input->dma_image != EGL_NO_IMAGE_KHR) {
eglDestroyImageKHR(program->egl_display, input->dma_image);
}
+8 -7
View File
@@ -3,20 +3,21 @@
#ifndef SHADERS_H
#define SHADERS_H
void shaders_init(ShaderProgram *program, Project *project,
SharedContext *context, VideoCaptureArray *inputs,
void shaders_init(ShaderProgram *program, const Project *project,
const SharedContext *context, VideoCaptureArray *inputs,
bool rebind);
void shaders_update(ShaderProgram *program, File *fragment_shader,
void shaders_update(ShaderProgram *program, const File *fragment_shader,
unsigned int i);
void shaders_compute(ShaderProgram *program, SharedContext *context,
void shaders_compute(ShaderProgram *program, const SharedContext *context,
bool monitor, bool output_only);
void shaders_free(ShaderProgram *program);
void shaders_free(const ShaderProgram *program);
void shaders_free_window(ShaderProgram *program, bool secondary);
void shaders_free_window(const ShaderProgram *program, bool secondary);
void shaders_free_input(ShaderProgram *program, VideoCapture *input);
void shaders_free_input(const ShaderProgram *program,
const VideoCapture *input);
#endif /* SHADERS_H */
+24 -20
View File
@@ -12,7 +12,7 @@
#include "state.h"
#include "tempo.h"
void state_parse_config(StateConfig *state_config, ConfigFile *config) {
void state_parse_config(StateConfig *state_config, const ConfigFile *config) {
unsigned int offset;
unsigned int count;
char name[STR_LEN];
@@ -118,15 +118,16 @@ void state_parse_config(StateConfig *state_config, ConfigFile *config) {
config_file_get_int(config, "TAP_TEMPO", UNSET_MIDI_CODE);
}
static void safe_midi_write(MidiDevice *midi, unsigned int code,
static void safe_midi_write(const MidiDevice *midi, unsigned int code,
unsigned char value) {
if (code != UNSET_MIDI_CODE) {
midi_write(midi, code, value);
}
}
static void update_page(SharedContext *context, StateConfig *state_config,
MidiDevice *midi) {
static void update_page(const SharedContext *context,
const StateConfig *state_config,
const MidiDevice *midi) {
unsigned int page_item_min;
unsigned int page_item_max;
// SHOW PAGE
@@ -155,8 +156,9 @@ static void update_page(SharedContext *context, StateConfig *state_config,
}
}
static void update_active(SharedContext *context, StateConfig *state_config,
MidiDevice *midi) {
static void update_active(const SharedContext *context,
const StateConfig *state_config,
const MidiDevice *midi) {
unsigned int k;
for (unsigned int i = 0; i < state_config->midi_active_counts.length; i++) {
@@ -169,8 +171,9 @@ static void update_active(SharedContext *context, StateConfig *state_config,
}
}
static void update_values(SharedContext *context, StateConfig *state_config,
MidiDevice *midi) {
static void update_values(const SharedContext *context,
const StateConfig *state_config,
const MidiDevice *midi) {
unsigned int j;
unsigned int k;
unsigned int part;
@@ -185,8 +188,8 @@ static void update_values(SharedContext *context, StateConfig *state_config,
}
}
void state_apply_event(SharedContext *context, StateConfig *state_config,
MidiDevice *midi, unsigned char code,
void state_apply_event(SharedContext *context, const StateConfig *state_config,
const MidiDevice *midi, unsigned char code,
unsigned char value, bool trace_midi) {
unsigned int i, j, k, part;
bool found;
@@ -276,8 +279,9 @@ void state_apply_event(SharedContext *context, StateConfig *state_config,
}
}
bool state_background_write(SharedContext *context, StateConfig *state_config,
MidiDevice *midi) {
bool state_background_write(SharedContext *context,
const StateConfig *state_config,
const MidiDevice *midi) {
pid_t pid;
bool beat_active, last_active, change, last_change;
@@ -327,8 +331,8 @@ bool state_background_write(SharedContext *context, StateConfig *state_config,
return false;
}
static void state_load(SharedContext *context, StateConfig *state_config,
char *state_file) {
static void state_load(SharedContext *context, const StateConfig *state_config,
const char *state_file) {
ConfigFile saved_state;
char key[STR_LEN];
@@ -367,9 +371,9 @@ static void state_load(SharedContext *context, StateConfig *state_config,
config_file_free(&saved_state);
}
void state_init(SharedContext *context, StateConfig *state_config, bool demo,
bool auto_random, unsigned int base_tempo, char *state_file,
bool load_state) {
void state_init(SharedContext *context, const StateConfig *state_config,
bool demo, bool auto_random, unsigned int base_tempo,
const char *state_file, bool load_state) {
tempo_init(&context->tempo);
tempo_set(&context->tempo, base_tempo);
context->demo = demo;
@@ -399,14 +403,14 @@ void state_init(SharedContext *context, StateConfig *state_config, bool demo,
}
}
void state_randomize(SharedContext *context, StateConfig *state_config) {
void state_randomize(SharedContext *context, const StateConfig *state_config) {
for (unsigned int i = 0; i < context->state.length; i++) {
context->state.values[i] = rand_uint(state_config->state_max);
}
}
void state_save(SharedContext *context, StateConfig *state_config,
char *state_file) {
void state_save(const SharedContext *context, const StateConfig *state_config,
const char *state_file) {
StringArray lines;
log_info("Saving state to '%s'...", state_file);
+12 -11
View File
@@ -3,22 +3,23 @@
#ifndef STATE_H
#define STATE_H
void state_parse_config(StateConfig *state_config, ConfigFile *config);
void state_parse_config(StateConfig *state_config, const ConfigFile *config);
void state_apply_event(SharedContext *context, StateConfig *state_config,
MidiDevice *midi, unsigned char code,
void state_apply_event(SharedContext *context, const StateConfig *state_config,
const MidiDevice *midi, unsigned char code,
unsigned char value, bool trace_midi);
bool state_background_write(SharedContext *context, StateConfig *state_config,
MidiDevice *midi);
bool state_background_write(SharedContext *context,
const StateConfig *state_config,
const MidiDevice *midi);
void state_init(SharedContext *context, StateConfig *state_config, bool demo,
bool auto_random, unsigned int base_tempo, char *state_file,
bool load_state);
void state_init(SharedContext *context, const StateConfig *state_config,
bool demo, bool auto_random, unsigned int base_tempo,
const char *state_file, bool load_state);
void state_randomize(SharedContext *context, StateConfig *state_config);
void state_randomize(SharedContext *context, const StateConfig *state_config);
void state_save(SharedContext *context, StateConfig *state_config,
char *state_file);
void state_save(const SharedContext *context, const StateConfig *state_config,
const char *state_file);
#endif /* STATE_H */
+4 -4
View File
@@ -33,12 +33,12 @@ void tempo_init(Tempo *tempo) {
reset_tap_chain(tempo, t);
}
static bool is_chain_active(Tempo tempo, long t) {
static bool is_chain_active(const Tempo tempo, long t) {
return (tempo.last_tap + MAX_BEAT_LENGTH) > t &&
(tempo.last_tap + (tempo.beat_length * BEATS_UNTIL_CHAIN_RESET)) > t;
}
static long get_average_tap_duration(Tempo tempo) {
static long get_average_tap_duration(const Tempo tempo) {
unsigned int amount;
long running_total;
long average_tap_duration;
@@ -115,7 +115,7 @@ void tempo_tap(Tempo *tempo) {
add_tap_to_chain(tempo, t);
}
double tempo_total(Tempo *tempo) {
double tempo_total(const Tempo *tempo) {
long t;
t = now();
@@ -123,6 +123,6 @@ double tempo_total(Tempo *tempo) {
return (double)(t - tempo->last_reset) / (double)tempo->beat_length;
}
double tempo_progress(Tempo *tempo, double modulo) {
double tempo_progress(const Tempo *tempo, double modulo) {
return fmod(tempo_total(tempo), modulo);
}
+2 -2
View File
@@ -9,8 +9,8 @@ void tempo_tap(Tempo *tempo);
void tempo_set(Tempo *tempo, float value);
double tempo_total(Tempo *tempo);
double tempo_total(const Tempo *tempo);
double tempo_progress(Tempo *tempo, double modulo);
double tempo_progress(const Tempo *tempo, double modulo);
#endif /* TEMPO_H */
+4 -4
View File
@@ -66,7 +66,7 @@ static void ioctl_error(VideoCapture *video_capture, const char *operation,
video_capture->error = true;
}
static void open_device(VideoCapture *video_capture, char *name) {
static void open_device(VideoCapture *video_capture, const char *name) {
strlcpy(video_capture->name, name, STR_LEN);
video_capture->error = false;
video_capture->fd = -1;
@@ -266,11 +266,11 @@ static void create_image_buffer(VideoCapture *video_capture) {
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf);
}
static void close_stream(VideoCapture *video_capture) {
static void close_stream(const VideoCapture *video_capture) {
ioctl(video_capture->fd, VIDIOC_STREAMOFF, &buf_type);
}
void video_init(VideoCapture *video_capture, char *name,
void video_init(VideoCapture *video_capture, const char *name,
unsigned int preferred_height) {
open_device(video_capture, name);
@@ -363,7 +363,7 @@ bool video_background_read(VideoCapture *video_capture, SharedContext *context,
return false;
}
void video_free(VideoCapture *video_capture) {
void video_free(const VideoCapture *video_capture) {
if (!video_capture->error) {
close_stream(video_capture);
}
+2 -2
View File
@@ -3,12 +3,12 @@
#ifndef VIDEO_H
#define VIDEO_H
void video_init(VideoCapture *video_capture, char *name,
void video_init(VideoCapture *video_capture, const char *name,
unsigned int preferred_height);
bool video_background_read(VideoCapture *video_capture, SharedContext *context,
int input_index, bool trace_fps);
void video_free(VideoCapture *video_capture);
void video_free(const VideoCapture *video_capture);
#endif /* VIDEO_H */
+4 -4
View File
@@ -44,7 +44,7 @@ static GLFWmonitor *get_monitor(unsigned char monitor_index) {
}
static GLFWwindow *
create_window(GLFWmonitor *monitor, char *title, Window *shared_context,
create_window(GLFWmonitor *monitor, const char *title, Window *shared_context,
void (*key_callback)(Window *, int, int, int, int)) {
GLFWwindow *window;
@@ -92,8 +92,8 @@ void window_terminate() {
glfwTerminate();
}
Window *window_init(char *title, unsigned char monitor_index, bool windowed,
Window *shared_context,
Window *window_init(const char *title, unsigned char monitor_index,
bool windowed, Window *shared_context,
void (*key_callback)(Window *, int, int, int, int)) {
GLFWwindow *window;
GLFWmonitor *monitor;
@@ -107,7 +107,7 @@ Window *window_init(char *title, unsigned char monitor_index, bool windowed,
return window;
}
void window_update_title(Window *window, char *title) {
void window_update_title(Window *window, const char *title) {
glfwSetWindowTitle(window, title);
}
+3 -3
View File
@@ -7,11 +7,11 @@ void window_startup(void (*error_callback)(int, const char *));
void window_terminate();
Window *window_init(char *title, unsigned char monitor_index, bool windowed,
Window *shared_context,
Window *window_init(const char *title, unsigned char monitor_index,
bool windowed, Window *shared_context,
void (*key_callback)(Window *, int, int, int, int));
void window_update_title(Window *window, char *title);
void window_update_title(Window *window, const char *title);
double window_get_time();