2019-01-24 22:32:49 +11:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2018-06-03 11:33:16 +10:00
|
|
|
#include <libgen.h>
|
2019-01-24 22:32:49 +11:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-06-03 11:33:16 +10:00
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
2018-06-22 23:41:44 +10:00
|
|
|
#include <errno.h>
|
2018-06-03 11:33:16 +10:00
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/config.h"
|
2018-08-09 03:46:36 +10:00
|
|
|
#include "sway/swaynag.h"
|
2018-06-03 11:33:16 +10:00
|
|
|
#include "log.h"
|
|
|
|
#include "stringop.h"
|
|
|
|
|
2018-06-04 00:29:00 +10:00
|
|
|
static const char *bg_options[] = {
|
2018-06-03 11:33:16 +10:00
|
|
|
"stretch",
|
|
|
|
"center",
|
|
|
|
"fill",
|
|
|
|
"fit",
|
|
|
|
"tile",
|
|
|
|
};
|
|
|
|
|
2019-03-15 18:00:40 +11:00
|
|
|
static bool validate_color(const char *color) {
|
|
|
|
if (strlen(color) != 7 || color[0] != '#') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ptr = NULL;
|
|
|
|
strtol(&color[1], &ptr, 16);
|
|
|
|
return *ptr == '\0';
|
|
|
|
}
|
|
|
|
|
2018-06-03 11:33:16 +10:00
|
|
|
struct cmd_results *output_cmd_background(int argc, char **argv) {
|
|
|
|
if (!config->handler_context.output_config) {
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_FAILURE, "Missing output config");
|
2018-06-03 11:33:16 +10:00
|
|
|
}
|
|
|
|
if (!argc) {
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_INVALID,
|
2018-06-03 11:33:16 +10:00
|
|
|
"Missing background file or color specification.");
|
|
|
|
}
|
|
|
|
if (argc < 2) {
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_INVALID,
|
2018-06-03 11:33:16 +10:00
|
|
|
"Missing background scaling mode or `solid_color`.");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct output_config *output = config->handler_context.output_config;
|
|
|
|
|
|
|
|
if (strcasecmp(argv[1], "solid_color") == 0) {
|
2019-03-15 18:00:40 +11:00
|
|
|
if (!validate_color(argv[0])) {
|
|
|
|
return cmd_results_new(CMD_INVALID,
|
|
|
|
"Colors should be of the form #RRGGBB");
|
|
|
|
}
|
2019-01-18 19:22:33 +11:00
|
|
|
output->background = strdup(argv[0]);
|
2018-06-03 11:33:16 +10:00
|
|
|
output->background_option = strdup("solid_color");
|
2018-08-09 03:46:36 +10:00
|
|
|
output->background_fallback = NULL;
|
2018-06-03 11:33:16 +10:00
|
|
|
argc -= 2; argv += 2;
|
|
|
|
} else {
|
|
|
|
bool valid = false;
|
|
|
|
char *mode;
|
|
|
|
size_t j;
|
|
|
|
for (j = 0; j < (size_t)argc; ++j) {
|
|
|
|
mode = argv[j];
|
|
|
|
size_t n = sizeof(bg_options) / sizeof(char *);
|
|
|
|
for (size_t k = 0; k < n; ++k) {
|
|
|
|
if (strcasecmp(mode, bg_options[k]) == 0) {
|
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!valid) {
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_INVALID,
|
2018-06-03 11:33:16 +10:00
|
|
|
"Missing background scaling mode.");
|
|
|
|
}
|
2019-02-06 06:13:27 +11:00
|
|
|
if (j == 0) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "Missing background file");
|
|
|
|
}
|
2018-06-03 11:33:16 +10:00
|
|
|
|
|
|
|
char *src = join_args(argv, j);
|
2019-11-21 14:10:03 +11:00
|
|
|
if (!expand_path(&src)) {
|
2019-01-11 10:27:21 +11:00
|
|
|
struct cmd_results *cmd_res = cmd_results_new(CMD_INVALID,
|
2018-06-26 20:53:47 +10:00
|
|
|
"Invalid syntax (%s)", src);
|
|
|
|
free(src);
|
|
|
|
return cmd_res;
|
2018-06-03 11:33:16 +10:00
|
|
|
}
|
2018-06-26 20:57:22 +10:00
|
|
|
if (!src) {
|
2019-11-21 14:10:03 +11:00
|
|
|
sway_log(SWAY_ERROR, "Failed to allocate expanded path");
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_FAILURE, "Unable to allocate resource");
|
2018-06-26 20:57:22 +10:00
|
|
|
}
|
|
|
|
|
2018-06-03 11:33:16 +10:00
|
|
|
if (config->reading && *src != '/') {
|
2018-06-26 20:57:22 +10:00
|
|
|
// src file is inside configuration dir
|
|
|
|
|
2018-07-09 05:34:47 +10:00
|
|
|
char *conf = strdup(config->current_config_path);
|
2018-07-01 23:54:41 +10:00
|
|
|
if (!conf) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_ERROR, "Failed to duplicate string");
|
2018-07-01 23:54:41 +10:00
|
|
|
free(src);
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_FAILURE,
|
2018-06-22 23:41:44 +10:00
|
|
|
"Unable to allocate resources");
|
2018-06-26 20:57:22 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
char *conf_path = dirname(conf);
|
|
|
|
char *rel_path = src;
|
|
|
|
src = malloc(strlen(conf_path) + strlen(src) + 2);
|
|
|
|
if (!src) {
|
|
|
|
free(rel_path);
|
2018-06-03 11:33:16 +10:00
|
|
|
free(conf);
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_ERROR, "Unable to allocate memory");
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_FAILURE,
|
2018-06-26 20:57:22 +10:00
|
|
|
"Unable to allocate resources");
|
2018-06-03 11:33:16 +10:00
|
|
|
}
|
2018-06-26 20:57:22 +10:00
|
|
|
|
|
|
|
sprintf(src, "%s/%s", conf_path, rel_path);
|
|
|
|
free(rel_path);
|
|
|
|
free(conf);
|
2018-06-03 11:33:16 +10:00
|
|
|
}
|
2018-06-22 23:41:44 +10:00
|
|
|
|
2018-08-09 03:46:36 +10:00
|
|
|
bool can_access = access(src, F_OK) != -1;
|
|
|
|
if (!can_access) {
|
2019-01-24 22:32:49 +11:00
|
|
|
sway_log_errno(SWAY_ERROR, "Unable to access background file '%s'",
|
|
|
|
src);
|
2018-11-29 03:08:54 +11:00
|
|
|
config_add_swaynag_warning("Unable to access background file '%s'",
|
|
|
|
src);
|
2018-06-22 23:41:44 +10:00
|
|
|
free(src);
|
2018-08-09 03:46:36 +10:00
|
|
|
} else {
|
|
|
|
output->background = src;
|
|
|
|
output->background_option = strdup(mode);
|
2018-06-03 11:33:16 +10:00
|
|
|
}
|
|
|
|
argc -= j + 1; argv += j + 1;
|
2018-08-09 03:46:36 +10:00
|
|
|
|
|
|
|
output->background_fallback = NULL;
|
|
|
|
if (argc && *argv[0] == '#') {
|
2019-03-15 18:00:40 +11:00
|
|
|
if (!validate_color(argv[0])) {
|
|
|
|
return cmd_results_new(CMD_INVALID,
|
|
|
|
"fallback color should be of the form #RRGGBB");
|
|
|
|
}
|
|
|
|
|
2019-01-18 19:22:33 +11:00
|
|
|
output->background_fallback = strdup(argv[0]);
|
2018-08-09 03:46:36 +10:00
|
|
|
argc--; argv++;
|
|
|
|
|
|
|
|
if (!can_access) {
|
|
|
|
output->background = output->background_fallback;
|
|
|
|
output->background_option = strdup("solid_color");
|
|
|
|
output->background_fallback = NULL;
|
|
|
|
}
|
|
|
|
}
|
2018-06-03 11:33:16 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
config->handler_context.leftovers.argc = argc;
|
|
|
|
config->handler_context.leftovers.argv = argv;
|
|
|
|
return NULL;
|
|
|
|
}
|