ref: 5faa340a8137b82aaa7039bc57642df300daf3df
parent: 3bcefff865cb638d4635fe5f41b1681c7322541d
author: Jeff Alyanak <jeff@alyanak.ca>
date: Sun Oct 3 07:07:10 EDT 2021
Moved config code to its own module I've also created a struct which holds all the config parameters, including the location of the config file. The defaults are set in the init_config() function.
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
#Set all your object files (the object files of all the .c files in your project, e.g. main.o my_sub_functions.o )
-OBJ = main.o serial.o slip.o command.o write.o render.o ini.o input.o font.o
+OBJ = main.o serial.o slip.o command.o write.o render.o ini.o config.o input.o font.o
#Set any dependant header files so that if they are edited they cause a complete re-compile (e.g. main.h some_subfunctions.h some_definitions_file.h ), or leave blank
-DEPS = serial.h slip.h command.h write.h render.h ini.h input.h
+DEPS = serial.h slip.h command.h write.h render.h ini.h config.h input.h
#Any special libraries you are using in your project (e.g. -lbcm2835 -lrt `pkg-config --libs gtk+-3.0` ), or leave blank
INCLUDES = -lserialport
--- /dev/null
+++ b/config.c
@@ -1,0 +1,37 @@
+// Copyright 2021 Jonne Kokkonen
+// Released under the MIT licence, https://opensource.org/licenses/MIT
+
+#include <SDL2/SDL.h>
+#include "config.h"
+
+config_params_s init_config() {+ config_params_s c;
+
+ c.filename = "config.ini"; // default config file to load
+ c.init_fullscreen = 0; // default fullscreen state at load
+
+ return c;
+}
+
+// Read config
+void read_config(struct config_params_s *conf) {+ // Load the config and read the fullscreen setting from the graphics section
+ ini_t *ini = ini_load(conf->filename);
+ if (ini == NULL) {+ return;
+ }
+
+ conf->init_fullscreen = read_fullscreen(ini);
+
+ // Frees the mem used for the config
+ ini_free(ini);
+}
+
+int read_fullscreen(ini_t *config) {+ const char *param = ini_get(config, "graphics", "fullscreen");
+ // This obviously requires the parameter to be a lowercase true to enable fullscreen
+ if ( strcmp(param, "true") == 0 ) {+ return 1;
+ }
+ else return 0;
+}
\ No newline at end of file
--- /dev/null
+++ b/config.h
@@ -1,0 +1,19 @@
+// Copyright 2021 Jonne Kokkonen
+// Released under the MIT licence, https://opensource.org/licenses/MIT
+
+#ifndef CONFIG_H_
+#define CONFIG_H_
+
+#include "ini.h"
+
+typedef struct config_params_s {+ char *filename;
+ int init_fullscreen;
+} config_params_s;
+
+
+config_params_s init_config();
+void read_config();
+int read_fullscreen(ini_t *config);
+
+#endif
\ No newline at end of file
--- a/config.ini
+++ b/config.ini
@@ -1,3 +1,3 @@
[graphics]
; set this to true to have m8c start fullscreen
-fullscreen=false
+fullscreen=true
--- a/main.c
+++ b/main.c
@@ -8,7 +8,7 @@
#include <unistd.h>
#include "command.h"
-#include "ini.h"
+#include "config.h"
#include "input.h"
#include "render.h"
#include "serial.h"
@@ -24,32 +24,14 @@
// Handles CTRL+C / SIGINT
void intHandler(int dummy) { run = 0; }-// Config variables
-int init_fullscreen = 0;
-
-// Read config
-void read_config() {- // Load the config and read the fullscreen setting from the graphics section
- ini_t *config = ini_load("config.ini");- if (config == NULL) {- return;
- }
-
- const char *setting_fullscren = ini_get(config, "graphics", "fullscreen");
- // Other settings could be read here, too.
-
- // This obviously requires the parameter to be a lowercase true to enable fullscreen
- if ( strcmp(setting_fullscren, "true") == 0 ) {- init_fullscreen = 1;
- }
-
- // Frees the mem used for the config
- ini_free(config);
-}
-
int main(int argc, char *argv[]) {- read_config();
+ // Initialize the config to defaults read in the params from the
+ // configfile if present
+ config_params_s conf = init_config();
+ // TODO: take cli parameter to override default configfile location
+ read_config(&conf);
+
// allocate memory for serial buffer
uint8_t *serial_buf = malloc(serial_read_size);
@@ -79,7 +61,7 @@
if (enable_and_reset_display(port) == -1)
run = 0;
- if (initialize_sdl(init_fullscreen) == -1)
+ if (initialize_sdl(conf.init_fullscreen) == -1)
run = 0;
uint8_t prev_input = 0;
--
⑨