Fix loading the search paths for DCP files and add XDG_CONFIG_HOME as extra path
This commit is contained in:
112
src/dcp.c
112
src/dcp.c
@@ -3,35 +3,121 @@
|
|||||||
#include <libdng.h>
|
#include <libdng.h>
|
||||||
#include <libmegapixels.h>
|
#include <libmegapixels.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#ifndef SYSCONFDIR
|
#ifndef SYSCONFDIR
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool
|
char *
|
||||||
find_calibration(size_t size, char *conffile, const char *sensor)
|
mprintf(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
char model[512];
|
char *str = NULL;
|
||||||
int model_found = libmegapixels_get_model(sizeof(model), model);
|
va_list args;
|
||||||
if(!model_found)
|
va_start(args, fmt);
|
||||||
return false;
|
va_list args_copy;
|
||||||
static const char *paths[] = {
|
va_copy(args_copy, args);
|
||||||
"config/%s,%s.dcp",
|
int len = vsnprintf(NULL, 0, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
if (len < 0) {
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
str = malloc(len + 1);
|
||||||
|
if (str == NULL) {
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
vsnprintf(str, len + 1, fmt, args_copy);
|
||||||
|
out:
|
||||||
|
va_end(args_copy);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
mread(const char *path, long *size_out)
|
||||||
|
{
|
||||||
|
FILE *src = fopen(path, "r");
|
||||||
|
if (src == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
fseek(src, 0L, SEEK_END);
|
||||||
|
long src_size = ftell(src);
|
||||||
|
rewind(src);
|
||||||
|
char *data = malloc(src_size + 1);
|
||||||
|
if (data == NULL) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
fread(data, src_size, 1, src);
|
||||||
|
fclose(src);
|
||||||
|
data[src_size] = '\0';
|
||||||
|
if (size_out != NULL) {
|
||||||
|
*size_out = src_size;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
find_calibration_by_model(size_t maxlen,
|
||||||
|
char *conffile,
|
||||||
|
char *model,
|
||||||
|
const char *sensor)
|
||||||
|
{
|
||||||
|
const char *home = getenv("HOME");
|
||||||
|
char *config_home = getenv("XDG_CONFIG_HOME");
|
||||||
|
int fch = 0;
|
||||||
|
if ((config_home == NULL || config_home[0] == '\0') && home != NULL) {
|
||||||
|
config_home = mprintf("%s/.config", home);
|
||||||
|
fch = 1;
|
||||||
|
}
|
||||||
|
// Check in XDG_CONFIG_HOME/megapixels/config
|
||||||
|
snprintf(conffile,
|
||||||
|
maxlen,
|
||||||
|
"%s/megapixels/config/%s.conf",
|
||||||
|
config_home,
|
||||||
|
model);
|
||||||
|
if (access(conffile, F_OK) != -1) {
|
||||||
|
if (fch) {
|
||||||
|
free(config_home);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *paths[] = { "config/%s,%s.dcp",
|
||||||
SYSCONFDIR "/megapixels/config/%s,%s.dcp",
|
SYSCONFDIR "/megapixels/config/%s,%s.dcp",
|
||||||
DATADIR "/megapixels/config/%s,%s.dcp",
|
DATADIR "/megapixels/config/%s,%s.dcp",
|
||||||
NULL
|
NULL };
|
||||||
};
|
for (const char *fmt = paths[0]; fmt; fmt++) {
|
||||||
for (const char **fmt = paths; *fmt; fmt++) {
|
snprintf(conffile, maxlen, fmt, model, sensor);
|
||||||
snprintf(conffile, size, *fmt, model, sensor);
|
|
||||||
if (access(conffile, F_OK) != -1) {
|
if (access(conffile, F_OK) != -1) {
|
||||||
printf("Found calibration file at %s\n", conffile);
|
printf("Found calibration file at %s\n", conffile);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("No calibration found for %s,%s\n", model, sensor);
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
find_calibration(size_t maxlen, char *conffile, const char *sensor)
|
||||||
|
{
|
||||||
|
long size = 0;
|
||||||
|
char *compatible = mread("/proc/device-tree/compatible", &size);
|
||||||
|
if (compatible == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *modelptr = compatible;
|
||||||
|
while ((modelptr - compatible) < size) {
|
||||||
|
if (find_calibration_by_model(maxlen, conffile, modelptr, sensor)) {
|
||||||
|
free(compatible);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
modelptr += strlen(modelptr) + 1;
|
||||||
|
}
|
||||||
|
free(compatible);
|
||||||
|
printf("No calibration found %s\n", sensor);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user