Browse Source

Change up config lookup and add documentation for it

Martijn Braam 4 years ago
parent
commit
4fb0a5b133
3 changed files with 74 additions and 1 deletions
  1. 34 0
      README.md
  2. 33 1
      main.c
  3. 7 0
      meson.build

+ 34 - 0
README.md

@@ -14,3 +14,37 @@ $ sudo ninja install
 # Developing
 
 See the mailing list and issue tracker on https://sr.ht/~martijnbraam/Megapixels/
+
+# Config
+
+Megapixels checks multiple locations for it's configuration file and uses the first one it finds.
+As first step it will get the first compatible name in the device tree, in the case of a PinePhone
+this might be "pine64,pinephone-1.2". Then that dtname will be used as the filename in the search
+path in this order:
+
+* $XDG_CONFIG_DIR/megapixels/config/$dtname.ini
+* ~/.config/megapixels/config/$dtname.ini
+* /etc/megapixels/config/$dtname.ini
+* /usr/share/megapixels/config/$dtname.ini
+
+The files in /usr/share/megapixels should be the config files distributed in this repository. The other
+locations allow the user or distribution to override config.
+
+## Config file format
+
+Configuration files are INI format files. 
+
+### [device]
+
+This provides global info, currently only the `csi` key exists, telling megapixels which device in the 
+media-ctl tree is the interface to the kernel. This should provide the /dev/video* node.
+
+### [rear] and [front]
+
+These are the sections describing the sensors.
+
+* `driver=ov5640` the name of the media node that provides the sensor and it's /dev/v4l-subdev* node.
+* `width=640` and `height=480` the resolution to use for the sensor
+* `rate=15` the refresh rate in fps to use for the sensor
+* `fmt=BGGR8` sets the pixel and bus formats used when capturing from the sensor, only BGGR8 is fully supported
+* `rotate=90` the rotation angle to make the sensor match the screen

+ 33 - 1
main.c

@@ -12,7 +12,9 @@
 #include <linux/kdev_t.h>
 #include <sys/sysmacros.h>
 #include <asm/errno.h>
+#include <wordexp.h>
 #include <gtk/gtk.h>
+#include "config.h"
 #include "ini.h"
 #include "bayer.h"
 #include "quickdebayer.h"
@@ -847,17 +849,45 @@ int
 find_config(char *conffile)
 {
 	char buf[512];
+	char *xdg_config_home;
+	wordexp_t exp_result;
 	FILE *fp;
 
+	// Resolve XDG stuff
+	if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
+		xdg_config_home = "~/.config";
+	}
+	wordexp(xdg_config_home, &exp_result, 0);
+	xdg_config_home = strdup(exp_result.we_wordv[0]);
+	wordfree(&exp_result);
+
 	if(access("/proc/device-tree/compatible", F_OK) != -1) {
+		// Reads to compatible string of the current device tree, looks like:
+		// pine64,pinephone-1.2\0allwinner,sun50i-a64\0
 		fp = fopen("/proc/device-tree/compatible", "r");
 		fgets(buf, 512, fp);
 		fclose(fp);
-		sprintf(conffile, "/usr/share/megapixels/config/%s.ini", buf);
+
+		// Check for a config file in XDG_CONFIG_HOME
+		sprintf(conffile, "%s/megapixels/config/%s.ini", xdg_config_home, buf);
 		if(access(conffile, F_OK) != -1) {
 			printf("Found config file at %s\n", conffile);
 			return 0;
 		}
+
+		// Check user overridden /etc/megapixels/config/$dt.ini
+		sprintf(conffile, "%s/megapixels/config/%s.ini", SYSCONFDIR, buf);
+		if(access(conffile, F_OK) != -1) {
+			printf("Found config file at %s\n", conffile);
+			return 0;
+		}
+		// Check packaged /usr/share/megapixels/config/$dt.ini
+		sprintf(conffile, "%s/megapixels/config/%s.ini", DATADIR, buf);
+		if(access(conffile, F_OK) != -1) {
+			printf("Found config file at %s\n", conffile);
+			return 0;
+		}
+		// Check config/%dt.ini in the current working directory
 		sprintf(conffile, "config/%s.ini", buf);
 		if(access(conffile, F_OK) != -1) {
 			printf("Found config file at %s\n", conffile);
@@ -867,6 +897,8 @@ find_config(char *conffile)
 	} else {
 		printf("Could not read device name from device tree\n");
 	}
+
+	// If all else fails, fall back to /etc/megapixels.ini
 	conffile = "/etc/megapixels.ini";
 	if(access(conffile, F_OK) != -1) {
 		printf("Found config file at %s\n", conffile);

+ 7 - 0
meson.build

@@ -7,6 +7,13 @@ libm = cc.find_library('m', required: false)
 
 resources = gnome.compile_resources('megapixels-resources', 'org.postmarketos.Megapixels.gresource.xml')
 
+conf = configuration_data()
+conf.set_quoted('DATADIR', join_paths(get_option('prefix'), get_option('datadir')))
+conf.set_quoted('SYSCONFDIR', get_option('sysconfdir'))
+configure_file(
+  output: 'config.h',
+  configuration: conf )
+
 executable('megapixels', 'main.c', 'ini.c', 'bayer.c', 'quickdebayer.c', resources, dependencies : [gtkdep, libm], install : true)
 
 install_data(['org.postmarketos.Megapixels.desktop'],