Skip to content

Commit

Permalink
Add support for config file
Browse files Browse the repository at this point in the history
  • Loading branch information
columbarius committed Dec 30, 2020
1 parent 727f13f commit 556788d
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 14 deletions.
1 change: 1 addition & 0 deletions .builds/alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ packages:
- pipewire-dev
- wayland-dev
- wayland-protocols
- iniparser-dev
sources:
- https://github.com/emersion/xdg-desktop-portal-wlr
tasks:
Expand Down
1 change: 1 addition & 0 deletions .builds/archlinux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ packages:
- wayland
- wayland-protocols
- pipewire
- iniparser
sources:
- https://github.com/emersion/xdg-desktop-portal-wlr
tasks:
Expand Down
6 changes: 6 additions & 0 deletions contrib/config.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[logger]
level=ERROR
logfile=stderr

[screencast]
output=
29 changes: 29 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef CONFIG_H
#define CONFIG_H

#include "logger.h"

struct config_general {
char *configfile;
};

struct config_logger {
char *loglevel;
char *logfile;
};

struct config_screencast {
char *output_name;
};

struct xdpw_config {
struct config_general conf;
struct config_logger logger_conf;
struct config_screencast screencast_conf;
};

void print_config(struct xdpw_config *config);
void destroy_config(struct xdpw_config *config);
void init_config(struct xdpw_config *config);

#endif
2 changes: 2 additions & 0 deletions include/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct logger_properties {
FILE *dst;
};

FILE* open_logger_file(char *file_name);
void destroy_logger();
void init_logger(FILE *dst, enum LOGLEVEL level);
enum LOGLEVEL get_loglevel(const char *level);
void logprint(enum LOGLEVEL level, char *msg, ...);
Expand Down
3 changes: 0 additions & 3 deletions include/screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ struct xdpw_screencast_context {
struct zxdg_output_manager_v1* xdg_output_manager;
struct wl_shm *shm;

// cli options
const char *output_name;

// sessions
struct wl_list screencast_instances;
};
Expand Down
4 changes: 3 additions & 1 deletion include/xdpw.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include "screencast_common.h"
#include "config.h"

