sway

i3-compatible Wayland compositor
git clone https://git.awy.one/sway
Log | Files | Refs | README | LICENSE

commit 733993a651c71f7e2198d505960d6bbd31e0e107
parent 0c8491f7d0c735299a25f0ab929f5d1e0866b929
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 18 Nov 2017 11:22:02 -0500

Move everything to sway/old/

Diffstat:
MCMakeLists.txt | 12++++++++----
Mcommon/log.c | 2+-
Dinclude/sway.h | 6------
Dinclude/sway/handlers.h | 11-----------
Dinclude/sway/ipc-server.h | 41-----------------------------------------
Rinclude/sway/border.h -> include/sway/old/border.h | 0
Rinclude/sway/commands.h -> include/sway/old/commands.h | 0
Rinclude/sway/config.h -> include/sway/old/config.h | 0
Rinclude/sway/container.h -> include/sway/old/container.h | 0
Rinclude/sway/criteria.h -> include/sway/old/criteria.h | 0
Rinclude/sway/focus.h -> include/sway/old/focus.h | 0
Rinclude/sway/input.h -> include/sway/old/input.h | 0
Rinclude/sway/input_state.h -> include/sway/old/input_state.h | 0
Rinclude/sway/ipc-json.h -> include/sway/old/ipc-json.h | 0
Ainclude/sway/old/ipc-server.h | 33+++++++++++++++++++++++++++++++++
Rinclude/sway/layout.h -> include/sway/old/layout.h | 0
Cinclude/sway/output.h -> include/sway/old/output.h | 0
Rinclude/sway/workspace.h -> include/sway/old/workspace.h | 0
Minclude/sway/output.h | 22+---------------------
Msway/CMakeLists.txt | 27---------------------------
Dsway/config.c | 1449-------------------------------------------------------------------------------
Dsway/debug_log.c | 103-------------------------------------------------------------------------------
Msway/desktop/output.c | 19++++---------------
Msway/desktop/xdg_shell_v6.c | 61+------------------------------------------------------------
Dsway/ipc-server.c | 845-------------------------------------------------------------------------------
Msway/main.c | 48+++++++++++++++++++++---------------------------
Rsway/border.c -> sway/old/border.c | 0
Rsway/commands.c -> sway/old/commands.c | 0
Rsway/commands/assign.c -> sway/old/commands/assign.c | 0
Rsway/commands/bar.c -> sway/old/commands/bar.c | 0
Rsway/commands/bar/activate_button.c -> sway/old/commands/bar/activate_button.c | 0
Rsway/commands/bar/binding_mode_indicator.c -> sway/old/commands/bar/binding_mode_indicator.c | 0
Rsway/commands/bar/bindsym.c -> sway/old/commands/bar/bindsym.c | 0
Rsway/commands/bar/colors.c -> sway/old/commands/bar/colors.c | 0
Rsway/commands/bar/context_button.c -> sway/old/commands/bar/context_button.c | 0
Rsway/commands/bar/font.c -> sway/old/commands/bar/font.c | 0
Rsway/commands/bar/height.c -> sway/old/commands/bar/height.c | 0
Rsway/commands/bar/hidden_state.c -> sway/old/commands/bar/hidden_state.c | 0
Rsway/commands/bar/icon_theme.c -> sway/old/commands/bar/icon_theme.c | 0
Rsway/commands/bar/id.c -> sway/old/commands/bar/id.c | 0
Rsway/commands/bar/mode.c -> sway/old/commands/bar/mode.c | 0
Rsway/commands/bar/modifier.c -> sway/old/commands/bar/modifier.c | 0
Rsway/commands/bar/output.c -> sway/old/commands/bar/output.c | 0
Rsway/commands/bar/pango_markup.c -> sway/old/commands/bar/pango_markup.c | 0
Rsway/commands/bar/position.c -> sway/old/commands/bar/position.c | 0
Rsway/commands/bar/secondary_button.c -> sway/old/commands/bar/secondary_button.c | 0
Rsway/commands/bar/separator_symbol.c -> sway/old/commands/bar/separator_symbol.c | 0
Rsway/commands/bar/status_command.c -> sway/old/commands/bar/status_command.c | 0
Rsway/commands/bar/strip_workspace_numbers.c -> sway/old/commands/bar/strip_workspace_numbers.c | 0
Rsway/commands/bar/swaybar_command.c -> sway/old/commands/bar/swaybar_command.c | 0
Rsway/commands/bar/tray_output.c -> sway/old/commands/bar/tray_output.c | 0
Rsway/commands/bar/tray_padding.c -> sway/old/commands/bar/tray_padding.c | 0
Rsway/commands/bar/workspace_buttons.c -> sway/old/commands/bar/workspace_buttons.c | 0
Rsway/commands/bar/wrap_scroll.c -> sway/old/commands/bar/wrap_scroll.c | 0
Rsway/commands/bind.c -> sway/old/commands/bind.c | 0
Rsway/commands/border.c -> sway/old/commands/border.c | 0
Rsway/commands/client.c -> sway/old/commands/client.c | 0
Rsway/commands/clipboard.c -> sway/old/commands/clipboard.c | 0
Rsway/commands/commands.c -> sway/old/commands/commands.c | 0
Rsway/commands/debuglog.c -> sway/old/commands/debuglog.c | 0
Rsway/commands/default_border.c -> sway/old/commands/default_border.c | 0
Rsway/commands/default_floating_border.c -> sway/old/commands/default_floating_border.c | 0
Rsway/commands/exec.c -> sway/old/commands/exec.c | 0
Rsway/commands/exec_always.c -> sway/old/commands/exec_always.c | 0
Rsway/commands/exit.c -> sway/old/commands/exit.c | 0
Rsway/commands/floating.c -> sway/old/commands/floating.c | 0
Rsway/commands/floating_maximum_size.c -> sway/old/commands/floating_maximum_size.c | 0
Rsway/commands/floating_minimum_size.c -> sway/old/commands/floating_minimum_size.c | 0
Rsway/commands/floating_mod.c -> sway/old/commands/floating_mod.c | 0
Rsway/commands/floating_scroll.c -> sway/old/commands/floating_scroll.c | 0
Rsway/commands/focus.c -> sway/old/commands/focus.c | 0
Rsway/commands/focus_follows_mouse.c -> sway/old/commands/focus_follows_mouse.c | 0
Rsway/commands/font.c -> sway/old/commands/font.c | 0
Rsway/commands/for_window.c -> sway/old/commands/for_window.c | 0
Rsway/commands/force_focus_wrapping.c -> sway/old/commands/force_focus_wrapping.c | 0
Rsway/commands/fullscreen.c -> sway/old/commands/fullscreen.c | 0
Rsway/commands/gaps.c -> sway/old/commands/gaps.c | 0
Rsway/commands/hide_edge_borders.c -> sway/old/commands/hide_edge_borders.c | 0
Rsway/commands/include.c -> sway/old/commands/include.c | 0
Rsway/commands/input.c -> sway/old/commands/input.c | 0
Rsway/commands/input/accel_profile.c -> sway/old/commands/input/accel_profile.c | 0
Rsway/commands/input/click_method.c -> sway/old/commands/input/click_method.c | 0
Rsway/commands/input/drag_lock.c -> sway/old/commands/input/drag_lock.c | 0
Rsway/commands/input/dwt.c -> sway/old/commands/input/dwt.c | 0
Rsway/commands/input/events.c -> sway/old/commands/input/events.c | 0
Rsway/commands/input/left_handed.c -> sway/old/commands/input/left_handed.c | 0
Rsway/commands/input/middle_emulation.c -> sway/old/commands/input/middle_emulation.c | 0
Rsway/commands/input/natural_scroll.c -> sway/old/commands/input/natural_scroll.c | 0
Rsway/commands/input/pointer_accel.c -> sway/old/commands/input/pointer_accel.c | 0
Rsway/commands/input/scroll_method.c -> sway/old/commands/input/scroll_method.c | 0
Rsway/commands/input/tap.c -> sway/old/commands/input/tap.c | 0
Rsway/commands/ipc.c -> sway/old/commands/ipc.c | 0
Rsway/commands/kill.c -> sway/old/commands/kill.c | 0
Rsway/commands/layout.c -> sway/old/commands/layout.c | 0
Rsway/commands/log_colors.c -> sway/old/commands/log_colors.c | 0
Rsway/commands/mark.c -> sway/old/commands/mark.c | 0
Rsway/commands/mode.c -> sway/old/commands/mode.c | 0
Rsway/commands/mouse_warping.c -> sway/old/commands/mouse_warping.c | 0
Rsway/commands/move.c -> sway/old/commands/move.c | 0
Rsway/commands/new_float.c -> sway/old/commands/new_float.c | 0
Rsway/commands/new_window.c -> sway/old/commands/new_window.c | 0
Rsway/commands/no_focus.c -> sway/old/commands/no_focus.c | 0
Rsway/commands/orientation.c -> sway/old/commands/orientation.c | 0
Rsway/commands/output.c -> sway/old/commands/output.c | 0
Rsway/commands/permit.c -> sway/old/commands/permit.c | 0
Rsway/commands/reload.c -> sway/old/commands/reload.c | 0
Rsway/commands/resize.c -> sway/old/commands/resize.c | 0
Rsway/commands/scratchpad.c -> sway/old/commands/scratchpad.c | 0
Rsway/commands/seamless_mouse.c -> sway/old/commands/seamless_mouse.c | 0
Rsway/commands/set.c -> sway/old/commands/set.c | 0
Rsway/commands/show_marks.c -> sway/old/commands/show_marks.c | 0
Rsway/commands/smart_gaps.c -> sway/old/commands/smart_gaps.c | 0
Rsway/commands/split.c -> sway/old/commands/split.c | 0
Rsway/commands/sticky.c -> sway/old/commands/sticky.c | 0
Rsway/commands/unmark.c -> sway/old/commands/unmark.c | 0
Rsway/commands/workspace.c -> sway/old/commands/workspace.c | 0
Rsway/commands/workspace_auto_back_and_forth.c -> sway/old/commands/workspace_auto_back_and_forth.c | 0
Rsway/commands/workspace_layout.c -> sway/old/commands/workspace_layout.c | 0
Asway/old/config.c | 1313+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsway/tree/container.c -> sway/old/container.c | 0
Rsway/tree/criteria.c -> sway/old/criteria.c | 0
Rsway/tree/focus.c -> sway/old/focus.c | 0
Rsway/handlers.c -> sway/old/handlers.c | 0
Rsway/input_state.c -> sway/old/input_state.c | 0
Rsway/ipc-json.c -> sway/old/ipc-json.c | 0
Asway/old/ipc-server.c | 840+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsway/tree/layout.c -> sway/old/layout.c | 0
Rsway/tree/output.c -> sway/old/output.c | 0
Rsway/security.c -> sway/old/security.c | 0
Rsway/tree/workspace.c -> sway/old/workspace.c | 0
Msway/server.c | 2--
131 files changed, 2222 insertions(+), 2612 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -116,7 +116,8 @@ add_subdirectory(wayland) add_subdirectory(sway) if(enable-swaybg) if(CAIRO_FOUND AND PANGO_FOUND) - add_subdirectory(swaybg) + # TODO WLR + #add_subdirectory(swaybg) else() message(WARNING "Not building swaybg - cairo, and pango are required.") endif() @@ -125,18 +126,21 @@ if(enable-swaymsg) add_subdirectory(swaymsg) endif() if(enable-swaygrab) - add_subdirectory(swaygrab) + # TODO WLR + #add_subdirectory(swaygrab) endif() if(enable-swaybar) if(CAIRO_FOUND AND PANGO_FOUND) - add_subdirectory(swaybar) + # TODO WLR + #add_subdirectory(swaybar) else() message(WARNING "Not building swaybar - cairo, and pango are required.") endif() endif() if(enable-swaylock) if(CAIRO_FOUND AND PANGO_FOUND AND PAM_FOUND) - add_subdirectory(swaylock) + # TODO WLR + #add_subdirectory(swaylock) else() message(WARNING "Not building swaylock - cairo, pango, and PAM are required.") endif() diff --git a/common/log.c b/common/log.c @@ -9,7 +9,6 @@ #include <string.h> #include <time.h> #include "log.h" -#include "sway.h" #include "readline.h" static int colored = 1; @@ -117,6 +116,7 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const va_end(args); } +void sway_terminate(int code); void _sway_abort(const char *filename, int line, const char* format, ...) { va_list args; diff --git a/include/sway.h b/include/sway.h @@ -1,6 +0,0 @@ -#ifndef _SWAY_SWAY_H -#define _SWAY_SWAY_H - -void sway_terminate(int exit_code); - -#endif diff --git a/include/sway/handlers.h b/include/sway/handlers.h @@ -1,11 +0,0 @@ -#ifndef _SWAY_HANDLERS_H -#define _SWAY_HANDLERS_H -#include "container.h" -#include <stdbool.h> -#include <wlc/wlc.h> - -void register_wlc_handlers(); - -extern uint32_t keys_pressed[32]; - -#endif diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h @@ -1,41 +0,0 @@ -#ifndef _SWAY_IPC_SERVER_H -#define _SWAY_IPC_SERVER_H - -#include <wlc/wlc.h> - -#include "container.h" -#include "config.h" -#include "ipc.h" - -void ipc_init(void); -void ipc_terminate(void); -struct sockaddr_un *ipc_user_sockaddr(void); - -void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change); -void ipc_event_barconfig_update(struct bar_config *bar); -/** - * Send IPC mode event to all listening clients - */ -void ipc_event_mode(const char *mode); -/** - * Send IPC window change event - */ -void ipc_event_window(swayc_t *window, const char *change); -/** - * Sends an IPC modifier event to all listening clients. The modifier event - * includes a key 'change' with the value of state and a key 'modifier' with - * the name of that modifier. - */ -void ipc_event_modifier(uint32_t modifier, const char *state); -/** - * Send IPC keyboard binding event. - */ -void ipc_event_binding_keyboard(struct sway_binding *sb); -const char *swayc_type_string(enum swayc_types type); - -/** - * Send pixel data to registered clients. - */ -void ipc_get_pixels(wlc_handle output); - -#endif diff --git a/include/sway/border.h b/include/sway/old/border.h diff --git a/include/sway/commands.h b/include/sway/old/commands.h diff --git a/include/sway/config.h b/include/sway/old/config.h diff --git a/include/sway/container.h b/include/sway/old/container.h diff --git a/include/sway/criteria.h b/include/sway/old/criteria.h diff --git a/include/sway/focus.h b/include/sway/old/focus.h diff --git a/include/sway/input.h b/include/sway/old/input.h diff --git a/include/sway/input_state.h b/include/sway/old/input_state.h diff --git a/include/sway/ipc-json.h b/include/sway/old/ipc-json.h diff --git a/include/sway/old/ipc-server.h b/include/sway/old/ipc-server.h @@ -0,0 +1,33 @@ +#ifndef _SWAY_IPC_SERVER_H +#define _SWAY_IPC_SERVER_H +#include "container.h" +#include "config.h" +#include "ipc.h" + +void ipc_init(void); +void ipc_terminate(void); +struct sockaddr_un *ipc_user_sockaddr(void); + +void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change); +void ipc_event_barconfig_update(struct bar_config *bar); +/** + * Send IPC mode event to all listening clients + */ +void ipc_event_mode(const char *mode); +/** + * Send IPC window change event + */ +void ipc_event_window(swayc_t *window, const char *change); +/** + * Sends an IPC modifier event to all listening clients. The modifier event + * includes a key 'change' with the value of state and a key 'modifier' with + * the name of that modifier. + */ +void ipc_event_modifier(uint32_t modifier, const char *state); +/** + * Send IPC keyboard binding event. + */ +void ipc_event_binding_keyboard(struct sway_binding *sb); +const char *swayc_type_string(enum swayc_types type); + +#endif diff --git a/include/sway/layout.h b/include/sway/old/layout.h diff --git a/include/sway/output.h b/include/sway/old/output.h diff --git a/include/sway/workspace.h b/include/sway/old/workspace.h diff --git a/include/sway/output.h b/include/sway/output.h @@ -3,34 +3,14 @@ #include <time.h> #include <wayland-server.h> #include <wlr/types/wlr_output.h> -#include "container.h" -#include "focus.h" struct sway_server; struct sway_output { struct wlr_output *wlr_output; - struct wl_listener frame; struct sway_server *server; struct timespec last_frame; + struct wl_listener frame; }; -// Position is absolute coordinates on the edge where the adjacent output -// should be searched for. -swayc_t *output_by_name(const char* name, const struct wlc_point *abs_pos); -swayc_t *swayc_opposite_output(enum movement_direction dir, const struct wlc_point *abs_pos); -swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, const struct wlc_point *abs_pos, bool pick_closest); - -// Place absolute coordinates for given container into given wlc_point. -void get_absolute_position(swayc_t *container, struct wlc_point *point); - -// Place absolute coordinates for the center point of given container into -// given wlc_point. -void get_absolute_center_position(swayc_t *container, struct wlc_point *point); - -// stable sort workspaces on this output -void sort_workspaces(swayc_t *output); - -void output_get_scaled_size(wlc_handle handle, struct wlc_size *size); - #endif diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt @@ -1,6 +1,5 @@ include_directories( ${PROTOCOLS_INCLUDE_DIRS} - ${WLC_INCLUDE_DIRS} ${WLR_INCLUDE_DIRS} ${PCRE_INCLUDE_DIRS} ${JSONC_INCLUDE_DIRS} @@ -11,37 +10,12 @@ include_directories( ${WAYLAND_INCLUDE_DIR} ) -file(GLOB cmds - "commands/*.c" - "commands/bar/*.c" - "commands/input/*.c" -) - add_executable(sway desktop/output.c desktop/xdg_shell_v6.c - tree/container.c - tree/criteria.c - tree/focus.c - tree/output.c - tree/workspace.c - tree/layout.c - - input/input.c - - commands.c - ${cmds} - base64.c - config.c - debug_log.c - input_state.c - ipc-json.c - ipc-server.c main.c - border.c - security.c server.c ) @@ -53,7 +27,6 @@ target_link_libraries(sway sway-common sway-protocols sway-wayland - ${WLC_LIBRARIES} ${WLR_LIBRARIES} ${XKBCOMMON_LIBRARIES} ${PCRE_LIBRARIES} diff --git a/sway/config.c b/sway/config.c @@ -1,1449 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#define _XOPEN_SOURCE 700 -#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 <float.h> -#include <dirent.h> -#include <strings.h> -#include <wlr/types/wlr_output.h> -#include "wayland-desktop-shell-server-protocol.h" -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/layout.h" -#include "sway/input_state.h" -#include "sway/criteria.h" -#include "sway/input.h" -#include "sway/border.h" -#include "readline.h" -#include "stringop.h" -#include "list.h" -#include "log.h" - -struct sway_config *config = NULL; - -static void terminate_swaybar(pid_t pid); - -static void free_variable(struct sway_variable *var) { - if (!var) { - return; - } - free(var->name); - free(var->value); - free(var); -} - -static void free_binding(struct sway_binding *bind) { - if (!bind) { - return; - } - free_flat_list(bind->keys); - free(bind->command); - free(bind); -} - -static void free_mode(struct sway_mode *mode) { - if (!mode) { - return; - } - free(mode->name); - int i; - for (i = 0; mode->bindings && i < mode->bindings->length; ++i) { - free_binding(mode->bindings->items[i]); - } - list_free(mode->bindings); - free(mode); -} - -static void free_bar(struct bar_config *bar) { - if (!bar) { - return; - } - free(bar->mode); - free(bar->hidden_state); -#ifdef ENABLE_TRAY - free(bar->tray_output); - free(bar->icon_theme); -#endif - free(bar->status_command); - free(bar->font); - free(bar->separator_symbol); - int i; - for (i = 0; bar->bindings && i < bar->bindings->length; ++i) { - free_sway_mouse_binding(bar->bindings->items[i]); - } - list_free(bar->bindings); - - if (bar->outputs) { - free_flat_list(bar->outputs); - } - - if (bar->pid != 0) { - terminate_swaybar(bar->pid); - } - - free(bar->colors.background); - free(bar->colors.statusline); - free(bar->colors.separator); - free(bar->colors.focused_background); - free(bar->colors.focused_statusline); - free(bar->colors.focused_separator); - free(bar->colors.focused_workspace_border); - free(bar->colors.focused_workspace_bg); - free(bar->colors.focused_workspace_text); - free(bar->colors.active_workspace_border); - free(bar->colors.active_workspace_bg); - free(bar->colors.active_workspace_text); - free(bar->colors.inactive_workspace_border); - free(bar->colors.inactive_workspace_bg); - free(bar->colors.inactive_workspace_text); - free(bar->colors.urgent_workspace_border); - free(bar->colors.urgent_workspace_bg); - free(bar->colors.urgent_workspace_text); - free(bar->colors.binding_mode_border); - free(bar->colors.binding_mode_bg); - free(bar->colors.binding_mode_text); - - free(bar); -} - -void free_input_config(struct input_config *ic) { - if (!ic) { - return; - } - free(ic->identifier); - free(ic); -} - -void free_output_config(struct output_config *oc) { - if (!oc) { - return; - } - free(oc->name); - free(oc->background); - free(oc->background_option); - free(oc); -} - -static void free_workspace_output(struct workspace_output *wo) { - if (!wo) { - return; - } - free(wo->output); - free(wo->workspace); - free(wo); -} - -static void pid_workspace_cleanup() { - struct timespec ts; - struct pid_workspace *pw = NULL; - - clock_gettime(CLOCK_MONOTONIC, &ts); - - // work backwards through list and remove any entries - // older than PID_WORKSPACE_TIMEOUT - for (int i = config->pid_workspaces->length - 1; i > -1; i--) { - pw = config->pid_workspaces->items[i]; - - if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) { - free_pid_workspace(config->pid_workspaces->items[i]); - list_del(config->pid_workspaces, i); - } - } -} - -// de-dupe pid_workspaces to ensure pid uniqueness -void pid_workspace_add(struct pid_workspace *pw) { - struct pid_workspace *list_pw = NULL; - struct timespec ts; - time_t *now = malloc(sizeof(time_t)); - if (!now) { - sway_log(L_ERROR, "Allocating time for pid_workspace failed"); - return; - } - - pid_workspace_cleanup(); - - // add current time to pw - clock_gettime(CLOCK_MONOTONIC, &ts); - *now = ts.tv_sec; - - pw->time_added = now; - - // work backwards through list and delete any entries that - // have the same pid as that in our new pid_workspace - for (int i = config->pid_workspaces->length - 1; i > -1; i--) { - list_pw = config->pid_workspaces->items[i]; - - if (pw->pid == list_pw->pid) { - free_pid_workspace(config->pid_workspaces->items[i]); - list_del(config->pid_workspaces, i); - } - } - - list_add(config->pid_workspaces, pw); -} - -void free_pid_workspace(struct pid_workspace *pw) { - if (!pw) { - return; - } - free(pw->pid); - free(pw->workspace); - free(pw->time_added); - free(pw); -} - -void free_command_policy(struct command_policy *policy) { - if (!policy) { - return; - } - free(policy->command); - free(policy); -} - -void free_feature_policy(struct feature_policy *policy) { - if (!policy) { - return; - } - free(policy->program); - free(policy); -} - -void free_config(struct sway_config *config) { - if (!config) { - return; - } - int i; - for (i = 0; config->symbols && i < config->symbols->length; ++i) { - free_variable(config->symbols->items[i]); - } - list_free(config->symbols); - - for (i = 0; config->modes && i < config->modes->length; ++i) { - free_mode(config->modes->items[i]); - } - list_free(config->modes); - - for (i = 0; config->bars && i < config->bars->length; ++i) { - free_bar(config->bars->items[i]); - } - list_free(config->bars); - - free_flat_list(config->cmd_queue); - - for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) { - free_workspace_output(config->workspace_outputs->items[i]); - } - list_free(config->workspace_outputs); - - for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) { - free_pid_workspace(config->pid_workspaces->items[i]); - } - list_free(config->pid_workspaces); - - for (i = 0; config->criteria && i < config->criteria->length; ++i) { - free_criteria(config->criteria->items[i]); - } - list_free(config->criteria); - - for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { - free_criteria(config->no_focus->items[i]); - } - list_free(config->no_focus); - - for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { - free_input_config(config->input_configs->items[i]); - } - list_free(config->input_configs); - - for (i = 0; config->output_configs && i < config->output_configs->length; ++i) { - free_output_config(config->output_configs->items[i]); - } - list_free(config->output_configs); - - for (i = 0; config->command_policies && i < config->command_policies->length; ++i) { - free_command_policy(config->command_policies->items[i]); - } - list_free(config->command_policies); - - for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) { - free_feature_policy(config->feature_policies->items[i]); - } - list_free(config->feature_policies); - - list_free(config->active_bar_modifiers); - free_flat_list(config->config_chain); - free(config->font); - 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); -} - - -static bool file_exists(const char *path) { - return path && access(path, R_OK) != -1; -} - -static void config_defaults(struct sway_config *config) { - if (!(config->symbols = create_list())) goto cleanup; - if (!(config->modes = create_list())) goto cleanup; - if (!(config->bars = create_list())) goto cleanup; - if (!(config->workspace_outputs = create_list())) goto cleanup; - if (!(config->pid_workspaces = create_list())) goto cleanup; - if (!(config->criteria = create_list())) goto cleanup; - if (!(config->no_focus = create_list())) goto cleanup; - if (!(config->input_configs = create_list())) goto cleanup; - if (!(config->output_configs = create_list())) goto cleanup; - - if (!(config->cmd_queue = create_list())) goto cleanup; - - if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) goto cleanup; - if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup; - strcpy(config->current_mode->name, "default"); - if (!(config->current_mode->bindings = create_list())) goto cleanup; - list_add(config->modes, config->current_mode); - - config->floating_mod = 0; - config->dragging_key = M_LEFT_CLICK; - config->resizing_key = M_RIGHT_CLICK; - 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; - config->font_height = get_font_text_height(config->font); - - // floating view - config->floating_maximum_width = 0; - config->floating_maximum_height = 0; - config->floating_minimum_width = 75; - config->floating_minimum_height = 50; - - // Flags - config->focus_follows_mouse = true; - config->mouse_warping = true; - config->reloading = false; - config->active = false; - config->failed = false; - config->auto_back_and_forth = false; - config->seamless_mouse = true; - config->reading = false; - config->show_marks = true; - - config->edge_gaps = true; - config->smart_gaps = false; - config->gaps_inner = 0; - config->gaps_outer = 0; - - if (!(config->active_bar_modifiers = create_list())) goto cleanup; - - if (!(config->config_chain = create_list())) goto cleanup; - 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; - - // border colors - config->border_colors.focused.border = 0x4C7899FF; - config->border_colors.focused.background = 0x285577FF; - config->border_colors.focused.text = 0xFFFFFFFF; - config->border_colors.focused.indicator = 0x2E9EF4FF; - config->border_colors.focused.child_border = 0x285577FF; - - config->border_colors.focused_inactive.border = 0x333333FF; - config->border_colors.focused_inactive.background = 0x5F676AFF; - config->border_colors.focused_inactive.text = 0xFFFFFFFF; - config->border_colors.focused_inactive.indicator = 0x484E50FF; - config->border_colors.focused_inactive.child_border = 0x5F676AFF; - - config->border_colors.unfocused.border = 0x333333FF; - config->border_colors.unfocused.background = 0x222222FF; - config->border_colors.unfocused.text = 0x888888FF; - config->border_colors.unfocused.indicator = 0x292D2EFF; - config->border_colors.unfocused.child_border = 0x222222FF; - - config->border_colors.urgent.border = 0x2F343AFF; - config->border_colors.urgent.background = 0x900000FF; - config->border_colors.urgent.text = 0xFFFFFFFF; - config->border_colors.urgent.indicator = 0x900000FF; - config->border_colors.urgent.child_border = 0x900000FF; - - config->border_colors.placeholder.border = 0x000000FF; - config->border_colors.placeholder.background = 0x0C0C0CFF; - config->border_colors.placeholder.text = 0xFFFFFFFF; - config->border_colors.placeholder.indicator = 0x000000FF; - config->border_colors.placeholder.child_border = 0x0C0C0CFF; - - config->border_colors.background = 0xFFFFFFFF; - - // 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 int compare_modifiers(const void *left, const void *right) { - uint32_t a = *(uint32_t *)left; - uint32_t b = *(uint32_t *)right; - - return a - b; -} - -void update_active_bar_modifiers() { - if (config->active_bar_modifiers->length > 0) { - list_free(config->active_bar_modifiers); - config->active_bar_modifiers = create_list(); - } - - struct bar_config *bar; - int i; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; - if (strcmp(bar->mode, "hide") == 0 && strcmp(bar->hidden_state, "hide") == 0) { - if (list_seq_find(config->active_bar_modifiers, compare_modifiers, &bar->modifier) < 0) { - list_add(config->active_bar_modifiers, &bar->modifier); - } - } - } -} - -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", - }; - - if (!getenv("XDG_CONFIG_HOME")) { - char *home = getenv("HOME"); - char *config_home = malloc(strlen(home) + strlen("/.config") + 1); - if (!config_home) { - sway_log(L_ERROR, "Unable to allocate $HOME/.config"); - } else { - strcpy(config_home, home); - strcat(config_home, "/.config"); - setenv("XDG_CONFIG_HOME", config_home, 1); - sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); - free(config_home); - } - } - - wordexp_t p; - char *path; - - int i; - for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) { - if (wordexp(config_paths[i], &p, 0) == 0) { - path = strdup(p.we_wordv[0]); - wordfree(&p); - if (file_exists(path)) { - return path; - } - } - } - - return NULL; // Not reached -} - -const char *current_config_path; - -static bool load_config(const char *path, struct sway_config *config) { - sway_log(L_INFO, "Loading config from %s", path); - current_config_path = path; - - struct stat sb; - if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { - return false; - } - - if (path == NULL) { - sway_log(L_ERROR, "Unable to find a config file!"); - return false; - } - - FILE *f = fopen(path, "r"); - if (!f) { - sway_log(L_ERROR, "Unable to open %s for reading", path); - return false; - } - - bool config_load_success = read_config(f, config); - fclose(f); - - if (!config_load_success) { - sway_log(L_ERROR, "Error(s) loading config!"); - } - - current_config_path = NULL; - return true; -} - -static int qstrcmp(const void* a, const void* b) { - return strcmp(*((char**) a), *((char**) b)); -} - -bool load_main_config(const char *file, bool is_active) { - input_init(); - - 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); - if (is_active) { - sway_log(L_DEBUG, "Performing configuration file reload"); - config->reloading = true; - config->active = true; - } - - config->current_config = path; - list_add(config->config_chain, path); - - config->reading = true; - - // Read security configs - bool success = true; - DIR *dir = opendir(SYSCONFDIR "/sway/security.d"); - if (!dir) { - sway_log(L_ERROR, "%s does not exist, sway will have no security configuration" - " and will probably be broken", SYSCONFDIR "/sway/security.d"); - } 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]; - if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || (((s.st_mode & 0777) != 0644) && (s.st_mode & 0777) != 0444)) { - sway_log(L_ERROR, "Refusing to load %s - it must be owned by root and mode 644 or 444", _path); - success = false; - } else { - success = success && load_config(_path, config); - } - } - - free_flat_list(secconfigs); - } - - success = success && load_config(path, config); - - if (is_active) { - config->reloading = false; - } - - if (old_config) { - free_config(old_config); - } - config->reading = false; - - if (success) { - update_active_bar_modifiers(); - } - - return success; -} - -static bool load_include_config(const char *path, const char *parent_dir, struct sway_config *config) { - // save parent config - const char *parent_config = config->current_config; - - char *full_path = strdup(path); - int len = strlen(path); - if (len >= 1 && path[0] != '/') { - len = len + strlen(parent_dir) + 2; - full_path = malloc(len * sizeof(char)); - if (!full_path) { - sway_log(L_ERROR, "Unable to allocate full path to included config"); - return false; - } - snprintf(full_path, len, "%s/%s", parent_dir, path); - } - - char *real_path = realpath(full_path, NULL); - free(full_path); - - if (real_path == NULL) { - sway_log(L_DEBUG, "%s not found.", path); - 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) { - sway_log(L_DEBUG, "%s already included once, won't be included again.", real_path); - free(real_path); - return false; - } - } - - config->current_config = real_path; - list_add(config->config_chain, real_path); - int index = config->config_chain->length - 1; - - if (!load_config(real_path, config)) { - free(real_path); - config->current_config = parent_config; - list_del(config->config_chain, index); - return false; - } - - // restore current_config - config->current_config = parent_config; - return true; -} - -bool load_include_configs(const char *path, struct sway_config *config) { - char *wd = getcwd(NULL, 0); - char *parent_path = strdup(config->current_config); - 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) { - load_include_config(w[i], parent_dir, config); - } - free(parent_path); - wordfree(&p); - - // restore wd - if (chdir(wd) < 0) { - free(wd); - sway_log(L_ERROR, "failed to restore working directory"); - return false; - } - - free(wd); - return true; -} - -struct cmd_results *check_security_config() { - if (!current_config_path || strncmp(SYSCONFDIR "/sway/security.d/", current_config_path, - strlen(SYSCONFDIR "/sway/security.d/")) != 0) { - return cmd_results_new(CMD_INVALID, "permit", - "This command is only permitted to run from " SYSCONFDIR "/sway/security.d/*"); - } - return NULL; -} - -bool read_config(FILE *file, struct sway_config *config) { - bool success = true; - enum cmd_status block = CMD_BLOCK_END; - - int line_number = 0; - char *line; - while (!feof(file)) { - line = read_line(file); - if (!line) { - continue; - } - line_number++; - line = strip_whitespace(line); - if (line[0] == '#') { - free(line); - continue; - } - struct cmd_results *res; - if (block == CMD_BLOCK_COMMANDS) { - // Special case - res = config_commands_command(line); - } else { - res = config_command(line, block); - } - switch(res->status) { - case CMD_FAILURE: - case CMD_INVALID: - sway_log(L_ERROR, "Error on line %i '%s': %s (%s)", line_number, line, - res->error, config->current_config); - success = false; - break; - - case CMD_DEFER: - sway_log(L_DEBUG, "Defferring command `%s'", line); - list_add(config->cmd_queue, strdup(line)); - break; - - case CMD_BLOCK_MODE: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_MODE; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_INPUT: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_INPUT; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_BAR: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_BAR; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_BAR_COLORS: - if (block == CMD_BLOCK_BAR) { - block = CMD_BLOCK_BAR_COLORS; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_COMMANDS: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_COMMANDS; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_IPC: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_IPC; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_IPC_EVENTS: - if (block == CMD_BLOCK_IPC) { - block = CMD_BLOCK_IPC_EVENTS; - } else { - sway_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_END: - switch(block) { - case CMD_BLOCK_MODE: - sway_log(L_DEBUG, "End of mode block"); - config->current_mode = config->modes->items[0]; - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_INPUT: - sway_log(L_DEBUG, "End of input block"); - current_input_config = NULL; - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_BAR: - sway_log(L_DEBUG, "End of bar block"); - config->current_bar = NULL; - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_BAR_COLORS: - sway_log(L_DEBUG, "End of bar colors block"); - block = CMD_BLOCK_BAR; - break; - - case CMD_BLOCK_COMMANDS: - sway_log(L_DEBUG, "End of commands block"); - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_IPC: - sway_log(L_DEBUG, "End of IPC block"); - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_IPC_EVENTS: - sway_log(L_DEBUG, "End of IPC events block"); - block = CMD_BLOCK_IPC; - break; - - case CMD_BLOCK_END: - sway_log(L_ERROR, "Unmatched }"); - break; - - default:; - } - default:; - } - free(line); - free_cmd_results(res); - } - - return success; -} - -int input_identifier_cmp(const void *item, const void *data) { - const struct input_config *ic = item; - const char *identifier = data; - return strcmp(ic->identifier, identifier); -} - -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); -} - -void merge_input_config(struct input_config *dst, struct input_config *src) { - if (src->identifier) { - if (dst->identifier) { - free(dst->identifier); - } - dst->identifier = strdup(src->identifier); - } - if (src->accel_profile != INT_MIN) { - dst->accel_profile = src->accel_profile; - } - if (src->click_method != INT_MIN) { - dst->click_method = src->click_method; - } - if (src->drag_lock != INT_MIN) { - dst->drag_lock = src->drag_lock; - } - if (src->dwt != INT_MIN) { - dst->dwt = src->dwt; - } - if (src->middle_emulation != INT_MIN) { - dst->middle_emulation = src->middle_emulation; - } - if (src->natural_scroll != INT_MIN) { - dst->natural_scroll = src->natural_scroll; - } - if (src->pointer_accel != FLT_MIN) { - dst->pointer_accel = src->pointer_accel; - } - if (src->scroll_method != INT_MIN) { - dst->scroll_method = src->scroll_method; - } - if (src->send_events != INT_MIN) { - dst->send_events = src->send_events; - } - if (src->tap != INT_MIN) { - dst->tap = src->tap; - } -} - -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->scale != -1) { - dst->scale = src->scale; - } - 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); - } -} - -static void invoke_swaybar(struct bar_config *bar) { - return; // TODO WLR - sway_log(L_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); - // Pipe to communicate errors - int filedes[2]; - if (pipe(filedes) == -1) { - sway_log(L_ERROR, "Pipe setup failed! Cannot fork into bar"); - return; - } - - bar->pid = fork(); - if (bar->pid == 0) { - close(filedes[0]); - if (!bar->swaybar_command) { - char *const cmd[] = { - "swaybar", - "-b", - bar->id, - NULL, - }; - - close(filedes[1]); - execvp(cmd[0], cmd); - _exit(EXIT_SUCCESS); - } else { - // run custom swaybar - int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; - char *command = malloc(len * sizeof(char)); - if (!command) { - const char msg[] = "Unable to allocate swaybar command string"; - int len = sizeof(msg); - if (write(filedes[1], &len, sizeof(int))) {}; - if (write(filedes[1], msg, len)) {}; - close(filedes[1]); - _exit(EXIT_FAILURE); - } - snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); - - char *const cmd[] = { - "sh", - "-c", - command, - NULL, - }; - - close(filedes[1]); - execvp(cmd[0], cmd); - free(command); - _exit(EXIT_SUCCESS); - } - } - close(filedes[0]); - int len; - if(read(filedes[1], &len, sizeof(int)) == sizeof(int)) { - char *buf = malloc(len); - if(!buf) { - sway_log(L_ERROR, "Cannot allocate error string"); - return; - } - if(read(filedes[1], buf, len)) { - sway_log(L_ERROR, "%s", buf); - } - free(buf); - } - close(filedes[1]); -} - -static void terminate_swaybar(pid_t pid) { - int ret = kill(pid, SIGTERM); - if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", pid); - } else { - int status; - waitpid(pid, &status, 0); - } -} - -void terminate_swaybg(pid_t pid) { - int ret = kill(pid, SIGTERM); - if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", pid); - } else { - int status; - waitpid(pid, &status, 0); - } -} - -static bool active_output(const char *name) { - int i; - swayc_t *cont = NULL; - for (i = 0; i < root_container.children->length; ++i) { - cont = root_container.children->items[i]; - if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { - return true; - } - } - - return false; -} - -void load_swaybars() { - // Check for bars - list_t *bars = create_list(); - struct bar_config *bar = NULL; - int i; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; - bool apply = false; - if (bar->outputs) { - int j; - for (j = 0; j < bar->outputs->length; ++j) { - char *o = bar->outputs->items[j]; - if (!strcmp(o, "*") || active_output(o)) { - apply = true; - break; - } - } - } else { - apply = true; - } - if (apply) { - list_add(bars, bar); - } - } - - for (i = 0; i < bars->length; ++i) { - bar = bars->items[i]; - if (bar->pid != 0) { - terminate_swaybar(bar->pid); - } - invoke_swaybar(bar); - } - - list_free(bars); -} - -void apply_input_config(struct input_config *ic, struct libinput_device *dev) { - if (!ic) { - return; - } - - sway_log(L_DEBUG, "apply_input_config(%s)", ic->identifier); - - if (ic->accel_profile != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) accel_set_profile(%d)", ic->identifier, ic->accel_profile); - libinput_device_config_accel_set_profile(dev, ic->accel_profile); - } - if (ic->click_method != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) click_set_method(%d)", ic->identifier, ic->click_method); - libinput_device_config_click_set_method(dev, ic->click_method); - } - if (ic->drag_lock != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) tap_set_drag_lock_enabled(%d)", ic->identifier, ic->click_method); - libinput_device_config_tap_set_drag_lock_enabled(dev, ic->drag_lock); - } - if (ic->dwt != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) dwt_set_enabled(%d)", ic->identifier, ic->dwt); - libinput_device_config_dwt_set_enabled(dev, ic->dwt); - } - if (ic->left_handed != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) left_handed_set_enabled(%d)", ic->identifier, ic->left_handed); - libinput_device_config_left_handed_set(dev, ic->left_handed); - } - if (ic->middle_emulation != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) middle_emulation_set_enabled(%d)", ic->identifier, ic->middle_emulation); - libinput_device_config_middle_emulation_set_enabled(dev, ic->middle_emulation); - } - if (ic->natural_scroll != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) natural_scroll_set_enabled(%d)", ic->identifier, ic->natural_scroll); - libinput_device_config_scroll_set_natural_scroll_enabled(dev, ic->natural_scroll); - } - if (ic->pointer_accel != FLT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) accel_set_speed(%f)", ic->identifier, ic->pointer_accel); - libinput_device_config_accel_set_speed(dev, ic->pointer_accel); - } - if (ic->scroll_method != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) scroll_set_method(%d)", ic->identifier, ic->scroll_method); - libinput_device_config_scroll_set_method(dev, ic->scroll_method); - } - if (ic->send_events != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) send_events_set_mode(%d)", ic->identifier, ic->send_events); - libinput_device_config_send_events_set_mode(dev, ic->send_events); - } - if (ic->tap != INT_MIN) { - sway_log(L_DEBUG, "apply_input_config(%s) tap_set_enabled(%d)", ic->identifier, ic->tap); - libinput_device_config_tap_set_enabled(dev, ic->tap); - } -} - -void apply_output_config(struct output_config *oc, swayc_t *output) { - if (oc && oc->enabled == 0) { - destroy_output(output); - return; - } - - if (oc && oc->width > 0 && oc->height > 0) { - output->width = oc->width; - output->height = oc->height; - - sway_log(L_DEBUG, "Set %s size to %ix%i (%d)", oc->name, oc->width, oc->height, oc->scale); - // TODO WLR: modes - //struct wlc_size new_size = { .w = oc->width, .h = oc->height }; - //wlc_output_set_resolution(output->handle, &new_size, (uint32_t)oc->scale); - } else if (oc) { - //const struct wlc_size *new_size = wlc_output_get_resolution(output->handle); - //wlc_output_set_resolution(output->handle, new_size, (uint32_t)oc->scale); - } - - // TODO WLR: wlr_output_layout - // Find position for it - if (oc && oc->x != -1 && oc->y != -1) { - sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y); - output->x = oc->x; - output->y = oc->y; - } else { - int x = 0; - for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; - if (c->type == C_OUTPUT) { - if (c->width + c->x > x) { - x = c->width + c->x; - } - } - } - output->x = x; - } - - 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; - } - } - - /* TODO WLR - if (oc && oc->background) { - if (output->bg_pid != 0) { - terminate_swaybg(output->bg_pid); - } - - sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); - - size_t bufsize = 12; - char output_id[bufsize]; - snprintf(output_id, bufsize, "%d", output_i); - output_id[bufsize-1] = 0; - - char *const cmd[] = { - "swaybg", - output_id, - oc->background, - oc->background_option, - NULL, - }; - - output->bg_pid = fork(); - if (output->bg_pid == 0) { - execvp(cmd[0], cmd); - } - } - */ -} - -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; - } - } - // 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) { - sway_log(L_ERROR, - "Unable to allocate replacement during variable expansion"); - 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; -} - -// 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) { - const struct workspace_output *wsa = a, *wsb = b; - return lenient_strcmp(wsa->workspace, wsb->workspace); -} - -int sway_binding_cmp_keys(const void *a, const void *b) { - const struct sway_binding *binda = a, *bindb = b; - - // Count keys pressed for this binding. important so we check long before - // short ones. for example mod+a+b before mod+a - unsigned int moda = 0, modb = 0, i; - - // Count how any modifiers are pressed - for (i = 0; i < 8 * sizeof(binda->modifiers); ++i) { - moda += (binda->modifiers & 1 << i) != 0; - modb += (bindb->modifiers & 1 << i) != 0; - } - if (bindb->keys->length + modb != binda->keys->length + moda) { - return (bindb->keys->length + modb) - (binda->keys->length + moda); - } - - // Otherwise compare keys - if (binda->modifiers > bindb->modifiers) { - return 1; - } else if (binda->modifiers < bindb->modifiers) { - return -1; - } - struct wlc_modifiers no_mods = { 0, 0 }; - for (int i = 0; i < binda->keys->length; i++) { - xkb_keysym_t ka = *(xkb_keysym_t *)binda->keys->items[i], - kb = *(xkb_keysym_t *)bindb->keys->items[i]; - if (binda->bindcode) { - uint32_t *keycode = binda->keys->items[i]; - ka = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); - } - - if (bindb->bindcode) { - uint32_t *keycode = bindb->keys->items[i]; - kb = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); - } - - if (ka > kb) { - return 1; - } else if (ka < kb) { - return -1; - } - } - - return 0; -} - -int sway_binding_cmp(const void *a, const void *b) { - int cmp = 0; - if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { - return cmp; - } - const struct sway_binding *binda = a, *bindb = b; - return lenient_strcmp(binda->command, bindb->command); -} - -int sway_binding_cmp_qsort(const void *a, const void *b) { - return sway_binding_cmp(*(void **)a, *(void **)b); -} - -void free_sway_binding(struct sway_binding *binding) { - if (binding->keys) { - for (int i = 0; i < binding->keys->length; i++) { - free(binding->keys->items[i]); - } - list_free(binding->keys); - } - if (binding->command) { - free(binding->command); - } - free(binding); -} - -int sway_mouse_binding_cmp_buttons(const void *a, const void *b) { - const struct sway_mouse_binding *binda = a, *bindb = b; - if (binda->button > bindb->button) { - return 1; - } - if (binda->button < bindb->button) { - return -1; - } - return 0; -} - -int sway_mouse_binding_cmp(const void *a, const void *b) { - int cmp = 0; - if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { - return cmp; - } - const struct sway_mouse_binding *binda = a, *bindb = b; - return lenient_strcmp(binda->command, bindb->command); -} - -int sway_mouse_binding_cmp_qsort(const void *a, const void *b) { - return sway_mouse_binding_cmp(*(void **)a, *(void **)b); -} - -void free_sway_mouse_binding(struct sway_mouse_binding *binding) { - if (binding->command) { - free(binding->command); - } - free(binding); -} - -struct sway_binding *sway_binding_dup(struct sway_binding *sb) { - struct sway_binding *new_sb = malloc(sizeof(struct sway_binding)); - if (!new_sb) { - return NULL; - } - - new_sb->order = sb->order; - new_sb->modifiers = sb->modifiers; - new_sb->command = strdup(sb->command); - - new_sb->keys = create_list(); - int i; - for (i = 0; i < sb->keys->length; ++i) { - xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); - if (!key) { - free_sway_binding(new_sb); - return NULL; - } - *key = *(xkb_keysym_t *)sb->keys->items[i]; - list_add(new_sb->keys, key); - } - - return new_sb; -} - -struct bar_config *default_bar_config(void) { - struct bar_config *bar = NULL; - bar = malloc(sizeof(struct bar_config)); - if (!bar) { - return NULL; - } - if (!(bar->mode = strdup("dock"))) goto cleanup; - if (!(bar->hidden_state = strdup("hide"))) goto cleanup; - bar->modifier = WLC_BIT_MOD_LOGO; - bar->outputs = NULL; - bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; - if (!(bar->bindings = create_list())) goto cleanup; - if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p'; sleep 1; done"))) goto cleanup; - bar->pango_markup = false; - bar->swaybar_command = NULL; - bar->font = NULL; - bar->height = -1; - bar->workspace_buttons = true; - bar->wrap_scroll = false; - bar->separator_symbol = NULL; - bar->strip_workspace_numbers = false; - bar->binding_mode_indicator = true; -#ifdef ENABLE_TRAY - bar->tray_output = NULL; - bar->icon_theme = NULL; - bar->tray_padding = 2; - bar->activate_button = 0x110; /* BTN_LEFT */ - bar->context_button = 0x111; /* BTN_RIGHT */ - bar->secondary_button = 0x112; /* BTN_MIDDLE */ -#endif - bar->verbose = false; - bar->pid = 0; - // set default colors - if (!(bar->colors.background = strndup("#000000ff", 9))) goto cleanup; - if (!(bar->colors.statusline = strndup("#ffffffff", 9))) goto cleanup; - if (!(bar->colors.separator = strndup("#666666ff", 9))) goto cleanup; - if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) goto cleanup; - if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) goto cleanup; - if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) goto cleanup; - if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) goto cleanup; - if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) goto cleanup; - if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) goto cleanup; - if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) goto cleanup; - if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) goto cleanup; - if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) goto cleanup; - if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) goto cleanup; - if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) goto cleanup; - if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) goto cleanup; - // if the following colors stay undefined, they fall back to background, - // statusline, separator and urgent_workspace_*. - bar->colors.focused_background = NULL; - bar->colors.focused_statusline = NULL; - bar->colors.focused_separator = NULL; - bar->colors.binding_mode_border = NULL; - bar->colors.binding_mode_bg = NULL; - bar->colors.binding_mode_text = NULL; - - list_add(config->bars, bar); - - return bar; - -cleanup: - free_bar(bar); - return NULL; -} diff --git a/sway/debug_log.c b/sway/debug_log.c @@ -1,103 +0,0 @@ -#include "log.h" -#include "sway.h" -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <libgen.h> -#include <fcntl.h> -#include <unistd.h> -#include <signal.h> -#include <errno.h> -#include <string.h> -#include <stringop.h> -#include "sway/workspace.h" - -/* XXX:DEBUG:XXX */ -static void container_log(const swayc_t *c, int depth) { - fprintf(stderr, "focus:%c", - c == get_focused_view(&root_container) ? 'K': - c == get_focused_container(&root_container) ? 'F' : // Focused - c == swayc_active_workspace() ? 'W' : // active workspace - c == &root_container ? 'R' : // root - 'X');// not any others - for (int i = 6; i > depth; i--) { fprintf(stderr, " "); } - fprintf(stderr,"|(%p)",c); - fprintf(stderr,"(p:%-8p)",c->parent); - fprintf(stderr,"(f:%-8p)",c->focused); - fprintf(stderr,"(h:%2" PRIuPTR ")",c->handle); - fprintf(stderr,"Type:%-4s|", - c->type == C_ROOT ? "root" : - c->type == C_OUTPUT ? "op" : - c->type == C_WORKSPACE ? "ws" : - c->type == C_CONTAINER ? "cont" : - c->type == C_VIEW ? "view" : "?"); - fprintf(stderr,"layout:%-5s|", - c->layout == L_NONE ? "-" : - c->layout == L_HORIZ ? "Horiz": - c->layout == L_VERT ? "Vert": - c->layout == L_STACKED ? "Stack": - c->layout == L_TABBED ? "Tab": - c->layout == L_FLOATING ? "Float": - c->layout == L_AUTO_LEFT ? "A_lft": - c->layout == L_AUTO_RIGHT ? "A_rgt": - c->layout == L_AUTO_TOP ? "A_top": - c->layout == L_AUTO_BOTTOM ? "A_bot": - "Unknown"); - fprintf(stderr, "w:%4.f|h:%4.f|", c->width, c->height); - fprintf(stderr, "x:%4.f|y:%4.f|", c->x, c->y); - fprintf(stderr, "g:%3d|",c->gaps); - fprintf(stderr, "vis:%c|", c->visible?'t':'f'); - fprintf(stderr, "children:%2d|",c->children?c->children->length:0); - fprintf(stderr, "name:%.16s\n", c->name); -} -void layout_log(const swayc_t *c, int depth) { - if (L_DEBUG > get_log_level()) return; - int i, d; - int e = c->children ? c->children->length : 0; - container_log(c, depth); - if (e) { - for (i = 0; i < e; ++i) { - fputc('|',stderr); - for (d = 0; d < depth; ++d) fputc('-', stderr); - layout_log(c->children->items[i], depth + 1); - } - } - if (c->type == C_WORKSPACE) { - e = c->floating?c->floating->length:0; - if (e) { - for (i = 0; i < e; ++i) { - fputc('|',stderr); - for (d = 0; d < depth; ++d) fputc('=', stderr); - layout_log(c->floating->items[i], depth + 1); - } - } - } -} - -const char *swayc_type_string(enum swayc_types type) { - return type == C_ROOT ? "ROOT" : - type == C_OUTPUT ? "OUTPUT" : - type == C_WORKSPACE ? "WORKSPACE" : - type == C_CONTAINER ? "CONTAINER" : - type == C_VIEW ? "VIEW" : - "UNKNOWN"; -} - -// Like sway_log, but also appends some info about given container to log output. -void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) { - sway_assert(cont, "swayc_log: no container ..."); - va_list args; - va_start(args, format); - char *txt = malloc(128); - vsprintf(txt, format, args); - va_end(args); - - char *debug_txt = malloc(32); - snprintf(debug_txt, 32, "%s '%s'", swayc_type_string(cont->type), cont->name); - - sway_log(verbosity, "%s (%s)", txt, debug_txt); - free(txt); - free(debug_txt); -} - -/* XXX:DEBUG:XXX */ diff --git a/sway/desktop/output.c b/sway/desktop/output.c @@ -4,11 +4,9 @@ #include <wayland-server.h> #include <wlr/types/wlr_output.h> #include <wlr/render.h> -#include "sway/server.h" -#include "sway/container.h" -#include "sway/workspace.h" -#include "sway/output.h" #include "log.h" +#include "sway/output.h" +#include "sway/server.h" static void output_frame_notify(struct wl_listener *listener, void *data) { struct sway_output *soutput = wl_container_of( @@ -37,19 +35,10 @@ void output_add_notify(struct wl_listener *listener, void *data) { output->wlr_output = wlr_output; output->server = server; - swayc_t *node = new_output(output); - if (!sway_assert(node, "Failed to allocate output")) { - return; - } - - // Switch to workspace if we need to - if (swayc_active_workspace() == NULL) { - swayc_t *ws = node->children->items[0]; - workspace_switch(ws); - } - output->frame.notify = output_frame_notify; wl_signal_add(&wlr_output->events.frame, &output->frame); + + // TODO: Add to tree } void output_remove_notify(struct wl_listener *listener, void *data) { diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c @@ -1,49 +1,10 @@ #include <stdlib.h> #include <wayland-server.h> #include <wlr/types/wlr_xdg_shell_v6.h> -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" -#include "sway/ipc-server.h" #include "sway/server.h" #include "sway/view.h" #include "log.h" -// TODO: move elsewhere -static void temp_ws_cleanup() { - swayc_t *op, *ws; - int i = 0, j; - if (!root_container.children) - return; - while (i < root_container.children->length) { - op = root_container.children->items[i++]; - if (!op->children) - continue; - j = 0; - while (j < op->children->length) { - ws = op->children->items[j++]; - if (ws->children->length == 0 && ws->floating->length == 0 && ws != op->focused) { - if (destroy_workspace(ws)) { - j--; - } - } - } - } -} - -// TODO: move elsewhere -static swayc_t *move_focus_to_tiling(swayc_t *focused) { - if (focused->is_floating) { - if (focused->parent->children->length == 0) { - return focused->parent; - } - // TODO find a better way of doing this - // Or to focused container - return get_focused_container(focused->parent->children->items[0]); - } - return focused; -} - static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) { if (!sway_assert(view->type == SWAY_XDG_SHELL_V6_VIEW, "xdg get_prop for non-xdg view!")) { @@ -88,30 +49,10 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { sway_surface->view = sway_view; // TODO: - // - Consolodate common logic between shells + // - Add to tree // - Wire up listeners // - Handle popups // - Look up pid and open on appropriate workspace // - Set new view to maximized so it behaves nicely // - Criteria - - suspend_workspace_cleanup = true; - //swayc_t *current_ws = swayc_active_workspace(); - swayc_t *prev_focus = get_focused_container(&root_container); - swayc_t *focused = move_focus_to_tiling(prev_focus); - - // TODO: fix new_view - swayc_t *view = new_view(focused, sway_view); - ipc_event_window(view, "new"); - set_focused_container(view); - - swayc_t *output = swayc_parent_by_type(view, C_OUTPUT); - arrange_windows(output, -1, -1); - - swayc_t *workspace = swayc_parent_by_type(focused, C_WORKSPACE); - if (workspace && workspace->fullscreen) { - set_focused_container(workspace->fullscreen); - } - suspend_workspace_cleanup = false; - temp_ws_cleanup(); } diff --git a/sway/ipc-server.c b/sway/ipc-server.c @@ -1,845 +0,0 @@ -// See https://i3wm.org/docs/ipc.html for protocol information - -#ifndef __FreeBSD__ -// Any value will hide SOCK_CLOEXEC on FreeBSD (__BSD_VISIBLE=0) -#define _XOPEN_SOURCE 700 -#endif - -#include <errno.h> -#include <string.h> -#include <sys/socket.h> -#include <sys/un.h> -#include <stdbool.h> -#include <unistd.h> -#include <stdlib.h> -#include <sys/ioctl.h> -#include <fcntl.h> -#include <json-c/json.h> -#include <list.h> -#include <libinput.h> -#ifdef __linux__ -struct ucred { - pid_t pid; - uid_t uid; - gid_t gid; -}; -#endif -#include "sway/ipc-json.h" -#include "sway/ipc-server.h" -#include "sway/security.h" -#include "sway/config.h" -#include "sway/commands.h" -#include "sway/input.h" -#include "sway/server.h" -#include "stringop.h" -#include "log.h" -#include "list.h" -#include "util.h" - -static int ipc_socket = -1; -static struct wl_event_source *ipc_event_source = NULL; -static struct sockaddr_un *ipc_sockaddr = NULL; -static list_t *ipc_client_list = NULL; - -static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; - -struct ipc_client { - struct wl_event_source *event_source; - struct wl_event_source *writable_event_source; - int fd; - uint32_t payload_length; - uint32_t security_policy; - enum ipc_command_type current_command; - enum ipc_command_type subscribed_events; - size_t write_buffer_len; - size_t write_buffer_size; - char *write_buffer; -}; - -static list_t *ipc_get_pixel_requests = NULL; - -struct sockaddr_un *ipc_user_sockaddr(void); -int ipc_handle_connection(int fd, uint32_t mask, void *data); -int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); -int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data); -void ipc_client_disconnect(struct ipc_client *client); -void ipc_client_handle_command(struct ipc_client *client); -bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); -void ipc_get_workspaces_callback(swayc_t *workspace, void *data); -void ipc_get_outputs_callback(swayc_t *container, void *data); -static void ipc_get_marks_callback(swayc_t *container, void *data); - -void ipc_init(void) { - ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); - if (ipc_socket == -1) { - sway_abort("Unable to create IPC socket"); - } - - ipc_sockaddr = ipc_user_sockaddr(); - - // We want to use socket name set by user, not existing socket from another sway instance. - if (getenv("SWAYSOCK") != NULL && access(getenv("SWAYSOCK"), F_OK) == -1) { - strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path)); - ipc_sockaddr->sun_path[sizeof(ipc_sockaddr->sun_path) - 1] = 0; - } - - unlink(ipc_sockaddr->sun_path); - if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) { - sway_abort("Unable to bind IPC socket"); - } - - if (listen(ipc_socket, 3) == -1) { - sway_abort("Unable to listen on IPC socket"); - } - - // Set i3 IPC socket path so that i3-msg works out of the box - setenv("I3SOCK", ipc_sockaddr->sun_path, 1); - setenv("SWAYSOCK", ipc_sockaddr->sun_path, 1); - - ipc_client_list = create_list(); - ipc_get_pixel_requests = create_list(); - - ipc_event_source = wl_event_loop_add_fd(server.wl_event_loop, ipc_socket, - WL_EVENT_READABLE, ipc_handle_connection, NULL); -} - -void ipc_terminate(void) { - if (ipc_event_source) { - wl_event_source_remove(ipc_event_source); - } - close(ipc_socket); - unlink(ipc_sockaddr->sun_path); - - list_free(ipc_client_list); - - if (ipc_sockaddr) { - free(ipc_sockaddr); - } -} - -struct sockaddr_un *ipc_user_sockaddr(void) { - struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); - if (ipc_sockaddr == NULL) { - sway_abort("Can't allocate ipc_sockaddr"); - } - - ipc_sockaddr->sun_family = AF_UNIX; - int path_size = sizeof(ipc_sockaddr->sun_path); - - // Env var typically set by logind, e.g. "/run/user/<user-id>" - const char *dir = getenv("XDG_RUNTIME_DIR"); - if (!dir) { - dir = "/tmp"; - } - if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size, - "%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) { - sway_abort("Socket path won't fit into ipc_sockaddr->sun_path"); - } - - return ipc_sockaddr; -} - -static pid_t get_client_pid(int client_fd) { -// FreeBSD supports getting uid/gid, but not pid -#ifdef __linux__ - struct ucred ucred; - socklen_t len = sizeof(struct ucred); - - if (getsockopt(client_fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { - return -1; - } - - return ucred.pid; -#else - return -1; -#endif -} - -int ipc_handle_connection(int fd, uint32_t mask, void *data) { - (void) fd; (void) data; - sway_log(L_DEBUG, "Event on IPC listening socket"); - assert(mask == WL_EVENT_READABLE); - - int client_fd = accept(ipc_socket, NULL, NULL); - if (client_fd == -1) { - sway_log_errno(L_ERROR, "Unable to accept IPC client connection"); - return 0; - } - - int flags; - if ((flags = fcntl(client_fd, F_GETFD)) == -1 - || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { - sway_log_errno(L_ERROR, "Unable to set CLOEXEC on IPC client socket"); - close(client_fd); - return 0; - } - if ((flags = fcntl(client_fd, F_GETFL)) == -1 - || fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) { - sway_log_errno(L_ERROR, "Unable to set NONBLOCK on IPC client socket"); - close(client_fd); - return 0; - } - - struct ipc_client* client = malloc(sizeof(struct ipc_client)); - if (!client) { - sway_log(L_ERROR, "Unable to allocate ipc client"); - close(client_fd); - return 0; - } - client->payload_length = 0; - client->fd = client_fd; - client->subscribed_events = 0; - client->event_source = wl_event_loop_add_fd(server.wl_event_loop, client_fd, - WL_EVENT_READABLE, ipc_client_handle_readable, client); - client->writable_event_source = NULL; - - client->write_buffer_size = 128; - client->write_buffer_len = 0; - client->write_buffer = malloc(client->write_buffer_size); - if (!client->write_buffer) { - sway_log(L_ERROR, "Unable to allocate ipc client write buffer"); - close(client_fd); - return 0; - } - - pid_t pid = get_client_pid(client->fd); - client->security_policy = get_ipc_policy_mask(pid); - - sway_log(L_DEBUG, "New client: fd %d, pid %d", client_fd, pid); - - list_add(ipc_client_list, client); - - return 0; -} - -static const int ipc_header_size = sizeof(ipc_magic)+8; - -int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { - struct ipc_client *client = data; - - if (mask & WL_EVENT_ERROR) { - sway_log(L_ERROR, "IPC Client socket error, removing client"); - ipc_client_disconnect(client); - return 0; - } - - if (mask & WL_EVENT_HANGUP) { - sway_log(L_DEBUG, "Client %d hung up", client->fd); - ipc_client_disconnect(client); - return 0; - } - - sway_log(L_DEBUG, "Client %d readable", client->fd); - - int read_available; - if (ioctl(client_fd, FIONREAD, &read_available) == -1) { - sway_log_errno(L_INFO, "Unable to read IPC socket buffer size"); - ipc_client_disconnect(client); - return 0; - } - - // Wait for the rest of the command payload in case the header has already been read - if (client->payload_length > 0) { - if ((uint32_t)read_available >= client->payload_length) { - ipc_client_handle_command(client); - } - return 0; - } - - if (read_available < ipc_header_size) { - return 0; - } - - uint8_t buf[ipc_header_size]; - uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic)); - // Should be fully available, because read_available >= ipc_header_size - ssize_t received = recv(client_fd, buf, ipc_header_size, 0); - if (received == -1) { - sway_log_errno(L_INFO, "Unable to receive header from IPC client"); - ipc_client_disconnect(client); - return 0; - } - - if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { - sway_log(L_DEBUG, "IPC header check failed"); - ipc_client_disconnect(client); - return 0; - } - - client->payload_length = buf32[0]; - client->current_command = (enum ipc_command_type)buf32[1]; - - if (read_available - received >= (long)client->payload_length) { - ipc_client_handle_command(client); - } - - return 0; -} - -int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { - struct ipc_client *client = data; - - if (mask & WL_EVENT_ERROR) { - sway_log(L_ERROR, "IPC Client socket error, removing client"); - ipc_client_disconnect(client); - return 0; - } - - if (mask & WL_EVENT_HANGUP) { - sway_log(L_DEBUG, "Client %d hung up", client->fd); - ipc_client_disconnect(client); - return 0; - } - - if (client->write_buffer_len <= 0) { - return 0; - } - - sway_log(L_DEBUG, "Client %d writable", client->fd); - - ssize_t written = write(client->fd, client->write_buffer, client->write_buffer_len); - - if (written == -1 && errno == EAGAIN) { - return 0; - } else if (written == -1) { - sway_log_errno(L_INFO, "Unable to send data from queue to IPC client"); - ipc_client_disconnect(client); - return 0; - } - - memmove(client->write_buffer, client->write_buffer + written, client->write_buffer_len - written); - client->write_buffer_len -= written; - - if (client->write_buffer_len == 0 && client->writable_event_source) { - wl_event_source_remove(client->writable_event_source); - client->writable_event_source = NULL; - } - - return 0; -} - -void ipc_client_disconnect(struct ipc_client *client) { - if (!sway_assert(client != NULL, "client != NULL")) { - return; - } - - if (client->fd != -1) { - shutdown(client->fd, SHUT_RDWR); - } - - sway_log(L_INFO, "IPC Client %d disconnected", client->fd); - wl_event_source_remove(client->event_source); - if (client->writable_event_source) { - wl_event_source_remove(client->writable_event_source); - } - int i = 0; - while (i < ipc_client_list->length && ipc_client_list->items[i] != client) i++; - list_del(ipc_client_list, i); - free(client->write_buffer); - close(client->fd); - free(client); -} - -bool output_by_name_test(swayc_t *view, void *data) { - char *name = (char *)data; - if (view->type != C_OUTPUT) { - return false; - } - return !strcmp(name, view->name); -} - -// greedy wildcard (only "*") matching -bool mime_type_matches(const char *mime_type, const char *pattern) { - const char *wildcard = NULL; - while (*mime_type && *pattern) { - if (*pattern == '*' && !wildcard) { - wildcard = pattern; - ++pattern; - } - - if (*mime_type != *pattern) { - if (!wildcard) - return false; - - pattern = wildcard; - ++mime_type; - continue; - } - - ++mime_type; - ++pattern; - } - - while (*pattern == '*') { - ++pattern; - } - - return (*mime_type == *pattern); -} - -void ipc_client_handle_command(struct ipc_client *client) { - if (!sway_assert(client != NULL, "client != NULL")) { - return; - } - - char *buf = malloc(client->payload_length + 1); - if (!buf) { - sway_log_errno(L_INFO, "Unable to allocate IPC payload"); - ipc_client_disconnect(client); - return; - } - if (client->payload_length > 0) { - // Payload should be fully available - ssize_t received = recv(client->fd, buf, client->payload_length, 0); - if (received == -1) - { - sway_log_errno(L_INFO, "Unable to receive payload from IPC client"); - ipc_client_disconnect(client); - free(buf); - return; - } - } - buf[client->payload_length] = '\0'; - - const char *error_denied = "{ \"success\": false, \"error\": \"Permission denied\" }"; - - switch (client->current_command) { - case IPC_COMMAND: - { - if (!(client->security_policy & IPC_FEATURE_COMMAND)) { - goto exit_denied; - } - struct cmd_results *results = handle_command(buf, CONTEXT_IPC); - const char *json = cmd_results_to_json(results); - char reply[256]; - int length = snprintf(reply, sizeof(reply), "%s", json); - ipc_send_reply(client, reply, (uint32_t) length); - free_cmd_results(results); - goto exit_cleanup; - } - - case IPC_SUBSCRIBE: - { - // TODO: Check if they're permitted to use these events - struct json_object *request = json_tokener_parse(buf); - if (request == NULL) { - ipc_send_reply(client, "{\"success\": false}", 18); - sway_log_errno(L_INFO, "Failed to read request"); - goto exit_cleanup; - } - - // parse requested event types - for (int i = 0; i < json_object_array_length(request); i++) { - const char *event_type = json_object_get_string(json_object_array_get_idx(request, i)); - if (strcmp(event_type, "workspace") == 0) { - client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE); - } else if (strcmp(event_type, "barconfig_update") == 0) { - client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE); - } else if (strcmp(event_type, "mode") == 0) { - client->subscribed_events |= event_mask(IPC_EVENT_MODE); - } else if (strcmp(event_type, "window") == 0) { - client->subscribed_events |= event_mask(IPC_EVENT_WINDOW); - } else if (strcmp(event_type, "modifier") == 0) { - client->subscribed_events |= event_mask(IPC_EVENT_MODIFIER); - } else if (strcmp(event_type, "binding") == 0) { - client->subscribed_events |= event_mask(IPC_EVENT_BINDING); - } else { - ipc_send_reply(client, "{\"success\": false}", 18); - json_object_put(request); - sway_log_errno(L_INFO, "Failed to parse request"); - goto exit_cleanup; - } - } - - json_object_put(request); - - ipc_send_reply(client, "{\"success\": true}", 17); - goto exit_cleanup; - } - - case IPC_GET_WORKSPACES: - { - if (!(client->security_policy & IPC_FEATURE_GET_WORKSPACES)) { - goto exit_denied; - } - json_object *workspaces = json_object_new_array(); - container_map(&root_container, ipc_get_workspaces_callback, workspaces); - const char *json_string = json_object_to_json_string(workspaces); - ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); - json_object_put(workspaces); // free - goto exit_cleanup; - } - - case IPC_GET_INPUTS: - { - if (!(client->security_policy & IPC_FEATURE_GET_INPUTS)) { - goto exit_denied; - } - json_object *inputs = json_object_new_array(); - /* TODO WLR - if (input_devices) { - for(int i = 0; i<input_devices->length; i++) { - struct libinput_device *device = input_devices->items[i]; - json_object_array_add(inputs, ipc_json_describe_input(device)); - } - } - */ - const char *json_string = json_object_to_json_string(inputs); - ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); - json_object_put(inputs); - goto exit_cleanup; - } - - case IPC_GET_OUTPUTS: - { - if (!(client->security_policy & IPC_FEATURE_GET_OUTPUTS)) { - goto exit_denied; - } - json_object *outputs = json_object_new_array(); - container_map(&root_container, ipc_get_outputs_callback, outputs); - const char *json_string = json_object_to_json_string(outputs); - ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); - json_object_put(outputs); // free - goto exit_cleanup; - } - - case IPC_GET_TREE: - { - if (!(client->security_policy & IPC_FEATURE_GET_TREE)) { - goto exit_denied; - } - json_object *tree = ipc_json_describe_container_recursive(&root_container); - const char *json_string = json_object_to_json_string(tree); - ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); - json_object_put(tree); - goto exit_cleanup; - } - - case IPC_GET_MARKS: - { - if (!(client->security_policy & IPC_FEATURE_GET_MARKS)) { - goto exit_denied; - } - json_object *marks = json_object_new_array(); - container_map(&root_container, ipc_get_marks_callback, marks); - const char *json_string = json_object_to_json_string(marks); - ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); - json_object_put(marks); - goto exit_cleanup; - } - - case IPC_GET_VERSION: - { - json_object *version = ipc_json_get_version(); - const char *json_string = json_object_to_json_string(version); - ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); - json_object_put(version); // free - goto exit_cleanup; - } - - case IPC_GET_BAR_CONFIG: - { - if (!(client->security_policy & IPC_FEATURE_GET_BAR_CONFIG)) { - goto exit_denied; - } - if (!buf[0]) { - // Send list of configured bar IDs - json_object *bars = json_object_new_array(); - int i; - for (i = 0; i < config->bars->length; ++i) { - struct bar_config *bar = config->bars->items[i]; - json_object_array_add(bars, json_object_new_string(bar->id)); - } - const char *json_string = json_object_to_json_string(bars); - ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); - json_object_put(bars); // free - } else { - // Send particular bar's details - struct bar_config *bar = NULL; - int i; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; - if (strcmp(buf, bar->id) == 0) { - break; - } - bar = NULL; - } - if (!bar) { - const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }"; - ipc_send_reply(client, error, (uint32_t)strlen(error)); - goto exit_cleanup; - } - json_object *json = ipc_json_describe_bar_config(bar); - const char *json_string = json_object_to_json_string(json); - ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); - json_object_put(json); // free - } - goto exit_cleanup; - } - - case IPC_GET_CLIPBOARD: - // TODO WLR - break; - - default: - sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); - goto exit_cleanup; - } - -exit_denied: - ipc_send_reply(client, error_denied, (uint32_t)strlen(error_denied)); - sway_log(L_DEBUG, "Denied IPC client access to %i", client->current_command); - -exit_cleanup: - client->payload_length = 0; - free(buf); - return; -} - -bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { - assert(payload); - - char data[ipc_header_size]; - uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); - - memcpy(data, ipc_magic, sizeof(ipc_magic)); - data32[0] = payload_length; - data32[1] = client->current_command; - - while (client->write_buffer_len + ipc_header_size + payload_length >= - client->write_buffer_size) { - client->write_buffer_size *= 2; - } - - // TODO: reduce the limit back to 4 MB when screenshooter is implemented - if (client->write_buffer_size > (1 << 28)) { // 256 MB - sway_log(L_ERROR, "Client write buffer too big, disconnecting client"); - ipc_client_disconnect(client); - return false; - } - - char *new_buffer = realloc(client->write_buffer, client->write_buffer_size); - if (!new_buffer) { - sway_log(L_ERROR, "Unable to reallocate ipc client write buffer"); - ipc_client_disconnect(client); - return false; - } - client->write_buffer = new_buffer; - - memcpy(client->write_buffer + client->write_buffer_len, data, ipc_header_size); - client->write_buffer_len += ipc_header_size; - memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length); - client->write_buffer_len += payload_length; - - if (!client->writable_event_source) { - client->writable_event_source = wl_event_loop_add_fd( - server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, - ipc_client_handle_writable, client); - } - - sway_log(L_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload); - - return true; -} - -void ipc_get_workspaces_callback(swayc_t *workspace, void *data) { - if (workspace->type == C_WORKSPACE) { - json_object *workspace_json = ipc_json_describe_container(workspace); - // override the default focused indicator because - // it's set differently for the get_workspaces reply - bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace; - json_object_object_del(workspace_json, "focused"); - json_object_object_add(workspace_json, "focused", json_object_new_boolean(focused)); - json_object_array_add((json_object *)data, workspace_json); - } -} - -void ipc_get_outputs_callback(swayc_t *container, void *data) { - if (container->type == C_OUTPUT) { - json_object_array_add((json_object *)data, ipc_json_describe_container(container)); - } -} - -static void ipc_get_marks_callback(swayc_t *container, void *data) { - json_object *object = (json_object *)data; - if (container->marks) { - for (int i = 0; i < container->marks->length; ++i) { - char *mark = (char *)container->marks->items[i]; - json_object_array_add(object, json_object_new_string(mark)); - } - } -} - -void ipc_send_event(const char *json_string, enum ipc_command_type event) { - static struct { - enum ipc_command_type event; - enum ipc_feature feature; - } security_mappings[] = { - { IPC_EVENT_WORKSPACE, IPC_FEATURE_EVENT_WORKSPACE }, - { IPC_EVENT_OUTPUT, IPC_FEATURE_EVENT_OUTPUT }, - { IPC_EVENT_MODE, IPC_FEATURE_EVENT_MODE }, - { IPC_EVENT_WINDOW, IPC_FEATURE_EVENT_WINDOW }, - { IPC_EVENT_BINDING, IPC_FEATURE_EVENT_BINDING }, - { IPC_EVENT_INPUT, IPC_FEATURE_EVENT_INPUT } - }; - - uint32_t security_mask = 0; - for (size_t i = 0; i < sizeof(security_mappings) / sizeof(security_mappings[0]); ++i) { - if (security_mappings[i].event == event) { - security_mask = security_mappings[i].feature; - break; - } - } - - int i; - struct ipc_client *client; - for (i = 0; i < ipc_client_list->length; i++) { - client = ipc_client_list->items[i]; - if (!(client->security_policy & security_mask)) { - continue; - } - if ((client->subscribed_events & event_mask(event)) == 0) { - continue; - } - client->current_command = event; - if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) { - sway_log_errno(L_INFO, "Unable to send reply to IPC client"); - ipc_client_disconnect(client); - } - } -} - -void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change) { - sway_log(L_DEBUG, "Sending workspace::%s event", change); - json_object *obj = json_object_new_object(); - json_object_object_add(obj, "change", json_object_new_string(change)); - if (strcmp("focus", change) == 0) { - if (old) { - json_object_object_add(obj, "old", ipc_json_describe_container_recursive(old)); - } else { - json_object_object_add(obj, "old", NULL); - } - } - - if (new) { - json_object_object_add(obj, "current", ipc_json_describe_container_recursive(new)); - } else { - json_object_object_add(obj, "current", NULL); - } - - const char *json_string = json_object_to_json_string(obj); - ipc_send_event(json_string, IPC_EVENT_WORKSPACE); - - json_object_put(obj); // free -} - -void ipc_event_window(swayc_t *window, const char *change) { - sway_log(L_DEBUG, "Sending window::%s event", change); - json_object *obj = json_object_new_object(); - json_object_object_add(obj, "change", json_object_new_string(change)); - json_object_object_add(obj, "container", ipc_json_describe_container_recursive(window)); - - const char *json_string = json_object_to_json_string(obj); - ipc_send_event(json_string, IPC_EVENT_WINDOW); - - json_object_put(obj); // free -} - -void ipc_event_barconfig_update(struct bar_config *bar) { - sway_log(L_DEBUG, "Sending barconfig_update event"); - json_object *json = ipc_json_describe_bar_config(bar); - const char *json_string = json_object_to_json_string(json); - ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE); - - json_object_put(json); // free -} - -void ipc_event_mode(const char *mode) { - sway_log(L_DEBUG, "Sending mode::%s event", mode); - json_object *obj = json_object_new_object(); - json_object_object_add(obj, "change", json_object_new_string(mode)); - - const char *json_string = json_object_to_json_string(obj); - ipc_send_event(json_string, IPC_EVENT_MODE); - - json_object_put(obj); // free -} - -void ipc_event_modifier(uint32_t modifier, const char *state) { - sway_log(L_DEBUG, "Sending modifier::%s event", state); - json_object *obj = json_object_new_object(); - json_object_object_add(obj, "change", json_object_new_string(state)); - - const char *modifier_name = get_modifier_name_by_mask(modifier); - json_object_object_add(obj, "modifier", json_object_new_string(modifier_name)); - - const char *json_string = json_object_to_json_string(obj); - ipc_send_event(json_string, IPC_EVENT_MODIFIER); - - json_object_put(obj); // free -} - -static void ipc_event_binding(json_object *sb_obj) { - sway_log(L_DEBUG, "Sending binding::run event"); - json_object *obj = json_object_new_object(); - json_object_object_add(obj, "change", json_object_new_string("run")); - json_object_object_add(obj, "binding", sb_obj); - - const char *json_string = json_object_to_json_string(obj); - ipc_send_event(json_string, IPC_EVENT_BINDING); - - json_object_put(obj); // free -} - -void ipc_event_binding_keyboard(struct sway_binding *sb) { - json_object *sb_obj = json_object_new_object(); - json_object_object_add(sb_obj, "command", json_object_new_string(sb->command)); - - const char *names[10]; - - int len = get_modifier_names(names, sb->modifiers); - int i; - json_object *modifiers = json_object_new_array(); - for (i = 0; i < len; ++i) { - json_object_array_add(modifiers, json_object_new_string(names[i])); - } - - json_object_object_add(sb_obj, "event_state_mask", modifiers); - - json_object *input_codes = json_object_new_array(); - int input_code = 0; - json_object *symbols = json_object_new_array(); - json_object *symbol = NULL; - - if (sb->bindcode) { // bindcode: populate input_codes - uint32_t keycode; - for (i = 0; i < sb->keys->length; ++i) { - keycode = *(uint32_t *)sb->keys->items[i]; - json_object_array_add(input_codes, json_object_new_int(keycode)); - if (i == 0) { - input_code = keycode; - } - } - } else { // bindsym: populate symbols - uint32_t keysym; - char buffer[64]; - for (i = 0; i < sb->keys->length; ++i) { - keysym = *(uint32_t *)sb->keys->items[i]; - if (xkb_keysym_get_name(keysym, buffer, 64) > 0) { - json_object *str = json_object_new_string(buffer); - json_object_array_add(symbols, str); - if (i == 0) { - symbol = str; - } - } - } - } - - json_object_object_add(sb_obj, "input_codes", input_codes); - json_object_object_add(sb_obj, "input_code", json_object_new_int(input_code)); - json_object_object_add(sb_obj, "symbols", symbols); - json_object_object_add(sb_obj, "symbol", symbol); - json_object_object_add(sb_obj, "input_type", json_object_new_string("keyboard")); - - ipc_event_binding(sb_obj); -} diff --git a/sway/main.c b/sway/main.c @@ -1,30 +1,24 @@ #define _XOPEN_SOURCE 700 #define _POSIX_C_SOURCE 200112L -#include <stdio.h> -#include <stdlib.h> +#include <getopt.h> +#include <signal.h> #include <stdbool.h> -#include <sys/wait.h> -#include <sys/types.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> #include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> #include <sys/un.h> -#include <signal.h> #include <unistd.h> -#include <getopt.h> #ifdef __linux__ #include <sys/capability.h> #include <sys/prctl.h> #endif -#include "sway/layout.h" -#include "sway/config.h" -#include "sway/security.h" -#include "sway/handlers.h" -#include "sway/input.h" -#include "sway/ipc-server.h" #include "sway/server.h" #include "ipc-client.h" #include "readline.h" #include "stringop.h" -#include "sway.h" #include "log.h" #include "util.h" @@ -39,7 +33,7 @@ void sway_terminate(int exit_code) { } void sig_handler(int signal) { - close_views(&root_container); + //close_views(&root_container); sway_terminate(EXIT_SUCCESS); } @@ -442,17 +436,17 @@ int main(int argc, char **argv) { return 1; } - init_layout(); - ipc_init(); + //init_layout(); + //ipc_init(); - if (validate) { - bool valid = load_main_config(config_path, false); - return valid ? 0 : 1; - } + //if (validate) { + // bool valid = load_main_config(config_path, false); + // return valid ? 0 : 1; + //} - if (!load_main_config(config_path, false)) { - sway_terminate(EXIT_FAILURE); - } + //if (!load_main_config(config_path, false)) { + // sway_terminate(EXIT_FAILURE); + //} if (config_path) { free(config_path); @@ -466,11 +460,11 @@ int main(int argc, char **argv) { server_fini(&server); - ipc_terminate(); + //ipc_terminate(); - if (config) { - free_config(config); - } + //if (config) { + // free_config(config); + //} return exit_value; } diff --git a/sway/border.c b/sway/old/border.c diff --git a/sway/commands.c b/sway/old/commands.c diff --git a/sway/commands/assign.c b/sway/old/commands/assign.c diff --git a/sway/commands/bar.c b/sway/old/commands/bar.c diff --git a/sway/commands/bar/activate_button.c b/sway/old/commands/bar/activate_button.c diff --git a/sway/commands/bar/binding_mode_indicator.c b/sway/old/commands/bar/binding_mode_indicator.c diff --git a/sway/commands/bar/bindsym.c b/sway/old/commands/bar/bindsym.c diff --git a/sway/commands/bar/colors.c b/sway/old/commands/bar/colors.c diff --git a/sway/commands/bar/context_button.c b/sway/old/commands/bar/context_button.c diff --git a/sway/commands/bar/font.c b/sway/old/commands/bar/font.c diff --git a/sway/commands/bar/height.c b/sway/old/commands/bar/height.c diff --git a/sway/commands/bar/hidden_state.c b/sway/old/commands/bar/hidden_state.c diff --git a/sway/commands/bar/icon_theme.c b/sway/old/commands/bar/icon_theme.c diff --git a/sway/commands/bar/id.c b/sway/old/commands/bar/id.c diff --git a/sway/commands/bar/mode.c b/sway/old/commands/bar/mode.c diff --git a/sway/commands/bar/modifier.c b/sway/old/commands/bar/modifier.c diff --git a/sway/commands/bar/output.c b/sway/old/commands/bar/output.c diff --git a/sway/commands/bar/pango_markup.c b/sway/old/commands/bar/pango_markup.c diff --git a/sway/commands/bar/position.c b/sway/old/commands/bar/position.c diff --git a/sway/commands/bar/secondary_button.c b/sway/old/commands/bar/secondary_button.c diff --git a/sway/commands/bar/separator_symbol.c b/sway/old/commands/bar/separator_symbol.c diff --git a/sway/commands/bar/status_command.c b/sway/old/commands/bar/status_command.c diff --git a/sway/commands/bar/strip_workspace_numbers.c b/sway/old/commands/bar/strip_workspace_numbers.c diff --git a/sway/commands/bar/swaybar_command.c b/sway/old/commands/bar/swaybar_command.c diff --git a/sway/commands/bar/tray_output.c b/sway/old/commands/bar/tray_output.c diff --git a/sway/commands/bar/tray_padding.c b/sway/old/commands/bar/tray_padding.c diff --git a/sway/commands/bar/workspace_buttons.c b/sway/old/commands/bar/workspace_buttons.c diff --git a/sway/commands/bar/wrap_scroll.c b/sway/old/commands/bar/wrap_scroll.c diff --git a/sway/commands/bind.c b/sway/old/commands/bind.c diff --git a/sway/commands/border.c b/sway/old/commands/border.c diff --git a/sway/commands/client.c b/sway/old/commands/client.c diff --git a/sway/commands/clipboard.c b/sway/old/commands/clipboard.c diff --git a/sway/commands/commands.c b/sway/old/commands/commands.c diff --git a/sway/commands/debuglog.c b/sway/old/commands/debuglog.c diff --git a/sway/commands/default_border.c b/sway/old/commands/default_border.c diff --git a/sway/commands/default_floating_border.c b/sway/old/commands/default_floating_border.c diff --git a/sway/commands/exec.c b/sway/old/commands/exec.c diff --git a/sway/commands/exec_always.c b/sway/old/commands/exec_always.c diff --git a/sway/commands/exit.c b/sway/old/commands/exit.c diff --git a/sway/commands/floating.c b/sway/old/commands/floating.c diff --git a/sway/commands/floating_maximum_size.c b/sway/old/commands/floating_maximum_size.c diff --git a/sway/commands/floating_minimum_size.c b/sway/old/commands/floating_minimum_size.c diff --git a/sway/commands/floating_mod.c b/sway/old/commands/floating_mod.c diff --git a/sway/commands/floating_scroll.c b/sway/old/commands/floating_scroll.c diff --git a/sway/commands/focus.c b/sway/old/commands/focus.c diff --git a/sway/commands/focus_follows_mouse.c b/sway/old/commands/focus_follows_mouse.c diff --git a/sway/commands/font.c b/sway/old/commands/font.c diff --git a/sway/commands/for_window.c b/sway/old/commands/for_window.c diff --git a/sway/commands/force_focus_wrapping.c b/sway/old/commands/force_focus_wrapping.c diff --git a/sway/commands/fullscreen.c b/sway/old/commands/fullscreen.c diff --git a/sway/commands/gaps.c b/sway/old/commands/gaps.c diff --git a/sway/commands/hide_edge_borders.c b/sway/old/commands/hide_edge_borders.c diff --git a/sway/commands/include.c b/sway/old/commands/include.c diff --git a/sway/commands/input.c b/sway/old/commands/input.c diff --git a/sway/commands/input/accel_profile.c b/sway/old/commands/input/accel_profile.c diff --git a/sway/commands/input/click_method.c b/sway/old/commands/input/click_method.c diff --git a/sway/commands/input/drag_lock.c b/sway/old/commands/input/drag_lock.c diff --git a/sway/commands/input/dwt.c b/sway/old/commands/input/dwt.c diff --git a/sway/commands/input/events.c b/sway/old/commands/input/events.c diff --git a/sway/commands/input/left_handed.c b/sway/old/commands/input/left_handed.c diff --git a/sway/commands/input/middle_emulation.c b/sway/old/commands/input/middle_emulation.c diff --git a/sway/commands/input/natural_scroll.c b/sway/old/commands/input/natural_scroll.c diff --git a/sway/commands/input/pointer_accel.c b/sway/old/commands/input/pointer_accel.c diff --git a/sway/commands/input/scroll_method.c b/sway/old/commands/input/scroll_method.c diff --git a/sway/commands/input/tap.c b/sway/old/commands/input/tap.c diff --git a/sway/commands/ipc.c b/sway/old/commands/ipc.c diff --git a/sway/commands/kill.c b/sway/old/commands/kill.c diff --git a/sway/commands/layout.c b/sway/old/commands/layout.c diff --git a/sway/commands/log_colors.c b/sway/old/commands/log_colors.c diff --git a/sway/commands/mark.c b/sway/old/commands/mark.c diff --git a/sway/commands/mode.c b/sway/old/commands/mode.c diff --git a/sway/commands/mouse_warping.c b/sway/old/commands/mouse_warping.c diff --git a/sway/commands/move.c b/sway/old/commands/move.c diff --git a/sway/commands/new_float.c b/sway/old/commands/new_float.c diff --git a/sway/commands/new_window.c b/sway/old/commands/new_window.c diff --git a/sway/commands/no_focus.c b/sway/old/commands/no_focus.c diff --git a/sway/commands/orientation.c b/sway/old/commands/orientation.c diff --git a/sway/commands/output.c b/sway/old/commands/output.c diff --git a/sway/commands/permit.c b/sway/old/commands/permit.c diff --git a/sway/commands/reload.c b/sway/old/commands/reload.c diff --git a/sway/commands/resize.c b/sway/old/commands/resize.c diff --git a/sway/commands/scratchpad.c b/sway/old/commands/scratchpad.c diff --git a/sway/commands/seamless_mouse.c b/sway/old/commands/seamless_mouse.c diff --git a/sway/commands/set.c b/sway/old/commands/set.c diff --git a/sway/commands/show_marks.c b/sway/old/commands/show_marks.c diff --git a/sway/commands/smart_gaps.c b/sway/old/commands/smart_gaps.c diff --git a/sway/commands/split.c b/sway/old/commands/split.c diff --git a/sway/commands/sticky.c b/sway/old/commands/sticky.c diff --git a/sway/commands/unmark.c b/sway/old/commands/unmark.c diff --git a/sway/commands/workspace.c b/sway/old/commands/workspace.c diff --git a/sway/commands/workspace_auto_back_and_forth.c b/sway/old/commands/workspace_auto_back_and_forth.c diff --git a/sway/commands/workspace_layout.c b/sway/old/commands/workspace_layout.c diff --git a/sway/old/config.c b/sway/old/config.c @@ -0,0 +1,1313 @@ +#define _POSIX_C_SOURCE 200809L +#define _XOPEN_SOURCE 700 +#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 <float.h> +#include <dirent.h> +#include <strings.h> +#include <wlr/types/wlr_output.h> +#include "wayland-desktop-shell-server-protocol.h" +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/layout.h" +#include "sway/input_state.h" +#include "sway/criteria.h" +#include "sway/input.h" +#include "sway/border.h" +#include "readline.h" +#include "stringop.h" +#include "list.h" +#include "log.h" + +struct sway_config *config = NULL; + +static void terminate_swaybar(pid_t pid); + +static void free_variable(struct sway_variable *var) { + if (!var) { + return; + } + free(var->name); + free(var->value); + free(var); +} + +static void free_binding(struct sway_binding *bind) { + if (!bind) { + return; + } + free_flat_list(bind->keys); + free(bind->command); + free(bind); +} + +static void free_mode(struct sway_mode *mode) { + if (!mode) { + return; + } + free(mode->name); + int i; + for (i = 0; mode->bindings && i < mode->bindings->length; ++i) { + free_binding(mode->bindings->items[i]); + } + list_free(mode->bindings); + free(mode); +} + +static void free_bar(struct bar_config *bar) { + if (!bar) { + return; + } + free(bar->mode); + free(bar->hidden_state); +#ifdef ENABLE_TRAY + free(bar->tray_output); + free(bar->icon_theme); +#endif + free(bar->status_command); + free(bar->font); + free(bar->separator_symbol); + int i; + for (i = 0; bar->bindings && i < bar->bindings->length; ++i) { + free_sway_mouse_binding(bar->bindings->items[i]); + } + list_free(bar->bindings); + + if (bar->outputs) { + free_flat_list(bar->outputs); + } + + if (bar->pid != 0) { + terminate_swaybar(bar->pid); + } + + free(bar->colors.background); + free(bar->colors.statusline); + free(bar->colors.separator); + free(bar->colors.focused_background); + free(bar->colors.focused_statusline); + free(bar->colors.focused_separator); + free(bar->colors.focused_workspace_border); + free(bar->colors.focused_workspace_bg); + free(bar->colors.focused_workspace_text); + free(bar->colors.active_workspace_border); + free(bar->colors.active_workspace_bg); + free(bar->colors.active_workspace_text); + free(bar->colors.inactive_workspace_border); + free(bar->colors.inactive_workspace_bg); + free(bar->colors.inactive_workspace_text); + free(bar->colors.urgent_workspace_border); + free(bar->colors.urgent_workspace_bg); + free(bar->colors.urgent_workspace_text); + free(bar->colors.binding_mode_border); + free(bar->colors.binding_mode_bg); + free(bar->colors.binding_mode_text); + + free(bar); +} + +void free_input_config(struct input_config *ic) { + if (!ic) { + return; + } + free(ic->identifier); + free(ic); +} + +void free_output_config(struct output_config *oc) { + if (!oc) { + return; + } + free(oc->name); + free(oc->background); + free(oc->background_option); + free(oc); +} + +static void free_workspace_output(struct workspace_output *wo) { + if (!wo) { + return; + } + free(wo->output); + free(wo->workspace); + free(wo); +} + +static void pid_workspace_cleanup() { + struct timespec ts; + struct pid_workspace *pw = NULL; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + // work backwards through list and remove any entries + // older than PID_WORKSPACE_TIMEOUT + for (int i = config->pid_workspaces->length - 1; i > -1; i--) { + pw = config->pid_workspaces->items[i]; + + if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) { + free_pid_workspace(config->pid_workspaces->items[i]); + list_del(config->pid_workspaces, i); + } + } +} + +// de-dupe pid_workspaces to ensure pid uniqueness +void pid_workspace_add(struct pid_workspace *pw) { + struct pid_workspace *list_pw = NULL; + struct timespec ts; + time_t *now = malloc(sizeof(time_t)); + if (!now) { + sway_log(L_ERROR, "Allocating time for pid_workspace failed"); + return; + } + + pid_workspace_cleanup(); + + // add current time to pw + clock_gettime(CLOCK_MONOTONIC, &ts); + *now = ts.tv_sec; + + pw->time_added = now; + + // work backwards through list and delete any entries that + // have the same pid as that in our new pid_workspace + for (int i = config->pid_workspaces->length - 1; i > -1; i--) { + list_pw = config->pid_workspaces->items[i]; + + if (pw->pid == list_pw->pid) { + free_pid_workspace(config->pid_workspaces->items[i]); + list_del(config->pid_workspaces, i); + } + } + + list_add(config->pid_workspaces, pw); +} + +void free_pid_workspace(struct pid_workspace *pw) { + if (!pw) { + return; + } + free(pw->pid); + free(pw->workspace); + free(pw->time_added); + free(pw); +} + +void free_command_policy(struct command_policy *policy) { + if (!policy) { + return; + } + free(policy->command); + free(policy); +} + +void free_feature_policy(struct feature_policy *policy) { + if (!policy) { + return; + } + free(policy->program); + free(policy); +} + +void free_config(struct sway_config *config) { + if (!config) { + return; + } + int i; + for (i = 0; config->symbols && i < config->symbols->length; ++i) { + free_variable(config->symbols->items[i]); + } + list_free(config->symbols); + + for (i = 0; config->modes && i < config->modes->length; ++i) { + free_mode(config->modes->items[i]); + } + list_free(config->modes); + + for (i = 0; config->bars && i < config->bars->length; ++i) { + free_bar(config->bars->items[i]); + } + list_free(config->bars); + + free_flat_list(config->cmd_queue); + + for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) { + free_workspace_output(config->workspace_outputs->items[i]); + } + list_free(config->workspace_outputs); + + for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) { + free_pid_workspace(config->pid_workspaces->items[i]); + } + list_free(config->pid_workspaces); + + for (i = 0; config->criteria && i < config->criteria->length; ++i) { + free_criteria(config->criteria->items[i]); + } + list_free(config->criteria); + + for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { + free_criteria(config->no_focus->items[i]); + } + list_free(config->no_focus); + + for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { + free_input_config(config->input_configs->items[i]); + } + list_free(config->input_configs); + + for (i = 0; config->output_configs && i < config->output_configs->length; ++i) { + free_output_config(config->output_configs->items[i]); + } + list_free(config->output_configs); + + for (i = 0; config->command_policies && i < config->command_policies->length; ++i) { + free_command_policy(config->command_policies->items[i]); + } + list_free(config->command_policies); + + for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) { + free_feature_policy(config->feature_policies->items[i]); + } + list_free(config->feature_policies); + + list_free(config->active_bar_modifiers); + free_flat_list(config->config_chain); + free(config->font); + 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); +} + + +static bool file_exists(const char *path) { + return path && access(path, R_OK) != -1; +} + +static void config_defaults(struct sway_config *config) { + if (!(config->symbols = create_list())) goto cleanup; + if (!(config->modes = create_list())) goto cleanup; + if (!(config->bars = create_list())) goto cleanup; + if (!(config->workspace_outputs = create_list())) goto cleanup; + if (!(config->pid_workspaces = create_list())) goto cleanup; + if (!(config->criteria = create_list())) goto cleanup; + if (!(config->no_focus = create_list())) goto cleanup; + if (!(config->input_configs = create_list())) goto cleanup; + if (!(config->output_configs = create_list())) goto cleanup; + + if (!(config->cmd_queue = create_list())) goto cleanup; + + if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) goto cleanup; + if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup; + strcpy(config->current_mode->name, "default"); + if (!(config->current_mode->bindings = create_list())) goto cleanup; + list_add(config->modes, config->current_mode); + + config->floating_mod = 0; + config->dragging_key = M_LEFT_CLICK; + config->resizing_key = M_RIGHT_CLICK; + 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; + config->font_height = get_font_text_height(config->font); + + // floating view + config->floating_maximum_width = 0; + config->floating_maximum_height = 0; + config->floating_minimum_width = 75; + config->floating_minimum_height = 50; + + // Flags + config->focus_follows_mouse = true; + config->mouse_warping = true; + config->reloading = false; + config->active = false; + config->failed = false; + config->auto_back_and_forth = false; + config->seamless_mouse = true; + config->reading = false; + config->show_marks = true; + + config->edge_gaps = true; + config->smart_gaps = false; + config->gaps_inner = 0; + config->gaps_outer = 0; + + if (!(config->active_bar_modifiers = create_list())) goto cleanup; + + if (!(config->config_chain = create_list())) goto cleanup; + 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; + + // border colors + config->border_colors.focused.border = 0x4C7899FF; + config->border_colors.focused.background = 0x285577FF; + config->border_colors.focused.text = 0xFFFFFFFF; + config->border_colors.focused.indicator = 0x2E9EF4FF; + config->border_colors.focused.child_border = 0x285577FF; + + config->border_colors.focused_inactive.border = 0x333333FF; + config->border_colors.focused_inactive.background = 0x5F676AFF; + config->border_colors.focused_inactive.text = 0xFFFFFFFF; + config->border_colors.focused_inactive.indicator = 0x484E50FF; + config->border_colors.focused_inactive.child_border = 0x5F676AFF; + + config->border_colors.unfocused.border = 0x333333FF; + config->border_colors.unfocused.background = 0x222222FF; + config->border_colors.unfocused.text = 0x888888FF; + config->border_colors.unfocused.indicator = 0x292D2EFF; + config->border_colors.unfocused.child_border = 0x222222FF; + + config->border_colors.urgent.border = 0x2F343AFF; + config->border_colors.urgent.background = 0x900000FF; + config->border_colors.urgent.text = 0xFFFFFFFF; + config->border_colors.urgent.indicator = 0x900000FF; + config->border_colors.urgent.child_border = 0x900000FF; + + config->border_colors.placeholder.border = 0x000000FF; + config->border_colors.placeholder.background = 0x0C0C0CFF; + config->border_colors.placeholder.text = 0xFFFFFFFF; + config->border_colors.placeholder.indicator = 0x000000FF; + config->border_colors.placeholder.child_border = 0x0C0C0CFF; + + config->border_colors.background = 0xFFFFFFFF; + + // 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 int compare_modifiers(const void *left, const void *right) { + uint32_t a = *(uint32_t *)left; + uint32_t b = *(uint32_t *)right; + + return a - b; +} + +void update_active_bar_modifiers() { + if (config->active_bar_modifiers->length > 0) { + list_free(config->active_bar_modifiers); + config->active_bar_modifiers = create_list(); + } + + struct bar_config *bar; + int i; + for (i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + if (strcmp(bar->mode, "hide") == 0 && strcmp(bar->hidden_state, "hide") == 0) { + if (list_seq_find(config->active_bar_modifiers, compare_modifiers, &bar->modifier) < 0) { + list_add(config->active_bar_modifiers, &bar->modifier); + } + } + } +} + +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", + }; + + if (!getenv("XDG_CONFIG_HOME")) { + char *home = getenv("HOME"); + char *config_home = malloc(strlen(home) + strlen("/.config") + 1); + if (!config_home) { + sway_log(L_ERROR, "Unable to allocate $HOME/.config"); + } else { + strcpy(config_home, home); + strcat(config_home, "/.config"); + setenv("XDG_CONFIG_HOME", config_home, 1); + sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); + free(config_home); + } + } + + wordexp_t p; + char *path; + + int i; + for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) { + if (wordexp(config_paths[i], &p, 0) == 0) { + path = strdup(p.we_wordv[0]); + wordfree(&p); + if (file_exists(path)) { + return path; + } + } + } + + return NULL; // Not reached +} + +const char *current_config_path; + +static bool load_config(const char *path, struct sway_config *config) { + sway_log(L_INFO, "Loading config from %s", path); + current_config_path = path; + + struct stat sb; + if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { + return false; + } + + if (path == NULL) { + sway_log(L_ERROR, "Unable to find a config file!"); + return false; + } + + FILE *f = fopen(path, "r"); + if (!f) { + sway_log(L_ERROR, "Unable to open %s for reading", path); + return false; + } + + bool config_load_success = read_config(f, config); + fclose(f); + + if (!config_load_success) { + sway_log(L_ERROR, "Error(s) loading config!"); + } + + current_config_path = NULL; + return true; +} + +static int qstrcmp(const void* a, const void* b) { + return strcmp(*((char**) a), *((char**) b)); +} + +bool load_main_config(const char *file, bool is_active) { + input_init(); + + 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); + if (is_active) { + sway_log(L_DEBUG, "Performing configuration file reload"); + config->reloading = true; + config->active = true; + } + + config->current_config = path; + list_add(config->config_chain, path); + + config->reading = true; + + // Read security configs + bool success = true; + DIR *dir = opendir(SYSCONFDIR "/sway/security.d"); + if (!dir) { + sway_log(L_ERROR, "%s does not exist, sway will have no security configuration" + " and will probably be broken", SYSCONFDIR "/sway/security.d"); + } 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]; + if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || (((s.st_mode & 0777) != 0644) && (s.st_mode & 0777) != 0444)) { + sway_log(L_ERROR, "Refusing to load %s - it must be owned by root and mode 644 or 444", _path); + success = false; + } else { + success = success && load_config(_path, config); + } + } + + free_flat_list(secconfigs); + } + + success = success && load_config(path, config); + + if (is_active) { + config->reloading = false; + } + + if (old_config) { + free_config(old_config); + } + config->reading = false; + + if (success) { + update_active_bar_modifiers(); + } + + return success; +} + +static bool load_include_config(const char *path, const char *parent_dir, struct sway_config *config) { + // save parent config + const char *parent_config = config->current_config; + + char *full_path = strdup(path); + int len = strlen(path); + if (len >= 1 && path[0] != '/') { + len = len + strlen(parent_dir) + 2; + full_path = malloc(len * sizeof(char)); + if (!full_path) { + sway_log(L_ERROR, "Unable to allocate full path to included config"); + return false; + } + snprintf(full_path, len, "%s/%s", parent_dir, path); + } + + char *real_path = realpath(full_path, NULL); + free(full_path); + + if (real_path == NULL) { + sway_log(L_DEBUG, "%s not found.", path); + 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) { + sway_log(L_DEBUG, "%s already included once, won't be included again.", real_path); + free(real_path); + return false; + } + } + + config->current_config = real_path; + list_add(config->config_chain, real_path); + int index = config->config_chain->length - 1; + + if (!load_config(real_path, config)) { + free(real_path); + config->current_config = parent_config; + list_del(config->config_chain, index); + return false; + } + + // restore current_config + config->current_config = parent_config; + return true; +} + +bool load_include_configs(const char *path, struct sway_config *config) { + char *wd = getcwd(NULL, 0); + char *parent_path = strdup(config->current_config); + 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) { + load_include_config(w[i], parent_dir, config); + } + free(parent_path); + wordfree(&p); + + // restore wd + if (chdir(wd) < 0) { + free(wd); + sway_log(L_ERROR, "failed to restore working directory"); + return false; + } + + free(wd); + return true; +} + +struct cmd_results *check_security_config() { + if (!current_config_path || strncmp(SYSCONFDIR "/sway/security.d/", current_config_path, + strlen(SYSCONFDIR "/sway/security.d/")) != 0) { + return cmd_results_new(CMD_INVALID, "permit", + "This command is only permitted to run from " SYSCONFDIR "/sway/security.d/*"); + } + return NULL; +} + +bool read_config(FILE *file, struct sway_config *config) { + bool success = true; + enum cmd_status block = CMD_BLOCK_END; + + int line_number = 0; + char *line; + while (!feof(file)) { + line = read_line(file); + if (!line) { + continue; + } + line_number++; + line = strip_whitespace(line); + if (line[0] == '#') { + free(line); + continue; + } + struct cmd_results *res; + if (block == CMD_BLOCK_COMMANDS) { + // Special case + res = config_commands_command(line); + } else { + res = config_command(line, block); + } + switch(res->status) { + case CMD_FAILURE: + case CMD_INVALID: + sway_log(L_ERROR, "Error on line %i '%s': %s (%s)", line_number, line, + res->error, config->current_config); + success = false; + break; + + case CMD_DEFER: + sway_log(L_DEBUG, "Defferring command `%s'", line); + list_add(config->cmd_queue, strdup(line)); + break; + + case CMD_BLOCK_MODE: + if (block == CMD_BLOCK_END) { + block = CMD_BLOCK_MODE; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_INPUT: + if (block == CMD_BLOCK_END) { + block = CMD_BLOCK_INPUT; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_BAR: + if (block == CMD_BLOCK_END) { + block = CMD_BLOCK_BAR; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_BAR_COLORS: + if (block == CMD_BLOCK_BAR) { + block = CMD_BLOCK_BAR_COLORS; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_COMMANDS: + if (block == CMD_BLOCK_END) { + block = CMD_BLOCK_COMMANDS; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_IPC: + if (block == CMD_BLOCK_END) { + block = CMD_BLOCK_IPC; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_IPC_EVENTS: + if (block == CMD_BLOCK_IPC) { + block = CMD_BLOCK_IPC_EVENTS; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + + case CMD_BLOCK_END: + switch(block) { + case CMD_BLOCK_MODE: + sway_log(L_DEBUG, "End of mode block"); + config->current_mode = config->modes->items[0]; + block = CMD_BLOCK_END; + break; + + case CMD_BLOCK_INPUT: + sway_log(L_DEBUG, "End of input block"); + current_input_config = NULL; + block = CMD_BLOCK_END; + break; + + case CMD_BLOCK_BAR: + sway_log(L_DEBUG, "End of bar block"); + config->current_bar = NULL; + block = CMD_BLOCK_END; + break; + + case CMD_BLOCK_BAR_COLORS: + sway_log(L_DEBUG, "End of bar colors block"); + block = CMD_BLOCK_BAR; + break; + + case CMD_BLOCK_COMMANDS: + sway_log(L_DEBUG, "End of commands block"); + block = CMD_BLOCK_END; + break; + + case CMD_BLOCK_IPC: + sway_log(L_DEBUG, "End of IPC block"); + block = CMD_BLOCK_END; + break; + + case CMD_BLOCK_IPC_EVENTS: + sway_log(L_DEBUG, "End of IPC events block"); + block = CMD_BLOCK_IPC; + break; + + case CMD_BLOCK_END: + sway_log(L_ERROR, "Unmatched }"); + break; + + default:; + } + default:; + } + free(line); + free_cmd_results(res); + } + + return success; +} + +int input_identifier_cmp(const void *item, const void *data) { + const struct input_config *ic = item; + const char *identifier = data; + return strcmp(ic->identifier, identifier); +} + +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); +} + +void merge_input_config(struct input_config *dst, struct input_config *src) { + if (src->identifier) { + if (dst->identifier) { + free(dst->identifier); + } + dst->identifier = strdup(src->identifier); + } + if (src->accel_profile != INT_MIN) { + dst->accel_profile = src->accel_profile; + } + if (src->click_method != INT_MIN) { + dst->click_method = src->click_method; + } + if (src->drag_lock != INT_MIN) { + dst->drag_lock = src->drag_lock; + } + if (src->dwt != INT_MIN) { + dst->dwt = src->dwt; + } + if (src->middle_emulation != INT_MIN) { + dst->middle_emulation = src->middle_emulation; + } + if (src->natural_scroll != INT_MIN) { + dst->natural_scroll = src->natural_scroll; + } + if (src->pointer_accel != FLT_MIN) { + dst->pointer_accel = src->pointer_accel; + } + if (src->scroll_method != INT_MIN) { + dst->scroll_method = src->scroll_method; + } + if (src->send_events != INT_MIN) { + dst->send_events = src->send_events; + } + if (src->tap != INT_MIN) { + dst->tap = src->tap; + } +} + +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->scale != -1) { + dst->scale = src->scale; + } + 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); + } +} + +static void invoke_swaybar(struct bar_config *bar) { + return; // TODO WLR + sway_log(L_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); + // Pipe to communicate errors + int filedes[2]; + if (pipe(filedes) == -1) { + sway_log(L_ERROR, "Pipe setup failed! Cannot fork into bar"); + return; + } + + bar->pid = fork(); + if (bar->pid == 0) { + close(filedes[0]); + if (!bar->swaybar_command) { + char *const cmd[] = { + "swaybar", + "-b", + bar->id, + NULL, + }; + + close(filedes[1]); + execvp(cmd[0], cmd); + _exit(EXIT_SUCCESS); + } else { + // run custom swaybar + int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; + char *command = malloc(len * sizeof(char)); + if (!command) { + const char msg[] = "Unable to allocate swaybar command string"; + int len = sizeof(msg); + if (write(filedes[1], &len, sizeof(int))) {}; + if (write(filedes[1], msg, len)) {}; + close(filedes[1]); + _exit(EXIT_FAILURE); + } + snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); + + char *const cmd[] = { + "sh", + "-c", + command, + NULL, + }; + + close(filedes[1]); + execvp(cmd[0], cmd); + free(command); + _exit(EXIT_SUCCESS); + } + } + close(filedes[0]); + int len; + if(read(filedes[1], &len, sizeof(int)) == sizeof(int)) { + char *buf = malloc(len); + if(!buf) { + sway_log(L_ERROR, "Cannot allocate error string"); + return; + } + if(read(filedes[1], buf, len)) { + sway_log(L_ERROR, "%s", buf); + } + free(buf); + } + close(filedes[1]); +} + +static void terminate_swaybar(pid_t pid) { + return; // TODO WLR + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } +} + +void terminate_swaybg(pid_t pid) { + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } +} + +static bool active_output(const char *name) { + int i; + swayc_t *cont = NULL; + for (i = 0; i < root_container.children->length; ++i) { + cont = root_container.children->items[i]; + if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { + return true; + } + } + + return false; +} + +void load_swaybars() { + // Check for bars + list_t *bars = create_list(); + struct bar_config *bar = NULL; + int i; + for (i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + bool apply = false; + if (bar->outputs) { + int j; + for (j = 0; j < bar->outputs->length; ++j) { + char *o = bar->outputs->items[j]; + if (!strcmp(o, "*") || active_output(o)) { + apply = true; + break; + } + } + } else { + apply = true; + } + if (apply) { + list_add(bars, bar); + } + } + + for (i = 0; i < bars->length; ++i) { + bar = bars->items[i]; + if (bar->pid != 0) { + terminate_swaybar(bar->pid); + } + invoke_swaybar(bar); + } + + list_free(bars); +} + +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; + } + } + // 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) { + sway_log(L_ERROR, + "Unable to allocate replacement during variable expansion"); + 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; +} + +// 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) { + const struct workspace_output *wsa = a, *wsb = b; + return lenient_strcmp(wsa->workspace, wsb->workspace); +} + +int sway_binding_cmp_keys(const void *a, const void *b) { + const struct sway_binding *binda = a, *bindb = b; + + // Count keys pressed for this binding. important so we check long before + // short ones. for example mod+a+b before mod+a + unsigned int moda = 0, modb = 0, i; + + // Count how any modifiers are pressed + for (i = 0; i < 8 * sizeof(binda->modifiers); ++i) { + moda += (binda->modifiers & 1 << i) != 0; + modb += (bindb->modifiers & 1 << i) != 0; + } + if (bindb->keys->length + modb != binda->keys->length + moda) { + return (bindb->keys->length + modb) - (binda->keys->length + moda); + } + + // Otherwise compare keys + if (binda->modifiers > bindb->modifiers) { + return 1; + } else if (binda->modifiers < bindb->modifiers) { + return -1; + } + struct wlc_modifiers no_mods = { 0, 0 }; + for (int i = 0; i < binda->keys->length; i++) { + xkb_keysym_t ka = *(xkb_keysym_t *)binda->keys->items[i], + kb = *(xkb_keysym_t *)bindb->keys->items[i]; + if (binda->bindcode) { + uint32_t *keycode = binda->keys->items[i]; + ka = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); + } + + if (bindb->bindcode) { + uint32_t *keycode = bindb->keys->items[i]; + kb = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); + } + + if (ka > kb) { + return 1; + } else if (ka < kb) { + return -1; + } + } + + return 0; +} + +int sway_binding_cmp(const void *a, const void *b) { + int cmp = 0; + if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { + return cmp; + } + const struct sway_binding *binda = a, *bindb = b; + return lenient_strcmp(binda->command, bindb->command); +} + +int sway_binding_cmp_qsort(const void *a, const void *b) { + return sway_binding_cmp(*(void **)a, *(void **)b); +} + +void free_sway_binding(struct sway_binding *binding) { + if (binding->keys) { + for (int i = 0; i < binding->keys->length; i++) { + free(binding->keys->items[i]); + } + list_free(binding->keys); + } + if (binding->command) { + free(binding->command); + } + free(binding); +} + +int sway_mouse_binding_cmp_buttons(const void *a, const void *b) { + const struct sway_mouse_binding *binda = a, *bindb = b; + if (binda->button > bindb->button) { + return 1; + } + if (binda->button < bindb->button) { + return -1; + } + return 0; +} + +int sway_mouse_binding_cmp(const void *a, const void *b) { + int cmp = 0; + if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { + return cmp; + } + const struct sway_mouse_binding *binda = a, *bindb = b; + return lenient_strcmp(binda->command, bindb->command); +} + +int sway_mouse_binding_cmp_qsort(const void *a, const void *b) { + return sway_mouse_binding_cmp(*(void **)a, *(void **)b); +} + +void free_sway_mouse_binding(struct sway_mouse_binding *binding) { + if (binding->command) { + free(binding->command); + } + free(binding); +} + +struct sway_binding *sway_binding_dup(struct sway_binding *sb) { + struct sway_binding *new_sb = malloc(sizeof(struct sway_binding)); + if (!new_sb) { + return NULL; + } + + new_sb->order = sb->order; + new_sb->modifiers = sb->modifiers; + new_sb->command = strdup(sb->command); + + new_sb->keys = create_list(); + int i; + for (i = 0; i < sb->keys->length; ++i) { + xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); + if (!key) { + free_sway_binding(new_sb); + return NULL; + } + *key = *(xkb_keysym_t *)sb->keys->items[i]; + list_add(new_sb->keys, key); + } + + return new_sb; +} + +struct bar_config *default_bar_config(void) { + struct bar_config *bar = NULL; + bar = malloc(sizeof(struct bar_config)); + if (!bar) { + return NULL; + } + if (!(bar->mode = strdup("dock"))) goto cleanup; + if (!(bar->hidden_state = strdup("hide"))) goto cleanup; + bar->modifier = WLC_BIT_MOD_LOGO; + bar->outputs = NULL; + bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; + if (!(bar->bindings = create_list())) goto cleanup; + if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p'; sleep 1; done"))) goto cleanup; + bar->pango_markup = false; + bar->swaybar_command = NULL; + bar->font = NULL; + bar->height = -1; + bar->workspace_buttons = true; + bar->wrap_scroll = false; + bar->separator_symbol = NULL; + bar->strip_workspace_numbers = false; + bar->binding_mode_indicator = true; +#ifdef ENABLE_TRAY + bar->tray_output = NULL; + bar->icon_theme = NULL; + bar->tray_padding = 2; + bar->activate_button = 0x110; /* BTN_LEFT */ + bar->context_button = 0x111; /* BTN_RIGHT */ + bar->secondary_button = 0x112; /* BTN_MIDDLE */ +#endif + bar->verbose = false; + bar->pid = 0; + // set default colors + if (!(bar->colors.background = strndup("#000000ff", 9))) goto cleanup; + if (!(bar->colors.statusline = strndup("#ffffffff", 9))) goto cleanup; + if (!(bar->colors.separator = strndup("#666666ff", 9))) goto cleanup; + if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) goto cleanup; + if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) goto cleanup; + if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) goto cleanup; + if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) goto cleanup; + if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) goto cleanup; + if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) goto cleanup; + if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) goto cleanup; + if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) goto cleanup; + if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) goto cleanup; + if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) goto cleanup; + if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) goto cleanup; + if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) goto cleanup; + // if the following colors stay undefined, they fall back to background, + // statusline, separator and urgent_workspace_*. + bar->colors.focused_background = NULL; + bar->colors.focused_statusline = NULL; + bar->colors.focused_separator = NULL; + bar->colors.binding_mode_border = NULL; + bar->colors.binding_mode_bg = NULL; + bar->colors.binding_mode_text = NULL; + + list_add(config->bars, bar); + + return bar; + +cleanup: + free_bar(bar); + return NULL; +} diff --git a/sway/tree/container.c b/sway/old/container.c diff --git a/sway/tree/criteria.c b/sway/old/criteria.c diff --git a/sway/tree/focus.c b/sway/old/focus.c diff --git a/sway/handlers.c b/sway/old/handlers.c diff --git a/sway/input_state.c b/sway/old/input_state.c diff --git a/sway/ipc-json.c b/sway/old/ipc-json.c diff --git a/sway/old/ipc-server.c b/sway/old/ipc-server.c @@ -0,0 +1,840 @@ +// See https://i3wm.org/docs/ipc.html for protocol information +#ifndef __FreeBSD__ +// Any value will hide SOCK_CLOEXEC on FreeBSD (__BSD_VISIBLE=0) +#define _XOPEN_SOURCE 700 +#endif +#include <errno.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <stdbool.h> +#include <unistd.h> +#include <stdlib.h> +#include <sys/ioctl.h> +#include <fcntl.h> +#include <json-c/json.h> +#include <list.h> +#include <libinput.h> +#ifdef __linux__ +struct ucred { + pid_t pid; + uid_t uid; + gid_t gid; +}; +#endif +#include "sway/ipc-json.h" +#include "sway/ipc-server.h" +#include "sway/security.h" +#include "sway/config.h" +#include "sway/commands.h" +#include "sway/input.h" +#include "sway/server.h" +#include "stringop.h" +#include "log.h" +#include "list.h" +#include "util.h" + +static int ipc_socket = -1; +static struct wl_event_source *ipc_event_source = NULL; +static struct sockaddr_un *ipc_sockaddr = NULL; +static list_t *ipc_client_list = NULL; + +static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; + +struct ipc_client { + struct wl_event_source *event_source; + struct wl_event_source *writable_event_source; + int fd; + uint32_t payload_length; + uint32_t security_policy; + enum ipc_command_type current_command; + enum ipc_command_type subscribed_events; + size_t write_buffer_len; + size_t write_buffer_size; + char *write_buffer; +}; + +struct sockaddr_un *ipc_user_sockaddr(void); +int ipc_handle_connection(int fd, uint32_t mask, void *data); +int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); +int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data); +void ipc_client_disconnect(struct ipc_client *client); +void ipc_client_handle_command(struct ipc_client *client); +bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); +void ipc_get_workspaces_callback(swayc_t *workspace, void *data); +void ipc_get_outputs_callback(swayc_t *container, void *data); +static void ipc_get_marks_callback(swayc_t *container, void *data); + +void ipc_init(void) { + ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); + if (ipc_socket == -1) { + sway_abort("Unable to create IPC socket"); + } + + ipc_sockaddr = ipc_user_sockaddr(); + + // We want to use socket name set by user, not existing socket from another sway instance. + if (getenv("SWAYSOCK") != NULL && access(getenv("SWAYSOCK"), F_OK) == -1) { + strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path)); + ipc_sockaddr->sun_path[sizeof(ipc_sockaddr->sun_path) - 1] = 0; + } + + unlink(ipc_sockaddr->sun_path); + if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) { + sway_abort("Unable to bind IPC socket"); + } + + if (listen(ipc_socket, 3) == -1) { + sway_abort("Unable to listen on IPC socket"); + } + + // Set i3 IPC socket path so that i3-msg works out of the box + setenv("I3SOCK", ipc_sockaddr->sun_path, 1); + setenv("SWAYSOCK", ipc_sockaddr->sun_path, 1); + + ipc_client_list = create_list(); + + ipc_event_source = wl_event_loop_add_fd(server.wl_event_loop, ipc_socket, + WL_EVENT_READABLE, ipc_handle_connection, NULL); +} + +void ipc_terminate(void) { + if (ipc_event_source) { + wl_event_source_remove(ipc_event_source); + } + close(ipc_socket); + unlink(ipc_sockaddr->sun_path); + + list_free(ipc_client_list); + + if (ipc_sockaddr) { + free(ipc_sockaddr); + } +} + +struct sockaddr_un *ipc_user_sockaddr(void) { + struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); + if (ipc_sockaddr == NULL) { + sway_abort("Can't allocate ipc_sockaddr"); + } + + ipc_sockaddr->sun_family = AF_UNIX; + int path_size = sizeof(ipc_sockaddr->sun_path); + + // Env var typically set by logind, e.g. "/run/user/<user-id>" + const char *dir = getenv("XDG_RUNTIME_DIR"); + if (!dir) { + dir = "/tmp"; + } + if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size, + "%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) { + sway_abort("Socket path won't fit into ipc_sockaddr->sun_path"); + } + + return ipc_sockaddr; +} + +static pid_t get_client_pid(int client_fd) { +// FreeBSD supports getting uid/gid, but not pid +#ifdef __linux__ + struct ucred ucred; + socklen_t len = sizeof(struct ucred); + + if (getsockopt(client_fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { + return -1; + } + + return ucred.pid; +#else + return -1; +#endif +} + +int ipc_handle_connection(int fd, uint32_t mask, void *data) { + (void) fd; (void) data; + sway_log(L_DEBUG, "Event on IPC listening socket"); + assert(mask == WL_EVENT_READABLE); + + int client_fd = accept(ipc_socket, NULL, NULL); + if (client_fd == -1) { + sway_log_errno(L_ERROR, "Unable to accept IPC client connection"); + return 0; + } + + int flags; + if ((flags = fcntl(client_fd, F_GETFD)) == -1 + || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { + sway_log_errno(L_ERROR, "Unable to set CLOEXEC on IPC client socket"); + close(client_fd); + return 0; + } + if ((flags = fcntl(client_fd, F_GETFL)) == -1 + || fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) { + sway_log_errno(L_ERROR, "Unable to set NONBLOCK on IPC client socket"); + close(client_fd); + return 0; + } + + struct ipc_client* client = malloc(sizeof(struct ipc_client)); + if (!client) { + sway_log(L_ERROR, "Unable to allocate ipc client"); + close(client_fd); + return 0; + } + client->payload_length = 0; + client->fd = client_fd; + client->subscribed_events = 0; + client->event_source = wl_event_loop_add_fd(server.wl_event_loop, client_fd, + WL_EVENT_READABLE, ipc_client_handle_readable, client); + client->writable_event_source = NULL; + + client->write_buffer_size = 128; + client->write_buffer_len = 0; + client->write_buffer = malloc(client->write_buffer_size); + if (!client->write_buffer) { + sway_log(L_ERROR, "Unable to allocate ipc client write buffer"); + close(client_fd); + return 0; + } + + pid_t pid = get_client_pid(client->fd); + client->security_policy = get_ipc_policy_mask(pid); + + sway_log(L_DEBUG, "New client: fd %d, pid %d", client_fd, pid); + + list_add(ipc_client_list, client); + + return 0; +} + +static const int ipc_header_size = sizeof(ipc_magic)+8; + +int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { + struct ipc_client *client = data; + + if (mask & WL_EVENT_ERROR) { + sway_log(L_ERROR, "IPC Client socket error, removing client"); + ipc_client_disconnect(client); + return 0; + } + + if (mask & WL_EVENT_HANGUP) { + sway_log(L_DEBUG, "Client %d hung up", client->fd); + ipc_client_disconnect(client); + return 0; + } + + sway_log(L_DEBUG, "Client %d readable", client->fd); + + int read_available; + if (ioctl(client_fd, FIONREAD, &read_available) == -1) { + sway_log_errno(L_INFO, "Unable to read IPC socket buffer size"); + ipc_client_disconnect(client); + return 0; + } + + // Wait for the rest of the command payload in case the header has already been read + if (client->payload_length > 0) { + if ((uint32_t)read_available >= client->payload_length) { + ipc_client_handle_command(client); + } + return 0; + } + + if (read_available < ipc_header_size) { + return 0; + } + + uint8_t buf[ipc_header_size]; + uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic)); + // Should be fully available, because read_available >= ipc_header_size + ssize_t received = recv(client_fd, buf, ipc_header_size, 0); + if (received == -1) { + sway_log_errno(L_INFO, "Unable to receive header from IPC client"); + ipc_client_disconnect(client); + return 0; + } + + if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { + sway_log(L_DEBUG, "IPC header check failed"); + ipc_client_disconnect(client); + return 0; + } + + client->payload_length = buf32[0]; + client->current_command = (enum ipc_command_type)buf32[1]; + + if (read_available - received >= (long)client->payload_length) { + ipc_client_handle_command(client); + } + + return 0; +} + +int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { + struct ipc_client *client = data; + + if (mask & WL_EVENT_ERROR) { + sway_log(L_ERROR, "IPC Client socket error, removing client"); + ipc_client_disconnect(client); + return 0; + } + + if (mask & WL_EVENT_HANGUP) { + sway_log(L_DEBUG, "Client %d hung up", client->fd); + ipc_client_disconnect(client); + return 0; + } + + if (client->write_buffer_len <= 0) { + return 0; + } + + sway_log(L_DEBUG, "Client %d writable", client->fd); + + ssize_t written = write(client->fd, client->write_buffer, client->write_buffer_len); + + if (written == -1 && errno == EAGAIN) { + return 0; + } else if (written == -1) { + sway_log_errno(L_INFO, "Unable to send data from queue to IPC client"); + ipc_client_disconnect(client); + return 0; + } + + memmove(client->write_buffer, client->write_buffer + written, client->write_buffer_len - written); + client->write_buffer_len -= written; + + if (client->write_buffer_len == 0 && client->writable_event_source) { + wl_event_source_remove(client->writable_event_source); + client->writable_event_source = NULL; + } + + return 0; +} + +void ipc_client_disconnect(struct ipc_client *client) { + if (!sway_assert(client != NULL, "client != NULL")) { + return; + } + + if (client->fd != -1) { + shutdown(client->fd, SHUT_RDWR); + } + + sway_log(L_INFO, "IPC Client %d disconnected", client->fd); + wl_event_source_remove(client->event_source); + if (client->writable_event_source) { + wl_event_source_remove(client->writable_event_source); + } + int i = 0; + while (i < ipc_client_list->length && ipc_client_list->items[i] != client) i++; + list_del(ipc_client_list, i); + free(client->write_buffer); + close(client->fd); + free(client); +} + +bool output_by_name_test(swayc_t *view, void *data) { + char *name = (char *)data; + if (view->type != C_OUTPUT) { + return false; + } + return !strcmp(name, view->name); +} + +// greedy wildcard (only "*") matching +bool mime_type_matches(const char *mime_type, const char *pattern) { + const char *wildcard = NULL; + while (*mime_type && *pattern) { + if (*pattern == '*' && !wildcard) { + wildcard = pattern; + ++pattern; + } + + if (*mime_type != *pattern) { + if (!wildcard) + return false; + + pattern = wildcard; + ++mime_type; + continue; + } + + ++mime_type; + ++pattern; + } + + while (*pattern == '*') { + ++pattern; + } + + return (*mime_type == *pattern); +} + +void ipc_client_handle_command(struct ipc_client *client) { + if (!sway_assert(client != NULL, "client != NULL")) { + return; + } + + char *buf = malloc(client->payload_length + 1); + if (!buf) { + sway_log_errno(L_INFO, "Unable to allocate IPC payload"); + ipc_client_disconnect(client); + return; + } + if (client->payload_length > 0) { + // Payload should be fully available + ssize_t received = recv(client->fd, buf, client->payload_length, 0); + if (received == -1) + { + sway_log_errno(L_INFO, "Unable to receive payload from IPC client"); + ipc_client_disconnect(client); + free(buf); + return; + } + } + buf[client->payload_length] = '\0'; + + const char *error_denied = "{ \"success\": false, \"error\": \"Permission denied\" }"; + + switch (client->current_command) { + case IPC_COMMAND: + { + if (!(client->security_policy & IPC_FEATURE_COMMAND)) { + goto exit_denied; + } + struct cmd_results *results = handle_command(buf, CONTEXT_IPC); + const char *json = cmd_results_to_json(results); + char reply[256]; + int length = snprintf(reply, sizeof(reply), "%s", json); + ipc_send_reply(client, reply, (uint32_t) length); + free_cmd_results(results); + goto exit_cleanup; + } + + case IPC_SUBSCRIBE: + { + // TODO: Check if they're permitted to use these events + struct json_object *request = json_tokener_parse(buf); + if (request == NULL) { + ipc_send_reply(client, "{\"success\": false}", 18); + sway_log_errno(L_INFO, "Failed to read request"); + goto exit_cleanup; + } + + // parse requested event types + for (int i = 0; i < json_object_array_length(request); i++) { + const char *event_type = json_object_get_string(json_object_array_get_idx(request, i)); + if (strcmp(event_type, "workspace") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE); + } else if (strcmp(event_type, "barconfig_update") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE); + } else if (strcmp(event_type, "mode") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_MODE); + } else if (strcmp(event_type, "window") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_WINDOW); + } else if (strcmp(event_type, "modifier") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_MODIFIER); + } else if (strcmp(event_type, "binding") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_BINDING); + } else { + ipc_send_reply(client, "{\"success\": false}", 18); + json_object_put(request); + sway_log_errno(L_INFO, "Failed to parse request"); + goto exit_cleanup; + } + } + + json_object_put(request); + + ipc_send_reply(client, "{\"success\": true}", 17); + goto exit_cleanup; + } + + case IPC_GET_WORKSPACES: + { + if (!(client->security_policy & IPC_FEATURE_GET_WORKSPACES)) { + goto exit_denied; + } + json_object *workspaces = json_object_new_array(); + container_map(&root_container, ipc_get_workspaces_callback, workspaces); + const char *json_string = json_object_to_json_string(workspaces); + ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); + json_object_put(workspaces); // free + goto exit_cleanup; + } + + case IPC_GET_INPUTS: + { + if (!(client->security_policy & IPC_FEATURE_GET_INPUTS)) { + goto exit_denied; + } + json_object *inputs = json_object_new_array(); + /* TODO WLR + if (input_devices) { + for(int i = 0; i<input_devices->length; i++) { + struct libinput_device *device = input_devices->items[i]; + json_object_array_add(inputs, ipc_json_describe_input(device)); + } + } + */ + const char *json_string = json_object_to_json_string(inputs); + ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); + json_object_put(inputs); + goto exit_cleanup; + } + + case IPC_GET_OUTPUTS: + { + if (!(client->security_policy & IPC_FEATURE_GET_OUTPUTS)) { + goto exit_denied; + } + json_object *outputs = json_object_new_array(); + container_map(&root_container, ipc_get_outputs_callback, outputs); + const char *json_string = json_object_to_json_string(outputs); + ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); + json_object_put(outputs); // free + goto exit_cleanup; + } + + case IPC_GET_TREE: + { + if (!(client->security_policy & IPC_FEATURE_GET_TREE)) { + goto exit_denied; + } + json_object *tree = ipc_json_describe_container_recursive(&root_container); + const char *json_string = json_object_to_json_string(tree); + ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); + json_object_put(tree); + goto exit_cleanup; + } + + case IPC_GET_MARKS: + { + if (!(client->security_policy & IPC_FEATURE_GET_MARKS)) { + goto exit_denied; + } + json_object *marks = json_object_new_array(); + container_map(&root_container, ipc_get_marks_callback, marks); + const char *json_string = json_object_to_json_string(marks); + ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); + json_object_put(marks); + goto exit_cleanup; + } + + case IPC_GET_VERSION: + { + json_object *version = ipc_json_get_version(); + const char *json_string = json_object_to_json_string(version); + ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); + json_object_put(version); // free + goto exit_cleanup; + } + + case IPC_GET_BAR_CONFIG: + { + if (!(client->security_policy & IPC_FEATURE_GET_BAR_CONFIG)) { + goto exit_denied; + } + if (!buf[0]) { + // Send list of configured bar IDs + json_object *bars = json_object_new_array(); + int i; + for (i = 0; i < config->bars->length; ++i) { + struct bar_config *bar = config->bars->items[i]; + json_object_array_add(bars, json_object_new_string(bar->id)); + } + const char *json_string = json_object_to_json_string(bars); + ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); + json_object_put(bars); // free + } else { + // Send particular bar's details + struct bar_config *bar = NULL; + int i; + for (i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + if (strcmp(buf, bar->id) == 0) { + break; + } + bar = NULL; + } + if (!bar) { + const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }"; + ipc_send_reply(client, error, (uint32_t)strlen(error)); + goto exit_cleanup; + } + json_object *json = ipc_json_describe_bar_config(bar); + const char *json_string = json_object_to_json_string(json); + ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); + json_object_put(json); // free + } + goto exit_cleanup; + } + + case IPC_GET_CLIPBOARD: + // TODO WLR + break; + + default: + sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); + goto exit_cleanup; + } + +exit_denied: + ipc_send_reply(client, error_denied, (uint32_t)strlen(error_denied)); + sway_log(L_DEBUG, "Denied IPC client access to %i", client->current_command); + +exit_cleanup: + client->payload_length = 0; + free(buf); + return; +} + +bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { + assert(payload); + + char data[ipc_header_size]; + uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); + + memcpy(data, ipc_magic, sizeof(ipc_magic)); + data32[0] = payload_length; + data32[1] = client->current_command; + + while (client->write_buffer_len + ipc_header_size + payload_length >= + client->write_buffer_size) { + client->write_buffer_size *= 2; + } + + // TODO: reduce the limit back to 4 MB when screenshooter is implemented + if (client->write_buffer_size > (1 << 28)) { // 256 MB + sway_log(L_ERROR, "Client write buffer too big, disconnecting client"); + ipc_client_disconnect(client); + return false; + } + + char *new_buffer = realloc(client->write_buffer, client->write_buffer_size); + if (!new_buffer) { + sway_log(L_ERROR, "Unable to reallocate ipc client write buffer"); + ipc_client_disconnect(client); + return false; + } + client->write_buffer = new_buffer; + + memcpy(client->write_buffer + client->write_buffer_len, data, ipc_header_size); + client->write_buffer_len += ipc_header_size; + memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length); + client->write_buffer_len += payload_length; + + if (!client->writable_event_source) { + client->writable_event_source = wl_event_loop_add_fd( + server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, + ipc_client_handle_writable, client); + } + + sway_log(L_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload); + + return true; +} + +void ipc_get_workspaces_callback(swayc_t *workspace, void *data) { + if (workspace->type == C_WORKSPACE) { + json_object *workspace_json = ipc_json_describe_container(workspace); + // override the default focused indicator because + // it's set differently for the get_workspaces reply + bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace; + json_object_object_del(workspace_json, "focused"); + json_object_object_add(workspace_json, "focused", json_object_new_boolean(focused)); + json_object_array_add((json_object *)data, workspace_json); + } +} + +void ipc_get_outputs_callback(swayc_t *container, void *data) { + if (container->type == C_OUTPUT) { + json_object_array_add((json_object *)data, ipc_json_describe_container(container)); + } +} + +static void ipc_get_marks_callback(swayc_t *container, void *data) { + json_object *object = (json_object *)data; + if (container->marks) { + for (int i = 0; i < container->marks->length; ++i) { + char *mark = (char *)container->marks->items[i]; + json_object_array_add(object, json_object_new_string(mark)); + } + } +} + +void ipc_send_event(const char *json_string, enum ipc_command_type event) { + static struct { + enum ipc_command_type event; + enum ipc_feature feature; + } security_mappings[] = { + { IPC_EVENT_WORKSPACE, IPC_FEATURE_EVENT_WORKSPACE }, + { IPC_EVENT_OUTPUT, IPC_FEATURE_EVENT_OUTPUT }, + { IPC_EVENT_MODE, IPC_FEATURE_EVENT_MODE }, + { IPC_EVENT_WINDOW, IPC_FEATURE_EVENT_WINDOW }, + { IPC_EVENT_BINDING, IPC_FEATURE_EVENT_BINDING }, + { IPC_EVENT_INPUT, IPC_FEATURE_EVENT_INPUT } + }; + + uint32_t security_mask = 0; + for (size_t i = 0; i < sizeof(security_mappings) / sizeof(security_mappings[0]); ++i) { + if (security_mappings[i].event == event) { + security_mask = security_mappings[i].feature; + break; + } + } + + int i; + struct ipc_client *client; + for (i = 0; i < ipc_client_list->length; i++) { + client = ipc_client_list->items[i]; + if (!(client->security_policy & security_mask)) { + continue; + } + if ((client->subscribed_events & event_mask(event)) == 0) { + continue; + } + client->current_command = event; + if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) { + sway_log_errno(L_INFO, "Unable to send reply to IPC client"); + ipc_client_disconnect(client); + } + } +} + +void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change) { + sway_log(L_DEBUG, "Sending workspace::%s event", change); + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "change", json_object_new_string(change)); + if (strcmp("focus", change) == 0) { + if (old) { + json_object_object_add(obj, "old", ipc_json_describe_container_recursive(old)); + } else { + json_object_object_add(obj, "old", NULL); + } + } + + if (new) { + json_object_object_add(obj, "current", ipc_json_describe_container_recursive(new)); + } else { + json_object_object_add(obj, "current", NULL); + } + + const char *json_string = json_object_to_json_string(obj); + ipc_send_event(json_string, IPC_EVENT_WORKSPACE); + + json_object_put(obj); // free +} + +void ipc_event_window(swayc_t *window, const char *change) { + sway_log(L_DEBUG, "Sending window::%s event", change); + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "change", json_object_new_string(change)); + json_object_object_add(obj, "container", ipc_json_describe_container_recursive(window)); + + const char *json_string = json_object_to_json_string(obj); + ipc_send_event(json_string, IPC_EVENT_WINDOW); + + json_object_put(obj); // free +} + +void ipc_event_barconfig_update(struct bar_config *bar) { + sway_log(L_DEBUG, "Sending barconfig_update event"); + json_object *json = ipc_json_describe_bar_config(bar); + const char *json_string = json_object_to_json_string(json); + ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE); + + json_object_put(json); // free +} + +void ipc_event_mode(const char *mode) { + sway_log(L_DEBUG, "Sending mode::%s event", mode); + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "change", json_object_new_string(mode)); + + const char *json_string = json_object_to_json_string(obj); + ipc_send_event(json_string, IPC_EVENT_MODE); + + json_object_put(obj); // free +} + +void ipc_event_modifier(uint32_t modifier, const char *state) { + sway_log(L_DEBUG, "Sending modifier::%s event", state); + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "change", json_object_new_string(state)); + + const char *modifier_name = get_modifier_name_by_mask(modifier); + json_object_object_add(obj, "modifier", json_object_new_string(modifier_name)); + + const char *json_string = json_object_to_json_string(obj); + ipc_send_event(json_string, IPC_EVENT_MODIFIER); + + json_object_put(obj); // free +} + +static void ipc_event_binding(json_object *sb_obj) { + sway_log(L_DEBUG, "Sending binding::run event"); + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "change", json_object_new_string("run")); + json_object_object_add(obj, "binding", sb_obj); + + const char *json_string = json_object_to_json_string(obj); + ipc_send_event(json_string, IPC_EVENT_BINDING); + + json_object_put(obj); // free +} + +void ipc_event_binding_keyboard(struct sway_binding *sb) { + json_object *sb_obj = json_object_new_object(); + json_object_object_add(sb_obj, "command", json_object_new_string(sb->command)); + + const char *names[10]; + + int len = get_modifier_names(names, sb->modifiers); + int i; + json_object *modifiers = json_object_new_array(); + for (i = 0; i < len; ++i) { + json_object_array_add(modifiers, json_object_new_string(names[i])); + } + + json_object_object_add(sb_obj, "event_state_mask", modifiers); + + json_object *input_codes = json_object_new_array(); + int input_code = 0; + json_object *symbols = json_object_new_array(); + json_object *symbol = NULL; + + if (sb->bindcode) { // bindcode: populate input_codes + uint32_t keycode; + for (i = 0; i < sb->keys->length; ++i) { + keycode = *(uint32_t *)sb->keys->items[i]; + json_object_array_add(input_codes, json_object_new_int(keycode)); + if (i == 0) { + input_code = keycode; + } + } + } else { // bindsym: populate symbols + uint32_t keysym; + char buffer[64]; + for (i = 0; i < sb->keys->length; ++i) { + keysym = *(uint32_t *)sb->keys->items[i]; + if (xkb_keysym_get_name(keysym, buffer, 64) > 0) { + json_object *str = json_object_new_string(buffer); + json_object_array_add(symbols, str); + if (i == 0) { + symbol = str; + } + } + } + } + + json_object_object_add(sb_obj, "input_codes", input_codes); + json_object_object_add(sb_obj, "input_code", json_object_new_int(input_code)); + json_object_object_add(sb_obj, "symbols", symbols); + json_object_object_add(sb_obj, "symbol", symbol); + json_object_object_add(sb_obj, "input_type", json_object_new_string("keyboard")); + + ipc_event_binding(sb_obj); +} diff --git a/sway/tree/layout.c b/sway/old/layout.c diff --git a/sway/tree/output.c b/sway/old/output.c diff --git a/sway/security.c b/sway/old/security.c diff --git a/sway/tree/workspace.c b/sway/old/workspace.c diff --git a/sway/server.c b/sway/server.c @@ -10,7 +10,6 @@ // TODO WLR: make Xwayland optional #include <wlr/xwayland.h> #include "sway/server.h" -#include "sway/input.h" #include "log.h" bool server_init(struct sway_server *server) { @@ -23,7 +22,6 @@ bool server_init(struct sway_server *server) { server->renderer = wlr_gles2_renderer_create(server->backend); wl_display_init_shm(server->wl_display); - server->input = sway_input_create(server); server->data_device_manager = wlr_data_device_manager_create(server->wl_display);