2017-08-25 14:06:38 +02:00
|
|
|
#ifndef _POSIX_C_SOURCE
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#endif
|
2017-08-24 21:26:51 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
2017-08-25 14:06:38 +02:00
|
|
|
#include <unistd.h>
|
2017-08-29 14:32:02 +02:00
|
|
|
#include <sys/param.h>
|
2017-08-25 14:06:38 +02:00
|
|
|
#include <wlr/util/log.h>
|
2017-08-29 18:08:49 +02:00
|
|
|
#include <wlr/types/wlr_box.h>
|
2017-08-24 21:26:51 +02:00
|
|
|
#include "shared.h"
|
|
|
|
#include "config.h"
|
2017-08-25 14:06:38 +02:00
|
|
|
#include "ini.h"
|
2017-08-24 21:26:51 +02:00
|
|
|
|
|
|
|
static void usage(const char *name, int ret) {
|
|
|
|
fprintf(stderr,
|
2017-08-25 14:06:38 +02:00
|
|
|
"usage: %s [-C <FILE>]\n"
|
2017-08-24 21:26:51 +02:00
|
|
|
"\n"
|
2017-08-29 14:32:02 +02:00
|
|
|
" -C <FILE> Path to the configuration file\n"
|
|
|
|
" (default: wlr-example.ini).\n"
|
|
|
|
" See `examples/wlr-example.ini.example` for config\n"
|
|
|
|
" file documentation.\n", name);
|
2017-08-24 21:26:51 +02:00
|
|
|
|
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
static struct wlr_box *parse_geometry(const char *str) {
|
2017-08-25 19:26:13 +02:00
|
|
|
// format: {width}x{height}+{x}+{y}
|
2017-08-29 14:32:02 +02:00
|
|
|
if (strlen(str) > 255) {
|
2017-08-25 19:26:13 +02:00
|
|
|
wlr_log(L_ERROR, "cannot parse geometry string, too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = strdup(str);
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *box = calloc(1, sizeof(struct wlr_box));
|
2017-08-25 19:26:13 +02:00
|
|
|
|
2017-08-26 17:53:16 +02:00
|
|
|
bool has_width = false;
|
|
|
|
bool has_height = false;
|
|
|
|
bool has_x = false;
|
|
|
|
bool has_y = false;
|
|
|
|
|
2017-08-25 19:26:13 +02:00
|
|
|
char *pch = strtok(buf, "x+");
|
|
|
|
while (pch != NULL) {
|
|
|
|
errno = 0;
|
|
|
|
char *endptr;
|
|
|
|
long val = strtol(pch, &endptr, 0);
|
|
|
|
|
2017-08-29 14:32:02 +02:00
|
|
|
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
|
|
|
|
(errno != 0 && val == 0)) {
|
2017-08-25 19:26:13 +02:00
|
|
|
goto invalid_input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endptr == pch) {
|
|
|
|
goto invalid_input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_width) {
|
2017-08-29 18:08:49 +02:00
|
|
|
box->width = val;
|
2017-08-25 19:26:13 +02:00
|
|
|
has_width = true;
|
|
|
|
} else if (!has_height) {
|
2017-08-29 18:08:49 +02:00
|
|
|
box->height = val;
|
2017-08-25 19:26:13 +02:00
|
|
|
has_height = true;
|
|
|
|
} else if (!has_x) {
|
2017-08-29 18:08:49 +02:00
|
|
|
box->x = val;
|
2017-08-25 19:26:13 +02:00
|
|
|
has_x = true;
|
|
|
|
} else if (!has_y) {
|
2017-08-29 18:08:49 +02:00
|
|
|
box->y = val;
|
2017-08-25 19:26:13 +02:00
|
|
|
has_y = true;
|
|
|
|
} else {
|
2017-08-26 17:53:16 +02:00
|
|
|
break;
|
2017-08-25 19:26:13 +02:00
|
|
|
}
|
|
|
|
pch = strtok(NULL, "x+");
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:32:02 +02:00
|
|
|
if (!has_width || !has_height) {
|
2017-08-25 19:26:13 +02:00
|
|
|
goto invalid_input;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
2017-08-29 18:08:49 +02:00
|
|
|
return box;
|
2017-08-25 19:26:13 +02:00
|
|
|
|
|
|
|
invalid_input:
|
|
|
|
wlr_log(L_ERROR, "could not parse geometry string: %s", str);
|
|
|
|
free(buf);
|
2017-08-29 18:08:49 +02:00
|
|
|
free(box);
|
2017-08-25 19:26:13 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
static const char *output_prefix = "output:";
|
2017-08-25 15:40:01 +02:00
|
|
|
static const char *device_prefix = "device:";
|
2017-08-24 21:26:51 +02:00
|
|
|
|
2017-08-29 14:32:02 +02:00
|
|
|
static int config_ini_handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2017-08-25 14:06:38 +02:00
|
|
|
struct example_config *config = user;
|
|
|
|
if (strncmp(output_prefix, section, strlen(output_prefix)) == 0) {
|
|
|
|
const char *output_name = section + strlen(output_prefix);
|
|
|
|
struct output_config *oc;
|
|
|
|
bool found = false;
|
2017-08-24 21:26:51 +02:00
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
wl_list_for_each(oc, &config->outputs, link) {
|
|
|
|
if (strcmp(oc->name, output_name) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
2017-08-24 21:26:51 +02:00
|
|
|
}
|
2017-08-25 14:06:38 +02:00
|
|
|
}
|
2017-08-24 21:26:51 +02:00
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
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(name, "x") == 0) {
|
|
|
|
oc->x = strtol(value, NULL, 10);
|
|
|
|
} else if (strcmp(name, "y") == 0) {
|
|
|
|
oc->y = strtol(value, NULL, 10);
|
|
|
|
} else if (strcmp(name, "rotate") == 0) {
|
|
|
|
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;
|
2017-08-24 21:26:51 +02:00
|
|
|
} else {
|
2017-08-25 14:06:38 +02:00
|
|
|
wlr_log(L_ERROR, "got unknown transform value: %s", value);
|
2017-08-24 21:26:51 +02:00
|
|
|
}
|
2017-08-25 14:06:38 +02:00
|
|
|
}
|
2017-08-25 14:58:02 +02:00
|
|
|
} else if (strcmp(section, "cursor") == 0) {
|
|
|
|
if (strcmp(name, "map-to-output") == 0) {
|
2017-08-29 14:32:02 +02:00
|
|
|
free(config->cursor.mapped_output);
|
2017-08-25 14:58:02 +02:00
|
|
|
config->cursor.mapped_output = strdup(value);
|
2017-08-25 19:26:13 +02:00
|
|
|
} else if (strcmp(name, "geometry") == 0) {
|
2017-08-29 18:08:49 +02:00
|
|
|
free(config->cursor.mapped_box);
|
|
|
|
config->cursor.mapped_box = parse_geometry(value);
|
2017-08-25 14:58:02 +02:00
|
|
|
} else {
|
|
|
|
wlr_log(L_ERROR, "got unknown cursor config: %s", name);
|
|
|
|
}
|
2017-08-25 15:40:01 +02:00
|
|
|
} else if (strncmp(device_prefix, section, strlen(device_prefix)) == 0) {
|
|
|
|
const char *device_name = section + strlen(device_prefix);
|
|
|
|
struct device_config *dc;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
wl_list_for_each(dc, &config->devices, link) {
|
|
|
|
if (strcmp(dc->name, device_name) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
dc = calloc(1, sizeof(struct device_config));
|
|
|
|
dc->name = strdup(device_name);
|
|
|
|
wl_list_insert(&config->devices, &dc->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(name, "map-to-output") == 0) {
|
2017-08-29 14:32:02 +02:00
|
|
|
free(dc->mapped_output);
|
2017-08-25 15:40:01 +02:00
|
|
|
dc->mapped_output = strdup(value);
|
2017-08-25 19:26:13 +02:00
|
|
|
} else if (strcmp(name, "geometry") == 0) {
|
2017-08-29 18:08:49 +02:00
|
|
|
free(dc->mapped_box);
|
|
|
|
dc->mapped_box = parse_geometry(value);
|
2017-08-25 15:40:01 +02:00
|
|
|
} else {
|
|
|
|
wlr_log(L_ERROR, "got unknown device config: %s", name);
|
|
|
|
}
|
2017-08-25 14:06:38 +02:00
|
|
|
} else {
|
|
|
|
wlr_log(L_ERROR, "got unknown config section: %s", section);
|
|
|
|
}
|
2017-08-24 21:26:51 +02:00
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
return 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);
|
2017-08-25 15:40:01 +02:00
|
|
|
wl_list_init(&config->devices);
|
2017-08-24 21:26:51 +02:00
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
int c;
|
|
|
|
while ((c = getopt(argc, argv, "C:h")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'C':
|
|
|
|
config->config_path = strdup(optarg);
|
2017-08-24 21:26:51 +02:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage(argv[0], c != 'h');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
if (!config->config_path) {
|
|
|
|
// get the config path from the current directory
|
2017-08-29 14:32:02 +02:00
|
|
|
char cwd[MAXPATHLEN];
|
2017-08-25 14:06:38 +02:00
|
|
|
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
2017-08-29 14:32:02 +02:00
|
|
|
char buf[MAXPATHLEN];
|
|
|
|
snprintf(buf, MAXPATHLEN, "%s/%s", cwd, "wlr-example.ini");
|
2017-08-25 14:06:38 +02:00
|
|
|
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) {
|
2017-08-29 18:43:33 +02:00
|
|
|
wlr_log(L_DEBUG, "No config file found. Using empty config.");
|
2017-08-25 14:06:38 +02:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:26:51 +02:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
void example_config_destroy(struct example_config *config) {
|
2017-08-25 15:40:01 +02:00
|
|
|
struct output_config *oc, *otmp = NULL;
|
|
|
|
wl_list_for_each_safe(oc, otmp, &config->outputs, link) {
|
2017-08-25 14:06:38 +02:00
|
|
|
free(oc->name);
|
2017-08-24 21:26:51 +02:00
|
|
|
free(oc);
|
|
|
|
}
|
2017-08-25 15:40:01 +02:00
|
|
|
|
|
|
|
struct device_config *dc, *dtmp = NULL;
|
|
|
|
wl_list_for_each_safe(dc, dtmp, &config->devices, link) {
|
|
|
|
free(dc->name);
|
|
|
|
if (dc->mapped_output) {
|
|
|
|
free(dc->mapped_output);
|
|
|
|
}
|
2017-08-29 18:08:49 +02:00
|
|
|
if (dc->mapped_box) {
|
|
|
|
free(dc->mapped_box);
|
2017-08-25 19:26:13 +02:00
|
|
|
}
|
2017-08-25 15:40:01 +02:00
|
|
|
free(dc);
|
|
|
|
}
|
|
|
|
|
2017-08-25 14:06:38 +02:00
|
|
|
if (config->config_path) {
|
|
|
|
free(config->config_path);
|
|
|
|
}
|
2017-08-25 14:58:02 +02:00
|
|
|
if (config->cursor.mapped_output) {
|
|
|
|
free(config->cursor.mapped_output);
|
|
|
|
}
|
2017-08-29 18:08:49 +02:00
|
|
|
if (config->cursor.mapped_box) {
|
|
|
|
free(config->cursor.mapped_box);
|
2017-08-25 19:26:13 +02:00
|
|
|
}
|
2017-08-24 21:26:51 +02:00
|
|
|
free(config);
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:32:02 +02:00
|
|
|
struct wlr_output_layout *configure_layout(struct example_config *config,
|
|
|
|
struct wl_list *outputs) {
|
2017-08-24 21:26:51 +02:00
|
|
|
struct wlr_output_layout *layout = wlr_output_layout_init();
|
|
|
|
int max_x = INT_MIN;
|
|
|
|
int max_x_y = INT_MIN; // y value for the max_x output
|
|
|
|
|
|
|
|
// first add all the configured outputs
|
|
|
|
struct output_state *output;
|
|
|
|
wl_list_for_each(output, outputs, link) {
|
|
|
|
struct output_config *conf;
|
|
|
|
wl_list_for_each(conf, &config->outputs, link) {
|
|
|
|
if (strcmp(conf->name, output->output->name) == 0) {
|
|
|
|
wlr_output_layout_add(layout, output->output,
|
|
|
|
conf->x, conf->y);
|
|
|
|
wlr_output_transform(output->output, conf->transform);
|
|
|
|
int width, height;
|
2017-08-29 14:32:02 +02:00
|
|
|
wlr_output_effective_resolution(output->output, &width,
|
|
|
|
&height);
|
2017-08-24 21:26:51 +02:00
|
|
|
if (conf->x + width > max_x) {
|
|
|
|
max_x = conf->x + width;
|
|
|
|
max_x_y = conf->y;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_x == INT_MIN) {
|
|
|
|
// couldn't find a configured output
|
|
|
|
max_x = 0;
|
|
|
|
max_x_y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now add all the other configured outputs in a sensible position
|
|
|
|
wl_list_for_each(output, outputs, link) {
|
|
|
|
if (wlr_output_layout_get(layout, output->output)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wlr_output_layout_add(layout, output->output, max_x, max_x_y);
|
|
|
|
int width, height;
|
|
|
|
wlr_output_effective_resolution(output->output, &width, &height);
|
|
|
|
max_x += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|