2017-12-06 22:36:06 +11:00
|
|
|
#define _XOPEN_SOURCE 700
|
2018-03-29 07:38:11 +11:00
|
|
|
#include <assert.h>
|
2017-12-30 05:04:16 +11:00
|
|
|
#include <stdbool.h>
|
2017-12-06 22:36:06 +11:00
|
|
|
#include <string.h>
|
2018-03-29 07:38:11 +11:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
2018-03-29 09:10:43 +11:00
|
|
|
#include <unistd.h>
|
2017-12-06 22:36:06 +11:00
|
|
|
#include <wlr/types/wlr_output.h>
|
2017-12-06 22:57:13 +11:00
|
|
|
#include <wlr/types/wlr_output_layout.h>
|
2017-12-06 22:36:06 +11:00
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/output.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-30 05:04:16 +11:00
|
|
|
void output_get_identifier(char *identifier, size_t len,
|
|
|
|
struct sway_output *output) {
|
|
|
|
struct wlr_output *wlr_output = output->wlr_output;
|
|
|
|
snprintf(identifier, len, "%s %s %s", wlr_output->make, wlr_output->model,
|
|
|
|
wlr_output->serial);
|
|
|
|
}
|
|
|
|
|
2017-12-28 07:23:30 +11:00
|
|
|
struct output_config *new_output_config(const char *name) {
|
2017-12-07 05:45:43 +11:00
|
|
|
struct output_config *oc = calloc(1, sizeof(struct output_config));
|
|
|
|
if (oc == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-28 07:23:30 +11:00
|
|
|
oc->name = strdup(name);
|
|
|
|
if (oc->name == NULL) {
|
|
|
|
free(oc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-06 22:57:13 +11:00
|
|
|
oc->enabled = -1;
|
2017-12-14 10:45:47 +11:00
|
|
|
oc->width = oc->height = -1;
|
2017-12-06 22:57:13 +11:00
|
|
|
oc->refresh_rate = -1;
|
|
|
|
oc->x = oc->y = -1;
|
|
|
|
oc->scale = -1;
|
|
|
|
oc->transform = -1;
|
2017-12-07 05:45:43 +11:00
|
|
|
return oc;
|
2017-12-06 22:57:13 +11:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:36:06 +11:00
|
|
|
void merge_output_config(struct output_config *dst, struct output_config *src) {
|
|
|
|
if (src->name) {
|
2017-12-07 05:16:12 +11:00
|
|
|
free(dst->name);
|
2017-12-06 22:36:06 +11:00
|
|
|
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->scale != -1) {
|
|
|
|
dst->scale = src->scale;
|
|
|
|
}
|
2017-12-06 22:57:13 +11:00
|
|
|
if (src->refresh_rate != -1) {
|
|
|
|
dst->refresh_rate = src->refresh_rate;
|
|
|
|
}
|
|
|
|
if (src->transform != -1) {
|
|
|
|
dst->transform = src->transform;
|
|
|
|
}
|
2017-12-06 22:36:06 +11:00
|
|
|
if (src->background) {
|
2017-12-07 05:16:12 +11:00
|
|
|
free(dst->background);
|
2017-12-06 22:36:06 +11:00
|
|
|
dst->background = strdup(src->background);
|
|
|
|
}
|
|
|
|
if (src->background_option) {
|
2017-12-07 05:16:12 +11:00
|
|
|
free(dst->background_option);
|
2017-12-06 22:36:06 +11:00
|
|
|
dst->background_option = strdup(src->background_option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_mode(struct wlr_output *output, int width, int height,
|
|
|
|
float refresh_rate) {
|
|
|
|
int mhz = (int)(refresh_rate * 1000);
|
2017-12-12 07:47:40 +11:00
|
|
|
if (wl_list_empty(&output->modes)) {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_DEBUG, "Assigning custom mode to %s", output->name);
|
2017-12-12 07:47:40 +11:00
|
|
|
wlr_output_set_custom_mode(output, width, height, mhz);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_output_mode *mode, *best = NULL;
|
2017-12-06 22:36:06 +11:00
|
|
|
wl_list_for_each(mode, &output->modes, link) {
|
|
|
|
if (mode->width == width && mode->height == height) {
|
|
|
|
if (mode->refresh == mhz) {
|
|
|
|
best = mode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
best = mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!best) {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_ERROR, "Configured mode for %s not available", output->name);
|
2017-12-06 22:36:06 +11:00
|
|
|
} else {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_DEBUG, "Assigning configured mode to %s", output->name);
|
2017-12-06 22:36:06 +11:00
|
|
|
wlr_output_set_mode(output, best);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 07:38:11 +11:00
|
|
|
void terminate_swaybg(pid_t pid) {
|
|
|
|
int ret = kill(pid, SIGTERM);
|
|
|
|
if (ret != 0) {
|
|
|
|
wlr_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", pid);
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 07:17:55 +11:00
|
|
|
void apply_output_config(struct output_config *oc, struct sway_container *output) {
|
2017-12-06 22:36:06 +11:00
|
|
|
assert(output->type == C_OUTPUT);
|
|
|
|
|
2017-12-10 01:48:52 +11:00
|
|
|
struct wlr_output *wlr_output = output->sway_output->wlr_output;
|
2017-12-06 22:36:06 +11:00
|
|
|
if (oc && oc->enabled == 0) {
|
2017-12-13 06:02:01 +11:00
|
|
|
wlr_output_layout_remove(root_container.sway_root->output_layout,
|
|
|
|
wlr_output);
|
2018-03-30 08:06:29 +11:00
|
|
|
container_output_destroy(output);
|
2017-12-06 22:36:06 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oc && oc->width > 0 && oc->height > 0) {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width,
|
2017-12-12 07:47:40 +11:00
|
|
|
oc->height, oc->refresh_rate);
|
2017-12-06 22:36:06 +11:00
|
|
|
set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
|
|
|
|
}
|
|
|
|
if (oc && oc->scale > 0) {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_DEBUG, "Set %s scale to %f", oc->name, oc->scale);
|
2017-12-07 05:16:12 +11:00
|
|
|
wlr_output_set_scale(wlr_output, oc->scale);
|
2017-12-06 22:36:06 +11:00
|
|
|
}
|
2017-12-06 22:57:13 +11:00
|
|
|
if (oc && oc->transform >= 0) {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
|
2017-12-14 01:52:18 +11:00
|
|
|
wlr_output_set_transform(wlr_output, oc->transform);
|
2017-12-06 22:36:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find position for it
|
2017-12-06 22:57:13 +11:00
|
|
|
if (oc && (oc->x != -1 || oc->y != -1)) {
|
2018-01-06 08:32:51 +11:00
|
|
|
wlr_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y);
|
2017-12-13 06:02:01 +11:00
|
|
|
wlr_output_layout_add(root_container.sway_root->output_layout,
|
|
|
|
wlr_output, oc->x, oc->y);
|
2017-12-06 22:36:06 +11:00
|
|
|
} else {
|
2017-12-13 06:02:01 +11:00
|
|
|
wlr_output_layout_add_auto(root_container.sway_root->output_layout,
|
|
|
|
wlr_output);
|
2017-12-06 22:36:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!oc || !oc->background) {
|
|
|
|
// Look for a * config for background
|
|
|
|
int i = list_seq_find(config->output_configs, output_name_cmp, "*");
|
|
|
|
if (i >= 0) {
|
|
|
|
oc = config->output_configs->items[i];
|
|
|
|
} else {
|
|
|
|
oc = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int output_i;
|
|
|
|
for (output_i = 0; output_i < root_container.children->length; ++output_i) {
|
|
|
|
if (root_container.children->items[output_i] == output) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oc && oc->background) {
|
2018-03-29 07:38:11 +11:00
|
|
|
if (output->sway_output->bg_pid != 0) {
|
|
|
|
terminate_swaybg(output->sway_output->bg_pid);
|
2017-12-06 22:36:06 +11:00
|
|
|
}
|
|
|
|
|
2018-03-29 07:38:11 +11:00
|
|
|
wlr_log(L_DEBUG, "Setting background for output %d to %s",
|
|
|
|
output_i, oc->background);
|
2017-12-06 22:36:06 +11:00
|
|
|
|
|
|
|
size_t bufsize = 12;
|
|
|
|
char output_id[bufsize];
|
|
|
|
snprintf(output_id, bufsize, "%d", output_i);
|
|
|
|
output_id[bufsize-1] = 0;
|
|
|
|
|
|
|
|
char *const cmd[] = {
|
2018-03-29 07:38:11 +11:00
|
|
|
"./swaybg/swaybg",
|
2017-12-06 22:36:06 +11:00
|
|
|
output_id,
|
|
|
|
oc->background,
|
|
|
|
oc->background_option,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2018-03-29 07:38:11 +11:00
|
|
|
output->sway_output->bg_pid = fork();
|
|
|
|
if (output->sway_output->bg_pid == 0) {
|
2017-12-06 22:36:06 +11:00
|
|
|
execvp(cmd[0], cmd);
|
2018-03-29 07:38:11 +11:00
|
|
|
}
|
2017-12-06 22:36:06 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_output_config(struct output_config *oc) {
|
|
|
|
if (!oc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(oc->name);
|
|
|
|
free(oc->background);
|
|
|
|
free(oc->background_option);
|
|
|
|
free(oc);
|
|
|
|
}
|