struct xdpw_state {
struct wl_list xdpw_sessions;
Expand All @@ -19,6 +20,7 @@ struct xdpw_state {
uint32_t screencast_source_types; // bitfield of enum source_types
uint32_t screencast_cursor_modes; // bitfield of enum cursor_modes
uint32_t screencast_version;
struct xdpw_config *config;
};

struct xdpw_request {
Expand All @@ -39,7 +41,7 @@ enum {
};

int xdpw_screenshot_init(struct xdpw_state *state);
int xdpw_screencast_init(struct xdpw_state *state, const char *output_name);
int xdpw_screencast_init(struct xdpw_state *state);

struct xdpw_request *xdpw_request_create(sd_bus *bus, const char *object_path);
void xdpw_request_destroy(struct xdpw_request *req);
Expand Down
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rt = cc.find_library('rt')
pipewire = dependency('libpipewire-0.3', version: '>= 0.3.2')
wayland_client = dependency('wayland-client')
wayland_protos = dependency('wayland-protocols', version: '>=1.14')
iniparser = cc.find_library('iniparser')

logind = dependency('libsystemd', required: false)
if logind.found()
Expand All @@ -42,6 +43,7 @@ executable(
files([
'src/core/main.c',
'src/core/logger.c',
'src/core/config.c',
'src/core/request.c',
'src/core/session.c',
'src/screenshot/screenshot.c',
Expand All @@ -56,6 +58,7 @@ executable(
logind,
pipewire,
rt,
iniparser,
],
include_directories: [inc],
install: true,
Expand Down
111 changes: 111 additions & 0 deletions src/core/config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "config.h"
#include "xdpw.h"

#include <dictionary.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wordexp.h>
#include "iniparser.h"

#define SYSCONFDIR "/etc"

void print_config(struct xdpw_config *config) {
printf("Configfile: %s\n",config->conf.configfile);
printf("Logfile: %s\n",config->logger_conf.logfile);
printf("Loglevel: %s\n",config->logger_conf.loglevel);
printf("Output_Name: %s\n",config->screencast_conf.output_name);
}

void free_configstring(char **dest) {
if (*dest != NULL) {
free(*dest);
*dest = NULL;
}
}

void destroy_config(struct xdpw_config *config) {
// logger
free_configstring(&config->logger_conf.loglevel);
free_configstring(&config->logger_conf.logfile);

// screencast
free_configstring(&config->screencast_conf.output_name);
}

void getstring_from_conffile(dictionary *d, char *key, char **dest, char *def) {
if (*dest == NULL) {
const char *c = iniparser_getstring(d, key, def);
if (c != NULL) {
*dest = strdup(c);
}
}
}

static bool file_exists(const char *path) {
return path && access(path, R_OK) != -1;
}

static char *expand_path(char *path, bool realloc) {
wordexp_t p;
if (wordexp(path, &p, WRDE_UNDEF) == 0) {
if (realloc) {
free(path);
}
return strdup(p.we_wordv[0]);
}

if (realloc) {
free(path);
}
return NULL;
}

void config_parse_file(struct xdpw_config *config) {
dictionary *d = iniparser_load(config->conf.configfile);

// logger
getstring_from_conffile(d, "logger:loglevel", &config->logger_conf.loglevel, "ERROR");
getstring_from_conffile(d, "logger:logfile", &config->logger_conf.logfile, "stderr");
config->logger_conf.logfile = expand_path(config->logger_conf.logfile, true);

// screencast
getstring_from_conffile(d, "screencast:output_name", &config->screencast_conf.output_name, NULL);

iniparser_freedict(d);
}

static char *get_config_path(void) {
static const char *config_paths[] = {
"$XDG_CONFIG_HOME/xdg-desktop-portal-wlr/$XDG_CURRENT_DESKTOP",
"$XDG_CONFIG_HOME/xdg-desktop-portal-wlr/config",
SYSCONFDIR "/xdg-desktop-portal-wlr/$XDG_CURRENT_DESKTOP",
SYSCONFDIR "/xdg-desktop-portal-wlr/config",
};

char *config_home = getenv("XDG_CONFIG_HOME");
if (!config_home || !*config_home) {
config_paths[0] = "$HOME/.config/xdg-desktop-portal-wlr/$XDG_CURRENT_DESKTOP";
config_paths[1] = "$HOME/.config/xdg-desktop-portal-wlr/config";
}

for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) {
char *path = expand_path((char*)config_paths[i], false);
if (file_exists(path)) {
return path;
}
}

return NULL;
}

void init_config(struct xdpw_config *config) {
if (config->conf.configfile == NULL) {
config->conf.configfile = get_config_path();
}

config_parse_file(config);
}
14 changes: 14 additions & 0 deletions src/core/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ static const char *loglevels[] = {

static struct logger_properties logprops;

FILE* open_logger_file(char *file_name) {
if (strcmp(file_name, "stderr") == 0 || file_name == NULL) {
return stderr;
} else {
return fopen(file_name, "w");
}
}

void destroy_logger() {
if (logprops.dst != stderr) {
fclose(logprops.dst);
}
}

void init_logger(FILE *dst, enum LOGLEVEL level) {
logprops.dst = dst;
logprops.level = level;
Expand Down
24 changes: 18 additions & 6 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
" -h, --help Get help (this text).\n"
"\n";

Expand All @@ -32,8 +34,7 @@ static int xdpw_usage(FILE* stream, int rc) {
}

int main(int argc, char *argv[]) {
const char* output_name = NULL;
enum LOGLEVEL loglevel = ERROR;
struct xdpw_config config = {0};

static const char* shortopts = "l:o:h";
static const struct option longopts[] = {
Expand All @@ -50,10 +51,13 @@ int main(int argc, char *argv[]) {

switch (c) {
case 'l':
loglevel = get_loglevel(optarg);
config.logger_conf.loglevel = strdup(optarg);
break;
case 'o':
output_name = optarg;
config.screencast_conf.output_name = optarg;
break;
case 'c':
config.conf.configfile = optarg;
break;
case 'h':
return xdpw_usage(stdout, EXIT_SUCCESS);
Expand All @@ -62,7 +66,12 @@ int main(int argc, char *argv[]) {
}
}

init_logger(stderr, loglevel);
init_config(&config);

print_config(&config);

init_logger(open_logger_file(config.logger_conf.logfile),
get_loglevel(config.logger_conf.loglevel));

int ret = 0;

Expand Down Expand Up @@ -99,12 +108,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;
Expand Down Expand Up @@ -176,6 +186,8 @@ int main(int argc, char *argv[]) {
}

// TODO: cleanup
destroy_logger();
destroy_config(&config);

return EXIT_SUCCESS;

Expand Down
8 changes: 4 additions & 4 deletions src/screencast/screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ int setup_outputs(struct xdpw_screencast_context *ctx, struct xdpw_session *sess
}

struct xdpw_wlr_output *out;
if (ctx->output_name) {
out = xdpw_wlr_output_find_by_name(&ctx->output_list, ctx->output_name);
if (ctx->state->config->screencast_conf.output_name) {
out = xdpw_wlr_output_find_by_name(&ctx->output_list,
ctx->state->config->screencast_conf.output_name);
if (!out) {
logprint(ERROR, "wlroots: no such output");
abort();
Expand Down Expand Up @@ -434,12 +435,11 @@ static const sd_bus_vtable screencast_vtable[] = {
SD_BUS_VTABLE_END
};

int xdpw_screencast_init(struct xdpw_state *state, const char *output_name) {
int xdpw_screencast_init(struct xdpw_state *state) {
sd_bus_slot *slot = NULL;

state->screencast = (struct xdpw_screencast_context) { 0 };
state->screencast.state = state;
state->screencast.output_name = output_name;

int err;
err = xdpw_pwr_core_connect(state);
Expand Down

0 comments on commit 556788d

Please sign in to comment.