2018-12-09 13:09:11 +01:00
|
|
|
#define _XOPEN_SOURCE 700 // for realpath
|
2017-12-05 10:40:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <libinput.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <linux/input-event-codes.h>
|
|
|
|
#include <wlr/types/wlr_output.h>
|
2017-12-12 15:02:30 +01:00
|
|
|
#include "sway/input/input-manager.h"
|
2018-01-16 21:16:04 +01:00
|
|
|
#include "sway/input/seat.h"
|
2017-12-05 10:40:55 +01:00
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/config.h"
|
2018-07-17 17:19:32 +02:00
|
|
|
#include "sway/criteria.h"
|
2018-08-03 03:37:29 +02:00
|
|
|
#include "sway/swaynag.h"
|
2018-05-05 10:25:31 +02:00
|
|
|
#include "sway/tree/arrange.h"
|
2018-08-26 04:05:16 +02:00
|
|
|
#include "sway/tree/root.h"
|
2018-05-30 02:27:16 +02:00
|
|
|
#include "sway/tree/workspace.h"
|
2018-05-02 15:07:52 +02:00
|
|
|
#include "cairo.h"
|
|
|
|
#include "pango.h"
|
2017-12-05 10:40:55 +01:00
|
|
|
#include "stringop.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
struct sway_config *config = NULL;
|
|
|
|
|
2017-12-29 11:31:08 +01:00
|
|
|
static void free_mode(struct sway_mode *mode) {
|
|
|
|
if (!mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(mode->name);
|
|
|
|
if (mode->keysym_bindings) {
|
2018-12-09 00:55:14 +01:00
|
|
|
for (int i = 0; i < mode->keysym_bindings->length; i++) {
|
2017-12-29 14:44:39 +01:00
|
|
|
free_sway_binding(mode->keysym_bindings->items[i]);
|
2017-12-29 11:31:08 +01:00
|
|
|
}
|
|
|
|
list_free(mode->keysym_bindings);
|
|
|
|
}
|
|
|
|
if (mode->keycode_bindings) {
|
2018-12-09 00:55:14 +01:00
|
|
|
for (int i = 0; i < mode->keycode_bindings->length; i++) {
|
2017-12-29 14:44:39 +01:00
|
|
|
free_sway_binding(mode->keycode_bindings->items[i]);
|
2017-12-29 11:31:08 +01:00
|
|
|
}
|
|
|
|
list_free(mode->keycode_bindings);
|
|
|
|
}
|
Parse mouse binding options
First, the existing sway_binding structure is given an
enumerated type code. As all flags to bindsym/bindcode
are boolean, a single uint32 is used to hold all flags.
The _BORDER, _CONTENTS, _TITLEBAR flags, when active,
indicate in which part of a container the binding can
trigger; to localize complexity, they do not overlap
with the command line arguments, which center around
_TITLEBAR being set by default.
The keyboard handling code is adjusted for this change,
as is binding_key_compare; note that BINDING_LOCKED
is *not* part of the key portion of the binding.
Next, list of mouse bindings is introduced and cleaned up.
Finally, the binding command parsing code is extended
to handle the case where bindsym is used to describe
a mouse binding rather than a keysym binding; the
difference between the two may be detected as late as
when the first key/button is parsed, or as early as
the first flag. As bindings can have multiple
keycodes/keysyms/buttons, mixed keysym/button sequences
are prohibited.
2018-07-18 04:01:06 +02:00
|
|
|
if (mode->mouse_bindings) {
|
2018-12-09 00:55:14 +01:00
|
|
|
for (int i = 0; i < mode->mouse_bindings->length; i++) {
|
Parse mouse binding options
First, the existing sway_binding structure is given an
enumerated type code. As all flags to bindsym/bindcode
are boolean, a single uint32 is used to hold all flags.
The _BORDER, _CONTENTS, _TITLEBAR flags, when active,
indicate in which part of a container the binding can
trigger; to localize complexity, they do not overlap
with the command line arguments, which center around
_TITLEBAR being set by default.
The keyboard handling code is adjusted for this change,
as is binding_key_compare; note that BINDING_LOCKED
is *not* part of the key portion of the binding.
Next, list of mouse bindings is introduced and cleaned up.
Finally, the binding command parsing code is extended
to handle the case where bindsym is used to describe
a mouse binding rather than a keysym binding; the
difference between the two may be detected as late as
when the first key/button is parsed, or as early as
the first flag. As bindings can have multiple
keycodes/keysyms/buttons, mixed keysym/button sequences
are prohibited.
2018-07-18 04:01:06 +02:00
|
|
|
free_sway_binding(mode->mouse_bindings->items[i]);
|
|
|
|
}
|
|
|
|
list_free(mode->mouse_bindings);
|
|
|
|
}
|
2017-12-29 11:31:08 +01:00
|
|
|
free(mode);
|
|
|
|
}
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
void free_config(struct sway_config *config) {
|
2017-12-29 11:31:08 +01:00
|
|
|
if (!config) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:20:02 +02:00
|
|
|
memset(&config->handler_context, 0, sizeof(config->handler_context));
|
|
|
|
|
2017-12-29 11:31:08 +01:00
|
|
|
// TODO: handle all currently unhandled lists as we add implementations
|
2017-12-29 15:31:04 +01:00
|
|
|
if (config->symbols) {
|
2018-03-31 06:35:06 +02:00
|
|
|
for (int i = 0; i < config->symbols->length; ++i) {
|
2017-12-29 15:31:04 +01:00
|
|
|
free_sway_variable(config->symbols->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->symbols);
|
|
|
|
}
|
2017-12-29 11:31:08 +01:00
|
|
|
if (config->modes) {
|
2018-03-31 06:35:06 +02:00
|
|
|
for (int i = 0; i < config->modes->length; ++i) {
|
2017-12-29 11:31:08 +01:00
|
|
|
free_mode(config->modes->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->modes);
|
|
|
|
}
|
2018-03-31 06:35:06 +02:00
|
|
|
if (config->bars) {
|
|
|
|
for (int i = 0; i < config->bars->length; ++i) {
|
|
|
|
free_bar_config(config->bars->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->bars);
|
|
|
|
}
|
2017-12-29 11:31:08 +01:00
|
|
|
list_free(config->cmd_queue);
|
2018-09-28 13:58:23 +02:00
|
|
|
if (config->workspace_configs) {
|
|
|
|
for (int i = 0; i < config->workspace_configs->length; i++) {
|
|
|
|
free_workspace_config(config->workspace_configs->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->workspace_configs);
|
|
|
|
}
|
2018-07-04 11:43:35 +02:00
|
|
|
if (config->output_configs) {
|
|
|
|
for (int i = 0; i < config->output_configs->length; i++) {
|
|
|
|
free_output_config(config->output_configs->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->output_configs);
|
|
|
|
}
|
2017-12-29 11:31:08 +01:00
|
|
|
if (config->input_configs) {
|
2018-03-31 06:35:06 +02:00
|
|
|
for (int i = 0; i < config->input_configs->length; i++) {
|
2017-12-29 11:31:08 +01:00
|
|
|
free_input_config(config->input_configs->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->input_configs);
|
|
|
|
}
|
2018-01-17 15:49:02 +01:00
|
|
|
if (config->seat_configs) {
|
2018-03-31 06:35:06 +02:00
|
|
|
for (int i = 0; i < config->seat_configs->length; i++) {
|
2018-01-17 15:49:02 +01:00
|
|
|
free_seat_config(config->seat_configs->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->seat_configs);
|
|
|
|
}
|
2018-07-17 17:19:32 +02:00
|
|
|
if (config->criteria) {
|
|
|
|
for (int i = 0; i < config->criteria->length; ++i) {
|
|
|
|
criteria_destroy(config->criteria->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->criteria);
|
|
|
|
}
|
2017-12-29 11:31:08 +01:00
|
|
|
list_free(config->no_focus);
|
|
|
|
list_free(config->active_bar_modifiers);
|
|
|
|
list_free(config->config_chain);
|
|
|
|
list_free(config->command_policies);
|
|
|
|
list_free(config->feature_policies);
|
|
|
|
list_free(config->ipc_policies);
|
|
|
|
free(config->floating_scroll_up_cmd);
|
|
|
|
free(config->floating_scroll_down_cmd);
|
|
|
|
free(config->floating_scroll_left_cmd);
|
|
|
|
free(config->floating_scroll_right_cmd);
|
|
|
|
free(config->font);
|
2018-10-08 15:08:33 +02:00
|
|
|
free(config->swaybg_command);
|
2018-10-08 15:56:34 +02:00
|
|
|
free(config->swaynag_command);
|
2018-07-08 21:34:47 +02:00
|
|
|
free((char *)config->current_config_path);
|
2017-12-29 11:31:08 +01:00
|
|
|
free((char *)config->current_config);
|
|
|
|
free(config);
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 21:16:04 +01:00
|
|
|
static void destroy_removed_seats(struct sway_config *old_config,
|
|
|
|
struct sway_config *new_config) {
|
|
|
|
struct seat_config *seat_config;
|
|
|
|
struct sway_seat *seat;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < old_config->seat_configs->length; i++) {
|
|
|
|
seat_config = old_config->seat_configs->items[i];
|
|
|
|
/* Also destroy seats that aren't present in new config */
|
|
|
|
if (new_config && list_seq_find(new_config->seat_configs,
|
|
|
|
seat_name_cmp, seat_config->name) < 0) {
|
2018-10-18 13:20:00 +02:00
|
|
|
seat = input_manager_get_seat(seat_config->name);
|
2018-04-02 14:45:37 +02:00
|
|
|
seat_destroy(seat);
|
2018-01-16 21:16:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:48:57 +02:00
|
|
|
static void set_color(float dest[static 4], uint32_t color) {
|
|
|
|
dest[0] = ((color >> 16) & 0xff) / 255.0;
|
|
|
|
dest[1] = ((color >> 8) & 0xff) / 255.0;
|
|
|
|
dest[2] = (color & 0xff) / 255.0;
|
|
|
|
dest[3] = 1.0;
|
2018-04-30 13:24:13 +02:00
|
|
|
}
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
static void config_defaults(struct sway_config *config) {
|
2018-10-08 15:56:34 +02:00
|
|
|
if (!(config->swaynag_command = strdup("swaynag"))) goto cleanup;
|
2018-08-03 03:37:29 +02:00
|
|
|
config->swaynag_config_errors = (struct swaynag_instance){
|
|
|
|
.args = "--type error "
|
|
|
|
"--message 'There are errors in your config file' "
|
|
|
|
"--detailed-message "
|
|
|
|
"--button 'Exit sway' 'swaymsg exit' "
|
|
|
|
"--button 'Reload sway' 'swaymsg reload'",
|
|
|
|
.pid = -1,
|
|
|
|
.detailed = true,
|
|
|
|
};
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
if (!(config->symbols = create_list())) goto cleanup;
|
|
|
|
if (!(config->modes = create_list())) goto cleanup;
|
|
|
|
if (!(config->bars = create_list())) goto cleanup;
|
2018-09-28 13:58:23 +02:00
|
|
|
if (!(config->workspace_configs = create_list())) goto cleanup;
|
2017-12-05 10:40:55 +01:00
|
|
|
if (!(config->criteria = create_list())) goto cleanup;
|
|
|
|
if (!(config->no_focus = create_list())) goto cleanup;
|
|
|
|
if (!(config->input_configs = create_list())) goto cleanup;
|
2017-12-14 17:11:56 +01:00
|
|
|
if (!(config->seat_configs = create_list())) goto cleanup;
|
2017-12-05 10:40:55 +01:00
|
|
|
if (!(config->output_configs = create_list())) goto cleanup;
|
|
|
|
|
|
|
|
if (!(config->cmd_queue = create_list())) goto cleanup;
|
|
|
|
|
2017-12-12 15:02:30 +01:00
|
|
|
if (!(config->current_mode = malloc(sizeof(struct sway_mode))))
|
|
|
|
goto cleanup;
|
2017-12-05 10:40:55 +01:00
|
|
|
if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup;
|
|
|
|
strcpy(config->current_mode->name, "default");
|
2017-12-27 16:08:18 +01:00
|
|
|
if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup;
|
|
|
|
if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup;
|
Parse mouse binding options
First, the existing sway_binding structure is given an
enumerated type code. As all flags to bindsym/bindcode
are boolean, a single uint32 is used to hold all flags.
The _BORDER, _CONTENTS, _TITLEBAR flags, when active,
indicate in which part of a container the binding can
trigger; to localize complexity, they do not overlap
with the command line arguments, which center around
_TITLEBAR being set by default.
The keyboard handling code is adjusted for this change,
as is binding_key_compare; note that BINDING_LOCKED
is *not* part of the key portion of the binding.
Next, list of mouse bindings is introduced and cleaned up.
Finally, the binding command parsing code is extended
to handle the case where bindsym is used to describe
a mouse binding rather than a keysym binding; the
difference between the two may be detected as late as
when the first key/button is parsed, or as early as
the first flag. As bindings can have multiple
keycodes/keysyms/buttons, mixed keysym/button sequences
are prohibited.
2018-07-18 04:01:06 +02:00
|
|
|
if (!(config->current_mode->mouse_bindings = create_list())) goto cleanup;
|
2017-12-05 10:40:55 +01:00
|
|
|
list_add(config->modes, config->current_mode);
|
|
|
|
|
|
|
|
config->floating_mod = 0;
|
2018-07-25 00:41:08 +02:00
|
|
|
config->floating_mod_inverse = false;
|
2017-12-05 10:40:55 +01:00
|
|
|
config->dragging_key = BTN_LEFT;
|
|
|
|
config->resizing_key = BTN_RIGHT;
|
2018-04-17 09:54:02 +02:00
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup;
|
|
|
|
if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup;
|
|
|
|
if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup;
|
|
|
|
if (!(config->floating_scroll_right_cmd = strdup(""))) goto cleanup;
|
|
|
|
config->default_layout = L_NONE;
|
|
|
|
config->default_orientation = L_NONE;
|
|
|
|
if (!(config->font = strdup("monospace 10"))) goto cleanup;
|
2018-05-10 13:24:00 +02:00
|
|
|
config->font_height = 17; // height of monospace 10
|
2018-07-20 11:37:27 +02:00
|
|
|
config->urgent_timeout = 500;
|
2018-10-07 12:40:05 +02:00
|
|
|
config->popup_during_fullscreen = POPUP_SMART;
|
2018-11-17 12:29:59 +01:00
|
|
|
config->xwayland = true;
|
2017-12-05 10:40:55 +01:00
|
|
|
|
2018-11-17 20:06:48 +01:00
|
|
|
config->titlebar_border_thickness = 1;
|
|
|
|
config->titlebar_h_padding = 5;
|
|
|
|
config->titlebar_v_padding = 4;
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
// floating view
|
|
|
|
config->floating_maximum_width = 0;
|
|
|
|
config->floating_maximum_height = 0;
|
|
|
|
config->floating_minimum_width = 75;
|
|
|
|
config->floating_minimum_height = 50;
|
|
|
|
|
|
|
|
// Flags
|
2018-11-06 21:58:08 +01:00
|
|
|
config->focus_follows_mouse = FOLLOWS_YES;
|
2018-10-10 11:28:37 +02:00
|
|
|
config->mouse_warping = WARP_OUTPUT;
|
2018-05-28 05:20:21 +02:00
|
|
|
config->focus_wrapping = WRAP_YES;
|
2018-08-03 03:37:29 +02:00
|
|
|
config->validating = false;
|
2017-12-05 10:40:55 +01:00
|
|
|
config->reloading = false;
|
|
|
|
config->active = false;
|
|
|
|
config->failed = false;
|
|
|
|
config->auto_back_and_forth = false;
|
|
|
|
config->reading = false;
|
|
|
|
config->show_marks = true;
|
2018-11-26 04:08:58 +01:00
|
|
|
config->title_align = ALIGN_LEFT;
|
2018-09-11 09:17:19 +02:00
|
|
|
config->tiling_drag = true;
|
2019-01-01 14:45:21 +01:00
|
|
|
config->tiling_drag_threshold = 9;
|
2017-12-05 10:40:55 +01:00
|
|
|
|
|
|
|
config->smart_gaps = false;
|
|
|
|
config->gaps_inner = 0;
|
2018-11-08 04:44:11 +01:00
|
|
|
config->gaps_outer.top = 0;
|
|
|
|
config->gaps_outer.right = 0;
|
|
|
|
config->gaps_outer.bottom = 0;
|
|
|
|
config->gaps_outer.left = 0;
|
2017-12-05 10:40:55 +01:00
|
|
|
|
|
|
|
if (!(config->active_bar_modifiers = create_list())) goto cleanup;
|
|
|
|
|
2018-10-08 15:08:33 +02:00
|
|
|
if (!(config->swaybg_command = strdup("swaybg"))) goto cleanup;
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
if (!(config->config_chain = create_list())) goto cleanup;
|
2018-07-08 21:34:47 +02:00
|
|
|
config->current_config_path = NULL;
|
2017-12-05 10:40:55 +01:00
|
|
|
config->current_config = NULL;
|
|
|
|
|
|
|
|
// borders
|
|
|
|
config->border = B_NORMAL;
|
|
|
|
config->floating_border = B_NORMAL;
|
|
|
|
config->border_thickness = 2;
|
|
|
|
config->floating_border_thickness = 2;
|
|
|
|
config->hide_edge_borders = E_NONE;
|
2018-10-01 18:56:27 +02:00
|
|
|
config->saved_edge_borders = E_NONE;
|
2017-12-05 10:40:55 +01:00
|
|
|
|
|
|
|
// border colors
|
2018-05-01 14:48:57 +02:00
|
|
|
set_color(config->border_colors.focused.border, 0x4C7899);
|
|
|
|
set_color(config->border_colors.focused.background, 0x285577);
|
2018-05-03 14:14:17 +02:00
|
|
|
set_color(config->border_colors.focused.text, 0xFFFFFFFF);
|
2018-05-01 14:48:57 +02:00
|
|
|
set_color(config->border_colors.focused.indicator, 0x2E9EF4);
|
|
|
|
set_color(config->border_colors.focused.child_border, 0x285577);
|
|
|
|
|
|
|
|
set_color(config->border_colors.focused_inactive.border, 0x333333);
|
|
|
|
set_color(config->border_colors.focused_inactive.background, 0x5F676A);
|
2018-05-03 14:14:17 +02:00
|
|
|
set_color(config->border_colors.focused_inactive.text, 0xFFFFFFFF);
|
2018-05-01 14:48:57 +02:00
|
|
|
set_color(config->border_colors.focused_inactive.indicator, 0x484E50);
|
|
|
|
set_color(config->border_colors.focused_inactive.child_border, 0x5F676A);
|
|
|
|
|
|
|
|
set_color(config->border_colors.unfocused.border, 0x333333);
|
|
|
|
set_color(config->border_colors.unfocused.background, 0x222222);
|
2018-05-03 14:22:16 +02:00
|
|
|
set_color(config->border_colors.unfocused.text, 0x88888888);
|
2018-05-01 14:48:57 +02:00
|
|
|
set_color(config->border_colors.unfocused.indicator, 0x292D2E);
|
|
|
|
set_color(config->border_colors.unfocused.child_border, 0x222222);
|
|
|
|
|
|
|
|
set_color(config->border_colors.urgent.border, 0x2F343A);
|
|
|
|
set_color(config->border_colors.urgent.background, 0x900000);
|
2018-05-03 14:14:17 +02:00
|
|
|
set_color(config->border_colors.urgent.text, 0xFFFFFFFF);
|
2018-05-01 14:48:57 +02:00
|
|
|
set_color(config->border_colors.urgent.indicator, 0x900000);
|
|
|
|
set_color(config->border_colors.urgent.child_border, 0x900000);
|
|
|
|
|
|
|
|
set_color(config->border_colors.placeholder.border, 0x000000);
|
|
|
|
set_color(config->border_colors.placeholder.background, 0x0C0C0C);
|
2018-05-03 14:14:17 +02:00
|
|
|
set_color(config->border_colors.placeholder.text, 0xFFFFFFFF);
|
2018-05-01 14:48:57 +02:00
|
|
|
set_color(config->border_colors.placeholder.indicator, 0x000000);
|
|
|
|
set_color(config->border_colors.placeholder.child_border, 0x0C0C0C);
|
|
|
|
|
|
|
|
set_color(config->border_colors.background, 0xFFFFFF);
|
2017-12-05 10:40:55 +01:00
|
|
|
|
|
|
|
// Security
|
|
|
|
if (!(config->command_policies = create_list())) goto cleanup;
|
|
|
|
if (!(config->feature_policies = create_list())) goto cleanup;
|
|
|
|
if (!(config->ipc_policies = create_list())) goto cleanup;
|
|
|
|
|
|
|
|
return;
|
|
|
|
cleanup:
|
|
|
|
sway_abort("Unable to allocate config structures");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool file_exists(const char *path) {
|
|
|
|
return path && access(path, R_OK) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_config_path(void) {
|
|
|
|
static const char *config_paths[] = {
|
|
|
|
"$HOME/.sway/config",
|
|
|
|
"$XDG_CONFIG_HOME/sway/config",
|
|
|
|
"$HOME/.i3/config",
|
|
|
|
"$XDG_CONFIG_HOME/i3/config",
|
|
|
|
SYSCONFDIR "/sway/config",
|
|
|
|
SYSCONFDIR "/i3/config",
|
|
|
|
};
|
|
|
|
|
2018-12-03 12:28:23 +01:00
|
|
|
char *config_home = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (!config_home || !*config_home) {
|
|
|
|
config_paths[1] = "$HOME/.config/sway/config";
|
|
|
|
config_paths[3] = "$HOME/.config/i3/config";
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:28:23 +01:00
|
|
|
for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) {
|
|
|
|
wordexp_t p;
|
|
|
|
if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) {
|
|
|
|
char *path = strdup(p.we_wordv[0]);
|
2017-12-05 10:40:55 +01:00
|
|
|
wordfree(&p);
|
|
|
|
if (file_exists(path)) {
|
|
|
|
return path;
|
|
|
|
}
|
2017-12-29 11:31:08 +01:00
|
|
|
free(path);
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:28:23 +01:00
|
|
|
return NULL;
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 05:54:40 +02:00
|
|
|
static bool load_config(const char *path, struct sway_config *config,
|
2018-08-03 03:37:29 +02:00
|
|
|
struct swaynag_instance *swaynag) {
|
2018-07-01 16:36:44 +02:00
|
|
|
if (path == NULL) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Unable to find a config file!");
|
2018-07-01 16:36:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_INFO, "Loading config from %s", path);
|
2017-12-05 10:40:55 +01:00
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fopen(path, "r");
|
|
|
|
if (!f) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Unable to open %s for reading", path);
|
2017-12-05 10:40:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-03 03:37:29 +02:00
|
|
|
bool config_load_success = read_config(f, config, swaynag);
|
2017-12-05 10:40:55 +01:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (!config_load_success) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Error(s) loading config!");
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 03:37:29 +02:00
|
|
|
bool load_main_config(const char *file, bool is_active, bool validating) {
|
2017-12-05 10:40:55 +01:00
|
|
|
char *path;
|
|
|
|
if (file != NULL) {
|
|
|
|
path = strdup(file);
|
|
|
|
} else {
|
|
|
|
path = get_config_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sway_config *old_config = config;
|
|
|
|
config = calloc(1, sizeof(struct sway_config));
|
|
|
|
if (!config) {
|
|
|
|
sway_abort("Unable to allocate config");
|
|
|
|
}
|
|
|
|
|
|
|
|
config_defaults(config);
|
2018-08-03 03:37:29 +02:00
|
|
|
config->validating = validating;
|
2017-12-05 10:40:55 +01:00
|
|
|
if (is_active) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Performing configuration file %s",
|
2018-11-05 20:32:05 +01:00
|
|
|
validating ? "validation" : "reload");
|
2017-12-05 10:40:55 +01:00
|
|
|
config->reloading = true;
|
|
|
|
config->active = true;
|
2018-08-03 03:37:29 +02:00
|
|
|
|
|
|
|
swaynag_kill(&old_config->swaynag_config_errors);
|
2018-08-03 16:40:09 +02:00
|
|
|
memcpy(&config->swaynag_config_errors,
|
|
|
|
&old_config->swaynag_config_errors,
|
|
|
|
sizeof(struct swaynag_instance));
|
2019-01-09 06:09:20 +01:00
|
|
|
|
|
|
|
input_manager_reset_all_inputs();
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:34:47 +02:00
|
|
|
config->current_config_path = path;
|
2017-12-05 10:40:55 +01:00
|
|
|
list_add(config->config_chain, path);
|
|
|
|
|
|
|
|
config->reading = true;
|
|
|
|
|
|
|
|
// Read security configs
|
2018-01-31 05:09:21 +01:00
|
|
|
// TODO: Security
|
2017-12-05 10:40:55 +01:00
|
|
|
bool success = true;
|
2018-01-31 05:09:21 +01:00
|
|
|
/*
|
2017-12-05 10:40:55 +01:00
|
|
|
DIR *dir = opendir(SYSCONFDIR "/sway/security.d");
|
|
|
|
if (!dir) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR,
|
2017-12-12 15:02:30 +01:00
|
|
|
"%s does not exist, sway will have no security configuration"
|
|
|
|
" and will probably be broken", SYSCONFDIR "/sway/security.d");
|
2017-12-05 10:40:55 +01:00
|
|
|
} else {
|
|
|
|
list_t *secconfigs = create_list();
|
|
|
|
char *base = SYSCONFDIR "/sway/security.d/";
|
|
|
|
struct dirent *ent = readdir(dir);
|
|
|
|
struct stat s;
|
|
|
|
while (ent != NULL) {
|
|
|
|
char *_path = malloc(strlen(ent->d_name) + strlen(base) + 1);
|
|
|
|
strcpy(_path, base);
|
|
|
|
strcat(_path, ent->d_name);
|
|
|
|
lstat(_path, &s);
|
|
|
|
if (S_ISREG(s.st_mode) && ent->d_name[0] != '.') {
|
|
|
|
list_add(secconfigs, _path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
free(_path);
|
|
|
|
}
|
|
|
|
ent = readdir(dir);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
list_qsort(secconfigs, qstrcmp);
|
|
|
|
for (int i = 0; i < secconfigs->length; ++i) {
|
|
|
|
char *_path = secconfigs->items[i];
|
2017-12-12 15:02:30 +01:00
|
|
|
if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 ||
|
|
|
|
(((s.st_mode & 0777) != 0644) &&
|
2017-12-14 17:11:56 +01:00
|
|
|
(s.st_mode & 0777) != 0444)) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR,
|
2017-12-12 15:02:30 +01:00
|
|
|
"Refusing to load %s - it must be owned by root "
|
|
|
|
"and mode 644 or 444", _path);
|
2017-12-05 10:40:55 +01:00
|
|
|
success = false;
|
|
|
|
} else {
|
|
|
|
success = success && load_config(_path, config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 02:15:23 +01:00
|
|
|
list_free_items_and_destroy(secconfigs);
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
2018-01-31 05:09:21 +01:00
|
|
|
*/
|
2017-12-05 10:40:55 +01:00
|
|
|
|
2018-08-03 03:37:29 +02:00
|
|
|
success = success && load_config(path, config,
|
|
|
|
&config->swaynag_config_errors);
|
2017-12-05 10:40:55 +01:00
|
|
|
|
2018-10-08 11:28:53 +02:00
|
|
|
if (validating) {
|
|
|
|
free_config(config);
|
|
|
|
config = old_config;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
if (is_active) {
|
2018-07-21 04:17:20 +02:00
|
|
|
for (int i = 0; i < config->output_configs->length; i++) {
|
|
|
|
apply_output_config_to_outputs(config->output_configs->items[i]);
|
|
|
|
}
|
2017-12-05 10:40:55 +01:00
|
|
|
config->reloading = false;
|
2018-08-03 03:37:29 +02:00
|
|
|
if (config->swaynag_config_errors.pid > 0) {
|
|
|
|
swaynag_show(&config->swaynag_config_errors);
|
|
|
|
}
|
2019-01-09 06:57:23 +01:00
|
|
|
|
2018-12-30 03:50:03 +01:00
|
|
|
input_manager_verify_fallback_seat();
|
2019-01-09 06:57:23 +01:00
|
|
|
for (int i = 0; i < config->seat_configs->length; i++) {
|
|
|
|
input_manager_apply_seat_config(config->seat_configs->items[i]);
|
|
|
|
}
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old_config) {
|
2018-01-16 21:16:04 +01:00
|
|
|
destroy_removed_seats(old_config, config);
|
2017-12-05 10:40:55 +01:00
|
|
|
free_config(old_config);
|
|
|
|
}
|
|
|
|
config->reading = false;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:02:30 +01:00
|
|
|
static bool load_include_config(const char *path, const char *parent_dir,
|
2018-08-03 03:37:29 +02:00
|
|
|
struct sway_config *config, struct swaynag_instance *swaynag) {
|
2017-12-05 18:47:57 +01:00
|
|
|
// save parent config
|
2018-07-08 21:34:47 +02:00
|
|
|
const char *parent_config = config->current_config_path;
|
2017-12-05 18:47:57 +01:00
|
|
|
|
2018-07-01 16:34:07 +02:00
|
|
|
char *full_path;
|
2017-12-05 18:47:57 +01:00
|
|
|
int len = strlen(path);
|
|
|
|
if (len >= 1 && path[0] != '/') {
|
|
|
|
len = len + strlen(parent_dir) + 2;
|
|
|
|
full_path = malloc(len * sizeof(char));
|
|
|
|
if (!full_path) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR,
|
2017-12-12 15:02:30 +01:00
|
|
|
"Unable to allocate full path to included config");
|
2017-12-05 18:47:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
snprintf(full_path, len, "%s/%s", parent_dir, path);
|
2018-07-01 16:34:07 +02:00
|
|
|
} else {
|
|
|
|
full_path = strdup(path);
|
2017-12-05 18:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char *real_path = realpath(full_path, NULL);
|
|
|
|
free(full_path);
|
|
|
|
|
|
|
|
if (real_path == NULL) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "%s not found.", path);
|
2017-12-05 18:47:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if config has already been included
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < config->config_chain->length; ++j) {
|
|
|
|
char *old_path = config->config_chain->items[j];
|
|
|
|
if (strcmp(real_path, old_path) == 0) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG,
|
2017-12-12 15:02:30 +01:00
|
|
|
"%s already included once, won't be included again.",
|
|
|
|
real_path);
|
2017-12-05 18:47:57 +01:00
|
|
|
free(real_path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:34:47 +02:00
|
|
|
config->current_config_path = real_path;
|
2017-12-05 18:47:57 +01:00
|
|
|
list_add(config->config_chain, real_path);
|
|
|
|
int index = config->config_chain->length - 1;
|
|
|
|
|
2018-08-03 03:37:29 +02:00
|
|
|
if (!load_config(real_path, config, swaynag)) {
|
2017-12-05 18:47:57 +01:00
|
|
|
free(real_path);
|
2018-07-08 21:34:47 +02:00
|
|
|
config->current_config_path = parent_config;
|
2017-12-05 18:47:57 +01:00
|
|
|
list_del(config->config_chain, index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:34:47 +02:00
|
|
|
// restore current_config_path
|
|
|
|
config->current_config_path = parent_config;
|
2017-12-05 18:47:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-02 05:54:40 +02:00
|
|
|
bool load_include_configs(const char *path, struct sway_config *config,
|
2018-08-03 03:37:29 +02:00
|
|
|
struct swaynag_instance *swaynag) {
|
2017-12-05 18:47:57 +01:00
|
|
|
char *wd = getcwd(NULL, 0);
|
2018-07-08 21:34:47 +02:00
|
|
|
char *parent_path = strdup(config->current_config_path);
|
2017-12-05 18:47:57 +01:00
|
|
|
const char *parent_dir = dirname(parent_path);
|
|
|
|
|
|
|
|
if (chdir(parent_dir) < 0) {
|
|
|
|
free(parent_path);
|
|
|
|
free(wd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wordexp_t p;
|
|
|
|
|
|
|
|
if (wordexp(path, &p, 0) < 0) {
|
|
|
|
free(parent_path);
|
|
|
|
free(wd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char **w = p.we_wordv;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < p.we_wordc; ++i) {
|
2018-08-03 03:37:29 +02:00
|
|
|
load_include_config(w[i], parent_dir, config, swaynag);
|
2017-12-05 18:47:57 +01:00
|
|
|
}
|
|
|
|
free(parent_path);
|
|
|
|
wordfree(&p);
|
|
|
|
|
|
|
|
// restore wd
|
|
|
|
if (chdir(wd) < 0) {
|
|
|
|
free(wd);
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "failed to restore working directory");
|
2017-12-05 18:47:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(wd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-01 00:00:49 +01:00
|
|
|
// get line, with backslash continuation
|
2019-01-12 02:41:38 +01:00
|
|
|
static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file,
|
|
|
|
int *nlines) {
|
2019-01-01 00:00:49 +01:00
|
|
|
char *next_line = NULL;
|
|
|
|
size_t next_line_size = 0;
|
|
|
|
ssize_t nread = getline(lineptr, line_size, file);
|
2019-01-12 02:41:38 +01:00
|
|
|
*nlines = nread == -1 ? 0 : 1;
|
2019-01-01 10:01:09 +01:00
|
|
|
while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) {
|
2019-01-01 00:00:49 +01:00
|
|
|
ssize_t next_nread = getline(&next_line, &next_line_size, file);
|
|
|
|
if (next_nread == -1) {
|
|
|
|
break;
|
|
|
|
}
|
2019-01-12 02:41:38 +01:00
|
|
|
(*nlines)++;
|
2019-01-01 00:00:49 +01:00
|
|
|
|
|
|
|
nread += next_nread - 2;
|
|
|
|
if ((ssize_t) *line_size < nread + 1) {
|
|
|
|
*line_size = nread + 1;
|
|
|
|
*lineptr = realloc(*lineptr, *line_size);
|
2019-01-01 10:01:09 +01:00
|
|
|
if (!*lineptr) {
|
2019-01-01 00:00:49 +01:00
|
|
|
nread = -1;
|
|
|
|
break;
|
2018-05-30 21:06:25 +02:00
|
|
|
}
|
2019-01-01 00:00:49 +01:00
|
|
|
}
|
|
|
|
strcpy(&(*lineptr)[nread - next_nread], next_line);
|
|
|
|
}
|
|
|
|
free(next_line);
|
|
|
|
return nread;
|
|
|
|
}
|
2018-05-30 21:06:25 +02:00
|
|
|
|
2018-12-09 13:09:11 +01:00
|
|
|
static int detect_brace(FILE *file) {
|
2019-01-10 11:59:07 +01:00
|
|
|
int ret = 0;
|
2018-05-30 21:06:25 +02:00
|
|
|
int lines = 0;
|
2018-12-09 13:09:11 +01:00
|
|
|
long pos = ftell(file);
|
|
|
|
char *line = NULL;
|
|
|
|
size_t line_size = 0;
|
|
|
|
while ((getline(&line, &line_size, file)) != -1) {
|
|
|
|
lines++;
|
|
|
|
strip_whitespace(line);
|
|
|
|
if (*line) {
|
2019-01-10 11:59:07 +01:00
|
|
|
if (strcmp(line, "{") == 0) {
|
|
|
|
ret = lines;
|
2018-05-30 21:06:25 +02:00
|
|
|
}
|
2018-12-09 13:09:11 +01:00
|
|
|
break;
|
2018-05-30 21:06:25 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-09 13:09:11 +01:00
|
|
|
free(line);
|
2019-01-12 02:02:20 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
fseek(file, pos, SEEK_SET);
|
|
|
|
}
|
2019-01-10 11:59:07 +01:00
|
|
|
return ret;
|
2018-05-30 21:06:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 04:23:11 +02:00
|
|
|
static char *expand_line(const char *block, const char *line, bool add_brace) {
|
2018-05-30 21:06:25 +02:00
|
|
|
int size = (block ? strlen(block) + 1 : 0) + strlen(line)
|
|
|
|
+ (add_brace ? 2 : 0) + 1;
|
|
|
|
char *expanded = calloc(1, size);
|
|
|
|
if (!expanded) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Cannot allocate expanded line buffer");
|
2018-05-30 21:06:25 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-31 04:23:11 +02:00
|
|
|
snprintf(expanded, size, "%s%s%s%s", block ? block : "",
|
|
|
|
block ? " " : "", line, add_brace ? " {" : "");
|
2018-05-30 21:06:25 +02:00
|
|
|
return expanded;
|
|
|
|
}
|
|
|
|
|
2018-08-03 03:37:29 +02:00
|
|
|
bool read_config(FILE *file, struct sway_config *config,
|
|
|
|
struct swaynag_instance *swaynag) {
|
2018-07-08 21:34:47 +02:00
|
|
|
bool reading_main_config = false;
|
2018-07-15 15:59:54 +02:00
|
|
|
char *this_config = NULL;
|
2018-07-15 16:36:51 +02:00
|
|
|
size_t config_size = 0;
|
2018-07-08 21:34:47 +02:00
|
|
|
if (config->current_config == NULL) {
|
|
|
|
reading_main_config = true;
|
|
|
|
|
2018-07-15 16:36:51 +02:00
|
|
|
int ret_seek = fseek(file, 0, SEEK_END);
|
|
|
|
long ret_tell = ftell(file);
|
|
|
|
if (ret_seek == -1 || ret_tell == -1) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Unable to get size of config file");
|
2018-07-15 16:36:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
config_size = ret_tell;
|
2018-07-08 21:34:47 +02:00
|
|
|
rewind(file);
|
|
|
|
|
2018-07-15 15:59:54 +02:00
|
|
|
config->current_config = this_config = calloc(1, config_size + 1);
|
2018-07-12 19:00:57 +02:00
|
|
|
if (this_config == NULL) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Unable to allocate buffer for config contents");
|
2018-07-08 21:34:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
bool success = true;
|
|
|
|
int line_number = 0;
|
2018-12-09 13:09:11 +01:00
|
|
|
char *line = NULL;
|
|
|
|
size_t line_size = 0;
|
|
|
|
ssize_t nread;
|
2018-05-30 19:20:02 +02:00
|
|
|
list_t *stack = create_list();
|
2018-07-15 15:59:54 +02:00
|
|
|
size_t read = 0;
|
2019-01-12 02:41:38 +01:00
|
|
|
int nlines = 0;
|
|
|
|
while ((nread = getline_with_cont(&line, &line_size, file, &nlines)) != -1) {
|
2018-07-08 21:34:47 +02:00
|
|
|
if (reading_main_config) {
|
2018-12-09 13:09:11 +01:00
|
|
|
if (read + nread > config_size) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Config file changed during reading");
|
2018-12-09 13:09:11 +01:00
|
|
|
success = false;
|
|
|
|
break;
|
2018-07-15 15:59:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 13:09:11 +01:00
|
|
|
strcpy(&this_config[read], line);
|
|
|
|
read += nread;
|
2018-07-08 21:34:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 13:09:11 +01:00
|
|
|
if (line[nread - 1] == '\n') {
|
|
|
|
line[nread - 1] = '\0';
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
2018-12-09 13:09:11 +01:00
|
|
|
|
2019-01-12 02:41:38 +01:00
|
|
|
line_number += nlines;
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Read line %d: %s", line_number, line);
|
2018-12-09 13:09:11 +01:00
|
|
|
|
|
|
|
strip_whitespace(line);
|
|
|
|
if (!*line || line[0] == '#') {
|
2018-05-30 19:20:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-09 13:09:11 +01:00
|
|
|
int brace_detected = 0;
|
|
|
|
if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
|
|
|
|
brace_detected = detect_brace(file);
|
|
|
|
if (brace_detected > 0) {
|
|
|
|
line_number += brace_detected;
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Detected open brace on line %d", line_number);
|
2018-12-09 13:09:11 +01:00
|
|
|
}
|
2018-05-30 21:06:25 +02:00
|
|
|
}
|
2018-12-09 13:09:11 +01:00
|
|
|
char *block = stack->length ? stack->items[0] : NULL;
|
2018-05-30 21:06:25 +02:00
|
|
|
char *expanded = expand_line(block, line, brace_detected > 0);
|
|
|
|
if (!expanded) {
|
2018-12-09 13:09:11 +01:00
|
|
|
success = false;
|
|
|
|
break;
|
2018-05-30 21:06:25 +02:00
|
|
|
}
|
2018-11-28 17:08:54 +01:00
|
|
|
config->current_config_line_number = line_number;
|
|
|
|
config->current_config_line = line;
|
2017-12-05 10:40:55 +01:00
|
|
|
struct cmd_results *res;
|
2019-01-10 14:27:52 +01:00
|
|
|
char *new_block = NULL;
|
2018-05-30 19:20:02 +02:00
|
|
|
if (block && strcmp(block, "<commands>") == 0) {
|
2017-12-05 10:40:55 +01:00
|
|
|
// Special case
|
2018-05-30 21:06:25 +02:00
|
|
|
res = config_commands_command(expanded);
|
2017-12-05 10:40:55 +01:00
|
|
|
} else {
|
2019-01-10 14:27:52 +01:00
|
|
|
res = config_command(expanded, &new_block);
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
switch(res->status) {
|
|
|
|
case CMD_FAILURE:
|
|
|
|
case CMD_INVALID:
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Error on line %i '%s': %s (%s)", line_number,
|
2018-07-08 21:34:47 +02:00
|
|
|
line, res->error, config->current_config_path);
|
2018-08-03 03:37:29 +02:00
|
|
|
if (!config->validating) {
|
|
|
|
swaynag_log(config->swaynag_command, swaynag,
|
|
|
|
"Error on line %i (%s) '%s': %s", line_number,
|
|
|
|
config->current_config_path, line, res->error);
|
|
|
|
}
|
2017-12-05 10:40:55 +01:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_DEFER:
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Deferring command `%s'", line);
|
2018-07-19 07:39:58 +02:00
|
|
|
list_add(config->cmd_queue, strdup(expanded));
|
2017-12-05 10:40:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_BLOCK_COMMANDS:
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Entering commands block");
|
2018-05-30 19:20:02 +02:00
|
|
|
list_insert(stack, 0, "<commands>");
|
2017-12-05 10:40:55 +01:00
|
|
|
break;
|
|
|
|
|
2018-05-30 19:20:02 +02:00
|
|
|
case CMD_BLOCK:
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Entering block '%s'", new_block);
|
2019-01-10 14:27:52 +01:00
|
|
|
list_insert(stack, 0, strdup(new_block));
|
|
|
|
if (strcmp(new_block, "bar") == 0) {
|
2018-05-30 19:20:02 +02:00
|
|
|
config->current_bar = NULL;
|
2017-12-05 10:40:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_BLOCK_END:
|
2018-05-30 19:20:02 +02:00
|
|
|
if (!block) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Unmatched '}' on line %i", line_number);
|
2018-05-30 19:20:02 +02:00
|
|
|
success = false;
|
2017-12-14 17:11:56 +01:00
|
|
|
break;
|
2018-05-30 19:20:02 +02:00
|
|
|
}
|
|
|
|
if (strcmp(block, "bar") == 0) {
|
2017-12-05 10:40:55 +01:00
|
|
|
config->current_bar = NULL;
|
|
|
|
}
|
2018-06-02 17:31:45 +02:00
|
|
|
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Exiting block '%s'", block);
|
2018-06-02 17:31:45 +02:00
|
|
|
list_del(stack, 0);
|
|
|
|
free(block);
|
2018-05-30 19:20:02 +02:00
|
|
|
memset(&config->handler_context, 0,
|
|
|
|
sizeof(config->handler_context));
|
2017-12-05 10:40:55 +01:00
|
|
|
default:;
|
|
|
|
}
|
2019-01-10 14:27:52 +01:00
|
|
|
free(new_block);
|
2018-07-19 07:39:58 +02:00
|
|
|
free(expanded);
|
2017-12-05 10:40:55 +01:00
|
|
|
free_cmd_results(res);
|
|
|
|
}
|
2018-12-09 13:09:11 +01:00
|
|
|
free(line);
|
2018-12-09 02:15:23 +01:00
|
|
|
list_free_items_and_destroy(stack);
|
2018-11-28 17:08:54 +01:00
|
|
|
config->current_config_line_number = 0;
|
|
|
|
config->current_config_line = NULL;
|
2017-12-05 10:40:55 +01:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:08:54 +01:00
|
|
|
void config_add_swaynag_warning(char *fmt, ...) {
|
|
|
|
if (config->reading && !config->validating) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
size_t length = vsnprintf(NULL, 0, fmt, args) + 1;
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
char *temp = malloc(length + 1);
|
|
|
|
if (!temp) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Failed to allocate buffer for warning.");
|
2018-11-28 17:08:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(temp, length, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
swaynag_log(config->swaynag_command, &config->swaynag_config_errors,
|
|
|
|
"Warning on line %i (%s) '%s': %s",
|
|
|
|
config->current_config_line_number, config->current_config_path,
|
|
|
|
config->current_config_line, temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 10:40:55 +01:00
|
|
|
char *do_var_replacement(char *str) {
|
|
|
|
int i;
|
|
|
|
char *find = str;
|
|
|
|
while ((find = strchr(find, '$'))) {
|
|
|
|
// Skip if escaped.
|
|
|
|
if (find > str && find[-1] == '\\') {
|
|
|
|
if (find == str + 1 || !(find > str + 1 && find[-2] == '\\')) {
|
|
|
|
++find;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 04:24:34 +02:00
|
|
|
// Unescape double $ and move on
|
|
|
|
if (find[1] == '$') {
|
|
|
|
size_t length = strlen(find + 1);
|
2018-06-08 02:37:08 +02:00
|
|
|
memmove(find, find + 1, length);
|
2018-06-05 04:24:34 +02:00
|
|
|
find[length] = '\0';
|
2018-06-05 16:07:42 +02:00
|
|
|
++find;
|
2018-06-05 04:24:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-05 10:40:55 +01:00
|
|
|
// Find matching variable
|
|
|
|
for (i = 0; i < config->symbols->length; ++i) {
|
|
|
|
struct sway_variable *var = config->symbols->items[i];
|
|
|
|
int vnlen = strlen(var->name);
|
|
|
|
if (strncmp(find, var->name, vnlen) == 0) {
|
|
|
|
int vvlen = strlen(var->value);
|
|
|
|
char *newstr = malloc(strlen(str) - vnlen + vvlen + 1);
|
|
|
|
if (!newstr) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR,
|
2017-12-12 15:02:30 +01:00
|
|
|
"Unable to allocate replacement "
|
|
|
|
"during variable expansion");
|
2017-12-05 10:40:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
char *newptr = newstr;
|
|
|
|
int offset = find - str;
|
|
|
|
strncpy(newptr, str, offset);
|
|
|
|
newptr += offset;
|
|
|
|
strncpy(newptr, var->value, vvlen);
|
|
|
|
newptr += vvlen;
|
|
|
|
strcpy(newptr, find + vnlen);
|
|
|
|
free(str);
|
|
|
|
str = newstr;
|
|
|
|
find = str + offset + vvlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == config->symbols->length) {
|
|
|
|
++find;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2018-01-31 05:09:21 +01:00
|
|
|
|
|
|
|
// the naming is intentional (albeit long): a workspace_output_cmp function
|
|
|
|
// would compare two structs in full, while this method only compares the
|
|
|
|
// workspace.
|
|
|
|
int workspace_output_cmp_workspace(const void *a, const void *b) {
|
2018-09-28 13:58:23 +02:00
|
|
|
const struct workspace_config *wsa = a, *wsb = b;
|
2018-01-31 05:09:21 +01:00
|
|
|
return lenient_strcmp(wsa->workspace, wsb->workspace);
|
|
|
|
}
|
2018-05-03 07:02:16 +02:00
|
|
|
|
2018-09-08 08:19:31 +02:00
|
|
|
static void find_font_height_iterator(struct sway_container *con, void *data) {
|
|
|
|
size_t amount_below_baseline = con->title_height - con->title_baseline;
|
|
|
|
size_t extended_height = config->font_baseline + amount_below_baseline;
|
|
|
|
if (extended_height > config->font_height) {
|
|
|
|
config->font_height = extended_height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_baseline_iterator(struct sway_container *con, void *data) {
|
2018-05-03 07:02:16 +02:00
|
|
|
bool *recalculate = data;
|
|
|
|
if (*recalculate) {
|
2018-09-08 08:19:31 +02:00
|
|
|
container_calculate_title_height(con);
|
2018-05-03 07:02:16 +02:00
|
|
|
}
|
2018-09-08 08:19:31 +02:00
|
|
|
if (con->title_baseline > config->font_baseline) {
|
|
|
|
config->font_baseline = con->title_baseline;
|
2018-05-03 07:02:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 10:25:31 +02:00
|
|
|
void config_update_font_height(bool recalculate) {
|
|
|
|
size_t prev_max_height = config->font_height;
|
2018-05-03 07:02:16 +02:00
|
|
|
config->font_height = 0;
|
2018-09-08 08:19:31 +02:00
|
|
|
config->font_baseline = 0;
|
2018-05-05 10:25:31 +02:00
|
|
|
|
2018-09-08 08:19:31 +02:00
|
|
|
root_for_each_container(find_baseline_iterator, &recalculate);
|
|
|
|
root_for_each_container(find_font_height_iterator, NULL);
|
2018-05-30 02:27:16 +02:00
|
|
|
|
2018-05-05 10:25:31 +02:00
|
|
|
if (config->font_height != prev_max_height) {
|
Implement type safe arguments and demote sway_container
This commit changes the meaning of sway_container so that it only refers
to layout containers and view containers. Workspaces, outputs and the
root are no longer known as containers. Instead, root, outputs,
workspaces and containers are all a type of node, and containers come in
two types: layout containers and view containers.
In addition to the above, this implements type safe variables. This
means we use specific types such as sway_output and sway_workspace
instead of generic containers or nodes. However, it's worth noting that
in a few places places (eg. seat focus and transactions) referring to
them in a generic way is unavoidable which is why we still use nodes in
some places.
If you want a TL;DR, look at node.h, as well as the struct definitions
for root, output, workspace and container. Note that sway_output now
contains a workspaces list, and workspaces now contain a tiling and
floating list, and containers now contain a pointer back to the
workspace.
There are now functions for seat_get_focused_workspace and
seat_get_focused_container. The latter will return NULL if a workspace
itself is focused. Most other seat functions like seat_get_focus and
seat_set_focus now accept and return nodes.
In the config->handler_context struct, current_container has been
replaced with three pointers: node, container and workspace. node is the
same as what current_container was, while workspace is the workspace
that the node resides on and container is the actual container, which
may be NULL if a workspace itself is focused.
The global root_container variable has been replaced with one simply
called root, which is a pointer to the sway_root instance.
The way outputs are created, enabled, disabled and destroyed has
changed. Previously we'd wrap the sway_output in a container when it is
enabled, but as we don't have containers any more it needs a different
approach. The output_create and output_destroy functions previously
created/destroyed the container, but now they create/destroy the
sway_output. There is a new function output_disable to disable an output
without destroying it.
Containers have a new view property. If this is populated then the
container is a view container, otherwise it's a layout container. Like
before, this property is immutable for the life of the container.
Containers have both a `sway_container *parent` and
`sway_workspace *workspace`. As we use specific types now, parent cannot
point to a workspace so it'll be NULL for containers which are direct
children of the workspace. The workspace property is set for all
containers, except those which are hidden in the scratchpad as they have
no workspace.
In some cases we need to refer to workspaces in a container-like way.
For example, workspaces have layout and children, but when using
specific types this makes it difficult. Likewise, it's difficult for a
container to get its parent's layout when the parent could be another
container or a workspace. To make it easier, some helper functions have
been created: container_parent_layout and container_get_siblings.
container_remove_child has been renamed to container_detach and
container_replace_child has been renamed to container_replace.
`container_handle_fullscreen_reparent(con, old_parent)` has had the
old_parent removed. We now unfullscreen the workspace when detaching the
container, so this function is simplified and only needs one argument
now.
container_notify_subtree_changed has been renamed to
container_update_representation. This is more descriptive of its
purpose. I also wanted to be able to call it with whatever container was
changed rather than the container's parent, which makes bubbling up to
the workspace easier.
There are now state structs per node thing. ie. sway_output_state,
sway_workspace_state and sway_container_state.
The focus, move and layout commands have been completely refactored to
work with the specific types. I considered making these a separate PR,
but I'd be backporting my changes only to replace them again, and it's
easier just to test everything at once.
2018-08-30 13:00:10 +02:00
|
|
|
arrange_root();
|
2018-05-05 10:25:31 +02:00
|
|
|
}
|
2018-05-03 07:02:16 +02:00
|
|
|
}
|