1
0
Fork 0
mirror of https://github.com/swaywm/sway.git synced 2025-03-17 12:04:27 +00:00

Fix output configuration

Don't overwrite existing configuration. Previously, the configuration:

output WLC-1 position 1680,0
output WLC-1 background ~/background.png stretch

Would discard the positioning information.

This change fixes this, but removes support for combining different output
subcommands, to simplify error handling in individual subcommands.
This commit is contained in:
Christoph Gysin 2015-11-29 00:50:48 +02:00
parent 01617131f1
commit 80d628dffe
2 changed files with 89 additions and 75 deletions

View file

@ -133,10 +133,6 @@ Commands
Disables the specified output. Disables the specified output.
**NOTES FOR THE OUTPUT COMMAND**:: **NOTES FOR THE OUTPUT COMMAND**::
You may combine output commands into one, like so:
+
output HDMI-A-1 res 1920x1080 pos 1920,0 bg ~/wallpaper.png stretch
+
You can get a list of output names like so: You can get a list of output names like so:
+ +
swaymsg -t get_outputs swaymsg -t get_outputs

View file

@ -711,90 +711,106 @@ static struct cmd_results *cmd_orientation(int argc, char **argv) {
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }
int output_name_cmp(const void *item, const void *data)
{
const struct output_config *output = item;
const char *name = data;
return strcmp(output->name, name);
}
static struct cmd_results *cmd_output(int argc, char **argv) { static struct cmd_results *cmd_output(int argc, char **argv) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct output_config *output = calloc(1, sizeof(struct output_config));
output->x = output->y = output->width = output->height = -1; const char *name = argv[0];
output->name = strdup(argv[0]); const char *command = argv[1];
output->enabled = true;
struct output_config *output;
int index = list_seq_find(config->output_configs, output_name_cmp, name);
if (index >= 0) {
output = config->output_configs->items[index];
} else {
output = calloc(1, sizeof(struct output_config));
output->x = output->y = output->width = output->height = -1;
output->name = strdup(name);
output->enabled = true;
}
// TODO: atoi doesn't handle invalid numbers // TODO: atoi doesn't handle invalid numbers
if (strcasecmp(argv[1], "disable") == 0) { if (strcasecmp(command, "disable") == 0) {
output->enabled = false; output->enabled = false;
} }
// TODO: Check missing params after each sub-command // TODO: Check missing params after each sub-command
int i; if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
for (i = 1; i < argc; ++i) { char *res = argv[2];
if (strcasecmp(argv[i], "resolution") == 0 || strcasecmp(argv[i], "res") == 0) { char *x = strchr(res, 'x');
char *res = argv[++i]; int width = -1, height = -1;
char *x = strchr(res, 'x'); if (x != NULL) {
int width = -1, height = -1; // Format is 1234x4321
if (x != NULL) { *x = '\0';
// Format is 1234x4321 width = atoi(res);
*x = '\0'; height = atoi(x + 1);
width = atoi(res); *x = 'x';
height = atoi(x + 1); } else {
*x = 'x'; // Format is 1234 4321
} else { width = atoi(res);
// Format is 1234 4321 res = argv[3];
width = atoi(res); height = atoi(res);
res = argv[++i];
height = atoi(res);
}
output->width = width;
output->height = height;
} else if (strcasecmp(argv[i], "position") == 0 || strcasecmp(argv[i], "pos") == 0) {
char *res = argv[++i];
char *c = strchr(res, ',');
int x = -1, y = -1;
if (c != NULL) {
// Format is 1234,4321
*c = '\0';
x = atoi(res);
y = atoi(c + 1);
*c = ',';
} else {
// Format is 1234 4321
x = atoi(res);
res = argv[++i];
y = atoi(res);
}
output->x = x;
output->y = y;
} else if (strcasecmp(argv[i], "bg") == 0 || strcasecmp(argv[i], "background") == 0) {
wordexp_t p;
char *src = argv[++i];
char *mode = argv[++i];
if (wordexp(src, &p, 0) != 0) {
return cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
}
src = p.we_wordv[0];
if (access(src, F_OK) == -1) {
return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
}
for (char *m = mode; *m; ++m) *m = tolower(*m);
// Check mode
bool valid = false;
size_t j;
for (j = 0; j < sizeof(bg_options) / sizeof(char *); ++j) {
if (strcasecmp(mode, bg_options[j]) == 0) {
valid = true;
break;
}
}
if (!valid) {
return cmd_results_new(CMD_INVALID, "output", "Invalid background scaling mode.");
}
output->background = strdup(src);
output->background_option = strdup(mode);
wordfree(&p);
} }
output->width = width;
output->height = height;
} else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
char *res = argv[2];
char *c = strchr(res, ',');
int x = -1, y = -1;
if (c != NULL) {
// Format is 1234,4321
*c = '\0';
x = atoi(res);
y = atoi(c + 1);
*c = ',';
} else {
// Format is 1234 4321
x = atoi(res);
res = argv[3];
y = atoi(res);
}
output->x = x;
output->y = y;
} else if (strcasecmp(command, "bg") == 0 || strcasecmp(command, "background") == 0) {
wordexp_t p;
char *src = argv[2];
char *mode = argv[3];
if (wordexp(src, &p, 0) != 0) {
return cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
}
src = p.we_wordv[0];
if (access(src, F_OK) == -1) {
return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
}
for (char *m = mode; *m; ++m) *m = tolower(*m);
// Check mode
bool valid = false;
size_t j;
for (j = 0; j < sizeof(bg_options) / sizeof(char *); ++j) {
if (strcasecmp(mode, bg_options[j]) == 0) {
valid = true;
break;
}
}
if (!valid) {
return cmd_results_new(CMD_INVALID, "output", "Invalid background scaling mode.");
}
output->background = strdup(src);
output->background_option = strdup(mode);
wordfree(&p);
} }
int i = 1;
for (i = 0; i < config->output_configs->length; ++i) { for (i = 0; i < config->output_configs->length; ++i) {
struct output_config *oc = config->output_configs->items[i]; struct output_config *oc = config->output_configs->items[i];
if (strcmp(oc->name, output->name) == 0) { if (strcmp(oc->name, output->name) == 0) {
@ -804,7 +820,9 @@ static struct cmd_results *cmd_output(int argc, char **argv) {
break; break;
} }
} }
list_add(config->output_configs, output); if (index == -1) {
list_add(config->output_configs, output);
}
sway_log(L_DEBUG, "Config stored for output %s (%d x %d @ %d, %d) (bg %s %s)", sway_log(L_DEBUG, "Config stored for output %s (%d x %d @ %d, %d) (bg %s %s)",
output->name, output->width, output->height, output->x, output->y, output->name, output->width, output->height, output->x, output->y,