add configuration file

master
Tony Crisci 7 years ago
parent 54f87146c3
commit b3a43e2261

1
.gitignore vendored

@ -12,3 +12,4 @@ test/
build/ build/
.lvimrc .lvimrc
wayland-*-protocol.* wayland-*-protocol.*
wlr-example.ini

@ -1,86 +1,88 @@
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <getopt.h> #include <getopt.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <wlr/util/log.h>
#include "shared.h" #include "shared.h"
#include "config.h" #include "config.h"
#include "ini.h"
static void usage(const char *name, int ret) { static void usage(const char *name, int ret) {
fprintf(stderr, fprintf(stderr,
"usage: %s [-d <name> [-r <rotation> | -f]]*\n" "usage: %s [-C <FILE>]\n"
"\n" "\n"
" -o <output> The name of the DRM display. e.g. DVI-I-1.\n" " -C <FILE> Path to the configuration file (default: wlr-example.ini).\n"
" -r <rotation> The rotation counter clockwise. Valid values are 90, 180, 270.\n" " See `examples/wlr-example.ini.example` for config file documentation.\n", name);
" -x <position> The X-axis coordinate position of this output in the layout.\n"
" -y <position> The Y-axis coordinate position of this output in the layout.\n"
" -f Flip the output along the vertical axis.\n", name);
exit(ret); exit(ret);
} }
struct example_config *parse_args(int argc, char *argv[]) { static const char *output_prefix = "output:";
struct example_config *config = calloc(1, sizeof(struct example_config));
wl_list_init(&config->outputs);
struct output_config *oc = NULL;
int c; static int config_ini_handler(void *user, const char *section, const char *name, const char *value) {
while ((c = getopt(argc, argv, "o:r:x:y:fh")) != -1) { struct example_config *config = user;
switch (c) { if (strncmp(output_prefix, section, strlen(output_prefix)) == 0) {
case 'o': const char *output_name = section + strlen(output_prefix);
oc = calloc(1, sizeof(*oc)); struct output_config *oc;
oc->name = optarg; bool found = false;
oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
wl_list_insert(&config->outputs, &oc->link);
break;
case 'r':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
if (oc->transform != WL_OUTPUT_TRANSFORM_NORMAL wl_list_for_each(oc, &config->outputs, link) {
&& oc->transform != WL_OUTPUT_TRANSFORM_FLIPPED) { if (strcmp(oc->name, output_name) == 0) {
fprintf(stderr, "Rotation for %s already specified\n", oc->name); found = true;
usage(argv[0], 1); break;
} }
}
if (!found) {
oc = calloc(1, sizeof(struct output_config));
oc->name = strdup(output_name);
oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
wl_list_insert(&config->outputs, &oc->link);
}
if (strcmp(optarg, "90") == 0) { if (strcmp(name, "x") == 0) {
oc->transform += WL_OUTPUT_TRANSFORM_90; oc->x = strtol(value, NULL, 10);
} else if (strcmp(optarg, "180") == 0) { } else if (strcmp(name, "y") == 0) {
oc->transform += WL_OUTPUT_TRANSFORM_180; oc->y = strtol(value, NULL, 10);
} else if (strcmp(optarg, "270") == 0) { } else if (strcmp(name, "rotate") == 0) {
oc->transform += WL_OUTPUT_TRANSFORM_270; if (strcmp(value, "90") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_90;
} else if (strcmp(value, "180") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_180;
} else if (strcmp(value, "270") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_270;
} else if (strcmp(value, "flipped") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
} else if (strcmp(value, "flipped-90") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
} else if (strcmp(value, "flipped-180") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
} else if (strcmp(value, "flipped-270") == 0) {
oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
} else { } else {
fprintf(stderr, "Invalid rotation '%s'\n", optarg); wlr_log(L_ERROR, "got unknown transform value: %s", value);
usage(argv[0], 1);
}
break;
case 'x':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
oc->x = strtol(optarg, NULL, 0);
break;
case 'y':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
oc->y = strtol(optarg, NULL, 0);
break;
case 'f':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
} }
}
} else {
wlr_log(L_ERROR, "got unknown config section: %s", section);
}
if (oc->transform >= WL_OUTPUT_TRANSFORM_FLIPPED) { return 1;
fprintf(stderr, "Flip for %s already specified\n", oc->name); }
usage(argv[0], 1);
} struct example_config *parse_args(int argc, char *argv[]) {
struct example_config *config = calloc(1, sizeof(struct example_config));
wl_list_init(&config->outputs);
oc->transform += WL_OUTPUT_TRANSFORM_FLIPPED; int c;
while ((c = getopt(argc, argv, "C:h")) != -1) {
switch (c) {
case 'C':
config->config_path = strdup(optarg);
break; break;
case 'h': case 'h':
case '?': case '?':
@ -88,14 +90,44 @@ struct example_config *parse_args(int argc, char *argv[]) {
} }
} }
if (!config->config_path) {
// get the config path from the current directory
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
char buf[1024];
sprintf(buf, "%s/%s", cwd, "wlr-example.ini");
config->config_path = strdup(buf);
} else {
wlr_log(L_ERROR, "could not get cwd");
exit(1);
}
}
int result = ini_parse(config->config_path, config_ini_handler, config);
if (result == -1) {
wlr_log(L_ERROR, "Could not find config file at %s", config->config_path);
exit(1);
} else if (result == -2) {
wlr_log(L_ERROR, "Could not allocate memory to parse config file");
exit(1);
} else if (result != 0) {
wlr_log(L_ERROR, "Could not parse config file");
exit(1);
}
return config; return config;
} }
void example_config_destroy(struct example_config *config) { void example_config_destroy(struct example_config *config) {
struct output_config *oc, *tmp = NULL; struct output_config *oc, *tmp = NULL;
wl_list_for_each_safe(oc, tmp, &config->outputs, link) { wl_list_for_each_safe(oc, tmp, &config->outputs, link) {
free(oc->name);
free(oc); free(oc);
} }
if (config->config_path) {
free(config->config_path);
}
free(config); free(config);
} }

@ -14,6 +14,7 @@ struct output_config {
struct example_config { struct example_config {
struct wl_list outputs; struct wl_list outputs;
char *config_path;
}; };
struct example_config *parse_args(int argc, char *argv[]); struct example_config *parse_args(int argc, char *argv[]);

@ -0,0 +1,33 @@
# Configuration
# -------------
# Some examples will read a configuration file. Not all examples will use all of
# the configuration options. The configuration file will be loaded from
# `wlr-example.ini` from the current directory or the path can be specified by the
# `-C` option given on the command line.
#
# Output configuration
# ~~~~~~~~~~~~~~~~~~~~
# Each output is specified in a section named [output:{NAME}] where NAME is the
# drm name for this output.
#
# Value "x" specifies the x-coordinate in the output layout.
#
# Value "y" specifies the y-coordinate in the output layout.
#
# Value "rotate" specifies output rotation and can be 90, 180, 270, flipped,
# flipped-90, flipped-180, or flipped-270
[output:HDMI-A-1]
x=3000
y=0
rotate=90
[output:DP-1]
x=0
y=0
rotate=270
[output:DVI-D-1]
x=1080
y=232
# vim:filetype=dosini
Loading…
Cancel
Save