Merge pull request #278 from christophgysin/merge

cmd_output: Merge instead of replace output config
This commit is contained in:
Drew DeVault 2015-11-29 16:26:11 -05:00
commit 3f950b6e52
3 changed files with 48 additions and 9 deletions

View file

@ -40,7 +40,7 @@ struct sway_mode {
*/ */
struct output_config { struct output_config {
char *name; char *name;
bool enabled; int enabled;
int width, height; int width, height;
int x, y; int x, y;
char *background; char *background;
@ -99,6 +99,7 @@ bool read_config(FILE *file, bool is_active);
*/ */
char *do_var_replacement(char *str); char *do_var_replacement(char *str);
int output_name_cmp(const void *item, const void *data); int output_name_cmp(const void *item, const void *data);
void merge_output_config(struct output_config *dst, struct output_config *src);
/** Sets up a WLC output handle based on a given output_config. /** Sets up a WLC output handle based on a given output_config.
*/ */
void apply_output_config(struct output_config *oc, swayc_t *output); void apply_output_config(struct output_config *oc, swayc_t *output);

View file

@ -719,7 +719,7 @@ static struct cmd_results *cmd_output(int argc, char **argv) {
struct output_config *output = calloc(1, sizeof(struct output_config)); struct output_config *output = calloc(1, sizeof(struct output_config));
output->x = output->y = output->width = output->height = -1; output->x = output->y = output->width = output->height = -1;
output->name = strdup(name); output->name = strdup(name);
output->enabled = true; output->enabled = -1;
// TODO: atoi doesn't handle invalid numbers // TODO: atoi doesn't handle invalid numbers
@ -728,7 +728,7 @@ static struct cmd_results *cmd_output(int argc, char **argv) {
const char *command = argv[i]; const char *command = argv[i];
if (strcasecmp(command, "disable") == 0) { if (strcasecmp(command, "disable") == 0) {
output->enabled = false; output->enabled = 0;
} else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
if (++i >= argc) { if (++i >= argc) {
return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument.");
@ -815,15 +815,17 @@ static struct cmd_results *cmd_output(int argc, char **argv) {
i = list_seq_find(config->output_configs, output_name_cmp, name); i = list_seq_find(config->output_configs, output_name_cmp, name);
if (i >= 0) { if (i >= 0) {
// replace existing config // merge existing config
struct output_config *oc = config->output_configs->items[i]; struct output_config *oc = config->output_configs->items[i];
list_del(config->output_configs, i); merge_output_config(oc, output);
free_output_config(oc); free_output_config(output);
output = oc;
} else {
list_add(config->output_configs, output);
} }
list_add(config->output_configs, output);
sway_log(L_DEBUG, "Config stored for output %s (%s) (%d x %d @ %d, %d) (bg %s %s)", sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d) (bg %s %s)",
output->name, output->enabled ? "enable" : "disable", output->width, output->name, output->enabled, output->width,
output->height, output->x, output->y, output->background, output->height, output->x, output->y, output->background,
output->background_option); output->background_option);

View file

@ -271,6 +271,42 @@ int output_name_cmp(const void *item, const void *data) {
return strcmp(output->name, name); return strcmp(output->name, name);
} }
void merge_output_config(struct output_config *dst, struct output_config *src) {
if (src->name) {
if (dst->name) {
free(dst->name);
}
dst->name = strdup(src->name);
}
if (src->enabled != -1) {
dst->enabled = src->enabled;
}
if (src->width != -1) {
dst->width = src->width;
}
if (src->height != -1) {
dst->height = src->height;
}
if (src->x != -1) {
dst->x = src->x;
}
if (src->y != -1) {
dst->y = src->y;
}
if (src->background) {
if (dst->background) {
free(dst->background);
}
dst->background = strdup(src->background);
}
if (src->background_option) {
if (dst->background_option) {
free(dst->background_option);
}
dst->background_option = strdup(src->background_option);
}
}
void apply_output_config(struct output_config *oc, swayc_t *output) { void apply_output_config(struct output_config *oc, swayc_t *output) {
if (oc && oc->width > 0 && oc->height > 0) { if (oc && oc->width > 0 && oc->height > 0) {
output->width = oc->width; output->width = oc->width;