-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for config file #75
Merged
+206
−30
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fe6a1a5
Add support for config file
columbarius 815e693
logger: hardcode loglevels and add DEFAULT_LOGLEVEL makro
columbarius 596956a
build: add library path workaround for freebsd
columbarius 4273c44
fixup! Add support for config file
columbarius 4205051
fixup! logger: hardcode loglevels and add DEFAULT_LOGLEVEL makro
columbarius bcfe7a8
fixup! fixup! Add support for config file
columbarius 4a35ce4
fixup: config: improve logging
columbarius e9a950b
config: fix const configfile
columbarius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[screencast] | ||
output= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
|
||
#include "logger.h" | ||
|
||
struct config_screencast { | ||
char *output_name; | ||
}; | ||
|
||
struct xdpw_config { | ||
struct config_screencast screencast_conf; | ||
}; | ||
|
||
void print_config(enum LOGLEVEL loglevel, struct xdpw_config *config); | ||
void finish_config(struct xdpw_config *config); | ||
void init_config(const char **configfile, struct xdpw_config *config); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include "config.h" | ||
#include "xdpw.h" | ||
#include "logger.h" | ||
|
||
#include <dictionary.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <iniparser.h> | ||
|
||
void print_config(enum LOGLEVEL loglevel, struct xdpw_config *config) { | ||
logprint(loglevel, "config: outputname %s", config->screencast_conf.output_name); | ||
} | ||
|
||
// NOTE: calling finish_config won't prepare the config to be read again from config file | ||
// with init_config since to pointers and other values won't be reset to NULL, or 0 | ||
void finish_config(struct xdpw_config *config) { | ||
logprint(DEBUG, "config: destroying config"); | ||
|
||
// screencast | ||
free(&config->screencast_conf.output_name); | ||
} | ||
|
||
static void getstring_from_conffile(dictionary *d, | ||
const char *key, char **dest, const char *fallback) { | ||
if (*dest != NULL) { | ||
return; | ||
} | ||
const char *c = iniparser_getstring(d, key, fallback); | ||
if (c == NULL) { | ||
return; | ||
} | ||
// Allow keys without value as default | ||
if (strcmp(c, "") != 0) { | ||
*dest = strdup(c); | ||
} else { | ||
*dest = fallback ? strdup(fallback) : NULL; | ||
} | ||
} | ||
|
||
static bool file_exists(const char *path) { | ||
return path && access(path, R_OK) != -1; | ||
} | ||
|
||
static char *config_path(char *prefix, char *filename) { | ||
if (!prefix || !prefix[0] || !filename || !filename[0]) { | ||
return NULL; | ||
} | ||
|
||
char *config_folder = "xdg-desktop-portal-wlr"; | ||
|
||
size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename); | ||
char *path = calloc(size, sizeof(char)); | ||
snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename); | ||
return path; | ||
} | ||
|
||
static void config_parse_file(const char *configfile, struct xdpw_config *config) { | ||
dictionary *d = iniparser_load(configfile); | ||
if (configfile && !d) { | ||
logprint(ERROR, "config: unable to load config file %s", configfile); | ||
} | ||
|
||
// screencast | ||
getstring_from_conffile(d, "screencast:output_name", &config->screencast_conf.output_name, NULL); | ||
|
||
iniparser_freedict(d); | ||
logprint(DEBUG, "config: config file parsed"); | ||
print_config(DEBUG, config); | ||
} | ||
|
||
static char *get_config_path(void) { | ||
const char *home = getenv("HOME"); | ||
size_t size_fallback = 1 + strlen(home) + strlen("/.config"); | ||
char *config_home_fallback = calloc(size_fallback, sizeof(char)); | ||
snprintf(config_home_fallback, size_fallback, "%s/.config", home); | ||
|
||
char *prefix[4]; | ||
prefix[0] = getenv("XDG_CONFIG_HOME"); | ||
prefix[1] = config_home_fallback; | ||
prefix[2] = SYSCONFDIR "/xdg"; | ||
prefix[3] = SYSCONFDIR; | ||
|
||
char *config[2]; | ||
config[0] = getenv("XDG_CURRENT_DESKTOP"); | ||
config[1] = "config"; | ||
|
||
for (size_t i = 0; i < 4; i++) { | ||
for (size_t j = 0; j < 2; j++) { | ||
char *path = config_path(prefix[i], config[j]); | ||
if (!path) { | ||
continue; | ||
} | ||
logprint(TRACE, "config: trying config file %s", path); | ||
if (file_exists(path)) { | ||
return path; | ||
} | ||
free(path); | ||
} | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
void init_config(const char **configfile, struct xdpw_config *config) { | ||
if (*configfile == NULL) { | ||
*configfile = get_config_path(); | ||
} | ||
|
||
if (*configfile) { | ||
logprint(INFO, "config: using config file %s", *configfile); | ||
} else { | ||
logprint(INFO, "config: no config file found"); | ||
} | ||
config_parse_file(*configfile, config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ static int xdpw_usage(FILE* stream, int rc) { | |
" QUIET, ERROR, WARN, INFO, DEBUG, TRACE\n" | ||
" -o, --output=<name> Select output to capture.\n" | ||
" metadata (performs no conversion).\n" | ||
" -c, --config=<config file> Select config file.\n" | ||
" (default is $XDG_CONFIG_HOME/xdg-desktop-portal-wlr/config)\n" | ||
" -r, --replace Replace a running instance.\n" | ||
" -h, --help Get help (this text).\n" | ||
"\n"; | ||
|
@@ -39,14 +41,16 @@ static int handle_name_lost(sd_bus_message *m, void *userdata, sd_bus_error *ret | |
} | ||
|
||
int main(int argc, char *argv[]) { | ||
const char* output_name = NULL; | ||
enum LOGLEVEL loglevel = ERROR; | ||
struct xdpw_config config = {0}; | ||
const char *configfile = NULL; | ||
enum LOGLEVEL loglevel = DEFAULT_LOGLEVEL; | ||
bool replace = false; | ||
|
||
static const char* shortopts = "l:o:rh"; | ||
static const char* shortopts = "l:o:c:rh"; | ||
static const struct option longopts[] = { | ||
{ "loglevel", required_argument, NULL, 'l' }, | ||
{ "output", required_argument, NULL, 'o' }, | ||
{ "config", required_argument, NULL, 'c' }, | ||
{ "replace", no_argument, NULL, 'r' }, | ||
{ "help", no_argument, NULL, 'h' }, | ||
{ NULL, 0, NULL, 0 } | ||
|
@@ -62,7 +66,10 @@ int main(int argc, char *argv[]) { | |
loglevel = get_loglevel(optarg); | ||
break; | ||
case 'o': | ||
output_name = optarg; | ||
config.screencast_conf.output_name = strdup(optarg); | ||
break; | ||
case 'c': | ||
configfile = strdup(optarg); | ||
break; | ||
case 'r': | ||
replace = true; | ||
|
@@ -75,6 +82,7 @@ int main(int argc, char *argv[]) { | |
} | ||
|
||
init_logger(stderr, loglevel); | ||
init_config(&configfile, &config); | ||
|
||
int ret = 0; | ||
|
||
|
@@ -111,12 +119,13 @@ int main(int argc, char *argv[]) { | |
.screencast_source_types = MONITOR, | ||
.screencast_cursor_modes = HIDDEN | EMBEDDED, | ||
.screencast_version = XDP_CAST_PROTO_VER, | ||
.config = &config, | ||
}; | ||
|
||
wl_list_init(&state.xdpw_sessions); | ||
|
||
xdpw_screenshot_init(&state); | ||
ret = xdpw_screencast_init(&state, output_name); | ||
ret = xdpw_screencast_init(&state); | ||
if (ret < 0) { | ||
logprint(ERROR, "xdpw: failed to initialize screencast"); | ||
goto error; | ||
|
@@ -217,6 +226,8 @@ int main(int argc, char *argv[]) { | |
} | ||
|
||
// TODO: cleanup | ||
finish_config(&config); | ||
free((char *)configfile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something that we need to free can't be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hope i got the const and not const declaration right this time. |
||
|
||
return EXIT_SUCCESS; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for a NULL
d
here, in caseiniparser_load
failed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, if
d
isNULL
iniparser_get* will just return the fallback.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but if iniparser failed to read the file we want a proper error message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added logging, but don't know how to supress the log from iniparser. We could check before and skip it when the config file is null, or just have the line in the log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, which log from iniparser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is the line
iniparser: cannot open (NIL) or path to file
in stderr if iniparser_load fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe don't call
iniparser_load
ifconfigfile == NULL
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved all checks of configfile to the part where we load it with iniparser_load, so we don't have to check for the same things multiple times.