From 1efd5f819f9986bf27e390f4988359388606cea0 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 11 Nov 2017 14:41:18 -0500 Subject: Wire up output frame loop --- sway/commands/focus.c | 1 + sway/commands/move.c | 1 + 2 files changed, 2 insertions(+) (limited to 'sway/commands') diff --git a/sway/commands/focus.c b/sway/commands/focus.c index defaba29..c83157b8 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200809L #include #include #include diff --git a/sway/commands/move.c b/sway/commands/move.c index 8d89f2ef..52c73e22 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200809L #include #include #include -- cgit v1.2.3 From 733993a651c71f7e2198d505960d6bbd31e0e107 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 18 Nov 2017 11:22:02 -0500 Subject: Move everything to sway/old/ --- CMakeLists.txt | 12 +- common/log.c | 2 +- include/sway.h | 6 - include/sway/border.h | 28 - include/sway/commands.h | 214 --- include/sway/config.h | 406 ----- include/sway/container.h | 362 ----- include/sway/criteria.h | 42 - include/sway/focus.h | 45 - include/sway/handlers.h | 11 - include/sway/input.h | 24 - include/sway/input_state.h | 102 -- include/sway/ipc-json.h | 15 - include/sway/ipc-server.h | 41 - include/sway/layout.h | 85 - include/sway/old/border.h | 28 + include/sway/old/commands.h | 214 +++ include/sway/old/config.h | 406 +++++ include/sway/old/container.h | 362 +++++ include/sway/old/criteria.h | 42 + include/sway/old/focus.h | 45 + include/sway/old/input.h | 24 + include/sway/old/input_state.h | 102 ++ include/sway/old/ipc-json.h | 15 + include/sway/old/ipc-server.h | 33 + include/sway/old/layout.h | 85 + include/sway/old/output.h | 36 + include/sway/old/workspace.h | 22 + include/sway/output.h | 22 +- include/sway/workspace.h | 22 - sway/CMakeLists.txt | 27 - sway/border.c | 510 ------ sway/commands.c | 686 -------- sway/commands/assign.c | 57 - sway/commands/bar.c | 59 - sway/commands/bar/activate_button.c | 26 - sway/commands/bar/binding_mode_indicator.c | 27 - sway/commands/bar/bindsym.c | 48 - sway/commands/bar/colors.c | 129 -- sway/commands/bar/context_button.c | 26 - sway/commands/bar/font.c | 26 - sway/commands/bar/height.c | 21 - sway/commands/bar/hidden_state.c | 79 - sway/commands/bar/icon_theme.c | 25 - sway/commands/bar/id.c | 33 - sway/commands/bar/mode.c | 81 - sway/commands/bar/modifier.c | 35 - sway/commands/bar/output.c | 50 - sway/commands/bar/pango_markup.c | 27 - sway/commands/bar/position.c | 33 - sway/commands/bar/secondary_button.c | 26 - sway/commands/bar/separator_symbol.c | 21 - sway/commands/bar/status_command.c | 21 - sway/commands/bar/strip_workspace_numbers.c | 27 - sway/commands/bar/swaybar_command.c | 21 - sway/commands/bar/tray_output.c | 29 - sway/commands/bar/tray_padding.c | 34 - sway/commands/bar/workspace_buttons.c | 27 - sway/commands/bar/wrap_scroll.c | 27 - sway/commands/bind.c | 170 -- sway/commands/border.c | 65 - sway/commands/client.c | 72 - sway/commands/clipboard.c | 38 - sway/commands/commands.c | 26 - sway/commands/debuglog.c | 27 - sway/commands/default_border.c | 44 - sway/commands/default_floating_border.c | 45 - sway/commands/exec.c | 16 - sway/commands/exec_always.c | 85 - sway/commands/exit.c | 17 - sway/commands/floating.c | 81 - sway/commands/floating_maximum_size.c | 38 - sway/commands/floating_minimum_size.c | 38 - sway/commands/floating_mod.c | 42 - sway/commands/floating_scroll.c | 46 - sway/commands/focus.c | 100 -- sway/commands/focus_follows_mouse.c | 13 - sway/commands/font.c | 27 - sway/commands/for_window.c | 41 - sway/commands/force_focus_wrapping.c | 13 - sway/commands/fullscreen.c | 60 - sway/commands/gaps.c | 166 -- sway/commands/hide_edge_borders.c | 27 - sway/commands/include.c | 15 - sway/commands/input.c | 55 - sway/commands/input/accel_profile.c | 27 - sway/commands/input/click_method.c | 30 - sway/commands/input/drag_lock.c | 26 - sway/commands/input/dwt.c | 26 - sway/commands/input/events.c | 30 - sway/commands/input/left_handed.c | 26 - sway/commands/input/middle_emulation.c | 26 - sway/commands/input/natural_scroll.c | 26 - sway/commands/input/pointer_accel.c | 24 - sway/commands/input/scroll_method.c | 30 - sway/commands/input/tap.c | 29 - sway/commands/ipc.c | 162 -- sway/commands/kill.c | 12 - sway/commands/layout.c | 196 --- sway/commands/log_colors.c | 22 - sway/commands/mark.c | 87 - sway/commands/mode.c | 57 - sway/commands/mouse_warping.c | 17 - sway/commands/move.c | 189 --- sway/commands/new_float.c | 8 - sway/commands/new_window.c | 8 - sway/commands/no_focus.c | 41 - sway/commands/orientation.c | 21 - sway/commands/output.c | 219 --- sway/commands/permit.c | 108 -- sway/commands/reload.c | 19 - sway/commands/resize.c | 375 ----- sway/commands/scratchpad.c | 72 - sway/commands/seamless_mouse.c | 13 - sway/commands/set.c | 61 - sway/commands/show_marks.c | 13 - sway/commands/smart_gaps.c | 20 - sway/commands/split.c | 98 -- sway/commands/sticky.c | 25 - sway/commands/unmark.c | 31 - sway/commands/workspace.c | 92 -- sway/commands/workspace_auto_back_and_forth.c | 18 - sway/commands/workspace_layout.c | 40 - sway/config.c | 1449 ----------------- sway/debug_log.c | 103 -- sway/desktop/output.c | 19 +- sway/desktop/xdg_shell_v6.c | 61 +- sway/handlers.c | 1143 ------------- sway/input_state.c | 490 ------ sway/ipc-json.c | 521 ------ sway/ipc-server.c | 845 ---------- sway/main.c | 48 +- sway/old/border.c | 510 ++++++ sway/old/commands.c | 686 ++++++++ sway/old/commands/assign.c | 57 + sway/old/commands/bar.c | 59 + sway/old/commands/bar/activate_button.c | 26 + sway/old/commands/bar/binding_mode_indicator.c | 27 + sway/old/commands/bar/bindsym.c | 48 + sway/old/commands/bar/colors.c | 129 ++ sway/old/commands/bar/context_button.c | 26 + sway/old/commands/bar/font.c | 26 + sway/old/commands/bar/height.c | 21 + sway/old/commands/bar/hidden_state.c | 79 + sway/old/commands/bar/icon_theme.c | 25 + sway/old/commands/bar/id.c | 33 + sway/old/commands/bar/mode.c | 81 + sway/old/commands/bar/modifier.c | 35 + sway/old/commands/bar/output.c | 50 + sway/old/commands/bar/pango_markup.c | 27 + sway/old/commands/bar/position.c | 33 + sway/old/commands/bar/secondary_button.c | 26 + sway/old/commands/bar/separator_symbol.c | 21 + sway/old/commands/bar/status_command.c | 21 + sway/old/commands/bar/strip_workspace_numbers.c | 27 + sway/old/commands/bar/swaybar_command.c | 21 + sway/old/commands/bar/tray_output.c | 29 + sway/old/commands/bar/tray_padding.c | 34 + sway/old/commands/bar/workspace_buttons.c | 27 + sway/old/commands/bar/wrap_scroll.c | 27 + sway/old/commands/bind.c | 170 ++ sway/old/commands/border.c | 65 + sway/old/commands/client.c | 72 + sway/old/commands/clipboard.c | 38 + sway/old/commands/commands.c | 26 + sway/old/commands/debuglog.c | 27 + sway/old/commands/default_border.c | 44 + sway/old/commands/default_floating_border.c | 45 + sway/old/commands/exec.c | 16 + sway/old/commands/exec_always.c | 85 + sway/old/commands/exit.c | 17 + sway/old/commands/floating.c | 81 + sway/old/commands/floating_maximum_size.c | 38 + sway/old/commands/floating_minimum_size.c | 38 + sway/old/commands/floating_mod.c | 42 + sway/old/commands/floating_scroll.c | 46 + sway/old/commands/focus.c | 100 ++ sway/old/commands/focus_follows_mouse.c | 13 + sway/old/commands/font.c | 27 + sway/old/commands/for_window.c | 41 + sway/old/commands/force_focus_wrapping.c | 13 + sway/old/commands/fullscreen.c | 60 + sway/old/commands/gaps.c | 166 ++ sway/old/commands/hide_edge_borders.c | 27 + sway/old/commands/include.c | 15 + sway/old/commands/input.c | 55 + sway/old/commands/input/accel_profile.c | 27 + sway/old/commands/input/click_method.c | 30 + sway/old/commands/input/drag_lock.c | 26 + sway/old/commands/input/dwt.c | 26 + sway/old/commands/input/events.c | 30 + sway/old/commands/input/left_handed.c | 26 + sway/old/commands/input/middle_emulation.c | 26 + sway/old/commands/input/natural_scroll.c | 26 + sway/old/commands/input/pointer_accel.c | 24 + sway/old/commands/input/scroll_method.c | 30 + sway/old/commands/input/tap.c | 29 + sway/old/commands/ipc.c | 162 ++ sway/old/commands/kill.c | 12 + sway/old/commands/layout.c | 196 +++ sway/old/commands/log_colors.c | 22 + sway/old/commands/mark.c | 87 + sway/old/commands/mode.c | 57 + sway/old/commands/mouse_warping.c | 17 + sway/old/commands/move.c | 189 +++ sway/old/commands/new_float.c | 8 + sway/old/commands/new_window.c | 8 + sway/old/commands/no_focus.c | 41 + sway/old/commands/orientation.c | 21 + sway/old/commands/output.c | 219 +++ sway/old/commands/permit.c | 108 ++ sway/old/commands/reload.c | 19 + sway/old/commands/resize.c | 375 +++++ sway/old/commands/scratchpad.c | 72 + sway/old/commands/seamless_mouse.c | 13 + sway/old/commands/set.c | 61 + sway/old/commands/show_marks.c | 13 + sway/old/commands/smart_gaps.c | 20 + sway/old/commands/split.c | 98 ++ sway/old/commands/sticky.c | 25 + sway/old/commands/unmark.c | 31 + sway/old/commands/workspace.c | 92 ++ sway/old/commands/workspace_auto_back_and_forth.c | 18 + sway/old/commands/workspace_layout.c | 40 + sway/old/config.c | 1313 +++++++++++++++ sway/old/container.c | 1024 ++++++++++++ sway/old/criteria.c | 451 ++++++ sway/old/focus.c | 278 ++++ sway/old/handlers.c | 1143 +++++++++++++ sway/old/input_state.c | 490 ++++++ sway/old/ipc-json.c | 521 ++++++ sway/old/ipc-server.c | 840 ++++++++++ sway/old/layout.c | 1773 +++++++++++++++++++++ sway/old/output.c | 278 ++++ sway/old/security.c | 228 +++ sway/old/workspace.c | 373 +++++ sway/security.c | 228 --- sway/server.c | 2 - sway/tree/container.c | 1024 ------------ sway/tree/criteria.c | 451 ------ sway/tree/focus.c | 278 ---- sway/tree/layout.c | 1773 --------------------- sway/tree/output.c | 278 ---- sway/tree/workspace.c | 373 ----- 244 files changed, 16064 insertions(+), 16418 deletions(-) delete mode 100644 include/sway.h delete mode 100644 include/sway/border.h delete mode 100644 include/sway/commands.h delete mode 100644 include/sway/config.h delete mode 100644 include/sway/container.h delete mode 100644 include/sway/criteria.h delete mode 100644 include/sway/focus.h delete mode 100644 include/sway/handlers.h delete mode 100644 include/sway/input.h delete mode 100644 include/sway/input_state.h delete mode 100644 include/sway/ipc-json.h delete mode 100644 include/sway/ipc-server.h delete mode 100644 include/sway/layout.h create mode 100644 include/sway/old/border.h create mode 100644 include/sway/old/commands.h create mode 100644 include/sway/old/config.h create mode 100644 include/sway/old/container.h create mode 100644 include/sway/old/criteria.h create mode 100644 include/sway/old/focus.h create mode 100644 include/sway/old/input.h create mode 100644 include/sway/old/input_state.h create mode 100644 include/sway/old/ipc-json.h create mode 100644 include/sway/old/ipc-server.h create mode 100644 include/sway/old/layout.h create mode 100644 include/sway/old/output.h create mode 100644 include/sway/old/workspace.h delete mode 100644 include/sway/workspace.h delete mode 100644 sway/border.c delete mode 100644 sway/commands.c delete mode 100644 sway/commands/assign.c delete mode 100644 sway/commands/bar.c delete mode 100644 sway/commands/bar/activate_button.c delete mode 100644 sway/commands/bar/binding_mode_indicator.c delete mode 100644 sway/commands/bar/bindsym.c delete mode 100644 sway/commands/bar/colors.c delete mode 100644 sway/commands/bar/context_button.c delete mode 100644 sway/commands/bar/font.c delete mode 100644 sway/commands/bar/height.c delete mode 100644 sway/commands/bar/hidden_state.c delete mode 100644 sway/commands/bar/icon_theme.c delete mode 100644 sway/commands/bar/id.c delete mode 100644 sway/commands/bar/mode.c delete mode 100644 sway/commands/bar/modifier.c delete mode 100644 sway/commands/bar/output.c delete mode 100644 sway/commands/bar/pango_markup.c delete mode 100644 sway/commands/bar/position.c delete mode 100644 sway/commands/bar/secondary_button.c delete mode 100644 sway/commands/bar/separator_symbol.c delete mode 100644 sway/commands/bar/status_command.c delete mode 100644 sway/commands/bar/strip_workspace_numbers.c delete mode 100644 sway/commands/bar/swaybar_command.c delete mode 100644 sway/commands/bar/tray_output.c delete mode 100644 sway/commands/bar/tray_padding.c delete mode 100644 sway/commands/bar/workspace_buttons.c delete mode 100644 sway/commands/bar/wrap_scroll.c delete mode 100644 sway/commands/bind.c delete mode 100644 sway/commands/border.c delete mode 100644 sway/commands/client.c delete mode 100644 sway/commands/clipboard.c delete mode 100644 sway/commands/commands.c delete mode 100644 sway/commands/debuglog.c delete mode 100644 sway/commands/default_border.c delete mode 100644 sway/commands/default_floating_border.c delete mode 100644 sway/commands/exec.c delete mode 100644 sway/commands/exec_always.c delete mode 100644 sway/commands/exit.c delete mode 100644 sway/commands/floating.c delete mode 100644 sway/commands/floating_maximum_size.c delete mode 100644 sway/commands/floating_minimum_size.c delete mode 100644 sway/commands/floating_mod.c delete mode 100644 sway/commands/floating_scroll.c delete mode 100644 sway/commands/focus.c delete mode 100644 sway/commands/focus_follows_mouse.c delete mode 100644 sway/commands/font.c delete mode 100644 sway/commands/for_window.c delete mode 100644 sway/commands/force_focus_wrapping.c delete mode 100644 sway/commands/fullscreen.c delete mode 100644 sway/commands/gaps.c delete mode 100644 sway/commands/hide_edge_borders.c delete mode 100644 sway/commands/include.c delete mode 100644 sway/commands/input.c delete mode 100644 sway/commands/input/accel_profile.c delete mode 100644 sway/commands/input/click_method.c delete mode 100644 sway/commands/input/drag_lock.c delete mode 100644 sway/commands/input/dwt.c delete mode 100644 sway/commands/input/events.c delete mode 100644 sway/commands/input/left_handed.c delete mode 100644 sway/commands/input/middle_emulation.c delete mode 100644 sway/commands/input/natural_scroll.c delete mode 100644 sway/commands/input/pointer_accel.c delete mode 100644 sway/commands/input/scroll_method.c delete mode 100644 sway/commands/input/tap.c delete mode 100644 sway/commands/ipc.c delete mode 100644 sway/commands/kill.c delete mode 100644 sway/commands/layout.c delete mode 100644 sway/commands/log_colors.c delete mode 100644 sway/commands/mark.c delete mode 100644 sway/commands/mode.c delete mode 100644 sway/commands/mouse_warping.c delete mode 100644 sway/commands/move.c delete mode 100644 sway/commands/new_float.c delete mode 100644 sway/commands/new_window.c delete mode 100644 sway/commands/no_focus.c delete mode 100644 sway/commands/orientation.c delete mode 100644 sway/commands/output.c delete mode 100644 sway/commands/permit.c delete mode 100644 sway/commands/reload.c delete mode 100644 sway/commands/resize.c delete mode 100644 sway/commands/scratchpad.c delete mode 100644 sway/commands/seamless_mouse.c delete mode 100644 sway/commands/set.c delete mode 100644 sway/commands/show_marks.c delete mode 100644 sway/commands/smart_gaps.c delete mode 100644 sway/commands/split.c delete mode 100644 sway/commands/sticky.c delete mode 100644 sway/commands/unmark.c delete mode 100644 sway/commands/workspace.c delete mode 100644 sway/commands/workspace_auto_back_and_forth.c delete mode 100644 sway/commands/workspace_layout.c delete mode 100644 sway/config.c delete mode 100644 sway/debug_log.c delete mode 100644 sway/handlers.c delete mode 100644 sway/input_state.c delete mode 100644 sway/ipc-json.c delete mode 100644 sway/ipc-server.c create mode 100644 sway/old/border.c create mode 100644 sway/old/commands.c create mode 100644 sway/old/commands/assign.c create mode 100644 sway/old/commands/bar.c create mode 100644 sway/old/commands/bar/activate_button.c create mode 100644 sway/old/commands/bar/binding_mode_indicator.c create mode 100644 sway/old/commands/bar/bindsym.c create mode 100644 sway/old/commands/bar/colors.c create mode 100644 sway/old/commands/bar/context_button.c create mode 100644 sway/old/commands/bar/font.c create mode 100644 sway/old/commands/bar/height.c create mode 100644 sway/old/commands/bar/hidden_state.c create mode 100644 sway/old/commands/bar/icon_theme.c create mode 100644 sway/old/commands/bar/id.c create mode 100644 sway/old/commands/bar/mode.c create mode 100644 sway/old/commands/bar/modifier.c create mode 100644 sway/old/commands/bar/output.c create mode 100644 sway/old/commands/bar/pango_markup.c create mode 100644 sway/old/commands/bar/position.c create mode 100644 sway/old/commands/bar/secondary_button.c create mode 100644 sway/old/commands/bar/separator_symbol.c create mode 100644 sway/old/commands/bar/status_command.c create mode 100644 sway/old/commands/bar/strip_workspace_numbers.c create mode 100644 sway/old/commands/bar/swaybar_command.c create mode 100644 sway/old/commands/bar/tray_output.c create mode 100644 sway/old/commands/bar/tray_padding.c create mode 100644 sway/old/commands/bar/workspace_buttons.c create mode 100644 sway/old/commands/bar/wrap_scroll.c create mode 100644 sway/old/commands/bind.c create mode 100644 sway/old/commands/border.c create mode 100644 sway/old/commands/client.c create mode 100644 sway/old/commands/clipboard.c create mode 100644 sway/old/commands/commands.c create mode 100644 sway/old/commands/debuglog.c create mode 100644 sway/old/commands/default_border.c create mode 100644 sway/old/commands/default_floating_border.c create mode 100644 sway/old/commands/exec.c create mode 100644 sway/old/commands/exec_always.c create mode 100644 sway/old/commands/exit.c create mode 100644 sway/old/commands/floating.c create mode 100644 sway/old/commands/floating_maximum_size.c create mode 100644 sway/old/commands/floating_minimum_size.c create mode 100644 sway/old/commands/floating_mod.c create mode 100644 sway/old/commands/floating_scroll.c create mode 100644 sway/old/commands/focus.c create mode 100644 sway/old/commands/focus_follows_mouse.c create mode 100644 sway/old/commands/font.c create mode 100644 sway/old/commands/for_window.c create mode 100644 sway/old/commands/force_focus_wrapping.c create mode 100644 sway/old/commands/fullscreen.c create mode 100644 sway/old/commands/gaps.c create mode 100644 sway/old/commands/hide_edge_borders.c create mode 100644 sway/old/commands/include.c create mode 100644 sway/old/commands/input.c create mode 100644 sway/old/commands/input/accel_profile.c create mode 100644 sway/old/commands/input/click_method.c create mode 100644 sway/old/commands/input/drag_lock.c create mode 100644 sway/old/commands/input/dwt.c create mode 100644 sway/old/commands/input/events.c create mode 100644 sway/old/commands/input/left_handed.c create mode 100644 sway/old/commands/input/middle_emulation.c create mode 100644 sway/old/commands/input/natural_scroll.c create mode 100644 sway/old/commands/input/pointer_accel.c create mode 100644 sway/old/commands/input/scroll_method.c create mode 100644 sway/old/commands/input/tap.c create mode 100644 sway/old/commands/ipc.c create mode 100644 sway/old/commands/kill.c create mode 100644 sway/old/commands/layout.c create mode 100644 sway/old/commands/log_colors.c create mode 100644 sway/old/commands/mark.c create mode 100644 sway/old/commands/mode.c create mode 100644 sway/old/commands/mouse_warping.c create mode 100644 sway/old/commands/move.c create mode 100644 sway/old/commands/new_float.c create mode 100644 sway/old/commands/new_window.c create mode 100644 sway/old/commands/no_focus.c create mode 100644 sway/old/commands/orientation.c create mode 100644 sway/old/commands/output.c create mode 100644 sway/old/commands/permit.c create mode 100644 sway/old/commands/reload.c create mode 100644 sway/old/commands/resize.c create mode 100644 sway/old/commands/scratchpad.c create mode 100644 sway/old/commands/seamless_mouse.c create mode 100644 sway/old/commands/set.c create mode 100644 sway/old/commands/show_marks.c create mode 100644 sway/old/commands/smart_gaps.c create mode 100644 sway/old/commands/split.c create mode 100644 sway/old/commands/sticky.c create mode 100644 sway/old/commands/unmark.c create mode 100644 sway/old/commands/workspace.c create mode 100644 sway/old/commands/workspace_auto_back_and_forth.c create mode 100644 sway/old/commands/workspace_layout.c create mode 100644 sway/old/config.c create mode 100644 sway/old/container.c create mode 100644 sway/old/criteria.c create mode 100644 sway/old/focus.c create mode 100644 sway/old/handlers.c create mode 100644 sway/old/input_state.c create mode 100644 sway/old/ipc-json.c create mode 100644 sway/old/ipc-server.c create mode 100644 sway/old/layout.c create mode 100644 sway/old/output.c create mode 100644 sway/old/security.c create mode 100644 sway/old/workspace.c delete mode 100644 sway/security.c delete mode 100644 sway/tree/container.c delete mode 100644 sway/tree/criteria.c delete mode 100644 sway/tree/focus.c delete mode 100644 sway/tree/layout.c delete mode 100644 sway/tree/output.c delete mode 100644 sway/tree/workspace.c (limited to 'sway/commands') diff --git a/CMakeLists.txt b/CMakeLists.txt index 34098eb3..b53670b6 100644 --- 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 index 6dc9d743..c47b4eea 100644 --- a/common/log.c +++ b/common/log.c @@ -9,7 +9,6 @@ #include #include #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 deleted file mode 100644 index b5cfb668..00000000 --- a/include/sway.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _SWAY_SWAY_H -#define _SWAY_SWAY_H - -void sway_terminate(int exit_code); - -#endif diff --git a/include/sway/border.h b/include/sway/border.h deleted file mode 100644 index c30c9da3..00000000 --- a/include/sway/border.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _SWAY_BORDER_H -#define _SWAY_BORDER_H -#include -#include "container.h" - -/** - * Border pixel buffer and corresponding geometry. - */ -struct border { - unsigned char *buffer; - struct wlc_geometry geometry; -}; - -/** - * Clear border buffer. - */ -void border_clear(struct border *border); - -/** - * Recursively update all of the borders within a container. - */ -void update_container_border(swayc_t *container); - -void render_view_borders(wlc_handle view); -int get_font_text_height(const char *font); -bool should_hide_top_border(swayc_t *con, double y); - -#endif diff --git a/include/sway/commands.h b/include/sway/commands.h deleted file mode 100644 index 660da2c2..00000000 --- a/include/sway/commands.h +++ /dev/null @@ -1,214 +0,0 @@ -#ifndef _SWAY_COMMANDS_H -#define _SWAY_COMMANDS_H -#include -#include -#include -#include "config.h" - -// Container that a called command should act upon. Only valid in command functions. -extern swayc_t *current_container; - -/** - * Indicates the result of a command's execution. - */ -enum cmd_status { - CMD_SUCCESS, /**< The command was successful */ - CMD_FAILURE, /**< The command resulted in an error */ - CMD_INVALID, /**< Unknown command or parser error */ - CMD_DEFER, /**< Command execution deferred */ - // Config Blocks - CMD_BLOCK_END, - CMD_BLOCK_MODE, - CMD_BLOCK_BAR, - CMD_BLOCK_BAR_COLORS, - CMD_BLOCK_INPUT, - CMD_BLOCK_COMMANDS, - CMD_BLOCK_IPC, - CMD_BLOCK_IPC_EVENTS, -}; - -/** - * Stores the result of executing a command. - */ -struct cmd_results { - enum cmd_status status; - char *input; - /** - * Human friendly error message, or NULL on success - */ - char *error; -}; - -enum expected_args { - EXPECTED_MORE_THAN, - EXPECTED_AT_LEAST, - EXPECTED_LESS_THAN, - EXPECTED_EQUAL_TO -}; - -struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val); -struct cmd_results *add_color(const char*, char*, const char*); -void input_cmd_apply(struct input_config *input); -void hide_view_in_scratchpad(swayc_t *sp_view); - -swayc_t *sp_view; -int sp_index; - -/** - * Parse and handles a command. - */ -struct cmd_results *handle_command(char *command, enum command_context context); -/** - * Parse and handles a command during config file loading. - * - * Do not use this under normal conditions. - */ -struct cmd_results *config_command(char *command, enum cmd_status block); -/* - * Parses a command policy rule. - */ -struct cmd_results *config_commands_command(char *exec); - -/** - * Allocates a cmd_results object. - */ -struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *error, ...); -/** - * Frees a cmd_results object. - */ -void free_cmd_results(struct cmd_results *results); -/** - * Serializes cmd_results to a JSON string. - * - * Free the JSON string later on. - */ -const char *cmd_results_to_json(struct cmd_results *results); - -void remove_view_from_scratchpad(swayc_t *); - -/** - * Actual command function signatures for individual .c files in commands/ directory. - */ - -typedef struct cmd_results *sway_cmd(int argc, char **argv); - -sway_cmd cmd_assign; -sway_cmd cmd_bar; -sway_cmd cmd_bindcode; -sway_cmd cmd_bindsym; -sway_cmd cmd_border; -sway_cmd cmd_client_focused; -sway_cmd cmd_client_focused_inactive; -sway_cmd cmd_client_unfocused; -sway_cmd cmd_client_urgent; -sway_cmd cmd_client_placeholder; -sway_cmd cmd_client_background; -sway_cmd cmd_clipboard; -sway_cmd cmd_commands; -sway_cmd cmd_debuglog; -sway_cmd cmd_default_border; -sway_cmd cmd_default_floating_border; -sway_cmd cmd_exec; -sway_cmd cmd_exec_always; -sway_cmd cmd_exit; -sway_cmd cmd_floating; -sway_cmd cmd_floating_maximum_size; -sway_cmd cmd_floating_minimum_size; -sway_cmd cmd_floating_mod; -sway_cmd cmd_floating_scroll; -sway_cmd cmd_focus; -sway_cmd cmd_focus_follows_mouse; -sway_cmd cmd_font; -sway_cmd cmd_for_window; -sway_cmd cmd_force_focus_wrapping; -sway_cmd cmd_fullscreen; -sway_cmd cmd_gaps; -sway_cmd cmd_hide_edge_borders; -sway_cmd cmd_include; -sway_cmd cmd_input; -sway_cmd cmd_ipc; -sway_cmd cmd_kill; -sway_cmd cmd_layout; -sway_cmd cmd_log_colors; -sway_cmd cmd_mark; -sway_cmd cmd_mode; -sway_cmd cmd_mouse_warping; -sway_cmd cmd_move; -sway_cmd cmd_new_float; -sway_cmd cmd_new_window; -sway_cmd cmd_no_focus; -sway_cmd cmd_orientation; -sway_cmd cmd_output; -sway_cmd cmd_permit; -sway_cmd cmd_reject; -sway_cmd cmd_reload; -sway_cmd cmd_resize; -sway_cmd cmd_scratchpad; -sway_cmd cmd_seamless_mouse; -sway_cmd cmd_set; -sway_cmd cmd_show_marks; -sway_cmd cmd_smart_gaps; -sway_cmd cmd_split; -sway_cmd cmd_splith; -sway_cmd cmd_splitt; -sway_cmd cmd_splitv; -sway_cmd cmd_sticky; -sway_cmd cmd_unmark; -sway_cmd cmd_workspace; -sway_cmd cmd_ws_auto_back_and_forth; -sway_cmd cmd_workspace_layout; - -sway_cmd bar_cmd_activate_button; -sway_cmd bar_cmd_binding_mode_indicator; -sway_cmd bar_cmd_bindsym; -sway_cmd bar_cmd_colors; -sway_cmd bar_cmd_context_button; -sway_cmd bar_cmd_font; -sway_cmd bar_cmd_mode; -sway_cmd bar_cmd_modifier; -sway_cmd bar_cmd_output; -sway_cmd bar_cmd_height; -sway_cmd bar_cmd_hidden_state; -sway_cmd bar_cmd_icon_theme; -sway_cmd bar_cmd_id; -sway_cmd bar_cmd_position; -sway_cmd bar_cmd_secondary_button; -sway_cmd bar_cmd_separator_symbol; -sway_cmd bar_cmd_status_command; -sway_cmd bar_cmd_pango_markup; -sway_cmd bar_cmd_strip_workspace_numbers; -sway_cmd bar_cmd_swaybar_command; -sway_cmd bar_cmd_tray_output; -sway_cmd bar_cmd_tray_padding; -sway_cmd bar_cmd_wrap_scroll; -sway_cmd bar_cmd_workspace_buttons; - -sway_cmd bar_colors_cmd_active_workspace; -sway_cmd bar_colors_cmd_background; -sway_cmd bar_colors_cmd_focused_background; -sway_cmd bar_colors_cmd_binding_mode; -sway_cmd bar_colors_cmd_focused_workspace; -sway_cmd bar_colors_cmd_inactive_workspace; -sway_cmd bar_colors_cmd_separator; -sway_cmd bar_colors_cmd_focused_separator; -sway_cmd bar_colors_cmd_statusline; -sway_cmd bar_colors_cmd_focused_statusline; -sway_cmd bar_colors_cmd_urgent_workspace; - -sway_cmd input_cmd_accel_profile; -sway_cmd input_cmd_click_method; -sway_cmd input_cmd_drag_lock; -sway_cmd input_cmd_dwt; -sway_cmd input_cmd_events; -sway_cmd input_cmd_left_handed; -sway_cmd input_cmd_middle_emulation; -sway_cmd input_cmd_natural_scroll; -sway_cmd input_cmd_pointer_accel; -sway_cmd input_cmd_scroll_method; -sway_cmd input_cmd_tap; - -sway_cmd cmd_ipc_cmd; -sway_cmd cmd_ipc_events; -sway_cmd cmd_ipc_event_cmd; - -#endif diff --git a/include/sway/config.h b/include/sway/config.h deleted file mode 100644 index a05d5ede..00000000 --- a/include/sway/config.h +++ /dev/null @@ -1,406 +0,0 @@ -#ifndef _SWAY_CONFIG_H -#define _SWAY_CONFIG_H - -#define PID_WORKSPACE_TIMEOUT 60 - -#include -#include -#include -#include -#include -#include -#include "wayland-desktop-shell-server-protocol.h" -#include "list.h" -#include "layout.h" -#include "container.h" - -/** - * Describes a variable created via the `set` command. - */ -struct sway_variable { - char *name; - char *value; -}; - -/** - * A key binding and an associated command. - */ -struct sway_binding { - int order; - bool release; - bool bindcode; - list_t *keys; - uint32_t modifiers; - char *command; -}; - -/** - * A mouse binding and an associated command. - */ -struct sway_mouse_binding { - uint32_t button; - char *command; -}; - -/** - * A "mode" of keybindings created via the `mode` command. - */ -struct sway_mode { - char *name; - list_t *bindings; -}; - -/** - * libinput options for input devices - */ -struct input_config { - char *identifier; - - int accel_profile; - int click_method; - int drag_lock; - int dwt; - int left_handed; - int middle_emulation; - int natural_scroll; - float pointer_accel; - int scroll_method; - int send_events; - int tap; - - bool capturable; - struct wlc_geometry region; -}; - -/** - * Size and position configuration for a particular output. - * - * This is set via the `output` command. - */ -struct output_config { - char *name; - int enabled; - int width, height; - int x, y; - int scale; - char *background; - char *background_option; -}; - -/** - * Maps a workspace name to an output name. - * - * Set via `workspace output ` - */ -struct workspace_output { - char *output; - char *workspace; -}; - -struct pid_workspace { - pid_t *pid; - char *workspace; - time_t *time_added; -}; - -struct bar_config { - /** - * One of "dock", "hide", "invisible" - * - * Always visible in dock mode. Visible only when modifier key is held in hide mode. - * Never visible in invisible mode. - */ - char *mode; - /** - * One of "show" or "hide". - * - * In "show" mode, it will always be shown on top of the active workspace. - */ - char *hidden_state; - /** - * Id name used to identify the bar through IPC. - * - * Defaults to bar-x, where x corresponds to the position of the - * embedding bar block in the config file (bar-0, bar-1, ...). - */ - char *id; - uint32_t modifier; - list_t *outputs; - enum desktop_shell_panel_position position; - list_t *bindings; - char *status_command; - bool pango_markup; - char *swaybar_command; - char *font; - int height; // -1 not defined - -#ifdef ENABLE_TRAY - // Tray - char *tray_output; - char *icon_theme; - uint32_t tray_padding; - uint32_t activate_button; - uint32_t context_button; - uint32_t secondary_button; -#endif - - bool workspace_buttons; - bool wrap_scroll; - char *separator_symbol; - bool strip_workspace_numbers; - bool binding_mode_indicator; - bool verbose; - pid_t pid; - struct { - char *background; - char *statusline; - char *separator; - char *focused_background; - char *focused_statusline; - char *focused_separator; - char *focused_workspace_border; - char *focused_workspace_bg; - char *focused_workspace_text; - char *active_workspace_border; - char *active_workspace_bg; - char *active_workspace_text; - char *inactive_workspace_border; - char *inactive_workspace_bg; - char *inactive_workspace_text; - char *urgent_workspace_border; - char *urgent_workspace_bg; - char *urgent_workspace_text; - char *binding_mode_border; - char *binding_mode_bg; - char *binding_mode_text; - } colors; -}; - -struct border_colors { - uint32_t border; - uint32_t background; - uint32_t text; - uint32_t indicator; - uint32_t child_border; -}; - -enum edge_border_types { - E_NONE, /**< Don't hide edge borders */ - E_VERTICAL, /**< hide vertical edge borders */ - E_HORIZONTAL, /**< hide horizontal edge borders */ - E_BOTH, /**< hide vertical and horizontal edge borders */ - E_SMART /**< hide both if precisely one window is present in workspace */ -}; - -enum command_context { - CONTEXT_CONFIG = 1, - CONTEXT_BINDING = 2, - CONTEXT_IPC = 4, - CONTEXT_CRITERIA = 8, - CONTEXT_ALL = 0xFFFFFFFF, -}; - -struct command_policy { - char *command; - uint32_t context; -}; - -enum secure_feature { - FEATURE_LOCK = 1, - FEATURE_PANEL = 2, - FEATURE_BACKGROUND = 4, - FEATURE_SCREENSHOT = 8, - FEATURE_FULLSCREEN = 16, - FEATURE_KEYBOARD = 32, - FEATURE_MOUSE = 64, -}; - -struct feature_policy { - char *program; - uint32_t features; -}; - -enum ipc_feature { - IPC_FEATURE_COMMAND = 1, - IPC_FEATURE_GET_WORKSPACES = 2, - IPC_FEATURE_GET_OUTPUTS = 4, - IPC_FEATURE_GET_TREE = 8, - IPC_FEATURE_GET_MARKS = 16, - IPC_FEATURE_GET_BAR_CONFIG = 32, - IPC_FEATURE_GET_VERSION = 64, - IPC_FEATURE_GET_INPUTS = 128, - IPC_FEATURE_EVENT_WORKSPACE = 256, - IPC_FEATURE_EVENT_OUTPUT = 512, - IPC_FEATURE_EVENT_MODE = 1024, - IPC_FEATURE_EVENT_WINDOW = 2048, - IPC_FEATURE_EVENT_BINDING = 4096, - IPC_FEATURE_EVENT_INPUT = 8192, - IPC_FEATURE_GET_CLIPBOARD = 16384, - - IPC_FEATURE_ALL_COMMANDS = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 16384, - IPC_FEATURE_ALL_EVENTS = 256 | 512 | 1024 | 2048 | 4096 | 8192, - - IPC_FEATURE_ALL = IPC_FEATURE_ALL_COMMANDS | IPC_FEATURE_ALL_EVENTS, -}; - -struct ipc_policy { - char *program; - uint32_t features; -}; - -/** - * The configuration struct. The result of loading a config file. - */ -struct sway_config { - list_t *symbols; - list_t *modes; - list_t *bars; - list_t *cmd_queue; - list_t *workspace_outputs; - list_t *pid_workspaces; - list_t *output_configs; - list_t *input_configs; - list_t *criteria; - list_t *no_focus; - list_t *active_bar_modifiers; - struct sway_mode *current_mode; - struct bar_config *current_bar; - uint32_t floating_mod; - uint32_t dragging_key; - uint32_t resizing_key; - char *floating_scroll_up_cmd; - char *floating_scroll_down_cmd; - char *floating_scroll_left_cmd; - char *floating_scroll_right_cmd; - enum swayc_layouts default_orientation; - enum swayc_layouts default_layout; - char *font; - int font_height; - - // Flags - bool focus_follows_mouse; - bool mouse_warping; - bool force_focus_wrapping; - bool active; - bool failed; - bool reloading; - bool reading; - bool auto_back_and_forth; - bool seamless_mouse; - bool show_marks; - - bool edge_gaps; - bool smart_gaps; - int gaps_inner; - int gaps_outer; - - list_t *config_chain; - const char *current_config; - - enum swayc_border_types border; - enum swayc_border_types floating_border; - int border_thickness; - int floating_border_thickness; - enum edge_border_types hide_edge_borders; - - // border colors - struct { - struct border_colors focused; - struct border_colors focused_inactive; - struct border_colors unfocused; - struct border_colors urgent; - struct border_colors placeholder; - uint32_t background; - } border_colors; - - // floating view - int32_t floating_maximum_width; - int32_t floating_maximum_height; - int32_t floating_minimum_width; - int32_t floating_minimum_height; - - // Security - list_t *command_policies; - list_t *feature_policies; - list_t *ipc_policies; -}; - -void pid_workspace_add(struct pid_workspace *pw); -void free_pid_workspace(struct pid_workspace *pw); - -/** - * Loads the main config from the given path. is_active should be true when - * reloading the config. - */ -bool load_main_config(const char *path, bool is_active); - -/** - * Loads an included config. Can only be used after load_main_config. - */ -bool load_include_configs(const char *path, struct sway_config *config); - -/** - * Reads the config from the given FILE. - */ -bool read_config(FILE *file, struct sway_config *config); - -/** - * Free config struct - */ -void free_config(struct sway_config *config); -/** - * Does variable replacement for a string based on the config's currently loaded variables. - */ -char *do_var_replacement(char *str); - -struct cmd_results *check_security_config(); - -int input_identifier_cmp(const void *item, const void *data); -void merge_input_config(struct input_config *dst, struct input_config *src); -void apply_input_config(struct input_config *ic, struct libinput_device *dev); -void free_input_config(struct input_config *ic); - -int output_name_cmp(const void *item, const void *data); -void merge_output_config(struct output_config *dst, struct output_config *src); -/** Sets up a WLC output handle based on a given output_config. - */ -void apply_output_config(struct output_config *oc, swayc_t *output); -void free_output_config(struct output_config *oc); - -/** - * Updates the list of active bar modifiers - */ -void update_active_bar_modifiers(void); - -int workspace_output_cmp_workspace(const void *a, const void *b); - -int sway_binding_cmp(const void *a, const void *b); -int sway_binding_cmp_qsort(const void *a, const void *b); -int sway_binding_cmp_keys(const void *a, const void *b); -void free_sway_binding(struct sway_binding *sb); -struct sway_binding *sway_binding_dup(struct sway_binding *sb); - -int sway_mouse_binding_cmp(const void *a, const void *b); -int sway_mouse_binding_cmp_qsort(const void *a, const void *b); -int sway_mouse_binding_cmp_buttons(const void *a, const void *b); -void free_sway_mouse_binding(struct sway_mouse_binding *smb); - -void load_swaybars(); -void terminate_swaybg(pid_t pid); - -/** - * Allocate and initialize default bar configuration. - */ -struct bar_config *default_bar_config(void); - -/** - * Global config singleton. - */ -extern struct sway_config *config; - -/** - * Config file currently being read. - */ -extern const char *current_config_path; - -#endif diff --git a/include/sway/container.h b/include/sway/container.h deleted file mode 100644 index d46ffa63..00000000 --- a/include/sway/container.h +++ /dev/null @@ -1,362 +0,0 @@ -#ifndef _SWAY_CONTAINER_H -#define _SWAY_CONTAINER_H -#include -#include -#include -#include -#include "list.h" - -typedef struct sway_container swayc_t; - -extern swayc_t root_container; -extern swayc_t *current_focus; - -struct sway_view; - -/** - * Different kinds of containers. - * - * This enum is in order. A container will never be inside of a container below - * it on this list. - */ -enum swayc_types { - C_ROOT, /**< The root container. Only one of these ever exists. */ - C_OUTPUT, /**< An output (aka monitor, head, etc). */ - C_WORKSPACE, /**< A workspace. */ - C_CONTAINER, /**< A manually created container. */ - C_VIEW, /**< A view (aka window). */ - // Keep last - C_TYPES, -}; - -/** - * Different ways to arrange a container. - */ -enum swayc_layouts { - L_NONE, /**< Used for containers that have no layout (views, root) */ - L_HORIZ, - L_VERT, - L_STACKED, - L_TABBED, - L_FLOATING, /**< A psuedo-container, removed from the tree, to hold floating windows */ - - /* Awesome/Monad style auto layouts */ - L_AUTO_LEFT, - L_AUTO_RIGHT, - L_AUTO_TOP, - L_AUTO_BOTTOM, - - L_AUTO_FIRST = L_AUTO_LEFT, - L_AUTO_LAST = L_AUTO_BOTTOM, - - // Keep last - L_LAYOUTS, -}; - -enum swayc_border_types { - B_NONE, /**< No border */ - B_PIXEL, /**< 1px border */ - B_NORMAL /**< Normal border with title bar */ -}; - -/** - * Stores information about a container. - * - * The tree is made of these. Views are containers that cannot have children. - */ -struct sway_container { - // TODO WLR: reconcile these - wlc_handle handle; - - union { - struct sway_output *output; - struct sway_view *view; - } _handle; - - /** - * A unique ID to identify this container. Primarily used in the - * get_tree JSON output. - */ - size_t id; - - enum swayc_types type; - enum swayc_layouts layout; - enum swayc_layouts prev_layout; - enum swayc_layouts workspace_layout; - - /** - * Width and height of this container, without borders or gaps. - */ - double width, height; - - /** - * Views may request geometry, which is stored in this and ignored until - * the views are floated. - */ - int desired_width, desired_height; - - /** - * The coordinates that this view appear at, relative to the output they - * are located on (output containers have absolute coordinates). - */ - double x, y; - - /** - * Cached geometry used to store view/container geometry when switching - * between tabbed/stacked and horizontal/vertical layouts. - */ - struct wlc_geometry cached_geometry; - - /** - * False if this view is invisible. It could be in the scratchpad or on a - * workspace that is not shown. - */ - bool visible; - bool is_floating; - bool is_focused; - bool sticky; // floating view always visible on its output - - // Attributes that mostly views have. - char *name; - char *class; - char *instance; - char *app_id; - - // Used by output containers to keep track of swaybg child processes. - pid_t bg_pid; - - int gaps; - - list_t *children; - /** - * Children of this container that are floated. - */ - list_t *floating; - /** - * Unmanaged view handles in this container. - */ - list_t *unmanaged; - - /** - * The parent of this container. NULL for the root container. - */ - struct sway_container *parent; - /** - * Which of this container's children has focus. - */ - struct sway_container *focused; - /** - * If this container's children include a fullscreen view, this is that view. - */ - struct sway_container *fullscreen; - /** - * If this container is a view, this may be set to the window's decoration - * buffer (or NULL). - */ - struct border *border; - enum swayc_border_types border_type; - struct wlc_geometry border_geometry; - struct wlc_geometry title_bar_geometry; - struct wlc_geometry actual_geometry; - int border_thickness; - - /** - * Number of master views in auto layouts. - */ - size_t nb_master; - - /** - * Number of slave groups (e.g. columns) in auto layouts. - */ - size_t nb_slave_groups; - - /** - * Marks applied to the container, list_t of char*. - */ - list_t *marks; -}; - -enum visibility_mask { - VISIBLE = true -} visible; - -struct sway_output; -/** - * Allocates a new output container. - */ -swayc_t *new_output(struct sway_output *sway_output); -/** - * Allocates a new workspace container. - */ -swayc_t *new_workspace(swayc_t *output, const char *name); -/** - * Allocates a new container and places a child into it. - * - * This is used from the split command, which creates a new container with the - * requested layout and replaces the focused container in the tree with the new - * one. Then the removed container is added as a child of the new container. - */ -swayc_t *new_container(swayc_t *child, enum swayc_layouts layout); -/** - * Allocates a new view container. - * - * Pass in a sibling view, or a workspace to become this container's parent. - */ -swayc_t *new_view(swayc_t *sibling, struct sway_view *view); -/** - * Allocates a new floating view in the active workspace. - */ -swayc_t *new_floating_view(wlc_handle handle); - -void floating_view_sane_size(swayc_t *view); - -/** - * Frees an output's container. - */ -swayc_t *destroy_output(swayc_t *output); -/** - * Destroys a workspace container and returns the parent pointer, or NULL. - */ -swayc_t *destroy_workspace(swayc_t *workspace); -/** - * Destroys a container and all empty parents. Returns the topmost non-empty - * parent container, or NULL. - */ -swayc_t *destroy_container(swayc_t *container); -/** - * Destroys a view container and all empty parents. Returns the topmost - * non-empty parent container, or NULL. - */ -swayc_t *destroy_view(swayc_t *view); - -/** - * Finds a container based on test criteria. Returns the first container that - * passes the test. - */ -swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); -/** - * Finds a parent container with the given swayc_type. - */ -swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types); -/** - * Finds a parent with the given swayc_layout. - */ -swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts); -/** - * Finds the bottom-most focused container of a type. - */ -swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types); -/** - * Finds the bottom-most focused container of a layout. - */ -swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts); - -/** - * Gets the swayc_t associated with a wlc_handle. - */ -swayc_t *swayc_by_handle(wlc_handle handle); -/** - * Gets the named swayc_t. - */ -swayc_t *swayc_by_name(const char *name); -/** - * Gets the active output's container. - */ -swayc_t *swayc_active_output(void); -/** - * Gets the active workspace's container. - */ -swayc_t *swayc_active_workspace(void); -/** - * Gets the workspace for the given view container. - */ -swayc_t *swayc_active_workspace_for(swayc_t *view); -/** - * Finds the container currently underneath the pointer. - */ -swayc_t *container_under_pointer(void); -/** - * Finds the first container following a callback. - */ -swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), const void *data); - -/** - * Returns true if a container is fullscreen. - */ -bool swayc_is_fullscreen(swayc_t *view); -/** - * Returns true if this view is focused. - */ -bool swayc_is_active(swayc_t *view); -/** - * Returns true if the parent is an ancestor of the child. - */ -bool swayc_is_parent_of(swayc_t *parent, swayc_t *child); -/** - * Returns true if the child is a desecendant of the parent. - */ -bool swayc_is_child_of(swayc_t *child, swayc_t *parent); - -/** - * Returns true if this container is an empty workspace. - */ -bool swayc_is_empty_workspace(swayc_t *container); - -/** - * Returns the top most tabbed or stacked parent container. Returns NULL if - * view is not in a tabbed/stacked layout. - */ -swayc_t *swayc_tabbed_stacked_ancestor(swayc_t *view); - -/** - * Returns the immediate tabbed or stacked parent container. Returns NULL if - * view is not directly in a tabbed/stacked layout. - */ -swayc_t *swayc_tabbed_stacked_parent(swayc_t *view); - -/** - * Returns the gap (padding) of the container. - * - * This returns the inner gaps for a view, the outer gaps for a workspace, and - * 0 otherwise. - */ -int swayc_gap(swayc_t *container); - -/** - * Maps a container's children over a function. - */ -void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *); - -/** - * Set a view as visible or invisible. - * - * This will perform the required wlc calls as well; it is not sufficient to - * simply toggle the boolean in swayc_t. - */ -void set_view_visibility(swayc_t *view, void *data); -/** - * Set the gaps value for a view. - */ -void set_gaps(swayc_t *view, void *amount); -/** - * Add to the gaps value for a view. - */ -void add_gaps(swayc_t *view, void *amount); - -/** - * Issue wlc calls to make the visibility of a container consistent. - */ -void update_visibility(swayc_t *container); - -/** - * Close all child views of container - */ -void close_views(swayc_t *container); - -/** - * Assign layout to a container. Needed due to workspace container specifics. - * Workspace should always have either L_VERT or L_HORIZ layout. - */ -swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); - -#endif diff --git a/include/sway/criteria.h b/include/sway/criteria.h deleted file mode 100644 index c5ed9857..00000000 --- a/include/sway/criteria.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef _SWAY_CRITERIA_H -#define _SWAY_CRITERIA_H - -#include "container.h" -#include "list.h" - -/** - * Maps criteria (as a list of criteria tokens) to a command list. - * - * A list of tokens together represent a single criteria string (e.g. - * '[class="abc" title="xyz"]' becomes two criteria tokens). - * - * for_window: Views matching all criteria will have the bound command list - * executed on them. - * - * Set via `for_window `. - */ -struct criteria { - list_t *tokens; // struct crit_token, contains compiled regex. - char *crit_raw; // entire criteria string (for logging) - - char *cmdlist; -}; - -int criteria_cmp(const void *item, const void *data); -void free_criteria(struct criteria *crit); - -// Pouplate list with crit_tokens extracted from criteria string, returns error -// string or NULL if successful. -char *extract_crit_tokens(list_t *tokens, const char *criteria); - -// Returns list of criteria that match given container. These criteria have -// been set with `for_window` commands and have an associated cmdlist. -list_t *criteria_for(swayc_t *cont); - -// Returns a list of all containers that match the given list of tokens. -list_t *container_for(list_t *tokens); - -// Returns true if any criteria in the given list matches this container -bool criteria_any(swayc_t *cont, list_t *criteria); - -#endif diff --git a/include/sway/focus.h b/include/sway/focus.h deleted file mode 100644 index 652cdccc..00000000 --- a/include/sway/focus.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef _SWAY_FOCUS_H -#define _SWAY_FOCUS_H -enum movement_direction { - MOVE_LEFT, - MOVE_RIGHT, - MOVE_UP, - MOVE_DOWN, - MOVE_PARENT, - MOVE_CHILD, - MOVE_NEXT, - MOVE_PREV, - MOVE_FIRST -}; - -#include "container.h" - -// focused_container - the container found by following the `focused` pointer -// from a given container to a container with `is_focused` boolean set -// --- -// focused_view - the container found by following the `focused` pointer from a -// given container to a view. -// --- - -swayc_t *get_focused_container(swayc_t *parent); -swayc_t *get_focused_view(swayc_t *parent); -swayc_t *get_focused_float(swayc_t *ws); - -// a special-case function to get the focused view, regardless -// of whether it's tiled or floating -swayc_t *get_focused_view_include_floating(swayc_t *parent); - -bool set_focused_container(swayc_t *container); -bool set_focused_container_for(swayc_t *ancestor, swayc_t *container); - -// lock focused container/view. locked by windows with OVERRIDE attribute -// and unlocked when they are destroyed - -extern bool locked_container_focus; - -// Prevents wss from being destroyed on focus switch -extern bool suspend_workspace_cleanup; - -bool move_focus(enum movement_direction direction); - -#endif diff --git a/include/sway/handlers.h b/include/sway/handlers.h deleted file mode 100644 index 956b98f4..00000000 --- a/include/sway/handlers.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _SWAY_HANDLERS_H -#define _SWAY_HANDLERS_H -#include "container.h" -#include -#include - -void register_wlc_handlers(); - -extern uint32_t keys_pressed[32]; - -#endif diff --git a/include/sway/input.h b/include/sway/input.h deleted file mode 100644 index eb92e470..00000000 --- a/include/sway/input.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _SWAY_INPUT_H -#define _SWAY_INPUT_H -#include -#include "sway/server.h" -#include "config.h" -#include "list.h" - -struct sway_input { - list_t *input_devices; -}; - -struct input_config *new_input_config(const char* identifier); - -char* libinput_dev_unique_id(struct libinput_device *dev); - -struct sway_input *sway_input_create(struct sway_server *server); - -/** - * Pointer used when reading input blocked. - * Shared so that it can be cleared from commands.c when closing the block - */ -extern struct input_config *current_input_config; - -#endif diff --git a/include/sway/input_state.h b/include/sway/input_state.h deleted file mode 100644 index fd5a3a25..00000000 --- a/include/sway/input_state.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef _SWAY_KEY_STATE_H -#define _SWAY_KEY_STATE_H -#include -#include -#include "container.h" - -/* Keyboard state */ - -// returns true if key has been pressed, otherwise false -bool check_key(uint32_t key_sym, uint32_t key_code); - -// returns true if key_sym matches latest released key. -bool check_released_key(uint32_t key_sym); - -// sets a key as pressed -void press_key(uint32_t key_sym, uint32_t key_code); - -// unsets a key as pressed -void release_key(uint32_t key_sym, uint32_t key_code); - - -/* Pointer state */ - -enum pointer_values { - M_LEFT_CLICK = 272, - M_RIGHT_CLICK = 273, - M_SCROLL_CLICK = 274, - M_SCROLL_UP = 275, - M_SCROLL_DOWN = 276, -}; - -enum pointer_mode { - // Target - M_FLOATING = 1, - M_TILING = 2, - // Action - M_DRAGGING = 4, - M_RESIZING = 8, -}; - -struct pointer_button_state { - bool held; - // state at the point it was pressed - int x, y; - swayc_t *view; -}; - -extern struct pointer_state { - // mouse clicks - struct pointer_button_state left; - struct pointer_button_state right; - struct pointer_button_state scroll; - - // change in pointer position - struct { - int x, y; - } delta; - - // view pointer is currently over - swayc_t *view; - - // Pointer mode - int mode; -} pointer_state; - -enum modifier_state { - MOD_STATE_UNCHANGED = 0, - MOD_STATE_PRESSED = 1, - MOD_STATE_RELEASED = 2 -}; - -void pointer_position_set(double new_x, double new_y, bool force_focus); -void center_pointer_on(swayc_t *view); - -// on button release unset mode depending on the button. -// on button press set mode conditionally depending on the button -void pointer_mode_set(uint32_t button, bool condition); - -// Update mode in mouse motion -void pointer_mode_update(void); - -// Reset mode on any keypress; -void pointer_mode_reset(void); - -void input_init(void); - -/** - * Check if state of mod changed from current state to new_state. - * - * Returns MOD_STATE_UNCHANGED if the state didn't change, MOD_STATE_PRESSED if - * the state changed to pressed and MOD_STATE_RELEASED if the state changed to - * released. - */ -uint32_t modifier_state_changed(uint32_t new_state, uint32_t mod); - -/** - * Update the current modifiers state to new_state. - */ -void modifiers_state_update(uint32_t new_state); - -#endif - diff --git a/include/sway/ipc-json.h b/include/sway/ipc-json.h deleted file mode 100644 index 3a5af0f5..00000000 --- a/include/sway/ipc-json.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef _SWAY_IPC_JSON_H -#define _SWAY_IPC_JSON_H - -#include -#include "config.h" -#include "container.h" - -json_object *ipc_json_get_version(); -json_object *ipc_json_describe_bar_config(struct bar_config *bar); -json_object *ipc_json_describe_container(swayc_t *c); -json_object *ipc_json_describe_container_recursive(swayc_t *c); -json_object *ipc_json_describe_window(swayc_t *c); -json_object *ipc_json_describe_input(struct libinput_device *device); - -#endif diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h deleted file mode 100644 index 1d199134..00000000 --- a/include/sway/ipc-server.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef _SWAY_IPC_SERVER_H -#define _SWAY_IPC_SERVER_H - -#include - -#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/layout.h b/include/sway/layout.h deleted file mode 100644 index f0791588..00000000 --- a/include/sway/layout.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef _SWAY_LAYOUT_H -#define _SWAY_LAYOUT_H - -#include -#include "log.h" -#include "list.h" -#include "container.h" -#include "focus.h" - -extern list_t *scratchpad; - -extern int min_sane_w; -extern int min_sane_h; - -// Set initial values for root_container -void init_layout(void); - -// Returns the index of child for its parent -int index_child(const swayc_t *child); - -// Adds child to parent, if parent has no focus, it is set to child -// parent must be of type C_WORKSPACE or C_CONTAINER -void add_child(swayc_t *parent, swayc_t *child); - -// Adds child to parent at index, if parent has no focus, it is set to child -// parent must be of type C_WORKSPACE or C_CONTAINER -void insert_child(swayc_t *parent, swayc_t *child, int index); - -// Adds child as floating window to ws, if there is no focus it is set to child. -// ws must be of type C_WORKSPACE -void add_floating(swayc_t *ws, swayc_t *child); - -// insert child after sibling in parents children. -swayc_t *add_sibling(swayc_t *sibling, swayc_t *child); - -// Replace child with new_child in parents children -// new_child will inherit childs geometry, childs geometry will be reset -// if parents focus is on child, it will be changed to new_child -swayc_t *replace_child(swayc_t *child, swayc_t *new_child); - -// Remove child from its parent, if focus is on child, focus will be changed to -// a sibling, or to a floating window, or NULL -swayc_t *remove_child(swayc_t *child); - -// 2 containers are swapped, they inherit eachothers focus -void swap_container(swayc_t *a, swayc_t *b); - -// 2 Containers geometry are swapped, used with `swap_container` -void swap_geometry(swayc_t *a, swayc_t *b); - -void move_container(swayc_t* container, enum movement_direction direction, int move_amt); -void move_container_to(swayc_t* container, swayc_t* destination); -void move_workspace_to(swayc_t* workspace, swayc_t* destination); - -// Layout -/** - * Update child container geometries when switching between layouts. - */ -void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout); -void update_geometry(swayc_t *view); -void arrange_windows(swayc_t *container, double width, double height); -void arrange_backgrounds(void); - -swayc_t *get_focused_container(swayc_t *parent); -swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir); -swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_direction dir, swayc_t *limit); - -void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge); - -void layout_log(const swayc_t *c, int depth); -void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) __attribute__((format(printf,3,4))); - -/** - * Get default layout. - */ -enum swayc_layouts default_layout(swayc_t *output); - -bool is_auto_layout(enum swayc_layouts layout); -int auto_group_start_index(const swayc_t *container, int index); -int auto_group_end_index(const swayc_t *container, int index); -size_t auto_group_count(const swayc_t *container); -size_t auto_group_index(const swayc_t *container, int index); -bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end); - -#endif diff --git a/include/sway/old/border.h b/include/sway/old/border.h new file mode 100644 index 00000000..c30c9da3 --- /dev/null +++ b/include/sway/old/border.h @@ -0,0 +1,28 @@ +#ifndef _SWAY_BORDER_H +#define _SWAY_BORDER_H +#include +#include "container.h" + +/** + * Border pixel buffer and corresponding geometry. + */ +struct border { + unsigned char *buffer; + struct wlc_geometry geometry; +}; + +/** + * Clear border buffer. + */ +void border_clear(struct border *border); + +/** + * Recursively update all of the borders within a container. + */ +void update_container_border(swayc_t *container); + +void render_view_borders(wlc_handle view); +int get_font_text_height(const char *font); +bool should_hide_top_border(swayc_t *con, double y); + +#endif diff --git a/include/sway/old/commands.h b/include/sway/old/commands.h new file mode 100644 index 00000000..660da2c2 --- /dev/null +++ b/include/sway/old/commands.h @@ -0,0 +1,214 @@ +#ifndef _SWAY_COMMANDS_H +#define _SWAY_COMMANDS_H +#include +#include +#include +#include "config.h" + +// Container that a called command should act upon. Only valid in command functions. +extern swayc_t *current_container; + +/** + * Indicates the result of a command's execution. + */ +enum cmd_status { + CMD_SUCCESS, /**< The command was successful */ + CMD_FAILURE, /**< The command resulted in an error */ + CMD_INVALID, /**< Unknown command or parser error */ + CMD_DEFER, /**< Command execution deferred */ + // Config Blocks + CMD_BLOCK_END, + CMD_BLOCK_MODE, + CMD_BLOCK_BAR, + CMD_BLOCK_BAR_COLORS, + CMD_BLOCK_INPUT, + CMD_BLOCK_COMMANDS, + CMD_BLOCK_IPC, + CMD_BLOCK_IPC_EVENTS, +}; + +/** + * Stores the result of executing a command. + */ +struct cmd_results { + enum cmd_status status; + char *input; + /** + * Human friendly error message, or NULL on success + */ + char *error; +}; + +enum expected_args { + EXPECTED_MORE_THAN, + EXPECTED_AT_LEAST, + EXPECTED_LESS_THAN, + EXPECTED_EQUAL_TO +}; + +struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val); +struct cmd_results *add_color(const char*, char*, const char*); +void input_cmd_apply(struct input_config *input); +void hide_view_in_scratchpad(swayc_t *sp_view); + +swayc_t *sp_view; +int sp_index; + +/** + * Parse and handles a command. + */ +struct cmd_results *handle_command(char *command, enum command_context context); +/** + * Parse and handles a command during config file loading. + * + * Do not use this under normal conditions. + */ +struct cmd_results *config_command(char *command, enum cmd_status block); +/* + * Parses a command policy rule. + */ +struct cmd_results *config_commands_command(char *exec); + +/** + * Allocates a cmd_results object. + */ +struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *error, ...); +/** + * Frees a cmd_results object. + */ +void free_cmd_results(struct cmd_results *results); +/** + * Serializes cmd_results to a JSON string. + * + * Free the JSON string later on. + */ +const char *cmd_results_to_json(struct cmd_results *results); + +void remove_view_from_scratchpad(swayc_t *); + +/** + * Actual command function signatures for individual .c files in commands/ directory. + */ + +typedef struct cmd_results *sway_cmd(int argc, char **argv); + +sway_cmd cmd_assign; +sway_cmd cmd_bar; +sway_cmd cmd_bindcode; +sway_cmd cmd_bindsym; +sway_cmd cmd_border; +sway_cmd cmd_client_focused; +sway_cmd cmd_client_focused_inactive; +sway_cmd cmd_client_unfocused; +sway_cmd cmd_client_urgent; +sway_cmd cmd_client_placeholder; +sway_cmd cmd_client_background; +sway_cmd cmd_clipboard; +sway_cmd cmd_commands; +sway_cmd cmd_debuglog; +sway_cmd cmd_default_border; +sway_cmd cmd_default_floating_border; +sway_cmd cmd_exec; +sway_cmd cmd_exec_always; +sway_cmd cmd_exit; +sway_cmd cmd_floating; +sway_cmd cmd_floating_maximum_size; +sway_cmd cmd_floating_minimum_size; +sway_cmd cmd_floating_mod; +sway_cmd cmd_floating_scroll; +sway_cmd cmd_focus; +sway_cmd cmd_focus_follows_mouse; +sway_cmd cmd_font; +sway_cmd cmd_for_window; +sway_cmd cmd_force_focus_wrapping; +sway_cmd cmd_fullscreen; +sway_cmd cmd_gaps; +sway_cmd cmd_hide_edge_borders; +sway_cmd cmd_include; +sway_cmd cmd_input; +sway_cmd cmd_ipc; +sway_cmd cmd_kill; +sway_cmd cmd_layout; +sway_cmd cmd_log_colors; +sway_cmd cmd_mark; +sway_cmd cmd_mode; +sway_cmd cmd_mouse_warping; +sway_cmd cmd_move; +sway_cmd cmd_new_float; +sway_cmd cmd_new_window; +sway_cmd cmd_no_focus; +sway_cmd cmd_orientation; +sway_cmd cmd_output; +sway_cmd cmd_permit; +sway_cmd cmd_reject; +sway_cmd cmd_reload; +sway_cmd cmd_resize; +sway_cmd cmd_scratchpad; +sway_cmd cmd_seamless_mouse; +sway_cmd cmd_set; +sway_cmd cmd_show_marks; +sway_cmd cmd_smart_gaps; +sway_cmd cmd_split; +sway_cmd cmd_splith; +sway_cmd cmd_splitt; +sway_cmd cmd_splitv; +sway_cmd cmd_sticky; +sway_cmd cmd_unmark; +sway_cmd cmd_workspace; +sway_cmd cmd_ws_auto_back_and_forth; +sway_cmd cmd_workspace_layout; + +sway_cmd bar_cmd_activate_button; +sway_cmd bar_cmd_binding_mode_indicator; +sway_cmd bar_cmd_bindsym; +sway_cmd bar_cmd_colors; +sway_cmd bar_cmd_context_button; +sway_cmd bar_cmd_font; +sway_cmd bar_cmd_mode; +sway_cmd bar_cmd_modifier; +sway_cmd bar_cmd_output; +sway_cmd bar_cmd_height; +sway_cmd bar_cmd_hidden_state; +sway_cmd bar_cmd_icon_theme; +sway_cmd bar_cmd_id; +sway_cmd bar_cmd_position; +sway_cmd bar_cmd_secondary_button; +sway_cmd bar_cmd_separator_symbol; +sway_cmd bar_cmd_status_command; +sway_cmd bar_cmd_pango_markup; +sway_cmd bar_cmd_strip_workspace_numbers; +sway_cmd bar_cmd_swaybar_command; +sway_cmd bar_cmd_tray_output; +sway_cmd bar_cmd_tray_padding; +sway_cmd bar_cmd_wrap_scroll; +sway_cmd bar_cmd_workspace_buttons; + +sway_cmd bar_colors_cmd_active_workspace; +sway_cmd bar_colors_cmd_background; +sway_cmd bar_colors_cmd_focused_background; +sway_cmd bar_colors_cmd_binding_mode; +sway_cmd bar_colors_cmd_focused_workspace; +sway_cmd bar_colors_cmd_inactive_workspace; +sway_cmd bar_colors_cmd_separator; +sway_cmd bar_colors_cmd_focused_separator; +sway_cmd bar_colors_cmd_statusline; +sway_cmd bar_colors_cmd_focused_statusline; +sway_cmd bar_colors_cmd_urgent_workspace; + +sway_cmd input_cmd_accel_profile; +sway_cmd input_cmd_click_method; +sway_cmd input_cmd_drag_lock; +sway_cmd input_cmd_dwt; +sway_cmd input_cmd_events; +sway_cmd input_cmd_left_handed; +sway_cmd input_cmd_middle_emulation; +sway_cmd input_cmd_natural_scroll; +sway_cmd input_cmd_pointer_accel; +sway_cmd input_cmd_scroll_method; +sway_cmd input_cmd_tap; + +sway_cmd cmd_ipc_cmd; +sway_cmd cmd_ipc_events; +sway_cmd cmd_ipc_event_cmd; + +#endif diff --git a/include/sway/old/config.h b/include/sway/old/config.h new file mode 100644 index 00000000..a05d5ede --- /dev/null +++ b/include/sway/old/config.h @@ -0,0 +1,406 @@ +#ifndef _SWAY_CONFIG_H +#define _SWAY_CONFIG_H + +#define PID_WORKSPACE_TIMEOUT 60 + +#include +#include +#include +#include +#include +#include +#include "wayland-desktop-shell-server-protocol.h" +#include "list.h" +#include "layout.h" +#include "container.h" + +/** + * Describes a variable created via the `set` command. + */ +struct sway_variable { + char *name; + char *value; +}; + +/** + * A key binding and an associated command. + */ +struct sway_binding { + int order; + bool release; + bool bindcode; + list_t *keys; + uint32_t modifiers; + char *command; +}; + +/** + * A mouse binding and an associated command. + */ +struct sway_mouse_binding { + uint32_t button; + char *command; +}; + +/** + * A "mode" of keybindings created via the `mode` command. + */ +struct sway_mode { + char *name; + list_t *bindings; +}; + +/** + * libinput options for input devices + */ +struct input_config { + char *identifier; + + int accel_profile; + int click_method; + int drag_lock; + int dwt; + int left_handed; + int middle_emulation; + int natural_scroll; + float pointer_accel; + int scroll_method; + int send_events; + int tap; + + bool capturable; + struct wlc_geometry region; +}; + +/** + * Size and position configuration for a particular output. + * + * This is set via the `output` command. + */ +struct output_config { + char *name; + int enabled; + int width, height; + int x, y; + int scale; + char *background; + char *background_option; +}; + +/** + * Maps a workspace name to an output name. + * + * Set via `workspace output ` + */ +struct workspace_output { + char *output; + char *workspace; +}; + +struct pid_workspace { + pid_t *pid; + char *workspace; + time_t *time_added; +}; + +struct bar_config { + /** + * One of "dock", "hide", "invisible" + * + * Always visible in dock mode. Visible only when modifier key is held in hide mode. + * Never visible in invisible mode. + */ + char *mode; + /** + * One of "show" or "hide". + * + * In "show" mode, it will always be shown on top of the active workspace. + */ + char *hidden_state; + /** + * Id name used to identify the bar through IPC. + * + * Defaults to bar-x, where x corresponds to the position of the + * embedding bar block in the config file (bar-0, bar-1, ...). + */ + char *id; + uint32_t modifier; + list_t *outputs; + enum desktop_shell_panel_position position; + list_t *bindings; + char *status_command; + bool pango_markup; + char *swaybar_command; + char *font; + int height; // -1 not defined + +#ifdef ENABLE_TRAY + // Tray + char *tray_output; + char *icon_theme; + uint32_t tray_padding; + uint32_t activate_button; + uint32_t context_button; + uint32_t secondary_button; +#endif + + bool workspace_buttons; + bool wrap_scroll; + char *separator_symbol; + bool strip_workspace_numbers; + bool binding_mode_indicator; + bool verbose; + pid_t pid; + struct { + char *background; + char *statusline; + char *separator; + char *focused_background; + char *focused_statusline; + char *focused_separator; + char *focused_workspace_border; + char *focused_workspace_bg; + char *focused_workspace_text; + char *active_workspace_border; + char *active_workspace_bg; + char *active_workspace_text; + char *inactive_workspace_border; + char *inactive_workspace_bg; + char *inactive_workspace_text; + char *urgent_workspace_border; + char *urgent_workspace_bg; + char *urgent_workspace_text; + char *binding_mode_border; + char *binding_mode_bg; + char *binding_mode_text; + } colors; +}; + +struct border_colors { + uint32_t border; + uint32_t background; + uint32_t text; + uint32_t indicator; + uint32_t child_border; +}; + +enum edge_border_types { + E_NONE, /**< Don't hide edge borders */ + E_VERTICAL, /**< hide vertical edge borders */ + E_HORIZONTAL, /**< hide horizontal edge borders */ + E_BOTH, /**< hide vertical and horizontal edge borders */ + E_SMART /**< hide both if precisely one window is present in workspace */ +}; + +enum command_context { + CONTEXT_CONFIG = 1, + CONTEXT_BINDING = 2, + CONTEXT_IPC = 4, + CONTEXT_CRITERIA = 8, + CONTEXT_ALL = 0xFFFFFFFF, +}; + +struct command_policy { + char *command; + uint32_t context; +}; + +enum secure_feature { + FEATURE_LOCK = 1, + FEATURE_PANEL = 2, + FEATURE_BACKGROUND = 4, + FEATURE_SCREENSHOT = 8, + FEATURE_FULLSCREEN = 16, + FEATURE_KEYBOARD = 32, + FEATURE_MOUSE = 64, +}; + +struct feature_policy { + char *program; + uint32_t features; +}; + +enum ipc_feature { + IPC_FEATURE_COMMAND = 1, + IPC_FEATURE_GET_WORKSPACES = 2, + IPC_FEATURE_GET_OUTPUTS = 4, + IPC_FEATURE_GET_TREE = 8, + IPC_FEATURE_GET_MARKS = 16, + IPC_FEATURE_GET_BAR_CONFIG = 32, + IPC_FEATURE_GET_VERSION = 64, + IPC_FEATURE_GET_INPUTS = 128, + IPC_FEATURE_EVENT_WORKSPACE = 256, + IPC_FEATURE_EVENT_OUTPUT = 512, + IPC_FEATURE_EVENT_MODE = 1024, + IPC_FEATURE_EVENT_WINDOW = 2048, + IPC_FEATURE_EVENT_BINDING = 4096, + IPC_FEATURE_EVENT_INPUT = 8192, + IPC_FEATURE_GET_CLIPBOARD = 16384, + + IPC_FEATURE_ALL_COMMANDS = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 16384, + IPC_FEATURE_ALL_EVENTS = 256 | 512 | 1024 | 2048 | 4096 | 8192, + + IPC_FEATURE_ALL = IPC_FEATURE_ALL_COMMANDS | IPC_FEATURE_ALL_EVENTS, +}; + +struct ipc_policy { + char *program; + uint32_t features; +}; + +/** + * The configuration struct. The result of loading a config file. + */ +struct sway_config { + list_t *symbols; + list_t *modes; + list_t *bars; + list_t *cmd_queue; + list_t *workspace_outputs; + list_t *pid_workspaces; + list_t *output_configs; + list_t *input_configs; + list_t *criteria; + list_t *no_focus; + list_t *active_bar_modifiers; + struct sway_mode *current_mode; + struct bar_config *current_bar; + uint32_t floating_mod; + uint32_t dragging_key; + uint32_t resizing_key; + char *floating_scroll_up_cmd; + char *floating_scroll_down_cmd; + char *floating_scroll_left_cmd; + char *floating_scroll_right_cmd; + enum swayc_layouts default_orientation; + enum swayc_layouts default_layout; + char *font; + int font_height; + + // Flags + bool focus_follows_mouse; + bool mouse_warping; + bool force_focus_wrapping; + bool active; + bool failed; + bool reloading; + bool reading; + bool auto_back_and_forth; + bool seamless_mouse; + bool show_marks; + + bool edge_gaps; + bool smart_gaps; + int gaps_inner; + int gaps_outer; + + list_t *config_chain; + const char *current_config; + + enum swayc_border_types border; + enum swayc_border_types floating_border; + int border_thickness; + int floating_border_thickness; + enum edge_border_types hide_edge_borders; + + // border colors + struct { + struct border_colors focused; + struct border_colors focused_inactive; + struct border_colors unfocused; + struct border_colors urgent; + struct border_colors placeholder; + uint32_t background; + } border_colors; + + // floating view + int32_t floating_maximum_width; + int32_t floating_maximum_height; + int32_t floating_minimum_width; + int32_t floating_minimum_height; + + // Security + list_t *command_policies; + list_t *feature_policies; + list_t *ipc_policies; +}; + +void pid_workspace_add(struct pid_workspace *pw); +void free_pid_workspace(struct pid_workspace *pw); + +/** + * Loads the main config from the given path. is_active should be true when + * reloading the config. + */ +bool load_main_config(const char *path, bool is_active); + +/** + * Loads an included config. Can only be used after load_main_config. + */ +bool load_include_configs(const char *path, struct sway_config *config); + +/** + * Reads the config from the given FILE. + */ +bool read_config(FILE *file, struct sway_config *config); + +/** + * Free config struct + */ +void free_config(struct sway_config *config); +/** + * Does variable replacement for a string based on the config's currently loaded variables. + */ +char *do_var_replacement(char *str); + +struct cmd_results *check_security_config(); + +int input_identifier_cmp(const void *item, const void *data); +void merge_input_config(struct input_config *dst, struct input_config *src); +void apply_input_config(struct input_config *ic, struct libinput_device *dev); +void free_input_config(struct input_config *ic); + +int output_name_cmp(const void *item, const void *data); +void merge_output_config(struct output_config *dst, struct output_config *src); +/** Sets up a WLC output handle based on a given output_config. + */ +void apply_output_config(struct output_config *oc, swayc_t *output); +void free_output_config(struct output_config *oc); + +/** + * Updates the list of active bar modifiers + */ +void update_active_bar_modifiers(void); + +int workspace_output_cmp_workspace(const void *a, const void *b); + +int sway_binding_cmp(const void *a, const void *b); +int sway_binding_cmp_qsort(const void *a, const void *b); +int sway_binding_cmp_keys(const void *a, const void *b); +void free_sway_binding(struct sway_binding *sb); +struct sway_binding *sway_binding_dup(struct sway_binding *sb); + +int sway_mouse_binding_cmp(const void *a, const void *b); +int sway_mouse_binding_cmp_qsort(const void *a, const void *b); +int sway_mouse_binding_cmp_buttons(const void *a, const void *b); +void free_sway_mouse_binding(struct sway_mouse_binding *smb); + +void load_swaybars(); +void terminate_swaybg(pid_t pid); + +/** + * Allocate and initialize default bar configuration. + */ +struct bar_config *default_bar_config(void); + +/** + * Global config singleton. + */ +extern struct sway_config *config; + +/** + * Config file currently being read. + */ +extern const char *current_config_path; + +#endif diff --git a/include/sway/old/container.h b/include/sway/old/container.h new file mode 100644 index 00000000..d46ffa63 --- /dev/null +++ b/include/sway/old/container.h @@ -0,0 +1,362 @@ +#ifndef _SWAY_CONTAINER_H +#define _SWAY_CONTAINER_H +#include +#include +#include +#include +#include "list.h" + +typedef struct sway_container swayc_t; + +extern swayc_t root_container; +extern swayc_t *current_focus; + +struct sway_view; + +/** + * Different kinds of containers. + * + * This enum is in order. A container will never be inside of a container below + * it on this list. + */ +enum swayc_types { + C_ROOT, /**< The root container. Only one of these ever exists. */ + C_OUTPUT, /**< An output (aka monitor, head, etc). */ + C_WORKSPACE, /**< A workspace. */ + C_CONTAINER, /**< A manually created container. */ + C_VIEW, /**< A view (aka window). */ + // Keep last + C_TYPES, +}; + +/** + * Different ways to arrange a container. + */ +enum swayc_layouts { + L_NONE, /**< Used for containers that have no layout (views, root) */ + L_HORIZ, + L_VERT, + L_STACKED, + L_TABBED, + L_FLOATING, /**< A psuedo-container, removed from the tree, to hold floating windows */ + + /* Awesome/Monad style auto layouts */ + L_AUTO_LEFT, + L_AUTO_RIGHT, + L_AUTO_TOP, + L_AUTO_BOTTOM, + + L_AUTO_FIRST = L_AUTO_LEFT, + L_AUTO_LAST = L_AUTO_BOTTOM, + + // Keep last + L_LAYOUTS, +}; + +enum swayc_border_types { + B_NONE, /**< No border */ + B_PIXEL, /**< 1px border */ + B_NORMAL /**< Normal border with title bar */ +}; + +/** + * Stores information about a container. + * + * The tree is made of these. Views are containers that cannot have children. + */ +struct sway_container { + // TODO WLR: reconcile these + wlc_handle handle; + + union { + struct sway_output *output; + struct sway_view *view; + } _handle; + + /** + * A unique ID to identify this container. Primarily used in the + * get_tree JSON output. + */ + size_t id; + + enum swayc_types type; + enum swayc_layouts layout; + enum swayc_layouts prev_layout; + enum swayc_layouts workspace_layout; + + /** + * Width and height of this container, without borders or gaps. + */ + double width, height; + + /** + * Views may request geometry, which is stored in this and ignored until + * the views are floated. + */ + int desired_width, desired_height; + + /** + * The coordinates that this view appear at, relative to the output they + * are located on (output containers have absolute coordinates). + */ + double x, y; + + /** + * Cached geometry used to store view/container geometry when switching + * between tabbed/stacked and horizontal/vertical layouts. + */ + struct wlc_geometry cached_geometry; + + /** + * False if this view is invisible. It could be in the scratchpad or on a + * workspace that is not shown. + */ + bool visible; + bool is_floating; + bool is_focused; + bool sticky; // floating view always visible on its output + + // Attributes that mostly views have. + char *name; + char *class; + char *instance; + char *app_id; + + // Used by output containers to keep track of swaybg child processes. + pid_t bg_pid; + + int gaps; + + list_t *children; + /** + * Children of this container that are floated. + */ + list_t *floating; + /** + * Unmanaged view handles in this container. + */ + list_t *unmanaged; + + /** + * The parent of this container. NULL for the root container. + */ + struct sway_container *parent; + /** + * Which of this container's children has focus. + */ + struct sway_container *focused; + /** + * If this container's children include a fullscreen view, this is that view. + */ + struct sway_container *fullscreen; + /** + * If this container is a view, this may be set to the window's decoration + * buffer (or NULL). + */ + struct border *border; + enum swayc_border_types border_type; + struct wlc_geometry border_geometry; + struct wlc_geometry title_bar_geometry; + struct wlc_geometry actual_geometry; + int border_thickness; + + /** + * Number of master views in auto layouts. + */ + size_t nb_master; + + /** + * Number of slave groups (e.g. columns) in auto layouts. + */ + size_t nb_slave_groups; + + /** + * Marks applied to the container, list_t of char*. + */ + list_t *marks; +}; + +enum visibility_mask { + VISIBLE = true +} visible; + +struct sway_output; +/** + * Allocates a new output container. + */ +swayc_t *new_output(struct sway_output *sway_output); +/** + * Allocates a new workspace container. + */ +swayc_t *new_workspace(swayc_t *output, const char *name); +/** + * Allocates a new container and places a child into it. + * + * This is used from the split command, which creates a new container with the + * requested layout and replaces the focused container in the tree with the new + * one. Then the removed container is added as a child of the new container. + */ +swayc_t *new_container(swayc_t *child, enum swayc_layouts layout); +/** + * Allocates a new view container. + * + * Pass in a sibling view, or a workspace to become this container's parent. + */ +swayc_t *new_view(swayc_t *sibling, struct sway_view *view); +/** + * Allocates a new floating view in the active workspace. + */ +swayc_t *new_floating_view(wlc_handle handle); + +void floating_view_sane_size(swayc_t *view); + +/** + * Frees an output's container. + */ +swayc_t *destroy_output(swayc_t *output); +/** + * Destroys a workspace container and returns the parent pointer, or NULL. + */ +swayc_t *destroy_workspace(swayc_t *workspace); +/** + * Destroys a container and all empty parents. Returns the topmost non-empty + * parent container, or NULL. + */ +swayc_t *destroy_container(swayc_t *container); +/** + * Destroys a view container and all empty parents. Returns the topmost + * non-empty parent container, or NULL. + */ +swayc_t *destroy_view(swayc_t *view); + +/** + * Finds a container based on test criteria. Returns the first container that + * passes the test. + */ +swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); +/** + * Finds a parent container with the given swayc_type. + */ +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types); +/** + * Finds a parent with the given swayc_layout. + */ +swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts); +/** + * Finds the bottom-most focused container of a type. + */ +swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types); +/** + * Finds the bottom-most focused container of a layout. + */ +swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts); + +/** + * Gets the swayc_t associated with a wlc_handle. + */ +swayc_t *swayc_by_handle(wlc_handle handle); +/** + * Gets the named swayc_t. + */ +swayc_t *swayc_by_name(const char *name); +/** + * Gets the active output's container. + */ +swayc_t *swayc_active_output(void); +/** + * Gets the active workspace's container. + */ +swayc_t *swayc_active_workspace(void); +/** + * Gets the workspace for the given view container. + */ +swayc_t *swayc_active_workspace_for(swayc_t *view); +/** + * Finds the container currently underneath the pointer. + */ +swayc_t *container_under_pointer(void); +/** + * Finds the first container following a callback. + */ +swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), const void *data); + +/** + * Returns true if a container is fullscreen. + */ +bool swayc_is_fullscreen(swayc_t *view); +/** + * Returns true if this view is focused. + */ +bool swayc_is_active(swayc_t *view); +/** + * Returns true if the parent is an ancestor of the child. + */ +bool swayc_is_parent_of(swayc_t *parent, swayc_t *child); +/** + * Returns true if the child is a desecendant of the parent. + */ +bool swayc_is_child_of(swayc_t *child, swayc_t *parent); + +/** + * Returns true if this container is an empty workspace. + */ +bool swayc_is_empty_workspace(swayc_t *container); + +/** + * Returns the top most tabbed or stacked parent container. Returns NULL if + * view is not in a tabbed/stacked layout. + */ +swayc_t *swayc_tabbed_stacked_ancestor(swayc_t *view); + +/** + * Returns the immediate tabbed or stacked parent container. Returns NULL if + * view is not directly in a tabbed/stacked layout. + */ +swayc_t *swayc_tabbed_stacked_parent(swayc_t *view); + +/** + * Returns the gap (padding) of the container. + * + * This returns the inner gaps for a view, the outer gaps for a workspace, and + * 0 otherwise. + */ +int swayc_gap(swayc_t *container); + +/** + * Maps a container's children over a function. + */ +void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *); + +/** + * Set a view as visible or invisible. + * + * This will perform the required wlc calls as well; it is not sufficient to + * simply toggle the boolean in swayc_t. + */ +void set_view_visibility(swayc_t *view, void *data); +/** + * Set the gaps value for a view. + */ +void set_gaps(swayc_t *view, void *amount); +/** + * Add to the gaps value for a view. + */ +void add_gaps(swayc_t *view, void *amount); + +/** + * Issue wlc calls to make the visibility of a container consistent. + */ +void update_visibility(swayc_t *container); + +/** + * Close all child views of container + */ +void close_views(swayc_t *container); + +/** + * Assign layout to a container. Needed due to workspace container specifics. + * Workspace should always have either L_VERT or L_HORIZ layout. + */ +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); + +#endif diff --git a/include/sway/old/criteria.h b/include/sway/old/criteria.h new file mode 100644 index 00000000..c5ed9857 --- /dev/null +++ b/include/sway/old/criteria.h @@ -0,0 +1,42 @@ +#ifndef _SWAY_CRITERIA_H +#define _SWAY_CRITERIA_H + +#include "container.h" +#include "list.h" + +/** + * Maps criteria (as a list of criteria tokens) to a command list. + * + * A list of tokens together represent a single criteria string (e.g. + * '[class="abc" title="xyz"]' becomes two criteria tokens). + * + * for_window: Views matching all criteria will have the bound command list + * executed on them. + * + * Set via `for_window `. + */ +struct criteria { + list_t *tokens; // struct crit_token, contains compiled regex. + char *crit_raw; // entire criteria string (for logging) + + char *cmdlist; +}; + +int criteria_cmp(const void *item, const void *data); +void free_criteria(struct criteria *crit); + +// Pouplate list with crit_tokens extracted from criteria string, returns error +// string or NULL if successful. +char *extract_crit_tokens(list_t *tokens, const char *criteria); + +// Returns list of criteria that match given container. These criteria have +// been set with `for_window` commands and have an associated cmdlist. +list_t *criteria_for(swayc_t *cont); + +// Returns a list of all containers that match the given list of tokens. +list_t *container_for(list_t *tokens); + +// Returns true if any criteria in the given list matches this container +bool criteria_any(swayc_t *cont, list_t *criteria); + +#endif diff --git a/include/sway/old/focus.h b/include/sway/old/focus.h new file mode 100644 index 00000000..652cdccc --- /dev/null +++ b/include/sway/old/focus.h @@ -0,0 +1,45 @@ +#ifndef _SWAY_FOCUS_H +#define _SWAY_FOCUS_H +enum movement_direction { + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN, + MOVE_PARENT, + MOVE_CHILD, + MOVE_NEXT, + MOVE_PREV, + MOVE_FIRST +}; + +#include "container.h" + +// focused_container - the container found by following the `focused` pointer +// from a given container to a container with `is_focused` boolean set +// --- +// focused_view - the container found by following the `focused` pointer from a +// given container to a view. +// --- + +swayc_t *get_focused_container(swayc_t *parent); +swayc_t *get_focused_view(swayc_t *parent); +swayc_t *get_focused_float(swayc_t *ws); + +// a special-case function to get the focused view, regardless +// of whether it's tiled or floating +swayc_t *get_focused_view_include_floating(swayc_t *parent); + +bool set_focused_container(swayc_t *container); +bool set_focused_container_for(swayc_t *ancestor, swayc_t *container); + +// lock focused container/view. locked by windows with OVERRIDE attribute +// and unlocked when they are destroyed + +extern bool locked_container_focus; + +// Prevents wss from being destroyed on focus switch +extern bool suspend_workspace_cleanup; + +bool move_focus(enum movement_direction direction); + +#endif diff --git a/include/sway/old/input.h b/include/sway/old/input.h new file mode 100644 index 00000000..eb92e470 --- /dev/null +++ b/include/sway/old/input.h @@ -0,0 +1,24 @@ +#ifndef _SWAY_INPUT_H +#define _SWAY_INPUT_H +#include +#include "sway/server.h" +#include "config.h" +#include "list.h" + +struct sway_input { + list_t *input_devices; +}; + +struct input_config *new_input_config(const char* identifier); + +char* libinput_dev_unique_id(struct libinput_device *dev); + +struct sway_input *sway_input_create(struct sway_server *server); + +/** + * Pointer used when reading input blocked. + * Shared so that it can be cleared from commands.c when closing the block + */ +extern struct input_config *current_input_config; + +#endif diff --git a/include/sway/old/input_state.h b/include/sway/old/input_state.h new file mode 100644 index 00000000..fd5a3a25 --- /dev/null +++ b/include/sway/old/input_state.h @@ -0,0 +1,102 @@ +#ifndef _SWAY_KEY_STATE_H +#define _SWAY_KEY_STATE_H +#include +#include +#include "container.h" + +/* Keyboard state */ + +// returns true if key has been pressed, otherwise false +bool check_key(uint32_t key_sym, uint32_t key_code); + +// returns true if key_sym matches latest released key. +bool check_released_key(uint32_t key_sym); + +// sets a key as pressed +void press_key(uint32_t key_sym, uint32_t key_code); + +// unsets a key as pressed +void release_key(uint32_t key_sym, uint32_t key_code); + + +/* Pointer state */ + +enum pointer_values { + M_LEFT_CLICK = 272, + M_RIGHT_CLICK = 273, + M_SCROLL_CLICK = 274, + M_SCROLL_UP = 275, + M_SCROLL_DOWN = 276, +}; + +enum pointer_mode { + // Target + M_FLOATING = 1, + M_TILING = 2, + // Action + M_DRAGGING = 4, + M_RESIZING = 8, +}; + +struct pointer_button_state { + bool held; + // state at the point it was pressed + int x, y; + swayc_t *view; +}; + +extern struct pointer_state { + // mouse clicks + struct pointer_button_state left; + struct pointer_button_state right; + struct pointer_button_state scroll; + + // change in pointer position + struct { + int x, y; + } delta; + + // view pointer is currently over + swayc_t *view; + + // Pointer mode + int mode; +} pointer_state; + +enum modifier_state { + MOD_STATE_UNCHANGED = 0, + MOD_STATE_PRESSED = 1, + MOD_STATE_RELEASED = 2 +}; + +void pointer_position_set(double new_x, double new_y, bool force_focus); +void center_pointer_on(swayc_t *view); + +// on button release unset mode depending on the button. +// on button press set mode conditionally depending on the button +void pointer_mode_set(uint32_t button, bool condition); + +// Update mode in mouse motion +void pointer_mode_update(void); + +// Reset mode on any keypress; +void pointer_mode_reset(void); + +void input_init(void); + +/** + * Check if state of mod changed from current state to new_state. + * + * Returns MOD_STATE_UNCHANGED if the state didn't change, MOD_STATE_PRESSED if + * the state changed to pressed and MOD_STATE_RELEASED if the state changed to + * released. + */ +uint32_t modifier_state_changed(uint32_t new_state, uint32_t mod); + +/** + * Update the current modifiers state to new_state. + */ +void modifiers_state_update(uint32_t new_state); + +#endif + diff --git a/include/sway/old/ipc-json.h b/include/sway/old/ipc-json.h new file mode 100644 index 00000000..3a5af0f5 --- /dev/null +++ b/include/sway/old/ipc-json.h @@ -0,0 +1,15 @@ +#ifndef _SWAY_IPC_JSON_H +#define _SWAY_IPC_JSON_H + +#include +#include "config.h" +#include "container.h" + +json_object *ipc_json_get_version(); +json_object *ipc_json_describe_bar_config(struct bar_config *bar); +json_object *ipc_json_describe_container(swayc_t *c); +json_object *ipc_json_describe_container_recursive(swayc_t *c); +json_object *ipc_json_describe_window(swayc_t *c); +json_object *ipc_json_describe_input(struct libinput_device *device); + +#endif diff --git a/include/sway/old/ipc-server.h b/include/sway/old/ipc-server.h new file mode 100644 index 00000000..0cc26d99 --- /dev/null +++ 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/old/layout.h b/include/sway/old/layout.h new file mode 100644 index 00000000..f0791588 --- /dev/null +++ b/include/sway/old/layout.h @@ -0,0 +1,85 @@ +#ifndef _SWAY_LAYOUT_H +#define _SWAY_LAYOUT_H + +#include +#include "log.h" +#include "list.h" +#include "container.h" +#include "focus.h" + +extern list_t *scratchpad; + +extern int min_sane_w; +extern int min_sane_h; + +// Set initial values for root_container +void init_layout(void); + +// Returns the index of child for its parent +int index_child(const swayc_t *child); + +// Adds child to parent, if parent has no focus, it is set to child +// parent must be of type C_WORKSPACE or C_CONTAINER +void add_child(swayc_t *parent, swayc_t *child); + +// Adds child to parent at index, if parent has no focus, it is set to child +// parent must be of type C_WORKSPACE or C_CONTAINER +void insert_child(swayc_t *parent, swayc_t *child, int index); + +// Adds child as floating window to ws, if there is no focus it is set to child. +// ws must be of type C_WORKSPACE +void add_floating(swayc_t *ws, swayc_t *child); + +// insert child after sibling in parents children. +swayc_t *add_sibling(swayc_t *sibling, swayc_t *child); + +// Replace child with new_child in parents children +// new_child will inherit childs geometry, childs geometry will be reset +// if parents focus is on child, it will be changed to new_child +swayc_t *replace_child(swayc_t *child, swayc_t *new_child); + +// Remove child from its parent, if focus is on child, focus will be changed to +// a sibling, or to a floating window, or NULL +swayc_t *remove_child(swayc_t *child); + +// 2 containers are swapped, they inherit eachothers focus +void swap_container(swayc_t *a, swayc_t *b); + +// 2 Containers geometry are swapped, used with `swap_container` +void swap_geometry(swayc_t *a, swayc_t *b); + +void move_container(swayc_t* container, enum movement_direction direction, int move_amt); +void move_container_to(swayc_t* container, swayc_t* destination); +void move_workspace_to(swayc_t* workspace, swayc_t* destination); + +// Layout +/** + * Update child container geometries when switching between layouts. + */ +void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout); +void update_geometry(swayc_t *view); +void arrange_windows(swayc_t *container, double width, double height); +void arrange_backgrounds(void); + +swayc_t *get_focused_container(swayc_t *parent); +swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir); +swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_direction dir, swayc_t *limit); + +void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge); + +void layout_log(const swayc_t *c, int depth); +void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) __attribute__((format(printf,3,4))); + +/** + * Get default layout. + */ +enum swayc_layouts default_layout(swayc_t *output); + +bool is_auto_layout(enum swayc_layouts layout); +int auto_group_start_index(const swayc_t *container, int index); +int auto_group_end_index(const swayc_t *container, int index); +size_t auto_group_count(const swayc_t *container); +size_t auto_group_index(const swayc_t *container, int index); +bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end); + +#endif diff --git a/include/sway/old/output.h b/include/sway/old/output.h new file mode 100644 index 00000000..2a222238 --- /dev/null +++ b/include/sway/old/output.h @@ -0,0 +1,36 @@ +#ifndef _SWAY_OUTPUT_H +#define _SWAY_OUTPUT_H +#include +#include +#include +#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; +}; + +// 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/include/sway/old/workspace.h b/include/sway/old/workspace.h new file mode 100644 index 00000000..c268fafa --- /dev/null +++ b/include/sway/old/workspace.h @@ -0,0 +1,22 @@ +#ifndef _SWAY_WORKSPACE_H +#define _SWAY_WORKSPACE_H + +#include +#include +#include "list.h" +#include "layout.h" + +extern char *prev_workspace_name; + +char *workspace_next_name(const char *output_name); +swayc_t *workspace_create(const char*); +swayc_t *workspace_by_name(const char*); +swayc_t *workspace_by_number(const char*); +bool workspace_switch(swayc_t*); +swayc_t *workspace_output_next(); +swayc_t *workspace_next(); +swayc_t *workspace_output_prev(); +swayc_t *workspace_prev(); +swayc_t *workspace_for_pid(pid_t pid); + +#endif diff --git a/include/sway/output.h b/include/sway/output.h index 2a222238..ffc6708d 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -3,34 +3,14 @@ #include #include #include -#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/include/sway/workspace.h b/include/sway/workspace.h deleted file mode 100644 index c268fafa..00000000 --- a/include/sway/workspace.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _SWAY_WORKSPACE_H -#define _SWAY_WORKSPACE_H - -#include -#include -#include "list.h" -#include "layout.h" - -extern char *prev_workspace_name; - -char *workspace_next_name(const char *output_name); -swayc_t *workspace_create(const char*); -swayc_t *workspace_by_name(const char*); -swayc_t *workspace_by_number(const char*); -bool workspace_switch(swayc_t*); -swayc_t *workspace_output_next(); -swayc_t *workspace_next(); -swayc_t *workspace_output_prev(); -swayc_t *workspace_prev(); -swayc_t *workspace_for_pid(pid_t pid); - -#endif diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt index f9209189..73531a84 100644 --- 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/border.c b/sway/border.c deleted file mode 100644 index df0022ce..00000000 --- a/sway/border.c +++ /dev/null @@ -1,510 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include -#include -#include -#include -#include "sway/border.h" -#include "sway/container.h" -#include "sway/config.h" -#include "client/pango.h" - -void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { - color = htonl(color); - - cairo_set_source_rgba(cairo, - (color >> (2*8) & 0xFF) / 255.0, - (color >> (1*8) & 0xFF) / 255.0, - (color >> (0*8) & 0xFF) / 255.0, - (color >> (3*8) & 0xFF) / 255.0); -} - -void border_clear(struct border *border) { - if (border && border->buffer) { - free(border->buffer); - border->buffer = NULL; - } -} - -static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry g, cairo_surface_t **surface) { - if (view->border == NULL) { - view->border = malloc(sizeof(struct border)); - if (!view->border) { - sway_log(L_ERROR, "Unable to allocate window border information"); - return NULL; - } - } - cairo_t *cr; - int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, g.size.w); - view->border->buffer = calloc(stride * g.size.h, sizeof(unsigned char)); - view->border->geometry = g; - if (!view->border->buffer) { - sway_log(L_ERROR, "Unable to allocate window border buffer"); - return NULL; - } - *surface = cairo_image_surface_create_for_data(view->border->buffer, - CAIRO_FORMAT_ARGB32, g.size.w, g.size.h, stride); - if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) { - border_clear(view->border); - sway_log(L_ERROR, "Unable to allocate window border surface"); - return NULL; - } - cr = cairo_create(*surface); - cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); - if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) { - cairo_surface_destroy(*surface); - border_clear(view->border); - sway_log(L_ERROR, "Unable to create cairo context"); - return NULL; - } - return cr; -} - -// TODO: move to client/cairo.h when local set_source_u32 is fixed. -/** - * Renders a sharp line of any width and height. - * - * The line is drawn from (x,y) to (x+width,y+height) where width/height is 0 - * if the line has a width/height of one pixel, respectively. - */ -static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y, double width, double height) { - cairo_set_source_u32(cairo, color); - - if (width > 1 && height > 1) { - cairo_rectangle(cairo, x, y, width, height); - cairo_fill(cairo); - } else { - if (width == 1) { - x += 0.5; - height += y; - width = x; - } - - if (height == 1) { - y += 0.5; - width += x; - height = y; - } - - cairo_move_to(cairo, x, y); - cairo_set_line_width(cairo, 1.0); - cairo_line_to(cairo, width, height); - cairo_stroke(cairo); - } -} - -int get_font_text_height(const char *font) { - cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200); - cairo_t *cr = cairo_create(surface); - int width, height; - get_text_size(cr, font, &width, &height, 1, false, "Gg"); - cairo_surface_destroy(surface); - cairo_destroy(cr); - return height; -} - -static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *colors, bool top) { - struct wlc_geometry *g = &view->border->geometry; - struct wlc_geometry *b = &view->border_geometry; - struct wlc_geometry *v = &view->actual_geometry; - enum swayc_layouts layout = view->parent->layout; - uint32_t color; - - int x = b->origin.x - g->origin.x; - int y = b->origin.y - g->origin.y; - - // draw vertical/horizontal indicator if container is the only child of its parent container - bool is_only_child = view->parent && view->parent->children && view->parent->children->length == 1; - - // left border - int left_border = v->origin.x - b->origin.x; - if (left_border > 0) { - render_sharp_line(cr, - colors->child_border, - x, y, - left_border, - b->size.h); - } - - // right border - int right_border = b->size.w - v->size.w - left_border; - if (right_border > 0) { - if (is_only_child && layout == L_HORIZ && !view->is_floating) { - color = colors->indicator; - } else { - color = colors->child_border; - } - render_sharp_line(cr, - color, - x + b->size.w - right_border, - y, - right_border, - b->size.h); - } - - // top border - int top_border = v->origin.y - b->origin.y; - if (top && top_border > 0) { - render_sharp_line(cr, - colors->child_border, - x, y, - b->size.w, - top_border); - } - - // bottom border - int bottom_border = b->size.h - (top_border + v->size.h); - if (bottom_border > 0) { - if (is_only_child && layout == L_VERT && !view->is_floating) { - color = colors->indicator; - } else { - color = colors->child_border; - } - render_sharp_line(cr, - color, - x, - y + b->size.h - bottom_border, - b->size.w, - bottom_border); - } -} - -static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b, struct border_colors *colors) { - struct wlc_geometry *tb = &view->title_bar_geometry; - int x = MIN(tb->origin.x, tb->origin.x - b->origin.x); - int y = MIN(tb->origin.y, tb->origin.y - b->origin.y); - - // title bar background - cairo_set_source_u32(cr, colors->background); - cairo_rectangle(cr, x, y, tb->size.w, tb->size.h); - cairo_fill(cr); - - // header top line - render_sharp_line(cr, colors->border, x, y, tb->size.w, 1); - - // text - if (view->name) { - int width, height; - get_text_size(cr, config->font, &width, &height, 1, false, "%s", view->name); - cairo_move_to(cr, x + 2, y + 2); - cairo_set_source_u32(cr, colors->text); - pango_printf(cr, config->font, 1, false, "%s", view->name); - } - // Marks - if (config->show_marks && view->marks) { - int total_len = 0; - - for(int i = view->marks->length - 1; i >= 0; --i) { - char *mark = (char *)view->marks->items[i]; - if (*mark != '_') { - int width, height; - get_text_size(cr, config->font, &width, &height, 1, false, "[%s]", mark); - total_len += width; - if ((int)tb->size.w + x - (total_len + 2) < x + 2) { - break; - } else { - cairo_move_to(cr, (int)tb->size.w + x - (total_len + 2), y + 2); - cairo_set_source_u32(cr, colors->text); - pango_printf(cr, config->font, 1, false, "[%s]", mark); - } - } - } - } - - // titlebars has a border all around for tabbed layouts - if (view->parent->layout == L_TABBED) { - // header bottom line - render_sharp_line(cr, colors->border, x, y + tb->size.h - 1, - tb->size.w, 1); - - // left border - render_sharp_line(cr, colors->border, x, y, 1, tb->size.h); - - // right border - render_sharp_line(cr, colors->border, x + tb->size.w - 1, y, - 1, tb->size.h); - - return; - } - - if ((uint32_t)(view->actual_geometry.origin.y - tb->origin.y) == tb->size.h) { - // header bottom line - render_sharp_line(cr, colors->border, - x + view->actual_geometry.origin.x - tb->origin.x, - y + tb->size.h - 1, - view->actual_geometry.size.w, 1); - } else { - // header bottom line - render_sharp_line(cr, colors->border, x, - y + tb->size.h - 1, - tb->size.w, 1); - } -} - -/** - * Generate nested container title for tabbed/stacked layouts - */ -static char *generate_container_title(swayc_t *container) { - char layout = 'H'; - char *name, *prev_name = NULL; - switch (container->layout) { - case L_TABBED: - layout = 'T'; - break; - case L_STACKED: - layout = 'S'; - break; - case L_VERT: - layout = 'V'; - break; - default: - layout = 'H'; - } - int len = 9; - name = malloc(len * sizeof(char)); - if (!name) { - sway_log(L_ERROR, "Unable to allocate container title"); - return NULL; - } - snprintf(name, len, "sway: %c[", layout); - - int i; - for (i = 0; i < container->children->length; ++i) { - prev_name = name; - swayc_t* child = container->children->items[i]; - const char *title = NULL; - if (child->type == C_VIEW) { - title = child->app_id ? child->app_id : - (child->instance ? child->instance : - (child->class ? child->class :"(null)")); - } else { //child->type == C_CONTAINER - title = generate_container_title(child); - } - - len = strlen(name) + strlen(title) + 1; - if (i < container->children->length-1) { - len++; - } - - name = malloc(len * sizeof(char)); - if (!name) { - free(prev_name); - sway_log(L_ERROR, "Unable to allocate container title"); - return NULL; - } - if (i < container->children->length-1) { - snprintf(name, len, "%s%s ", prev_name, title); - } else { - snprintf(name, len, "%s%s", prev_name, title); - } - free(prev_name); - } - - prev_name = name; - len = strlen(name) + 2; - name = malloc(len * sizeof(char)); - if (!name) { - free(prev_name); - sway_log(L_ERROR, "Unable to allocate container title"); - return NULL; - } - snprintf(name, len, "%s]", prev_name); - free(prev_name); - free(container->name); - container->name = name; - return container->name + 6; // don't include "sway: " -} - -void update_tabbed_stacked_titlebars(swayc_t *c, cairo_t *cr, struct wlc_geometry *g, swayc_t *focused, swayc_t *focused_inactive) { - if (c->type == C_CONTAINER) { - if (c->parent->focused == c) { - render_title_bar(c, cr, g, &config->border_colors.focused_inactive); - } else { - render_title_bar(c, cr, g, &config->border_colors.unfocused); - } - - if (!c->visible) { - return; - } - - int i; - for (i = 0; i < c->children->length; ++i) { - swayc_t *child = c->children->items[i]; - update_tabbed_stacked_titlebars(child, cr, g, focused, focused_inactive); - } - } else { - bool is_child_of_focused = swayc_is_child_of(c, get_focused_container(&root_container)); - - if (focused == c || is_child_of_focused) { - render_title_bar(c, cr, g, &config->border_colors.focused); - } else if (focused_inactive == c) { - render_title_bar(c, cr, g, &config->border_colors.focused_inactive); - } else { - render_title_bar(c, cr, g, &config->border_colors.unfocused); - } - } -} - -static void update_view_border(swayc_t *view) { - if (!view->visible) { - return; - } - - cairo_t *cr = NULL; - cairo_surface_t *surface = NULL; - - // clear previous border buffer. - border_clear(view->border); - - // get focused and focused_inactive views - swayc_t *focused = get_focused_view(&root_container); - swayc_t *container = swayc_parent_by_type(view, C_CONTAINER); - swayc_t *focused_inactive = NULL; - - bool is_child_of_focused = swayc_is_parent_of(get_focused_container(&root_container), view); - - if (container) { - focused_inactive = swayc_focus_by_type(container, C_VIEW); - } else { - container = swayc_parent_by_type(view, C_WORKSPACE); - if (container) { - focused_inactive = swayc_focus_by_type(container, C_VIEW); - } - } - - // for tabbed/stacked layouts the focused view has to draw all the - // titlebars of the hidden views. - swayc_t *p = NULL; - if (view->parent->focused == view && (p = swayc_tabbed_stacked_ancestor(view))) { - struct wlc_geometry g = { - .origin = { - .x = p->x, - .y = p->y - }, - .size = { - .w = p->width, - .h = p->height - } - }; - cr = create_border_buffer(view, g, &surface); - if (!cr) { - goto cleanup; - } - - bool render_top = !should_hide_top_border(view, view->y); - if (view == focused || is_child_of_focused) { - render_borders(view, cr, &config->border_colors.focused, render_top); - } else { - render_borders(view, cr, &config->border_colors.focused_inactive, render_top); - } - - // generate container titles - int i; - for (i = 0; i < p->children->length; ++i) { - swayc_t *child = p->children->items[i]; - if (child->type == C_CONTAINER) { - generate_container_title(child); - } - } - - update_tabbed_stacked_titlebars(p, cr, &g, focused, focused_inactive); - } else { - switch (view->border_type) { - case B_NONE: - break; - case B_PIXEL: - cr = create_border_buffer(view, view->border_geometry, &surface); - if (!cr) { - break; - } - - if (focused == view || is_child_of_focused) { - render_borders(view, cr, &config->border_colors.focused, true); - } else if (focused_inactive == view) { - render_borders(view, cr, &config->border_colors.focused_inactive, true); - } else { - render_borders(view, cr, &config->border_colors.unfocused, true); - } - - break; - case B_NORMAL: - cr = create_border_buffer(view, view->border_geometry, &surface); - if (!cr) { - break; - } - - if (focused == view || is_child_of_focused) { - render_borders(view, cr, &config->border_colors.focused, false); - render_title_bar(view, cr, &view->border_geometry, - &config->border_colors.focused); - } else if (focused_inactive == view) { - render_borders(view, cr, &config->border_colors.focused_inactive, false); - render_title_bar(view, cr, &view->border_geometry, - &config->border_colors.focused_inactive); - } else { - render_borders(view, cr, &config->border_colors.unfocused, false); - render_title_bar(view, cr, &view->border_geometry, - &config->border_colors.unfocused); - } - - break; - } - } - -cleanup: - - if (surface) { - cairo_surface_flush(surface); - cairo_surface_destroy(surface); - } - - if (cr) { - cairo_destroy(cr); - } -} - -void update_container_border(swayc_t *container) { - if (container->type == C_VIEW) { - update_view_border(container); - return; - } else { - for (int i = 0; i < container->children->length; ++i) { - update_container_border(container->children->items[i]); - } - } -} - -void render_view_borders(wlc_handle view) { - swayc_t *c = swayc_by_handle(view); - - - // emulate i3 behavior for drawing borders for tabbed and stacked layouts: - // if we are not the only child in the container, always draw borders, - // regardless of the border setting on the individual view - if (!c || (c->border_type == B_NONE - && !((c->parent->layout == L_TABBED || c->parent->layout == L_STACKED) - && c->parent->children->length > 1))) { - return; - } - - if (c->border && c->border->buffer) { - wlc_pixels_write(WLC_RGBA8888, &c->border->geometry, c->border->buffer); - } -} - -bool should_hide_top_border(swayc_t *con, double y) { - // returns true if container is child of tabbed/stacked layout and is - // sharing top border with tabbed titlebar - swayc_t *par = con->parent; - while (par->type != C_WORKSPACE) { - if (par->layout == L_TABBED || par->layout == L_STACKED) { - return con->y == y; - } - con = par; - par = par->parent; - } - return false; -} diff --git a/sway/commands.c b/sway/commands.c deleted file mode 100644 index e1181893..00000000 --- a/sway/commands.c +++ /dev/null @@ -1,686 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "sway/layout.h" -#include "sway/focus.h" -#include "sway/workspace.h" -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/output.h" -#include "sway/handlers.h" -#include "sway/input_state.h" -#include "sway/criteria.h" -#include "sway/ipc-server.h" -#include "sway/security.h" -#include "sway/input.h" -#include "sway/border.h" -#include "stringop.h" -#include "sway.h" -#include "util.h" -#include "list.h" -#include "log.h" - -struct cmd_handler { - char *command; - sway_cmd *handle; -}; - -int sp_index = 0; - -swayc_t *current_container = NULL; - -// Returns error object, or NULL if check succeeds. -struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { - struct cmd_results *error = NULL; - switch (type) { - case EXPECTED_MORE_THAN: - if (argc > val) { - return NULL; - } - error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(expected more than %d argument%s, got %d)", - name, val, (char*[2]){"s", ""}[argc==1], argc); - break; - case EXPECTED_AT_LEAST: - if (argc >= val) { - return NULL; - } - error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(expected at least %d argument%s, got %d)", - name, val, (char*[2]){"s", ""}[argc==1], argc); - break; - case EXPECTED_LESS_THAN: - if (argc < val) { - return NULL; - }; - error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(expected less than %d argument%s, got %d)", - name, val, (char*[2]){"s", ""}[argc==1], argc); - break; - case EXPECTED_EQUAL_TO: - if (argc == val) { - return NULL; - }; - error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(expected %d arguments, got %d)", name, val, argc); - break; - } - return error; -} - -void hide_view_in_scratchpad(swayc_t *sp_view) { - if (sp_view == NULL) { - return; - } - - wlc_view_set_mask(sp_view->handle, 0); - sp_view->visible = false; - swayc_t *ws = sp_view->parent; - remove_child(sp_view); - if (swayc_active_workspace() != ws && ws->floating->length == 0 && ws->children->length == 0) { - destroy_workspace(ws); - } else { - arrange_windows(ws, -1, -1); - } - set_focused_container(container_under_pointer()); -} - -void input_cmd_apply(struct input_config *input) { - int i; - i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier); - if (i >= 0) { - // merge existing config - struct input_config *ic = config->input_configs->items[i]; - merge_input_config(ic, input); - free_input_config(input); - input = ic; - } else { - list_add(config->input_configs, input); - } - - current_input_config = input; - - if (input->identifier) { - // Try to find the input device and apply configuration now. If - // this is during startup then there will be no container and config - // will be applied during normal "new input" event from wlc. - /* TODO WLR - struct libinput_device *device = NULL; - for (int i = 0; i < input_devices->length; ++i) { - device = input_devices->items[i]; - char* dev_identifier = libinput_dev_unique_id(device); - if (!dev_identifier) { - break; - } - int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0; - free(dev_identifier); - if (match) { - apply_input_config(input, device); - break; - } - } - */ - } -} - -void remove_view_from_scratchpad(swayc_t *view) { - int i; - for (i = 0; i < scratchpad->length; i++) { - if (scratchpad->items[i] == view) { - if (sp_index == 0) { - sp_index = scratchpad->length - 1; - } else { - sp_index--; - } - list_del(scratchpad, sp_index); - sp_view = NULL; - } - } -} - -/* Keep alphabetized */ -static struct cmd_handler handlers[] = { - { "assign", cmd_assign }, - { "bar", cmd_bar }, - { "bindcode", cmd_bindcode }, - { "bindsym", cmd_bindsym }, - { "border", cmd_border }, - { "client.background", cmd_client_background }, - { "client.focused", cmd_client_focused }, - { "client.focused_inactive", cmd_client_focused_inactive }, - { "client.placeholder", cmd_client_placeholder }, - { "client.unfocused", cmd_client_unfocused }, - { "client.urgent", cmd_client_urgent }, - { "clipboard", cmd_clipboard }, - { "commands", cmd_commands }, - { "debuglog", cmd_debuglog }, - { "default_border", cmd_default_border }, - { "default_floating_border", cmd_default_floating_border }, - { "default_orientation", cmd_orientation }, - { "exec", cmd_exec }, - { "exec_always", cmd_exec_always }, - { "exit", cmd_exit }, - { "floating", cmd_floating }, - { "floating_maximum_size", cmd_floating_maximum_size }, - { "floating_minimum_size", cmd_floating_minimum_size }, - { "floating_modifier", cmd_floating_mod }, - { "floating_scroll", cmd_floating_scroll }, - { "focus", cmd_focus }, - { "focus_follows_mouse", cmd_focus_follows_mouse }, - { "font", cmd_font }, - { "for_window", cmd_for_window }, - { "force_focus_wrapping", cmd_force_focus_wrapping }, - { "fullscreen", cmd_fullscreen }, - { "gaps", cmd_gaps }, - { "hide_edge_borders", cmd_hide_edge_borders }, - { "include", cmd_include }, - { "input", cmd_input }, - { "ipc", cmd_ipc }, - { "kill", cmd_kill }, - { "layout", cmd_layout }, - { "log_colors", cmd_log_colors }, - { "mark", cmd_mark }, - { "mode", cmd_mode }, - { "mouse_warping", cmd_mouse_warping }, - { "move", cmd_move }, - { "new_float", cmd_new_float }, - { "new_window", cmd_new_window }, - { "no_focus", cmd_no_focus }, - { "output", cmd_output }, - { "permit", cmd_permit }, - { "reject", cmd_reject }, - { "reload", cmd_reload }, - { "resize", cmd_resize }, - { "scratchpad", cmd_scratchpad }, - { "seamless_mouse", cmd_seamless_mouse }, - { "set", cmd_set }, - { "show_marks", cmd_show_marks }, - { "smart_gaps", cmd_smart_gaps }, - { "split", cmd_split }, - { "splith", cmd_splith }, - { "splitt", cmd_splitt }, - { "splitv", cmd_splitv }, - { "sticky", cmd_sticky }, - { "unmark", cmd_unmark }, - { "workspace", cmd_workspace }, - { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, - { "workspace_layout", cmd_workspace_layout }, -}; - -static struct cmd_handler bar_handlers[] = { - { "activate_button", bar_cmd_activate_button }, - { "binding_mode_indicator", bar_cmd_binding_mode_indicator }, - { "bindsym", bar_cmd_bindsym }, - { "colors", bar_cmd_colors }, - { "context_button", bar_cmd_context_button }, - { "font", bar_cmd_font }, - { "height", bar_cmd_height }, - { "hidden_state", bar_cmd_hidden_state }, - { "icon_theme", bar_cmd_icon_theme }, - { "id", bar_cmd_id }, - { "mode", bar_cmd_mode }, - { "modifier", bar_cmd_modifier }, - { "output", bar_cmd_output }, - { "pango_markup", bar_cmd_pango_markup }, - { "position", bar_cmd_position }, - { "secondary_button", bar_cmd_secondary_button }, - { "separator_symbol", bar_cmd_separator_symbol }, - { "status_command", bar_cmd_status_command }, - { "strip_workspace_numbers", bar_cmd_strip_workspace_numbers }, - { "swaybar_command", bar_cmd_swaybar_command }, - { "tray_output", bar_cmd_tray_output }, - { "tray_padding", bar_cmd_tray_padding }, - { "workspace_buttons", bar_cmd_workspace_buttons }, - { "wrap_scroll", bar_cmd_wrap_scroll }, -}; - -/** - * Check and add color to buffer. - * - * return error object, or NULL if color is valid. - */ -struct cmd_results *add_color(const char *name, char *buffer, const char *color) { - int len = strlen(color); - if (len != 7 && len != 9) { - return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); - } - - if (color[0] != '#') { - return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); - } - - int i; - for (i = 1; i < len; ++i) { - if (!isxdigit(color[i])) { - return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); - } - } - - // copy color to buffer - strncpy(buffer, color, len); - // add default alpha channel if color was defined without it - if (len == 7) { - buffer[7] = 'f'; - buffer[8] = 'f'; - } - buffer[9] = '\0'; - - return NULL; -} - -static struct cmd_handler input_handlers[] = { - { "accel_profile", input_cmd_accel_profile }, - { "click_method", input_cmd_click_method }, - { "drag_lock", input_cmd_drag_lock }, - { "dwt", input_cmd_dwt }, - { "events", input_cmd_events }, - { "left_handed", input_cmd_left_handed }, - { "middle_emulation", input_cmd_middle_emulation }, - { "natural_scroll", input_cmd_natural_scroll }, - { "pointer_accel", input_cmd_pointer_accel }, - { "scroll_method", input_cmd_scroll_method }, - { "tap", input_cmd_tap }, -}; - -static struct cmd_handler bar_colors_handlers[] = { - { "active_workspace", bar_colors_cmd_active_workspace }, - { "background", bar_colors_cmd_background }, - { "binding_mode", bar_colors_cmd_binding_mode }, - { "focused_background", bar_colors_cmd_focused_background }, - { "focused_separator", bar_colors_cmd_focused_separator }, - { "focused_statusline", bar_colors_cmd_focused_statusline }, - { "focused_workspace", bar_colors_cmd_focused_workspace }, - { "inactive_workspace", bar_colors_cmd_inactive_workspace }, - { "separator", bar_colors_cmd_separator }, - { "statusline", bar_colors_cmd_statusline }, - { "urgent_workspace", bar_colors_cmd_urgent_workspace }, -}; - -static struct cmd_handler ipc_handlers[] = { - { "*", cmd_ipc_cmd }, - { "bar-config", cmd_ipc_cmd }, - { "command", cmd_ipc_cmd }, - { "events", cmd_ipc_events }, - { "inputs", cmd_ipc_cmd }, - { "marks", cmd_ipc_cmd }, - { "outputs", cmd_ipc_cmd }, - { "tree", cmd_ipc_cmd }, - { "workspaces", cmd_ipc_cmd }, -}; - -static struct cmd_handler ipc_event_handlers[] = { - { "*", cmd_ipc_event_cmd }, - { "binding", cmd_ipc_event_cmd }, - { "input", cmd_ipc_event_cmd }, - { "mode", cmd_ipc_event_cmd }, - { "output", cmd_ipc_event_cmd }, - { "window", cmd_ipc_event_cmd }, - { "workspace", cmd_ipc_event_cmd }, -}; - -static int handler_compare(const void *_a, const void *_b) { - const struct cmd_handler *a = _a; - const struct cmd_handler *b = _b; - return strcasecmp(a->command, b->command); -} - -static struct cmd_handler *find_handler(char *line, enum cmd_status block) { - struct cmd_handler d = { .command=line }; - struct cmd_handler *res = NULL; - sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT); - if (block == CMD_BLOCK_BAR) { - res = bsearch(&d, bar_handlers, - sizeof(bar_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_BAR_COLORS){ - res = bsearch(&d, bar_colors_handlers, - sizeof(bar_colors_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_INPUT) { - res = bsearch(&d, input_handlers, - sizeof(input_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_IPC) { - res = bsearch(&d, ipc_handlers, - sizeof(ipc_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_IPC_EVENTS) { - res = bsearch(&d, ipc_event_handlers, - sizeof(ipc_event_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else { - res = bsearch(&d, handlers, - sizeof(handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } - return res; -} - -struct cmd_results *handle_command(char *_exec, enum command_context context) { - // Even though this function will process multiple commands we will only - // return the last error, if any (for now). (Since we have access to an - // error string we could e.g. concatonate all errors there.) - struct cmd_results *results = NULL; - char *exec = strdup(_exec); - char *head = exec; - char *cmdlist; - char *cmd; - list_t *containers = NULL; - - head = exec; - do { - // Extract criteria (valid for this command list only). - if (*head == '[') { - ++head; - char *criteria_string = argsep(&head, "]"); - if (head) { - ++head; - list_t *tokens = create_list(); - char *error; - - if ((error = extract_crit_tokens(tokens, criteria_string))) { - results = cmd_results_new(CMD_INVALID, criteria_string, - "Can't parse criteria string: %s", error); - free(error); - free(tokens); - goto cleanup; - } - containers = container_for(tokens); - - free(tokens); - } else { - if (!results) { - results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched ["); - } - goto cleanup; - } - // Skip leading whitespace - head += strspn(head, whitespace); - } - // Split command list - cmdlist = argsep(&head, ";"); - cmdlist += strspn(cmdlist, whitespace); - do { - // Split commands - cmd = argsep(&cmdlist, ","); - cmd += strspn(cmd, whitespace); - if (strcmp(cmd, "") == 0) { - sway_log(L_INFO, "Ignoring empty command."); - continue; - } - sway_log(L_INFO, "Handling command '%s'", cmd); - //TODO better handling of argv - int argc; - char **argv = split_args(cmd, &argc); - if (strcmp(argv[0], "exec") != 0) { - int i; - for (i = 1; i < argc; ++i) { - if (*argv[i] == '\"' || *argv[i] == '\'') { - strip_quotes(argv[i]); - } - } - } - struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END); - if (!handler) { - if (results) { - free_cmd_results(results); - } - results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command"); - free_argv(argc, argv); - goto cleanup; - } - if (!(get_command_policy_mask(argv[0]) & context)) { - if (results) { - free_cmd_results(results); - } - results = cmd_results_new(CMD_INVALID, cmd, - "Permission denied for %s via %s", cmd, - command_policy_str(context)); - free_argv(argc, argv); - goto cleanup; - } - int i = 0; - do { - if (!containers) { - current_container = get_focused_container(&root_container); - } else if (containers->length == 0) { - if (results) { - free_cmd_results(results); - } - results = cmd_results_new(CMD_FAILURE, argv[0], "No matching container"); - goto cleanup; - } else { - current_container = (swayc_t *)containers->items[i]; - } - sway_log(L_INFO, "Running on container '%s'", current_container->name); - - struct cmd_results *res = handler->handle(argc-1, argv+1); - if (res->status != CMD_SUCCESS) { - free_argv(argc, argv); - if (results) { - free_cmd_results(results); - } - results = res; - goto cleanup; - } - free_cmd_results(res); - ++i; - } while(containers && i < containers->length); - - free_argv(argc, argv); - } while(cmdlist); - - if (containers) { - list_free(containers); - containers = NULL; - } - } while(head); - cleanup: - free(exec); - if (containers) { - free(containers); - } - if (!results) { - results = cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - return results; -} - -// this is like handle_command above, except: -// 1) it ignores empty commands (empty lines) -// 2) it does variable substitution -// 3) it doesn't split commands (because the multiple commands are supposed to -// be chained together) -// 4) handle_command handles all state internally while config_command has some -// state handled outside (notably the block mode, in read_config) -struct cmd_results *config_command(char *exec, enum cmd_status block) { - struct cmd_results *results = NULL; - int argc; - char **argv = split_args(exec, &argc); - if (!argc) { - results = cmd_results_new(CMD_SUCCESS, NULL, NULL); - goto cleanup; - } - - sway_log(L_INFO, "handling config command '%s'", exec); - // Endblock - if (**argv == '}') { - results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); - goto cleanup; - } - struct cmd_handler *handler = find_handler(argv[0], block); - if (!handler) { - char *input = argv[0] ? argv[0] : "(empty)"; - results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); - goto cleanup; - } - int i; - // Var replacement, for all but first argument of set - for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) { - argv[i] = do_var_replacement(argv[i]); - unescape_string(argv[i]); - } - /* Strip quotes for first argument. - * TODO This part needs to be handled much better */ - if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) { - strip_quotes(argv[1]); - } - if (handler->handle) { - results = handler->handle(argc-1, argv+1); - } else { - results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented"); - } - -cleanup: - free_argv(argc, argv); - return results; -} - -struct cmd_results *config_commands_command(char *exec) { - struct cmd_results *results = NULL; - int argc; - char **argv = split_args(exec, &argc); - if (!argc) { - results = cmd_results_new(CMD_SUCCESS, NULL, NULL); - goto cleanup; - } - - // Find handler for the command this is setting a policy for - char *cmd = argv[0]; - - if (strcmp(cmd, "}") == 0) { - results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); - goto cleanup; - } - - struct cmd_handler *handler = find_handler(cmd, CMD_BLOCK_END); - if (!handler && strcmp(cmd, "*") != 0) { - char *input = cmd ? cmd : "(empty)"; - results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); - goto cleanup; - } - - enum command_context context = 0; - - struct { - char *name; - enum command_context context; - } context_names[] = { - { "config", CONTEXT_CONFIG }, - { "binding", CONTEXT_BINDING }, - { "ipc", CONTEXT_IPC }, - { "criteria", CONTEXT_CRITERIA }, - { "all", CONTEXT_ALL }, - }; - - for (int i = 1; i < argc; ++i) { - size_t j; - for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) { - if (strcmp(context_names[j].name, argv[i]) == 0) { - break; - } - } - if (j == sizeof(context_names) / sizeof(context_names[0])) { - results = cmd_results_new(CMD_INVALID, cmd, - "Invalid command context %s", argv[i]); - goto cleanup; - } - context |= context_names[j].context; - } - - struct command_policy *policy = NULL; - for (int i = 0; i < config->command_policies->length; ++i) { - struct command_policy *p = config->command_policies->items[i]; - if (strcmp(p->command, cmd) == 0) { - policy = p; - break; - } - } - if (!policy) { - policy = alloc_command_policy(cmd); - sway_assert(policy, "Unable to allocate security policy"); - if (policy) { - list_add(config->command_policies, policy); - } - } - policy->context = context; - - sway_log(L_INFO, "Set command policy for %s to %d", - policy->command, policy->context); - - results = cmd_results_new(CMD_SUCCESS, NULL, NULL); - -cleanup: - free_argv(argc, argv); - return results; -} - -struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) { - struct cmd_results *results = malloc(sizeof(struct cmd_results)); - if (!results) { - sway_log(L_ERROR, "Unable to allocate command results"); - return NULL; - } - results->status = status; - if (input) { - results->input = strdup(input); // input is the command name - } else { - results->input = NULL; - } - if (format) { - char *error = malloc(256); - va_list args; - va_start(args, format); - if (error) { - vsnprintf(error, 256, format, args); - } - va_end(args); - results->error = error; - } else { - results->error = NULL; - } - return results; -} - -void free_cmd_results(struct cmd_results *results) { - if (results->input) { - free(results->input); - } - if (results->error) { - free(results->error); - } - free(results); -} - -const char *cmd_results_to_json(struct cmd_results *results) { - json_object *result_array = json_object_new_array(); - json_object *root = json_object_new_object(); - json_object_object_add(root, "success", json_object_new_boolean(results->status == CMD_SUCCESS)); - if (results->input) { - json_object_object_add(root, "input", json_object_new_string(results->input)); - } - if (results->error) { - json_object_object_add(root, "error", json_object_new_string(results->error)); - } - json_object_array_add(result_array, root); - const char *json = json_object_to_json_string(result_array); - free(result_array); - free(root); - return json; -} diff --git a/sway/commands/assign.c b/sway/commands/assign.c deleted file mode 100644 index c3b03bbc..00000000 --- a/sway/commands/assign.c +++ /dev/null @@ -1,57 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include "sway/commands.h" -#include "sway/criteria.h" -#include "list.h" -#include "log.h" - -struct cmd_results *cmd_assign(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) { - return error; - } - - char *criteria = *argv++; - - if (strncmp(*argv, "→", strlen("→")) == 0) { - if (argc < 3) { - return cmd_results_new(CMD_INVALID, "assign", "Missing workspace"); - } - argv++; - } - - char *movecmd = "move container to workspace "; - int arglen = strlen(movecmd) + strlen(*argv) + 1; - char *cmdlist = calloc(1, arglen); - if (!cmdlist) { - return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list"); - } - snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); - - struct criteria *crit = malloc(sizeof(struct criteria)); - if (!crit) { - free(cmdlist); - return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria"); - } - crit->crit_raw = strdup(criteria); - crit->cmdlist = cmdlist; - crit->tokens = create_list(); - char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); - - if (err_str) { - error = cmd_results_new(CMD_INVALID, "assign", err_str); - free(err_str); - free_criteria(crit); - } else if (crit->tokens->length == 0) { - error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); - free_criteria(crit); - } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { - sway_log(L_DEBUG, "assign: Duplicate, skipping."); - free_criteria(crit); - } else { - sway_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); - list_add(config->criteria, crit); - } - return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar.c b/sway/commands/bar.c deleted file mode 100644 index 04745a6e..00000000 --- a/sway/commands/bar.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "log.h" -#include "util.h" - -struct cmd_results *cmd_bar(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (config->reading && strcmp("{", argv[0]) != 0) { - return cmd_results_new(CMD_INVALID, "bar", - "Expected '{' at start of bar config definition."); - } - - if (!config->reading) { - if (argc > 1) { - if (strcasecmp("mode", argv[0]) == 0) { - return bar_cmd_mode(argc-1, argv + 1); - } - - if (strcasecmp("hidden_state", argv[0]) == 0) { - return bar_cmd_hidden_state(argc-1, argv + 1); - } - } - - return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file."); - } - - // Create new bar with default values - struct bar_config *bar = default_bar_config(); - if (!bar) { - return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state"); - } - - // set bar id - int i; - for (i = 0; i < config->bars->length; ++i) { - if (bar == config->bars->items[i]) { - const int len = 5 + numlen(i); // "bar-" + i + \0 - bar->id = malloc(len * sizeof(char)); - if (bar->id) { - snprintf(bar->id, len, "bar-%d", i); - } else { - return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID"); - } - break; - } - } - - // Set current bar - config->current_bar = bar; - sway_log(L_DEBUG, "Configuring bar %s", bar->id); - return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL); -} diff --git a/sway/commands/bar/activate_button.c b/sway/commands/bar/activate_button.c deleted file mode 100644 index 32a1d3e5..00000000 --- a/sway/commands/bar/activate_button.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_activate_button(int argc, char **argv) { - const char *cmd_name = "activate_button"; -#ifndef ENABLE_TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " - "%s called, but sway was compiled without tray support", - cmd_name, cmd_name); -#else - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); - } - - // User should be able to prefix with 0x or whatever they want - config->current_bar->secondary_button = strtoul(argv[0], NULL, 0); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -#endif -} diff --git a/sway/commands/bar/binding_mode_indicator.c b/sway/commands/bar/binding_mode_indicator.c deleted file mode 100644 index 64f5b84f..00000000 --- a/sway/commands/bar/binding_mode_indicator.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_binding_mode_indicator(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "binding_mode_indicator", "No bar defined."); - } - - if (strcasecmp("yes", argv[0]) == 0) { - config->current_bar->binding_mode_indicator = true; - sway_log(L_DEBUG, "Enabling binding mode indicator on bar: %s", config->current_bar->id); - } else if (strcasecmp("no", argv[0]) == 0) { - config->current_bar->binding_mode_indicator = false; - sway_log(L_DEBUG, "Disabling binding mode indicator on bar: %s", config->current_bar->id); - } else { - error = cmd_results_new(CMD_INVALID, "binding_mode_indicator", "Invalid value %s", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c deleted file mode 100644 index 5f90b51a..00000000 --- a/sway/commands/bar/bindsym.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "list.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { - return error; - } else if (!config->reading) { - return cmd_results_new(CMD_FAILURE, "bindsym", "Can only be used in config file."); - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "bindsym", "No bar defined."); - } - - if (strlen(argv[1]) != 7) { - return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); - } - uint32_t numbutton = (uint32_t)atoi(argv[1] + 6); - if (numbutton < 1 || numbutton > 5 || strncmp(argv[1], "button", 6) != 0) { - return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); - } - struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding)); - if (!binding) { - return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); - } - binding->button = numbutton; - binding->command = join_args(argv + 1, argc - 1); - - struct bar_config *bar = config->current_bar; - int i = list_seq_find(bar->bindings, sway_mouse_binding_cmp_buttons, binding); - if (i > -1) { - sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); - struct sway_mouse_binding *dup = bar->bindings->items[i]; - free_sway_mouse_binding(dup); - list_del(bar->bindings, i); - } - list_add(bar->bindings, binding); - list_qsort(bar->bindings, sway_mouse_binding_cmp_qsort); - - sway_log(L_DEBUG, "bindsym - Bound %s to command %s when clicking swaybar", argv[0], binding->command); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c deleted file mode 100644 index 8b3b0aac..00000000 --- a/sway/commands/bar/colors.c +++ /dev/null @@ -1,129 +0,0 @@ -#include -#include "sway/commands.h" - -static struct cmd_results *parse_single_color(char **color, const char *cmd_name, int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!*color) { - *color = malloc(10); - if (!*color) { - return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); - } - } - - error = add_color(cmd_name, *color, argv[0]); - if (error) { - return error; - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -static struct cmd_results *parse_three_colors(char ***colors, const char *cmd_name, int argc, char **argv) { - struct cmd_results *error = NULL; - if (argc != 3) { - return cmd_results_new(CMD_INVALID, cmd_name, "Requires exactly three color values"); - } - - int i; - for (i = 0; i < 3; i++) { - if (!*colors[i]) { - *(colors[i]) = malloc(10); - if (!*(colors[i])) { - return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); - } - } - error = add_color(cmd_name, *(colors[i]), argv[i]); - if (error) { - return error; - } - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *bar_cmd_colors(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (strcmp("{", argv[0]) != 0) { - return cmd_results_new(CMD_INVALID, "colors", - "Expected '{' at the start of colors config definition."); - } - - return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL); -} - -struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) { - char **colors[3] = { - &(config->current_bar->colors.active_workspace_border), - &(config->current_bar->colors.active_workspace_bg), - &(config->current_bar->colors.active_workspace_text) - }; - return parse_three_colors(colors, "active_workspace", argc, argv); -} - -struct cmd_results *bar_colors_cmd_background(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.background), "background", argc, argv); -} - -struct cmd_results *bar_colors_cmd_focused_background(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.focused_background), "focused_background", argc, argv); -} - -struct cmd_results *bar_colors_cmd_binding_mode(int argc, char **argv) { - char **colors[3] = { - &(config->current_bar->colors.binding_mode_border), - &(config->current_bar->colors.binding_mode_bg), - &(config->current_bar->colors.binding_mode_text) - }; - return parse_three_colors(colors, "binding_mode", argc, argv); -} - -struct cmd_results *bar_colors_cmd_focused_workspace(int argc, char **argv) { - char **colors[3] = { - &(config->current_bar->colors.focused_workspace_border), - &(config->current_bar->colors.focused_workspace_bg), - &(config->current_bar->colors.focused_workspace_text) - }; - return parse_three_colors(colors, "focused_workspace", argc, argv); -} - -struct cmd_results *bar_colors_cmd_inactive_workspace(int argc, char **argv) { - char **colors[3] = { - &(config->current_bar->colors.inactive_workspace_border), - &(config->current_bar->colors.inactive_workspace_bg), - &(config->current_bar->colors.inactive_workspace_text) - }; - return parse_three_colors(colors, "inactive_workspace", argc, argv); -} - -struct cmd_results *bar_colors_cmd_separator(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.separator), "separator", argc, argv); -} - -struct cmd_results *bar_colors_cmd_focused_separator(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); -} - -struct cmd_results *bar_colors_cmd_statusline(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.statusline), "statusline", argc, argv); -} - -struct cmd_results *bar_colors_cmd_focused_statusline(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); -} - -struct cmd_results *bar_colors_cmd_urgent_workspace(int argc, char **argv) { - char **colors[3] = { - &(config->current_bar->colors.urgent_workspace_border), - &(config->current_bar->colors.urgent_workspace_bg), - &(config->current_bar->colors.urgent_workspace_text) - }; - return parse_three_colors(colors, "urgent_workspace", argc, argv); -} diff --git a/sway/commands/bar/context_button.c b/sway/commands/bar/context_button.c deleted file mode 100644 index 6d7d7aec..00000000 --- a/sway/commands/bar/context_button.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_context_button(int argc, char **argv) { - const char *cmd_name = "context_button"; -#ifndef ENABLE_TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " - "%s called, but sway was compiled without tray support", - cmd_name, cmd_name); -#else - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); - } - - // User should be able to prefix with 0x or whatever they want - config->current_bar->context_button = strtoul(argv[0], NULL, 0); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -#endif -} diff --git a/sway/commands/bar/font.c b/sway/commands/bar/font.c deleted file mode 100644 index c586c5bc..00000000 --- a/sway/commands/bar/font.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *bar_cmd_font(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "font", "No bar defined."); - } - - char *font = join_args(argv, argc); - free(config->current_bar->font); - if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) { - config->current_bar->font = font; - } else { - config->current_bar->font = font; - } - - sway_log(L_DEBUG, "Settings font '%s' for bar: %s", config->current_bar->font, config->current_bar->id); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/height.c b/sway/commands/bar/height.c deleted file mode 100644 index eb576ab3..00000000 --- a/sway/commands/bar/height.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_height(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "height", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - int height = atoi(argv[0]); - if (height < 0) { - return cmd_results_new(CMD_INVALID, "height", - "Invalid height value: %s", argv[0]); - } - - config->current_bar->height = height; - sway_log(L_DEBUG, "Setting bar height to %d on bar: %s", height, config->current_bar->id); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c deleted file mode 100644 index 0b49aa6b..00000000 --- a/sway/commands/bar/hidden_state.c +++ /dev/null @@ -1,79 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/ipc-server.h" -#include "log.h" - -static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, const char *hidden_state) { - char *old_state = bar->hidden_state; - if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) { - if (strcasecmp("hide", bar->hidden_state) == 0) { - bar->hidden_state = strdup("show"); - } else if (strcasecmp("show", bar->hidden_state) == 0) { - bar->hidden_state = strdup("hide"); - } - } else if (strcasecmp("hide", hidden_state) == 0) { - bar->hidden_state = strdup("hide"); - } else if (strcasecmp("show", hidden_state) == 0) { - bar->hidden_state = strdup("show"); - } else { - return cmd_results_new(CMD_INVALID, "hidden_state", "Invalid value %s", hidden_state); - } - - if (strcmp(old_state, bar->hidden_state) != 0) { - if (!config->reading) { - ipc_event_barconfig_update(bar); - } - sway_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", bar->hidden_state, bar->id); - } - - // free old mode - free(old_state); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) { - return error; - } - if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) { - return error; - } - - if (config->reading && argc > 1) { - return cmd_results_new(CMD_INVALID, "hidden_state", "Unexpected value %s in config mode", argv[1]); - } - - const char *state = argv[0]; - - if (config->reading) { - return bar_set_hidden_state(config->current_bar, state); - } - - const char *id = NULL; - if (argc == 2) { - id = argv[1]; - } - - int i; - struct bar_config *bar; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; - if (id && strcmp(id, bar->id) == 0) { - return bar_set_hidden_state(bar, state); - } - - error = bar_set_hidden_state(bar, state); - if (error) { - return error; - } - } - - // active bar modifiers might have changed. - update_active_bar_modifiers(); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/icon_theme.c b/sway/commands/bar/icon_theme.c deleted file mode 100644 index cbfc0be5..00000000 --- a/sway/commands/bar/icon_theme.c +++ /dev/null @@ -1,25 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" - -struct cmd_results *bar_cmd_icon_theme(int argc, char **argv) { - const char *cmd_name = "tray_output"; -#ifndef ENABLE_TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " - "%s called, but sway was compiled without tray support", - cmd_name, cmd_name); -#else - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); - } - - config->current_bar->icon_theme = strdup(argv[0]); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -#endif -} diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c deleted file mode 100644 index 1221ebf6..00000000 --- a/sway/commands/bar/id.c +++ /dev/null @@ -1,33 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_id(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "id", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - const char *name = argv[0]; - const char *oldname = config->current_bar->id; - - // check if id is used by a previously defined bar - int i; - for (i = 0; i < config->bars->length; ++i) { - struct bar_config *find = config->bars->items[i]; - if (strcmp(name, find->id) == 0 && config->current_bar != find) { - return cmd_results_new(CMD_FAILURE, "id", - "Id '%s' already defined for another bar. Id unchanged (%s).", - name, oldname); - } - } - - sway_log(L_DEBUG, "Renaming bar: '%s' to '%s'", oldname, name); - - // free old bar id - free(config->current_bar->id); - - config->current_bar->id = strdup(name); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c deleted file mode 100644 index 36816b93..00000000 --- a/sway/commands/bar/mode.c +++ /dev/null @@ -1,81 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/ipc-server.h" -#include "log.h" - -static struct cmd_results *bar_set_mode(struct bar_config *bar, const char *mode) { - char *old_mode = bar->mode; - if (strcasecmp("toggle", mode) == 0 && !config->reading) { - if (strcasecmp("dock", bar->mode) == 0) { - bar->mode = strdup("hide"); - } else if (strcasecmp("hide", bar->mode) == 0) { - bar->mode = strdup("dock"); - } - } else if (strcasecmp("dock", mode) == 0) { - bar->mode = strdup("dock"); - } else if (strcasecmp("hide", mode) == 0) { - bar->mode = strdup("hide"); - } else if (strcasecmp("invisible", mode) == 0) { - bar->mode = strdup("invisible"); - } else { - return cmd_results_new(CMD_INVALID, "mode", "Invalid value %s", mode); - } - - if (strcmp(old_mode, bar->mode) != 0) { - if (!config->reading) { - ipc_event_barconfig_update(bar); - - // active bar modifiers might have changed. - update_active_bar_modifiers(); - } - sway_log(L_DEBUG, "Setting mode: '%s' for bar: %s", bar->mode, bar->id); - } - - // free old mode - free(old_mode); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *bar_cmd_mode(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { - return error; - } - if ((error = checkarg(argc, "mode", EXPECTED_LESS_THAN, 3))) { - return error; - } - - if (config->reading && argc > 1) { - return cmd_results_new(CMD_INVALID, "mode", "Unexpected value %s in config mode", argv[1]); - } - - const char *mode = argv[0]; - - if (config->reading) { - return bar_set_mode(config->current_bar, mode); - } - - const char *id = NULL; - if (argc == 2) { - id = argv[1]; - } - - int i; - struct bar_config *bar; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; - if (id && strcmp(id, bar->id) == 0) { - return bar_set_mode(bar, mode); - } - - error = bar_set_mode(bar, mode); - if (error) { - return error; - } - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c deleted file mode 100644 index 153d87e6..00000000 --- a/sway/commands/bar/modifier.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" -#include "util.h" - -struct cmd_results *bar_cmd_modifier(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "modifier", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "modifier", "No bar defined."); - } - - uint32_t mod = 0; - - list_t *split = split_string(argv[0], "+"); - for (int i = 0; i < split->length; ++i) { - uint32_t tmp_mod; - if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) { - mod |= tmp_mod; - continue; - } else { - free_flat_list(split); - return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]); - } - } - free_flat_list(split); - - config->current_bar->modifier = mod; - sway_log(L_DEBUG, "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c deleted file mode 100644 index a5710bc0..00000000 --- a/sway/commands/bar/output.c +++ /dev/null @@ -1,50 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" -#include "list.h" -#include "log.h" - -struct cmd_results *bar_cmd_output(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "output", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "output", "No bar defined."); - } - - const char *output = argv[0]; - list_t *outputs = config->current_bar->outputs; - if (!outputs) { - outputs = create_list(); - config->current_bar->outputs = outputs; - } - - int i; - int add_output = 1; - if (strcmp("*", output) == 0) { - // remove all previous defined outputs and replace with '*' - for (i = 0; i < outputs->length; ++i) { - free(outputs->items[i]); - list_del(outputs, i); - } - } else { - // only add output if not already defined with either the same - // name or as '*' - for (i = 0; i < outputs->length; ++i) { - const char *find = outputs->items[i]; - if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { - add_output = 0; - break; - } - } - } - - if (add_output) { - list_add(outputs, strdup(output)); - sway_log(L_DEBUG, "Adding bar: '%s' to output '%s'", config->current_bar->id, output); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/pango_markup.c b/sway/commands/bar/pango_markup.c deleted file mode 100644 index f69e882f..00000000 --- a/sway/commands/bar/pango_markup.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_pango_markup(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "pango_markup", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "pango_markup", "No bar defined."); - } - - if (strcasecmp("enabled", argv[0]) == 0) { - config->current_bar->pango_markup = true; - sway_log(L_DEBUG, "Enabling pango markup for bar: %s", config->current_bar->id); - } else if (strcasecmp("disabled", argv[0]) == 0) { - config->current_bar->pango_markup = false; - sway_log(L_DEBUG, "Disabling pango markup for bar: %s", config->current_bar->id); - } else { - error = cmd_results_new(CMD_INVALID, "pango_markup", "Invalid value %s", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/position.c b/sway/commands/bar/position.c deleted file mode 100644 index 50de58e2..00000000 --- a/sway/commands/bar/position.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_position(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "position", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "position", "No bar defined."); - } - - if (strcasecmp("top", argv[0]) == 0) { - config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_TOP; - } else if (strcasecmp("bottom", argv[0]) == 0) { - config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; - } else if (strcasecmp("left", argv[0]) == 0) { - sway_log(L_INFO, "Warning: swaybar currently only supports top and bottom positioning. YMMV"); - config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_LEFT; - } else if (strcasecmp("right", argv[0]) == 0) { - sway_log(L_INFO, "Warning: swaybar currently only supports top and bottom positioning. YMMV"); - config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_RIGHT; - } else { - error = cmd_results_new(CMD_INVALID, "position", "Invalid value %s", argv[0]); - return error; - } - - sway_log(L_DEBUG, "Setting bar position '%s' for bar: %s", argv[0], config->current_bar->id); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/secondary_button.c b/sway/commands/bar/secondary_button.c deleted file mode 100644 index 745045c5..00000000 --- a/sway/commands/bar/secondary_button.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_secondary_button(int argc, char **argv) { - const char *cmd_name = "secondary_button"; -#ifndef ENABLE_TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " - "%s called, but sway was compiled without tray support", - cmd_name, cmd_name); -#else - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); - } - - // User should be able to prefix with 0x or whatever they want - config->current_bar->secondary_button = strtoul(argv[0], NULL, 0); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -#endif -} diff --git a/sway/commands/bar/separator_symbol.c b/sway/commands/bar/separator_symbol.c deleted file mode 100644 index 2766d8a2..00000000 --- a/sway/commands/bar/separator_symbol.c +++ /dev/null @@ -1,21 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_separator_symbol(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "separator_symbol", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "separator_symbol", "No bar defined."); - } - - free(config->current_bar->separator_symbol); - config->current_bar->separator_symbol = strdup(argv[0]); - sway_log(L_DEBUG, "Settings separator_symbol '%s' for bar: %s", config->current_bar->separator_symbol, config->current_bar->id); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/status_command.c b/sway/commands/bar/status_command.c deleted file mode 100644 index b227ac47..00000000 --- a/sway/commands/bar/status_command.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *bar_cmd_status_command(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "status_command", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "status_command", "No bar defined."); - } - - free(config->current_bar->status_command); - config->current_bar->status_command = join_args(argv, argc); - sway_log(L_DEBUG, "Feeding bar with status command: %s", config->current_bar->status_command); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/strip_workspace_numbers.c b/sway/commands/bar/strip_workspace_numbers.c deleted file mode 100644 index 9ac32482..00000000 --- a/sway/commands/bar/strip_workspace_numbers.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "strip_workspace_numbers", "No bar defined."); - } - - if (strcasecmp("yes", argv[0]) == 0) { - config->current_bar->strip_workspace_numbers = true; - sway_log(L_DEBUG, "Stripping workspace numbers on bar: %s", config->current_bar->id); - } else if (strcasecmp("no", argv[0]) == 0) { - config->current_bar->strip_workspace_numbers = false; - sway_log(L_DEBUG, "Enabling workspace numbers on bar: %s", config->current_bar->id); - } else { - error = cmd_results_new(CMD_INVALID, "strip_workspace_numbers", "Invalid value %s", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/swaybar_command.c b/sway/commands/bar/swaybar_command.c deleted file mode 100644 index 452e2df5..00000000 --- a/sway/commands/bar/swaybar_command.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *bar_cmd_swaybar_command(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "swaybar_command", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "swaybar_command", "No bar defined."); - } - - free(config->current_bar->swaybar_command); - config->current_bar->swaybar_command = join_args(argv, argc); - sway_log(L_DEBUG, "Using custom swaybar command: %s", config->current_bar->swaybar_command); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/tray_output.c b/sway/commands/bar/tray_output.c deleted file mode 100644 index 012304a9..00000000 --- a/sway/commands/bar/tray_output.c +++ /dev/null @@ -1,29 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" - -struct cmd_results *bar_cmd_tray_output(int argc, char **argv) { - const char *cmd_name = "tray_output"; -#ifndef ENABLE_TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " - "%s called, but sway was compiled without tray support", - cmd_name, cmd_name); -#else - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); - } - - if (strcmp(argv[0], "all") == 0) { - // Default behaviour - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - config->current_bar->tray_output = strdup(argv[0]); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -#endif -} diff --git a/sway/commands/bar/tray_padding.c b/sway/commands/bar/tray_padding.c deleted file mode 100644 index ac0572ce..00000000 --- a/sway/commands/bar/tray_padding.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) { - const char *cmd_name = "tray_padding"; -#ifndef ENABLE_TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command" - "%s called, but sway was compiled without tray support", - cmd_name, cmd_name); -#else - struct cmd_results *error = NULL; - if ((error = checkarg(argc, cmd_name, EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); - } - - if (argc == 1 || (argc == 2 && strcasecmp("px", argv[1]) == 0)) { - char *inv; - uint32_t padding = strtoul(argv[0], &inv, 10); - if (*inv == '\0' || strcasecmp(inv, "px") == 0) { - config->current_bar->tray_padding = padding; - sway_log(L_DEBUG, "Enabling tray padding of %d px on bar: %s", padding, config->current_bar->id); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - } - return cmd_results_new(CMD_FAILURE, cmd_name, - "Expected 'tray_padding [px]'"); -#endif -} diff --git a/sway/commands/bar/workspace_buttons.c b/sway/commands/bar/workspace_buttons.c deleted file mode 100644 index 67dd2d31..00000000 --- a/sway/commands/bar/workspace_buttons.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "workspace_buttons", "No bar defined."); - } - - if (strcasecmp("yes", argv[0]) == 0) { - config->current_bar->workspace_buttons = true; - sway_log(L_DEBUG, "Enabling workspace buttons on bar: %s", config->current_bar->id); - } else if (strcasecmp("no", argv[0]) == 0) { - config->current_bar->workspace_buttons = false; - sway_log(L_DEBUG, "Disabling workspace buttons on bar: %s", config->current_bar->id); - } else { - error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bar/wrap_scroll.c b/sway/commands/bar/wrap_scroll.c deleted file mode 100644 index 4ed1f12a..00000000 --- a/sway/commands/bar/wrap_scroll.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *bar_cmd_wrap_scroll(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "wrap_scroll", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "wrap_scroll", "No bar defined."); - } - - if (strcasecmp("yes", argv[0]) == 0) { - config->current_bar->wrap_scroll = true; - sway_log(L_DEBUG, "Enabling wrap scroll on bar: %s", config->current_bar->id); - } else if (strcasecmp("no", argv[0]) == 0) { - config->current_bar->wrap_scroll = false; - sway_log(L_DEBUG, "Disabling wrap scroll on bar: %s", config->current_bar->id); - } else { - error = cmd_results_new(CMD_INVALID, "wrap_scroll", "Invalid value %s", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/bind.c b/sway/commands/bind.c deleted file mode 100644 index d9ea37b7..00000000 --- a/sway/commands/bind.c +++ /dev/null @@ -1,170 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/input_state.h" -#include "list.h" -#include "log.h" -#include "stringop.h" -#include "util.h" - -int binding_order = 0; - -struct cmd_results *cmd_bindsym(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { - return error; - } - - struct sway_binding *binding = malloc(sizeof(struct sway_binding)); - if (!binding) { - return cmd_results_new(CMD_FAILURE, "bindsym", - "Unable to allocate binding"); - } - binding->keys = create_list(); - binding->modifiers = 0; - binding->release = false; - binding->bindcode = false; - - // Handle --release - if (strcmp("--release", argv[0]) == 0) { - if (argc >= 3) { - binding->release = true; - argv++; - argc--; - } else { - free_sway_binding(binding); - return cmd_results_new(CMD_FAILURE, "bindsym", - "Invalid bindsym command " - "(expected more than 2 arguments, got %d)", argc); - } - } - - binding->command = join_args(argv + 1, argc - 1); - - list_t *split = split_string(argv[0], "+"); - for (int i = 0; i < split->length; ++i) { - // Check for a modifier key - uint32_t mod; - if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { - binding->modifiers |= mod; - continue; - } - // Check for xkb key - xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], - XKB_KEYSYM_CASE_INSENSITIVE); - - // Check for mouse binding - if (strncasecmp(split->items[i], "button", strlen("button")) == 0 && - strlen(split->items[i]) == strlen("button0")) { - sym = ((char *)split->items[i])[strlen("button")] - '1' + M_LEFT_CLICK; - } - if (!sym) { - struct cmd_results *ret = cmd_results_new(CMD_INVALID, "bindsym", - "Unknown key '%s'", (char *)split->items[i]); - free_sway_binding(binding); - free_flat_list(split); - return ret; - } - xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); - if (!key) { - free_sway_binding(binding); - free_flat_list(split); - return cmd_results_new(CMD_FAILURE, "bindsym", - "Unable to allocate binding"); - } - *key = sym; - list_add(binding->keys, key); - } - free_flat_list(split); - - struct sway_mode *mode = config->current_mode; - int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding); - if (i > -1) { - sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]); - struct sway_binding *dup = mode->bindings->items[i]; - free_sway_binding(dup); - list_del(mode->bindings, i); - } - binding->order = binding_order++; - list_add(mode->bindings, binding); - list_qsort(mode->bindings, sway_binding_cmp_qsort); - - sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_bindcode(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "bindcode", EXPECTED_MORE_THAN, 1))) { - return error; - } - - struct sway_binding *binding = malloc(sizeof(struct sway_binding)); - if (!binding) { - return cmd_results_new(CMD_FAILURE, "bindsym", - "Unable to allocate binding"); - } - binding->keys = create_list(); - binding->modifiers = 0; - binding->release = false; - binding->bindcode = true; - - // Handle --release - if (strcmp("--release", argv[0]) == 0) { - if (argc >= 3) { - binding->release = true; - argv++; - argc--; - } else { - free_sway_binding(binding); - return cmd_results_new(CMD_FAILURE, "bindcode", - "Invalid bindcode command " - "(expected more than 2 arguments, got %d)", argc); - } - } - - binding->command = join_args(argv + 1, argc - 1); - - list_t *split = split_string(argv[0], "+"); - for (int i = 0; i < split->length; ++i) { - // Check for a modifier key - uint32_t mod; - if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { - binding->modifiers |= mod; - continue; - } - // parse keycode - xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10); - if (!xkb_keycode_is_legal_ext(keycode)) { - error = cmd_results_new(CMD_INVALID, "bindcode", "Invalid keycode '%s'", (char *)split->items[i]); - free_sway_binding(binding); - list_free(split); - return error; - } - xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t)); - *key = keycode - 8; - list_add(binding->keys, key); - } - free_flat_list(split); - - struct sway_mode *mode = config->current_mode; - int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding); - if (i > -1) { - struct sway_binding *dup = mode->bindings->items[i]; - if (dup->bindcode) { - sway_log(L_DEBUG, "bindcode - '%s' already exists, overwriting", argv[0]); - } else { - sway_log(L_DEBUG, "bindcode - '%s' already exists as bindsym, overwriting", argv[0]); - } - free_sway_binding(dup); - list_del(mode->bindings, i); - } - binding->order = binding_order++; - list_add(mode->bindings, binding); - list_qsort(mode->bindings, sway_binding_cmp_qsort); - - sway_log(L_DEBUG, "bindcode - Bound %s to command %s", argv[0], binding->command); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/border.c b/sway/commands/border.c deleted file mode 100644 index c888622e..00000000 --- a/sway/commands/border.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" - -struct cmd_results *cmd_border(int argc, char **argv) { - struct cmd_results *error = NULL; - if (!config->active) { - return cmd_results_new(CMD_FAILURE, "border", "Can only be used when sway is running."); - } - if ((error = checkarg(argc, "border", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (argc > 2) { - return cmd_results_new(CMD_INVALID, "border", - "Expected 'border []"); - } - - swayc_t *view = current_container; - enum swayc_border_types border = view->border_type; - int thickness = view->border_thickness; - - if (strcasecmp(argv[0], "none") == 0) { - border = B_NONE; - } else if (strcasecmp(argv[0], "normal") == 0) { - border = B_NORMAL; - } else if (strcasecmp(argv[0], "pixel") == 0) { - border = B_PIXEL; - } else if (strcasecmp(argv[0], "toggle") == 0) { - switch (border) { - case B_NONE: - border = B_PIXEL; - break; - case B_NORMAL: - border = B_NONE; - break; - case B_PIXEL: - border = B_NORMAL; - break; - } - } else { - return cmd_results_new(CMD_INVALID, "border", - "Expected 'border "); - } - - if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { - thickness = (int)strtol(argv[1], NULL, 10); - if (errno == ERANGE || thickness < 0) { - errno = 0; - return cmd_results_new(CMD_INVALID, "border", "Number is out of range."); - } - } - - if (view) { - view->border_type = border; - view->border_thickness = thickness; - update_geometry(view); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/client.c b/sway/commands/client.c deleted file mode 100644 index f3d879cd..00000000 --- a/sway/commands/client.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include "sway/commands.h" - -static struct cmd_results *parse_border_color(struct border_colors *border_colors, const char *cmd_name, int argc, char **argv) { - struct cmd_results *error = NULL; - if (argc < 3 || argc > 5) { - return cmd_results_new(CMD_INVALID, cmd_name, "Requires between three and five color values"); - } - - uint32_t *colors[5] = { - &border_colors->border, - &border_colors->background, - &border_colors->text, - &border_colors->indicator, - &border_colors->child_border - }; - int i; - for (i = 0; i < argc; i++) { - char buffer[10]; - error = add_color(cmd_name, buffer, argv[i]); - if (error) { - return error; - } - *colors[i] = strtoul(buffer + 1, NULL, 16); - } - - if (argc < 5) { - border_colors->child_border = border_colors->background; - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_client_focused(int argc, char **argv) { - return parse_border_color(&config->border_colors.focused, "client.focused", argc, argv); -} - -struct cmd_results *cmd_client_focused_inactive(int argc, char **argv) { - return parse_border_color(&config->border_colors.focused_inactive, "client.focused_inactive", argc, argv); -} - -struct cmd_results *cmd_client_unfocused(int argc, char **argv) { - return parse_border_color(&config->border_colors.unfocused, "client.unfocused", argc, argv); -} - -struct cmd_results *cmd_client_urgent(int argc, char **argv) { - return parse_border_color(&config->border_colors.urgent, "client.urgent", argc, argv); -} - -struct cmd_results *cmd_client_placeholder(int argc, char **argv) { - return parse_border_color(&config->border_colors.placeholder, "client.placeholder", argc, argv); -} - -struct cmd_results *cmd_client_background(int argc, char **argv) { - char buffer[10]; - struct cmd_results *error = NULL; - uint32_t background; - - if (argc != 1) { - return cmd_results_new(CMD_INVALID, "client.background", "Requires exactly one color value"); - } - - error = add_color("client.background", buffer, argv[0]); - if (error) { - return error; - } - - background = strtoul(buffer+1, NULL, 16); - config->border_colors.background = background; - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/clipboard.c b/sway/commands/clipboard.c deleted file mode 100644 index 95514e78..00000000 --- a/sway/commands/clipboard.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "stringop.h" - -static void send_clipboard(void *data, const char *type, int fd) { - if (strcmp(type, "text/plain") != 0 - && strcmp(type, "text/plain;charset=utf-8") != 0) { - close(fd); - return; - } - - const char *str = data; - write(fd, str, strlen(str)); - close(fd); -} - -struct cmd_results *cmd_clipboard(int argc, char **argv) { - static char *current_data = NULL; - - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "clipboard", EXPECTED_AT_LEAST, 1))) { - return error; - } - - static const char *types[2] = { - "text/plain", - "text/plain;charset=utf-8" - }; - - char *str = join_args(argv, argc); - wlc_set_selection(str, types, 2, &send_clipboard); - - free(current_data); - current_data = str; - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/commands.c b/sway/commands/commands.c deleted file mode 100644 index 0c64970c..00000000 --- a/sway/commands/commands.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "list.h" -#include "log.h" - -struct cmd_results *cmd_commands(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "commands", EXPECTED_EQUAL_TO, 1))) { - return error; - } - if ((error = check_security_config())) { - return error; - } - - if (strcmp(argv[0], "{") != 0) { - return cmd_results_new(CMD_FAILURE, "commands", "Expected block declaration"); - } - - if (!config->reading) { - return cmd_results_new(CMD_FAILURE, "commands", "Can only be used in config file."); - } - - return cmd_results_new(CMD_BLOCK_COMMANDS, NULL, NULL); -} diff --git a/sway/commands/debuglog.c b/sway/commands/debuglog.c deleted file mode 100644 index 658d6165..00000000 --- a/sway/commands/debuglog.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *cmd_debuglog(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "debuglog", EXPECTED_EQUAL_TO, 1))) { - return error; - } else if (strcasecmp(argv[0], "toggle") == 0) { - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "debuglog toggle", "Can't be used in config file."); - } - if (toggle_debug_logging()) { - sway_log(L_DEBUG, "Debuglog turned on."); - } - } else if (strcasecmp(argv[0], "on") == 0) { - set_log_level(L_DEBUG); - sway_log(L_DEBUG, "Debuglog turned on."); - } else if (strcasecmp(argv[0], "off") == 0) { - reset_log_level(); - } else { - return cmd_results_new(CMD_FAILURE, "debuglog", "Expected 'debuglog on|off|toggle'"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - diff --git a/sway/commands/default_border.c b/sway/commands/default_border.c deleted file mode 100644 index 8fbe8d19..00000000 --- a/sway/commands/default_border.c +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" - -struct cmd_results *cmd_default_border(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "default_border", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (argc > 2) { - return cmd_results_new(CMD_INVALID, "default_border", - "Expected 'default_border []"); - } - - enum swayc_border_types border = config->border; - int thickness = config->border_thickness; - - if (strcasecmp(argv[0], "none") == 0) { - border = B_NONE; - } else if (strcasecmp(argv[0], "normal") == 0) { - border = B_NORMAL; - } else if (strcasecmp(argv[0], "pixel") == 0) { - border = B_PIXEL; - } else { - return cmd_results_new(CMD_INVALID, "default_border", - "Expected 'default_border []"); - } - - if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { - thickness = (int)strtol(argv[1], NULL, 10); - if (errno == ERANGE || thickness < 0) { - errno = 0; - return cmd_results_new(CMD_INVALID, "default_border", "Number is out out of range."); - } - } - - config->border = border; - config->border_thickness = thickness; - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/default_floating_border.c b/sway/commands/default_floating_border.c deleted file mode 100644 index fb48c1c0..00000000 --- a/sway/commands/default_floating_border.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" - -struct cmd_results *cmd_default_floating_border(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "default_floating_border", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (argc > 2) { - return cmd_results_new(CMD_INVALID, "default_floating_border", - "Expected 'default_floating_border []"); - } - - enum swayc_border_types border = config->floating_border; - int thickness = config->floating_border_thickness; - - if (strcasecmp(argv[0], "none") == 0) { - border = B_NONE; - } else if (strcasecmp(argv[0], "normal") == 0) { - border = B_NORMAL; - } else if (strcasecmp(argv[0], "pixel") == 0) { - border = B_PIXEL; - } else { - return cmd_results_new(CMD_INVALID, "default_floating_border", - "Expected 'default_floating_border []"); - } - - if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { - thickness = (int)strtol(argv[1], NULL, 10); - if (errno == ERANGE || thickness < 0) { - errno = 0; - return cmd_results_new(CMD_INVALID, "default_floating_border", - "Number is out out of range."); - } - } - - config->floating_border = border; - config->floating_border_thickness = thickness; - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/exec.c b/sway/commands/exec.c deleted file mode 100644 index 58ef5f94..00000000 --- a/sway/commands/exec.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_exec(int argc, char **argv) { - if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); - if (config->reloading) { - char *args = join_args(argv, argc); - sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args); - free(args); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - return cmd_exec_always(argc, argv); -} - diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c deleted file mode 100644 index ab2d8622..00000000 --- a/sway/commands/exec_always.c +++ /dev/null @@ -1,85 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_exec_always(int argc, char **argv) { - struct cmd_results *error = NULL; - if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL); - if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) { - return error; - } - - char *tmp = NULL; - if (strcmp((char*)*argv, "--no-startup-id") == 0) { - sway_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored."); - if ((error = checkarg(argc - 1, "exec_always", EXPECTED_MORE_THAN, 0))) { - return error; - } - - tmp = join_args(argv + 1, argc - 1); - } else { - tmp = join_args(argv, argc); - } - - // Put argument into cmd array - char cmd[4096]; - strncpy(cmd, tmp, sizeof(cmd)); - cmd[sizeof(cmd) - 1] = 0; - free(tmp); - sway_log(L_DEBUG, "Executing %s", cmd); - - int fd[2]; - if (pipe(fd) != 0) { - sway_log(L_ERROR, "Unable to create pipe for fork"); - } - - pid_t pid; - pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space - if (!child) { - return cmd_results_new(CMD_FAILURE, "exec_always", "Unable to allocate child pid"); - } - // Fork process - if ((pid = fork()) == 0) { - // Fork child process again - setsid(); - if ((*child = fork()) == 0) { - execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL); - /* Not reached */ - } - close(fd[0]); - ssize_t s = 0; - while ((size_t)s < sizeof(pid_t)) { - s += write(fd[1], ((uint8_t *)child) + s, sizeof(pid_t) - s); - } - close(fd[1]); - _exit(0); // Close child process - } else if (pid < 0) { - free(child); - return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed"); - } - close(fd[1]); // close write - ssize_t s = 0; - while ((size_t)s < sizeof(pid_t)) { - s += read(fd[0], ((uint8_t *)child) + s, sizeof(pid_t) - s); - } - close(fd[0]); - // cleanup child process - wait(0); - swayc_t *ws = swayc_active_workspace(); - if (*child > 0 && ws) { - sway_log(L_DEBUG, "Child process created with pid %d for workspace %s", *child, ws->name); - struct pid_workspace *pw = malloc(sizeof(struct pid_workspace)); - pw->pid = child; - pw->workspace = strdup(ws->name); - pid_workspace_add(pw); - } else { - free(child); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/exit.c b/sway/commands/exit.c deleted file mode 100644 index f192f86a..00000000 --- a/sway/commands/exit.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "sway/commands.h" -#include "sway/container.h" - -void sway_terminate(int exit_code); - -struct cmd_results *cmd_exit(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file."); - if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) { - return error; - } - // Close all views - close_views(&root_container); - sway_terminate(EXIT_SUCCESS); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - diff --git a/sway/commands/floating.c b/sway/commands/floating.c deleted file mode 100644 index ccfde532..00000000 --- a/sway/commands/floating.c +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/ipc-server.h" -#include "sway/layout.h" -#include "list.h" -#include "log.h" - -struct cmd_results *cmd_floating(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "floating", "Can't be used in config file."); - if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) { - return error; - } - swayc_t *view = current_container; - bool wants_floating; - if (strcasecmp(argv[0], "enable") == 0) { - wants_floating = true; - } else if (strcasecmp(argv[0], "disable") == 0) { - wants_floating = false; - } else if (strcasecmp(argv[0], "toggle") == 0) { - wants_floating = !view->is_floating; - } else { - return cmd_results_new(CMD_FAILURE, "floating", - "Expected 'floating "); - } - - // Prevent running floating commands on things like workspaces - if (view->type != C_VIEW) { - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - - // Change from nonfloating to floating - if (!view->is_floating && wants_floating) { - // Remove view from its current location - destroy_container(remove_child(view)); - - // and move it into workspace floating - add_floating(swayc_active_workspace(), view); - view->x = (swayc_active_workspace()->width - view->width)/2; - view->y = (swayc_active_workspace()->height - view->height)/2; - if (view->desired_width != -1) { - view->width = view->desired_width; - } - if (view->desired_height != -1) { - view->height = view->desired_height; - } - arrange_windows(swayc_active_workspace(), -1, -1); - - } else if (view->is_floating && !wants_floating) { - // Delete the view from the floating list and unset its is_floating flag - remove_child(view); - view->is_floating = false; - // Get the properly focused container, and add in the view there - swayc_t *focused = container_under_pointer(); - // If focused is null, it's because the currently focused container is a workspace - if (focused == NULL || focused->is_floating) { - focused = swayc_active_workspace(); - } - set_focused_container(focused); - - sway_log(L_DEBUG, "Non-floating focused container is %p", focused); - - // Case of focused workspace, just create as child of it - if (focused->type == C_WORKSPACE) { - add_child(focused, view); - } - // Regular case, create as sibling of current container - else { - add_sibling(focused, view); - } - // Refocus on the view once its been put back into the layout - view->width = view->height = 0; - arrange_windows(swayc_active_workspace(), -1, -1); - remove_view_from_scratchpad(view); - ipc_event_window(view, "floating"); - } - set_focused_container(view); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/floating_maximum_size.c b/sway/commands/floating_maximum_size.c deleted file mode 100644 index 5bca4d7c..00000000 --- a/sway/commands/floating_maximum_size.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *cmd_floating_maximum_size(int argc, char **argv) { - struct cmd_results *error = NULL; - int32_t width; - int32_t height; - char *ptr; - - if ((error = checkarg(argc, "floating_maximum_size", EXPECTED_EQUAL_TO, 3))) { - return error; - } - width = strtol(argv[0], &ptr, 10); - height = strtol(argv[2], &ptr, 10); - - if (width < -1) { - sway_log(L_DEBUG, "floating_maximum_size invalid width value: '%s'", argv[0]); - - } else { - config->floating_maximum_width = width; - - } - - if (height < -1) { - sway_log(L_DEBUG, "floating_maximum_size invalid height value: '%s'", argv[2]); - } - else { - config->floating_maximum_height = height; - - } - - sway_log(L_DEBUG, "New floating_maximum_size: '%d' x '%d'", config->floating_maximum_width, - config->floating_maximum_height); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/floating_minimum_size.c b/sway/commands/floating_minimum_size.c deleted file mode 100644 index ba72454c..00000000 --- a/sway/commands/floating_minimum_size.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *cmd_floating_minimum_size(int argc, char **argv) { - struct cmd_results *error = NULL; - int32_t width; - int32_t height; - char *ptr; - - if ((error = checkarg(argc, "floating_minimum_size", EXPECTED_EQUAL_TO, 3))) { - return error; - } - width = strtol(argv[0], &ptr, 10); - height = strtol(argv[2], &ptr, 10); - - if (width <= 0) { - sway_log(L_DEBUG, "floating_minimum_size invalid width value: '%s'", argv[0]); - - } else { - config->floating_minimum_width = width; - - } - - if (height <= 0) { - sway_log(L_DEBUG, "floating_minimum_size invalid height value: '%s'", argv[2]); - } - else { - config->floating_minimum_height = height; - - } - - sway_log(L_DEBUG, "New floating_minimum_size: '%d' x '%d'", config->floating_minimum_width, - config->floating_minimum_height); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/floating_mod.c b/sway/commands/floating_mod.c deleted file mode 100644 index b8e81ab9..00000000 --- a/sway/commands/floating_mod.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input_state.h" -#include "list.h" -#include "log.h" -#include "stringop.h" -#include "util.h" - -struct cmd_results *cmd_floating_mod(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "floating_modifier", EXPECTED_AT_LEAST, 1))) { - return error; - } - int i; - list_t *split = split_string(argv[0], "+"); - config->floating_mod = 0; - - // set modifier keys - for (i = 0; i < split->length; ++i) { - config->floating_mod |= get_modifier_mask_by_name(split->items[i]); - } - free_flat_list(split); - if (!config->floating_mod) { - error = cmd_results_new(CMD_INVALID, "floating_modifier", "Unknown keys %s", argv[0]); - return error; - } - - if (argc >= 2) { - if (strcasecmp("inverse", argv[1]) == 0) { - config->dragging_key = M_RIGHT_CLICK; - config->resizing_key = M_LEFT_CLICK; - } else if (strcasecmp("normal", argv[1]) == 0) { - config->dragging_key = M_LEFT_CLICK; - config->resizing_key = M_RIGHT_CLICK; - } else { - error = cmd_results_new(CMD_INVALID, "floating_modifier", "Invalid definition %s", argv[1]); - return error; - } - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/floating_scroll.c b/sway/commands/floating_scroll.c deleted file mode 100644 index 8c50c5bd..00000000 --- a/sway/commands/floating_scroll.c +++ /dev/null @@ -1,46 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_floating_scroll(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "floating_scroll", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!strcasecmp("up", argv[0])) { - free(config->floating_scroll_up_cmd); - if (argc < 2) { - config->floating_scroll_up_cmd = strdup(""); - } else { - config->floating_scroll_up_cmd = join_args(argv + 1, argc - 1); - } - } else if (!strcasecmp("down", argv[0])) { - free(config->floating_scroll_down_cmd); - if (argc < 2) { - config->floating_scroll_down_cmd = strdup(""); - } else { - config->floating_scroll_down_cmd = join_args(argv + 1, argc - 1); - } - } else if (!strcasecmp("left", argv[0])) { - free(config->floating_scroll_left_cmd); - if (argc < 2) { - config->floating_scroll_left_cmd = strdup(""); - } else { - config->floating_scroll_left_cmd = join_args(argv + 1, argc - 1); - } - } else if (!strcasecmp("right", argv[0])) { - free(config->floating_scroll_right_cmd); - if (argc < 2) { - config->floating_scroll_right_cmd = strdup(""); - } else { - config->floating_scroll_right_cmd = join_args(argv + 1, argc - 1); - } - } else { - error = cmd_results_new(CMD_INVALID, "floating_scroll", "Unknown command: '%s'", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/focus.c b/sway/commands/focus.c deleted file mode 100644 index c83157b8..00000000 --- a/sway/commands/focus.c +++ /dev/null @@ -1,100 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" -#include "sway/input_state.h" -#include "sway/output.h" -#include "sway/workspace.h" - -struct cmd_results *cmd_focus(int argc, char **argv) { - if (config->reading) return cmd_results_new(CMD_FAILURE, "focus", "Can't be used in config file."); - struct cmd_results *error = NULL; - if (argc > 0 && strcasecmp(argv[0], "output") == 0) { - swayc_t *output = NULL; - struct wlc_point abs_pos; - get_absolute_center_position(get_focused_container(&root_container), &abs_pos); - if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 2))) { - return error; - } else if (!(output = output_by_name(argv[1], &abs_pos))) { - return cmd_results_new(CMD_FAILURE, "focus output", - "Can't find output with name/at direction '%s' @ (%i,%i)", argv[1], abs_pos.x, abs_pos.y); - } else if (!workspace_switch(swayc_active_workspace_for(output))) { - return cmd_results_new(CMD_FAILURE, "focus output", - "Switching to workspace on output '%s' was blocked", argv[1]); - } else if (config->mouse_warping) { - swayc_t *focused = get_focused_view(output); - if (focused && focused->type == C_VIEW) { - center_pointer_on(focused); - } - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } else if (argc == 0) { - set_focused_container(current_container); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) { - return error; - } - static int floating_toggled_index = 0; - static int tiled_toggled_index = 0; - if (strcasecmp(argv[0], "left") == 0) { - move_focus(MOVE_LEFT); - } else if (strcasecmp(argv[0], "right") == 0) { - move_focus(MOVE_RIGHT); - } else if (strcasecmp(argv[0], "up") == 0) { - move_focus(MOVE_UP); - } else if (strcasecmp(argv[0], "down") == 0) { - move_focus(MOVE_DOWN); - } else if (strcasecmp(argv[0], "parent") == 0) { - move_focus(MOVE_PARENT); - } else if (strcasecmp(argv[0], "child") == 0) { - move_focus(MOVE_CHILD); - } else if (strcasecmp(argv[0], "next") == 0) { - move_focus(MOVE_NEXT); - } else if (strcasecmp(argv[0], "prev") == 0) { - move_focus(MOVE_PREV); - } else if (strcasecmp(argv[0], "mode_toggle") == 0) { - int i; - swayc_t *workspace = swayc_active_workspace(); - swayc_t *focused = get_focused_view(workspace); - if (focused->is_floating) { - if (workspace->children->length > 0) { - for (i = 0;i < workspace->floating->length; i++) { - if (workspace->floating->items[i] == focused) { - floating_toggled_index = i; - break; - } - } - if (workspace->children->length > tiled_toggled_index) { - set_focused_container(get_focused_view(workspace->children->items[tiled_toggled_index])); - } else { - set_focused_container(get_focused_view(workspace->children->items[0])); - tiled_toggled_index = 0; - } - } - } else { - if (workspace->floating->length > 0) { - for (i = 0;i < workspace->children->length; i++) { - if (workspace->children->items[i] == focused) { - tiled_toggled_index = i; - break; - } - } - if (workspace->floating->length > floating_toggled_index) { - swayc_t *floating = workspace->floating->items[floating_toggled_index]; - set_focused_container(get_focused_view(floating)); - } else { - swayc_t *floating = workspace->floating->items[workspace->floating->length - 1]; - set_focused_container(get_focused_view(floating)); - tiled_toggled_index = workspace->floating->length - 1; - } - } - } - } else { - return cmd_results_new(CMD_INVALID, "focus", - "Expected 'focus ' or 'focus output '"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/focus_follows_mouse.c b/sway/commands/focus_follows_mouse.c deleted file mode 100644 index 7c9c2b13..00000000 --- a/sway/commands/focus_follows_mouse.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_focus_follows_mouse(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - config->focus_follows_mouse = !strcasecmp(argv[0], "yes"); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/font.c b/sway/commands/font.c deleted file mode 100644 index 32994f8a..00000000 --- a/sway/commands/font.c +++ /dev/null @@ -1,27 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/border.h" -#include "sway/commands.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_font(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) { - return error; - } - - char *font = join_args(argv, argc); - free(config->font); - if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) { - config->font = strdup(font + 6); - free(font); - } else { - config->font = font; - } - - config->font_height = get_font_text_height(config->font); - - sway_log(L_DEBUG, "Settings font %s", config->font); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c deleted file mode 100644 index d1fd1641..00000000 --- a/sway/commands/for_window.c +++ /dev/null @@ -1,41 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" -#include "sway/criteria.h" -#include "list.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_for_window(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) { - return error; - } - // add command to a criteria/command pair that is run against views when they appear. - char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1); - - struct criteria *crit = malloc(sizeof(struct criteria)); - if (!crit) { - return cmd_results_new(CMD_FAILURE, "for_window", "Unable to allocate criteria"); - } - crit->crit_raw = strdup(criteria); - crit->cmdlist = cmdlist; - crit->tokens = create_list(); - char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); - - if (err_str) { - error = cmd_results_new(CMD_INVALID, "for_window", err_str); - free(err_str); - free_criteria(crit); - } else if (crit->tokens->length == 0) { - error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria"); - free_criteria(crit); - } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { - sway_log(L_DEBUG, "for_window: Duplicate, skipping."); - free_criteria(crit); - } else { - sway_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); - list_add(config->criteria, crit); - } - return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/force_focus_wrapping.c b/sway/commands/force_focus_wrapping.c deleted file mode 100644 index f19dd163..00000000 --- a/sway/commands/force_focus_wrapping.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_force_focus_wrapping(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "force_focus_wrapping", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - config->force_focus_wrapping = !strcasecmp(argv[0], "yes"); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/fullscreen.c b/sway/commands/fullscreen.c deleted file mode 100644 index bfff82f9..00000000 --- a/sway/commands/fullscreen.c +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" -#include "sway/ipc-server.h" -#include "sway/layout.h" - -struct cmd_results *cmd_fullscreen(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can only be used when sway is running."); - if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0))) { - return error; - } - swayc_t *container = current_container; - if(container->type != C_VIEW){ - return cmd_results_new(CMD_INVALID, "fullscreen", "Only views can fullscreen"); - } - swayc_t *workspace = swayc_parent_by_type(container, C_WORKSPACE); - bool current = swayc_is_fullscreen(container); - wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current); - - if (container->is_floating) { - if (current) { - // set dimensions back to what they were before we fullscreened this - container->x = container->cached_geometry.origin.x; - container->y = container->cached_geometry.origin.y; - container->width = container->cached_geometry.size.w; - container->height = container->cached_geometry.size.h; - } else { - // cache dimensions so we can reset them after we "unfullscreen" this - struct wlc_geometry geo = { - .origin = { - .x = container->x, - .y = container->y - }, - .size = { - .w = container->width, - .h = container->height - } - }; - container->cached_geometry = geo; - } - } - - // Resize workspace if going from fullscreen -> notfullscreen - // otherwise just resize container - if (!current) { - arrange_windows(workspace, -1, -1); - workspace->fullscreen = container; - } else { - arrange_windows(container, -1, -1); - workspace->fullscreen = NULL; - } - ipc_event_window(container, "fullscreen_mode"); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c deleted file mode 100644 index 0a48592d..00000000 --- a/sway/commands/gaps.c +++ /dev/null @@ -1,166 +0,0 @@ -#include -#include -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" -#include "sway/layout.h" - -struct cmd_results *cmd_gaps(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1))) { - return error; - } - const char *expected_syntax = - "Expected 'gaps edge_gaps ' or " - "'gaps '"; - const char *amount_str = argv[0]; - // gaps amount - if (argc >= 1 && isdigit(*amount_str)) { - int amount = (int)strtol(amount_str, NULL, 10); - if (errno == ERANGE) { - errno = 0; - return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range."); - } - config->gaps_inner = config->gaps_outer = amount; - arrange_windows(&root_container, -1, -1); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - // gaps inner|outer n - else if (argc >= 2 && isdigit((amount_str = argv[1])[0])) { - int amount = (int)strtol(amount_str, NULL, 10); - if (errno == ERANGE) { - errno = 0; - return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range."); - } - const char *target_str = argv[0]; - if (strcasecmp(target_str, "inner") == 0) { - config->gaps_inner = amount; - } else if (strcasecmp(target_str, "outer") == 0) { - config->gaps_outer = amount; - } - arrange_windows(&root_container, -1, -1); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } else if (argc == 2 && strcasecmp(argv[0], "edge_gaps") == 0) { - // gaps edge_gaps - if (strcasecmp(argv[1], "toggle") == 0) { - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "gaps edge_gaps toggle", - "Can't be used in config file."); - } - config->edge_gaps = !config->edge_gaps; - } else { - config->edge_gaps = - (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "on") == 0); - } - arrange_windows(&root_container, -1, -1); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - // gaps inner|outer current|all set|plus|minus n - if (argc < 4 || config->reading) { - return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); - } - // gaps inner|outer ... - const char *inout_str = argv[0]; - enum {INNER, OUTER} inout; - if (strcasecmp(inout_str, "inner") == 0) { - inout = INNER; - } else if (strcasecmp(inout_str, "outer") == 0) { - inout = OUTER; - } else { - return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); - } - - // gaps ... current|all ... - const char *target_str = argv[1]; - enum {CURRENT, WORKSPACE, ALL} target; - if (strcasecmp(target_str, "current") == 0) { - target = CURRENT; - } else if (strcasecmp(target_str, "all") == 0) { - target = ALL; - } else if (strcasecmp(target_str, "workspace") == 0) { - if (inout == OUTER) { - target = CURRENT; - } else { - // Set gap for views in workspace - target = WORKSPACE; - } - } else { - return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); - } - - // gaps ... n - amount_str = argv[3]; - int amount = (int)strtol(amount_str, NULL, 10); - if (errno == ERANGE) { - errno = 0; - return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range."); - } - - // gaps ... set|plus|minus ... - const char *method_str = argv[2]; - enum {SET, ADD} method; - if (strcasecmp(method_str, "set") == 0) { - method = SET; - } else if (strcasecmp(method_str, "plus") == 0) { - method = ADD; - } else if (strcasecmp(method_str, "minus") == 0) { - method = ADD; - amount *= -1; - } else { - return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); - } - - if (target == CURRENT) { - swayc_t *cont; - if (inout == OUTER) { - if ((cont = swayc_active_workspace()) == NULL) { - return cmd_results_new(CMD_FAILURE, "gaps", "There's no active workspace."); - } - } else { - if ((cont = get_focused_view(&root_container))->type != C_VIEW) { - return cmd_results_new(CMD_FAILURE, "gaps", "Currently focused item is not a view."); - } - } - cont->gaps = swayc_gap(cont); - if (method == SET) { - cont->gaps = amount; - } else if ((cont->gaps += amount) < 0) { - cont->gaps = 0; - } - arrange_windows(cont->parent, -1, -1); - } else if (inout == OUTER) { - //resize all workspace. - int i,j; - for (i = 0; i < root_container.children->length; ++i) { - swayc_t *op = root_container.children->items[i]; - for (j = 0; j < op->children->length; ++j) { - swayc_t *ws = op->children->items[j]; - if (method == SET) { - ws->gaps = amount; - } else if ((ws->gaps += amount) < 0) { - ws->gaps = 0; - } - } - } - arrange_windows(&root_container, -1, -1); - } else { - // Resize gaps for all views in workspace - swayc_t *top; - if (target == WORKSPACE) { - if ((top = swayc_active_workspace()) == NULL) { - return cmd_results_new(CMD_FAILURE, "gaps", "There's currently no active workspace."); - } - } else { - top = &root_container; - } - int top_gap = top->gaps; - container_map(top, method == SET ? set_gaps : add_gaps, &amount); - top->gaps = top_gap; - arrange_windows(top, -1, -1); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/hide_edge_borders.c b/sway/commands/hide_edge_borders.c deleted file mode 100644 index ee2a2644..00000000 --- a/sway/commands/hide_edge_borders.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_hide_edge_borders(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "hide_edge_borders", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (strcasecmp(argv[0], "none") == 0) { - config->hide_edge_borders = E_NONE; - } else if (strcasecmp(argv[0], "vertical") == 0) { - config->hide_edge_borders = E_VERTICAL; - } else if (strcasecmp(argv[0], "horizontal") == 0) { - config->hide_edge_borders = E_HORIZONTAL; - } else if (strcasecmp(argv[0], "both") == 0) { - config->hide_edge_borders = E_BOTH; - } else if (strcasecmp(argv[0], "smart") == 0) { - config->hide_edge_borders = E_SMART; - } else { - return cmd_results_new(CMD_INVALID, "hide_edge_borders", - "Expected 'hide_edge_borders '"); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/include.c b/sway/commands/include.c deleted file mode 100644 index 1ba9a10d..00000000 --- a/sway/commands/include.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "sway/commands.h" -#include "sway/config.h" - -struct cmd_results *cmd_include(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "include", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (!load_include_configs(argv[0], config)) { - return cmd_results_new(CMD_INVALID, "include", "Failed to include sub configuration file: %s", argv[0]); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input.c b/sway/commands/input.c deleted file mode 100644 index ad53d272..00000000 --- a/sway/commands/input.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" -#include "log.h" - -struct cmd_results *cmd_input(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) { - return error; - } - - if (config->reading && strcmp("{", argv[1]) == 0) { - current_input_config = new_input_config(argv[0]); - sway_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); - return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); - } - - if (argc > 2) { - int argc_new = argc-2; - char **argv_new = argv+2; - - struct cmd_results *res; - current_input_config = new_input_config(argv[0]); - if (strcasecmp("accel_profile", argv[1]) == 0) { - res = input_cmd_accel_profile(argc_new, argv_new); - } else if (strcasecmp("click_method", argv[1]) == 0) { - res = input_cmd_click_method(argc_new, argv_new); - } else if (strcasecmp("drag_lock", argv[1]) == 0) { - res = input_cmd_drag_lock(argc_new, argv_new); - } else if (strcasecmp("dwt", argv[1]) == 0) { - res = input_cmd_dwt(argc_new, argv_new); - } else if (strcasecmp("events", argv[1]) == 0) { - res = input_cmd_events(argc_new, argv_new); - } else if (strcasecmp("left_handed", argv[1]) == 0) { - res = input_cmd_left_handed(argc_new, argv_new); - } else if (strcasecmp("middle_emulation", argv[1]) == 0) { - res = input_cmd_middle_emulation(argc_new, argv_new); - } else if (strcasecmp("natural_scroll", argv[1]) == 0) { - res = input_cmd_natural_scroll(argc_new, argv_new); - } else if (strcasecmp("pointer_accel", argv[1]) == 0) { - res = input_cmd_pointer_accel(argc_new, argv_new); - } else if (strcasecmp("scroll_method", argv[1]) == 0) { - res = input_cmd_scroll_method(argc_new, argv_new); - } else if (strcasecmp("tap", argv[1]) == 0) { - res = input_cmd_tap(argc_new, argv_new); - } else { - res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); - } - current_input_config = NULL; - return res; - } - - return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); -} diff --git a/sway/commands/input/accel_profile.c b/sway/commands/input/accel_profile.c deleted file mode 100644 index 8288c1ad..00000000 --- a/sway/commands/input/accel_profile.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_accel_profile(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "accel_profile", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "adaptive") == 0) { - new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE; - } else if (strcasecmp(argv[0], "flat") == 0) { - new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT; - } else { - return cmd_results_new(CMD_INVALID, "accel_profile", - "Expected 'accel_profile '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c deleted file mode 100644 index 5e9d3dcb..00000000 --- a/sway/commands/input/click_method.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" -#include "log.h" - -struct cmd_results *input_cmd_click_method(int argc, char **argv) { - sway_log(L_DEBUG, "click_method for device: %d %s", current_input_config==NULL, current_input_config->identifier); - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "click_method", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "none") == 0) { - new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE; - } else if (strcasecmp(argv[0], "button_areas") == 0) { - new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS; - } else if (strcasecmp(argv[0], "clickfinger") == 0) { - new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER; - } else { - return cmd_results_new(CMD_INVALID, "click_method", "Expected 'click_method -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_drag_lock(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "drag_lock", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_ENABLED; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED; - } else { - return cmd_results_new(CMD_INVALID, "drag_lock", "Expected 'drag_lock '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/dwt.c b/sway/commands/input/dwt.c deleted file mode 100644 index 557b2207..00000000 --- a/sway/commands/input/dwt.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_dwt(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->dwt = LIBINPUT_CONFIG_DWT_ENABLED; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->dwt = LIBINPUT_CONFIG_DWT_DISABLED; - } else { - return cmd_results_new(CMD_INVALID, "dwt", "Expected 'dwt '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c deleted file mode 100644 index 9d54287a..00000000 --- a/sway/commands/input/events.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" -#include "log.h" - -struct cmd_results *input_cmd_events(int argc, char **argv) { - sway_log(L_DEBUG, "events for device: %s", current_input_config->identifier); - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "events", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED; - } else if (strcasecmp(argv[0], "disabled_on_external_mouse") == 0) { - new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE; - } else { - return cmd_results_new(CMD_INVALID, "events", "Expected 'events '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/left_handed.c b/sway/commands/input/left_handed.c deleted file mode 100644 index 6c913e70..00000000 --- a/sway/commands/input/left_handed.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_left_handed(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "left_handed", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "left_handed", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->left_handed = 1; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->left_handed = 0; - } else { - return cmd_results_new(CMD_INVALID, "left_handed", "Expected 'left_handed '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/middle_emulation.c b/sway/commands/input/middle_emulation.c deleted file mode 100644 index 33cdd7d6..00000000 --- a/sway/commands/input/middle_emulation.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "middle_emulation", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; - } else { - return cmd_results_new(CMD_INVALID, "middle_emulation", "Expected 'middle_emulation '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/natural_scroll.c b/sway/commands/input/natural_scroll.c deleted file mode 100644 index 7bc8b8d0..00000000 --- a/sway/commands/input/natural_scroll.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "natural_scoll", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->natural_scroll = 1; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->natural_scroll = 0; - } else { - return cmd_results_new(CMD_INVALID, "natural_scroll", "Expected 'natural_scroll '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c deleted file mode 100644 index 94f595d1..00000000 --- a/sway/commands/input/pointer_accel.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "pointer_accel", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - float pointer_accel = atof(argv[0]); - if (pointer_accel < -1 || pointer_accel > 1) { - return cmd_results_new(CMD_INVALID, "pointer_accel", "Input out of range [-1, 1]"); - } - new_config->pointer_accel = pointer_accel; - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/scroll_method.c b/sway/commands/input/scroll_method.c deleted file mode 100644 index 5c6c3d7a..00000000 --- a/sway/commands/input/scroll_method.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" - -struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "scroll_method", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "none") == 0) { - new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; - } else if (strcasecmp(argv[0], "two_finger") == 0) { - new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_2FG; - } else if (strcasecmp(argv[0], "edge") == 0) { - new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE; - } else if (strcasecmp(argv[0], "on_button_down") == 0) { - new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; - } else { - return cmd_results_new(CMD_INVALID, "scroll_method", "Expected 'scroll_method '"); - } - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c deleted file mode 100644 index 9e3ca2af..00000000 --- a/sway/commands/input/tap.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/input.h" -#include "log.h" - -struct cmd_results *input_cmd_tap(int argc, char **argv) { - sway_log(L_DEBUG, "tap for device: %s", current_input_config->identifier); - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "tap", "No input device defined."); - } - struct input_config *new_config = new_input_config(current_input_config->identifier); - - if (strcasecmp(argv[0], "enabled") == 0) { - new_config->tap = LIBINPUT_CONFIG_TAP_ENABLED; - } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->tap = LIBINPUT_CONFIG_TAP_DISABLED; - } else { - return cmd_results_new(CMD_INVALID, "tap", "Expected 'tap '"); - } - - sway_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c deleted file mode 100644 index 0c678961..00000000 --- a/sway/commands/ipc.c +++ /dev/null @@ -1,162 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include "sway/security.h" -#include "sway/commands.h" -#include "sway/config.h" -#include "ipc.h" -#include "log.h" -#include "util.h" - -static struct ipc_policy *current_policy = NULL; - -struct cmd_results *cmd_ipc(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 2))) { - return error; - } - if ((error = check_security_config())) { - return error; - } - - char *program = NULL; - - if (!strcmp(argv[0], "*")) { - program = strdup(argv[0]); - } else if (!(program = resolve_path(argv[0]))) { - return cmd_results_new( - CMD_INVALID, "ipc", "Unable to resolve IPC Policy target."); - } - if (config->reading && strcmp("{", argv[1]) != 0) { - return cmd_results_new(CMD_INVALID, "ipc", - "Expected '{' at start of IPC config definition."); - } - - if (!config->reading) { - return cmd_results_new(CMD_FAILURE, "ipc", "Can only be used in config file."); - } - - current_policy = alloc_ipc_policy(program); - list_add(config->ipc_policies, current_policy); - - free(program); - return cmd_results_new(CMD_BLOCK_IPC, NULL, NULL); -} - -struct cmd_results *cmd_ipc_events(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "events", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (config->reading && strcmp("{", argv[0]) != 0) { - return cmd_results_new(CMD_INVALID, "events", - "Expected '{' at start of IPC event config definition."); - } - - if (!config->reading) { - return cmd_results_new(CMD_FAILURE, "events", "Can only be used in config file."); - } - - return cmd_results_new(CMD_BLOCK_IPC_EVENTS, NULL, NULL); -} - -struct cmd_results *cmd_ipc_cmd(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - bool enabled; - if (strcmp(argv[0], "enabled") == 0) { - enabled = true; - } else if (strcmp(argv[0], "disabled") == 0) { - enabled = false; - } else { - return cmd_results_new(CMD_INVALID, argv[-1], - "Argument must be one of 'enabled' or 'disabled'"); - } - - struct { - char *name; - enum ipc_feature type; - } types[] = { - { "*", IPC_FEATURE_ALL_COMMANDS }, - { "command", IPC_FEATURE_COMMAND }, - { "workspaces", IPC_FEATURE_GET_WORKSPACES }, - { "outputs", IPC_FEATURE_GET_OUTPUTS }, - { "tree", IPC_FEATURE_GET_TREE }, - { "marks", IPC_FEATURE_GET_MARKS }, - { "bar-config", IPC_FEATURE_GET_BAR_CONFIG }, - { "inputs", IPC_FEATURE_GET_INPUTS }, - { "clipboard", IPC_FEATURE_GET_CLIPBOARD }, - }; - - uint32_t type = 0; - - for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { - if (strcmp(types[i].name, argv[-1]) == 0) { - type = types[i].type; - break; - } - } - - if (enabled) { - current_policy->features |= type; - sway_log(L_DEBUG, "Enabled IPC %s feature", argv[-1]); - } else { - current_policy->features &= ~type; - sway_log(L_DEBUG, "Disabled IPC %s feature", argv[-1]); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_ipc_event_cmd(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - bool enabled; - if (strcmp(argv[0], "enabled") == 0) { - enabled = true; - } else if (strcmp(argv[0], "disabled") == 0) { - enabled = false; - } else { - return cmd_results_new(CMD_INVALID, argv[-1], - "Argument must be one of 'enabled' or 'disabled'"); - } - - struct { - char *name; - enum ipc_feature type; - } types[] = { - { "*", IPC_FEATURE_ALL_EVENTS }, - { "workspace", IPC_FEATURE_EVENT_WORKSPACE }, - { "output", IPC_FEATURE_EVENT_OUTPUT }, - { "mode", IPC_FEATURE_EVENT_MODE }, - { "window", IPC_FEATURE_EVENT_WINDOW }, - { "binding", IPC_FEATURE_EVENT_BINDING }, - { "input", IPC_FEATURE_EVENT_INPUT }, - }; - - uint32_t type = 0; - - for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { - if (strcmp(types[i].name, argv[-1]) == 0) { - type = types[i].type; - break; - } - } - - if (enabled) { - current_policy->features |= type; - sway_log(L_DEBUG, "Enabled IPC %s event", argv[-1]); - } else { - current_policy->features &= ~type; - sway_log(L_DEBUG, "Disabled IPC %s event", argv[-1]); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/kill.c b/sway/commands/kill.c deleted file mode 100644 index 742e2b86..00000000 --- a/sway/commands/kill.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" - -struct cmd_results *cmd_kill(int argc, char **argv) { - if (config->reading) return cmd_results_new(CMD_FAILURE, "kill", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "kill", "Can only be used when sway is running."); - - swayc_t *container = current_container; - close_views(container); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/layout.c b/sway/commands/layout.c deleted file mode 100644 index 57a86565..00000000 --- a/sway/commands/layout.c +++ /dev/null @@ -1,196 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/layout.h" - -/** - * handle "layout auto" command group - */ -static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv); - -struct cmd_results *cmd_layout(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "layout", "Can only be used when sway is running."); - if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { - return error; - } - swayc_t *parent = current_container; - if (parent->is_floating) { - return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows"); - } - - while (parent->type == C_VIEW) { - parent = parent->parent; - } - - enum swayc_layouts old_layout = parent->layout; - - if (strcasecmp(argv[0], "default") == 0) { - swayc_change_layout(parent, parent->prev_layout); - if (parent->layout == L_NONE) { - swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); - swayc_change_layout(parent, default_layout(output)); - } - } else { - if (parent->layout != L_TABBED && parent->layout != L_STACKED) { - parent->prev_layout = parent->layout; - } - - if (strcasecmp(argv[0], "tabbed") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_TABBED); - } - - swayc_change_layout(parent, L_TABBED); - } else if (strcasecmp(argv[0], "stacking") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)) { - parent = new_container(parent, L_STACKED); - } - - swayc_change_layout(parent, L_STACKED); - } else if (strcasecmp(argv[0], "splith") == 0) { - swayc_change_layout(parent, L_HORIZ); - } else if (strcasecmp(argv[0], "splitv") == 0) { - swayc_change_layout(parent, L_VERT); - } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { - if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE - || parent->workspace_layout == L_HORIZ)) { - swayc_change_layout(parent, L_VERT); - } else { - swayc_change_layout(parent, L_HORIZ); - } - } else if (strcasecmp(argv[0], "auto") == 0) { - return cmd_layout_auto(parent, argc, argv); - } - } - - update_layout_geometry(parent, old_layout); - update_geometry(parent); - - arrange_windows(parent, parent->width, parent->height); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv) { - // called after checking that argv[0] is auto, so just continue parsing from there - struct cmd_results *error = NULL; - const char *cmd_name = "layout auto"; - const char *set_inc_cmd_name = "layout auto [master|ncol] [set|inc]"; - const char *err_msg = "Allowed arguments are "; - - bool need_layout_update = false; - enum swayc_layouts old_layout = container->layout; - enum swayc_layouts layout = old_layout; - - if ((error = checkarg(argc, "layout auto", EXPECTED_MORE_THAN, 1))) { - return error; - } - - if (strcasecmp(argv[1], "left") == 0) { - layout = L_AUTO_LEFT; - } else if (strcasecmp(argv[1], "right") == 0) { - layout = L_AUTO_RIGHT; - } else if (strcasecmp(argv[1], "top") == 0) { - layout = L_AUTO_TOP; - } else if (strcasecmp(argv[1], "bottom") == 0) { - layout = L_AUTO_BOTTOM; - } else if (strcasecmp(argv[1], "next") == 0) { - if (is_auto_layout(container->layout) && container->layout < L_AUTO_LAST) { - layout = container->layout + 1; - } else { - layout = L_AUTO_FIRST; - } - } else if (strcasecmp(argv[1], "prev") == 0) { - if (is_auto_layout(container->layout) && container->layout > L_AUTO_FIRST) { - layout = container->layout - 1; - } else { - layout = L_AUTO_LAST; - } - } else { - bool is_nmaster; - bool is_set; - if (strcasecmp(argv[1], "master") == 0) { - is_nmaster = true; - } else if (strcasecmp(argv[1], "ncol") == 0) { - is_nmaster = false; - } else { - return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command. %s", - cmd_name, err_msg); - } - if ((error = checkarg(argc, "auto ", EXPECTED_EQUAL_TO, 4))) { - return error; - } - if (strcasecmp(argv[2], "set") == 0) { - is_set = true; - } else if (strcasecmp(argv[2], "inc") == 0) { - is_set = false; - } else { - return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command. %s, " - "Argument must be on of ", - set_inc_cmd_name); - } - char *end; - int n = (int)strtol(argv[3], &end, 10); - if (*end) { - return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " - "(argument must be an integer)", set_inc_cmd_name); - } - if (is_auto_layout(container->layout)) { - int inc = 0; /* difference between current master/ncol and requested value */ - if (is_nmaster) { - if (is_set) { - if (n < 0) { - return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " - "(master must be >= 0)", set_inc_cmd_name); - } - inc = n - (int)container->nb_master; - } else { /* inc command */ - if ((int)container->nb_master + n >= 0) { - inc = n; - } - } - if (inc) { - for (int i = container->nb_master; - i >= 0 && i < container->children->length - && i != (int)container->nb_master + inc;) { - ((swayc_t *)container->children->items[i])->height = -1; - ((swayc_t *)container->children->items[i])->width = -1; - i += inc > 0 ? 1 : -1; - } - container->nb_master += inc; - need_layout_update = true; - } - } else { /* ncol modification */ - if (is_set) { - if (n <= 0) { - return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " - "(ncol must be > 0)", set_inc_cmd_name); - } - inc = n - (int)container->nb_slave_groups; - } else { /* inc command */ - if ((int)container->nb_slave_groups + n > 0) { - inc = n; - } - } - if (inc) { - container->nb_slave_groups += inc; - need_layout_update = true; - } - } - } - } - - if (layout != old_layout) { - swayc_change_layout(container, layout); - update_layout_geometry(container, old_layout); - need_layout_update = true; - } - if (need_layout_update) { - update_geometry(container); - arrange_windows(container, container->width, container->height); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/log_colors.c b/sway/commands/log_colors.c deleted file mode 100644 index 815d1942..00000000 --- a/sway/commands/log_colors.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "log.h" - -struct cmd_results *cmd_log_colors(int argc, char **argv) { - struct cmd_results *error = NULL; - if (!config->reading) return cmd_results_new(CMD_FAILURE, "log_colors", "Can only be used in config file."); - if ((error = checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1))) { - return error; - } - if (strcasecmp(argv[0], "no") == 0) { - sway_log_colors(0); - } else if (strcasecmp(argv[0], "yes") == 0) { - sway_log_colors(1); - } else { - error = cmd_results_new(CMD_FAILURE, "log_colors", - "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/mark.c b/sway/commands/mark.c deleted file mode 100644 index c1d959df..00000000 --- a/sway/commands/mark.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "list.h" -#include "stringop.h" - -static void find_marks_callback(swayc_t *container, void *_mark) { - char *mark = (char *)_mark; - - int index; - if (container->marks && ((index = list_seq_find(container->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1)) { - list_del(container->marks, index); - } -} - -struct cmd_results *cmd_mark(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); - if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) { - return error; - } - - swayc_t *view = current_container; - bool add = false; - bool toggle = false; - - if (strcmp(argv[0], "--add") == 0) { - --argc; ++argv; - add = true; - } else if (strcmp(argv[0], "--replace") == 0) { - --argc; ++argv; - } - - if (argc && strcmp(argv[0], "--toggle") == 0) { - --argc; ++argv; - toggle = true; - } - - if (argc) { - char *mark = join_args(argv, argc); - - // Remove all existing marks of this type - container_map(&root_container, find_marks_callback, mark); - - if (view->marks) { - if (add) { - int index; - if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { - if (toggle) { - free(view->marks->items[index]); - list_del(view->marks, index); - - if (0 == view->marks->length) { - list_free(view->marks); - view->marks = NULL; - } - } - free(mark); - } else { - list_add(view->marks, mark); - } - } else { - if (toggle && list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark) != -1) { - // Delete the list - list_foreach(view->marks, free); - list_free(view->marks); - view->marks = NULL; - } else { - // Delete and replace with a new list - list_foreach(view->marks, free); - list_free(view->marks); - - view->marks = create_list(); - list_add(view->marks, mark); - } - } - } else { - view->marks = create_list(); - list_add(view->marks, mark); - } - } else { - return cmd_results_new(CMD_FAILURE, "mark", - "Expected 'mark [--add|--replace] [--toggle] '"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/mode.c b/sway/commands/mode.c deleted file mode 100644 index d2985c54..00000000 --- a/sway/commands/mode.c +++ /dev/null @@ -1,57 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/ipc-server.h" -#include "list.h" -#include "log.h" - -struct cmd_results *cmd_mode(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { - return error; - } - - const char *mode_name = argv[0]; - bool mode_make = (argc == 2 && strcmp(argv[1], "{") == 0); - if (mode_make) { - if (!config->reading) - return cmd_results_new(CMD_FAILURE, "mode", "Can only be used in config file."); - } - struct sway_mode *mode = NULL; - // Find mode - int i, len = config->modes->length; - for (i = 0; i < len; ++i) { - struct sway_mode *find = config->modes->items[i]; - if (strcasecmp(find->name, mode_name) == 0) { - mode = find; - break; - } - } - // Create mode if it doesn't exist - if (!mode && mode_make) { - mode = malloc(sizeof(struct sway_mode)); - if (!mode) { - return cmd_results_new(CMD_FAILURE, "mode", "Unable to allocate mode"); - } - mode->name = strdup(mode_name); - mode->bindings = create_list(); - list_add(config->modes, mode); - } - if (!mode) { - error = cmd_results_new(CMD_INVALID, "mode", "Unknown mode `%s'", mode_name); - return error; - } - if ((config->reading && mode_make) || (!config->reading && !mode_make)) { - sway_log(L_DEBUG, "Switching to mode `%s'",mode->name); - } - // Set current mode - config->current_mode = mode; - if (!mode_make) { - // trigger IPC mode event - ipc_event_mode(config->current_mode->name); - } - return cmd_results_new(mode_make ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/mouse_warping.c b/sway/commands/mouse_warping.c deleted file mode 100644 index 5596d483..00000000 --- a/sway/commands/mouse_warping.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_mouse_warping(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "mouse_warping", EXPECTED_EQUAL_TO, 1))) { - return error; - } else if (strcasecmp(argv[0], "output") == 0) { - config->mouse_warping = true; - } else if (strcasecmp(argv[0], "none") == 0) { - config->mouse_warping = false; - } else { - return cmd_results_new(CMD_FAILURE, "mouse_warping", "Expected 'mouse_warping output|none'"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/move.c b/sway/commands/move.c deleted file mode 100644 index 52c73e22..00000000 --- a/sway/commands/move.c +++ /dev/null @@ -1,189 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/layout.h" -#include "sway/output.h" -#include "sway/workspace.h" -#include "list.h" -#include "stringop.h" - -struct cmd_results *cmd_move(int argc, char **argv) { - struct cmd_results *error = NULL; - int move_amt = 10; - - if (config->reading) return cmd_results_new(CMD_FAILURE, "move", "Can't be used in config file."); - if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) { - return error; - } - const char* expected_syntax = "Expected 'move <[px] px>' or " - "'move to workspace ' or " - "'move to output ' or " - "'move position mouse'"; - swayc_t *view = current_container; - - if (argc == 2 || (argc == 3 && strcasecmp(argv[2], "px") == 0 )) { - char *inv; - move_amt = (int)strtol(argv[1], &inv, 10); - if (*inv != '\0' && strcasecmp(inv, "px") != 0) { - move_amt = 10; - } - } - - if (strcasecmp(argv[0], "left") == 0) { - move_container(view, MOVE_LEFT, move_amt); - } else if (strcasecmp(argv[0], "right") == 0) { - move_container(view, MOVE_RIGHT, move_amt); - } else if (strcasecmp(argv[0], "up") == 0) { - move_container(view, MOVE_UP, move_amt); - } else if (strcasecmp(argv[0], "down") == 0) { - move_container(view, MOVE_DOWN, move_amt); - } else if (strcasecmp(argv[0], "next") == 0) { - move_container(view, MOVE_NEXT, move_amt); - } else if (strcasecmp(argv[0], "prev") == 0) { - move_container(view, MOVE_PREV, move_amt); - } else if (strcasecmp(argv[0], "first") == 0) { - move_container(view, MOVE_FIRST, move_amt); - } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) { - // "move container ... - if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) { - return error; - } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "workspace") == 0) { - // move container to workspace x - if (view->type == C_WORKSPACE) { - if (!view->children || view->children->length == 0) { - return cmd_results_new(CMD_FAILURE, "move", "Cannot move an empty workspace"); - } - view = new_container(view, view->workspace_layout); - } if (view->type != C_CONTAINER && view->type != C_VIEW) { - return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views."); - } - - swayc_t *ws; - const char *num_name = NULL; - char *ws_name = NULL; - if (argc == 5 && strcasecmp(argv[3], "number") == 0) { - // move "container to workspace number x" - num_name = argv[4]; - ws = workspace_by_number(num_name); - } else { - ws_name = join_args(argv + 3, argc - 3); - ws = workspace_by_name(ws_name); - } - - if (ws == NULL) { - ws = workspace_create(ws_name ? ws_name : num_name); - } - if (ws_name) { - free(ws_name); - } - move_container_to(view, get_focused_container(ws)); - } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "output") == 0) { - // move container to output x - swayc_t *output = NULL; - struct wlc_point abs_pos; - get_absolute_center_position(view, &abs_pos); - if (view->type == C_WORKSPACE) { - if (!view->children || view->children->length == 0) { - return cmd_results_new(CMD_FAILURE, "move", "Cannot move an empty workspace"); - } - view = new_container(view, view->workspace_layout); - } else if (view->type != C_CONTAINER && view->type != C_VIEW) { - return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views."); - } else if (!(output = output_by_name(argv[3], &abs_pos))) { - return cmd_results_new(CMD_FAILURE, "move", - "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y); - } - - swayc_t *container = get_focused_container(output); - if (container->is_floating) { - move_container_to(view, container->parent); - } else { - move_container_to(view, container); - } - } else { - return cmd_results_new(CMD_INVALID, "move", expected_syntax); - } - } else if (strcasecmp(argv[0], "workspace") == 0) { - // move workspace (to output x) - swayc_t *output = NULL; - struct wlc_point abs_pos; - get_absolute_center_position(view, &abs_pos); - if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 4))) { - return error; - } else if (strcasecmp(argv[1], "to") != 0 || strcasecmp(argv[2], "output") != 0) { - return cmd_results_new(CMD_INVALID, "move", expected_syntax); - } else if (!(output = output_by_name(argv[3], &abs_pos))) { - return cmd_results_new(CMD_FAILURE, "move workspace", - "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y); - } - if (view->type == C_WORKSPACE) { - // This probably means we're moving an empty workspace, but - // that's fine. - move_workspace_to(view, output); - } else { - swayc_t *workspace = swayc_parent_by_type(view, C_WORKSPACE); - move_workspace_to(workspace, output); - } - } else if (strcasecmp(argv[0], "scratchpad") == 0 || (strcasecmp(argv[0], "to") == 0 && strcasecmp(argv[1], "scratchpad") == 0)) { - // move scratchpad ... - if (view->type != C_CONTAINER && view->type != C_VIEW) { - return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views."); - } - swayc_t *view = current_container; - int i; - for (i = 0; i < scratchpad->length; i++) { - if (scratchpad->items[i] == view) { - hide_view_in_scratchpad(view); - sp_view = NULL; - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - } - list_add(scratchpad, view); - if (!view->is_floating) { - destroy_container(remove_child(view)); - } else { - remove_child(view); - } - wlc_view_set_mask(view->handle, 0); - arrange_windows(swayc_active_workspace(), -1, -1); - swayc_t *focused = container_under_pointer(); - if (focused == NULL) { - focused = swayc_active_workspace(); - } - set_focused_container(focused); - } else if (strcasecmp(argv[0], "position") == 0) { - if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 2))) { - return error; - } - if (strcasecmp(argv[1], "mouse")) { - return cmd_results_new(CMD_INVALID, "move", expected_syntax); - } - - if (view->is_floating) { - swayc_t *output = swayc_parent_by_type(view, C_OUTPUT); - struct wlc_geometry g; - wlc_view_get_visible_geometry(view->handle, &g); - const struct wlc_size *size = wlc_output_get_resolution(output->handle); - - double x_pos, y_pos; - wlc_pointer_get_position_v2(&x_pos, &y_pos); - - int32_t x = x_pos - g.size.w / 2; - int32_t y = y_pos - g.size.h / 2; - - uint32_t w = size->w - g.size.w; - uint32_t h = size->h - g.size.h; - - view->x = g.origin.x = MIN((int32_t)w, MAX(x, 0)); - view->y = g.origin.y = MIN((int32_t)h, MAX(y, 0)); - - wlc_view_set_geometry(view->handle, 0, &g); - } - } else { - return cmd_results_new(CMD_INVALID, "move", expected_syntax); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/new_float.c b/sway/commands/new_float.c deleted file mode 100644 index d0f96093..00000000 --- a/sway/commands/new_float.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "log.h" -#include "sway/commands.h" - -struct cmd_results *cmd_new_float(int argc, char **argv) { - sway_log(L_INFO, "`new_float` is deprecated and will be removed in the future. " - "Please use `default_floating_border` instead."); - return cmd_default_floating_border(argc, argv); -} diff --git a/sway/commands/new_window.c b/sway/commands/new_window.c deleted file mode 100644 index 574a4527..00000000 --- a/sway/commands/new_window.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "log.h" -#include "sway/commands.h" - -struct cmd_results *cmd_new_window(int argc, char **argv) { - sway_log(L_INFO, "`new_window` is deprecated and will be removed in the future. " - "Please use `default_border` instead."); - return cmd_default_border(argc, argv); -} diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c deleted file mode 100644 index b3b88e5a..00000000 --- a/sway/commands/no_focus.c +++ /dev/null @@ -1,41 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" -#include "sway/criteria.h" -#include "list.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_no_focus(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "no_focus", EXPECTED_EQUAL_TO, 1))) { - return error; - } - // add command to a criteria/command pair that is run against views when they appear. - char *criteria = argv[0]; - - struct criteria *crit = malloc(sizeof(struct criteria)); - if (!crit) { - return cmd_results_new(CMD_FAILURE, "no_focus", "Unable to allocate criteria"); - } - crit->crit_raw = strdup(criteria); - crit->tokens = create_list(); - crit->cmdlist = NULL; - char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); - - if (err_str) { - error = cmd_results_new(CMD_INVALID, "no_focus", err_str); - free(err_str); - free_criteria(crit); - } else if (crit->tokens->length == 0) { - error = cmd_results_new(CMD_INVALID, "no_focus", "Found no name/value pairs in criteria"); - free_criteria(crit); - } else if (list_seq_find(config->no_focus, criteria_cmp, crit) != -1) { - sway_log(L_DEBUG, "no_focus: Duplicate, skipping."); - free_criteria(crit); - } else { - sway_log(L_DEBUG, "no_focus: '%s' added", crit->crit_raw); - list_add(config->no_focus, crit); - } - return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/orientation.c b/sway/commands/orientation.c deleted file mode 100644 index e54b60ee..00000000 --- a/sway/commands/orientation.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_orientation(int argc, char **argv) { - struct cmd_results *error = NULL; - if (!config->reading) return cmd_results_new(CMD_FAILURE, "orientation", "Can only be used in config file."); - if ((error = checkarg(argc, "orientation", EXPECTED_EQUAL_TO, 1))) { - return error; - } - if (strcasecmp(argv[0], "horizontal") == 0) { - config->default_orientation = L_HORIZ; - } else if (strcasecmp(argv[0], "vertical") == 0) { - config->default_orientation = L_VERT; - } else if (strcasecmp(argv[0], "auto") == 0) { - // Do nothing - } else { - return cmd_results_new(CMD_INVALID, "orientation", "Expected 'orientation '"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/output.c b/sway/commands/output.c deleted file mode 100644 index 911391d2..00000000 --- a/sway/commands/output.c +++ /dev/null @@ -1,219 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "list.h" -#include "log.h" -#include "stringop.h" - -static char *bg_options[] = { - "stretch", - "center", - "fill", - "fit", - "tile" -}; - -struct cmd_results *cmd_output(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { - return error; - } - const char *name = argv[0]; - - struct output_config *output = calloc(1, sizeof(struct output_config)); - if (!output) { - return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); - } - output->x = output->y = output->width = output->height = -1; - output->name = strdup(name); - output->enabled = -1; - output->scale = 1; - - // TODO: atoi doesn't handle invalid numbers - - int i; - for (i = 1; i < argc; ++i) { - const char *command = argv[i]; - - if (strcasecmp(command, "disable") == 0) { - output->enabled = 0; - } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); - goto fail; - } - char *res = argv[i]; - char *x = strchr(res, 'x'); - int width = -1, height = -1; - if (x != NULL) { - // Format is 1234x4321 - *x = '\0'; - width = atoi(res); - height = atoi(x + 1); - *x = 'x'; - } else { - // Format is 1234 4321 - width = atoi(res); - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height)."); - goto fail; - } - res = argv[i]; - height = atoi(res); - } - output->width = width; - output->height = height; - } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); - goto fail; - } - char *res = argv[i]; - char *c = strchr(res, ','); - int x = -1, y = -1; - if (c != NULL) { - // Format is 1234,4321 - *c = '\0'; - x = atoi(res); - y = atoi(c + 1); - *c = ','; - } else { - // Format is 1234 4321 - x = atoi(res); - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); - goto fail; - } - res = argv[i]; - y = atoi(res); - } - output->x = x; - output->y = y; - } else if (strcasecmp(command, "scale") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); - goto fail; - } - output->scale = atoi(argv[i]); - } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { - wordexp_t p; - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); - goto fail; - } - if (i + 1 >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); - goto fail; - } - if (strcasecmp(argv[i + 1], "solid_color") == 0) { - output->background = strdup(argv[argc - 2]); - output->background_option = strdup("solid_color"); - } else { - // argv[i+j]=bg_option - bool valid = false; - char *mode; - size_t j; - for (j = 0; j < (size_t) (argc - i); ++j) { - mode = argv[i + j]; - for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { - if (strcasecmp(mode, bg_options[k]) == 0) { - valid = true; - break; - } - } - if (valid) { - break; - } - } - if (!valid) { - error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); - goto fail; - } - - char *src = join_args(argv + i, j); - if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { - error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); - goto fail; - } - free(src); - src = p.we_wordv[0]; - if (config->reading && *src != '/') { - char *conf = strdup(config->current_config); - if (conf) { - char *conf_path = dirname(conf); - src = malloc(strlen(conf_path) + strlen(src) + 2); - if (src) { - sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); - } else { - sway_log(L_ERROR, "Unable to allocate background source"); - } - free(conf); - } else { - sway_log(L_ERROR, "Unable to allocate background source"); - } - } - if (!src || access(src, F_OK) == -1) { - error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); - wordfree(&p); - goto fail; - } - - output->background = strdup(src); - output->background_option = strdup(mode); - if (src != p.we_wordv[0]) { - free(src); - } - wordfree(&p); - - i += j; - } - } - } - - i = list_seq_find(config->output_configs, output_name_cmp, name); - if (i >= 0) { - // merge existing config - struct output_config *oc = config->output_configs->items[i]; - merge_output_config(oc, output); - free_output_config(output); - output = oc; - } else { - list_add(config->output_configs, output); - } - - sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)", - output->name, output->enabled, output->width, - output->height, output->x, output->y, output->scale, - output->background, output->background_option); - - if (output->name) { - // Try to find the output container and apply configuration now. If - // this is during startup then there will be no container and config - // will be applied during normal "new output" event from wlc. - swayc_t *cont = NULL; - for (int i = 0; i < root_container.children->length; ++i) { - cont = root_container.children->items[i]; - if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { - apply_output_config(output, cont); - - if (strcmp(output->name, "*") != 0) { - // stop looking if the output config isn't applicable to all outputs - break; - } - } - } - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - -fail: - free_output_config(output); - return error; -} diff --git a/sway/commands/permit.c b/sway/commands/permit.c deleted file mode 100644 index 7a5e06f7..00000000 --- a/sway/commands/permit.c +++ /dev/null @@ -1,108 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/security.h" -#include "util.h" -#include "log.h" - -static enum secure_feature get_features(int argc, char **argv, - struct cmd_results **error) { - enum secure_feature features = 0; - - struct { - char *name; - enum secure_feature feature; - } feature_names[] = { - { "lock", FEATURE_LOCK }, - { "panel", FEATURE_PANEL }, - { "background", FEATURE_BACKGROUND }, - { "screenshot", FEATURE_SCREENSHOT }, - { "fullscreen", FEATURE_FULLSCREEN }, - { "keyboard", FEATURE_KEYBOARD }, - { "mouse", FEATURE_MOUSE }, - }; - - for (int i = 1; i < argc; ++i) { - size_t j; - for (j = 0; j < sizeof(feature_names) / sizeof(feature_names[0]); ++j) { - if (strcmp(feature_names[j].name, argv[i]) == 0) { - break; - } - } - if (j == sizeof(feature_names) / sizeof(feature_names[0])) { - *error = cmd_results_new(CMD_INVALID, - "permit", "Invalid feature grant %s", argv[i]); - return 0; - } - features |= feature_names[j].feature; - } - return features; -} - -struct cmd_results *cmd_permit(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "permit", EXPECTED_MORE_THAN, 1))) { - return error; - } - if ((error = check_security_config())) { - return error; - } - - bool assign_perms = true; - char *program = NULL; - - if (!strcmp(argv[0], "*")) { - program = strdup(argv[0]); - } else { - program = resolve_path(argv[0]); - } - if (!program) { - sway_assert(program, "Unable to resolve IPC permit target '%s'." - " will issue empty policy", argv[0]); - assign_perms = false; - program = strdup(argv[0]); - } - - struct feature_policy *policy = get_feature_policy(program); - if (policy && assign_perms) { - policy->features |= get_features(argc, argv, &error); - sway_log(L_DEBUG, "Permissions granted to %s for features %d", - policy->program, policy->features); - } - - free(program); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_reject(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "reject", EXPECTED_MORE_THAN, 1))) { - return error; - } - if ((error = check_security_config())) { - return error; - } - - char *program = NULL; - if (!strcmp(argv[0], "*")) { - program = strdup(argv[0]); - } else { - program = resolve_path(argv[0]); - } - if (!program) { - // Punt - sway_log(L_INFO, "Unable to resolve IPC reject target '%s'." - " Will use provided path", argv[0]); - program = strdup(argv[0]); - } - - struct feature_policy *policy = get_feature_policy(program); - policy->features &= ~get_features(argc, argv, &error); - - sway_log(L_DEBUG, "Permissions granted to %s for features %d", - policy->program, policy->features); - - free(program); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/reload.c b/sway/commands/reload.c deleted file mode 100644 index 01fcc5ba..00000000 --- a/sway/commands/reload.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/layout.h" - -struct cmd_results *cmd_reload(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "reload", "Can't be used in config file."); - if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) { - return error; - } - if (!load_main_config(config->current_config, true)) { - return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); - } - - load_swaybars(); - - arrange_windows(&root_container, -1, -1); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/resize.c b/sway/commands/resize.c deleted file mode 100644 index ef52bb07..00000000 --- a/sway/commands/resize.c +++ /dev/null @@ -1,375 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "sway/commands.h" -#include "sway/layout.h" -#include "sway/focus.h" -#include "sway/input_state.h" -#include "sway/handlers.h" -#include "log.h" - -enum resize_dim_types { - RESIZE_DIM_PX, - RESIZE_DIM_PPT, - RESIZE_DIM_DEFAULT, -}; - -static bool set_size_floating(int new_dimension, bool use_width) { - swayc_t *view = current_container; - if (view) { - if (use_width) { - int current_width = view->width; - view->desired_width = new_dimension; - floating_view_sane_size(view); - - int new_x = view->x + (int)(((view->desired_width - current_width) / 2) * -1); - view->width = view->desired_width; - view->x = new_x; - - update_geometry(view); - } else { - int current_height = view->height; - view->desired_height = new_dimension; - floating_view_sane_size(view); - - int new_y = view->y + (int)(((view->desired_height - current_height) / 2) * -1); - view->height = view->desired_height; - view->y = new_y; - - update_geometry(view); - } - - return true; - } - - return false; -} - -static bool resize_floating(int amount, bool use_width) { - swayc_t *view = current_container; - - if (view) { - if (use_width) { - return set_size_floating(view->width + amount, true); - } else { - return set_size_floating(view->height + amount, false); - } - } - - return false; -} - -static bool resize_tiled(int amount, bool use_width) { - swayc_t *container = current_container; - swayc_t *parent = container->parent; - int idx_focused = 0; - bool use_major = false; - size_t nb_before = 0; - size_t nb_after = 0; - - // 1. Identify a container ancestor that will allow the focused child to grow in the requested - // direction. - while (container->parent) { - parent = container->parent; - if ((parent->children && parent->children->length > 1) - && (is_auto_layout(parent->layout) - || (use_width ? parent->layout == L_HORIZ : parent->layout == L_VERT))) { - // check if container has siblings that can provide/absorb the space needed for - // the resize operation. - use_major = use_width - ? parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT - : parent->layout == L_AUTO_TOP || parent->layout == L_AUTO_BOTTOM; - // Note: use_major will be false for L_HORIZ and L_VERT - - idx_focused = index_child(container); - if (idx_focused < 0) { - sway_log(L_ERROR, "Something weird is happening, child container not " - "present in its parent's children list."); - continue; - } - if (use_major) { - nb_before = auto_group_index(parent, idx_focused); - nb_after = auto_group_count(parent) - nb_before - 1; - } else { - nb_before = idx_focused - auto_group_start_index(parent, idx_focused); - nb_after = auto_group_end_index(parent, idx_focused) - idx_focused - 1; - sway_log(L_DEBUG, "+++ focused: %d, start: %d, end: %d, before: %d, after: %d", - idx_focused, - (int)auto_group_start_index(parent, idx_focused), - (int)auto_group_end_index(parent, idx_focused), - (int)nb_before, (int)nb_after); - - } - if (nb_before || nb_after) { - break; - } - } - container = parent; /* continue up the tree to the next ancestor */ - } - if (parent == &root_container) { - return true; - } - sway_log(L_DEBUG, "Found the proper parent: %p. It has %zu before conts, " - "and %zu after conts", parent, nb_before, nb_after); - // 2. Ensure that the resize operation will not make one of the resized containers drop - // below the "sane" size threshold. - bool valid = true; - swayc_t *focused = parent->children->items[idx_focused]; - int start = use_major ? 0 : auto_group_start_index(parent, idx_focused); - int end = use_major ? parent->children->length : auto_group_end_index(parent, idx_focused); - sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end); - for (int i = start; i < end; ) { - swayc_t *sibling = parent->children->items[i]; - double pixels = amount; - bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; - bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; - if (is_before || is_after) { - pixels = -pixels; - pixels /= is_before ? nb_before : nb_after; - if (nb_after != 0 && nb_before != 0) { - pixels /= 2; - } - } - sway_log(L_DEBUG, "Check container %p: width %g vs %d, height %g vs %d", sibling, sibling->width + pixels, min_sane_w, sibling->height + pixels, min_sane_h); - if (use_width ? - sibling->width + pixels < min_sane_w : - sibling->height + pixels < min_sane_h) { - valid = false; - sway_log(L_DEBUG, "Container size no longer sane"); - break; - } - i = use_major ? auto_group_end_index(parent, i) : (i + 1); - sway_log(L_DEBUG, "+++++ check %i", i); - } - // 3. Apply the size change - if (valid) { - for (int i = start; i < end; ) { - int next_i = use_major ? auto_group_end_index(parent, i) : (i + 1); - swayc_t *sibling = parent->children->items[i]; - double pixels = amount; - bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; - bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; - if (is_before || is_after) { - pixels = -pixels; - pixels /= is_before ? nb_before : nb_after; - if (nb_after != 0 && nb_before != 0) { - pixels /= 2; - } - sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after"); - if (use_major) { - for (int j = i; j < next_i; ++j) { - recursive_resize(parent->children->items[j], pixels, - use_width ? - (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : - (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); - } - } else { - recursive_resize(sibling, pixels, - use_width ? - (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : - (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); - } - } else { - if (use_major) { - for (int j = i; j < next_i; ++j) { - recursive_resize(parent->children->items[j], pixels / 2, - use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); - recursive_resize(parent->children->items[j], pixels / 2, - use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); - } - } else { - recursive_resize(sibling, pixels / 2, - use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); - recursive_resize(sibling, pixels / 2, - use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); - } - } - i = next_i; - } - // Recursive resize does not handle positions, let arrange_windows - // take care of that. - arrange_windows(swayc_active_workspace(), -1, -1); - } - return true; -} - -static bool set_size_tiled(int amount, bool use_width) { - int desired; - swayc_t *focused = current_container; - - if (use_width) { - desired = amount - focused->width; - } else { - desired = amount - focused->height; - } - - return resize_tiled(desired, use_width); -} - -static bool set_size(int dimension, bool use_width) { - swayc_t *focused = current_container; - - if (focused) { - if (focused->is_floating) { - return set_size_floating(dimension, use_width); - } else { - return set_size_tiled(dimension, use_width); - } - } - - return false; -} - -static bool resize(int dimension, bool use_width, enum resize_dim_types dim_type) { - swayc_t *focused = current_container; - - // translate "10 ppt" (10%) to appropriate # of pixels in case we need it - float ppt_dim = (float)dimension / 100; - - if (use_width) { - ppt_dim = focused->width * ppt_dim; - } else { - ppt_dim = focused->height * ppt_dim; - } - - if (focused) { - if (focused->is_floating) { - // floating view resize dimensions should default to px, so only - // use ppt if specified - if (dim_type == RESIZE_DIM_PPT) { - dimension = (int)ppt_dim; - } - - return resize_floating(dimension, use_width); - } else { - // tiled view resize dimensions should default to ppt, so only use - // px if specified - if (dim_type != RESIZE_DIM_PX) { - dimension = (int)ppt_dim; - } - - return resize_tiled(dimension, use_width); - } - } - - return false; -} - -static struct cmd_results *cmd_resize_set(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) { - return error; - } - - if (strcasecmp(argv[0], "width") == 0 || strcasecmp(argv[0], "height") == 0) { - // handle `reset set width 100 px height 100 px` syntax, also allows - // specifying only one dimension for a `resize set` - int cmd_num = 0; - int dim; - - while ((cmd_num + 1) < argc) { - dim = (int)strtol(argv[cmd_num + 1], NULL, 10); - if (errno == ERANGE || dim == 0) { - errno = 0; - return cmd_results_new(CMD_INVALID, "resize set", - "Expected 'resize set [px] [ [px]]'"); - } - - if (strcasecmp(argv[cmd_num], "width") == 0) { - set_size(dim, true); - } else if (strcasecmp(argv[cmd_num], "height") == 0) { - set_size(dim, false); - } else { - return cmd_results_new(CMD_INVALID, "resize set", - "Expected 'resize set [px] [ [px]]'"); - } - - cmd_num += 2; - - if (cmd_num < argc && strcasecmp(argv[cmd_num], "px") == 0) { - // if this was `resize set width 400 px height 300 px`, disregard the `px` arg - cmd_num++; - } - } - } else { - // handle `reset set 100 px 100 px` syntax - int width = (int)strtol(argv[0], NULL, 10); - if (errno == ERANGE || width == 0) { - errno = 0; - return cmd_results_new(CMD_INVALID, "resize set", - "Expected 'resize set [px] [px]'"); - } - - int height_arg = 1; - if (strcasecmp(argv[1], "px") == 0) { - height_arg = 2; - } - - int height = (int)strtol(argv[height_arg], NULL, 10); - if (errno == ERANGE || height == 0) { - errno = 0; - return cmd_results_new(CMD_INVALID, "resize set", - "Expected 'resize set [px] [px]'"); - } - - set_size(width, true); - set_size(height, false); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_resize(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "resize", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "resize", "Can only be used when sway is running."); - - if (strcasecmp(argv[0], "set") == 0) { - return cmd_resize_set(argc - 1, &argv[1]); - } - - if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { - return error; - } - - int dim_arg = argc - 1; - - enum resize_dim_types dim_type = RESIZE_DIM_DEFAULT; - if (strcasecmp(argv[dim_arg], "ppt") == 0) { - dim_type = RESIZE_DIM_PPT; - dim_arg--; - } else if (strcasecmp(argv[dim_arg], "px") == 0) { - dim_type = RESIZE_DIM_PX; - dim_arg--; - } - - int amount = (int)strtol(argv[dim_arg], NULL, 10); - if (errno == ERANGE || amount == 0) { - errno = 0; - amount = 10; // this is the default resize dimension used by i3 for both px and ppt - sway_log(L_DEBUG, "Tried to get resize dimension out of '%s' but failed; setting dimension to default %d", - argv[dim_arg], amount); - } - - bool use_width = false; - if (strcasecmp(argv[1], "width") == 0) { - use_width = true; - } else if (strcasecmp(argv[1], "height") != 0) { - return cmd_results_new(CMD_INVALID, "resize", - "Expected 'resize [] [px|ppt]'"); - } - - if (strcasecmp(argv[0], "shrink") == 0) { - amount *= -1; - } else if (strcasecmp(argv[0], "grow") != 0) { - return cmd_results_new(CMD_INVALID, "resize", - "Expected 'resize [] [px|ppt]'"); - } - - resize(amount, use_width, dim_type); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c deleted file mode 100644 index 6c5c92df..00000000 --- a/sway/commands/scratchpad.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" -#include "sway/layout.h" - -static swayc_t *fetch_view_from_scratchpad() { - sp_index = (sp_index + 1) % scratchpad->length; - swayc_t *view = scratchpad->items[sp_index]; - - if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) { - wlc_view_set_output(view->handle, swayc_active_output()->handle); - } - if (!view->is_floating) { - view->width = swayc_active_workspace()->width * 0.5; - view->height = swayc_active_workspace()->height * 0.75; - view->x = (swayc_active_workspace()->width - view->width)/2; - view->y = (swayc_active_workspace()->height - view->height)/2; - } - if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) { - view->x = (swayc_active_workspace()->width - view->width)/2; - } - if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) { - view->y = (swayc_active_workspace()->height - view->height)/2; - } - - add_floating(swayc_active_workspace(), view); - wlc_view_set_mask(view->handle, VISIBLE); - view->visible = true; - arrange_windows(swayc_active_workspace(), -1, -1); - set_focused_container(view); - return view; -} - -struct cmd_results *cmd_scratchpad(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can only be used when sway is running."); - if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) { - if (!sp_view) { - if (current_container) { - // Haxor the scratchpad index if criteria'd - for (int i = 0; i < scratchpad->length; ++i) { - if (scratchpad->items[i] == current_container) { - sp_index = (i - 1) % scratchpad->length; - } - } - } - sp_view = fetch_view_from_scratchpad(); - } else { - if (swayc_active_workspace() != sp_view->parent) { - hide_view_in_scratchpad(sp_view); - if (sp_index == 0) { - sp_index = scratchpad->length; - } - sp_index--; - sp_view = fetch_view_from_scratchpad(); - } else { - hide_view_in_scratchpad(sp_view); - sp_view = NULL; - } - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - return cmd_results_new(CMD_FAILURE, "scratchpad", "Expected 'scratchpad show' when scratchpad is not empty."); -} diff --git a/sway/commands/seamless_mouse.c b/sway/commands/seamless_mouse.c deleted file mode 100644 index 7760e88d..00000000 --- a/sway/commands/seamless_mouse.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_seamless_mouse(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "seamless_mouse", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - config->seamless_mouse = (strcasecmp(argv[0], "on") == 0 || strcasecmp(argv[0], "yes") == 0); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/set.c b/sway/commands/set.c deleted file mode 100644 index 46fc6d38..00000000 --- a/sway/commands/set.c +++ /dev/null @@ -1,61 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "list.h" -#include "stringop.h" - -// sort in order of longest->shortest -static int compare_set_qsort(const void *_l, const void *_r) { - struct sway_variable const *l = *(void **)_l; - struct sway_variable const *r = *(void **)_r; - return strlen(r->name) - strlen(l->name); -} - -struct cmd_results *cmd_set(int argc, char **argv) { - char *tmp; - struct cmd_results *error = NULL; - if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file."); - if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) { - return error; - } - - if (argv[0][0] != '$') { - sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]); - - size_t size = snprintf(NULL, 0, "$%s", argv[0]); - tmp = malloc(size + 1); - if (!tmp) { - return cmd_results_new(CMD_FAILURE, "set", "Not possible to create variable $'%s'", argv[0]); - } - snprintf(tmp, size+1, "$%s", argv[0]); - - argv[0] = tmp; - } - - struct sway_variable *var = NULL; - // Find old variable if it exists - int i; - for (i = 0; i < config->symbols->length; ++i) { - var = config->symbols->items[i]; - if (strcmp(var->name, argv[0]) == 0) { - break; - } - var = NULL; - } - if (var) { - free(var->value); - } else { - var = malloc(sizeof(struct sway_variable)); - if (!var) { - return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable"); - } - var->name = strdup(argv[0]); - list_add(config->symbols, var); - list_qsort(config->symbols, compare_set_qsort); - } - var->value = join_args(argv + 1, argc - 1); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/show_marks.c b/sway/commands/show_marks.c deleted file mode 100644 index ed56d9e5..00000000 --- a/sway/commands/show_marks.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_show_marks(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "show_marks", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - config->show_marks = !strcasecmp(argv[0], "on"); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/smart_gaps.c b/sway/commands/smart_gaps.c deleted file mode 100644 index 815fc501..00000000 --- a/sway/commands/smart_gaps.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_smart_gaps(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "smart_gaps", EXPECTED_EQUAL_TO, 1))) { - return error; - } - - if (strcasecmp(argv[0], "on") == 0) { - config->smart_gaps = true; - } else if (strcasecmp(argv[0], "off") == 0) { - config->smart_gaps = false; - } else { - return cmd_results_new(CMD_INVALID, "smart_gaps", "Expected 'smart_gaps '"); - } - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/split.c b/sway/commands/split.c deleted file mode 100644 index e3045a4f..00000000 --- a/sway/commands/split.c +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include -#include -#include "sway/border.h" -#include "sway/commands.h" -#include "sway/container.h" -#include "sway/focus.h" -#include "sway/layout.h" -#include "log.h" - -static struct cmd_results *_do_split(int argc, char **argv, int layout) { - char *name = layout == L_VERT ? "splitv" : - layout == L_HORIZ ? "splith" : "split"; - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, name, "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, name, "Can only be used when sway is running."); - if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) { - return error; - } - swayc_t *focused = current_container; - - // Case of floating window, don't split - if (focused->is_floating) { - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } - /* Case that focus is on an workspace with 0/1 children.change its layout */ - if (focused->type == C_WORKSPACE && focused->children->length <= 1) { - sway_log(L_DEBUG, "changing workspace layout"); - swayc_change_layout(focused, layout); - } else if (focused->type != C_WORKSPACE && focused->parent->children->length == 1) { - /* Case of no siblings. change parent layout */ - sway_log(L_DEBUG, "changing container layout"); - swayc_change_layout(focused->parent, layout); - } else { - /* regular case where new split container is build around focused container - * or in case of workspace, container inherits its children */ - sway_log(L_DEBUG, "Adding new container around current focused container"); - sway_log(L_INFO, "FOCUSED SIZE: %.f %.f", focused->width, focused->height); - swayc_t *parent = new_container(focused, layout); - set_focused_container(focused); - arrange_windows(parent, -1, -1); - } - - // update container every time - // if it is tabbed/stacked then the title must change - // if the indicator color is different then the border must change - update_container_border(focused); - swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT); - // schedule render to make changes take effect right away, - // otherwise we would have to wait for the view to render, - // which is unpredictable. - wlc_output_schedule_render(output->handle); - - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_split(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "split", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "split", "Can only be used when sway is running."); - if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) { - return error; - } - if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) { - _do_split(argc - 1, argv + 1, L_VERT); - } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) { - _do_split(argc - 1, argv + 1, L_HORIZ); - } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) { - swayc_t *focused = current_container; - if (focused->parent->layout == L_VERT) { - _do_split(argc - 1, argv + 1, L_HORIZ); - } else { - _do_split(argc - 1, argv + 1, L_VERT); - } - } else { - error = cmd_results_new(CMD_FAILURE, "split", - "Invalid split command (expected either horizontal or vertical)."); - return error; - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} - -struct cmd_results *cmd_splitv(int argc, char **argv) { - return _do_split(argc, argv, L_VERT); -} - -struct cmd_results *cmd_splith(int argc, char **argv) { - return _do_split(argc, argv, L_HORIZ); -} - -struct cmd_results *cmd_splitt(int argc, char **argv) { - swayc_t *focused = current_container; - if (focused->parent->layout == L_VERT) { - return _do_split(argc, argv, L_HORIZ); - } else { - return _do_split(argc, argv, L_VERT); - } -} diff --git a/sway/commands/sticky.c b/sway/commands/sticky.c deleted file mode 100644 index 4899e061..00000000 --- a/sway/commands/sticky.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include "sway/commands.h" -#include "sway/focus.h" - -struct cmd_results *cmd_sticky(int argc, char **argv) { - struct cmd_results *error = NULL; - if (config->reading) return cmd_results_new(CMD_FAILURE, "sticky", "Can't be used in config file."); - if (!config->active) return cmd_results_new(CMD_FAILURE, "sticky", "Can only be used when sway is running."); - if ((error = checkarg(argc, "sticky", EXPECTED_EQUAL_TO, 1))) { - return error; - } - char *action = argv[0]; - swayc_t *cont = get_focused_view(&root_container); - if (strcmp(action, "toggle") == 0) { - cont->sticky = !cont->sticky; - } else if (strcmp(action, "enable") == 0) { - cont->sticky = true; - } else if (strcmp(action, "disable") == 0) { - cont->sticky = false; - } else { - return cmd_results_new(CMD_FAILURE, "sticky", - "Expected 'sticky enable|disable|toggle'"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c deleted file mode 100644 index ac213261..00000000 --- a/sway/commands/unmark.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include "sway/commands.h" -#include "list.h" -#include "stringop.h" - -struct cmd_results *cmd_unmark(int argc, char **argv) { - swayc_t *view = current_container; - - if (view->marks) { - if (argc) { - char *mark = join_args(argv, argc); - int index; - if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { - free(view->marks->items[index]); - list_del(view->marks, index); - - if (view->marks->length == 0) { - list_free(view->marks); - view->marks = NULL; - } - } - free(mark); - } else { - list_foreach(view->marks, free); - list_free(view->marks); - view->marks = NULL; - } - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c deleted file mode 100644 index a7839746..00000000 --- a/sway/commands/workspace.c +++ /dev/null @@ -1,92 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include "sway/commands.h" -#include "sway/config.h" -#include "sway/input_state.h" -#include "sway/workspace.h" -#include "list.h" -#include "log.h" -#include "stringop.h" - -struct cmd_results *cmd_workspace(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) { - return error; - } - - int output_location = -1; - - for (int i = 0; i < argc; ++i) { - if (strcasecmp(argv[i], "output") == 0) { - output_location = i; - break; - } - } - if (output_location >= 0) { - if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, output_location + 2))) { - return error; - } - struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); - if (!wso) { - return cmd_results_new(CMD_FAILURE, "workspace output", - "Unable to allocate workspace output"); - } - wso->workspace = join_args(argv, argc - 2); - wso->output = strdup(argv[output_location + 1]); - int i = -1; - if ((i = list_seq_find(config->workspace_outputs, workspace_output_cmp_workspace, wso)) != -1) { - struct workspace_output *old = config->workspace_outputs->items[i]; - free(old); // workspaces can only be assigned to a single output - list_del(config->workspace_outputs, i); - } - sway_log(L_DEBUG, "Assigning workspace %s to output %s", wso->workspace, wso->output); - list_add(config->workspace_outputs, wso); - } else { - if (config->reading || !config->active) { - return cmd_results_new(CMD_DEFER, "workspace", NULL); - } - swayc_t *ws = NULL; - if (strcasecmp(argv[0], "number") == 0) { - if (!(ws = workspace_by_number(argv[1]))) { - char *name = join_args(argv + 1, argc - 1); - ws = workspace_create(name); - free(name); - } - } else if (strcasecmp(argv[0], "next") == 0) { - ws = workspace_next(); - } else if (strcasecmp(argv[0], "prev") == 0) { - ws = workspace_prev(); - } else if (strcasecmp(argv[0], "next_on_output") == 0) { - ws = workspace_output_next(); - } else if (strcasecmp(argv[0], "prev_on_output") == 0) { - ws = workspace_output_prev(); - } else if (strcasecmp(argv[0], "back_and_forth") == 0) { - // if auto_back_and_forth is enabled, workspace_switch will swap - // the workspaces. If we created prev_workspace here, workspace_switch - // would put us back on original workspace. - if (config->auto_back_and_forth) { - ws = swayc_active_workspace(); - } else if (prev_workspace_name && !(ws = workspace_by_name(prev_workspace_name))) { - ws = workspace_create(prev_workspace_name); - } - } else { - char *name = join_args(argv, argc); - if (!(ws = workspace_by_name(name))) { - ws = workspace_create(name); - } - free(name); - } - swayc_t *old_output = swayc_active_output(); - workspace_switch(ws); - swayc_t *new_output = swayc_active_output(); - - if (config->mouse_warping && old_output != new_output) { - swayc_t *focused = get_focused_view(ws); - if (focused && focused->type == C_VIEW) { - center_pointer_on(focused); - } - } - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/workspace_auto_back_and_forth.c b/sway/commands/workspace_auto_back_and_forth.c deleted file mode 100644 index d58ae5c8..00000000 --- a/sway/commands/workspace_auto_back_and_forth.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_ws_auto_back_and_forth(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1))) { - return error; - } - if (strcasecmp(argv[0], "yes") == 0) { - config->auto_back_and_forth = true; - } else if (strcasecmp(argv[0], "no") == 0) { - config->auto_back_and_forth = false; - } else { - return cmd_results_new(CMD_INVALID, "workspace_auto_back_and_forth", "Expected 'workspace_auto_back_and_forth '"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/workspace_layout.c b/sway/commands/workspace_layout.c deleted file mode 100644 index 9ac84be2..00000000 --- a/sway/commands/workspace_layout.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include "sway/commands.h" - -struct cmd_results *cmd_workspace_layout(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "workspace_layout", EXPECTED_AT_LEAST, 1))) { - return error; - } - - if (strcasecmp(argv[0], "default") == 0) { - config->default_layout = L_NONE; - } else if (strcasecmp(argv[0], "stacking") == 0) { - config->default_layout = L_STACKED; - } else if (strcasecmp(argv[0], "tabbed") == 0) { - config->default_layout = L_TABBED; - } else if (strcasecmp(argv[0], "auto") == 0) { - if (argc == 1) { - config->default_layout = L_AUTO_FIRST; - } else { - if ((error = checkarg(argc, "workspace_layout auto", EXPECTED_EQUAL_TO, 2))) { - return error; - } - if (strcasecmp(argv[1], "left") == 0) { - config->default_layout = L_AUTO_LEFT; - } else if (strcasecmp(argv[1], "right") == 0) { - config->default_layout = L_AUTO_RIGHT; - } else if (strcasecmp(argv[1], "top") == 0) { - config->default_layout = L_AUTO_TOP; - } else if (strcasecmp(argv[1], "bottom") == 0) { - config->default_layout = L_AUTO_BOTTOM; - } else { - return cmd_results_new(CMD_INVALID, "workspace_layout auto", "Expected 'workspace_layout auto '"); - } - } - } else { - return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); - } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/config.c b/sway/config.c deleted file mode 100644 index aa40c49a..00000000 --- a/sway/config.c +++ /dev/null @@ -1,1449 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#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 deleted file mode 100644 index d1eafae8..00000000 --- a/sway/debug_log.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "log.h" -#include "sway.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#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 index 6d0bebc5..6ddcac3b 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -4,11 +4,9 @@ #include #include #include -#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 index e29b46d7..04f3f42c 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -1,49 +1,10 @@ #include #include #include -#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/handlers.c b/sway/handlers.c deleted file mode 100644 index 33e75d6b..00000000 --- a/sway/handlers.c +++ /dev/null @@ -1,1143 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "sway/handlers.h" -#include "sway/border.h" -#include "sway/layout.h" -#include "sway/config.h" -#include "sway/commands.h" -#include "sway/workspace.h" -#include "sway/container.h" -#include "sway/output.h" -#include "sway/focus.h" -#include "sway/input_state.h" -#include "sway/extensions.h" -#include "sway/criteria.h" -#include "sway/ipc-server.h" -#include "sway/input.h" -#include "sway/security.h" -#include "list.h" -#include "stringop.h" -#include "log.h" - -// Event should be sent to client -#define EVENT_PASSTHROUGH false - -// Event handled by sway and should not be sent to client -#define EVENT_HANDLED true - -static struct panel_config *if_panel_find_config(struct wl_client *client) { - int i; - for (i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *config = desktop_shell.panels->items[i]; - if (config->client == client) { - return config; - } - } - return NULL; -} - -static struct background_config *if_background_find_config(struct wl_client *client) { - int i; - for (i = 0; i < desktop_shell.backgrounds->length; i++) { - struct background_config *config = desktop_shell.backgrounds->items[i]; - if (config->client == client) { - return config; - } - } - return NULL; -} - -static struct wlc_geometry compute_panel_geometry(struct panel_config *config) { - struct wlc_size resolution; - output_get_scaled_size(config->output, &resolution); - const struct wlc_geometry *old = wlc_view_get_geometry(config->handle); - struct wlc_geometry new; - - switch (config->panel_position) { - case DESKTOP_SHELL_PANEL_POSITION_TOP: - new.origin.x = 0; - new.origin.y = 0; - new.size.w = resolution.w; - new.size.h = old->size.h; - break; - case DESKTOP_SHELL_PANEL_POSITION_BOTTOM: - new.origin.x = 0; - new.origin.y = resolution.h - old->size.h; - new.size.w = resolution.w; - new.size.h = old->size.h; - break; - case DESKTOP_SHELL_PANEL_POSITION_LEFT: - new.origin.x = 0; - new.origin.y = 0; - new.size.w = old->size.w; - new.size.h = resolution.h; - break; - case DESKTOP_SHELL_PANEL_POSITION_RIGHT: - new.origin.x = resolution.w - old->size.w; - new.origin.y = 0; - new.size.w = old->size.w; - new.size.h = resolution.h; - break; - } - - return new; -} - -static void update_panel_geometry(struct panel_config *config) { - struct wlc_geometry geometry = compute_panel_geometry(config); - wlc_view_set_geometry(config->handle, 0, &geometry); -} - -static void update_panel_geometries(wlc_handle output) { - for (int i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *config = desktop_shell.panels->items[i]; - if (config->output == output) { - update_panel_geometry(config); - } - } -} - -static void update_background_geometry(struct background_config *config) { - struct wlc_geometry geometry = wlc_geometry_zero; - output_get_scaled_size(config->output, &geometry.size); - wlc_view_set_geometry(config->handle, 0, &geometry); -} - -static void update_background_geometries(wlc_handle output) { - for (int i = 0; i < desktop_shell.backgrounds->length; i++) { - struct background_config *config = desktop_shell.backgrounds->items[i]; - if (config->output == output) { - update_background_geometry(config); - } - } -} - -/* Handles */ - -static bool handle_input_created(struct libinput_device *device) { - const char *identifier = libinput_dev_unique_id(device); - if (!identifier) { - sway_log(L_ERROR, "Unable to allocate unique name for input device %p", - device); - return true; - } - sway_log(L_INFO, "Found input device (%s)", identifier); - - list_add(input_devices, device); - - struct input_config *ic = NULL; - int i; - for (i = 0; i < config->input_configs->length; ++i) { - struct input_config *cur = config->input_configs->items[i]; - if (strcasecmp(identifier, cur->identifier) == 0) { - sway_log(L_DEBUG, "Matched input config for %s", - identifier); - ic = cur; - break; - } - if (strcasecmp("*", cur->identifier) == 0) { - sway_log(L_DEBUG, "Matched wildcard input config for %s", - identifier); - ic = cur; - break; - } - } - - apply_input_config(ic, device); - return true; -} - -static void handle_input_destroyed(struct libinput_device *device) { - int i; - list_t *list = input_devices; - for (i = 0; i < list->length; ++i) { - if(((struct libinput_device *)list->items[i]) == device) { - list_del(list, i); - break; - } - } -} - -static bool handle_output_created(wlc_handle output) { - swayc_t *op = new_output(output); - - // Visibility mask to be able to make view invisible - wlc_output_set_mask(output, VISIBLE); - - if (!op) { - return false; - } - - // Switch to workspace if we need to - if (swayc_active_workspace() == NULL) { - swayc_t *ws = op->children->items[0]; - workspace_switch(ws); - } - - // Fixes issues with backgrounds and wlc - wlc_handle prev = wlc_get_focused_output(); - wlc_output_focus(output); - wlc_output_focus(prev); - return true; -} - -static void handle_output_destroyed(wlc_handle output) { - int i; - list_t *list = root_container.children; - for (i = 0; i < list->length; ++i) { - if (((swayc_t *)list->items[i])->handle == output) { - break; - } - } - if (i < list->length) { - destroy_output(list->items[i]); - } else { - return; - } - if (list->length > 0) { - // switch to other outputs active workspace - workspace_switch(((swayc_t *)root_container.children->items[0])->focused); - } -} - -static void handle_output_post_render(wlc_handle output) { - ipc_get_pixels(output); -} - -static void handle_view_pre_render(wlc_handle view) { - render_view_borders(view); -} - -static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { - sway_log(L_DEBUG, "Output %u resolution changed to %d x %d", (unsigned int)output, to->w, to->h); - - swayc_t *c = swayc_by_handle(output); - if (!c) { - return; - } - c->width = to->w; - c->height = to->h; - - update_panel_geometries(output); - update_background_geometries(output); - - arrange_windows(&root_container, -1, -1); -} - -static void handle_output_focused(wlc_handle output, bool focus) { - swayc_t *c = swayc_by_handle(output); - // if for some reason this output doesn't exist, create it. - if (!c) { - handle_output_created(output); - } - if (focus) { - set_focused_container(get_focused_container(c)); - } -} - -static void 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--; - } - } - } - } -} - -static void positioner_place_window(wlc_handle handle) { - const struct wlc_geometry *anchor = wlc_view_positioner_get_anchor_rect(handle); - const struct wlc_size *sr = wlc_view_positioner_get_size(handle); - // a positioner is required to have a non-null anchor and a non-negative size - if (!anchor || !sr || - sr->w <= 0 || sr->h <= 0 || - anchor->size.w <= 0 || anchor->size.h <= 0) { - return; - } - const struct wlc_point offset = *wlc_view_positioner_get_offset(handle); - enum wlc_positioner_anchor_bit anchors = wlc_view_positioner_get_anchor(handle); - enum wlc_positioner_gravity_bit gravity = wlc_view_positioner_get_gravity(handle); - struct wlc_geometry geo = { - .origin = offset, - .size = *sr - }; - - if (anchors & WLC_BIT_ANCHOR_TOP) { - geo.origin.y += anchor->origin.y; - } else if (anchors & WLC_BIT_ANCHOR_BOTTOM) { - geo.origin.y += anchor->origin.y + anchor->size.h; - } else { - geo.origin.y += anchor->origin.y + anchor->size.h / 2; - } - if (anchors & WLC_BIT_ANCHOR_LEFT) { - geo.origin.x += anchor->origin.x; - } else if (anchors & WLC_BIT_ANCHOR_RIGHT) { - geo.origin.x += anchor->origin.x + anchor->size.w; - } else { - geo.origin.x += anchor->origin.x + anchor->size.w / 2; - } - - if (gravity & WLC_BIT_GRAVITY_TOP) { - geo.origin.y -= geo.size.h; - } else if (gravity & WLC_BIT_GRAVITY_BOTTOM) { - /* default */ - } else { - geo.origin.y -= geo.size.h / 2; - } - if (gravity & WLC_BIT_GRAVITY_LEFT) { - geo.origin.x -= geo.size.w; - } else if (gravity & WLC_BIT_GRAVITY_RIGHT) { - /* default */ - } else { - geo.origin.x -= geo.size.w / 2; - } - - sway_log(L_DEBUG, "xdg-positioner: placing window %" PRIuPTR " " - "sized (%u,%u) offset by (%d,%d), " - "anchor rectangle sized (%u,%u) at (%d,%d), " - "anchor edges: %s %s, gravity: %s %s", - handle, - sr->w, sr->h, offset.x, offset.y, - anchor->size.w, anchor->size.h, anchor->origin.x, anchor->origin.y, - anchors & WLC_BIT_ANCHOR_TOP ? "top" : - (anchors & WLC_BIT_ANCHOR_BOTTOM ? "bottom" : "middle"), - anchors & WLC_BIT_ANCHOR_LEFT ? "left" : - (anchors & WLC_BIT_ANCHOR_RIGHT ? "right" : "center"), - gravity & WLC_BIT_GRAVITY_TOP ? "top" : - (gravity & WLC_BIT_GRAVITY_BOTTOM ? "bottom" : "middle"), - gravity & WLC_BIT_GRAVITY_LEFT ? "left" : - (gravity & WLC_BIT_GRAVITY_RIGHT ? "right" : "center")); - - wlc_handle parent = wlc_view_get_parent(handle); - if (parent) { - const struct wlc_geometry *pg = wlc_view_get_geometry(parent); - geo.origin.x += pg->origin.x; - geo.origin.y += pg->origin.y; - } - wlc_view_set_geometry(handle, 0, &geo); -} - -static bool handle_view_created(wlc_handle handle) { - // if view is child of another view, the use that as focused container - wlc_handle parent = wlc_view_get_parent(handle); - swayc_t *focused = NULL; - swayc_t *newview = NULL; - swayc_t *current_ws = swayc_active_workspace(); - bool return_to_workspace = false; - struct wl_client *client = wlc_view_get_wl_client(handle); - struct wl_resource *resource = wlc_surface_get_wl_resource( - wlc_view_get_surface(handle)); - pid_t pid; - struct panel_config *panel_config = NULL; - struct background_config *background_config = NULL; - - panel_config = if_panel_find_config(client); - if (panel_config) { - panel_config->handle = handle; - update_panel_geometry(panel_config); - wlc_view_set_mask(handle, VISIBLE); - wlc_view_set_output(handle, panel_config->output); - wlc_view_bring_to_front(handle); - arrange_windows(&root_container, -1, -1); - return true; - } - - background_config = if_background_find_config(client); - if (background_config) { - background_config->handle = handle; - update_background_geometry(background_config); - wlc_view_set_mask(handle, VISIBLE); - wlc_view_set_output(handle, background_config->output); - wlc_view_send_to_back(handle); - return true; - } - - // Get parent container, to add view in - if (parent) { - focused = swayc_by_handle(parent); - } - - if (client) { - pid = wlc_view_get_pid(handle); - - if (pid) { - // using newview as a temp storage location here, - // rather than adding yet another workspace var - newview = workspace_for_pid(pid); - if (newview) { - focused = get_focused_container(newview); - return_to_workspace = true; - } - newview = NULL; - } - } - - swayc_t *prev_focus = get_focused_container(&root_container); - - if (!focused || focused->type == C_OUTPUT) { - focused = prev_focus; - // Move focus from floating view - if (focused->is_floating) { - // To workspace if there are no children - if (focused->parent->children->length == 0) { - focused = focused->parent; - } - // TODO find a better way of doing this - // Or to focused container - else { - focused = get_focused_container(focused->parent->children->items[0]); - } - } - } - - positioner_place_window(handle); - - sway_log(L_DEBUG, "handle:%" PRIuPTR " type:%x state:%x parent:%" PRIuPTR " " - "mask:%d (x:%d y:%d w:%d h:%d) title:%s " - "class:%s appid:%s", - handle, wlc_view_get_type(handle), wlc_view_get_state(handle), parent, - wlc_view_get_mask(handle), wlc_view_get_geometry(handle)->origin.x, - wlc_view_get_geometry(handle)->origin.y,wlc_view_get_geometry(handle)->size.w, - wlc_view_get_geometry(handle)->size.h, wlc_view_get_title(handle), - wlc_view_get_class(handle), wlc_view_get_app_id(handle)); - - // TODO properly figure out how each window should be handled. - switch (wlc_view_get_type(handle)) { - // regular view created regularly - case 0: - if (parent) { - newview = new_floating_view(handle); - } else { - newview = new_view(focused, handle); - wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); - } - break; - - // Dmenu keeps viewfocus, but others with this flag don't, for now simulate - // dmenu - case WLC_BIT_OVERRIDE_REDIRECT: - wlc_view_focus(handle); - wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); - wlc_view_bring_to_front(handle); - break; - - // Firefox popups have this flag set. - case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: - wlc_view_bring_to_front(handle); - locked_container_focus = true; - break; - - // Modals, get focus, popups do not - case WLC_BIT_MODAL: - wlc_view_focus(handle); - wlc_view_bring_to_front(handle); - newview = new_floating_view(handle); - /* fallthrough */ - case WLC_BIT_POPUP: - wlc_view_bring_to_front(handle); - break; - } - - // Prevent current ws from being destroyed, if empty - suspend_workspace_cleanup = true; - - if (newview) { - ipc_event_window(newview, "new"); - set_focused_container(newview); - wlc_view_set_mask(handle, VISIBLE); - swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT); - arrange_windows(output, -1, -1); - // check if it matches for_window in config and execute if so - list_t *criteria = criteria_for(newview); - for (int i = 0; i < criteria->length; i++) { - struct criteria *crit = criteria->items[i]; - sway_log(L_DEBUG, "for_window '%s' matches new view %p, cmd: '%s'", - crit->crit_raw, newview, crit->cmdlist); - struct cmd_results *res = handle_command(crit->cmdlist, CONTEXT_CRITERIA); - if (res->status != CMD_SUCCESS) { - sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); - } - free_cmd_results(res); - // view must be focused for commands to affect it, so always - // refocus in-between command lists - set_focused_container(newview); - } - swayc_t *workspace = swayc_parent_by_type(focused, C_WORKSPACE); - if (workspace && workspace->fullscreen) { - set_focused_container(workspace->fullscreen); - } - for (int i = 0; i < decoration_state.csd_resources->length; ++i) { - struct wl_resource *res = decoration_state.csd_resources->items[i]; - if (res == resource) { - list_del(decoration_state.csd_resources, i); - server_decoration_enable_csd(handle); - break; - } - } - } else { - swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT); - wlc_handle *h = malloc(sizeof(wlc_handle)); - if (!h) { - sway_log(L_ERROR, "Unable to allocate window handle, view handler bailing out"); - return true; - } - *h = handle; - sway_log(L_DEBUG, "Adding unmanaged window %p to %p", h, output->unmanaged); - list_add(output->unmanaged, h); - wlc_view_set_mask(handle, VISIBLE); - } - - if (return_to_workspace && current_ws != swayc_active_workspace()) { - // we were on one workspace, switched to another to add this view, - // now let's return to where we were - workspace_switch(current_ws); - set_focused_container(get_focused_container(current_ws)); - } - if (prev_focus && prev_focus->type == C_VIEW - && newview && criteria_any(newview, config->no_focus)) { - // Restore focus - swayc_t *ws = swayc_parent_by_type(newview, C_WORKSPACE); - if (!ws || ws != newview->parent - || ws->children->length + ws->floating->length != 1) { - sway_log(L_DEBUG, "no_focus: restoring focus to %s", prev_focus->name); - set_focused_container(prev_focus); - } - } - - suspend_workspace_cleanup = false; - ws_cleanup(); - return true; -} - -static void handle_view_destroyed(wlc_handle handle) { - sway_log(L_DEBUG, "Destroying window %" PRIuPTR, handle); - swayc_t *view = swayc_by_handle(handle); - - // destroy views by type - switch (wlc_view_get_type(handle)) { - // regular view created regularly - case 0: - case WLC_BIT_MODAL: - case WLC_BIT_POPUP: - break; - // DMENU has this flag, and takes view_focus, but other things with this - // flag don't - case WLC_BIT_OVERRIDE_REDIRECT: - break; - case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: - locked_container_focus = false; - break; - } - - if (view) { - bool fullscreen = swayc_is_fullscreen(view); - remove_view_from_scratchpad(view); - swayc_t *parent = destroy_view(view), *iter = NULL; - if (parent) { - ipc_event_window(parent, "close"); - - // Destroy empty workspaces - if (parent->type == C_WORKSPACE && - parent->children->length == 0 && - parent->floating->length == 0 && - swayc_active_workspace() != parent && - !parent->visible) { - parent = destroy_workspace(parent); - } - - if (fullscreen) { - iter = parent; - while (iter) { - if (iter->fullscreen) { - iter->fullscreen = NULL; - break; - } - iter = iter->parent; - } - } - - - arrange_windows(iter ? iter : parent, -1, -1); - } - } else { - // Is it unmanaged? - int i; - for (i = 0; i < root_container.children->length; ++i) { - swayc_t *output = root_container.children->items[i]; - int j; - for (j = 0; j < output->unmanaged->length; ++j) { - wlc_handle *_handle = output->unmanaged->items[j]; - if (*_handle == handle) { - list_del(output->unmanaged, j); - free(_handle); - break; - } - } - } - // Is it in the scratchpad? - for (i = 0; i < scratchpad->length; ++i) { - swayc_t *item = scratchpad->items[i]; - if (item->handle == handle) { - list_del(scratchpad, i); - destroy_view(item); - break; - } - } - } - set_focused_container(get_focused_view(&root_container)); -} - -static void handle_view_focus(wlc_handle view, bool focus) { - return; -} - -static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry *geometry) { - sway_log(L_DEBUG, "geometry request for %" PRIuPTR " %dx%d @ %d,%d", handle, - geometry->size.w, geometry->size.h, geometry->origin.x, geometry->origin.y); - // If the view is floating, then apply the geometry. - // Otherwise save the desired width/height for the view. - // This will not do anything for the time being as WLC improperly sends geometry requests - swayc_t *view = swayc_by_handle(handle); - if (view) { - view->desired_width = geometry->size.w; - view->desired_height = geometry->size.h; - - if (view->is_floating) { - floating_view_sane_size(view); - view->width = view->desired_width; - view->height = view->desired_height; - view->x = geometry->origin.x; - view->y = geometry->origin.y; - update_geometry(view); - } - } else { - wlc_view_set_geometry(handle, 0, geometry); - } -} - -static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) { - swayc_t *c = swayc_by_handle(view); - pid_t pid = wlc_view_get_pid(view); - switch (state) { - case WLC_BIT_FULLSCREEN: - if (!(get_feature_policy_mask(pid) & FEATURE_FULLSCREEN)) { - sway_log(L_INFO, "Denying fullscreen to %d (%s)", pid, c->name); - break; - } - // i3 just lets it become fullscreen - wlc_view_set_state(view, state, toggle); - if (c) { - sway_log(L_DEBUG, "setting view %" PRIuPTR " %s, fullscreen %d", view, c->name, toggle); - arrange_windows(c->parent, -1, -1); - // Set it as focused window for that workspace if its going fullscreen - swayc_t *ws = swayc_parent_by_type(c, C_WORKSPACE); - if (toggle) { - // Set ws focus to c - set_focused_container_for(ws, c); - ws->fullscreen = c; - } else { - ws->fullscreen = NULL; - } - } - break; - case WLC_BIT_MAXIMIZED: - case WLC_BIT_RESIZING: - case WLC_BIT_MOVING: - break; - case WLC_BIT_ACTIVATED: - sway_log(L_DEBUG, "View %p requested to be activated", c); - break; - } - return; -} - -static void handle_view_properties_updated(wlc_handle view, uint32_t mask) { - if (mask == WLC_BIT_PROPERTY_TITLE) { - swayc_t *c = swayc_by_handle(view); - if (!c) { - return; - } - - // update window title - const char *new_name = wlc_view_get_title(view); - - if (new_name) { - if (!c->name || strcmp(c->name, new_name) != 0) { - free(c->name); - c->name = strdup(new_name); - swayc_t *p = swayc_tabbed_stacked_ancestor(c); - if (p) { - // TODO: we only got the topmost tabbed/stacked container, update borders of all containers on the path - update_container_border(get_focused_view(p)); - } else if (c->border_type == B_NORMAL) { - update_container_border(c); - } - ipc_event_window(c, "title"); - } - } - } -} - -static void handle_binding_command(struct sway_binding *binding) { - struct sway_binding *binding_copy = binding; - bool reload = false; - // if this is a reload command we need to make a duplicate of the - // binding since it will be gone after the reload has completed. - if (strcasecmp(binding->command, "reload") == 0) { - binding_copy = sway_binding_dup(binding); - if (!binding_copy) { - sway_log(L_ERROR, "Unable to duplicate binding during reload"); - return; - } - reload = true; - } - - struct cmd_results *res = handle_command(binding->command, CONTEXT_BINDING); - if (res->status != CMD_SUCCESS) { - sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); - } - ipc_event_binding_keyboard(binding_copy); - - if (reload) { // free the binding if we made a copy - free_sway_binding(binding_copy); - } - - free_cmd_results(res); -} - -static bool handle_bindsym(struct sway_binding *binding, uint32_t keysym, uint32_t keycode) { - int i; - for (i = 0; i < binding->keys->length; ++i) { - if (binding->bindcode) { - xkb_keycode_t *key = binding->keys->items[i]; - if (keycode == *key) { - handle_binding_command(binding); - return true; - } - } else { - xkb_keysym_t *key = binding->keys->items[i]; - if (keysym == *key) { - handle_binding_command(binding); - return true; - } - } - } - - return false; -} - -static bool valid_bindsym(struct sway_binding *binding) { - bool match = false; - int i; - for (i = 0; i < binding->keys->length; ++i) { - if (binding->bindcode) { - xkb_keycode_t *key = binding->keys->items[i]; - if ((match = check_key(0, *key)) == false) { - break; - } - } else { - xkb_keysym_t *key = binding->keys->items[i]; - if ((match = check_key(*key, 0)) == false) { - break; - } - } - } - - return match; -} - -static bool handle_bindsym_release(struct sway_binding *binding) { - if (binding->keys->length == 1) { - xkb_keysym_t *key = binding->keys->items[0]; - if (check_released_key(*key)) { - handle_binding_command(binding); - return true; - } - } - - return false; -} - -static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, - uint32_t key, enum wlc_key_state state) { - - if (desktop_shell.is_locked) { - return EVENT_PASSTHROUGH; - } - - // reset pointer mode on keypress - if (state == WLC_KEY_STATE_PRESSED && pointer_state.mode) { - pointer_mode_reset(); - } - - struct sway_mode *mode = config->current_mode; - - struct wlc_modifiers no_mods = { 0, 0 }; - uint32_t sym = tolower(wlc_keyboard_get_keysym_for_key(key, &no_mods)); - - int i; - - if (state == WLC_KEY_STATE_PRESSED) { - press_key(sym, key); - } else { // WLC_KEY_STATE_RELEASED - release_key(sym, key); - } - - // handle bar modifiers pressed/released - uint32_t modifier; - for (i = 0; i < config->active_bar_modifiers->length; ++i) { - modifier = *(uint32_t *)config->active_bar_modifiers->items[i]; - - switch (modifier_state_changed(modifiers->mods, modifier)) { - case MOD_STATE_PRESSED: - ipc_event_modifier(modifier, "pressed"); - break; - case MOD_STATE_RELEASED: - ipc_event_modifier(modifier, "released"); - break; - } - } - // update modifiers state - modifiers_state_update(modifiers->mods); - - // handle bindings - list_t *candidates = create_list(); - for (i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = mode->bindings->items[i]; - if ((modifiers->mods ^ binding->modifiers) == 0) { - switch (state) { - case WLC_KEY_STATE_PRESSED: - if (!binding->release && valid_bindsym(binding)) { - list_add(candidates, binding); - } - break; - case WLC_KEY_STATE_RELEASED: - if (binding->release && handle_bindsym_release(binding)) { - list_free(candidates); - return EVENT_HANDLED; - } - break; - } - } - } - - for (i = 0; i < candidates->length; ++i) { - struct sway_binding *binding = candidates->items[i]; - if (state == WLC_KEY_STATE_PRESSED) { - if (!binding->release && handle_bindsym(binding, sym, key)) { - list_free(candidates); - return EVENT_HANDLED; - } - } - } - - list_free(candidates); - - swayc_t *focused = get_focused_container(&root_container); - if (focused->type == C_VIEW) { - pid_t pid = wlc_view_get_pid(focused->handle); - if (!(get_feature_policy_mask(pid) & FEATURE_KEYBOARD)) { - return EVENT_HANDLED; - } - } - return EVENT_PASSTHROUGH; -} - -static bool handle_pointer_motion(wlc_handle handle, uint32_t time, double x, double y) { - if (desktop_shell.is_locked) { - return EVENT_PASSTHROUGH; - } - - double new_x = x; - double new_y = y; - // Switch to adjacent output if touching output edge. - // - // Since this doesn't currently support moving windows between outputs we - // don't do the switch if the pointer is in a mode. - if (config->seamless_mouse && !pointer_state.mode && - !pointer_state.left.held && !pointer_state.right.held && !pointer_state.scroll.held) { - - swayc_t *output = swayc_active_output(), *adjacent = NULL; - struct wlc_point abs_pos = { .x = x + output->x, .y = y + output->y }; - if (x <= 0) { // Left edge - if ((adjacent = swayc_adjacent_output(output, MOVE_LEFT, &abs_pos, false))) { - if (workspace_switch(swayc_active_workspace_for(adjacent))) { - new_x = adjacent->width; - // adjust for differently aligned outputs (well, this is - // only correct when the two outputs have the same - // resolution or the same dpi I guess, it should take - // physical attributes into account) - new_y += (output->y - adjacent->y); - } - } - } else if (x >= output->width) { // Right edge - if ((adjacent = swayc_adjacent_output(output, MOVE_RIGHT, &abs_pos, false))) { - if (workspace_switch(swayc_active_workspace_for(adjacent))) { - new_x = 0; - new_y += (output->y - adjacent->y); - } - } - } else if (y <= 0) { // Top edge - if ((adjacent = swayc_adjacent_output(output, MOVE_UP, &abs_pos, false))) { - if (workspace_switch(swayc_active_workspace_for(adjacent))) { - new_y = adjacent->height; - new_x += (output->x - adjacent->x); - } - } - } else if (y >= output->height) { // Bottom edge - if ((adjacent = swayc_adjacent_output(output, MOVE_DOWN, &abs_pos, false))) { - if (workspace_switch(swayc_active_workspace_for(adjacent))) { - new_y = 0; - new_x += (output->x - adjacent->x); - } - } - } - } - - pointer_position_set(new_x, new_y, false); - - swayc_t *focused = get_focused_container(&root_container); - if (focused->type == C_VIEW) { - pid_t pid = wlc_view_get_pid(focused->handle); - if (!(get_feature_policy_mask(pid) & FEATURE_MOUSE)) { - return EVENT_HANDLED; - } - } - - return EVENT_PASSTHROUGH; -} - -static bool swayc_border_check(swayc_t *c, const void *_origin) { - const struct wlc_point *origin = _origin; - const struct wlc_geometry title_bar = c->title_bar_geometry; - - if (c->border_type != B_NORMAL) { - return false; - } - - if (origin->x >= title_bar.origin.x && origin->y >= title_bar.origin.y - && origin->x < title_bar.origin.x + (int32_t)title_bar.size.w - && origin->y < title_bar.origin.y + (int32_t)title_bar.size.h) { - return true; - } - return false; -} - -static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, - uint32_t button, enum wlc_button_state state, const struct wlc_point *origin) { - - // Update view pointer is on - pointer_state.view = container_under_pointer(); - - struct sway_mode *mode = config->current_mode; - // handle bindings - for (int i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = mode->bindings->items[i]; - if ((modifiers->mods ^ binding->modifiers) == 0) { - switch (state) { - case WLC_BUTTON_STATE_PRESSED: - if (!binding->release && handle_bindsym(binding, button, 0)) { - return EVENT_HANDLED; - } - break; - case WLC_BUTTON_STATE_RELEASED: - if (binding->release && handle_bindsym(binding, button, 0)) { - return EVENT_HANDLED; - } - break; - } - } - } - - // Update pointer_state - switch (button) { - case M_LEFT_CLICK: - if (state == WLC_BUTTON_STATE_PRESSED) { - pointer_state.left.held = true; - pointer_state.left.x = origin->x; - pointer_state.left.y = origin->y; - pointer_state.left.view = pointer_state.view; - } else { - pointer_state.left.held = false; - } - break; - - case M_RIGHT_CLICK: - if (state == WLC_BUTTON_STATE_PRESSED) { - pointer_state.right.held = true; - pointer_state.right.x = origin->x; - pointer_state.right.y = origin->y; - pointer_state.right.view = pointer_state.view; - } else { - pointer_state.right.held = false; - } - break; - - case M_SCROLL_CLICK: - if (state == WLC_BUTTON_STATE_PRESSED) { - pointer_state.scroll.held = true; - pointer_state.scroll.x = origin->x; - pointer_state.scroll.y = origin->y; - pointer_state.scroll.view = pointer_state.view; - } else { - pointer_state.scroll.held = false; - } - break; - - //TODO scrolling behavior - case M_SCROLL_UP: - case M_SCROLL_DOWN: - break; - } - - // get focused window and check if to change focus on mouse click - swayc_t *focused = get_focused_container(&root_container); - - // don't change focus or mode if fullscreen - if (swayc_is_fullscreen(focused)) { - if (focused->type == C_VIEW) { - pid_t pid = wlc_view_get_pid(focused->handle); - if (!(get_feature_policy_mask(pid) & FEATURE_MOUSE)) { - return EVENT_HANDLED; - } - } - return EVENT_PASSTHROUGH; - } - - // set pointer mode only if floating mod has been set - if (config->floating_mod) { - pointer_mode_set(button, !(modifiers->mods ^ config->floating_mod)); - } - - // Check whether to change focus - swayc_t *pointer = pointer_state.view; - if (pointer) { - swayc_t *ws = swayc_parent_by_type(focused, C_WORKSPACE); - if (ws != NULL) { - swayc_t *find = container_find(ws, &swayc_border_check, origin); - if (find != NULL) { - set_focused_container(find); - return EVENT_HANDLED; - } - } - - if (focused != pointer) { - set_focused_container(pointer_state.view); - } - // Send to front if floating - if (pointer->is_floating) { - int i; - for (i = 0; i < pointer->parent->floating->length; i++) { - if (pointer->parent->floating->items[i] == pointer) { - list_del(pointer->parent->floating, i); - list_add(pointer->parent->floating, pointer); - break; - } - } - wlc_view_bring_to_front(pointer->handle); - } - } - - // Return if mode has been set - if (pointer_state.mode) { - return EVENT_HANDLED; - } - - if (focused->type == C_VIEW) { - pid_t pid = wlc_view_get_pid(focused->handle); - if (!(get_feature_policy_mask(pid) & FEATURE_MOUSE)) { - return EVENT_HANDLED; - } - } - - // Always send mouse release - if (state == WLC_BUTTON_STATE_RELEASED) { - return EVENT_PASSTHROUGH; - } - - // Finally send click - return EVENT_PASSTHROUGH; -} - -bool handle_pointer_scroll(wlc_handle view, uint32_t time, const struct wlc_modifiers* modifiers, - uint8_t axis_bits, double _amount[2]) { - if (!(modifiers->mods ^ config->floating_mod)) { - int x_amount = (int)_amount[0]; - int y_amount = (int)_amount[1]; - - if (x_amount > 0 && strcmp(config->floating_scroll_up_cmd, "")) { - handle_command(config->floating_scroll_up_cmd, CONTEXT_BINDING); - return EVENT_HANDLED; - } else if (x_amount < 0 && strcmp(config->floating_scroll_down_cmd, "")) { - handle_command(config->floating_scroll_down_cmd, CONTEXT_BINDING); - return EVENT_HANDLED; - } - - if (y_amount > 0 && strcmp(config->floating_scroll_right_cmd, "")) { - handle_command(config->floating_scroll_right_cmd, CONTEXT_BINDING); - return EVENT_HANDLED; - } else if (y_amount < 0 && strcmp(config->floating_scroll_left_cmd, "")) { - handle_command(config->floating_scroll_left_cmd, CONTEXT_BINDING); - return EVENT_HANDLED; - } - } - return EVENT_PASSTHROUGH; -} - -static void handle_wlc_ready(void) { - sway_log(L_DEBUG, "Compositor is ready, executing cmds in queue"); - // Execute commands until there are none left - config->active = true; - while (config->cmd_queue->length) { - char *line = config->cmd_queue->items[0]; - struct cmd_results *res = handle_command(line, CONTEXT_CONFIG); - if (res->status != CMD_SUCCESS) { - sway_log(L_ERROR, "Error on line '%s': %s", line, res->error); - } - free_cmd_results(res); - free(line); - list_del(config->cmd_queue, 0); - } -} - -void register_wlc_handlers() { - wlc_set_output_created_cb(handle_output_created); - wlc_set_output_destroyed_cb(handle_output_destroyed); - wlc_set_output_resolution_cb(handle_output_resolution_change); - wlc_set_output_focus_cb(handle_output_focused); - wlc_set_output_render_post_cb(handle_output_post_render); - wlc_set_view_created_cb(handle_view_created); - wlc_set_view_destroyed_cb(handle_view_destroyed); - wlc_set_view_focus_cb(handle_view_focus); - wlc_set_view_render_pre_cb(handle_view_pre_render); - wlc_set_view_request_geometry_cb(handle_view_geometry_request); - wlc_set_view_request_state_cb(handle_view_state_request); - wlc_set_view_properties_updated_cb(handle_view_properties_updated); - wlc_set_keyboard_key_cb(handle_key); - wlc_set_pointer_motion_cb_v2(handle_pointer_motion); - wlc_set_pointer_button_cb(handle_pointer_button); - wlc_set_pointer_scroll_cb(handle_pointer_scroll); - wlc_set_compositor_ready_cb(handle_wlc_ready); - wlc_set_input_created_cb(handle_input_created); - wlc_set_input_destroyed_cb(handle_input_destroyed); -} diff --git a/sway/input_state.c b/sway/input_state.c deleted file mode 100644 index 04aafd37..00000000 --- a/sway/input_state.c +++ /dev/null @@ -1,490 +0,0 @@ -#include -#include -#include -#include "sway/input_state.h" -#include "sway/config.h" -#include "log.h" - -#define KEY_STATE_MAX_LENGTH 64 - -struct key_state { - /* - * Aims to store state regardless of modifiers. - * If you press a key, then hold shift, then release the key, we'll - * get two different key syms, but the same key code. This handles - * that scenario and makes sure we can use the right bindings. - */ - uint32_t key_sym; - uint32_t alt_sym; - uint32_t key_code; -}; - -static struct key_state key_state_array[KEY_STATE_MAX_LENGTH]; - -static struct key_state last_released; - -static uint32_t modifiers_state; - -void input_init(void) { - int i; - for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { - struct key_state none = { 0, 0, 0 }; - key_state_array[i] = none; - } - - struct key_state none = { 0, 0, 0 }; - last_released = none; - - modifiers_state = 0; -} - -uint32_t modifier_state_changed(uint32_t new_state, uint32_t mod) { - if ((new_state & mod) != 0) { // pressed - if ((modifiers_state & mod) != 0) { // already pressed - return MOD_STATE_UNCHANGED; - } else { // pressed - return MOD_STATE_PRESSED; - } - } else { // not pressed - if ((modifiers_state & mod) != 0) { // released - return MOD_STATE_RELEASED; - } else { // already released - return MOD_STATE_UNCHANGED; - } - } -} - -void modifiers_state_update(uint32_t new_state) { - modifiers_state = new_state; -} - -static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) { - int i; - for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { - if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) { - break; - } - if (key_sym != 0 && (key_state_array[i].key_sym == key_sym - || key_state_array[i].alt_sym == key_sym)) { - break; - } - if (update && key_state_array[i].key_code == key_code) { - key_state_array[i].alt_sym = key_sym; - break; - } - if (key_sym == 0 && key_code != 0 && key_state_array[i].key_code == key_code) { - break; - } - } - return i; -} - -bool check_key(uint32_t key_sym, uint32_t key_code) { - return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH; -} - -bool check_released_key(uint32_t key_sym) { - return (key_sym != 0 - && (last_released.key_sym == key_sym - || last_released.alt_sym == key_sym)); -} - -void press_key(uint32_t key_sym, uint32_t key_code) { - if (key_code == 0) { - return; - } - // Check if key exists - if (!check_key(key_sym, key_code)) { - // Check that we don't exceed buffer length - int insert = find_key(0, 0, true); - if (insert < KEY_STATE_MAX_LENGTH) { - key_state_array[insert].key_sym = key_sym; - key_state_array[insert].key_code = key_code; - } - } -} - -void release_key(uint32_t key_sym, uint32_t key_code) { - uint8_t index = find_key(key_sym, key_code, true); - if (index < KEY_STATE_MAX_LENGTH) { - last_released.key_sym = key_state_array[index].key_sym; - last_released.alt_sym = key_state_array[index].alt_sym; - last_released.key_code = key_state_array[index].key_code; - struct key_state none = { 0, 0, 0 }; - key_state_array[index] = none; - } -} - -// Pointer state and mode - -struct pointer_state pointer_state; - -static struct mode_state { - // initial view state - double x, y, w, h; - swayc_t *ptr; - // Containers used for resizing horizontally - struct { - double w; - swayc_t *ptr; - struct { - double w; - swayc_t *ptr; - } parent; - } horiz; - // Containers used for resizing vertically - struct { - double h; - swayc_t *ptr; - struct { - double h; - swayc_t *ptr; - } parent; - } vert; -} initial; - -static struct { - bool left; - bool top; -} lock; - -// initial set/unset - -static void set_initial_view(swayc_t *view) { - initial.ptr = view; - initial.x = view->x; - initial.y = view->y; - initial.w = view->width; - initial.h = view->height; -} - -static void set_initial_sibling(void) { - bool reset = true; - swayc_t *ws = swayc_active_workspace_for(initial.ptr); - if ((initial.horiz.ptr = get_swayc_in_direction_under(initial.ptr, - lock.left ? MOVE_RIGHT: MOVE_LEFT, ws))) { - initial.horiz.w = initial.horiz.ptr->width; - initial.horiz.parent.ptr = get_swayc_in_direction_under(initial.horiz.ptr, - lock.left ? MOVE_LEFT : MOVE_RIGHT, ws); - initial.horiz.parent.w = initial.horiz.parent.ptr->width; - reset = false; - } - if ((initial.vert.ptr = get_swayc_in_direction_under(initial.ptr, - lock.top ? MOVE_DOWN: MOVE_UP, ws))) { - initial.vert.h = initial.vert.ptr->height; - initial.vert.parent.ptr = get_swayc_in_direction_under(initial.vert.ptr, - lock.top ? MOVE_UP : MOVE_DOWN, ws); - initial.vert.parent.h = initial.vert.parent.ptr->height; - reset = false; - } - // If nothing will change just undo the mode - if (reset) { - pointer_state.mode = 0; - } -} - -static void reset_initial_view(void) { - initial.ptr->x = initial.x; - initial.ptr->y = initial.y; - initial.ptr->width = initial.w; - initial.ptr->height = initial.h; - arrange_windows(initial.ptr, -1, -1); - pointer_state.mode = 0; -} - -static void reset_initial_sibling(void) { - initial.horiz.ptr->width = initial.horiz.w; - initial.horiz.parent.ptr->width = initial.horiz.parent.w; - initial.vert.ptr->height = initial.vert.h; - initial.vert.parent.ptr->height = initial.vert.parent.h; - arrange_windows(initial.horiz.ptr->parent, -1, -1); - arrange_windows(initial.vert.ptr->parent, -1, -1); - pointer_state.mode = 0; -} - -void pointer_position_set(double new_x, double new_y, bool force_focus) { - double x, y; - wlc_pointer_get_position_v2(&x, &y); - pointer_state.delta.x = new_x - x; - pointer_state.delta.y = new_y - y; - - wlc_pointer_set_position_v2(new_x, new_y); - - // Update view under pointer - swayc_t *prev_view = pointer_state.view; - pointer_state.view = container_under_pointer(); - - // If pointer is in a mode, update it - if (pointer_state.mode) { - pointer_mode_update(); - // Otherwise change focus if config is set - } else if (force_focus || (prev_view != pointer_state.view && config->focus_follows_mouse)) { - if (pointer_state.view && pointer_state.view->type == C_VIEW) { - set_focused_container(pointer_state.view); - } - } -} - -void center_pointer_on(swayc_t *view) { - pointer_position_set(view->x + view->width/2, view->y + view->height/2, true); -} - -// Mode set left/right click - -static void pointer_mode_set_dragging(void) { - switch (config->dragging_key) { - case M_LEFT_CLICK: - set_initial_view(pointer_state.left.view); - break; - case M_RIGHT_CLICK: - set_initial_view(pointer_state.right.view); - break; - } - - if (initial.ptr->is_floating) { - pointer_state.mode = M_DRAGGING | M_FLOATING; - } else { - pointer_state.mode = M_DRAGGING | M_TILING; - // unset mode if we can't drag tile - if (initial.ptr->parent->type == C_WORKSPACE && - initial.ptr->parent->children->length == 1) { - pointer_state.mode = 0; - } - } -} - -static void pointer_mode_set_resizing(void) { - switch (config->resizing_key) { - case M_LEFT_CLICK: - set_initial_view(pointer_state.left.view); - break; - case M_RIGHT_CLICK: - set_initial_view(pointer_state.right.view); - break; - } - // Setup locking information - int midway_x = initial.ptr->x + initial.ptr->width/2; - int midway_y = initial.ptr->y + initial.ptr->height/2; - - double x, y; - wlc_pointer_get_position_v2(&x, &y); - lock.left = x > midway_x; - lock.top = y > midway_y; - - if (initial.ptr->is_floating) { - pointer_state.mode = M_RESIZING | M_FLOATING; - } else { - pointer_state.mode = M_RESIZING | M_TILING; - set_initial_sibling(); - } -} - -// Mode set/update/reset - -void pointer_mode_set(uint32_t button, bool condition) { - // switch on drag/resize mode - switch (pointer_state.mode & (M_DRAGGING | M_RESIZING)) { - case M_DRAGGING: - // end drag mode when 'dragging' click is unpressed - if (config->dragging_key == M_LEFT_CLICK && !pointer_state.left.held) { - pointer_state.mode = 0; - } else if (config->dragging_key == M_RIGHT_CLICK && !pointer_state.right.held) { - pointer_state.mode = 0; - } - break; - - case M_RESIZING: - // end resize mode when 'resizing' click is unpressed - if (config->resizing_key == M_LEFT_CLICK && !pointer_state.left.held) { - pointer_state.mode = 0; - } else if (config->resizing_key == M_RIGHT_CLICK && !pointer_state.right.held) { - pointer_state.mode = 0; - } - break; - - // No mode case - default: - // return if failed condition, or no view - if (!condition || !pointer_state.view) { - break; - } - - // Set mode depending on current button press - switch (button) { - // Start left-click mode - case M_LEFT_CLICK: - // if button release don't do anything - if (pointer_state.left.held) { - if (config->dragging_key == M_LEFT_CLICK) { - pointer_mode_set_dragging(); - } else if (config->resizing_key == M_LEFT_CLICK) { - pointer_mode_set_resizing(); - } - } - break; - - // Start right-click mode - case M_RIGHT_CLICK: - // if button release don't do anyhting - if (pointer_state.right.held) { - if (config->dragging_key == M_RIGHT_CLICK) { - pointer_mode_set_dragging(); - } else if (config->resizing_key == M_RIGHT_CLICK) { - pointer_mode_set_resizing(); - } - } - break; - } - } -} - -void pointer_mode_update(void) { - if (initial.ptr->type != C_VIEW) { - pointer_state.mode = 0; - return; - } - double x, y; - wlc_pointer_get_position_v2(&x, &y); - int dx = x; - int dy = y; - - switch (pointer_state.mode) { - case M_FLOATING | M_DRAGGING: - // Update position - switch (config->dragging_key) { - case M_LEFT_CLICK: - dx -= pointer_state.left.x; - dy -= pointer_state.left.y; - break; - case M_RIGHT_CLICK: - dx -= pointer_state.right.x; - dy -= pointer_state.right.y; - break; - } - - if (initial.x + dx != initial.ptr->x) { - initial.ptr->x = initial.x + dx; - } - if (initial.y + dy != initial.ptr->y) { - initial.ptr->y = initial.y + dy; - } - update_geometry(initial.ptr); - break; - - case M_FLOATING | M_RESIZING: - switch (config->resizing_key) { - case M_LEFT_CLICK: - dx -= pointer_state.left.x; - dy -= pointer_state.left.y; - initial.ptr = pointer_state.left.view; - break; - case M_RIGHT_CLICK: - dx -= pointer_state.right.x; - dy -= pointer_state.right.y; - initial.ptr = pointer_state.right.view; - break; - } - - if (lock.left) { - if (initial.w + dx > min_sane_w) { - initial.ptr->width = initial.w + dx; - } - } else { // lock.right - if (initial.w - dx > min_sane_w) { - initial.ptr->width = initial.w - dx; - initial.ptr->x = initial.x + dx; - } - } - if (lock.top) { - if (initial.h + dy > min_sane_h) { - initial.ptr->height = initial.h + dy; - } - } else { // lock.bottom - if (initial.h - dy > min_sane_h) { - initial.ptr->height = initial.h - dy; - initial.ptr->y = initial.y + dy; - } - } - update_geometry(initial.ptr); - break; - - case M_TILING | M_DRAGGING: - // swap current view under pointer with dragged view - if (pointer_state.view && pointer_state.view->type == C_VIEW - && pointer_state.view != initial.ptr - && !pointer_state.view->is_floating) { - // Swap them around - swap_container(pointer_state.view, initial.ptr); - swap_geometry(pointer_state.view, initial.ptr); - update_geometry(pointer_state.view); - update_geometry(initial.ptr); - // Set focus back to initial view - set_focused_container(initial.ptr); - // Arrange the windows - arrange_windows(&root_container, -1, -1); - } - break; - - case M_TILING | M_RESIZING: - switch (config->resizing_key) { - case M_LEFT_CLICK: - dx -= pointer_state.left.x; - dy -= pointer_state.left.y; - break; - case M_RIGHT_CLICK: - dx -= pointer_state.right.x; - dy -= pointer_state.right.y; - break; - } - - // resize if we can - if (initial.horiz.ptr) { - if (lock.left) { - // Check whether its fine to resize - if (initial.w + dx > min_sane_w && initial.horiz.w - dx > min_sane_w) { - initial.horiz.ptr->width = initial.horiz.w - dx; - initial.horiz.parent.ptr->width = initial.horiz.parent.w + dx; - } - } else { // lock.right - if (initial.w - dx > min_sane_w && initial.horiz.w + dx > min_sane_w) { - initial.horiz.ptr->width = initial.horiz.w + dx; - initial.horiz.parent.ptr->width = initial.horiz.parent.w - dx; - } - } - arrange_windows(initial.horiz.ptr->parent, -1, -1); - } - if (initial.vert.ptr) { - if (lock.top) { - if (initial.h + dy > min_sane_h && initial.vert.h - dy > min_sane_h) { - initial.vert.ptr->height = initial.vert.h - dy; - initial.vert.parent.ptr->height = initial.vert.parent.h + dy; - } - } else { // lock.bottom - if (initial.h - dy > min_sane_h && initial.vert.h + dy > min_sane_h) { - initial.vert.ptr->height = initial.vert.h + dy; - initial.vert.parent.ptr->height = initial.vert.parent.h - dy; - } - } - arrange_windows(initial.vert.ptr->parent, -1, -1); - } - default: - return; - } -} - -void pointer_mode_reset(void) { - switch (pointer_state.mode) { - case M_FLOATING | M_RESIZING: - case M_FLOATING | M_DRAGGING: - reset_initial_view(); - break; - - case M_TILING | M_RESIZING: - (void) reset_initial_sibling; - break; - - case M_TILING | M_DRAGGING: - default: - break; - } -} diff --git a/sway/ipc-json.c b/sway/ipc-json.c deleted file mode 100644 index 1579a2d9..00000000 --- a/sway/ipc-json.c +++ /dev/null @@ -1,521 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include -#include -#include "sway/output.h" -#include "sway/container.h" -#include "sway/input.h" -#include "sway/ipc-json.h" -#include "util.h" - -static json_object *ipc_json_create_rect(swayc_t *c) { - json_object *rect = json_object_new_object(); - - json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x)); - json_object_object_add(rect, "y", json_object_new_int((int32_t)c->y)); - - struct wlr_box box; - if (c->type == C_OUTPUT) { - wlr_output_effective_resolution(c->_handle.output->wlr_output, - &box.width, &box.height); - } else { - box.width = c->width; - box.width = c->height; - } - - json_object_object_add(rect, "width", - json_object_new_int((int32_t)box.width)); - json_object_object_add(rect, "height", - json_object_new_int((int32_t)box.height)); - - return rect; -} - -static json_object *ipc_json_create_rect_from_geometry(struct wlc_geometry g) { - json_object *rect = json_object_new_object(); - - json_object_object_add(rect, "x", json_object_new_int(g.origin.x)); - json_object_object_add(rect, "y", json_object_new_int(g.origin.y)); - json_object_object_add(rect, "width", json_object_new_int(g.size.w)); - json_object_object_add(rect, "height", json_object_new_int(g.size.h)); - - return rect; -} - -static const char *ipc_json_border_description(swayc_t *c) { - const char *border; - - switch (c->border_type) { - case B_PIXEL: - border = "1pixel"; - break; - - case B_NORMAL: - border = "normal"; - break; - - case B_NONE: // fallthrough - default: - border = "none"; - break; - } - - return border; -} - -static const char *ipc_json_layout_description(enum swayc_layouts l) { - const char *layout; - - switch (l) { - case L_VERT: - layout = "splitv"; - break; - - case L_HORIZ: - layout = "splith"; - break; - - case L_TABBED: - layout = "tabbed"; - break; - - case L_STACKED: - layout = "stacked"; - break; - - case L_FLOATING: - layout = "floating"; - break; - - case L_NONE: // fallthrough - case L_LAYOUTS: // fallthrough; this should never happen, I'm just trying to silence compiler warnings - default: - layout = "null"; - break; - } - - return layout; -} - -static float ipc_json_child_percentage(swayc_t *c) { - float percent = 0; - swayc_t *parent = c->parent; - - if (parent) { - switch (parent->layout) { - case L_VERT: - percent = c->height / parent->height; - break; - - case L_HORIZ: - percent = c->width / parent->width; - break; - - case L_STACKED: // fallthrough - case L_TABBED: // fallthrough - percent = 1.0; - break; - - default: - break; - } - } - - return percent; -} - -static void ipc_json_describe_output(swayc_t *output, json_object *object) { - uint32_t scale = wlc_output_get_scale(output->handle); - json_object_object_add(object, "active", json_object_new_boolean(true)); - json_object_object_add(object, "primary", json_object_new_boolean(false)); - json_object_object_add(object, "layout", json_object_new_string("output")); - json_object_object_add(object, "type", json_object_new_string("output")); - json_object_object_add(object, "current_workspace", - (output->focused) ? json_object_new_string(output->focused->name) : NULL); - json_object_object_add(object, "scale", json_object_new_int(scale)); -} - -static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) { - int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1; - const char *layout = ipc_json_layout_description(workspace->workspace_layout); - - json_object_object_add(object, "num", json_object_new_int(num)); - json_object_object_add(object, "output", (workspace->parent) ? json_object_new_string(workspace->parent->name) : NULL); - json_object_object_add(object, "type", json_object_new_string("workspace")); - json_object_object_add(object, "layout", (strcmp(layout, "null") == 0) ? NULL : json_object_new_string(layout)); -} - -// window is in the scratchpad ? changed : none -static const char *ipc_json_get_scratchpad_state(swayc_t *c) { - int i; - for (i = 0; i < scratchpad->length; i++) { - if (scratchpad->items[i] == c) { - return "changed"; - } - } - return "none"; // we ignore the fresh value -} - -static void ipc_json_describe_view(swayc_t *c, json_object *object) { - json_object *props = json_object_new_object(); - json_object_object_add(object, "type", json_object_new_string((c->is_floating) ? "floating_con" : "con")); - - wlc_handle parent = wlc_view_get_parent(c->handle); - json_object_object_add(object, "scratchpad_state", - json_object_new_string(ipc_json_get_scratchpad_state(c))); - - json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); - - json_object_object_add(props, "class", c->class ? json_object_new_string(c->class) : - c->app_id ? json_object_new_string(c->app_id) : NULL); - json_object_object_add(props, "instance", c->instance ? json_object_new_string(c->instance) : - c->app_id ? json_object_new_string(c->app_id) : NULL); - json_object_object_add(props, "title", (c->name) ? json_object_new_string(c->name) : NULL); - json_object_object_add(props, "transient_for", parent ? json_object_new_int(parent) : NULL); - json_object_object_add(object, "window_properties", props); - - json_object_object_add(object, "fullscreen_mode", - json_object_new_int(swayc_is_fullscreen(c) ? 1 : 0)); - json_object_object_add(object, "sticky", json_object_new_boolean(c->sticky)); - json_object_object_add(object, "floating", json_object_new_string( - c->is_floating ? "auto_on" : "auto_off")); // we can't state the cause - - json_object_object_add(object, "app_id", c->app_id ? json_object_new_string(c->app_id) : NULL); - - if (c->parent) { - const char *layout = (c->parent->type == C_CONTAINER) ? - ipc_json_layout_description(c->parent->layout) : "none"; - const char *last_layout = (c->parent->type == C_CONTAINER) ? - ipc_json_layout_description(c->parent->prev_layout) : "none"; - json_object_object_add(object, "layout", - (strcmp(layout, "null") == 0) ? NULL : json_object_new_string(layout)); - json_object_object_add(object, "last_split_layout", - (strcmp(last_layout, "null") == 0) ? NULL : json_object_new_string(last_layout)); - json_object_object_add(object, "workspace_layout", - json_object_new_string(ipc_json_layout_description(swayc_parent_by_type(c, C_WORKSPACE)->workspace_layout))); - } -} - -static void ipc_json_describe_root(swayc_t *c, json_object *object) { - json_object_object_add(object, "type", json_object_new_string("root")); - json_object_object_add(object, "layout", json_object_new_string("splith")); -} - -json_object *ipc_json_describe_container(swayc_t *c) { - float percent = ipc_json_child_percentage(c); - - if (!(sway_assert(c, "Container must not be null."))) { - return NULL; - } - - json_object *object = json_object_new_object(); - - json_object_object_add(object, "id", json_object_new_int((int)c->id)); - json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); - json_object_object_add(object, "rect", ipc_json_create_rect(c)); - json_object_object_add(object, "visible", json_object_new_boolean(c->visible)); - json_object_object_add(object, "focused", json_object_new_boolean(c == current_focus)); - - json_object_object_add(object, "border", json_object_new_string(ipc_json_border_description(c))); - json_object_object_add(object, "window_rect", ipc_json_create_rect_from_geometry(c->actual_geometry)); - json_object_object_add(object, "deco_rect", ipc_json_create_rect_from_geometry(c->title_bar_geometry)); - json_object_object_add(object, "geometry", ipc_json_create_rect_from_geometry(c->cached_geometry)); - json_object_object_add(object, "percent", (percent > 0) ? json_object_new_double(percent) : NULL); - json_object_object_add(object, "window", json_object_new_int(c->handle)); // for the sake of i3 compat - // TODO: make urgency actually work once Sway supports it - json_object_object_add(object, "urgent", json_object_new_boolean(false)); - json_object_object_add(object, "current_border_width", json_object_new_int(c->border_thickness)); - - switch (c->type) { - case C_ROOT: - ipc_json_describe_root(c, object); - break; - - case C_OUTPUT: - ipc_json_describe_output(c, object); - break; - - case C_CONTAINER: // fallthrough - case C_VIEW: - ipc_json_describe_view(c, object); - break; - - case C_WORKSPACE: - ipc_json_describe_workspace(c, object); - break; - - case C_TYPES: // fallthrough; this should never happen, I'm just trying to silence compiler warnings - default: - break; - } - - return object; -} - -json_object *ipc_json_describe_input(struct libinput_device *device) { - char* identifier = libinput_dev_unique_id(device); - int vendor = libinput_device_get_id_vendor(device); - int product = libinput_device_get_id_product(device); - const char *name = libinput_device_get_name(device); - double width = -1, height = -1; - int has_size = libinput_device_get_size(device, &width, &height); - - json_object *device_object = json_object_new_object(); - json_object_object_add(device_object,"identifier", - identifier ? json_object_new_string(identifier) : NULL); - json_object_object_add(device_object, - "vendor", json_object_new_int(vendor)); - json_object_object_add(device_object, - "product", json_object_new_int(product)); - json_object_object_add(device_object, - "name", json_object_new_string(name)); - if (has_size == 0) { - json_object *size_object = json_object_new_object(); - json_object_object_add(size_object, - "width", json_object_new_double(width)); - json_object_object_add(size_object, - "height", json_object_new_double(height)); - } else { - json_object_object_add(device_object, "size", NULL); - } - - struct { - enum libinput_device_capability cap; - const char *name; - // If anyone feels like implementing device-specific IPC output, - // be my guest - json_object *(*describe)(struct libinput_device *); - } caps[] = { - { LIBINPUT_DEVICE_CAP_KEYBOARD, "keyboard", NULL }, - { LIBINPUT_DEVICE_CAP_POINTER, "pointer", NULL }, - { LIBINPUT_DEVICE_CAP_TOUCH, "touch", NULL }, - { LIBINPUT_DEVICE_CAP_TABLET_TOOL, "tablet_tool", NULL }, - { LIBINPUT_DEVICE_CAP_TABLET_PAD, "tablet_pad", NULL }, - { LIBINPUT_DEVICE_CAP_GESTURE, "gesture", NULL }, -#ifdef LIBINPUT_DEVICE_CAP_SWITCH // libinput 1.7.0+ - { LIBINPUT_DEVICE_CAP_SWITCH, "switch", NULL }, -#endif - }; - - json_object *_caps = json_object_new_array(); - for (size_t i = 0; i < sizeof(caps) / sizeof(caps[0]); ++i) { - if (libinput_device_has_capability(device, caps[i].cap)) { - json_object_array_add(_caps, json_object_new_string(caps[i].name)); - if (caps[i].describe) { - json_object *desc = caps[i].describe(device); - json_object_object_add(device_object, caps[i].name, desc); - } - } - } - json_object_object_add(device_object, "capabilities", _caps); - - free(identifier); - return device_object; -} - -json_object *ipc_json_get_version() { - int major = 0, minor = 0, patch = 0; - json_object *version = json_object_new_object(); - - sscanf(SWAY_VERSION, "%u.%u.%u", &major, &minor, &patch); - - json_object_object_add(version, "human_readable", json_object_new_string(SWAY_VERSION)); - json_object_object_add(version, "variant", json_object_new_string("sway")); - json_object_object_add(version, "major", json_object_new_int(major)); - json_object_object_add(version, "minor", json_object_new_int(minor)); - json_object_object_add(version, "patch", json_object_new_int(patch)); - - return version; -} - -json_object *ipc_json_describe_bar_config(struct bar_config *bar) { - if (!sway_assert(bar, "Bar must not be NULL")) { - return NULL; - } - - json_object *json = json_object_new_object(); - json_object_object_add(json, "id", json_object_new_string(bar->id)); -#ifdef ENABLE_TRAY - if (bar->tray_output) { - json_object_object_add(json, "tray_output", json_object_new_string(bar->tray_output)); - } else { - json_object_object_add(json, "tray_output", NULL); - } - if (bar->icon_theme) { - json_object_object_add(json, "icon_theme", json_object_new_string(bar->icon_theme)); - } else { - json_object_object_add(json, "icon_theme", NULL); - } - json_object_object_add(json, "tray_padding", json_object_new_int(bar->tray_padding)); - json_object_object_add(json, "activate_button", json_object_new_int(bar->activate_button)); - json_object_object_add(json, "context_button", json_object_new_int(bar->context_button)); - json_object_object_add(json, "secondary_button", json_object_new_int(bar->secondary_button)); -#endif - json_object_object_add(json, "mode", json_object_new_string(bar->mode)); - json_object_object_add(json, "hidden_state", json_object_new_string(bar->hidden_state)); - json_object_object_add(json, "modifier", json_object_new_string(get_modifier_name_by_mask(bar->modifier))); - switch (bar->position) { - case DESKTOP_SHELL_PANEL_POSITION_TOP: - json_object_object_add(json, "position", json_object_new_string("top")); - break; - case DESKTOP_SHELL_PANEL_POSITION_BOTTOM: - json_object_object_add(json, "position", json_object_new_string("bottom")); - break; - case DESKTOP_SHELL_PANEL_POSITION_LEFT: - json_object_object_add(json, "position", json_object_new_string("left")); - break; - case DESKTOP_SHELL_PANEL_POSITION_RIGHT: - json_object_object_add(json, "position", json_object_new_string("right")); - break; - } - json_object_object_add(json, "status_command", json_object_new_string(bar->status_command)); - json_object_object_add(json, "font", json_object_new_string((bar->font) ? bar->font : config->font)); - if (bar->separator_symbol) { - json_object_object_add(json, "separator_symbol", json_object_new_string(bar->separator_symbol)); - } - json_object_object_add(json, "bar_height", json_object_new_int(bar->height)); - json_object_object_add(json, "wrap_scroll", json_object_new_boolean(bar->wrap_scroll)); - json_object_object_add(json, "workspace_buttons", json_object_new_boolean(bar->workspace_buttons)); - json_object_object_add(json, "strip_workspace_numbers", json_object_new_boolean(bar->strip_workspace_numbers)); - json_object_object_add(json, "binding_mode_indicator", json_object_new_boolean(bar->binding_mode_indicator)); - json_object_object_add(json, "verbose", json_object_new_boolean(bar->verbose)); - json_object_object_add(json, "pango_markup", json_object_new_boolean(bar->pango_markup)); - - json_object *colors = json_object_new_object(); - json_object_object_add(colors, "background", json_object_new_string(bar->colors.background)); - json_object_object_add(colors, "statusline", json_object_new_string(bar->colors.statusline)); - json_object_object_add(colors, "separator", json_object_new_string(bar->colors.separator)); - - if (bar->colors.focused_background) { - json_object_object_add(colors, "focused_background", json_object_new_string(bar->colors.focused_background)); - } else { - json_object_object_add(colors, "focused_background", json_object_new_string(bar->colors.background)); - } - - if (bar->colors.focused_statusline) { - json_object_object_add(colors, "focused_statusline", json_object_new_string(bar->colors.focused_statusline)); - } else { - json_object_object_add(colors, "focused_statusline", json_object_new_string(bar->colors.statusline)); - } - - if (bar->colors.focused_separator) { - json_object_object_add(colors, "focused_separator", json_object_new_string(bar->colors.focused_separator)); - } else { - json_object_object_add(colors, "focused_separator", json_object_new_string(bar->colors.separator)); - } - - json_object_object_add(colors, "focused_workspace_border", json_object_new_string(bar->colors.focused_workspace_border)); - json_object_object_add(colors, "focused_workspace_bg", json_object_new_string(bar->colors.focused_workspace_bg)); - json_object_object_add(colors, "focused_workspace_text", json_object_new_string(bar->colors.focused_workspace_text)); - - json_object_object_add(colors, "inactive_workspace_border", json_object_new_string(bar->colors.inactive_workspace_border)); - json_object_object_add(colors, "inactive_workspace_bg", json_object_new_string(bar->colors.inactive_workspace_bg)); - json_object_object_add(colors, "inactive_workspace_text", json_object_new_string(bar->colors.inactive_workspace_text)); - - json_object_object_add(colors, "active_workspace_border", json_object_new_string(bar->colors.active_workspace_border)); - json_object_object_add(colors, "active_workspace_bg", json_object_new_string(bar->colors.active_workspace_bg)); - json_object_object_add(colors, "active_workspace_text", json_object_new_string(bar->colors.active_workspace_text)); - - json_object_object_add(colors, "urgent_workspace_border", json_object_new_string(bar->colors.urgent_workspace_border)); - json_object_object_add(colors, "urgent_workspace_bg", json_object_new_string(bar->colors.urgent_workspace_bg)); - json_object_object_add(colors, "urgent_workspace_text", json_object_new_string(bar->colors.urgent_workspace_text)); - - if (bar->colors.binding_mode_border) { - json_object_object_add(colors, "binding_mode_border", json_object_new_string(bar->colors.binding_mode_border)); - } else { - json_object_object_add(colors, "binding_mode_border", json_object_new_string(bar->colors.urgent_workspace_border)); - } - - if (bar->colors.binding_mode_bg) { - json_object_object_add(colors, "binding_mode_bg", json_object_new_string(bar->colors.binding_mode_bg)); - } else { - json_object_object_add(colors, "binding_mode_bg", json_object_new_string(bar->colors.urgent_workspace_bg)); - } - - if (bar->colors.binding_mode_text) { - json_object_object_add(colors, "binding_mode_text", json_object_new_string(bar->colors.binding_mode_text)); - } else { - json_object_object_add(colors, "binding_mode_text", json_object_new_string(bar->colors.urgent_workspace_text)); - } - - json_object_object_add(json, "colors", colors); - - // Add outputs if defined - if (bar->outputs && bar->outputs->length > 0) { - json_object *outputs = json_object_new_array(); - int i; - for (i = 0; i < bar->outputs->length; ++i) { - const char *name = bar->outputs->items[i]; - json_object_array_add(outputs, json_object_new_string(name)); - } - json_object_object_add(json, "outputs", outputs); - } - - return json; -} - -json_object *ipc_json_describe_container_recursive(swayc_t *c) { - json_object *object = ipc_json_describe_container(c); - int i; - - json_object *floating = json_object_new_array(); - if (c->type != C_VIEW && c->floating) { - for (i = 0; i < c->floating->length; ++i) { - swayc_t *item = c->floating->items[i]; - json_object_array_add(floating, ipc_json_describe_container_recursive(item)); - } - } - json_object_object_add(object, "floating_nodes", floating); - - json_object *children = json_object_new_array(); - if (c->type != C_VIEW && c->children) { - for (i = 0; i < c->children->length; ++i) { - json_object_array_add(children, ipc_json_describe_container_recursive(c->children->items[i])); - } - } - json_object_object_add(object, "nodes", children); - - json_object *focus = json_object_new_array(); - if (c->type != C_VIEW) { - if (c->focused) { - json_object_array_add(focus, json_object_new_double(c->focused->id)); - } - if (c->floating) { - for (i = 0; i < c->floating->length; ++i) { - swayc_t *item = c->floating->items[i]; - if (item == c->focused) { - continue; - } - - json_object_array_add(focus, json_object_new_double(item->id)); - } - } - if (c->children) { - for (i = 0; i < c->children->length; ++i) { - swayc_t *item = c->children->items[i]; - if (item == c->focused) { - continue; - } - - json_object_array_add(focus, json_object_new_double(item->id)); - } - } - } - json_object_object_add(object, "focus", focus); - - if (c->type == C_ROOT) { - json_object *scratchpad_json = json_object_new_array(); - if (scratchpad->length > 0) { - for (i = 0; i < scratchpad->length; ++i) { - json_object_array_add(scratchpad_json, ipc_json_describe_container_recursive(scratchpad->items[i])); - } - } - json_object_object_add(object, "scratchpad", scratchpad_json); - } - - return object; -} diff --git a/sway/ipc-server.c b/sway/ipc-server.c deleted file mode 100644 index d4db4e7a..00000000 --- a/sway/ipc-server.c +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#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/" - 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; ilength; 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 index 7d6f2873..5710a099 100644 --- a/sway/main.c +++ b/sway/main.c @@ -1,30 +1,24 @@ #define _XOPEN_SOURCE 700 #define _POSIX_C_SOURCE 200112L -#include -#include +#include +#include #include -#include -#include +#include +#include +#include #include +#include +#include #include -#include #include -#include #ifdef __linux__ #include #include #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/old/border.c b/sway/old/border.c new file mode 100644 index 00000000..df0022ce --- /dev/null +++ b/sway/old/border.c @@ -0,0 +1,510 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include "sway/border.h" +#include "sway/container.h" +#include "sway/config.h" +#include "client/pango.h" + +void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { + color = htonl(color); + + cairo_set_source_rgba(cairo, + (color >> (2*8) & 0xFF) / 255.0, + (color >> (1*8) & 0xFF) / 255.0, + (color >> (0*8) & 0xFF) / 255.0, + (color >> (3*8) & 0xFF) / 255.0); +} + +void border_clear(struct border *border) { + if (border && border->buffer) { + free(border->buffer); + border->buffer = NULL; + } +} + +static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry g, cairo_surface_t **surface) { + if (view->border == NULL) { + view->border = malloc(sizeof(struct border)); + if (!view->border) { + sway_log(L_ERROR, "Unable to allocate window border information"); + return NULL; + } + } + cairo_t *cr; + int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, g.size.w); + view->border->buffer = calloc(stride * g.size.h, sizeof(unsigned char)); + view->border->geometry = g; + if (!view->border->buffer) { + sway_log(L_ERROR, "Unable to allocate window border buffer"); + return NULL; + } + *surface = cairo_image_surface_create_for_data(view->border->buffer, + CAIRO_FORMAT_ARGB32, g.size.w, g.size.h, stride); + if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) { + border_clear(view->border); + sway_log(L_ERROR, "Unable to allocate window border surface"); + return NULL; + } + cr = cairo_create(*surface); + cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); + if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) { + cairo_surface_destroy(*surface); + border_clear(view->border); + sway_log(L_ERROR, "Unable to create cairo context"); + return NULL; + } + return cr; +} + +// TODO: move to client/cairo.h when local set_source_u32 is fixed. +/** + * Renders a sharp line of any width and height. + * + * The line is drawn from (x,y) to (x+width,y+height) where width/height is 0 + * if the line has a width/height of one pixel, respectively. + */ +static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y, double width, double height) { + cairo_set_source_u32(cairo, color); + + if (width > 1 && height > 1) { + cairo_rectangle(cairo, x, y, width, height); + cairo_fill(cairo); + } else { + if (width == 1) { + x += 0.5; + height += y; + width = x; + } + + if (height == 1) { + y += 0.5; + width += x; + height = y; + } + + cairo_move_to(cairo, x, y); + cairo_set_line_width(cairo, 1.0); + cairo_line_to(cairo, width, height); + cairo_stroke(cairo); + } +} + +int get_font_text_height(const char *font) { + cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200); + cairo_t *cr = cairo_create(surface); + int width, height; + get_text_size(cr, font, &width, &height, 1, false, "Gg"); + cairo_surface_destroy(surface); + cairo_destroy(cr); + return height; +} + +static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *colors, bool top) { + struct wlc_geometry *g = &view->border->geometry; + struct wlc_geometry *b = &view->border_geometry; + struct wlc_geometry *v = &view->actual_geometry; + enum swayc_layouts layout = view->parent->layout; + uint32_t color; + + int x = b->origin.x - g->origin.x; + int y = b->origin.y - g->origin.y; + + // draw vertical/horizontal indicator if container is the only child of its parent container + bool is_only_child = view->parent && view->parent->children && view->parent->children->length == 1; + + // left border + int left_border = v->origin.x - b->origin.x; + if (left_border > 0) { + render_sharp_line(cr, + colors->child_border, + x, y, + left_border, + b->size.h); + } + + // right border + int right_border = b->size.w - v->size.w - left_border; + if (right_border > 0) { + if (is_only_child && layout == L_HORIZ && !view->is_floating) { + color = colors->indicator; + } else { + color = colors->child_border; + } + render_sharp_line(cr, + color, + x + b->size.w - right_border, + y, + right_border, + b->size.h); + } + + // top border + int top_border = v->origin.y - b->origin.y; + if (top && top_border > 0) { + render_sharp_line(cr, + colors->child_border, + x, y, + b->size.w, + top_border); + } + + // bottom border + int bottom_border = b->size.h - (top_border + v->size.h); + if (bottom_border > 0) { + if (is_only_child && layout == L_VERT && !view->is_floating) { + color = colors->indicator; + } else { + color = colors->child_border; + } + render_sharp_line(cr, + color, + x, + y + b->size.h - bottom_border, + b->size.w, + bottom_border); + } +} + +static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b, struct border_colors *colors) { + struct wlc_geometry *tb = &view->title_bar_geometry; + int x = MIN(tb->origin.x, tb->origin.x - b->origin.x); + int y = MIN(tb->origin.y, tb->origin.y - b->origin.y); + + // title bar background + cairo_set_source_u32(cr, colors->background); + cairo_rectangle(cr, x, y, tb->size.w, tb->size.h); + cairo_fill(cr); + + // header top line + render_sharp_line(cr, colors->border, x, y, tb->size.w, 1); + + // text + if (view->name) { + int width, height; + get_text_size(cr, config->font, &width, &height, 1, false, "%s", view->name); + cairo_move_to(cr, x + 2, y + 2); + cairo_set_source_u32(cr, colors->text); + pango_printf(cr, config->font, 1, false, "%s", view->name); + } + // Marks + if (config->show_marks && view->marks) { + int total_len = 0; + + for(int i = view->marks->length - 1; i >= 0; --i) { + char *mark = (char *)view->marks->items[i]; + if (*mark != '_') { + int width, height; + get_text_size(cr, config->font, &width, &height, 1, false, "[%s]", mark); + total_len += width; + if ((int)tb->size.w + x - (total_len + 2) < x + 2) { + break; + } else { + cairo_move_to(cr, (int)tb->size.w + x - (total_len + 2), y + 2); + cairo_set_source_u32(cr, colors->text); + pango_printf(cr, config->font, 1, false, "[%s]", mark); + } + } + } + } + + // titlebars has a border all around for tabbed layouts + if (view->parent->layout == L_TABBED) { + // header bottom line + render_sharp_line(cr, colors->border, x, y + tb->size.h - 1, + tb->size.w, 1); + + // left border + render_sharp_line(cr, colors->border, x, y, 1, tb->size.h); + + // right border + render_sharp_line(cr, colors->border, x + tb->size.w - 1, y, + 1, tb->size.h); + + return; + } + + if ((uint32_t)(view->actual_geometry.origin.y - tb->origin.y) == tb->size.h) { + // header bottom line + render_sharp_line(cr, colors->border, + x + view->actual_geometry.origin.x - tb->origin.x, + y + tb->size.h - 1, + view->actual_geometry.size.w, 1); + } else { + // header bottom line + render_sharp_line(cr, colors->border, x, + y + tb->size.h - 1, + tb->size.w, 1); + } +} + +/** + * Generate nested container title for tabbed/stacked layouts + */ +static char *generate_container_title(swayc_t *container) { + char layout = 'H'; + char *name, *prev_name = NULL; + switch (container->layout) { + case L_TABBED: + layout = 'T'; + break; + case L_STACKED: + layout = 'S'; + break; + case L_VERT: + layout = 'V'; + break; + default: + layout = 'H'; + } + int len = 9; + name = malloc(len * sizeof(char)); + if (!name) { + sway_log(L_ERROR, "Unable to allocate container title"); + return NULL; + } + snprintf(name, len, "sway: %c[", layout); + + int i; + for (i = 0; i < container->children->length; ++i) { + prev_name = name; + swayc_t* child = container->children->items[i]; + const char *title = NULL; + if (child->type == C_VIEW) { + title = child->app_id ? child->app_id : + (child->instance ? child->instance : + (child->class ? child->class :"(null)")); + } else { //child->type == C_CONTAINER + title = generate_container_title(child); + } + + len = strlen(name) + strlen(title) + 1; + if (i < container->children->length-1) { + len++; + } + + name = malloc(len * sizeof(char)); + if (!name) { + free(prev_name); + sway_log(L_ERROR, "Unable to allocate container title"); + return NULL; + } + if (i < container->children->length-1) { + snprintf(name, len, "%s%s ", prev_name, title); + } else { + snprintf(name, len, "%s%s", prev_name, title); + } + free(prev_name); + } + + prev_name = name; + len = strlen(name) + 2; + name = malloc(len * sizeof(char)); + if (!name) { + free(prev_name); + sway_log(L_ERROR, "Unable to allocate container title"); + return NULL; + } + snprintf(name, len, "%s]", prev_name); + free(prev_name); + free(container->name); + container->name = name; + return container->name + 6; // don't include "sway: " +} + +void update_tabbed_stacked_titlebars(swayc_t *c, cairo_t *cr, struct wlc_geometry *g, swayc_t *focused, swayc_t *focused_inactive) { + if (c->type == C_CONTAINER) { + if (c->parent->focused == c) { + render_title_bar(c, cr, g, &config->border_colors.focused_inactive); + } else { + render_title_bar(c, cr, g, &config->border_colors.unfocused); + } + + if (!c->visible) { + return; + } + + int i; + for (i = 0; i < c->children->length; ++i) { + swayc_t *child = c->children->items[i]; + update_tabbed_stacked_titlebars(child, cr, g, focused, focused_inactive); + } + } else { + bool is_child_of_focused = swayc_is_child_of(c, get_focused_container(&root_container)); + + if (focused == c || is_child_of_focused) { + render_title_bar(c, cr, g, &config->border_colors.focused); + } else if (focused_inactive == c) { + render_title_bar(c, cr, g, &config->border_colors.focused_inactive); + } else { + render_title_bar(c, cr, g, &config->border_colors.unfocused); + } + } +} + +static void update_view_border(swayc_t *view) { + if (!view->visible) { + return; + } + + cairo_t *cr = NULL; + cairo_surface_t *surface = NULL; + + // clear previous border buffer. + border_clear(view->border); + + // get focused and focused_inactive views + swayc_t *focused = get_focused_view(&root_container); + swayc_t *container = swayc_parent_by_type(view, C_CONTAINER); + swayc_t *focused_inactive = NULL; + + bool is_child_of_focused = swayc_is_parent_of(get_focused_container(&root_container), view); + + if (container) { + focused_inactive = swayc_focus_by_type(container, C_VIEW); + } else { + container = swayc_parent_by_type(view, C_WORKSPACE); + if (container) { + focused_inactive = swayc_focus_by_type(container, C_VIEW); + } + } + + // for tabbed/stacked layouts the focused view has to draw all the + // titlebars of the hidden views. + swayc_t *p = NULL; + if (view->parent->focused == view && (p = swayc_tabbed_stacked_ancestor(view))) { + struct wlc_geometry g = { + .origin = { + .x = p->x, + .y = p->y + }, + .size = { + .w = p->width, + .h = p->height + } + }; + cr = create_border_buffer(view, g, &surface); + if (!cr) { + goto cleanup; + } + + bool render_top = !should_hide_top_border(view, view->y); + if (view == focused || is_child_of_focused) { + render_borders(view, cr, &config->border_colors.focused, render_top); + } else { + render_borders(view, cr, &config->border_colors.focused_inactive, render_top); + } + + // generate container titles + int i; + for (i = 0; i < p->children->length; ++i) { + swayc_t *child = p->children->items[i]; + if (child->type == C_CONTAINER) { + generate_container_title(child); + } + } + + update_tabbed_stacked_titlebars(p, cr, &g, focused, focused_inactive); + } else { + switch (view->border_type) { + case B_NONE: + break; + case B_PIXEL: + cr = create_border_buffer(view, view->border_geometry, &surface); + if (!cr) { + break; + } + + if (focused == view || is_child_of_focused) { + render_borders(view, cr, &config->border_colors.focused, true); + } else if (focused_inactive == view) { + render_borders(view, cr, &config->border_colors.focused_inactive, true); + } else { + render_borders(view, cr, &config->border_colors.unfocused, true); + } + + break; + case B_NORMAL: + cr = create_border_buffer(view, view->border_geometry, &surface); + if (!cr) { + break; + } + + if (focused == view || is_child_of_focused) { + render_borders(view, cr, &config->border_colors.focused, false); + render_title_bar(view, cr, &view->border_geometry, + &config->border_colors.focused); + } else if (focused_inactive == view) { + render_borders(view, cr, &config->border_colors.focused_inactive, false); + render_title_bar(view, cr, &view->border_geometry, + &config->border_colors.focused_inactive); + } else { + render_borders(view, cr, &config->border_colors.unfocused, false); + render_title_bar(view, cr, &view->border_geometry, + &config->border_colors.unfocused); + } + + break; + } + } + +cleanup: + + if (surface) { + cairo_surface_flush(surface); + cairo_surface_destroy(surface); + } + + if (cr) { + cairo_destroy(cr); + } +} + +void update_container_border(swayc_t *container) { + if (container->type == C_VIEW) { + update_view_border(container); + return; + } else { + for (int i = 0; i < container->children->length; ++i) { + update_container_border(container->children->items[i]); + } + } +} + +void render_view_borders(wlc_handle view) { + swayc_t *c = swayc_by_handle(view); + + + // emulate i3 behavior for drawing borders for tabbed and stacked layouts: + // if we are not the only child in the container, always draw borders, + // regardless of the border setting on the individual view + if (!c || (c->border_type == B_NONE + && !((c->parent->layout == L_TABBED || c->parent->layout == L_STACKED) + && c->parent->children->length > 1))) { + return; + } + + if (c->border && c->border->buffer) { + wlc_pixels_write(WLC_RGBA8888, &c->border->geometry, c->border->buffer); + } +} + +bool should_hide_top_border(swayc_t *con, double y) { + // returns true if container is child of tabbed/stacked layout and is + // sharing top border with tabbed titlebar + swayc_t *par = con->parent; + while (par->type != C_WORKSPACE) { + if (par->layout == L_TABBED || par->layout == L_STACKED) { + return con->y == y; + } + con = par; + par = par->parent; + } + return false; +} diff --git a/sway/old/commands.c b/sway/old/commands.c new file mode 100644 index 00000000..e1181893 --- /dev/null +++ b/sway/old/commands.c @@ -0,0 +1,686 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sway/layout.h" +#include "sway/focus.h" +#include "sway/workspace.h" +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/output.h" +#include "sway/handlers.h" +#include "sway/input_state.h" +#include "sway/criteria.h" +#include "sway/ipc-server.h" +#include "sway/security.h" +#include "sway/input.h" +#include "sway/border.h" +#include "stringop.h" +#include "sway.h" +#include "util.h" +#include "list.h" +#include "log.h" + +struct cmd_handler { + char *command; + sway_cmd *handle; +}; + +int sp_index = 0; + +swayc_t *current_container = NULL; + +// Returns error object, or NULL if check succeeds. +struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { + struct cmd_results *error = NULL; + switch (type) { + case EXPECTED_MORE_THAN: + if (argc > val) { + return NULL; + } + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected more than %d argument%s, got %d)", + name, val, (char*[2]){"s", ""}[argc==1], argc); + break; + case EXPECTED_AT_LEAST: + if (argc >= val) { + return NULL; + } + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected at least %d argument%s, got %d)", + name, val, (char*[2]){"s", ""}[argc==1], argc); + break; + case EXPECTED_LESS_THAN: + if (argc < val) { + return NULL; + }; + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected less than %d argument%s, got %d)", + name, val, (char*[2]){"s", ""}[argc==1], argc); + break; + case EXPECTED_EQUAL_TO: + if (argc == val) { + return NULL; + }; + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected %d arguments, got %d)", name, val, argc); + break; + } + return error; +} + +void hide_view_in_scratchpad(swayc_t *sp_view) { + if (sp_view == NULL) { + return; + } + + wlc_view_set_mask(sp_view->handle, 0); + sp_view->visible = false; + swayc_t *ws = sp_view->parent; + remove_child(sp_view); + if (swayc_active_workspace() != ws && ws->floating->length == 0 && ws->children->length == 0) { + destroy_workspace(ws); + } else { + arrange_windows(ws, -1, -1); + } + set_focused_container(container_under_pointer()); +} + +void input_cmd_apply(struct input_config *input) { + int i; + i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier); + if (i >= 0) { + // merge existing config + struct input_config *ic = config->input_configs->items[i]; + merge_input_config(ic, input); + free_input_config(input); + input = ic; + } else { + list_add(config->input_configs, input); + } + + current_input_config = input; + + if (input->identifier) { + // Try to find the input device and apply configuration now. If + // this is during startup then there will be no container and config + // will be applied during normal "new input" event from wlc. + /* TODO WLR + struct libinput_device *device = NULL; + for (int i = 0; i < input_devices->length; ++i) { + device = input_devices->items[i]; + char* dev_identifier = libinput_dev_unique_id(device); + if (!dev_identifier) { + break; + } + int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0; + free(dev_identifier); + if (match) { + apply_input_config(input, device); + break; + } + } + */ + } +} + +void remove_view_from_scratchpad(swayc_t *view) { + int i; + for (i = 0; i < scratchpad->length; i++) { + if (scratchpad->items[i] == view) { + if (sp_index == 0) { + sp_index = scratchpad->length - 1; + } else { + sp_index--; + } + list_del(scratchpad, sp_index); + sp_view = NULL; + } + } +} + +/* Keep alphabetized */ +static struct cmd_handler handlers[] = { + { "assign", cmd_assign }, + { "bar", cmd_bar }, + { "bindcode", cmd_bindcode }, + { "bindsym", cmd_bindsym }, + { "border", cmd_border }, + { "client.background", cmd_client_background }, + { "client.focused", cmd_client_focused }, + { "client.focused_inactive", cmd_client_focused_inactive }, + { "client.placeholder", cmd_client_placeholder }, + { "client.unfocused", cmd_client_unfocused }, + { "client.urgent", cmd_client_urgent }, + { "clipboard", cmd_clipboard }, + { "commands", cmd_commands }, + { "debuglog", cmd_debuglog }, + { "default_border", cmd_default_border }, + { "default_floating_border", cmd_default_floating_border }, + { "default_orientation", cmd_orientation }, + { "exec", cmd_exec }, + { "exec_always", cmd_exec_always }, + { "exit", cmd_exit }, + { "floating", cmd_floating }, + { "floating_maximum_size", cmd_floating_maximum_size }, + { "floating_minimum_size", cmd_floating_minimum_size }, + { "floating_modifier", cmd_floating_mod }, + { "floating_scroll", cmd_floating_scroll }, + { "focus", cmd_focus }, + { "focus_follows_mouse", cmd_focus_follows_mouse }, + { "font", cmd_font }, + { "for_window", cmd_for_window }, + { "force_focus_wrapping", cmd_force_focus_wrapping }, + { "fullscreen", cmd_fullscreen }, + { "gaps", cmd_gaps }, + { "hide_edge_borders", cmd_hide_edge_borders }, + { "include", cmd_include }, + { "input", cmd_input }, + { "ipc", cmd_ipc }, + { "kill", cmd_kill }, + { "layout", cmd_layout }, + { "log_colors", cmd_log_colors }, + { "mark", cmd_mark }, + { "mode", cmd_mode }, + { "mouse_warping", cmd_mouse_warping }, + { "move", cmd_move }, + { "new_float", cmd_new_float }, + { "new_window", cmd_new_window }, + { "no_focus", cmd_no_focus }, + { "output", cmd_output }, + { "permit", cmd_permit }, + { "reject", cmd_reject }, + { "reload", cmd_reload }, + { "resize", cmd_resize }, + { "scratchpad", cmd_scratchpad }, + { "seamless_mouse", cmd_seamless_mouse }, + { "set", cmd_set }, + { "show_marks", cmd_show_marks }, + { "smart_gaps", cmd_smart_gaps }, + { "split", cmd_split }, + { "splith", cmd_splith }, + { "splitt", cmd_splitt }, + { "splitv", cmd_splitv }, + { "sticky", cmd_sticky }, + { "unmark", cmd_unmark }, + { "workspace", cmd_workspace }, + { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, + { "workspace_layout", cmd_workspace_layout }, +}; + +static struct cmd_handler bar_handlers[] = { + { "activate_button", bar_cmd_activate_button }, + { "binding_mode_indicator", bar_cmd_binding_mode_indicator }, + { "bindsym", bar_cmd_bindsym }, + { "colors", bar_cmd_colors }, + { "context_button", bar_cmd_context_button }, + { "font", bar_cmd_font }, + { "height", bar_cmd_height }, + { "hidden_state", bar_cmd_hidden_state }, + { "icon_theme", bar_cmd_icon_theme }, + { "id", bar_cmd_id }, + { "mode", bar_cmd_mode }, + { "modifier", bar_cmd_modifier }, + { "output", bar_cmd_output }, + { "pango_markup", bar_cmd_pango_markup }, + { "position", bar_cmd_position }, + { "secondary_button", bar_cmd_secondary_button }, + { "separator_symbol", bar_cmd_separator_symbol }, + { "status_command", bar_cmd_status_command }, + { "strip_workspace_numbers", bar_cmd_strip_workspace_numbers }, + { "swaybar_command", bar_cmd_swaybar_command }, + { "tray_output", bar_cmd_tray_output }, + { "tray_padding", bar_cmd_tray_padding }, + { "workspace_buttons", bar_cmd_workspace_buttons }, + { "wrap_scroll", bar_cmd_wrap_scroll }, +}; + +/** + * Check and add color to buffer. + * + * return error object, or NULL if color is valid. + */ +struct cmd_results *add_color(const char *name, char *buffer, const char *color) { + int len = strlen(color); + if (len != 7 && len != 9) { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + + if (color[0] != '#') { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + + int i; + for (i = 1; i < len; ++i) { + if (!isxdigit(color[i])) { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + } + + // copy color to buffer + strncpy(buffer, color, len); + // add default alpha channel if color was defined without it + if (len == 7) { + buffer[7] = 'f'; + buffer[8] = 'f'; + } + buffer[9] = '\0'; + + return NULL; +} + +static struct cmd_handler input_handlers[] = { + { "accel_profile", input_cmd_accel_profile }, + { "click_method", input_cmd_click_method }, + { "drag_lock", input_cmd_drag_lock }, + { "dwt", input_cmd_dwt }, + { "events", input_cmd_events }, + { "left_handed", input_cmd_left_handed }, + { "middle_emulation", input_cmd_middle_emulation }, + { "natural_scroll", input_cmd_natural_scroll }, + { "pointer_accel", input_cmd_pointer_accel }, + { "scroll_method", input_cmd_scroll_method }, + { "tap", input_cmd_tap }, +}; + +static struct cmd_handler bar_colors_handlers[] = { + { "active_workspace", bar_colors_cmd_active_workspace }, + { "background", bar_colors_cmd_background }, + { "binding_mode", bar_colors_cmd_binding_mode }, + { "focused_background", bar_colors_cmd_focused_background }, + { "focused_separator", bar_colors_cmd_focused_separator }, + { "focused_statusline", bar_colors_cmd_focused_statusline }, + { "focused_workspace", bar_colors_cmd_focused_workspace }, + { "inactive_workspace", bar_colors_cmd_inactive_workspace }, + { "separator", bar_colors_cmd_separator }, + { "statusline", bar_colors_cmd_statusline }, + { "urgent_workspace", bar_colors_cmd_urgent_workspace }, +}; + +static struct cmd_handler ipc_handlers[] = { + { "*", cmd_ipc_cmd }, + { "bar-config", cmd_ipc_cmd }, + { "command", cmd_ipc_cmd }, + { "events", cmd_ipc_events }, + { "inputs", cmd_ipc_cmd }, + { "marks", cmd_ipc_cmd }, + { "outputs", cmd_ipc_cmd }, + { "tree", cmd_ipc_cmd }, + { "workspaces", cmd_ipc_cmd }, +}; + +static struct cmd_handler ipc_event_handlers[] = { + { "*", cmd_ipc_event_cmd }, + { "binding", cmd_ipc_event_cmd }, + { "input", cmd_ipc_event_cmd }, + { "mode", cmd_ipc_event_cmd }, + { "output", cmd_ipc_event_cmd }, + { "window", cmd_ipc_event_cmd }, + { "workspace", cmd_ipc_event_cmd }, +}; + +static int handler_compare(const void *_a, const void *_b) { + const struct cmd_handler *a = _a; + const struct cmd_handler *b = _b; + return strcasecmp(a->command, b->command); +} + +static struct cmd_handler *find_handler(char *line, enum cmd_status block) { + struct cmd_handler d = { .command=line }; + struct cmd_handler *res = NULL; + sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT); + if (block == CMD_BLOCK_BAR) { + res = bsearch(&d, bar_handlers, + sizeof(bar_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_BAR_COLORS){ + res = bsearch(&d, bar_colors_handlers, + sizeof(bar_colors_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_INPUT) { + res = bsearch(&d, input_handlers, + sizeof(input_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_IPC) { + res = bsearch(&d, ipc_handlers, + sizeof(ipc_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_IPC_EVENTS) { + res = bsearch(&d, ipc_event_handlers, + sizeof(ipc_event_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else { + res = bsearch(&d, handlers, + sizeof(handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } + return res; +} + +struct cmd_results *handle_command(char *_exec, enum command_context context) { + // Even though this function will process multiple commands we will only + // return the last error, if any (for now). (Since we have access to an + // error string we could e.g. concatonate all errors there.) + struct cmd_results *results = NULL; + char *exec = strdup(_exec); + char *head = exec; + char *cmdlist; + char *cmd; + list_t *containers = NULL; + + head = exec; + do { + // Extract criteria (valid for this command list only). + if (*head == '[') { + ++head; + char *criteria_string = argsep(&head, "]"); + if (head) { + ++head; + list_t *tokens = create_list(); + char *error; + + if ((error = extract_crit_tokens(tokens, criteria_string))) { + results = cmd_results_new(CMD_INVALID, criteria_string, + "Can't parse criteria string: %s", error); + free(error); + free(tokens); + goto cleanup; + } + containers = container_for(tokens); + + free(tokens); + } else { + if (!results) { + results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched ["); + } + goto cleanup; + } + // Skip leading whitespace + head += strspn(head, whitespace); + } + // Split command list + cmdlist = argsep(&head, ";"); + cmdlist += strspn(cmdlist, whitespace); + do { + // Split commands + cmd = argsep(&cmdlist, ","); + cmd += strspn(cmd, whitespace); + if (strcmp(cmd, "") == 0) { + sway_log(L_INFO, "Ignoring empty command."); + continue; + } + sway_log(L_INFO, "Handling command '%s'", cmd); + //TODO better handling of argv + int argc; + char **argv = split_args(cmd, &argc); + if (strcmp(argv[0], "exec") != 0) { + int i; + for (i = 1; i < argc; ++i) { + if (*argv[i] == '\"' || *argv[i] == '\'') { + strip_quotes(argv[i]); + } + } + } + struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END); + if (!handler) { + if (results) { + free_cmd_results(results); + } + results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command"); + free_argv(argc, argv); + goto cleanup; + } + if (!(get_command_policy_mask(argv[0]) & context)) { + if (results) { + free_cmd_results(results); + } + results = cmd_results_new(CMD_INVALID, cmd, + "Permission denied for %s via %s", cmd, + command_policy_str(context)); + free_argv(argc, argv); + goto cleanup; + } + int i = 0; + do { + if (!containers) { + current_container = get_focused_container(&root_container); + } else if (containers->length == 0) { + if (results) { + free_cmd_results(results); + } + results = cmd_results_new(CMD_FAILURE, argv[0], "No matching container"); + goto cleanup; + } else { + current_container = (swayc_t *)containers->items[i]; + } + sway_log(L_INFO, "Running on container '%s'", current_container->name); + + struct cmd_results *res = handler->handle(argc-1, argv+1); + if (res->status != CMD_SUCCESS) { + free_argv(argc, argv); + if (results) { + free_cmd_results(results); + } + results = res; + goto cleanup; + } + free_cmd_results(res); + ++i; + } while(containers && i < containers->length); + + free_argv(argc, argv); + } while(cmdlist); + + if (containers) { + list_free(containers); + containers = NULL; + } + } while(head); + cleanup: + free(exec); + if (containers) { + free(containers); + } + if (!results) { + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + return results; +} + +// this is like handle_command above, except: +// 1) it ignores empty commands (empty lines) +// 2) it does variable substitution +// 3) it doesn't split commands (because the multiple commands are supposed to +// be chained together) +// 4) handle_command handles all state internally while config_command has some +// state handled outside (notably the block mode, in read_config) +struct cmd_results *config_command(char *exec, enum cmd_status block) { + struct cmd_results *results = NULL; + int argc; + char **argv = split_args(exec, &argc); + if (!argc) { + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + goto cleanup; + } + + sway_log(L_INFO, "handling config command '%s'", exec); + // Endblock + if (**argv == '}') { + results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); + goto cleanup; + } + struct cmd_handler *handler = find_handler(argv[0], block); + if (!handler) { + char *input = argv[0] ? argv[0] : "(empty)"; + results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); + goto cleanup; + } + int i; + // Var replacement, for all but first argument of set + for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) { + argv[i] = do_var_replacement(argv[i]); + unescape_string(argv[i]); + } + /* Strip quotes for first argument. + * TODO This part needs to be handled much better */ + if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) { + strip_quotes(argv[1]); + } + if (handler->handle) { + results = handler->handle(argc-1, argv+1); + } else { + results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented"); + } + +cleanup: + free_argv(argc, argv); + return results; +} + +struct cmd_results *config_commands_command(char *exec) { + struct cmd_results *results = NULL; + int argc; + char **argv = split_args(exec, &argc); + if (!argc) { + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + goto cleanup; + } + + // Find handler for the command this is setting a policy for + char *cmd = argv[0]; + + if (strcmp(cmd, "}") == 0) { + results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); + goto cleanup; + } + + struct cmd_handler *handler = find_handler(cmd, CMD_BLOCK_END); + if (!handler && strcmp(cmd, "*") != 0) { + char *input = cmd ? cmd : "(empty)"; + results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); + goto cleanup; + } + + enum command_context context = 0; + + struct { + char *name; + enum command_context context; + } context_names[] = { + { "config", CONTEXT_CONFIG }, + { "binding", CONTEXT_BINDING }, + { "ipc", CONTEXT_IPC }, + { "criteria", CONTEXT_CRITERIA }, + { "all", CONTEXT_ALL }, + }; + + for (int i = 1; i < argc; ++i) { + size_t j; + for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) { + if (strcmp(context_names[j].name, argv[i]) == 0) { + break; + } + } + if (j == sizeof(context_names) / sizeof(context_names[0])) { + results = cmd_results_new(CMD_INVALID, cmd, + "Invalid command context %s", argv[i]); + goto cleanup; + } + context |= context_names[j].context; + } + + struct command_policy *policy = NULL; + for (int i = 0; i < config->command_policies->length; ++i) { + struct command_policy *p = config->command_policies->items[i]; + if (strcmp(p->command, cmd) == 0) { + policy = p; + break; + } + } + if (!policy) { + policy = alloc_command_policy(cmd); + sway_assert(policy, "Unable to allocate security policy"); + if (policy) { + list_add(config->command_policies, policy); + } + } + policy->context = context; + + sway_log(L_INFO, "Set command policy for %s to %d", + policy->command, policy->context); + + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + +cleanup: + free_argv(argc, argv); + return results; +} + +struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) { + struct cmd_results *results = malloc(sizeof(struct cmd_results)); + if (!results) { + sway_log(L_ERROR, "Unable to allocate command results"); + return NULL; + } + results->status = status; + if (input) { + results->input = strdup(input); // input is the command name + } else { + results->input = NULL; + } + if (format) { + char *error = malloc(256); + va_list args; + va_start(args, format); + if (error) { + vsnprintf(error, 256, format, args); + } + va_end(args); + results->error = error; + } else { + results->error = NULL; + } + return results; +} + +void free_cmd_results(struct cmd_results *results) { + if (results->input) { + free(results->input); + } + if (results->error) { + free(results->error); + } + free(results); +} + +const char *cmd_results_to_json(struct cmd_results *results) { + json_object *result_array = json_object_new_array(); + json_object *root = json_object_new_object(); + json_object_object_add(root, "success", json_object_new_boolean(results->status == CMD_SUCCESS)); + if (results->input) { + json_object_object_add(root, "input", json_object_new_string(results->input)); + } + if (results->error) { + json_object_object_add(root, "error", json_object_new_string(results->error)); + } + json_object_array_add(result_array, root); + const char *json = json_object_to_json_string(result_array); + free(result_array); + free(root); + return json; +} diff --git a/sway/old/commands/assign.c b/sway/old/commands/assign.c new file mode 100644 index 00000000..c3b03bbc --- /dev/null +++ b/sway/old/commands/assign.c @@ -0,0 +1,57 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include "sway/commands.h" +#include "sway/criteria.h" +#include "list.h" +#include "log.h" + +struct cmd_results *cmd_assign(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) { + return error; + } + + char *criteria = *argv++; + + if (strncmp(*argv, "→", strlen("→")) == 0) { + if (argc < 3) { + return cmd_results_new(CMD_INVALID, "assign", "Missing workspace"); + } + argv++; + } + + char *movecmd = "move container to workspace "; + int arglen = strlen(movecmd) + strlen(*argv) + 1; + char *cmdlist = calloc(1, arglen); + if (!cmdlist) { + return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list"); + } + snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); + + struct criteria *crit = malloc(sizeof(struct criteria)); + if (!crit) { + free(cmdlist); + return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria"); + } + crit->crit_raw = strdup(criteria); + crit->cmdlist = cmdlist; + crit->tokens = create_list(); + char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); + + if (err_str) { + error = cmd_results_new(CMD_INVALID, "assign", err_str); + free(err_str); + free_criteria(crit); + } else if (crit->tokens->length == 0) { + error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); + free_criteria(crit); + } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { + sway_log(L_DEBUG, "assign: Duplicate, skipping."); + free_criteria(crit); + } else { + sway_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); + list_add(config->criteria, crit); + } + return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar.c b/sway/old/commands/bar.c new file mode 100644 index 00000000..04745a6e --- /dev/null +++ b/sway/old/commands/bar.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "log.h" +#include "util.h" + +struct cmd_results *cmd_bar(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (config->reading && strcmp("{", argv[0]) != 0) { + return cmd_results_new(CMD_INVALID, "bar", + "Expected '{' at start of bar config definition."); + } + + if (!config->reading) { + if (argc > 1) { + if (strcasecmp("mode", argv[0]) == 0) { + return bar_cmd_mode(argc-1, argv + 1); + } + + if (strcasecmp("hidden_state", argv[0]) == 0) { + return bar_cmd_hidden_state(argc-1, argv + 1); + } + } + + return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file."); + } + + // Create new bar with default values + struct bar_config *bar = default_bar_config(); + if (!bar) { + return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state"); + } + + // set bar id + int i; + for (i = 0; i < config->bars->length; ++i) { + if (bar == config->bars->items[i]) { + const int len = 5 + numlen(i); // "bar-" + i + \0 + bar->id = malloc(len * sizeof(char)); + if (bar->id) { + snprintf(bar->id, len, "bar-%d", i); + } else { + return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID"); + } + break; + } + } + + // Set current bar + config->current_bar = bar; + sway_log(L_DEBUG, "Configuring bar %s", bar->id); + return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL); +} diff --git a/sway/old/commands/bar/activate_button.c b/sway/old/commands/bar/activate_button.c new file mode 100644 index 00000000..32a1d3e5 --- /dev/null +++ b/sway/old/commands/bar/activate_button.c @@ -0,0 +1,26 @@ +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_activate_button(int argc, char **argv) { + const char *cmd_name = "activate_button"; +#ifndef ENABLE_TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " + "%s called, but sway was compiled without tray support", + cmd_name, cmd_name); +#else + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); + } + + // User should be able to prefix with 0x or whatever they want + config->current_bar->secondary_button = strtoul(argv[0], NULL, 0); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +#endif +} diff --git a/sway/old/commands/bar/binding_mode_indicator.c b/sway/old/commands/bar/binding_mode_indicator.c new file mode 100644 index 00000000..64f5b84f --- /dev/null +++ b/sway/old/commands/bar/binding_mode_indicator.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_binding_mode_indicator(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "binding_mode_indicator", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->binding_mode_indicator = true; + sway_log(L_DEBUG, "Enabling binding mode indicator on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->binding_mode_indicator = false; + sway_log(L_DEBUG, "Disabling binding mode indicator on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "binding_mode_indicator", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/bindsym.c b/sway/old/commands/bar/bindsym.c new file mode 100644 index 00000000..5f90b51a --- /dev/null +++ b/sway/old/commands/bar/bindsym.c @@ -0,0 +1,48 @@ +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { + return error; + } else if (!config->reading) { + return cmd_results_new(CMD_FAILURE, "bindsym", "Can only be used in config file."); + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "bindsym", "No bar defined."); + } + + if (strlen(argv[1]) != 7) { + return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); + } + uint32_t numbutton = (uint32_t)atoi(argv[1] + 6); + if (numbutton < 1 || numbutton > 5 || strncmp(argv[1], "button", 6) != 0) { + return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); + } + struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); + } + binding->button = numbutton; + binding->command = join_args(argv + 1, argc - 1); + + struct bar_config *bar = config->current_bar; + int i = list_seq_find(bar->bindings, sway_mouse_binding_cmp_buttons, binding); + if (i > -1) { + sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); + struct sway_mouse_binding *dup = bar->bindings->items[i]; + free_sway_mouse_binding(dup); + list_del(bar->bindings, i); + } + list_add(bar->bindings, binding); + list_qsort(bar->bindings, sway_mouse_binding_cmp_qsort); + + sway_log(L_DEBUG, "bindsym - Bound %s to command %s when clicking swaybar", argv[0], binding->command); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/colors.c b/sway/old/commands/bar/colors.c new file mode 100644 index 00000000..8b3b0aac --- /dev/null +++ b/sway/old/commands/bar/colors.c @@ -0,0 +1,129 @@ +#include +#include "sway/commands.h" + +static struct cmd_results *parse_single_color(char **color, const char *cmd_name, int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!*color) { + *color = malloc(10); + if (!*color) { + return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); + } + } + + error = add_color(cmd_name, *color, argv[0]); + if (error) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *parse_three_colors(char ***colors, const char *cmd_name, int argc, char **argv) { + struct cmd_results *error = NULL; + if (argc != 3) { + return cmd_results_new(CMD_INVALID, cmd_name, "Requires exactly three color values"); + } + + int i; + for (i = 0; i < 3; i++) { + if (!*colors[i]) { + *(colors[i]) = malloc(10); + if (!*(colors[i])) { + return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); + } + } + error = add_color(cmd_name, *(colors[i]), argv[i]); + if (error) { + return error; + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *bar_cmd_colors(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcmp("{", argv[0]) != 0) { + return cmd_results_new(CMD_INVALID, "colors", + "Expected '{' at the start of colors config definition."); + } + + return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL); +} + +struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.active_workspace_border), + &(config->current_bar->colors.active_workspace_bg), + &(config->current_bar->colors.active_workspace_text) + }; + return parse_three_colors(colors, "active_workspace", argc, argv); +} + +struct cmd_results *bar_colors_cmd_background(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.background), "background", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_background(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.focused_background), "focused_background", argc, argv); +} + +struct cmd_results *bar_colors_cmd_binding_mode(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.binding_mode_border), + &(config->current_bar->colors.binding_mode_bg), + &(config->current_bar->colors.binding_mode_text) + }; + return parse_three_colors(colors, "binding_mode", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.focused_workspace_border), + &(config->current_bar->colors.focused_workspace_bg), + &(config->current_bar->colors.focused_workspace_text) + }; + return parse_three_colors(colors, "focused_workspace", argc, argv); +} + +struct cmd_results *bar_colors_cmd_inactive_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.inactive_workspace_border), + &(config->current_bar->colors.inactive_workspace_bg), + &(config->current_bar->colors.inactive_workspace_text) + }; + return parse_three_colors(colors, "inactive_workspace", argc, argv); +} + +struct cmd_results *bar_colors_cmd_separator(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.separator), "separator", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_separator(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); +} + +struct cmd_results *bar_colors_cmd_statusline(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.statusline), "statusline", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_statusline(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); +} + +struct cmd_results *bar_colors_cmd_urgent_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.urgent_workspace_border), + &(config->current_bar->colors.urgent_workspace_bg), + &(config->current_bar->colors.urgent_workspace_text) + }; + return parse_three_colors(colors, "urgent_workspace", argc, argv); +} diff --git a/sway/old/commands/bar/context_button.c b/sway/old/commands/bar/context_button.c new file mode 100644 index 00000000..6d7d7aec --- /dev/null +++ b/sway/old/commands/bar/context_button.c @@ -0,0 +1,26 @@ +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_context_button(int argc, char **argv) { + const char *cmd_name = "context_button"; +#ifndef ENABLE_TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " + "%s called, but sway was compiled without tray support", + cmd_name, cmd_name); +#else + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); + } + + // User should be able to prefix with 0x or whatever they want + config->current_bar->context_button = strtoul(argv[0], NULL, 0); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +#endif +} diff --git a/sway/old/commands/bar/font.c b/sway/old/commands/bar/font.c new file mode 100644 index 00000000..c586c5bc --- /dev/null +++ b/sway/old/commands/bar/font.c @@ -0,0 +1,26 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_font(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "font", "No bar defined."); + } + + char *font = join_args(argv, argc); + free(config->current_bar->font); + if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) { + config->current_bar->font = font; + } else { + config->current_bar->font = font; + } + + sway_log(L_DEBUG, "Settings font '%s' for bar: %s", config->current_bar->font, config->current_bar->id); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/height.c b/sway/old/commands/bar/height.c new file mode 100644 index 00000000..eb576ab3 --- /dev/null +++ b/sway/old/commands/bar/height.c @@ -0,0 +1,21 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_height(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "height", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + int height = atoi(argv[0]); + if (height < 0) { + return cmd_results_new(CMD_INVALID, "height", + "Invalid height value: %s", argv[0]); + } + + config->current_bar->height = height; + sway_log(L_DEBUG, "Setting bar height to %d on bar: %s", height, config->current_bar->id); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/hidden_state.c b/sway/old/commands/bar/hidden_state.c new file mode 100644 index 00000000..0b49aa6b --- /dev/null +++ b/sway/old/commands/bar/hidden_state.c @@ -0,0 +1,79 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/ipc-server.h" +#include "log.h" + +static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, const char *hidden_state) { + char *old_state = bar->hidden_state; + if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) { + if (strcasecmp("hide", bar->hidden_state) == 0) { + bar->hidden_state = strdup("show"); + } else if (strcasecmp("show", bar->hidden_state) == 0) { + bar->hidden_state = strdup("hide"); + } + } else if (strcasecmp("hide", hidden_state) == 0) { + bar->hidden_state = strdup("hide"); + } else if (strcasecmp("show", hidden_state) == 0) { + bar->hidden_state = strdup("show"); + } else { + return cmd_results_new(CMD_INVALID, "hidden_state", "Invalid value %s", hidden_state); + } + + if (strcmp(old_state, bar->hidden_state) != 0) { + if (!config->reading) { + ipc_event_barconfig_update(bar); + } + sway_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", bar->hidden_state, bar->id); + } + + // free old mode + free(old_state); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) { + return error; + } + if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) { + return error; + } + + if (config->reading && argc > 1) { + return cmd_results_new(CMD_INVALID, "hidden_state", "Unexpected value %s in config mode", argv[1]); + } + + const char *state = argv[0]; + + if (config->reading) { + return bar_set_hidden_state(config->current_bar, state); + } + + const char *id = NULL; + if (argc == 2) { + id = argv[1]; + } + + int i; + struct bar_config *bar; + for (i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + if (id && strcmp(id, bar->id) == 0) { + return bar_set_hidden_state(bar, state); + } + + error = bar_set_hidden_state(bar, state); + if (error) { + return error; + } + } + + // active bar modifiers might have changed. + update_active_bar_modifiers(); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/icon_theme.c b/sway/old/commands/bar/icon_theme.c new file mode 100644 index 00000000..cbfc0be5 --- /dev/null +++ b/sway/old/commands/bar/icon_theme.c @@ -0,0 +1,25 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" + +struct cmd_results *bar_cmd_icon_theme(int argc, char **argv) { + const char *cmd_name = "tray_output"; +#ifndef ENABLE_TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " + "%s called, but sway was compiled without tray support", + cmd_name, cmd_name); +#else + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); + } + + config->current_bar->icon_theme = strdup(argv[0]); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +#endif +} diff --git a/sway/old/commands/bar/id.c b/sway/old/commands/bar/id.c new file mode 100644 index 00000000..1221ebf6 --- /dev/null +++ b/sway/old/commands/bar/id.c @@ -0,0 +1,33 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_id(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "id", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + const char *name = argv[0]; + const char *oldname = config->current_bar->id; + + // check if id is used by a previously defined bar + int i; + for (i = 0; i < config->bars->length; ++i) { + struct bar_config *find = config->bars->items[i]; + if (strcmp(name, find->id) == 0 && config->current_bar != find) { + return cmd_results_new(CMD_FAILURE, "id", + "Id '%s' already defined for another bar. Id unchanged (%s).", + name, oldname); + } + } + + sway_log(L_DEBUG, "Renaming bar: '%s' to '%s'", oldname, name); + + // free old bar id + free(config->current_bar->id); + + config->current_bar->id = strdup(name); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/mode.c b/sway/old/commands/bar/mode.c new file mode 100644 index 00000000..36816b93 --- /dev/null +++ b/sway/old/commands/bar/mode.c @@ -0,0 +1,81 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/ipc-server.h" +#include "log.h" + +static struct cmd_results *bar_set_mode(struct bar_config *bar, const char *mode) { + char *old_mode = bar->mode; + if (strcasecmp("toggle", mode) == 0 && !config->reading) { + if (strcasecmp("dock", bar->mode) == 0) { + bar->mode = strdup("hide"); + } else if (strcasecmp("hide", bar->mode) == 0) { + bar->mode = strdup("dock"); + } + } else if (strcasecmp("dock", mode) == 0) { + bar->mode = strdup("dock"); + } else if (strcasecmp("hide", mode) == 0) { + bar->mode = strdup("hide"); + } else if (strcasecmp("invisible", mode) == 0) { + bar->mode = strdup("invisible"); + } else { + return cmd_results_new(CMD_INVALID, "mode", "Invalid value %s", mode); + } + + if (strcmp(old_mode, bar->mode) != 0) { + if (!config->reading) { + ipc_event_barconfig_update(bar); + + // active bar modifiers might have changed. + update_active_bar_modifiers(); + } + sway_log(L_DEBUG, "Setting mode: '%s' for bar: %s", bar->mode, bar->id); + } + + // free old mode + free(old_mode); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *bar_cmd_mode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { + return error; + } + if ((error = checkarg(argc, "mode", EXPECTED_LESS_THAN, 3))) { + return error; + } + + if (config->reading && argc > 1) { + return cmd_results_new(CMD_INVALID, "mode", "Unexpected value %s in config mode", argv[1]); + } + + const char *mode = argv[0]; + + if (config->reading) { + return bar_set_mode(config->current_bar, mode); + } + + const char *id = NULL; + if (argc == 2) { + id = argv[1]; + } + + int i; + struct bar_config *bar; + for (i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + if (id && strcmp(id, bar->id) == 0) { + return bar_set_mode(bar, mode); + } + + error = bar_set_mode(bar, mode); + if (error) { + return error; + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/modifier.c b/sway/old/commands/bar/modifier.c new file mode 100644 index 00000000..153d87e6 --- /dev/null +++ b/sway/old/commands/bar/modifier.c @@ -0,0 +1,35 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" +#include "util.h" + +struct cmd_results *bar_cmd_modifier(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "modifier", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "modifier", "No bar defined."); + } + + uint32_t mod = 0; + + list_t *split = split_string(argv[0], "+"); + for (int i = 0; i < split->length; ++i) { + uint32_t tmp_mod; + if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) { + mod |= tmp_mod; + continue; + } else { + free_flat_list(split); + return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]); + } + } + free_flat_list(split); + + config->current_bar->modifier = mod; + sway_log(L_DEBUG, "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/output.c b/sway/old/commands/bar/output.c new file mode 100644 index 00000000..a5710bc0 --- /dev/null +++ b/sway/old/commands/bar/output.c @@ -0,0 +1,50 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "list.h" +#include "log.h" + +struct cmd_results *bar_cmd_output(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "output", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "output", "No bar defined."); + } + + const char *output = argv[0]; + list_t *outputs = config->current_bar->outputs; + if (!outputs) { + outputs = create_list(); + config->current_bar->outputs = outputs; + } + + int i; + int add_output = 1; + if (strcmp("*", output) == 0) { + // remove all previous defined outputs and replace with '*' + for (i = 0; i < outputs->length; ++i) { + free(outputs->items[i]); + list_del(outputs, i); + } + } else { + // only add output if not already defined with either the same + // name or as '*' + for (i = 0; i < outputs->length; ++i) { + const char *find = outputs->items[i]; + if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { + add_output = 0; + break; + } + } + } + + if (add_output) { + list_add(outputs, strdup(output)); + sway_log(L_DEBUG, "Adding bar: '%s' to output '%s'", config->current_bar->id, output); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/pango_markup.c b/sway/old/commands/bar/pango_markup.c new file mode 100644 index 00000000..f69e882f --- /dev/null +++ b/sway/old/commands/bar/pango_markup.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_pango_markup(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "pango_markup", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "pango_markup", "No bar defined."); + } + + if (strcasecmp("enabled", argv[0]) == 0) { + config->current_bar->pango_markup = true; + sway_log(L_DEBUG, "Enabling pango markup for bar: %s", config->current_bar->id); + } else if (strcasecmp("disabled", argv[0]) == 0) { + config->current_bar->pango_markup = false; + sway_log(L_DEBUG, "Disabling pango markup for bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "pango_markup", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/position.c b/sway/old/commands/bar/position.c new file mode 100644 index 00000000..50de58e2 --- /dev/null +++ b/sway/old/commands/bar/position.c @@ -0,0 +1,33 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_position(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "position", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "position", "No bar defined."); + } + + if (strcasecmp("top", argv[0]) == 0) { + config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_TOP; + } else if (strcasecmp("bottom", argv[0]) == 0) { + config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; + } else if (strcasecmp("left", argv[0]) == 0) { + sway_log(L_INFO, "Warning: swaybar currently only supports top and bottom positioning. YMMV"); + config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_LEFT; + } else if (strcasecmp("right", argv[0]) == 0) { + sway_log(L_INFO, "Warning: swaybar currently only supports top and bottom positioning. YMMV"); + config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_RIGHT; + } else { + error = cmd_results_new(CMD_INVALID, "position", "Invalid value %s", argv[0]); + return error; + } + + sway_log(L_DEBUG, "Setting bar position '%s' for bar: %s", argv[0], config->current_bar->id); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/secondary_button.c b/sway/old/commands/bar/secondary_button.c new file mode 100644 index 00000000..745045c5 --- /dev/null +++ b/sway/old/commands/bar/secondary_button.c @@ -0,0 +1,26 @@ +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_secondary_button(int argc, char **argv) { + const char *cmd_name = "secondary_button"; +#ifndef ENABLE_TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " + "%s called, but sway was compiled without tray support", + cmd_name, cmd_name); +#else + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); + } + + // User should be able to prefix with 0x or whatever they want + config->current_bar->secondary_button = strtoul(argv[0], NULL, 0); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +#endif +} diff --git a/sway/old/commands/bar/separator_symbol.c b/sway/old/commands/bar/separator_symbol.c new file mode 100644 index 00000000..2766d8a2 --- /dev/null +++ b/sway/old/commands/bar/separator_symbol.c @@ -0,0 +1,21 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_separator_symbol(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "separator_symbol", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "separator_symbol", "No bar defined."); + } + + free(config->current_bar->separator_symbol); + config->current_bar->separator_symbol = strdup(argv[0]); + sway_log(L_DEBUG, "Settings separator_symbol '%s' for bar: %s", config->current_bar->separator_symbol, config->current_bar->id); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/status_command.c b/sway/old/commands/bar/status_command.c new file mode 100644 index 00000000..b227ac47 --- /dev/null +++ b/sway/old/commands/bar/status_command.c @@ -0,0 +1,21 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_status_command(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "status_command", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "status_command", "No bar defined."); + } + + free(config->current_bar->status_command); + config->current_bar->status_command = join_args(argv, argc); + sway_log(L_DEBUG, "Feeding bar with status command: %s", config->current_bar->status_command); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/strip_workspace_numbers.c b/sway/old/commands/bar/strip_workspace_numbers.c new file mode 100644 index 00000000..9ac32482 --- /dev/null +++ b/sway/old/commands/bar/strip_workspace_numbers.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "strip_workspace_numbers", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->strip_workspace_numbers = true; + sway_log(L_DEBUG, "Stripping workspace numbers on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->strip_workspace_numbers = false; + sway_log(L_DEBUG, "Enabling workspace numbers on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "strip_workspace_numbers", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/swaybar_command.c b/sway/old/commands/bar/swaybar_command.c new file mode 100644 index 00000000..452e2df5 --- /dev/null +++ b/sway/old/commands/bar/swaybar_command.c @@ -0,0 +1,21 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_swaybar_command(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "swaybar_command", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "swaybar_command", "No bar defined."); + } + + free(config->current_bar->swaybar_command); + config->current_bar->swaybar_command = join_args(argv, argc); + sway_log(L_DEBUG, "Using custom swaybar command: %s", config->current_bar->swaybar_command); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/tray_output.c b/sway/old/commands/bar/tray_output.c new file mode 100644 index 00000000..012304a9 --- /dev/null +++ b/sway/old/commands/bar/tray_output.c @@ -0,0 +1,29 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" + +struct cmd_results *bar_cmd_tray_output(int argc, char **argv) { + const char *cmd_name = "tray_output"; +#ifndef ENABLE_TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command " + "%s called, but sway was compiled without tray support", + cmd_name, cmd_name); +#else + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); + } + + if (strcmp(argv[0], "all") == 0) { + // Default behaviour + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + config->current_bar->tray_output = strdup(argv[0]); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +#endif +} diff --git a/sway/old/commands/bar/tray_padding.c b/sway/old/commands/bar/tray_padding.c new file mode 100644 index 00000000..ac0572ce --- /dev/null +++ b/sway/old/commands/bar/tray_padding.c @@ -0,0 +1,34 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) { + const char *cmd_name = "tray_padding"; +#ifndef ENABLE_TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command" + "%s called, but sway was compiled without tray support", + cmd_name, cmd_name); +#else + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, cmd_name, "No bar defined."); + } + + if (argc == 1 || (argc == 2 && strcasecmp("px", argv[1]) == 0)) { + char *inv; + uint32_t padding = strtoul(argv[0], &inv, 10); + if (*inv == '\0' || strcasecmp(inv, "px") == 0) { + config->current_bar->tray_padding = padding; + sway_log(L_DEBUG, "Enabling tray padding of %d px on bar: %s", padding, config->current_bar->id); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + } + return cmd_results_new(CMD_FAILURE, cmd_name, + "Expected 'tray_padding [px]'"); +#endif +} diff --git a/sway/old/commands/bar/workspace_buttons.c b/sway/old/commands/bar/workspace_buttons.c new file mode 100644 index 00000000..67dd2d31 --- /dev/null +++ b/sway/old/commands/bar/workspace_buttons.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "workspace_buttons", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->workspace_buttons = true; + sway_log(L_DEBUG, "Enabling workspace buttons on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->workspace_buttons = false; + sway_log(L_DEBUG, "Disabling workspace buttons on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bar/wrap_scroll.c b/sway/old/commands/bar/wrap_scroll.c new file mode 100644 index 00000000..4ed1f12a --- /dev/null +++ b/sway/old/commands/bar/wrap_scroll.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_wrap_scroll(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "wrap_scroll", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "wrap_scroll", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->wrap_scroll = true; + sway_log(L_DEBUG, "Enabling wrap scroll on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->wrap_scroll = false; + sway_log(L_DEBUG, "Disabling wrap scroll on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "wrap_scroll", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/bind.c b/sway/old/commands/bind.c new file mode 100644 index 00000000..d9ea37b7 --- /dev/null +++ b/sway/old/commands/bind.c @@ -0,0 +1,170 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/input_state.h" +#include "list.h" +#include "log.h" +#include "stringop.h" +#include "util.h" + +int binding_order = 0; + +struct cmd_results *cmd_bindsym(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { + return error; + } + + struct sway_binding *binding = malloc(sizeof(struct sway_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bindsym", + "Unable to allocate binding"); + } + binding->keys = create_list(); + binding->modifiers = 0; + binding->release = false; + binding->bindcode = false; + + // Handle --release + if (strcmp("--release", argv[0]) == 0) { + if (argc >= 3) { + binding->release = true; + argv++; + argc--; + } else { + free_sway_binding(binding); + return cmd_results_new(CMD_FAILURE, "bindsym", + "Invalid bindsym command " + "(expected more than 2 arguments, got %d)", argc); + } + } + + binding->command = join_args(argv + 1, argc - 1); + + list_t *split = split_string(argv[0], "+"); + for (int i = 0; i < split->length; ++i) { + // Check for a modifier key + uint32_t mod; + if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { + binding->modifiers |= mod; + continue; + } + // Check for xkb key + xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], + XKB_KEYSYM_CASE_INSENSITIVE); + + // Check for mouse binding + if (strncasecmp(split->items[i], "button", strlen("button")) == 0 && + strlen(split->items[i]) == strlen("button0")) { + sym = ((char *)split->items[i])[strlen("button")] - '1' + M_LEFT_CLICK; + } + if (!sym) { + struct cmd_results *ret = cmd_results_new(CMD_INVALID, "bindsym", + "Unknown key '%s'", (char *)split->items[i]); + free_sway_binding(binding); + free_flat_list(split); + return ret; + } + xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); + if (!key) { + free_sway_binding(binding); + free_flat_list(split); + return cmd_results_new(CMD_FAILURE, "bindsym", + "Unable to allocate binding"); + } + *key = sym; + list_add(binding->keys, key); + } + free_flat_list(split); + + struct sway_mode *mode = config->current_mode; + int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding); + if (i > -1) { + sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]); + struct sway_binding *dup = mode->bindings->items[i]; + free_sway_binding(dup); + list_del(mode->bindings, i); + } + binding->order = binding_order++; + list_add(mode->bindings, binding); + list_qsort(mode->bindings, sway_binding_cmp_qsort); + + sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_bindcode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bindcode", EXPECTED_MORE_THAN, 1))) { + return error; + } + + struct sway_binding *binding = malloc(sizeof(struct sway_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bindsym", + "Unable to allocate binding"); + } + binding->keys = create_list(); + binding->modifiers = 0; + binding->release = false; + binding->bindcode = true; + + // Handle --release + if (strcmp("--release", argv[0]) == 0) { + if (argc >= 3) { + binding->release = true; + argv++; + argc--; + } else { + free_sway_binding(binding); + return cmd_results_new(CMD_FAILURE, "bindcode", + "Invalid bindcode command " + "(expected more than 2 arguments, got %d)", argc); + } + } + + binding->command = join_args(argv + 1, argc - 1); + + list_t *split = split_string(argv[0], "+"); + for (int i = 0; i < split->length; ++i) { + // Check for a modifier key + uint32_t mod; + if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { + binding->modifiers |= mod; + continue; + } + // parse keycode + xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10); + if (!xkb_keycode_is_legal_ext(keycode)) { + error = cmd_results_new(CMD_INVALID, "bindcode", "Invalid keycode '%s'", (char *)split->items[i]); + free_sway_binding(binding); + list_free(split); + return error; + } + xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t)); + *key = keycode - 8; + list_add(binding->keys, key); + } + free_flat_list(split); + + struct sway_mode *mode = config->current_mode; + int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding); + if (i > -1) { + struct sway_binding *dup = mode->bindings->items[i]; + if (dup->bindcode) { + sway_log(L_DEBUG, "bindcode - '%s' already exists, overwriting", argv[0]); + } else { + sway_log(L_DEBUG, "bindcode - '%s' already exists as bindsym, overwriting", argv[0]); + } + free_sway_binding(dup); + list_del(mode->bindings, i); + } + binding->order = binding_order++; + list_add(mode->bindings, binding); + list_qsort(mode->bindings, sway_binding_cmp_qsort); + + sway_log(L_DEBUG, "bindcode - Bound %s to command %s", argv[0], binding->command); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/border.c b/sway/old/commands/border.c new file mode 100644 index 00000000..c888622e --- /dev/null +++ b/sway/old/commands/border.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" + +struct cmd_results *cmd_border(int argc, char **argv) { + struct cmd_results *error = NULL; + if (!config->active) { + return cmd_results_new(CMD_FAILURE, "border", "Can only be used when sway is running."); + } + if ((error = checkarg(argc, "border", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (argc > 2) { + return cmd_results_new(CMD_INVALID, "border", + "Expected 'border []"); + } + + swayc_t *view = current_container; + enum swayc_border_types border = view->border_type; + int thickness = view->border_thickness; + + if (strcasecmp(argv[0], "none") == 0) { + border = B_NONE; + } else if (strcasecmp(argv[0], "normal") == 0) { + border = B_NORMAL; + } else if (strcasecmp(argv[0], "pixel") == 0) { + border = B_PIXEL; + } else if (strcasecmp(argv[0], "toggle") == 0) { + switch (border) { + case B_NONE: + border = B_PIXEL; + break; + case B_NORMAL: + border = B_NONE; + break; + case B_PIXEL: + border = B_NORMAL; + break; + } + } else { + return cmd_results_new(CMD_INVALID, "border", + "Expected 'border "); + } + + if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { + thickness = (int)strtol(argv[1], NULL, 10); + if (errno == ERANGE || thickness < 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "border", "Number is out of range."); + } + } + + if (view) { + view->border_type = border; + view->border_thickness = thickness; + update_geometry(view); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/client.c b/sway/old/commands/client.c new file mode 100644 index 00000000..f3d879cd --- /dev/null +++ b/sway/old/commands/client.c @@ -0,0 +1,72 @@ +#include +#include +#include "sway/commands.h" + +static struct cmd_results *parse_border_color(struct border_colors *border_colors, const char *cmd_name, int argc, char **argv) { + struct cmd_results *error = NULL; + if (argc < 3 || argc > 5) { + return cmd_results_new(CMD_INVALID, cmd_name, "Requires between three and five color values"); + } + + uint32_t *colors[5] = { + &border_colors->border, + &border_colors->background, + &border_colors->text, + &border_colors->indicator, + &border_colors->child_border + }; + int i; + for (i = 0; i < argc; i++) { + char buffer[10]; + error = add_color(cmd_name, buffer, argv[i]); + if (error) { + return error; + } + *colors[i] = strtoul(buffer + 1, NULL, 16); + } + + if (argc < 5) { + border_colors->child_border = border_colors->background; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_client_focused(int argc, char **argv) { + return parse_border_color(&config->border_colors.focused, "client.focused", argc, argv); +} + +struct cmd_results *cmd_client_focused_inactive(int argc, char **argv) { + return parse_border_color(&config->border_colors.focused_inactive, "client.focused_inactive", argc, argv); +} + +struct cmd_results *cmd_client_unfocused(int argc, char **argv) { + return parse_border_color(&config->border_colors.unfocused, "client.unfocused", argc, argv); +} + +struct cmd_results *cmd_client_urgent(int argc, char **argv) { + return parse_border_color(&config->border_colors.urgent, "client.urgent", argc, argv); +} + +struct cmd_results *cmd_client_placeholder(int argc, char **argv) { + return parse_border_color(&config->border_colors.placeholder, "client.placeholder", argc, argv); +} + +struct cmd_results *cmd_client_background(int argc, char **argv) { + char buffer[10]; + struct cmd_results *error = NULL; + uint32_t background; + + if (argc != 1) { + return cmd_results_new(CMD_INVALID, "client.background", "Requires exactly one color value"); + } + + error = add_color("client.background", buffer, argv[0]); + if (error) { + return error; + } + + background = strtoul(buffer+1, NULL, 16); + config->border_colors.background = background; + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/clipboard.c b/sway/old/commands/clipboard.c new file mode 100644 index 00000000..95514e78 --- /dev/null +++ b/sway/old/commands/clipboard.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "sway/commands.h" +#include "stringop.h" + +static void send_clipboard(void *data, const char *type, int fd) { + if (strcmp(type, "text/plain") != 0 + && strcmp(type, "text/plain;charset=utf-8") != 0) { + close(fd); + return; + } + + const char *str = data; + write(fd, str, strlen(str)); + close(fd); +} + +struct cmd_results *cmd_clipboard(int argc, char **argv) { + static char *current_data = NULL; + + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "clipboard", EXPECTED_AT_LEAST, 1))) { + return error; + } + + static const char *types[2] = { + "text/plain", + "text/plain;charset=utf-8" + }; + + char *str = join_args(argv, argc); + wlc_set_selection(str, types, 2, &send_clipboard); + + free(current_data); + current_data = str; + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/commands.c b/sway/old/commands/commands.c new file mode 100644 index 00000000..0c64970c --- /dev/null +++ b/sway/old/commands/commands.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" + +struct cmd_results *cmd_commands(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "commands", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if ((error = check_security_config())) { + return error; + } + + if (strcmp(argv[0], "{") != 0) { + return cmd_results_new(CMD_FAILURE, "commands", "Expected block declaration"); + } + + if (!config->reading) { + return cmd_results_new(CMD_FAILURE, "commands", "Can only be used in config file."); + } + + return cmd_results_new(CMD_BLOCK_COMMANDS, NULL, NULL); +} diff --git a/sway/old/commands/debuglog.c b/sway/old/commands/debuglog.c new file mode 100644 index 00000000..658d6165 --- /dev/null +++ b/sway/old/commands/debuglog.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *cmd_debuglog(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "debuglog", EXPECTED_EQUAL_TO, 1))) { + return error; + } else if (strcasecmp(argv[0], "toggle") == 0) { + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "debuglog toggle", "Can't be used in config file."); + } + if (toggle_debug_logging()) { + sway_log(L_DEBUG, "Debuglog turned on."); + } + } else if (strcasecmp(argv[0], "on") == 0) { + set_log_level(L_DEBUG); + sway_log(L_DEBUG, "Debuglog turned on."); + } else if (strcasecmp(argv[0], "off") == 0) { + reset_log_level(); + } else { + return cmd_results_new(CMD_FAILURE, "debuglog", "Expected 'debuglog on|off|toggle'"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + diff --git a/sway/old/commands/default_border.c b/sway/old/commands/default_border.c new file mode 100644 index 00000000..8fbe8d19 --- /dev/null +++ b/sway/old/commands/default_border.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" + +struct cmd_results *cmd_default_border(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "default_border", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (argc > 2) { + return cmd_results_new(CMD_INVALID, "default_border", + "Expected 'default_border []"); + } + + enum swayc_border_types border = config->border; + int thickness = config->border_thickness; + + if (strcasecmp(argv[0], "none") == 0) { + border = B_NONE; + } else if (strcasecmp(argv[0], "normal") == 0) { + border = B_NORMAL; + } else if (strcasecmp(argv[0], "pixel") == 0) { + border = B_PIXEL; + } else { + return cmd_results_new(CMD_INVALID, "default_border", + "Expected 'default_border []"); + } + + if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { + thickness = (int)strtol(argv[1], NULL, 10); + if (errno == ERANGE || thickness < 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "default_border", "Number is out out of range."); + } + } + + config->border = border; + config->border_thickness = thickness; + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/default_floating_border.c b/sway/old/commands/default_floating_border.c new file mode 100644 index 00000000..fb48c1c0 --- /dev/null +++ b/sway/old/commands/default_floating_border.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" + +struct cmd_results *cmd_default_floating_border(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "default_floating_border", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (argc > 2) { + return cmd_results_new(CMD_INVALID, "default_floating_border", + "Expected 'default_floating_border []"); + } + + enum swayc_border_types border = config->floating_border; + int thickness = config->floating_border_thickness; + + if (strcasecmp(argv[0], "none") == 0) { + border = B_NONE; + } else if (strcasecmp(argv[0], "normal") == 0) { + border = B_NORMAL; + } else if (strcasecmp(argv[0], "pixel") == 0) { + border = B_PIXEL; + } else { + return cmd_results_new(CMD_INVALID, "default_floating_border", + "Expected 'default_floating_border []"); + } + + if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { + thickness = (int)strtol(argv[1], NULL, 10); + if (errno == ERANGE || thickness < 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "default_floating_border", + "Number is out out of range."); + } + } + + config->floating_border = border; + config->floating_border_thickness = thickness; + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/exec.c b/sway/old/commands/exec.c new file mode 100644 index 00000000..58ef5f94 --- /dev/null +++ b/sway/old/commands/exec.c @@ -0,0 +1,16 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_exec(int argc, char **argv) { + if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); + if (config->reloading) { + char *args = join_args(argv, argc); + sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args); + free(args); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + return cmd_exec_always(argc, argv); +} + diff --git a/sway/old/commands/exec_always.c b/sway/old/commands/exec_always.c new file mode 100644 index 00000000..ab2d8622 --- /dev/null +++ b/sway/old/commands/exec_always.c @@ -0,0 +1,85 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_exec_always(int argc, char **argv) { + struct cmd_results *error = NULL; + if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL); + if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) { + return error; + } + + char *tmp = NULL; + if (strcmp((char*)*argv, "--no-startup-id") == 0) { + sway_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored."); + if ((error = checkarg(argc - 1, "exec_always", EXPECTED_MORE_THAN, 0))) { + return error; + } + + tmp = join_args(argv + 1, argc - 1); + } else { + tmp = join_args(argv, argc); + } + + // Put argument into cmd array + char cmd[4096]; + strncpy(cmd, tmp, sizeof(cmd)); + cmd[sizeof(cmd) - 1] = 0; + free(tmp); + sway_log(L_DEBUG, "Executing %s", cmd); + + int fd[2]; + if (pipe(fd) != 0) { + sway_log(L_ERROR, "Unable to create pipe for fork"); + } + + pid_t pid; + pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space + if (!child) { + return cmd_results_new(CMD_FAILURE, "exec_always", "Unable to allocate child pid"); + } + // Fork process + if ((pid = fork()) == 0) { + // Fork child process again + setsid(); + if ((*child = fork()) == 0) { + execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL); + /* Not reached */ + } + close(fd[0]); + ssize_t s = 0; + while ((size_t)s < sizeof(pid_t)) { + s += write(fd[1], ((uint8_t *)child) + s, sizeof(pid_t) - s); + } + close(fd[1]); + _exit(0); // Close child process + } else if (pid < 0) { + free(child); + return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed"); + } + close(fd[1]); // close write + ssize_t s = 0; + while ((size_t)s < sizeof(pid_t)) { + s += read(fd[0], ((uint8_t *)child) + s, sizeof(pid_t) - s); + } + close(fd[0]); + // cleanup child process + wait(0); + swayc_t *ws = swayc_active_workspace(); + if (*child > 0 && ws) { + sway_log(L_DEBUG, "Child process created with pid %d for workspace %s", *child, ws->name); + struct pid_workspace *pw = malloc(sizeof(struct pid_workspace)); + pw->pid = child; + pw->workspace = strdup(ws->name); + pid_workspace_add(pw); + } else { + free(child); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/exit.c b/sway/old/commands/exit.c new file mode 100644 index 00000000..f192f86a --- /dev/null +++ b/sway/old/commands/exit.c @@ -0,0 +1,17 @@ +#include "sway/commands.h" +#include "sway/container.h" + +void sway_terminate(int exit_code); + +struct cmd_results *cmd_exit(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file."); + if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) { + return error; + } + // Close all views + close_views(&root_container); + sway_terminate(EXIT_SUCCESS); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + diff --git a/sway/old/commands/floating.c b/sway/old/commands/floating.c new file mode 100644 index 00000000..ccfde532 --- /dev/null +++ b/sway/old/commands/floating.c @@ -0,0 +1,81 @@ +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/ipc-server.h" +#include "sway/layout.h" +#include "list.h" +#include "log.h" + +struct cmd_results *cmd_floating(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "floating", "Can't be used in config file."); + if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) { + return error; + } + swayc_t *view = current_container; + bool wants_floating; + if (strcasecmp(argv[0], "enable") == 0) { + wants_floating = true; + } else if (strcasecmp(argv[0], "disable") == 0) { + wants_floating = false; + } else if (strcasecmp(argv[0], "toggle") == 0) { + wants_floating = !view->is_floating; + } else { + return cmd_results_new(CMD_FAILURE, "floating", + "Expected 'floating "); + } + + // Prevent running floating commands on things like workspaces + if (view->type != C_VIEW) { + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + + // Change from nonfloating to floating + if (!view->is_floating && wants_floating) { + // Remove view from its current location + destroy_container(remove_child(view)); + + // and move it into workspace floating + add_floating(swayc_active_workspace(), view); + view->x = (swayc_active_workspace()->width - view->width)/2; + view->y = (swayc_active_workspace()->height - view->height)/2; + if (view->desired_width != -1) { + view->width = view->desired_width; + } + if (view->desired_height != -1) { + view->height = view->desired_height; + } + arrange_windows(swayc_active_workspace(), -1, -1); + + } else if (view->is_floating && !wants_floating) { + // Delete the view from the floating list and unset its is_floating flag + remove_child(view); + view->is_floating = false; + // Get the properly focused container, and add in the view there + swayc_t *focused = container_under_pointer(); + // If focused is null, it's because the currently focused container is a workspace + if (focused == NULL || focused->is_floating) { + focused = swayc_active_workspace(); + } + set_focused_container(focused); + + sway_log(L_DEBUG, "Non-floating focused container is %p", focused); + + // Case of focused workspace, just create as child of it + if (focused->type == C_WORKSPACE) { + add_child(focused, view); + } + // Regular case, create as sibling of current container + else { + add_sibling(focused, view); + } + // Refocus on the view once its been put back into the layout + view->width = view->height = 0; + arrange_windows(swayc_active_workspace(), -1, -1); + remove_view_from_scratchpad(view); + ipc_event_window(view, "floating"); + } + set_focused_container(view); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/floating_maximum_size.c b/sway/old/commands/floating_maximum_size.c new file mode 100644 index 00000000..5bca4d7c --- /dev/null +++ b/sway/old/commands/floating_maximum_size.c @@ -0,0 +1,38 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *cmd_floating_maximum_size(int argc, char **argv) { + struct cmd_results *error = NULL; + int32_t width; + int32_t height; + char *ptr; + + if ((error = checkarg(argc, "floating_maximum_size", EXPECTED_EQUAL_TO, 3))) { + return error; + } + width = strtol(argv[0], &ptr, 10); + height = strtol(argv[2], &ptr, 10); + + if (width < -1) { + sway_log(L_DEBUG, "floating_maximum_size invalid width value: '%s'", argv[0]); + + } else { + config->floating_maximum_width = width; + + } + + if (height < -1) { + sway_log(L_DEBUG, "floating_maximum_size invalid height value: '%s'", argv[2]); + } + else { + config->floating_maximum_height = height; + + } + + sway_log(L_DEBUG, "New floating_maximum_size: '%d' x '%d'", config->floating_maximum_width, + config->floating_maximum_height); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/floating_minimum_size.c b/sway/old/commands/floating_minimum_size.c new file mode 100644 index 00000000..ba72454c --- /dev/null +++ b/sway/old/commands/floating_minimum_size.c @@ -0,0 +1,38 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *cmd_floating_minimum_size(int argc, char **argv) { + struct cmd_results *error = NULL; + int32_t width; + int32_t height; + char *ptr; + + if ((error = checkarg(argc, "floating_minimum_size", EXPECTED_EQUAL_TO, 3))) { + return error; + } + width = strtol(argv[0], &ptr, 10); + height = strtol(argv[2], &ptr, 10); + + if (width <= 0) { + sway_log(L_DEBUG, "floating_minimum_size invalid width value: '%s'", argv[0]); + + } else { + config->floating_minimum_width = width; + + } + + if (height <= 0) { + sway_log(L_DEBUG, "floating_minimum_size invalid height value: '%s'", argv[2]); + } + else { + config->floating_minimum_height = height; + + } + + sway_log(L_DEBUG, "New floating_minimum_size: '%d' x '%d'", config->floating_minimum_width, + config->floating_minimum_height); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/floating_mod.c b/sway/old/commands/floating_mod.c new file mode 100644 index 00000000..b8e81ab9 --- /dev/null +++ b/sway/old/commands/floating_mod.c @@ -0,0 +1,42 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input_state.h" +#include "list.h" +#include "log.h" +#include "stringop.h" +#include "util.h" + +struct cmd_results *cmd_floating_mod(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "floating_modifier", EXPECTED_AT_LEAST, 1))) { + return error; + } + int i; + list_t *split = split_string(argv[0], "+"); + config->floating_mod = 0; + + // set modifier keys + for (i = 0; i < split->length; ++i) { + config->floating_mod |= get_modifier_mask_by_name(split->items[i]); + } + free_flat_list(split); + if (!config->floating_mod) { + error = cmd_results_new(CMD_INVALID, "floating_modifier", "Unknown keys %s", argv[0]); + return error; + } + + if (argc >= 2) { + if (strcasecmp("inverse", argv[1]) == 0) { + config->dragging_key = M_RIGHT_CLICK; + config->resizing_key = M_LEFT_CLICK; + } else if (strcasecmp("normal", argv[1]) == 0) { + config->dragging_key = M_LEFT_CLICK; + config->resizing_key = M_RIGHT_CLICK; + } else { + error = cmd_results_new(CMD_INVALID, "floating_modifier", "Invalid definition %s", argv[1]); + return error; + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/floating_scroll.c b/sway/old/commands/floating_scroll.c new file mode 100644 index 00000000..8c50c5bd --- /dev/null +++ b/sway/old/commands/floating_scroll.c @@ -0,0 +1,46 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_floating_scroll(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "floating_scroll", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!strcasecmp("up", argv[0])) { + free(config->floating_scroll_up_cmd); + if (argc < 2) { + config->floating_scroll_up_cmd = strdup(""); + } else { + config->floating_scroll_up_cmd = join_args(argv + 1, argc - 1); + } + } else if (!strcasecmp("down", argv[0])) { + free(config->floating_scroll_down_cmd); + if (argc < 2) { + config->floating_scroll_down_cmd = strdup(""); + } else { + config->floating_scroll_down_cmd = join_args(argv + 1, argc - 1); + } + } else if (!strcasecmp("left", argv[0])) { + free(config->floating_scroll_left_cmd); + if (argc < 2) { + config->floating_scroll_left_cmd = strdup(""); + } else { + config->floating_scroll_left_cmd = join_args(argv + 1, argc - 1); + } + } else if (!strcasecmp("right", argv[0])) { + free(config->floating_scroll_right_cmd); + if (argc < 2) { + config->floating_scroll_right_cmd = strdup(""); + } else { + config->floating_scroll_right_cmd = join_args(argv + 1, argc - 1); + } + } else { + error = cmd_results_new(CMD_INVALID, "floating_scroll", "Unknown command: '%s'", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/focus.c b/sway/old/commands/focus.c new file mode 100644 index 00000000..c83157b8 --- /dev/null +++ b/sway/old/commands/focus.c @@ -0,0 +1,100 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" +#include "sway/input_state.h" +#include "sway/output.h" +#include "sway/workspace.h" + +struct cmd_results *cmd_focus(int argc, char **argv) { + if (config->reading) return cmd_results_new(CMD_FAILURE, "focus", "Can't be used in config file."); + struct cmd_results *error = NULL; + if (argc > 0 && strcasecmp(argv[0], "output") == 0) { + swayc_t *output = NULL; + struct wlc_point abs_pos; + get_absolute_center_position(get_focused_container(&root_container), &abs_pos); + if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 2))) { + return error; + } else if (!(output = output_by_name(argv[1], &abs_pos))) { + return cmd_results_new(CMD_FAILURE, "focus output", + "Can't find output with name/at direction '%s' @ (%i,%i)", argv[1], abs_pos.x, abs_pos.y); + } else if (!workspace_switch(swayc_active_workspace_for(output))) { + return cmd_results_new(CMD_FAILURE, "focus output", + "Switching to workspace on output '%s' was blocked", argv[1]); + } else if (config->mouse_warping) { + swayc_t *focused = get_focused_view(output); + if (focused && focused->type == C_VIEW) { + center_pointer_on(focused); + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } else if (argc == 0) { + set_focused_container(current_container); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) { + return error; + } + static int floating_toggled_index = 0; + static int tiled_toggled_index = 0; + if (strcasecmp(argv[0], "left") == 0) { + move_focus(MOVE_LEFT); + } else if (strcasecmp(argv[0], "right") == 0) { + move_focus(MOVE_RIGHT); + } else if (strcasecmp(argv[0], "up") == 0) { + move_focus(MOVE_UP); + } else if (strcasecmp(argv[0], "down") == 0) { + move_focus(MOVE_DOWN); + } else if (strcasecmp(argv[0], "parent") == 0) { + move_focus(MOVE_PARENT); + } else if (strcasecmp(argv[0], "child") == 0) { + move_focus(MOVE_CHILD); + } else if (strcasecmp(argv[0], "next") == 0) { + move_focus(MOVE_NEXT); + } else if (strcasecmp(argv[0], "prev") == 0) { + move_focus(MOVE_PREV); + } else if (strcasecmp(argv[0], "mode_toggle") == 0) { + int i; + swayc_t *workspace = swayc_active_workspace(); + swayc_t *focused = get_focused_view(workspace); + if (focused->is_floating) { + if (workspace->children->length > 0) { + for (i = 0;i < workspace->floating->length; i++) { + if (workspace->floating->items[i] == focused) { + floating_toggled_index = i; + break; + } + } + if (workspace->children->length > tiled_toggled_index) { + set_focused_container(get_focused_view(workspace->children->items[tiled_toggled_index])); + } else { + set_focused_container(get_focused_view(workspace->children->items[0])); + tiled_toggled_index = 0; + } + } + } else { + if (workspace->floating->length > 0) { + for (i = 0;i < workspace->children->length; i++) { + if (workspace->children->items[i] == focused) { + tiled_toggled_index = i; + break; + } + } + if (workspace->floating->length > floating_toggled_index) { + swayc_t *floating = workspace->floating->items[floating_toggled_index]; + set_focused_container(get_focused_view(floating)); + } else { + swayc_t *floating = workspace->floating->items[workspace->floating->length - 1]; + set_focused_container(get_focused_view(floating)); + tiled_toggled_index = workspace->floating->length - 1; + } + } + } + } else { + return cmd_results_new(CMD_INVALID, "focus", + "Expected 'focus ' or 'focus output '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/focus_follows_mouse.c b/sway/old/commands/focus_follows_mouse.c new file mode 100644 index 00000000..7c9c2b13 --- /dev/null +++ b/sway/old/commands/focus_follows_mouse.c @@ -0,0 +1,13 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_focus_follows_mouse(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + config->focus_follows_mouse = !strcasecmp(argv[0], "yes"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/font.c b/sway/old/commands/font.c new file mode 100644 index 00000000..32994f8a --- /dev/null +++ b/sway/old/commands/font.c @@ -0,0 +1,27 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/border.h" +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_font(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) { + return error; + } + + char *font = join_args(argv, argc); + free(config->font); + if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) { + config->font = strdup(font + 6); + free(font); + } else { + config->font = font; + } + + config->font_height = get_font_text_height(config->font); + + sway_log(L_DEBUG, "Settings font %s", config->font); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/for_window.c b/sway/old/commands/for_window.c new file mode 100644 index 00000000..d1fd1641 --- /dev/null +++ b/sway/old/commands/for_window.c @@ -0,0 +1,41 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "sway/criteria.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_for_window(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) { + return error; + } + // add command to a criteria/command pair that is run against views when they appear. + char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1); + + struct criteria *crit = malloc(sizeof(struct criteria)); + if (!crit) { + return cmd_results_new(CMD_FAILURE, "for_window", "Unable to allocate criteria"); + } + crit->crit_raw = strdup(criteria); + crit->cmdlist = cmdlist; + crit->tokens = create_list(); + char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); + + if (err_str) { + error = cmd_results_new(CMD_INVALID, "for_window", err_str); + free(err_str); + free_criteria(crit); + } else if (crit->tokens->length == 0) { + error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria"); + free_criteria(crit); + } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { + sway_log(L_DEBUG, "for_window: Duplicate, skipping."); + free_criteria(crit); + } else { + sway_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); + list_add(config->criteria, crit); + } + return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/force_focus_wrapping.c b/sway/old/commands/force_focus_wrapping.c new file mode 100644 index 00000000..f19dd163 --- /dev/null +++ b/sway/old/commands/force_focus_wrapping.c @@ -0,0 +1,13 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_force_focus_wrapping(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "force_focus_wrapping", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + config->force_focus_wrapping = !strcasecmp(argv[0], "yes"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/fullscreen.c b/sway/old/commands/fullscreen.c new file mode 100644 index 00000000..bfff82f9 --- /dev/null +++ b/sway/old/commands/fullscreen.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" +#include "sway/ipc-server.h" +#include "sway/layout.h" + +struct cmd_results *cmd_fullscreen(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can only be used when sway is running."); + if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0))) { + return error; + } + swayc_t *container = current_container; + if(container->type != C_VIEW){ + return cmd_results_new(CMD_INVALID, "fullscreen", "Only views can fullscreen"); + } + swayc_t *workspace = swayc_parent_by_type(container, C_WORKSPACE); + bool current = swayc_is_fullscreen(container); + wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current); + + if (container->is_floating) { + if (current) { + // set dimensions back to what they were before we fullscreened this + container->x = container->cached_geometry.origin.x; + container->y = container->cached_geometry.origin.y; + container->width = container->cached_geometry.size.w; + container->height = container->cached_geometry.size.h; + } else { + // cache dimensions so we can reset them after we "unfullscreen" this + struct wlc_geometry geo = { + .origin = { + .x = container->x, + .y = container->y + }, + .size = { + .w = container->width, + .h = container->height + } + }; + container->cached_geometry = geo; + } + } + + // Resize workspace if going from fullscreen -> notfullscreen + // otherwise just resize container + if (!current) { + arrange_windows(workspace, -1, -1); + workspace->fullscreen = container; + } else { + arrange_windows(container, -1, -1); + workspace->fullscreen = NULL; + } + ipc_event_window(container, "fullscreen_mode"); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/gaps.c b/sway/old/commands/gaps.c new file mode 100644 index 00000000..0a48592d --- /dev/null +++ b/sway/old/commands/gaps.c @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" +#include "sway/layout.h" + +struct cmd_results *cmd_gaps(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1))) { + return error; + } + const char *expected_syntax = + "Expected 'gaps edge_gaps ' or " + "'gaps '"; + const char *amount_str = argv[0]; + // gaps amount + if (argc >= 1 && isdigit(*amount_str)) { + int amount = (int)strtol(amount_str, NULL, 10); + if (errno == ERANGE) { + errno = 0; + return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range."); + } + config->gaps_inner = config->gaps_outer = amount; + arrange_windows(&root_container, -1, -1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + // gaps inner|outer n + else if (argc >= 2 && isdigit((amount_str = argv[1])[0])) { + int amount = (int)strtol(amount_str, NULL, 10); + if (errno == ERANGE) { + errno = 0; + return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range."); + } + const char *target_str = argv[0]; + if (strcasecmp(target_str, "inner") == 0) { + config->gaps_inner = amount; + } else if (strcasecmp(target_str, "outer") == 0) { + config->gaps_outer = amount; + } + arrange_windows(&root_container, -1, -1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } else if (argc == 2 && strcasecmp(argv[0], "edge_gaps") == 0) { + // gaps edge_gaps + if (strcasecmp(argv[1], "toggle") == 0) { + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "gaps edge_gaps toggle", + "Can't be used in config file."); + } + config->edge_gaps = !config->edge_gaps; + } else { + config->edge_gaps = + (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "on") == 0); + } + arrange_windows(&root_container, -1, -1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + // gaps inner|outer current|all set|plus|minus n + if (argc < 4 || config->reading) { + return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); + } + // gaps inner|outer ... + const char *inout_str = argv[0]; + enum {INNER, OUTER} inout; + if (strcasecmp(inout_str, "inner") == 0) { + inout = INNER; + } else if (strcasecmp(inout_str, "outer") == 0) { + inout = OUTER; + } else { + return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); + } + + // gaps ... current|all ... + const char *target_str = argv[1]; + enum {CURRENT, WORKSPACE, ALL} target; + if (strcasecmp(target_str, "current") == 0) { + target = CURRENT; + } else if (strcasecmp(target_str, "all") == 0) { + target = ALL; + } else if (strcasecmp(target_str, "workspace") == 0) { + if (inout == OUTER) { + target = CURRENT; + } else { + // Set gap for views in workspace + target = WORKSPACE; + } + } else { + return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); + } + + // gaps ... n + amount_str = argv[3]; + int amount = (int)strtol(amount_str, NULL, 10); + if (errno == ERANGE) { + errno = 0; + return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range."); + } + + // gaps ... set|plus|minus ... + const char *method_str = argv[2]; + enum {SET, ADD} method; + if (strcasecmp(method_str, "set") == 0) { + method = SET; + } else if (strcasecmp(method_str, "plus") == 0) { + method = ADD; + } else if (strcasecmp(method_str, "minus") == 0) { + method = ADD; + amount *= -1; + } else { + return cmd_results_new(CMD_INVALID, "gaps", expected_syntax); + } + + if (target == CURRENT) { + swayc_t *cont; + if (inout == OUTER) { + if ((cont = swayc_active_workspace()) == NULL) { + return cmd_results_new(CMD_FAILURE, "gaps", "There's no active workspace."); + } + } else { + if ((cont = get_focused_view(&root_container))->type != C_VIEW) { + return cmd_results_new(CMD_FAILURE, "gaps", "Currently focused item is not a view."); + } + } + cont->gaps = swayc_gap(cont); + if (method == SET) { + cont->gaps = amount; + } else if ((cont->gaps += amount) < 0) { + cont->gaps = 0; + } + arrange_windows(cont->parent, -1, -1); + } else if (inout == OUTER) { + //resize all workspace. + int i,j; + for (i = 0; i < root_container.children->length; ++i) { + swayc_t *op = root_container.children->items[i]; + for (j = 0; j < op->children->length; ++j) { + swayc_t *ws = op->children->items[j]; + if (method == SET) { + ws->gaps = amount; + } else if ((ws->gaps += amount) < 0) { + ws->gaps = 0; + } + } + } + arrange_windows(&root_container, -1, -1); + } else { + // Resize gaps for all views in workspace + swayc_t *top; + if (target == WORKSPACE) { + if ((top = swayc_active_workspace()) == NULL) { + return cmd_results_new(CMD_FAILURE, "gaps", "There's currently no active workspace."); + } + } else { + top = &root_container; + } + int top_gap = top->gaps; + container_map(top, method == SET ? set_gaps : add_gaps, &amount); + top->gaps = top_gap; + arrange_windows(top, -1, -1); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/hide_edge_borders.c b/sway/old/commands/hide_edge_borders.c new file mode 100644 index 00000000..ee2a2644 --- /dev/null +++ b/sway/old/commands/hide_edge_borders.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_hide_edge_borders(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "hide_edge_borders", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcasecmp(argv[0], "none") == 0) { + config->hide_edge_borders = E_NONE; + } else if (strcasecmp(argv[0], "vertical") == 0) { + config->hide_edge_borders = E_VERTICAL; + } else if (strcasecmp(argv[0], "horizontal") == 0) { + config->hide_edge_borders = E_HORIZONTAL; + } else if (strcasecmp(argv[0], "both") == 0) { + config->hide_edge_borders = E_BOTH; + } else if (strcasecmp(argv[0], "smart") == 0) { + config->hide_edge_borders = E_SMART; + } else { + return cmd_results_new(CMD_INVALID, "hide_edge_borders", + "Expected 'hide_edge_borders '"); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/include.c b/sway/old/commands/include.c new file mode 100644 index 00000000..1ba9a10d --- /dev/null +++ b/sway/old/commands/include.c @@ -0,0 +1,15 @@ +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *cmd_include(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "include", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!load_include_configs(argv[0], config)) { + return cmd_results_new(CMD_INVALID, "include", "Failed to include sub configuration file: %s", argv[0]); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input.c b/sway/old/commands/input.c new file mode 100644 index 00000000..ad53d272 --- /dev/null +++ b/sway/old/commands/input.c @@ -0,0 +1,55 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" +#include "log.h" + +struct cmd_results *cmd_input(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) { + return error; + } + + if (config->reading && strcmp("{", argv[1]) == 0) { + current_input_config = new_input_config(argv[0]); + sway_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); + return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); + } + + if (argc > 2) { + int argc_new = argc-2; + char **argv_new = argv+2; + + struct cmd_results *res; + current_input_config = new_input_config(argv[0]); + if (strcasecmp("accel_profile", argv[1]) == 0) { + res = input_cmd_accel_profile(argc_new, argv_new); + } else if (strcasecmp("click_method", argv[1]) == 0) { + res = input_cmd_click_method(argc_new, argv_new); + } else if (strcasecmp("drag_lock", argv[1]) == 0) { + res = input_cmd_drag_lock(argc_new, argv_new); + } else if (strcasecmp("dwt", argv[1]) == 0) { + res = input_cmd_dwt(argc_new, argv_new); + } else if (strcasecmp("events", argv[1]) == 0) { + res = input_cmd_events(argc_new, argv_new); + } else if (strcasecmp("left_handed", argv[1]) == 0) { + res = input_cmd_left_handed(argc_new, argv_new); + } else if (strcasecmp("middle_emulation", argv[1]) == 0) { + res = input_cmd_middle_emulation(argc_new, argv_new); + } else if (strcasecmp("natural_scroll", argv[1]) == 0) { + res = input_cmd_natural_scroll(argc_new, argv_new); + } else if (strcasecmp("pointer_accel", argv[1]) == 0) { + res = input_cmd_pointer_accel(argc_new, argv_new); + } else if (strcasecmp("scroll_method", argv[1]) == 0) { + res = input_cmd_scroll_method(argc_new, argv_new); + } else if (strcasecmp("tap", argv[1]) == 0) { + res = input_cmd_tap(argc_new, argv_new); + } else { + res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); + } + current_input_config = NULL; + return res; + } + + return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); +} diff --git a/sway/old/commands/input/accel_profile.c b/sway/old/commands/input/accel_profile.c new file mode 100644 index 00000000..8288c1ad --- /dev/null +++ b/sway/old/commands/input/accel_profile.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_accel_profile(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "accel_profile", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "adaptive") == 0) { + new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE; + } else if (strcasecmp(argv[0], "flat") == 0) { + new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT; + } else { + return cmd_results_new(CMD_INVALID, "accel_profile", + "Expected 'accel_profile '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/click_method.c b/sway/old/commands/input/click_method.c new file mode 100644 index 00000000..5e9d3dcb --- /dev/null +++ b/sway/old/commands/input/click_method.c @@ -0,0 +1,30 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" +#include "log.h" + +struct cmd_results *input_cmd_click_method(int argc, char **argv) { + sway_log(L_DEBUG, "click_method for device: %d %s", current_input_config==NULL, current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "click_method", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "none") == 0) { + new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE; + } else if (strcasecmp(argv[0], "button_areas") == 0) { + new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS; + } else if (strcasecmp(argv[0], "clickfinger") == 0) { + new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER; + } else { + return cmd_results_new(CMD_INVALID, "click_method", "Expected 'click_method +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_drag_lock(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "drag_lock", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "drag_lock", "Expected 'drag_lock '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/dwt.c b/sway/old/commands/input/dwt.c new file mode 100644 index 00000000..557b2207 --- /dev/null +++ b/sway/old/commands/input/dwt.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_dwt(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->dwt = LIBINPUT_CONFIG_DWT_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->dwt = LIBINPUT_CONFIG_DWT_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "dwt", "Expected 'dwt '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/events.c b/sway/old/commands/input/events.c new file mode 100644 index 00000000..9d54287a --- /dev/null +++ b/sway/old/commands/input/events.c @@ -0,0 +1,30 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" +#include "log.h" + +struct cmd_results *input_cmd_events(int argc, char **argv) { + sway_log(L_DEBUG, "events for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "events", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED; + } else if (strcasecmp(argv[0], "disabled_on_external_mouse") == 0) { + new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE; + } else { + return cmd_results_new(CMD_INVALID, "events", "Expected 'events '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/left_handed.c b/sway/old/commands/input/left_handed.c new file mode 100644 index 00000000..6c913e70 --- /dev/null +++ b/sway/old/commands/input/left_handed.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_left_handed(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "left_handed", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "left_handed", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->left_handed = 1; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->left_handed = 0; + } else { + return cmd_results_new(CMD_INVALID, "left_handed", "Expected 'left_handed '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/middle_emulation.c b/sway/old/commands/input/middle_emulation.c new file mode 100644 index 00000000..33cdd7d6 --- /dev/null +++ b/sway/old/commands/input/middle_emulation.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "middle_emulation", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "middle_emulation", "Expected 'middle_emulation '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/natural_scroll.c b/sway/old/commands/input/natural_scroll.c new file mode 100644 index 00000000..7bc8b8d0 --- /dev/null +++ b/sway/old/commands/input/natural_scroll.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "natural_scoll", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->natural_scroll = 1; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->natural_scroll = 0; + } else { + return cmd_results_new(CMD_INVALID, "natural_scroll", "Expected 'natural_scroll '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/pointer_accel.c b/sway/old/commands/input/pointer_accel.c new file mode 100644 index 00000000..94f595d1 --- /dev/null +++ b/sway/old/commands/input/pointer_accel.c @@ -0,0 +1,24 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "pointer_accel", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + float pointer_accel = atof(argv[0]); + if (pointer_accel < -1 || pointer_accel > 1) { + return cmd_results_new(CMD_INVALID, "pointer_accel", "Input out of range [-1, 1]"); + } + new_config->pointer_accel = pointer_accel; + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/scroll_method.c b/sway/old/commands/input/scroll_method.c new file mode 100644 index 00000000..5c6c3d7a --- /dev/null +++ b/sway/old/commands/input/scroll_method.c @@ -0,0 +1,30 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" + +struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "scroll_method", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "none") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; + } else if (strcasecmp(argv[0], "two_finger") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_2FG; + } else if (strcasecmp(argv[0], "edge") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE; + } else if (strcasecmp(argv[0], "on_button_down") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; + } else { + return cmd_results_new(CMD_INVALID, "scroll_method", "Expected 'scroll_method '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/input/tap.c b/sway/old/commands/input/tap.c new file mode 100644 index 00000000..9e3ca2af --- /dev/null +++ b/sway/old/commands/input/tap.c @@ -0,0 +1,29 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input.h" +#include "log.h" + +struct cmd_results *input_cmd_tap(int argc, char **argv) { + sway_log(L_DEBUG, "tap for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "tap", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->tap = LIBINPUT_CONFIG_TAP_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->tap = LIBINPUT_CONFIG_TAP_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "tap", "Expected 'tap '"); + } + + sway_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/ipc.c b/sway/old/commands/ipc.c new file mode 100644 index 00000000..0c678961 --- /dev/null +++ b/sway/old/commands/ipc.c @@ -0,0 +1,162 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/security.h" +#include "sway/commands.h" +#include "sway/config.h" +#include "ipc.h" +#include "log.h" +#include "util.h" + +static struct ipc_policy *current_policy = NULL; + +struct cmd_results *cmd_ipc(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 2))) { + return error; + } + if ((error = check_security_config())) { + return error; + } + + char *program = NULL; + + if (!strcmp(argv[0], "*")) { + program = strdup(argv[0]); + } else if (!(program = resolve_path(argv[0]))) { + return cmd_results_new( + CMD_INVALID, "ipc", "Unable to resolve IPC Policy target."); + } + if (config->reading && strcmp("{", argv[1]) != 0) { + return cmd_results_new(CMD_INVALID, "ipc", + "Expected '{' at start of IPC config definition."); + } + + if (!config->reading) { + return cmd_results_new(CMD_FAILURE, "ipc", "Can only be used in config file."); + } + + current_policy = alloc_ipc_policy(program); + list_add(config->ipc_policies, current_policy); + + free(program); + return cmd_results_new(CMD_BLOCK_IPC, NULL, NULL); +} + +struct cmd_results *cmd_ipc_events(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "events", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (config->reading && strcmp("{", argv[0]) != 0) { + return cmd_results_new(CMD_INVALID, "events", + "Expected '{' at start of IPC event config definition."); + } + + if (!config->reading) { + return cmd_results_new(CMD_FAILURE, "events", "Can only be used in config file."); + } + + return cmd_results_new(CMD_BLOCK_IPC_EVENTS, NULL, NULL); +} + +struct cmd_results *cmd_ipc_cmd(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + bool enabled; + if (strcmp(argv[0], "enabled") == 0) { + enabled = true; + } else if (strcmp(argv[0], "disabled") == 0) { + enabled = false; + } else { + return cmd_results_new(CMD_INVALID, argv[-1], + "Argument must be one of 'enabled' or 'disabled'"); + } + + struct { + char *name; + enum ipc_feature type; + } types[] = { + { "*", IPC_FEATURE_ALL_COMMANDS }, + { "command", IPC_FEATURE_COMMAND }, + { "workspaces", IPC_FEATURE_GET_WORKSPACES }, + { "outputs", IPC_FEATURE_GET_OUTPUTS }, + { "tree", IPC_FEATURE_GET_TREE }, + { "marks", IPC_FEATURE_GET_MARKS }, + { "bar-config", IPC_FEATURE_GET_BAR_CONFIG }, + { "inputs", IPC_FEATURE_GET_INPUTS }, + { "clipboard", IPC_FEATURE_GET_CLIPBOARD }, + }; + + uint32_t type = 0; + + for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { + if (strcmp(types[i].name, argv[-1]) == 0) { + type = types[i].type; + break; + } + } + + if (enabled) { + current_policy->features |= type; + sway_log(L_DEBUG, "Enabled IPC %s feature", argv[-1]); + } else { + current_policy->features &= ~type; + sway_log(L_DEBUG, "Disabled IPC %s feature", argv[-1]); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_ipc_event_cmd(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + bool enabled; + if (strcmp(argv[0], "enabled") == 0) { + enabled = true; + } else if (strcmp(argv[0], "disabled") == 0) { + enabled = false; + } else { + return cmd_results_new(CMD_INVALID, argv[-1], + "Argument must be one of 'enabled' or 'disabled'"); + } + + struct { + char *name; + enum ipc_feature type; + } types[] = { + { "*", IPC_FEATURE_ALL_EVENTS }, + { "workspace", IPC_FEATURE_EVENT_WORKSPACE }, + { "output", IPC_FEATURE_EVENT_OUTPUT }, + { "mode", IPC_FEATURE_EVENT_MODE }, + { "window", IPC_FEATURE_EVENT_WINDOW }, + { "binding", IPC_FEATURE_EVENT_BINDING }, + { "input", IPC_FEATURE_EVENT_INPUT }, + }; + + uint32_t type = 0; + + for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { + if (strcmp(types[i].name, argv[-1]) == 0) { + type = types[i].type; + break; + } + } + + if (enabled) { + current_policy->features |= type; + sway_log(L_DEBUG, "Enabled IPC %s event", argv[-1]); + } else { + current_policy->features &= ~type; + sway_log(L_DEBUG, "Disabled IPC %s event", argv[-1]); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/kill.c b/sway/old/commands/kill.c new file mode 100644 index 00000000..742e2b86 --- /dev/null +++ b/sway/old/commands/kill.c @@ -0,0 +1,12 @@ +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" + +struct cmd_results *cmd_kill(int argc, char **argv) { + if (config->reading) return cmd_results_new(CMD_FAILURE, "kill", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "kill", "Can only be used when sway is running."); + + swayc_t *container = current_container; + close_views(container); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/layout.c b/sway/old/commands/layout.c new file mode 100644 index 00000000..57a86565 --- /dev/null +++ b/sway/old/commands/layout.c @@ -0,0 +1,196 @@ +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/layout.h" + +/** + * handle "layout auto" command group + */ +static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv); + +struct cmd_results *cmd_layout(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "layout", "Can only be used when sway is running."); + if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { + return error; + } + swayc_t *parent = current_container; + if (parent->is_floating) { + return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows"); + } + + while (parent->type == C_VIEW) { + parent = parent->parent; + } + + enum swayc_layouts old_layout = parent->layout; + + if (strcasecmp(argv[0], "default") == 0) { + swayc_change_layout(parent, parent->prev_layout); + if (parent->layout == L_NONE) { + swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); + swayc_change_layout(parent, default_layout(output)); + } + } else { + if (parent->layout != L_TABBED && parent->layout != L_STACKED) { + parent->prev_layout = parent->layout; + } + + if (strcasecmp(argv[0], "tabbed") == 0) { + if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ + parent = new_container(parent, L_TABBED); + } + + swayc_change_layout(parent, L_TABBED); + } else if (strcasecmp(argv[0], "stacking") == 0) { + if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)) { + parent = new_container(parent, L_STACKED); + } + + swayc_change_layout(parent, L_STACKED); + } else if (strcasecmp(argv[0], "splith") == 0) { + swayc_change_layout(parent, L_HORIZ); + } else if (strcasecmp(argv[0], "splitv") == 0) { + swayc_change_layout(parent, L_VERT); + } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { + if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE + || parent->workspace_layout == L_HORIZ)) { + swayc_change_layout(parent, L_VERT); + } else { + swayc_change_layout(parent, L_HORIZ); + } + } else if (strcasecmp(argv[0], "auto") == 0) { + return cmd_layout_auto(parent, argc, argv); + } + } + + update_layout_geometry(parent, old_layout); + update_geometry(parent); + + arrange_windows(parent, parent->width, parent->height); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv) { + // called after checking that argv[0] is auto, so just continue parsing from there + struct cmd_results *error = NULL; + const char *cmd_name = "layout auto"; + const char *set_inc_cmd_name = "layout auto [master|ncol] [set|inc]"; + const char *err_msg = "Allowed arguments are "; + + bool need_layout_update = false; + enum swayc_layouts old_layout = container->layout; + enum swayc_layouts layout = old_layout; + + if ((error = checkarg(argc, "layout auto", EXPECTED_MORE_THAN, 1))) { + return error; + } + + if (strcasecmp(argv[1], "left") == 0) { + layout = L_AUTO_LEFT; + } else if (strcasecmp(argv[1], "right") == 0) { + layout = L_AUTO_RIGHT; + } else if (strcasecmp(argv[1], "top") == 0) { + layout = L_AUTO_TOP; + } else if (strcasecmp(argv[1], "bottom") == 0) { + layout = L_AUTO_BOTTOM; + } else if (strcasecmp(argv[1], "next") == 0) { + if (is_auto_layout(container->layout) && container->layout < L_AUTO_LAST) { + layout = container->layout + 1; + } else { + layout = L_AUTO_FIRST; + } + } else if (strcasecmp(argv[1], "prev") == 0) { + if (is_auto_layout(container->layout) && container->layout > L_AUTO_FIRST) { + layout = container->layout - 1; + } else { + layout = L_AUTO_LAST; + } + } else { + bool is_nmaster; + bool is_set; + if (strcasecmp(argv[1], "master") == 0) { + is_nmaster = true; + } else if (strcasecmp(argv[1], "ncol") == 0) { + is_nmaster = false; + } else { + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command. %s", + cmd_name, err_msg); + } + if ((error = checkarg(argc, "auto ", EXPECTED_EQUAL_TO, 4))) { + return error; + } + if (strcasecmp(argv[2], "set") == 0) { + is_set = true; + } else if (strcasecmp(argv[2], "inc") == 0) { + is_set = false; + } else { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command. %s, " + "Argument must be on of ", + set_inc_cmd_name); + } + char *end; + int n = (int)strtol(argv[3], &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " + "(argument must be an integer)", set_inc_cmd_name); + } + if (is_auto_layout(container->layout)) { + int inc = 0; /* difference between current master/ncol and requested value */ + if (is_nmaster) { + if (is_set) { + if (n < 0) { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " + "(master must be >= 0)", set_inc_cmd_name); + } + inc = n - (int)container->nb_master; + } else { /* inc command */ + if ((int)container->nb_master + n >= 0) { + inc = n; + } + } + if (inc) { + for (int i = container->nb_master; + i >= 0 && i < container->children->length + && i != (int)container->nb_master + inc;) { + ((swayc_t *)container->children->items[i])->height = -1; + ((swayc_t *)container->children->items[i])->width = -1; + i += inc > 0 ? 1 : -1; + } + container->nb_master += inc; + need_layout_update = true; + } + } else { /* ncol modification */ + if (is_set) { + if (n <= 0) { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " + "(ncol must be > 0)", set_inc_cmd_name); + } + inc = n - (int)container->nb_slave_groups; + } else { /* inc command */ + if ((int)container->nb_slave_groups + n > 0) { + inc = n; + } + } + if (inc) { + container->nb_slave_groups += inc; + need_layout_update = true; + } + } + } + } + + if (layout != old_layout) { + swayc_change_layout(container, layout); + update_layout_geometry(container, old_layout); + need_layout_update = true; + } + if (need_layout_update) { + update_geometry(container); + arrange_windows(container, container->width, container->height); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/log_colors.c b/sway/old/commands/log_colors.c new file mode 100644 index 00000000..815d1942 --- /dev/null +++ b/sway/old/commands/log_colors.c @@ -0,0 +1,22 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *cmd_log_colors(int argc, char **argv) { + struct cmd_results *error = NULL; + if (!config->reading) return cmd_results_new(CMD_FAILURE, "log_colors", "Can only be used in config file."); + if ((error = checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if (strcasecmp(argv[0], "no") == 0) { + sway_log_colors(0); + } else if (strcasecmp(argv[0], "yes") == 0) { + sway_log_colors(1); + } else { + error = cmd_results_new(CMD_FAILURE, "log_colors", + "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/mark.c b/sway/old/commands/mark.c new file mode 100644 index 00000000..c1d959df --- /dev/null +++ b/sway/old/commands/mark.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include "sway/commands.h" +#include "list.h" +#include "stringop.h" + +static void find_marks_callback(swayc_t *container, void *_mark) { + char *mark = (char *)_mark; + + int index; + if (container->marks && ((index = list_seq_find(container->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1)) { + list_del(container->marks, index); + } +} + +struct cmd_results *cmd_mark(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); + if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) { + return error; + } + + swayc_t *view = current_container; + bool add = false; + bool toggle = false; + + if (strcmp(argv[0], "--add") == 0) { + --argc; ++argv; + add = true; + } else if (strcmp(argv[0], "--replace") == 0) { + --argc; ++argv; + } + + if (argc && strcmp(argv[0], "--toggle") == 0) { + --argc; ++argv; + toggle = true; + } + + if (argc) { + char *mark = join_args(argv, argc); + + // Remove all existing marks of this type + container_map(&root_container, find_marks_callback, mark); + + if (view->marks) { + if (add) { + int index; + if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { + if (toggle) { + free(view->marks->items[index]); + list_del(view->marks, index); + + if (0 == view->marks->length) { + list_free(view->marks); + view->marks = NULL; + } + } + free(mark); + } else { + list_add(view->marks, mark); + } + } else { + if (toggle && list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark) != -1) { + // Delete the list + list_foreach(view->marks, free); + list_free(view->marks); + view->marks = NULL; + } else { + // Delete and replace with a new list + list_foreach(view->marks, free); + list_free(view->marks); + + view->marks = create_list(); + list_add(view->marks, mark); + } + } + } else { + view->marks = create_list(); + list_add(view->marks, mark); + } + } else { + return cmd_results_new(CMD_FAILURE, "mark", + "Expected 'mark [--add|--replace] [--toggle] '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/mode.c b/sway/old/commands/mode.c new file mode 100644 index 00000000..d2985c54 --- /dev/null +++ b/sway/old/commands/mode.c @@ -0,0 +1,57 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/ipc-server.h" +#include "list.h" +#include "log.h" + +struct cmd_results *cmd_mode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { + return error; + } + + const char *mode_name = argv[0]; + bool mode_make = (argc == 2 && strcmp(argv[1], "{") == 0); + if (mode_make) { + if (!config->reading) + return cmd_results_new(CMD_FAILURE, "mode", "Can only be used in config file."); + } + struct sway_mode *mode = NULL; + // Find mode + int i, len = config->modes->length; + for (i = 0; i < len; ++i) { + struct sway_mode *find = config->modes->items[i]; + if (strcasecmp(find->name, mode_name) == 0) { + mode = find; + break; + } + } + // Create mode if it doesn't exist + if (!mode && mode_make) { + mode = malloc(sizeof(struct sway_mode)); + if (!mode) { + return cmd_results_new(CMD_FAILURE, "mode", "Unable to allocate mode"); + } + mode->name = strdup(mode_name); + mode->bindings = create_list(); + list_add(config->modes, mode); + } + if (!mode) { + error = cmd_results_new(CMD_INVALID, "mode", "Unknown mode `%s'", mode_name); + return error; + } + if ((config->reading && mode_make) || (!config->reading && !mode_make)) { + sway_log(L_DEBUG, "Switching to mode `%s'",mode->name); + } + // Set current mode + config->current_mode = mode; + if (!mode_make) { + // trigger IPC mode event + ipc_event_mode(config->current_mode->name); + } + return cmd_results_new(mode_make ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/mouse_warping.c b/sway/old/commands/mouse_warping.c new file mode 100644 index 00000000..5596d483 --- /dev/null +++ b/sway/old/commands/mouse_warping.c @@ -0,0 +1,17 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_mouse_warping(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "mouse_warping", EXPECTED_EQUAL_TO, 1))) { + return error; + } else if (strcasecmp(argv[0], "output") == 0) { + config->mouse_warping = true; + } else if (strcasecmp(argv[0], "none") == 0) { + config->mouse_warping = false; + } else { + return cmd_results_new(CMD_FAILURE, "mouse_warping", "Expected 'mouse_warping output|none'"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/move.c b/sway/old/commands/move.c new file mode 100644 index 00000000..52c73e22 --- /dev/null +++ b/sway/old/commands/move.c @@ -0,0 +1,189 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/layout.h" +#include "sway/output.h" +#include "sway/workspace.h" +#include "list.h" +#include "stringop.h" + +struct cmd_results *cmd_move(int argc, char **argv) { + struct cmd_results *error = NULL; + int move_amt = 10; + + if (config->reading) return cmd_results_new(CMD_FAILURE, "move", "Can't be used in config file."); + if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) { + return error; + } + const char* expected_syntax = "Expected 'move <[px] px>' or " + "'move to workspace ' or " + "'move to output ' or " + "'move position mouse'"; + swayc_t *view = current_container; + + if (argc == 2 || (argc == 3 && strcasecmp(argv[2], "px") == 0 )) { + char *inv; + move_amt = (int)strtol(argv[1], &inv, 10); + if (*inv != '\0' && strcasecmp(inv, "px") != 0) { + move_amt = 10; + } + } + + if (strcasecmp(argv[0], "left") == 0) { + move_container(view, MOVE_LEFT, move_amt); + } else if (strcasecmp(argv[0], "right") == 0) { + move_container(view, MOVE_RIGHT, move_amt); + } else if (strcasecmp(argv[0], "up") == 0) { + move_container(view, MOVE_UP, move_amt); + } else if (strcasecmp(argv[0], "down") == 0) { + move_container(view, MOVE_DOWN, move_amt); + } else if (strcasecmp(argv[0], "next") == 0) { + move_container(view, MOVE_NEXT, move_amt); + } else if (strcasecmp(argv[0], "prev") == 0) { + move_container(view, MOVE_PREV, move_amt); + } else if (strcasecmp(argv[0], "first") == 0) { + move_container(view, MOVE_FIRST, move_amt); + } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) { + // "move container ... + if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) { + return error; + } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "workspace") == 0) { + // move container to workspace x + if (view->type == C_WORKSPACE) { + if (!view->children || view->children->length == 0) { + return cmd_results_new(CMD_FAILURE, "move", "Cannot move an empty workspace"); + } + view = new_container(view, view->workspace_layout); + } if (view->type != C_CONTAINER && view->type != C_VIEW) { + return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views."); + } + + swayc_t *ws; + const char *num_name = NULL; + char *ws_name = NULL; + if (argc == 5 && strcasecmp(argv[3], "number") == 0) { + // move "container to workspace number x" + num_name = argv[4]; + ws = workspace_by_number(num_name); + } else { + ws_name = join_args(argv + 3, argc - 3); + ws = workspace_by_name(ws_name); + } + + if (ws == NULL) { + ws = workspace_create(ws_name ? ws_name : num_name); + } + if (ws_name) { + free(ws_name); + } + move_container_to(view, get_focused_container(ws)); + } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "output") == 0) { + // move container to output x + swayc_t *output = NULL; + struct wlc_point abs_pos; + get_absolute_center_position(view, &abs_pos); + if (view->type == C_WORKSPACE) { + if (!view->children || view->children->length == 0) { + return cmd_results_new(CMD_FAILURE, "move", "Cannot move an empty workspace"); + } + view = new_container(view, view->workspace_layout); + } else if (view->type != C_CONTAINER && view->type != C_VIEW) { + return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views."); + } else if (!(output = output_by_name(argv[3], &abs_pos))) { + return cmd_results_new(CMD_FAILURE, "move", + "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y); + } + + swayc_t *container = get_focused_container(output); + if (container->is_floating) { + move_container_to(view, container->parent); + } else { + move_container_to(view, container); + } + } else { + return cmd_results_new(CMD_INVALID, "move", expected_syntax); + } + } else if (strcasecmp(argv[0], "workspace") == 0) { + // move workspace (to output x) + swayc_t *output = NULL; + struct wlc_point abs_pos; + get_absolute_center_position(view, &abs_pos); + if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 4))) { + return error; + } else if (strcasecmp(argv[1], "to") != 0 || strcasecmp(argv[2], "output") != 0) { + return cmd_results_new(CMD_INVALID, "move", expected_syntax); + } else if (!(output = output_by_name(argv[3], &abs_pos))) { + return cmd_results_new(CMD_FAILURE, "move workspace", + "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y); + } + if (view->type == C_WORKSPACE) { + // This probably means we're moving an empty workspace, but + // that's fine. + move_workspace_to(view, output); + } else { + swayc_t *workspace = swayc_parent_by_type(view, C_WORKSPACE); + move_workspace_to(workspace, output); + } + } else if (strcasecmp(argv[0], "scratchpad") == 0 || (strcasecmp(argv[0], "to") == 0 && strcasecmp(argv[1], "scratchpad") == 0)) { + // move scratchpad ... + if (view->type != C_CONTAINER && view->type != C_VIEW) { + return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views."); + } + swayc_t *view = current_container; + int i; + for (i = 0; i < scratchpad->length; i++) { + if (scratchpad->items[i] == view) { + hide_view_in_scratchpad(view); + sp_view = NULL; + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + } + list_add(scratchpad, view); + if (!view->is_floating) { + destroy_container(remove_child(view)); + } else { + remove_child(view); + } + wlc_view_set_mask(view->handle, 0); + arrange_windows(swayc_active_workspace(), -1, -1); + swayc_t *focused = container_under_pointer(); + if (focused == NULL) { + focused = swayc_active_workspace(); + } + set_focused_container(focused); + } else if (strcasecmp(argv[0], "position") == 0) { + if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 2))) { + return error; + } + if (strcasecmp(argv[1], "mouse")) { + return cmd_results_new(CMD_INVALID, "move", expected_syntax); + } + + if (view->is_floating) { + swayc_t *output = swayc_parent_by_type(view, C_OUTPUT); + struct wlc_geometry g; + wlc_view_get_visible_geometry(view->handle, &g); + const struct wlc_size *size = wlc_output_get_resolution(output->handle); + + double x_pos, y_pos; + wlc_pointer_get_position_v2(&x_pos, &y_pos); + + int32_t x = x_pos - g.size.w / 2; + int32_t y = y_pos - g.size.h / 2; + + uint32_t w = size->w - g.size.w; + uint32_t h = size->h - g.size.h; + + view->x = g.origin.x = MIN((int32_t)w, MAX(x, 0)); + view->y = g.origin.y = MIN((int32_t)h, MAX(y, 0)); + + wlc_view_set_geometry(view->handle, 0, &g); + } + } else { + return cmd_results_new(CMD_INVALID, "move", expected_syntax); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/new_float.c b/sway/old/commands/new_float.c new file mode 100644 index 00000000..d0f96093 --- /dev/null +++ b/sway/old/commands/new_float.c @@ -0,0 +1,8 @@ +#include "log.h" +#include "sway/commands.h" + +struct cmd_results *cmd_new_float(int argc, char **argv) { + sway_log(L_INFO, "`new_float` is deprecated and will be removed in the future. " + "Please use `default_floating_border` instead."); + return cmd_default_floating_border(argc, argv); +} diff --git a/sway/old/commands/new_window.c b/sway/old/commands/new_window.c new file mode 100644 index 00000000..574a4527 --- /dev/null +++ b/sway/old/commands/new_window.c @@ -0,0 +1,8 @@ +#include "log.h" +#include "sway/commands.h" + +struct cmd_results *cmd_new_window(int argc, char **argv) { + sway_log(L_INFO, "`new_window` is deprecated and will be removed in the future. " + "Please use `default_border` instead."); + return cmd_default_border(argc, argv); +} diff --git a/sway/old/commands/no_focus.c b/sway/old/commands/no_focus.c new file mode 100644 index 00000000..b3b88e5a --- /dev/null +++ b/sway/old/commands/no_focus.c @@ -0,0 +1,41 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "sway/criteria.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_no_focus(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "no_focus", EXPECTED_EQUAL_TO, 1))) { + return error; + } + // add command to a criteria/command pair that is run against views when they appear. + char *criteria = argv[0]; + + struct criteria *crit = malloc(sizeof(struct criteria)); + if (!crit) { + return cmd_results_new(CMD_FAILURE, "no_focus", "Unable to allocate criteria"); + } + crit->crit_raw = strdup(criteria); + crit->tokens = create_list(); + crit->cmdlist = NULL; + char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); + + if (err_str) { + error = cmd_results_new(CMD_INVALID, "no_focus", err_str); + free(err_str); + free_criteria(crit); + } else if (crit->tokens->length == 0) { + error = cmd_results_new(CMD_INVALID, "no_focus", "Found no name/value pairs in criteria"); + free_criteria(crit); + } else if (list_seq_find(config->no_focus, criteria_cmp, crit) != -1) { + sway_log(L_DEBUG, "no_focus: Duplicate, skipping."); + free_criteria(crit); + } else { + sway_log(L_DEBUG, "no_focus: '%s' added", crit->crit_raw); + list_add(config->no_focus, crit); + } + return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/orientation.c b/sway/old/commands/orientation.c new file mode 100644 index 00000000..e54b60ee --- /dev/null +++ b/sway/old/commands/orientation.c @@ -0,0 +1,21 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_orientation(int argc, char **argv) { + struct cmd_results *error = NULL; + if (!config->reading) return cmd_results_new(CMD_FAILURE, "orientation", "Can only be used in config file."); + if ((error = checkarg(argc, "orientation", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if (strcasecmp(argv[0], "horizontal") == 0) { + config->default_orientation = L_HORIZ; + } else if (strcasecmp(argv[0], "vertical") == 0) { + config->default_orientation = L_VERT; + } else if (strcasecmp(argv[0], "auto") == 0) { + // Do nothing + } else { + return cmd_results_new(CMD_INVALID, "orientation", "Expected 'orientation '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/output.c b/sway/old/commands/output.c new file mode 100644 index 00000000..911391d2 --- /dev/null +++ b/sway/old/commands/output.c @@ -0,0 +1,219 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +static char *bg_options[] = { + "stretch", + "center", + "fill", + "fit", + "tile" +}; + +struct cmd_results *cmd_output(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { + return error; + } + const char *name = argv[0]; + + struct output_config *output = calloc(1, sizeof(struct output_config)); + if (!output) { + return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); + } + output->x = output->y = output->width = output->height = -1; + output->name = strdup(name); + output->enabled = -1; + output->scale = 1; + + // TODO: atoi doesn't handle invalid numbers + + int i; + for (i = 1; i < argc; ++i) { + const char *command = argv[i]; + + if (strcasecmp(command, "disable") == 0) { + output->enabled = 0; + } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); + goto fail; + } + char *res = argv[i]; + char *x = strchr(res, 'x'); + int width = -1, height = -1; + if (x != NULL) { + // Format is 1234x4321 + *x = '\0'; + width = atoi(res); + height = atoi(x + 1); + *x = 'x'; + } else { + // Format is 1234 4321 + width = atoi(res); + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height)."); + goto fail; + } + res = argv[i]; + height = atoi(res); + } + output->width = width; + output->height = height; + } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); + goto fail; + } + char *res = argv[i]; + char *c = strchr(res, ','); + int x = -1, y = -1; + if (c != NULL) { + // Format is 1234,4321 + *c = '\0'; + x = atoi(res); + y = atoi(c + 1); + *c = ','; + } else { + // Format is 1234 4321 + x = atoi(res); + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); + goto fail; + } + res = argv[i]; + y = atoi(res); + } + output->x = x; + output->y = y; + } else if (strcasecmp(command, "scale") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); + goto fail; + } + output->scale = atoi(argv[i]); + } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { + wordexp_t p; + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); + goto fail; + } + if (i + 1 >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); + goto fail; + } + if (strcasecmp(argv[i + 1], "solid_color") == 0) { + output->background = strdup(argv[argc - 2]); + output->background_option = strdup("solid_color"); + } else { + // argv[i+j]=bg_option + bool valid = false; + char *mode; + size_t j; + for (j = 0; j < (size_t) (argc - i); ++j) { + mode = argv[i + j]; + for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { + if (strcasecmp(mode, bg_options[k]) == 0) { + valid = true; + break; + } + } + if (valid) { + break; + } + } + if (!valid) { + error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); + goto fail; + } + + char *src = join_args(argv + i, j); + if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { + error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); + goto fail; + } + free(src); + src = p.we_wordv[0]; + if (config->reading && *src != '/') { + char *conf = strdup(config->current_config); + if (conf) { + char *conf_path = dirname(conf); + src = malloc(strlen(conf_path) + strlen(src) + 2); + if (src) { + sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); + } else { + sway_log(L_ERROR, "Unable to allocate background source"); + } + free(conf); + } else { + sway_log(L_ERROR, "Unable to allocate background source"); + } + } + if (!src || access(src, F_OK) == -1) { + error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); + wordfree(&p); + goto fail; + } + + output->background = strdup(src); + output->background_option = strdup(mode); + if (src != p.we_wordv[0]) { + free(src); + } + wordfree(&p); + + i += j; + } + } + } + + i = list_seq_find(config->output_configs, output_name_cmp, name); + if (i >= 0) { + // merge existing config + struct output_config *oc = config->output_configs->items[i]; + merge_output_config(oc, output); + free_output_config(output); + output = oc; + } else { + list_add(config->output_configs, output); + } + + sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)", + output->name, output->enabled, output->width, + output->height, output->x, output->y, output->scale, + output->background, output->background_option); + + if (output->name) { + // Try to find the output container and apply configuration now. If + // this is during startup then there will be no container and config + // will be applied during normal "new output" event from wlc. + swayc_t *cont = NULL; + for (int i = 0; i < root_container.children->length; ++i) { + cont = root_container.children->items[i]; + if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { + apply_output_config(output, cont); + + if (strcmp(output->name, "*") != 0) { + // stop looking if the output config isn't applicable to all outputs + break; + } + } + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + +fail: + free_output_config(output); + return error; +} diff --git a/sway/old/commands/permit.c b/sway/old/commands/permit.c new file mode 100644 index 00000000..7a5e06f7 --- /dev/null +++ b/sway/old/commands/permit.c @@ -0,0 +1,108 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/security.h" +#include "util.h" +#include "log.h" + +static enum secure_feature get_features(int argc, char **argv, + struct cmd_results **error) { + enum secure_feature features = 0; + + struct { + char *name; + enum secure_feature feature; + } feature_names[] = { + { "lock", FEATURE_LOCK }, + { "panel", FEATURE_PANEL }, + { "background", FEATURE_BACKGROUND }, + { "screenshot", FEATURE_SCREENSHOT }, + { "fullscreen", FEATURE_FULLSCREEN }, + { "keyboard", FEATURE_KEYBOARD }, + { "mouse", FEATURE_MOUSE }, + }; + + for (int i = 1; i < argc; ++i) { + size_t j; + for (j = 0; j < sizeof(feature_names) / sizeof(feature_names[0]); ++j) { + if (strcmp(feature_names[j].name, argv[i]) == 0) { + break; + } + } + if (j == sizeof(feature_names) / sizeof(feature_names[0])) { + *error = cmd_results_new(CMD_INVALID, + "permit", "Invalid feature grant %s", argv[i]); + return 0; + } + features |= feature_names[j].feature; + } + return features; +} + +struct cmd_results *cmd_permit(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "permit", EXPECTED_MORE_THAN, 1))) { + return error; + } + if ((error = check_security_config())) { + return error; + } + + bool assign_perms = true; + char *program = NULL; + + if (!strcmp(argv[0], "*")) { + program = strdup(argv[0]); + } else { + program = resolve_path(argv[0]); + } + if (!program) { + sway_assert(program, "Unable to resolve IPC permit target '%s'." + " will issue empty policy", argv[0]); + assign_perms = false; + program = strdup(argv[0]); + } + + struct feature_policy *policy = get_feature_policy(program); + if (policy && assign_perms) { + policy->features |= get_features(argc, argv, &error); + sway_log(L_DEBUG, "Permissions granted to %s for features %d", + policy->program, policy->features); + } + + free(program); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_reject(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "reject", EXPECTED_MORE_THAN, 1))) { + return error; + } + if ((error = check_security_config())) { + return error; + } + + char *program = NULL; + if (!strcmp(argv[0], "*")) { + program = strdup(argv[0]); + } else { + program = resolve_path(argv[0]); + } + if (!program) { + // Punt + sway_log(L_INFO, "Unable to resolve IPC reject target '%s'." + " Will use provided path", argv[0]); + program = strdup(argv[0]); + } + + struct feature_policy *policy = get_feature_policy(program); + policy->features &= ~get_features(argc, argv, &error); + + sway_log(L_DEBUG, "Permissions granted to %s for features %d", + policy->program, policy->features); + + free(program); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/reload.c b/sway/old/commands/reload.c new file mode 100644 index 00000000..01fcc5ba --- /dev/null +++ b/sway/old/commands/reload.c @@ -0,0 +1,19 @@ +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/layout.h" + +struct cmd_results *cmd_reload(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "reload", "Can't be used in config file."); + if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) { + return error; + } + if (!load_main_config(config->current_config, true)) { + return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); + } + + load_swaybars(); + + arrange_windows(&root_container, -1, -1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/resize.c b/sway/old/commands/resize.c new file mode 100644 index 00000000..ef52bb07 --- /dev/null +++ b/sway/old/commands/resize.c @@ -0,0 +1,375 @@ +#include +#include +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/layout.h" +#include "sway/focus.h" +#include "sway/input_state.h" +#include "sway/handlers.h" +#include "log.h" + +enum resize_dim_types { + RESIZE_DIM_PX, + RESIZE_DIM_PPT, + RESIZE_DIM_DEFAULT, +}; + +static bool set_size_floating(int new_dimension, bool use_width) { + swayc_t *view = current_container; + if (view) { + if (use_width) { + int current_width = view->width; + view->desired_width = new_dimension; + floating_view_sane_size(view); + + int new_x = view->x + (int)(((view->desired_width - current_width) / 2) * -1); + view->width = view->desired_width; + view->x = new_x; + + update_geometry(view); + } else { + int current_height = view->height; + view->desired_height = new_dimension; + floating_view_sane_size(view); + + int new_y = view->y + (int)(((view->desired_height - current_height) / 2) * -1); + view->height = view->desired_height; + view->y = new_y; + + update_geometry(view); + } + + return true; + } + + return false; +} + +static bool resize_floating(int amount, bool use_width) { + swayc_t *view = current_container; + + if (view) { + if (use_width) { + return set_size_floating(view->width + amount, true); + } else { + return set_size_floating(view->height + amount, false); + } + } + + return false; +} + +static bool resize_tiled(int amount, bool use_width) { + swayc_t *container = current_container; + swayc_t *parent = container->parent; + int idx_focused = 0; + bool use_major = false; + size_t nb_before = 0; + size_t nb_after = 0; + + // 1. Identify a container ancestor that will allow the focused child to grow in the requested + // direction. + while (container->parent) { + parent = container->parent; + if ((parent->children && parent->children->length > 1) + && (is_auto_layout(parent->layout) + || (use_width ? parent->layout == L_HORIZ : parent->layout == L_VERT))) { + // check if container has siblings that can provide/absorb the space needed for + // the resize operation. + use_major = use_width + ? parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT + : parent->layout == L_AUTO_TOP || parent->layout == L_AUTO_BOTTOM; + // Note: use_major will be false for L_HORIZ and L_VERT + + idx_focused = index_child(container); + if (idx_focused < 0) { + sway_log(L_ERROR, "Something weird is happening, child container not " + "present in its parent's children list."); + continue; + } + if (use_major) { + nb_before = auto_group_index(parent, idx_focused); + nb_after = auto_group_count(parent) - nb_before - 1; + } else { + nb_before = idx_focused - auto_group_start_index(parent, idx_focused); + nb_after = auto_group_end_index(parent, idx_focused) - idx_focused - 1; + sway_log(L_DEBUG, "+++ focused: %d, start: %d, end: %d, before: %d, after: %d", + idx_focused, + (int)auto_group_start_index(parent, idx_focused), + (int)auto_group_end_index(parent, idx_focused), + (int)nb_before, (int)nb_after); + + } + if (nb_before || nb_after) { + break; + } + } + container = parent; /* continue up the tree to the next ancestor */ + } + if (parent == &root_container) { + return true; + } + sway_log(L_DEBUG, "Found the proper parent: %p. It has %zu before conts, " + "and %zu after conts", parent, nb_before, nb_after); + // 2. Ensure that the resize operation will not make one of the resized containers drop + // below the "sane" size threshold. + bool valid = true; + swayc_t *focused = parent->children->items[idx_focused]; + int start = use_major ? 0 : auto_group_start_index(parent, idx_focused); + int end = use_major ? parent->children->length : auto_group_end_index(parent, idx_focused); + sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end); + for (int i = start; i < end; ) { + swayc_t *sibling = parent->children->items[i]; + double pixels = amount; + bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; + bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; + if (is_before || is_after) { + pixels = -pixels; + pixels /= is_before ? nb_before : nb_after; + if (nb_after != 0 && nb_before != 0) { + pixels /= 2; + } + } + sway_log(L_DEBUG, "Check container %p: width %g vs %d, height %g vs %d", sibling, sibling->width + pixels, min_sane_w, sibling->height + pixels, min_sane_h); + if (use_width ? + sibling->width + pixels < min_sane_w : + sibling->height + pixels < min_sane_h) { + valid = false; + sway_log(L_DEBUG, "Container size no longer sane"); + break; + } + i = use_major ? auto_group_end_index(parent, i) : (i + 1); + sway_log(L_DEBUG, "+++++ check %i", i); + } + // 3. Apply the size change + if (valid) { + for (int i = start; i < end; ) { + int next_i = use_major ? auto_group_end_index(parent, i) : (i + 1); + swayc_t *sibling = parent->children->items[i]; + double pixels = amount; + bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; + bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; + if (is_before || is_after) { + pixels = -pixels; + pixels /= is_before ? nb_before : nb_after; + if (nb_after != 0 && nb_before != 0) { + pixels /= 2; + } + sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after"); + if (use_major) { + for (int j = i; j < next_i; ++j) { + recursive_resize(parent->children->items[j], pixels, + use_width ? + (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : + (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); + } + } else { + recursive_resize(sibling, pixels, + use_width ? + (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : + (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); + } + } else { + if (use_major) { + for (int j = i; j < next_i; ++j) { + recursive_resize(parent->children->items[j], pixels / 2, + use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); + recursive_resize(parent->children->items[j], pixels / 2, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); + } + } else { + recursive_resize(sibling, pixels / 2, + use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); + recursive_resize(sibling, pixels / 2, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); + } + } + i = next_i; + } + // Recursive resize does not handle positions, let arrange_windows + // take care of that. + arrange_windows(swayc_active_workspace(), -1, -1); + } + return true; +} + +static bool set_size_tiled(int amount, bool use_width) { + int desired; + swayc_t *focused = current_container; + + if (use_width) { + desired = amount - focused->width; + } else { + desired = amount - focused->height; + } + + return resize_tiled(desired, use_width); +} + +static bool set_size(int dimension, bool use_width) { + swayc_t *focused = current_container; + + if (focused) { + if (focused->is_floating) { + return set_size_floating(dimension, use_width); + } else { + return set_size_tiled(dimension, use_width); + } + } + + return false; +} + +static bool resize(int dimension, bool use_width, enum resize_dim_types dim_type) { + swayc_t *focused = current_container; + + // translate "10 ppt" (10%) to appropriate # of pixels in case we need it + float ppt_dim = (float)dimension / 100; + + if (use_width) { + ppt_dim = focused->width * ppt_dim; + } else { + ppt_dim = focused->height * ppt_dim; + } + + if (focused) { + if (focused->is_floating) { + // floating view resize dimensions should default to px, so only + // use ppt if specified + if (dim_type == RESIZE_DIM_PPT) { + dimension = (int)ppt_dim; + } + + return resize_floating(dimension, use_width); + } else { + // tiled view resize dimensions should default to ppt, so only use + // px if specified + if (dim_type != RESIZE_DIM_PX) { + dimension = (int)ppt_dim; + } + + return resize_tiled(dimension, use_width); + } + } + + return false; +} + +static struct cmd_results *cmd_resize_set(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) { + return error; + } + + if (strcasecmp(argv[0], "width") == 0 || strcasecmp(argv[0], "height") == 0) { + // handle `reset set width 100 px height 100 px` syntax, also allows + // specifying only one dimension for a `resize set` + int cmd_num = 0; + int dim; + + while ((cmd_num + 1) < argc) { + dim = (int)strtol(argv[cmd_num + 1], NULL, 10); + if (errno == ERANGE || dim == 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "resize set", + "Expected 'resize set [px] [ [px]]'"); + } + + if (strcasecmp(argv[cmd_num], "width") == 0) { + set_size(dim, true); + } else if (strcasecmp(argv[cmd_num], "height") == 0) { + set_size(dim, false); + } else { + return cmd_results_new(CMD_INVALID, "resize set", + "Expected 'resize set [px] [ [px]]'"); + } + + cmd_num += 2; + + if (cmd_num < argc && strcasecmp(argv[cmd_num], "px") == 0) { + // if this was `resize set width 400 px height 300 px`, disregard the `px` arg + cmd_num++; + } + } + } else { + // handle `reset set 100 px 100 px` syntax + int width = (int)strtol(argv[0], NULL, 10); + if (errno == ERANGE || width == 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "resize set", + "Expected 'resize set [px] [px]'"); + } + + int height_arg = 1; + if (strcasecmp(argv[1], "px") == 0) { + height_arg = 2; + } + + int height = (int)strtol(argv[height_arg], NULL, 10); + if (errno == ERANGE || height == 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "resize set", + "Expected 'resize set [px] [px]'"); + } + + set_size(width, true); + set_size(height, false); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_resize(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "resize", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "resize", "Can only be used when sway is running."); + + if (strcasecmp(argv[0], "set") == 0) { + return cmd_resize_set(argc - 1, &argv[1]); + } + + if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { + return error; + } + + int dim_arg = argc - 1; + + enum resize_dim_types dim_type = RESIZE_DIM_DEFAULT; + if (strcasecmp(argv[dim_arg], "ppt") == 0) { + dim_type = RESIZE_DIM_PPT; + dim_arg--; + } else if (strcasecmp(argv[dim_arg], "px") == 0) { + dim_type = RESIZE_DIM_PX; + dim_arg--; + } + + int amount = (int)strtol(argv[dim_arg], NULL, 10); + if (errno == ERANGE || amount == 0) { + errno = 0; + amount = 10; // this is the default resize dimension used by i3 for both px and ppt + sway_log(L_DEBUG, "Tried to get resize dimension out of '%s' but failed; setting dimension to default %d", + argv[dim_arg], amount); + } + + bool use_width = false; + if (strcasecmp(argv[1], "width") == 0) { + use_width = true; + } else if (strcasecmp(argv[1], "height") != 0) { + return cmd_results_new(CMD_INVALID, "resize", + "Expected 'resize [] [px|ppt]'"); + } + + if (strcasecmp(argv[0], "shrink") == 0) { + amount *= -1; + } else if (strcasecmp(argv[0], "grow") != 0) { + return cmd_results_new(CMD_INVALID, "resize", + "Expected 'resize [] [px|ppt]'"); + } + + resize(amount, use_width, dim_type); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/scratchpad.c b/sway/old/commands/scratchpad.c new file mode 100644 index 00000000..6c5c92df --- /dev/null +++ b/sway/old/commands/scratchpad.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" +#include "sway/layout.h" + +static swayc_t *fetch_view_from_scratchpad() { + sp_index = (sp_index + 1) % scratchpad->length; + swayc_t *view = scratchpad->items[sp_index]; + + if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) { + wlc_view_set_output(view->handle, swayc_active_output()->handle); + } + if (!view->is_floating) { + view->width = swayc_active_workspace()->width * 0.5; + view->height = swayc_active_workspace()->height * 0.75; + view->x = (swayc_active_workspace()->width - view->width)/2; + view->y = (swayc_active_workspace()->height - view->height)/2; + } + if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) { + view->x = (swayc_active_workspace()->width - view->width)/2; + } + if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) { + view->y = (swayc_active_workspace()->height - view->height)/2; + } + + add_floating(swayc_active_workspace(), view); + wlc_view_set_mask(view->handle, VISIBLE); + view->visible = true; + arrange_windows(swayc_active_workspace(), -1, -1); + set_focused_container(view); + return view; +} + +struct cmd_results *cmd_scratchpad(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can only be used when sway is running."); + if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) { + if (!sp_view) { + if (current_container) { + // Haxor the scratchpad index if criteria'd + for (int i = 0; i < scratchpad->length; ++i) { + if (scratchpad->items[i] == current_container) { + sp_index = (i - 1) % scratchpad->length; + } + } + } + sp_view = fetch_view_from_scratchpad(); + } else { + if (swayc_active_workspace() != sp_view->parent) { + hide_view_in_scratchpad(sp_view); + if (sp_index == 0) { + sp_index = scratchpad->length; + } + sp_index--; + sp_view = fetch_view_from_scratchpad(); + } else { + hide_view_in_scratchpad(sp_view); + sp_view = NULL; + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + return cmd_results_new(CMD_FAILURE, "scratchpad", "Expected 'scratchpad show' when scratchpad is not empty."); +} diff --git a/sway/old/commands/seamless_mouse.c b/sway/old/commands/seamless_mouse.c new file mode 100644 index 00000000..7760e88d --- /dev/null +++ b/sway/old/commands/seamless_mouse.c @@ -0,0 +1,13 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_seamless_mouse(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "seamless_mouse", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + config->seamless_mouse = (strcasecmp(argv[0], "on") == 0 || strcasecmp(argv[0], "yes") == 0); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/set.c b/sway/old/commands/set.c new file mode 100644 index 00000000..46fc6d38 --- /dev/null +++ b/sway/old/commands/set.c @@ -0,0 +1,61 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "stringop.h" + +// sort in order of longest->shortest +static int compare_set_qsort(const void *_l, const void *_r) { + struct sway_variable const *l = *(void **)_l; + struct sway_variable const *r = *(void **)_r; + return strlen(r->name) - strlen(l->name); +} + +struct cmd_results *cmd_set(int argc, char **argv) { + char *tmp; + struct cmd_results *error = NULL; + if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file."); + if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) { + return error; + } + + if (argv[0][0] != '$') { + sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]); + + size_t size = snprintf(NULL, 0, "$%s", argv[0]); + tmp = malloc(size + 1); + if (!tmp) { + return cmd_results_new(CMD_FAILURE, "set", "Not possible to create variable $'%s'", argv[0]); + } + snprintf(tmp, size+1, "$%s", argv[0]); + + argv[0] = tmp; + } + + struct sway_variable *var = NULL; + // Find old variable if it exists + int i; + for (i = 0; i < config->symbols->length; ++i) { + var = config->symbols->items[i]; + if (strcmp(var->name, argv[0]) == 0) { + break; + } + var = NULL; + } + if (var) { + free(var->value); + } else { + var = malloc(sizeof(struct sway_variable)); + if (!var) { + return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable"); + } + var->name = strdup(argv[0]); + list_add(config->symbols, var); + list_qsort(config->symbols, compare_set_qsort); + } + var->value = join_args(argv + 1, argc - 1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/show_marks.c b/sway/old/commands/show_marks.c new file mode 100644 index 00000000..ed56d9e5 --- /dev/null +++ b/sway/old/commands/show_marks.c @@ -0,0 +1,13 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_show_marks(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "show_marks", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + config->show_marks = !strcasecmp(argv[0], "on"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/smart_gaps.c b/sway/old/commands/smart_gaps.c new file mode 100644 index 00000000..815fc501 --- /dev/null +++ b/sway/old/commands/smart_gaps.c @@ -0,0 +1,20 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_smart_gaps(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "smart_gaps", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcasecmp(argv[0], "on") == 0) { + config->smart_gaps = true; + } else if (strcasecmp(argv[0], "off") == 0) { + config->smart_gaps = false; + } else { + return cmd_results_new(CMD_INVALID, "smart_gaps", "Expected 'smart_gaps '"); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/split.c b/sway/old/commands/split.c new file mode 100644 index 00000000..e3045a4f --- /dev/null +++ b/sway/old/commands/split.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include "sway/border.h" +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/focus.h" +#include "sway/layout.h" +#include "log.h" + +static struct cmd_results *_do_split(int argc, char **argv, int layout) { + char *name = layout == L_VERT ? "splitv" : + layout == L_HORIZ ? "splith" : "split"; + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, name, "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, name, "Can only be used when sway is running."); + if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) { + return error; + } + swayc_t *focused = current_container; + + // Case of floating window, don't split + if (focused->is_floating) { + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + /* Case that focus is on an workspace with 0/1 children.change its layout */ + if (focused->type == C_WORKSPACE && focused->children->length <= 1) { + sway_log(L_DEBUG, "changing workspace layout"); + swayc_change_layout(focused, layout); + } else if (focused->type != C_WORKSPACE && focused->parent->children->length == 1) { + /* Case of no siblings. change parent layout */ + sway_log(L_DEBUG, "changing container layout"); + swayc_change_layout(focused->parent, layout); + } else { + /* regular case where new split container is build around focused container + * or in case of workspace, container inherits its children */ + sway_log(L_DEBUG, "Adding new container around current focused container"); + sway_log(L_INFO, "FOCUSED SIZE: %.f %.f", focused->width, focused->height); + swayc_t *parent = new_container(focused, layout); + set_focused_container(focused); + arrange_windows(parent, -1, -1); + } + + // update container every time + // if it is tabbed/stacked then the title must change + // if the indicator color is different then the border must change + update_container_border(focused); + swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT); + // schedule render to make changes take effect right away, + // otherwise we would have to wait for the view to render, + // which is unpredictable. + wlc_output_schedule_render(output->handle); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_split(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "split", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "split", "Can only be used when sway is running."); + if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) { + _do_split(argc - 1, argv + 1, L_VERT); + } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) { + _do_split(argc - 1, argv + 1, L_HORIZ); + } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) { + swayc_t *focused = current_container; + if (focused->parent->layout == L_VERT) { + _do_split(argc - 1, argv + 1, L_HORIZ); + } else { + _do_split(argc - 1, argv + 1, L_VERT); + } + } else { + error = cmd_results_new(CMD_FAILURE, "split", + "Invalid split command (expected either horizontal or vertical)."); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_splitv(int argc, char **argv) { + return _do_split(argc, argv, L_VERT); +} + +struct cmd_results *cmd_splith(int argc, char **argv) { + return _do_split(argc, argv, L_HORIZ); +} + +struct cmd_results *cmd_splitt(int argc, char **argv) { + swayc_t *focused = current_container; + if (focused->parent->layout == L_VERT) { + return _do_split(argc, argv, L_HORIZ); + } else { + return _do_split(argc, argv, L_VERT); + } +} diff --git a/sway/old/commands/sticky.c b/sway/old/commands/sticky.c new file mode 100644 index 00000000..4899e061 --- /dev/null +++ b/sway/old/commands/sticky.c @@ -0,0 +1,25 @@ +#include +#include "sway/commands.h" +#include "sway/focus.h" + +struct cmd_results *cmd_sticky(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "sticky", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "sticky", "Can only be used when sway is running."); + if ((error = checkarg(argc, "sticky", EXPECTED_EQUAL_TO, 1))) { + return error; + } + char *action = argv[0]; + swayc_t *cont = get_focused_view(&root_container); + if (strcmp(action, "toggle") == 0) { + cont->sticky = !cont->sticky; + } else if (strcmp(action, "enable") == 0) { + cont->sticky = true; + } else if (strcmp(action, "disable") == 0) { + cont->sticky = false; + } else { + return cmd_results_new(CMD_FAILURE, "sticky", + "Expected 'sticky enable|disable|toggle'"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/unmark.c b/sway/old/commands/unmark.c new file mode 100644 index 00000000..ac213261 --- /dev/null +++ b/sway/old/commands/unmark.c @@ -0,0 +1,31 @@ +#include +#include +#include "sway/commands.h" +#include "list.h" +#include "stringop.h" + +struct cmd_results *cmd_unmark(int argc, char **argv) { + swayc_t *view = current_container; + + if (view->marks) { + if (argc) { + char *mark = join_args(argv, argc); + int index; + if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { + free(view->marks->items[index]); + list_del(view->marks, index); + + if (view->marks->length == 0) { + list_free(view->marks); + view->marks = NULL; + } + } + free(mark); + } else { + list_foreach(view->marks, free); + list_free(view->marks); + view->marks = NULL; + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/workspace.c b/sway/old/commands/workspace.c new file mode 100644 index 00000000..a7839746 --- /dev/null +++ b/sway/old/commands/workspace.c @@ -0,0 +1,92 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/input_state.h" +#include "sway/workspace.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_workspace(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) { + return error; + } + + int output_location = -1; + + for (int i = 0; i < argc; ++i) { + if (strcasecmp(argv[i], "output") == 0) { + output_location = i; + break; + } + } + if (output_location >= 0) { + if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, output_location + 2))) { + return error; + } + struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); + if (!wso) { + return cmd_results_new(CMD_FAILURE, "workspace output", + "Unable to allocate workspace output"); + } + wso->workspace = join_args(argv, argc - 2); + wso->output = strdup(argv[output_location + 1]); + int i = -1; + if ((i = list_seq_find(config->workspace_outputs, workspace_output_cmp_workspace, wso)) != -1) { + struct workspace_output *old = config->workspace_outputs->items[i]; + free(old); // workspaces can only be assigned to a single output + list_del(config->workspace_outputs, i); + } + sway_log(L_DEBUG, "Assigning workspace %s to output %s", wso->workspace, wso->output); + list_add(config->workspace_outputs, wso); + } else { + if (config->reading || !config->active) { + return cmd_results_new(CMD_DEFER, "workspace", NULL); + } + swayc_t *ws = NULL; + if (strcasecmp(argv[0], "number") == 0) { + if (!(ws = workspace_by_number(argv[1]))) { + char *name = join_args(argv + 1, argc - 1); + ws = workspace_create(name); + free(name); + } + } else if (strcasecmp(argv[0], "next") == 0) { + ws = workspace_next(); + } else if (strcasecmp(argv[0], "prev") == 0) { + ws = workspace_prev(); + } else if (strcasecmp(argv[0], "next_on_output") == 0) { + ws = workspace_output_next(); + } else if (strcasecmp(argv[0], "prev_on_output") == 0) { + ws = workspace_output_prev(); + } else if (strcasecmp(argv[0], "back_and_forth") == 0) { + // if auto_back_and_forth is enabled, workspace_switch will swap + // the workspaces. If we created prev_workspace here, workspace_switch + // would put us back on original workspace. + if (config->auto_back_and_forth) { + ws = swayc_active_workspace(); + } else if (prev_workspace_name && !(ws = workspace_by_name(prev_workspace_name))) { + ws = workspace_create(prev_workspace_name); + } + } else { + char *name = join_args(argv, argc); + if (!(ws = workspace_by_name(name))) { + ws = workspace_create(name); + } + free(name); + } + swayc_t *old_output = swayc_active_output(); + workspace_switch(ws); + swayc_t *new_output = swayc_active_output(); + + if (config->mouse_warping && old_output != new_output) { + swayc_t *focused = get_focused_view(ws); + if (focused && focused->type == C_VIEW) { + center_pointer_on(focused); + } + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/workspace_auto_back_and_forth.c b/sway/old/commands/workspace_auto_back_and_forth.c new file mode 100644 index 00000000..d58ae5c8 --- /dev/null +++ b/sway/old/commands/workspace_auto_back_and_forth.c @@ -0,0 +1,18 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_ws_auto_back_and_forth(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if (strcasecmp(argv[0], "yes") == 0) { + config->auto_back_and_forth = true; + } else if (strcasecmp(argv[0], "no") == 0) { + config->auto_back_and_forth = false; + } else { + return cmd_results_new(CMD_INVALID, "workspace_auto_back_and_forth", "Expected 'workspace_auto_back_and_forth '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/commands/workspace_layout.c b/sway/old/commands/workspace_layout.c new file mode 100644 index 00000000..9ac84be2 --- /dev/null +++ b/sway/old/commands/workspace_layout.c @@ -0,0 +1,40 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_workspace_layout(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_layout", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (strcasecmp(argv[0], "default") == 0) { + config->default_layout = L_NONE; + } else if (strcasecmp(argv[0], "stacking") == 0) { + config->default_layout = L_STACKED; + } else if (strcasecmp(argv[0], "tabbed") == 0) { + config->default_layout = L_TABBED; + } else if (strcasecmp(argv[0], "auto") == 0) { + if (argc == 1) { + config->default_layout = L_AUTO_FIRST; + } else { + if ((error = checkarg(argc, "workspace_layout auto", EXPECTED_EQUAL_TO, 2))) { + return error; + } + if (strcasecmp(argv[1], "left") == 0) { + config->default_layout = L_AUTO_LEFT; + } else if (strcasecmp(argv[1], "right") == 0) { + config->default_layout = L_AUTO_RIGHT; + } else if (strcasecmp(argv[1], "top") == 0) { + config->default_layout = L_AUTO_TOP; + } else if (strcasecmp(argv[1], "bottom") == 0) { + config->default_layout = L_AUTO_BOTTOM; + } else { + return cmd_results_new(CMD_INVALID, "workspace_layout auto", "Expected 'workspace_layout auto '"); + } + } + } else { + return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/old/config.c b/sway/old/config.c new file mode 100644 index 00000000..d14653e3 --- /dev/null +++ b/sway/old/config.c @@ -0,0 +1,1313 @@ +#define _POSIX_C_SOURCE 200809L +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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/old/container.c b/sway/old/container.c new file mode 100644 index 00000000..82c0d877 --- /dev/null +++ b/sway/old/container.c @@ -0,0 +1,1024 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include "sway/config.h" +#include "sway/container.h" +#include "sway/workspace.h" +#include "sway/focus.h" +#include "sway/border.h" +#include "sway/layout.h" +#include "sway/input_state.h" +#include "sway/ipc-server.h" +#include "sway/output.h" +#include "sway/view.h" +#include "log.h" +#include "stringop.h" + +#define ASSERT_NONNULL(PTR) \ + sway_assert (PTR, #PTR "must be non-null") + +static swayc_t *new_swayc(enum swayc_types type) { + // next id starts at 1 because 0 is assigned to root_container in layout.c + static size_t next_id = 1; + swayc_t *c = calloc(1, sizeof(swayc_t)); + if (!c) { + return NULL; + } + c->id = next_id++; + c->handle = -1; + c->gaps = -1; + c->layout = L_NONE; + c->workspace_layout = L_NONE; + c->type = type; + c->nb_master = 1; + c->nb_slave_groups = 1; + if (type != C_VIEW) { + c->children = create_list(); + } + return c; +} + +static void free_swayc(swayc_t *cont) { + if (!ASSERT_NONNULL(cont)) { + return; + } + if (cont->children) { + // remove children until there are no more, free_swayc calls + // remove_child, which removes child from this container + while (cont->children->length) { + free_swayc(cont->children->items[0]); + } + list_free(cont->children); + } + if (cont->unmanaged) { + list_free(cont->unmanaged); + } + if (cont->floating) { + while (cont->floating->length) { + free_swayc(cont->floating->items[0]); + } + list_free(cont->floating); + } + if (cont->marks) { + list_foreach(cont->marks, free); + list_free(cont->marks); + } + if (cont->parent) { + remove_child(cont); + } + if (cont->name) { + free(cont->name); + } + if (cont->class) { + free(cont->class); + } + if (cont->instance) { + free(cont->instance); + } + if (cont->app_id) { + free(cont->app_id); + } + if (cont->bg_pid != 0) { + terminate_swaybg(cont->bg_pid); + } + if (cont->border) { + if (cont->border->buffer) { + free(cont->border->buffer); + } + free(cont->border); + } + free(cont); +} + +static void update_root_geometry() { + int width = 0; + int height = 0; + swayc_t *child; + int child_width; + int child_height; + + for (int i = 0; i < root_container.children->length; ++i) { + child = root_container.children->items[i]; + child_width = child->width + child->x; + child_height = child->height + child->y; + if (child_width > width) { + width = child_width; + } + + if (child_height > height) { + height = child_height; + } + } + + root_container.width = width; + root_container.height = height; +} + +// New containers + +swayc_t *new_output(struct sway_output *sway_output) { + struct wlr_box size; + wlr_output_effective_resolution(sway_output->wlr_output, + &size.width, &size.height); + const char *name = sway_output->wlr_output->name; + // Find current outputs to see if this already exists + { + int i, len = root_container.children->length; + for (i = 0; i < len; ++i) { + swayc_t *op = root_container.children->items[i]; + const char *op_name = op->name; + if (op_name && name && strcmp(op_name, name) == 0) { + sway_log(L_DEBUG, "restoring output %p: %s", + sway_output, op_name); + return op; + } + } + } + + struct output_config *oc = NULL, *all = NULL; + int i; + for (i = 0; i < config->output_configs->length; ++i) { + struct output_config *cur = config->output_configs->items[i]; + if (strcasecmp(name, cur->name) == 0) { + sway_log(L_DEBUG, "Matched output config for %s", name); + oc = cur; + } + if (strcasecmp("*", cur->name) == 0) { + sway_log(L_DEBUG, "Matched wildcard output config for %s", name); + all = cur; + } + + if (oc && all) { + break; + } + } + + if (!oc) { + oc = all; + } + + if (oc && !oc->enabled) { + return NULL; + } + + swayc_t *output = new_swayc(C_OUTPUT); + output->_handle.output = sway_output; + output->name = name ? strdup(name) : NULL; + output->width = size.width; + output->height = size.width; + output->unmanaged = create_list(); + output->bg_pid = 0; + + apply_output_config(oc, output); + add_child(&root_container, output); + load_swaybars(); + + // Create workspace + char *ws_name = NULL; + swayc_t *ws = NULL; + + if (name) { + for (i = 0; i < config->workspace_outputs->length; ++i) { + struct workspace_output *wso = config->workspace_outputs->items[i]; + if (strcasecmp(wso->output, name) == 0) { + sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); + // Check if any other workspaces are using this name + if ((ws = workspace_by_name(wso->workspace))) { + // if yes, move those to this output, because they should be here + move_workspace_to(ws, output); + } else if (!ws_name) { + // set a workspace name in case we need to create a default one + ws_name = strdup(wso->workspace); + } + } + } + } + + if (output->children->length == 0) { + if (!ws_name) { + ws_name = workspace_next_name(output->name); + } + // create and initialize default workspace + sway_log(L_DEBUG, "Creating default workspace %s", ws_name); + ws = new_workspace(output, ws_name); + ws->is_focused = true; + } else { + sort_workspaces(output); + set_focused_container(output->children->items[0]); + } + + free(ws_name); + update_root_geometry(); + return output; +} + +swayc_t *new_workspace(swayc_t *output, const char *name) { + if (!ASSERT_NONNULL(output)) { + return NULL; + } + sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle); + swayc_t *workspace = new_swayc(C_WORKSPACE); + + workspace->prev_layout = L_NONE; + workspace->layout = default_layout(output); + workspace->workspace_layout = default_layout(output); + + workspace->x = output->x; + workspace->y = output->y; + workspace->width = output->width; + workspace->height = output->height; + workspace->name = !name ? NULL : strdup(name); + workspace->visible = false; + workspace->floating = create_list(); + + add_child(output, workspace); + sort_workspaces(output); + + return workspace; +} + +swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { + if (!ASSERT_NONNULL(child) + && !sway_assert(!child->is_floating, "cannot create container around floating window")) { + return NULL; + } + swayc_t *cont = new_swayc(C_CONTAINER); + + sway_log(L_DEBUG, "creating container %p around %p", cont, child); + + cont->prev_layout = L_NONE; + cont->layout = layout; + cont->width = child->width; + cont->height = child->height; + cont->x = child->x; + cont->y = child->y; + cont->visible = child->visible; + cont->cached_geometry = child->cached_geometry; + cont->gaps = child->gaps; + + /* Container inherits all of workspaces children, layout and whatnot */ + if (child->type == C_WORKSPACE) { + swayc_t *workspace = child; + // reorder focus + cont->focused = workspace->focused; + workspace->focused = cont; + // set all children focu to container + int i; + for (i = 0; i < workspace->children->length; ++i) { + ((swayc_t *)workspace->children->items[i])->parent = cont; + } + // Swap children + list_t *tmp_list = workspace->children; + workspace->children = cont->children; + cont->children = tmp_list; + // add container to workspace chidren + add_child(workspace, cont); + // give them proper layouts + cont->layout = workspace->workspace_layout; + cont->prev_layout = workspace->prev_layout; + /* TODO: might break shit in move_container!!! workspace->layout = layout; */ + set_focused_container_for(workspace, get_focused_view(workspace)); + } else { // Or is built around container + swayc_t *parent = replace_child(child, cont); + if (parent) { + add_child(cont, child); + } + } + return cont; +} + +swayc_t *new_view(swayc_t *sibling, struct sway_view *view) { + if (!ASSERT_NONNULL(sibling)) { + return NULL; + } + const char *title = view->iface.get_prop(view, VIEW_PROP_TITLE); + swayc_t *swayc = new_swayc(C_VIEW); + sway_log(L_DEBUG, "Adding new view %p:%s to container %p %d", + swayc, title, sibling, sibling ? sibling->type : 0); + // Setup values + swayc->_handle.view = view; + + swayc->name = title ? strdup(title) : NULL; + + const char *class = view->iface.get_prop(view, VIEW_PROP_CLASS); + swayc->class = class ? strdup(class) : NULL; + + const char *instance = view->iface.get_prop(view, VIEW_PROP_INSTANCE); + swayc->instance = instance ? strdup(instance) : NULL; + + const char *app_id = view->iface.get_prop(view, VIEW_PROP_APP_ID); + swayc->app_id = app_id ? strdup(app_id) : NULL; + + swayc->visible = true; + swayc->is_focused = true; + swayc->sticky = false; + swayc->width = 0; + swayc->height = 0; + swayc->desired_width = -1; + swayc->desired_height = -1; + // setup border + swayc->border_type = config->border; + swayc->border_thickness = config->border_thickness; + + swayc->is_floating = false; + + if (sibling->type == C_WORKSPACE) { + // Case of focused workspace, just create as child of it + add_child(sibling, swayc); + } else { + // Regular case, create as sibling of current container + add_sibling(sibling, swayc); + } + return swayc; +} + +swayc_t *new_floating_view(wlc_handle handle) { + if (swayc_active_workspace() == NULL) { + return NULL; + } + const char *title = wlc_view_get_title(handle); + swayc_t *view = new_swayc(C_VIEW); + sway_log(L_DEBUG, "Adding new view %" PRIuPTR ":%x:%s as a floating view", + handle, wlc_view_get_type(handle), title); + // Setup values + view->handle = handle; + view->name = title ? strdup(title) : NULL; + const char *class = wlc_view_get_class(handle); + view->class = class ? strdup(class) : NULL; + const char *instance = wlc_view_get_instance(handle); + view->instance = instance ? strdup(instance) : NULL; + const char *app_id = wlc_view_get_app_id(handle); + view->app_id = app_id ? strdup(app_id) : NULL; + view->visible = true; + view->sticky = false; + + // Set the geometry of the floating view + const struct wlc_geometry *geometry = wlc_view_get_geometry(handle); + + // give it requested geometry, but place in center if possible + // in top left otherwise + if (geometry->size.w != 0) { + view->x = (swayc_active_workspace()->width - geometry->size.w) / 2; + } else { + view->x = 0; + } + if (geometry->size.h != 0) { + view->y = (swayc_active_workspace()->height - geometry->size.h) / 2; + } else { + view->y = 0; + } + + view->width = geometry->size.w; + view->height = geometry->size.h; + + view->desired_width = view->width; + view->desired_height = view->height; + + // setup border + view->border_type = config->floating_border; + view->border_thickness = config->floating_border_thickness; + + view->is_floating = true; + + // Case of focused workspace, just create as child of it + list_add(swayc_active_workspace()->floating, view); + view->parent = swayc_active_workspace(); + if (swayc_active_workspace()->focused == NULL) { + set_focused_container_for(swayc_active_workspace(), view); + } + return view; +} + +void floating_view_sane_size(swayc_t *view) { + // floating_minimum is used as sane value. + // floating_maximum has priority in case of conflict + // TODO: implement total_outputs_dimensions() + if (config->floating_minimum_height != -1 && + view->desired_height < config->floating_minimum_height) { + view->desired_height = config->floating_minimum_height; + } + if (config->floating_minimum_width != -1 && + view->desired_width < config->floating_minimum_width) { + view->desired_width = config->floating_minimum_width; + } + + // if 0 do not resize, only enforce max value + if (config->floating_maximum_height == 0) { + // Missing total_outputs_dimensions() using swayc_active_workspace() + config->floating_maximum_height = swayc_active_workspace()->height; + + } else if (config->floating_maximum_height != -1 && + view->desired_height > config->floating_maximum_height) { + view->desired_height = config->floating_maximum_height; + } + + // if 0 do not resize, only enforce max value + if (config->floating_maximum_width == 0) { + // Missing total_outputs_dimensions() using swayc_active_workspace() + config->floating_maximum_width = swayc_active_workspace()->width; + + } else if (config->floating_maximum_width != -1 && + view->desired_width > config->floating_maximum_width) { + view->desired_width = config->floating_maximum_width; + } + + sway_log(L_DEBUG, "Sane values for view to %d x %d @ %.f, %.f", + view->desired_width, view->desired_height, view->x, view->y); + + return; +} + + +// Destroy container + +swayc_t *destroy_output(swayc_t *output) { + if (!ASSERT_NONNULL(output)) { + return NULL; + } + if (output->children->length > 0) { + // TODO save workspaces when there are no outputs. + // TODO also check if there will ever be no outputs except for exiting + // program + if (root_container.children->length > 1) { + int p = root_container.children->items[0] == output; + // Move workspace from this output to another output + while (output->children->length) { + swayc_t *child = output->children->items[0]; + remove_child(child); + add_child(root_container.children->items[p], child); + } + sort_workspaces(root_container.children->items[p]); + update_visibility(root_container.children->items[p]); + arrange_windows(root_container.children->items[p], -1, -1); + } + } + sway_log(L_DEBUG, "OUTPUT: Destroying output '%" PRIuPTR "'", output->handle); + free_swayc(output); + update_root_geometry(); + return &root_container; +} + +swayc_t *destroy_workspace(swayc_t *workspace) { + if (!ASSERT_NONNULL(workspace)) { + return NULL; + } + + // Do not destroy this if it's the last workspace on this output + swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); + if (output && output->children->length == 1) { + return NULL; + } + + swayc_t *parent = workspace->parent; + // destroy the WS if there are no children + if (workspace->children->length == 0 && workspace->floating->length == 0) { + sway_log(L_DEBUG, "destroying workspace '%s'", workspace->name); + ipc_event_workspace(workspace, NULL, "empty"); + } else { + // Move children to a different workspace on this output + swayc_t *new_workspace = NULL; + int i; + for(i = 0; i < output->children->length; i++) { + if(output->children->items[i] != workspace) { + break; + } + } + new_workspace = output->children->items[i]; + + sway_log(L_DEBUG, "moving children to different workspace '%s' -> '%s'", + workspace->name, new_workspace->name); + + for(i = 0; i < workspace->children->length; i++) { + move_container_to(workspace->children->items[i], new_workspace); + } + + for(i = 0; i < workspace->floating->length; i++) { + move_container_to(workspace->floating->items[i], new_workspace); + } + } + + free_swayc(workspace); + return parent; +} + +swayc_t *destroy_container(swayc_t *container) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + while (container->children->length == 0 && container->type == C_CONTAINER) { + sway_log(L_DEBUG, "Container: Destroying container '%p'", container); + swayc_t *parent = container->parent; + free_swayc(container); + container = parent; + } + return container; +} + +swayc_t *destroy_view(swayc_t *view) { + if (!ASSERT_NONNULL(view)) { + return NULL; + } + sway_log(L_DEBUG, "Destroying view '%p'", view); + swayc_t *parent = view->parent; + free_swayc(view); + + // Destroy empty containers + if (parent && parent->type == C_CONTAINER) { + return destroy_container(parent); + } + return parent; +} + +// Container lookup + + +swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { + if (!container->children) { + return NULL; + } + // Special case for checking floating stuff + int i; + if (container->type == C_WORKSPACE) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *child = container->floating->items[i]; + if (test(child, data)) { + return child; + } + } + } + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + if (test(child, data)) { + return child; + } else { + swayc_t *res = swayc_by_test(child, test, data); + if (res) { + return res; + } + } + } + return NULL; +} + +static bool test_name(swayc_t *view, void *data) { + if (!view || !view->name) { + return false; + } + return strcmp(view->name, data) == 0; +} + +swayc_t *swayc_by_name(const char *name) { + return swayc_by_test(&root_container, test_name, (void *)name); +} + +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) { + return NULL; + } + do { + container = container->parent; + } while (container && container->type != type); + return container; +} + +swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) { + return NULL; + } + do { + container = container->parent; + } while (container && container->layout != layout); + return container; +} + +swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types type) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) { + return NULL; + } + do { + container = container->focused; + } while (container && container->type != type); + return container; +} + +swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts layout) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) { + return NULL; + } + do { + container = container->focused; + } while (container && container->layout != layout); + return container; +} + + +static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) { + if (!parent || !parent->children) { + return NULL; + } + int i, len; + swayc_t **child; + if (parent->type == C_WORKSPACE) { + len = parent->floating->length; + child = (swayc_t **)parent->floating->items; + for (i = 0; i < len; ++i, ++child) { + if ((*child)->handle == handle) { + return *child; + } + } + } + + len = parent->children->length; + child = (swayc_t**)parent->children->items; + for (i = 0; i < len; ++i, ++child) { + if ((*child)->handle == handle) { + return *child; + } else { + swayc_t *res; + if ((res = _swayc_by_handle_helper(handle, *child))) { + return res; + } + } + } + return NULL; +} + +swayc_t *swayc_by_handle(wlc_handle handle) { + return _swayc_by_handle_helper(handle, &root_container); +} + +swayc_t *swayc_active_output(void) { + return root_container.focused; +} + +swayc_t *swayc_active_workspace(void) { + return root_container.focused ? root_container.focused->focused : NULL; +} + +swayc_t *swayc_active_workspace_for(swayc_t *cont) { + if (!cont) { + return NULL; + } + switch (cont->type) { + case C_ROOT: + cont = cont->focused; + /* Fallthrough */ + + case C_OUTPUT: + cont = cont ? cont->focused : NULL; + /* Fallthrough */ + + case C_WORKSPACE: + return cont; + + default: + return swayc_parent_by_type(cont, C_WORKSPACE); + } +} + +static bool pointer_test(swayc_t *view, void *_origin) { + const struct wlc_point *origin = _origin; + // Determine the output that the view is under + swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT); + if (origin->x >= view->x && origin->y >= view->y + && origin->x < view->x + view->width && origin->y < view->y + view->height + && view->visible && parent == root_container.focused) { + return true; + } + return false; +} + +swayc_t *container_under_pointer(void) { + // root.output->workspace + if (!root_container.focused) { + return NULL; + } + swayc_t *lookup = root_container.focused; + // Case of empty workspace + if (lookup->children && !lookup->unmanaged) { + return NULL; + } + double x, y; + wlc_pointer_get_position_v2(&x, &y); + struct wlc_point origin = { .x = x, .y = y }; + + while (lookup && lookup->type != C_VIEW) { + int i; + int len; + for (int _i = 0; lookup->unmanaged && _i < lookup->unmanaged->length; ++_i) { + wlc_handle *handle = lookup->unmanaged->items[_i]; + const struct wlc_geometry *geo = wlc_view_get_geometry(*handle); + if (origin.x >= geo->origin.x && origin.y >= geo->origin.y + && origin.x < geo->origin.x + (int)geo->size.w + && origin.y < geo->origin.y + (int)geo->size.h) { + // Hack: we force focus upon unmanaged views here + wlc_view_focus(*handle); + return NULL; + } + } + // if tabbed/stacked go directly to focused container, otherwise search + // children + if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) { + lookup = lookup->focused; + continue; + } + // if workspace, search floating + if (lookup->type == C_WORKSPACE) { + i = len = lookup->floating->length; + bool got_floating = false; + while (--i > -1) { + if (pointer_test(lookup->floating->items[i], &origin)) { + lookup = lookup->floating->items[i]; + got_floating = true; + break; + } + } + if (got_floating) { + continue; + } + } + // search children + len = lookup->children->length; + for (i = 0; i < len; ++i) { + if (pointer_test(lookup->children->items[i], &origin)) { + lookup = lookup->children->items[i]; + break; + } + } + // when border and titles are done, this could happen + if (i == len) { + break; + } + } + return lookup; +} + +swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), const void *data) { + if (container->children == NULL || container->children->length == 0) { + return NULL; + } + + swayc_t *con; + if (container->type == C_WORKSPACE) { + for (int i = 0; i < container->floating->length; ++i) { + con = container->floating->items[i]; + if (f(con, data)) { + return con; + } + con = container_find(con, f, data); + if (con != NULL) { + return con; + } + } + } + + for (int i = 0; i < container->children->length; ++i) { + con = container->children->items[i]; + if (f(con, data)) { + return con; + } + + con = container_find(con, f, data); + if (con != NULL) { + return con; + } + } + + return NULL; +} + +// Container information + +bool swayc_is_fullscreen(swayc_t *view) { + return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN); +} + +bool swayc_is_active(swayc_t *view) { + return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_ACTIVATED); +} + +bool swayc_is_parent_of(swayc_t *parent, swayc_t *child) { + while (child != &root_container) { + child = child->parent; + if (child == parent) { + return true; + } + } + return false; +} + +bool swayc_is_child_of(swayc_t *child, swayc_t *parent) { + return swayc_is_parent_of(parent, child); +} + +bool swayc_is_empty_workspace(swayc_t *container) { + return container->type == C_WORKSPACE && container->children->length == 0; +} + +int swayc_gap(swayc_t *container) { + if (container->type == C_VIEW || container->type == C_CONTAINER) { + return container->gaps >= 0 ? container->gaps : config->gaps_inner; + } else if (container->type == C_WORKSPACE) { + int base = container->gaps >= 0 ? container->gaps : config->gaps_outer; + if (config->edge_gaps && !(config->smart_gaps && container->children->length == 1)) { + // the inner gap is created via a margin around each window which + // is half the gap size, so the workspace also needs half a gap + // size to make the outermost gap the same size (excluding the + // actual "outer gap" size which is handled independently) + return base + config->gaps_inner / 2; + } else if (config->smart_gaps && container->children->length == 1) { + return 0; + } else { + return base; + } + } else { + return 0; + } +} + +// Mapping + +void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { + if (container) { + int i; + if (container->children) { + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + container_map(child, f, data); + } + } + if (container->floating) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *child = container->floating->items[i]; + container_map(child, f, data); + } + } + f(container, data); + } +} + +void update_visibility_output(swayc_t *container, wlc_handle output) { + // Inherit visibility + swayc_t *parent = container->parent; + container->visible = parent->visible; + // special cases where visibility depends on focus + if (parent->type == C_OUTPUT || parent->layout == L_TABBED || + parent->layout == L_STACKED) { + container->visible = parent->focused == container && parent->visible; + } + // Set visibility and output for view + if (container->type == C_VIEW) { + wlc_view_set_output(container->handle, output); + wlc_view_set_mask(container->handle, container->visible ? VISIBLE : 0); + } + // Update visibility for children + else { + if (container->children) { + int i, len = container->children->length; + for (i = 0; i < len; ++i) { + update_visibility_output(container->children->items[i], output); + } + } + if (container->floating) { + int i, len = container->floating->length; + for (i = 0; i < len; ++i) { + update_visibility_output(container->floating->items[i], output); + } + } + } +} + +void update_visibility(swayc_t *container) { + if (!container) return; + switch (container->type) { + case C_ROOT: + container->visible = true; + if (container->children) { + int i, len = container->children->length; + for (i = 0; i < len; ++i) { + update_visibility(container->children->items[i]); + } + } + return; + + case C_OUTPUT: + container->visible = true; + if (container->children) { + int i, len = container->children->length; + for (i = 0; i < len; ++i) { + update_visibility_output(container->children->items[i], container->handle); + } + } + return; + + default: + { + swayc_t *op = swayc_parent_by_type(container, C_OUTPUT); + update_visibility_output(container, op->handle); + } + } +} + +void set_gaps(swayc_t *view, void *_data) { + int *data = _data; + if (!ASSERT_NONNULL(view)) { + return; + } + if (view->type == C_WORKSPACE || view->type == C_VIEW) { + view->gaps = *data; + } +} + +void add_gaps(swayc_t *view, void *_data) { + int *data = _data; + if (!ASSERT_NONNULL(view)) { + return; + } + if (view->type == C_WORKSPACE || view->type == C_VIEW) { + if ((view->gaps += *data) < 0) { + view->gaps = 0; + } + } +} + +static void close_view(swayc_t *container, void *data) { + if (container->type == C_VIEW) { + wlc_view_close(container->handle); + } +} + +void close_views(swayc_t *container) { + container_map(container, close_view, NULL); +} + +swayc_t *swayc_tabbed_stacked_ancestor(swayc_t *view) { + swayc_t *parent = NULL; + if (!ASSERT_NONNULL(view)) { + return NULL; + } + while (view->type != C_WORKSPACE && view->parent && view->parent->type != C_WORKSPACE) { + view = view->parent; + if (view->layout == L_TABBED || view->layout == L_STACKED) { + parent = view; + } + } + + return parent; +} + +swayc_t *swayc_tabbed_stacked_parent(swayc_t *con) { + if (!ASSERT_NONNULL(con)) { + return NULL; + } + if (con->parent && (con->parent->layout == L_TABBED || con->parent->layout == L_STACKED)) { + return con->parent; + } + return NULL; +} + +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { + // if layout change modifies the auto layout's major axis, swap width and height + // to preserve current ratios. + if (is_auto_layout(layout) && is_auto_layout(container->layout)) { + enum swayc_layouts prev_major = + container->layout == L_AUTO_LEFT || container->layout == L_AUTO_RIGHT + ? L_HORIZ : L_VERT; + enum swayc_layouts new_major = + layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT + ? L_HORIZ : L_VERT; + if (new_major != prev_major) { + for (int i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + double h = child->height; + child->height = child->width; + child->width = h; + } + } + } + if (container->type == C_WORKSPACE) { + container->workspace_layout = layout; + if (layout == L_HORIZ || layout == L_VERT || is_auto_layout(layout)) { + container->layout = layout; + } + } else { + container->layout = layout; + } + return container; +} diff --git a/sway/old/criteria.c b/sway/old/criteria.c new file mode 100644 index 00000000..e8978ebe --- /dev/null +++ b/sway/old/criteria.c @@ -0,0 +1,451 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include "sway/criteria.h" +#include "sway/container.h" +#include "sway/config.h" +#include "stringop.h" +#include "list.h" +#include "log.h" + +enum criteria_type { // *must* keep in sync with criteria_strings[] + CRIT_CLASS, + CRIT_CON_ID, + CRIT_CON_MARK, + CRIT_FLOATING, + CRIT_ID, + CRIT_INSTANCE, + CRIT_TILING, + CRIT_TITLE, + CRIT_URGENT, + CRIT_WINDOW_ROLE, + CRIT_WINDOW_TYPE, + CRIT_WORKSPACE, + CRIT_LAST +}; + +static const char * const criteria_strings[CRIT_LAST] = { + [CRIT_CLASS] = "class", + [CRIT_CON_ID] = "con_id", + [CRIT_CON_MARK] = "con_mark", + [CRIT_FLOATING] = "floating", + [CRIT_ID] = "id", + [CRIT_INSTANCE] = "instance", + [CRIT_TILING] = "tiling", + [CRIT_TITLE] = "title", + [CRIT_URGENT] = "urgent", // either "latest" or "oldest" ... + [CRIT_WINDOW_ROLE] = "window_role", + [CRIT_WINDOW_TYPE] = "window_type", + [CRIT_WORKSPACE] = "workspace" +}; + +/** + * A single criteria token (ie. value/regex pair), + * e.g. 'class="some class regex"'. + */ +struct crit_token { + enum criteria_type type; + pcre *regex; + char *raw; +}; + +static void free_crit_token(struct crit_token *crit) { + pcre_free(crit->regex); + free(crit->raw); + free(crit); +} + +static void free_crit_tokens(list_t *crit_tokens) { + for (int i = 0; i < crit_tokens->length; i++) { + free_crit_token(crit_tokens->items[i]); + } + list_free(crit_tokens); +} + +// Extracts criteria string from its brackets. Returns new (duplicate) +// substring. +static char *criteria_from(const char *arg) { + char *criteria = NULL; + if (*arg == '[') { + criteria = strdup(arg + 1); + } else { + criteria = strdup(arg); + } + + int last = strlen(criteria) - 1; + if (criteria[last] == ']') { + criteria[last] = '\0'; + } + return criteria; +} + +// Return instances of c found in str. +static int countchr(char *str, char c) { + int found = 0; + for (int i = 0; str[i]; i++) { + if (str[i] == c) { + ++found; + } + } + return found; +} + +// criteria_str is e.g. '[class="some class regex" instance="instance name"]'. +// +// Will create array of pointers in buf, where first is duplicate of given +// string (must be freed) and the rest are pointers to names and values in the +// base string (every other, naturally). argc will be populated with the length +// of buf. +// +// Returns error string or NULL if successful. +static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str) { + sway_log(L_DEBUG, "Parsing criteria: '%s'", criteria_str); + char *base = criteria_from(criteria_str); + char *head = base; + char *namep = head; // start of criteria name + char *valp = NULL; // start of value + + // We're going to place EOS markers where we need to and fill up an array + // of pointers to the start of each token (either name or value). + int pairs = countchr(base, '='); + int max_tokens = pairs * 2 + 1; // this gives us at least enough slots + + char **argv = *buf = calloc(max_tokens, sizeof(char*)); + argv[0] = base; // this needs to be freed by caller + bool quoted = true; + + *argc = 1; // uneven = name, even = value + while (*head && *argc < max_tokens) { + if (namep != head && *(head - 1) == '\\') { + // escaped character: don't try to parse this + } else if (*head == '=' && namep != head) { + if (*argc % 2 != 1) { + // we're not expecting a name + return strdup("Unable to parse criteria: " + "Found out of place equal sign"); + } else { + // name ends here + char *end = head; // don't want to rewind the head + while (*(end - 1) == ' ') { + --end; + } + *end = '\0'; + if (*(namep) == ' ') { + namep = strrchr(namep, ' ') + 1; + } + argv[*argc] = namep; + *argc += 1; + } + } else if (*head == '"') { + if (*argc % 2 != 0) { + // we're not expecting a value + return strdup("Unable to parse criteria: " + "Found quoted value where it was not expected"); + } else if (!valp) { // value starts here + valp = head + 1; + quoted = true; + } else { + // value ends here + argv[*argc] = valp; + *argc += 1; + *head = '\0'; + valp = NULL; + namep = head + 1; + } + } else if (*argc % 2 == 0 && *head != ' ') { + // parse unquoted values + if (!valp) { + quoted = false; + valp = head; // value starts here + } + } else if (valp && !quoted && *head == ' ') { + // value ends here + argv[*argc] = valp; + *argc += 1; + *head = '\0'; + valp = NULL; + namep = head + 1; + } + head++; + } + + // catch last unquoted value if needed + if (valp && !quoted && !*head) { + argv[*argc] = valp; + *argc += 1; + } + + return NULL; +} + +// Returns error string on failure or NULL otherwise. +static char *parse_criteria_name(enum criteria_type *type, char *name) { + *type = CRIT_LAST; + for (int i = 0; i < CRIT_LAST; i++) { + if (strcmp(criteria_strings[i], name) == 0) { + *type = (enum criteria_type) i; + break; + } + } + if (*type == CRIT_LAST) { + const char *fmt = "Criteria type '%s' is invalid or unsupported."; + int len = strlen(name) + strlen(fmt) - 1; + char *error = malloc(len); + snprintf(error, len, fmt, name); + return error; + } else if (*type == CRIT_URGENT || *type == CRIT_WINDOW_ROLE || + *type == CRIT_WINDOW_TYPE) { + // (we're just being helpful here) + const char *fmt = "\"%s\" criteria currently unsupported, " + "no window will match this"; + int len = strlen(fmt) + strlen(name) - 1; + char *error = malloc(len); + snprintf(error, len, fmt, name); + return error; + } + return NULL; +} + +// Returns error string on failure or NULL otherwise. +static char *generate_regex(pcre **regex, char *value) { + const char *reg_err; + int offset; + + *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL); + + if (!*regex) { + const char *fmt = "Regex compilation (for '%s') failed: %s"; + int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; + char *error = malloc(len); + snprintf(error, len, fmt, value, reg_err); + return error; + } + return NULL; +} + +// Test whether the criterion corresponds to the currently focused window +static bool crit_is_focused(const char *value) { + return !strcmp(value, "focused") || !strcmp(value, "__focused__"); +} + +// Populate list with crit_tokens extracted from criteria string, returns error +// string or NULL if successful. +char *extract_crit_tokens(list_t *tokens, const char * const criteria) { + int argc; + char **argv = NULL, *error = NULL; + if ((error = crit_tokens(&argc, &argv, criteria))) { + goto ect_cleanup; + } + for (int i = 1; i + 1 < argc; i += 2) { + char* name = argv[i], *value = argv[i + 1]; + struct crit_token *token = calloc(1, sizeof(struct crit_token)); + token->raw = strdup(value); + + if ((error = parse_criteria_name(&token->type, name))) { + free_crit_token(token); + goto ect_cleanup; + } else if (token->type == CRIT_URGENT || crit_is_focused(value)) { + sway_log(L_DEBUG, "%s -> \"%s\"", name, value); + list_add(tokens, token); + } else if((error = generate_regex(&token->regex, value))) { + free_crit_token(token); + goto ect_cleanup; + } else { + sway_log(L_DEBUG, "%s -> /%s/", name, value); + list_add(tokens, token); + } + } +ect_cleanup: + free(argv[0]); // base string + free(argv); + return error; +} + +static int regex_cmp(const char *item, const pcre *regex) { + return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); +} + +// test a single view if it matches list of criteria tokens (all of them). +static bool criteria_test(swayc_t *cont, list_t *tokens) { + if (cont->type != C_VIEW) { + return false; + } + int matches = 0; + for (int i = 0; i < tokens->length; i++) { + struct crit_token *crit = tokens->items[i]; + switch (crit->type) { + case CRIT_CLASS: + if (!cont->class) { + // ignore + } else if (crit_is_focused(crit->raw)) { + swayc_t *focused = get_focused_view(&root_container); + if (focused->class && strcmp(cont->class, focused->class) == 0) { + matches++; + } + } else if (crit->regex && regex_cmp(cont->class, crit->regex) == 0) { + matches++; + } + break; + case CRIT_CON_ID: { + char *endptr; + size_t crit_id = strtoul(crit->raw, &endptr, 10); + + if (*endptr == 0 && cont->id == crit_id) { + ++matches; + } + break; + } + case CRIT_CON_MARK: + if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) { + // Make sure it isn't matching the NUL string + if ((strcmp(crit->raw, "") == 0) == (list_seq_find(cont->marks, (int (*)(const void *, const void *))strcmp, "") != -1)) { + ++matches; + } + } + break; + case CRIT_FLOATING: + if (cont->is_floating) { + matches++; + } + break; + case CRIT_ID: + if (!cont->app_id) { + // ignore + } else if (crit->regex && regex_cmp(cont->app_id, crit->regex) == 0) { + matches++; + } + break; + case CRIT_INSTANCE: + if (!cont->instance) { + // ignore + } else if (crit_is_focused(crit->raw)) { + swayc_t *focused = get_focused_view(&root_container); + if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { + matches++; + } + } else if (crit->regex && regex_cmp(cont->instance, crit->regex) == 0) { + matches++; + } + break; + case CRIT_TILING: + if (!cont->is_floating) { + matches++; + } + break; + case CRIT_TITLE: + if (!cont->name) { + // ignore + } else if (crit_is_focused(crit->raw)) { + swayc_t *focused = get_focused_view(&root_container); + if (focused->name && strcmp(cont->name, focused->name) == 0) { + matches++; + } + } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) { + matches++; + } + break; + case CRIT_URGENT: // "latest" or "oldest" + break; + case CRIT_WINDOW_ROLE: + break; + case CRIT_WINDOW_TYPE: + // TODO wlc indeed exposes this information + break; + case CRIT_WORKSPACE: ; + swayc_t *cont_ws = swayc_parent_by_type(cont, C_WORKSPACE); + if (!cont_ws || !cont_ws->name) { + // ignore + } else if (crit_is_focused(crit->raw)) { + swayc_t *focused_ws = swayc_active_workspace(); + if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { + matches++; + } + } else if (crit->regex && regex_cmp(cont_ws->name, crit->regex) == 0) { + matches++; + } + break; + default: + sway_abort("Invalid criteria type (%i)", crit->type); + break; + } + } + return matches == tokens->length; +} + +int criteria_cmp(const void *a, const void *b) { + if (a == b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + const struct criteria *crit_a = a, *crit_b = b; + int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); + if (cmp != 0) { + return cmp; + } + return lenient_strcmp(crit_a->crit_raw, crit_b->crit_raw); +} + +void free_criteria(struct criteria *crit) { + if (crit->tokens) { + free_crit_tokens(crit->tokens); + } + if (crit->cmdlist) { + free(crit->cmdlist); + } + if (crit->crit_raw) { + free(crit->crit_raw); + } + free(crit); +} + +bool criteria_any(swayc_t *cont, list_t *criteria) { + for (int i = 0; i < criteria->length; i++) { + struct criteria *bc = criteria->items[i]; + if (criteria_test(cont, bc->tokens)) { + return true; + } + } + return false; +} + +list_t *criteria_for(swayc_t *cont) { + list_t *criteria = config->criteria, *matches = create_list(); + for (int i = 0; i < criteria->length; i++) { + struct criteria *bc = criteria->items[i]; + if (criteria_test(cont, bc->tokens)) { + list_add(matches, bc); + } + } + return matches; +} + +struct list_tokens { + list_t *list; + list_t *tokens; +}; + +static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) { + if (criteria_test(container, list_tokens->tokens)) { + list_add(list_tokens->list, container); + } +} + +list_t *container_for(list_t *tokens) { + struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; + + container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens); + + for (int i = 0; i < scratchpad->length; ++i) { + swayc_t *c = scratchpad->items[i]; + if (criteria_test(c, tokens)) { + list_add(list_tokens.list, c); + } + } + + return list_tokens.list; +} diff --git a/sway/old/focus.c b/sway/old/focus.c new file mode 100644 index 00000000..66f7ee17 --- /dev/null +++ b/sway/old/focus.c @@ -0,0 +1,278 @@ +#include "stdbool.h" +#include +#include "sway/focus.h" +#include "sway/workspace.h" +#include "sway/layout.h" +#include "sway/config.h" +#include "sway/input_state.h" +#include "sway/ipc-server.h" +#include "sway/border.h" +#include "log.h" + +bool locked_container_focus = false; +bool suspend_workspace_cleanup = false; + +// switches parent focus to c. will switch it accordingly +static void update_focus(swayc_t *c) { + // Handle if focus switches + swayc_t *parent = c->parent; + if (!parent) return; + if (parent->focused != c) { + // Get previous focus + swayc_t *prev = parent->focused; + // Set new focus + parent->focused = c; + + switch (c->type) { + // Shouldn't happen + case C_ROOT: return; + + // Case where output changes + case C_OUTPUT: + wlc_output_focus(c->handle); + break; + + // Case where workspace changes + case C_WORKSPACE: + if (prev) { + ipc_event_workspace(prev, c, "focus"); + + // if the old workspace has no children, destroy it + if(prev->children->length == 0 && prev->floating->length == 0 && !suspend_workspace_cleanup) { + destroy_workspace(prev); + } else { + // update visibility of old workspace + update_visibility(prev); + } + } + // Update visibility of newly focused workspace + update_visibility(c); + break; + + default: + case C_VIEW: + case C_CONTAINER: + break; + } + } +} + +bool move_focus(enum movement_direction direction) { + swayc_t *old_view = get_focused_container(&root_container); + swayc_t *new_view = get_swayc_in_direction(old_view, direction); + if (!new_view) { + return false; + } else if (new_view->type == C_ROOT) { + sway_log(L_DEBUG, "Not setting focus above the workspace level"); + return false; + } else if (new_view->type == C_OUTPUT) { + return set_focused_container(swayc_active_workspace_for(new_view)); + } else if (direction == MOVE_PARENT || direction == MOVE_CHILD) { + return set_focused_container(new_view); + } else if (config->mouse_warping) { + swayc_t *old_op = old_view->type == C_OUTPUT ? + old_view : swayc_parent_by_type(old_view, C_OUTPUT); + swayc_t *focused = get_focused_view(new_view); + if (set_focused_container(focused)) { + if (old_op != swayc_active_output() && focused && focused->type == C_VIEW) { + center_pointer_on(focused); + } + return true; + } + } else { + return set_focused_container(get_focused_view(new_view)); + } + return false; +} + +swayc_t *get_focused_container(swayc_t *parent) { + if (!parent) { + return swayc_active_workspace(); + } + while (!parent->is_focused && parent->focused) { + parent = parent->focused; + } + return parent; +} + +bool set_focused_container(swayc_t *c) { + if (locked_container_focus || !c || !c->parent) { + return false; + } + + // current ("old") workspace for sending workspace change event later + swayc_t *old_ws = swayc_active_workspace(); + // keep track of child count so we can determine if it gets destroyed + int old_ws_child_count = 0; + if (old_ws) { + old_ws_child_count = old_ws->children->length + old_ws->floating->length; + } + + // current ("old") focused container + swayc_t *old_focus = get_focused_container(&root_container); + // if old_focus is a workspace, then it's the same workspace as + // old_ws, and we'll need to null its pointer too, since it will + // be destroyed in the update_focus() call + bool old_focus_was_ws = (old_focus->type == C_WORKSPACE); + + // workspace of new focused container + swayc_t *workspace = swayc_active_workspace_for(c); + + if (swayc_is_fullscreen(get_focused_container(workspace))) { + // if switching to a workspace with a fullscreen view, + // focus on the fullscreen view + c = get_focused_container(workspace); + } + + swayc_log(L_DEBUG, c, "Setting focus to %p:%" PRIuPTR, c, c->handle); + + if (c->type == C_VIEW) { + // dispatch a window event + ipc_event_window(c, "focus"); + } + + // update the global pointer + current_focus = c; + + // update container focus from here to root, making necessary changes along + // the way + swayc_t *p = c; + if (p->type != C_OUTPUT && p->type != C_ROOT) { + p->is_focused = true; + } + while (p != &root_container) { + update_focus(p); + p = p->parent; + p->is_focused = false; + } + + if (old_focus_was_ws && old_ws_child_count == 0) { + // this workspace was destroyed in update_focus(), so null the pointers + old_focus = NULL; + old_ws = NULL; + } + + if (!(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) { + if (old_focus) { + if (old_focus->type == C_VIEW) { + wlc_view_set_state(old_focus->handle, WLC_BIT_ACTIVATED, false); + } + update_container_border(old_focus); + } + if (c->type == C_VIEW) { + wlc_view_set_state(c->handle, WLC_BIT_ACTIVATED, true); + } + /* TODO WLR + if (!desktop_shell.is_locked) { + // If the system is locked, we do everything _but_ actually setting + // focus. This includes making our internals think that this view is + // focused. + wlc_view_focus(c->handle); + } + */ + if (c->parent->layout != L_TABBED && c->parent->layout != L_STACKED) { + update_container_border(c); + } + + swayc_t *parent = swayc_tabbed_stacked_ancestor(c); + if (parent != NULL) { + arrange_backgrounds(); + arrange_windows(parent, -1, -1); + } + } + + if (old_ws != workspace) { + // old_ws might be NULL here but that's ok + ipc_event_workspace(old_ws, workspace, "focus"); + } + + return true; +} + +bool set_focused_container_for(swayc_t *a, swayc_t *c) { + if (locked_container_focus || !c) { + return false; + } + swayc_t *find = c; + while (find != a && (find = find->parent)) { + if (find == &root_container) { + return false; + } + } + + // Get workspace for c, get that workspaces current focused container. + swayc_t *workspace = swayc_active_workspace_for(c); + swayc_t *focused = get_focused_view(workspace); + // if the workspace we are changing focus to has a fullscreen view return + if (swayc_is_fullscreen(focused) && c != focused) { + return false; + } + + // Check if we are changing a parent container that will see change + bool effective = true; + while (find != &root_container) { + if (find->parent->focused != find) { + effective = false; + } + find = find->parent; + } + if (effective) { + // Go to set_focused_container + return set_focused_container(c); + } + + sway_log(L_DEBUG, "Setting focus for %p:%" PRIuPTR " to %p:%" PRIuPTR, + a, a->handle, c, c->handle); + + c->is_focused = true; + swayc_t *p = c; + while (p != a) { + update_focus(p); + p = p->parent; + p->is_focused = false; + } + return true; +} + +swayc_t *get_focused_view(swayc_t *parent) { + swayc_t *c = parent; + while (c && c->type != C_VIEW) { + if (c->type == C_WORKSPACE && c->focused == NULL) { + return c; + } + c = c->focused; + } + if (c == NULL) { + c = swayc_active_workspace_for(parent); + } + return c; +} + +swayc_t *get_focused_float(swayc_t *ws) { + if(!sway_assert(ws->type == C_WORKSPACE, "must be of workspace type")) { + ws = swayc_active_workspace(); + } + if (ws->floating->length) { + return ws->floating->items[ws->floating->length - 1]; + } + return NULL; +} + +swayc_t *get_focused_view_include_floating(swayc_t *parent) { + swayc_t *c = parent; + swayc_t *f = NULL; + + while (c && c->type != C_VIEW) { + if (c->type == C_WORKSPACE && c->focused == NULL) { + return ((f = get_focused_float(c))) ? f : c; + } + + c = c->focused; + } + + if (c == NULL) { + c = swayc_active_workspace_for(parent); + } + + return c; +} diff --git a/sway/old/handlers.c b/sway/old/handlers.c new file mode 100644 index 00000000..33e75d6b --- /dev/null +++ b/sway/old/handlers.c @@ -0,0 +1,1143 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sway/handlers.h" +#include "sway/border.h" +#include "sway/layout.h" +#include "sway/config.h" +#include "sway/commands.h" +#include "sway/workspace.h" +#include "sway/container.h" +#include "sway/output.h" +#include "sway/focus.h" +#include "sway/input_state.h" +#include "sway/extensions.h" +#include "sway/criteria.h" +#include "sway/ipc-server.h" +#include "sway/input.h" +#include "sway/security.h" +#include "list.h" +#include "stringop.h" +#include "log.h" + +// Event should be sent to client +#define EVENT_PASSTHROUGH false + +// Event handled by sway and should not be sent to client +#define EVENT_HANDLED true + +static struct panel_config *if_panel_find_config(struct wl_client *client) { + int i; + for (i = 0; i < desktop_shell.panels->length; i++) { + struct panel_config *config = desktop_shell.panels->items[i]; + if (config->client == client) { + return config; + } + } + return NULL; +} + +static struct background_config *if_background_find_config(struct wl_client *client) { + int i; + for (i = 0; i < desktop_shell.backgrounds->length; i++) { + struct background_config *config = desktop_shell.backgrounds->items[i]; + if (config->client == client) { + return config; + } + } + return NULL; +} + +static struct wlc_geometry compute_panel_geometry(struct panel_config *config) { + struct wlc_size resolution; + output_get_scaled_size(config->output, &resolution); + const struct wlc_geometry *old = wlc_view_get_geometry(config->handle); + struct wlc_geometry new; + + switch (config->panel_position) { + case DESKTOP_SHELL_PANEL_POSITION_TOP: + new.origin.x = 0; + new.origin.y = 0; + new.size.w = resolution.w; + new.size.h = old->size.h; + break; + case DESKTOP_SHELL_PANEL_POSITION_BOTTOM: + new.origin.x = 0; + new.origin.y = resolution.h - old->size.h; + new.size.w = resolution.w; + new.size.h = old->size.h; + break; + case DESKTOP_SHELL_PANEL_POSITION_LEFT: + new.origin.x = 0; + new.origin.y = 0; + new.size.w = old->size.w; + new.size.h = resolution.h; + break; + case DESKTOP_SHELL_PANEL_POSITION_RIGHT: + new.origin.x = resolution.w - old->size.w; + new.origin.y = 0; + new.size.w = old->size.w; + new.size.h = resolution.h; + break; + } + + return new; +} + +static void update_panel_geometry(struct panel_config *config) { + struct wlc_geometry geometry = compute_panel_geometry(config); + wlc_view_set_geometry(config->handle, 0, &geometry); +} + +static void update_panel_geometries(wlc_handle output) { + for (int i = 0; i < desktop_shell.panels->length; i++) { + struct panel_config *config = desktop_shell.panels->items[i]; + if (config->output == output) { + update_panel_geometry(config); + } + } +} + +static void update_background_geometry(struct background_config *config) { + struct wlc_geometry geometry = wlc_geometry_zero; + output_get_scaled_size(config->output, &geometry.size); + wlc_view_set_geometry(config->handle, 0, &geometry); +} + +static void update_background_geometries(wlc_handle output) { + for (int i = 0; i < desktop_shell.backgrounds->length; i++) { + struct background_config *config = desktop_shell.backgrounds->items[i]; + if (config->output == output) { + update_background_geometry(config); + } + } +} + +/* Handles */ + +static bool handle_input_created(struct libinput_device *device) { + const char *identifier = libinput_dev_unique_id(device); + if (!identifier) { + sway_log(L_ERROR, "Unable to allocate unique name for input device %p", + device); + return true; + } + sway_log(L_INFO, "Found input device (%s)", identifier); + + list_add(input_devices, device); + + struct input_config *ic = NULL; + int i; + for (i = 0; i < config->input_configs->length; ++i) { + struct input_config *cur = config->input_configs->items[i]; + if (strcasecmp(identifier, cur->identifier) == 0) { + sway_log(L_DEBUG, "Matched input config for %s", + identifier); + ic = cur; + break; + } + if (strcasecmp("*", cur->identifier) == 0) { + sway_log(L_DEBUG, "Matched wildcard input config for %s", + identifier); + ic = cur; + break; + } + } + + apply_input_config(ic, device); + return true; +} + +static void handle_input_destroyed(struct libinput_device *device) { + int i; + list_t *list = input_devices; + for (i = 0; i < list->length; ++i) { + if(((struct libinput_device *)list->items[i]) == device) { + list_del(list, i); + break; + } + } +} + +static bool handle_output_created(wlc_handle output) { + swayc_t *op = new_output(output); + + // Visibility mask to be able to make view invisible + wlc_output_set_mask(output, VISIBLE); + + if (!op) { + return false; + } + + // Switch to workspace if we need to + if (swayc_active_workspace() == NULL) { + swayc_t *ws = op->children->items[0]; + workspace_switch(ws); + } + + // Fixes issues with backgrounds and wlc + wlc_handle prev = wlc_get_focused_output(); + wlc_output_focus(output); + wlc_output_focus(prev); + return true; +} + +static void handle_output_destroyed(wlc_handle output) { + int i; + list_t *list = root_container.children; + for (i = 0; i < list->length; ++i) { + if (((swayc_t *)list->items[i])->handle == output) { + break; + } + } + if (i < list->length) { + destroy_output(list->items[i]); + } else { + return; + } + if (list->length > 0) { + // switch to other outputs active workspace + workspace_switch(((swayc_t *)root_container.children->items[0])->focused); + } +} + +static void handle_output_post_render(wlc_handle output) { + ipc_get_pixels(output); +} + +static void handle_view_pre_render(wlc_handle view) { + render_view_borders(view); +} + +static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { + sway_log(L_DEBUG, "Output %u resolution changed to %d x %d", (unsigned int)output, to->w, to->h); + + swayc_t *c = swayc_by_handle(output); + if (!c) { + return; + } + c->width = to->w; + c->height = to->h; + + update_panel_geometries(output); + update_background_geometries(output); + + arrange_windows(&root_container, -1, -1); +} + +static void handle_output_focused(wlc_handle output, bool focus) { + swayc_t *c = swayc_by_handle(output); + // if for some reason this output doesn't exist, create it. + if (!c) { + handle_output_created(output); + } + if (focus) { + set_focused_container(get_focused_container(c)); + } +} + +static void 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--; + } + } + } + } +} + +static void positioner_place_window(wlc_handle handle) { + const struct wlc_geometry *anchor = wlc_view_positioner_get_anchor_rect(handle); + const struct wlc_size *sr = wlc_view_positioner_get_size(handle); + // a positioner is required to have a non-null anchor and a non-negative size + if (!anchor || !sr || + sr->w <= 0 || sr->h <= 0 || + anchor->size.w <= 0 || anchor->size.h <= 0) { + return; + } + const struct wlc_point offset = *wlc_view_positioner_get_offset(handle); + enum wlc_positioner_anchor_bit anchors = wlc_view_positioner_get_anchor(handle); + enum wlc_positioner_gravity_bit gravity = wlc_view_positioner_get_gravity(handle); + struct wlc_geometry geo = { + .origin = offset, + .size = *sr + }; + + if (anchors & WLC_BIT_ANCHOR_TOP) { + geo.origin.y += anchor->origin.y; + } else if (anchors & WLC_BIT_ANCHOR_BOTTOM) { + geo.origin.y += anchor->origin.y + anchor->size.h; + } else { + geo.origin.y += anchor->origin.y + anchor->size.h / 2; + } + if (anchors & WLC_BIT_ANCHOR_LEFT) { + geo.origin.x += anchor->origin.x; + } else if (anchors & WLC_BIT_ANCHOR_RIGHT) { + geo.origin.x += anchor->origin.x + anchor->size.w; + } else { + geo.origin.x += anchor->origin.x + anchor->size.w / 2; + } + + if (gravity & WLC_BIT_GRAVITY_TOP) { + geo.origin.y -= geo.size.h; + } else if (gravity & WLC_BIT_GRAVITY_BOTTOM) { + /* default */ + } else { + geo.origin.y -= geo.size.h / 2; + } + if (gravity & WLC_BIT_GRAVITY_LEFT) { + geo.origin.x -= geo.size.w; + } else if (gravity & WLC_BIT_GRAVITY_RIGHT) { + /* default */ + } else { + geo.origin.x -= geo.size.w / 2; + } + + sway_log(L_DEBUG, "xdg-positioner: placing window %" PRIuPTR " " + "sized (%u,%u) offset by (%d,%d), " + "anchor rectangle sized (%u,%u) at (%d,%d), " + "anchor edges: %s %s, gravity: %s %s", + handle, + sr->w, sr->h, offset.x, offset.y, + anchor->size.w, anchor->size.h, anchor->origin.x, anchor->origin.y, + anchors & WLC_BIT_ANCHOR_TOP ? "top" : + (anchors & WLC_BIT_ANCHOR_BOTTOM ? "bottom" : "middle"), + anchors & WLC_BIT_ANCHOR_LEFT ? "left" : + (anchors & WLC_BIT_ANCHOR_RIGHT ? "right" : "center"), + gravity & WLC_BIT_GRAVITY_TOP ? "top" : + (gravity & WLC_BIT_GRAVITY_BOTTOM ? "bottom" : "middle"), + gravity & WLC_BIT_GRAVITY_LEFT ? "left" : + (gravity & WLC_BIT_GRAVITY_RIGHT ? "right" : "center")); + + wlc_handle parent = wlc_view_get_parent(handle); + if (parent) { + const struct wlc_geometry *pg = wlc_view_get_geometry(parent); + geo.origin.x += pg->origin.x; + geo.origin.y += pg->origin.y; + } + wlc_view_set_geometry(handle, 0, &geo); +} + +static bool handle_view_created(wlc_handle handle) { + // if view is child of another view, the use that as focused container + wlc_handle parent = wlc_view_get_parent(handle); + swayc_t *focused = NULL; + swayc_t *newview = NULL; + swayc_t *current_ws = swayc_active_workspace(); + bool return_to_workspace = false; + struct wl_client *client = wlc_view_get_wl_client(handle); + struct wl_resource *resource = wlc_surface_get_wl_resource( + wlc_view_get_surface(handle)); + pid_t pid; + struct panel_config *panel_config = NULL; + struct background_config *background_config = NULL; + + panel_config = if_panel_find_config(client); + if (panel_config) { + panel_config->handle = handle; + update_panel_geometry(panel_config); + wlc_view_set_mask(handle, VISIBLE); + wlc_view_set_output(handle, panel_config->output); + wlc_view_bring_to_front(handle); + arrange_windows(&root_container, -1, -1); + return true; + } + + background_config = if_background_find_config(client); + if (background_config) { + background_config->handle = handle; + update_background_geometry(background_config); + wlc_view_set_mask(handle, VISIBLE); + wlc_view_set_output(handle, background_config->output); + wlc_view_send_to_back(handle); + return true; + } + + // Get parent container, to add view in + if (parent) { + focused = swayc_by_handle(parent); + } + + if (client) { + pid = wlc_view_get_pid(handle); + + if (pid) { + // using newview as a temp storage location here, + // rather than adding yet another workspace var + newview = workspace_for_pid(pid); + if (newview) { + focused = get_focused_container(newview); + return_to_workspace = true; + } + newview = NULL; + } + } + + swayc_t *prev_focus = get_focused_container(&root_container); + + if (!focused || focused->type == C_OUTPUT) { + focused = prev_focus; + // Move focus from floating view + if (focused->is_floating) { + // To workspace if there are no children + if (focused->parent->children->length == 0) { + focused = focused->parent; + } + // TODO find a better way of doing this + // Or to focused container + else { + focused = get_focused_container(focused->parent->children->items[0]); + } + } + } + + positioner_place_window(handle); + + sway_log(L_DEBUG, "handle:%" PRIuPTR " type:%x state:%x parent:%" PRIuPTR " " + "mask:%d (x:%d y:%d w:%d h:%d) title:%s " + "class:%s appid:%s", + handle, wlc_view_get_type(handle), wlc_view_get_state(handle), parent, + wlc_view_get_mask(handle), wlc_view_get_geometry(handle)->origin.x, + wlc_view_get_geometry(handle)->origin.y,wlc_view_get_geometry(handle)->size.w, + wlc_view_get_geometry(handle)->size.h, wlc_view_get_title(handle), + wlc_view_get_class(handle), wlc_view_get_app_id(handle)); + + // TODO properly figure out how each window should be handled. + switch (wlc_view_get_type(handle)) { + // regular view created regularly + case 0: + if (parent) { + newview = new_floating_view(handle); + } else { + newview = new_view(focused, handle); + wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); + } + break; + + // Dmenu keeps viewfocus, but others with this flag don't, for now simulate + // dmenu + case WLC_BIT_OVERRIDE_REDIRECT: + wlc_view_focus(handle); + wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); + wlc_view_bring_to_front(handle); + break; + + // Firefox popups have this flag set. + case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: + wlc_view_bring_to_front(handle); + locked_container_focus = true; + break; + + // Modals, get focus, popups do not + case WLC_BIT_MODAL: + wlc_view_focus(handle); + wlc_view_bring_to_front(handle); + newview = new_floating_view(handle); + /* fallthrough */ + case WLC_BIT_POPUP: + wlc_view_bring_to_front(handle); + break; + } + + // Prevent current ws from being destroyed, if empty + suspend_workspace_cleanup = true; + + if (newview) { + ipc_event_window(newview, "new"); + set_focused_container(newview); + wlc_view_set_mask(handle, VISIBLE); + swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT); + arrange_windows(output, -1, -1); + // check if it matches for_window in config and execute if so + list_t *criteria = criteria_for(newview); + for (int i = 0; i < criteria->length; i++) { + struct criteria *crit = criteria->items[i]; + sway_log(L_DEBUG, "for_window '%s' matches new view %p, cmd: '%s'", + crit->crit_raw, newview, crit->cmdlist); + struct cmd_results *res = handle_command(crit->cmdlist, CONTEXT_CRITERIA); + if (res->status != CMD_SUCCESS) { + sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); + } + free_cmd_results(res); + // view must be focused for commands to affect it, so always + // refocus in-between command lists + set_focused_container(newview); + } + swayc_t *workspace = swayc_parent_by_type(focused, C_WORKSPACE); + if (workspace && workspace->fullscreen) { + set_focused_container(workspace->fullscreen); + } + for (int i = 0; i < decoration_state.csd_resources->length; ++i) { + struct wl_resource *res = decoration_state.csd_resources->items[i]; + if (res == resource) { + list_del(decoration_state.csd_resources, i); + server_decoration_enable_csd(handle); + break; + } + } + } else { + swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT); + wlc_handle *h = malloc(sizeof(wlc_handle)); + if (!h) { + sway_log(L_ERROR, "Unable to allocate window handle, view handler bailing out"); + return true; + } + *h = handle; + sway_log(L_DEBUG, "Adding unmanaged window %p to %p", h, output->unmanaged); + list_add(output->unmanaged, h); + wlc_view_set_mask(handle, VISIBLE); + } + + if (return_to_workspace && current_ws != swayc_active_workspace()) { + // we were on one workspace, switched to another to add this view, + // now let's return to where we were + workspace_switch(current_ws); + set_focused_container(get_focused_container(current_ws)); + } + if (prev_focus && prev_focus->type == C_VIEW + && newview && criteria_any(newview, config->no_focus)) { + // Restore focus + swayc_t *ws = swayc_parent_by_type(newview, C_WORKSPACE); + if (!ws || ws != newview->parent + || ws->children->length + ws->floating->length != 1) { + sway_log(L_DEBUG, "no_focus: restoring focus to %s", prev_focus->name); + set_focused_container(prev_focus); + } + } + + suspend_workspace_cleanup = false; + ws_cleanup(); + return true; +} + +static void handle_view_destroyed(wlc_handle handle) { + sway_log(L_DEBUG, "Destroying window %" PRIuPTR, handle); + swayc_t *view = swayc_by_handle(handle); + + // destroy views by type + switch (wlc_view_get_type(handle)) { + // regular view created regularly + case 0: + case WLC_BIT_MODAL: + case WLC_BIT_POPUP: + break; + // DMENU has this flag, and takes view_focus, but other things with this + // flag don't + case WLC_BIT_OVERRIDE_REDIRECT: + break; + case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: + locked_container_focus = false; + break; + } + + if (view) { + bool fullscreen = swayc_is_fullscreen(view); + remove_view_from_scratchpad(view); + swayc_t *parent = destroy_view(view), *iter = NULL; + if (parent) { + ipc_event_window(parent, "close"); + + // Destroy empty workspaces + if (parent->type == C_WORKSPACE && + parent->children->length == 0 && + parent->floating->length == 0 && + swayc_active_workspace() != parent && + !parent->visible) { + parent = destroy_workspace(parent); + } + + if (fullscreen) { + iter = parent; + while (iter) { + if (iter->fullscreen) { + iter->fullscreen = NULL; + break; + } + iter = iter->parent; + } + } + + + arrange_windows(iter ? iter : parent, -1, -1); + } + } else { + // Is it unmanaged? + int i; + for (i = 0; i < root_container.children->length; ++i) { + swayc_t *output = root_container.children->items[i]; + int j; + for (j = 0; j < output->unmanaged->length; ++j) { + wlc_handle *_handle = output->unmanaged->items[j]; + if (*_handle == handle) { + list_del(output->unmanaged, j); + free(_handle); + break; + } + } + } + // Is it in the scratchpad? + for (i = 0; i < scratchpad->length; ++i) { + swayc_t *item = scratchpad->items[i]; + if (item->handle == handle) { + list_del(scratchpad, i); + destroy_view(item); + break; + } + } + } + set_focused_container(get_focused_view(&root_container)); +} + +static void handle_view_focus(wlc_handle view, bool focus) { + return; +} + +static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry *geometry) { + sway_log(L_DEBUG, "geometry request for %" PRIuPTR " %dx%d @ %d,%d", handle, + geometry->size.w, geometry->size.h, geometry->origin.x, geometry->origin.y); + // If the view is floating, then apply the geometry. + // Otherwise save the desired width/height for the view. + // This will not do anything for the time being as WLC improperly sends geometry requests + swayc_t *view = swayc_by_handle(handle); + if (view) { + view->desired_width = geometry->size.w; + view->desired_height = geometry->size.h; + + if (view->is_floating) { + floating_view_sane_size(view); + view->width = view->desired_width; + view->height = view->desired_height; + view->x = geometry->origin.x; + view->y = geometry->origin.y; + update_geometry(view); + } + } else { + wlc_view_set_geometry(handle, 0, geometry); + } +} + +static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) { + swayc_t *c = swayc_by_handle(view); + pid_t pid = wlc_view_get_pid(view); + switch (state) { + case WLC_BIT_FULLSCREEN: + if (!(get_feature_policy_mask(pid) & FEATURE_FULLSCREEN)) { + sway_log(L_INFO, "Denying fullscreen to %d (%s)", pid, c->name); + break; + } + // i3 just lets it become fullscreen + wlc_view_set_state(view, state, toggle); + if (c) { + sway_log(L_DEBUG, "setting view %" PRIuPTR " %s, fullscreen %d", view, c->name, toggle); + arrange_windows(c->parent, -1, -1); + // Set it as focused window for that workspace if its going fullscreen + swayc_t *ws = swayc_parent_by_type(c, C_WORKSPACE); + if (toggle) { + // Set ws focus to c + set_focused_container_for(ws, c); + ws->fullscreen = c; + } else { + ws->fullscreen = NULL; + } + } + break; + case WLC_BIT_MAXIMIZED: + case WLC_BIT_RESIZING: + case WLC_BIT_MOVING: + break; + case WLC_BIT_ACTIVATED: + sway_log(L_DEBUG, "View %p requested to be activated", c); + break; + } + return; +} + +static void handle_view_properties_updated(wlc_handle view, uint32_t mask) { + if (mask == WLC_BIT_PROPERTY_TITLE) { + swayc_t *c = swayc_by_handle(view); + if (!c) { + return; + } + + // update window title + const char *new_name = wlc_view_get_title(view); + + if (new_name) { + if (!c->name || strcmp(c->name, new_name) != 0) { + free(c->name); + c->name = strdup(new_name); + swayc_t *p = swayc_tabbed_stacked_ancestor(c); + if (p) { + // TODO: we only got the topmost tabbed/stacked container, update borders of all containers on the path + update_container_border(get_focused_view(p)); + } else if (c->border_type == B_NORMAL) { + update_container_border(c); + } + ipc_event_window(c, "title"); + } + } + } +} + +static void handle_binding_command(struct sway_binding *binding) { + struct sway_binding *binding_copy = binding; + bool reload = false; + // if this is a reload command we need to make a duplicate of the + // binding since it will be gone after the reload has completed. + if (strcasecmp(binding->command, "reload") == 0) { + binding_copy = sway_binding_dup(binding); + if (!binding_copy) { + sway_log(L_ERROR, "Unable to duplicate binding during reload"); + return; + } + reload = true; + } + + struct cmd_results *res = handle_command(binding->command, CONTEXT_BINDING); + if (res->status != CMD_SUCCESS) { + sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); + } + ipc_event_binding_keyboard(binding_copy); + + if (reload) { // free the binding if we made a copy + free_sway_binding(binding_copy); + } + + free_cmd_results(res); +} + +static bool handle_bindsym(struct sway_binding *binding, uint32_t keysym, uint32_t keycode) { + int i; + for (i = 0; i < binding->keys->length; ++i) { + if (binding->bindcode) { + xkb_keycode_t *key = binding->keys->items[i]; + if (keycode == *key) { + handle_binding_command(binding); + return true; + } + } else { + xkb_keysym_t *key = binding->keys->items[i]; + if (keysym == *key) { + handle_binding_command(binding); + return true; + } + } + } + + return false; +} + +static bool valid_bindsym(struct sway_binding *binding) { + bool match = false; + int i; + for (i = 0; i < binding->keys->length; ++i) { + if (binding->bindcode) { + xkb_keycode_t *key = binding->keys->items[i]; + if ((match = check_key(0, *key)) == false) { + break; + } + } else { + xkb_keysym_t *key = binding->keys->items[i]; + if ((match = check_key(*key, 0)) == false) { + break; + } + } + } + + return match; +} + +static bool handle_bindsym_release(struct sway_binding *binding) { + if (binding->keys->length == 1) { + xkb_keysym_t *key = binding->keys->items[0]; + if (check_released_key(*key)) { + handle_binding_command(binding); + return true; + } + } + + return false; +} + +static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, + uint32_t key, enum wlc_key_state state) { + + if (desktop_shell.is_locked) { + return EVENT_PASSTHROUGH; + } + + // reset pointer mode on keypress + if (state == WLC_KEY_STATE_PRESSED && pointer_state.mode) { + pointer_mode_reset(); + } + + struct sway_mode *mode = config->current_mode; + + struct wlc_modifiers no_mods = { 0, 0 }; + uint32_t sym = tolower(wlc_keyboard_get_keysym_for_key(key, &no_mods)); + + int i; + + if (state == WLC_KEY_STATE_PRESSED) { + press_key(sym, key); + } else { // WLC_KEY_STATE_RELEASED + release_key(sym, key); + } + + // handle bar modifiers pressed/released + uint32_t modifier; + for (i = 0; i < config->active_bar_modifiers->length; ++i) { + modifier = *(uint32_t *)config->active_bar_modifiers->items[i]; + + switch (modifier_state_changed(modifiers->mods, modifier)) { + case MOD_STATE_PRESSED: + ipc_event_modifier(modifier, "pressed"); + break; + case MOD_STATE_RELEASED: + ipc_event_modifier(modifier, "released"); + break; + } + } + // update modifiers state + modifiers_state_update(modifiers->mods); + + // handle bindings + list_t *candidates = create_list(); + for (i = 0; i < mode->bindings->length; ++i) { + struct sway_binding *binding = mode->bindings->items[i]; + if ((modifiers->mods ^ binding->modifiers) == 0) { + switch (state) { + case WLC_KEY_STATE_PRESSED: + if (!binding->release && valid_bindsym(binding)) { + list_add(candidates, binding); + } + break; + case WLC_KEY_STATE_RELEASED: + if (binding->release && handle_bindsym_release(binding)) { + list_free(candidates); + return EVENT_HANDLED; + } + break; + } + } + } + + for (i = 0; i < candidates->length; ++i) { + struct sway_binding *binding = candidates->items[i]; + if (state == WLC_KEY_STATE_PRESSED) { + if (!binding->release && handle_bindsym(binding, sym, key)) { + list_free(candidates); + return EVENT_HANDLED; + } + } + } + + list_free(candidates); + + swayc_t *focused = get_focused_container(&root_container); + if (focused->type == C_VIEW) { + pid_t pid = wlc_view_get_pid(focused->handle); + if (!(get_feature_policy_mask(pid) & FEATURE_KEYBOARD)) { + return EVENT_HANDLED; + } + } + return EVENT_PASSTHROUGH; +} + +static bool handle_pointer_motion(wlc_handle handle, uint32_t time, double x, double y) { + if (desktop_shell.is_locked) { + return EVENT_PASSTHROUGH; + } + + double new_x = x; + double new_y = y; + // Switch to adjacent output if touching output edge. + // + // Since this doesn't currently support moving windows between outputs we + // don't do the switch if the pointer is in a mode. + if (config->seamless_mouse && !pointer_state.mode && + !pointer_state.left.held && !pointer_state.right.held && !pointer_state.scroll.held) { + + swayc_t *output = swayc_active_output(), *adjacent = NULL; + struct wlc_point abs_pos = { .x = x + output->x, .y = y + output->y }; + if (x <= 0) { // Left edge + if ((adjacent = swayc_adjacent_output(output, MOVE_LEFT, &abs_pos, false))) { + if (workspace_switch(swayc_active_workspace_for(adjacent))) { + new_x = adjacent->width; + // adjust for differently aligned outputs (well, this is + // only correct when the two outputs have the same + // resolution or the same dpi I guess, it should take + // physical attributes into account) + new_y += (output->y - adjacent->y); + } + } + } else if (x >= output->width) { // Right edge + if ((adjacent = swayc_adjacent_output(output, MOVE_RIGHT, &abs_pos, false))) { + if (workspace_switch(swayc_active_workspace_for(adjacent))) { + new_x = 0; + new_y += (output->y - adjacent->y); + } + } + } else if (y <= 0) { // Top edge + if ((adjacent = swayc_adjacent_output(output, MOVE_UP, &abs_pos, false))) { + if (workspace_switch(swayc_active_workspace_for(adjacent))) { + new_y = adjacent->height; + new_x += (output->x - adjacent->x); + } + } + } else if (y >= output->height) { // Bottom edge + if ((adjacent = swayc_adjacent_output(output, MOVE_DOWN, &abs_pos, false))) { + if (workspace_switch(swayc_active_workspace_for(adjacent))) { + new_y = 0; + new_x += (output->x - adjacent->x); + } + } + } + } + + pointer_position_set(new_x, new_y, false); + + swayc_t *focused = get_focused_container(&root_container); + if (focused->type == C_VIEW) { + pid_t pid = wlc_view_get_pid(focused->handle); + if (!(get_feature_policy_mask(pid) & FEATURE_MOUSE)) { + return EVENT_HANDLED; + } + } + + return EVENT_PASSTHROUGH; +} + +static bool swayc_border_check(swayc_t *c, const void *_origin) { + const struct wlc_point *origin = _origin; + const struct wlc_geometry title_bar = c->title_bar_geometry; + + if (c->border_type != B_NORMAL) { + return false; + } + + if (origin->x >= title_bar.origin.x && origin->y >= title_bar.origin.y + && origin->x < title_bar.origin.x + (int32_t)title_bar.size.w + && origin->y < title_bar.origin.y + (int32_t)title_bar.size.h) { + return true; + } + return false; +} + +static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, + uint32_t button, enum wlc_button_state state, const struct wlc_point *origin) { + + // Update view pointer is on + pointer_state.view = container_under_pointer(); + + struct sway_mode *mode = config->current_mode; + // handle bindings + for (int i = 0; i < mode->bindings->length; ++i) { + struct sway_binding *binding = mode->bindings->items[i]; + if ((modifiers->mods ^ binding->modifiers) == 0) { + switch (state) { + case WLC_BUTTON_STATE_PRESSED: + if (!binding->release && handle_bindsym(binding, button, 0)) { + return EVENT_HANDLED; + } + break; + case WLC_BUTTON_STATE_RELEASED: + if (binding->release && handle_bindsym(binding, button, 0)) { + return EVENT_HANDLED; + } + break; + } + } + } + + // Update pointer_state + switch (button) { + case M_LEFT_CLICK: + if (state == WLC_BUTTON_STATE_PRESSED) { + pointer_state.left.held = true; + pointer_state.left.x = origin->x; + pointer_state.left.y = origin->y; + pointer_state.left.view = pointer_state.view; + } else { + pointer_state.left.held = false; + } + break; + + case M_RIGHT_CLICK: + if (state == WLC_BUTTON_STATE_PRESSED) { + pointer_state.right.held = true; + pointer_state.right.x = origin->x; + pointer_state.right.y = origin->y; + pointer_state.right.view = pointer_state.view; + } else { + pointer_state.right.held = false; + } + break; + + case M_SCROLL_CLICK: + if (state == WLC_BUTTON_STATE_PRESSED) { + pointer_state.scroll.held = true; + pointer_state.scroll.x = origin->x; + pointer_state.scroll.y = origin->y; + pointer_state.scroll.view = pointer_state.view; + } else { + pointer_state.scroll.held = false; + } + break; + + //TODO scrolling behavior + case M_SCROLL_UP: + case M_SCROLL_DOWN: + break; + } + + // get focused window and check if to change focus on mouse click + swayc_t *focused = get_focused_container(&root_container); + + // don't change focus or mode if fullscreen + if (swayc_is_fullscreen(focused)) { + if (focused->type == C_VIEW) { + pid_t pid = wlc_view_get_pid(focused->handle); + if (!(get_feature_policy_mask(pid) & FEATURE_MOUSE)) { + return EVENT_HANDLED; + } + } + return EVENT_PASSTHROUGH; + } + + // set pointer mode only if floating mod has been set + if (config->floating_mod) { + pointer_mode_set(button, !(modifiers->mods ^ config->floating_mod)); + } + + // Check whether to change focus + swayc_t *pointer = pointer_state.view; + if (pointer) { + swayc_t *ws = swayc_parent_by_type(focused, C_WORKSPACE); + if (ws != NULL) { + swayc_t *find = container_find(ws, &swayc_border_check, origin); + if (find != NULL) { + set_focused_container(find); + return EVENT_HANDLED; + } + } + + if (focused != pointer) { + set_focused_container(pointer_state.view); + } + // Send to front if floating + if (pointer->is_floating) { + int i; + for (i = 0; i < pointer->parent->floating->length; i++) { + if (pointer->parent->floating->items[i] == pointer) { + list_del(pointer->parent->floating, i); + list_add(pointer->parent->floating, pointer); + break; + } + } + wlc_view_bring_to_front(pointer->handle); + } + } + + // Return if mode has been set + if (pointer_state.mode) { + return EVENT_HANDLED; + } + + if (focused->type == C_VIEW) { + pid_t pid = wlc_view_get_pid(focused->handle); + if (!(get_feature_policy_mask(pid) & FEATURE_MOUSE)) { + return EVENT_HANDLED; + } + } + + // Always send mouse release + if (state == WLC_BUTTON_STATE_RELEASED) { + return EVENT_PASSTHROUGH; + } + + // Finally send click + return EVENT_PASSTHROUGH; +} + +bool handle_pointer_scroll(wlc_handle view, uint32_t time, const struct wlc_modifiers* modifiers, + uint8_t axis_bits, double _amount[2]) { + if (!(modifiers->mods ^ config->floating_mod)) { + int x_amount = (int)_amount[0]; + int y_amount = (int)_amount[1]; + + if (x_amount > 0 && strcmp(config->floating_scroll_up_cmd, "")) { + handle_command(config->floating_scroll_up_cmd, CONTEXT_BINDING); + return EVENT_HANDLED; + } else if (x_amount < 0 && strcmp(config->floating_scroll_down_cmd, "")) { + handle_command(config->floating_scroll_down_cmd, CONTEXT_BINDING); + return EVENT_HANDLED; + } + + if (y_amount > 0 && strcmp(config->floating_scroll_right_cmd, "")) { + handle_command(config->floating_scroll_right_cmd, CONTEXT_BINDING); + return EVENT_HANDLED; + } else if (y_amount < 0 && strcmp(config->floating_scroll_left_cmd, "")) { + handle_command(config->floating_scroll_left_cmd, CONTEXT_BINDING); + return EVENT_HANDLED; + } + } + return EVENT_PASSTHROUGH; +} + +static void handle_wlc_ready(void) { + sway_log(L_DEBUG, "Compositor is ready, executing cmds in queue"); + // Execute commands until there are none left + config->active = true; + while (config->cmd_queue->length) { + char *line = config->cmd_queue->items[0]; + struct cmd_results *res = handle_command(line, CONTEXT_CONFIG); + if (res->status != CMD_SUCCESS) { + sway_log(L_ERROR, "Error on line '%s': %s", line, res->error); + } + free_cmd_results(res); + free(line); + list_del(config->cmd_queue, 0); + } +} + +void register_wlc_handlers() { + wlc_set_output_created_cb(handle_output_created); + wlc_set_output_destroyed_cb(handle_output_destroyed); + wlc_set_output_resolution_cb(handle_output_resolution_change); + wlc_set_output_focus_cb(handle_output_focused); + wlc_set_output_render_post_cb(handle_output_post_render); + wlc_set_view_created_cb(handle_view_created); + wlc_set_view_destroyed_cb(handle_view_destroyed); + wlc_set_view_focus_cb(handle_view_focus); + wlc_set_view_render_pre_cb(handle_view_pre_render); + wlc_set_view_request_geometry_cb(handle_view_geometry_request); + wlc_set_view_request_state_cb(handle_view_state_request); + wlc_set_view_properties_updated_cb(handle_view_properties_updated); + wlc_set_keyboard_key_cb(handle_key); + wlc_set_pointer_motion_cb_v2(handle_pointer_motion); + wlc_set_pointer_button_cb(handle_pointer_button); + wlc_set_pointer_scroll_cb(handle_pointer_scroll); + wlc_set_compositor_ready_cb(handle_wlc_ready); + wlc_set_input_created_cb(handle_input_created); + wlc_set_input_destroyed_cb(handle_input_destroyed); +} diff --git a/sway/old/input_state.c b/sway/old/input_state.c new file mode 100644 index 00000000..04aafd37 --- /dev/null +++ b/sway/old/input_state.c @@ -0,0 +1,490 @@ +#include +#include +#include +#include "sway/input_state.h" +#include "sway/config.h" +#include "log.h" + +#define KEY_STATE_MAX_LENGTH 64 + +struct key_state { + /* + * Aims to store state regardless of modifiers. + * If you press a key, then hold shift, then release the key, we'll + * get two different key syms, but the same key code. This handles + * that scenario and makes sure we can use the right bindings. + */ + uint32_t key_sym; + uint32_t alt_sym; + uint32_t key_code; +}; + +static struct key_state key_state_array[KEY_STATE_MAX_LENGTH]; + +static struct key_state last_released; + +static uint32_t modifiers_state; + +void input_init(void) { + int i; + for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { + struct key_state none = { 0, 0, 0 }; + key_state_array[i] = none; + } + + struct key_state none = { 0, 0, 0 }; + last_released = none; + + modifiers_state = 0; +} + +uint32_t modifier_state_changed(uint32_t new_state, uint32_t mod) { + if ((new_state & mod) != 0) { // pressed + if ((modifiers_state & mod) != 0) { // already pressed + return MOD_STATE_UNCHANGED; + } else { // pressed + return MOD_STATE_PRESSED; + } + } else { // not pressed + if ((modifiers_state & mod) != 0) { // released + return MOD_STATE_RELEASED; + } else { // already released + return MOD_STATE_UNCHANGED; + } + } +} + +void modifiers_state_update(uint32_t new_state) { + modifiers_state = new_state; +} + +static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) { + int i; + for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { + if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) { + break; + } + if (key_sym != 0 && (key_state_array[i].key_sym == key_sym + || key_state_array[i].alt_sym == key_sym)) { + break; + } + if (update && key_state_array[i].key_code == key_code) { + key_state_array[i].alt_sym = key_sym; + break; + } + if (key_sym == 0 && key_code != 0 && key_state_array[i].key_code == key_code) { + break; + } + } + return i; +} + +bool check_key(uint32_t key_sym, uint32_t key_code) { + return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH; +} + +bool check_released_key(uint32_t key_sym) { + return (key_sym != 0 + && (last_released.key_sym == key_sym + || last_released.alt_sym == key_sym)); +} + +void press_key(uint32_t key_sym, uint32_t key_code) { + if (key_code == 0) { + return; + } + // Check if key exists + if (!check_key(key_sym, key_code)) { + // Check that we don't exceed buffer length + int insert = find_key(0, 0, true); + if (insert < KEY_STATE_MAX_LENGTH) { + key_state_array[insert].key_sym = key_sym; + key_state_array[insert].key_code = key_code; + } + } +} + +void release_key(uint32_t key_sym, uint32_t key_code) { + uint8_t index = find_key(key_sym, key_code, true); + if (index < KEY_STATE_MAX_LENGTH) { + last_released.key_sym = key_state_array[index].key_sym; + last_released.alt_sym = key_state_array[index].alt_sym; + last_released.key_code = key_state_array[index].key_code; + struct key_state none = { 0, 0, 0 }; + key_state_array[index] = none; + } +} + +// Pointer state and mode + +struct pointer_state pointer_state; + +static struct mode_state { + // initial view state + double x, y, w, h; + swayc_t *ptr; + // Containers used for resizing horizontally + struct { + double w; + swayc_t *ptr; + struct { + double w; + swayc_t *ptr; + } parent; + } horiz; + // Containers used for resizing vertically + struct { + double h; + swayc_t *ptr; + struct { + double h; + swayc_t *ptr; + } parent; + } vert; +} initial; + +static struct { + bool left; + bool top; +} lock; + +// initial set/unset + +static void set_initial_view(swayc_t *view) { + initial.ptr = view; + initial.x = view->x; + initial.y = view->y; + initial.w = view->width; + initial.h = view->height; +} + +static void set_initial_sibling(void) { + bool reset = true; + swayc_t *ws = swayc_active_workspace_for(initial.ptr); + if ((initial.horiz.ptr = get_swayc_in_direction_under(initial.ptr, + lock.left ? MOVE_RIGHT: MOVE_LEFT, ws))) { + initial.horiz.w = initial.horiz.ptr->width; + initial.horiz.parent.ptr = get_swayc_in_direction_under(initial.horiz.ptr, + lock.left ? MOVE_LEFT : MOVE_RIGHT, ws); + initial.horiz.parent.w = initial.horiz.parent.ptr->width; + reset = false; + } + if ((initial.vert.ptr = get_swayc_in_direction_under(initial.ptr, + lock.top ? MOVE_DOWN: MOVE_UP, ws))) { + initial.vert.h = initial.vert.ptr->height; + initial.vert.parent.ptr = get_swayc_in_direction_under(initial.vert.ptr, + lock.top ? MOVE_UP : MOVE_DOWN, ws); + initial.vert.parent.h = initial.vert.parent.ptr->height; + reset = false; + } + // If nothing will change just undo the mode + if (reset) { + pointer_state.mode = 0; + } +} + +static void reset_initial_view(void) { + initial.ptr->x = initial.x; + initial.ptr->y = initial.y; + initial.ptr->width = initial.w; + initial.ptr->height = initial.h; + arrange_windows(initial.ptr, -1, -1); + pointer_state.mode = 0; +} + +static void reset_initial_sibling(void) { + initial.horiz.ptr->width = initial.horiz.w; + initial.horiz.parent.ptr->width = initial.horiz.parent.w; + initial.vert.ptr->height = initial.vert.h; + initial.vert.parent.ptr->height = initial.vert.parent.h; + arrange_windows(initial.horiz.ptr->parent, -1, -1); + arrange_windows(initial.vert.ptr->parent, -1, -1); + pointer_state.mode = 0; +} + +void pointer_position_set(double new_x, double new_y, bool force_focus) { + double x, y; + wlc_pointer_get_position_v2(&x, &y); + pointer_state.delta.x = new_x - x; + pointer_state.delta.y = new_y - y; + + wlc_pointer_set_position_v2(new_x, new_y); + + // Update view under pointer + swayc_t *prev_view = pointer_state.view; + pointer_state.view = container_under_pointer(); + + // If pointer is in a mode, update it + if (pointer_state.mode) { + pointer_mode_update(); + // Otherwise change focus if config is set + } else if (force_focus || (prev_view != pointer_state.view && config->focus_follows_mouse)) { + if (pointer_state.view && pointer_state.view->type == C_VIEW) { + set_focused_container(pointer_state.view); + } + } +} + +void center_pointer_on(swayc_t *view) { + pointer_position_set(view->x + view->width/2, view->y + view->height/2, true); +} + +// Mode set left/right click + +static void pointer_mode_set_dragging(void) { + switch (config->dragging_key) { + case M_LEFT_CLICK: + set_initial_view(pointer_state.left.view); + break; + case M_RIGHT_CLICK: + set_initial_view(pointer_state.right.view); + break; + } + + if (initial.ptr->is_floating) { + pointer_state.mode = M_DRAGGING | M_FLOATING; + } else { + pointer_state.mode = M_DRAGGING | M_TILING; + // unset mode if we can't drag tile + if (initial.ptr->parent->type == C_WORKSPACE && + initial.ptr->parent->children->length == 1) { + pointer_state.mode = 0; + } + } +} + +static void pointer_mode_set_resizing(void) { + switch (config->resizing_key) { + case M_LEFT_CLICK: + set_initial_view(pointer_state.left.view); + break; + case M_RIGHT_CLICK: + set_initial_view(pointer_state.right.view); + break; + } + // Setup locking information + int midway_x = initial.ptr->x + initial.ptr->width/2; + int midway_y = initial.ptr->y + initial.ptr->height/2; + + double x, y; + wlc_pointer_get_position_v2(&x, &y); + lock.left = x > midway_x; + lock.top = y > midway_y; + + if (initial.ptr->is_floating) { + pointer_state.mode = M_RESIZING | M_FLOATING; + } else { + pointer_state.mode = M_RESIZING | M_TILING; + set_initial_sibling(); + } +} + +// Mode set/update/reset + +void pointer_mode_set(uint32_t button, bool condition) { + // switch on drag/resize mode + switch (pointer_state.mode & (M_DRAGGING | M_RESIZING)) { + case M_DRAGGING: + // end drag mode when 'dragging' click is unpressed + if (config->dragging_key == M_LEFT_CLICK && !pointer_state.left.held) { + pointer_state.mode = 0; + } else if (config->dragging_key == M_RIGHT_CLICK && !pointer_state.right.held) { + pointer_state.mode = 0; + } + break; + + case M_RESIZING: + // end resize mode when 'resizing' click is unpressed + if (config->resizing_key == M_LEFT_CLICK && !pointer_state.left.held) { + pointer_state.mode = 0; + } else if (config->resizing_key == M_RIGHT_CLICK && !pointer_state.right.held) { + pointer_state.mode = 0; + } + break; + + // No mode case + default: + // return if failed condition, or no view + if (!condition || !pointer_state.view) { + break; + } + + // Set mode depending on current button press + switch (button) { + // Start left-click mode + case M_LEFT_CLICK: + // if button release don't do anything + if (pointer_state.left.held) { + if (config->dragging_key == M_LEFT_CLICK) { + pointer_mode_set_dragging(); + } else if (config->resizing_key == M_LEFT_CLICK) { + pointer_mode_set_resizing(); + } + } + break; + + // Start right-click mode + case M_RIGHT_CLICK: + // if button release don't do anyhting + if (pointer_state.right.held) { + if (config->dragging_key == M_RIGHT_CLICK) { + pointer_mode_set_dragging(); + } else if (config->resizing_key == M_RIGHT_CLICK) { + pointer_mode_set_resizing(); + } + } + break; + } + } +} + +void pointer_mode_update(void) { + if (initial.ptr->type != C_VIEW) { + pointer_state.mode = 0; + return; + } + double x, y; + wlc_pointer_get_position_v2(&x, &y); + int dx = x; + int dy = y; + + switch (pointer_state.mode) { + case M_FLOATING | M_DRAGGING: + // Update position + switch (config->dragging_key) { + case M_LEFT_CLICK: + dx -= pointer_state.left.x; + dy -= pointer_state.left.y; + break; + case M_RIGHT_CLICK: + dx -= pointer_state.right.x; + dy -= pointer_state.right.y; + break; + } + + if (initial.x + dx != initial.ptr->x) { + initial.ptr->x = initial.x + dx; + } + if (initial.y + dy != initial.ptr->y) { + initial.ptr->y = initial.y + dy; + } + update_geometry(initial.ptr); + break; + + case M_FLOATING | M_RESIZING: + switch (config->resizing_key) { + case M_LEFT_CLICK: + dx -= pointer_state.left.x; + dy -= pointer_state.left.y; + initial.ptr = pointer_state.left.view; + break; + case M_RIGHT_CLICK: + dx -= pointer_state.right.x; + dy -= pointer_state.right.y; + initial.ptr = pointer_state.right.view; + break; + } + + if (lock.left) { + if (initial.w + dx > min_sane_w) { + initial.ptr->width = initial.w + dx; + } + } else { // lock.right + if (initial.w - dx > min_sane_w) { + initial.ptr->width = initial.w - dx; + initial.ptr->x = initial.x + dx; + } + } + if (lock.top) { + if (initial.h + dy > min_sane_h) { + initial.ptr->height = initial.h + dy; + } + } else { // lock.bottom + if (initial.h - dy > min_sane_h) { + initial.ptr->height = initial.h - dy; + initial.ptr->y = initial.y + dy; + } + } + update_geometry(initial.ptr); + break; + + case M_TILING | M_DRAGGING: + // swap current view under pointer with dragged view + if (pointer_state.view && pointer_state.view->type == C_VIEW + && pointer_state.view != initial.ptr + && !pointer_state.view->is_floating) { + // Swap them around + swap_container(pointer_state.view, initial.ptr); + swap_geometry(pointer_state.view, initial.ptr); + update_geometry(pointer_state.view); + update_geometry(initial.ptr); + // Set focus back to initial view + set_focused_container(initial.ptr); + // Arrange the windows + arrange_windows(&root_container, -1, -1); + } + break; + + case M_TILING | M_RESIZING: + switch (config->resizing_key) { + case M_LEFT_CLICK: + dx -= pointer_state.left.x; + dy -= pointer_state.left.y; + break; + case M_RIGHT_CLICK: + dx -= pointer_state.right.x; + dy -= pointer_state.right.y; + break; + } + + // resize if we can + if (initial.horiz.ptr) { + if (lock.left) { + // Check whether its fine to resize + if (initial.w + dx > min_sane_w && initial.horiz.w - dx > min_sane_w) { + initial.horiz.ptr->width = initial.horiz.w - dx; + initial.horiz.parent.ptr->width = initial.horiz.parent.w + dx; + } + } else { // lock.right + if (initial.w - dx > min_sane_w && initial.horiz.w + dx > min_sane_w) { + initial.horiz.ptr->width = initial.horiz.w + dx; + initial.horiz.parent.ptr->width = initial.horiz.parent.w - dx; + } + } + arrange_windows(initial.horiz.ptr->parent, -1, -1); + } + if (initial.vert.ptr) { + if (lock.top) { + if (initial.h + dy > min_sane_h && initial.vert.h - dy > min_sane_h) { + initial.vert.ptr->height = initial.vert.h - dy; + initial.vert.parent.ptr->height = initial.vert.parent.h + dy; + } + } else { // lock.bottom + if (initial.h - dy > min_sane_h && initial.vert.h + dy > min_sane_h) { + initial.vert.ptr->height = initial.vert.h + dy; + initial.vert.parent.ptr->height = initial.vert.parent.h - dy; + } + } + arrange_windows(initial.vert.ptr->parent, -1, -1); + } + default: + return; + } +} + +void pointer_mode_reset(void) { + switch (pointer_state.mode) { + case M_FLOATING | M_RESIZING: + case M_FLOATING | M_DRAGGING: + reset_initial_view(); + break; + + case M_TILING | M_RESIZING: + (void) reset_initial_sibling; + break; + + case M_TILING | M_DRAGGING: + default: + break; + } +} diff --git a/sway/old/ipc-json.c b/sway/old/ipc-json.c new file mode 100644 index 00000000..1579a2d9 --- /dev/null +++ b/sway/old/ipc-json.c @@ -0,0 +1,521 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include "sway/output.h" +#include "sway/container.h" +#include "sway/input.h" +#include "sway/ipc-json.h" +#include "util.h" + +static json_object *ipc_json_create_rect(swayc_t *c) { + json_object *rect = json_object_new_object(); + + json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x)); + json_object_object_add(rect, "y", json_object_new_int((int32_t)c->y)); + + struct wlr_box box; + if (c->type == C_OUTPUT) { + wlr_output_effective_resolution(c->_handle.output->wlr_output, + &box.width, &box.height); + } else { + box.width = c->width; + box.width = c->height; + } + + json_object_object_add(rect, "width", + json_object_new_int((int32_t)box.width)); + json_object_object_add(rect, "height", + json_object_new_int((int32_t)box.height)); + + return rect; +} + +static json_object *ipc_json_create_rect_from_geometry(struct wlc_geometry g) { + json_object *rect = json_object_new_object(); + + json_object_object_add(rect, "x", json_object_new_int(g.origin.x)); + json_object_object_add(rect, "y", json_object_new_int(g.origin.y)); + json_object_object_add(rect, "width", json_object_new_int(g.size.w)); + json_object_object_add(rect, "height", json_object_new_int(g.size.h)); + + return rect; +} + +static const char *ipc_json_border_description(swayc_t *c) { + const char *border; + + switch (c->border_type) { + case B_PIXEL: + border = "1pixel"; + break; + + case B_NORMAL: + border = "normal"; + break; + + case B_NONE: // fallthrough + default: + border = "none"; + break; + } + + return border; +} + +static const char *ipc_json_layout_description(enum swayc_layouts l) { + const char *layout; + + switch (l) { + case L_VERT: + layout = "splitv"; + break; + + case L_HORIZ: + layout = "splith"; + break; + + case L_TABBED: + layout = "tabbed"; + break; + + case L_STACKED: + layout = "stacked"; + break; + + case L_FLOATING: + layout = "floating"; + break; + + case L_NONE: // fallthrough + case L_LAYOUTS: // fallthrough; this should never happen, I'm just trying to silence compiler warnings + default: + layout = "null"; + break; + } + + return layout; +} + +static float ipc_json_child_percentage(swayc_t *c) { + float percent = 0; + swayc_t *parent = c->parent; + + if (parent) { + switch (parent->layout) { + case L_VERT: + percent = c->height / parent->height; + break; + + case L_HORIZ: + percent = c->width / parent->width; + break; + + case L_STACKED: // fallthrough + case L_TABBED: // fallthrough + percent = 1.0; + break; + + default: + break; + } + } + + return percent; +} + +static void ipc_json_describe_output(swayc_t *output, json_object *object) { + uint32_t scale = wlc_output_get_scale(output->handle); + json_object_object_add(object, "active", json_object_new_boolean(true)); + json_object_object_add(object, "primary", json_object_new_boolean(false)); + json_object_object_add(object, "layout", json_object_new_string("output")); + json_object_object_add(object, "type", json_object_new_string("output")); + json_object_object_add(object, "current_workspace", + (output->focused) ? json_object_new_string(output->focused->name) : NULL); + json_object_object_add(object, "scale", json_object_new_int(scale)); +} + +static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) { + int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1; + const char *layout = ipc_json_layout_description(workspace->workspace_layout); + + json_object_object_add(object, "num", json_object_new_int(num)); + json_object_object_add(object, "output", (workspace->parent) ? json_object_new_string(workspace->parent->name) : NULL); + json_object_object_add(object, "type", json_object_new_string("workspace")); + json_object_object_add(object, "layout", (strcmp(layout, "null") == 0) ? NULL : json_object_new_string(layout)); +} + +// window is in the scratchpad ? changed : none +static const char *ipc_json_get_scratchpad_state(swayc_t *c) { + int i; + for (i = 0; i < scratchpad->length; i++) { + if (scratchpad->items[i] == c) { + return "changed"; + } + } + return "none"; // we ignore the fresh value +} + +static void ipc_json_describe_view(swayc_t *c, json_object *object) { + json_object *props = json_object_new_object(); + json_object_object_add(object, "type", json_object_new_string((c->is_floating) ? "floating_con" : "con")); + + wlc_handle parent = wlc_view_get_parent(c->handle); + json_object_object_add(object, "scratchpad_state", + json_object_new_string(ipc_json_get_scratchpad_state(c))); + + json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); + + json_object_object_add(props, "class", c->class ? json_object_new_string(c->class) : + c->app_id ? json_object_new_string(c->app_id) : NULL); + json_object_object_add(props, "instance", c->instance ? json_object_new_string(c->instance) : + c->app_id ? json_object_new_string(c->app_id) : NULL); + json_object_object_add(props, "title", (c->name) ? json_object_new_string(c->name) : NULL); + json_object_object_add(props, "transient_for", parent ? json_object_new_int(parent) : NULL); + json_object_object_add(object, "window_properties", props); + + json_object_object_add(object, "fullscreen_mode", + json_object_new_int(swayc_is_fullscreen(c) ? 1 : 0)); + json_object_object_add(object, "sticky", json_object_new_boolean(c->sticky)); + json_object_object_add(object, "floating", json_object_new_string( + c->is_floating ? "auto_on" : "auto_off")); // we can't state the cause + + json_object_object_add(object, "app_id", c->app_id ? json_object_new_string(c->app_id) : NULL); + + if (c->parent) { + const char *layout = (c->parent->type == C_CONTAINER) ? + ipc_json_layout_description(c->parent->layout) : "none"; + const char *last_layout = (c->parent->type == C_CONTAINER) ? + ipc_json_layout_description(c->parent->prev_layout) : "none"; + json_object_object_add(object, "layout", + (strcmp(layout, "null") == 0) ? NULL : json_object_new_string(layout)); + json_object_object_add(object, "last_split_layout", + (strcmp(last_layout, "null") == 0) ? NULL : json_object_new_string(last_layout)); + json_object_object_add(object, "workspace_layout", + json_object_new_string(ipc_json_layout_description(swayc_parent_by_type(c, C_WORKSPACE)->workspace_layout))); + } +} + +static void ipc_json_describe_root(swayc_t *c, json_object *object) { + json_object_object_add(object, "type", json_object_new_string("root")); + json_object_object_add(object, "layout", json_object_new_string("splith")); +} + +json_object *ipc_json_describe_container(swayc_t *c) { + float percent = ipc_json_child_percentage(c); + + if (!(sway_assert(c, "Container must not be null."))) { + return NULL; + } + + json_object *object = json_object_new_object(); + + json_object_object_add(object, "id", json_object_new_int((int)c->id)); + json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); + json_object_object_add(object, "rect", ipc_json_create_rect(c)); + json_object_object_add(object, "visible", json_object_new_boolean(c->visible)); + json_object_object_add(object, "focused", json_object_new_boolean(c == current_focus)); + + json_object_object_add(object, "border", json_object_new_string(ipc_json_border_description(c))); + json_object_object_add(object, "window_rect", ipc_json_create_rect_from_geometry(c->actual_geometry)); + json_object_object_add(object, "deco_rect", ipc_json_create_rect_from_geometry(c->title_bar_geometry)); + json_object_object_add(object, "geometry", ipc_json_create_rect_from_geometry(c->cached_geometry)); + json_object_object_add(object, "percent", (percent > 0) ? json_object_new_double(percent) : NULL); + json_object_object_add(object, "window", json_object_new_int(c->handle)); // for the sake of i3 compat + // TODO: make urgency actually work once Sway supports it + json_object_object_add(object, "urgent", json_object_new_boolean(false)); + json_object_object_add(object, "current_border_width", json_object_new_int(c->border_thickness)); + + switch (c->type) { + case C_ROOT: + ipc_json_describe_root(c, object); + break; + + case C_OUTPUT: + ipc_json_describe_output(c, object); + break; + + case C_CONTAINER: // fallthrough + case C_VIEW: + ipc_json_describe_view(c, object); + break; + + case C_WORKSPACE: + ipc_json_describe_workspace(c, object); + break; + + case C_TYPES: // fallthrough; this should never happen, I'm just trying to silence compiler warnings + default: + break; + } + + return object; +} + +json_object *ipc_json_describe_input(struct libinput_device *device) { + char* identifier = libinput_dev_unique_id(device); + int vendor = libinput_device_get_id_vendor(device); + int product = libinput_device_get_id_product(device); + const char *name = libinput_device_get_name(device); + double width = -1, height = -1; + int has_size = libinput_device_get_size(device, &width, &height); + + json_object *device_object = json_object_new_object(); + json_object_object_add(device_object,"identifier", + identifier ? json_object_new_string(identifier) : NULL); + json_object_object_add(device_object, + "vendor", json_object_new_int(vendor)); + json_object_object_add(device_object, + "product", json_object_new_int(product)); + json_object_object_add(device_object, + "name", json_object_new_string(name)); + if (has_size == 0) { + json_object *size_object = json_object_new_object(); + json_object_object_add(size_object, + "width", json_object_new_double(width)); + json_object_object_add(size_object, + "height", json_object_new_double(height)); + } else { + json_object_object_add(device_object, "size", NULL); + } + + struct { + enum libinput_device_capability cap; + const char *name; + // If anyone feels like implementing device-specific IPC output, + // be my guest + json_object *(*describe)(struct libinput_device *); + } caps[] = { + { LIBINPUT_DEVICE_CAP_KEYBOARD, "keyboard", NULL }, + { LIBINPUT_DEVICE_CAP_POINTER, "pointer", NULL }, + { LIBINPUT_DEVICE_CAP_TOUCH, "touch", NULL }, + { LIBINPUT_DEVICE_CAP_TABLET_TOOL, "tablet_tool", NULL }, + { LIBINPUT_DEVICE_CAP_TABLET_PAD, "tablet_pad", NULL }, + { LIBINPUT_DEVICE_CAP_GESTURE, "gesture", NULL }, +#ifdef LIBINPUT_DEVICE_CAP_SWITCH // libinput 1.7.0+ + { LIBINPUT_DEVICE_CAP_SWITCH, "switch", NULL }, +#endif + }; + + json_object *_caps = json_object_new_array(); + for (size_t i = 0; i < sizeof(caps) / sizeof(caps[0]); ++i) { + if (libinput_device_has_capability(device, caps[i].cap)) { + json_object_array_add(_caps, json_object_new_string(caps[i].name)); + if (caps[i].describe) { + json_object *desc = caps[i].describe(device); + json_object_object_add(device_object, caps[i].name, desc); + } + } + } + json_object_object_add(device_object, "capabilities", _caps); + + free(identifier); + return device_object; +} + +json_object *ipc_json_get_version() { + int major = 0, minor = 0, patch = 0; + json_object *version = json_object_new_object(); + + sscanf(SWAY_VERSION, "%u.%u.%u", &major, &minor, &patch); + + json_object_object_add(version, "human_readable", json_object_new_string(SWAY_VERSION)); + json_object_object_add(version, "variant", json_object_new_string("sway")); + json_object_object_add(version, "major", json_object_new_int(major)); + json_object_object_add(version, "minor", json_object_new_int(minor)); + json_object_object_add(version, "patch", json_object_new_int(patch)); + + return version; +} + +json_object *ipc_json_describe_bar_config(struct bar_config *bar) { + if (!sway_assert(bar, "Bar must not be NULL")) { + return NULL; + } + + json_object *json = json_object_new_object(); + json_object_object_add(json, "id", json_object_new_string(bar->id)); +#ifdef ENABLE_TRAY + if (bar->tray_output) { + json_object_object_add(json, "tray_output", json_object_new_string(bar->tray_output)); + } else { + json_object_object_add(json, "tray_output", NULL); + } + if (bar->icon_theme) { + json_object_object_add(json, "icon_theme", json_object_new_string(bar->icon_theme)); + } else { + json_object_object_add(json, "icon_theme", NULL); + } + json_object_object_add(json, "tray_padding", json_object_new_int(bar->tray_padding)); + json_object_object_add(json, "activate_button", json_object_new_int(bar->activate_button)); + json_object_object_add(json, "context_button", json_object_new_int(bar->context_button)); + json_object_object_add(json, "secondary_button", json_object_new_int(bar->secondary_button)); +#endif + json_object_object_add(json, "mode", json_object_new_string(bar->mode)); + json_object_object_add(json, "hidden_state", json_object_new_string(bar->hidden_state)); + json_object_object_add(json, "modifier", json_object_new_string(get_modifier_name_by_mask(bar->modifier))); + switch (bar->position) { + case DESKTOP_SHELL_PANEL_POSITION_TOP: + json_object_object_add(json, "position", json_object_new_string("top")); + break; + case DESKTOP_SHELL_PANEL_POSITION_BOTTOM: + json_object_object_add(json, "position", json_object_new_string("bottom")); + break; + case DESKTOP_SHELL_PANEL_POSITION_LEFT: + json_object_object_add(json, "position", json_object_new_string("left")); + break; + case DESKTOP_SHELL_PANEL_POSITION_RIGHT: + json_object_object_add(json, "position", json_object_new_string("right")); + break; + } + json_object_object_add(json, "status_command", json_object_new_string(bar->status_command)); + json_object_object_add(json, "font", json_object_new_string((bar->font) ? bar->font : config->font)); + if (bar->separator_symbol) { + json_object_object_add(json, "separator_symbol", json_object_new_string(bar->separator_symbol)); + } + json_object_object_add(json, "bar_height", json_object_new_int(bar->height)); + json_object_object_add(json, "wrap_scroll", json_object_new_boolean(bar->wrap_scroll)); + json_object_object_add(json, "workspace_buttons", json_object_new_boolean(bar->workspace_buttons)); + json_object_object_add(json, "strip_workspace_numbers", json_object_new_boolean(bar->strip_workspace_numbers)); + json_object_object_add(json, "binding_mode_indicator", json_object_new_boolean(bar->binding_mode_indicator)); + json_object_object_add(json, "verbose", json_object_new_boolean(bar->verbose)); + json_object_object_add(json, "pango_markup", json_object_new_boolean(bar->pango_markup)); + + json_object *colors = json_object_new_object(); + json_object_object_add(colors, "background", json_object_new_string(bar->colors.background)); + json_object_object_add(colors, "statusline", json_object_new_string(bar->colors.statusline)); + json_object_object_add(colors, "separator", json_object_new_string(bar->colors.separator)); + + if (bar->colors.focused_background) { + json_object_object_add(colors, "focused_background", json_object_new_string(bar->colors.focused_background)); + } else { + json_object_object_add(colors, "focused_background", json_object_new_string(bar->colors.background)); + } + + if (bar->colors.focused_statusline) { + json_object_object_add(colors, "focused_statusline", json_object_new_string(bar->colors.focused_statusline)); + } else { + json_object_object_add(colors, "focused_statusline", json_object_new_string(bar->colors.statusline)); + } + + if (bar->colors.focused_separator) { + json_object_object_add(colors, "focused_separator", json_object_new_string(bar->colors.focused_separator)); + } else { + json_object_object_add(colors, "focused_separator", json_object_new_string(bar->colors.separator)); + } + + json_object_object_add(colors, "focused_workspace_border", json_object_new_string(bar->colors.focused_workspace_border)); + json_object_object_add(colors, "focused_workspace_bg", json_object_new_string(bar->colors.focused_workspace_bg)); + json_object_object_add(colors, "focused_workspace_text", json_object_new_string(bar->colors.focused_workspace_text)); + + json_object_object_add(colors, "inactive_workspace_border", json_object_new_string(bar->colors.inactive_workspace_border)); + json_object_object_add(colors, "inactive_workspace_bg", json_object_new_string(bar->colors.inactive_workspace_bg)); + json_object_object_add(colors, "inactive_workspace_text", json_object_new_string(bar->colors.inactive_workspace_text)); + + json_object_object_add(colors, "active_workspace_border", json_object_new_string(bar->colors.active_workspace_border)); + json_object_object_add(colors, "active_workspace_bg", json_object_new_string(bar->colors.active_workspace_bg)); + json_object_object_add(colors, "active_workspace_text", json_object_new_string(bar->colors.active_workspace_text)); + + json_object_object_add(colors, "urgent_workspace_border", json_object_new_string(bar->colors.urgent_workspace_border)); + json_object_object_add(colors, "urgent_workspace_bg", json_object_new_string(bar->colors.urgent_workspace_bg)); + json_object_object_add(colors, "urgent_workspace_text", json_object_new_string(bar->colors.urgent_workspace_text)); + + if (bar->colors.binding_mode_border) { + json_object_object_add(colors, "binding_mode_border", json_object_new_string(bar->colors.binding_mode_border)); + } else { + json_object_object_add(colors, "binding_mode_border", json_object_new_string(bar->colors.urgent_workspace_border)); + } + + if (bar->colors.binding_mode_bg) { + json_object_object_add(colors, "binding_mode_bg", json_object_new_string(bar->colors.binding_mode_bg)); + } else { + json_object_object_add(colors, "binding_mode_bg", json_object_new_string(bar->colors.urgent_workspace_bg)); + } + + if (bar->colors.binding_mode_text) { + json_object_object_add(colors, "binding_mode_text", json_object_new_string(bar->colors.binding_mode_text)); + } else { + json_object_object_add(colors, "binding_mode_text", json_object_new_string(bar->colors.urgent_workspace_text)); + } + + json_object_object_add(json, "colors", colors); + + // Add outputs if defined + if (bar->outputs && bar->outputs->length > 0) { + json_object *outputs = json_object_new_array(); + int i; + for (i = 0; i < bar->outputs->length; ++i) { + const char *name = bar->outputs->items[i]; + json_object_array_add(outputs, json_object_new_string(name)); + } + json_object_object_add(json, "outputs", outputs); + } + + return json; +} + +json_object *ipc_json_describe_container_recursive(swayc_t *c) { + json_object *object = ipc_json_describe_container(c); + int i; + + json_object *floating = json_object_new_array(); + if (c->type != C_VIEW && c->floating) { + for (i = 0; i < c->floating->length; ++i) { + swayc_t *item = c->floating->items[i]; + json_object_array_add(floating, ipc_json_describe_container_recursive(item)); + } + } + json_object_object_add(object, "floating_nodes", floating); + + json_object *children = json_object_new_array(); + if (c->type != C_VIEW && c->children) { + for (i = 0; i < c->children->length; ++i) { + json_object_array_add(children, ipc_json_describe_container_recursive(c->children->items[i])); + } + } + json_object_object_add(object, "nodes", children); + + json_object *focus = json_object_new_array(); + if (c->type != C_VIEW) { + if (c->focused) { + json_object_array_add(focus, json_object_new_double(c->focused->id)); + } + if (c->floating) { + for (i = 0; i < c->floating->length; ++i) { + swayc_t *item = c->floating->items[i]; + if (item == c->focused) { + continue; + } + + json_object_array_add(focus, json_object_new_double(item->id)); + } + } + if (c->children) { + for (i = 0; i < c->children->length; ++i) { + swayc_t *item = c->children->items[i]; + if (item == c->focused) { + continue; + } + + json_object_array_add(focus, json_object_new_double(item->id)); + } + } + } + json_object_object_add(object, "focus", focus); + + if (c->type == C_ROOT) { + json_object *scratchpad_json = json_object_new_array(); + if (scratchpad->length > 0) { + for (i = 0; i < scratchpad->length; ++i) { + json_object_array_add(scratchpad_json, ipc_json_describe_container_recursive(scratchpad->items[i])); + } + } + json_object_object_add(object, "scratchpad", scratchpad_json); + } + + return object; +} diff --git a/sway/old/ipc-server.c b/sway/old/ipc-server.c new file mode 100644 index 00000000..fd64e81b --- /dev/null +++ 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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/" + 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; ilength; 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/old/layout.c b/sway/old/layout.c new file mode 100644 index 00000000..22f81688 --- /dev/null +++ b/sway/old/layout.c @@ -0,0 +1,1773 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include "sway/config.h" +#include "sway/container.h" +#include "sway/workspace.h" +#include "sway/focus.h" +#include "sway/output.h" +#include "sway/ipc-server.h" +#include "sway/border.h" +#include "sway/layout.h" +#include "list.h" +#include "log.h" + +swayc_t root_container; +swayc_t *current_focus; +list_t *scratchpad; + +int min_sane_h = 60; +int min_sane_w = 100; + +void init_layout(void) { + root_container.id = 0; // normally assigned in new_swayc() + root_container.type = C_ROOT; + root_container.layout = L_NONE; + root_container.name = strdup("root"); + root_container.children = create_list(); + root_container.handle = -1; + root_container.visible = true; + current_focus = &root_container; + scratchpad = create_list(); +} + +int index_child(const swayc_t *child) { + swayc_t *parent = child->parent; + int i, len; + if (!child->is_floating) { + len = parent->children->length; + for (i = 0; i < len; ++i) { + if (parent->children->items[i] == child) { + break; + } + } + } else { + len = parent->floating->length; + for (i = 0; i < len; ++i) { + if (parent->floating->items[i] == child) { + break; + } + } + } + if (!sway_assert(i < len, "Stray container")) { + return -1; + } + return i; +} + +void add_child(swayc_t *parent, swayc_t *child) { + sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, + child->width, child->height, parent, parent->type, parent->width, parent->height); + list_add(parent->children, child); + child->parent = parent; + // set focus for this container + if (!parent->focused) { + parent->focused = child; + } + if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { + child = new_container(child, parent->workspace_layout); + } +} + +static double *get_height(swayc_t *cont) { + return &cont->height; +} + +static double *get_width(swayc_t *cont) { + return &cont->width; +} + +void insert_child(swayc_t *parent, swayc_t *child, int index) { + if (index > parent->children->length) { + index = parent->children->length; + } + if (index < 0) { + index = 0; + } + list_insert(parent->children, index, child); + child->parent = parent; + if (!parent->focused) { + parent->focused = child; + } + if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { + child = new_container(child, parent->workspace_layout); + } + if (is_auto_layout(parent->layout)) { + /* go through each group, adjust the size of the first child of each group */ + double *(*get_maj_dim)(swayc_t *cont); + double *(*get_min_dim)(swayc_t *cont); + if (parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT) { + get_maj_dim = get_width; + get_min_dim = get_height; + } else { + get_maj_dim = get_height; + get_min_dim = get_width; + } + for (int i = index; i < parent->children->length;) { + int start = auto_group_start_index(parent, i); + int end = auto_group_end_index(parent, i); + swayc_t *first = parent->children->items[start]; + if (start + 1 < parent->children->length) { + /* preserve the group's dimension along major axis */ + *get_maj_dim(first) = *get_maj_dim(parent->children->items[start + 1]); + } else { + /* new group, let the apply_layout handle it */ + first->height = first->width = 0; + break; + } + double remaining = *get_min_dim(parent); + for (int j = end - 1; j > start; --j) { + swayc_t *sibling = parent->children->items[j]; + if (sibling == child) { + /* the inserted child won't yet have its minor + dimension set */ + remaining -= *get_min_dim(parent) / (end - start); + } else { + remaining -= *get_min_dim(sibling); + } + } + *get_min_dim(first) = remaining; + i = end; + } + } +} + +void add_floating(swayc_t *ws, swayc_t *child) { + sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, + child->width, child->height, ws, ws->type, ws->width, ws->height); + if (!sway_assert(ws->type == C_WORKSPACE, "Must be of workspace type")) { + return; + } + list_add(ws->floating, child); + child->parent = ws; + child->is_floating = true; + if (!ws->focused) { + ws->focused = child; + } + ipc_event_window(child, "floating"); +} + +swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { + swayc_t *parent = fixed->parent; + if (fixed->is_floating) { + if (active->is_floating) { + int i = index_child(fixed); + list_insert(parent->floating, i + 1, active); + } else { + list_add(parent->children, active); + } + } else { + if (active->is_floating) { + list_add(parent->floating, active); + } else { + int i = index_child(fixed); + if (is_auto_layout(parent->layout)) { + list_add(parent->children, active); + } else { + list_insert(parent->children, i + 1, active); + } + } + } + active->parent = parent; + // focus new child + parent->focused = active; + return active->parent; +} + +swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { + swayc_t *parent = child->parent; + if (parent == NULL) { + return NULL; + } + int i = index_child(child); + if (child->is_floating) { + parent->floating->items[i] = new_child; + } else { + parent->children->items[i] = new_child; + } + // Set parent and focus for new_child + new_child->parent = child->parent; + if (child->parent->focused == child) { + child->parent->focused = new_child; + } + child->parent = NULL; + + // Set geometry for new child + new_child->x = child->x; + new_child->y = child->y; + new_child->width = child->width; + new_child->height = child->height; + + // reset geometry for child + child->width = 0; + child->height = 0; + + // deactivate child + if (child->type == C_VIEW) { + wlc_view_set_state(child->handle, WLC_BIT_ACTIVATED, false); + } + return parent; +} + +swayc_t *remove_child(swayc_t *child) { + int i; + swayc_t *parent = child->parent; + if (child->is_floating) { + // Special case for floating views + for (i = 0; i < parent->floating->length; ++i) { + if (parent->floating->items[i] == child) { + list_del(parent->floating, i); + break; + } + } + i = 0; + } else { + for (i = 0; i < parent->children->length; ++i) { + if (parent->children->items[i] == child) { + list_del(parent->children, i); + break; + } + } + if (is_auto_layout(parent->layout) && parent->children->length) { + /* go through each group, adjust the size of the last child of each group */ + double *(*get_maj_dim)(swayc_t *cont); + double *(*get_min_dim)(swayc_t *cont); + if (parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT) { + get_maj_dim = get_width; + get_min_dim = get_height; + } else { + get_maj_dim = get_height; + get_min_dim = get_width; + } + for (int j = parent->children->length - 1; j >= i;) { + int start = auto_group_start_index(parent, j); + int end = auto_group_end_index(parent, j); + swayc_t *first = parent->children->items[start]; + if (i == start) { + /* removed element was first child in the current group, + use its size along the major axis */ + *get_maj_dim(first) = *get_maj_dim(child); + } else if (start > i) { + /* preserve the group's dimension along major axis */ + *get_maj_dim(first) = *get_maj_dim(parent->children->items[start - 1]); + } + if (end != parent->children->length) { + double remaining = *get_min_dim(parent); + for (int k = start; k < end - 1; ++k) { + swayc_t *sibling = parent->children->items[k]; + remaining -= *get_min_dim(sibling); + } + /* last element of the group gets remaining size, elements + that don't change groups keep their ratio */ + *get_min_dim((swayc_t *) parent->children->items[end - 1]) = remaining; + } /* else last group, let apply_layout handle it */ + j = start - 1; + } + } + } + // Set focused to new container + if (parent->focused == child) { + if (parent->children->length > 0) { + parent->focused = parent->children->items[i ? i-1:0]; + } else if (parent->floating && parent->floating->length) { + parent->focused = parent->floating->items[parent->floating->length - 1]; + } else { + parent->focused = NULL; + } + } + child->parent = NULL; + // deactivate view + if (child->type == C_VIEW) { + wlc_view_set_state(child->handle, WLC_BIT_ACTIVATED, false); + } + return parent; +} + +void swap_container(swayc_t *a, swayc_t *b) { + if (!sway_assert(a&&b, "parameters must be non null") || + !sway_assert(a->parent && b->parent, "containers must have parents")) { + return; + } + size_t a_index = index_child(a); + size_t b_index = index_child(b); + swayc_t *a_parent = a->parent; + swayc_t *b_parent = b->parent; + // Swap the pointers + if (a->is_floating) { + a_parent->floating->items[a_index] = b; + } else { + a_parent->children->items[a_index] = b; + } + if (b->is_floating) { + b_parent->floating->items[b_index] = a; + } else { + b_parent->children->items[b_index] = a; + } + a->parent = b_parent; + b->parent = a_parent; + if (a_parent->focused == a) { + a_parent->focused = b; + } + // don't want to double switch + if (b_parent->focused == b && a_parent != b_parent) { + b_parent->focused = a; + } +} + +void swap_geometry(swayc_t *a, swayc_t *b) { + double x = a->x; + double y = a->y; + double w = a->width; + double h = a->height; + a->x = b->x; + a->y = b->y; + a->width = b->width; + a->height = b->height; + b->x = x; + b->y = y; + b->width = w; + b->height = h; +} + +static void swap_children(swayc_t *container, int a, int b) { + if (a >= 0 && b >= 0 && a < container->children->length + && b < container->children->length + && a != b) { + swayc_t *pa = (swayc_t *)container->children->items[a]; + swayc_t *pb = (swayc_t *)container->children->items[b]; + container->children->items[a] = container->children->items[b]; + container->children->items[b] = pa; + if (is_auto_layout(container->layout)) { + size_t ga = auto_group_index(container, a); + size_t gb = auto_group_index(container, b); + if (ga != gb) { + swap_geometry(pa, pb); + } + } + } +} + +void move_container(swayc_t *container, enum movement_direction dir, int move_amt) { + enum swayc_layouts layout = L_NONE; + swayc_t *parent = container->parent; + if (container->is_floating) { + swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + switch(dir) { + case MOVE_LEFT: + container->x = MAX(0, container->x - move_amt); + break; + case MOVE_RIGHT: + container->x = MIN(output->width - container->width, container->x + move_amt); + break; + case MOVE_UP: + container->y = MAX(0, container->y - move_amt); + break; + case MOVE_DOWN: + container->y = MIN(output->height - container->height, container->y + move_amt); + break; + default: + break; + } + update_geometry(container); + return; + } + if (container->type != C_VIEW && container->type != C_CONTAINER) { + return; + } + if (dir == MOVE_UP || dir == MOVE_DOWN) { + layout = L_VERT; + } else if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { + layout = L_HORIZ; + } else if (dir == MOVE_FIRST) { + // swap first child in auto layout with currently focused child + if (is_auto_layout(parent->layout)) { + int focused_idx = index_child(container); + swayc_t *first = parent->children->items[0]; + if (focused_idx > 0) { + list_swap(parent->children, 0, focused_idx); + swap_geometry(first, container); + } + arrange_windows(parent->parent, -1, -1); + ipc_event_window(container, "move"); + set_focused_container_for(parent->parent, container); + } + return; + } else if (! (dir == MOVE_NEXT || dir == MOVE_PREV)) { + return; + } + swayc_t *child = container; + bool ascended = false; + + // View is wrapped in intermediate container which is needed for displaying + // the titlebar. Moving only the view outside of its parent container would just + // wrap it again under worspace. There would effectively be no movement, + // just a change of wrapping container. + if (child->type == C_VIEW && + parent->type == C_CONTAINER && + parent->children->length == 1 && + parent->parent->type == C_WORKSPACE) { + child = parent; + parent = parent->parent; + } + + while (true) { + sway_log(L_DEBUG, "container:%p, parent:%p, child %p,", + container,parent,child); + if (parent->layout == layout + || (layout == L_NONE && (parent->type == C_CONTAINER || parent->type == C_WORKSPACE)) /* accept any layout for next/prev direction */ + || (parent->layout == L_TABBED && layout == L_HORIZ) + || (parent->layout == L_STACKED && layout == L_VERT) + || is_auto_layout(parent->layout)) { + int diff; + // If it has ascended (parent has moved up), no container is removed + // so insert it at index, or index+1. + // if it has not, the moved container is removed, so it needs to be + // inserted at index-1, or index+1 + if (ascended) { + diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? 0 : 1; + } else { + diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? -1 : 1; + } + int idx = index_child(child); + int desired = idx + diff; + if (dir == MOVE_NEXT || dir == MOVE_PREV) { + // Next/Prev always wrap. + if (desired < 0) { + desired += parent->children->length; + } else if (desired >= parent->children->length) { + desired = 0; + } + } + // when it has ascended, legal insertion position is 0:len + // when it has not, legal insertion position is 0:len-1 + if (desired >= 0 && desired - ascended < parent->children->length) { + if (!ascended) { + child = parent->children->items[desired]; + // Move container into sibling container + if (child->type == C_CONTAINER) { + parent = child; + // Insert it in first/last if matching layout, otherwise + // insert it next to focused container + if (parent->layout == layout + || (parent->layout == L_TABBED && layout == L_HORIZ) + || (parent->layout == L_STACKED && layout == L_VERT) + || is_auto_layout(parent->layout)) { + desired = (diff < 0) * parent->children->length; + } else { + desired = index_child(child->focused) + 1; + } + //reset geometry + container->width = container->height = 0; + } + } + if (container->parent == parent) { + swap_children(parent, idx, desired); + } else { + swayc_t *old_parent = remove_child(container); + insert_child(parent, container, desired); + destroy_container(old_parent); + sway_log(L_DEBUG,"Moving to %p %d", parent, desired); + } + break; + } + } + // Change parent layout if we need to + if (parent->children->length == 1 && parent->layout != layout && layout != L_NONE) { + /* swayc_change_layout(parent, layout); */ + parent->layout = layout; + continue; + } + if (parent->type == C_WORKSPACE) { + // If moving to an adjacent output we need a starting position (since this + // output might border to multiple outputs). + struct wlc_point abs_pos; + get_absolute_center_position(container, &abs_pos); + + swayc_t *output = swayc_adjacent_output(parent->parent, dir, &abs_pos, true); + + if (output) { + sway_log(L_DEBUG, "Moving between outputs"); + swayc_t *old_parent = remove_child(container); + destroy_container(old_parent); + + swayc_t *dest = output->focused; + switch (dir) { + case MOVE_LEFT: + case MOVE_UP: + // reset container geometry + container->width = container->height = 0; + add_child(dest, container); + break; + case MOVE_RIGHT: + case MOVE_DOWN: + // reset container geometry + container->width = container->height = 0; + insert_child(dest, container, 0); + break; + default: + break; + } + // arrange new workspace + arrange_windows(dest, -1, -1); + set_focused_container(container); + break; + } + + // We simply cannot move any further. + if (parent->layout == layout) { + break; + } + // Create container around workspace to insert child into + parent = new_container(parent, layout); + // Previous line set the resulting container's layout to + // workspace_layout. It should have been just layout. + parent->layout = parent->parent->layout; + } + ascended = true; + child = parent; + parent = child->parent; + } + arrange_windows(parent->parent, -1, -1); + ipc_event_window(container, "move"); + set_focused_container_for(parent->parent, container); +} + +void move_container_to(swayc_t* container, swayc_t* destination) { + if (container == destination || swayc_is_parent_of(container, destination)) { + return; + } + swayc_t *parent = remove_child(container); + // Send to new destination + if (container->is_floating) { + swayc_t *ws = swayc_active_workspace_for(destination); + add_floating(ws, container); + + // If the workspace only has one child after adding one, it + // means that the workspace was just initialized. + if (ws->children->length + ws->floating->length == 1) { + ipc_event_workspace(NULL, ws, "init"); + } + } else if (destination->type == C_WORKSPACE) { + // reset container geometry + container->width = container->height = 0; + add_child(destination, container); + + // If the workspace only has one child after adding one, it + // means that the workspace was just initialized. + if (destination->children->length + destination->floating->length == 1) { + ipc_event_workspace(NULL, destination, "init"); + } + } else { + // reset container geometry + container->width = container->height = 0; + add_sibling(destination, container); + } + // Destroy old container if we need to + parent = destroy_container(parent); + // Refocus + swayc_t *op1 = swayc_parent_by_type(destination, C_OUTPUT); + swayc_t *op2 = swayc_parent_by_type(parent, C_OUTPUT); + set_focused_container(get_focused_view(op1)); + arrange_windows(op1, -1, -1); + update_visibility(op1); + if (op1 != op2) { + set_focused_container(get_focused_view(op2)); + arrange_windows(op2, -1, -1); + update_visibility(op2); + } +} + +void move_workspace_to(swayc_t* workspace, swayc_t* destination) { + if (workspace == destination || swayc_is_parent_of(workspace, destination)) { + return; + } + swayc_t *src_op = remove_child(workspace); + // reset container geometry + workspace->width = workspace->height = 0; + add_child(destination, workspace); + sort_workspaces(destination); + // Refocus destination (change to new workspace) + set_focused_container(get_focused_view(workspace)); + arrange_windows(destination, -1, -1); + update_visibility(destination); + + // make sure source output has a workspace + if (src_op->children->length == 0) { + char *ws_name = workspace_next_name(src_op->name); + swayc_t *ws = new_workspace(src_op, ws_name); + ws->is_focused = true; + free(ws_name); + } + set_focused_container(get_focused_view(src_op)); + update_visibility(src_op); +} + +static void adjust_border_geometry(swayc_t *c, struct wlc_geometry *g, + const struct wlc_size *res, int left, int right, int top, int bottom) { + + g->size.w += left + right; + if (g->origin.x - left < 0) { + g->size.w += g->origin.x - left; + } else if (g->origin.x + g->size.w - right > res->w) { + g->size.w = res->w - g->origin.x + right; + } + + g->size.h += top + bottom; + if (g->origin.y - top < 0) { + g->size.h += g->origin.y - top; + } else if (g->origin.y + g->size.h - top > res->h) { + g->size.h = res->h - g->origin.y + top; + } + + g->origin.x = MIN((uint32_t)MAX(g->origin.x - left, 0), res->w); + g->origin.y = MIN((uint32_t)MAX(g->origin.y - top, 0), res->h); + +} + +static void update_border_geometry_floating(swayc_t *c, struct wlc_geometry *geometry) { + struct wlc_geometry g = *geometry; + c->actual_geometry = g; + + swayc_t *output = swayc_parent_by_type(c, C_OUTPUT); + struct wlc_size res; + output_get_scaled_size(output->handle, &res); + + switch (c->border_type) { + case B_NONE: + break; + case B_PIXEL: + adjust_border_geometry(c, &g, &res, c->border_thickness, + c->border_thickness, c->border_thickness, c->border_thickness); + break; + case B_NORMAL: + { + int title_bar_height = config->font_height + 4; // borders + padding + + adjust_border_geometry(c, &g, &res, c->border_thickness, + c->border_thickness, title_bar_height, c->border_thickness); + + struct wlc_geometry title_bar = { + .origin = { + .x = c->actual_geometry.origin.x - c->border_thickness, + .y = c->actual_geometry.origin.y - title_bar_height + }, + .size = { + .w = c->actual_geometry.size.w + (2 * c->border_thickness), + .h = title_bar_height + } + }; + c->title_bar_geometry = title_bar; + break; + } + } + + c->border_geometry = g; + *geometry = c->actual_geometry; + + update_container_border(c); +} + +void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) { + switch (parent->layout) { + case L_TABBED: + case L_STACKED: + if (prev_layout != L_TABBED && prev_layout != L_STACKED) { + // cache current geometry for all non-float children + int i; + for (i = 0; i < parent->children->length; ++i) { + swayc_t *child = parent->children->items[i]; + child->cached_geometry.origin.x = child->x; + child->cached_geometry.origin.y = child->y; + child->cached_geometry.size.w = child->width; + child->cached_geometry.size.h = child->height; + } + } + break; + default: + if (prev_layout == L_TABBED || prev_layout == L_STACKED) { + // recover cached geometry for all non-float children + int i; + for (i = 0; i < parent->children->length; ++i) { + swayc_t *child = parent->children->items[i]; + // only recoverer cached geometry if non-zero + if (!wlc_geometry_equals(&child->cached_geometry, &wlc_geometry_zero)) { + child->x = child->cached_geometry.origin.x; + child->y = child->cached_geometry.origin.y; + child->width = child->cached_geometry.size.w; + child->height = child->cached_geometry.size.h; + } + } + } + break; + } +} + +static int update_gap_geometry(swayc_t *container, struct wlc_geometry *g) { + swayc_t *ws = swayc_parent_by_type(container, C_WORKSPACE); + swayc_t *op = ws->parent; + int gap = container->is_floating ? 0 : swayc_gap(container); + if (gap % 2 != 0) { + // because gaps are implemented as "half sized margins" it's currently + // not possible to align views properly with odd sized gaps. + gap -= 1; + } + + g->origin.x = container->x + gap/2 < op->width ? container->x + gap/2 : op->width-1; + g->origin.y = container->y + gap/2 < op->height ? container->y + gap/2 : op->height-1; + g->size.w = container->width > gap ? container->width - gap : 1; + g->size.h = container->height > gap ? container->height - gap : 1; + + if ((!config->edge_gaps && gap > 0) || (config->smart_gaps && ws->children->length == 1)) { + // Remove gap against the workspace edges. Because a pixel is not + // divisable, depending on gap size and the number of siblings our view + // might be at the workspace edge without being exactly so (thus test + // with gap, and align correctly). + if (container->x - gap <= ws->x) { + g->origin.x = ws->x; + g->size.w = container->width - gap/2; + } + if (container->y - gap <= ws->y) { + g->origin.y = ws->y; + g->size.h = container->height - gap/2; + } + if (container->x + container->width + gap >= ws->x + ws->width) { + g->size.w = ws->x + ws->width - g->origin.x; + } + if (container->y + container->height + gap >= ws->y + ws->height) { + g->size.h = ws->y + ws->height - g->origin.y; + } + } + + return gap; +} + +void update_geometry(swayc_t *container) { + if (container->type != C_VIEW && container->type != C_CONTAINER) { + return; + } + + swayc_t *workspace = swayc_parent_by_type(container, C_WORKSPACE); + swayc_t *op = workspace->parent; + swayc_t *parent = container->parent; + + struct wlc_geometry geometry = { + .origin = { + .x = container->x < op->width ? container->x : op->width-1, + .y = container->y < op->height ? container->y : op->height-1 + }, + .size = { + .w = container->width, + .h = container->height, + } + }; + + int gap = 0; + + // apply inner gaps to non-tabbed/stacked containers + swayc_t *p = swayc_tabbed_stacked_ancestor(container); + if (p == NULL) { + gap = update_gap_geometry(container, &geometry); + } + + swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + struct wlc_size size; + output_get_scaled_size(output->handle, &size); + + if (swayc_is_fullscreen(container)) { + geometry.origin.x = 0; + geometry.origin.y = 0; + geometry.size.w = size.w; + geometry.size.h = size.h; + if (op->focused == workspace) { + wlc_view_bring_to_front(container->handle); + } + + container->border_geometry = wlc_geometry_zero; + container->title_bar_geometry = wlc_geometry_zero; + border_clear(container->border); + } else if (container->is_floating) { // allocate border for floating window + update_border_geometry_floating(container, &geometry); + } else if (!container->is_floating) { // allocate border for titled window + container->border_geometry = geometry; + + int border_top = container->border_thickness; + int border_bottom = container->border_thickness; + int border_left = container->border_thickness; + int border_right = container->border_thickness; + + // handle hide_edge_borders + if (config->hide_edge_borders != E_NONE && (gap <= 0 || (config->smart_gaps && workspace->children->length == 1))) { + if (config->hide_edge_borders == E_VERTICAL || config->hide_edge_borders == E_BOTH) { + if (geometry.origin.x == workspace->x) { + border_left = 0; + } + + if (geometry.origin.x + geometry.size.w == workspace->x + workspace->width) { + border_right = 0; + } + } + + if (config->hide_edge_borders == E_HORIZONTAL || config->hide_edge_borders == E_BOTH) { + if (geometry.origin.y == workspace->y || should_hide_top_border(container, geometry.origin.y)) { + border_top = 0; + } + + if (geometry.origin.y + geometry.size.h == workspace->y + workspace->height) { + border_bottom = 0; + } + } + + if (config->hide_edge_borders == E_SMART && workspace->children->length == 1) { + border_top = 0; + border_bottom = 0; + border_left = 0; + border_right = 0; + } + } + + int title_bar_height = config->font_height + 4; //borders + padding + + if (parent->layout == L_TABBED && parent->children->length > 1) { + int i, x = 0, w, l, r; + l = parent->children->length; + w = geometry.size.w / l; + r = geometry.size.w % l; + for (i = 0; i < parent->children->length; ++i) { + swayc_t *view = parent->children->items[i]; + if (view == container) { + x = w * i; + if (i == l - 1) { + w += r; + } + break; + } + } + + struct wlc_geometry title_bar = { + .origin = { + .x = container->border_geometry.origin.x + x, + .y = container->border_geometry.origin.y + }, + .size = { + .w = w, + .h = title_bar_height + } + }; + geometry.origin.x += border_left; + geometry.origin.y += title_bar.size.h; + geometry.size.w -= (border_left + border_right); + geometry.size.h -= (border_bottom + title_bar.size.h); + container->title_bar_geometry = title_bar; + } else if (parent->layout == L_STACKED && parent->children->length > 1) { + int i, y = 0; + for (i = 0; i < parent->children->length; ++i) { + swayc_t *view = parent->children->items[i]; + if (view == container) { + y = title_bar_height * i; + } + } + + struct wlc_geometry title_bar = { + .origin = { + .x = container->border_geometry.origin.x, + .y = container->border_geometry.origin.y + y + }, + .size = { + .w = container->border_geometry.size.w, + .h = title_bar_height + } + }; + title_bar_height = title_bar_height * parent->children->length; + geometry.origin.x += border_left; + geometry.origin.y += title_bar_height; + geometry.size.w -= (border_left + border_right); + geometry.size.h -= (border_bottom + title_bar_height); + container->title_bar_geometry = title_bar; + } else { + switch (container->border_type) { + case B_NONE: + break; + case B_PIXEL: + geometry.origin.x += border_left; + geometry.origin.y += border_top; + geometry.size.w -= (border_left + border_right); + geometry.size.h -= (border_top + border_bottom); + break; + case B_NORMAL: + { + struct wlc_geometry title_bar = { + .origin = { + .x = container->border_geometry.origin.x, + .y = container->border_geometry.origin.y + }, + .size = { + .w = container->border_geometry.size.w, + .h = title_bar_height + } + }; + geometry.origin.x += border_left; + geometry.origin.y += title_bar.size.h; + geometry.size.w -= (border_left + border_right); + geometry.size.h -= (border_bottom + title_bar.size.h); + container->title_bar_geometry = title_bar; + break; + } + } + } + + container->actual_geometry = geometry; + + if (container->type == C_VIEW) { + update_container_border(container); + } + } + + if (container->type == C_VIEW) { + wlc_view_set_geometry(container->handle, 0, &geometry); + } +} + +/** + * Layout application prototypes + */ +static void apply_horiz_layout(swayc_t *container, const double x, + const double y, const double width, + const double height, const int start, + const int end); +static void apply_vert_layout(swayc_t *container, const double x, + const double y, const double width, + const double height, const int start, + const int end); +static void apply_tabbed_or_stacked_layout(swayc_t *container, double x, + double y, double width, + double height); + +static void apply_auto_layout(swayc_t *container, const double x, const double y, + const double width, const double height, + enum swayc_layouts group_layout, + bool master_first); + +static void arrange_windows_r(swayc_t *container, double width, double height) { + int i; + if (width == -1 || height == -1) { + swayc_log(L_DEBUG, container, "Arranging layout for %p", container); + width = container->width; + height = container->height; + } + // pixels are indivisible. if we don't round the pixels, then the view + // calculations will be off (e.g. 50.5 + 50.5 = 101, but in reality it's + // 50 + 50 = 100). doing it here cascades properly to all width/height/x/y. + width = floor(width); + height = floor(height); + + sway_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container, + container->name, container->width, container->height, container->x, + container->y); + + double x = 0, y = 0; + switch (container->type) { + case C_ROOT: + for (i = 0; i < container->children->length; ++i) { + swayc_t *output = container->children->items[i]; + sway_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); + arrange_windows_r(output, -1, -1); + } + return; + case C_OUTPUT: + { + struct wlc_size resolution; + output_get_scaled_size(container->handle, &resolution); + width = resolution.w; height = resolution.h; + // output must have correct size due to e.g. seamless mouse, + // but a workspace might be smaller depending on panels. + container->width = width; + container->height = height; + } + // arrange all workspaces: + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + arrange_windows_r(child, -1, -1); + } + // Bring all unmanaged views to the front + for (i = 0; i < container->unmanaged->length; ++i) { + wlc_handle *handle = container->unmanaged->items[i]; + wlc_view_bring_to_front(*handle); + } + return; + case C_WORKSPACE: + { + swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + width = output->width, height = output->height; + /* TODO WLR + for (i = 0; i < desktop_shell.panels->length; ++i) { + struct panel_config *config = desktop_shell.panels->items[i]; + if (config->output == output->handle) { + struct wlc_size size = *wlc_surface_get_size(config->surface); + sway_log(L_DEBUG, "-> Found panel for this workspace: %ux%u, position: %u", size.w, size.h, config->panel_position); + switch (config->panel_position) { + case DESKTOP_SHELL_PANEL_POSITION_TOP: + y += size.h; height -= size.h; + break; + case DESKTOP_SHELL_PANEL_POSITION_BOTTOM: + height -= size.h; + break; + case DESKTOP_SHELL_PANEL_POSITION_LEFT: + x += size.w; width -= size.w; + break; + case DESKTOP_SHELL_PANEL_POSITION_RIGHT: + width -= size.w; + break; + } + } + } + */ + int gap = swayc_gap(container); + x = container->x = x + gap; + y = container->y = y + gap; + width = container->width = width - gap * 2; + height = container->height = height - gap * 2; + sway_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", container->name, container->x, container->y); + } + // children are properly handled below + break; + case C_VIEW: + { + container->width = width; + container->height = height; + update_geometry(container); + sway_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f", container->width, + container->height, container->x, container->y); + } + return; + default: + container->width = width; + container->height = height; + x = container->x; + y = container->y; + + // add gaps to top level tapped/stacked container + if (container->parent->type == C_WORKSPACE && + (container->layout == L_TABBED || container->layout == L_STACKED)) { + update_geometry(container); + width = container->border_geometry.size.w; + height = container->border_geometry.size.h; + x = container->border_geometry.origin.x; + y = container->border_geometry.origin.y; + } + + // update container size if it's a direct child in a tabbed/stacked layout + // if parent is a workspace, its actual_geometry won't be initialized + if (swayc_tabbed_stacked_parent(container) != NULL && + container->parent->type != C_WORKSPACE) { + // Use parent actual_geometry as a base for calculating + // container geometry + container->width = container->parent->actual_geometry.size.w; + container->height = container->parent->actual_geometry.size.h; + container->x = container->parent->actual_geometry.origin.x; + container->y = container->parent->actual_geometry.origin.y; + + update_geometry(container); + width = container->width = container->actual_geometry.size.w; + height = container->height = container->actual_geometry.size.h; + x = container->x = container->actual_geometry.origin.x; + y = container->y = container->actual_geometry.origin.y; + } + + break; + } + + switch (container->layout) { + case L_HORIZ: + default: + apply_horiz_layout(container, x, y, width, height, 0, + container->children->length); + break; + case L_VERT: + apply_vert_layout(container, x, y, width, height, 0, + container->children->length); + break; + case L_TABBED: + case L_STACKED: + apply_tabbed_or_stacked_layout(container, x, y, width, height); + break; + case L_AUTO_LEFT: + apply_auto_layout(container, x, y, width, height, L_VERT, true); + break; + case L_AUTO_RIGHT: + apply_auto_layout(container, x, y, width, height, L_VERT, false); + break; + case L_AUTO_TOP: + apply_auto_layout(container, x, y, width, height, L_HORIZ, true); + break; + case L_AUTO_BOTTOM: + apply_auto_layout(container, x, y, width, height, L_HORIZ, false); + break; + } + + // Arrage floating layouts for workspaces last + if (container->type == C_WORKSPACE) { + for (int i = 0; i < container->floating->length; ++i) { + swayc_t *view = container->floating->items[i]; + if (view->type == C_VIEW) { + update_geometry(view); + sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", + view->width, view->height, view->x, view->y); + if (swayc_is_fullscreen(view)) { + wlc_view_bring_to_front(view->handle); + } else if (!container->focused || + !swayc_is_fullscreen(container->focused)) { + wlc_view_bring_to_front(view->handle); + } + } + } + } +} + +void apply_horiz_layout(swayc_t *container, const double x, const double y, + const double width, const double height, + const int start, const int end) { + double scale = 0; + // Calculate total width + for (int i = start; i < end; ++i) { + double *old_width = &((swayc_t *)container->children->items[i])->width; + if (*old_width <= 0) { + if (end - start > 1) { + *old_width = width / (end - start - 1); + } else { + *old_width = width; + } + } + scale += *old_width; + } + scale = width / scale; + + // Resize windows + double child_x = x; + if (scale > 0.1) { + sway_log(L_DEBUG, "Arranging %p horizontally", container); + swayc_t *focused = NULL; + for (int i = start; i < end; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, + "Calculating arrangement for %p:%d (will scale %f by %f)", child, + child->type, width, scale); + child->x = child_x; + child->y = y; + + if (child == container->focused) { + focused = child; + } + + if (i == end - 1) { + double remaining_width = x + width - child_x; + arrange_windows_r(child, remaining_width, height); + } else { + arrange_windows_r(child, child->width * scale, height); + } + child_x += child->width; + } + + // update focused view border last because it may + // depend on the title bar geometry of its siblings. + if (focused && container->children->length > 1) { + update_container_border(focused); + } + } +} + +void apply_vert_layout(swayc_t *container, const double x, const double y, + const double width, const double height, const int start, + const int end) { + int i; + double scale = 0; + // Calculate total height + for (i = start; i < end; ++i) { + double *old_height = &((swayc_t *)container->children->items[i])->height; + if (*old_height <= 0) { + if (end - start > 1) { + *old_height = height / (end - start - 1); + } else { + *old_height = height; + } + } + scale += *old_height; + } + scale = height / scale; + + // Resize + double child_y = y; + if (scale > 0.1) { + sway_log(L_DEBUG, "Arranging %p vertically", container); + swayc_t *focused = NULL; + for (i = start; i < end; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, + "Calculating arrangement for %p:%d (will scale %f by %f)", child, + child->type, height, scale); + child->x = x; + child->y = child_y; + + if (child == container->focused) { + focused = child; + } + + if (i == end - 1) { + double remaining_height = y + height - child_y; + arrange_windows_r(child, width, remaining_height); + } else { + arrange_windows_r(child, width, child->height * scale); + } + child_y += child->height; + } + + // update focused view border last because it may + // depend on the title bar geometry of its siblings. + if (focused && container->children->length > 1) { + update_container_border(focused); + } + } +} + +void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, + double width, double height) { + int i; + swayc_t *focused = NULL; + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + child->x = x; + child->y = y; + if (child == container->focused) { + focused = child; + } else { + arrange_windows_r(child, width, height); + } + } + + if (focused) { + arrange_windows_r(focused, width, height); + } +} + +void apply_auto_layout(swayc_t *container, const double x, const double y, + const double width, const double height, + enum swayc_layouts group_layout, + bool master_first) { + // Auto layout "container" in width x height @ x, y + // using "group_layout" for each of the groups in the container. + // There is one "master" group, plus container->nb_slave_groups. + // Each group is layed out side by side following the "major" axis. + // The direction of the layout used for groups is the "minor" axis. + // Example: + // + // ---- major axis --> + // +---------+-----------+ + // | | | | + // | master | slave 1 | | + // | +-----------+ | minor axis (direction of group_layout) + // | | | | + // | | slave 2 | V + // +---------+-----------+ + // + // container with three children (one master and two slaves) and + // a single slave group (containing slave 1 and 2). The master + // group and slave group are layed out using L_VERT. + + size_t nb_groups = auto_group_count(container); + + // the target dimension of the container along the "major" axis, each + // group in the container will be layed out using "group_layout" along + // the "minor" axis. + double dim_maj; + double pos_maj; + + // x and y coords for the next group to be laid out. + const double *group_x, *group_y; + + // pos of the next group to layout along the major axis + double pos; + + // size of the next group along the major axis. + double group_dim; + + // height and width of next group to be laid out. + const double *group_h, *group_w; + + switch (group_layout) { + default: + sway_log(L_DEBUG, "Unknown layout type (%d) used in %s()", + group_layout, __func__); + /* fall through */ + case L_VERT: + dim_maj = width; + pos_maj = x; + + group_x = &pos; + group_y = &y; + group_w = &group_dim; + group_h = &height; + break; + case L_HORIZ: + dim_maj = height; + pos_maj = y; + + group_x = &x; + group_y = &pos; + group_w = &width; + group_h = &group_dim; + break; + } + + /* Determine the dimension of each of the groups in the layout. + * Dimension will be width for a VERT layout and height for a HORIZ + * layout. */ + double old_group_dim[nb_groups]; + double old_dim = 0; + for (size_t group = 0; group < nb_groups; ++group) { + int idx; + if (auto_group_bounds(container, group, &idx, NULL)) { + swayc_t *child = container->children->items[idx]; + double *dim = group_layout == L_HORIZ ? &child->height : &child->width; + if (*dim <= 0) { + // New child with uninitialized dimension + *dim = dim_maj; + if (nb_groups > 1) { + // child gets a dimension proportional to existing groups, + // it will be later scaled based on to the available size + // in the major axis. + *dim /= (nb_groups - 1); + } + } + old_dim += *dim; + old_group_dim[group] = *dim; + } + } + double scale = dim_maj / old_dim; + + /* Apply layout to each group */ + pos = pos_maj; + + for (size_t group = 0; group < nb_groups; ++group) { + int start, end; // index of first (inclusive) and last (exclusive) child in the group + if (auto_group_bounds(container, group, &start, &end)) { + // adjusted size of the group + group_dim = old_group_dim[group] * scale; + if (group == nb_groups - 1) { + group_dim = pos_maj + dim_maj - pos; // remaining width + } + sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", + container, group, start, end, *group_w, *group_h, *group_x, *group_y); + switch (group_layout) { + default: + case L_VERT: + apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; + case L_HORIZ: + apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; + } + + /* update position for next group */ + pos += group_dim; + } + } +} + +void arrange_windows(swayc_t *container, double width, double height) { + update_visibility(container); + arrange_windows_r(container, width, height); + layout_log(&root_container, 0); +} + +void arrange_backgrounds(void) { + /* TODO WLR + struct background_config *bg; + for (int i = 0; i < desktop_shell.backgrounds->length; ++i) { + bg = desktop_shell.backgrounds->items[i]; + wlc_view_send_to_back(bg->handle); + } + */ +} + +/** + * Get swayc in the direction of newly entered output. + */ +static swayc_t *get_swayc_in_output_direction(swayc_t *output, enum movement_direction dir) { + if (!output) { + return NULL; + } + + swayc_t *ws = swayc_focus_by_type(output, C_WORKSPACE); + if (ws && ws->children->length > 0) { + switch (dir) { + case MOVE_LEFT: + // get most right child of new output + return ws->children->items[ws->children->length-1]; + case MOVE_RIGHT: + // get most left child of new output + return ws->children->items[0]; + case MOVE_UP: + case MOVE_DOWN: + { + swayc_t *focused_view = swayc_focus_by_type(ws, C_VIEW); + if (focused_view && focused_view->parent) { + swayc_t *parent = focused_view->parent; + if (parent->layout == L_VERT) { + if (dir == MOVE_UP) { + // get child furthest down on new output + return parent->children->items[parent->children->length-1]; + } else if (dir == MOVE_DOWN) { + // get child furthest up on new output + return parent->children->items[0]; + } + } + return focused_view; + } + break; + } + default: + break; + } + } + + return output; +} + +swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_direction dir, swayc_t *limit) { + if (dir == MOVE_CHILD) { + return container->focused; + } + + swayc_t *parent = container->parent; + if (dir == MOVE_PARENT) { + if (parent->type == C_OUTPUT) { + return NULL; + } else { + return parent; + } + } + + if (dir == MOVE_PREV || dir == MOVE_NEXT) { + int focused_idx = index_child(container); + if (focused_idx == -1) { + return NULL; + } else { + int desired = (focused_idx + (dir == MOVE_NEXT ? 1 : -1)) % + parent->children->length; + if (desired < 0) { + desired += parent->children->length; + } + return parent->children->items[desired]; + } + } + + // If moving to an adjacent output we need a starting position (since this + // output might border to multiple outputs). + struct wlc_point abs_pos; + get_absolute_center_position(container, &abs_pos); + + if (container->type == C_VIEW && swayc_is_fullscreen(container)) { + sway_log(L_DEBUG, "Moving from fullscreen view, skipping to output"); + container = swayc_parent_by_type(container, C_OUTPUT); + get_absolute_center_position(container, &abs_pos); + swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); + return get_swayc_in_output_direction(output, dir); + } + + if (container->type == C_WORKSPACE && container->fullscreen) { + sway_log(L_DEBUG, "Moving to fullscreen view"); + return container->fullscreen; + } + + swayc_t *wrap_candidate = NULL; + while (true) { + // Test if we can even make a difference here + bool can_move = false; + int desired; + int idx = index_child(container); + if (parent->type == C_ROOT) { + swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); + if (!output || output == container) { + return wrap_candidate; + } + sway_log(L_DEBUG, "Moving between outputs"); + return get_swayc_in_output_direction(output, dir); + } else { + if (is_auto_layout(parent->layout)) { + bool is_major = parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT + ? dir == MOVE_LEFT || dir == MOVE_RIGHT + : dir == MOVE_DOWN || dir == MOVE_UP; + size_t gidx = auto_group_index(parent, idx); + if (is_major) { + size_t desired_grp = gidx + (dir == MOVE_RIGHT || dir == MOVE_DOWN ? 1 : -1); + can_move = auto_group_bounds(parent, desired_grp, &desired, NULL); + } else { + desired = idx + (dir == MOVE_RIGHT || dir == MOVE_DOWN ? 1 : -1); + int start, end; + can_move = auto_group_bounds(parent, gidx, &start, &end) + && desired >= start && desired < end; + } + } else { + if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { + if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { + can_move = true; + desired = idx + (dir == MOVE_LEFT ? -1 : 1); + } + } else { + if (parent->layout == L_VERT || parent->layout == L_STACKED) { + can_move = true; + desired = idx + (dir == MOVE_UP ? -1 : 1); + } + } + } + } + + if (can_move) { + if (container->is_floating) { + if (desired < 0) { + wrap_candidate = parent->floating->items[parent->floating->length-1]; + } else if (desired >= parent->floating->length){ + wrap_candidate = parent->floating->items[0]; + } else { + wrap_candidate = parent->floating->items[desired]; + } + if (wrap_candidate) { + wlc_view_bring_to_front(wrap_candidate->handle); + } + return wrap_candidate; + } else if (desired < 0 || desired >= parent->children->length) { + can_move = false; + int len = parent->children->length; + if (!wrap_candidate && len > 1) { + if (desired < 0) { + wrap_candidate = parent->children->items[len-1]; + } else { + wrap_candidate = parent->children->items[0]; + } + if (config->force_focus_wrapping) { + return wrap_candidate; + } + } + } else { + sway_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, + idx, container, dir, desired, parent->children->items[desired]); + return parent->children->items[desired]; + } + } + if (!can_move) { + container = parent; + parent = parent->parent; + if (!parent || container == limit) { + // wrapping is the last chance + return wrap_candidate; + } + } + } +} + +swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) { + return get_swayc_in_direction_under(container, dir, NULL); +} + +void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge) { + int i; + bool layout_match = true; + sway_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); + if (edge == WLC_RESIZE_EDGE_LEFT || edge == WLC_RESIZE_EDGE_RIGHT) { + container->width += amount; + layout_match = container->layout == L_HORIZ; + } else if (edge == WLC_RESIZE_EDGE_TOP || edge == WLC_RESIZE_EDGE_BOTTOM) { + container->height += amount; + layout_match = container->layout == L_VERT; + } + if (container->type == C_VIEW) { + update_geometry(container); + return; + } + if (layout_match) { + for (i = 0; i < container->children->length; i++) { + recursive_resize(container->children->items[i], amount/container->children->length, edge); + } + } else { + for (i = 0; i < container->children->length; i++) { + recursive_resize(container->children->items[i], amount, edge); + } + } +} + +enum swayc_layouts default_layout(swayc_t *output) { + if (config->default_layout != L_NONE) { + return config->default_layout; + } else if (config->default_orientation != L_NONE) { + return config->default_orientation; + } else if (output->width >= output->height) { + return L_HORIZ; + } else { + return L_VERT; + } +} + +bool is_auto_layout(enum swayc_layouts layout) { + return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); +} + +/** + * Return the number of master elements in a container + */ +static inline size_t auto_master_count(const swayc_t *container) { + sway_assert(container->children->length >= 0, "Container %p has (negative) children %d", + container, container->children->length); + return MIN(container->nb_master, (size_t)container->children->length); +} + +/** + * Return the number of children in the slave groups. This corresponds to the children + * that are not members of the master group. + */ +static inline size_t auto_slave_count(const swayc_t *container) { + return container->children->length - auto_master_count(container); +} + +/** + * Return the number of slave groups in the container. + */ +size_t auto_slave_group_count(const swayc_t *container) { + return MIN(container->nb_slave_groups, auto_slave_count(container)); +} + +/** + * Return the combined number of master and slave groups in the container. + */ +size_t auto_group_count(const swayc_t *container) { + return auto_slave_group_count(container) + + (container->children->length && container->nb_master ? 1 : 0); +} + +/** + * given the index of a container's child, return the index of the first child of the group + * which index is a member of. + */ +int auto_group_start_index(const swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout) + || (size_t)index < container->nb_master) { + return 0; + } else { + size_t nb_slaves = auto_slave_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; + int start_idx; + if (index < idx2) { + start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; + } else { + start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); + } + return MIN(start_idx, container->children->length); + } +} + +/** + * given the index of a container's child, return the index of the first child of the group + * that follows the one which index is a member of. + * This makes the function usable to walk through the groups in a container. + */ +int auto_group_end_index(const swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout)) { + return container->children->length; + } else { + int nxt_idx; + if ((size_t)index < container->nb_master) { + nxt_idx = auto_master_count(container); + } else { + size_t nb_slaves = auto_slave_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; + } else { + nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); + } + } + return MIN(nxt_idx, container->children->length); + } +} + +/** + * return the index of the Group containing th child of . + * The index is the order of the group along the container's major axis (starting at 0). + */ +size_t auto_group_index(const swayc_t *container, int index) { + if (index < 0) { + return 0; + } + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + size_t nb_slaves = auto_slave_count(container); + if ((size_t)index < container->nb_master) { + if (master_first || nb_slaves <= 0) { + return 0; + } else { + return auto_slave_group_count(container); + } + } else { + size_t nb_slave_grp = auto_slave_group_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; + size_t grp_idx; + if (index < idx2) { + grp_idx = (index - container->nb_master) / grp_sz; + } else { + grp_idx = (nb_slave_grp - remainder) + (index - idx2) / (grp_sz + 1) ; + } + return grp_idx + (master_first && container-> nb_master ? 1 : 0); + } +} + +/** + * Return the first index (inclusive) and last index (exclusive) of the elements of a group in + * an auto layout. + * If the bounds of the given group can be calculated, they are returned in the start/end + * parameters (int pointers) and the return value will be true. + * The indexes are passed by reference and can be NULL. + */ +bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end) { + size_t nb_grp = auto_group_count(container); + if (group_index >= nb_grp) { + return false; + } + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + size_t nb_master = auto_master_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + int g_start, g_end; + if (nb_master && (master_first ? group_index == 0 : group_index == nb_grp - 1)) { + g_start = 0; + g_end = nb_master; + } else { + size_t nb_slaves = auto_slave_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + size_t g0 = master_first && container->nb_master ? 1 : 0; + size_t g1 = g0 + nb_slave_grp - remainder; + if (group_index < g1) { + g_start = container->nb_master + (group_index - g0) * grp_sz; + g_end = g_start + grp_sz; + } else { + size_t g2 = group_index - g1; + g_start = container->nb_master + + (nb_slave_grp - remainder) * grp_sz + + g2 * (grp_sz + 1); + g_end = g_start + grp_sz + 1; + } + } + if (start) { + *start = g_start; + } + if (end) { + *end = g_end; + } + return true; +} diff --git a/sway/old/output.c b/sway/old/output.c new file mode 100644 index 00000000..edfcac98 --- /dev/null +++ b/sway/old/output.c @@ -0,0 +1,278 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include "sway/output.h" +#include "log.h" +#include "list.h" + +void output_get_scaled_size(wlc_handle handle, struct wlc_size *size) { + *size = *wlc_output_get_resolution(handle); + uint32_t scale = wlc_output_get_scale(handle); + size->w /= scale; + size->h /= scale; +} + +swayc_t *output_by_name(const char* name, const struct wlc_point *abs_pos) { + swayc_t *output = NULL; + // If there is no output directly next to the current one, use + // swayc_opposite_output to wrap. + if (strcasecmp(name, "left") == 0) { + output = swayc_adjacent_output(NULL, MOVE_LEFT, abs_pos, true); + if (!output) { + output = swayc_opposite_output(MOVE_RIGHT, abs_pos); + } + } else if (strcasecmp(name, "right") == 0) { + output = swayc_adjacent_output(NULL, MOVE_RIGHT, abs_pos, true); + if (!output) { + output = swayc_opposite_output(MOVE_LEFT, abs_pos); + } + } else if (strcasecmp(name, "up") == 0) { + output = swayc_adjacent_output(NULL, MOVE_UP, abs_pos, true); + if (!output) { + output = swayc_opposite_output(MOVE_DOWN, abs_pos); + } + } else if (strcasecmp(name, "down") == 0) { + output = swayc_adjacent_output(NULL, MOVE_DOWN, abs_pos, true); + if (!output) { + output = swayc_opposite_output(MOVE_UP, abs_pos); + } + } else { + for(int i = 0; i < root_container.children->length; ++i) { + swayc_t *c = root_container.children->items[i]; + if (c->type == C_OUTPUT && strcasecmp(c->name, name) == 0) { + return c; + } + } + } + return output; +} + +swayc_t *swayc_opposite_output(enum movement_direction dir, + const struct wlc_point *abs_pos) { + + // Search through all the outputs and pick the output whose edge covers the + // given position, and is at leftmost/rightmost/upmost/downmost side of the + // screen (decided by the direction given). + swayc_t *opposite = NULL; + char *dir_text = NULL; + switch(dir) { + case MOVE_LEFT: + case MOVE_RIGHT: ; + for (int i = 0; i < root_container.children->length; ++i) { + swayc_t *c = root_container.children->items[i]; + if (abs_pos->y >= c->y && abs_pos->y <= c->y + c->height) { + if (!opposite) { + opposite = c; + } else if ((dir == MOVE_LEFT && c->x < opposite->x) + || (dir == MOVE_RIGHT && c->x > opposite->x)) { + opposite = c; + } + } + } + dir_text = dir == MOVE_LEFT ? "leftmost" : "rightmost"; + break; + case MOVE_UP: + case MOVE_DOWN: ; + for (int i = 0; i < root_container.children->length; ++i) { + swayc_t *c = root_container.children->items[i]; + if (abs_pos->x >= c->x && abs_pos->x <= c->x + c->width) { + if (!opposite) { + opposite = c; + } else if ((dir == MOVE_UP && c->y < opposite->y) + || (dir == MOVE_DOWN && c->y > opposite->y)) { + opposite = c; + } + } + } + dir_text = dir == MOVE_UP ? "upmost" : "downmost"; + break; + default: + sway_abort("Function called with invalid argument."); + break; + } + if (opposite) { + sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s from y-position %i", + opposite->name, opposite->width, opposite->height, opposite->x, opposite->y, + dir_text, abs_pos->y); + } + return opposite; +} + +// Position is where on the edge (as absolute position) the adjacent output should be searched for. +swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, + const struct wlc_point *abs_pos, bool pick_closest) { + + if (!output) { + output = swayc_active_output(); + } + // In order to find adjacent outputs we need to test that the outputs are + // aligned on one axis (decided by the direction given) and that the given + // position is within the edge of the adjacent output. If no such output + // exists we pick the adjacent output within the edge that is closest to + // the given position, if any. + swayc_t *adjacent = NULL; + char *dir_text = NULL; + switch(dir) { + case MOVE_LEFT: + case MOVE_RIGHT: ; + double delta_y = 0; + for(int i = 0; i < root_container.children->length; ++i) { + swayc_t *c = root_container.children->items[i]; + if (c == output || c->type != C_OUTPUT) { + continue; + } + bool x_aligned = dir == MOVE_LEFT ? + c->x + c->width == output->x : + c->x == output->x + output->width; + if (!x_aligned) { + continue; + } + if (abs_pos->y >= c->y && abs_pos->y <= c->y + c->height) { + delta_y = 0; + adjacent = c; + break; + } else if (pick_closest) { + // track closest adjacent output + double top_y = c->y, bottom_y = c->y + c->height; + if (top_y >= output->y && top_y <= output->y + output->height) { + double delta = top_y - abs_pos->y; + if (delta < 0) delta = -delta; + if (delta < delta_y || !adjacent) { + delta_y = delta; + adjacent = c; + } + } + // we check both points and pick the closest + if (bottom_y >= output->y && bottom_y <= output->y + output->height) { + double delta = bottom_y - abs_pos->y; + if (delta < 0) delta = -delta; + if (delta < delta_y || !adjacent) { + delta_y = delta; + adjacent = c; + } + } + } + } + dir_text = dir == MOVE_LEFT ? "left of" : "right of"; + if (adjacent && delta_y == 0) { + sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (y-position %i)", + adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, + dir_text, output->name, abs_pos->y); + } else if (adjacent) { + // so we end up picking the closest adjacent output because + // there is no directly adjacent to the given position + sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (y-position %i, delta: %.0f)", + adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, + dir_text, output->name, abs_pos->y, delta_y); + } + break; + case MOVE_UP: + case MOVE_DOWN: ; + double delta_x = 0; + for(int i = 0; i < root_container.children->length; ++i) { + swayc_t *c = root_container.children->items[i]; + if (c == output || c->type != C_OUTPUT) { + continue; + } + bool y_aligned = dir == MOVE_UP ? + c->y + c->height == output->y : + c->y == output->y + output->height; + if (!y_aligned) { + continue; + } + if (abs_pos->x >= c->x && abs_pos->x <= c->x + c->width) { + delta_x = 0; + adjacent = c; + break; + } else if (pick_closest) { + // track closest adjacent output + double left_x = c->x, right_x = c->x + c->width; + if (left_x >= output->x && left_x <= output->x + output->width) { + double delta = left_x - abs_pos->x; + if (delta < 0) delta = -delta; + if (delta < delta_x || !adjacent) { + delta_x = delta; + adjacent = c; + } + } + // we check both points and pick the closest + if (right_x >= output->x && right_x <= output->x + output->width) { + double delta = right_x - abs_pos->x; + if (delta < 0) delta = -delta; + if (delta < delta_x || !adjacent) { + delta_x = delta; + adjacent = c; + } + } + } + } + dir_text = dir == MOVE_UP ? "above" : "below"; + if (adjacent && delta_x == 0) { + sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (x-position %i)", + adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, + dir_text, output->name, abs_pos->x); + } else if (adjacent) { + // so we end up picking the closest adjacent output because + // there is no directly adjacent to the given position + sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (x-position %i, delta: %.0f)", + adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, + dir_text, output->name, abs_pos->x, delta_x); + } + break; + default: + sway_abort("Function called with invalid argument."); + break; + } + return adjacent; +} + +void get_absolute_position(swayc_t *container, struct wlc_point *point) { + if (!container || !point) + sway_abort("Need container and wlc_point (was %p, %p).", container, point); + + if (container->type == C_OUTPUT) { + // Coordinates are already absolute. + point->x = container->x; + point->y = container->y; + } else { + swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + if (container->type == C_WORKSPACE) { + // Workspace coordinates are actually wrong/arbitrary, but should + // be same as output. + point->x = output->x; + point->y = output->y; + } else { + point->x = output->x + container->x; + point->y = output->y + container->y; + } + } +} + +void get_absolute_center_position(swayc_t *container, struct wlc_point *point) { + get_absolute_position(container, point); + point->x += container->width/2; + point->y += container->height/2; +} + +static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { + swayc_t *a = *(void **)_a; + swayc_t *b = *(void **)_b; + int retval = 0; + + if (isdigit(a->name[0]) && isdigit(b->name[0])) { + int a_num = strtol(a->name, NULL, 10); + int b_num = strtol(b->name, NULL, 10); + retval = (a_num < b_num) ? -1 : (a_num > b_num); + } else if (isdigit(a->name[0])) { + retval = -1; + } else if (isdigit(b->name[0])) { + retval = 1; + } + + return retval; +} + +void sort_workspaces(swayc_t *output) { + list_stable_sort(output->children, sort_workspace_cmp_qsort); +} diff --git a/sway/old/security.c b/sway/old/security.c new file mode 100644 index 00000000..fcd70f9d --- /dev/null +++ b/sway/old/security.c @@ -0,0 +1,228 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include "sway/config.h" +#include "sway/security.h" +#include "log.h" + +static bool validate_ipc_target(const char *program) { + struct stat sb; + + sway_log(L_DEBUG, "Validating IPC target '%s'", program); + + if (!strcmp(program, "*")) { + return true; + } + if (lstat(program, &sb) == -1) { + return false; + } + if (!S_ISREG(sb.st_mode)) { + sway_log(L_ERROR, + "IPC target '%s' MUST be/point at an existing regular file", + program); + return false; + } + if (sb.st_uid != 0) { +#ifdef NDEBUG + sway_log(L_ERROR, "IPC target '%s' MUST be owned by root", program); + return false; +#else + sway_log(L_INFO, "IPC target '%s' MUST be owned by root (waived for debug build)", program); + return true; +#endif + } + if (sb.st_mode & S_IWOTH) { + sway_log(L_ERROR, "IPC target '%s' MUST NOT be world writable", program); + return false; + } + + return true; +} + +struct feature_policy *alloc_feature_policy(const char *program) { + uint32_t default_policy = 0; + + if (!validate_ipc_target(program)) { + return NULL; + } + for (int i = 0; i < config->feature_policies->length; ++i) { + struct feature_policy *policy = config->feature_policies->items[i]; + if (strcmp(policy->program, "*") == 0) { + default_policy = policy->features; + break; + } + } + + struct feature_policy *policy = malloc(sizeof(struct feature_policy)); + if (!policy) { + return NULL; + } + policy->program = strdup(program); + if (!policy->program) { + free(policy); + return NULL; + } + policy->features = default_policy; + + return policy; +} + +struct ipc_policy *alloc_ipc_policy(const char *program) { + uint32_t default_policy = 0; + + if (!validate_ipc_target(program)) { + return NULL; + } + for (int i = 0; i < config->ipc_policies->length; ++i) { + struct ipc_policy *policy = config->ipc_policies->items[i]; + if (strcmp(policy->program, "*") == 0) { + default_policy = policy->features; + break; + } + } + + struct ipc_policy *policy = malloc(sizeof(struct ipc_policy)); + if (!policy) { + return NULL; + } + policy->program = strdup(program); + if (!policy->program) { + free(policy); + return NULL; + } + policy->features = default_policy; + + return policy; +} + +struct command_policy *alloc_command_policy(const char *command) { + struct command_policy *policy = malloc(sizeof(struct command_policy)); + if (!policy) { + return NULL; + } + policy->command = strdup(command); + if (!policy->command) { + free(policy); + return NULL; + } + policy->context = 0; + return policy; +} + +static const char *get_pid_exe(pid_t pid) { +#ifdef __FreeBSD__ + const char *fmt = "/proc/%d/file"; +#else + const char *fmt = "/proc/%d/exe"; +#endif + int pathlen = snprintf(NULL, 0, fmt, pid); + char *path = malloc(pathlen + 1); + if (path) { + snprintf(path, pathlen + 1, fmt, pid); + } + + static char link[2048]; + + ssize_t len = !path ? -1 : readlink(path, link, sizeof(link)); + if (len < 0) { + sway_log(L_INFO, + "WARNING: unable to read %s for security check. Using default policy.", + path); + strcpy(link, "*"); + } else { + link[len] = '\0'; + } + free(path); + + return link; +} + +struct feature_policy *get_feature_policy(const char *name) { + struct feature_policy *policy = NULL; + + for (int i = 0; i < config->feature_policies->length; ++i) { + struct feature_policy *p = config->feature_policies->items[i]; + if (strcmp(p->program, name) == 0) { + policy = p; + break; + } + } + if (!policy) { + policy = alloc_feature_policy(name); + sway_assert(policy, "Unable to allocate security policy"); + if (policy) { + list_add(config->feature_policies, policy); + } + } + return policy; +} + +uint32_t get_feature_policy_mask(pid_t pid) { + uint32_t default_policy = 0; + const char *link = get_pid_exe(pid); + + for (int i = 0; i < config->feature_policies->length; ++i) { + struct feature_policy *policy = config->feature_policies->items[i]; + if (strcmp(policy->program, "*") == 0) { + default_policy = policy->features; + } + if (strcmp(policy->program, link) == 0) { + return policy->features; + } + } + + return default_policy; +} + +uint32_t get_ipc_policy_mask(pid_t pid) { + uint32_t default_policy = 0; + const char *link = get_pid_exe(pid); + + for (int i = 0; i < config->ipc_policies->length; ++i) { + struct ipc_policy *policy = config->ipc_policies->items[i]; + if (strcmp(policy->program, "*") == 0) { + default_policy = policy->features; + } + if (strcmp(policy->program, link) == 0) { + return policy->features; + } + } + + return default_policy; +} + +uint32_t get_command_policy_mask(const char *cmd) { + uint32_t default_policy = 0; + + for (int i = 0; i < config->command_policies->length; ++i) { + struct command_policy *policy = config->command_policies->items[i]; + if (strcmp(policy->command, "*") == 0) { + default_policy = policy->context; + } + if (strcmp(policy->command, cmd) == 0) { + return policy->context; + } + } + + return default_policy; +} + +const char *command_policy_str(enum command_context context) { + switch (context) { + case CONTEXT_ALL: + return "all"; + case CONTEXT_CONFIG: + return "config"; + case CONTEXT_BINDING: + return "binding"; + case CONTEXT_IPC: + return "IPC"; + case CONTEXT_CRITERIA: + return "criteria"; + default: + return "unknown"; + } +} diff --git a/sway/old/workspace.c b/sway/old/workspace.c new file mode 100644 index 00000000..14cde146 --- /dev/null +++ b/sway/old/workspace.c @@ -0,0 +1,373 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include +#include "sway/ipc-server.h" +#include "sway/workspace.h" +#include "sway/layout.h" +#include "sway/container.h" +#include "sway/handlers.h" +#include "sway/config.h" +#include "sway/focus.h" +#include "stringop.h" +#include "util.h" +#include "list.h" +#include "log.h" +#include "ipc.h" + +char *prev_workspace_name = NULL; +struct workspace_by_number_data { + int len; + const char *cset; + const char *name; +}; + +static bool workspace_valid_on_output(const char *output_name, const char *ws_name) { + int i; + for (i = 0; i < config->workspace_outputs->length; ++i) { + struct workspace_output *wso = config->workspace_outputs->items[i]; + if (strcasecmp(wso->workspace, ws_name) == 0) { + if (strcasecmp(wso->output, output_name) != 0) { + return false; + } + } + } + + return true; +} + +char *workspace_next_name(const char *output_name) { + sway_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", output_name); + int i; + int l = 1; + // Scan all workspace bindings to find the next available workspace name, + // if none are found/available then default to a number + struct sway_mode *mode = config->current_mode; + + int order = INT_MAX; + char *target = NULL; + for (i = 0; i < mode->bindings->length; ++i) { + struct sway_binding *binding = mode->bindings->items[i]; + char *cmdlist = strdup(binding->command); + char *dup = cmdlist; + char *name = NULL; + + // workspace n + char *cmd = argsep(&cmdlist, " "); + if (cmdlist) { + name = argsep(&cmdlist, ",;"); + } + + if (strcmp("workspace", cmd) == 0 && name) { + sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", name); + char *_target = strdup(name); + strip_quotes(_target); + while (isspace(*_target)) + _target++; + + // Make sure that the command references an actual workspace + // not a command about workspaces + if (strcmp(_target, "next") == 0 || + strcmp(_target, "prev") == 0 || + strcmp(_target, "next_on_output") == 0 || + strcmp(_target, "prev_on_output") == 0 || + strcmp(_target, "number") == 0 || + strcmp(_target, "back_and_forth") == 0 || + strcmp(_target, "current") == 0) + { + free(_target); + free(dup); + continue; + } + + // Make sure that the workspace doesn't already exist + if (workspace_by_name(_target)) { + free(_target); + free(dup); + continue; + } + + // make sure that the workspace can appear on the given + // output + if (!workspace_valid_on_output(output_name, _target)) { + free(_target); + free(dup); + continue; + } + + if (binding->order < order) { + order = binding->order; + free(target); + target = _target; + sway_log(L_DEBUG, "Workspace: Found free name %s", _target); + } + } + free(dup); + } + if (target != NULL) { + return target; + } + // As a fall back, get the current number of active workspaces + // and return that + 1 for the next workspace's name + int ws_num = root_container.children->length; + if (ws_num >= 10) { + l = 2; + } else if (ws_num >= 100) { + l = 3; + } + char *name = malloc(l + 1); + if (!name) { + sway_log(L_ERROR, "Could not allocate workspace name"); + return NULL; + } + sprintf(name, "%d", ws_num++); + return name; +} + +swayc_t *workspace_create(const char* name) { + swayc_t *parent; + // Search for workspace<->output pair + int i, e = config->workspace_outputs->length; + for (i = 0; i < e; ++i) { + struct workspace_output *wso = config->workspace_outputs->items[i]; + if (strcasecmp(wso->workspace, name) == 0) + { + // Find output to use if it exists + e = root_container.children->length; + for (i = 0; i < e; ++i) { + parent = root_container.children->items[i]; + if (strcmp(parent->name, wso->output) == 0) { + return new_workspace(parent, name); + } + } + break; + } + } + // Otherwise create a new one + parent = get_focused_container(&root_container); + parent = swayc_parent_by_type(parent, C_OUTPUT); + return new_workspace(parent, name); +} + +static bool _workspace_by_name(swayc_t *view, void *data) { + return (view->type == C_WORKSPACE) && + (strcasecmp(view->name, (char *) data) == 0); +} + +swayc_t *workspace_by_name(const char* name) { + if (strcmp(name, "prev") == 0) { + return workspace_prev(); + } + else if (strcmp(name, "prev_on_output") == 0) { + return workspace_output_prev(); + } + else if (strcmp(name, "next") == 0) { + return workspace_next(); + } + else if (strcmp(name, "next_on_output") == 0) { + return workspace_output_next(); + } + else if (strcmp(name, "current") == 0) { + return swayc_active_workspace(); + } + else { + return swayc_by_test(&root_container, _workspace_by_name, (void *) name); + } +} + +static bool _workspace_by_number(swayc_t *view, void *data) { + if (view->type != C_WORKSPACE) { + return false; + } + struct workspace_by_number_data *wbnd = data; + int a = strspn(view->name, wbnd->cset); + return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; +} +swayc_t *workspace_by_number(const char* name) { + struct workspace_by_number_data wbnd = {0, "1234567890", name}; + wbnd.len = strspn(name, wbnd.cset); + if (wbnd.len <= 0) { + return NULL; + } + return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); +} + +/** + * Get the previous or next workspace on the specified output. + * Wraps around at the end and beginning. + * If next is false, the previous workspace is returned, otherwise the next one is returned. + */ +swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { + if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) { + return NULL; + } + + int i; + for (i = 0; i < output->children->length; i++) { + if (output->children->items[i] == output->focused) { + return output->children->items[wrap(i + (next ? 1 : -1), output->children->length)]; + } + } + + // Doesn't happen, at worst the for loop returns the previously active workspace + return NULL; +} + +/** + * Get the previous or next workspace. If the first/last workspace on an output is active, + * proceed to the previous/next output's previous/next workspace. + * If next is false, the previous workspace is returned, otherwise the next one is returned. + */ +swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { + if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) { + return NULL; + } + + swayc_t *current_output = workspace->parent; + int offset = next ? 1 : -1; + int start = next ? 0 : 1; + int end = next ? (current_output->children->length) - 1 : current_output->children->length; + int i; + for (i = start; i < end; i++) { + if (current_output->children->items[i] == workspace) { + return current_output->children->items[i + offset]; + } + } + + // Given workspace is the first/last on the output, jump to the previous/next output + int num_outputs = root_container.children->length; + for (i = 0; i < num_outputs; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *next_output = root_container.children->items[wrap(i + offset, num_outputs)]; + return workspace_output_prev_next_impl(next_output, next); + } + } + + // Doesn't happen, at worst the for loop returns the previously active workspace on the active output + return NULL; +} + +swayc_t *workspace_output_next() { + return workspace_output_prev_next_impl(swayc_active_output(), true); +} + +swayc_t *workspace_next() { + return workspace_prev_next_impl(swayc_active_workspace(), true); +} + +swayc_t *workspace_output_prev() { + return workspace_output_prev_next_impl(swayc_active_output(), false); +} + +swayc_t *workspace_prev() { + return workspace_prev_next_impl(swayc_active_workspace(), false); +} + +bool workspace_switch(swayc_t *workspace) { + if (!workspace) { + return false; + } + swayc_t *active_ws = swayc_active_workspace(); + if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) { + swayc_t *new_ws = workspace_by_name(prev_workspace_name); + workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); + } + + if (!prev_workspace_name + || (strcmp(prev_workspace_name, active_ws->name) + && active_ws != workspace)) { + free(prev_workspace_name); + prev_workspace_name = malloc(strlen(active_ws->name) + 1); + if (!prev_workspace_name) { + sway_log(L_ERROR, "Unable to allocate previous workspace name"); + return false; + } + strcpy(prev_workspace_name, active_ws->name); + } + + // move sticky containers + if (swayc_parent_by_type(active_ws, C_OUTPUT) == swayc_parent_by_type(workspace, C_OUTPUT)) { + // don't change list while traversing it, use intermediate list instead + list_t *stickies = create_list(); + for (int i = 0; i < active_ws->floating->length; i++) { + swayc_t *cont = active_ws->floating->items[i]; + if (cont->sticky) { + list_add(stickies, cont); + } + } + for (int i = 0; i < stickies->length; i++) { + swayc_t *cont = stickies->items[i]; + sway_log(L_DEBUG, "Moving sticky container %p to %p:%s", + cont, workspace, workspace->name); + swayc_t *parent = remove_child(cont); + add_floating(workspace, cont); + // Destroy old container if we need to + destroy_container(parent); + } + list_free(stickies); + } + sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); + if (!set_focused_container(get_focused_view(workspace))) { + return false; + } + swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); + arrange_backgrounds(); + arrange_windows(output, -1, -1); + return true; +} + +swayc_t *workspace_for_pid(pid_t pid) { + int i; + swayc_t *ws = NULL; + struct pid_workspace *pw = NULL; + + sway_log(L_DEBUG, "looking for workspace for pid %d", pid); + + // leaving this here as it's useful for debugging + // sway_log(L_DEBUG, "all pid_workspaces"); + // for (int k = 0; k < config->pid_workspaces->length; k++) { + // pw = config->pid_workspaces->items[k]; + // sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added); + // } + + do { + for (i = 0; i < config->pid_workspaces->length; i++) { + pw = config->pid_workspaces->items[i]; + pid_t *pw_pid = pw->pid; + + if (pid == *pw_pid) { + sway_log(L_DEBUG, "found pid_workspace for pid %d, workspace %s", pid, pw->workspace); + break; // out of for loop + } + + pw = NULL; + } + + if (pw) { + break; // out of do-while loop + } + + pid = get_parent_pid(pid); + // no sense in looking for matches for pid 0. + // also, if pid == getpid(), that is the compositor's + // pid, which definitely isn't helpful + } while (pid > 0 && pid != getpid()); + + if (pw) { + ws = workspace_by_name(pw->workspace); + + if (!ws) { + sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid); + ws = workspace_create(pw->workspace); + } + + list_del(config->pid_workspaces, i); + } + + return ws; +} diff --git a/sway/security.c b/sway/security.c deleted file mode 100644 index fcd70f9d..00000000 --- a/sway/security.c +++ /dev/null @@ -1,228 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include -#include "sway/config.h" -#include "sway/security.h" -#include "log.h" - -static bool validate_ipc_target(const char *program) { - struct stat sb; - - sway_log(L_DEBUG, "Validating IPC target '%s'", program); - - if (!strcmp(program, "*")) { - return true; - } - if (lstat(program, &sb) == -1) { - return false; - } - if (!S_ISREG(sb.st_mode)) { - sway_log(L_ERROR, - "IPC target '%s' MUST be/point at an existing regular file", - program); - return false; - } - if (sb.st_uid != 0) { -#ifdef NDEBUG - sway_log(L_ERROR, "IPC target '%s' MUST be owned by root", program); - return false; -#else - sway_log(L_INFO, "IPC target '%s' MUST be owned by root (waived for debug build)", program); - return true; -#endif - } - if (sb.st_mode & S_IWOTH) { - sway_log(L_ERROR, "IPC target '%s' MUST NOT be world writable", program); - return false; - } - - return true; -} - -struct feature_policy *alloc_feature_policy(const char *program) { - uint32_t default_policy = 0; - - if (!validate_ipc_target(program)) { - return NULL; - } - for (int i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *policy = config->feature_policies->items[i]; - if (strcmp(policy->program, "*") == 0) { - default_policy = policy->features; - break; - } - } - - struct feature_policy *policy = malloc(sizeof(struct feature_policy)); - if (!policy) { - return NULL; - } - policy->program = strdup(program); - if (!policy->program) { - free(policy); - return NULL; - } - policy->features = default_policy; - - return policy; -} - -struct ipc_policy *alloc_ipc_policy(const char *program) { - uint32_t default_policy = 0; - - if (!validate_ipc_target(program)) { - return NULL; - } - for (int i = 0; i < config->ipc_policies->length; ++i) { - struct ipc_policy *policy = config->ipc_policies->items[i]; - if (strcmp(policy->program, "*") == 0) { - default_policy = policy->features; - break; - } - } - - struct ipc_policy *policy = malloc(sizeof(struct ipc_policy)); - if (!policy) { - return NULL; - } - policy->program = strdup(program); - if (!policy->program) { - free(policy); - return NULL; - } - policy->features = default_policy; - - return policy; -} - -struct command_policy *alloc_command_policy(const char *command) { - struct command_policy *policy = malloc(sizeof(struct command_policy)); - if (!policy) { - return NULL; - } - policy->command = strdup(command); - if (!policy->command) { - free(policy); - return NULL; - } - policy->context = 0; - return policy; -} - -static const char *get_pid_exe(pid_t pid) { -#ifdef __FreeBSD__ - const char *fmt = "/proc/%d/file"; -#else - const char *fmt = "/proc/%d/exe"; -#endif - int pathlen = snprintf(NULL, 0, fmt, pid); - char *path = malloc(pathlen + 1); - if (path) { - snprintf(path, pathlen + 1, fmt, pid); - } - - static char link[2048]; - - ssize_t len = !path ? -1 : readlink(path, link, sizeof(link)); - if (len < 0) { - sway_log(L_INFO, - "WARNING: unable to read %s for security check. Using default policy.", - path); - strcpy(link, "*"); - } else { - link[len] = '\0'; - } - free(path); - - return link; -} - -struct feature_policy *get_feature_policy(const char *name) { - struct feature_policy *policy = NULL; - - for (int i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *p = config->feature_policies->items[i]; - if (strcmp(p->program, name) == 0) { - policy = p; - break; - } - } - if (!policy) { - policy = alloc_feature_policy(name); - sway_assert(policy, "Unable to allocate security policy"); - if (policy) { - list_add(config->feature_policies, policy); - } - } - return policy; -} - -uint32_t get_feature_policy_mask(pid_t pid) { - uint32_t default_policy = 0; - const char *link = get_pid_exe(pid); - - for (int i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *policy = config->feature_policies->items[i]; - if (strcmp(policy->program, "*") == 0) { - default_policy = policy->features; - } - if (strcmp(policy->program, link) == 0) { - return policy->features; - } - } - - return default_policy; -} - -uint32_t get_ipc_policy_mask(pid_t pid) { - uint32_t default_policy = 0; - const char *link = get_pid_exe(pid); - - for (int i = 0; i < config->ipc_policies->length; ++i) { - struct ipc_policy *policy = config->ipc_policies->items[i]; - if (strcmp(policy->program, "*") == 0) { - default_policy = policy->features; - } - if (strcmp(policy->program, link) == 0) { - return policy->features; - } - } - - return default_policy; -} - -uint32_t get_command_policy_mask(const char *cmd) { - uint32_t default_policy = 0; - - for (int i = 0; i < config->command_policies->length; ++i) { - struct command_policy *policy = config->command_policies->items[i]; - if (strcmp(policy->command, "*") == 0) { - default_policy = policy->context; - } - if (strcmp(policy->command, cmd) == 0) { - return policy->context; - } - } - - return default_policy; -} - -const char *command_policy_str(enum command_context context) { - switch (context) { - case CONTEXT_ALL: - return "all"; - case CONTEXT_CONFIG: - return "config"; - case CONTEXT_BINDING: - return "binding"; - case CONTEXT_IPC: - return "IPC"; - case CONTEXT_CRITERIA: - return "criteria"; - default: - return "unknown"; - } -} diff --git a/sway/server.c b/sway/server.c index 940f28b3..8892b917 100644 --- a/sway/server.c +++ b/sway/server.c @@ -10,7 +10,6 @@ // TODO WLR: make Xwayland optional #include #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); diff --git a/sway/tree/container.c b/sway/tree/container.c deleted file mode 100644 index 82c0d877..00000000 --- a/sway/tree/container.c +++ /dev/null @@ -1,1024 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include -#include -#include "sway/config.h" -#include "sway/container.h" -#include "sway/workspace.h" -#include "sway/focus.h" -#include "sway/border.h" -#include "sway/layout.h" -#include "sway/input_state.h" -#include "sway/ipc-server.h" -#include "sway/output.h" -#include "sway/view.h" -#include "log.h" -#include "stringop.h" - -#define ASSERT_NONNULL(PTR) \ - sway_assert (PTR, #PTR "must be non-null") - -static swayc_t *new_swayc(enum swayc_types type) { - // next id starts at 1 because 0 is assigned to root_container in layout.c - static size_t next_id = 1; - swayc_t *c = calloc(1, sizeof(swayc_t)); - if (!c) { - return NULL; - } - c->id = next_id++; - c->handle = -1; - c->gaps = -1; - c->layout = L_NONE; - c->workspace_layout = L_NONE; - c->type = type; - c->nb_master = 1; - c->nb_slave_groups = 1; - if (type != C_VIEW) { - c->children = create_list(); - } - return c; -} - -static void free_swayc(swayc_t *cont) { - if (!ASSERT_NONNULL(cont)) { - return; - } - if (cont->children) { - // remove children until there are no more, free_swayc calls - // remove_child, which removes child from this container - while (cont->children->length) { - free_swayc(cont->children->items[0]); - } - list_free(cont->children); - } - if (cont->unmanaged) { - list_free(cont->unmanaged); - } - if (cont->floating) { - while (cont->floating->length) { - free_swayc(cont->floating->items[0]); - } - list_free(cont->floating); - } - if (cont->marks) { - list_foreach(cont->marks, free); - list_free(cont->marks); - } - if (cont->parent) { - remove_child(cont); - } - if (cont->name) { - free(cont->name); - } - if (cont->class) { - free(cont->class); - } - if (cont->instance) { - free(cont->instance); - } - if (cont->app_id) { - free(cont->app_id); - } - if (cont->bg_pid != 0) { - terminate_swaybg(cont->bg_pid); - } - if (cont->border) { - if (cont->border->buffer) { - free(cont->border->buffer); - } - free(cont->border); - } - free(cont); -} - -static void update_root_geometry() { - int width = 0; - int height = 0; - swayc_t *child; - int child_width; - int child_height; - - for (int i = 0; i < root_container.children->length; ++i) { - child = root_container.children->items[i]; - child_width = child->width + child->x; - child_height = child->height + child->y; - if (child_width > width) { - width = child_width; - } - - if (child_height > height) { - height = child_height; - } - } - - root_container.width = width; - root_container.height = height; -} - -// New containers - -swayc_t *new_output(struct sway_output *sway_output) { - struct wlr_box size; - wlr_output_effective_resolution(sway_output->wlr_output, - &size.width, &size.height); - const char *name = sway_output->wlr_output->name; - // Find current outputs to see if this already exists - { - int i, len = root_container.children->length; - for (i = 0; i < len; ++i) { - swayc_t *op = root_container.children->items[i]; - const char *op_name = op->name; - if (op_name && name && strcmp(op_name, name) == 0) { - sway_log(L_DEBUG, "restoring output %p: %s", - sway_output, op_name); - return op; - } - } - } - - struct output_config *oc = NULL, *all = NULL; - int i; - for (i = 0; i < config->output_configs->length; ++i) { - struct output_config *cur = config->output_configs->items[i]; - if (strcasecmp(name, cur->name) == 0) { - sway_log(L_DEBUG, "Matched output config for %s", name); - oc = cur; - } - if (strcasecmp("*", cur->name) == 0) { - sway_log(L_DEBUG, "Matched wildcard output config for %s", name); - all = cur; - } - - if (oc && all) { - break; - } - } - - if (!oc) { - oc = all; - } - - if (oc && !oc->enabled) { - return NULL; - } - - swayc_t *output = new_swayc(C_OUTPUT); - output->_handle.output = sway_output; - output->name = name ? strdup(name) : NULL; - output->width = size.width; - output->height = size.width; - output->unmanaged = create_list(); - output->bg_pid = 0; - - apply_output_config(oc, output); - add_child(&root_container, output); - load_swaybars(); - - // Create workspace - char *ws_name = NULL; - swayc_t *ws = NULL; - - if (name) { - for (i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; - if (strcasecmp(wso->output, name) == 0) { - sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); - // Check if any other workspaces are using this name - if ((ws = workspace_by_name(wso->workspace))) { - // if yes, move those to this output, because they should be here - move_workspace_to(ws, output); - } else if (!ws_name) { - // set a workspace name in case we need to create a default one - ws_name = strdup(wso->workspace); - } - } - } - } - - if (output->children->length == 0) { - if (!ws_name) { - ws_name = workspace_next_name(output->name); - } - // create and initialize default workspace - sway_log(L_DEBUG, "Creating default workspace %s", ws_name); - ws = new_workspace(output, ws_name); - ws->is_focused = true; - } else { - sort_workspaces(output); - set_focused_container(output->children->items[0]); - } - - free(ws_name); - update_root_geometry(); - return output; -} - -swayc_t *new_workspace(swayc_t *output, const char *name) { - if (!ASSERT_NONNULL(output)) { - return NULL; - } - sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle); - swayc_t *workspace = new_swayc(C_WORKSPACE); - - workspace->prev_layout = L_NONE; - workspace->layout = default_layout(output); - workspace->workspace_layout = default_layout(output); - - workspace->x = output->x; - workspace->y = output->y; - workspace->width = output->width; - workspace->height = output->height; - workspace->name = !name ? NULL : strdup(name); - workspace->visible = false; - workspace->floating = create_list(); - - add_child(output, workspace); - sort_workspaces(output); - - return workspace; -} - -swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { - if (!ASSERT_NONNULL(child) - && !sway_assert(!child->is_floating, "cannot create container around floating window")) { - return NULL; - } - swayc_t *cont = new_swayc(C_CONTAINER); - - sway_log(L_DEBUG, "creating container %p around %p", cont, child); - - cont->prev_layout = L_NONE; - cont->layout = layout; - cont->width = child->width; - cont->height = child->height; - cont->x = child->x; - cont->y = child->y; - cont->visible = child->visible; - cont->cached_geometry = child->cached_geometry; - cont->gaps = child->gaps; - - /* Container inherits all of workspaces children, layout and whatnot */ - if (child->type == C_WORKSPACE) { - swayc_t *workspace = child; - // reorder focus - cont->focused = workspace->focused; - workspace->focused = cont; - // set all children focu to container - int i; - for (i = 0; i < workspace->children->length; ++i) { - ((swayc_t *)workspace->children->items[i])->parent = cont; - } - // Swap children - list_t *tmp_list = workspace->children; - workspace->children = cont->children; - cont->children = tmp_list; - // add container to workspace chidren - add_child(workspace, cont); - // give them proper layouts - cont->layout = workspace->workspace_layout; - cont->prev_layout = workspace->prev_layout; - /* TODO: might break shit in move_container!!! workspace->layout = layout; */ - set_focused_container_for(workspace, get_focused_view(workspace)); - } else { // Or is built around container - swayc_t *parent = replace_child(child, cont); - if (parent) { - add_child(cont, child); - } - } - return cont; -} - -swayc_t *new_view(swayc_t *sibling, struct sway_view *view) { - if (!ASSERT_NONNULL(sibling)) { - return NULL; - } - const char *title = view->iface.get_prop(view, VIEW_PROP_TITLE); - swayc_t *swayc = new_swayc(C_VIEW); - sway_log(L_DEBUG, "Adding new view %p:%s to container %p %d", - swayc, title, sibling, sibling ? sibling->type : 0); - // Setup values - swayc->_handle.view = view; - - swayc->name = title ? strdup(title) : NULL; - - const char *class = view->iface.get_prop(view, VIEW_PROP_CLASS); - swayc->class = class ? strdup(class) : NULL; - - const char *instance = view->iface.get_prop(view, VIEW_PROP_INSTANCE); - swayc->instance = instance ? strdup(instance) : NULL; - - const char *app_id = view->iface.get_prop(view, VIEW_PROP_APP_ID); - swayc->app_id = app_id ? strdup(app_id) : NULL; - - swayc->visible = true; - swayc->is_focused = true; - swayc->sticky = false; - swayc->width = 0; - swayc->height = 0; - swayc->desired_width = -1; - swayc->desired_height = -1; - // setup border - swayc->border_type = config->border; - swayc->border_thickness = config->border_thickness; - - swayc->is_floating = false; - - if (sibling->type == C_WORKSPACE) { - // Case of focused workspace, just create as child of it - add_child(sibling, swayc); - } else { - // Regular case, create as sibling of current container - add_sibling(sibling, swayc); - } - return swayc; -} - -swayc_t *new_floating_view(wlc_handle handle) { - if (swayc_active_workspace() == NULL) { - return NULL; - } - const char *title = wlc_view_get_title(handle); - swayc_t *view = new_swayc(C_VIEW); - sway_log(L_DEBUG, "Adding new view %" PRIuPTR ":%x:%s as a floating view", - handle, wlc_view_get_type(handle), title); - // Setup values - view->handle = handle; - view->name = title ? strdup(title) : NULL; - const char *class = wlc_view_get_class(handle); - view->class = class ? strdup(class) : NULL; - const char *instance = wlc_view_get_instance(handle); - view->instance = instance ? strdup(instance) : NULL; - const char *app_id = wlc_view_get_app_id(handle); - view->app_id = app_id ? strdup(app_id) : NULL; - view->visible = true; - view->sticky = false; - - // Set the geometry of the floating view - const struct wlc_geometry *geometry = wlc_view_get_geometry(handle); - - // give it requested geometry, but place in center if possible - // in top left otherwise - if (geometry->size.w != 0) { - view->x = (swayc_active_workspace()->width - geometry->size.w) / 2; - } else { - view->x = 0; - } - if (geometry->size.h != 0) { - view->y = (swayc_active_workspace()->height - geometry->size.h) / 2; - } else { - view->y = 0; - } - - view->width = geometry->size.w; - view->height = geometry->size.h; - - view->desired_width = view->width; - view->desired_height = view->height; - - // setup border - view->border_type = config->floating_border; - view->border_thickness = config->floating_border_thickness; - - view->is_floating = true; - - // Case of focused workspace, just create as child of it - list_add(swayc_active_workspace()->floating, view); - view->parent = swayc_active_workspace(); - if (swayc_active_workspace()->focused == NULL) { - set_focused_container_for(swayc_active_workspace(), view); - } - return view; -} - -void floating_view_sane_size(swayc_t *view) { - // floating_minimum is used as sane value. - // floating_maximum has priority in case of conflict - // TODO: implement total_outputs_dimensions() - if (config->floating_minimum_height != -1 && - view->desired_height < config->floating_minimum_height) { - view->desired_height = config->floating_minimum_height; - } - if (config->floating_minimum_width != -1 && - view->desired_width < config->floating_minimum_width) { - view->desired_width = config->floating_minimum_width; - } - - // if 0 do not resize, only enforce max value - if (config->floating_maximum_height == 0) { - // Missing total_outputs_dimensions() using swayc_active_workspace() - config->floating_maximum_height = swayc_active_workspace()->height; - - } else if (config->floating_maximum_height != -1 && - view->desired_height > config->floating_maximum_height) { - view->desired_height = config->floating_maximum_height; - } - - // if 0 do not resize, only enforce max value - if (config->floating_maximum_width == 0) { - // Missing total_outputs_dimensions() using swayc_active_workspace() - config->floating_maximum_width = swayc_active_workspace()->width; - - } else if (config->floating_maximum_width != -1 && - view->desired_width > config->floating_maximum_width) { - view->desired_width = config->floating_maximum_width; - } - - sway_log(L_DEBUG, "Sane values for view to %d x %d @ %.f, %.f", - view->desired_width, view->desired_height, view->x, view->y); - - return; -} - - -// Destroy container - -swayc_t *destroy_output(swayc_t *output) { - if (!ASSERT_NONNULL(output)) { - return NULL; - } - if (output->children->length > 0) { - // TODO save workspaces when there are no outputs. - // TODO also check if there will ever be no outputs except for exiting - // program - if (root_container.children->length > 1) { - int p = root_container.children->items[0] == output; - // Move workspace from this output to another output - while (output->children->length) { - swayc_t *child = output->children->items[0]; - remove_child(child); - add_child(root_container.children->items[p], child); - } - sort_workspaces(root_container.children->items[p]); - update_visibility(root_container.children->items[p]); - arrange_windows(root_container.children->items[p], -1, -1); - } - } - sway_log(L_DEBUG, "OUTPUT: Destroying output '%" PRIuPTR "'", output->handle); - free_swayc(output); - update_root_geometry(); - return &root_container; -} - -swayc_t *destroy_workspace(swayc_t *workspace) { - if (!ASSERT_NONNULL(workspace)) { - return NULL; - } - - // Do not destroy this if it's the last workspace on this output - swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); - if (output && output->children->length == 1) { - return NULL; - } - - swayc_t *parent = workspace->parent; - // destroy the WS if there are no children - if (workspace->children->length == 0 && workspace->floating->length == 0) { - sway_log(L_DEBUG, "destroying workspace '%s'", workspace->name); - ipc_event_workspace(workspace, NULL, "empty"); - } else { - // Move children to a different workspace on this output - swayc_t *new_workspace = NULL; - int i; - for(i = 0; i < output->children->length; i++) { - if(output->children->items[i] != workspace) { - break; - } - } - new_workspace = output->children->items[i]; - - sway_log(L_DEBUG, "moving children to different workspace '%s' -> '%s'", - workspace->name, new_workspace->name); - - for(i = 0; i < workspace->children->length; i++) { - move_container_to(workspace->children->items[i], new_workspace); - } - - for(i = 0; i < workspace->floating->length; i++) { - move_container_to(workspace->floating->items[i], new_workspace); - } - } - - free_swayc(workspace); - return parent; -} - -swayc_t *destroy_container(swayc_t *container) { - if (!ASSERT_NONNULL(container)) { - return NULL; - } - while (container->children->length == 0 && container->type == C_CONTAINER) { - sway_log(L_DEBUG, "Container: Destroying container '%p'", container); - swayc_t *parent = container->parent; - free_swayc(container); - container = parent; - } - return container; -} - -swayc_t *destroy_view(swayc_t *view) { - if (!ASSERT_NONNULL(view)) { - return NULL; - } - sway_log(L_DEBUG, "Destroying view '%p'", view); - swayc_t *parent = view->parent; - free_swayc(view); - - // Destroy empty containers - if (parent && parent->type == C_CONTAINER) { - return destroy_container(parent); - } - return parent; -} - -// Container lookup - - -swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { - if (!container->children) { - return NULL; - } - // Special case for checking floating stuff - int i; - if (container->type == C_WORKSPACE) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; - if (test(child, data)) { - return child; - } - } - } - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - if (test(child, data)) { - return child; - } else { - swayc_t *res = swayc_by_test(child, test, data); - if (res) { - return res; - } - } - } - return NULL; -} - -static bool test_name(swayc_t *view, void *data) { - if (!view || !view->name) { - return false; - } - return strcmp(view->name, data) == 0; -} - -swayc_t *swayc_by_name(const char *name) { - return swayc_by_test(&root_container, test_name, (void *)name); -} - -swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { - if (!ASSERT_NONNULL(container)) { - return NULL; - } - if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) { - return NULL; - } - do { - container = container->parent; - } while (container && container->type != type); - return container; -} - -swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) { - if (!ASSERT_NONNULL(container)) { - return NULL; - } - if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) { - return NULL; - } - do { - container = container->parent; - } while (container && container->layout != layout); - return container; -} - -swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types type) { - if (!ASSERT_NONNULL(container)) { - return NULL; - } - if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) { - return NULL; - } - do { - container = container->focused; - } while (container && container->type != type); - return container; -} - -swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts layout) { - if (!ASSERT_NONNULL(container)) { - return NULL; - } - if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) { - return NULL; - } - do { - container = container->focused; - } while (container && container->layout != layout); - return container; -} - - -static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) { - if (!parent || !parent->children) { - return NULL; - } - int i, len; - swayc_t **child; - if (parent->type == C_WORKSPACE) { - len = parent->floating->length; - child = (swayc_t **)parent->floating->items; - for (i = 0; i < len; ++i, ++child) { - if ((*child)->handle == handle) { - return *child; - } - } - } - - len = parent->children->length; - child = (swayc_t**)parent->children->items; - for (i = 0; i < len; ++i, ++child) { - if ((*child)->handle == handle) { - return *child; - } else { - swayc_t *res; - if ((res = _swayc_by_handle_helper(handle, *child))) { - return res; - } - } - } - return NULL; -} - -swayc_t *swayc_by_handle(wlc_handle handle) { - return _swayc_by_handle_helper(handle, &root_container); -} - -swayc_t *swayc_active_output(void) { - return root_container.focused; -} - -swayc_t *swayc_active_workspace(void) { - return root_container.focused ? root_container.focused->focused : NULL; -} - -swayc_t *swayc_active_workspace_for(swayc_t *cont) { - if (!cont) { - return NULL; - } - switch (cont->type) { - case C_ROOT: - cont = cont->focused; - /* Fallthrough */ - - case C_OUTPUT: - cont = cont ? cont->focused : NULL; - /* Fallthrough */ - - case C_WORKSPACE: - return cont; - - default: - return swayc_parent_by_type(cont, C_WORKSPACE); - } -} - -static bool pointer_test(swayc_t *view, void *_origin) { - const struct wlc_point *origin = _origin; - // Determine the output that the view is under - swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT); - if (origin->x >= view->x && origin->y >= view->y - && origin->x < view->x + view->width && origin->y < view->y + view->height - && view->visible && parent == root_container.focused) { - return true; - } - return false; -} - -swayc_t *container_under_pointer(void) { - // root.output->workspace - if (!root_container.focused) { - return NULL; - } - swayc_t *lookup = root_container.focused; - // Case of empty workspace - if (lookup->children && !lookup->unmanaged) { - return NULL; - } - double x, y; - wlc_pointer_get_position_v2(&x, &y); - struct wlc_point origin = { .x = x, .y = y }; - - while (lookup && lookup->type != C_VIEW) { - int i; - int len; - for (int _i = 0; lookup->unmanaged && _i < lookup->unmanaged->length; ++_i) { - wlc_handle *handle = lookup->unmanaged->items[_i]; - const struct wlc_geometry *geo = wlc_view_get_geometry(*handle); - if (origin.x >= geo->origin.x && origin.y >= geo->origin.y - && origin.x < geo->origin.x + (int)geo->size.w - && origin.y < geo->origin.y + (int)geo->size.h) { - // Hack: we force focus upon unmanaged views here - wlc_view_focus(*handle); - return NULL; - } - } - // if tabbed/stacked go directly to focused container, otherwise search - // children - if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) { - lookup = lookup->focused; - continue; - } - // if workspace, search floating - if (lookup->type == C_WORKSPACE) { - i = len = lookup->floating->length; - bool got_floating = false; - while (--i > -1) { - if (pointer_test(lookup->floating->items[i], &origin)) { - lookup = lookup->floating->items[i]; - got_floating = true; - break; - } - } - if (got_floating) { - continue; - } - } - // search children - len = lookup->children->length; - for (i = 0; i < len; ++i) { - if (pointer_test(lookup->children->items[i], &origin)) { - lookup = lookup->children->items[i]; - break; - } - } - // when border and titles are done, this could happen - if (i == len) { - break; - } - } - return lookup; -} - -swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), const void *data) { - if (container->children == NULL || container->children->length == 0) { - return NULL; - } - - swayc_t *con; - if (container->type == C_WORKSPACE) { - for (int i = 0; i < container->floating->length; ++i) { - con = container->floating->items[i]; - if (f(con, data)) { - return con; - } - con = container_find(con, f, data); - if (con != NULL) { - return con; - } - } - } - - for (int i = 0; i < container->children->length; ++i) { - con = container->children->items[i]; - if (f(con, data)) { - return con; - } - - con = container_find(con, f, data); - if (con != NULL) { - return con; - } - } - - return NULL; -} - -// Container information - -bool swayc_is_fullscreen(swayc_t *view) { - return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN); -} - -bool swayc_is_active(swayc_t *view) { - return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_ACTIVATED); -} - -bool swayc_is_parent_of(swayc_t *parent, swayc_t *child) { - while (child != &root_container) { - child = child->parent; - if (child == parent) { - return true; - } - } - return false; -} - -bool swayc_is_child_of(swayc_t *child, swayc_t *parent) { - return swayc_is_parent_of(parent, child); -} - -bool swayc_is_empty_workspace(swayc_t *container) { - return container->type == C_WORKSPACE && container->children->length == 0; -} - -int swayc_gap(swayc_t *container) { - if (container->type == C_VIEW || container->type == C_CONTAINER) { - return container->gaps >= 0 ? container->gaps : config->gaps_inner; - } else if (container->type == C_WORKSPACE) { - int base = container->gaps >= 0 ? container->gaps : config->gaps_outer; - if (config->edge_gaps && !(config->smart_gaps && container->children->length == 1)) { - // the inner gap is created via a margin around each window which - // is half the gap size, so the workspace also needs half a gap - // size to make the outermost gap the same size (excluding the - // actual "outer gap" size which is handled independently) - return base + config->gaps_inner / 2; - } else if (config->smart_gaps && container->children->length == 1) { - return 0; - } else { - return base; - } - } else { - return 0; - } -} - -// Mapping - -void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { - if (container) { - int i; - if (container->children) { - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - container_map(child, f, data); - } - } - if (container->floating) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; - container_map(child, f, data); - } - } - f(container, data); - } -} - -void update_visibility_output(swayc_t *container, wlc_handle output) { - // Inherit visibility - swayc_t *parent = container->parent; - container->visible = parent->visible; - // special cases where visibility depends on focus - if (parent->type == C_OUTPUT || parent->layout == L_TABBED || - parent->layout == L_STACKED) { - container->visible = parent->focused == container && parent->visible; - } - // Set visibility and output for view - if (container->type == C_VIEW) { - wlc_view_set_output(container->handle, output); - wlc_view_set_mask(container->handle, container->visible ? VISIBLE : 0); - } - // Update visibility for children - else { - if (container->children) { - int i, len = container->children->length; - for (i = 0; i < len; ++i) { - update_visibility_output(container->children->items[i], output); - } - } - if (container->floating) { - int i, len = container->floating->length; - for (i = 0; i < len; ++i) { - update_visibility_output(container->floating->items[i], output); - } - } - } -} - -void update_visibility(swayc_t *container) { - if (!container) return; - switch (container->type) { - case C_ROOT: - container->visible = true; - if (container->children) { - int i, len = container->children->length; - for (i = 0; i < len; ++i) { - update_visibility(container->children->items[i]); - } - } - return; - - case C_OUTPUT: - container->visible = true; - if (container->children) { - int i, len = container->children->length; - for (i = 0; i < len; ++i) { - update_visibility_output(container->children->items[i], container->handle); - } - } - return; - - default: - { - swayc_t *op = swayc_parent_by_type(container, C_OUTPUT); - update_visibility_output(container, op->handle); - } - } -} - -void set_gaps(swayc_t *view, void *_data) { - int *data = _data; - if (!ASSERT_NONNULL(view)) { - return; - } - if (view->type == C_WORKSPACE || view->type == C_VIEW) { - view->gaps = *data; - } -} - -void add_gaps(swayc_t *view, void *_data) { - int *data = _data; - if (!ASSERT_NONNULL(view)) { - return; - } - if (view->type == C_WORKSPACE || view->type == C_VIEW) { - if ((view->gaps += *data) < 0) { - view->gaps = 0; - } - } -} - -static void close_view(swayc_t *container, void *data) { - if (container->type == C_VIEW) { - wlc_view_close(container->handle); - } -} - -void close_views(swayc_t *container) { - container_map(container, close_view, NULL); -} - -swayc_t *swayc_tabbed_stacked_ancestor(swayc_t *view) { - swayc_t *parent = NULL; - if (!ASSERT_NONNULL(view)) { - return NULL; - } - while (view->type != C_WORKSPACE && view->parent && view->parent->type != C_WORKSPACE) { - view = view->parent; - if (view->layout == L_TABBED || view->layout == L_STACKED) { - parent = view; - } - } - - return parent; -} - -swayc_t *swayc_tabbed_stacked_parent(swayc_t *con) { - if (!ASSERT_NONNULL(con)) { - return NULL; - } - if (con->parent && (con->parent->layout == L_TABBED || con->parent->layout == L_STACKED)) { - return con->parent; - } - return NULL; -} - -swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { - // if layout change modifies the auto layout's major axis, swap width and height - // to preserve current ratios. - if (is_auto_layout(layout) && is_auto_layout(container->layout)) { - enum swayc_layouts prev_major = - container->layout == L_AUTO_LEFT || container->layout == L_AUTO_RIGHT - ? L_HORIZ : L_VERT; - enum swayc_layouts new_major = - layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT - ? L_HORIZ : L_VERT; - if (new_major != prev_major) { - for (int i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - double h = child->height; - child->height = child->width; - child->width = h; - } - } - } - if (container->type == C_WORKSPACE) { - container->workspace_layout = layout; - if (layout == L_HORIZ || layout == L_VERT || is_auto_layout(layout)) { - container->layout = layout; - } - } else { - container->layout = layout; - } - return container; -} diff --git a/sway/tree/criteria.c b/sway/tree/criteria.c deleted file mode 100644 index e8978ebe..00000000 --- a/sway/tree/criteria.c +++ /dev/null @@ -1,451 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include "sway/criteria.h" -#include "sway/container.h" -#include "sway/config.h" -#include "stringop.h" -#include "list.h" -#include "log.h" - -enum criteria_type { // *must* keep in sync with criteria_strings[] - CRIT_CLASS, - CRIT_CON_ID, - CRIT_CON_MARK, - CRIT_FLOATING, - CRIT_ID, - CRIT_INSTANCE, - CRIT_TILING, - CRIT_TITLE, - CRIT_URGENT, - CRIT_WINDOW_ROLE, - CRIT_WINDOW_TYPE, - CRIT_WORKSPACE, - CRIT_LAST -}; - -static const char * const criteria_strings[CRIT_LAST] = { - [CRIT_CLASS] = "class", - [CRIT_CON_ID] = "con_id", - [CRIT_CON_MARK] = "con_mark", - [CRIT_FLOATING] = "floating", - [CRIT_ID] = "id", - [CRIT_INSTANCE] = "instance", - [CRIT_TILING] = "tiling", - [CRIT_TITLE] = "title", - [CRIT_URGENT] = "urgent", // either "latest" or "oldest" ... - [CRIT_WINDOW_ROLE] = "window_role", - [CRIT_WINDOW_TYPE] = "window_type", - [CRIT_WORKSPACE] = "workspace" -}; - -/** - * A single criteria token (ie. value/regex pair), - * e.g. 'class="some class regex"'. - */ -struct crit_token { - enum criteria_type type; - pcre *regex; - char *raw; -}; - -static void free_crit_token(struct crit_token *crit) { - pcre_free(crit->regex); - free(crit->raw); - free(crit); -} - -static void free_crit_tokens(list_t *crit_tokens) { - for (int i = 0; i < crit_tokens->length; i++) { - free_crit_token(crit_tokens->items[i]); - } - list_free(crit_tokens); -} - -// Extracts criteria string from its brackets. Returns new (duplicate) -// substring. -static char *criteria_from(const char *arg) { - char *criteria = NULL; - if (*arg == '[') { - criteria = strdup(arg + 1); - } else { - criteria = strdup(arg); - } - - int last = strlen(criteria) - 1; - if (criteria[last] == ']') { - criteria[last] = '\0'; - } - return criteria; -} - -// Return instances of c found in str. -static int countchr(char *str, char c) { - int found = 0; - for (int i = 0; str[i]; i++) { - if (str[i] == c) { - ++found; - } - } - return found; -} - -// criteria_str is e.g. '[class="some class regex" instance="instance name"]'. -// -// Will create array of pointers in buf, where first is duplicate of given -// string (must be freed) and the rest are pointers to names and values in the -// base string (every other, naturally). argc will be populated with the length -// of buf. -// -// Returns error string or NULL if successful. -static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str) { - sway_log(L_DEBUG, "Parsing criteria: '%s'", criteria_str); - char *base = criteria_from(criteria_str); - char *head = base; - char *namep = head; // start of criteria name - char *valp = NULL; // start of value - - // We're going to place EOS markers where we need to and fill up an array - // of pointers to the start of each token (either name or value). - int pairs = countchr(base, '='); - int max_tokens = pairs * 2 + 1; // this gives us at least enough slots - - char **argv = *buf = calloc(max_tokens, sizeof(char*)); - argv[0] = base; // this needs to be freed by caller - bool quoted = true; - - *argc = 1; // uneven = name, even = value - while (*head && *argc < max_tokens) { - if (namep != head && *(head - 1) == '\\') { - // escaped character: don't try to parse this - } else if (*head == '=' && namep != head) { - if (*argc % 2 != 1) { - // we're not expecting a name - return strdup("Unable to parse criteria: " - "Found out of place equal sign"); - } else { - // name ends here - char *end = head; // don't want to rewind the head - while (*(end - 1) == ' ') { - --end; - } - *end = '\0'; - if (*(namep) == ' ') { - namep = strrchr(namep, ' ') + 1; - } - argv[*argc] = namep; - *argc += 1; - } - } else if (*head == '"') { - if (*argc % 2 != 0) { - // we're not expecting a value - return strdup("Unable to parse criteria: " - "Found quoted value where it was not expected"); - } else if (!valp) { // value starts here - valp = head + 1; - quoted = true; - } else { - // value ends here - argv[*argc] = valp; - *argc += 1; - *head = '\0'; - valp = NULL; - namep = head + 1; - } - } else if (*argc % 2 == 0 && *head != ' ') { - // parse unquoted values - if (!valp) { - quoted = false; - valp = head; // value starts here - } - } else if (valp && !quoted && *head == ' ') { - // value ends here - argv[*argc] = valp; - *argc += 1; - *head = '\0'; - valp = NULL; - namep = head + 1; - } - head++; - } - - // catch last unquoted value if needed - if (valp && !quoted && !*head) { - argv[*argc] = valp; - *argc += 1; - } - - return NULL; -} - -// Returns error string on failure or NULL otherwise. -static char *parse_criteria_name(enum criteria_type *type, char *name) { - *type = CRIT_LAST; - for (int i = 0; i < CRIT_LAST; i++) { - if (strcmp(criteria_strings[i], name) == 0) { - *type = (enum criteria_type) i; - break; - } - } - if (*type == CRIT_LAST) { - const char *fmt = "Criteria type '%s' is invalid or unsupported."; - int len = strlen(name) + strlen(fmt) - 1; - char *error = malloc(len); - snprintf(error, len, fmt, name); - return error; - } else if (*type == CRIT_URGENT || *type == CRIT_WINDOW_ROLE || - *type == CRIT_WINDOW_TYPE) { - // (we're just being helpful here) - const char *fmt = "\"%s\" criteria currently unsupported, " - "no window will match this"; - int len = strlen(fmt) + strlen(name) - 1; - char *error = malloc(len); - snprintf(error, len, fmt, name); - return error; - } - return NULL; -} - -// Returns error string on failure or NULL otherwise. -static char *generate_regex(pcre **regex, char *value) { - const char *reg_err; - int offset; - - *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL); - - if (!*regex) { - const char *fmt = "Regex compilation (for '%s') failed: %s"; - int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; - char *error = malloc(len); - snprintf(error, len, fmt, value, reg_err); - return error; - } - return NULL; -} - -// Test whether the criterion corresponds to the currently focused window -static bool crit_is_focused(const char *value) { - return !strcmp(value, "focused") || !strcmp(value, "__focused__"); -} - -// Populate list with crit_tokens extracted from criteria string, returns error -// string or NULL if successful. -char *extract_crit_tokens(list_t *tokens, const char * const criteria) { - int argc; - char **argv = NULL, *error = NULL; - if ((error = crit_tokens(&argc, &argv, criteria))) { - goto ect_cleanup; - } - for (int i = 1; i + 1 < argc; i += 2) { - char* name = argv[i], *value = argv[i + 1]; - struct crit_token *token = calloc(1, sizeof(struct crit_token)); - token->raw = strdup(value); - - if ((error = parse_criteria_name(&token->type, name))) { - free_crit_token(token); - goto ect_cleanup; - } else if (token->type == CRIT_URGENT || crit_is_focused(value)) { - sway_log(L_DEBUG, "%s -> \"%s\"", name, value); - list_add(tokens, token); - } else if((error = generate_regex(&token->regex, value))) { - free_crit_token(token); - goto ect_cleanup; - } else { - sway_log(L_DEBUG, "%s -> /%s/", name, value); - list_add(tokens, token); - } - } -ect_cleanup: - free(argv[0]); // base string - free(argv); - return error; -} - -static int regex_cmp(const char *item, const pcre *regex) { - return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); -} - -// test a single view if it matches list of criteria tokens (all of them). -static bool criteria_test(swayc_t *cont, list_t *tokens) { - if (cont->type != C_VIEW) { - return false; - } - int matches = 0; - for (int i = 0; i < tokens->length; i++) { - struct crit_token *crit = tokens->items[i]; - switch (crit->type) { - case CRIT_CLASS: - if (!cont->class) { - // ignore - } else if (crit_is_focused(crit->raw)) { - swayc_t *focused = get_focused_view(&root_container); - if (focused->class && strcmp(cont->class, focused->class) == 0) { - matches++; - } - } else if (crit->regex && regex_cmp(cont->class, crit->regex) == 0) { - matches++; - } - break; - case CRIT_CON_ID: { - char *endptr; - size_t crit_id = strtoul(crit->raw, &endptr, 10); - - if (*endptr == 0 && cont->id == crit_id) { - ++matches; - } - break; - } - case CRIT_CON_MARK: - if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) { - // Make sure it isn't matching the NUL string - if ((strcmp(crit->raw, "") == 0) == (list_seq_find(cont->marks, (int (*)(const void *, const void *))strcmp, "") != -1)) { - ++matches; - } - } - break; - case CRIT_FLOATING: - if (cont->is_floating) { - matches++; - } - break; - case CRIT_ID: - if (!cont->app_id) { - // ignore - } else if (crit->regex && regex_cmp(cont->app_id, crit->regex) == 0) { - matches++; - } - break; - case CRIT_INSTANCE: - if (!cont->instance) { - // ignore - } else if (crit_is_focused(crit->raw)) { - swayc_t *focused = get_focused_view(&root_container); - if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { - matches++; - } - } else if (crit->regex && regex_cmp(cont->instance, crit->regex) == 0) { - matches++; - } - break; - case CRIT_TILING: - if (!cont->is_floating) { - matches++; - } - break; - case CRIT_TITLE: - if (!cont->name) { - // ignore - } else if (crit_is_focused(crit->raw)) { - swayc_t *focused = get_focused_view(&root_container); - if (focused->name && strcmp(cont->name, focused->name) == 0) { - matches++; - } - } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) { - matches++; - } - break; - case CRIT_URGENT: // "latest" or "oldest" - break; - case CRIT_WINDOW_ROLE: - break; - case CRIT_WINDOW_TYPE: - // TODO wlc indeed exposes this information - break; - case CRIT_WORKSPACE: ; - swayc_t *cont_ws = swayc_parent_by_type(cont, C_WORKSPACE); - if (!cont_ws || !cont_ws->name) { - // ignore - } else if (crit_is_focused(crit->raw)) { - swayc_t *focused_ws = swayc_active_workspace(); - if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { - matches++; - } - } else if (crit->regex && regex_cmp(cont_ws->name, crit->regex) == 0) { - matches++; - } - break; - default: - sway_abort("Invalid criteria type (%i)", crit->type); - break; - } - } - return matches == tokens->length; -} - -int criteria_cmp(const void *a, const void *b) { - if (a == b) { - return 0; - } else if (!a) { - return -1; - } else if (!b) { - return 1; - } - const struct criteria *crit_a = a, *crit_b = b; - int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); - if (cmp != 0) { - return cmp; - } - return lenient_strcmp(crit_a->crit_raw, crit_b->crit_raw); -} - -void free_criteria(struct criteria *crit) { - if (crit->tokens) { - free_crit_tokens(crit->tokens); - } - if (crit->cmdlist) { - free(crit->cmdlist); - } - if (crit->crit_raw) { - free(crit->crit_raw); - } - free(crit); -} - -bool criteria_any(swayc_t *cont, list_t *criteria) { - for (int i = 0; i < criteria->length; i++) { - struct criteria *bc = criteria->items[i]; - if (criteria_test(cont, bc->tokens)) { - return true; - } - } - return false; -} - -list_t *criteria_for(swayc_t *cont) { - list_t *criteria = config->criteria, *matches = create_list(); - for (int i = 0; i < criteria->length; i++) { - struct criteria *bc = criteria->items[i]; - if (criteria_test(cont, bc->tokens)) { - list_add(matches, bc); - } - } - return matches; -} - -struct list_tokens { - list_t *list; - list_t *tokens; -}; - -static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) { - if (criteria_test(container, list_tokens->tokens)) { - list_add(list_tokens->list, container); - } -} - -list_t *container_for(list_t *tokens) { - struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; - - container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens); - - for (int i = 0; i < scratchpad->length; ++i) { - swayc_t *c = scratchpad->items[i]; - if (criteria_test(c, tokens)) { - list_add(list_tokens.list, c); - } - } - - return list_tokens.list; -} diff --git a/sway/tree/focus.c b/sway/tree/focus.c deleted file mode 100644 index 66f7ee17..00000000 --- a/sway/tree/focus.c +++ /dev/null @@ -1,278 +0,0 @@ -#include "stdbool.h" -#include -#include "sway/focus.h" -#include "sway/workspace.h" -#include "sway/layout.h" -#include "sway/config.h" -#include "sway/input_state.h" -#include "sway/ipc-server.h" -#include "sway/border.h" -#include "log.h" - -bool locked_container_focus = false; -bool suspend_workspace_cleanup = false; - -// switches parent focus to c. will switch it accordingly -static void update_focus(swayc_t *c) { - // Handle if focus switches - swayc_t *parent = c->parent; - if (!parent) return; - if (parent->focused != c) { - // Get previous focus - swayc_t *prev = parent->focused; - // Set new focus - parent->focused = c; - - switch (c->type) { - // Shouldn't happen - case C_ROOT: return; - - // Case where output changes - case C_OUTPUT: - wlc_output_focus(c->handle); - break; - - // Case where workspace changes - case C_WORKSPACE: - if (prev) { - ipc_event_workspace(prev, c, "focus"); - - // if the old workspace has no children, destroy it - if(prev->children->length == 0 && prev->floating->length == 0 && !suspend_workspace_cleanup) { - destroy_workspace(prev); - } else { - // update visibility of old workspace - update_visibility(prev); - } - } - // Update visibility of newly focused workspace - update_visibility(c); - break; - - default: - case C_VIEW: - case C_CONTAINER: - break; - } - } -} - -bool move_focus(enum movement_direction direction) { - swayc_t *old_view = get_focused_container(&root_container); - swayc_t *new_view = get_swayc_in_direction(old_view, direction); - if (!new_view) { - return false; - } else if (new_view->type == C_ROOT) { - sway_log(L_DEBUG, "Not setting focus above the workspace level"); - return false; - } else if (new_view->type == C_OUTPUT) { - return set_focused_container(swayc_active_workspace_for(new_view)); - } else if (direction == MOVE_PARENT || direction == MOVE_CHILD) { - return set_focused_container(new_view); - } else if (config->mouse_warping) { - swayc_t *old_op = old_view->type == C_OUTPUT ? - old_view : swayc_parent_by_type(old_view, C_OUTPUT); - swayc_t *focused = get_focused_view(new_view); - if (set_focused_container(focused)) { - if (old_op != swayc_active_output() && focused && focused->type == C_VIEW) { - center_pointer_on(focused); - } - return true; - } - } else { - return set_focused_container(get_focused_view(new_view)); - } - return false; -} - -swayc_t *get_focused_container(swayc_t *parent) { - if (!parent) { - return swayc_active_workspace(); - } - while (!parent->is_focused && parent->focused) { - parent = parent->focused; - } - return parent; -} - -bool set_focused_container(swayc_t *c) { - if (locked_container_focus || !c || !c->parent) { - return false; - } - - // current ("old") workspace for sending workspace change event later - swayc_t *old_ws = swayc_active_workspace(); - // keep track of child count so we can determine if it gets destroyed - int old_ws_child_count = 0; - if (old_ws) { - old_ws_child_count = old_ws->children->length + old_ws->floating->length; - } - - // current ("old") focused container - swayc_t *old_focus = get_focused_container(&root_container); - // if old_focus is a workspace, then it's the same workspace as - // old_ws, and we'll need to null its pointer too, since it will - // be destroyed in the update_focus() call - bool old_focus_was_ws = (old_focus->type == C_WORKSPACE); - - // workspace of new focused container - swayc_t *workspace = swayc_active_workspace_for(c); - - if (swayc_is_fullscreen(get_focused_container(workspace))) { - // if switching to a workspace with a fullscreen view, - // focus on the fullscreen view - c = get_focused_container(workspace); - } - - swayc_log(L_DEBUG, c, "Setting focus to %p:%" PRIuPTR, c, c->handle); - - if (c->type == C_VIEW) { - // dispatch a window event - ipc_event_window(c, "focus"); - } - - // update the global pointer - current_focus = c; - - // update container focus from here to root, making necessary changes along - // the way - swayc_t *p = c; - if (p->type != C_OUTPUT && p->type != C_ROOT) { - p->is_focused = true; - } - while (p != &root_container) { - update_focus(p); - p = p->parent; - p->is_focused = false; - } - - if (old_focus_was_ws && old_ws_child_count == 0) { - // this workspace was destroyed in update_focus(), so null the pointers - old_focus = NULL; - old_ws = NULL; - } - - if (!(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) { - if (old_focus) { - if (old_focus->type == C_VIEW) { - wlc_view_set_state(old_focus->handle, WLC_BIT_ACTIVATED, false); - } - update_container_border(old_focus); - } - if (c->type == C_VIEW) { - wlc_view_set_state(c->handle, WLC_BIT_ACTIVATED, true); - } - /* TODO WLR - if (!desktop_shell.is_locked) { - // If the system is locked, we do everything _but_ actually setting - // focus. This includes making our internals think that this view is - // focused. - wlc_view_focus(c->handle); - } - */ - if (c->parent->layout != L_TABBED && c->parent->layout != L_STACKED) { - update_container_border(c); - } - - swayc_t *parent = swayc_tabbed_stacked_ancestor(c); - if (parent != NULL) { - arrange_backgrounds(); - arrange_windows(parent, -1, -1); - } - } - - if (old_ws != workspace) { - // old_ws might be NULL here but that's ok - ipc_event_workspace(old_ws, workspace, "focus"); - } - - return true; -} - -bool set_focused_container_for(swayc_t *a, swayc_t *c) { - if (locked_container_focus || !c) { - return false; - } - swayc_t *find = c; - while (find != a && (find = find->parent)) { - if (find == &root_container) { - return false; - } - } - - // Get workspace for c, get that workspaces current focused container. - swayc_t *workspace = swayc_active_workspace_for(c); - swayc_t *focused = get_focused_view(workspace); - // if the workspace we are changing focus to has a fullscreen view return - if (swayc_is_fullscreen(focused) && c != focused) { - return false; - } - - // Check if we are changing a parent container that will see change - bool effective = true; - while (find != &root_container) { - if (find->parent->focused != find) { - effective = false; - } - find = find->parent; - } - if (effective) { - // Go to set_focused_container - return set_focused_container(c); - } - - sway_log(L_DEBUG, "Setting focus for %p:%" PRIuPTR " to %p:%" PRIuPTR, - a, a->handle, c, c->handle); - - c->is_focused = true; - swayc_t *p = c; - while (p != a) { - update_focus(p); - p = p->parent; - p->is_focused = false; - } - return true; -} - -swayc_t *get_focused_view(swayc_t *parent) { - swayc_t *c = parent; - while (c && c->type != C_VIEW) { - if (c->type == C_WORKSPACE && c->focused == NULL) { - return c; - } - c = c->focused; - } - if (c == NULL) { - c = swayc_active_workspace_for(parent); - } - return c; -} - -swayc_t *get_focused_float(swayc_t *ws) { - if(!sway_assert(ws->type == C_WORKSPACE, "must be of workspace type")) { - ws = swayc_active_workspace(); - } - if (ws->floating->length) { - return ws->floating->items[ws->floating->length - 1]; - } - return NULL; -} - -swayc_t *get_focused_view_include_floating(swayc_t *parent) { - swayc_t *c = parent; - swayc_t *f = NULL; - - while (c && c->type != C_VIEW) { - if (c->type == C_WORKSPACE && c->focused == NULL) { - return ((f = get_focused_float(c))) ? f : c; - } - - c = c->focused; - } - - if (c == NULL) { - c = swayc_active_workspace_for(parent); - } - - return c; -} diff --git a/sway/tree/layout.c b/sway/tree/layout.c deleted file mode 100644 index 22f81688..00000000 --- a/sway/tree/layout.c +++ /dev/null @@ -1,1773 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include "sway/config.h" -#include "sway/container.h" -#include "sway/workspace.h" -#include "sway/focus.h" -#include "sway/output.h" -#include "sway/ipc-server.h" -#include "sway/border.h" -#include "sway/layout.h" -#include "list.h" -#include "log.h" - -swayc_t root_container; -swayc_t *current_focus; -list_t *scratchpad; - -int min_sane_h = 60; -int min_sane_w = 100; - -void init_layout(void) { - root_container.id = 0; // normally assigned in new_swayc() - root_container.type = C_ROOT; - root_container.layout = L_NONE; - root_container.name = strdup("root"); - root_container.children = create_list(); - root_container.handle = -1; - root_container.visible = true; - current_focus = &root_container; - scratchpad = create_list(); -} - -int index_child(const swayc_t *child) { - swayc_t *parent = child->parent; - int i, len; - if (!child->is_floating) { - len = parent->children->length; - for (i = 0; i < len; ++i) { - if (parent->children->items[i] == child) { - break; - } - } - } else { - len = parent->floating->length; - for (i = 0; i < len; ++i) { - if (parent->floating->items[i] == child) { - break; - } - } - } - if (!sway_assert(i < len, "Stray container")) { - return -1; - } - return i; -} - -void add_child(swayc_t *parent, swayc_t *child) { - sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, - child->width, child->height, parent, parent->type, parent->width, parent->height); - list_add(parent->children, child); - child->parent = parent; - // set focus for this container - if (!parent->focused) { - parent->focused = child; - } - if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { - child = new_container(child, parent->workspace_layout); - } -} - -static double *get_height(swayc_t *cont) { - return &cont->height; -} - -static double *get_width(swayc_t *cont) { - return &cont->width; -} - -void insert_child(swayc_t *parent, swayc_t *child, int index) { - if (index > parent->children->length) { - index = parent->children->length; - } - if (index < 0) { - index = 0; - } - list_insert(parent->children, index, child); - child->parent = parent; - if (!parent->focused) { - parent->focused = child; - } - if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { - child = new_container(child, parent->workspace_layout); - } - if (is_auto_layout(parent->layout)) { - /* go through each group, adjust the size of the first child of each group */ - double *(*get_maj_dim)(swayc_t *cont); - double *(*get_min_dim)(swayc_t *cont); - if (parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT) { - get_maj_dim = get_width; - get_min_dim = get_height; - } else { - get_maj_dim = get_height; - get_min_dim = get_width; - } - for (int i = index; i < parent->children->length;) { - int start = auto_group_start_index(parent, i); - int end = auto_group_end_index(parent, i); - swayc_t *first = parent->children->items[start]; - if (start + 1 < parent->children->length) { - /* preserve the group's dimension along major axis */ - *get_maj_dim(first) = *get_maj_dim(parent->children->items[start + 1]); - } else { - /* new group, let the apply_layout handle it */ - first->height = first->width = 0; - break; - } - double remaining = *get_min_dim(parent); - for (int j = end - 1; j > start; --j) { - swayc_t *sibling = parent->children->items[j]; - if (sibling == child) { - /* the inserted child won't yet have its minor - dimension set */ - remaining -= *get_min_dim(parent) / (end - start); - } else { - remaining -= *get_min_dim(sibling); - } - } - *get_min_dim(first) = remaining; - i = end; - } - } -} - -void add_floating(swayc_t *ws, swayc_t *child) { - sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, - child->width, child->height, ws, ws->type, ws->width, ws->height); - if (!sway_assert(ws->type == C_WORKSPACE, "Must be of workspace type")) { - return; - } - list_add(ws->floating, child); - child->parent = ws; - child->is_floating = true; - if (!ws->focused) { - ws->focused = child; - } - ipc_event_window(child, "floating"); -} - -swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { - swayc_t *parent = fixed->parent; - if (fixed->is_floating) { - if (active->is_floating) { - int i = index_child(fixed); - list_insert(parent->floating, i + 1, active); - } else { - list_add(parent->children, active); - } - } else { - if (active->is_floating) { - list_add(parent->floating, active); - } else { - int i = index_child(fixed); - if (is_auto_layout(parent->layout)) { - list_add(parent->children, active); - } else { - list_insert(parent->children, i + 1, active); - } - } - } - active->parent = parent; - // focus new child - parent->focused = active; - return active->parent; -} - -swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { - swayc_t *parent = child->parent; - if (parent == NULL) { - return NULL; - } - int i = index_child(child); - if (child->is_floating) { - parent->floating->items[i] = new_child; - } else { - parent->children->items[i] = new_child; - } - // Set parent and focus for new_child - new_child->parent = child->parent; - if (child->parent->focused == child) { - child->parent->focused = new_child; - } - child->parent = NULL; - - // Set geometry for new child - new_child->x = child->x; - new_child->y = child->y; - new_child->width = child->width; - new_child->height = child->height; - - // reset geometry for child - child->width = 0; - child->height = 0; - - // deactivate child - if (child->type == C_VIEW) { - wlc_view_set_state(child->handle, WLC_BIT_ACTIVATED, false); - } - return parent; -} - -swayc_t *remove_child(swayc_t *child) { - int i; - swayc_t *parent = child->parent; - if (child->is_floating) { - // Special case for floating views - for (i = 0; i < parent->floating->length; ++i) { - if (parent->floating->items[i] == child) { - list_del(parent->floating, i); - break; - } - } - i = 0; - } else { - for (i = 0; i < parent->children->length; ++i) { - if (parent->children->items[i] == child) { - list_del(parent->children, i); - break; - } - } - if (is_auto_layout(parent->layout) && parent->children->length) { - /* go through each group, adjust the size of the last child of each group */ - double *(*get_maj_dim)(swayc_t *cont); - double *(*get_min_dim)(swayc_t *cont); - if (parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT) { - get_maj_dim = get_width; - get_min_dim = get_height; - } else { - get_maj_dim = get_height; - get_min_dim = get_width; - } - for (int j = parent->children->length - 1; j >= i;) { - int start = auto_group_start_index(parent, j); - int end = auto_group_end_index(parent, j); - swayc_t *first = parent->children->items[start]; - if (i == start) { - /* removed element was first child in the current group, - use its size along the major axis */ - *get_maj_dim(first) = *get_maj_dim(child); - } else if (start > i) { - /* preserve the group's dimension along major axis */ - *get_maj_dim(first) = *get_maj_dim(parent->children->items[start - 1]); - } - if (end != parent->children->length) { - double remaining = *get_min_dim(parent); - for (int k = start; k < end - 1; ++k) { - swayc_t *sibling = parent->children->items[k]; - remaining -= *get_min_dim(sibling); - } - /* last element of the group gets remaining size, elements - that don't change groups keep their ratio */ - *get_min_dim((swayc_t *) parent->children->items[end - 1]) = remaining; - } /* else last group, let apply_layout handle it */ - j = start - 1; - } - } - } - // Set focused to new container - if (parent->focused == child) { - if (parent->children->length > 0) { - parent->focused = parent->children->items[i ? i-1:0]; - } else if (parent->floating && parent->floating->length) { - parent->focused = parent->floating->items[parent->floating->length - 1]; - } else { - parent->focused = NULL; - } - } - child->parent = NULL; - // deactivate view - if (child->type == C_VIEW) { - wlc_view_set_state(child->handle, WLC_BIT_ACTIVATED, false); - } - return parent; -} - -void swap_container(swayc_t *a, swayc_t *b) { - if (!sway_assert(a&&b, "parameters must be non null") || - !sway_assert(a->parent && b->parent, "containers must have parents")) { - return; - } - size_t a_index = index_child(a); - size_t b_index = index_child(b); - swayc_t *a_parent = a->parent; - swayc_t *b_parent = b->parent; - // Swap the pointers - if (a->is_floating) { - a_parent->floating->items[a_index] = b; - } else { - a_parent->children->items[a_index] = b; - } - if (b->is_floating) { - b_parent->floating->items[b_index] = a; - } else { - b_parent->children->items[b_index] = a; - } - a->parent = b_parent; - b->parent = a_parent; - if (a_parent->focused == a) { - a_parent->focused = b; - } - // don't want to double switch - if (b_parent->focused == b && a_parent != b_parent) { - b_parent->focused = a; - } -} - -void swap_geometry(swayc_t *a, swayc_t *b) { - double x = a->x; - double y = a->y; - double w = a->width; - double h = a->height; - a->x = b->x; - a->y = b->y; - a->width = b->width; - a->height = b->height; - b->x = x; - b->y = y; - b->width = w; - b->height = h; -} - -static void swap_children(swayc_t *container, int a, int b) { - if (a >= 0 && b >= 0 && a < container->children->length - && b < container->children->length - && a != b) { - swayc_t *pa = (swayc_t *)container->children->items[a]; - swayc_t *pb = (swayc_t *)container->children->items[b]; - container->children->items[a] = container->children->items[b]; - container->children->items[b] = pa; - if (is_auto_layout(container->layout)) { - size_t ga = auto_group_index(container, a); - size_t gb = auto_group_index(container, b); - if (ga != gb) { - swap_geometry(pa, pb); - } - } - } -} - -void move_container(swayc_t *container, enum movement_direction dir, int move_amt) { - enum swayc_layouts layout = L_NONE; - swayc_t *parent = container->parent; - if (container->is_floating) { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); - switch(dir) { - case MOVE_LEFT: - container->x = MAX(0, container->x - move_amt); - break; - case MOVE_RIGHT: - container->x = MIN(output->width - container->width, container->x + move_amt); - break; - case MOVE_UP: - container->y = MAX(0, container->y - move_amt); - break; - case MOVE_DOWN: - container->y = MIN(output->height - container->height, container->y + move_amt); - break; - default: - break; - } - update_geometry(container); - return; - } - if (container->type != C_VIEW && container->type != C_CONTAINER) { - return; - } - if (dir == MOVE_UP || dir == MOVE_DOWN) { - layout = L_VERT; - } else if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { - layout = L_HORIZ; - } else if (dir == MOVE_FIRST) { - // swap first child in auto layout with currently focused child - if (is_auto_layout(parent->layout)) { - int focused_idx = index_child(container); - swayc_t *first = parent->children->items[0]; - if (focused_idx > 0) { - list_swap(parent->children, 0, focused_idx); - swap_geometry(first, container); - } - arrange_windows(parent->parent, -1, -1); - ipc_event_window(container, "move"); - set_focused_container_for(parent->parent, container); - } - return; - } else if (! (dir == MOVE_NEXT || dir == MOVE_PREV)) { - return; - } - swayc_t *child = container; - bool ascended = false; - - // View is wrapped in intermediate container which is needed for displaying - // the titlebar. Moving only the view outside of its parent container would just - // wrap it again under worspace. There would effectively be no movement, - // just a change of wrapping container. - if (child->type == C_VIEW && - parent->type == C_CONTAINER && - parent->children->length == 1 && - parent->parent->type == C_WORKSPACE) { - child = parent; - parent = parent->parent; - } - - while (true) { - sway_log(L_DEBUG, "container:%p, parent:%p, child %p,", - container,parent,child); - if (parent->layout == layout - || (layout == L_NONE && (parent->type == C_CONTAINER || parent->type == C_WORKSPACE)) /* accept any layout for next/prev direction */ - || (parent->layout == L_TABBED && layout == L_HORIZ) - || (parent->layout == L_STACKED && layout == L_VERT) - || is_auto_layout(parent->layout)) { - int diff; - // If it has ascended (parent has moved up), no container is removed - // so insert it at index, or index+1. - // if it has not, the moved container is removed, so it needs to be - // inserted at index-1, or index+1 - if (ascended) { - diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? 0 : 1; - } else { - diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? -1 : 1; - } - int idx = index_child(child); - int desired = idx + diff; - if (dir == MOVE_NEXT || dir == MOVE_PREV) { - // Next/Prev always wrap. - if (desired < 0) { - desired += parent->children->length; - } else if (desired >= parent->children->length) { - desired = 0; - } - } - // when it has ascended, legal insertion position is 0:len - // when it has not, legal insertion position is 0:len-1 - if (desired >= 0 && desired - ascended < parent->children->length) { - if (!ascended) { - child = parent->children->items[desired]; - // Move container into sibling container - if (child->type == C_CONTAINER) { - parent = child; - // Insert it in first/last if matching layout, otherwise - // insert it next to focused container - if (parent->layout == layout - || (parent->layout == L_TABBED && layout == L_HORIZ) - || (parent->layout == L_STACKED && layout == L_VERT) - || is_auto_layout(parent->layout)) { - desired = (diff < 0) * parent->children->length; - } else { - desired = index_child(child->focused) + 1; - } - //reset geometry - container->width = container->height = 0; - } - } - if (container->parent == parent) { - swap_children(parent, idx, desired); - } else { - swayc_t *old_parent = remove_child(container); - insert_child(parent, container, desired); - destroy_container(old_parent); - sway_log(L_DEBUG,"Moving to %p %d", parent, desired); - } - break; - } - } - // Change parent layout if we need to - if (parent->children->length == 1 && parent->layout != layout && layout != L_NONE) { - /* swayc_change_layout(parent, layout); */ - parent->layout = layout; - continue; - } - if (parent->type == C_WORKSPACE) { - // If moving to an adjacent output we need a starting position (since this - // output might border to multiple outputs). - struct wlc_point abs_pos; - get_absolute_center_position(container, &abs_pos); - - swayc_t *output = swayc_adjacent_output(parent->parent, dir, &abs_pos, true); - - if (output) { - sway_log(L_DEBUG, "Moving between outputs"); - swayc_t *old_parent = remove_child(container); - destroy_container(old_parent); - - swayc_t *dest = output->focused; - switch (dir) { - case MOVE_LEFT: - case MOVE_UP: - // reset container geometry - container->width = container->height = 0; - add_child(dest, container); - break; - case MOVE_RIGHT: - case MOVE_DOWN: - // reset container geometry - container->width = container->height = 0; - insert_child(dest, container, 0); - break; - default: - break; - } - // arrange new workspace - arrange_windows(dest, -1, -1); - set_focused_container(container); - break; - } - - // We simply cannot move any further. - if (parent->layout == layout) { - break; - } - // Create container around workspace to insert child into - parent = new_container(parent, layout); - // Previous line set the resulting container's layout to - // workspace_layout. It should have been just layout. - parent->layout = parent->parent->layout; - } - ascended = true; - child = parent; - parent = child->parent; - } - arrange_windows(parent->parent, -1, -1); - ipc_event_window(container, "move"); - set_focused_container_for(parent->parent, container); -} - -void move_container_to(swayc_t* container, swayc_t* destination) { - if (container == destination || swayc_is_parent_of(container, destination)) { - return; - } - swayc_t *parent = remove_child(container); - // Send to new destination - if (container->is_floating) { - swayc_t *ws = swayc_active_workspace_for(destination); - add_floating(ws, container); - - // If the workspace only has one child after adding one, it - // means that the workspace was just initialized. - if (ws->children->length + ws->floating->length == 1) { - ipc_event_workspace(NULL, ws, "init"); - } - } else if (destination->type == C_WORKSPACE) { - // reset container geometry - container->width = container->height = 0; - add_child(destination, container); - - // If the workspace only has one child after adding one, it - // means that the workspace was just initialized. - if (destination->children->length + destination->floating->length == 1) { - ipc_event_workspace(NULL, destination, "init"); - } - } else { - // reset container geometry - container->width = container->height = 0; - add_sibling(destination, container); - } - // Destroy old container if we need to - parent = destroy_container(parent); - // Refocus - swayc_t *op1 = swayc_parent_by_type(destination, C_OUTPUT); - swayc_t *op2 = swayc_parent_by_type(parent, C_OUTPUT); - set_focused_container(get_focused_view(op1)); - arrange_windows(op1, -1, -1); - update_visibility(op1); - if (op1 != op2) { - set_focused_container(get_focused_view(op2)); - arrange_windows(op2, -1, -1); - update_visibility(op2); - } -} - -void move_workspace_to(swayc_t* workspace, swayc_t* destination) { - if (workspace == destination || swayc_is_parent_of(workspace, destination)) { - return; - } - swayc_t *src_op = remove_child(workspace); - // reset container geometry - workspace->width = workspace->height = 0; - add_child(destination, workspace); - sort_workspaces(destination); - // Refocus destination (change to new workspace) - set_focused_container(get_focused_view(workspace)); - arrange_windows(destination, -1, -1); - update_visibility(destination); - - // make sure source output has a workspace - if (src_op->children->length == 0) { - char *ws_name = workspace_next_name(src_op->name); - swayc_t *ws = new_workspace(src_op, ws_name); - ws->is_focused = true; - free(ws_name); - } - set_focused_container(get_focused_view(src_op)); - update_visibility(src_op); -} - -static void adjust_border_geometry(swayc_t *c, struct wlc_geometry *g, - const struct wlc_size *res, int left, int right, int top, int bottom) { - - g->size.w += left + right; - if (g->origin.x - left < 0) { - g->size.w += g->origin.x - left; - } else if (g->origin.x + g->size.w - right > res->w) { - g->size.w = res->w - g->origin.x + right; - } - - g->size.h += top + bottom; - if (g->origin.y - top < 0) { - g->size.h += g->origin.y - top; - } else if (g->origin.y + g->size.h - top > res->h) { - g->size.h = res->h - g->origin.y + top; - } - - g->origin.x = MIN((uint32_t)MAX(g->origin.x - left, 0), res->w); - g->origin.y = MIN((uint32_t)MAX(g->origin.y - top, 0), res->h); - -} - -static void update_border_geometry_floating(swayc_t *c, struct wlc_geometry *geometry) { - struct wlc_geometry g = *geometry; - c->actual_geometry = g; - - swayc_t *output = swayc_parent_by_type(c, C_OUTPUT); - struct wlc_size res; - output_get_scaled_size(output->handle, &res); - - switch (c->border_type) { - case B_NONE: - break; - case B_PIXEL: - adjust_border_geometry(c, &g, &res, c->border_thickness, - c->border_thickness, c->border_thickness, c->border_thickness); - break; - case B_NORMAL: - { - int title_bar_height = config->font_height + 4; // borders + padding - - adjust_border_geometry(c, &g, &res, c->border_thickness, - c->border_thickness, title_bar_height, c->border_thickness); - - struct wlc_geometry title_bar = { - .origin = { - .x = c->actual_geometry.origin.x - c->border_thickness, - .y = c->actual_geometry.origin.y - title_bar_height - }, - .size = { - .w = c->actual_geometry.size.w + (2 * c->border_thickness), - .h = title_bar_height - } - }; - c->title_bar_geometry = title_bar; - break; - } - } - - c->border_geometry = g; - *geometry = c->actual_geometry; - - update_container_border(c); -} - -void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) { - switch (parent->layout) { - case L_TABBED: - case L_STACKED: - if (prev_layout != L_TABBED && prev_layout != L_STACKED) { - // cache current geometry for all non-float children - int i; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *child = parent->children->items[i]; - child->cached_geometry.origin.x = child->x; - child->cached_geometry.origin.y = child->y; - child->cached_geometry.size.w = child->width; - child->cached_geometry.size.h = child->height; - } - } - break; - default: - if (prev_layout == L_TABBED || prev_layout == L_STACKED) { - // recover cached geometry for all non-float children - int i; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *child = parent->children->items[i]; - // only recoverer cached geometry if non-zero - if (!wlc_geometry_equals(&child->cached_geometry, &wlc_geometry_zero)) { - child->x = child->cached_geometry.origin.x; - child->y = child->cached_geometry.origin.y; - child->width = child->cached_geometry.size.w; - child->height = child->cached_geometry.size.h; - } - } - } - break; - } -} - -static int update_gap_geometry(swayc_t *container, struct wlc_geometry *g) { - swayc_t *ws = swayc_parent_by_type(container, C_WORKSPACE); - swayc_t *op = ws->parent; - int gap = container->is_floating ? 0 : swayc_gap(container); - if (gap % 2 != 0) { - // because gaps are implemented as "half sized margins" it's currently - // not possible to align views properly with odd sized gaps. - gap -= 1; - } - - g->origin.x = container->x + gap/2 < op->width ? container->x + gap/2 : op->width-1; - g->origin.y = container->y + gap/2 < op->height ? container->y + gap/2 : op->height-1; - g->size.w = container->width > gap ? container->width - gap : 1; - g->size.h = container->height > gap ? container->height - gap : 1; - - if ((!config->edge_gaps && gap > 0) || (config->smart_gaps && ws->children->length == 1)) { - // Remove gap against the workspace edges. Because a pixel is not - // divisable, depending on gap size and the number of siblings our view - // might be at the workspace edge without being exactly so (thus test - // with gap, and align correctly). - if (container->x - gap <= ws->x) { - g->origin.x = ws->x; - g->size.w = container->width - gap/2; - } - if (container->y - gap <= ws->y) { - g->origin.y = ws->y; - g->size.h = container->height - gap/2; - } - if (container->x + container->width + gap >= ws->x + ws->width) { - g->size.w = ws->x + ws->width - g->origin.x; - } - if (container->y + container->height + gap >= ws->y + ws->height) { - g->size.h = ws->y + ws->height - g->origin.y; - } - } - - return gap; -} - -void update_geometry(swayc_t *container) { - if (container->type != C_VIEW && container->type != C_CONTAINER) { - return; - } - - swayc_t *workspace = swayc_parent_by_type(container, C_WORKSPACE); - swayc_t *op = workspace->parent; - swayc_t *parent = container->parent; - - struct wlc_geometry geometry = { - .origin = { - .x = container->x < op->width ? container->x : op->width-1, - .y = container->y < op->height ? container->y : op->height-1 - }, - .size = { - .w = container->width, - .h = container->height, - } - }; - - int gap = 0; - - // apply inner gaps to non-tabbed/stacked containers - swayc_t *p = swayc_tabbed_stacked_ancestor(container); - if (p == NULL) { - gap = update_gap_geometry(container, &geometry); - } - - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); - struct wlc_size size; - output_get_scaled_size(output->handle, &size); - - if (swayc_is_fullscreen(container)) { - geometry.origin.x = 0; - geometry.origin.y = 0; - geometry.size.w = size.w; - geometry.size.h = size.h; - if (op->focused == workspace) { - wlc_view_bring_to_front(container->handle); - } - - container->border_geometry = wlc_geometry_zero; - container->title_bar_geometry = wlc_geometry_zero; - border_clear(container->border); - } else if (container->is_floating) { // allocate border for floating window - update_border_geometry_floating(container, &geometry); - } else if (!container->is_floating) { // allocate border for titled window - container->border_geometry = geometry; - - int border_top = container->border_thickness; - int border_bottom = container->border_thickness; - int border_left = container->border_thickness; - int border_right = container->border_thickness; - - // handle hide_edge_borders - if (config->hide_edge_borders != E_NONE && (gap <= 0 || (config->smart_gaps && workspace->children->length == 1))) { - if (config->hide_edge_borders == E_VERTICAL || config->hide_edge_borders == E_BOTH) { - if (geometry.origin.x == workspace->x) { - border_left = 0; - } - - if (geometry.origin.x + geometry.size.w == workspace->x + workspace->width) { - border_right = 0; - } - } - - if (config->hide_edge_borders == E_HORIZONTAL || config->hide_edge_borders == E_BOTH) { - if (geometry.origin.y == workspace->y || should_hide_top_border(container, geometry.origin.y)) { - border_top = 0; - } - - if (geometry.origin.y + geometry.size.h == workspace->y + workspace->height) { - border_bottom = 0; - } - } - - if (config->hide_edge_borders == E_SMART && workspace->children->length == 1) { - border_top = 0; - border_bottom = 0; - border_left = 0; - border_right = 0; - } - } - - int title_bar_height = config->font_height + 4; //borders + padding - - if (parent->layout == L_TABBED && parent->children->length > 1) { - int i, x = 0, w, l, r; - l = parent->children->length; - w = geometry.size.w / l; - r = geometry.size.w % l; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *view = parent->children->items[i]; - if (view == container) { - x = w * i; - if (i == l - 1) { - w += r; - } - break; - } - } - - struct wlc_geometry title_bar = { - .origin = { - .x = container->border_geometry.origin.x + x, - .y = container->border_geometry.origin.y - }, - .size = { - .w = w, - .h = title_bar_height - } - }; - geometry.origin.x += border_left; - geometry.origin.y += title_bar.size.h; - geometry.size.w -= (border_left + border_right); - geometry.size.h -= (border_bottom + title_bar.size.h); - container->title_bar_geometry = title_bar; - } else if (parent->layout == L_STACKED && parent->children->length > 1) { - int i, y = 0; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *view = parent->children->items[i]; - if (view == container) { - y = title_bar_height * i; - } - } - - struct wlc_geometry title_bar = { - .origin = { - .x = container->border_geometry.origin.x, - .y = container->border_geometry.origin.y + y - }, - .size = { - .w = container->border_geometry.size.w, - .h = title_bar_height - } - }; - title_bar_height = title_bar_height * parent->children->length; - geometry.origin.x += border_left; - geometry.origin.y += title_bar_height; - geometry.size.w -= (border_left + border_right); - geometry.size.h -= (border_bottom + title_bar_height); - container->title_bar_geometry = title_bar; - } else { - switch (container->border_type) { - case B_NONE: - break; - case B_PIXEL: - geometry.origin.x += border_left; - geometry.origin.y += border_top; - geometry.size.w -= (border_left + border_right); - geometry.size.h -= (border_top + border_bottom); - break; - case B_NORMAL: - { - struct wlc_geometry title_bar = { - .origin = { - .x = container->border_geometry.origin.x, - .y = container->border_geometry.origin.y - }, - .size = { - .w = container->border_geometry.size.w, - .h = title_bar_height - } - }; - geometry.origin.x += border_left; - geometry.origin.y += title_bar.size.h; - geometry.size.w -= (border_left + border_right); - geometry.size.h -= (border_bottom + title_bar.size.h); - container->title_bar_geometry = title_bar; - break; - } - } - } - - container->actual_geometry = geometry; - - if (container->type == C_VIEW) { - update_container_border(container); - } - } - - if (container->type == C_VIEW) { - wlc_view_set_geometry(container->handle, 0, &geometry); - } -} - -/** - * Layout application prototypes - */ -static void apply_horiz_layout(swayc_t *container, const double x, - const double y, const double width, - const double height, const int start, - const int end); -static void apply_vert_layout(swayc_t *container, const double x, - const double y, const double width, - const double height, const int start, - const int end); -static void apply_tabbed_or_stacked_layout(swayc_t *container, double x, - double y, double width, - double height); - -static void apply_auto_layout(swayc_t *container, const double x, const double y, - const double width, const double height, - enum swayc_layouts group_layout, - bool master_first); - -static void arrange_windows_r(swayc_t *container, double width, double height) { - int i; - if (width == -1 || height == -1) { - swayc_log(L_DEBUG, container, "Arranging layout for %p", container); - width = container->width; - height = container->height; - } - // pixels are indivisible. if we don't round the pixels, then the view - // calculations will be off (e.g. 50.5 + 50.5 = 101, but in reality it's - // 50 + 50 = 100). doing it here cascades properly to all width/height/x/y. - width = floor(width); - height = floor(height); - - sway_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container, - container->name, container->width, container->height, container->x, - container->y); - - double x = 0, y = 0; - switch (container->type) { - case C_ROOT: - for (i = 0; i < container->children->length; ++i) { - swayc_t *output = container->children->items[i]; - sway_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); - arrange_windows_r(output, -1, -1); - } - return; - case C_OUTPUT: - { - struct wlc_size resolution; - output_get_scaled_size(container->handle, &resolution); - width = resolution.w; height = resolution.h; - // output must have correct size due to e.g. seamless mouse, - // but a workspace might be smaller depending on panels. - container->width = width; - container->height = height; - } - // arrange all workspaces: - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - arrange_windows_r(child, -1, -1); - } - // Bring all unmanaged views to the front - for (i = 0; i < container->unmanaged->length; ++i) { - wlc_handle *handle = container->unmanaged->items[i]; - wlc_view_bring_to_front(*handle); - } - return; - case C_WORKSPACE: - { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); - width = output->width, height = output->height; - /* TODO WLR - for (i = 0; i < desktop_shell.panels->length; ++i) { - struct panel_config *config = desktop_shell.panels->items[i]; - if (config->output == output->handle) { - struct wlc_size size = *wlc_surface_get_size(config->surface); - sway_log(L_DEBUG, "-> Found panel for this workspace: %ux%u, position: %u", size.w, size.h, config->panel_position); - switch (config->panel_position) { - case DESKTOP_SHELL_PANEL_POSITION_TOP: - y += size.h; height -= size.h; - break; - case DESKTOP_SHELL_PANEL_POSITION_BOTTOM: - height -= size.h; - break; - case DESKTOP_SHELL_PANEL_POSITION_LEFT: - x += size.w; width -= size.w; - break; - case DESKTOP_SHELL_PANEL_POSITION_RIGHT: - width -= size.w; - break; - } - } - } - */ - int gap = swayc_gap(container); - x = container->x = x + gap; - y = container->y = y + gap; - width = container->width = width - gap * 2; - height = container->height = height - gap * 2; - sway_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", container->name, container->x, container->y); - } - // children are properly handled below - break; - case C_VIEW: - { - container->width = width; - container->height = height; - update_geometry(container); - sway_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f", container->width, - container->height, container->x, container->y); - } - return; - default: - container->width = width; - container->height = height; - x = container->x; - y = container->y; - - // add gaps to top level tapped/stacked container - if (container->parent->type == C_WORKSPACE && - (container->layout == L_TABBED || container->layout == L_STACKED)) { - update_geometry(container); - width = container->border_geometry.size.w; - height = container->border_geometry.size.h; - x = container->border_geometry.origin.x; - y = container->border_geometry.origin.y; - } - - // update container size if it's a direct child in a tabbed/stacked layout - // if parent is a workspace, its actual_geometry won't be initialized - if (swayc_tabbed_stacked_parent(container) != NULL && - container->parent->type != C_WORKSPACE) { - // Use parent actual_geometry as a base for calculating - // container geometry - container->width = container->parent->actual_geometry.size.w; - container->height = container->parent->actual_geometry.size.h; - container->x = container->parent->actual_geometry.origin.x; - container->y = container->parent->actual_geometry.origin.y; - - update_geometry(container); - width = container->width = container->actual_geometry.size.w; - height = container->height = container->actual_geometry.size.h; - x = container->x = container->actual_geometry.origin.x; - y = container->y = container->actual_geometry.origin.y; - } - - break; - } - - switch (container->layout) { - case L_HORIZ: - default: - apply_horiz_layout(container, x, y, width, height, 0, - container->children->length); - break; - case L_VERT: - apply_vert_layout(container, x, y, width, height, 0, - container->children->length); - break; - case L_TABBED: - case L_STACKED: - apply_tabbed_or_stacked_layout(container, x, y, width, height); - break; - case L_AUTO_LEFT: - apply_auto_layout(container, x, y, width, height, L_VERT, true); - break; - case L_AUTO_RIGHT: - apply_auto_layout(container, x, y, width, height, L_VERT, false); - break; - case L_AUTO_TOP: - apply_auto_layout(container, x, y, width, height, L_HORIZ, true); - break; - case L_AUTO_BOTTOM: - apply_auto_layout(container, x, y, width, height, L_HORIZ, false); - break; - } - - // Arrage floating layouts for workspaces last - if (container->type == C_WORKSPACE) { - for (int i = 0; i < container->floating->length; ++i) { - swayc_t *view = container->floating->items[i]; - if (view->type == C_VIEW) { - update_geometry(view); - sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", - view->width, view->height, view->x, view->y); - if (swayc_is_fullscreen(view)) { - wlc_view_bring_to_front(view->handle); - } else if (!container->focused || - !swayc_is_fullscreen(container->focused)) { - wlc_view_bring_to_front(view->handle); - } - } - } - } -} - -void apply_horiz_layout(swayc_t *container, const double x, const double y, - const double width, const double height, - const int start, const int end) { - double scale = 0; - // Calculate total width - for (int i = start; i < end; ++i) { - double *old_width = &((swayc_t *)container->children->items[i])->width; - if (*old_width <= 0) { - if (end - start > 1) { - *old_width = width / (end - start - 1); - } else { - *old_width = width; - } - } - scale += *old_width; - } - scale = width / scale; - - // Resize windows - double child_x = x; - if (scale > 0.1) { - sway_log(L_DEBUG, "Arranging %p horizontally", container); - swayc_t *focused = NULL; - for (int i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; - sway_log(L_DEBUG, - "Calculating arrangement for %p:%d (will scale %f by %f)", child, - child->type, width, scale); - child->x = child_x; - child->y = y; - - if (child == container->focused) { - focused = child; - } - - if (i == end - 1) { - double remaining_width = x + width - child_x; - arrange_windows_r(child, remaining_width, height); - } else { - arrange_windows_r(child, child->width * scale, height); - } - child_x += child->width; - } - - // update focused view border last because it may - // depend on the title bar geometry of its siblings. - if (focused && container->children->length > 1) { - update_container_border(focused); - } - } -} - -void apply_vert_layout(swayc_t *container, const double x, const double y, - const double width, const double height, const int start, - const int end) { - int i; - double scale = 0; - // Calculate total height - for (i = start; i < end; ++i) { - double *old_height = &((swayc_t *)container->children->items[i])->height; - if (*old_height <= 0) { - if (end - start > 1) { - *old_height = height / (end - start - 1); - } else { - *old_height = height; - } - } - scale += *old_height; - } - scale = height / scale; - - // Resize - double child_y = y; - if (scale > 0.1) { - sway_log(L_DEBUG, "Arranging %p vertically", container); - swayc_t *focused = NULL; - for (i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; - sway_log(L_DEBUG, - "Calculating arrangement for %p:%d (will scale %f by %f)", child, - child->type, height, scale); - child->x = x; - child->y = child_y; - - if (child == container->focused) { - focused = child; - } - - if (i == end - 1) { - double remaining_height = y + height - child_y; - arrange_windows_r(child, width, remaining_height); - } else { - arrange_windows_r(child, width, child->height * scale); - } - child_y += child->height; - } - - // update focused view border last because it may - // depend on the title bar geometry of its siblings. - if (focused && container->children->length > 1) { - update_container_border(focused); - } - } -} - -void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, - double width, double height) { - int i; - swayc_t *focused = NULL; - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - child->x = x; - child->y = y; - if (child == container->focused) { - focused = child; - } else { - arrange_windows_r(child, width, height); - } - } - - if (focused) { - arrange_windows_r(focused, width, height); - } -} - -void apply_auto_layout(swayc_t *container, const double x, const double y, - const double width, const double height, - enum swayc_layouts group_layout, - bool master_first) { - // Auto layout "container" in width x height @ x, y - // using "group_layout" for each of the groups in the container. - // There is one "master" group, plus container->nb_slave_groups. - // Each group is layed out side by side following the "major" axis. - // The direction of the layout used for groups is the "minor" axis. - // Example: - // - // ---- major axis --> - // +---------+-----------+ - // | | | | - // | master | slave 1 | | - // | +-----------+ | minor axis (direction of group_layout) - // | | | | - // | | slave 2 | V - // +---------+-----------+ - // - // container with three children (one master and two slaves) and - // a single slave group (containing slave 1 and 2). The master - // group and slave group are layed out using L_VERT. - - size_t nb_groups = auto_group_count(container); - - // the target dimension of the container along the "major" axis, each - // group in the container will be layed out using "group_layout" along - // the "minor" axis. - double dim_maj; - double pos_maj; - - // x and y coords for the next group to be laid out. - const double *group_x, *group_y; - - // pos of the next group to layout along the major axis - double pos; - - // size of the next group along the major axis. - double group_dim; - - // height and width of next group to be laid out. - const double *group_h, *group_w; - - switch (group_layout) { - default: - sway_log(L_DEBUG, "Unknown layout type (%d) used in %s()", - group_layout, __func__); - /* fall through */ - case L_VERT: - dim_maj = width; - pos_maj = x; - - group_x = &pos; - group_y = &y; - group_w = &group_dim; - group_h = &height; - break; - case L_HORIZ: - dim_maj = height; - pos_maj = y; - - group_x = &x; - group_y = &pos; - group_w = &width; - group_h = &group_dim; - break; - } - - /* Determine the dimension of each of the groups in the layout. - * Dimension will be width for a VERT layout and height for a HORIZ - * layout. */ - double old_group_dim[nb_groups]; - double old_dim = 0; - for (size_t group = 0; group < nb_groups; ++group) { - int idx; - if (auto_group_bounds(container, group, &idx, NULL)) { - swayc_t *child = container->children->items[idx]; - double *dim = group_layout == L_HORIZ ? &child->height : &child->width; - if (*dim <= 0) { - // New child with uninitialized dimension - *dim = dim_maj; - if (nb_groups > 1) { - // child gets a dimension proportional to existing groups, - // it will be later scaled based on to the available size - // in the major axis. - *dim /= (nb_groups - 1); - } - } - old_dim += *dim; - old_group_dim[group] = *dim; - } - } - double scale = dim_maj / old_dim; - - /* Apply layout to each group */ - pos = pos_maj; - - for (size_t group = 0; group < nb_groups; ++group) { - int start, end; // index of first (inclusive) and last (exclusive) child in the group - if (auto_group_bounds(container, group, &start, &end)) { - // adjusted size of the group - group_dim = old_group_dim[group] * scale; - if (group == nb_groups - 1) { - group_dim = pos_maj + dim_maj - pos; // remaining width - } - sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", - container, group, start, end, *group_w, *group_h, *group_x, *group_y); - switch (group_layout) { - default: - case L_VERT: - apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); - break; - case L_HORIZ: - apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); - break; - } - - /* update position for next group */ - pos += group_dim; - } - } -} - -void arrange_windows(swayc_t *container, double width, double height) { - update_visibility(container); - arrange_windows_r(container, width, height); - layout_log(&root_container, 0); -} - -void arrange_backgrounds(void) { - /* TODO WLR - struct background_config *bg; - for (int i = 0; i < desktop_shell.backgrounds->length; ++i) { - bg = desktop_shell.backgrounds->items[i]; - wlc_view_send_to_back(bg->handle); - } - */ -} - -/** - * Get swayc in the direction of newly entered output. - */ -static swayc_t *get_swayc_in_output_direction(swayc_t *output, enum movement_direction dir) { - if (!output) { - return NULL; - } - - swayc_t *ws = swayc_focus_by_type(output, C_WORKSPACE); - if (ws && ws->children->length > 0) { - switch (dir) { - case MOVE_LEFT: - // get most right child of new output - return ws->children->items[ws->children->length-1]; - case MOVE_RIGHT: - // get most left child of new output - return ws->children->items[0]; - case MOVE_UP: - case MOVE_DOWN: - { - swayc_t *focused_view = swayc_focus_by_type(ws, C_VIEW); - if (focused_view && focused_view->parent) { - swayc_t *parent = focused_view->parent; - if (parent->layout == L_VERT) { - if (dir == MOVE_UP) { - // get child furthest down on new output - return parent->children->items[parent->children->length-1]; - } else if (dir == MOVE_DOWN) { - // get child furthest up on new output - return parent->children->items[0]; - } - } - return focused_view; - } - break; - } - default: - break; - } - } - - return output; -} - -swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_direction dir, swayc_t *limit) { - if (dir == MOVE_CHILD) { - return container->focused; - } - - swayc_t *parent = container->parent; - if (dir == MOVE_PARENT) { - if (parent->type == C_OUTPUT) { - return NULL; - } else { - return parent; - } - } - - if (dir == MOVE_PREV || dir == MOVE_NEXT) { - int focused_idx = index_child(container); - if (focused_idx == -1) { - return NULL; - } else { - int desired = (focused_idx + (dir == MOVE_NEXT ? 1 : -1)) % - parent->children->length; - if (desired < 0) { - desired += parent->children->length; - } - return parent->children->items[desired]; - } - } - - // If moving to an adjacent output we need a starting position (since this - // output might border to multiple outputs). - struct wlc_point abs_pos; - get_absolute_center_position(container, &abs_pos); - - if (container->type == C_VIEW && swayc_is_fullscreen(container)) { - sway_log(L_DEBUG, "Moving from fullscreen view, skipping to output"); - container = swayc_parent_by_type(container, C_OUTPUT); - get_absolute_center_position(container, &abs_pos); - swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); - return get_swayc_in_output_direction(output, dir); - } - - if (container->type == C_WORKSPACE && container->fullscreen) { - sway_log(L_DEBUG, "Moving to fullscreen view"); - return container->fullscreen; - } - - swayc_t *wrap_candidate = NULL; - while (true) { - // Test if we can even make a difference here - bool can_move = false; - int desired; - int idx = index_child(container); - if (parent->type == C_ROOT) { - swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); - if (!output || output == container) { - return wrap_candidate; - } - sway_log(L_DEBUG, "Moving between outputs"); - return get_swayc_in_output_direction(output, dir); - } else { - if (is_auto_layout(parent->layout)) { - bool is_major = parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT - ? dir == MOVE_LEFT || dir == MOVE_RIGHT - : dir == MOVE_DOWN || dir == MOVE_UP; - size_t gidx = auto_group_index(parent, idx); - if (is_major) { - size_t desired_grp = gidx + (dir == MOVE_RIGHT || dir == MOVE_DOWN ? 1 : -1); - can_move = auto_group_bounds(parent, desired_grp, &desired, NULL); - } else { - desired = idx + (dir == MOVE_RIGHT || dir == MOVE_DOWN ? 1 : -1); - int start, end; - can_move = auto_group_bounds(parent, gidx, &start, &end) - && desired >= start && desired < end; - } - } else { - if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { - if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { - can_move = true; - desired = idx + (dir == MOVE_LEFT ? -1 : 1); - } - } else { - if (parent->layout == L_VERT || parent->layout == L_STACKED) { - can_move = true; - desired = idx + (dir == MOVE_UP ? -1 : 1); - } - } - } - } - - if (can_move) { - if (container->is_floating) { - if (desired < 0) { - wrap_candidate = parent->floating->items[parent->floating->length-1]; - } else if (desired >= parent->floating->length){ - wrap_candidate = parent->floating->items[0]; - } else { - wrap_candidate = parent->floating->items[desired]; - } - if (wrap_candidate) { - wlc_view_bring_to_front(wrap_candidate->handle); - } - return wrap_candidate; - } else if (desired < 0 || desired >= parent->children->length) { - can_move = false; - int len = parent->children->length; - if (!wrap_candidate && len > 1) { - if (desired < 0) { - wrap_candidate = parent->children->items[len-1]; - } else { - wrap_candidate = parent->children->items[0]; - } - if (config->force_focus_wrapping) { - return wrap_candidate; - } - } - } else { - sway_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, - idx, container, dir, desired, parent->children->items[desired]); - return parent->children->items[desired]; - } - } - if (!can_move) { - container = parent; - parent = parent->parent; - if (!parent || container == limit) { - // wrapping is the last chance - return wrap_candidate; - } - } - } -} - -swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) { - return get_swayc_in_direction_under(container, dir, NULL); -} - -void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge) { - int i; - bool layout_match = true; - sway_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); - if (edge == WLC_RESIZE_EDGE_LEFT || edge == WLC_RESIZE_EDGE_RIGHT) { - container->width += amount; - layout_match = container->layout == L_HORIZ; - } else if (edge == WLC_RESIZE_EDGE_TOP || edge == WLC_RESIZE_EDGE_BOTTOM) { - container->height += amount; - layout_match = container->layout == L_VERT; - } - if (container->type == C_VIEW) { - update_geometry(container); - return; - } - if (layout_match) { - for (i = 0; i < container->children->length; i++) { - recursive_resize(container->children->items[i], amount/container->children->length, edge); - } - } else { - for (i = 0; i < container->children->length; i++) { - recursive_resize(container->children->items[i], amount, edge); - } - } -} - -enum swayc_layouts default_layout(swayc_t *output) { - if (config->default_layout != L_NONE) { - return config->default_layout; - } else if (config->default_orientation != L_NONE) { - return config->default_orientation; - } else if (output->width >= output->height) { - return L_HORIZ; - } else { - return L_VERT; - } -} - -bool is_auto_layout(enum swayc_layouts layout) { - return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); -} - -/** - * Return the number of master elements in a container - */ -static inline size_t auto_master_count(const swayc_t *container) { - sway_assert(container->children->length >= 0, "Container %p has (negative) children %d", - container, container->children->length); - return MIN(container->nb_master, (size_t)container->children->length); -} - -/** - * Return the number of children in the slave groups. This corresponds to the children - * that are not members of the master group. - */ -static inline size_t auto_slave_count(const swayc_t *container) { - return container->children->length - auto_master_count(container); -} - -/** - * Return the number of slave groups in the container. - */ -size_t auto_slave_group_count(const swayc_t *container) { - return MIN(container->nb_slave_groups, auto_slave_count(container)); -} - -/** - * Return the combined number of master and slave groups in the container. - */ -size_t auto_group_count(const swayc_t *container) { - return auto_slave_group_count(container) - + (container->children->length && container->nb_master ? 1 : 0); -} - -/** - * given the index of a container's child, return the index of the first child of the group - * which index is a member of. - */ -int auto_group_start_index(const swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout) - || (size_t)index < container->nb_master) { - return 0; - } else { - size_t nb_slaves = auto_slave_count(container); - size_t nb_slave_grp = auto_slave_group_count(container); - size_t grp_sz = nb_slaves / nb_slave_grp; - size_t remainder = nb_slaves % nb_slave_grp; - int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; - int start_idx; - if (index < idx2) { - start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; - } else { - start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); - } - return MIN(start_idx, container->children->length); - } -} - -/** - * given the index of a container's child, return the index of the first child of the group - * that follows the one which index is a member of. - * This makes the function usable to walk through the groups in a container. - */ -int auto_group_end_index(const swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout)) { - return container->children->length; - } else { - int nxt_idx; - if ((size_t)index < container->nb_master) { - nxt_idx = auto_master_count(container); - } else { - size_t nb_slaves = auto_slave_count(container); - size_t nb_slave_grp = auto_slave_group_count(container); - size_t grp_sz = nb_slaves / nb_slave_grp; - size_t remainder = nb_slaves % nb_slave_grp; - int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; - } else { - nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); - } - } - return MIN(nxt_idx, container->children->length); - } -} - -/** - * return the index of the Group containing th child of . - * The index is the order of the group along the container's major axis (starting at 0). - */ -size_t auto_group_index(const swayc_t *container, int index) { - if (index < 0) { - return 0; - } - bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); - size_t nb_slaves = auto_slave_count(container); - if ((size_t)index < container->nb_master) { - if (master_first || nb_slaves <= 0) { - return 0; - } else { - return auto_slave_group_count(container); - } - } else { - size_t nb_slave_grp = auto_slave_group_count(container); - size_t grp_sz = nb_slaves / nb_slave_grp; - size_t remainder = nb_slaves % nb_slave_grp; - int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; - size_t grp_idx; - if (index < idx2) { - grp_idx = (index - container->nb_master) / grp_sz; - } else { - grp_idx = (nb_slave_grp - remainder) + (index - idx2) / (grp_sz + 1) ; - } - return grp_idx + (master_first && container-> nb_master ? 1 : 0); - } -} - -/** - * Return the first index (inclusive) and last index (exclusive) of the elements of a group in - * an auto layout. - * If the bounds of the given group can be calculated, they are returned in the start/end - * parameters (int pointers) and the return value will be true. - * The indexes are passed by reference and can be NULL. - */ -bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end) { - size_t nb_grp = auto_group_count(container); - if (group_index >= nb_grp) { - return false; - } - bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); - size_t nb_master = auto_master_count(container); - size_t nb_slave_grp = auto_slave_group_count(container); - int g_start, g_end; - if (nb_master && (master_first ? group_index == 0 : group_index == nb_grp - 1)) { - g_start = 0; - g_end = nb_master; - } else { - size_t nb_slaves = auto_slave_count(container); - size_t grp_sz = nb_slaves / nb_slave_grp; - size_t remainder = nb_slaves % nb_slave_grp; - size_t g0 = master_first && container->nb_master ? 1 : 0; - size_t g1 = g0 + nb_slave_grp - remainder; - if (group_index < g1) { - g_start = container->nb_master + (group_index - g0) * grp_sz; - g_end = g_start + grp_sz; - } else { - size_t g2 = group_index - g1; - g_start = container->nb_master - + (nb_slave_grp - remainder) * grp_sz - + g2 * (grp_sz + 1); - g_end = g_start + grp_sz + 1; - } - } - if (start) { - *start = g_start; - } - if (end) { - *end = g_end; - } - return true; -} diff --git a/sway/tree/output.c b/sway/tree/output.c deleted file mode 100644 index edfcac98..00000000 --- a/sway/tree/output.c +++ /dev/null @@ -1,278 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include "sway/output.h" -#include "log.h" -#include "list.h" - -void output_get_scaled_size(wlc_handle handle, struct wlc_size *size) { - *size = *wlc_output_get_resolution(handle); - uint32_t scale = wlc_output_get_scale(handle); - size->w /= scale; - size->h /= scale; -} - -swayc_t *output_by_name(const char* name, const struct wlc_point *abs_pos) { - swayc_t *output = NULL; - // If there is no output directly next to the current one, use - // swayc_opposite_output to wrap. - if (strcasecmp(name, "left") == 0) { - output = swayc_adjacent_output(NULL, MOVE_LEFT, abs_pos, true); - if (!output) { - output = swayc_opposite_output(MOVE_RIGHT, abs_pos); - } - } else if (strcasecmp(name, "right") == 0) { - output = swayc_adjacent_output(NULL, MOVE_RIGHT, abs_pos, true); - if (!output) { - output = swayc_opposite_output(MOVE_LEFT, abs_pos); - } - } else if (strcasecmp(name, "up") == 0) { - output = swayc_adjacent_output(NULL, MOVE_UP, abs_pos, true); - if (!output) { - output = swayc_opposite_output(MOVE_DOWN, abs_pos); - } - } else if (strcasecmp(name, "down") == 0) { - output = swayc_adjacent_output(NULL, MOVE_DOWN, abs_pos, true); - if (!output) { - output = swayc_opposite_output(MOVE_UP, abs_pos); - } - } else { - for(int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; - if (c->type == C_OUTPUT && strcasecmp(c->name, name) == 0) { - return c; - } - } - } - return output; -} - -swayc_t *swayc_opposite_output(enum movement_direction dir, - const struct wlc_point *abs_pos) { - - // Search through all the outputs and pick the output whose edge covers the - // given position, and is at leftmost/rightmost/upmost/downmost side of the - // screen (decided by the direction given). - swayc_t *opposite = NULL; - char *dir_text = NULL; - switch(dir) { - case MOVE_LEFT: - case MOVE_RIGHT: ; - for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; - if (abs_pos->y >= c->y && abs_pos->y <= c->y + c->height) { - if (!opposite) { - opposite = c; - } else if ((dir == MOVE_LEFT && c->x < opposite->x) - || (dir == MOVE_RIGHT && c->x > opposite->x)) { - opposite = c; - } - } - } - dir_text = dir == MOVE_LEFT ? "leftmost" : "rightmost"; - break; - case MOVE_UP: - case MOVE_DOWN: ; - for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; - if (abs_pos->x >= c->x && abs_pos->x <= c->x + c->width) { - if (!opposite) { - opposite = c; - } else if ((dir == MOVE_UP && c->y < opposite->y) - || (dir == MOVE_DOWN && c->y > opposite->y)) { - opposite = c; - } - } - } - dir_text = dir == MOVE_UP ? "upmost" : "downmost"; - break; - default: - sway_abort("Function called with invalid argument."); - break; - } - if (opposite) { - sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s from y-position %i", - opposite->name, opposite->width, opposite->height, opposite->x, opposite->y, - dir_text, abs_pos->y); - } - return opposite; -} - -// Position is where on the edge (as absolute position) the adjacent output should be searched for. -swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, - const struct wlc_point *abs_pos, bool pick_closest) { - - if (!output) { - output = swayc_active_output(); - } - // In order to find adjacent outputs we need to test that the outputs are - // aligned on one axis (decided by the direction given) and that the given - // position is within the edge of the adjacent output. If no such output - // exists we pick the adjacent output within the edge that is closest to - // the given position, if any. - swayc_t *adjacent = NULL; - char *dir_text = NULL; - switch(dir) { - case MOVE_LEFT: - case MOVE_RIGHT: ; - double delta_y = 0; - for(int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; - if (c == output || c->type != C_OUTPUT) { - continue; - } - bool x_aligned = dir == MOVE_LEFT ? - c->x + c->width == output->x : - c->x == output->x + output->width; - if (!x_aligned) { - continue; - } - if (abs_pos->y >= c->y && abs_pos->y <= c->y + c->height) { - delta_y = 0; - adjacent = c; - break; - } else if (pick_closest) { - // track closest adjacent output - double top_y = c->y, bottom_y = c->y + c->height; - if (top_y >= output->y && top_y <= output->y + output->height) { - double delta = top_y - abs_pos->y; - if (delta < 0) delta = -delta; - if (delta < delta_y || !adjacent) { - delta_y = delta; - adjacent = c; - } - } - // we check both points and pick the closest - if (bottom_y >= output->y && bottom_y <= output->y + output->height) { - double delta = bottom_y - abs_pos->y; - if (delta < 0) delta = -delta; - if (delta < delta_y || !adjacent) { - delta_y = delta; - adjacent = c; - } - } - } - } - dir_text = dir == MOVE_LEFT ? "left of" : "right of"; - if (adjacent && delta_y == 0) { - sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (y-position %i)", - adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, - dir_text, output->name, abs_pos->y); - } else if (adjacent) { - // so we end up picking the closest adjacent output because - // there is no directly adjacent to the given position - sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (y-position %i, delta: %.0f)", - adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, - dir_text, output->name, abs_pos->y, delta_y); - } - break; - case MOVE_UP: - case MOVE_DOWN: ; - double delta_x = 0; - for(int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; - if (c == output || c->type != C_OUTPUT) { - continue; - } - bool y_aligned = dir == MOVE_UP ? - c->y + c->height == output->y : - c->y == output->y + output->height; - if (!y_aligned) { - continue; - } - if (abs_pos->x >= c->x && abs_pos->x <= c->x + c->width) { - delta_x = 0; - adjacent = c; - break; - } else if (pick_closest) { - // track closest adjacent output - double left_x = c->x, right_x = c->x + c->width; - if (left_x >= output->x && left_x <= output->x + output->width) { - double delta = left_x - abs_pos->x; - if (delta < 0) delta = -delta; - if (delta < delta_x || !adjacent) { - delta_x = delta; - adjacent = c; - } - } - // we check both points and pick the closest - if (right_x >= output->x && right_x <= output->x + output->width) { - double delta = right_x - abs_pos->x; - if (delta < 0) delta = -delta; - if (delta < delta_x || !adjacent) { - delta_x = delta; - adjacent = c; - } - } - } - } - dir_text = dir == MOVE_UP ? "above" : "below"; - if (adjacent && delta_x == 0) { - sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (x-position %i)", - adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, - dir_text, output->name, abs_pos->x); - } else if (adjacent) { - // so we end up picking the closest adjacent output because - // there is no directly adjacent to the given position - sway_log(L_DEBUG, "%s (%.0fx%.0f+%.0f+%.0f) is %s current output %s (x-position %i, delta: %.0f)", - adjacent->name, adjacent->width, adjacent->height, adjacent->x, adjacent->y, - dir_text, output->name, abs_pos->x, delta_x); - } - break; - default: - sway_abort("Function called with invalid argument."); - break; - } - return adjacent; -} - -void get_absolute_position(swayc_t *container, struct wlc_point *point) { - if (!container || !point) - sway_abort("Need container and wlc_point (was %p, %p).", container, point); - - if (container->type == C_OUTPUT) { - // Coordinates are already absolute. - point->x = container->x; - point->y = container->y; - } else { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); - if (container->type == C_WORKSPACE) { - // Workspace coordinates are actually wrong/arbitrary, but should - // be same as output. - point->x = output->x; - point->y = output->y; - } else { - point->x = output->x + container->x; - point->y = output->y + container->y; - } - } -} - -void get_absolute_center_position(swayc_t *container, struct wlc_point *point) { - get_absolute_position(container, point); - point->x += container->width/2; - point->y += container->height/2; -} - -static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { - swayc_t *a = *(void **)_a; - swayc_t *b = *(void **)_b; - int retval = 0; - - if (isdigit(a->name[0]) && isdigit(b->name[0])) { - int a_num = strtol(a->name, NULL, 10); - int b_num = strtol(b->name, NULL, 10); - retval = (a_num < b_num) ? -1 : (a_num > b_num); - } else if (isdigit(a->name[0])) { - retval = -1; - } else if (isdigit(b->name[0])) { - retval = 1; - } - - return retval; -} - -void sort_workspaces(swayc_t *output) { - list_stable_sort(output->children, sort_workspace_cmp_qsort); -} diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c deleted file mode 100644 index 14cde146..00000000 --- a/sway/tree/workspace.c +++ /dev/null @@ -1,373 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include -#include -#include -#include "sway/ipc-server.h" -#include "sway/workspace.h" -#include "sway/layout.h" -#include "sway/container.h" -#include "sway/handlers.h" -#include "sway/config.h" -#include "sway/focus.h" -#include "stringop.h" -#include "util.h" -#include "list.h" -#include "log.h" -#include "ipc.h" - -char *prev_workspace_name = NULL; -struct workspace_by_number_data { - int len; - const char *cset; - const char *name; -}; - -static bool workspace_valid_on_output(const char *output_name, const char *ws_name) { - int i; - for (i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; - if (strcasecmp(wso->workspace, ws_name) == 0) { - if (strcasecmp(wso->output, output_name) != 0) { - return false; - } - } - } - - return true; -} - -char *workspace_next_name(const char *output_name) { - sway_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", output_name); - int i; - int l = 1; - // Scan all workspace bindings to find the next available workspace name, - // if none are found/available then default to a number - struct sway_mode *mode = config->current_mode; - - int order = INT_MAX; - char *target = NULL; - for (i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = mode->bindings->items[i]; - char *cmdlist = strdup(binding->command); - char *dup = cmdlist; - char *name = NULL; - - // workspace n - char *cmd = argsep(&cmdlist, " "); - if (cmdlist) { - name = argsep(&cmdlist, ",;"); - } - - if (strcmp("workspace", cmd) == 0 && name) { - sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", name); - char *_target = strdup(name); - strip_quotes(_target); - while (isspace(*_target)) - _target++; - - // Make sure that the command references an actual workspace - // not a command about workspaces - if (strcmp(_target, "next") == 0 || - strcmp(_target, "prev") == 0 || - strcmp(_target, "next_on_output") == 0 || - strcmp(_target, "prev_on_output") == 0 || - strcmp(_target, "number") == 0 || - strcmp(_target, "back_and_forth") == 0 || - strcmp(_target, "current") == 0) - { - free(_target); - free(dup); - continue; - } - - // Make sure that the workspace doesn't already exist - if (workspace_by_name(_target)) { - free(_target); - free(dup); - continue; - } - - // make sure that the workspace can appear on the given - // output - if (!workspace_valid_on_output(output_name, _target)) { - free(_target); - free(dup); - continue; - } - - if (binding->order < order) { - order = binding->order; - free(target); - target = _target; - sway_log(L_DEBUG, "Workspace: Found free name %s", _target); - } - } - free(dup); - } - if (target != NULL) { - return target; - } - // As a fall back, get the current number of active workspaces - // and return that + 1 for the next workspace's name - int ws_num = root_container.children->length; - if (ws_num >= 10) { - l = 2; - } else if (ws_num >= 100) { - l = 3; - } - char *name = malloc(l + 1); - if (!name) { - sway_log(L_ERROR, "Could not allocate workspace name"); - return NULL; - } - sprintf(name, "%d", ws_num++); - return name; -} - -swayc_t *workspace_create(const char* name) { - swayc_t *parent; - // Search for workspace<->output pair - int i, e = config->workspace_outputs->length; - for (i = 0; i < e; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; - if (strcasecmp(wso->workspace, name) == 0) - { - // Find output to use if it exists - e = root_container.children->length; - for (i = 0; i < e; ++i) { - parent = root_container.children->items[i]; - if (strcmp(parent->name, wso->output) == 0) { - return new_workspace(parent, name); - } - } - break; - } - } - // Otherwise create a new one - parent = get_focused_container(&root_container); - parent = swayc_parent_by_type(parent, C_OUTPUT); - return new_workspace(parent, name); -} - -static bool _workspace_by_name(swayc_t *view, void *data) { - return (view->type == C_WORKSPACE) && - (strcasecmp(view->name, (char *) data) == 0); -} - -swayc_t *workspace_by_name(const char* name) { - if (strcmp(name, "prev") == 0) { - return workspace_prev(); - } - else if (strcmp(name, "prev_on_output") == 0) { - return workspace_output_prev(); - } - else if (strcmp(name, "next") == 0) { - return workspace_next(); - } - else if (strcmp(name, "next_on_output") == 0) { - return workspace_output_next(); - } - else if (strcmp(name, "current") == 0) { - return swayc_active_workspace(); - } - else { - return swayc_by_test(&root_container, _workspace_by_name, (void *) name); - } -} - -static bool _workspace_by_number(swayc_t *view, void *data) { - if (view->type != C_WORKSPACE) { - return false; - } - struct workspace_by_number_data *wbnd = data; - int a = strspn(view->name, wbnd->cset); - return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; -} -swayc_t *workspace_by_number(const char* name) { - struct workspace_by_number_data wbnd = {0, "1234567890", name}; - wbnd.len = strspn(name, wbnd.cset); - if (wbnd.len <= 0) { - return NULL; - } - return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); -} - -/** - * Get the previous or next workspace on the specified output. - * Wraps around at the end and beginning. - * If next is false, the previous workspace is returned, otherwise the next one is returned. - */ -swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { - if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) { - return NULL; - } - - int i; - for (i = 0; i < output->children->length; i++) { - if (output->children->items[i] == output->focused) { - return output->children->items[wrap(i + (next ? 1 : -1), output->children->length)]; - } - } - - // Doesn't happen, at worst the for loop returns the previously active workspace - return NULL; -} - -/** - * Get the previous or next workspace. If the first/last workspace on an output is active, - * proceed to the previous/next output's previous/next workspace. - * If next is false, the previous workspace is returned, otherwise the next one is returned. - */ -swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { - if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) { - return NULL; - } - - swayc_t *current_output = workspace->parent; - int offset = next ? 1 : -1; - int start = next ? 0 : 1; - int end = next ? (current_output->children->length) - 1 : current_output->children->length; - int i; - for (i = start; i < end; i++) { - if (current_output->children->items[i] == workspace) { - return current_output->children->items[i + offset]; - } - } - - // Given workspace is the first/last on the output, jump to the previous/next output - int num_outputs = root_container.children->length; - for (i = 0; i < num_outputs; i++) { - if (root_container.children->items[i] == current_output) { - swayc_t *next_output = root_container.children->items[wrap(i + offset, num_outputs)]; - return workspace_output_prev_next_impl(next_output, next); - } - } - - // Doesn't happen, at worst the for loop returns the previously active workspace on the active output - return NULL; -} - -swayc_t *workspace_output_next() { - return workspace_output_prev_next_impl(swayc_active_output(), true); -} - -swayc_t *workspace_next() { - return workspace_prev_next_impl(swayc_active_workspace(), true); -} - -swayc_t *workspace_output_prev() { - return workspace_output_prev_next_impl(swayc_active_output(), false); -} - -swayc_t *workspace_prev() { - return workspace_prev_next_impl(swayc_active_workspace(), false); -} - -bool workspace_switch(swayc_t *workspace) { - if (!workspace) { - return false; - } - swayc_t *active_ws = swayc_active_workspace(); - if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) { - swayc_t *new_ws = workspace_by_name(prev_workspace_name); - workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); - } - - if (!prev_workspace_name - || (strcmp(prev_workspace_name, active_ws->name) - && active_ws != workspace)) { - free(prev_workspace_name); - prev_workspace_name = malloc(strlen(active_ws->name) + 1); - if (!prev_workspace_name) { - sway_log(L_ERROR, "Unable to allocate previous workspace name"); - return false; - } - strcpy(prev_workspace_name, active_ws->name); - } - - // move sticky containers - if (swayc_parent_by_type(active_ws, C_OUTPUT) == swayc_parent_by_type(workspace, C_OUTPUT)) { - // don't change list while traversing it, use intermediate list instead - list_t *stickies = create_list(); - for (int i = 0; i < active_ws->floating->length; i++) { - swayc_t *cont = active_ws->floating->items[i]; - if (cont->sticky) { - list_add(stickies, cont); - } - } - for (int i = 0; i < stickies->length; i++) { - swayc_t *cont = stickies->items[i]; - sway_log(L_DEBUG, "Moving sticky container %p to %p:%s", - cont, workspace, workspace->name); - swayc_t *parent = remove_child(cont); - add_floating(workspace, cont); - // Destroy old container if we need to - destroy_container(parent); - } - list_free(stickies); - } - sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); - if (!set_focused_container(get_focused_view(workspace))) { - return false; - } - swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); - arrange_backgrounds(); - arrange_windows(output, -1, -1); - return true; -} - -swayc_t *workspace_for_pid(pid_t pid) { - int i; - swayc_t *ws = NULL; - struct pid_workspace *pw = NULL; - - sway_log(L_DEBUG, "looking for workspace for pid %d", pid); - - // leaving this here as it's useful for debugging - // sway_log(L_DEBUG, "all pid_workspaces"); - // for (int k = 0; k < config->pid_workspaces->length; k++) { - // pw = config->pid_workspaces->items[k]; - // sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added); - // } - - do { - for (i = 0; i < config->pid_workspaces->length; i++) { - pw = config->pid_workspaces->items[i]; - pid_t *pw_pid = pw->pid; - - if (pid == *pw_pid) { - sway_log(L_DEBUG, "found pid_workspace for pid %d, workspace %s", pid, pw->workspace); - break; // out of for loop - } - - pw = NULL; - } - - if (pw) { - break; // out of do-while loop - } - - pid = get_parent_pid(pid); - // no sense in looking for matches for pid 0. - // also, if pid == getpid(), that is the compositor's - // pid, which definitely isn't helpful - } while (pid > 0 && pid != getpid()); - - if (pw) { - ws = workspace_by_name(pw->workspace); - - if (!ws) { - sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid); - ws = workspace_create(pw->workspace); - } - - list_del(config->pid_workspaces, i); - } - - return ws; -} -- cgit v1.2.3 From d7d21bb0f895248cafefc3d12e4aed033a8e5d17 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 22 Nov 2017 21:20:41 -0500 Subject: Add initial command subsystem (untested) Need to spin up the IPC server to test this --- include/sway/commands.h | 185 ++++++++++++++++++++++++++++++++++++ sway/CMakeLists.txt | 3 + sway/commands.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++ sway/commands/exit.c | 19 ++++ 4 files changed, 454 insertions(+) create mode 100644 include/sway/commands.h create mode 100644 sway/commands.c create mode 100644 sway/commands/exit.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h new file mode 100644 index 00000000..df5c6859 --- /dev/null +++ b/include/sway/commands.h @@ -0,0 +1,185 @@ +#ifndef _SWAY_COMMANDS_H +#define _SWAY_COMMANDS_H + +/** + * Indicates the result of a command's execution. + */ +enum cmd_status { + CMD_SUCCESS, /**< The command was successful */ + CMD_FAILURE, /**< The command resulted in an error */ + CMD_INVALID, /**< Unknown command or parser error */ + CMD_DEFER, /**< Command execution deferred */ + // Config Blocks + CMD_BLOCK_END, + CMD_BLOCK_MODE, + CMD_BLOCK_BAR, + CMD_BLOCK_BAR_COLORS, + CMD_BLOCK_INPUT, + CMD_BLOCK_COMMANDS, + CMD_BLOCK_IPC, + CMD_BLOCK_IPC_EVENTS, +}; + +/** + * Stores the result of executing a command. + */ +struct cmd_results { + enum cmd_status status; + char *input; + /** + * Human friendly error message, or NULL on success + */ + char *error; +}; + +enum expected_args { + EXPECTED_MORE_THAN, + EXPECTED_AT_LEAST, + EXPECTED_LESS_THAN, + EXPECTED_EQUAL_TO +}; + +struct cmd_results *checkarg(int argc, const char *name, + enum expected_args type, int val); + +/** + * Parse and handles a command. + */ +struct cmd_results *handle_command(char *command); +/** + * Allocates a cmd_results object. + */ +struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *error, ...); +/** + * Frees a cmd_results object. + */ +void free_cmd_results(struct cmd_results *results); +/** + * Serializes cmd_results to a JSON string. + * + * Free the JSON string later on. + */ +const char *cmd_results_to_json(struct cmd_results *results); + +typedef struct cmd_results *sway_cmd(int argc, char **argv); + +sway_cmd cmd_assign; +sway_cmd cmd_bar; +sway_cmd cmd_bindcode; +sway_cmd cmd_bindsym; +sway_cmd cmd_border; +sway_cmd cmd_client_focused; +sway_cmd cmd_client_focused_inactive; +sway_cmd cmd_client_unfocused; +sway_cmd cmd_client_urgent; +sway_cmd cmd_client_placeholder; +sway_cmd cmd_client_background; +sway_cmd cmd_clipboard; +sway_cmd cmd_commands; +sway_cmd cmd_debuglog; +sway_cmd cmd_default_border; +sway_cmd cmd_default_floating_border; +sway_cmd cmd_exec; +sway_cmd cmd_exec_always; +sway_cmd cmd_exit; +sway_cmd cmd_floating; +sway_cmd cmd_floating_maximum_size; +sway_cmd cmd_floating_minimum_size; +sway_cmd cmd_floating_mod; +sway_cmd cmd_floating_scroll; +sway_cmd cmd_focus; +sway_cmd cmd_focus_follows_mouse; +sway_cmd cmd_font; +sway_cmd cmd_for_window; +sway_cmd cmd_force_focus_wrapping; +sway_cmd cmd_fullscreen; +sway_cmd cmd_gaps; +sway_cmd cmd_hide_edge_borders; +sway_cmd cmd_include; +sway_cmd cmd_input; +sway_cmd cmd_ipc; +sway_cmd cmd_kill; +sway_cmd cmd_layout; +sway_cmd cmd_log_colors; +sway_cmd cmd_mark; +sway_cmd cmd_mode; +sway_cmd cmd_mouse_warping; +sway_cmd cmd_move; +sway_cmd cmd_new_float; +sway_cmd cmd_new_window; +sway_cmd cmd_no_focus; +sway_cmd cmd_orientation; +sway_cmd cmd_output; +sway_cmd cmd_permit; +sway_cmd cmd_reject; +sway_cmd cmd_reload; +sway_cmd cmd_resize; +sway_cmd cmd_scratchpad; +sway_cmd cmd_seamless_mouse; +sway_cmd cmd_set; +sway_cmd cmd_show_marks; +sway_cmd cmd_smart_gaps; +sway_cmd cmd_split; +sway_cmd cmd_splith; +sway_cmd cmd_splitt; +sway_cmd cmd_splitv; +sway_cmd cmd_sticky; +sway_cmd cmd_unmark; +sway_cmd cmd_workspace; +sway_cmd cmd_ws_auto_back_and_forth; +sway_cmd cmd_workspace_layout; + +sway_cmd bar_cmd_activate_button; +sway_cmd bar_cmd_binding_mode_indicator; +sway_cmd bar_cmd_bindsym; +sway_cmd bar_cmd_colors; +sway_cmd bar_cmd_context_button; +sway_cmd bar_cmd_font; +sway_cmd bar_cmd_mode; +sway_cmd bar_cmd_modifier; +sway_cmd bar_cmd_output; +sway_cmd bar_cmd_height; +sway_cmd bar_cmd_hidden_state; +sway_cmd bar_cmd_icon_theme; +sway_cmd bar_cmd_id; +sway_cmd bar_cmd_position; +sway_cmd bar_cmd_secondary_button; +sway_cmd bar_cmd_separator_symbol; +sway_cmd bar_cmd_status_command; +sway_cmd bar_cmd_pango_markup; +sway_cmd bar_cmd_strip_workspace_numbers; +sway_cmd bar_cmd_swaybar_command; +sway_cmd bar_cmd_tray_output; +sway_cmd bar_cmd_tray_padding; +sway_cmd bar_cmd_wrap_scroll; +sway_cmd bar_cmd_workspace_buttons; + +sway_cmd bar_colors_cmd_active_workspace; +sway_cmd bar_colors_cmd_background; +sway_cmd bar_colors_cmd_focused_background; +sway_cmd bar_colors_cmd_binding_mode; +sway_cmd bar_colors_cmd_focused_workspace; +sway_cmd bar_colors_cmd_inactive_workspace; +sway_cmd bar_colors_cmd_separator; +sway_cmd bar_colors_cmd_focused_separator; +sway_cmd bar_colors_cmd_statusline; +sway_cmd bar_colors_cmd_focused_statusline; +sway_cmd bar_colors_cmd_urgent_workspace; + +sway_cmd input_cmd_accel_profile; +sway_cmd input_cmd_click_method; +sway_cmd input_cmd_drag_lock; +sway_cmd input_cmd_dwt; +sway_cmd input_cmd_events; +sway_cmd input_cmd_left_handed; +sway_cmd input_cmd_middle_emulation; +sway_cmd input_cmd_natural_scroll; +sway_cmd input_cmd_pointer_accel; +sway_cmd input_cmd_scroll_method; +sway_cmd input_cmd_tap; + +sway_cmd cmd_ipc_cmd; +sway_cmd cmd_ipc_events; +sway_cmd cmd_ipc_event_cmd; + +#endif diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt index 6d520e76..b85d619f 100644 --- a/sway/CMakeLists.txt +++ b/sway/CMakeLists.txt @@ -14,6 +14,9 @@ add_executable(sway desktop/output.c desktop/xdg_shell_v6.c + commands.c + commands/exit.c + tree/container.c tree/layout.c tree/workspace.c diff --git a/sway/commands.c b/sway/commands.c new file mode 100644 index 00000000..f8c2fd99 --- /dev/null +++ b/sway/commands.c @@ -0,0 +1,247 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "stringop.h" +#include "log.h" + +struct cmd_handler { + char *command; + sway_cmd *handle; +}; + +// Returns error object, or NULL if check succeeds. +struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { + struct cmd_results *error = NULL; + switch (type) { + case EXPECTED_MORE_THAN: + if (argc > val) { + return NULL; + } + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected more than %d argument%s, got %d)", + name, val, (char*[2]){"s", ""}[argc==1], argc); + break; + case EXPECTED_AT_LEAST: + if (argc >= val) { + return NULL; + } + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected at least %d argument%s, got %d)", + name, val, (char*[2]){"s", ""}[argc==1], argc); + break; + case EXPECTED_LESS_THAN: + if (argc < val) { + return NULL; + }; + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected less than %d argument%s, got %d)", + name, val, (char*[2]){"s", ""}[argc==1], argc); + break; + case EXPECTED_EQUAL_TO: + if (argc == val) { + return NULL; + }; + error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(expected %d arguments, got %d)", name, val, argc); + break; + } + return error; +} + +/** + * Check and add color to buffer. + * + * return error object, or NULL if color is valid. + */ +struct cmd_results *add_color(const char *name, char *buffer, const char *color) { + int len = strlen(color); + if (len != 7 && len != 9) { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + + if (color[0] != '#') { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + + int i; + for (i = 1; i < len; ++i) { + if (!isxdigit(color[i])) { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + } + + // copy color to buffer + strncpy(buffer, color, len); + // add default alpha channel if color was defined without it + if (len == 7) { + buffer[7] = 'f'; + buffer[8] = 'f'; + } + buffer[9] = '\0'; + + return NULL; +} + +/* Keep alphabetized */ +static struct cmd_handler handlers[] = { + { "exit", cmd_exit }, +}; + +static int handler_compare(const void *_a, const void *_b) { + const struct cmd_handler *a = _a; + const struct cmd_handler *b = _b; + return strcasecmp(a->command, b->command); +} + +static struct cmd_handler *find_handler(char *line, enum cmd_status block) { + struct cmd_handler d = { .command=line }; + struct cmd_handler *res = NULL; + sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT); + /* TODO + if (block == CMD_BLOCK_BAR) { + res = bsearch(&d, bar_handlers, + sizeof(bar_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_BAR_COLORS){ + res = bsearch(&d, bar_colors_handlers, + sizeof(bar_colors_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_INPUT) { + res = bsearch(&d, input_handlers, + sizeof(input_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_IPC) { + res = bsearch(&d, ipc_handlers, + sizeof(ipc_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_IPC_EVENTS) { + res = bsearch(&d, ipc_event_handlers, + sizeof(ipc_event_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else { + */ + res = bsearch(&d, handlers, + sizeof(handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + //} + return res; +} + +struct cmd_results *handle_command(char *_exec) { + // Even though this function will process multiple commands we will only + // return the last error, if any (for now). (Since we have access to an + // error string we could e.g. concatonate all errors there.) + struct cmd_results *results = NULL; + char *exec = strdup(_exec); + char *head = exec; + char *cmdlist; + char *cmd; + + head = exec; + do { + // Split command list + cmdlist = argsep(&head, ";"); + cmdlist += strspn(cmdlist, whitespace); + do { + // Split commands + cmd = argsep(&cmdlist, ","); + cmd += strspn(cmd, whitespace); + if (strcmp(cmd, "") == 0) { + sway_log(L_INFO, "Ignoring empty command."); + continue; + } + sway_log(L_INFO, "Handling command '%s'", cmd); + //TODO better handling of argv + int argc; + char **argv = split_args(cmd, &argc); + if (strcmp(argv[0], "exec") != 0) { + int i; + for (i = 1; i < argc; ++i) { + if (*argv[i] == '\"' || *argv[i] == '\'') { + strip_quotes(argv[i]); + } + } + } + struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END); + if (!handler) { + if (results) { + free_cmd_results(results); + } + results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command"); + free_argv(argc, argv); + goto cleanup; + } + free_argv(argc, argv); + } while(cmdlist); + } while(head); +cleanup: + free(exec); + if (!results) { + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + return results; +} + +struct cmd_results *cmd_results_new(enum cmd_status status, + const char *input, const char *format, ...) { + struct cmd_results *results = malloc(sizeof(struct cmd_results)); + if (!results) { + sway_log(L_ERROR, "Unable to allocate command results"); + return NULL; + } + results->status = status; + if (input) { + results->input = strdup(input); // input is the command name + } else { + results->input = NULL; + } + if (format) { + char *error = malloc(256); + va_list args; + va_start(args, format); + if (error) { + vsnprintf(error, 256, format, args); + } + va_end(args); + results->error = error; + } else { + results->error = NULL; + } + return results; +} + +void free_cmd_results(struct cmd_results *results) { + if (results->input) { + free(results->input); + } + if (results->error) { + free(results->error); + } + free(results); +} + +const char *cmd_results_to_json(struct cmd_results *results) { + json_object *result_array = json_object_new_array(); + json_object *root = json_object_new_object(); + json_object_object_add(root, "success", + json_object_new_boolean(results->status == CMD_SUCCESS)); + if (results->input) { + json_object_object_add( + root, "input", json_object_new_string(results->input)); + } + if (results->error) { + json_object_object_add( + root, "error", json_object_new_string(results->error)); + } + json_object_array_add(result_array, root); + const char *json = json_object_to_json_string(result_array); + free(result_array); + free(root); + return json; +} diff --git a/sway/commands/exit.c b/sway/commands/exit.c new file mode 100644 index 00000000..d294e344 --- /dev/null +++ b/sway/commands/exit.c @@ -0,0 +1,19 @@ +#include +#include "sway/commands.h" + +void sway_terminate(int exit_code); + +struct cmd_results *cmd_exit(int argc, char **argv) { + struct cmd_results *error = NULL; + /* TODO + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file."); + } + */ + if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) { + return error; + } + sway_terminate(0); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + -- cgit v1.2.3 From 514c819ff98d72880c5b7e0beb7bd9ca866bc1ad Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 4 Dec 2017 22:43:49 +0100 Subject: Add exec and exec_always commands --- sway/commands.c | 2 ++ sway/commands/exec.c | 16 +++++++++ sway/commands/exec_always.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ sway/meson.build | 2 ++ 4 files changed, 105 insertions(+) create mode 100644 sway/commands/exec.c create mode 100644 sway/commands/exec_always.c (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index 94a45253..5fdcdbb6 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -90,6 +90,8 @@ struct cmd_results *add_color(const char *name, char *buffer, const char *color) /* Keep alphabetized */ static struct cmd_handler handlers[] = { + { "exec", cmd_exec }, + { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, }; diff --git a/sway/commands/exec.c b/sway/commands/exec.c new file mode 100644 index 00000000..dd71500a --- /dev/null +++ b/sway/commands/exec.c @@ -0,0 +1,16 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_exec(int argc, char **argv) { + // TODO: config + /*if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); + if (config->reloading) { + char *args = join_args(argv, argc); + sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args); + free(args); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + }*/ + return cmd_exec_always(argc, argv); +} diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c new file mode 100644 index 00000000..0a252e7b --- /dev/null +++ b/sway/commands/exec_always.c @@ -0,0 +1,85 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/workspace.h" +#include "sway/container.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_exec_always(int argc, char **argv) { + struct cmd_results *error = NULL; + // TODO: config + //if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL); + if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) { + return error; + } + + char *tmp = NULL; + if (strcmp((char*)*argv, "--no-startup-id") == 0) { + sway_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored."); + if ((error = checkarg(argc - 1, "exec_always", EXPECTED_MORE_THAN, 0))) { + return error; + } + + tmp = join_args(argv + 1, argc - 1); + } else { + tmp = join_args(argv, argc); + } + + // Put argument into cmd array + char cmd[4096]; + strncpy(cmd, tmp, sizeof(cmd)); + cmd[sizeof(cmd) - 1] = 0; + free(tmp); + sway_log(L_DEBUG, "Executing %s", cmd); + + int fd[2]; + if (pipe(fd) != 0) { + sway_log(L_ERROR, "Unable to create pipe for fork"); + } + + pid_t pid; + pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space + if (!child) { + return cmd_results_new(CMD_FAILURE, "exec_always", "Unable to allocate child pid"); + } + // Fork process + if ((pid = fork()) == 0) { + // Fork child process again + setsid(); + if ((*child = fork()) == 0) { + execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL); + // Not reached + } + close(fd[0]); + ssize_t s = 0; + while ((size_t)s < sizeof(pid_t)) { + s += write(fd[1], ((uint8_t *)child) + s, sizeof(pid_t) - s); + } + close(fd[1]); + _exit(0); // Close child process + } else if (pid < 0) { + free(child); + return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed"); + } + close(fd[1]); // close write + ssize_t s = 0; + while ((size_t)s < sizeof(pid_t)) { + s += read(fd[0], ((uint8_t *)child) + s, sizeof(pid_t) - s); + } + close(fd[0]); + // cleanup child process + wait(0); + if (*child > 0) { + sway_log(L_DEBUG, "Child process created with pid %d", *child); + // TODO: add PID to active workspace + } else { + free(child); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index 9f92f5d1..b224f15f 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -3,6 +3,8 @@ sway_sources = files( 'server.c', 'commands.c', 'commands/exit.c', + 'commands/exec.c', + 'commands/exec_always.c', 'ipc-json.c', 'ipc-server.c', 'desktop/output.c', -- cgit v1.2.3 From 90f7f1a0e61fa20ed1b74b9df057aa70abc791ed Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 5 Dec 2017 10:40:55 +0100 Subject: Add minimal config subsystem --- include/sway/commands.h | 10 + include/sway/config.h | 405 +++++++++++++++++++++++++++++++++++ meson.build | 2 + sway/commands.c | 132 ++++++++++++ sway/commands/exec.c | 6 +- sway/commands/exec_always.c | 6 +- sway/commands/exit.c | 4 +- sway/config.c | 504 ++++++++++++++++++++++++++++++++++++++++++++ sway/main.c | 21 +- sway/meson.build | 2 + sway/security.c | 18 ++ 11 files changed, 1091 insertions(+), 19 deletions(-) create mode 100644 include/sway/config.h create mode 100644 sway/config.c create mode 100644 sway/security.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index df5c6859..b1f0423d 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -46,6 +46,16 @@ struct cmd_results *checkarg(int argc, const char *name, * Parse and handles a command. */ struct cmd_results *handle_command(char *command); +/** + * Parse and handles a command during config file loading. + * + * Do not use this under normal conditions. + */ +struct cmd_results *config_command(char *command, enum cmd_status block); +/* + * Parses a command policy rule. + */ +struct cmd_results *config_commands_command(char *exec); /** * Allocates a cmd_results object. */ diff --git a/include/sway/config.h b/include/sway/config.h new file mode 100644 index 00000000..56b4e637 --- /dev/null +++ b/include/sway/config.h @@ -0,0 +1,405 @@ +#ifndef _SWAY_CONFIG_H +#define _SWAY_CONFIG_H + +#define PID_WORKSPACE_TIMEOUT 60 + +#include +#include +#include +#include +#include +#include +#include "list.h" +#include "layout.h" +#include "container.h" + +/** + * Describes a variable created via the `set` command. + */ +struct sway_variable { + char *name; + char *value; +}; + +/** + * A key binding and an associated command. + */ +struct sway_binding { + int order; + bool release; + bool bindcode; + list_t *keys; + uint32_t modifiers; + char *command; +}; + +/** + * A mouse binding and an associated command. + */ +struct sway_mouse_binding { + uint32_t button; + char *command; +}; + +/** + * A "mode" of keybindings created via the `mode` command. + */ +struct sway_mode { + char *name; + list_t *bindings; +}; + +/** + * libinput options for input devices + */ +struct input_config { + char *identifier; + + int accel_profile; + int click_method; + int drag_lock; + int dwt; + int left_handed; + int middle_emulation; + int natural_scroll; + float pointer_accel; + int scroll_method; + int send_events; + int tap; + + bool capturable; + struct wlc_geometry region; +}; + +/** + * Size and position configuration for a particular output. + * + * This is set via the `output` command. + */ +struct output_config { + char *name; + int enabled; + int width, height; + int x, y; + int scale; + char *background; + char *background_option; +}; + +/** + * Maps a workspace name to an output name. + * + * Set via `workspace output ` + */ +struct workspace_output { + char *output; + char *workspace; +}; + +struct pid_workspace { + pid_t *pid; + char *workspace; + time_t *time_added; +}; + +struct bar_config { + /** + * One of "dock", "hide", "invisible" + * + * Always visible in dock mode. Visible only when modifier key is held in hide mode. + * Never visible in invisible mode. + */ + char *mode; + /** + * One of "show" or "hide". + * + * In "show" mode, it will always be shown on top of the active workspace. + */ + char *hidden_state; + /** + * Id name used to identify the bar through IPC. + * + * Defaults to bar-x, where x corresponds to the position of the + * embedding bar block in the config file (bar-0, bar-1, ...). + */ + char *id; + uint32_t modifier; + list_t *outputs; + //enum desktop_shell_panel_position position; // TODO + list_t *bindings; + char *status_command; + bool pango_markup; + char *swaybar_command; + char *font; + int height; // -1 not defined + +#ifdef ENABLE_TRAY + // Tray + char *tray_output; + char *icon_theme; + uint32_t tray_padding; + uint32_t activate_button; + uint32_t context_button; + uint32_t secondary_button; +#endif + + bool workspace_buttons; + bool wrap_scroll; + char *separator_symbol; + bool strip_workspace_numbers; + bool binding_mode_indicator; + bool verbose; + pid_t pid; + struct { + char *background; + char *statusline; + char *separator; + char *focused_background; + char *focused_statusline; + char *focused_separator; + char *focused_workspace_border; + char *focused_workspace_bg; + char *focused_workspace_text; + char *active_workspace_border; + char *active_workspace_bg; + char *active_workspace_text; + char *inactive_workspace_border; + char *inactive_workspace_bg; + char *inactive_workspace_text; + char *urgent_workspace_border; + char *urgent_workspace_bg; + char *urgent_workspace_text; + char *binding_mode_border; + char *binding_mode_bg; + char *binding_mode_text; + } colors; +}; + +struct border_colors { + uint32_t border; + uint32_t background; + uint32_t text; + uint32_t indicator; + uint32_t child_border; +}; + +enum edge_border_types { + E_NONE, /**< Don't hide edge borders */ + E_VERTICAL, /**< hide vertical edge borders */ + E_HORIZONTAL, /**< hide horizontal edge borders */ + E_BOTH, /**< hide vertical and horizontal edge borders */ + E_SMART /**< hide both if precisely one window is present in workspace */ +}; + +enum command_context { + CONTEXT_CONFIG = 1, + CONTEXT_BINDING = 2, + CONTEXT_IPC = 4, + CONTEXT_CRITERIA = 8, + CONTEXT_ALL = 0xFFFFFFFF, +}; + +struct command_policy { + char *command; + uint32_t context; +}; + +enum secure_feature { + FEATURE_LOCK = 1, + FEATURE_PANEL = 2, + FEATURE_BACKGROUND = 4, + FEATURE_SCREENSHOT = 8, + FEATURE_FULLSCREEN = 16, + FEATURE_KEYBOARD = 32, + FEATURE_MOUSE = 64, +}; + +struct feature_policy { + char *program; + uint32_t features; +}; + +enum ipc_feature { + IPC_FEATURE_COMMAND = 1, + IPC_FEATURE_GET_WORKSPACES = 2, + IPC_FEATURE_GET_OUTPUTS = 4, + IPC_FEATURE_GET_TREE = 8, + IPC_FEATURE_GET_MARKS = 16, + IPC_FEATURE_GET_BAR_CONFIG = 32, + IPC_FEATURE_GET_VERSION = 64, + IPC_FEATURE_GET_INPUTS = 128, + IPC_FEATURE_EVENT_WORKSPACE = 256, + IPC_FEATURE_EVENT_OUTPUT = 512, + IPC_FEATURE_EVENT_MODE = 1024, + IPC_FEATURE_EVENT_WINDOW = 2048, + IPC_FEATURE_EVENT_BINDING = 4096, + IPC_FEATURE_EVENT_INPUT = 8192, + IPC_FEATURE_GET_CLIPBOARD = 16384, + + IPC_FEATURE_ALL_COMMANDS = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 16384, + IPC_FEATURE_ALL_EVENTS = 256 | 512 | 1024 | 2048 | 4096 | 8192, + + IPC_FEATURE_ALL = IPC_FEATURE_ALL_COMMANDS | IPC_FEATURE_ALL_EVENTS, +}; + +struct ipc_policy { + char *program; + uint32_t features; +}; + +/** + * The configuration struct. The result of loading a config file. + */ +struct sway_config { + list_t *symbols; + list_t *modes; + list_t *bars; + list_t *cmd_queue; + list_t *workspace_outputs; + list_t *pid_workspaces; + list_t *output_configs; + list_t *input_configs; + list_t *criteria; + list_t *no_focus; + list_t *active_bar_modifiers; + struct sway_mode *current_mode; + struct bar_config *current_bar; + uint32_t floating_mod; + uint32_t dragging_key; + uint32_t resizing_key; + char *floating_scroll_up_cmd; + char *floating_scroll_down_cmd; + char *floating_scroll_left_cmd; + char *floating_scroll_right_cmd; + enum swayc_layouts default_orientation; + enum swayc_layouts default_layout; + char *font; + int font_height; + + // Flags + bool focus_follows_mouse; + bool mouse_warping; + bool force_focus_wrapping; + bool active; + bool failed; + bool reloading; + bool reading; + bool auto_back_and_forth; + bool seamless_mouse; + bool show_marks; + + bool edge_gaps; + bool smart_gaps; + int gaps_inner; + int gaps_outer; + + list_t *config_chain; + const char *current_config; + + enum swayc_border_types border; + enum swayc_border_types floating_border; + int border_thickness; + int floating_border_thickness; + enum edge_border_types hide_edge_borders; + + // border colors + struct { + struct border_colors focused; + struct border_colors focused_inactive; + struct border_colors unfocused; + struct border_colors urgent; + struct border_colors placeholder; + uint32_t background; + } border_colors; + + // floating view + int32_t floating_maximum_width; + int32_t floating_maximum_height; + int32_t floating_minimum_width; + int32_t floating_minimum_height; + + // Security + list_t *command_policies; + list_t *feature_policies; + list_t *ipc_policies; +}; + +void pid_workspace_add(struct pid_workspace *pw); +void free_pid_workspace(struct pid_workspace *pw); + +/** + * Loads the main config from the given path. is_active should be true when + * reloading the config. + */ +bool load_main_config(const char *path, bool is_active); + +/** + * Loads an included config. Can only be used after load_main_config. + */ +bool load_include_configs(const char *path, struct sway_config *config); + +/** + * Reads the config from the given FILE. + */ +bool read_config(FILE *file, struct sway_config *config); + +/** + * Free config struct + */ +void free_config(struct sway_config *config); +/** + * Does variable replacement for a string based on the config's currently loaded variables. + */ +char *do_var_replacement(char *str); + +struct cmd_results *check_security_config(); + +int input_identifier_cmp(const void *item, const void *data); +void merge_input_config(struct input_config *dst, struct input_config *src); +void apply_input_config(struct input_config *ic, struct libinput_device *dev); +void free_input_config(struct input_config *ic); + +int output_name_cmp(const void *item, const void *data); +void merge_output_config(struct output_config *dst, struct output_config *src); +/** Sets up a WLC output handle based on a given output_config. + */ +void apply_output_config(struct output_config *oc, swayc_t *output); +void free_output_config(struct output_config *oc); + +/** + * Updates the list of active bar modifiers + */ +void update_active_bar_modifiers(void); + +int workspace_output_cmp_workspace(const void *a, const void *b); + +int sway_binding_cmp(const void *a, const void *b); +int sway_binding_cmp_qsort(const void *a, const void *b); +int sway_binding_cmp_keys(const void *a, const void *b); +void free_sway_binding(struct sway_binding *sb); +struct sway_binding *sway_binding_dup(struct sway_binding *sb); + +int sway_mouse_binding_cmp(const void *a, const void *b); +int sway_mouse_binding_cmp_qsort(const void *a, const void *b); +int sway_mouse_binding_cmp_buttons(const void *a, const void *b); +void free_sway_mouse_binding(struct sway_mouse_binding *smb); + +void load_swaybars(); +void terminate_swaybg(pid_t pid); + +/** + * Allocate and initialize default bar configuration. + */ +struct bar_config *default_bar_config(void); + +/** + * Global config singleton. + */ +extern struct sway_config *config; + +/** + * Config file currently being read. + */ +extern const char *current_config_path; + +#endif diff --git a/meson.build b/meson.build index 695e237e..8e7b98ed 100644 --- a/meson.build +++ b/meson.build @@ -65,6 +65,8 @@ if a2x.found() endforeach endif +add_project_arguments('-DSYSCONFDIR="/@0@"'.format(sysconfdir), language : 'c') + version = get_option('sway_version') if version != '' version = '"@0@"'.format(version) diff --git a/sway/commands.c b/sway/commands.c index 5fdcdbb6..17638129 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -7,6 +7,8 @@ #include #include #include "sway/commands.h" +#include "sway/config.h" +#include "sway/security.h" #include "stringop.h" #include "log.h" @@ -200,6 +202,136 @@ cleanup: return results; } +// this is like handle_command above, except: +// 1) it ignores empty commands (empty lines) +// 2) it does variable substitution +// 3) it doesn't split commands (because the multiple commands are supposed to +// be chained together) +// 4) handle_command handles all state internally while config_command has some +// state handled outside (notably the block mode, in read_config) +struct cmd_results *config_command(char *exec, enum cmd_status block) { + struct cmd_results *results = NULL; + int argc; + char **argv = split_args(exec, &argc); + if (!argc) { + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + goto cleanup; + } + + sway_log(L_INFO, "handling config command '%s'", exec); + // Endblock + if (**argv == '}') { + results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); + goto cleanup; + } + struct cmd_handler *handler = find_handler(argv[0], block); + if (!handler) { + char *input = argv[0] ? argv[0] : "(empty)"; + results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); + goto cleanup; + } + int i; + // Var replacement, for all but first argument of set + // TODO commands + for (i = /*handler->handle == cmd_set ? 2 :*/ 1; i < argc; ++i) { + argv[i] = do_var_replacement(argv[i]); + unescape_string(argv[i]); + } + /* Strip quotes for first argument. + * TODO This part needs to be handled much better */ + if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) { + strip_quotes(argv[1]); + } + if (handler->handle) { + results = handler->handle(argc-1, argv+1); + } else { + results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented"); + } + +cleanup: + free_argv(argc, argv); + return results; +} + +struct cmd_results *config_commands_command(char *exec) { + struct cmd_results *results = NULL; + int argc; + char **argv = split_args(exec, &argc); + if (!argc) { + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + goto cleanup; + } + + // Find handler for the command this is setting a policy for + char *cmd = argv[0]; + + if (strcmp(cmd, "}") == 0) { + results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); + goto cleanup; + } + + struct cmd_handler *handler = find_handler(cmd, CMD_BLOCK_END); + if (!handler && strcmp(cmd, "*") != 0) { + char *input = cmd ? cmd : "(empty)"; + results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); + goto cleanup; + } + + enum command_context context = 0; + + struct { + char *name; + enum command_context context; + } context_names[] = { + { "config", CONTEXT_CONFIG }, + { "binding", CONTEXT_BINDING }, + { "ipc", CONTEXT_IPC }, + { "criteria", CONTEXT_CRITERIA }, + { "all", CONTEXT_ALL }, + }; + + for (int i = 1; i < argc; ++i) { + size_t j; + for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) { + if (strcmp(context_names[j].name, argv[i]) == 0) { + break; + } + } + if (j == sizeof(context_names) / sizeof(context_names[0])) { + results = cmd_results_new(CMD_INVALID, cmd, + "Invalid command context %s", argv[i]); + goto cleanup; + } + context |= context_names[j].context; + } + + struct command_policy *policy = NULL; + for (int i = 0; i < config->command_policies->length; ++i) { + struct command_policy *p = config->command_policies->items[i]; + if (strcmp(p->command, cmd) == 0) { + policy = p; + break; + } + } + if (!policy) { + policy = alloc_command_policy(cmd); + sway_assert(policy, "Unable to allocate security policy"); + if (policy) { + list_add(config->command_policies, policy); + } + } + policy->context = context; + + sway_log(L_INFO, "Set command policy for %s to %d", + policy->command, policy->context); + + results = cmd_results_new(CMD_SUCCESS, NULL, NULL); + +cleanup: + free_argv(argc, argv); + return results; +} + struct cmd_results *cmd_results_new(enum cmd_status status, const char *input, const char *format, ...) { struct cmd_results *results = malloc(sizeof(struct cmd_results)); diff --git a/sway/commands/exec.c b/sway/commands/exec.c index dd71500a..fbbc4941 100644 --- a/sway/commands/exec.c +++ b/sway/commands/exec.c @@ -1,16 +1,16 @@ #include #include "sway/commands.h" +#include "sway/config.h" #include "log.h" #include "stringop.h" struct cmd_results *cmd_exec(int argc, char **argv) { - // TODO: config - /*if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); + if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); if (config->reloading) { char *args = join_args(argv, argc); sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args); free(args); return cmd_results_new(CMD_SUCCESS, NULL, NULL); - }*/ + } return cmd_exec_always(argc, argv); } diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index 0a252e7b..9527a487 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -5,15 +5,15 @@ #include #include #include "sway/commands.h" -#include "sway/workspace.h" +#include "sway/config.h" #include "sway/container.h" +#include "sway/workspace.h" #include "log.h" #include "stringop.h" struct cmd_results *cmd_exec_always(int argc, char **argv) { struct cmd_results *error = NULL; - // TODO: config - //if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL); + if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL); if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) { return error; } diff --git a/sway/commands/exit.c b/sway/commands/exit.c index d294e344..4bb6a97b 100644 --- a/sway/commands/exit.c +++ b/sway/commands/exit.c @@ -1,19 +1,17 @@ #include #include "sway/commands.h" +#include "sway/config.h" void sway_terminate(int exit_code); struct cmd_results *cmd_exit(int argc, char **argv) { struct cmd_results *error = NULL; - /* TODO if (config->reading) { return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file."); } - */ if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) { return error; } sway_terminate(0); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } - diff --git a/sway/config.c b/sway/config.c new file mode 100644 index 00000000..475e8b04 --- /dev/null +++ b/sway/config.c @@ -0,0 +1,504 @@ +#define _POSIX_C_SOURCE 200809L +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __linux__ +#include +#elif __FreeBSD__ +#include +#endif +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/layout.h" +#include "readline.h" +#include "stringop.h" +#include "list.h" +#include "log.h" + +struct sway_config *config = NULL; + +void free_config(struct sway_config *config) { + // TODO +} + +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 = BTN_LEFT; + config->resizing_key = BTN_RIGHT; + 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; + // TODO: border + //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 bool file_exists(const char *path) { + return path && access(path, R_OK) != -1; +} + +static char *get_config_path(void) { + static const char *config_paths[] = { + "$HOME/.sway/config", + "$XDG_CONFIG_HOME/sway/config", + "$HOME/.i3/config", + "$XDG_CONFIG_HOME/i3/config", + SYSCONFDIR "/sway/config", + SYSCONFDIR "/i3/config", + }; + + 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) { + 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) { + // TODO: bar + //update_active_bar_modifiers(); + } + + return success; +} + +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"); + // TODO: input + //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; +} + +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; +} diff --git a/sway/main.c b/sway/main.c index bc107309..bc843591 100644 --- a/sway/main.c +++ b/sway/main.c @@ -15,6 +15,7 @@ #include #include #endif +#include "sway/config.h" #include "sway/server.h" #include "sway/layout.h" #include "sway/ipc-server.h" @@ -388,14 +389,14 @@ int main(int argc, char **argv) { ipc_init(&server); log_env(); - //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); @@ -411,9 +412,9 @@ int main(int argc, char **argv) { ipc_terminate(); - //if (config) { - // free_config(config); - //} + if (config) { + free_config(config); + } return exit_value; } diff --git a/sway/meson.build b/sway/meson.build index b224f15f..84f48137 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -5,12 +5,14 @@ sway_sources = files( 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', + 'config.c', 'ipc-json.c', 'ipc-server.c', 'desktop/output.c', 'desktop/wl_shell.c', 'desktop/xdg_shell_v6.c', 'desktop/xwayland.c', + 'security.c', 'tree/container.c', 'tree/layout.c', 'tree/workspace.c', diff --git a/sway/security.c b/sway/security.c new file mode 100644 index 00000000..cc0d3f66 --- /dev/null +++ b/sway/security.c @@ -0,0 +1,18 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include "sway/security.h" + +struct command_policy *alloc_command_policy(const char *command) { + struct command_policy *policy = malloc(sizeof(struct command_policy)); + if (!policy) { + return NULL; + } + policy->command = strdup(command); + if (!policy->command) { + free(policy); + return NULL; + } + policy->context = 0; + return policy; +} -- cgit v1.2.3 From 9aeda824779d3174db01b6445fa349def031a035 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 5 Dec 2017 18:47:57 +0100 Subject: Add include command --- sway/commands.c | 1 + sway/commands/include.c | 15 +++++++++ sway/config.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ sway/meson.build | 1 + 4 files changed, 106 insertions(+) create mode 100644 sway/commands/include.c (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index 17638129..05a66a7f 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -95,6 +95,7 @@ static struct cmd_handler handlers[] = { { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, + { "include", cmd_include }, }; static int handler_compare(const void *_a, const void *_b) { diff --git a/sway/commands/include.c b/sway/commands/include.c new file mode 100644 index 00000000..1ba9a10d --- /dev/null +++ b/sway/commands/include.c @@ -0,0 +1,15 @@ +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *cmd_include(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "include", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!load_include_configs(argv[0], config)) { + return cmd_results_new(CMD_INVALID, "include", "Failed to include sub configuration file: %s", argv[0]); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 475e8b04..61131845 100644 --- a/sway/config.c +++ b/sway/config.c @@ -311,6 +311,95 @@ bool load_main_config(const char *file, bool is_active) { 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; +} + bool read_config(FILE *file, struct sway_config *config) { bool success = true; enum cmd_status block = CMD_BLOCK_END; diff --git a/sway/meson.build b/sway/meson.build index 84f48137..ab863e87 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -5,6 +5,7 @@ sway_sources = files( 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', + 'commands/include.c', 'config.c', 'ipc-json.c', 'ipc-server.c', -- cgit v1.2.3 From aaae59026ff3751190b93277ac6d7566e373c892 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 6 Dec 2017 12:36:06 +0100 Subject: Add output config --- include/sway/config.h | 4 + include/sway/container.h | 1 + sway/commands.c | 1 + sway/commands/output.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++ sway/config/output.c | 166 +++++++++++++++++++++++++++++++ sway/main.c | 4 + sway/meson.build | 2 + sway/tree/container.c | 83 +++++++++++++++- 8 files changed, 512 insertions(+), 2 deletions(-) create mode 100644 sway/commands/output.c create mode 100644 sway/config/output.c (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 7de85ab7..1b49c5c9 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -80,8 +81,11 @@ struct output_config { char *name; int enabled; int width, height; + float refresh_rate; int x, y; int scale; + enum wl_output_transform transform; + char *background; char *background_option; }; diff --git a/include/sway/container.h b/include/sway/container.h index 08a98ed9..e3f84fc6 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -132,6 +132,7 @@ swayc_t *new_output(struct sway_output *sway_output); swayc_t *new_workspace(swayc_t *output, const char *name); swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); +swayc_t *destroy_output(swayc_t *output); swayc_t *destroy_view(swayc_t *view); swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); diff --git a/sway/commands.c b/sway/commands.c index 17638129..961cb867 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -95,6 +95,7 @@ static struct cmd_handler handlers[] = { { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, + { "output", cmd_output }, }; static int handler_compare(const void *_a, const void *_b) { diff --git a/sway/commands/output.c b/sway/commands/output.c new file mode 100644 index 00000000..c964bef7 --- /dev/null +++ b/sway/commands/output.c @@ -0,0 +1,253 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +static char *bg_options[] = { + "stretch", + "center", + "fill", + "fit", + "tile", +}; + +struct cmd_results *cmd_output(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { + return error; + } + const char *name = argv[0]; + + struct output_config *output = calloc(1, sizeof(struct output_config)); + if (!output) { + return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); + } + output->x = output->y = output->width = output->height = -1; + output->name = strdup(name); + output->enabled = -1; + output->scale = 1; + + // TODO: atoi doesn't handle invalid numbers + + int i; + for (i = 1; i < argc; ++i) { + const char *command = argv[i]; + + if (strcasecmp(command, "disable") == 0) { + output->enabled = 0; + } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); + goto fail; + } + char *res = argv[i]; + char *x = strchr(res, 'x'); + int width = -1, height = -1; + if (x != NULL) { + // Format is 1234x4321 + *x = '\0'; + width = atoi(res); + height = atoi(x + 1); + *x = 'x'; + } else { + // Format is 1234 4321 + width = atoi(res); + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height)."); + goto fail; + } + res = argv[i]; + height = atoi(res); + } + output->width = width; + output->height = height; + } else if (strcasecmp(command, "refresh_rate") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing refresh_rate argument."); + goto fail; + } + output->refresh_rate = atof(argv[i]); + } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); + goto fail; + } + char *res = argv[i]; + char *c = strchr(res, ','); + int x = -1, y = -1; + if (c != NULL) { + // Format is 1234,4321 + *c = '\0'; + x = atoi(res); + y = atoi(c + 1); + *c = ','; + } else { + // Format is 1234 4321 + x = atoi(res); + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); + goto fail; + } + res = argv[i]; + y = atoi(res); + } + output->x = x; + output->y = y; + } else if (strcasecmp(command, "scale") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); + goto fail; + } + output->scale = atoi(argv[i]); + } else if (strcasecmp(command, "transform") == 0) { + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter."); + goto fail; + } + char *value = argv[i]; + if (strcmp(value, "normal") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_NORMAL; + } else if (strcmp(value, "90") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_90; + } else if (strcmp(value, "180") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_180; + } else if (strcmp(value, "270") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_270; + } else if (strcmp(value, "flipped") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; + } else if (strcmp(value, "flipped-90") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; + } else if (strcmp(value, "flipped-180") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; + } else if (strcmp(value, "flipped-270") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; + } else { + error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform."); + goto fail; + } + } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { + wordexp_t p; + if (++i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); + goto fail; + } + if (i + 1 >= argc) { + error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); + goto fail; + } + if (strcasecmp(argv[i + 1], "solid_color") == 0) { + output->background = strdup(argv[argc - 2]); + output->background_option = strdup("solid_color"); + } else { + // argv[i+j]=bg_option + bool valid = false; + char *mode; + size_t j; + for (j = 0; j < (size_t) (argc - i); ++j) { + mode = argv[i + j]; + for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { + if (strcasecmp(mode, bg_options[k]) == 0) { + valid = true; + break; + } + } + if (valid) { + break; + } + } + if (!valid) { + error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); + goto fail; + } + + char *src = join_args(argv + i, j); + if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { + error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); + goto fail; + } + free(src); + src = p.we_wordv[0]; + if (config->reading && *src != '/') { + char *conf = strdup(config->current_config); + if (conf) { + char *conf_path = dirname(conf); + src = malloc(strlen(conf_path) + strlen(src) + 2); + if (src) { + sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); + } else { + sway_log(L_ERROR, "Unable to allocate background source"); + } + free(conf); + } else { + sway_log(L_ERROR, "Unable to allocate background source"); + } + } + if (!src || access(src, F_OK) == -1) { + error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); + wordfree(&p); + goto fail; + } + + output->background = strdup(src); + output->background_option = strdup(mode); + if (src != p.we_wordv[0]) { + free(src); + } + wordfree(&p); + + i += j; + } + } + } + + i = list_seq_find(config->output_configs, output_name_cmp, name); + if (i >= 0) { + // merge existing config + struct output_config *oc = config->output_configs->items[i]; + merge_output_config(oc, output); + free_output_config(output); + output = oc; + } else { + list_add(config->output_configs, output); + } + + sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ " + "%d, %d scale %d transform %d refresh_rate %f) (bg %s %s)", + output->name, output->enabled, output->width, + output->height, output->x, output->y, output->scale, + output->transform, output->refresh_rate, + output->background, output->background_option); + + if (output->name) { + // Try to find the output container and apply configuration now. If + // this is during startup then there will be no container and config + // will be applied during normal "new output" event from wlc. + swayc_t *cont = NULL; + for (int i = 0; i < root_container.children->length; ++i) { + cont = root_container.children->items[i]; + if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { + apply_output_config(output, cont); + + if (strcmp(output->name, "*") != 0) { + // stop looking if the output config isn't applicable to all outputs + break; + } + } + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + +fail: + free_output_config(output); + return error; +} diff --git a/sway/config/output.c b/sway/config/output.c new file mode 100644 index 00000000..02e18e59 --- /dev/null +++ b/sway/config/output.c @@ -0,0 +1,166 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include "sway/config.h" +#include "sway/output.h" +#include "log.h" + +int output_name_cmp(const void *item, const void *data) { + const struct output_config *output = item; + const char *name = data; + + return strcmp(output->name, name); +} + +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 set_mode(struct wlr_output *output, int width, int height, + float refresh_rate) { + struct wlr_output_mode *mode, *best = NULL; + int mhz = (int)(refresh_rate * 1000); + wl_list_for_each(mode, &output->modes, link) { + if (mode->width == width && mode->height == height) { + if (mode->refresh == mhz) { + best = mode; + break; + } + best = mode; + } + } + if (!best) { + sway_log(L_ERROR, "Configured mode for %s not available", output->name); + } else { + sway_log(L_DEBUG, "Assigning configured mode to %s", output->name); + wlr_output_set_mode(output, best); + } +} + +void apply_output_config(struct output_config *oc, swayc_t *output) { + assert(output->type == C_OUTPUT); + + if (oc && oc->enabled == 0) { + destroy_output(output); + return; + } + + struct wlr_output *wlr_output = output->sway_output->wlr_output; + if (oc && oc->width > 0 && oc->height > 0) { + set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); + } + if (oc && oc->scale > 0) { + wlr_output->scale = oc->scale; + } + if (oc && oc->transform != WL_OUTPUT_TRANSFORM_NORMAL) { + wlr_output_transform(wlr_output, oc->transform); + } + + // 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; + } + } + + if (oc && oc->background) { + // TODO: swaybg + /*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); + }*/ + } +} + +void free_output_config(struct output_config *oc) { + if (!oc) { + return; + } + free(oc->name); + free(oc->background); + free(oc->background_option); + free(oc); +} diff --git a/sway/main.c b/sway/main.c index bc843591..8952f997 100644 --- a/sway/main.c +++ b/sway/main.c @@ -404,6 +404,10 @@ int main(int argc, char **argv) { security_sanity_check(); + // TODO: wait for server to be ready + // TODO: consume config->cmd_queue + config->active = true; + if (!terminate_request) { server_run(&server); } diff --git a/sway/meson.build b/sway/meson.build index 84f48137..5ae7fbb3 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -5,7 +5,9 @@ sway_sources = files( 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', + 'commands/output.c', 'config.c', + 'config/output.c', 'ipc-json.c', 'ipc-server.c', 'desktop/output.c', diff --git a/sway/tree/container.c b/sway/tree/container.c index e205fbcf..7720718f 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include "sway/config.h" #include "sway/container.h" #include "sway/layout.h" #include "sway/output.h" @@ -23,6 +25,30 @@ void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, } } +static void update_root_geometry() { + int width = 0; + int height = 0; + swayc_t *child; + int child_width; + int child_height; + + for (int i = 0; i < root_container.children->length; ++i) { + child = root_container.children->items[i]; + child_width = child->width + child->x; + child_height = child->height + child->y; + if (child_width > width) { + width = child_width; + } + + if (child_height > height) { + height = child_height; + } + } + + root_container.width = width; + root_container.height = height; +} + static swayc_t *new_swayc(enum swayc_types type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; @@ -44,10 +70,33 @@ static swayc_t *new_swayc(enum swayc_types type) { swayc_t *new_output(struct sway_output *sway_output) { struct wlr_box size; - wlr_output_effective_resolution( - sway_output->wlr_output, &size.width, &size.height); + wlr_output_effective_resolution(sway_output->wlr_output, &size.width, + &size.height); const char *name = sway_output->wlr_output->name; + struct output_config *oc = NULL, *all = NULL; + for (int i = 0; i < config->output_configs->length; ++i) { + struct output_config *cur = config->output_configs->items[i]; + if (strcasecmp(name, cur->name) == 0) { + sway_log(L_DEBUG, "Matched output config for %s", name); + oc = cur; + } + if (strcasecmp("*", cur->name) == 0) { + sway_log(L_DEBUG, "Matched wildcard output config for %s", name); + all = cur; + } + + if (oc && all) { + break; + } + } + if (!oc) { + oc = all; + } + if (oc && !oc->enabled) { + return NULL; + } + swayc_t *output = new_swayc(C_OUTPUT); output->sway_output = sway_output; output->name = name ? strdup(name) : NULL; @@ -58,6 +107,8 @@ swayc_t *new_output(struct sway_output *sway_output) { wlr_output_layout_add_auto(root_container.output_layout, sway_output->wlr_output); + apply_output_config(oc, output); + add_child(&root_container, output); // Create workspace @@ -139,6 +190,34 @@ static void free_swayc(swayc_t *cont) { free(cont); } +swayc_t *destroy_output(swayc_t *output) { + if (!sway_assert(output, "null output passed to destroy_output")) { + return NULL; + } + if (output->children->length > 0) { + // TODO save workspaces when there are no outputs. + // TODO also check if there will ever be no outputs except for exiting + // program + if (root_container.children->length > 1) { + int p = root_container.children->items[0] == output; + // Move workspace from this output to another output + while (output->children->length) { + swayc_t *child = output->children->items[0]; + remove_child(child); + add_child(root_container.children->items[p], child); + } + sort_workspaces(root_container.children->items[p]); + // TODO WLR: is this needed anymore? + //update_visibility(root_container.children->items[p]); + arrange_windows(root_container.children->items[p], -1, -1); + } + } + sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); + free_swayc(output); + update_root_geometry(); + return &root_container; +} + swayc_t *destroy_view(swayc_t *view) { if (!sway_assert(view, "null view passed to destroy_view")) { return NULL; -- cgit v1.2.3 From 68ae989ceef0a144988c0a55b13aaacf514b957d Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 6 Dec 2017 12:57:13 +0100 Subject: Init, merge output config params, use wlr_output_layout --- include/sway/config.h | 3 ++- sway/commands/output.c | 4 +--- sway/config/output.c | 45 ++++++++++++++++++++++++++++++--------------- sway/tree/container.c | 6 ------ 4 files changed, 33 insertions(+), 25 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 1b49c5c9..231356f2 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -84,7 +84,7 @@ struct output_config { float refresh_rate; int x, y; int scale; - enum wl_output_transform transform; + int32_t transform; char *background; char *background_option; @@ -364,6 +364,7 @@ void apply_input_config(struct input_config *ic, struct libinput_device *dev); void free_input_config(struct input_config *ic); int output_name_cmp(const void *item, const void *data); +void output_config_defaults(struct output_config *oc); void merge_output_config(struct output_config *dst, struct output_config *src); /** Sets up a WLC output handle based on a given output_config. */ diff --git a/sway/commands/output.c b/sway/commands/output.c index c964bef7..bbf8efc3 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -31,10 +31,8 @@ struct cmd_results *cmd_output(int argc, char **argv) { if (!output) { return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); } - output->x = output->y = output->width = output->height = -1; + output_config_defaults(output); output->name = strdup(name); - output->enabled = -1; - output->scale = 1; // TODO: atoi doesn't handle invalid numbers diff --git a/sway/config/output.c b/sway/config/output.c index 02e18e59..4ed2c531 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "sway/config.h" #include "sway/output.h" #include "log.h" @@ -13,6 +14,15 @@ int output_name_cmp(const void *item, const void *data) { return strcmp(output->name, name); } +void output_config_defaults(struct output_config *oc) { + oc->enabled = -1; + oc->width = oc->height -1; + oc->refresh_rate = -1; + oc->x = oc->y = -1; + oc->scale = -1; + oc->transform = -1; +} + void merge_output_config(struct output_config *dst, struct output_config *src) { if (src->name) { if (dst->name) { @@ -38,6 +48,12 @@ void merge_output_config(struct output_config *dst, struct output_config *src) { if (src->scale != -1) { dst->scale = src->scale; } + if (src->refresh_rate != -1) { + dst->refresh_rate = src->refresh_rate; + } + if (src->transform != -1) { + dst->transform = src->transform; + } if (src->background) { if (dst->background) { free(dst->background); @@ -86,29 +102,28 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); } if (oc && oc->scale > 0) { + sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale); wlr_output->scale = oc->scale; } - if (oc && oc->transform != WL_OUTPUT_TRANSFORM_NORMAL) { + if (oc && oc->transform >= 0) { + sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform); wlr_output_transform(wlr_output, oc->transform); } // Find position for it - if (oc && oc->x != -1 && oc->y != -1) { + 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; + wlr_output_layout_add(root_container.output_layout, wlr_output, oc->x, + 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; + wlr_output_layout_add_auto(root_container.output_layout, wlr_output); } + struct wlr_box *output_layout_box = + wlr_output_layout_get_box(root_container.output_layout, wlr_output); + output->x = output_layout_box->x; + output->y = output_layout_box->y; + output->width = output_layout_box->width; + output->height = output_layout_box->height; if (!oc || !oc->background) { // Look for a * config for background @@ -128,7 +143,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } if (oc && oc->background) { - // TODO: swaybg + // TODO swaybg /*if (output->bg_pid != 0) { terminate_swaybg(output->bg_pid); } diff --git a/sway/tree/container.c b/sway/tree/container.c index 7720718f..ba305efa 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -100,12 +100,6 @@ swayc_t *new_output(struct sway_output *sway_output) { swayc_t *output = new_swayc(C_OUTPUT); output->sway_output = sway_output; output->name = name ? strdup(name) : NULL; - output->width = size.width; - output->height = size.width; - - // TODO configure output layout position - wlr_output_layout_add_auto(root_container.output_layout, - sway_output->wlr_output); apply_output_config(oc, output); -- cgit v1.2.3 From 8764dc26c634379ca5b5c2c4fc26cf8be9adf027 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 6 Dec 2017 19:45:43 +0100 Subject: Add new_output_config, update root container size on output hotplug --- include/sway/config.h | 4 +--- sway/commands/output.c | 3 +-- sway/config/output.c | 7 ++++++- sway/tree/container.c | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 231356f2..4dd8e94c 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -364,10 +364,8 @@ void apply_input_config(struct input_config *ic, struct libinput_device *dev); void free_input_config(struct input_config *ic); int output_name_cmp(const void *item, const void *data); -void output_config_defaults(struct output_config *oc); +struct output_config *new_output_config(); void merge_output_config(struct output_config *dst, struct output_config *src); -/** Sets up a WLC output handle based on a given output_config. - */ void apply_output_config(struct output_config *oc, swayc_t *output); void free_output_config(struct output_config *oc); diff --git a/sway/commands/output.c b/sway/commands/output.c index bbf8efc3..11da0ff6 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -27,11 +27,10 @@ struct cmd_results *cmd_output(int argc, char **argv) { } const char *name = argv[0]; - struct output_config *output = calloc(1, sizeof(struct output_config)); + struct output_config *output = new_output_config(); if (!output) { return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); } - output_config_defaults(output); output->name = strdup(name); // TODO: atoi doesn't handle invalid numbers diff --git a/sway/config/output.c b/sway/config/output.c index 4a0a5cc9..26798503 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -14,13 +14,18 @@ int output_name_cmp(const void *item, const void *data) { return strcmp(output->name, name); } -void output_config_defaults(struct output_config *oc) { +struct output_config *new_output_config() { + struct output_config *oc = calloc(1, sizeof(struct output_config)); + if (oc == NULL) { + return NULL; + } oc->enabled = -1; oc->width = oc->height -1; oc->refresh_rate = -1; oc->x = oc->y = -1; oc->scale = -1; oc->transform = -1; + return oc; } void merge_output_config(struct output_config *dst, struct output_config *src) { diff --git a/sway/tree/container.c b/sway/tree/container.c index ec3311a0..d9bed7d8 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -93,6 +93,7 @@ swayc_t *new_output(struct sway_output *sway_output) { sway_log(L_DEBUG, "Creating default workspace %s", ws_name); new_workspace(output, ws_name); free(ws_name); + update_root_geometry(); return output; } -- cgit v1.2.3 From 4a14aa9ad99a6f316024e110332a0b482e231543 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 9 Dec 2017 15:48:52 +0100 Subject: Remove output from layout --- sway/commands/output.c | 2 +- sway/config/output.c | 3 ++- sway/tree/container.c | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/output.c b/sway/commands/output.c index 11da0ff6..be78358a 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -227,7 +227,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { if (output->name) { // Try to find the output container and apply configuration now. If // this is during startup then there will be no container and config - // will be applied during normal "new output" event from wlc. + // will be applied during normal "new output" event from wlroots. swayc_t *cont = NULL; for (int i = 0; i < root_container.children->length; ++i) { cont = root_container.children->items[i]; diff --git a/sway/config/output.c b/sway/config/output.c index 26798503..027a79ce 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -91,12 +91,13 @@ static void set_mode(struct wlr_output *output, int width, int height, void apply_output_config(struct output_config *oc, swayc_t *output) { assert(output->type == C_OUTPUT); + struct wlr_output *wlr_output = output->sway_output->wlr_output; if (oc && oc->enabled == 0) { + wlr_output_layout_remove(root_container.output_layout, wlr_output); destroy_output(output); return; } - struct wlr_output *wlr_output = output->sway_output->wlr_output; if (oc && oc->width > 0 && oc->height > 0) { set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); } diff --git a/sway/tree/container.c b/sway/tree/container.c index d9bed7d8..e4c27d61 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -8,6 +8,7 @@ #include "sway/container.h" #include "sway/layout.h" #include "sway/output.h" +#include "sway/server.h" #include "sway/view.h" #include "sway/workspace.h" #include "log.h" @@ -172,6 +173,7 @@ swayc_t *destroy_output(swayc_t *output) { if (!sway_assert(output, "null output passed to destroy_output")) { return NULL; } + if (output->children->length > 0) { // TODO save workspaces when there are no outputs. // TODO also check if there will ever be no outputs except for exiting @@ -190,9 +192,11 @@ swayc_t *destroy_output(swayc_t *output) { arrange_windows(root_container.children->items[p], -1, -1); } } + sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); free_swayc(output); update_root_geometry(); + return &root_container; } -- cgit v1.2.3 From 462a451328a1d6f0b17d34b431d6bf3dec87c1ba Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 11 Dec 2017 04:17:14 -0500 Subject: input config --- include/sway/commands.h | 4 ++ include/sway/input/input-manager.h | 2 + sway/commands.c | 90 ++++++++++++++++++++++++---------- sway/commands/input.c | 55 +++++++++++++++++++++ sway/commands/input/accel_profile.c | 27 ++++++++++ sway/commands/input/click_method.c | 30 ++++++++++++ sway/commands/input/drag_lock.c | 26 ++++++++++ sway/commands/input/dwt.c | 26 ++++++++++ sway/commands/input/events.c | 30 ++++++++++++ sway/commands/input/left_handed.c | 26 ++++++++++ sway/commands/input/middle_emulation.c | 26 ++++++++++ sway/commands/input/natural_scroll.c | 26 ++++++++++ sway/commands/input/pointer_accel.c | 24 +++++++++ sway/commands/input/scroll_method.c | 30 ++++++++++++ sway/commands/input/tap.c | 29 +++++++++++ sway/config.c | 53 ++++++++++++++++++++ sway/meson.build | 12 +++++ 17 files changed, 490 insertions(+), 26 deletions(-) create mode 100644 sway/commands/input.c create mode 100644 sway/commands/input/accel_profile.c create mode 100644 sway/commands/input/click_method.c create mode 100644 sway/commands/input/drag_lock.c create mode 100644 sway/commands/input/dwt.c create mode 100644 sway/commands/input/events.c create mode 100644 sway/commands/input/left_handed.c create mode 100644 sway/commands/input/middle_emulation.c create mode 100644 sway/commands/input/natural_scroll.c create mode 100644 sway/commands/input/pointer_accel.c create mode 100644 sway/commands/input/scroll_method.c create mode 100644 sway/commands/input/tap.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index b1f0423d..138e3c29 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -1,6 +1,8 @@ #ifndef _SWAY_COMMANDS_H #define _SWAY_COMMANDS_H +#include "config.h" + /** * Indicates the result of a command's execution. */ @@ -39,6 +41,8 @@ enum expected_args { EXPECTED_EQUAL_TO }; +void input_cmd_apply(struct input_config *input); + struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val); diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 9548c170..78bc161f 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -5,6 +5,8 @@ #include "sway/config.h" #include "list.h" +extern struct input_config *current_input_config; + struct sway_input_manager { struct wl_listener input_add; struct wl_listener input_remove; diff --git a/sway/commands.c b/sway/commands.c index 05a66a7f..7710c6ab 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -9,6 +9,7 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/security.h" +#include "sway/input/input-manager.h" #include "stringop.h" #include "log.h" @@ -56,6 +57,44 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type return error; } +void input_cmd_apply(struct input_config *input) { + int i; + i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier); + if (i >= 0) { + // merge existing config + struct input_config *ic = config->input_configs->items[i]; + merge_input_config(ic, input); + free_input_config(input); + input = ic; + } else { + list_add(config->input_configs, input); + } + + current_input_config = input; + + if (input->identifier) { + // Try to find the input device and apply configuration now. If + // this is during startup then there will be no container and config + // will be applied during normal "new input" event from wlc. + /* TODO WLR + struct libinput_device *device = NULL; + for (int i = 0; i < input_devices->length; ++i) { + device = input_devices->items[i]; + char* dev_identifier = libinput_dev_unique_id(device); + if (!dev_identifier) { + break; + } + int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0; + free(dev_identifier); + if (match) { + apply_input_config(input, device); + break; + } + } + */ + } +} + /** * Check and add color to buffer. * @@ -96,6 +135,7 @@ static struct cmd_handler handlers[] = { { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, { "include", cmd_include }, + { "input", cmd_input }, }; static int handler_compare(const void *_a, const void *_b) { @@ -104,37 +144,35 @@ static int handler_compare(const void *_a, const void *_b) { return strcasecmp(a->command, b->command); } +static struct cmd_handler input_handlers[] = { + { "accel_profile", input_cmd_accel_profile }, + { "click_method", input_cmd_click_method }, + { "drag_lock", input_cmd_drag_lock }, + { "dwt", input_cmd_dwt }, + { "events", input_cmd_events }, + { "left_handed", input_cmd_left_handed }, + { "middle_emulation", input_cmd_middle_emulation }, + { "natural_scroll", input_cmd_natural_scroll }, + { "pointer_accel", input_cmd_pointer_accel }, + { "scroll_method", input_cmd_scroll_method }, + { "tap", input_cmd_tap }, +}; + static struct cmd_handler *find_handler(char *line, enum cmd_status block) { struct cmd_handler d = { .command=line }; struct cmd_handler *res = NULL; sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT); - /* TODO - if (block == CMD_BLOCK_BAR) { - res = bsearch(&d, bar_handlers, - sizeof(bar_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_BAR_COLORS){ - res = bsearch(&d, bar_colors_handlers, - sizeof(bar_colors_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_INPUT) { + + if (block == CMD_BLOCK_INPUT) { res = bsearch(&d, input_handlers, - sizeof(input_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_IPC) { - res = bsearch(&d, ipc_handlers, - sizeof(ipc_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - } else if (block == CMD_BLOCK_IPC_EVENTS) { - res = bsearch(&d, ipc_event_handlers, - sizeof(ipc_event_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); + sizeof(input_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); } else { - */ res = bsearch(&d, handlers, - sizeof(handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - //} + sizeof(handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } + return res; } @@ -238,8 +276,8 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) { argv[i] = do_var_replacement(argv[i]); unescape_string(argv[i]); } - /* Strip quotes for first argument. - * TODO This part needs to be handled much better */ + // Strip quotes for first argument. + // TODO This part needs to be handled much better if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) { strip_quotes(argv[1]); } diff --git a/sway/commands/input.c b/sway/commands/input.c new file mode 100644 index 00000000..5ca9c2e6 --- /dev/null +++ b/sway/commands/input.c @@ -0,0 +1,55 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *cmd_input(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) { + return error; + } + + if (config->reading && strcmp("{", argv[1]) == 0) { + current_input_config = new_input_config(argv[0]); + sway_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); + return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); + } + + if (argc > 2) { + int argc_new = argc-2; + char **argv_new = argv+2; + + struct cmd_results *res; + current_input_config = new_input_config(argv[0]); + if (strcasecmp("accel_profile", argv[1]) == 0) { + res = input_cmd_accel_profile(argc_new, argv_new); + } else if (strcasecmp("click_method", argv[1]) == 0) { + res = input_cmd_click_method(argc_new, argv_new); + } else if (strcasecmp("drag_lock", argv[1]) == 0) { + res = input_cmd_drag_lock(argc_new, argv_new); + } else if (strcasecmp("dwt", argv[1]) == 0) { + res = input_cmd_dwt(argc_new, argv_new); + } else if (strcasecmp("events", argv[1]) == 0) { + res = input_cmd_events(argc_new, argv_new); + } else if (strcasecmp("left_handed", argv[1]) == 0) { + res = input_cmd_left_handed(argc_new, argv_new); + } else if (strcasecmp("middle_emulation", argv[1]) == 0) { + res = input_cmd_middle_emulation(argc_new, argv_new); + } else if (strcasecmp("natural_scroll", argv[1]) == 0) { + res = input_cmd_natural_scroll(argc_new, argv_new); + } else if (strcasecmp("pointer_accel", argv[1]) == 0) { + res = input_cmd_pointer_accel(argc_new, argv_new); + } else if (strcasecmp("scroll_method", argv[1]) == 0) { + res = input_cmd_scroll_method(argc_new, argv_new); + } else if (strcasecmp("tap", argv[1]) == 0) { + res = input_cmd_tap(argc_new, argv_new); + } else { + res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); + } + current_input_config = NULL; + return res; + } + + return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); +} diff --git a/sway/commands/input/accel_profile.c b/sway/commands/input/accel_profile.c new file mode 100644 index 00000000..13ded431 --- /dev/null +++ b/sway/commands/input/accel_profile.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_accel_profile(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "accel_profile", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "adaptive") == 0) { + new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE; + } else if (strcasecmp(argv[0], "flat") == 0) { + new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT; + } else { + return cmd_results_new(CMD_INVALID, "accel_profile", + "Expected 'accel_profile '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c new file mode 100644 index 00000000..40f075ce --- /dev/null +++ b/sway/commands/input/click_method.c @@ -0,0 +1,30 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_click_method(int argc, char **argv) { + sway_log(L_DEBUG, "click_method for device: %d %s", current_input_config==NULL, current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "click_method", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "none") == 0) { + new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE; + } else if (strcasecmp(argv[0], "button_areas") == 0) { + new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS; + } else if (strcasecmp(argv[0], "clickfinger") == 0) { + new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER; + } else { + return cmd_results_new(CMD_INVALID, "click_method", "Expected 'click_method +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_drag_lock(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "drag_lock", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "drag_lock", "Expected 'drag_lock '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/dwt.c b/sway/commands/input/dwt.c new file mode 100644 index 00000000..f3cbf252 --- /dev/null +++ b/sway/commands/input/dwt.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_dwt(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->dwt = LIBINPUT_CONFIG_DWT_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->dwt = LIBINPUT_CONFIG_DWT_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "dwt", "Expected 'dwt '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c new file mode 100644 index 00000000..4b2fdff5 --- /dev/null +++ b/sway/commands/input/events.c @@ -0,0 +1,30 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_events(int argc, char **argv) { + sway_log(L_DEBUG, "events for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "events", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED; + } else if (strcasecmp(argv[0], "disabled_on_external_mouse") == 0) { + new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE; + } else { + return cmd_results_new(CMD_INVALID, "events", "Expected 'events '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/left_handed.c b/sway/commands/input/left_handed.c new file mode 100644 index 00000000..715df2a1 --- /dev/null +++ b/sway/commands/input/left_handed.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_left_handed(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "left_handed", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "left_handed", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->left_handed = 1; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->left_handed = 0; + } else { + return cmd_results_new(CMD_INVALID, "left_handed", "Expected 'left_handed '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/middle_emulation.c b/sway/commands/input/middle_emulation.c new file mode 100644 index 00000000..d31ce950 --- /dev/null +++ b/sway/commands/input/middle_emulation.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "middle_emulation", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "middle_emulation", "Expected 'middle_emulation '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/natural_scroll.c b/sway/commands/input/natural_scroll.c new file mode 100644 index 00000000..9d1dc506 --- /dev/null +++ b/sway/commands/input/natural_scroll.c @@ -0,0 +1,26 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "natural_scoll", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->natural_scroll = 1; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->natural_scroll = 0; + } else { + return cmd_results_new(CMD_INVALID, "natural_scroll", "Expected 'natural_scroll '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c new file mode 100644 index 00000000..87fb5cff --- /dev/null +++ b/sway/commands/input/pointer_accel.c @@ -0,0 +1,24 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "pointer_accel", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + float pointer_accel = atof(argv[0]); + if (pointer_accel < -1 || pointer_accel > 1) { + return cmd_results_new(CMD_INVALID, "pointer_accel", "Input out of range [-1, 1]"); + } + new_config->pointer_accel = pointer_accel; + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/scroll_method.c b/sway/commands/input/scroll_method.c new file mode 100644 index 00000000..98873938 --- /dev/null +++ b/sway/commands/input/scroll_method.c @@ -0,0 +1,30 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "scroll_method", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "none") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; + } else if (strcasecmp(argv[0], "two_finger") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_2FG; + } else if (strcasecmp(argv[0], "edge") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE; + } else if (strcasecmp(argv[0], "on_button_down") == 0) { + new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; + } else { + return cmd_results_new(CMD_INVALID, "scroll_method", "Expected 'scroll_method '"); + } + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c new file mode 100644 index 00000000..1109466f --- /dev/null +++ b/sway/commands/input/tap.c @@ -0,0 +1,29 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_tap(int argc, char **argv) { + sway_log(L_DEBUG, "tap for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "tap", "No input device defined."); + } + struct input_config *new_config = new_input_config(current_input_config->identifier); + + if (strcasecmp(argv[0], "enabled") == 0) { + new_config->tap = LIBINPUT_CONFIG_TAP_ENABLED; + } else if (strcasecmp(argv[0], "disabled") == 0) { + new_config->tap = LIBINPUT_CONFIG_TAP_DISABLED; + } else { + return cmd_results_new(CMD_INVALID, "tap", "Expected 'tap '"); + } + + sway_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 61131845..ec8e89b4 100644 --- a/sway/config.c +++ b/sway/config.c @@ -226,6 +226,59 @@ static int qstrcmp(const void* a, const void* b) { return strcmp(*((char**) a), *((char**) b)); } +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 free_input_config(struct input_config *ic) { + if (!ic) { + return; + } + free(ic->identifier); + free(ic); +} + +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); +} + bool load_main_config(const char *file, bool is_active) { char *path; if (file != NULL) { diff --git a/sway/meson.build b/sway/meson.build index 79201f3a..aa3dd2a7 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -10,6 +10,18 @@ sway_sources = files( 'commands/exec.c', 'commands/exec_always.c', 'commands/include.c', + 'commands/input.c', + 'commands/input/accel_profile.c', + 'commands/input/click_method.c', + 'commands/input/drag_lock.c', + 'commands/input/dwt.c', + 'commands/input/events.c', + 'commands/input/left_handed.c', + 'commands/input/middle_emulation.c', + 'commands/input/natural_scroll.c', + 'commands/input/pointer_accel.c', + 'commands/input/scroll_method.c', + 'commands/input/tap.c', 'config.c', 'ipc-json.c', 'ipc-server.c', -- cgit v1.2.3 From 538903bc5ace56c1dab0f5287fb4d0bab78a0165 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 12 Dec 2017 09:02:30 -0500 Subject: config cleanup --- sway/commands/input/accel_profile.c | 6 ++++-- sway/commands/input/click_method.c | 12 +++++++---- sway/commands/input/drag_lock.c | 9 ++++++--- sway/commands/input/dwt.c | 6 ++++-- sway/commands/input/events.c | 15 +++++++++----- sway/commands/input/left_handed.c | 9 ++++++--- sway/commands/input/middle_emulation.c | 12 +++++++---- sway/commands/input/natural_scroll.c | 9 ++++++--- sway/commands/input/pointer_accel.c | 9 ++++++--- sway/commands/input/scroll_method.c | 9 ++++++--- sway/commands/input/tap.c | 9 ++++++--- sway/config.c | 37 ++++++++++++++++++++++------------ 12 files changed, 94 insertions(+), 48 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/input/accel_profile.c b/sway/commands/input/accel_profile.c index 13ded431..9b5fb7a2 100644 --- a/sway/commands/input/accel_profile.c +++ b/sway/commands/input/accel_profile.c @@ -9,9 +9,11 @@ struct cmd_results *input_cmd_accel_profile(int argc, char **argv) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "accel_profile", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "accel_profile", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "adaptive") == 0) { new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE; diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c index 40f075ce..a0e3bddf 100644 --- a/sway/commands/input/click_method.c +++ b/sway/commands/input/click_method.c @@ -5,15 +5,18 @@ #include "log.h" struct cmd_results *input_cmd_click_method(int argc, char **argv) { - sway_log(L_DEBUG, "click_method for device: %d %s", current_input_config==NULL, current_input_config->identifier); + sway_log(L_DEBUG, "click_method for device: %d %s", + current_input_config==NULL, current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "click_method", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "click_method", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "none") == 0) { new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE; @@ -22,7 +25,8 @@ struct cmd_results *input_cmd_click_method(int argc, char **argv) { } else if (strcasecmp(argv[0], "clickfinger") == 0) { new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER; } else { - return cmd_results_new(CMD_INVALID, "click_method", "Expected 'click_method identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_ENABLED; } else if (strcasecmp(argv[0], "disabled") == 0) { new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED; } else { - return cmd_results_new(CMD_INVALID, "drag_lock", "Expected 'drag_lock '"); + return cmd_results_new(CMD_INVALID, "drag_lock", + "Expected 'drag_lock '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/dwt.c b/sway/commands/input/dwt.c index f3cbf252..0954575c 100644 --- a/sway/commands/input/dwt.c +++ b/sway/commands/input/dwt.c @@ -11,14 +11,16 @@ struct cmd_results *input_cmd_dwt(int argc, char **argv) { if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->dwt = LIBINPUT_CONFIG_DWT_ENABLED; } else if (strcasecmp(argv[0], "disabled") == 0) { new_config->dwt = LIBINPUT_CONFIG_DWT_DISABLED; } else { - return cmd_results_new(CMD_INVALID, "dwt", "Expected 'dwt '"); + return cmd_results_new(CMD_INVALID, "dwt", + "Expected 'dwt '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c index 4b2fdff5..f44c0ec7 100644 --- a/sway/commands/input/events.c +++ b/sway/commands/input/events.c @@ -5,24 +5,29 @@ #include "log.h" struct cmd_results *input_cmd_events(int argc, char **argv) { - sway_log(L_DEBUG, "events for device: %s", current_input_config->identifier); + sway_log(L_DEBUG, "events for device: %s", + current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "events", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "events", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED; } else if (strcasecmp(argv[0], "disabled") == 0) { new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED; } else if (strcasecmp(argv[0], "disabled_on_external_mouse") == 0) { - new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE; + new_config->send_events = + LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE; } else { - return cmd_results_new(CMD_INVALID, "events", "Expected 'events '"); + return cmd_results_new(CMD_INVALID, "events", + "Expected 'events '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/left_handed.c b/sway/commands/input/left_handed.c index 715df2a1..dc8fcd56 100644 --- a/sway/commands/input/left_handed.c +++ b/sway/commands/input/left_handed.c @@ -9,16 +9,19 @@ struct cmd_results *input_cmd_left_handed(int argc, char **argv) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "left_handed", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "left_handed", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->left_handed = 1; } else if (strcasecmp(argv[0], "disabled") == 0) { new_config->left_handed = 0; } else { - return cmd_results_new(CMD_INVALID, "left_handed", "Expected 'left_handed '"); + return cmd_results_new(CMD_INVALID, "left_handed", + "Expected 'left_handed '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/middle_emulation.c b/sway/commands/input/middle_emulation.c index d31ce950..e19964d8 100644 --- a/sway/commands/input/middle_emulation.c +++ b/sway/commands/input/middle_emulation.c @@ -9,16 +9,20 @@ struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "middle_emulation", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "middle_emulation", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED; } else if (strcasecmp(argv[0], "disabled") == 0) { - new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; + new_config->middle_emulation = + LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; } else { - return cmd_results_new(CMD_INVALID, "middle_emulation", "Expected 'middle_emulation '"); + return cmd_results_new(CMD_INVALID, "middle_emulation", + "Expected 'middle_emulation '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/natural_scroll.c b/sway/commands/input/natural_scroll.c index 9d1dc506..8272c5b3 100644 --- a/sway/commands/input/natural_scroll.c +++ b/sway/commands/input/natural_scroll.c @@ -9,16 +9,19 @@ struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "natural_scoll", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "natural_scoll", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->natural_scroll = 1; } else if (strcasecmp(argv[0], "disabled") == 0) { new_config->natural_scroll = 0; } else { - return cmd_results_new(CMD_INVALID, "natural_scroll", "Expected 'natural_scroll '"); + return cmd_results_new(CMD_INVALID, "natural_scroll", + "Expected 'natural_scroll '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c index 87fb5cff..2c9db5bf 100644 --- a/sway/commands/input/pointer_accel.c +++ b/sway/commands/input/pointer_accel.c @@ -9,13 +9,16 @@ struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "pointer_accel", "No input device defined."); + return cmd_results_new(CMD_FAILURE, + "pointer_accel", "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); float pointer_accel = atof(argv[0]); if (pointer_accel < -1 || pointer_accel > 1) { - return cmd_results_new(CMD_INVALID, "pointer_accel", "Input out of range [-1, 1]"); + return cmd_results_new(CMD_INVALID, "pointer_accel", + "Input out of range [-1, 1]"); } new_config->pointer_accel = pointer_accel; diff --git a/sway/commands/input/scroll_method.c b/sway/commands/input/scroll_method.c index 98873938..40277155 100644 --- a/sway/commands/input/scroll_method.c +++ b/sway/commands/input/scroll_method.c @@ -9,9 +9,11 @@ struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { return error; } if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "scroll_method", "No input device defined."); + return cmd_results_new(CMD_FAILURE, "scroll_method", + "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "none") == 0) { new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; @@ -22,7 +24,8 @@ struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { } else if (strcasecmp(argv[0], "on_button_down") == 0) { new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; } else { - return cmd_results_new(CMD_INVALID, "scroll_method", "Expected 'scroll_method '"); + return cmd_results_new(CMD_INVALID, "scroll_method", + "Expected 'scroll_method '"); } input_cmd_apply(new_config); diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c index 1109466f..18a54087 100644 --- a/sway/commands/input/tap.c +++ b/sway/commands/input/tap.c @@ -13,17 +13,20 @@ struct cmd_results *input_cmd_tap(int argc, char **argv) { if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "tap", "No input device defined."); } - struct input_config *new_config = new_input_config(current_input_config->identifier); + struct input_config *new_config = + new_input_config(current_input_config->identifier); if (strcasecmp(argv[0], "enabled") == 0) { new_config->tap = LIBINPUT_CONFIG_TAP_ENABLED; } else if (strcasecmp(argv[0], "disabled") == 0) { new_config->tap = LIBINPUT_CONFIG_TAP_DISABLED; } else { - return cmd_results_new(CMD_INVALID, "tap", "Expected 'tap '"); + return cmd_results_new(CMD_INVALID, "tap", + "Expected 'tap '"); } - sway_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier); + sway_log(L_DEBUG, "apply-tap for device: %s", + current_input_config->identifier); input_cmd_apply(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/config.c b/sway/config.c index 7ae3c2a4..4bc74ee0 100644 --- a/sway/config.c +++ b/sway/config.c @@ -21,6 +21,7 @@ #include #endif #include +#include "sway/input/input-manager.h" #include "sway/commands.h" #include "sway/config.h" #include "sway/layout.h" @@ -48,7 +49,8 @@ static void config_defaults(struct sway_config *config) { if (!(config->cmd_queue = create_list())) goto cleanup; - if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) 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; @@ -337,8 +339,9 @@ bool load_main_config(const char *file, bool is_active) { 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"); + 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/"; @@ -362,8 +365,12 @@ bool load_main_config(const char *file, bool is_active) { 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); + 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); @@ -392,7 +399,8 @@ bool load_main_config(const char *file, bool is_active) { return success; } -static bool load_include_config(const char *path, const char *parent_dir, struct sway_config *config) { +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; @@ -402,7 +410,8 @@ static bool load_include_config(const char *path, const char *parent_dir, struct 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"); + sway_log(L_ERROR, + "Unable to allocate full path to included config"); return false; } snprintf(full_path, len, "%s/%s", parent_dir, path); @@ -421,7 +430,9 @@ static bool load_include_config(const char *path, const char *parent_dir, struct 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); + sway_log(L_DEBUG, + "%s already included once, won't be included again.", + real_path); free(real_path); return false; } @@ -509,8 +520,8 @@ bool read_config(FILE *file, struct sway_config *config) { 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); + sway_log(L_ERROR, "Error on line %i '%s': %s (%s)", line_number, + line, res->error, config->current_config); success = false; break; @@ -585,8 +596,7 @@ bool read_config(FILE *file, struct sway_config *config) { case CMD_BLOCK_INPUT: sway_log(L_DEBUG, "End of input block"); - // TODO: input - //current_input_config = NULL; + current_input_config = NULL; block = CMD_BLOCK_END; break; @@ -651,7 +661,8 @@ char *do_var_replacement(char *str) { char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); if (!newstr) { sway_log(L_ERROR, - "Unable to allocate replacement during variable expansion"); + "Unable to allocate replacement " + "during variable expansion"); break; } char *newptr = newstr; -- cgit v1.2.3 From c173d30b9203520c274f34eb72fc787aa33ca211 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 12 Dec 2017 10:55:20 -0500 Subject: seat configuration --- include/sway/commands.h | 1 + include/sway/config.h | 1 + sway/commands.c | 2 ++ sway/commands/input/seat.c | 28 ++++++++++++++++++++++++++++ sway/config.c | 1 + sway/input/input-manager.c | 11 +++++++---- sway/meson.build | 1 + 7 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 sway/commands/input/seat.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 138e3c29..75340e03 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -180,6 +180,7 @@ sway_cmd bar_colors_cmd_statusline; sway_cmd bar_colors_cmd_focused_statusline; sway_cmd bar_colors_cmd_urgent_workspace; +sway_cmd input_cmd_seat; sway_cmd input_cmd_accel_profile; sway_cmd input_cmd_click_method; sway_cmd input_cmd_drag_lock; diff --git a/include/sway/config.h b/include/sway/config.h index d80f5a39..9fcecfd6 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -69,6 +69,7 @@ struct input_config { bool capturable; struct wlr_box region; + char *seat; }; /** diff --git a/sway/commands.c b/sway/commands.c index 57f76ea9..6645436a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -123,6 +123,7 @@ static int handler_compare(const void *_a, const void *_b) { return strcasecmp(a->command, b->command); } +// must be in order for the bsearch static struct cmd_handler input_handlers[] = { { "accel_profile", input_cmd_accel_profile }, { "click_method", input_cmd_click_method }, @@ -134,6 +135,7 @@ static struct cmd_handler input_handlers[] = { { "natural_scroll", input_cmd_natural_scroll }, { "pointer_accel", input_cmd_pointer_accel }, { "scroll_method", input_cmd_scroll_method }, + { "seat", input_cmd_seat }, { "tap", input_cmd_tap }, }; diff --git a/sway/commands/input/seat.c b/sway/commands/input/seat.c new file mode 100644 index 00000000..9d86ac0e --- /dev/null +++ b/sway/commands/input/seat.c @@ -0,0 +1,28 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_seat(int argc, char **argv) { + sway_log(L_DEBUG, "seat for device: %d %s", + current_input_config==NULL, current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "seat", + "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + // TODO validate seat name + free(new_config->seat); + new_config->seat = strdup(argv[0]); + + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 4bc74ee0..b77b8b4b 100644 --- a/sway/config.c +++ b/sway/config.c @@ -300,6 +300,7 @@ void free_input_config(struct input_config *ic) { return; } free(ic->identifier); + free(ic->seat); free(ic); } diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index b7f5615c..b07a733e 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -104,7 +104,9 @@ static void input_add_notify(struct wl_listener *listener, void *data) { } } - struct sway_seat *seat = input_manager_get_seat(input, default_seat); + const char *seat_name = + (sway_device->config ? sway_device->config->seat : default_seat); + struct sway_seat *seat = input_manager_get_seat(input, seat_name); sway_seat_add_device(seat, sway_device); } @@ -176,9 +178,9 @@ void sway_input_manager_set_focus(struct sway_input_manager *input, } void sway_input_manager_apply_config(struct sway_input_manager *input, - struct input_config *config) { + struct input_config *input_config) { struct sway_input_device *sway_device = - input_sway_device_from_config(input, config); + input_sway_device_from_config(input, input_config); if (!sway_device) { return; } @@ -188,7 +190,8 @@ void sway_input_manager_apply_config(struct sway_input_manager *input, sway_seat_remove_device(seat, sway_device); } - seat = input_manager_get_seat(input, default_seat); + const char *seat_name = (input_config->seat ? input_config->seat : default_seat); + seat = input_manager_get_seat(input, seat_name); sway_seat_add_device(seat, sway_device); } diff --git a/sway/meson.build b/sway/meson.build index aa3dd2a7..fad1f88c 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -11,6 +11,7 @@ sway_sources = files( 'commands/exec_always.c', 'commands/include.c', 'commands/input.c', + 'commands/input/seat.c', 'commands/input/accel_profile.c', 'commands/input/click_method.c', 'commands/input/drag_lock.c', -- cgit v1.2.3 From d293c429424a9f96c3fc8143af457645326e7a0e Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 12 Dec 2017 21:09:51 +0100 Subject: Update output container box in event handler --- include/sway/config.h | 1 - sway/commands/output.c | 3 ++- sway/config/output.c | 6 ------ sway/desktop/output.c | 23 +++++++++++++++++------ 4 files changed, 19 insertions(+), 14 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 4dd8e94c..139d7800 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include diff --git a/sway/commands/output.c b/sway/commands/output.c index be78358a..23792855 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -29,7 +29,8 @@ struct cmd_results *cmd_output(int argc, char **argv) { struct output_config *output = new_output_config(); if (!output) { - return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); + sway_log(L_ERROR, "Failed to allocate output config"); + return NULL; } output->name = strdup(name); diff --git a/sway/config/output.c b/sway/config/output.c index ed47a617..6371763f 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -131,12 +131,6 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { wlr_output_layout_add_auto(root_container.sway_root->output_layout, wlr_output); } - struct wlr_box *output_layout_box = wlr_output_layout_get_box( - root_container.sway_root->output_layout, wlr_output); - output->x = output_layout_box->x; - output->y = output_layout_box->y; - output->width = output_layout_box->width; - output->height = output_layout_box->height; if (!oc || !oc->background) { // Look for a * config for background diff --git a/sway/desktop/output.c b/sway/desktop/output.c index bcdaa7d2..3fd49846 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -91,19 +91,30 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { soutput->last_frame = now; } +static void output_update_size(struct sway_output *output) { + struct wlr_box *output_layout_box = wlr_output_layout_get_box( + root_container.sway_root->output_layout, output->wlr_output); + output->swayc->x = output_layout_box->x; + output->swayc->y = output_layout_box->y; + output->swayc->width = output_layout_box->width; + output->swayc->height = output_layout_box->height; + + arrange_windows(output->swayc, -1, -1); +} + static void output_resolution_notify(struct wl_listener *listener, void *data) { - struct sway_output *soutput = wl_container_of(listener, soutput, resolution); - arrange_windows(soutput->swayc, -1, -1); + struct sway_output *output = wl_container_of(listener, output, resolution); + output_update_size(output); } static void output_scale_notify(struct wl_listener *listener, void *data) { - struct sway_output *soutput = wl_container_of(listener, soutput, scale); - arrange_windows(soutput->swayc, -1, -1); + struct sway_output *output = wl_container_of(listener, output, scale); + output_update_size(output); } static void output_transform_notify(struct wl_listener *listener, void *data) { - struct sway_output *soutput = wl_container_of(listener, soutput, transform); - arrange_windows(soutput->swayc, -1, -1); + struct sway_output *output = wl_container_of(listener, output, transform); + output_update_size(output); } void output_add_notify(struct wl_listener *listener, void *data) { -- cgit v1.2.3 From 4d389f8b6523af741761009effd4d6dd79156afe Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 14 Dec 2017 00:45:47 +0100 Subject: Replace refresh_rate and position by mode in output command --- sway/commands/output.c | 85 ++++++++++++++++++++++++++++++++------------------ sway/config/output.c | 2 +- 2 files changed, 55 insertions(+), 32 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/output.c b/sway/commands/output.c index 23792855..daaacad7 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -42,61 +42,84 @@ struct cmd_results *cmd_output(int argc, char **argv) { if (strcasecmp(command, "disable") == 0) { output->enabled = 0; - } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { + } else if (strcasecmp(command, "mode") == 0 || + strcasecmp(command, "resolution") == 0 || + strcasecmp(command, "res") == 0) { if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing mode argument."); goto fail; } - char *res = argv[i]; - char *x = strchr(res, 'x'); + int width = -1, height = -1; - if (x != NULL) { + float refresh_rate = -1; + + char *end; + width = strtol(argv[i], &end, 10); + if (*end) { // Format is 1234x4321 - *x = '\0'; - width = atoi(res); - height = atoi(x + 1); - *x = 'x'; + if (*end != 'x') { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid mode width."); + goto fail; + } + ++end; + height = strtol(end, &end, 10); + if (*end) { + if (*end != '@') { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid mode height."); + goto fail; + } + ++end; + refresh_rate = strtof(end, &end); + if (strcasecmp("Hz", end) != 0) { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid mode refresh rate."); + goto fail; + } + } } else { - // Format is 1234 4321 - width = atoi(res); - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height)."); + // Format is 1234 4321 (legacy) + ++i; + if (i >= argc) { + error = cmd_results_new(CMD_INVALID, "output", + "Missing mode argument (height)."); + goto fail; + } + height = strtol(argv[i], &end, 10); + if (*end) { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid mode height."); goto fail; } - res = argv[i]; - height = atoi(res); } output->width = width; output->height = height; - } else if (strcasecmp(command, "refresh_rate") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing refresh_rate argument."); - goto fail; - } - output->refresh_rate = atof(argv[i]); + output->refresh_rate = refresh_rate; } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { if (++i >= argc) { error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); goto fail; } - char *res = argv[i]; - char *c = strchr(res, ','); + char *pos = argv[i]; + char *c = strchr(pos, ','); int x = -1, y = -1; if (c != NULL) { // Format is 1234,4321 *c = '\0'; - x = atoi(res); + x = atoi(pos); y = atoi(c + 1); *c = ','; } else { // Format is 1234 4321 - x = atoi(res); + x = atoi(pos); if (++i >= argc) { error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); goto fail; } - res = argv[i]; - y = atoi(res); + pos = argv[i]; + y = atoi(pos); } output->x = x; output->y = y; @@ -218,11 +241,11 @@ struct cmd_results *cmd_output(int argc, char **argv) { list_add(config->output_configs, output); } - sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ " - "%d, %d scale %d transform %d refresh_rate %f) (bg %s %s)", + sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " + "position %d,%d scale %d transform %d) (bg %s %s)", output->name, output->enabled, output->width, - output->height, output->x, output->y, output->scale, - output->transform, output->refresh_rate, + output->height, output->refresh_rate, output->x, output->y, + output->scale, output->transform, output->background, output->background_option); if (output->name) { diff --git a/sway/config/output.c b/sway/config/output.c index 8d946386..dc9ee37c 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -20,7 +20,7 @@ struct output_config *new_output_config() { return NULL; } oc->enabled = -1; - oc->width = oc->height -1; + oc->width = oc->height = -1; oc->refresh_rate = -1; oc->x = oc->y = -1; oc->scale = -1; -- cgit v1.2.3 From 9254c5a93f1a38612d5391e34e4fe3b6c9e433a7 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 14 Dec 2017 00:50:01 +0100 Subject: Fail if unknown output subcommand --- sway/commands/output.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/output.c b/sway/commands/output.c index daaacad7..16b711f1 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -192,7 +192,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { char *src = join_args(argv + i, j); if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { - error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); + error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src); goto fail; } free(src); @@ -213,7 +213,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { } } if (!src || access(src, F_OK) == -1) { - error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); + error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s).", src); wordfree(&p); goto fail; } @@ -227,6 +227,9 @@ struct cmd_results *cmd_output(int argc, char **argv) { i += j; } + } else { + error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command); + goto fail; } } -- cgit v1.2.3 From cba592b3d2c516cab9f4e8325037dfdf6e30cb9a Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 14 Dec 2017 02:23:33 +0100 Subject: Use strtol instead of atoi in output command --- sway/commands/output.c | 114 ++++++++++++++++++++++++++++++++----------------- sway/tree/container.c | 2 - 2 files changed, 74 insertions(+), 42 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/output.c b/sway/commands/output.c index 16b711f1..d71e4d8d 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -34,8 +34,6 @@ struct cmd_results *cmd_output(int argc, char **argv) { } output->name = strdup(name); - // TODO: atoi doesn't handle invalid numbers - int i; for (i = 1; i < argc; ++i) { const char *command = argv[i]; @@ -80,9 +78,8 @@ struct cmd_results *cmd_output(int argc, char **argv) { } } } else { - // Format is 1234 4321 (legacy) - ++i; - if (i >= argc) { + // Format is 1234 4321 + if (++i >= argc) { error = cmd_results_new(CMD_INVALID, "output", "Missing mode argument (height)."); goto fail; @@ -97,41 +94,66 @@ struct cmd_results *cmd_output(int argc, char **argv) { output->width = width; output->height = height; output->refresh_rate = refresh_rate; - } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { + } else if (strcasecmp(command, "position") == 0 || + strcasecmp(command, "pos") == 0) { if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing position argument."); goto fail; } - char *pos = argv[i]; - char *c = strchr(pos, ','); + int x = -1, y = -1; - if (c != NULL) { + + char *end; + x = strtol(argv[i], &end, 10); + if (*end) { // Format is 1234,4321 - *c = '\0'; - x = atoi(pos); - y = atoi(c + 1); - *c = ','; + if (*end != ',') { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid position x."); + goto fail; + } + ++end; + y = strtol(end, &end, 10); + if (*end) { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid position y."); + goto fail; + } } else { - // Format is 1234 4321 - x = atoi(pos); + // Format is 1234 4321 (legacy) if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing position argument (y)."); + goto fail; + } + y = strtol(argv[i], &end, 10); + if (*end) { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid position y."); goto fail; } - pos = argv[i]; - y = atoi(pos); } + output->x = x; output->y = y; } else if (strcasecmp(command, "scale") == 0) { if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing scale parameter."); + goto fail; + } + char *end; + output->scale = strtol(argv[i], &end, 10); + if (*end) { + error = cmd_results_new(CMD_INVALID, "output", + "Invalid scale."); goto fail; } - output->scale = atoi(argv[i]); } else if (strcasecmp(command, "transform") == 0) { if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing transform parameter."); goto fail; } char *value = argv[i]; @@ -152,17 +174,21 @@ struct cmd_results *cmd_output(int argc, char **argv) { } else if (strcmp(value, "flipped-270") == 0) { output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; } else { - error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform."); + error = cmd_results_new(CMD_INVALID, "output", + "Invalid output transform."); goto fail; } - } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { + } else if (strcasecmp(command, "background") == 0 || + strcasecmp(command, "bg") == 0) { wordexp_t p; if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing background file or color specification."); goto fail; } if (i + 1 >= argc) { - error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing background scaling mode or `solid_color`."); goto fail; } if (strcasecmp(argv[i + 1], "solid_color") == 0) { @@ -175,7 +201,8 @@ struct cmd_results *cmd_output(int argc, char **argv) { size_t j; for (j = 0; j < (size_t) (argc - i); ++j) { mode = argv[i + j]; - for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { + size_t n = sizeof(bg_options) / sizeof(char *); + for (size_t k = 0; k < n; ++k) { if (strcasecmp(mode, bg_options[k]) == 0) { valid = true; break; @@ -186,13 +213,15 @@ struct cmd_results *cmd_output(int argc, char **argv) { } } if (!valid) { - error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); + error = cmd_results_new(CMD_INVALID, "output", + "Missing background scaling mode."); goto fail; } char *src = join_args(argv + i, j); if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { - error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src); + error = cmd_results_new(CMD_INVALID, "output", + "Invalid syntax (%s).", src); goto fail; } free(src); @@ -205,15 +234,18 @@ struct cmd_results *cmd_output(int argc, char **argv) { if (src) { sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); } else { - sway_log(L_ERROR, "Unable to allocate background source"); + sway_log(L_ERROR, + "Unable to allocate background source"); } free(conf); } else { - sway_log(L_ERROR, "Unable to allocate background source"); + sway_log(L_ERROR, + "Unable to allocate background source"); } } if (!src || access(src, F_OK) == -1) { - error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s).", src); + error = cmd_results_new(CMD_INVALID, "output", + "Background file unreadable (%s).", src); wordfree(&p); goto fail; } @@ -228,7 +260,8 @@ struct cmd_results *cmd_output(int argc, char **argv) { i += j; } } else { - error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command); + error = cmd_results_new(CMD_INVALID, "output", + "Invalid output subcommand: %s.", command); goto fail; } } @@ -245,11 +278,10 @@ struct cmd_results *cmd_output(int argc, char **argv) { } sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " - "position %d,%d scale %d transform %d) (bg %s %s)", - output->name, output->enabled, output->width, - output->height, output->refresh_rate, output->x, output->y, - output->scale, output->transform, - output->background, output->background_option); + "position %d,%d scale %d transform %d) (bg %s %s)", + output->name, output->enabled, output->width, output->height, + output->refresh_rate, output->x, output->y, output->scale, + output->transform, output->background, output->background_option); if (output->name) { // Try to find the output container and apply configuration now. If @@ -258,11 +290,13 @@ struct cmd_results *cmd_output(int argc, char **argv) { swayc_t *cont = NULL; for (int i = 0; i < root_container.children->length; ++i) { cont = root_container.children->items[i]; - if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { + if (cont->name && ((strcmp(cont->name, output->name) == 0) || + (strcmp(output->name, "*") == 0))) { apply_output_config(output, cont); if (strcmp(output->name, "*") != 0) { - // stop looking if the output config isn't applicable to all outputs + // Stop looking if the output config isn't applicable to all + // outputs break; } } diff --git a/sway/tree/container.c b/sway/tree/container.c index f70bccdc..5df10bcb 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -179,8 +179,6 @@ swayc_t *destroy_output(swayc_t *output) { add_child(root_container.children->items[p], child); } sort_workspaces(root_container.children->items[p]); - // TODO WLR: is this needed anymore? - //update_visibility(root_container.children->items[p]); arrange_windows(root_container.children->items[p], -1, -1); } } -- cgit v1.2.3 From 92fef27eaa0b52c9d37bdabff14ae21cd6660f46 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 14 Dec 2017 11:11:56 -0500 Subject: basic configuration --- include/sway/commands.h | 5 ++ include/sway/config.h | 27 +++++- include/sway/input/input-manager.h | 9 +- include/sway/input/keyboard.h | 9 +- include/sway/input/seat.h | 20 ++++- sway/commands.c | 32 +++++++- sway/commands/input/seat.c | 28 ------- sway/commands/seat.c | 35 ++++++++ sway/commands/seat/attach.c | 26 ++++++ sway/config.c | 144 ++++++++++++++++++++++++++++++-- sway/input/cursor.c | 8 +- sway/input/input-manager.c | 139 ++++++++++++++++++++++--------- sway/input/keyboard.c | 51 ++++++++---- sway/input/seat.c | 163 +++++++++++++++++++++++-------------- sway/meson.build | 3 +- 15 files changed, 534 insertions(+), 165 deletions(-) delete mode 100644 sway/commands/input/seat.c create mode 100644 sway/commands/seat.c create mode 100644 sway/commands/seat/attach.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 75340e03..ce74e1ed 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -17,6 +17,7 @@ enum cmd_status { CMD_BLOCK_BAR, CMD_BLOCK_BAR_COLORS, CMD_BLOCK_INPUT, + CMD_BLOCK_SEAT, CMD_BLOCK_COMMANDS, CMD_BLOCK_IPC, CMD_BLOCK_IPC_EVENTS, @@ -42,6 +43,7 @@ enum expected_args { }; void input_cmd_apply(struct input_config *input); +void seat_cmd_apply(struct seat_config *seat); struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val); @@ -111,6 +113,7 @@ sway_cmd cmd_gaps; sway_cmd cmd_hide_edge_borders; sway_cmd cmd_include; sway_cmd cmd_input; +sway_cmd cmd_seat; sway_cmd cmd_ipc; sway_cmd cmd_kill; sway_cmd cmd_layout; @@ -193,6 +196,8 @@ sway_cmd input_cmd_pointer_accel; sway_cmd input_cmd_scroll_method; sway_cmd input_cmd_tap; +sway_cmd seat_cmd_attach; + sway_cmd cmd_ipc_cmd; sway_cmd cmd_ipc_events; sway_cmd cmd_ipc_event_cmd; diff --git a/include/sway/config.h b/include/sway/config.h index 9fcecfd6..5df5d61e 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -69,7 +69,22 @@ struct input_config { bool capturable; struct wlr_box region; - char *seat; +}; + +/** + * Options for misc device configurations that happen in the seat block + */ +struct seat_attachment_config { + char *identifier; + // TODO other things are configured here for some reason +}; + +/** + * Options for multiseat and other misc device configurations + */ +struct seat_config { + char *name; + list_t *attachments; // list of seat_attachment configs }; /** @@ -260,6 +275,7 @@ struct sway_config { list_t *pid_workspaces; list_t *output_configs; list_t *input_configs; + list_t *seat_configs; list_t *criteria; list_t *no_focus; list_t *active_bar_modifiers; @@ -358,9 +374,16 @@ struct cmd_results *check_security_config(); int input_identifier_cmp(const void *item, const void *data); struct input_config *new_input_config(const char* identifier); void merge_input_config(struct input_config *dst, struct input_config *src); -void apply_input_config(struct input_config *ic, struct libinput_device *dev); void free_input_config(struct input_config *ic); +int seat_name_cmp(const void *item, const void *data); +struct seat_config *new_seat_config(const char* name); +void merge_seat_config(struct seat_config *dst, struct seat_config *src); +void free_seat_config(struct seat_config *ic); +struct seat_attachment_config *seat_attachment_config_new(); +struct seat_attachment_config *seat_config_get_attachment( + struct seat_config *seat_config, char *identifier); + int output_name_cmp(const void *item, const void *data); void merge_output_config(struct output_config *dst, struct output_config *src); /** Sets up a WLC output handle based on a given output_config. diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 7d7c463f..cdcffab6 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -6,6 +6,7 @@ #include "list.h" extern struct input_config *current_input_config; +extern struct seat_config *current_seat_config; /** * The global singleton input manager @@ -17,7 +18,6 @@ struct sway_input_device { char *identifier; struct wlr_input_device *wlr_device; struct input_config *config; - struct sway_keyboard *keyboard; // managed by the seat struct wl_list link; }; @@ -40,7 +40,10 @@ void sway_input_manager_set_focus(struct sway_input_manager *input, void sway_input_manager_configure_xcursor(struct sway_input_manager *input); -void sway_input_manager_apply_config(struct sway_input_manager *input, - struct input_config *config); +void sway_input_manager_apply_input_config(struct sway_input_manager *input, + struct input_config *input_config); + +void sway_input_manager_apply_seat_config(struct sway_input_manager *input, + struct seat_config *seat_config); #endif diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h index 881805b4..89cde3fa 100644 --- a/include/sway/input/keyboard.h +++ b/include/sway/input/keyboard.h @@ -1,15 +1,18 @@ #include "sway/input/seat.h" struct sway_keyboard { - struct sway_seat *seat; - struct sway_input_device *device; + struct sway_seat_device *seat_device; struct wl_list link; // sway_seat::keyboards + struct xkb_keymap *keymap; + struct wl_listener keyboard_key; struct wl_listener keyboard_modifiers; }; struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, - struct sway_input_device *device); + struct sway_seat_device *device); + +void sway_keyboard_configure(struct sway_keyboard *keyboard); void sway_keyboard_destroy(struct sway_keyboard *keyboard); diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index bd94a357..db69f83e 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -4,16 +4,25 @@ #include #include "sway/input/input-manager.h" +struct sway_seat_device { + struct sway_seat *sway_seat; + struct sway_input_device *input_device; + struct sway_keyboard *keyboard; + struct seat_attachment_config *attachment_config; + struct wl_list link; // sway_seat::devices +}; + struct sway_seat { - struct wlr_seat *seat; + struct wlr_seat *wlr_seat; + struct seat_config *config; struct sway_cursor *cursor; struct sway_input_manager *input; swayc_t *focus; - list_t *devices; - struct wl_listener focus_destroy; + struct wl_list devices; // sway_seat_device::link + struct wl_list link; // input_manager::seats }; @@ -23,6 +32,9 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, void sway_seat_add_device(struct sway_seat *seat, struct sway_input_device *device); +void sway_seat_configure_device(struct sway_seat *seat, + struct sway_input_device *device); + void sway_seat_remove_device(struct sway_seat *seat, struct sway_input_device *device); @@ -30,4 +42,6 @@ void sway_seat_configure_xcursor(struct sway_seat *seat); void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); +void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); + #endif diff --git a/sway/commands.c b/sway/commands.c index 6645436a..e003e06d 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -71,7 +71,24 @@ void input_cmd_apply(struct input_config *input) { } current_input_config = input; - sway_input_manager_apply_config(input_manager, input); + sway_input_manager_apply_input_config(input_manager, input); +} + +void seat_cmd_apply(struct seat_config *seat) { + int i; + i = list_seq_find(config->seat_configs, seat_name_cmp, seat->name); + if (i >= 0) { + // merge existing config + struct seat_config *sc = config->seat_configs->items[i]; + merge_seat_config(sc, seat); + free_seat_config(seat); + seat = sc; + } else { + list_add(config->seat_configs, seat); + } + + current_seat_config = seat; + sway_input_manager_apply_seat_config(input_manager, seat); } /** @@ -115,6 +132,7 @@ static struct cmd_handler handlers[] = { { "exit", cmd_exit }, { "include", cmd_include }, { "input", cmd_input }, + { "seat", cmd_seat }, }; static int handler_compare(const void *_a, const void *_b) { @@ -135,19 +153,27 @@ static struct cmd_handler input_handlers[] = { { "natural_scroll", input_cmd_natural_scroll }, { "pointer_accel", input_cmd_pointer_accel }, { "scroll_method", input_cmd_scroll_method }, - { "seat", input_cmd_seat }, { "tap", input_cmd_tap }, }; +// must be in order for the bsearch +static struct cmd_handler seat_handlers[] = { + { "attach", seat_cmd_attach }, +}; + static struct cmd_handler *find_handler(char *line, enum cmd_status block) { struct cmd_handler d = { .command=line }; struct cmd_handler *res = NULL; - sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT); + sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_SEAT); if (block == CMD_BLOCK_INPUT) { res = bsearch(&d, input_handlers, sizeof(input_handlers) / sizeof(struct cmd_handler), sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_SEAT) { + res = bsearch(&d, seat_handlers, + sizeof(seat_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); } else { res = bsearch(&d, handlers, sizeof(handlers) / sizeof(struct cmd_handler), diff --git a/sway/commands/input/seat.c b/sway/commands/input/seat.c deleted file mode 100644 index 9d86ac0e..00000000 --- a/sway/commands/input/seat.c +++ /dev/null @@ -1,28 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include "sway/commands.h" -#include "sway/input/input-manager.h" -#include "log.h" - -struct cmd_results *input_cmd_seat(int argc, char **argv) { - sway_log(L_DEBUG, "seat for device: %d %s", - current_input_config==NULL, current_input_config->identifier); - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 1))) { - return error; - } - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, "seat", - "No input device defined."); - } - struct input_config *new_config = - new_input_config(current_input_config->identifier); - - // TODO validate seat name - free(new_config->seat); - new_config->seat = strdup(argv[0]); - - input_cmd_apply(new_config); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); -} diff --git a/sway/commands/seat.c b/sway/commands/seat.c new file mode 100644 index 00000000..4f9e259b --- /dev/null +++ b/sway/commands/seat.c @@ -0,0 +1,35 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *cmd_seat(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 2))) { + return error; + } + + if (config->reading && strcmp("{", argv[1]) == 0) { + current_seat_config = new_seat_config(argv[0]); + sway_log(L_DEBUG, "entering seat block: %s", current_seat_config->name); + return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); + } + + if (argc > 2) { + int argc_new = argc-2; + char **argv_new = argv+2; + + struct cmd_results *res; + current_seat_config = new_seat_config(argv[0]); + if (strcasecmp("attach", argv[1]) == 0) { + res = seat_cmd_attach(argc_new, argv_new); + } else { + res = cmd_results_new(CMD_INVALID, "seat ", "Unknown command %s", argv[1]); + } + current_seat_config = NULL; + return res; + } + + return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); +} diff --git a/sway/commands/seat/attach.c b/sway/commands/seat/attach.c new file mode 100644 index 00000000..996c1bda --- /dev/null +++ b/sway/commands/seat/attach.c @@ -0,0 +1,26 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include "sway/input/input-manager.h" +#include "sway/commands.h" +#include "sway/config.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *seat_cmd_attach(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "attach", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_seat_config) { + return cmd_results_new(CMD_FAILURE, "attach", "No seat defined"); + } + + struct seat_config *new_config = new_seat_config(current_seat_config->name); + struct seat_attachment_config *new_attachment = seat_attachment_config_new(); + new_attachment->identifier = strdup(argv[0]); + list_add(new_config->attachments, new_attachment); + + seat_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index b77b8b4b..52633ad8 100644 --- a/sway/config.c +++ b/sway/config.c @@ -45,6 +45,7 @@ static void config_defaults(struct sway_config *config) { 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->seat_configs = create_list())) goto cleanup; if (!(config->output_configs = create_list())) goto cleanup; if (!(config->cmd_queue = create_list())) goto cleanup; @@ -258,9 +259,7 @@ struct input_config *new_input_config(const char* identifier) { void merge_input_config(struct input_config *dst, struct input_config *src) { if (src->identifier) { - if (dst->identifier) { - free(dst->identifier); - } + free(dst->identifier); dst->identifier = strdup(src->identifier); } if (src->accel_profile != INT_MIN) { @@ -300,7 +299,6 @@ void free_input_config(struct input_config *ic) { return; } free(ic->identifier); - free(ic->seat); free(ic); } @@ -310,6 +308,128 @@ int input_identifier_cmp(const void *item, const void *data) { return strcmp(ic->identifier, identifier); } +struct seat_config *new_seat_config(const char* name) { + struct seat_config *seat = calloc(1, sizeof(struct seat_config)); + if (!seat) { + sway_log(L_DEBUG, "Unable to allocate seat config"); + return NULL; + } + + sway_log(L_DEBUG, "new_seat_config(%s)", name); + seat->name = strdup(name); + if (!sway_assert(seat->name, "could not allocate name for seat")) { + return NULL; + } + + seat->attachments = create_list(); + if (!sway_assert(seat->attachments, + "could not allocate seat attachments list")) { + return NULL; + } + + return seat; +} + +struct seat_attachment_config *seat_attachment_config_new() { + struct seat_attachment_config *attachment = + calloc(1, sizeof(struct seat_attachment_config)); + if (!attachment) { + sway_log(L_DEBUG, "cannot allocate attachment config"); + return NULL; + } + return attachment; +} + +static void seat_attachment_config_free( + struct seat_attachment_config *attachment) { + free(attachment->identifier); + free(attachment); + return; +} + +static struct seat_attachment_config *seat_attachment_config_copy( + struct seat_attachment_config *attachment) { + struct seat_attachment_config *copy = seat_attachment_config_new(); + if (!copy) { + return NULL; + } + + copy->identifier = strdup(attachment->identifier); + + return copy; +} + +static void merge_seat_attachment_config(struct seat_attachment_config *dest, + struct seat_attachment_config *source) { + // nothing to merge yet, but there will be some day +} + +void merge_seat_config(struct seat_config *dest, struct seat_config *source) { + if (source->name) { + free(dest->name); + dest->name = strdup(source->name); + } + + for (int i = 0; i < source->attachments->length; ++i) { + struct seat_attachment_config *source_attachment = + source->attachments->items[i]; + bool found = false; + for (int j = 0; j < dest->attachments->length; ++j) { + struct seat_attachment_config *dest_attachment = + dest->attachments->items[j]; + if (strcmp(source_attachment->identifier, + dest_attachment->identifier) == 0) { + merge_seat_attachment_config(dest_attachment, + source_attachment); + found = true; + } + } + + if (!found) { + struct seat_attachment_config *copy = + seat_attachment_config_copy(source_attachment); + if (copy) { + list_add(dest->attachments, copy); + } + } + } +} + +void free_seat_config(struct seat_config *seat) { + if (!seat) { + return; + } + + free(seat->name); + for (int i = 0; i < seat->attachments->length; ++i) { + struct seat_attachment_config *attachment = + seat->attachments->items[i]; + seat_attachment_config_free(attachment); + } + + list_free(seat->attachments); + free(seat); +} + +int seat_name_cmp(const void *item, const void *data) { + const struct seat_config *sc = item; + const char *name = data; + return strcmp(sc->name, name); +} + +struct seat_attachment_config *seat_config_get_attachment( + struct seat_config *seat_config, char *identifier) { + for (int i = 0; i < seat_config->attachments->length; ++i) { + struct seat_attachment_config *attachment = + seat_config->attachments->items[i]; + if (strcmp(attachment->identifier, identifier) == 0) { + return attachment; + } + } + + return NULL; +} + bool load_main_config(const char *file, bool is_active) { char *path; if (file != NULL) { @@ -368,7 +488,7 @@ bool load_main_config(const char *file, bool is_active) { 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)) { + (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); @@ -547,6 +667,14 @@ bool read_config(FILE *file, struct sway_config *config) { } break; + case CMD_BLOCK_SEAT: + if (block == CMD_BLOCK_END) { + block = CMD_BLOCK_SEAT; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + case CMD_BLOCK_BAR: if (block == CMD_BLOCK_END) { block = CMD_BLOCK_BAR; @@ -601,6 +729,12 @@ bool read_config(FILE *file, struct sway_config *config) { block = CMD_BLOCK_END; break; + case CMD_BLOCK_SEAT: + sway_log(L_DEBUG, "End of seat block"); + current_seat_config = NULL; + block = CMD_BLOCK_END; + break; + case CMD_BLOCK_BAR: sway_log(L_DEBUG, "End of bar block"); config->current_bar = NULL; diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 217c2ddb..3aa2d1bc 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -24,7 +24,7 @@ static void cursor_update_position(struct sway_cursor *cursor) { static void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time) { - struct wlr_seat *seat = cursor->seat->seat; + struct wlr_seat *seat = cursor->seat->wlr_seat; struct wlr_surface *surface = NULL; double sx, sy; swayc_t *swayc = @@ -72,7 +72,7 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { sway_seat_set_focus(cursor->seat, swayc); } - wlr_seat_pointer_notify_button(cursor->seat->seat, event->time_msec, + wlr_seat_pointer_notify_button(cursor->seat->wlr_seat, event->time_msec, event->button, event->state); } @@ -80,7 +80,7 @@ static void handle_cursor_axis(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, axis); struct wlr_event_pointer_axis *event = data; - wlr_seat_pointer_notify_axis(cursor->seat->seat, event->time_msec, + wlr_seat_pointer_notify_axis(cursor->seat->wlr_seat, event->time_msec, event->orientation, event->delta); } @@ -173,7 +173,7 @@ struct sway_cursor *sway_cursor_create(struct sway_seat *seat) { wl_signal_add(&wlr_cursor->events.tablet_tool_tip, &cursor->tool_tip); cursor->tool_tip.notify = handle_tool_tip; - wl_signal_add(&seat->seat->events.request_set_cursor, + wl_signal_add(&seat->wlr_seat->events.request_set_cursor, &cursor->request_set_cursor); cursor->request_set_cursor.notify = handle_request_set_cursor; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index b07a733e..1950b6d9 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -18,12 +18,13 @@ static const char *default_seat = "seat0"; struct sway_input_manager *input_manager; struct input_config *current_input_config = NULL; +struct seat_config *current_seat_config = NULL; static struct sway_seat *input_manager_get_seat( struct sway_input_manager *input, const char *seat_name) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - if (strcmp(seat->seat->name, seat_name) == 0) { + if (strcmp(seat->wlr_seat->name, seat_name) == 0) { return seat; } } @@ -58,56 +59,88 @@ static char *get_device_identifier(struct wlr_input_device *device) { return identifier; } -static struct sway_input_device *input_sway_device_from_wlr(struct sway_input_manager *input, - struct wlr_input_device *device) { - struct sway_input_device *sway_device = NULL; - wl_list_for_each(sway_device, &input->devices, link) { - if (sway_device->wlr_device == device) { - return sway_device; +static struct sway_input_device *input_sway_device_from_wlr( + struct sway_input_manager *input, struct wlr_input_device *device) { + struct sway_input_device *input_device = NULL; + wl_list_for_each(input_device, &input->devices, link) { + if (input_device->wlr_device == device) { + return input_device; } } return NULL; } -static struct sway_input_device *input_sway_device_from_config(struct sway_input_manager *input, - struct input_config *config) { - struct sway_input_device *sway_device = NULL; - wl_list_for_each(sway_device, &input->devices, link) { - if (strcmp(sway_device->identifier, config->identifier) == 0) { - return sway_device; +static struct sway_input_device *input_sway_device_from_config( + struct sway_input_manager *input, struct input_config *config) { + struct sway_input_device *input_device = NULL; + wl_list_for_each(input_device, &input->devices, link) { + if (strcmp(input_device->identifier, config->identifier) == 0) { + return input_device; } } return NULL; } +static struct sway_input_device *input_sway_device_from_identifier( + struct sway_input_manager *input, char *identifier) { + struct sway_input_device *input_device = NULL; + wl_list_for_each(input_device, &input->devices, link) { + if (strcmp(input_device->identifier, identifier) == 0) { + return input_device; + } + } + return NULL; +} + +static bool input_has_seat_configuration(struct sway_input_manager *input) { + struct sway_seat *seat = NULL; + wl_list_for_each(seat, &input->seats, link) { + if (seat->config) { + return true; + } + } + + return false; +} + static void input_add_notify(struct wl_listener *listener, void *data) { struct sway_input_manager *input = wl_container_of(listener, input, input_add); struct wlr_input_device *device = data; - struct sway_input_device *sway_device = + struct sway_input_device *input_device = calloc(1, sizeof(struct sway_input_device)); - if (!sway_assert(sway_device, "could not allocate input device")) { + if (!sway_assert(input_device, "could not allocate input device")) { return; } - sway_device->wlr_device = device; - sway_device->identifier = get_device_identifier(device); - wl_list_insert(&input->devices, &sway_device->link); + input_device->wlr_device = device; + input_device->identifier = get_device_identifier(device); + wl_list_insert(&input->devices, &input_device->link); // find config for (int i = 0; i < config->input_configs->length; ++i) { struct input_config *input_config = config->input_configs->items[i]; - if (strcmp(input_config->identifier, sway_device->identifier) == 0) { - sway_device->config = input_config; + if (strcmp(input_config->identifier, input_device->identifier) == 0) { + input_device->config = input_config; break; } } - const char *seat_name = - (sway_device->config ? sway_device->config->seat : default_seat); - struct sway_seat *seat = input_manager_get_seat(input, seat_name); - sway_seat_add_device(seat, sway_device); + struct sway_seat *seat = NULL; + if (!input_has_seat_configuration(input)) { + seat = input_manager_get_seat(input, default_seat); + sway_seat_add_device(seat, input_device); + return; + } + + wl_list_for_each(seat, &input->seats, link) { + if (seat->config && + (seat_config_get_attachment(seat->config, input_device->identifier) || + seat_config_get_attachment(seat->config, "*"))) { + sway_seat_add_device(seat, input_device); + } + } } static void input_remove_notify(struct wl_listener *listener, void *data) { @@ -115,21 +148,21 @@ static void input_remove_notify(struct wl_listener *listener, void *data) { wl_container_of(listener, input, input_remove); struct wlr_input_device *device = data; - struct sway_input_device *sway_device = + struct sway_input_device *input_device = input_sway_device_from_wlr(input, device); - if (!sway_assert(sway_device, "could not find sway device")) { + if (!sway_assert(input_device, "could not find sway device")) { return; } struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - sway_seat_remove_device(seat, sway_device); + sway_seat_remove_device(seat, input_device); } - wl_list_remove(&sway_device->link); - free(sway_device->identifier); - free(sway_device); + wl_list_remove(&input_device->link); + free(input_device->identifier); + free(input_device); } struct sway_input_manager *sway_input_manager_create( @@ -139,7 +172,6 @@ struct sway_input_manager *sway_input_manager_create( if (!input) { return NULL; } - // XXX probably don't need the full server input->server = server; wl_list_init(&input->devices); @@ -177,22 +209,53 @@ void sway_input_manager_set_focus(struct sway_input_manager *input, } } -void sway_input_manager_apply_config(struct sway_input_manager *input, +void sway_input_manager_apply_input_config(struct sway_input_manager *input, struct input_config *input_config) { - struct sway_input_device *sway_device = + struct sway_input_device *input_device = input_sway_device_from_config(input, input_config); - if (!sway_device) { + if (!input_device) { return; } + input_device->config = input_config; struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - sway_seat_remove_device(seat, sway_device); + sway_seat_configure_device(seat, input_device); + } +} + +void sway_input_manager_apply_seat_config(struct sway_input_manager *input, + struct seat_config *seat_config) { + struct sway_seat *seat = input_manager_get_seat(input, seat_config->name); + // the old config is invalid so clear it + sway_seat_set_config(seat, NULL); + + // clear devices + struct sway_input_device *input_device = NULL; + wl_list_for_each(input_device, &input->devices, link) { + sway_seat_remove_device(seat, input_device); + } + + if (seat_config_get_attachment(seat_config, "*")) { + wl_list_for_each(input_device, &input->devices, link) { + sway_seat_add_device(seat, input_device); + } + } else { + for (int i = 0; i < seat_config->attachments->length; ++i) { + struct seat_attachment_config *attachment = + seat_config->attachments->items[i]; + + struct sway_input_device *device = + input_sway_device_from_identifier(input, + attachment->identifier); + + if (device) { + sway_seat_add_device(seat, device); + } + } } - const char *seat_name = (input_config->seat ? input_config->seat : default_seat); - seat = input_manager_get_seat(input, seat_name); - sway_seat_add_device(seat, sway_device); + sway_seat_set_config(seat, seat_config); } void sway_input_manager_configure_xcursor(struct sway_input_manager *input) { diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 6a792c65..53db3270 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -5,32 +5,46 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { struct sway_keyboard *keyboard = wl_container_of(listener, keyboard, keyboard_key); + struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat; + struct wlr_input_device *wlr_device = + keyboard->seat_device->input_device->wlr_device; struct wlr_event_keyboard_key *event = data; - wlr_seat_set_keyboard(keyboard->seat->seat, keyboard->device->wlr_device); - wlr_seat_keyboard_notify_key(keyboard->seat->seat, event->time_msec, - event->keycode, event->state); + wlr_keyboard_set_keymap(wlr_device->keyboard, keyboard->keymap); + wlr_seat_set_keyboard(wlr_seat, wlr_device); + wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec,event->keycode, + event->state); } static void handle_keyboard_modifiers(struct wl_listener *listener, void *data) { struct sway_keyboard *keyboard = wl_container_of(listener, keyboard, keyboard_modifiers); - wlr_seat_set_keyboard(keyboard->seat->seat, keyboard->device->wlr_device); - wlr_seat_keyboard_notify_modifiers(keyboard->seat->seat); + struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat; + struct wlr_input_device *wlr_device = + keyboard->seat_device->input_device->wlr_device; + wlr_keyboard_set_keymap(wlr_device->keyboard, keyboard->keymap); + wlr_seat_set_keyboard(wlr_seat, wlr_device); + wlr_seat_keyboard_notify_modifiers(wlr_seat); } struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, - struct sway_input_device *device) { + struct sway_seat_device *device) { struct sway_keyboard *keyboard = calloc(1, sizeof(struct sway_keyboard)); if (!sway_assert(keyboard, "could not allocate sway keyboard")) { return NULL; } - keyboard->device = device; - keyboard->seat = seat; + keyboard->seat_device = device; + device->keyboard = keyboard; - // TODO keyboard config + wl_list_init(&keyboard->keyboard_key.link); + wl_list_init(&keyboard->keyboard_modifiers.link); + + return keyboard; +} + +void sway_keyboard_configure(struct sway_keyboard *keyboard) { struct xkb_rule_names rules; memset(&rules, 0, sizeof(rules)); rules.rules = getenv("XKB_DEFAULT_RULES"); @@ -38,27 +52,32 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!sway_assert(context, "cannot create XKB context")) { - return NULL; + return; } - wlr_keyboard_set_keymap(device->wlr_device->keyboard, - xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + keyboard->keymap = + xkb_keymap_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); + wlr_keyboard_set_keymap(keyboard->seat_device->input_device->wlr_device->keyboard, keyboard->keymap); xkb_context_unref(context); - wl_signal_add(&device->wlr_device->keyboard->events.key, + wl_list_remove(&keyboard->keyboard_key.link); + wl_signal_add( + &keyboard->seat_device->input_device->wlr_device->keyboard->events.key, &keyboard->keyboard_key); keyboard->keyboard_key.notify = handle_keyboard_key; - wl_signal_add(&device->wlr_device->keyboard->events.modifiers, + wl_list_remove(&keyboard->keyboard_modifiers.link); + wl_signal_add( + &keyboard->seat_device->input_device->wlr_device->keyboard->events.modifiers, &keyboard->keyboard_modifiers); keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers; - - return keyboard; } void sway_keyboard_destroy(struct sway_keyboard *keyboard) { + xkb_keymap_unref(keyboard->keymap); wl_list_remove(&keyboard->keyboard_key.link); wl_list_remove(&keyboard->keyboard_modifiers.link); wl_list_remove(&keyboard->link); diff --git a/sway/input/seat.c b/sway/input/seat.c index 80c6424f..1b25419b 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -9,6 +9,18 @@ #include "sway/view.h" #include "log.h" +static void seat_device_destroy(struct sway_seat_device *seat_device) { + if (!seat_device) { + return; + } + + sway_keyboard_destroy(seat_device->keyboard); + wlr_cursor_detach_input_device(seat_device->sway_seat->cursor->cursor, + seat_device->input_device->wlr_device); + wl_list_remove(&seat_device->link); + free(seat_device); +} + struct sway_seat *sway_seat_create(struct sway_input_manager *input, const char *seat_name) { struct sway_seat *seat = calloc(1, sizeof(struct sway_seat)); @@ -16,22 +28,22 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, return NULL; } - seat->seat = wlr_seat_create(input->server->wl_display, seat_name); - if (!sway_assert(seat->seat, "could not allocate seat")) { + seat->wlr_seat = wlr_seat_create(input->server->wl_display, seat_name); + if (!sway_assert(seat->wlr_seat, "could not allocate seat")) { return NULL; } seat->cursor = sway_cursor_create(seat); if (!seat->cursor) { - wlr_seat_destroy(seat->seat); + wlr_seat_destroy(seat->wlr_seat); free(seat); return NULL; } seat->input = input; - seat->devices = create_list(); + wl_list_init(&seat->devices); - wlr_seat_set_capabilities(seat->seat, + wlr_seat_set_capabilities(seat->wlr_seat, WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH); @@ -43,88 +55,94 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, return seat; } -static void seat_add_pointer(struct sway_seat *seat, - struct sway_input_device *sway_device) { +static void seat_configure_pointer(struct sway_seat *seat, + struct sway_seat_device *sway_device) { // TODO pointer configuration wlr_cursor_attach_input_device(seat->cursor->cursor, - sway_device->wlr_device); + sway_device->input_device->wlr_device); } -static void seat_add_keyboard(struct sway_seat *seat, - struct sway_input_device *device) { - // TODO keyboard configuration - sway_keyboard_create(seat, device); - wlr_seat_set_keyboard(seat->seat, device->wlr_device); +static void seat_configure_keyboard(struct sway_seat *seat, + struct sway_seat_device *seat_device) { + if (!seat_device->keyboard) { + sway_keyboard_create(seat, seat_device); + } + sway_keyboard_configure(seat_device->keyboard); } -bool sway_seat_has_device(struct sway_seat *seat, - struct sway_input_device *device) { - return false; +static struct sway_seat_device *sway_seat_get_device(struct sway_seat *seat, + struct sway_input_device *input_device) { + struct sway_seat_device *seat_device = NULL; + wl_list_for_each(seat_device, &seat->devices, link) { + if (seat_device->input_device == input_device) { + return seat_device; + } + } + + return NULL; } -void sway_seat_add_device(struct sway_seat *seat, - struct sway_input_device *device) { - if (sway_seat_has_device(seat, device)) { +void sway_seat_configure_device(struct sway_seat *seat, + struct sway_input_device *input_device) { + struct sway_seat_device *seat_device = + sway_seat_get_device(seat, input_device); + if (!seat_device) { return; } - sway_log(L_DEBUG, "input add: %s", device->identifier); - switch (device->wlr_device->type) { + if (seat->config) { + seat_device->attachment_config = + seat_config_get_attachment(seat->config, input_device->identifier); + } + + switch (input_device->wlr_device->type) { case WLR_INPUT_DEVICE_POINTER: - seat_add_pointer(seat, device); + seat_configure_pointer(seat, seat_device); break; case WLR_INPUT_DEVICE_KEYBOARD: - seat_add_keyboard(seat, device); + seat_configure_keyboard(seat, seat_device); + wlr_seat_set_keyboard(seat->wlr_seat, + seat_device->input_device->wlr_device); break; case WLR_INPUT_DEVICE_TOUCH: case WLR_INPUT_DEVICE_TABLET_PAD: case WLR_INPUT_DEVICE_TABLET_TOOL: - sway_log(L_DEBUG, "TODO: add other devices"); + sway_log(L_DEBUG, "TODO: configure other devices"); break; } - - list_add(seat->devices, device); } -static void seat_remove_keyboard(struct sway_seat *seat, - struct sway_input_device *device) { - if (device && device->keyboard) { - sway_keyboard_destroy(device->keyboard); +void sway_seat_add_device(struct sway_seat *seat, + struct sway_input_device *input_device) { + if (sway_seat_get_device(seat, input_device)) { + return; } -} -static void seat_remove_pointer(struct sway_seat *seat, - struct sway_input_device *device) { - wlr_cursor_detach_input_device(seat->cursor->cursor, device->wlr_device); + struct sway_seat_device *seat_device = + calloc(1, sizeof(struct sway_seat_device)); + if (!seat_device) { + sway_log(L_DEBUG, "could not allocate seat device"); + return; + } + + seat_device->sway_seat = seat; + seat_device->input_device = input_device; + wl_list_insert(&seat->devices, &seat_device->link); + + sway_seat_configure_device(seat, input_device); } void sway_seat_remove_device(struct sway_seat *seat, - struct sway_input_device *device) { - sway_log(L_DEBUG, "input remove: %s", device->identifier); - if (!sway_seat_has_device(seat, device)) { - return; - } + struct sway_input_device *input_device) { + sway_log(L_DEBUG, "input remove: %s", input_device->identifier); + struct sway_seat_device *seat_device = + sway_seat_get_device(seat, input_device); - switch (device->wlr_device->type) { - case WLR_INPUT_DEVICE_POINTER: - seat_remove_pointer(seat, device); - break; - case WLR_INPUT_DEVICE_KEYBOARD: - seat_remove_keyboard(seat, device); - break; - case WLR_INPUT_DEVICE_TOUCH: - case WLR_INPUT_DEVICE_TABLET_PAD: - case WLR_INPUT_DEVICE_TABLET_TOOL: - sway_log(L_DEBUG, "TODO: remove other devices"); - break; + if (!seat_device) { + return; } - for (int i = 0; i < seat->devices->length; ++i) { - if (seat->devices->items[i] == device) { - list_del(seat->devices, i); - break; - } - } + seat_device_destroy(seat_device); } void sway_seat_configure_xcursor(struct sway_seat *seat) { @@ -135,7 +153,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { seat->cursor->xcursor_manager = wlr_xcursor_manager_create("default", 24); if (sway_assert(seat->cursor->xcursor_manager, - "Cannot create XCursor manager for theme %s", cursor_theme)) { + "Cannot create XCursor manager for theme %s", + cursor_theme)) { return; } } @@ -183,7 +202,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { view->iface.set_activated(view, true); wl_signal_add(&container->events.destroy, &seat->focus_destroy); seat->focus_destroy.notify = handle_focus_destroy; - wlr_seat_keyboard_notify_enter(seat->seat, view->surface); + wlr_seat_keyboard_notify_enter(seat->wlr_seat, view->surface); } seat->focus = container; @@ -195,3 +214,29 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { } } + +void sway_seat_set_config(struct sway_seat *seat, + struct seat_config *seat_config) { + // clear configs + seat->config = NULL; + + struct sway_seat_device *seat_device = NULL; + wl_list_for_each(seat_device, &seat->devices, link) { + seat_device->attachment_config = NULL; + } + + if (!seat_config) { + return; + } + + // add configs + seat->config = seat_config; + + wl_list_for_each(seat_device, &seat->devices, link) { + seat_device->attachment_config = + seat_config_get_attachment(seat_config, + seat_device->input_device->identifier); + sway_seat_configure_device(seat, seat_device->input_device); + } + +} diff --git a/sway/meson.build b/sway/meson.build index fad1f88c..ad8160eb 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -11,7 +11,8 @@ sway_sources = files( 'commands/exec_always.c', 'commands/include.c', 'commands/input.c', - 'commands/input/seat.c', + 'commands/seat.c', + 'commands/seat/attach.c', 'commands/input/accel_profile.c', 'commands/input/click_method.c', 'commands/input/drag_lock.c', -- cgit v1.2.3 From 9eecbb5d8a988a0dded57ead1982dd0121071454 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Fri, 15 Dec 2017 05:22:51 -0500 Subject: xkb config --- include/sway/commands.h | 5 +++++ include/sway/config.h | 6 ++++++ sway/commands.c | 5 +++++ sway/commands/input/xkb_layout.c | 24 ++++++++++++++++++++++++ sway/commands/input/xkb_model.c | 24 ++++++++++++++++++++++++ sway/commands/input/xkb_options.c | 24 ++++++++++++++++++++++++ sway/commands/input/xkb_rules.c | 24 ++++++++++++++++++++++++ sway/commands/input/xkb_variant.c | 24 ++++++++++++++++++++++++ sway/config.c | 20 ++++++++++++++++++++ sway/input/keyboard.c | 38 +++++++++++++++++++++++++++++++++----- sway/main.c | 1 + sway/meson.build | 5 +++++ 12 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 sway/commands/input/xkb_layout.c create mode 100644 sway/commands/input/xkb_model.c create mode 100644 sway/commands/input/xkb_options.c create mode 100644 sway/commands/input/xkb_rules.c create mode 100644 sway/commands/input/xkb_variant.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index ce74e1ed..61950d0d 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -195,6 +195,11 @@ sway_cmd input_cmd_natural_scroll; sway_cmd input_cmd_pointer_accel; sway_cmd input_cmd_scroll_method; sway_cmd input_cmd_tap; +sway_cmd input_cmd_xkb_layout; +sway_cmd input_cmd_xkb_model; +sway_cmd input_cmd_xkb_options; +sway_cmd input_cmd_xkb_rules; +sway_cmd input_cmd_xkb_variant; sway_cmd seat_cmd_attach; diff --git a/include/sway/config.h b/include/sway/config.h index 5df5d61e..9bfda259 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -67,6 +67,12 @@ struct input_config { int send_events; int tap; + char *xkb_layout; + char *xkb_model; + char *xkb_options; + char *xkb_rules; + char *xkb_variant; + bool capturable; struct wlr_box region; }; diff --git a/sway/commands.c b/sway/commands.c index e003e06d..b8948fb7 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -154,6 +154,11 @@ static struct cmd_handler input_handlers[] = { { "pointer_accel", input_cmd_pointer_accel }, { "scroll_method", input_cmd_scroll_method }, { "tap", input_cmd_tap }, + { "xkb_layout", input_cmd_xkb_layout }, + { "xkb_model", input_cmd_xkb_model }, + { "xkb_options", input_cmd_xkb_options }, + { "xkb_rules", input_cmd_xkb_rules }, + { "xkb_variant", input_cmd_xkb_variant }, }; // must be in order for the bsearch diff --git a/sway/commands/input/xkb_layout.c b/sway/commands/input/xkb_layout.c new file mode 100644 index 00000000..9a9ce044 --- /dev/null +++ b/sway/commands/input/xkb_layout.c @@ -0,0 +1,24 @@ +#define _XOPEN_SOURCE 700 +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { + sway_log(L_DEBUG, "xkb layout for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "xkb_layout", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "xkb_layout", "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + new_config->xkb_layout = strdup(argv[0]); + + sway_log(L_DEBUG, "apply-xkb_layout for device: %s", + current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/xkb_model.c b/sway/commands/input/xkb_model.c new file mode 100644 index 00000000..14a50ffb --- /dev/null +++ b/sway/commands/input/xkb_model.c @@ -0,0 +1,24 @@ +#define _XOPEN_SOURCE 700 +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { + sway_log(L_DEBUG, "xkb model for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "xkb_model", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "xkb_model", "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + new_config->xkb_model = strdup(argv[0]); + + sway_log(L_DEBUG, "apply-xkb_model for device: %s", + current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/xkb_options.c b/sway/commands/input/xkb_options.c new file mode 100644 index 00000000..67eb5342 --- /dev/null +++ b/sway/commands/input/xkb_options.c @@ -0,0 +1,24 @@ +#define _XOPEN_SOURCE 700 +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { + sway_log(L_DEBUG, "xkb options for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "xkb_options", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "xkb_options", "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + new_config->xkb_options = strdup(argv[0]); + + sway_log(L_DEBUG, "apply-xkb_options for device: %s", + current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/xkb_rules.c b/sway/commands/input/xkb_rules.c new file mode 100644 index 00000000..3eda0bdd --- /dev/null +++ b/sway/commands/input/xkb_rules.c @@ -0,0 +1,24 @@ +#define _XOPEN_SOURCE 700 +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { + sway_log(L_DEBUG, "xkb rules for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "xkb_rules", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "xkb_rules", "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + new_config->xkb_rules = strdup(argv[0]); + + sway_log(L_DEBUG, "apply-xkb_rules for device: %s", + current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/input/xkb_variant.c b/sway/commands/input/xkb_variant.c new file mode 100644 index 00000000..c7f93ad4 --- /dev/null +++ b/sway/commands/input/xkb_variant.c @@ -0,0 +1,24 @@ +#define _XOPEN_SOURCE 700 +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { + sway_log(L_DEBUG, "xkb variant for device: %s", current_input_config->identifier); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "xkb_variant", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "xkb_variant", "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + new_config->xkb_variant = strdup(argv[0]); + + sway_log(L_DEBUG, "apply-xkb_variant for device: %s", + current_input_config->identifier); + input_cmd_apply(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 52633ad8..4e34aa8c 100644 --- a/sway/config.c +++ b/sway/config.c @@ -292,6 +292,26 @@ void merge_input_config(struct input_config *dst, struct input_config *src) { if (src->tap != INT_MIN) { dst->tap = src->tap; } + if (src->xkb_layout) { + free(dst->xkb_layout); + dst->xkb_layout = strdup(src->xkb_layout); + } + if (src->xkb_model) { + free(dst->xkb_model); + dst->xkb_model = strdup(src->xkb_model); + } + if (src->xkb_options) { + free(dst->xkb_options); + dst->xkb_options = strdup(src->xkb_options); + } + if (src->xkb_rules) { + free(dst->xkb_rules); + dst->xkb_rules = strdup(src->xkb_rules); + } + if (src->xkb_variant) { + free(dst->xkb_variant); + dst->xkb_variant = strdup(src->xkb_variant); + } } void free_input_config(struct input_config *ic) { diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 53db3270..2ab0206a 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -1,5 +1,6 @@ #include "sway/input/seat.h" #include "sway/input/keyboard.h" +#include "sway/input/input-manager.h" #include "log.h" static void handle_keyboard_key(struct wl_listener *listener, void *data) { @@ -47,17 +48,44 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, void sway_keyboard_configure(struct sway_keyboard *keyboard) { struct xkb_rule_names rules; memset(&rules, 0, sizeof(rules)); - rules.rules = getenv("XKB_DEFAULT_RULES"); - rules.model = getenv("XKB_DEFAULT_MODEL"); - rules.layout = getenv("XKB_DEFAULT_LAYOUT"); - rules.variant = getenv("XKB_DEFAULT_VARIANT"); - rules.options = getenv("XKB_DEFAULT_OPTIONS"); + struct input_config *input_config = + keyboard->seat_device->input_device->config; + + if (input_config && input_config->xkb_layout) { + rules.layout = input_config->xkb_layout; + } else { + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + } + if (input_config && input_config->xkb_model) { + rules.model = input_config->xkb_model; + } else { + rules.model = getenv("XKB_DEFAULT_MODEL"); + } + + if (input_config && input_config->xkb_options) { + rules.options = input_config->xkb_options; + } else { + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + } + + if (input_config && input_config->xkb_rules) { + rules.rules = input_config->xkb_rules; + } else { + rules.rules = getenv("XKB_DEFAULT_RULES"); + } + + if (input_config && input_config->xkb_variant) { + rules.variant = input_config->xkb_variant; + } else { + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + } struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!sway_assert(context, "cannot create XKB context")) { return; } + xkb_keymap_unref(keyboard->keymap); keyboard->keymap = xkb_keymap_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); wlr_keyboard_set_keymap(keyboard->seat_device->input_device->wlr_device->keyboard, keyboard->keymap); diff --git a/sway/main.c b/sway/main.c index 363f4d96..25032aa0 100644 --- a/sway/main.c +++ b/sway/main.c @@ -158,6 +158,7 @@ static void log_distro() { } static void log_kernel() { + return; FILE *f = popen("uname -a", "r"); if (!f) { sway_log(L_INFO, "Unable to determine kernel version"); diff --git a/sway/meson.build b/sway/meson.build index ad8160eb..c81e1c2c 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -24,6 +24,11 @@ sway_sources = files( 'commands/input/pointer_accel.c', 'commands/input/scroll_method.c', 'commands/input/tap.c', + 'commands/input/xkb_layout.c', + 'commands/input/xkb_model.c', + 'commands/input/xkb_options.c', + 'commands/input/xkb_rules.c', + 'commands/input/xkb_variant.c', 'config.c', 'ipc-json.c', 'ipc-server.c', -- cgit v1.2.3 From f16aa3c0ad8328da0820e81c99a619835f36082f Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 16 Dec 2017 12:14:24 -0500 Subject: rename config apply cmds --- include/sway/commands.h | 3 --- include/sway/config.h | 2 ++ sway/commands.c | 4 ++-- sway/commands/input/accel_profile.c | 3 ++- sway/commands/input/click_method.c | 3 ++- sway/commands/input/drag_lock.c | 3 ++- sway/commands/input/dwt.c | 3 ++- sway/commands/input/events.c | 3 ++- sway/commands/input/left_handed.c | 3 ++- sway/commands/input/middle_emulation.c | 3 ++- sway/commands/input/natural_scroll.c | 3 ++- sway/commands/input/pointer_accel.c | 3 ++- sway/commands/input/scroll_method.c | 3 ++- sway/commands/input/tap.c | 3 ++- sway/commands/input/xkb_layout.c | 3 ++- sway/commands/input/xkb_model.c | 3 ++- sway/commands/input/xkb_options.c | 3 ++- sway/commands/input/xkb_rules.c | 3 ++- sway/commands/input/xkb_variant.c | 3 ++- sway/commands/seat/attach.c | 2 +- 20 files changed, 37 insertions(+), 22 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 61950d0d..5008831d 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -42,9 +42,6 @@ enum expected_args { EXPECTED_EQUAL_TO }; -void input_cmd_apply(struct input_config *input); -void seat_cmd_apply(struct seat_config *seat); - struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val); diff --git a/include/sway/config.h b/include/sway/config.h index 777fb5a8..eb642dc2 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -384,6 +384,7 @@ int input_identifier_cmp(const void *item, const void *data); struct input_config *new_input_config(const char* identifier); void merge_input_config(struct input_config *dst, struct input_config *src); void free_input_config(struct input_config *ic); +void apply_input_config(struct input_config *input); int seat_name_cmp(const void *item, const void *data); struct seat_config *new_seat_config(const char* name); @@ -392,6 +393,7 @@ void free_seat_config(struct seat_config *ic); struct seat_attachment_config *seat_attachment_config_new(); struct seat_attachment_config *seat_config_get_attachment( struct seat_config *seat_config, char *identifier); +void apply_seat_config(struct seat_config *seat); int output_name_cmp(const void *item, const void *data); struct output_config *new_output_config(); diff --git a/sway/commands.c b/sway/commands.c index 7485f2f6..3fb1842d 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -57,7 +57,7 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type return error; } -void input_cmd_apply(struct input_config *input) { +void apply_input_config(struct input_config *input) { int i; i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier); if (i >= 0) { @@ -74,7 +74,7 @@ void input_cmd_apply(struct input_config *input) { sway_input_manager_apply_input_config(input_manager, input); } -void seat_cmd_apply(struct seat_config *seat) { +void apply_seat_config(struct seat_config *seat) { int i; i = list_seq_find(config->seat_configs, seat_name_cmp, seat->name); if (i >= 0) { diff --git a/sway/commands/input/accel_profile.c b/sway/commands/input/accel_profile.c index 9b5fb7a2..f72b7d48 100644 --- a/sway/commands/input/accel_profile.c +++ b/sway/commands/input/accel_profile.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -24,6 +25,6 @@ struct cmd_results *input_cmd_accel_profile(int argc, char **argv) { "Expected 'accel_profile '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c index a0e3bddf..dcf64c1a 100644 --- a/sway/commands/input/click_method.c +++ b/sway/commands/input/click_method.c @@ -1,6 +1,7 @@ #include #include #include "sway/commands.h" +#include "sway/config.h" #include "sway/input/input-manager.h" #include "log.h" @@ -29,6 +30,6 @@ struct cmd_results *input_cmd_click_method(int argc, char **argv) { "Expected 'click_method #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -24,6 +25,6 @@ struct cmd_results *input_cmd_drag_lock(int argc, char **argv) { "Expected 'drag_lock '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/dwt.c b/sway/commands/input/dwt.c index 0954575c..8108a110 100644 --- a/sway/commands/input/dwt.c +++ b/sway/commands/input/dwt.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -23,6 +24,6 @@ struct cmd_results *input_cmd_dwt(int argc, char **argv) { "Expected 'dwt '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c index f44c0ec7..8a74c11e 100644 --- a/sway/commands/input/events.c +++ b/sway/commands/input/events.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -30,6 +31,6 @@ struct cmd_results *input_cmd_events(int argc, char **argv) { "Expected 'events '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/left_handed.c b/sway/commands/input/left_handed.c index dc8fcd56..35740df3 100644 --- a/sway/commands/input/left_handed.c +++ b/sway/commands/input/left_handed.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -24,6 +25,6 @@ struct cmd_results *input_cmd_left_handed(int argc, char **argv) { "Expected 'left_handed '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/middle_emulation.c b/sway/commands/input/middle_emulation.c index e19964d8..7bc08ae6 100644 --- a/sway/commands/input/middle_emulation.c +++ b/sway/commands/input/middle_emulation.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -25,6 +26,6 @@ struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) { "Expected 'middle_emulation '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/natural_scroll.c b/sway/commands/input/natural_scroll.c index 8272c5b3..a7dcdc2c 100644 --- a/sway/commands/input/natural_scroll.c +++ b/sway/commands/input/natural_scroll.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -24,6 +25,6 @@ struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) { "Expected 'natural_scroll '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c index 2c9db5bf..d2261a63 100644 --- a/sway/commands/input/pointer_accel.c +++ b/sway/commands/input/pointer_accel.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -22,6 +23,6 @@ struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { } new_config->pointer_accel = pointer_accel; - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/scroll_method.c b/sway/commands/input/scroll_method.c index 40277155..035262cf 100644 --- a/sway/commands/input/scroll_method.c +++ b/sway/commands/input/scroll_method.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" @@ -28,6 +29,6 @@ struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { "Expected 'scroll_method '"); } - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c index 18a54087..8547c0cd 100644 --- a/sway/commands/input/tap.c +++ b/sway/commands/input/tap.c @@ -1,5 +1,6 @@ #include #include +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -27,6 +28,6 @@ struct cmd_results *input_cmd_tap(int argc, char **argv) { sway_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_layout.c b/sway/commands/input/xkb_layout.c index 9a9ce044..4470fb1d 100644 --- a/sway/commands/input/xkb_layout.c +++ b/sway/commands/input/xkb_layout.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -19,6 +20,6 @@ struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { sway_log(L_DEBUG, "apply-xkb_layout for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_model.c b/sway/commands/input/xkb_model.c index 14a50ffb..167ce2e7 100644 --- a/sway/commands/input/xkb_model.c +++ b/sway/commands/input/xkb_model.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -19,6 +20,6 @@ struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { sway_log(L_DEBUG, "apply-xkb_model for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_options.c b/sway/commands/input/xkb_options.c index 67eb5342..180f40d5 100644 --- a/sway/commands/input/xkb_options.c +++ b/sway/commands/input/xkb_options.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -19,6 +20,6 @@ struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { sway_log(L_DEBUG, "apply-xkb_options for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_rules.c b/sway/commands/input/xkb_rules.c index 3eda0bdd..047e8c84 100644 --- a/sway/commands/input/xkb_rules.c +++ b/sway/commands/input/xkb_rules.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -19,6 +20,6 @@ struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { sway_log(L_DEBUG, "apply-xkb_rules for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_variant.c b/sway/commands/input/xkb_variant.c index c7f93ad4..0e998457 100644 --- a/sway/commands/input/xkb_variant.c +++ b/sway/commands/input/xkb_variant.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include "sway/config.h" #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" @@ -19,6 +20,6 @@ struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { sway_log(L_DEBUG, "apply-xkb_variant for device: %s", current_input_config->identifier); - input_cmd_apply(new_config); + apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/seat/attach.c b/sway/commands/seat/attach.c index 996c1bda..80ec63ce 100644 --- a/sway/commands/seat/attach.c +++ b/sway/commands/seat/attach.c @@ -21,6 +21,6 @@ struct cmd_results *seat_cmd_attach(int argc, char **argv) { new_attachment->identifier = strdup(argv[0]); list_add(new_config->attachments, new_attachment); - seat_cmd_apply(new_config); + apply_seat_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3 From b8261ab24bbc8159deffadb78f5108a612d27534 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 16 Dec 2017 12:40:58 -0500 Subject: expect exactly one xkb arg --- sway/commands/input/xkb_layout.c | 2 +- sway/commands/input/xkb_model.c | 2 +- sway/commands/input/xkb_options.c | 2 +- sway/commands/input/xkb_rules.c | 2 +- sway/commands/input/xkb_variant.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/input/xkb_layout.c b/sway/commands/input/xkb_layout.c index 4470fb1d..4fc33e0b 100644 --- a/sway/commands/input/xkb_layout.c +++ b/sway/commands/input/xkb_layout.c @@ -7,7 +7,7 @@ struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { sway_log(L_DEBUG, "xkb layout for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; - if ((error = checkarg(argc, "xkb_layout", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "xkb_layout", EXPECTED_EQUAL_TO, 1))) { return error; } if (!current_input_config) { diff --git a/sway/commands/input/xkb_model.c b/sway/commands/input/xkb_model.c index 167ce2e7..338bd7de 100644 --- a/sway/commands/input/xkb_model.c +++ b/sway/commands/input/xkb_model.c @@ -7,7 +7,7 @@ struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { sway_log(L_DEBUG, "xkb model for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; - if ((error = checkarg(argc, "xkb_model", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "xkb_model", EXPECTED_EQUAL_TO, 1))) { return error; } if (!current_input_config) { diff --git a/sway/commands/input/xkb_options.c b/sway/commands/input/xkb_options.c index 180f40d5..b24c33c1 100644 --- a/sway/commands/input/xkb_options.c +++ b/sway/commands/input/xkb_options.c @@ -7,7 +7,7 @@ struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { sway_log(L_DEBUG, "xkb options for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; - if ((error = checkarg(argc, "xkb_options", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "xkb_options", EXPECTED_EQUAL_TO, 1))) { return error; } if (!current_input_config) { diff --git a/sway/commands/input/xkb_rules.c b/sway/commands/input/xkb_rules.c index 047e8c84..def614e9 100644 --- a/sway/commands/input/xkb_rules.c +++ b/sway/commands/input/xkb_rules.c @@ -7,7 +7,7 @@ struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { sway_log(L_DEBUG, "xkb rules for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; - if ((error = checkarg(argc, "xkb_rules", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "xkb_rules", EXPECTED_EQUAL_TO, 1))) { return error; } if (!current_input_config) { diff --git a/sway/commands/input/xkb_variant.c b/sway/commands/input/xkb_variant.c index 0e998457..cc135cce 100644 --- a/sway/commands/input/xkb_variant.c +++ b/sway/commands/input/xkb_variant.c @@ -7,7 +7,7 @@ struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { sway_log(L_DEBUG, "xkb variant for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; - if ((error = checkarg(argc, "xkb_variant", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "xkb_variant", EXPECTED_EQUAL_TO, 1))) { return error; } if (!current_input_config) { -- cgit v1.2.3 From c41801b75e612b8bfb0e9e386a190b59141f7857 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 16 Dec 2017 20:06:58 -0500 Subject: set keyboard config at runtime --- sway/commands/input.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'sway/commands') diff --git a/sway/commands/input.c b/sway/commands/input.c index 5ca9c2e6..dac028a7 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -44,6 +44,16 @@ struct cmd_results *cmd_input(int argc, char **argv) { res = input_cmd_scroll_method(argc_new, argv_new); } else if (strcasecmp("tap", argv[1]) == 0) { res = input_cmd_tap(argc_new, argv_new); + } else if (strcasecmp("xkb_layout", argv[1]) == 0) { + res = input_cmd_xkb_layout(argc_new, argv_new); + } else if (strcasecmp("xkb_model", argv[1]) == 0) { + res = input_cmd_xkb_model(argc_new, argv_new); + } else if (strcasecmp("xkb_options", argv[1]) == 0) { + res = input_cmd_xkb_options(argc_new, argv_new); + } else if (strcasecmp("xkb_rules", argv[1]) == 0) { + res = input_cmd_xkb_rules(argc_new, argv_new); + } else if (strcasecmp("xkb_variant", argv[1]) == 0) { + res = input_cmd_xkb_variant(argc_new, argv_new); } else { res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); } -- cgit v1.2.3 From 88bcd43ebf59cfa03a9e9a158c6f7a258c1f7db2 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 17 Dec 2017 10:39:22 -0500 Subject: seat fallback config --- include/sway/commands.h | 1 + include/sway/config.h | 1 + sway/commands.c | 1 + sway/commands/seat.c | 2 ++ sway/commands/seat/fallback.c | 29 +++++++++++++++ sway/config/seat.c | 5 +++ sway/input/input-manager.c | 83 ++++++++++++++++++++++++++++++------------- sway/input/seat.c | 3 +- sway/meson.build | 1 + 9 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 sway/commands/seat/fallback.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 5008831d..4ee7af2a 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -198,6 +198,7 @@ sway_cmd input_cmd_xkb_options; sway_cmd input_cmd_xkb_rules; sway_cmd input_cmd_xkb_variant; +sway_cmd seat_cmd_fallback; sway_cmd seat_cmd_attach; sway_cmd cmd_ipc_cmd; diff --git a/include/sway/config.h b/include/sway/config.h index eb642dc2..fdfbbedb 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -90,6 +90,7 @@ struct seat_attachment_config { */ struct seat_config { char *name; + int fallback; // -1 means not set list_t *attachments; // list of seat_attachment configs }; diff --git a/sway/commands.c b/sway/commands.c index 3fb1842d..34afb6a0 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -165,6 +165,7 @@ static struct cmd_handler input_handlers[] = { // must be in order for the bsearch static struct cmd_handler seat_handlers[] = { { "attach", seat_cmd_attach }, + { "fallback", seat_cmd_fallback }, }; static struct cmd_handler *find_handler(char *line, enum cmd_status block) { diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 4f9e259b..0149762a 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -24,6 +24,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { current_seat_config = new_seat_config(argv[0]); if (strcasecmp("attach", argv[1]) == 0) { res = seat_cmd_attach(argc_new, argv_new); + } else if (strcasecmp("fallback", argv[1]) == 0) { + res = seat_cmd_fallback(argc_new, argv_new); } else { res = cmd_results_new(CMD_INVALID, "seat ", "Unknown command %s", argv[1]); } diff --git a/sway/commands/seat/fallback.c b/sway/commands/seat/fallback.c new file mode 100644 index 00000000..7c129aae --- /dev/null +++ b/sway/commands/seat/fallback.c @@ -0,0 +1,29 @@ +#include +#include +#include "sway/config.h" +#include "sway/commands.h" +#include "sway/input/input-manager.h" + +struct cmd_results *seat_cmd_fallback(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "fallback", EXPECTED_AT_LEAST, 1))) { + return error; + } + if (!current_seat_config) { + return cmd_results_new(CMD_FAILURE, "fallback", "No seat defined"); + } + struct seat_config *new_config = + new_seat_config(current_seat_config->name); + + if (strcasecmp(argv[0], "true") == 0) { + new_config->fallback = 1; + } else if (strcasecmp(argv[0], "false") == 0) { + new_config->fallback = 0; + } else { + return cmd_results_new(CMD_INVALID, "fallback", + "Expected 'fallback '"); + } + + apply_seat_config(new_config); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config/seat.c b/sway/config/seat.c index 3a2fdaa6..4c9e8d0d 100644 --- a/sway/config/seat.c +++ b/sway/config/seat.c @@ -17,6 +17,7 @@ struct seat_config *new_seat_config(const char* name) { return NULL; } + seat->fallback = -1; seat->attachments = create_list(); if (!sway_assert(seat->attachments, "could not allocate seat attachments list")) { @@ -66,6 +67,10 @@ void merge_seat_config(struct seat_config *dest, struct seat_config *source) { dest->name = strdup(source->name); } + if (source->fallback != -1) { + dest->fallback = source->fallback; + } + for (int i = 0; i < source->attachments->length; ++i) { struct seat_attachment_config *source_attachment = source->attachments->items[i]; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index fa8e4b49..16301489 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -56,9 +56,9 @@ static char *get_device_identifier(struct wlr_input_device *device) { int len = (strlen(name) + - strlen_num(device->vendor) + - strlen_num(device->product) + - 3) * sizeof(char); + strlen_num(device->vendor) + + strlen_num(device->product) + + 3) * sizeof(char); char *identifier = malloc(len); if (!identifier) { @@ -151,13 +151,30 @@ static void input_add_notify(struct wl_listener *listener, void *data) { return; } + bool added = false; wl_list_for_each(seat, &input->seats, link) { if (seat->config && (seat_config_get_attachment(seat->config, input_device->identifier) || seat_config_get_attachment(seat->config, "*"))) { sway_seat_add_device(seat, input_device); + added = true; } } + + if (!added) { + wl_list_for_each(seat, &input->seats, link) { + if (seat->config && seat->config->fallback == 1) { + sway_seat_add_device(seat, input_device); + added = true; + } + } + } + + if (!added) { + sway_log(L_DEBUG, + "device '%s' is not configured on any seats", + input_device->identifier); + } } static void input_remove_notify(struct wl_listener *listener, void *data) { @@ -248,35 +265,53 @@ void sway_input_manager_apply_seat_config(struct sway_input_manager *input, struct seat_config *seat_config) { sway_log(L_DEBUG, "applying new seat config for seat %s", seat_config->name); struct sway_seat *seat = input_manager_get_seat(input, seat_config->name); - // the old config is invalid so clear it - sway_seat_set_config(seat, NULL); + if (!seat) { + return; + } + + sway_seat_set_config(seat, seat_config); - // clear devices + // for every device, try to add it to a seat and if no seat has it + // attached, add it to the fallback seats. struct sway_input_device *input_device = NULL; wl_list_for_each(input_device, &input->devices, link) { - sway_seat_remove_device(seat, input_device); - } - - if (seat_config_get_attachment(seat_config, "*")) { - wl_list_for_each(input_device, &input->devices, link) { - sway_seat_add_device(seat, input_device); + list_t *seat_list = create_list(); + struct sway_seat *seat = NULL; + wl_list_for_each(seat, &input->seats, link) { + if (!seat->config) { + continue; + } + if (seat_config_get_attachment(seat->config, "*") || + seat_config_get_attachment(seat->config, input_device->identifier)) { + list_add(seat_list, seat); + } } - } else { - for (int i = 0; i < seat_config->attachments->length; ++i) { - struct seat_attachment_config *attachment = - seat_config->attachments->items[i]; - - struct sway_input_device *device = - input_sway_device_from_identifier(input, - attachment->identifier); - if (device) { - sway_seat_add_device(seat, device); + if (seat_list->length) { + wl_list_for_each(seat, &input->seats, link) { + bool attached = false; + for (int i = 0; i < seat_list->length; ++i) { + if (seat == seat_list->items[i]) { + attached = true; + break; + } + } + if (attached) { + sway_seat_add_device(seat, input_device); + } else { + sway_seat_remove_device(seat, input_device); + } + } + } else { + wl_list_for_each(seat, &input->seats, link) { + if (seat->config && seat->config->fallback == 1) { + sway_seat_add_device(seat, input_device); + } else { + sway_seat_remove_device(seat, input_device); + } } } } - - sway_seat_set_config(seat, seat_config); } void sway_input_manager_configure_xcursor(struct sway_input_manager *input) { diff --git a/sway/input/seat.c b/sway/input/seat.c index 8fe82b46..94503687 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -120,6 +120,7 @@ void sway_seat_configure_device(struct sway_seat *seat, void sway_seat_add_device(struct sway_seat *seat, struct sway_input_device *input_device) { if (sway_seat_get_device(seat, input_device)) { + sway_seat_configure_device(seat, input_device); return; } @@ -246,7 +247,5 @@ void sway_seat_set_config(struct sway_seat *seat, seat_device->attachment_config = seat_config_get_attachment(seat_config, seat_device->input_device->identifier); - sway_seat_configure_device(seat, seat_device->input_device); } - } diff --git a/sway/meson.build b/sway/meson.build index 3d38c7c9..fee2ddd2 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -13,6 +13,7 @@ sway_sources = files( 'commands/input.c', 'commands/seat.c', 'commands/seat/attach.c', + 'commands/seat/fallback.c', 'commands/input/accel_profile.c', 'commands/input/click_method.c', 'commands/input/drag_lock.c', -- cgit v1.2.3 From c815d6d1a97fe940052f079bd8eb7f174f5ab004 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 18 Dec 2017 14:13:07 +0100 Subject: Add support for fractional output scale --- include/sway/config.h | 2 +- sway/commands/output.c | 4 ++-- sway/config/output.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 139d7800..afff2738 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -82,7 +82,7 @@ struct output_config { int width, height; float refresh_rate; int x, y; - int scale; + float scale; int32_t transform; char *background; diff --git a/sway/commands/output.c b/sway/commands/output.c index d71e4d8d..7988e3e4 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -144,7 +144,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { goto fail; } char *end; - output->scale = strtol(argv[i], &end, 10); + output->scale = strtof(argv[i], &end); if (*end) { error = cmd_results_new(CMD_INVALID, "output", "Invalid scale."); @@ -278,7 +278,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { } sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " - "position %d,%d scale %d transform %d) (bg %s %s)", + "position %d,%d scale %f transform %d) (bg %s %s)", output->name, output->enabled, output->width, output->height, output->refresh_rate, output->x, output->y, output->scale, output->transform, output->background, output->background_option); diff --git a/sway/config/output.c b/sway/config/output.c index dc9ee37c..ff3f73a3 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -111,7 +111,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); } if (oc && oc->scale > 0) { - sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale); + sway_log(L_DEBUG, "Set %s scale to %f", oc->name, oc->scale); wlr_output_set_scale(wlr_output, oc->scale); } if (oc && oc->transform >= 0) { -- cgit v1.2.3 From 483ede0146cd5eb5b1a68dc365f99af0c66fd156 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 18 Dec 2017 10:44:25 -0500 Subject: improve xkb command logging --- sway/commands/input/xkb_layout.c | 4 ++-- sway/commands/input/xkb_model.c | 4 ++-- sway/commands/input/xkb_options.c | 4 ++-- sway/commands/input/xkb_rules.c | 4 ++-- sway/commands/input/xkb_variant.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/input/xkb_layout.c b/sway/commands/input/xkb_layout.c index 4fc33e0b..a25d3850 100644 --- a/sway/commands/input/xkb_layout.c +++ b/sway/commands/input/xkb_layout.c @@ -18,8 +18,8 @@ struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { new_config->xkb_layout = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_layout for device: %s", - current_input_config->identifier); + sway_log(L_DEBUG, "apply-xkb_layout for device: %s layout: %s", + current_input_config->identifier, new_config->xkb_layout); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_model.c b/sway/commands/input/xkb_model.c index 338bd7de..9729e869 100644 --- a/sway/commands/input/xkb_model.c +++ b/sway/commands/input/xkb_model.c @@ -18,8 +18,8 @@ struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { new_config->xkb_model = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_model for device: %s", - current_input_config->identifier); + sway_log(L_DEBUG, "apply-xkb_model for device: %s model: %s", + current_input_config->identifier, new_config->xkb_model); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_options.c b/sway/commands/input/xkb_options.c index b24c33c1..504849cc 100644 --- a/sway/commands/input/xkb_options.c +++ b/sway/commands/input/xkb_options.c @@ -18,8 +18,8 @@ struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { new_config->xkb_options = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_options for device: %s", - current_input_config->identifier); + sway_log(L_DEBUG, "apply-xkb_options for device: %s options: %s", + current_input_config->identifier, new_config->xkb_options); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_rules.c b/sway/commands/input/xkb_rules.c index def614e9..db7d8abe 100644 --- a/sway/commands/input/xkb_rules.c +++ b/sway/commands/input/xkb_rules.c @@ -18,8 +18,8 @@ struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { new_config->xkb_rules = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_rules for device: %s", - current_input_config->identifier); + sway_log(L_DEBUG, "apply-xkb_rules for device: %s rules: %s", + current_input_config->identifier, new_config->xkb_rules); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/input/xkb_variant.c b/sway/commands/input/xkb_variant.c index cc135cce..855e6abc 100644 --- a/sway/commands/input/xkb_variant.c +++ b/sway/commands/input/xkb_variant.c @@ -18,8 +18,8 @@ struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { new_config->xkb_variant = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_variant for device: %s", - current_input_config->identifier); + sway_log(L_DEBUG, "apply-xkb_variant for device: %s variant: %s", + current_input_config->identifier, new_config->xkb_variant); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3 From 5c036a3eac89e3dec71484fe9c22704b120254b1 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 19 Dec 2017 05:26:55 -0500 Subject: error on not enough input/seat args for cmd --- sway/commands/input.c | 88 +++++++++++++++++++++++++-------------------------- sway/commands/seat.c | 32 +++++++++---------- 2 files changed, 60 insertions(+), 60 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/input.c b/sway/commands/input.c index dac028a7..ccb1d276 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -16,50 +16,50 @@ struct cmd_results *cmd_input(int argc, char **argv) { return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); } - if (argc > 2) { - int argc_new = argc-2; - char **argv_new = argv+2; - - struct cmd_results *res; - current_input_config = new_input_config(argv[0]); - if (strcasecmp("accel_profile", argv[1]) == 0) { - res = input_cmd_accel_profile(argc_new, argv_new); - } else if (strcasecmp("click_method", argv[1]) == 0) { - res = input_cmd_click_method(argc_new, argv_new); - } else if (strcasecmp("drag_lock", argv[1]) == 0) { - res = input_cmd_drag_lock(argc_new, argv_new); - } else if (strcasecmp("dwt", argv[1]) == 0) { - res = input_cmd_dwt(argc_new, argv_new); - } else if (strcasecmp("events", argv[1]) == 0) { - res = input_cmd_events(argc_new, argv_new); - } else if (strcasecmp("left_handed", argv[1]) == 0) { - res = input_cmd_left_handed(argc_new, argv_new); - } else if (strcasecmp("middle_emulation", argv[1]) == 0) { - res = input_cmd_middle_emulation(argc_new, argv_new); - } else if (strcasecmp("natural_scroll", argv[1]) == 0) { - res = input_cmd_natural_scroll(argc_new, argv_new); - } else if (strcasecmp("pointer_accel", argv[1]) == 0) { - res = input_cmd_pointer_accel(argc_new, argv_new); - } else if (strcasecmp("scroll_method", argv[1]) == 0) { - res = input_cmd_scroll_method(argc_new, argv_new); - } else if (strcasecmp("tap", argv[1]) == 0) { - res = input_cmd_tap(argc_new, argv_new); - } else if (strcasecmp("xkb_layout", argv[1]) == 0) { - res = input_cmd_xkb_layout(argc_new, argv_new); - } else if (strcasecmp("xkb_model", argv[1]) == 0) { - res = input_cmd_xkb_model(argc_new, argv_new); - } else if (strcasecmp("xkb_options", argv[1]) == 0) { - res = input_cmd_xkb_options(argc_new, argv_new); - } else if (strcasecmp("xkb_rules", argv[1]) == 0) { - res = input_cmd_xkb_rules(argc_new, argv_new); - } else if (strcasecmp("xkb_variant", argv[1]) == 0) { - res = input_cmd_xkb_variant(argc_new, argv_new); - } else { - res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); - } - current_input_config = NULL; - return res; + if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 3))) { + return error; } - return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); + int argc_new = argc-2; + char **argv_new = argv+2; + + struct cmd_results *res; + current_input_config = new_input_config(argv[0]); + if (strcasecmp("accel_profile", argv[1]) == 0) { + res = input_cmd_accel_profile(argc_new, argv_new); + } else if (strcasecmp("click_method", argv[1]) == 0) { + res = input_cmd_click_method(argc_new, argv_new); + } else if (strcasecmp("drag_lock", argv[1]) == 0) { + res = input_cmd_drag_lock(argc_new, argv_new); + } else if (strcasecmp("dwt", argv[1]) == 0) { + res = input_cmd_dwt(argc_new, argv_new); + } else if (strcasecmp("events", argv[1]) == 0) { + res = input_cmd_events(argc_new, argv_new); + } else if (strcasecmp("left_handed", argv[1]) == 0) { + res = input_cmd_left_handed(argc_new, argv_new); + } else if (strcasecmp("middle_emulation", argv[1]) == 0) { + res = input_cmd_middle_emulation(argc_new, argv_new); + } else if (strcasecmp("natural_scroll", argv[1]) == 0) { + res = input_cmd_natural_scroll(argc_new, argv_new); + } else if (strcasecmp("pointer_accel", argv[1]) == 0) { + res = input_cmd_pointer_accel(argc_new, argv_new); + } else if (strcasecmp("scroll_method", argv[1]) == 0) { + res = input_cmd_scroll_method(argc_new, argv_new); + } else if (strcasecmp("tap", argv[1]) == 0) { + res = input_cmd_tap(argc_new, argv_new); + } else if (strcasecmp("xkb_layout", argv[1]) == 0) { + res = input_cmd_xkb_layout(argc_new, argv_new); + } else if (strcasecmp("xkb_model", argv[1]) == 0) { + res = input_cmd_xkb_model(argc_new, argv_new); + } else if (strcasecmp("xkb_options", argv[1]) == 0) { + res = input_cmd_xkb_options(argc_new, argv_new); + } else if (strcasecmp("xkb_rules", argv[1]) == 0) { + res = input_cmd_xkb_rules(argc_new, argv_new); + } else if (strcasecmp("xkb_variant", argv[1]) == 0) { + res = input_cmd_xkb_variant(argc_new, argv_new); + } else { + res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); + } + current_input_config = NULL; + return res; } diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 0149762a..155bc510 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -16,22 +16,22 @@ struct cmd_results *cmd_seat(int argc, char **argv) { return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); } - if (argc > 2) { - int argc_new = argc-2; - char **argv_new = argv+2; - - struct cmd_results *res; - current_seat_config = new_seat_config(argv[0]); - if (strcasecmp("attach", argv[1]) == 0) { - res = seat_cmd_attach(argc_new, argv_new); - } else if (strcasecmp("fallback", argv[1]) == 0) { - res = seat_cmd_fallback(argc_new, argv_new); - } else { - res = cmd_results_new(CMD_INVALID, "seat ", "Unknown command %s", argv[1]); - } - current_seat_config = NULL; - return res; + if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 3))) { + return error; } - return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); + int argc_new = argc-2; + char **argv_new = argv+2; + + struct cmd_results *res; + current_seat_config = new_seat_config(argv[0]); + if (strcasecmp("attach", argv[1]) == 0) { + res = seat_cmd_attach(argc_new, argv_new); + } else if (strcasecmp("fallback", argv[1]) == 0) { + res = seat_cmd_fallback(argc_new, argv_new); + } else { + res = cmd_results_new(CMD_INVALID, "seat ", "Unknown command %s", argv[1]); + } + current_seat_config = NULL; + return res; } -- cgit v1.2.3 From ba69f06695c24f98a05d138a53ba130108ebce6f Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Wed, 27 Dec 2017 10:08:18 -0500 Subject: binding config --- include/sway/config.h | 3 +- sway/commands.c | 2 + sway/commands/bind.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++ sway/config.c | 3 +- sway/meson.build | 1 + 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 sway/commands/bind.c (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 83ded720..7146623e 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -46,7 +46,8 @@ struct sway_mouse_binding { */ struct sway_mode { char *name; - list_t *bindings; + list_t *keysym_bindings; + list_t *keycode_bindings; }; /** diff --git a/sway/commands.c b/sway/commands.c index 34afb6a0..b0078a46 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -127,6 +127,8 @@ struct cmd_results *add_color(const char *name, char *buffer, const char *color) /* Keep alphabetized */ static struct cmd_handler handlers[] = { + { "bindcode", cmd_bindcode }, + { "bindsym", cmd_bindsym }, { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, diff --git a/sway/commands/bind.c b/sway/commands/bind.c new file mode 100644 index 00000000..62aa535a --- /dev/null +++ b/sway/commands/bind.c @@ -0,0 +1,173 @@ +#ifdef __linux__ +#include +#elif __FreeBSD__ +#include +#endif +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "stringop.h" +#include "util.h" + +int binding_order = 0; + +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); +} + +struct cmd_results *cmd_bindsym(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { + return error; + } + + struct sway_binding *binding = malloc(sizeof(struct sway_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bindsym", + "Unable to allocate binding"); + } + binding->keys = create_list(); + binding->modifiers = 0; + binding->release = false; + binding->bindcode = false; + + // Handle --release + if (strcmp("--release", argv[0]) == 0) { + if (argc >= 3) { + binding->release = true; + argv++; + argc--; + } else { + free_sway_binding(binding); + return cmd_results_new(CMD_FAILURE, "bindsym", + "Invalid bindsym command " + "(expected more than 2 arguments, got %d)", argc); + } + } + + binding->command = join_args(argv + 1, argc - 1); + + list_t *split = split_string(argv[0], "+"); + for (int i = 0; i < split->length; ++i) { + // Check for a modifier key + uint32_t mod; + if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { + binding->modifiers |= mod; + continue; + } + // Check for xkb key + xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], + XKB_KEYSYM_CASE_INSENSITIVE); + + // Check for mouse binding + if (strncasecmp(split->items[i], "button", strlen("button")) == 0 && + strlen(split->items[i]) == strlen("button0")) { + sym = ((char *)split->items[i])[strlen("button")] - '1' + BTN_LEFT; + } + if (!sym) { + struct cmd_results *ret = cmd_results_new(CMD_INVALID, "bindsym", + "Unknown key '%s'", (char *)split->items[i]); + free_sway_binding(binding); + free_flat_list(split); + return ret; + } + xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); + if (!key) { + free_sway_binding(binding); + free_flat_list(split); + return cmd_results_new(CMD_FAILURE, "bindsym", + "Unable to allocate binding"); + } + *key = sym; + list_add(binding->keys, key); + } + free_flat_list(split); + + struct sway_mode *mode = config->current_mode; + // TODO overwrite the binding if it already exists + binding->order = binding_order++; + list_add(mode->keysym_bindings, binding); + + sway_log(L_DEBUG, "bindsym - Bound %s to command %s", + argv[0], binding->command); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_bindcode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bindcode", EXPECTED_MORE_THAN, 1))) { + return error; + } + + struct sway_binding *binding = malloc(sizeof(struct sway_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bindsym", + "Unable to allocate binding"); + } + binding->keys = create_list(); + binding->modifiers = 0; + binding->release = false; + binding->bindcode = true; + + // Handle --release + if (strcmp("--release", argv[0]) == 0) { + if (argc >= 3) { + binding->release = true; + argv++; + argc--; + } else { + free_sway_binding(binding); + return cmd_results_new(CMD_FAILURE, "bindcode", + "Invalid bindcode command " + "(expected more than 2 arguments, got %d)", argc); + } + } + + binding->command = join_args(argv + 1, argc - 1); + + list_t *split = split_string(argv[0], "+"); + for (int i = 0; i < split->length; ++i) { + // Check for a modifier key + uint32_t mod; + if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { + binding->modifiers |= mod; + continue; + } + // parse keycode + xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10); + if (!xkb_keycode_is_legal_ext(keycode)) { + error = + cmd_results_new(CMD_INVALID, "bindcode", + "Invalid keycode '%s'", (char *)split->items[i]); + free_sway_binding(binding); + list_free(split); + return error; + } + xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t)); + *key = keycode - 8; + list_add(binding->keys, key); + } + free_flat_list(split); + + struct sway_mode *mode = config->current_mode; + // TODO overwrite binding if it already exists + binding->order = binding_order++; + list_add(mode->keycode_bindings, binding); + + sway_log(L_DEBUG, "bindcode - Bound %s to command %s", + argv[0], binding->command); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index b591ae9e..312e0779 100644 --- a/sway/config.c +++ b/sway/config.c @@ -53,7 +53,8 @@ static void config_defaults(struct sway_config *config) { 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; + if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup; + if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup; list_add(config->modes, config->current_mode); config->floating_mod = 0; diff --git a/sway/meson.build b/sway/meson.build index fee2ddd2..01d5ef36 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -6,6 +6,7 @@ sway_sources = files( 'input/seat.c', 'input/cursor.c', 'input/keyboard.c', + 'commands/bind.c', 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', -- cgit v1.2.3 From 21c61f1c0909b613471f18dd41e984c54540aca8 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 27 Dec 2017 21:23:30 +0100 Subject: Refactor output command, add output enable --- include/sway/config.h | 2 +- sway/commands/output.c | 441 +++++++++++++++++++++++++------------------------ sway/config/output.c | 7 +- 3 files changed, 231 insertions(+), 219 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 83ded720..045359ca 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -397,7 +397,7 @@ struct seat_attachment_config *seat_config_get_attachment( void apply_seat_config(struct seat_config *seat); int output_name_cmp(const void *item, const void *data); -struct output_config *new_output_config(); +struct output_config *new_output_config(const char *name); void merge_output_config(struct output_config *dst, struct output_config *src); void apply_output_config(struct output_config *oc, swayc_t *output); void free_output_config(struct output_config *oc); diff --git a/sway/commands/output.c b/sway/commands/output.c index 7988e3e4..8cc74bcc 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -20,253 +20,260 @@ static char *bg_options[] = { "tile", }; +static struct cmd_results *cmd_output_mode(struct output_config *output, + int *i, int argc, char **argv) { + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", "Missing mode argument."); + } + + char *end; + output->width = strtol(argv[*i], &end, 10); + if (*end) { + // Format is 1234x4321 + if (*end != 'x') { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode width."); + } + ++end; + output->height = strtol(end, &end, 10); + if (*end) { + if (*end != '@') { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode height."); + } + ++end; + output->refresh_rate = strtof(end, &end); + if (strcasecmp("Hz", end) != 0) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode refresh rate."); + } + } + } else { + // Format is 1234 4321 + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing mode argument (height)."); + } + output->height = strtol(argv[*i], &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode height."); + } + } + + return NULL; +} + +static struct cmd_results *cmd_output_position(struct output_config *output, + int *i, int argc, char **argv) { + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing position argument."); + } + + char *end; + output->x = strtol(argv[*i], &end, 10); + if (*end) { + // Format is 1234,4321 + if (*end != ',') { + return cmd_results_new(CMD_INVALID, "output", + "Invalid position x."); + } + ++end; + output->y = strtol(end, &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid position y."); + } + } else { + // Format is 1234 4321 (legacy) + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing position argument (y)."); + } + output->y = strtol(argv[*i], &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid position y."); + } + } + + return NULL; +} + +static struct cmd_results *cmd_output_scale(struct output_config *output, + int *i, int argc, char **argv) { + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing scale argument."); + } + + char *end; + output->scale = strtof(argv[*i], &end); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", "Invalid scale."); + } + + return NULL; +} + +static struct cmd_results *cmd_output_transform(struct output_config *output, + int *i, int argc, char **argv) { + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing transform argument."); + } + + char *value = argv[*i]; + if (strcmp(value, "normal") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_NORMAL; + } else if (strcmp(value, "90") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_90; + } else if (strcmp(value, "180") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_180; + } else if (strcmp(value, "270") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_270; + } else if (strcmp(value, "flipped") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; + } else if (strcmp(value, "flipped-90") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; + } else if (strcmp(value, "flipped-180") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; + } else if (strcmp(value, "flipped-270") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; + } else { + return cmd_results_new(CMD_INVALID, "output", + "Invalid output transform."); + } + + return NULL; +} + +static struct cmd_results *cmd_output_background(struct output_config *output, + int *i, int argc, char **argv) { + if (++*i >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing background file or color specification."); + } + const char *background = argv[*i]; + if (*i + 1 >= argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing background scaling mode or `solid_color`."); + } + const char *background_option = argv[*i]; + + if (strcasecmp(background_option, "solid_color") == 0) { + output->background = strdup(background); + output->background_option = strdup("solid_color"); + } else { + bool valid = false; + char *mode; + size_t j; + for (j = 0; j < (size_t)(argc - *i); ++j) { + mode = argv[*i + j]; + size_t n = sizeof(bg_options) / sizeof(char *); + for (size_t k = 0; k < n; ++k) { + if (strcasecmp(mode, bg_options[k]) == 0) { + valid = true; + break; + } + } + if (valid) { + break; + } + } + if (!valid) { + return cmd_results_new(CMD_INVALID, "output", + "Missing background scaling mode."); + } + + wordexp_t p; + char *src = join_args(argv + *i - 1, j); + if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid syntax (%s).", src); + } + free(src); + src = p.we_wordv[0]; + if (config->reading && *src != '/') { + char *conf = strdup(config->current_config); + if (conf) { + char *conf_path = dirname(conf); + src = malloc(strlen(conf_path) + strlen(src) + 2); + if (src) { + sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); + } else { + sway_log(L_ERROR, + "Unable to allocate background source"); + } + free(conf); + } else { + sway_log(L_ERROR, "Unable to allocate background source"); + } + } + if (!src || access(src, F_OK) == -1) { + wordfree(&p); + return cmd_results_new(CMD_INVALID, "output", + "Background file unreadable (%s).", src); + } + + output->background = strdup(src); + output->background_option = strdup(mode); + if (src != p.we_wordv[0]) { + free(src); + } + wordfree(&p); + + *i += j; + } + + return NULL; +} + struct cmd_results *cmd_output(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { return error; } - const char *name = argv[0]; - struct output_config *output = new_output_config(); + struct output_config *output = new_output_config(argv[0]); if (!output) { sway_log(L_ERROR, "Failed to allocate output config"); return NULL; } - output->name = strdup(name); - int i; - for (i = 1; i < argc; ++i) { + for (int i = 1; i < argc; ++i) { const char *command = argv[i]; - if (strcasecmp(command, "disable") == 0) { + if (strcasecmp(command, "enable") == 0) { + output->enabled = 1; + } else if (strcasecmp(command, "disable") == 0) { output->enabled = 0; } else if (strcasecmp(command, "mode") == 0 || strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing mode argument."); - goto fail; - } - - int width = -1, height = -1; - float refresh_rate = -1; - - char *end; - width = strtol(argv[i], &end, 10); - if (*end) { - // Format is 1234x4321 - if (*end != 'x') { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid mode width."); - goto fail; - } - ++end; - height = strtol(end, &end, 10); - if (*end) { - if (*end != '@') { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid mode height."); - goto fail; - } - ++end; - refresh_rate = strtof(end, &end); - if (strcasecmp("Hz", end) != 0) { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid mode refresh rate."); - goto fail; - } - } - } else { - // Format is 1234 4321 - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing mode argument (height)."); - goto fail; - } - height = strtol(argv[i], &end, 10); - if (*end) { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid mode height."); - goto fail; - } - } - output->width = width; - output->height = height; - output->refresh_rate = refresh_rate; + error = cmd_output_mode(output, &i, argc, argv); } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing position argument."); - goto fail; - } - - int x = -1, y = -1; - - char *end; - x = strtol(argv[i], &end, 10); - if (*end) { - // Format is 1234,4321 - if (*end != ',') { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid position x."); - goto fail; - } - ++end; - y = strtol(end, &end, 10); - if (*end) { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid position y."); - goto fail; - } - } else { - // Format is 1234 4321 (legacy) - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing position argument (y)."); - goto fail; - } - y = strtol(argv[i], &end, 10); - if (*end) { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid position y."); - goto fail; - } - } - - output->x = x; - output->y = y; + error = cmd_output_position(output, &i, argc, argv); } else if (strcasecmp(command, "scale") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing scale parameter."); - goto fail; - } - char *end; - output->scale = strtof(argv[i], &end); - if (*end) { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid scale."); - goto fail; - } + error = cmd_output_scale(output, &i, argc, argv); } else if (strcasecmp(command, "transform") == 0) { - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing transform parameter."); - goto fail; - } - char *value = argv[i]; - if (strcmp(value, "normal") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_NORMAL; - } else if (strcmp(value, "90") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_90; - } else if (strcmp(value, "180") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_180; - } else if (strcmp(value, "270") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_270; - } else if (strcmp(value, "flipped") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; - } else if (strcmp(value, "flipped-90") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; - } else if (strcmp(value, "flipped-180") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; - } else if (strcmp(value, "flipped-270") == 0) { - output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; - } else { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid output transform."); - goto fail; - } + error = cmd_output_transform(output, &i, argc, argv); } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { - wordexp_t p; - if (++i >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing background file or color specification."); - goto fail; - } - if (i + 1 >= argc) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing background scaling mode or `solid_color`."); - goto fail; - } - if (strcasecmp(argv[i + 1], "solid_color") == 0) { - output->background = strdup(argv[argc - 2]); - output->background_option = strdup("solid_color"); - } else { - // argv[i+j]=bg_option - bool valid = false; - char *mode; - size_t j; - for (j = 0; j < (size_t) (argc - i); ++j) { - mode = argv[i + j]; - size_t n = sizeof(bg_options) / sizeof(char *); - for (size_t k = 0; k < n; ++k) { - if (strcasecmp(mode, bg_options[k]) == 0) { - valid = true; - break; - } - } - if (valid) { - break; - } - } - if (!valid) { - error = cmd_results_new(CMD_INVALID, "output", - "Missing background scaling mode."); - goto fail; - } - - char *src = join_args(argv + i, j); - if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { - error = cmd_results_new(CMD_INVALID, "output", - "Invalid syntax (%s).", src); - goto fail; - } - free(src); - src = p.we_wordv[0]; - if (config->reading && *src != '/') { - char *conf = strdup(config->current_config); - if (conf) { - char *conf_path = dirname(conf); - src = malloc(strlen(conf_path) + strlen(src) + 2); - if (src) { - sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); - } else { - sway_log(L_ERROR, - "Unable to allocate background source"); - } - free(conf); - } else { - sway_log(L_ERROR, - "Unable to allocate background source"); - } - } - if (!src || access(src, F_OK) == -1) { - error = cmd_results_new(CMD_INVALID, "output", - "Background file unreadable (%s).", src); - wordfree(&p); - goto fail; - } - - output->background = strdup(src); - output->background_option = strdup(mode); - if (src != p.we_wordv[0]) { - free(src); - } - wordfree(&p); - - i += j; - } + error = cmd_output_background(output, &i, argc, argv); } else { error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command); + } + + if (error != NULL) { goto fail; } } - i = list_seq_find(config->output_configs, output_name_cmp, name); + int i = list_seq_find(config->output_configs, output_name_cmp, output->name); if (i >= 0) { // merge existing config struct output_config *oc = config->output_configs->items[i]; diff --git a/sway/config/output.c b/sway/config/output.c index ff3f73a3..f336c949 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -14,11 +14,16 @@ int output_name_cmp(const void *item, const void *data) { return strcmp(output->name, name); } -struct output_config *new_output_config() { +struct output_config *new_output_config(const char *name) { struct output_config *oc = calloc(1, sizeof(struct output_config)); if (oc == NULL) { return NULL; } + oc->name = strdup(name); + if (oc->name == NULL) { + free(oc); + return NULL; + } oc->enabled = -1; oc->width = oc->height = -1; oc->refresh_rate = -1; -- cgit v1.2.3 From 62b7ab3959468124086a1ba95361b3eed069b4a7 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 28 Dec 2017 18:50:22 -0500 Subject: overwrite old bindings --- sway/commands/bind.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++----- sway/input/keyboard.c | 14 +++++---- 2 files changed, 85 insertions(+), 12 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 62aa535a..99f54f46 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -28,6 +28,44 @@ void free_sway_binding(struct sway_binding *binding) { free(binding); } +/** + * Returns true if the bindings have the same key and modifier combinations. + * Note that keyboard layout is not considered, so the bindings might actually + * not be equivalent on some layouts. + */ +bool binding_key_compare(struct sway_binding *binding_a, + struct sway_binding *binding_b) { + if (binding_a->bindcode != binding_b->bindcode) { + return false; + } + + if (binding_a->modifiers ^ binding_b->modifiers) { + return false; + } + + if (binding_a->keys->length != binding_b->keys->length) { + return false; + } + + int keys_len = binding_a->keys->length; + for (int i = 0; i < keys_len; ++i) { + uint32_t key_a = *(uint32_t*)binding_a->keys->items[i]; + bool found = false; + for (int j = 0; j < keys_len; ++j) { + uint32_t key_b = *(uint32_t*)binding_b->keys->items[j]; + if (key_b == key_a) { + found = true; + break; + } + } + if (!found) { + return false; + } + } + + return true; +} + struct cmd_results *cmd_bindsym(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { @@ -95,11 +133,26 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { list_add(binding->keys, key); } free_flat_list(split); - - struct sway_mode *mode = config->current_mode; - // TODO overwrite the binding if it already exists binding->order = binding_order++; - list_add(mode->keysym_bindings, binding); + + list_t *mode_bindings = config->current_mode->keysym_bindings; + + // overwrite the binding if it already exists + bool overwritten = false; + for (int i = 0; i < mode_bindings->length; ++i) { + struct sway_binding *config_binding = mode_bindings->items[i]; + if (binding_key_compare(binding, config_binding)) { + sway_log(L_DEBUG, "overwriting old binding with command '%s'", + config_binding->command); + free_sway_binding(config_binding); + mode_bindings->items[i] = binding; + overwritten = true; + } + } + + if (!overwritten) { + list_add(mode_bindings, binding); + } sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); @@ -162,10 +215,26 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { } free_flat_list(split); - struct sway_mode *mode = config->current_mode; - // TODO overwrite binding if it already exists binding->order = binding_order++; - list_add(mode->keycode_bindings, binding); + + list_t *mode_bindings = config->current_mode->keycode_bindings; + + // overwrite the binding if it already exists + bool overwritten = false; + for (int i = 0; i < mode_bindings->length; ++i) { + struct sway_binding *config_binding = mode_bindings->items[i]; + if (binding_key_compare(binding, config_binding)) { + sway_log(L_DEBUG, "overwriting old binding with command '%s'", + config_binding->command); + free_sway_binding(config_binding); + mode_bindings->items[i] = binding; + overwritten = true; + } + } + + if (!overwritten) { + list_add(mode_bindings, binding); + } sway_log(L_DEBUG, "bindcode - Bound %s to command %s", argv[0], binding->command); diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index eacb6ceb..6272dcce 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -67,7 +67,9 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, list_t *keysym_bindings = config->current_mode->keysym_bindings; for (int i = 0; i < keysym_bindings->length; ++i) { struct sway_binding *binding = keysym_bindings->items[i]; + sway_log(L_DEBUG, "@@ checking binding: %s", binding->command); if (modifiers ^ binding->modifiers || n != binding->keys->length) { + sway_log(L_DEBUG, "@@ modifiers or key num dont match"); continue; } @@ -286,17 +288,18 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { xkb_keycode_t keycode = event->keycode + 8; bool handled = false; - const xkb_keysym_t *keysyms; // handle keycodes handled = keyboard_execute_bindcode(keyboard); + sway_log(L_DEBUG, "@@ handled by bindcode? %d", handled); // handle translated keysyms + const xkb_keysym_t *translated_keysyms; uint32_t translated_modifiers; size_t translated_keysyms_len = - keyboard_keysyms_translated(keyboard, keycode, &keysyms, + keyboard_keysyms_translated(keyboard, keycode, &translated_keysyms, &translated_modifiers); - pressed_keysyms_update(keyboard->pressed_keysyms_translated, keysyms, + pressed_keysyms_update(keyboard->pressed_keysyms_translated, translated_keysyms, translated_keysyms_len, event->state); if (event->state == WLR_KEY_PRESSED && !handled) { handled = keyboard_execute_bindsym(keyboard, @@ -305,9 +308,10 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { } // Handle raw keysyms + const xkb_keysym_t *raw_keysyms; uint32_t raw_modifiers; - size_t raw_keysyms_len = keyboard_keysyms_raw(keyboard, keycode, &keysyms, &raw_modifiers); - pressed_keysyms_update(keyboard->pressed_keysyms_raw, keysyms, raw_keysyms_len, + size_t raw_keysyms_len = keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers); + pressed_keysyms_update(keyboard->pressed_keysyms_raw, raw_keysyms, raw_keysyms_len, event->state); if (event->state == WLR_KEY_PRESSED && !handled) { handled = keyboard_execute_bindsym(keyboard, -- cgit v1.2.3 From bd3ca70e3d67476f4d66e9bc31c67b11aefb3519 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Fri, 29 Dec 2017 09:10:07 -0500 Subject: fix nitpicks --- sway/commands/bind.c | 21 ++++++++++----------- sway/input/keyboard.c | 17 ++++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 99f54f46..ef9a4b7a 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -16,15 +16,14 @@ int binding_order = 0; 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) { + return; } - if (binding->command) { - free(binding->command); + + if (binding->keys) { + free_flat_list(binding->keys); } + free(binding->command); free(binding); } @@ -72,7 +71,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { return error; } - struct sway_binding *binding = malloc(sizeof(struct sway_binding)); + struct sway_binding *binding = calloc(1, sizeof(struct sway_binding)); if (!binding) { return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); @@ -122,7 +121,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { free_flat_list(split); return ret; } - xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); + xkb_keysym_t *key = calloc(1, sizeof(xkb_keysym_t)); if (!key) { free_sway_binding(binding); free_flat_list(split); @@ -165,7 +164,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { return error; } - struct sway_binding *binding = malloc(sizeof(struct sway_binding)); + struct sway_binding *binding = calloc(1, sizeof(struct sway_binding)); if (!binding) { return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); @@ -209,7 +208,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { list_free(split); return error; } - xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t)); + xkb_keycode_t *key = calloc(1, sizeof(xkb_keycode_t)); *key = keycode - 8; list_add(binding->keys, key); } diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 6272dcce..3090d32f 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -67,9 +67,7 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, list_t *keysym_bindings = config->current_mode->keysym_bindings; for (int i = 0; i < keysym_bindings->length; ++i) { struct sway_binding *binding = keysym_bindings->items[i]; - sway_log(L_DEBUG, "@@ checking binding: %s", binding->command); if (modifiers ^ binding->modifiers || n != binding->keys->length) { - sway_log(L_DEBUG, "@@ modifiers or key num dont match"); continue; } @@ -85,7 +83,8 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, } if (match) { - sway_log(L_DEBUG, "running command for binding: %s", binding->command); + sway_log(L_DEBUG, "running command for binding: %s", + binding->command); struct cmd_results *results = handle_command(binding->command); if (results->status != CMD_SUCCESS) { sway_log(L_DEBUG, "could not run command for binding: %s", @@ -291,7 +290,6 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { // handle keycodes handled = keyboard_execute_bindcode(keyboard); - sway_log(L_DEBUG, "@@ handled by bindcode? %d", handled); // handle translated keysyms const xkb_keysym_t *translated_keysyms; @@ -299,8 +297,8 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { size_t translated_keysyms_len = keyboard_keysyms_translated(keyboard, keycode, &translated_keysyms, &translated_modifiers); - pressed_keysyms_update(keyboard->pressed_keysyms_translated, translated_keysyms, - translated_keysyms_len, event->state); + pressed_keysyms_update(keyboard->pressed_keysyms_translated, + translated_keysyms, translated_keysyms_len, event->state); if (event->state == WLR_KEY_PRESSED && !handled) { handled = keyboard_execute_bindsym(keyboard, keyboard->pressed_keysyms_translated, translated_modifiers, @@ -310,9 +308,10 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { // Handle raw keysyms const xkb_keysym_t *raw_keysyms; uint32_t raw_modifiers; - size_t raw_keysyms_len = keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers); - pressed_keysyms_update(keyboard->pressed_keysyms_raw, raw_keysyms, raw_keysyms_len, - event->state); + size_t raw_keysyms_len = + keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers); + pressed_keysyms_update(keyboard->pressed_keysyms_raw, raw_keysyms, + raw_keysyms_len, event->state); if (event->state == WLR_KEY_PRESSED && !handled) { handled = keyboard_execute_bindsym(keyboard, keyboard->pressed_keysyms_raw, raw_modifiers, -- cgit v1.2.3 From ead3f1e676923b0457cef77b0b482e76cac50307 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 29 Dec 2017 19:04:16 +0100 Subject: Allow to configure outputs by their identifier --- include/sway/config.h | 2 ++ sway/commands/output.c | 46 ++++++++++++++++++--------------- sway/config/output.c | 8 ++++++ sway/tree/container.c | 70 ++++++++++++++++++++++++++++---------------------- 4 files changed, 75 insertions(+), 51 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 045359ca..eecdde3a 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -397,6 +397,8 @@ struct seat_attachment_config *seat_config_get_attachment( void apply_seat_config(struct seat_config *seat); int output_name_cmp(const void *item, const void *data); +void output_get_identifier(char *identifier, size_t len, + struct sway_output *output); struct output_config *new_output_config(const char *name); void merge_output_config(struct output_config *dst, struct output_config *src); void apply_output_config(struct output_config *oc, swayc_t *output); diff --git a/sway/commands/output.c b/sway/commands/output.c index 8cc74bcc..8c0fa63c 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -231,8 +231,8 @@ static struct cmd_results *cmd_output_background(struct output_config *output, } struct cmd_results *cmd_output(int argc, char **argv) { - struct cmd_results *error = NULL; - if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { + struct cmd_results *error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1); + if (error != NULL) { return error; } @@ -275,11 +275,11 @@ struct cmd_results *cmd_output(int argc, char **argv) { int i = list_seq_find(config->output_configs, output_name_cmp, output->name); if (i >= 0) { - // merge existing config - struct output_config *oc = config->output_configs->items[i]; - merge_output_config(oc, output); + // Merge existing config + struct output_config *current = config->output_configs->items[i]; + merge_output_config(current, output); free_output_config(output); - output = oc; + output = current; } else { list_add(config->output_configs, output); } @@ -290,22 +290,26 @@ struct cmd_results *cmd_output(int argc, char **argv) { output->refresh_rate, output->x, output->y, output->scale, output->transform, output->background, output->background_option); - if (output->name) { - // Try to find the output container and apply configuration now. If - // this is during startup then there will be no container and config - // will be applied during normal "new output" event from wlroots. - swayc_t *cont = NULL; - for (int i = 0; i < root_container.children->length; ++i) { - cont = root_container.children->items[i]; - if (cont->name && ((strcmp(cont->name, output->name) == 0) || - (strcmp(output->name, "*") == 0))) { - apply_output_config(output, cont); + // Try to find the output container and apply configuration now. If + // this is during startup then there will be no container and config + // will be applied during normal "new output" event from wlroots. + char identifier[128]; + bool all = strcmp(output->name, "*") == 0; + for (int i = 0; i < root_container.children->length; ++i) { + swayc_t *cont = root_container.children->items[i]; + if (cont->type != C_OUTPUT) { + continue; + } - if (strcmp(output->name, "*") != 0) { - // Stop looking if the output config isn't applicable to all - // outputs - break; - } + output_get_identifier(identifier, sizeof(identifier), cont->sway_output); + if (all || strcmp(cont->name, output->name) == 0 || + strcmp(identifier, output->name) == 0) { + apply_output_config(output, cont); + + if (!all) { + // Stop looking if the output config isn't applicable to all + // outputs + break; } } } diff --git a/sway/config/output.c b/sway/config/output.c index f336c949..e798a20e 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include #include #include #include @@ -14,6 +15,13 @@ int output_name_cmp(const void *item, const void *data) { return strcmp(output->name, name); } +void output_get_identifier(char *identifier, size_t len, + struct sway_output *output) { + struct wlr_output *wlr_output = output->wlr_output; + snprintf(identifier, len, "%s %s %s", wlr_output->make, wlr_output->model, + wlr_output->serial); +} + struct output_config *new_output_config(const char *name) { struct output_config *oc = calloc(1, sizeof(struct output_config)); if (oc == NULL) { diff --git a/sway/tree/container.c b/sway/tree/container.c index 6f2a3abf..c5574275 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -49,16 +49,49 @@ static swayc_t *new_swayc(enum swayc_types type) { return c; } +static void free_swayc(swayc_t *cont) { + if (!sway_assert(cont, "free_swayc passed NULL")) { + return; + } + + wl_signal_emit(&cont->events.destroy, cont); + + if (cont->children) { + // remove children until there are no more, free_swayc calls + // remove_child, which removes child from this container + while (cont->children->length) { + free_swayc(cont->children->items[0]); + } + list_free(cont->children); + } + if (cont->marks) { + list_foreach(cont->marks, free); + list_free(cont->marks); + } + if (cont->parent) { + remove_child(cont); + } + if (cont->name) { + free(cont->name); + } + free(cont); +} + swayc_t *new_output(struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, &size.height); + const char *name = sway_output->wlr_output->name; + char identifier[128]; + output_get_identifier(identifier, sizeof(identifier), sway_output); struct output_config *oc = NULL, *all = NULL; for (int i = 0; i < config->output_configs->length; ++i) { struct output_config *cur = config->output_configs->items[i]; - if (strcasecmp(name, cur->name) == 0) { + + if (strcasecmp(name, cur->name) == 0 || + strcasecmp(identifier, cur->name) == 0) { sway_log(L_DEBUG, "Matched output config for %s", name); oc = cur; } @@ -74,13 +107,18 @@ swayc_t *new_output(struct sway_output *sway_output) { if (!oc) { oc = all; } + if (oc && !oc->enabled) { return NULL; } swayc_t *output = new_swayc(C_OUTPUT); output->sway_output = sway_output; - output->name = name ? strdup(name) : NULL; + output->name = strdup(name); + if (output->name == NULL) { + free_swayc(output); + return NULL; + } apply_output_config(oc, output); @@ -140,34 +178,6 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { return swayc; } -static void free_swayc(swayc_t *cont) { - if (!sway_assert(cont, "free_swayc passed NULL")) { - return; - } - - wl_signal_emit(&cont->events.destroy, cont); - - if (cont->children) { - // remove children until there are no more, free_swayc calls - // remove_child, which removes child from this container - while (cont->children->length) { - free_swayc(cont->children->items[0]); - } - list_free(cont->children); - } - if (cont->marks) { - list_foreach(cont->marks, free); - list_free(cont->marks); - } - if (cont->parent) { - remove_child(cont); - } - if (cont->name) { - free(cont->name); - } - free(cont); -} - swayc_t *destroy_output(swayc_t *output) { if (!sway_assert(output, "null output passed to destroy_output")) { return NULL; -- cgit v1.2.3 From 50e791cadbfc51d531fff44cfbe3323a3b456adf Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 4 Jan 2018 07:25:52 -0500 Subject: binding release --- include/sway/input/keyboard.h | 3 ++ sway/commands/bind.c | 4 ++ sway/input/keyboard.c | 96 +++++++++++++++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 21 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h index 0a5857f0..8ec3eb35 100644 --- a/include/sway/input/keyboard.h +++ b/include/sway/input/keyboard.h @@ -14,7 +14,10 @@ struct sway_keyboard { struct wl_listener keyboard_modifiers; xkb_keysym_t pressed_keysyms_translated[SWAY_KEYBOARD_PRESSED_KEYSYMS_CAP]; + uint32_t modifiers_translated; + xkb_keysym_t pressed_keysyms_raw[SWAY_KEYBOARD_PRESSED_KEYSYMS_CAP]; + uint32_t modifiers_raw; }; struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, diff --git a/sway/commands/bind.c b/sway/commands/bind.c index ef9a4b7a..79121404 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -34,6 +34,10 @@ void free_sway_binding(struct sway_binding *binding) { */ bool binding_key_compare(struct sway_binding *binding_a, struct sway_binding *binding_b) { + if (binding_a->release != binding_b->release) { + return false; + } + if (binding_a->bindcode != binding_b->bindcode) { return false; } diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 3090d32f..7b90dacf 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -1,3 +1,4 @@ +#include #include #include #include "sway/input/seat.h" @@ -54,6 +55,18 @@ static bool keyboard_execute_compositor_binding(struct sway_keyboard *keyboard, return false; } +static bool binding_matches_keystate(struct sway_binding *binding, + enum wlr_key_state key_state) { + if (key_state == WLR_KEY_PRESSED && !binding->release) { + return true; + } + if (key_state == WLR_KEY_RELEASED && binding->release) { + return true; + } + + return false; +} + /** * Execute keyboard bindings bound with `bindysm`. * @@ -61,13 +74,15 @@ static bool keyboard_execute_compositor_binding(struct sway_keyboard *keyboard, * should be propagated to clients. */ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, - xkb_keysym_t *pressed_keysyms, uint32_t modifiers, size_t keysyms_len) { + xkb_keysym_t *pressed_keysyms, uint32_t modifiers, enum wlr_key_state key_state) { // configured bindings int n = pressed_keysyms_length(pressed_keysyms); list_t *keysym_bindings = config->current_mode->keysym_bindings; for (int i = 0; i < keysym_bindings->length; ++i) { struct sway_binding *binding = keysym_bindings->items[i]; - if (modifiers ^ binding->modifiers || n != binding->keys->length) { + if (!binding_matches_keystate(binding, key_state) || + modifiers ^ binding->modifiers || + n != binding->keys->length) { continue; } @@ -115,16 +130,43 @@ static bool keysym_is_modifier(xkb_keysym_t keysym) { } static bool binding_matches_keycodes(struct wlr_keyboard *keyboard, - struct sway_binding *binding) { + struct sway_binding *binding, struct wlr_event_keyboard_key *event) { + assert(binding->bindcode); + + uint32_t keycode = event->keycode + 8; + + if (!binding_matches_keystate(binding, event->state)) { + return false; + } + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard); if (modifiers ^ binding->modifiers) { return false; } + // on release, the released key must be in the binding + if (event->state == WLR_KEY_RELEASED) { + bool found = false; + for (int i = 0; i < binding->keys->length; ++i) { + uint32_t binding_keycode = *(uint32_t*)binding->keys->items[i] + 8; + if (binding_keycode == keycode) { + found = true; + break; + } + } + if (!found) { + return false; + } + } + // every keycode in the binding must be present in the pressed keys on the // keyboard for (int i = 0; i < binding->keys->length; ++i) { uint32_t binding_keycode = *(uint32_t*)binding->keys->items[i] + 8; + if (event->state == WLR_KEY_RELEASED && keycode == binding_keycode) { + continue; + } + bool found = false; for (size_t j = 0; j < keyboard->num_keycodes; ++j) { xkb_keycode_t keycode = keyboard->keycodes[j] + 8; @@ -178,13 +220,14 @@ static bool binding_matches_keycodes(struct wlr_keyboard *keyboard, * Returns true if the keysym was handled by a binding and false if the event * should be propagated to clients. */ -static bool keyboard_execute_bindcode(struct sway_keyboard *keyboard) { +static bool keyboard_execute_bindcode(struct sway_keyboard *keyboard, + struct wlr_event_keyboard_key *event) { struct wlr_keyboard *wlr_keyboard = keyboard->seat_device->input_device->wlr_device->keyboard; list_t *keycode_bindings = config->current_mode->keycode_bindings; for (int i = 0; i < keycode_bindings->length; ++i) { struct sway_binding *binding = keycode_bindings->items[i]; - if (binding_matches_keycodes(wlr_keyboard, binding)) { + if (binding_matches_keycodes(wlr_keyboard, binding, event)) { struct cmd_results *results = handle_command(binding->command); if (results->status != CMD_SUCCESS) { sway_log(L_DEBUG, "could not run command for binding: %s", @@ -289,50 +332,61 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { bool handled = false; // handle keycodes - handled = keyboard_execute_bindcode(keyboard); + handled = keyboard_execute_bindcode(keyboard, event); // handle translated keysyms + if (!handled && event->state == WLR_KEY_RELEASED) { + handled = keyboard_execute_bindsym(keyboard, + keyboard->pressed_keysyms_translated, + keyboard->modifiers_translated, + event->state); + } const xkb_keysym_t *translated_keysyms; - uint32_t translated_modifiers; size_t translated_keysyms_len = keyboard_keysyms_translated(keyboard, keycode, &translated_keysyms, - &translated_modifiers); + &keyboard->modifiers_translated); pressed_keysyms_update(keyboard->pressed_keysyms_translated, translated_keysyms, translated_keysyms_len, event->state); - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_bindsym(keyboard, - keyboard->pressed_keysyms_translated, translated_modifiers, - translated_keysyms_len); + keyboard->pressed_keysyms_translated, + keyboard->modifiers_translated, + event->state); } // Handle raw keysyms + if (!handled && event->state == WLR_KEY_RELEASED) { + handled = keyboard_execute_bindsym(keyboard, + keyboard->pressed_keysyms_raw, keyboard->modifiers_raw, + event->state); + } const xkb_keysym_t *raw_keysyms; - uint32_t raw_modifiers; size_t raw_keysyms_len = - keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers); + keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &keyboard->modifiers_raw); pressed_keysyms_update(keyboard->pressed_keysyms_raw, raw_keysyms, raw_keysyms_len, event->state); - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_bindsym(keyboard, - keyboard->pressed_keysyms_raw, raw_modifiers, - raw_keysyms_len); + keyboard->pressed_keysyms_raw, keyboard->modifiers_raw, + event->state); } // Compositor bindings - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_compositor_binding(keyboard, - keyboard->pressed_keysyms_translated, translated_modifiers, + keyboard->pressed_keysyms_translated, + keyboard->modifiers_translated, translated_keysyms_len); } - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_compositor_binding(keyboard, - keyboard->pressed_keysyms_raw, raw_modifiers, + keyboard->pressed_keysyms_raw, keyboard->modifiers_raw, raw_keysyms_len); } - if (!handled) { + if (!handled || event->state == WLR_KEY_RELEASED) { wlr_seat_set_keyboard(wlr_seat, wlr_device); wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec, event->keycode, event->state); -- cgit v1.2.3 From 28b8ea9f1ee57672f39e42521f6ed2aece0d3b2e Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Fri, 29 Dec 2017 11:28:10 +0100 Subject: cmd_input: cleanup around current_input_config - Restore old one if we weren't part of a block (should be NULL anyway) - Check current_input_config got properly allocated - free temporary current_input_config when done using it --- sway/commands/input.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'sway/commands') diff --git a/sway/commands/input.c b/sway/commands/input.c index ccb1d276..edf45e4c 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -24,7 +24,11 @@ struct cmd_results *cmd_input(int argc, char **argv) { char **argv_new = argv+2; struct cmd_results *res; + struct input_config *old_input_config = current_input_config; current_input_config = new_input_config(argv[0]); + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + } if (strcasecmp("accel_profile", argv[1]) == 0) { res = input_cmd_accel_profile(argc_new, argv_new); } else if (strcasecmp("click_method", argv[1]) == 0) { @@ -60,6 +64,7 @@ struct cmd_results *cmd_input(int argc, char **argv) { } else { res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); } - current_input_config = NULL; + free_input_config(current_input_config); + current_input_config = old_input_config; return res; } -- cgit v1.2.3 From c83900593daace2ef85174163edf2748179e28f2 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Fri, 29 Dec 2017 15:31:04 +0100 Subject: config: add 'set' command --- include/sway/config.h | 1 + sway/commands.c | 3 ++- sway/commands/set.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ sway/config.c | 7 ++++- sway/meson.build | 1 + 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 sway/commands/set.c (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 405092e3..0a9c4595 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -375,6 +375,7 @@ bool read_config(FILE *file, struct sway_config *config); * Free config struct */ void free_config(struct sway_config *config); +void free_sway_variable(struct sway_variable *var); /** * Does variable replacement for a string based on the config's currently loaded variables. */ diff --git a/sway/commands.c b/sway/commands.c index c1c6dc5d..f01329db 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -138,6 +138,7 @@ static struct cmd_handler handlers[] = { { "input", cmd_input }, { "output", cmd_output }, { "seat", cmd_seat }, + { "set", cmd_set }, }; static int handler_compare(const void *_a, const void *_b) { @@ -290,7 +291,7 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) { int i; // Var replacement, for all but first argument of set // TODO commands - for (i = /*handler->handle == cmd_set ? 2 :*/ 1; i < argc; ++i) { + for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) { argv[i] = do_var_replacement(argv[i]); unescape_string(argv[i]); } diff --git a/sway/commands/set.c b/sway/commands/set.c new file mode 100644 index 00000000..dcd928ba --- /dev/null +++ b/sway/commands/set.c @@ -0,0 +1,71 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +// sort in order of longest->shortest +static int compare_set_qsort(const void *_l, const void *_r) { + struct sway_variable const *l = *(void **)_l; + struct sway_variable const *r = *(void **)_r; + return strlen(r->name) - strlen(l->name); +} + +void free_sway_variable(struct sway_variable *var) { + if (!var) { + return; + } + free(var->name); + free(var->value); + free(var); +} + +struct cmd_results *cmd_set(int argc, char **argv) { + char *tmp; + struct cmd_results *error = NULL; + if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file."); + if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) { + return error; + } + + if (argv[0][0] != '$') { + sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]); + + size_t size = snprintf(NULL, 0, "$%s", argv[0]); + tmp = malloc(size + 1); + if (!tmp) { + return cmd_results_new(CMD_FAILURE, "set", "Not possible to create variable $'%s'", argv[0]); + } + snprintf(tmp, size+1, "$%s", argv[0]); + + argv[0] = tmp; + } + + struct sway_variable *var = NULL; + // Find old variable if it exists + int i; + for (i = 0; i < config->symbols->length; ++i) { + var = config->symbols->items[i]; + if (strcmp(var->name, argv[0]) == 0) { + break; + } + var = NULL; + } + if (var) { + free(var->value); + } else { + var = malloc(sizeof(struct sway_variable)); + if (!var) { + return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable"); + } + var->name = strdup(argv[0]); + list_add(config->symbols, var); + list_qsort(config->symbols, compare_set_qsort); + } + var->value = join_args(argv + 1, argc - 1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 627ed94f..e0a93e19 100644 --- a/sway/config.c +++ b/sway/config.c @@ -61,7 +61,12 @@ void free_config(struct sway_config *config) { } // TODO: handle all currently unhandled lists as we add implementations - list_free(config->symbols); + if (config->symbols) { + for (i = 0; i < config->symbols->length; i++) { + free_sway_variable(config->symbols->items[i]); + } + list_free(config->symbols); + } if (config->modes) { for (i = 0; i < config->modes->length; i++) { free_mode(config->modes->items[i]); diff --git a/sway/meson.build b/sway/meson.build index 01d5ef36..30ec166b 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -15,6 +15,7 @@ sway_sources = files( 'commands/seat.c', 'commands/seat/attach.c', 'commands/seat/fallback.c', + 'commands/set.c', 'commands/input/accel_profile.c', 'commands/input/click_method.c', 'commands/input/drag_lock.c', -- cgit v1.2.3 From 67985e903188a464e602d04f9ed218bd397f5ab1 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Fri, 5 Jan 2018 22:32:51 +0100 Subject: sway: change all sway_log to wlr_log --- common/ipc-client.c | 2 +- common/readline.c | 4 +-- common/util.c | 2 +- include/log.h | 25 +-------------- sway/commands.c | 12 +++---- sway/commands/bind.c | 8 ++--- sway/commands/exec.c | 2 +- sway/commands/exec_always.c | 8 ++--- sway/commands/input.c | 2 +- sway/commands/input/click_method.c | 2 +- sway/commands/input/events.c | 2 +- sway/commands/input/tap.c | 4 +-- sway/commands/input/xkb_layout.c | 4 +-- sway/commands/input/xkb_model.c | 4 +-- sway/commands/input/xkb_options.c | 4 +-- sway/commands/input/xkb_rules.c | 4 +-- sway/commands/input/xkb_variant.c | 4 +-- sway/commands/output.c | 8 ++--- sway/commands/seat.c | 2 +- sway/commands/set.c | 2 +- sway/config.c | 66 +++++++++++++++++++------------------- sway/config/input.c | 6 ++-- sway/config/output.c | 16 ++++----- sway/config/seat.c | 6 ++-- sway/desktop/output.c | 4 +-- sway/desktop/wl_shell.c | 2 +- sway/desktop/xdg_shell_v6.c | 2 +- sway/desktop/xwayland.c | 2 +- sway/input/cursor.c | 12 +++---- sway/input/input-manager.c | 36 ++++++++++----------- sway/input/keyboard.c | 6 ++-- sway/input/seat.c | 8 ++--- sway/ipc-server.c | 50 ++++++++++++++--------------- sway/tree/container.c | 14 ++++---- sway/tree/layout.c | 20 ++++++------ sway/tree/workspace.c | 2 +- 36 files changed, 167 insertions(+), 190 deletions(-) (limited to 'sway/commands') diff --git a/common/ipc-client.c b/common/ipc-client.c index 1ab6627b..582c5e86 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -79,7 +79,7 @@ struct ipc_response *ipc_recv_response(int socketfd) { error_2: free(response); error_1: - sway_log(L_ERROR, "Unable to allocate memory for IPC response"); + wlr_log(L_ERROR, "Unable to allocate memory for IPC response"); return NULL; } diff --git a/common/readline.c b/common/readline.c index cc40a2cc..ed5801de 100644 --- a/common/readline.c +++ b/common/readline.c @@ -8,7 +8,7 @@ char *read_line(FILE *file) { char *string = malloc(size); char lastChar = '\0'; if (!string) { - sway_log(L_ERROR, "Unable to allocate memory for read_line"); + wlr_log(L_ERROR, "Unable to allocate memory for read_line"); return NULL; } while (1) { @@ -29,7 +29,7 @@ char *read_line(FILE *file) { char *new_string = realloc(string, size *= 2); if (!new_string) { free(string); - sway_log(L_ERROR, "Unable to allocate memory for read_line"); + wlr_log(L_ERROR, "Unable to allocate memory for read_line"); return NULL; } string = new_string; diff --git a/common/util.c b/common/util.c index 83981160..fb7f9454 100644 --- a/common/util.c +++ b/common/util.c @@ -113,7 +113,7 @@ uint32_t parse_color(const char *color) { int len = strlen(color); if (len != 6 && len != 8) { - sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); + wlr_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); return 0xFFFFFFFF; } uint32_t res = (uint32_t)strtoul(color, NULL, 16); diff --git a/include/log.h b/include/log.h index a1e33fa2..646776f5 100644 --- a/include/log.h +++ b/include/log.h @@ -1,22 +1,7 @@ #ifndef _SWAY_LOG_H #define _SWAY_LOG_H #include - -typedef enum { - L_SILENT = 0, - L_ERROR = 1, - L_INFO = 2, - L_DEBUG = 3, -} log_importance_t; - -void init_log(log_importance_t verbosity); -void set_log_level(log_importance_t verbosity); -log_importance_t get_log_level(void); -void reset_log_level(void); -// returns whether debug logging is on after switching. -bool toggle_debug_logging(void); -void sway_log_colors(int mode); -void sway_log_errno(log_importance_t verbosity, char* format, ...) __attribute__((format(printf,2,3))); +#include void _sway_abort(const char *filename, int line, const char* format, ...) __attribute__((format(printf,3,4))); #define sway_abort(FMT, ...) \ @@ -26,14 +11,6 @@ bool _sway_assert(bool condition, const char *filename, int line, const char* fo #define sway_assert(COND, FMT, ...) \ _sway_assert(COND, __FILE__, __LINE__, "%s:" FMT, __PRETTY_FUNCTION__, ##__VA_ARGS__) -void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,4,5))); - -#define sway_log(VERBOSITY, FMT, ...) \ - _sway_log(__FILE__, __LINE__, VERBOSITY, FMT, ##__VA_ARGS__) - -#define sway_vlog(VERBOSITY, FMT, VA_ARGS) \ - _sway_vlog(__FILE__, __LINE__, VERBOSITY, FMT, VA_ARGS) - void error_handler(int sig); #endif diff --git a/sway/commands.c b/sway/commands.c index f01329db..1005cf68 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -176,7 +176,7 @@ static struct cmd_handler seat_handlers[] = { static struct cmd_handler *find_handler(char *line, enum cmd_status block) { struct cmd_handler d = { .command=line }; struct cmd_handler *res = NULL; - sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_SEAT); + wlr_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_SEAT); if (block == CMD_BLOCK_INPUT) { res = bsearch(&d, input_handlers, @@ -215,10 +215,10 @@ struct cmd_results *handle_command(char *_exec) { cmd = argsep(&cmdlist, ","); cmd += strspn(cmd, whitespace); if (strcmp(cmd, "") == 0) { - sway_log(L_INFO, "Ignoring empty command."); + wlr_log(L_INFO, "Ignoring empty command."); continue; } - sway_log(L_INFO, "Handling command '%s'", cmd); + wlr_log(L_INFO, "Handling command '%s'", cmd); //TODO better handling of argv int argc; char **argv = split_args(cmd, &argc); @@ -276,7 +276,7 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) { goto cleanup; } - sway_log(L_INFO, "handling config command '%s'", exec); + wlr_log(L_INFO, "handling config command '%s'", exec); // Endblock if (**argv == '}') { results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); @@ -380,7 +380,7 @@ struct cmd_results *config_commands_command(char *exec) { } policy->context = context; - sway_log(L_INFO, "Set command policy for %s to %d", + wlr_log(L_INFO, "Set command policy for %s to %d", policy->command, policy->context); results = cmd_results_new(CMD_SUCCESS, NULL, NULL); @@ -394,7 +394,7 @@ struct cmd_results *cmd_results_new(enum cmd_status status, const char *input, const char *format, ...) { struct cmd_results *results = malloc(sizeof(struct cmd_results)); if (!results) { - sway_log(L_ERROR, "Unable to allocate command results"); + wlr_log(L_ERROR, "Unable to allocate command results"); return NULL; } results->status = status; diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 79121404..cbabb07b 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -145,7 +145,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { for (int i = 0; i < mode_bindings->length; ++i) { struct sway_binding *config_binding = mode_bindings->items[i]; if (binding_key_compare(binding, config_binding)) { - sway_log(L_DEBUG, "overwriting old binding with command '%s'", + wlr_log(L_DEBUG, "overwriting old binding with command '%s'", config_binding->command); free_sway_binding(config_binding); mode_bindings->items[i] = binding; @@ -157,7 +157,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { list_add(mode_bindings, binding); } - sway_log(L_DEBUG, "bindsym - Bound %s to command %s", + wlr_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } @@ -227,7 +227,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { for (int i = 0; i < mode_bindings->length; ++i) { struct sway_binding *config_binding = mode_bindings->items[i]; if (binding_key_compare(binding, config_binding)) { - sway_log(L_DEBUG, "overwriting old binding with command '%s'", + wlr_log(L_DEBUG, "overwriting old binding with command '%s'", config_binding->command); free_sway_binding(config_binding); mode_bindings->items[i] = binding; @@ -239,7 +239,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { list_add(mode_bindings, binding); } - sway_log(L_DEBUG, "bindcode - Bound %s to command %s", + wlr_log(L_DEBUG, "bindcode - Bound %s to command %s", argv[0], binding->command); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/exec.c b/sway/commands/exec.c index fbbc4941..363d5bef 100644 --- a/sway/commands/exec.c +++ b/sway/commands/exec.c @@ -8,7 +8,7 @@ struct cmd_results *cmd_exec(int argc, char **argv) { if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); if (config->reloading) { char *args = join_args(argv, argc); - sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args); + wlr_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args); free(args); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index 9527a487..61870c51 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -20,7 +20,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) { char *tmp = NULL; if (strcmp((char*)*argv, "--no-startup-id") == 0) { - sway_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored."); + wlr_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored."); if ((error = checkarg(argc - 1, "exec_always", EXPECTED_MORE_THAN, 0))) { return error; } @@ -35,11 +35,11 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) { strncpy(cmd, tmp, sizeof(cmd)); cmd[sizeof(cmd) - 1] = 0; free(tmp); - sway_log(L_DEBUG, "Executing %s", cmd); + wlr_log(L_DEBUG, "Executing %s", cmd); int fd[2]; if (pipe(fd) != 0) { - sway_log(L_ERROR, "Unable to create pipe for fork"); + wlr_log(L_ERROR, "Unable to create pipe for fork"); } pid_t pid; @@ -75,7 +75,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) { // cleanup child process wait(0); if (*child > 0) { - sway_log(L_DEBUG, "Child process created with pid %d", *child); + wlr_log(L_DEBUG, "Child process created with pid %d", *child); // TODO: add PID to active workspace } else { free(child); diff --git a/sway/commands/input.c b/sway/commands/input.c index edf45e4c..5ea39f62 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -12,7 +12,7 @@ struct cmd_results *cmd_input(int argc, char **argv) { if (config->reading && strcmp("{", argv[1]) == 0) { current_input_config = new_input_config(argv[0]); - sway_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); } diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c index dcf64c1a..22eb15f7 100644 --- a/sway/commands/input/click_method.c +++ b/sway/commands/input/click_method.c @@ -6,7 +6,7 @@ #include "log.h" struct cmd_results *input_cmd_click_method(int argc, char **argv) { - sway_log(L_DEBUG, "click_method for device: %d %s", + wlr_log(L_DEBUG, "click_method for device: %d %s", current_input_config==NULL, current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c index 8a74c11e..a1bfbacd 100644 --- a/sway/commands/input/events.c +++ b/sway/commands/input/events.c @@ -6,7 +6,7 @@ #include "log.h" struct cmd_results *input_cmd_events(int argc, char **argv) { - sway_log(L_DEBUG, "events for device: %s", + wlr_log(L_DEBUG, "events for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c index 8547c0cd..ecab9a5b 100644 --- a/sway/commands/input/tap.c +++ b/sway/commands/input/tap.c @@ -6,7 +6,7 @@ #include "log.h" struct cmd_results *input_cmd_tap(int argc, char **argv) { - sway_log(L_DEBUG, "tap for device: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "tap for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) { return error; @@ -26,7 +26,7 @@ struct cmd_results *input_cmd_tap(int argc, char **argv) { "Expected 'tap '"); } - sway_log(L_DEBUG, "apply-tap for device: %s", + wlr_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/input/xkb_layout.c b/sway/commands/input/xkb_layout.c index a25d3850..25db1a33 100644 --- a/sway/commands/input/xkb_layout.c +++ b/sway/commands/input/xkb_layout.c @@ -5,7 +5,7 @@ #include "log.h" struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { - sway_log(L_DEBUG, "xkb layout for device: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "xkb layout for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_layout", EXPECTED_EQUAL_TO, 1))) { return error; @@ -18,7 +18,7 @@ struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { new_config->xkb_layout = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_layout for device: %s layout: %s", + wlr_log(L_DEBUG, "apply-xkb_layout for device: %s layout: %s", current_input_config->identifier, new_config->xkb_layout); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/input/xkb_model.c b/sway/commands/input/xkb_model.c index 9729e869..819b796b 100644 --- a/sway/commands/input/xkb_model.c +++ b/sway/commands/input/xkb_model.c @@ -5,7 +5,7 @@ #include "log.h" struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { - sway_log(L_DEBUG, "xkb model for device: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "xkb model for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_model", EXPECTED_EQUAL_TO, 1))) { return error; @@ -18,7 +18,7 @@ struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { new_config->xkb_model = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_model for device: %s model: %s", + wlr_log(L_DEBUG, "apply-xkb_model for device: %s model: %s", current_input_config->identifier, new_config->xkb_model); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/input/xkb_options.c b/sway/commands/input/xkb_options.c index 504849cc..ff5f83ec 100644 --- a/sway/commands/input/xkb_options.c +++ b/sway/commands/input/xkb_options.c @@ -5,7 +5,7 @@ #include "log.h" struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { - sway_log(L_DEBUG, "xkb options for device: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "xkb options for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_options", EXPECTED_EQUAL_TO, 1))) { return error; @@ -18,7 +18,7 @@ struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { new_config->xkb_options = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_options for device: %s options: %s", + wlr_log(L_DEBUG, "apply-xkb_options for device: %s options: %s", current_input_config->identifier, new_config->xkb_options); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/input/xkb_rules.c b/sway/commands/input/xkb_rules.c index db7d8abe..aafe0003 100644 --- a/sway/commands/input/xkb_rules.c +++ b/sway/commands/input/xkb_rules.c @@ -5,7 +5,7 @@ #include "log.h" struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { - sway_log(L_DEBUG, "xkb rules for device: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "xkb rules for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_rules", EXPECTED_EQUAL_TO, 1))) { return error; @@ -18,7 +18,7 @@ struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { new_config->xkb_rules = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_rules for device: %s rules: %s", + wlr_log(L_DEBUG, "apply-xkb_rules for device: %s rules: %s", current_input_config->identifier, new_config->xkb_rules); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/input/xkb_variant.c b/sway/commands/input/xkb_variant.c index 855e6abc..89a61fdc 100644 --- a/sway/commands/input/xkb_variant.c +++ b/sway/commands/input/xkb_variant.c @@ -5,7 +5,7 @@ #include "log.h" struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { - sway_log(L_DEBUG, "xkb variant for device: %s", current_input_config->identifier); + wlr_log(L_DEBUG, "xkb variant for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_variant", EXPECTED_EQUAL_TO, 1))) { return error; @@ -18,7 +18,7 @@ struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { new_config->xkb_variant = strdup(argv[0]); - sway_log(L_DEBUG, "apply-xkb_variant for device: %s variant: %s", + wlr_log(L_DEBUG, "apply-xkb_variant for device: %s variant: %s", current_input_config->identifier, new_config->xkb_variant); apply_input_config(new_config); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/output.c b/sway/commands/output.c index 8c0fa63c..e747eb4e 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -203,12 +203,12 @@ static struct cmd_results *cmd_output_background(struct output_config *output, if (src) { sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); } else { - sway_log(L_ERROR, + wlr_log(L_ERROR, "Unable to allocate background source"); } free(conf); } else { - sway_log(L_ERROR, "Unable to allocate background source"); + wlr_log(L_ERROR, "Unable to allocate background source"); } } if (!src || access(src, F_OK) == -1) { @@ -238,7 +238,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { struct output_config *output = new_output_config(argv[0]); if (!output) { - sway_log(L_ERROR, "Failed to allocate output config"); + wlr_log(L_ERROR, "Failed to allocate output config"); return NULL; } @@ -284,7 +284,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { list_add(config->output_configs, output); } - sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " + wlr_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " "position %d,%d scale %f transform %d) (bg %s %s)", output->name, output->enabled, output->width, output->height, output->refresh_rate, output->x, output->y, output->scale, diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 155bc510..6284002b 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -12,7 +12,7 @@ struct cmd_results *cmd_seat(int argc, char **argv) { if (config->reading && strcmp("{", argv[1]) == 0) { current_seat_config = new_seat_config(argv[0]); - sway_log(L_DEBUG, "entering seat block: %s", current_seat_config->name); + wlr_log(L_DEBUG, "entering seat block: %s", current_seat_config->name); return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); } diff --git a/sway/commands/set.c b/sway/commands/set.c index dcd928ba..856c73e7 100644 --- a/sway/commands/set.c +++ b/sway/commands/set.c @@ -33,7 +33,7 @@ struct cmd_results *cmd_set(int argc, char **argv) { } if (argv[0][0] != '$') { - sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]); + wlr_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]); size_t size = snprintf(NULL, 0, "$%s", argv[0]); tmp = malloc(size + 1); diff --git a/sway/config.c b/sway/config.c index e0a93e19..5ec45b17 100644 --- a/sway/config.c +++ b/sway/config.c @@ -232,12 +232,12 @@ static char *get_config_path(void) { 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"); + wlr_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); + wlr_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); free(config_home); } } @@ -263,7 +263,7 @@ static char *get_config_path(void) { 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); + wlr_log(L_INFO, "Loading config from %s", path); current_config_path = path; struct stat sb; @@ -272,13 +272,13 @@ static bool load_config(const char *path, struct sway_config *config) { } if (path == NULL) { - sway_log(L_ERROR, "Unable to find a config file!"); + wlr_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); + wlr_log(L_ERROR, "Unable to open %s for reading", path); return false; } @@ -286,7 +286,7 @@ static bool load_config(const char *path, struct sway_config *config) { fclose(f); if (!config_load_success) { - sway_log(L_ERROR, "Error(s) loading config!"); + wlr_log(L_ERROR, "Error(s) loading config!"); } current_config_path = NULL; @@ -313,7 +313,7 @@ bool load_main_config(const char *file, bool is_active) { config_defaults(config); if (is_active) { - sway_log(L_DEBUG, "Performing configuration file reload"); + wlr_log(L_DEBUG, "Performing configuration file reload"); config->reloading = true; config->active = true; } @@ -327,7 +327,7 @@ bool load_main_config(const char *file, bool is_active) { bool success = true; DIR *dir = opendir(SYSCONFDIR "/sway/security.d"); if (!dir) { - sway_log(L_ERROR, + wlr_log(L_ERROR, "%s does not exist, sway will have no security configuration" " and will probably be broken", SYSCONFDIR "/sway/security.d"); } else { @@ -356,7 +356,7 @@ bool load_main_config(const char *file, bool is_active) { 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, + wlr_log(L_ERROR, "Refusing to load %s - it must be owned by root " "and mode 644 or 444", _path); success = false; @@ -398,7 +398,7 @@ static bool load_include_config(const char *path, const char *parent_dir, len = len + strlen(parent_dir) + 2; full_path = malloc(len * sizeof(char)); if (!full_path) { - sway_log(L_ERROR, + wlr_log(L_ERROR, "Unable to allocate full path to included config"); return false; } @@ -409,7 +409,7 @@ static bool load_include_config(const char *path, const char *parent_dir, free(full_path); if (real_path == NULL) { - sway_log(L_DEBUG, "%s not found.", path); + wlr_log(L_DEBUG, "%s not found.", path); return false; } @@ -418,7 +418,7 @@ static bool load_include_config(const char *path, const char *parent_dir, 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, + wlr_log(L_DEBUG, "%s already included once, won't be included again.", real_path); free(real_path); @@ -472,7 +472,7 @@ bool load_include_configs(const char *path, struct sway_config *config) { // restore wd if (chdir(wd) < 0) { free(wd); - sway_log(L_ERROR, "failed to restore working directory"); + wlr_log(L_ERROR, "failed to restore working directory"); return false; } @@ -508,13 +508,13 @@ bool read_config(FILE *file, struct sway_config *config) { switch(res->status) { case CMD_FAILURE: case CMD_INVALID: - sway_log(L_ERROR, "Error on line %i '%s': %s (%s)", line_number, + wlr_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, "Deferring command `%s'", line); + wlr_log(L_DEBUG, "Deferring command `%s'", line); list_add(config->cmd_queue, strdup(line)); break; @@ -522,7 +522,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_END) { block = CMD_BLOCK_MODE; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -530,7 +530,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_END) { block = CMD_BLOCK_INPUT; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -538,7 +538,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_END) { block = CMD_BLOCK_SEAT; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -546,7 +546,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_END) { block = CMD_BLOCK_BAR; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -554,7 +554,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_BAR) { block = CMD_BLOCK_BAR_COLORS; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -562,7 +562,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_END) { block = CMD_BLOCK_COMMANDS; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -570,7 +570,7 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_END) { block = CMD_BLOCK_IPC; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_log(L_ERROR, "Invalid block '%s'", line); } break; @@ -578,59 +578,59 @@ bool read_config(FILE *file, struct sway_config *config) { if (block == CMD_BLOCK_IPC) { block = CMD_BLOCK_IPC_EVENTS; } else { - sway_log(L_ERROR, "Invalid block '%s'", line); + wlr_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"); + wlr_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"); + wlr_log(L_DEBUG, "End of input block"); free_input_config(current_input_config); current_input_config = NULL; block = CMD_BLOCK_END; break; case CMD_BLOCK_SEAT: - sway_log(L_DEBUG, "End of seat block"); + wlr_log(L_DEBUG, "End of seat block"); current_seat_config = NULL; block = CMD_BLOCK_END; break; case CMD_BLOCK_BAR: - sway_log(L_DEBUG, "End of bar block"); + wlr_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"); + wlr_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"); + wlr_log(L_DEBUG, "End of commands block"); block = CMD_BLOCK_END; break; case CMD_BLOCK_IPC: - sway_log(L_DEBUG, "End of IPC block"); + wlr_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"); + wlr_log(L_DEBUG, "End of IPC events block"); block = CMD_BLOCK_IPC; break; case CMD_BLOCK_END: - sway_log(L_ERROR, "Unmatched }"); + wlr_log(L_ERROR, "Unmatched }"); break; default:; @@ -663,7 +663,7 @@ char *do_var_replacement(char *str) { int vvlen = strlen(var->value); char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); if (!newstr) { - sway_log(L_ERROR, + wlr_log(L_ERROR, "Unable to allocate replacement " "during variable expansion"); break; diff --git a/sway/config/input.c b/sway/config/input.c index 6f8d31f7..96181302 100644 --- a/sway/config/input.c +++ b/sway/config/input.c @@ -8,13 +8,13 @@ struct input_config *new_input_config(const char* identifier) { struct input_config *input = calloc(1, sizeof(struct input_config)); if (!input) { - sway_log(L_DEBUG, "Unable to allocate input config"); + wlr_log(L_DEBUG, "Unable to allocate input config"); return NULL; } - sway_log(L_DEBUG, "new_input_config(%s)", identifier); + wlr_log(L_DEBUG, "new_input_config(%s)", identifier); if (!(input->identifier = strdup(identifier))) { free(input); - sway_log(L_DEBUG, "Unable to allocate input config"); + wlr_log(L_DEBUG, "Unable to allocate input config"); return NULL; } diff --git a/sway/config/output.c b/sway/config/output.c index e798a20e..69e883f1 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -84,7 +84,7 @@ static void set_mode(struct wlr_output *output, int width, int height, float refresh_rate) { int mhz = (int)(refresh_rate * 1000); if (wl_list_empty(&output->modes)) { - sway_log(L_DEBUG, "Assigning custom mode to %s", output->name); + wlr_log(L_DEBUG, "Assigning custom mode to %s", output->name); wlr_output_set_custom_mode(output, width, height, mhz); return; } @@ -100,9 +100,9 @@ static void set_mode(struct wlr_output *output, int width, int height, } } if (!best) { - sway_log(L_ERROR, "Configured mode for %s not available", output->name); + wlr_log(L_ERROR, "Configured mode for %s not available", output->name); } else { - sway_log(L_DEBUG, "Assigning configured mode to %s", output->name); + wlr_log(L_DEBUG, "Assigning configured mode to %s", output->name); wlr_output_set_mode(output, best); } } @@ -119,22 +119,22 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } if (oc && oc->width > 0 && oc->height > 0) { - sway_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width, + wlr_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width, oc->height, oc->refresh_rate); set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); } if (oc && oc->scale > 0) { - sway_log(L_DEBUG, "Set %s scale to %f", oc->name, oc->scale); + wlr_log(L_DEBUG, "Set %s scale to %f", oc->name, oc->scale); wlr_output_set_scale(wlr_output, oc->scale); } if (oc && oc->transform >= 0) { - sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform); + wlr_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform); wlr_output_set_transform(wlr_output, oc->transform); } // 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); + wlr_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y); wlr_output_layout_add(root_container.sway_root->output_layout, wlr_output, oc->x, oc->y); } else { @@ -165,7 +165,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { terminate_swaybg(output->bg_pid); } - sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); + wlr_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); size_t bufsize = 12; char output_id[bufsize]; diff --git a/sway/config/seat.c b/sway/config/seat.c index 113139e8..03cc6d4e 100644 --- a/sway/config/seat.c +++ b/sway/config/seat.c @@ -7,11 +7,11 @@ struct seat_config *new_seat_config(const char* name) { struct seat_config *seat = calloc(1, sizeof(struct seat_config)); if (!seat) { - sway_log(L_DEBUG, "Unable to allocate seat config"); + wlr_log(L_DEBUG, "Unable to allocate seat config"); return NULL; } - sway_log(L_DEBUG, "new_seat_config(%s)", name); + wlr_log(L_DEBUG, "new_seat_config(%s)", name); seat->name = strdup(name); if (!sway_assert(seat->name, "could not allocate name for seat")) { free(seat); @@ -34,7 +34,7 @@ struct seat_attachment_config *seat_attachment_config_new() { struct seat_attachment_config *attachment = calloc(1, sizeof(struct seat_attachment_config)); if (!attachment) { - sway_log(L_DEBUG, "cannot allocate attachment config"); + wlr_log(L_DEBUG, "cannot allocate attachment config"); return NULL; } return attachment; diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 3b87c2e7..2b428c30 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -110,7 +110,7 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { void output_add_notify(struct wl_listener *listener, void *data) { struct sway_server *server = wl_container_of(listener, server, output_add); struct wlr_output *wlr_output = data; - sway_log(L_DEBUG, "New output %p: %s", wlr_output, wlr_output->name); + wlr_log(L_DEBUG, "New output %p: %s", wlr_output, wlr_output->name); struct sway_output *output = calloc(1, sizeof(struct sway_output)); if (!output) { @@ -140,7 +140,7 @@ void output_add_notify(struct wl_listener *listener, void *data) { void output_remove_notify(struct wl_listener *listener, void *data) { struct sway_server *server = wl_container_of(listener, server, output_remove); struct wlr_output *wlr_output = data; - sway_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); + wlr_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); swayc_t *output_container = NULL; for (int i = 0 ; i < root_container.children->length; ++i) { diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index a7bb8eb5..345a1398 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -82,7 +82,7 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { return; } - sway_log(L_DEBUG, "New wl_shell toplevel title='%s' app_id='%s'", + wlr_log(L_DEBUG, "New wl_shell toplevel title='%s' app_id='%s'", shell_surface->title, shell_surface->class); wlr_wl_shell_surface_ping(shell_surface); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 5ff19f7e..df48345c 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -88,7 +88,7 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { return; } - sway_log(L_DEBUG, "New xdg_shell_v6 toplevel title='%s' app_id='%s'", + wlr_log(L_DEBUG, "New xdg_shell_v6 toplevel title='%s' app_id='%s'", xdg_surface->title, xdg_surface->app_id); wlr_xdg_surface_v6_ping(xdg_surface); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 42e82c64..43bb2e00 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -124,7 +124,7 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { return; } - sway_log(L_DEBUG, "New xwayland surface title='%s' class='%s'", + wlr_log(L_DEBUG, "New xwayland surface title='%s' class='%s'", xsurface->title, xsurface->class); struct sway_xwayland_surface *sway_surface = diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 3b5cfce5..c51b59f9 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -85,35 +85,35 @@ static void handle_touch_down(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_down); struct wlr_event_touch_down *event = data; - sway_log(L_DEBUG, "TODO: handle touch down event: %p", event); + wlr_log(L_DEBUG, "TODO: handle touch down event: %p", event); } static void handle_touch_up(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_up); struct wlr_event_touch_up *event = data; - sway_log(L_DEBUG, "TODO: handle touch up event: %p", event); + wlr_log(L_DEBUG, "TODO: handle touch up event: %p", event); } static void handle_touch_motion(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_motion); struct wlr_event_touch_motion *event = data; - sway_log(L_DEBUG, "TODO: handle touch motion event: %p", event); + wlr_log(L_DEBUG, "TODO: handle touch motion event: %p", event); } static void handle_tool_axis(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_axis); struct wlr_event_tablet_tool_axis *event = data; - sway_log(L_DEBUG, "TODO: handle tool axis event: %p", event); + wlr_log(L_DEBUG, "TODO: handle tool axis event: %p", event); } static void handle_tool_tip(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_tip); struct wlr_event_tablet_tool_tip *event = data; - sway_log(L_DEBUG, "TODO: handle tool tip event: %p", event); + wlr_log(L_DEBUG, "TODO: handle tool tip event: %p", event); } static void handle_request_set_cursor(struct wl_listener *listener, @@ -121,7 +121,7 @@ static void handle_request_set_cursor(struct wl_listener *listener, struct sway_cursor *cursor = wl_container_of(listener, cursor, request_set_cursor); struct wlr_seat_pointer_request_set_cursor_event *event = data; - sway_log(L_DEBUG, "TODO: handle request set cursor event: %p", event); + wlr_log(L_DEBUG, "TODO: handle request set cursor event: %p", event); } struct sway_cursor *sway_cursor_create(struct sway_seat *seat) { diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 128a818a..26cf5035 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -52,7 +52,7 @@ static char *get_device_identifier(struct wlr_input_device *device) { int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1; char *identifier = malloc(len); if (!identifier) { - sway_log(L_ERROR, "Unable to allocate unique input device name"); + wlr_log(L_ERROR, "Unable to allocate unique input device name"); return NULL; } @@ -93,60 +93,60 @@ static void sway_input_manager_libinput_config_pointer(struct sway_input_device } libinput_device = wlr_libinput_get_device_handle(wlr_device); - sway_log(L_DEBUG, "sway_input_manager_libinput_config_pointer(%s)", ic->identifier); + wlr_log(L_DEBUG, "sway_input_manager_libinput_config_pointer(%s)", ic->identifier); if (ic->accel_profile != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) accel_set_profile(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) accel_set_profile(%d)", ic->identifier, ic->accel_profile); libinput_device_config_accel_set_profile(libinput_device, ic->accel_profile); } if (ic->click_method != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) click_set_method(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) click_set_method(%d)", ic->identifier, ic->click_method); libinput_device_config_click_set_method(libinput_device, ic->click_method); } if (ic->drag_lock != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) tap_set_drag_lock_enabled(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) tap_set_drag_lock_enabled(%d)", ic->identifier, ic->click_method); libinput_device_config_tap_set_drag_lock_enabled(libinput_device, ic->drag_lock); } if (ic->dwt != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) dwt_set_enabled(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) dwt_set_enabled(%d)", ic->identifier, ic->dwt); libinput_device_config_dwt_set_enabled(libinput_device, ic->dwt); } if (ic->left_handed != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) left_handed_set_enabled(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) left_handed_set_enabled(%d)", ic->identifier, ic->left_handed); libinput_device_config_left_handed_set(libinput_device, ic->left_handed); } if (ic->middle_emulation != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) middle_emulation_set_enabled(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) middle_emulation_set_enabled(%d)", ic->identifier, ic->middle_emulation); libinput_device_config_middle_emulation_set_enabled(libinput_device, ic->middle_emulation); } if (ic->natural_scroll != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) natural_scroll_set_enabled(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) natural_scroll_set_enabled(%d)", ic->identifier, ic->natural_scroll); libinput_device_config_scroll_set_natural_scroll_enabled(libinput_device, ic->natural_scroll); } if (ic->pointer_accel != FLT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) accel_set_speed(%f)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) accel_set_speed(%f)", ic->identifier, ic->pointer_accel); libinput_device_config_accel_set_speed(libinput_device, ic->pointer_accel); } if (ic->scroll_method != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) scroll_set_method(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) scroll_set_method(%d)", ic->identifier, ic->scroll_method); libinput_device_config_scroll_set_method(libinput_device, ic->scroll_method); } if (ic->send_events != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) send_events_set_mode(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) send_events_set_mode(%d)", ic->identifier, ic->send_events); libinput_device_config_send_events_set_mode(libinput_device, ic->send_events); } if (ic->tap != INT_MIN) { - sway_log(L_DEBUG, "libinput_config_pointer(%s) tap_set_enabled(%d)", + wlr_log(L_DEBUG, "libinput_config_pointer(%s) tap_set_enabled(%d)", ic->identifier, ic->tap); libinput_device_config_tap_set_enabled(libinput_device, ic->tap); } @@ -167,7 +167,7 @@ static void input_add_notify(struct wl_listener *listener, void *data) { input_device->identifier = get_device_identifier(device); wl_list_insert(&input->devices, &input_device->link); - sway_log(L_DEBUG, "adding device: '%s'", + wlr_log(L_DEBUG, "adding device: '%s'", input_device->identifier); // find config @@ -185,7 +185,7 @@ static void input_add_notify(struct wl_listener *listener, void *data) { struct sway_seat *seat = NULL; if (!input_has_seat_configuration(input)) { - sway_log(L_DEBUG, "no seat configuration, using default seat"); + wlr_log(L_DEBUG, "no seat configuration, using default seat"); seat = input_manager_get_seat(input, default_seat); sway_seat_add_device(seat, input_device); return; @@ -213,7 +213,7 @@ static void input_add_notify(struct wl_listener *listener, void *data) { } if (!added) { - sway_log(L_DEBUG, + wlr_log(L_DEBUG, "device '%s' is not configured on any seats", input_device->identifier); } @@ -231,7 +231,7 @@ static void input_remove_notify(struct wl_listener *listener, void *data) { return; } - sway_log(L_DEBUG, "removing device: '%s'", + wlr_log(L_DEBUG, "removing device: '%s'", input_device->identifier); struct sway_seat *seat = NULL; @@ -309,7 +309,7 @@ void sway_input_manager_apply_input_config(struct sway_input_manager *input, void sway_input_manager_apply_seat_config(struct sway_input_manager *input, struct seat_config *seat_config) { - sway_log(L_DEBUG, "applying new seat config for seat %s", + wlr_log(L_DEBUG, "applying new seat config for seat %s", seat_config->name); struct sway_seat *seat = input_manager_get_seat(input, seat_config->name); if (!seat) { diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index c2bb2578..e7539c48 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -90,11 +90,11 @@ static bool binding_matches_key_state(struct sway_binding *binding, } static void binding_execute_command(struct sway_binding *binding) { - sway_log(L_DEBUG, "running command for binding: %s", + wlr_log(L_DEBUG, "running command for binding: %s", binding->command); struct cmd_results *results = handle_command(binding->command); if (results->status != CMD_SUCCESS) { - sway_log(L_DEBUG, "could not run command for binding: %s", + wlr_log(L_DEBUG, "could not run command for binding: %s", binding->command); } free_cmd_results(results); @@ -467,7 +467,7 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) { xkb_keymap_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); if (!keymap) { - sway_log(L_DEBUG, "cannot configure keyboard: keymap does not exist"); + wlr_log(L_DEBUG, "cannot configure keyboard: keymap does not exist"); xkb_context_unref(context); return; } diff --git a/sway/input/seat.c b/sway/input/seat.c index 9a6a667b..268486ab 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -112,7 +112,7 @@ void sway_seat_configure_device(struct sway_seat *seat, case WLR_INPUT_DEVICE_TOUCH: case WLR_INPUT_DEVICE_TABLET_PAD: case WLR_INPUT_DEVICE_TABLET_TOOL: - sway_log(L_DEBUG, "TODO: configure other devices"); + wlr_log(L_DEBUG, "TODO: configure other devices"); break; } } @@ -127,11 +127,11 @@ void sway_seat_add_device(struct sway_seat *seat, struct sway_seat_device *seat_device = calloc(1, sizeof(struct sway_seat_device)); if (!seat_device) { - sway_log(L_DEBUG, "could not allocate seat device"); + wlr_log(L_DEBUG, "could not allocate seat device"); return; } - sway_log(L_DEBUG, "adding device %s to seat %s", + wlr_log(L_DEBUG, "adding device %s to seat %s", input_device->identifier, seat->wlr_seat->name); seat_device->sway_seat = seat; @@ -150,7 +150,7 @@ void sway_seat_remove_device(struct sway_seat *seat, return; } - sway_log(L_DEBUG, "removing device %s from seat %s", + wlr_log(L_DEBUG, "removing device %s from seat %s", input_device->identifier, seat->wlr_seat->name); seat_device_destroy(seat_device); diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 046e40a8..d2dd881f 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -125,32 +125,32 @@ struct sockaddr_un *ipc_user_sockaddr(void) { int ipc_handle_connection(int fd, uint32_t mask, void *data) { (void) fd; struct sway_server *server = data; - sway_log(L_DEBUG, "Event on IPC listening socket"); + wlr_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"); + wlr_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"); + wlr_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"); + wlr_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"); + wlr_log(L_ERROR, "Unable to allocate ipc client"); close(client_fd); return 0; } @@ -166,12 +166,12 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) { 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"); + wlr_log(L_ERROR, "Unable to allocate ipc client write buffer"); close(client_fd); return 0; } - sway_log(L_DEBUG, "New client: fd %d", client_fd); + wlr_log(L_DEBUG, "New client: fd %d", client_fd); list_add(ipc_client_list, client); return 0; } @@ -182,22 +182,22 @@ 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"); + wlr_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); + wlr_log(L_DEBUG, "Client %d hung up", client->fd); ipc_client_disconnect(client); return 0; } - sway_log(L_DEBUG, "Client %d readable", client->fd); + wlr_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"); + wlr_log_errno(L_INFO, "Unable to read IPC socket buffer size"); ipc_client_disconnect(client); return 0; } @@ -219,13 +219,13 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { // 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"); + wlr_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"); + wlr_log(L_DEBUG, "IPC header check failed"); ipc_client_disconnect(client); return 0; } @@ -244,13 +244,13 @@ 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"); + wlr_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); + wlr_log(L_DEBUG, "Client %d hung up", client->fd); ipc_client_disconnect(client); return 0; } @@ -259,14 +259,14 @@ int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { return 0; } - sway_log(L_DEBUG, "Client %d writable", client->fd); + wlr_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"); + wlr_log_errno(L_INFO, "Unable to send data from queue to IPC client"); ipc_client_disconnect(client); return 0; } @@ -291,7 +291,7 @@ void ipc_client_disconnect(struct ipc_client *client) { shutdown(client->fd, SHUT_RDWR); } - sway_log(L_INFO, "IPC Client %d disconnected", client->fd); + wlr_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); @@ -313,7 +313,7 @@ void ipc_client_handle_command(struct ipc_client *client) { char *buf = malloc(client->payload_length + 1); if (!buf) { - sway_log_errno(L_INFO, "Unable to allocate IPC payload"); + wlr_log_errno(L_INFO, "Unable to allocate IPC payload"); ipc_client_disconnect(client); return; } @@ -322,7 +322,7 @@ void ipc_client_handle_command(struct ipc_client *client) { 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"); + wlr_log_errno(L_INFO, "Unable to receive payload from IPC client"); ipc_client_disconnect(client); free(buf); return; @@ -393,12 +393,12 @@ void ipc_client_handle_command(struct ipc_client *client) { } default: - sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); + wlr_log(L_INFO, "Unknown IPC command type %i", client->current_command); goto exit_cleanup; } ipc_send_reply(client, error_denied, (uint32_t)strlen(error_denied)); - sway_log(L_DEBUG, "Denied IPC client access to %i", client->current_command); + wlr_log(L_DEBUG, "Denied IPC client access to %i", client->current_command); exit_cleanup: client->payload_length = 0; @@ -422,14 +422,14 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay } if (client->write_buffer_size > 4e6) { // 4 MB - sway_log(L_ERROR, "Client write buffer too big, disconnecting client"); + wlr_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"); + wlr_log(L_ERROR, "Unable to reallocate ipc client write buffer"); ipc_client_disconnect(client); return false; } @@ -446,6 +446,6 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay ipc_client_handle_writable, client); } - sway_log(L_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload); + wlr_log(L_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload); return true; } diff --git a/sway/tree/container.c b/sway/tree/container.c index c5574275..31ec2ce5 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -92,11 +92,11 @@ swayc_t *new_output(struct sway_output *sway_output) { if (strcasecmp(name, cur->name) == 0 || strcasecmp(identifier, cur->name) == 0) { - sway_log(L_DEBUG, "Matched output config for %s", name); + wlr_log(L_DEBUG, "Matched output config for %s", name); oc = cur; } if (strcasecmp("*", cur->name) == 0) { - sway_log(L_DEBUG, "Matched wildcard output config for %s", name); + wlr_log(L_DEBUG, "Matched wildcard output config for %s", name); all = cur; } @@ -126,7 +126,7 @@ swayc_t *new_output(struct sway_output *sway_output) { // Create workspace char *ws_name = workspace_next_name(output->name); - sway_log(L_DEBUG, "Creating default workspace %s", ws_name); + wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); new_workspace(output, ws_name); free(ws_name); return output; @@ -136,7 +136,7 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { if (!sway_assert(output, "new_workspace called with null output")) { return NULL; } - sway_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); + wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); swayc_t *workspace = new_swayc(C_WORKSPACE); workspace->x = output->x; @@ -159,7 +159,7 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { } const char *title = sway_view->iface.get_prop(sway_view, VIEW_PROP_TITLE); swayc_t *swayc = new_swayc(C_VIEW); - sway_log(L_DEBUG, "Adding new view %p:%s to container %p %d", + wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d", swayc, title, sibling, sibling ? sibling->type : 0); // Setup values swayc->sway_view = sway_view; @@ -200,7 +200,7 @@ swayc_t *destroy_output(swayc_t *output) { } } - sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); + wlr_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); free_swayc(output); return &root_container; @@ -210,7 +210,7 @@ swayc_t *destroy_view(swayc_t *view) { if (!sway_assert(view, "null view passed to destroy_view")) { return NULL; } - sway_log(L_DEBUG, "Destroying view '%s'", view->name); + wlr_log(L_DEBUG, "Destroying view '%s'", view->name); swayc_t *parent = view->parent; free_swayc(view); diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 4bcf0e2f..13b8a395 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -59,7 +59,7 @@ void init_layout(void) { } void add_child(swayc_t *parent, swayc_t *child) { - sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", + wlr_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); @@ -145,7 +145,7 @@ void arrange_windows(swayc_t *container, double width, double height) { width = floor(width); height = floor(height); - sway_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container, + wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container, container->name, container->width, container->height, container->x, container->y); @@ -155,7 +155,7 @@ void arrange_windows(swayc_t *container, double width, double height) { // TODO: wlr_output_layout probably for (i = 0; i < container->children->length; ++i) { swayc_t *output = container->children->items[i]; - sway_log(L_DEBUG, "Arranging output '%s' at %f,%f", + wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); arrange_windows(output, -1, -1); } @@ -181,7 +181,7 @@ void arrange_windows(swayc_t *container, double width, double height) { container->height = output->height; container->x = x; container->y = y; - sway_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", + wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", container->name, container->x, container->y); } // children are properly handled below @@ -192,7 +192,7 @@ void arrange_windows(swayc_t *container, double width, double height) { container->height = height; container->sway_view->iface.set_size(container->sway_view, container->width, container->height); - sway_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f", + wlr_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f", container->width, container->height, container->x, container->y); } @@ -215,7 +215,7 @@ void arrange_windows(swayc_t *container, double width, double height) { container->children->length); break; default: - sway_log(L_DEBUG, "TODO: arrange layout type %d", container->layout); + wlr_log(L_DEBUG, "TODO: arrange layout type %d", container->layout); apply_horiz_layout(container, x, y, width, height, 0, container->children->length); break; @@ -244,10 +244,10 @@ static void apply_horiz_layout(swayc_t *container, // Resize windows double child_x = x; if (scale > 0.1) { - sway_log(L_DEBUG, "Arranging %p horizontally", container); + wlr_log(L_DEBUG, "Arranging %p horizontally", container); for (int i = start; i < end; ++i) { swayc_t *child = container->children->items[i]; - sway_log(L_DEBUG, + wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); child->sway_view->iface.set_position(child->sway_view, child_x, y); @@ -294,10 +294,10 @@ void apply_vert_layout(swayc_t *container, // Resize double child_y = y; if (scale > 0.1) { - sway_log(L_DEBUG, "Arranging %p vertically", container); + wlr_log(L_DEBUG, "Arranging %p vertically", container); for (i = start; i < end; ++i) { swayc_t *child = container->children->items[i]; - sway_log(L_DEBUG, + wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); child->sway_view->iface.set_position(child->sway_view, x, child_y); diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index e8ed4102..c37a873c 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -11,7 +11,7 @@ void next_name_map(swayc_t *ws, void *data) { } char *workspace_next_name(const char *output_name) { - sway_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", + wlr_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", output_name); int count = 0; next_name_map(&root_container, &count); -- cgit v1.2.3 From 9e0595f26bcca2a4d0aa735c4cd9fc4f792918bf Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 20 Jan 2018 11:32:07 -0500 Subject: input config handler context --- include/sway/config.h | 8 ++++++++ include/sway/input/input-manager.h | 1 - sway/commands.c | 3 --- sway/commands/input.c | 28 +++++++++++++++++++--------- sway/commands/input/accel_profile.c | 2 ++ sway/commands/input/click_method.c | 4 ++-- sway/commands/input/drag_lock.c | 2 ++ sway/commands/input/dwt.c | 2 ++ sway/commands/input/events.c | 6 ++++-- sway/commands/input/left_handed.c | 2 ++ sway/commands/input/middle_emulation.c | 2 ++ sway/commands/input/natural_scroll.c | 2 ++ sway/commands/input/pointer_accel.c | 2 ++ sway/commands/input/scroll_method.c | 2 ++ sway/commands/input/tap.c | 3 ++- sway/commands/input/xkb_layout.c | 3 ++- sway/commands/input/xkb_model.c | 3 ++- sway/commands/input/xkb_options.c | 3 ++- sway/commands/input/xkb_rules.c | 3 ++- sway/commands/input/xkb_variant.c | 3 ++- sway/config.c | 10 ++++++++-- 21 files changed, 69 insertions(+), 25 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 0a9c4595..1ab96b51 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -350,6 +350,11 @@ struct sway_config { list_t *command_policies; list_t *feature_policies; list_t *ipc_policies; + + // Context for command handlers + struct { + struct input_config *input_config; + } handler_context; }; void pid_workspace_add(struct pid_workspace *pw); @@ -375,6 +380,9 @@ bool read_config(FILE *file, struct sway_config *config); * Free config struct */ void free_config(struct sway_config *config); + +void config_clear_handler_context(struct sway_config *config); + void free_sway_variable(struct sway_variable *var); /** * Does variable replacement for a string based on the config's currently loaded variables. diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 53064eed..8388f930 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -5,7 +5,6 @@ #include "sway/config.h" #include "list.h" -extern struct input_config *current_input_config; extern struct seat_config *current_seat_config; /** diff --git a/sway/commands.c b/sway/commands.c index 1005cf68..fd2e1514 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -70,10 +70,7 @@ void apply_input_config(struct input_config *input) { list_add(config->input_configs, input); } - struct input_config *old_input_config = current_input_config; - current_input_config = input; sway_input_manager_apply_input_config(input_manager, input); - current_input_config = old_input_config; } void apply_seat_config(struct seat_config *seat) { diff --git a/sway/commands/input.c b/sway/commands/input.c index 5ea39f62..5de65621 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -11,8 +11,12 @@ struct cmd_results *cmd_input(int argc, char **argv) { } if (config->reading && strcmp("{", argv[1]) == 0) { - current_input_config = new_input_config(argv[0]); - wlr_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); + free_input_config(config->handler_context.input_config); + config->handler_context.input_config = new_input_config(argv[0]); + if (!config->handler_context.input_config) { + return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + } + wlr_log(L_DEBUG, "entering input block: %s", argv[0]); return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); } @@ -20,15 +24,16 @@ struct cmd_results *cmd_input(int argc, char **argv) { return error; } + bool has_context = (config->handler_context.input_config != NULL); + if (!has_context) { + // caller did not give a context so create one just for this command + config->handler_context.input_config = new_input_config(argv[0]); + } + int argc_new = argc-2; char **argv_new = argv+2; struct cmd_results *res; - struct input_config *old_input_config = current_input_config; - current_input_config = new_input_config(argv[0]); - if (!current_input_config) { - return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); - } if (strcasecmp("accel_profile", argv[1]) == 0) { res = input_cmd_accel_profile(argc_new, argv_new); } else if (strcasecmp("click_method", argv[1]) == 0) { @@ -64,7 +69,12 @@ struct cmd_results *cmd_input(int argc, char **argv) { } else { res = cmd_results_new(CMD_INVALID, "input ", "Unknown command %s", argv[1]); } - free_input_config(current_input_config); - current_input_config = old_input_config; + + if (!has_context) { + // clean up the context we created earlier + free_input_config(config->handler_context.input_config); + config->handler_context.input_config = NULL; + } + return res; } diff --git a/sway/commands/input/accel_profile.c b/sway/commands/input/accel_profile.c index f72b7d48..37d6e133 100644 --- a/sway/commands/input/accel_profile.c +++ b/sway/commands/input/accel_profile.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_accel_profile(int argc, char **argv) { if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "accel_profile", "No input device defined."); diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c index 22eb15f7..8f1f0aa7 100644 --- a/sway/commands/input/click_method.c +++ b/sway/commands/input/click_method.c @@ -6,12 +6,12 @@ #include "log.h" struct cmd_results *input_cmd_click_method(int argc, char **argv) { - wlr_log(L_DEBUG, "click_method for device: %d %s", - current_input_config==NULL, current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "click_method", "No input device defined."); diff --git a/sway/commands/input/drag_lock.c b/sway/commands/input/drag_lock.c index 1783978a..8273a7d4 100644 --- a/sway/commands/input/drag_lock.c +++ b/sway/commands/input/drag_lock.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_drag_lock(int argc, char **argv) { if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "drag_lock", "No input device defined."); diff --git a/sway/commands/input/dwt.c b/sway/commands/input/dwt.c index 8108a110..995a2f47 100644 --- a/sway/commands/input/dwt.c +++ b/sway/commands/input/dwt.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_dwt(int argc, char **argv) { if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined."); } diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c index a1bfbacd..2217f5ce 100644 --- a/sway/commands/input/events.c +++ b/sway/commands/input/events.c @@ -6,16 +6,18 @@ #include "log.h" struct cmd_results *input_cmd_events(int argc, char **argv) { - wlr_log(L_DEBUG, "events for device: %s", - current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "events", "No input device defined."); } + wlr_log(L_DEBUG, "events for device: %s", + current_input_config->identifier); struct input_config *new_config = new_input_config(current_input_config->identifier); diff --git a/sway/commands/input/left_handed.c b/sway/commands/input/left_handed.c index 35740df3..94b8e03e 100644 --- a/sway/commands/input/left_handed.c +++ b/sway/commands/input/left_handed.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_left_handed(int argc, char **argv) { if ((error = checkarg(argc, "left_handed", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "left_handed", "No input device defined."); diff --git a/sway/commands/input/middle_emulation.c b/sway/commands/input/middle_emulation.c index 7bc08ae6..a551fd51 100644 --- a/sway/commands/input/middle_emulation.c +++ b/sway/commands/input/middle_emulation.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) { if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "middle_emulation", "No input device defined."); diff --git a/sway/commands/input/natural_scroll.c b/sway/commands/input/natural_scroll.c index a7dcdc2c..c4e19b78 100644 --- a/sway/commands/input/natural_scroll.c +++ b/sway/commands/input/natural_scroll.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) { if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "natural_scoll", "No input device defined."); diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c index d2261a63..171063aa 100644 --- a/sway/commands/input/pointer_accel.c +++ b/sway/commands/input/pointer_accel.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) { if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "pointer_accel", "No input device defined."); diff --git a/sway/commands/input/scroll_method.c b/sway/commands/input/scroll_method.c index 035262cf..0a1c57ac 100644 --- a/sway/commands/input/scroll_method.c +++ b/sway/commands/input/scroll_method.c @@ -9,6 +9,8 @@ struct cmd_results *input_cmd_scroll_method(int argc, char **argv) { if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "scroll_method", "No input device defined."); diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c index ecab9a5b..e7f03058 100644 --- a/sway/commands/input/tap.c +++ b/sway/commands/input/tap.c @@ -6,11 +6,12 @@ #include "log.h" struct cmd_results *input_cmd_tap(int argc, char **argv) { - wlr_log(L_DEBUG, "tap for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "tap", "No input device defined."); } diff --git a/sway/commands/input/xkb_layout.c b/sway/commands/input/xkb_layout.c index 25db1a33..867e65d3 100644 --- a/sway/commands/input/xkb_layout.c +++ b/sway/commands/input/xkb_layout.c @@ -5,11 +5,12 @@ #include "log.h" struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { - wlr_log(L_DEBUG, "xkb layout for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_layout", EXPECTED_EQUAL_TO, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "xkb_layout", "No input device defined."); } diff --git a/sway/commands/input/xkb_model.c b/sway/commands/input/xkb_model.c index 819b796b..e8c8e04e 100644 --- a/sway/commands/input/xkb_model.c +++ b/sway/commands/input/xkb_model.c @@ -5,11 +5,12 @@ #include "log.h" struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { - wlr_log(L_DEBUG, "xkb model for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_model", EXPECTED_EQUAL_TO, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "xkb_model", "No input device defined."); } diff --git a/sway/commands/input/xkb_options.c b/sway/commands/input/xkb_options.c index ff5f83ec..e9ddd6e3 100644 --- a/sway/commands/input/xkb_options.c +++ b/sway/commands/input/xkb_options.c @@ -5,11 +5,12 @@ #include "log.h" struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { - wlr_log(L_DEBUG, "xkb options for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_options", EXPECTED_EQUAL_TO, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "xkb_options", "No input device defined."); } diff --git a/sway/commands/input/xkb_rules.c b/sway/commands/input/xkb_rules.c index aafe0003..926d0ac1 100644 --- a/sway/commands/input/xkb_rules.c +++ b/sway/commands/input/xkb_rules.c @@ -5,11 +5,12 @@ #include "log.h" struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { - wlr_log(L_DEBUG, "xkb rules for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_rules", EXPECTED_EQUAL_TO, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "xkb_rules", "No input device defined."); } diff --git a/sway/commands/input/xkb_variant.c b/sway/commands/input/xkb_variant.c index 89a61fdc..0e3ffd41 100644 --- a/sway/commands/input/xkb_variant.c +++ b/sway/commands/input/xkb_variant.c @@ -5,11 +5,12 @@ #include "log.h" struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { - wlr_log(L_DEBUG, "xkb variant for device: %s", current_input_config->identifier); struct cmd_results *error = NULL; if ((error = checkarg(argc, "xkb_variant", EXPECTED_EQUAL_TO, 1))) { return error; } + struct input_config *current_input_config = + config->handler_context.input_config; if (!current_input_config) { return cmd_results_new(CMD_FAILURE, "xkb_variant", "No input device defined."); } diff --git a/sway/config.c b/sway/config.c index 5ec45b17..54357625 100644 --- a/sway/config.c +++ b/sway/config.c @@ -54,6 +54,8 @@ static void free_mode(struct sway_mode *mode) { } void free_config(struct sway_config *config) { + config_clear_handler_context(config); + int i; if (!config) { @@ -480,6 +482,11 @@ bool load_include_configs(const char *path, struct sway_config *config) { return true; } +void config_clear_handler_context(struct sway_config *config) { + free_input_config(config->handler_context.input_config); + + memset(&config->handler_context, 0, sizeof(config->handler_context)); +} bool read_config(FILE *file, struct sway_config *config) { bool success = true; @@ -592,8 +599,6 @@ bool read_config(FILE *file, struct sway_config *config) { case CMD_BLOCK_INPUT: wlr_log(L_DEBUG, "End of input block"); - free_input_config(current_input_config); - current_input_config = NULL; block = CMD_BLOCK_END; break; @@ -635,6 +640,7 @@ bool read_config(FILE *file, struct sway_config *config) { default:; } + config_clear_handler_context(config); default:; } free(line); -- cgit v1.2.3 From cc3c713889e529c74888d9cd89af7039bfbae20c Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 20 Jan 2018 11:44:34 -0500 Subject: seat config handler context --- include/sway/config.h | 1 + include/sway/input/input-manager.h | 2 -- sway/commands.c | 1 - sway/commands/input.c | 3 +++ sway/commands/seat.c | 25 +++++++++++++++++++++---- sway/commands/seat/attach.c | 2 ++ sway/commands/seat/fallback.c | 3 +++ sway/config.c | 2 +- 8 files changed, 31 insertions(+), 8 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 1ab96b51..27fae0c6 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -354,6 +354,7 @@ struct sway_config { // Context for command handlers struct { struct input_config *input_config; + struct seat_config *seat_config; } handler_context; }; diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 8388f930..58a93fe3 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -5,8 +5,6 @@ #include "sway/config.h" #include "list.h" -extern struct seat_config *current_seat_config; - /** * The global singleton input manager * TODO: make me not a global diff --git a/sway/commands.c b/sway/commands.c index fd2e1514..414ef809 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -86,7 +86,6 @@ void apply_seat_config(struct seat_config *seat) { list_add(config->seat_configs, seat); } - current_seat_config = seat; sway_input_manager_apply_seat_config(input_manager, seat); } diff --git a/sway/commands/input.c b/sway/commands/input.c index 5de65621..fa9cf05a 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c @@ -28,6 +28,9 @@ struct cmd_results *cmd_input(int argc, char **argv) { if (!has_context) { // caller did not give a context so create one just for this command config->handler_context.input_config = new_input_config(argv[0]); + if (!config->handler_context.input_config) { + return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + } } int argc_new = argc-2; diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 6284002b..45079616 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -11,8 +11,12 @@ struct cmd_results *cmd_seat(int argc, char **argv) { } if (config->reading && strcmp("{", argv[1]) == 0) { - current_seat_config = new_seat_config(argv[0]); - wlr_log(L_DEBUG, "entering seat block: %s", current_seat_config->name); + free_seat_config(config->handler_context.seat_config); + config->handler_context.seat_config = new_seat_config(argv[0]); + if (!config->handler_context.seat_config) { + return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + } + wlr_log(L_DEBUG, "entering seat block: %s", argv[0]); return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); } @@ -20,11 +24,18 @@ struct cmd_results *cmd_seat(int argc, char **argv) { return error; } + bool has_context = (config->handler_context.seat_config != NULL); + if (!has_context) { + config->handler_context.seat_config = new_seat_config(argv[0]); + if (!config->handler_context.seat_config) { + return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + } + } + int argc_new = argc-2; char **argv_new = argv+2; struct cmd_results *res; - current_seat_config = new_seat_config(argv[0]); if (strcasecmp("attach", argv[1]) == 0) { res = seat_cmd_attach(argc_new, argv_new); } else if (strcasecmp("fallback", argv[1]) == 0) { @@ -32,6 +43,12 @@ struct cmd_results *cmd_seat(int argc, char **argv) { } else { res = cmd_results_new(CMD_INVALID, "seat ", "Unknown command %s", argv[1]); } - current_seat_config = NULL; + + if (!has_context) { + // clean up the context we created earlier + free_seat_config(config->handler_context.seat_config); + config->handler_context.seat_config = NULL; + } + return res; } diff --git a/sway/commands/seat/attach.c b/sway/commands/seat/attach.c index 80ec63ce..3e771c00 100644 --- a/sway/commands/seat/attach.c +++ b/sway/commands/seat/attach.c @@ -12,6 +12,8 @@ struct cmd_results *seat_cmd_attach(int argc, char **argv) { if ((error = checkarg(argc, "attach", EXPECTED_AT_LEAST, 1))) { return error; } + struct seat_config *current_seat_config = + config->handler_context.seat_config; if (!current_seat_config) { return cmd_results_new(CMD_FAILURE, "attach", "No seat defined"); } diff --git a/sway/commands/seat/fallback.c b/sway/commands/seat/fallback.c index 7c129aae..56feaab5 100644 --- a/sway/commands/seat/fallback.c +++ b/sway/commands/seat/fallback.c @@ -9,6 +9,8 @@ struct cmd_results *seat_cmd_fallback(int argc, char **argv) { if ((error = checkarg(argc, "fallback", EXPECTED_AT_LEAST, 1))) { return error; } + struct seat_config *current_seat_config = + config->handler_context.seat_config; if (!current_seat_config) { return cmd_results_new(CMD_FAILURE, "fallback", "No seat defined"); } @@ -20,6 +22,7 @@ struct cmd_results *seat_cmd_fallback(int argc, char **argv) { } else if (strcasecmp(argv[0], "false") == 0) { new_config->fallback = 0; } else { + free_seat_config(new_config); return cmd_results_new(CMD_INVALID, "fallback", "Expected 'fallback '"); } diff --git a/sway/config.c b/sway/config.c index 54357625..2683d6bc 100644 --- a/sway/config.c +++ b/sway/config.c @@ -484,6 +484,7 @@ bool load_include_configs(const char *path, struct sway_config *config) { void config_clear_handler_context(struct sway_config *config) { free_input_config(config->handler_context.input_config); + free_seat_config(config->handler_context.seat_config); memset(&config->handler_context, 0, sizeof(config->handler_context)); } @@ -604,7 +605,6 @@ bool read_config(FILE *file, struct sway_config *config) { case CMD_BLOCK_SEAT: wlr_log(L_DEBUG, "End of seat block"); - current_seat_config = NULL; block = CMD_BLOCK_END; break; -- cgit v1.2.3 From c353e01c85049cfbc09510657e453b6aa5fd9c2d Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 20 Jan 2018 14:10:11 -0500 Subject: add kill command --- include/sway/config.h | 1 + include/sway/input/input-manager.h | 3 +++ include/sway/view.h | 1 + sway/commands.c | 1 + sway/commands/kill.c | 25 +++++++++++++++++++++++++ sway/desktop/wl_shell.c | 9 +++++++++ sway/desktop/xdg_shell_v6.c | 11 +++++++++++ sway/desktop/xwayland.c | 8 ++++++++ sway/input/input-manager.c | 11 +++++++++++ sway/input/keyboard.c | 9 ++++++--- sway/meson.build | 1 + 11 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 sway/commands/kill.c (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 27fae0c6..be29082e 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -355,6 +355,7 @@ struct sway_config { struct { struct input_config *input_config; struct seat_config *seat_config; + struct sway_seat *seat; } handler_context; }; diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 58a93fe3..2bf297ce 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -43,4 +43,7 @@ void sway_input_manager_apply_input_config(struct sway_input_manager *input, void sway_input_manager_apply_seat_config(struct sway_input_manager *input, struct seat_config *seat_config); +struct sway_seat *sway_input_manager_get_default_seat( + struct sway_input_manager *input); + #endif diff --git a/include/sway/view.h b/include/sway/view.h index 08c5480b..240ffaa5 100644 --- a/include/sway/view.h +++ b/include/sway/view.h @@ -92,6 +92,7 @@ struct sway_view { void (*set_position)(struct sway_view *view, double ox, double oy); void (*set_activated)(struct sway_view *view, bool activated); + void (*close)(struct sway_view *view); } iface; // only used for unmanaged views (shell specific) diff --git a/sway/commands.c b/sway/commands.c index 414ef809..28943963 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -132,6 +132,7 @@ static struct cmd_handler handlers[] = { { "exit", cmd_exit }, { "include", cmd_include }, { "input", cmd_input }, + { "kill", cmd_kill }, { "output", cmd_output }, { "seat", cmd_seat }, { "set", cmd_set }, diff --git a/sway/commands/kill.c b/sway/commands/kill.c new file mode 100644 index 00000000..4bbf94e5 --- /dev/null +++ b/sway/commands/kill.c @@ -0,0 +1,25 @@ +#include "sway/input/input-manager.h" +#include "sway/input/seat.h" +#include "sway/view.h" +#include "sway/commands.h" + +struct cmd_results *cmd_kill(int argc, char **argv) { + struct sway_seat *seat = config->handler_context.seat; + if (!seat) { + seat = sway_input_manager_get_default_seat(input_manager); + } + + // TODO context for arbitrary sway containers (when we get criteria + // working) will make seat context not explicitly required + if (!seat) { + return cmd_results_new(CMD_FAILURE, NULL, "no seat context given"); + } + + struct sway_view *view = seat->focus->sway_view; + + if (view->iface.close) { + view->iface.close(view); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index e34f5160..0cde6583 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -51,6 +51,14 @@ static void set_activated(struct sway_view *view, bool activated) { // no way to activate wl_shell } +static void close(struct sway_view *view) { + if (!assert_wl_shell(view)) { + return; + } + + wl_client_destroy(view->wlr_wl_shell_surface->client); +} + static void handle_commit(struct wl_listener *listener, void *data) { struct sway_wl_shell_surface *sway_surface = wl_container_of(listener, sway_surface, commit); @@ -103,6 +111,7 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { sway_view->iface.set_size = set_size; sway_view->iface.set_position = set_position; sway_view->iface.set_activated = set_activated; + sway_view->iface.close = close; sway_view->wlr_wl_shell_surface = shell_surface; sway_view->sway_wl_shell_surface = sway_surface; sway_view->surface = shell_surface->surface; diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index df48345c..4b50093f 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -57,6 +57,16 @@ static void set_activated(struct sway_view *view, bool activated) { } } +static void close(struct sway_view *view) { + if (!assert_xdg(view)) { + return; + } + struct wlr_xdg_surface_v6 *surface = view->wlr_xdg_surface_v6; + if (surface->role == WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) { + wlr_xdg_toplevel_v6_send_close(surface); + } +} + static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xdg_surface_v6 *sway_surface = wl_container_of(listener, sway_surface, commit); @@ -107,6 +117,7 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { sway_view->iface.set_size = set_size; sway_view->iface.set_position = set_position; sway_view->iface.set_activated = set_activated; + sway_view->iface.close = close; sway_view->wlr_xdg_surface_v6 = xdg_surface; sway_view->sway_xdg_surface_v6 = sway_surface; sway_view->surface = xdg_surface->surface; diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index a4d9687d..7603d3ca 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -80,6 +80,13 @@ static void set_activated(struct sway_view *view, bool activated) { wlr_xwayland_surface_activate(surface, activated); } +static void close(struct sway_view *view) { + if (!assert_xwayland(view)) { + return; + } + wlr_xwayland_surface_close(view->wlr_xwayland_surface); +} + static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xwayland_surface *sway_surface = wl_container_of(listener, sway_surface, commit); @@ -192,6 +199,7 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { sway_view->iface.set_size = set_size; sway_view->iface.set_position = set_position; sway_view->iface.set_activated = set_activated; + sway_view->iface.close = close; sway_view->wlr_xwayland_surface = xsurface; sway_view->sway_xwayland_surface = sway_surface; sway_view->surface = xsurface->surface; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 26cf5035..7b19991b 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -369,3 +369,14 @@ void sway_input_manager_configure_xcursor(struct sway_input_manager *input) { sway_seat_configure_xcursor(seat); } } + +struct sway_seat *sway_input_manager_get_default_seat( + struct sway_input_manager *input) { + struct sway_seat *seat = NULL; + wl_list_for_each(seat, &input->seats, link) { + if (strcmp(seat->wlr_seat->name, "seat0") == 0) { + return seat; + } + } + return seat; +} diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 5827a1ca..6dc57d46 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -89,9 +89,12 @@ static bool binding_matches_key_state(struct sway_binding *binding, return false; } -static void binding_execute_command(struct sway_binding *binding) { +static void keyboard_execute_command(struct sway_keyboard *keyboard, + struct sway_binding *binding) { wlr_log(L_DEBUG, "running command for binding: %s", binding->command); + config_clear_handler_context(config); + config->handler_context.seat = keyboard->seat_device->sway_seat; struct cmd_results *results = handle_command(binding->command); if (results->status != CMD_SUCCESS) { wlr_log(L_DEBUG, "could not run command for binding: %s", @@ -160,7 +163,7 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, } if (match) { - binding_execute_command(binding); + keyboard_execute_command(keyboard, binding); return true; } } @@ -267,7 +270,7 @@ static bool keyboard_execute_bindcode(struct sway_keyboard *keyboard, for (int i = 0; i < keycode_bindings->length; ++i) { struct sway_binding *binding = keycode_bindings->items[i]; if (binding_matches_keycodes(wlr_keyboard, binding, event)) { - binding_execute_command(binding); + keyboard_execute_command(keyboard, binding); return true; } } diff --git a/sway/meson.build b/sway/meson.build index 30ec166b..d5dead42 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -10,6 +10,7 @@ sway_sources = files( 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', + 'commands/kill.c', 'commands/include.c', 'commands/input.c', 'commands/seat.c', -- cgit v1.2.3 From 6a1d71b8b8f33bdea3fb41bcd0de9439c0452682 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 20 Jan 2018 16:21:45 -0500 Subject: basic command criteria --- include/sway/config.h | 1 + include/sway/container.h | 2 + include/sway/criteria.h | 42 +++++ sway/commands.c | 68 +++++++- sway/commands/kill.c | 8 +- sway/criteria.c | 413 +++++++++++++++++++++++++++++++++++++++++++++++ sway/meson.build | 2 + sway/tree/container.c | 22 +++ 8 files changed, 549 insertions(+), 9 deletions(-) create mode 100644 include/sway/criteria.h create mode 100644 sway/criteria.c (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index be29082e..d07a71df 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -356,6 +356,7 @@ struct sway_config { struct input_config *input_config; struct seat_config *seat_config; struct sway_seat *seat; + swayc_t *current_container; } handler_context; }; diff --git a/include/sway/container.h b/include/sway/container.h index 9a5e312b..a99e2694 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -145,4 +145,6 @@ swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); swayc_t *swayc_at(swayc_t *parent, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy); +void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data); + #endif diff --git a/include/sway/criteria.h b/include/sway/criteria.h new file mode 100644 index 00000000..c5ed9857 --- /dev/null +++ b/include/sway/criteria.h @@ -0,0 +1,42 @@ +#ifndef _SWAY_CRITERIA_H +#define _SWAY_CRITERIA_H + +#include "container.h" +#include "list.h" + +/** + * Maps criteria (as a list of criteria tokens) to a command list. + * + * A list of tokens together represent a single criteria string (e.g. + * '[class="abc" title="xyz"]' becomes two criteria tokens). + * + * for_window: Views matching all criteria will have the bound command list + * executed on them. + * + * Set via `for_window `. + */ +struct criteria { + list_t *tokens; // struct crit_token, contains compiled regex. + char *crit_raw; // entire criteria string (for logging) + + char *cmdlist; +}; + +int criteria_cmp(const void *item, const void *data); +void free_criteria(struct criteria *crit); + +// Pouplate list with crit_tokens extracted from criteria string, returns error +// string or NULL if successful. +char *extract_crit_tokens(list_t *tokens, const char *criteria); + +// Returns list of criteria that match given container. These criteria have +// been set with `for_window` commands and have an associated cmdlist. +list_t *criteria_for(swayc_t *cont); + +// Returns a list of all containers that match the given list of tokens. +list_t *container_for(list_t *tokens); + +// Returns true if any criteria in the given list matches this container +bool criteria_any(swayc_t *cont, list_t *criteria); + +#endif diff --git a/sway/commands.c b/sway/commands.c index 28943963..c1e25c5f 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -8,6 +8,7 @@ #include #include "sway/commands.h" #include "sway/config.h" +#include "sway/criteria.h" #include "sway/security.h" #include "sway/input/input-manager.h" #include "stringop.h" @@ -201,9 +202,41 @@ struct cmd_results *handle_command(char *_exec) { char *head = exec; char *cmdlist; char *cmd; + list_t *containers = NULL; head = exec; do { + // Extract criteria (valid for this command list only). + bool has_criteria = false; + if (*head == '[') { + has_criteria = true; + ++head; + char *criteria_string = argsep(&head, "]"); + if (head) { + ++head; + list_t *tokens = create_list(); + char *error; + + if ((error = extract_crit_tokens(tokens, criteria_string))) { + wlr_log(L_DEBUG, "criteria string parse error: %s", error); + results = cmd_results_new(CMD_INVALID, criteria_string, + "Can't parse criteria string: %s", error); + free(error); + free(tokens); + goto cleanup; + } + containers = container_for(tokens); + + free(tokens); + } else { + if (!results) { + results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched ["); + } + goto cleanup; + } + // Skip leading whitespace + head += strspn(head, whitespace); + } // Split command list cmdlist = argsep(&head, ";"); cmdlist += strspn(cmdlist, whitespace); @@ -236,16 +269,35 @@ struct cmd_results *handle_command(char *_exec) { free_argv(argc, argv); goto cleanup; } - struct cmd_results *res = handler->handle(argc-1, argv+1); - if (res->status != CMD_SUCCESS) { - free_argv(argc, argv); - if (results) { - free_cmd_results(results); + + if (!has_criteria) { + config->handler_context.current_container = NULL; + struct cmd_results *res = handler->handle(argc-1, argv+1); + if (res->status != CMD_SUCCESS) { + free_argv(argc, argv); + if (results) { + free_cmd_results(results); + } + results = res; + goto cleanup; + } + free_cmd_results(res); + } else { + wlr_log(L_DEBUG, "@@ running command on containers"); + for (int i = 0; i < containers->length; ++i) { + config->handler_context.current_container = containers->items[i]; + struct cmd_results *res = handler->handle(argc-1, argv+1); + if (res->status != CMD_SUCCESS) { + free_argv(argc, argv); + if (results) { + free_cmd_results(results); + } + results = res; + goto cleanup; + } + free_cmd_results(res); } - results = res; - goto cleanup; } - free_cmd_results(res); free_argv(argc, argv); } while(cmdlist); } while(head); diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 4bbf94e5..f0e3722a 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -15,7 +15,13 @@ struct cmd_results *cmd_kill(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, NULL, "no seat context given"); } - struct sway_view *view = seat->focus->sway_view; + struct sway_view *view = NULL; + + if (config->handler_context.current_container) { + view = config->handler_context.current_container->sway_view; + } else { + view = seat->focus->sway_view; + } if (view->iface.close) { view->iface.close(view); diff --git a/sway/criteria.c b/sway/criteria.c new file mode 100644 index 00000000..c15f6354 --- /dev/null +++ b/sway/criteria.c @@ -0,0 +1,413 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include "sway/criteria.h" +#include "sway/container.h" +#include "sway/config.h" +#include "sway/view.h" +#include "stringop.h" +#include "list.h" +#include "log.h" + +enum criteria_type { // *must* keep in sync with criteria_strings[] + CRIT_APP_ID, + CRIT_CLASS, + CRIT_CON_ID, + CRIT_CON_MARK, + CRIT_FLOATING, + CRIT_ID, + CRIT_INSTANCE, + CRIT_TILING, + CRIT_TITLE, + CRIT_URGENT, + CRIT_WINDOW_ROLE, + CRIT_WINDOW_TYPE, + CRIT_WORKSPACE, + CRIT_LAST +}; + +static const char * const criteria_strings[CRIT_LAST] = { + [CRIT_APP_ID] = "app_id", + [CRIT_CLASS] = "class", + [CRIT_CON_ID] = "con_id", + [CRIT_CON_MARK] = "con_mark", + [CRIT_FLOATING] = "floating", + [CRIT_ID] = "id", + [CRIT_INSTANCE] = "instance", + [CRIT_TILING] = "tiling", + [CRIT_TITLE] = "title", + [CRIT_URGENT] = "urgent", // either "latest" or "oldest" ... + [CRIT_WINDOW_ROLE] = "window_role", + [CRIT_WINDOW_TYPE] = "window_type", + [CRIT_WORKSPACE] = "workspace" +}; + +/** + * A single criteria token (ie. value/regex pair), + * e.g. 'class="some class regex"'. + */ +struct crit_token { + enum criteria_type type; + pcre *regex; + char *raw; +}; + +static void free_crit_token(struct crit_token *crit) { + pcre_free(crit->regex); + free(crit->raw); + free(crit); +} + +static void free_crit_tokens(list_t *crit_tokens) { + for (int i = 0; i < crit_tokens->length; i++) { + free_crit_token(crit_tokens->items[i]); + } + list_free(crit_tokens); +} + +// Extracts criteria string from its brackets. Returns new (duplicate) +// substring. +static char *criteria_from(const char *arg) { + char *criteria = NULL; + if (*arg == '[') { + criteria = strdup(arg + 1); + } else { + criteria = strdup(arg); + } + + int last = strlen(criteria) - 1; + if (criteria[last] == ']') { + criteria[last] = '\0'; + } + return criteria; +} + +// Return instances of c found in str. +static int countchr(char *str, char c) { + int found = 0; + for (int i = 0; str[i]; i++) { + if (str[i] == c) { + ++found; + } + } + return found; +} + +// criteria_str is e.g. '[class="some class regex" instance="instance name"]'. +// +// Will create array of pointers in buf, where first is duplicate of given +// string (must be freed) and the rest are pointers to names and values in the +// base string (every other, naturally). argc will be populated with the length +// of buf. +// +// Returns error string or NULL if successful. +static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str) { + wlr_log(L_DEBUG, "Parsing criteria: '%s'", criteria_str); + char *base = criteria_from(criteria_str); + char *head = base; + char *namep = head; // start of criteria name + char *valp = NULL; // start of value + + // We're going to place EOS markers where we need to and fill up an array + // of pointers to the start of each token (either name or value). + int pairs = countchr(base, '='); + int max_tokens = pairs * 2 + 1; // this gives us at least enough slots + + char **argv = *buf = calloc(max_tokens, sizeof(char*)); + argv[0] = base; // this needs to be freed by caller + bool quoted = true; + + *argc = 1; // uneven = name, even = value + while (*head && *argc < max_tokens) { + if (namep != head && *(head - 1) == '\\') { + // escaped character: don't try to parse this + } else if (*head == '=' && namep != head) { + if (*argc % 2 != 1) { + // we're not expecting a name + return strdup("Unable to parse criteria: " + "Found out of place equal sign"); + } else { + // name ends here + char *end = head; // don't want to rewind the head + while (*(end - 1) == ' ') { + --end; + } + *end = '\0'; + if (*(namep) == ' ') { + namep = strrchr(namep, ' ') + 1; + } + argv[*argc] = namep; + *argc += 1; + } + } else if (*head == '"') { + if (*argc % 2 != 0) { + // we're not expecting a value + return strdup("Unable to parse criteria: " + "Found quoted value where it was not expected"); + } else if (!valp) { // value starts here + valp = head + 1; + quoted = true; + } else { + // value ends here + argv[*argc] = valp; + *argc += 1; + *head = '\0'; + valp = NULL; + namep = head + 1; + } + } else if (*argc % 2 == 0 && *head != ' ') { + // parse unquoted values + if (!valp) { + quoted = false; + valp = head; // value starts here + } + } else if (valp && !quoted && *head == ' ') { + // value ends here + argv[*argc] = valp; + *argc += 1; + *head = '\0'; + valp = NULL; + namep = head + 1; + } + head++; + } + + // catch last unquoted value if needed + if (valp && !quoted && !*head) { + argv[*argc] = valp; + *argc += 1; + } + + return NULL; +} + +// Returns error string on failure or NULL otherwise. +static char *parse_criteria_name(enum criteria_type *type, char *name) { + *type = CRIT_LAST; + for (int i = 0; i < CRIT_LAST; i++) { + if (strcmp(criteria_strings[i], name) == 0) { + *type = (enum criteria_type) i; + break; + } + } + if (*type == CRIT_LAST) { + const char *fmt = "Criteria type '%s' is invalid or unsupported."; + int len = strlen(name) + strlen(fmt) - 1; + char *error = malloc(len); + snprintf(error, len, fmt, name); + return error; + } else if (*type == CRIT_URGENT || *type == CRIT_WINDOW_ROLE || + *type == CRIT_WINDOW_TYPE) { + // (we're just being helpful here) + const char *fmt = "\"%s\" criteria currently unsupported, " + "no window will match this"; + int len = strlen(fmt) + strlen(name) - 1; + char *error = malloc(len); + snprintf(error, len, fmt, name); + return error; + } + return NULL; +} + +// Returns error string on failure or NULL otherwise. +static char *generate_regex(pcre **regex, char *value) { + const char *reg_err; + int offset; + + *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL); + + if (!*regex) { + const char *fmt = "Regex compilation (for '%s') failed: %s"; + int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; + char *error = malloc(len); + snprintf(error, len, fmt, value, reg_err); + return error; + } + return NULL; +} + +// Test whether the criterion corresponds to the currently focused window +static bool crit_is_focused(const char *value) { + return !strcmp(value, "focused") || !strcmp(value, "__focused__"); +} + +// Populate list with crit_tokens extracted from criteria string, returns error +// string or NULL if successful. +char *extract_crit_tokens(list_t *tokens, const char * const criteria) { + int argc; + char **argv = NULL, *error = NULL; + if ((error = crit_tokens(&argc, &argv, criteria))) { + goto ect_cleanup; + } + for (int i = 1; i + 1 < argc; i += 2) { + char* name = argv[i], *value = argv[i + 1]; + struct crit_token *token = calloc(1, sizeof(struct crit_token)); + token->raw = strdup(value); + + if ((error = parse_criteria_name(&token->type, name))) { + free_crit_token(token); + goto ect_cleanup; + } else if (token->type == CRIT_URGENT || crit_is_focused(value)) { + wlr_log(L_DEBUG, "%s -> \"%s\"", name, value); + list_add(tokens, token); + } else if((error = generate_regex(&token->regex, value))) { + free_crit_token(token); + goto ect_cleanup; + } else { + wlr_log(L_DEBUG, "%s -> /%s/", name, value); + list_add(tokens, token); + } + } +ect_cleanup: + free(argv[0]); // base string + free(argv); + return error; +} + +static int regex_cmp(const char *item, const pcre *regex) { + return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); +} + +// test a single view if it matches list of criteria tokens (all of them). +static bool criteria_test(swayc_t *cont, list_t *tokens) { + if (cont->type != C_VIEW) { + return false; + } + struct sway_view *view = cont->sway_view; + + int matches = 0; + for (int i = 0; i < tokens->length; i++) { + struct crit_token *crit = tokens->items[i]; + switch (crit->type) { + case CRIT_CLASS: // TODO + break; + case CRIT_CON_ID: { + char *endptr; + size_t crit_id = strtoul(crit->raw, &endptr, 10); + + if (*endptr == 0 && cont->id == crit_id) { + ++matches; + } + break; + } + case CRIT_CON_MARK: // TODO + break; + case CRIT_FLOATING: // TODO + break; + case CRIT_ID: // TODO + break; + case CRIT_APP_ID: + if (!view->iface.get_prop) { + break; + } + + const char *app_id = + cont->sway_view->iface.get_prop(view, VIEW_PROP_APP_ID); + + if (!app_id) { + break; + } + + if (crit->regex && regex_cmp(app_id, crit->regex) == 0) { + matches++; + } + break; + case CRIT_INSTANCE: // TODO + break; + case CRIT_TILING: // TODO + break; + case CRIT_TITLE: + if (!cont->name) { + // ignore + } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) { + matches++; + } + break; + case CRIT_URGENT: // "latest" or "oldest" + break; + case CRIT_WINDOW_ROLE: + break; + case CRIT_WINDOW_TYPE: + break; + case CRIT_WORKSPACE: // TODO + break; + default: + sway_abort("Invalid criteria type (%i)", crit->type); + break; + } + } + return matches == tokens->length; +} + +int criteria_cmp(const void *a, const void *b) { + if (a == b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + const struct criteria *crit_a = a, *crit_b = b; + int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); + if (cmp != 0) { + return cmp; + } + return lenient_strcmp(crit_a->crit_raw, crit_b->crit_raw); +} + +void free_criteria(struct criteria *crit) { + if (crit->tokens) { + free_crit_tokens(crit->tokens); + } + if (crit->cmdlist) { + free(crit->cmdlist); + } + if (crit->crit_raw) { + free(crit->crit_raw); + } + free(crit); +} + +bool criteria_any(swayc_t *cont, list_t *criteria) { + for (int i = 0; i < criteria->length; i++) { + struct criteria *bc = criteria->items[i]; + if (criteria_test(cont, bc->tokens)) { + return true; + } + } + return false; +} + +list_t *criteria_for(swayc_t *cont) { + list_t *criteria = config->criteria, *matches = create_list(); + for (int i = 0; i < criteria->length; i++) { + struct criteria *bc = criteria->items[i]; + if (criteria_test(cont, bc->tokens)) { + list_add(matches, bc); + } + } + return matches; +} + +struct list_tokens { + list_t *list; + list_t *tokens; +}; + +static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) { + if (criteria_test(container, list_tokens->tokens)) { + list_add(list_tokens->list, container); + } +} + +list_t *container_for(list_t *tokens) { + struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; + + container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens); + + // TODO look in the scratchpad + + return list_tokens.list; +} diff --git a/sway/meson.build b/sway/meson.build index d5dead42..46d79d44 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -38,6 +38,7 @@ sway_sources = files( 'config/output.c', 'config/seat.c', 'config/input.c', + 'criteria.c', 'ipc-json.c', 'ipc-server.c', 'desktop/output.c', @@ -51,6 +52,7 @@ sway_sources = files( ) sway_deps = [ + pcre, pixman, wayland_server, jsonc, diff --git a/sway/tree/container.c b/sway/tree/container.c index d241f69a..e224539f 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -321,3 +321,25 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, return NULL; } + +void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { + if (container) { + int i; + if (container->children) { + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + container_map(child, f, data); + } + } + // TODO + /* + if (container->floating) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *child = container->floating->items[i]; + container_map(child, f, data); + } + } + */ + f(container, data); + } +} -- cgit v1.2.3 From 1156523ccf8b45102333cca7d80b3451930b48e8 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 21 Jan 2018 08:46:31 -0500 Subject: run all commands with focused container context --- sway/commands.c | 28 ++++++++++++++++++---------- sway/commands/kill.c | 28 ++++++++++------------------ 2 files changed, 28 insertions(+), 28 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index c1e25c5f..3ab5add6 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -11,6 +11,7 @@ #include "sway/criteria.h" #include "sway/security.h" #include "sway/input/input-manager.h" +#include "sway/input/seat.h" #include "stringop.h" #include "log.h" @@ -271,19 +272,26 @@ struct cmd_results *handle_command(char *_exec) { } if (!has_criteria) { - config->handler_context.current_container = NULL; - struct cmd_results *res = handler->handle(argc-1, argv+1); - if (res->status != CMD_SUCCESS) { - free_argv(argc, argv); - if (results) { - free_cmd_results(results); + // without criteria, the command acts upon the focused + // container + struct sway_seat *seat = config->handler_context.seat; + if (!seat) { + seat = sway_input_manager_get_default_seat(input_manager); + } + if (seat) { + config->handler_context.current_container = seat->focus; + struct cmd_results *res = handler->handle(argc-1, argv+1); + if (res->status != CMD_SUCCESS) { + free_argv(argc, argv); + if (results) { + free_cmd_results(results); + } + results = res; + goto cleanup; } - results = res; - goto cleanup; + free_cmd_results(res); } - free_cmd_results(res); } else { - wlr_log(L_DEBUG, "@@ running command on containers"); for (int i = 0; i < containers->length; ++i) { config->handler_context.current_container = containers->items[i]; struct cmd_results *res = handler->handle(argc-1, argv+1); diff --git a/sway/commands/kill.c b/sway/commands/kill.c index f0e3722a..a04c21f3 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -1,29 +1,21 @@ +#include #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/view.h" #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - struct sway_seat *seat = config->handler_context.seat; - if (!seat) { - seat = sway_input_manager_get_default_seat(input_manager); + if (!config->handler_context.current_container) { + wlr_log(L_ERROR, "cmd_kill called without container context"); + return cmd_results_new(CMD_INVALID, NULL, + "cmd_kill called without container context " + "(this is a bug in sway)"); } + // TODO close arbitrary containers without a view + struct sway_view *view = + config->handler_context.current_container->sway_view; - // TODO context for arbitrary sway containers (when we get criteria - // working) will make seat context not explicitly required - if (!seat) { - return cmd_results_new(CMD_FAILURE, NULL, "no seat context given"); - } - - struct sway_view *view = NULL; - - if (config->handler_context.current_container) { - view = config->handler_context.current_container->sway_view; - } else { - view = seat->focus->sway_view; - } - - if (view->iface.close) { + if (view && view->iface.close) { view->iface.close(view); } -- cgit v1.2.3 From 0e3eae4baa7717321ec87cf2c46f6798e89e3ded Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 21 Jan 2018 09:09:53 -0500 Subject: view interface --- include/sway/view.h | 16 ++++++++++++++++ sway/commands/kill.c | 4 ++-- sway/criteria.c | 20 ++++++++----------- sway/input/seat.c | 5 ++--- sway/meson.build | 1 + sway/tree/container.c | 2 +- sway/tree/layout.c | 8 ++++---- sway/tree/view.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 sway/tree/view.c (limited to 'sway/commands') diff --git a/include/sway/view.h b/include/sway/view.h index 240ffaa5..ac33e11a 100644 --- a/include/sway/view.h +++ b/include/sway/view.h @@ -99,4 +99,20 @@ struct sway_view { struct wl_list unmanaged_view_link; // sway_root::unmanaged views }; +const char *view_get_title(struct sway_view *view); + +const char *view_get_app_id(struct sway_view *view); + +const char *view_get_class(struct sway_view *view); + +const char *view_get_instance(struct sway_view *view); + +void view_set_size(struct sway_view *view, int width, int height); + +void view_set_position(struct sway_view *view, double ox, double oy); + +void view_set_activated(struct sway_view *view, bool activated); + +void view_close(struct sway_view *view); + #endif diff --git a/sway/commands/kill.c b/sway/commands/kill.c index a04c21f3..62a3a514 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -15,8 +15,8 @@ struct cmd_results *cmd_kill(int argc, char **argv) { struct sway_view *view = config->handler_context.current_container->sway_view; - if (view && view->iface.close) { - view->iface.close(view); + if (view) { + view_close(view); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/criteria.c b/sway/criteria.c index c15f6354..21278a94 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -299,21 +299,17 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { case CRIT_ID: // TODO break; case CRIT_APP_ID: - if (!view->iface.get_prop) { - break; - } - - const char *app_id = - cont->sway_view->iface.get_prop(view, VIEW_PROP_APP_ID); + { + const char *app_id = view_get_app_id(cont->sway_view); + if (!app_id) { + break; + } - if (!app_id) { + if (crit->regex && regex_cmp(app_id, crit->regex) == 0) { + matches++; + } break; } - - if (crit->regex && regex_cmp(app_id, crit->regex) == 0) { - matches++; - } - break; case CRIT_INSTANCE: // TODO break; case CRIT_TILING: // TODO diff --git a/sway/input/seat.c b/sway/input/seat.c index d24a6c7a..ae6dc7c4 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -214,7 +214,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { if (container) { struct sway_view *view = container->sway_view; - view->iface.set_activated(view, true); + view_set_activated(view, true); wl_signal_add(&container->events.destroy, &seat->focus_destroy); seat->focus_destroy.notify = handle_focus_destroy; @@ -234,8 +234,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { if (last_focus && !sway_input_manager_has_focus(seat->input, last_focus)) { struct sway_view *view = last_focus->sway_view; - view->iface.set_activated(view, false); - + view_set_activated(view, false); } } diff --git a/sway/meson.build b/sway/meson.build index 46d79d44..80ccc01d 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -48,6 +48,7 @@ sway_sources = files( 'security.c', 'tree/container.c', 'tree/layout.c', + 'tree/view.c', 'tree/workspace.c', ) diff --git a/sway/tree/container.c b/sway/tree/container.c index e224539f..b7b9bc68 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -157,7 +157,7 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { if (!sway_assert(sibling, "new_view called with NULL sibling/parent")) { return NULL; } - const char *title = sway_view->iface.get_prop(sway_view, VIEW_PROP_TITLE); + const char *title = view_get_title(sway_view); swayc_t *swayc = new_swayc(C_VIEW); wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d", swayc, title, sibling, sibling ? sibling->type : 0); diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 01535f2d..41ff81b2 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -191,8 +191,8 @@ void arrange_windows(swayc_t *container, double width, double height) { { container->width = width; container->height = height; - container->sway_view->iface.set_size(container->sway_view, - container->width, container->height); + view_set_size(container->sway_view, + container->width, container->height); wlr_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f", container->width, container->height, container->x, container->y); @@ -251,7 +251,7 @@ static void apply_horiz_layout(swayc_t *container, wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); - child->sway_view->iface.set_position(child->sway_view, child_x, y); + view_set_position(child->sway_view, child_x, y); if (i == end - 1) { double remaining_width = x + width - child_x; @@ -301,7 +301,7 @@ void apply_vert_layout(swayc_t *container, wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); - child->sway_view->iface.set_position(child->sway_view, x, child_y); + view_set_position(child->sway_view, x, child_y); if (i == end - 1) { double remaining_height = y + height - child_y; diff --git a/sway/tree/view.c b/sway/tree/view.c new file mode 100644 index 00000000..b46c3b17 --- /dev/null +++ b/sway/tree/view.c @@ -0,0 +1,53 @@ +#include "sway/view.h" + +const char *view_get_title(struct sway_view *view) { + if (view->iface.get_prop) { + return view->iface.get_prop(view, VIEW_PROP_TITLE); + } + return NULL; +} + +const char *view_get_app_id(struct sway_view *view) { + if (view->iface.get_prop) { + return view->iface.get_prop(view, VIEW_PROP_APP_ID); + } + return NULL; +} + +const char *view_get_class(struct sway_view *view) { + if (view->iface.get_prop) { + return view->iface.get_prop(view, VIEW_PROP_CLASS); + } + return NULL; +} + +const char *view_get_instance(struct sway_view *view) { + if (view->iface.get_prop) { + return view->iface.get_prop(view, VIEW_PROP_INSTANCE); + } + return NULL; +} + +void view_set_size(struct sway_view *view, int width, int height) { + if (view->iface.set_size) { + view->iface.set_size(view, width, height); + } +} + +void view_set_position(struct sway_view *view, double ox, double oy) { + if (view->iface.set_position) { + view->iface.set_position(view, ox, oy); + } +} + +void view_set_activated(struct sway_view *view, bool activated) { + if (view->iface.set_activated) { + view->iface.set_activated(view, activated); + } +} + +void view_close(struct sway_view *view) { + if (view->iface.close) { + view->iface.close(view); + } +} -- cgit v1.2.3 From c3fc0d446f64ef7f46bc2a152cd68331de4410d0 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 21 Jan 2018 14:15:10 -0500 Subject: cmd-kill: use sway_assert when no container --- sway/commands/kill.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 62a3a514..8208bc14 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -1,12 +1,13 @@ #include +#include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/view.h" #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - if (!config->handler_context.current_container) { - wlr_log(L_ERROR, "cmd_kill called without container context"); + if (!sway_assert(config->handler_context.current_container, + "cmd_kill called without container context")) { return cmd_results_new(CMD_INVALID, NULL, "cmd_kill called without container context " "(this is a bug in sway)"); -- cgit v1.2.3 From beb3805cf0300bc2640290233aa763d757c12466 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 21 Jan 2018 19:13:11 -0500 Subject: dont allow kill command in config --- sway/commands/kill.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'sway/commands') diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 8208bc14..3804f0b0 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -6,6 +6,10 @@ #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "kill", + "Command 'kill' cannot be used in the config file"); + } if (!sway_assert(config->handler_context.current_container, "cmd_kill called without container context")) { return cmd_results_new(CMD_INVALID, NULL, -- cgit v1.2.3 From 869be4378d44e08c686b550a86a5e4fa60915edb Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Fri, 5 Jan 2018 22:05:48 +0100 Subject: commands: add 'reload' command --- sway/commands.c | 1 + sway/commands/reload.c | 21 +++++++++++++++++++++ sway/config.c | 4 ++++ sway/meson.build | 1 + 4 files changed, 27 insertions(+) create mode 100644 sway/commands/reload.c (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index a77ff791..d4262c08 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -136,6 +136,7 @@ static struct cmd_handler handlers[] = { { "input", cmd_input }, { "kill", cmd_kill }, { "output", cmd_output }, + { "reload", cmd_reload }, { "seat", cmd_seat }, { "set", cmd_set }, }; diff --git a/sway/commands/reload.c b/sway/commands/reload.c new file mode 100644 index 00000000..2b553845 --- /dev/null +++ b/sway/commands/reload.c @@ -0,0 +1,21 @@ +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/layout.h" + +struct cmd_results *cmd_reload(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "reload", "Can't be used in config file."); + } + if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) { + return error; + } + if (!load_main_config(config->current_config, true)) { + return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); + } + + load_swaybars(); + + arrange_windows(&root_container, -1, -1); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 1fd123b7..cb22f664 100644 --- a/sway/config.c +++ b/sway/config.c @@ -698,3 +698,7 @@ char *do_var_replacement(char *str) { } return str; } + +void load_swaybars() { + /* stub function for reload commnd, to fill when we restore swaybars */ +} diff --git a/sway/meson.build b/sway/meson.build index 80ccc01d..51e9e4db 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -34,6 +34,7 @@ sway_sources = files( 'commands/input/xkb_rules.c', 'commands/input/xkb_variant.c', 'commands/output.c', + 'commands/reload.c', 'config.c', 'config/output.c', 'config/seat.c', -- cgit v1.2.3 From 6259831d3168898a3f1560fb832106d194b582b4 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Mon, 22 Jan 2018 07:19:12 +0100 Subject: commands/reload: remove unimplemented 'load_swaybars' call --- sway/commands/reload.c | 2 +- sway/config.c | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/reload.c b/sway/commands/reload.c index 2b553845..419c7de3 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -14,7 +14,7 @@ struct cmd_results *cmd_reload(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); } - load_swaybars(); + /* load_swaybars(); -- for when it's implemented */ arrange_windows(&root_container, -1, -1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/config.c b/sway/config.c index 0f91cce6..a67322d1 100644 --- a/sway/config.c +++ b/sway/config.c @@ -717,7 +717,3 @@ char *do_var_replacement(char *str) { } return str; } - -void load_swaybars() { - /* stub function for reload commnd, to fill when we restore swaybars */ -} -- cgit v1.2.3 From b28602aa7425cf435150e6008624429737e037d3 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 30 Jan 2018 23:09:21 -0500 Subject: Implement workspaces --- include/sway/container.h | 19 +++- include/sway/input/input-manager.h | 4 + include/sway/workspace.h | 14 +++ sway/commands.c | 1 + sway/commands/kill.c | 7 +- sway/commands/workspace.c | 101 ++++++++++++++++++ sway/config.c | 15 ++- sway/desktop/output.c | 4 +- sway/desktop/xdg_shell_v6.c | 9 +- sway/input/input-manager.c | 8 ++ sway/input/seat.c | 15 ++- sway/meson.build | 1 + sway/tree/container.c | 41 ++++++- sway/tree/workspace.c | 211 +++++++++++++++++++++++++++++++++++++ 14 files changed, 420 insertions(+), 30 deletions(-) create mode 100644 sway/commands/workspace.c (limited to 'sway/commands') diff --git a/include/sway/container.h b/include/sway/container.h index a99e2694..0c66932d 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -11,6 +11,7 @@ typedef struct sway_container swayc_t; extern swayc_t root_container; struct sway_view; +struct sway_seat; /** * Different kinds of containers. @@ -140,11 +141,25 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); swayc_t *destroy_output(swayc_t *output); swayc_t *destroy_view(swayc_t *view); +swayc_t *next_view_sibling(struct sway_seat *seat); + +/** + * Finds a container based on test criteria. Returns the first container that + * passes the test. + */ +swayc_t *swayc_by_test(swayc_t *container, + bool (*test)(swayc_t *view, void *data), void *data); +/** + * Finds a parent container with the given swayc_type. + */ swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); +/** + * Maps a container's children over a function. + */ +void container_map(swayc_t *container, + void (*f)(swayc_t *view, void *data), void *data); swayc_t *swayc_at(swayc_t *parent, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy); -void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data); - #endif diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 63806b8e..66ace262 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -48,4 +48,8 @@ struct sway_seat *sway_input_manager_get_default_seat( struct sway_seat *input_manager_get_seat(struct sway_input_manager *input, const char *seat_name); + +/** Gets the last seat the user interacted with */ +struct sway_seat *input_manager_current_seat(struct sway_input_manager *input); + #endif diff --git a/include/sway/workspace.h b/include/sway/workspace.h index 04b2ea4e..30bbdaa8 100644 --- a/include/sway/workspace.h +++ b/include/sway/workspace.h @@ -1,6 +1,20 @@ #ifndef _SWAY_WORKSPACE_H #define _SWAY_WORKSPACE_H +struct sway_container; + +extern char *prev_workspace_name; + char *workspace_next_name(const char *output_name); +swayc_t *workspace_create(const char *name); +bool workspace_switch(swayc_t *workspace); + +struct sway_container *workspace_by_number(const char* name); +swayc_t *workspace_by_name(const char*); + +struct sway_container *workspace_output_next(struct sway_container *current); +struct sway_container *workspace_next(struct sway_container *current); +struct sway_container *workspace_output_prev(struct sway_container *current); +struct sway_container *workspace_prev(struct sway_container *current); #endif diff --git a/sway/commands.c b/sway/commands.c index d4262c08..0d4aa104 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -139,6 +139,7 @@ static struct cmd_handler handlers[] = { { "reload", cmd_reload }, { "seat", cmd_seat }, { "set", cmd_set }, + { "workspace", cmd_workspace }, }; static int handler_compare(const void *_a, const void *_b) { diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 3804f0b0..cebf7f3c 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -10,11 +10,10 @@ struct cmd_results *cmd_kill(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "kill", "Command 'kill' cannot be used in the config file"); } - if (!sway_assert(config->handler_context.current_container, - "cmd_kill called without container context")) { + enum swayc_types type = config->handler_context.current_container->type; + if (type != C_VIEW || type != C_CONTAINER) { return cmd_results_new(CMD_INVALID, NULL, - "cmd_kill called without container context " - "(this is a bug in sway)"); + "Can only kill views and containers with this command"); } // TODO close arbitrary containers without a view struct sway_view *view = diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c new file mode 100644 index 00000000..12984ed4 --- /dev/null +++ b/sway/commands/workspace.c @@ -0,0 +1,101 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/input/seat.h" +#include "sway/workspace.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_workspace(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) { + return error; + } + + int output_location = -1; + + swayc_t *current_container = config->handler_context.current_container; + swayc_t *old_workspace = NULL, *old_output = NULL; + if (current_container) { + if (current_container->type == C_WORKSPACE) { + old_workspace = current_container; + } else { + old_workspace = swayc_parent_by_type(current_container, C_WORKSPACE); + } + old_output = swayc_parent_by_type(current_container, C_OUTPUT); + } + + for (int i = 0; i < argc; ++i) { + if (strcasecmp(argv[i], "output") == 0) { + output_location = i; + break; + } + } + if (output_location >= 0) { + if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, output_location + 2))) { + return error; + } + struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); + if (!wso) { + return cmd_results_new(CMD_FAILURE, "workspace output", + "Unable to allocate workspace output"); + } + wso->workspace = join_args(argv, argc - 2); + wso->output = strdup(argv[output_location + 1]); + int i = -1; + if ((i = list_seq_find(config->workspace_outputs, workspace_output_cmp_workspace, wso)) != -1) { + struct workspace_output *old = config->workspace_outputs->items[i]; + free(old); // workspaces can only be assigned to a single output + list_del(config->workspace_outputs, i); + } + wlr_log(L_DEBUG, "Assigning workspace %s to output %s", wso->workspace, wso->output); + list_add(config->workspace_outputs, wso); + } else { + if (config->reading || !config->active) { + return cmd_results_new(CMD_DEFER, "workspace", NULL); + } + swayc_t *ws = NULL; + if (strcasecmp(argv[0], "number") == 0) { + if (!(ws = workspace_by_number(argv[1]))) { + char *name = join_args(argv + 1, argc - 1); + ws = workspace_create(name); + free(name); + } + } else if (strcasecmp(argv[0], "next") == 0) { + ws = workspace_next(old_workspace); + } else if (strcasecmp(argv[0], "prev") == 0) { + ws = workspace_prev(old_workspace); + } else if (strcasecmp(argv[0], "next_on_output") == 0) { + ws = workspace_output_next(old_output); + } else if (strcasecmp(argv[0], "prev_on_output") == 0) { + ws = workspace_output_prev(old_output); + } else if (strcasecmp(argv[0], "back_and_forth") == 0) { + // if auto_back_and_forth is enabled, workspace_switch will swap + // the workspaces. If we created prev_workspace here, workspace_switch + // would put us back on original workspace. + if (config->auto_back_and_forth) { + ws = old_workspace; + } else if (prev_workspace_name + && !(ws = workspace_by_name(prev_workspace_name))) { + ws = workspace_create(prev_workspace_name); + } + } else { + char *name = join_args(argv, argc); + if (!(ws = workspace_by_name(name))) { + ws = workspace_create(name); + } + free(name); + } + workspace_switch(ws); + current_container = config->handler_context.seat->focus; + swayc_t *new_output = swayc_parent_by_type(current_container, C_OUTPUT); + + if (config->mouse_warping && old_output != new_output) { + // TODO: Warp mouse + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index a67322d1..213e7680 100644 --- a/sway/config.c +++ b/sway/config.c @@ -318,10 +318,6 @@ static bool load_config(const char *path, struct sway_config *config) { 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) { char *path; if (file != NULL) { @@ -349,7 +345,9 @@ bool load_main_config(const char *file, bool is_active) { config->reading = true; // Read security configs + // TODO: Security bool success = true; + /* DIR *dir = opendir(SYSCONFDIR "/sway/security.d"); if (!dir) { wlr_log(L_ERROR, @@ -392,6 +390,7 @@ bool load_main_config(const char *file, bool is_active) { free_flat_list(secconfigs); } + */ success = success && load_config(path, config); @@ -717,3 +716,11 @@ char *do_var_replacement(char *str) { } 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); +} diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 0f00222b..a650665f 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -219,8 +219,8 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { wlr_output_make_current(wlr_output); wlr_renderer_begin(server->renderer, wlr_output); - swayc_descendants_of_type( - &root_container, C_VIEW, output_frame_view, soutput); + swayc_t *workspace = soutput->swayc->focused; + swayc_descendants_of_type(workspace, C_VIEW, output_frame_view, soutput); // render unmanaged views on top struct sway_view *view; diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 4b50093f..ca56a9c0 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -124,8 +124,6 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { sway_surface->view = sway_view; // TODO: - // - Wire up listeners - // - Handle popups // - Look up pid and open on appropriate workspace // - Set new view to maximized so it behaves nicely // - Criteria @@ -136,11 +134,8 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { sway_surface->destroy.notify = handle_destroy; wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy); - // TODO: actual focus semantics - swayc_t *parent = root_container.children->items[0]; - parent = parent->children->items[0]; // workspace - - swayc_t *cont = new_view(parent, sway_view); + struct sway_seat *seat = input_manager_current_seat(input_manager); + swayc_t *cont = new_view(seat->focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 12b3a430..d789c7eb 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -23,6 +23,14 @@ struct sway_input_manager *input_manager; struct input_config *current_input_config = NULL; struct seat_config *current_seat_config = NULL; +struct sway_seat *input_manager_current_seat(struct sway_input_manager *input) { + struct sway_seat *seat = config->handler_context.seat; + if (!seat) { + seat = sway_input_manager_get_default_seat(input_manager); + } + return seat; +} + struct sway_seat *input_manager_get_seat( struct sway_input_manager *input, const char *seat_name) { struct sway_seat *seat = NULL; diff --git a/sway/input/seat.c b/sway/input/seat.c index 9ea08eec..5e87986d 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,6 +1,7 @@ #define _XOPEN_SOURCE 700 #include #include +#include "sway/container.h" #include "sway/input/seat.h" #include "sway/input/cursor.h" #include "sway/input/input-manager.h" @@ -81,7 +82,7 @@ static void seat_configure_keyboard(struct sway_seat *seat, sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); - if (seat->focus) { + if (seat->focus && seat->focus->type == C_VIEW) { // force notify reenter to pick up the new configuration wlr_seat_keyboard_clear_focus(seat->wlr_seat); wlr_seat_keyboard_notify_enter(seat->wlr_seat, @@ -205,10 +206,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { static void handle_focus_destroy(struct wl_listener *listener, void *data) { struct sway_seat *seat = wl_container_of(listener, seat, focus_destroy); - //swayc_t *container = data; - - // TODO set new focus based on the state of the tree - sway_seat_set_focus(seat, NULL); + swayc_t *container = data; + sway_seat_set_focus(seat, container->parent); } void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { @@ -218,11 +217,11 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { return; } - if (last_focus) { + if (last_focus && last_focus->type == C_VIEW) { wl_list_remove(&seat->focus_destroy.link); } - if (container) { + if (container && container->type == C_VIEW) { struct sway_view *view = container->sway_view; view_set_activated(view, true); wl_signal_add(&container->events.destroy, &seat->focus_destroy); @@ -241,7 +240,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { seat->focus = container; - if (last_focus && + if (last_focus && last_focus->type == C_VIEW && !sway_input_manager_has_focus(seat->input, last_focus)) { struct sway_view *view = last_focus->sway_view; view_set_activated(view, false); diff --git a/sway/meson.build b/sway/meson.build index 51e9e4db..271d4a99 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -35,6 +35,7 @@ sway_sources = files( 'commands/input/xkb_variant.c', 'commands/output.c', 'commands/reload.c', + 'commands/workspace.c', 'config.c', 'config/output.c', 'config/seat.c', diff --git a/sway/tree/container.c b/sway/tree/container.c index b7b9bc68..48aabd86 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -3,10 +3,13 @@ #include #include #include +#include #include #include #include "sway/config.h" #include "sway/container.h" +#include "sway/input/input-manager.h" +#include "sway/input/seat.h" #include "sway/layout.h" #include "sway/output.h" #include "sway/server.h" @@ -14,6 +17,26 @@ #include "sway/workspace.h" #include "log.h" +swayc_t *swayc_by_test(swayc_t *container, + bool (*test)(swayc_t *view, void *data), void *data) { + if (!container->children) { + return NULL; + } + // TODO: floating windows + for (int i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + if (test(child, data)) { + return child; + } else { + swayc_t *res = swayc_by_test(child, test, data); + if (res) { + return res; + } + } + } + return NULL; +} + void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, void (*func)(swayc_t *item, void *data), void *data) { for (int i = 0; i < root->children->length; ++i) { @@ -127,7 +150,19 @@ swayc_t *new_output(struct sway_output *sway_output) { // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - new_workspace(output, ws_name); + swayc_t *ws = new_workspace(output, ws_name); + output->focused = ws; + // Set each seat's focus if not already set + // TODO FOCUS: this is probably stupid, we shouldn't define focus in two + // places. We should probably put the active workspace on the sway_output + // struct instead of trying to do focus semantics like this + struct sway_seat *seat = NULL; + wl_list_for_each(seat, &input_manager->seats, link) { + if (!seat->focus) { + seat->focus = ws; + } + } + free(ws_name); return output; } @@ -159,8 +194,8 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { } const char *title = view_get_title(sway_view); swayc_t *swayc = new_swayc(C_VIEW); - wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d", - swayc, title, sibling, sibling ? sibling->type : 0); + wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d %s", + swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); // Setup values swayc->sway_view = sway_view; swayc->name = title ? strdup(title) : NULL; diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index c37a873c..23c630b6 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -2,8 +2,20 @@ #include #include #include +#include #include "sway/container.h" +#include "sway/input/input-manager.h" +#include "sway/input/seat.h" +#include "sway/workspace.h" #include "log.h" +#include "util.h" + +char *prev_workspace_name = NULL; +struct workspace_by_number_data { + int len; + const char *cset; + const char *name; +}; void next_name_map(swayc_t *ws, void *data) { int *count = data; @@ -24,3 +36,202 @@ char *workspace_next_name(const char *output_name) { snprintf(name, len + 1, "%d", count); return name; } + +static bool _workspace_by_number(swayc_t *view, void *data) { + if (view->type != C_WORKSPACE) { + return false; + } + struct workspace_by_number_data *wbnd = data; + int a = strspn(view->name, wbnd->cset); + return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; +} + +swayc_t *workspace_by_number(const char* name) { + struct workspace_by_number_data wbnd = {0, "1234567890", name}; + wbnd.len = strspn(name, wbnd.cset); + if (wbnd.len <= 0) { + return NULL; + } + return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); +} + +static bool _workspace_by_name(swayc_t *view, void *data) { + return (view->type == C_WORKSPACE) && + (strcasecmp(view->name, (char *) data) == 0); +} + +swayc_t *workspace_by_name(const char *name) { + struct sway_seat *seat = input_manager_current_seat(input_manager); + swayc_t *current_workspace = NULL, *current_output = NULL; + if (seat->focus) { + current_workspace = swayc_parent_by_type(seat->focus, C_WORKSPACE); + current_output = swayc_parent_by_type(seat->focus, C_OUTPUT); + } + if (strcmp(name, "prev") == 0) { + return workspace_prev(current_workspace); + } else if (strcmp(name, "prev_on_output") == 0) { + return workspace_output_prev(current_output); + } else if (strcmp(name, "next") == 0) { + return workspace_next(current_workspace); + } else if (strcmp(name, "next_on_output") == 0) { + return workspace_output_next(current_output); + } else if (strcmp(name, "current") == 0) { + return current_workspace; + } else { + return swayc_by_test(&root_container, _workspace_by_name, (void *) name); + } +} + +swayc_t *workspace_create(const char *name) { + swayc_t *parent; + // Search for workspace<->output pair + int i, e = config->workspace_outputs->length; + for (i = 0; i < e; ++i) { + struct workspace_output *wso = config->workspace_outputs->items[i]; + if (strcasecmp(wso->workspace, name) == 0) { + // Find output to use if it exists + e = root_container.children->length; + for (i = 0; i < e; ++i) { + parent = root_container.children->items[i]; + if (strcmp(parent->name, wso->output) == 0) { + return new_workspace(parent, name); + } + } + break; + } + } + // Otherwise create a new one + struct sway_seat *seat = input_manager_current_seat(input_manager); + parent = seat->focus; + parent = swayc_parent_by_type(parent, C_OUTPUT); + return new_workspace(parent, name); +} + +/** + * Get the previous or next workspace on the specified output. Wraps around at + * the end and beginning. If next is false, the previous workspace is returned, + * otherwise the next one is returned. + */ +swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { + if (!sway_assert(output->type == C_OUTPUT, + "Argument must be an output, is %d", output->type)) { + return NULL; + } + + int i; + for (i = 0; i < output->children->length; i++) { + if (output->children->items[i] == output->focused) { + return output->children->items[ + wrap(i + (next ? 1 : -1), output->children->length)]; + } + } + + // Doesn't happen, at worst the for loop returns the previously active workspace + return NULL; +} + +/** + * Get the previous or next workspace. If the first/last workspace on an output + * is active, proceed to the previous/next output's previous/next workspace. If + * next is false, the previous workspace is returned, otherwise the next one is + * returned. + */ +swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { + if (!sway_assert(workspace->type == C_WORKSPACE, + "Argument must be a workspace, is %d", workspace->type)) { + return NULL; + } + + swayc_t *current_output = workspace->parent; + int offset = next ? 1 : -1; + int start = next ? 0 : 1; + int end; + if (next) { + end = current_output->children->length - 1; + } else { + end = current_output->children->length; + } + int i; + for (i = start; i < end; i++) { + if (current_output->children->items[i] == workspace) { + return current_output->children->items[i + offset]; + } + } + + // Given workspace is the first/last on the output, jump to the previous/next output + int num_outputs = root_container.children->length; + for (i = 0; i < num_outputs; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *next_output = root_container.children->items[ + wrap(i + offset, num_outputs)]; + return workspace_output_prev_next_impl(next_output, next); + } + } + + // Doesn't happen, at worst the for loop returns the previously active workspace on the active output + return NULL; +} + +swayc_t *workspace_output_next(swayc_t *current) { + return workspace_output_prev_next_impl(current, true); +} + +swayc_t *workspace_next(swayc_t *current) { + return workspace_prev_next_impl(current, true); +} + +swayc_t *workspace_output_prev(swayc_t *current) { + return workspace_output_prev_next_impl(current, false); +} + +swayc_t *workspace_prev(swayc_t *current) { + return workspace_prev_next_impl(current, false); +} + +bool workspace_switch(swayc_t *workspace) { + if (!workspace) { + return false; + } + struct sway_seat *seat = input_manager_current_seat(input_manager); + if (!seat || !seat->focus) { + return false; + } + swayc_t *active_ws = seat->focus; + if (active_ws->type != C_WORKSPACE) { + swayc_parent_by_type(seat->focus, C_WORKSPACE); + } + + if (config->auto_back_and_forth + && active_ws == workspace + && prev_workspace_name) { + swayc_t *new_ws = workspace_by_name(prev_workspace_name); + workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); + } + + if (!prev_workspace_name || (strcmp(prev_workspace_name, active_ws->name) + && active_ws != workspace)) { + free(prev_workspace_name); + prev_workspace_name = malloc(strlen(active_ws->name) + 1); + if (!prev_workspace_name) { + wlr_log(L_ERROR, "Unable to allocate previous workspace name"); + return false; + } + strcpy(prev_workspace_name, active_ws->name); + } + + // TODO: Deal with sticky containers + + wlr_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); + // TODO FOCUS: Focus the last view this seat had focused on this workspace + if (workspace->children->length) { + // TODO FOCUS: This is really fucking stupid + sway_seat_set_focus(seat, workspace->children->items[0]); + } else { + sway_seat_set_focus(seat, workspace); + } + swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); + // TODO FOCUS: take a look at this + output->focused = workspace; + arrange_windows(output, -1, -1); + return true; +} -- cgit v1.2.3 From 515150229847c9ebdfd0cabb6f0026fca9d57a23 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 4 Feb 2018 13:39:10 -0500 Subject: basic focus overhaul --- include/sway/container.h | 6 ++ include/sway/input/seat.h | 16 +++++- include/sway/layout.h | 6 ++ sway/commands.c | 5 +- sway/commands/kill.c | 7 ++- sway/commands/workspace.c | 3 +- sway/desktop/xdg_shell_v6.c | 3 +- sway/input/input-manager.c | 2 +- sway/input/seat.c | 137 ++++++++++++++++++++++++++++++++++++-------- sway/tree/container.c | 33 +++++++++-- sway/tree/layout.c | 30 ++++++++++ sway/tree/workspace.c | 17 +++--- 12 files changed, 225 insertions(+), 40 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/container.h b/include/sway/container.h index 0c66932d..997240bd 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -162,4 +162,10 @@ void container_map(swayc_t *container, swayc_t *swayc_at(swayc_t *parent, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy); +/** + * Get a list of containers that are descendents of the container in rendering + * order + */ +list_t *container_list_children(swayc_t *con); + #endif diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index b21cbccb..8d5d6b75 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -12,14 +12,26 @@ struct sway_seat_device { struct wl_list link; // sway_seat::devices }; +struct sway_seat_container { + struct sway_seat *seat; + swayc_t *container; + + struct wl_list link; // sway_seat::focus_stack + + struct wl_listener destroy; +}; + struct sway_seat { struct wlr_seat *wlr_seat; struct seat_config *config; struct sway_cursor *cursor; struct sway_input_manager *input; - swayc_t *focus; + + bool has_focus; + struct wl_list focus_stack; // list of containers in focus order struct wl_listener focus_destroy; + struct wl_listener new_container; struct wl_list devices; // sway_seat_device::link @@ -44,6 +56,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat); void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); +swayc_t *sway_seat_get_focus(struct sway_seat *seat, swayc_t *container); + void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); #endif diff --git a/include/sway/layout.h b/include/sway/layout.h index af561453..69a66674 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -2,6 +2,7 @@ #define _SWAY_LAYOUT_H #include +#include "sway/container.h" struct sway_container; @@ -11,10 +12,15 @@ struct sway_root { struct wl_listener output_layout_change; struct wl_list unmanaged_views; // sway_view::unmanaged_view_link + + struct { + struct wl_signal new_container; + } events; }; void init_layout(void); void add_child(struct sway_container *parent, struct sway_container *child); +swayc_t *add_sibling(swayc_t *parent, swayc_t *child); struct sway_container *remove_child(struct sway_container *child); enum swayc_layouts default_layout(struct sway_container *output); void sort_workspaces(struct sway_container *output); diff --git a/sway/commands.c b/sway/commands.c index 0d4aa104..6bb4db0b 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -281,7 +281,10 @@ struct cmd_results *handle_command(char *_exec) { seat = sway_input_manager_get_default_seat(input_manager); } if (seat) { - config->handler_context.current_container = seat->focus; + config->handler_context.current_container = + (seat->has_focus ? + sway_seat_get_focus(seat, &root_container) : + NULL); struct cmd_results *res = handler->handle(argc-1, argv+1); if (res->status != CMD_SUCCESS) { free_argv(argc, argv); diff --git a/sway/commands/kill.c b/sway/commands/kill.c index cebf7f3c..4b3666be 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -10,11 +10,16 @@ struct cmd_results *cmd_kill(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "kill", "Command 'kill' cannot be used in the config file"); } + if (config->handler_context.current_container == NULL) { + wlr_log(L_DEBUG, "no container to kill"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } enum swayc_types type = config->handler_context.current_container->type; - if (type != C_VIEW || type != C_CONTAINER) { + if (type != C_VIEW && type != C_CONTAINER) { return cmd_results_new(CMD_INVALID, NULL, "Can only kill views and containers with this command"); } + // TODO close arbitrary containers without a view struct sway_view *view = config->handler_context.current_container->sway_view; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 12984ed4..e7d6cc9f 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -90,7 +90,8 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { free(name); } workspace_switch(ws); - current_container = config->handler_context.seat->focus; + current_container = + sway_seat_get_focus(config->handler_context.seat, &root_container); swayc_t *new_output = swayc_parent_by_type(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index ca56a9c0..04d89015 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -135,7 +135,8 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *cont = new_view(seat->focus, sway_view); + swayc_t *focus = sway_seat_get_focus(seat, &root_container); + swayc_t *cont = new_view(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index d789c7eb..a406636e 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -282,7 +282,7 @@ bool sway_input_manager_has_focus(struct sway_input_manager *input, swayc_t *container) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - if (seat->focus == container) { + if (seat->has_focus && sway_seat_get_focus(seat, &root_container) == container) { return true; } } diff --git a/sway/input/seat.c b/sway/input/seat.c index 5e87986d..cbf05abd 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -32,6 +32,44 @@ void sway_seat_destroy(struct sway_seat *seat) { wlr_seat_destroy(seat->wlr_seat); } +static void handle_seat_container_destroy(struct wl_listener *listener, + void *data) { + struct sway_seat_container *seat_con = + wl_container_of(listener, seat_con, destroy); + wl_list_remove(&seat_con->link); + wl_list_remove(&seat_con->destroy.link); + free(seat_con); +} + +static struct sway_seat_container *seat_container_from_container( + struct sway_seat *seat, swayc_t *con) { + struct sway_seat_container *seat_con = NULL; + wl_list_for_each(seat_con, &seat->focus_stack, link) { + if (seat_con->container == con) { + return seat_con; + } + } + + seat_con = calloc(1, sizeof(struct sway_seat_container)); + if (seat_con == NULL) { + wlr_log(L_ERROR, "could not allocate seat container"); + return NULL; + } + + seat_con->container = con; + wl_list_insert(seat->focus_stack.prev, &seat_con->link); + wl_signal_add(&con->events.destroy, &seat_con->destroy); + seat_con->destroy.notify = handle_seat_container_destroy; + + return seat_con; +} + +static void handle_new_container(struct wl_listener *listener, void *data) { + struct sway_seat *seat = wl_container_of(listener, seat, new_container); + swayc_t *con = data; + seat_container_from_container(seat, con); +} + struct sway_seat *sway_seat_create(struct sway_input_manager *input, const char *seat_name) { struct sway_seat *seat = calloc(1, sizeof(struct sway_seat)); @@ -52,6 +90,24 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, return NULL; } + // init the focus stack + wl_list_init(&seat->focus_stack); + list_t *containers = container_list_children(&root_container); + if (containers == NULL) { + wlr_seat_destroy(seat->wlr_seat); + free(seat); + return NULL; + } + for (int i = containers->length - 1; i >= 0; --i) { + swayc_t *con = containers->items[i]; + seat_container_from_container(seat, con); + } + free(containers); + + wl_signal_add(&root_container.sway_root->events.new_container, + &seat->new_container); + seat->new_container.notify = handle_new_container; + seat->input = input; wl_list_init(&seat->devices); @@ -82,12 +138,15 @@ static void seat_configure_keyboard(struct sway_seat *seat, sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); - if (seat->focus && seat->focus->type == C_VIEW) { - // force notify reenter to pick up the new configuration - wlr_seat_keyboard_clear_focus(seat->wlr_seat); - wlr_seat_keyboard_notify_enter(seat->wlr_seat, - seat->focus->sway_view->surface, wlr_keyboard->keycodes, - wlr_keyboard->num_keycodes, &wlr_keyboard->modifiers); + if (seat->has_focus) { + swayc_t *focus = sway_seat_get_focus(seat, &root_container); + if (focus && focus->type == C_VIEW) { + // force notify reenter to pick up the new configuration + wlr_seat_keyboard_clear_focus(seat->wlr_seat); + wlr_seat_keyboard_notify_enter(seat->wlr_seat, + focus->sway_view->surface, wlr_keyboard->keycodes, + wlr_keyboard->num_keycodes, &wlr_keyboard->modifiers); + } } } @@ -211,35 +270,43 @@ static void handle_focus_destroy(struct wl_listener *listener, void *data) { } void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { - swayc_t *last_focus = seat->focus; + swayc_t *last_focus = + (seat->has_focus ? sway_seat_get_focus(seat, &root_container) : NULL); - if (last_focus == container) { + if (container && last_focus == container) { return; } - if (last_focus && last_focus->type == C_VIEW) { + if (last_focus) { wl_list_remove(&seat->focus_destroy.link); + seat->has_focus = false; } - if (container && container->type == C_VIEW) { - struct sway_view *view = container->sway_view; - view_set_activated(view, true); + if (container) { + struct sway_seat_container *seat_con = + seat_container_from_container(seat, container); wl_signal_add(&container->events.destroy, &seat->focus_destroy); seat->focus_destroy.notify = handle_focus_destroy; - - struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat); - if (keyboard) { - wlr_seat_keyboard_notify_enter(seat->wlr_seat, view->surface, - keyboard->keycodes, keyboard->num_keycodes, - &keyboard->modifiers); - } else { - wlr_seat_keyboard_notify_enter(seat->wlr_seat, view->surface, - NULL, 0, NULL); + seat->has_focus = true; + + wl_list_remove(&seat_con->link); + wl_list_insert(&seat->focus_stack, &seat_con->link); + + if (container->type == C_VIEW) { + struct sway_view *view = container->sway_view; + view_set_activated(view, true); + struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat); + if (keyboard) { + wlr_seat_keyboard_notify_enter(seat->wlr_seat, view->surface, + keyboard->keycodes, keyboard->num_keycodes, + &keyboard->modifiers); + } else { + wlr_seat_keyboard_notify_enter(seat->wlr_seat, view->surface, + NULL, 0, NULL); + } } } - seat->focus = container; - if (last_focus && last_focus->type == C_VIEW && !sway_input_manager_has_focus(seat->input, last_focus)) { struct sway_view *view = last_focus->sway_view; @@ -247,6 +314,30 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { } } +swayc_t *sway_seat_get_focus(struct sway_seat *seat, swayc_t *container) { + struct sway_seat_container *current = NULL; + swayc_t *parent = NULL; + wl_list_for_each(current, &seat->focus_stack, link) { + if (current->container->type < C_WORKSPACE) { + continue; + } + parent = current->container->parent; + + if (current->container == container) { + return current->container; + } + + while (parent) { + if (parent == container) { + return current->container; + } + parent = parent->parent; + } + } + + return NULL; +} + void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config) { // clear configs diff --git a/sway/tree/container.c b/sway/tree/container.c index 48aabd86..67fac5ee 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -158,12 +158,13 @@ swayc_t *new_output(struct sway_output *sway_output) { // struct instead of trying to do focus semantics like this struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { - if (!seat->focus) { - seat->focus = ws; + if (!seat->has_focus) { + sway_seat_set_focus(seat, ws); } } free(ws_name); + wl_signal_emit(&root_container.sway_root->events.new_container, output); return output; } @@ -185,6 +186,7 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { add_child(output, workspace); sort_workspaces(output); + wl_signal_emit(&root_container.sway_root->events.new_container, workspace); return workspace; } @@ -207,9 +209,9 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { add_child(sibling, swayc); } else { // Regular case, create as sibling of current container - // TODO WLR - //add_sibling(sibling, swayc); + add_sibling(sibling, swayc); } + wl_signal_emit(&root_container.sway_root->events.new_container, swayc); return swayc; } @@ -378,3 +380,26 @@ void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), voi f(container, data); } } + +/** + * Get a list of containers that are descendents of the container in rendering + * order + */ +list_t *container_list_children(swayc_t *con) { + list_t *list = create_list(); + list_t *queue = create_list(); + + list_add(queue, con); + + swayc_t *current = NULL; + while (queue->length) { + current = queue->items[0]; + list_del(queue, 0); + list_add(list, current); + // TODO floating containers + list_cat(queue, current->children); + } + + list_free(queue); + return list; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 41ff81b2..45f8c3ae 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -48,10 +48,12 @@ void init_layout(void) { root_container.layout = L_NONE; root_container.name = strdup("root"); root_container.children = create_list(); + wl_signal_init(&root_container.events.destroy); root_container.sway_root = calloc(1, sizeof(*root_container.sway_root)); root_container.sway_root->output_layout = wlr_output_layout_create(); wl_list_init(&root_container.sway_root->unmanaged_views); + wl_signal_init(&root_container.sway_root->events.new_container); root_container.sway_root->output_layout_change.notify = output_layout_change_notify; @@ -59,6 +61,34 @@ void init_layout(void) { &root_container.sway_root->output_layout_change); } +int index_child(const swayc_t *child) { + // TODO handle floating + swayc_t *parent = child->parent; + int i, len; + len = parent->children->length; + for (i = 0; i < len; ++i) { + if (parent->children->items[i] == child) { + break; + } + } + + if (!sway_assert(i < len, "Stray container")) { + return -1; + } + return i; +} + +swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { + // TODO handle floating + swayc_t *parent = fixed->parent; + int i = index_child(fixed); + list_insert(parent->children, i + 1, active); + active->parent = parent; + // focus new child + parent->focused = active; + return active->parent; +} + void add_child(swayc_t *parent, swayc_t *child) { wlr_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 23c630b6..ce5b425c 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -63,9 +63,10 @@ static bool _workspace_by_name(swayc_t *view, void *data) { swayc_t *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); swayc_t *current_workspace = NULL, *current_output = NULL; - if (seat->focus) { - current_workspace = swayc_parent_by_type(seat->focus, C_WORKSPACE); - current_output = swayc_parent_by_type(seat->focus, C_OUTPUT); + if (seat->has_focus) { + swayc_t *focus = sway_seat_get_focus(seat, &root_container); + current_workspace = swayc_parent_by_type(focus, C_WORKSPACE); + current_output = swayc_parent_by_type(focus, C_OUTPUT); } if (strcmp(name, "prev") == 0) { return workspace_prev(current_workspace); @@ -102,7 +103,8 @@ swayc_t *workspace_create(const char *name) { } // Otherwise create a new one struct sway_seat *seat = input_manager_current_seat(input_manager); - parent = seat->focus; + swayc_t *focus = sway_seat_get_focus(seat, &root_container); + parent = focus; parent = swayc_parent_by_type(parent, C_OUTPUT); return new_workspace(parent, name); } @@ -193,12 +195,13 @@ bool workspace_switch(swayc_t *workspace) { return false; } struct sway_seat *seat = input_manager_current_seat(input_manager); - if (!seat || !seat->focus) { + swayc_t *focus = sway_seat_get_focus(seat, &root_container); + if (!seat || !focus) { return false; } - swayc_t *active_ws = seat->focus; + swayc_t *active_ws = focus; if (active_ws->type != C_WORKSPACE) { - swayc_parent_by_type(seat->focus, C_WORKSPACE); + swayc_parent_by_type(focus, C_WORKSPACE); } if (config->auto_back_and_forth -- cgit v1.2.3 From a7d49da23956c245f0e6b8f7dc9cb532eb14c4b9 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Wed, 7 Feb 2018 18:17:57 -0500 Subject: separate seat get focus and seat get focus inactive --- include/sway/input/seat.h | 4 +++- sway/commands.c | 4 +--- sway/commands/workspace.c | 2 +- sway/desktop/xdg_shell_v6.c | 2 +- sway/input/input-manager.c | 2 +- sway/input/seat.c | 32 +++++++++++++++++++------------- sway/tree/workspace.c | 8 ++++---- 7 files changed, 30 insertions(+), 24 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index 8d5d6b75..26a7e5dc 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -56,7 +56,9 @@ void sway_seat_configure_xcursor(struct sway_seat *seat); void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); -swayc_t *sway_seat_get_focus(struct sway_seat *seat, swayc_t *container); +swayc_t *sway_seat_get_focus(struct sway_seat *seat); + +swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container); void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); diff --git a/sway/commands.c b/sway/commands.c index 6bb4db0b..d8d29a1c 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -282,9 +282,7 @@ struct cmd_results *handle_command(char *_exec) { } if (seat) { config->handler_context.current_container = - (seat->has_focus ? - sway_seat_get_focus(seat, &root_container) : - NULL); + sway_seat_get_focus(seat); struct cmd_results *res = handler->handle(argc-1, argv+1); if (res->status != CMD_SUCCESS) { free_argv(argc, argv); diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index e7d6cc9f..fa891398 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -91,7 +91,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { } workspace_switch(ws); current_container = - sway_seat_get_focus(config->handler_context.seat, &root_container); + sway_seat_get_focus(config->handler_context.seat); swayc_t *new_output = swayc_parent_by_type(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 04d89015..b44d9e54 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -135,7 +135,7 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus(seat, &root_container); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); swayc_t *cont = new_view(focus, sway_view); sway_view->swayc = cont; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index a406636e..90eb8cf6 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -282,7 +282,7 @@ bool sway_input_manager_has_focus(struct sway_input_manager *input, swayc_t *container) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - if (seat->has_focus && sway_seat_get_focus(seat, &root_container) == container) { + if (sway_seat_get_focus(seat) == container) { return true; } } diff --git a/sway/input/seat.c b/sway/input/seat.c index cbf05abd..d2ffca64 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -137,16 +137,14 @@ static void seat_configure_keyboard(struct sway_seat *seat, struct wlr_keyboard *wlr_keyboard = seat_device->input_device->wlr_device->keyboard; sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, - seat_device->input_device->wlr_device); - if (seat->has_focus) { - swayc_t *focus = sway_seat_get_focus(seat, &root_container); - if (focus && focus->type == C_VIEW) { - // force notify reenter to pick up the new configuration - wlr_seat_keyboard_clear_focus(seat->wlr_seat); - wlr_seat_keyboard_notify_enter(seat->wlr_seat, - focus->sway_view->surface, wlr_keyboard->keycodes, - wlr_keyboard->num_keycodes, &wlr_keyboard->modifiers); - } + seat_device->input_device->wlr_device); + swayc_t *focus = sway_seat_get_focus(seat); + if (focus && focus->type == C_VIEW) { + // force notify reenter to pick up the new configuration + wlr_seat_keyboard_clear_focus(seat->wlr_seat); + wlr_seat_keyboard_notify_enter(seat->wlr_seat, + focus->sway_view->surface, wlr_keyboard->keycodes, + wlr_keyboard->num_keycodes, &wlr_keyboard->modifiers); } } @@ -266,12 +264,13 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { static void handle_focus_destroy(struct wl_listener *listener, void *data) { struct sway_seat *seat = wl_container_of(listener, seat, focus_destroy); swayc_t *container = data; + // TODO dont set focus to the parent, set focus to the next focus inactive + // of the parent sway_seat_set_focus(seat, container->parent); } void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { - swayc_t *last_focus = - (seat->has_focus ? sway_seat_get_focus(seat, &root_container) : NULL); + swayc_t *last_focus = sway_seat_get_focus(seat); if (container && last_focus == container) { return; @@ -314,7 +313,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { } } -swayc_t *sway_seat_get_focus(struct sway_seat *seat, swayc_t *container) { +swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container) { struct sway_seat_container *current = NULL; swayc_t *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { @@ -338,6 +337,13 @@ swayc_t *sway_seat_get_focus(struct sway_seat *seat, swayc_t *container) { return NULL; } +swayc_t *sway_seat_get_focus(struct sway_seat *seat) { + if (!seat->has_focus) { + return NULL; + } + return sway_seat_get_focus_inactive(seat, &root_container); +} + void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config) { // clear configs diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index ce5b425c..29f07f74 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -63,8 +63,8 @@ static bool _workspace_by_name(swayc_t *view, void *data) { swayc_t *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); swayc_t *current_workspace = NULL, *current_output = NULL; - if (seat->has_focus) { - swayc_t *focus = sway_seat_get_focus(seat, &root_container); + swayc_t *focus = sway_seat_get_focus(seat); + if (focus) { current_workspace = swayc_parent_by_type(focus, C_WORKSPACE); current_output = swayc_parent_by_type(focus, C_OUTPUT); } @@ -103,7 +103,7 @@ swayc_t *workspace_create(const char *name) { } // Otherwise create a new one struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus(seat, &root_container); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); parent = focus; parent = swayc_parent_by_type(parent, C_OUTPUT); return new_workspace(parent, name); @@ -195,7 +195,7 @@ bool workspace_switch(swayc_t *workspace) { return false; } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus(seat, &root_container); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); if (!seat || !focus) { return false; } -- cgit v1.2.3 From 52670c636cf5115560ce6d20e2aaab1d55c49d0b Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Wed, 14 Feb 2018 16:47:23 -0500 Subject: basic focus (without direction) --- sway/commands.c | 1 + sway/commands/focus.c | 32 ++++++++++++++++++++++++++++++++ sway/ipc-server.c | 1 + sway/meson.build | 1 + 4 files changed, 35 insertions(+) create mode 100644 sway/commands/focus.c (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index d8d29a1c..bc2a85d7 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -132,6 +132,7 @@ static struct cmd_handler handlers[] = { { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, + { "focus", cmd_focus }, { "include", cmd_include }, { "input", cmd_input }, { "kill", cmd_kill }, diff --git a/sway/commands/focus.c b/sway/commands/focus.c new file mode 100644 index 00000000..5286851f --- /dev/null +++ b/sway/commands/focus.c @@ -0,0 +1,32 @@ +#include +#include "log.h" +#include "sway/input/input-manager.h" +#include "sway/input/seat.h" +#include "sway/view.h" +#include "sway/commands.h" + +struct cmd_results *cmd_focus(int argc, char **argv) { + swayc_t *con = config->handler_context.current_container; + struct sway_seat *seat = config->handler_context.seat; + + if (!sway_assert(seat, "'focus' command called without seat context")) { + return cmd_results_new(CMD_FAILURE, "focus", + "Command 'focus' called without seat context (this is a bug in sway)"); + } + + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "focus", + "Command 'focus' cannot be used in the config file"); + } + if (con == NULL) { + wlr_log(L_DEBUG, "no container to focus"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + + if (argc == 0) { + sway_seat_set_focus(seat, con); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/ipc-server.c b/sway/ipc-server.c index a16a2b80..ee259c99 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -336,6 +336,7 @@ void ipc_client_handle_command(struct ipc_client *client) { case IPC_COMMAND: { config_clear_handler_context(config); + config->handler_context.seat = input_manager_current_seat(input_manager); struct cmd_results *results = handle_command(buf); const char *json = cmd_results_to_json(results); char reply[256]; diff --git a/sway/meson.build b/sway/meson.build index 271d4a99..8d5a97d2 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -10,6 +10,7 @@ sway_sources = files( 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', + 'commands/focus.c', 'commands/kill.c', 'commands/include.c', 'commands/input.c', -- cgit v1.2.3 From a3ae67af4e47467b134b876e42a148b1895e7283 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Wed, 14 Feb 2018 17:12:21 -0500 Subject: basic focus in direction --- sway/commands/focus.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'sway/commands') diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 5286851f..ba47d1e1 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -1,3 +1,4 @@ +#include #include #include "log.h" #include "sway/input/input-manager.h" @@ -5,6 +6,30 @@ #include "sway/view.h" #include "sway/commands.h" +static bool parse_movement_direction(const char *name, enum movement_direction *out) { + if (strcasecmp(name, "left") == 0) { + *out = MOVE_LEFT; + } else if (strcasecmp(name, "right") == 0) { + *out = MOVE_RIGHT; + } else if (strcasecmp(name, "up") == 0) { + *out = MOVE_UP; + } else if (strcasecmp(name, "down") == 0) { + *out = MOVE_DOWN; + } else if (strcasecmp(name, "parent") == 0) { + *out = MOVE_PARENT; + } else if (strcasecmp(name, "child") == 0) { + *out = MOVE_CHILD; + } else if (strcasecmp(name, "next") == 0) { + *out = MOVE_NEXT; + } else if (strcasecmp(name, "prev") == 0) { + *out = MOVE_PREV; + } else { + return false; + } + + return true; +} + struct cmd_results *cmd_focus(int argc, char **argv) { swayc_t *con = config->handler_context.current_container; struct sway_seat *seat = config->handler_context.seat; @@ -22,11 +47,27 @@ struct cmd_results *cmd_focus(int argc, char **argv) { wlr_log(L_DEBUG, "no container to focus"); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } + if (con->type < C_WORKSPACE) { + return cmd_results_new(CMD_FAILURE, "focus", + "Command 'focus' cannot be used above the workspace level"); + } if (argc == 0) { sway_seat_set_focus(seat, con); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } + // TODO mode_toggle + enum movement_direction direction = 0; + if (!parse_movement_direction(argv[0], &direction)) { + return cmd_results_new(CMD_INVALID, "focus", + "Expected 'focus ' or 'focus output '"); + } + + swayc_t *next_focus = get_swayc_in_direction(con, seat, direction); + if (next_focus) { + sway_seat_set_focus(seat, next_focus); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3 From 66d1e0b313c6bbee142bb08e4af07ce6f15cffca Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 22 Feb 2018 18:03:46 -0500 Subject: basic layout command --- include/sway/container.h | 2 ++ sway/commands.c | 1 + sway/commands/layout.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ sway/meson.build | 1 + sway/tree/container.c | 12 +++++++++ 5 files changed, 81 insertions(+) create mode 100644 sway/commands/layout.c (limited to 'sway/commands') diff --git a/include/sway/container.h b/include/sway/container.h index 48363be6..f200a1a2 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -164,4 +164,6 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), void *data); +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); + #endif diff --git a/sway/commands.c b/sway/commands.c index bc2a85d7..a7eb6b4a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -136,6 +136,7 @@ static struct cmd_handler handlers[] = { { "include", cmd_include }, { "input", cmd_input }, { "kill", cmd_kill }, + { "layout", cmd_layout }, { "output", cmd_output }, { "reload", cmd_reload }, { "seat", cmd_seat }, diff --git a/sway/commands/layout.c b/sway/commands/layout.c new file mode 100644 index 00000000..d953abc8 --- /dev/null +++ b/sway/commands/layout.c @@ -0,0 +1,65 @@ +#include +#include +#include "sway/commands.h" +#include "sway/container.h" +#include "sway/layout.h" +#include "log.h" + +struct cmd_results *cmd_layout(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file."); + } + if (!config->active) { + return cmd_results_new(CMD_FAILURE, "layout", "Can only be used when sway is running."); + } + if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { + return error; + } + swayc_t *parent = config->handler_context.current_container; + if (!sway_assert(parent != NULL, "command called without container context")) { + return NULL; + } + + // TODO: floating + /* + if (parent->is_floating) { + return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows"); + } + */ + + while (parent->type == C_VIEW) { + parent = parent->parent; + } + + // TODO: stacks and tabs + + if (strcasecmp(argv[0], "default") == 0) { + swayc_change_layout(parent, parent->prev_layout); + if (parent->layout == L_NONE) { + swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); + swayc_change_layout(parent, default_layout(output)); + } + } else { + if (parent->layout != L_TABBED && parent->layout != L_STACKED) { + parent->prev_layout = parent->layout; + } + + if (strcasecmp(argv[0], "splith") == 0) { + swayc_change_layout(parent, L_HORIZ); + } else if (strcasecmp(argv[0], "splitv") == 0) { + swayc_change_layout(parent, L_VERT); + } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { + if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE + || parent->workspace_layout == L_HORIZ)) { + swayc_change_layout(parent, L_VERT); + } else { + swayc_change_layout(parent, L_HORIZ); + } + } + } + + arrange_windows(parent, parent->width, parent->height); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index 8d5a97d2..26e56ad2 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -14,6 +14,7 @@ sway_sources = files( 'commands/kill.c', 'commands/include.c', 'commands/input.c', + 'commands/layout.c', 'commands/seat.c', 'commands/seat/attach.c', 'commands/seat/fallback.c', diff --git a/sway/tree/container.c b/sway/tree/container.c index 84e14ba6..b56e72e1 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -400,3 +400,15 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), list_free(queue); } + +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { + if (container->type == C_WORKSPACE) { + container->workspace_layout = layout; + if (layout == L_HORIZ || layout == L_VERT) { + container->layout = layout; + } + } else { + container->layout = layout; + } + return container; +} -- cgit v1.2.3 From 7262bf655f7a19af1d4c8681be74d70bfc6b8911 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 24 Feb 2018 13:22:57 -0500 Subject: remove checks for command handlers --- sway/commands/exit.c | 3 --- sway/commands/focus.c | 14 -------------- sway/commands/kill.c | 8 -------- sway/commands/layout.c | 9 --------- sway/commands/reload.c | 3 --- 5 files changed, 37 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/exit.c b/sway/commands/exit.c index 4bb6a97b..d5353c20 100644 --- a/sway/commands/exit.c +++ b/sway/commands/exit.c @@ -6,9 +6,6 @@ void sway_terminate(int exit_code); struct cmd_results *cmd_exit(int argc, char **argv) { struct cmd_results *error = NULL; - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file."); - } if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) { return error; } diff --git a/sway/commands/focus.c b/sway/commands/focus.c index ba47d1e1..f1a8078f 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -33,20 +33,6 @@ static bool parse_movement_direction(const char *name, enum movement_direction * struct cmd_results *cmd_focus(int argc, char **argv) { swayc_t *con = config->handler_context.current_container; struct sway_seat *seat = config->handler_context.seat; - - if (!sway_assert(seat, "'focus' command called without seat context")) { - return cmd_results_new(CMD_FAILURE, "focus", - "Command 'focus' called without seat context (this is a bug in sway)"); - } - - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "focus", - "Command 'focus' cannot be used in the config file"); - } - if (con == NULL) { - wlr_log(L_DEBUG, "no container to focus"); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } if (con->type < C_WORKSPACE) { return cmd_results_new(CMD_FAILURE, "focus", "Command 'focus' cannot be used above the workspace level"); diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 4b3666be..f408ce2a 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -6,14 +6,6 @@ #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "kill", - "Command 'kill' cannot be used in the config file"); - } - if (config->handler_context.current_container == NULL) { - wlr_log(L_DEBUG, "no container to kill"); - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } enum swayc_types type = config->handler_context.current_container->type; if (type != C_VIEW && type != C_CONTAINER) { return cmd_results_new(CMD_INVALID, NULL, diff --git a/sway/commands/layout.c b/sway/commands/layout.c index d953abc8..b0fc5d66 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -7,19 +7,10 @@ struct cmd_results *cmd_layout(int argc, char **argv) { struct cmd_results *error = NULL; - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file."); - } - if (!config->active) { - return cmd_results_new(CMD_FAILURE, "layout", "Can only be used when sway is running."); - } if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { return error; } swayc_t *parent = config->handler_context.current_container; - if (!sway_assert(parent != NULL, "command called without container context")) { - return NULL; - } // TODO: floating /* diff --git a/sway/commands/reload.c b/sway/commands/reload.c index 419c7de3..d54d40db 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -4,9 +4,6 @@ struct cmd_results *cmd_reload(int argc, char **argv) { struct cmd_results *error = NULL; - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "reload", "Can't be used in config file."); - } if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) { return error; } -- cgit v1.2.3 From 2154b7c7d623d99e2623f1928f6e77e95255a2d3 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 24 Feb 2018 13:34:47 -0500 Subject: add config handlers --- sway/commands.c | 19 ++++++++++++++++++- sway/commands/set.c | 1 - 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index ed8da3dc..b52eb200 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -138,10 +138,17 @@ static struct cmd_handler handlers[] = { { "input", cmd_input }, { "output", cmd_output }, { "seat", cmd_seat }, - { "set", cmd_set }, { "workspace", cmd_workspace }, }; +/** + * Commands that can *only* run in the config loading context + * Keep alphabetized + */ +static struct cmd_handler config_handlers[] = { + { "set", cmd_set }, +}; + /** * Commands that can *not* run in the config loading context * Keep alphabetized @@ -215,6 +222,16 @@ static struct cmd_handler *find_handler(char *line, enum cmd_status block) { } } + if (config->reading) { + res = bsearch(&d, config_handlers, + sizeof(config_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + + if (res) { + return res; + } + } + res = bsearch(&d, handlers, sizeof(handlers) / sizeof(struct cmd_handler), sizeof(struct cmd_handler), handler_compare); diff --git a/sway/commands/set.c b/sway/commands/set.c index 856c73e7..84e9b792 100644 --- a/sway/commands/set.c +++ b/sway/commands/set.c @@ -27,7 +27,6 @@ void free_sway_variable(struct sway_variable *var) { struct cmd_results *cmd_set(int argc, char **argv) { char *tmp; struct cmd_results *error = NULL; - if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file."); if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) { return error; } -- cgit v1.2.3 From 68cfa7ef6705c530ff28d9754c5b6cab7b429150 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 28 Mar 2018 16:38:11 -0400 Subject: Render layer surfaces and respect exclusive zone --- include/sway/layers.h | 3 + include/sway/output.h | 6 +- sway/commands/output.c | 2 +- sway/config/output.c | 31 ++++-- sway/desktop/layer_shell.c | 239 +++++++++++++++++++++++++++++++++++++++++++-- sway/desktop/output.c | 53 ++++++++-- sway/desktop/xwayland.c | 4 +- sway/tree/container.c | 4 +- sway/tree/layout.c | 11 ++- 9 files changed, 320 insertions(+), 33 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/layers.h b/include/sway/layers.h index 73fb7cb8..22054be1 100644 --- a/include/sway/layers.h +++ b/include/sway/layers.h @@ -21,4 +21,7 @@ struct sway_layer_surface { struct wlr_box geo; }; +struct sway_output; +void arrange_layers(struct sway_output *output); + #endif diff --git a/include/sway/output.h b/include/sway/output.h index 769d44d0..44d009d1 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -4,6 +4,7 @@ #include #include #include +#include struct sway_server; struct sway_container; @@ -18,7 +19,10 @@ struct sway_output { struct wlr_box usable_area; struct wl_listener frame; - struct wl_listener output_destroy; + struct wl_listener destroy; + struct wl_listener mode; + + pid_t bg_pid; }; #endif diff --git a/sway/commands/output.c b/sway/commands/output.c index e747eb4e..35bc8099 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -188,7 +188,7 @@ static struct cmd_results *cmd_output_background(struct output_config *output, } wordexp_t p; - char *src = join_args(argv + *i - 1, j); + char *src = join_args(argv + *i, j); if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { return cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src); diff --git a/sway/config/output.c b/sway/config/output.c index 69e883f1..c4168b4f 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -1,9 +1,12 @@ #define _XOPEN_SOURCE 700 +#include #include #include -#include +#include +#include #include #include +#include #include "sway/config.h" #include "sway/output.h" #include "log.h" @@ -107,6 +110,16 @@ static void set_mode(struct wlr_output *output, int width, int height, } } +void terminate_swaybg(pid_t pid) { + int ret = kill(pid, SIGTERM); + if (ret != 0) { + wlr_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } +} + void apply_output_config(struct output_config *oc, swayc_t *output) { assert(output->type == C_OUTPUT); @@ -160,12 +173,12 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } if (oc && oc->background) { - // TODO swaybg - /*if (output->bg_pid != 0) { - terminate_swaybg(output->bg_pid); + if (output->sway_output->bg_pid != 0) { + terminate_swaybg(output->sway_output->bg_pid); } - wlr_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); + wlr_log(L_DEBUG, "Setting background for output %d to %s", + output_i, oc->background); size_t bufsize = 12; char output_id[bufsize]; @@ -173,17 +186,17 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { output_id[bufsize-1] = 0; char *const cmd[] = { - "swaybg", + "./swaybg/swaybg", output_id, oc->background, oc->background_option, NULL, }; - output->bg_pid = fork(); - if (output->bg_pid == 0) { + output->sway_output->bg_pid = fork(); + if (output->sway_output->bg_pid == 0) { execvp(cmd[0], cmd); - }*/ + } } } diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index 3cf171bd..a2506d21 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -1,40 +1,261 @@ +#include #include +#include #include +#include +#include +#include #include #include "sway/layers.h" +#include "sway/layout.h" #include "sway/output.h" #include "sway/server.h" -static void arrange_layers(struct sway_output *output) { - // TODO +static void apply_exclusive(struct wlr_box *usable_area, + uint32_t anchor, int32_t exclusive, + int32_t margin_top, int32_t margin_right, + int32_t margin_bottom, int32_t margin_left) { + if (exclusive <= 0) { + return; + } + struct { + uint32_t anchors; + int *positive_axis; + int *negative_axis; + int margin; + } edges[] = { + { + .anchors = + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, + .positive_axis = &usable_area->y, + .negative_axis = &usable_area->height, + .margin = margin_top, + }, + { + .anchors = + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, + .positive_axis = NULL, + .negative_axis = &usable_area->height, + .margin = margin_bottom, + }, + { + .anchors = + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, + .positive_axis = &usable_area->x, + .negative_axis = &usable_area->width, + .margin = margin_left, + }, + { + .anchors = + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, + .positive_axis = NULL, + .negative_axis = &usable_area->width, + .margin = margin_right, + }, + }; + for (size_t i = 0; i < sizeof(edges) / sizeof(edges[0]); ++i) { + if ((anchor & edges[i].anchors) == edges[i].anchors) { + if (edges[i].positive_axis) { + *edges[i].positive_axis += exclusive + edges[i].margin; + } + if (edges[i].negative_axis) { + *edges[i].negative_axis -= exclusive + edges[i].margin; + } + } + } +} + +static void arrange_layer(struct sway_output *output, struct wl_list *list, + struct wlr_box *usable_area, bool exclusive) { + struct sway_layer_surface *sway_layer; + struct wlr_box full_area = { 0 }; + wlr_output_effective_resolution(output->wlr_output, + &full_area.width, &full_area.height); + wl_list_for_each(sway_layer, list, link) { + struct wlr_layer_surface *layer = sway_layer->layer_surface; + struct wlr_layer_surface_state *state = &layer->current; + if (exclusive != (state->exclusive_zone > 0)) { + continue; + } + struct wlr_box bounds; + if (state->exclusive_zone == -1) { + bounds = full_area; + } else { + bounds = *usable_area; + } + struct wlr_box box = { + .width = state->desired_width, + .height = state->desired_height + }; + // Horizontal axis + const uint32_t both_horiz = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + if ((state->anchor & both_horiz) && box.width == 0) { + box.x = bounds.x; + box.width = bounds.width; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) { + box.x = bounds.x; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) { + box.x = bounds.x + (bounds.width - box.width); + } else { + box.x = bounds.x + ((bounds.width / 2) - (box.width / 2)); + } + // Vertical axis + const uint32_t both_vert = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + if ((state->anchor & both_vert) && box.height == 0) { + box.y = bounds.y; + box.height = bounds.height; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) { + box.y = bounds.y; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) { + box.y = bounds.y + (bounds.height - box.height); + } else { + box.y = bounds.y + ((bounds.height / 2) - (box.height / 2)); + } + // Margin + if ((state->anchor & both_horiz) == both_horiz) { + box.x += state->margin.left; + box.width -= state->margin.left + state->margin.right; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) { + box.x += state->margin.left; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) { + box.x -= state->margin.right; + } + if ((state->anchor & both_vert) == both_vert) { + box.y += state->margin.top; + box.height -= state->margin.top + state->margin.bottom; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) { + box.y += state->margin.top; + } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) { + box.y -= state->margin.bottom; + } + if (box.width < 0 || box.height < 0) { + // TODO: Bubble up a protocol error? + wlr_layer_surface_close(layer); + continue; + } + // Apply + sway_layer->geo = box; + apply_exclusive(usable_area, state->anchor, state->exclusive_zone, + state->margin.top, state->margin.right, + state->margin.bottom, state->margin.left); + wlr_layer_surface_configure(layer, box.width, box.height); + } +} + +void arrange_layers(struct sway_output *output) { + struct wlr_box usable_area = { 0 }; + wlr_output_effective_resolution(output->wlr_output, + &usable_area.width, &usable_area.height); + struct wlr_box usable_area_before = output->usable_area; + + // Arrange exclusive surfaces from top->bottom + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY], + &usable_area, true); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP], + &usable_area, true); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM], + &usable_area, true); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND], + &usable_area, true); + memcpy(&output->usable_area, &usable_area, sizeof(struct wlr_box)); + + if (memcmp(&usable_area_before, + &usable_area, sizeof(struct wlr_box)) != 0) { + wlr_log(L_DEBUG, "arrange"); + arrange_windows(output->swayc, -1, -1); + } + + // Arrange non-exlusive surfaces from top->bottom + usable_area.x = usable_area.y = 0; + wlr_output_effective_resolution(output->wlr_output, + &usable_area.width, &usable_area.height); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY], + &usable_area, false); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP], + &usable_area, false); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM], + &usable_area, false); + arrange_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND], + &usable_area, false); } static void handle_output_destroy(struct wl_listener *listener, void *data) { - // TODO + struct sway_layer_surface *sway_layer = + wl_container_of(listener, sway_layer, output_destroy); + sway_layer->layer_surface->output = NULL; + wl_list_remove(&sway_layer->output_destroy.link); + wl_list_remove(&sway_layer->output_mode.link); + wlr_layer_surface_close(sway_layer->layer_surface); } static void handle_output_mode(struct wl_listener *listener, void *data) { - // TODO + struct wlr_output *output = data; + arrange_layers((struct sway_output *)output->data); } static void handle_output_transform(struct wl_listener *listener, void *data) { - // TODO + struct wlr_output *output = data; + arrange_layers((struct sway_output *)output->data); } static void handle_surface_commit(struct wl_listener *listener, void *data) { - // TODO + struct sway_layer_surface *layer = + wl_container_of(listener, layer, surface_commit); + struct wlr_layer_surface *layer_surface = layer->layer_surface; + struct wlr_output *wlr_output = layer_surface->output; + if (wlr_output != NULL) { + struct sway_output *output = wlr_output->data; + struct wlr_box old_geo = layer->geo; + arrange_layers(output); + if (memcmp(&old_geo, &layer->geo, sizeof(struct wlr_box)) != 0) { + // TODO DAMAGE apply whole surface from previous and new geos + } else { + // TODO DAMAGE from surface damage + } + } +} + +static void unmap(struct wlr_layer_surface *layer_surface) { + // TODO DAMAGE } static void handle_destroy(struct wl_listener *listener, void *data) { - // TODO + struct sway_layer_surface *sway_layer = wl_container_of( + listener, sway_layer, destroy); + if (sway_layer->layer_surface->mapped) { + unmap(sway_layer->layer_surface); + } + wl_list_remove(&sway_layer->link); + wl_list_remove(&sway_layer->destroy.link); + wl_list_remove(&sway_layer->map.link); + wl_list_remove(&sway_layer->unmap.link); + wl_list_remove(&sway_layer->surface_commit.link); + wl_list_remove(&sway_layer->output_destroy.link); + wl_list_remove(&sway_layer->output_mode.link); + wl_list_remove(&sway_layer->output_transform.link); + struct sway_output *output = sway_layer->layer_surface->output->data; + arrange_layers(output); + free(sway_layer); } static void handle_map(struct wl_listener *listener, void *data) { - // TODO + // TODO DAMAGE } static void handle_unmap(struct wl_listener *listener, void *data) { - // TODO + struct sway_layer_surface *sway_layer = wl_container_of( + listener, sway_layer, unmap); + unmap(sway_layer->layer_surface); } void handle_layer_shell_surface(struct wl_listener *listener, void *data) { diff --git a/sway/desktop/output.c b/sway/desktop/output.c index a9aa47a6..59f79a81 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -4,14 +4,17 @@ #include #include #include +#include #include #include +#include #include #include #include "log.h" #include "sway/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" +#include "sway/layers.h" #include "sway/layout.h" #include "sway/output.h" #include "sway/server.h" @@ -177,6 +180,20 @@ static void output_frame_view(swayc_t *view, void *data) { } } +static void render_layer(struct sway_output *output, + const struct wlr_box *output_layout_box, + struct timespec *when, + struct wl_list *layer) { + struct sway_layer_surface *sway_layer; + wl_list_for_each(sway_layer, layer, link) { + struct wlr_layer_surface *layer = sway_layer->layer_surface; + render_surface(layer->surface, output->wlr_output, when, + sway_layer->geo.x + output_layout_box->x, + sway_layer->geo.y + output_layout_box->y, 0); + wlr_surface_send_frame_done(layer->surface, when); + } +} + static void output_frame_notify(struct wl_listener *listener, void *data) { struct sway_output *soutput = wl_container_of(listener, soutput, frame); struct wlr_output *wlr_output = data; @@ -189,6 +206,18 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { wlr_output_make_current(wlr_output, &buffer_age); wlr_renderer_begin(server->renderer, wlr_output->width, wlr_output->height); + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + struct wlr_output_layout *layout = root_container.sway_root->output_layout; + const struct wlr_box *output_box = wlr_output_layout_get_box( + layout, wlr_output); + + render_layer(soutput, output_box, &now, + &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]); + render_layer(soutput, output_box, &now, + &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]); + struct sway_seat *seat = input_manager_current_seat(input_manager); swayc_t *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); swayc_t *workspace = (focus->type == C_WORKSPACE ? @@ -210,22 +239,32 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { } } + // TODO: Consider revising this when fullscreen windows are supported + render_layer(soutput, output_box, &now, + &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]); + render_layer(soutput, output_box, &now, + &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); + wlr_renderer_end(server->renderer); wlr_output_swap_buffers(wlr_output, &soutput->last_frame, NULL); - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); soutput->last_frame = now; } static void handle_output_destroy(struct wl_listener *listener, void *data) { - struct sway_output *output = wl_container_of(listener, output, output_destroy); + struct sway_output *output = wl_container_of(listener, output, destroy); struct wlr_output *wlr_output = data; wlr_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); destroy_output(output->swayc); } +static void handle_output_mode(struct wl_listener *listener, void *data) { + struct sway_output *output = wl_container_of(listener, output, mode); + arrange_layers(output); + arrange_windows(output->swayc, -1, -1); +} + void handle_new_output(struct wl_listener *listener, void *data) { struct sway_server *server = wl_container_of(listener, server, new_output); struct wlr_output *wlr_output = data; @@ -260,9 +299,11 @@ void handle_new_output(struct wl_listener *listener, void *data) { wl_signal_add(&wlr_output->events.frame, &output->frame); output->frame.notify = output_frame_notify; + wl_signal_add(&wlr_output->events.destroy, &output->destroy); + output->destroy.notify = handle_output_destroy; + wl_signal_add(&wlr_output->events.mode, &output->mode); + output->mode.notify = handle_output_mode; - wl_signal_add(&wlr_output->events.destroy, &output->output_destroy); - output->output_destroy.notify = handle_output_destroy; - + arrange_layers(output); arrange_windows(&root_container, -1, -1); } diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 6b5e03f9..f9b5242b 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -84,7 +84,7 @@ static void set_activated(struct sway_view *view, bool activated) { wlr_xwayland_surface_activate(surface, activated); } -static void close(struct sway_view *view) { +static void close_view(struct sway_view *view) { if (!assert_xwayland(view)) { return; } @@ -203,7 +203,7 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { sway_view->iface.set_size = set_size; sway_view->iface.set_position = set_position; sway_view->iface.set_activated = set_activated; - sway_view->iface.close = close; + sway_view->iface.close = close_view; sway_view->wlr_xwayland_surface = xsurface; sway_view->sway_xwayland_surface = sway_surface; sway_view->surface = xsurface->surface; diff --git a/sway/tree/container.c b/sway/tree/container.c index 705221d7..bbafe9ec 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -254,7 +254,9 @@ swayc_t *destroy_output(swayc_t *output) { } } - wl_list_remove(&output->sway_output->output_destroy.link); + wl_list_remove(&output->sway_output->frame.link); + wl_list_remove(&output->sway_output->destroy.link); + wl_list_remove(&output->sway_output->mode.link); wlr_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); free_swayc(output); diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 3d04a1a7..de9e7b58 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -204,10 +204,13 @@ void arrange_windows(swayc_t *container, double width, double height) { case C_WORKSPACE: { swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); - container->width = output->width; - container->height = output->height; - container->x = x; - container->y = y; + struct wlr_box *area = &output->sway_output->usable_area; + wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", + area->width, area->height, area->x, area->y); + container->width = area->width; + container->height = area->height; + container->x = x = area->x; + container->y = y = area->y; wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", container->name, container->x, container->y); } -- cgit v1.2.3 From 874f009866abaf8ca43ed4cd88a69d22a3fbfc5a Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 29 Mar 2018 12:15:31 -0400 Subject: move tree includes to their own directory --- include/sway/config.h | 4 +- include/sway/container.h | 169 ------------------------------------------ include/sway/criteria.h | 2 +- include/sway/ipc-json.h | 2 +- include/sway/ipc-server.h | 4 +- include/sway/layout.h | 43 ----------- include/sway/tree/container.h | 136 +++++++++++++++++++++++++++++++++ include/sway/tree/layout.h | 51 +++++++++++++ include/sway/tree/view.h | 116 +++++++++++++++++++++++++++++ include/sway/tree/workspace.h | 26 +++++++ include/sway/view.h | 120 ------------------------------ include/sway/workspace.h | 20 ----- sway/commands/exec_always.c | 4 +- sway/commands/focus.c | 5 +- sway/commands/kill.c | 2 +- sway/commands/layout.c | 4 +- sway/commands/reload.c | 2 +- sway/commands/workspace.c | 2 +- sway/config.c | 2 +- sway/criteria.c | 4 +- sway/desktop/layer_shell.c | 2 +- sway/desktop/output.c | 6 +- sway/desktop/wl_shell.c | 6 +- sway/desktop/xdg_shell_v6.c | 6 +- sway/desktop/xwayland.c | 6 +- sway/input/cursor.c | 2 +- sway/input/seat.c | 4 +- sway/ipc-json.c | 2 +- sway/main.c | 2 +- sway/tree/container.c | 10 +-- sway/tree/layout.c | 6 +- sway/tree/view.c | 7 +- sway/tree/workspace.c | 4 +- 33 files changed, 380 insertions(+), 401 deletions(-) delete mode 100644 include/sway/container.h delete mode 100644 include/sway/layout.h create mode 100644 include/sway/tree/container.h create mode 100644 include/sway/tree/layout.h create mode 100644 include/sway/tree/view.h create mode 100644 include/sway/tree/workspace.h delete mode 100644 include/sway/view.h delete mode 100644 include/sway/workspace.h (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 48a8b0ab..48ebba3b 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -10,8 +10,8 @@ #include #include #include "list.h" -#include "layout.h" -#include "container.h" +#include "tree/layout.h" +#include "tree/container.h" /** * Describes a variable created via the `set` command. diff --git a/include/sway/container.h b/include/sway/container.h deleted file mode 100644 index f200a1a2..00000000 --- a/include/sway/container.h +++ /dev/null @@ -1,169 +0,0 @@ -#ifndef _SWAY_CONTAINER_H -#define _SWAY_CONTAINER_H -#include -#include -#include -#include -#include "list.h" - -typedef struct sway_container swayc_t; - -extern swayc_t root_container; - -struct sway_view; -struct sway_seat; - -/** - * Different kinds of containers. - * - * This enum is in order. A container will never be inside of a container below - * it on this list. - */ -enum swayc_types { - C_ROOT, /**< The root container. Only one of these ever exists. */ - C_OUTPUT, /**< An output (aka monitor, head, etc). */ - C_WORKSPACE, /**< A workspace. */ - C_CONTAINER, /**< A manually created container. */ - C_VIEW, /**< A view (aka window). */ - - C_TYPES, -}; - -/** - * Different ways to arrange a container. - */ -enum swayc_layouts { - L_NONE, /**< Used for containers that have no layout (views, root) */ - L_HORIZ, - L_VERT, - L_STACKED, - L_TABBED, - L_FLOATING, /**< A psuedo-container, removed from the tree, to hold floating windows */ - - /* Awesome/Monad style auto layouts */ - L_AUTO_LEFT, - L_AUTO_RIGHT, - L_AUTO_TOP, - L_AUTO_BOTTOM, - - L_AUTO_FIRST = L_AUTO_LEFT, - L_AUTO_LAST = L_AUTO_BOTTOM, - - // Keep last - L_LAYOUTS, -}; - -enum swayc_border_types { - B_NONE, /**< No border */ - B_PIXEL, /**< 1px border */ - B_NORMAL, /**< Normal border with title bar */ -}; - -struct sway_root; -struct sway_output; -struct sway_view; - -/** - * Stores information about a container. - * - * The tree is made of these. Views are containers that cannot have children. - */ -struct sway_container { - union { - // TODO: Encapsulate state for other node types as well like C_CONTAINER - struct sway_root *sway_root; // C_ROOT - struct sway_output *sway_output; // C_OUTPUT - struct sway_view *sway_view; // C_VIEW - }; - - /** - * A unique ID to identify this container. Primarily used in the - * get_tree JSON output. - */ - size_t id; - - char *name; - - enum swayc_types type; - enum swayc_layouts layout; - enum swayc_layouts prev_layout; - enum swayc_layouts workspace_layout; - - /** - * The coordinates that this view appear at, relative to the output they - * are located on (output containers have absolute coordinates). - */ - double x, y; - - /** - * Width and height of this container, without borders or gaps. - */ - double width, height; - - list_t *children; - - /** - * The parent of this container. NULL for the root container. - */ - struct sway_container *parent; - - /** - * Number of master views in auto layouts. - */ - size_t nb_master; - - /** - * Number of slave groups (e.g. columns) in auto layouts. - */ - size_t nb_slave_groups; - - /** - * Marks applied to the container, list_t of char*. - */ - list_t *marks; - - struct { - struct wl_signal destroy; - } events; -}; - -void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, - void (*func)(swayc_t *item, void *data), void *data); - -swayc_t *new_output(struct sway_output *sway_output); -swayc_t *new_workspace(swayc_t *output, const char *name); -swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); - -swayc_t *destroy_output(swayc_t *output); -swayc_t *destroy_view(swayc_t *view); - -swayc_t *next_view_sibling(struct sway_seat *seat); - -/** - * Finds a container based on test criteria. Returns the first container that - * passes the test. - */ -swayc_t *swayc_by_test(swayc_t *container, - bool (*test)(swayc_t *view, void *data), void *data); -/** - * Finds a parent container with the given swayc_type. - */ -swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); -/** - * Maps a container's children over a function. - */ -void container_map(swayc_t *container, - void (*f)(swayc_t *view, void *data), void *data); - -swayc_t *swayc_at(swayc_t *parent, double lx, double ly, - struct wlr_surface **surface, double *sx, double *sy); - -/** - * Apply the function for each child of the container breadth first. - */ -void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), - void *data); - -swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); - -#endif diff --git a/include/sway/criteria.h b/include/sway/criteria.h index 9b4b4bef..431cfa3a 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -1,7 +1,7 @@ #ifndef _SWAY_CRITERIA_H #define _SWAY_CRITERIA_H -#include "container.h" +#include "tree/container.h" #include "list.h" /** diff --git a/include/sway/ipc-json.h b/include/sway/ipc-json.h index eef5a018..76b7d45b 100644 --- a/include/sway/ipc-json.h +++ b/include/sway/ipc-json.h @@ -1,7 +1,7 @@ #ifndef _SWAY_IPC_JSON_H #define _SWAY_IPC_JSON_H #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" json_object *ipc_json_get_version(); diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index bcf1c433..db690b6e 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h @@ -1,13 +1,15 @@ #ifndef _SWAY_IPC_SERVER_H #define _SWAY_IPC_SERVER_H #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "ipc.h" struct sway_server; void ipc_init(struct sway_server *server); + void ipc_terminate(void); + struct sockaddr_un *ipc_user_sockaddr(void); void ipc_event_window(swayc_t *window, const char *change); diff --git a/include/sway/layout.h b/include/sway/layout.h deleted file mode 100644 index e82c4442..00000000 --- a/include/sway/layout.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef _SWAY_LAYOUT_H -#define _SWAY_LAYOUT_H - -#include -#include "sway/container.h" - -enum movement_direction { - MOVE_LEFT, - MOVE_RIGHT, - MOVE_UP, - MOVE_DOWN, - MOVE_PARENT, - MOVE_CHILD, - MOVE_NEXT, - MOVE_PREV, - MOVE_FIRST -}; - -struct sway_container; - -struct sway_root { - struct wlr_output_layout *output_layout; - - struct wl_listener output_layout_change; - - struct wl_list unmanaged_views; // sway_view::unmanaged_view_link - - struct { - struct wl_signal new_container; - } events; -}; - -void init_layout(void); -void add_child(struct sway_container *parent, struct sway_container *child); -swayc_t *add_sibling(swayc_t *parent, swayc_t *child); -struct sway_container *remove_child(struct sway_container *child); -enum swayc_layouts default_layout(struct sway_container *output); -void sort_workspaces(struct sway_container *output); -void arrange_windows(struct sway_container *container, double width, double height); -swayc_t *get_swayc_in_direction(swayc_t *container, - struct sway_seat *seat, enum movement_direction dir); - -#endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h new file mode 100644 index 00000000..5a2ae349 --- /dev/null +++ b/include/sway/tree/container.h @@ -0,0 +1,136 @@ +#ifndef _SWAY_CONTAINER_H +#define _SWAY_CONTAINER_H +#include +#include +#include +#include +#include "list.h" + +typedef struct sway_container swayc_t; + +extern swayc_t root_container; + +struct sway_view; +struct sway_seat; + +/** + * Different kinds of containers. + * + * This enum is in order. A container will never be inside of a container below + * it on this list. + */ +enum swayc_types { + C_ROOT, + C_OUTPUT, + C_WORKSPACE, + C_CONTAINER, + C_VIEW, + + C_TYPES, +}; + +enum swayc_layouts { + L_NONE, + L_HORIZ, + L_VERT, + L_STACKED, + L_TABBED, + L_FLOATING, + + // Keep last + L_LAYOUTS, +}; + +enum swayc_border_types { + B_NONE, + B_PIXEL, + B_NORMAL, +}; + +struct sway_root; +struct sway_output; +struct sway_view; + +struct sway_container { + union { + // TODO: Encapsulate state for other node types as well like C_CONTAINER + struct sway_root *sway_root; + struct sway_output *sway_output; + struct sway_view *sway_view; + }; + + /** + * A unique ID to identify this container. Primarily used in the + * get_tree JSON output. + */ + size_t id; + + char *name; + + enum swayc_types type; + enum swayc_layouts layout; + enum swayc_layouts prev_layout; + enum swayc_layouts workspace_layout; + + // TODO convert to layout coordinates + double x, y; + + // does not include borders or gaps. + double width, height; + + list_t *children; + + struct sway_container *parent; + + list_t *marks; // list of char* + + struct { + struct wl_signal destroy; + } events; +}; + +void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, + void (*func)(swayc_t *item, void *data), void *data); + +// TODO only one container create function and pass the type? +swayc_t *new_output(struct sway_output *sway_output); + +swayc_t *new_workspace(swayc_t *output, const char *name); + +swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); + +swayc_t *destroy_output(swayc_t *output); +swayc_t *destroy_view(swayc_t *view); + +swayc_t *next_view_sibling(struct sway_seat *seat); + +/** + * Finds a container based on test criteria. Returns the first container that + * passes the test. + */ +swayc_t *swayc_by_test(swayc_t *container, + bool (*test)(swayc_t *view, void *data), void *data); + +/** + * Finds a parent container with the given swayc_type. + */ +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); + +/** + * Maps a container's children over a function. + */ +void container_map(swayc_t *container, + void (*f)(swayc_t *view, void *data), void *data); + +swayc_t *swayc_at(swayc_t *parent, double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy); + +/** + * Apply the function for each child of the container breadth first. + */ +void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), + void *data); + +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); + +#endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h new file mode 100644 index 00000000..39b7fb24 --- /dev/null +++ b/include/sway/tree/layout.h @@ -0,0 +1,51 @@ +#ifndef _SWAY_LAYOUT_H +#define _SWAY_LAYOUT_H + +#include +#include "sway/tree/container.h" + +enum movement_direction { + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN, + MOVE_PARENT, + MOVE_CHILD, + MOVE_NEXT, + MOVE_PREV, + MOVE_FIRST +}; + +struct sway_container; + +struct sway_root { + struct wlr_output_layout *output_layout; + + struct wl_listener output_layout_change; + + struct wl_list unmanaged_views; // sway_view::unmanaged_view_link + + struct { + struct wl_signal new_container; + } events; +}; + +void init_layout(void); + +void add_child(struct sway_container *parent, struct sway_container *child); + +swayc_t *add_sibling(swayc_t *parent, swayc_t *child); + +struct sway_container *remove_child(struct sway_container *child); + +enum swayc_layouts default_layout(struct sway_container *output); + +void sort_workspaces(struct sway_container *output); + +void arrange_windows(struct sway_container *container, + double width, double height); + +swayc_t *get_swayc_in_direction(swayc_t *container, + struct sway_seat *seat, enum movement_direction dir); + +#endif diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h new file mode 100644 index 00000000..e5f53f4e --- /dev/null +++ b/include/sway/tree/view.h @@ -0,0 +1,116 @@ +#ifndef _SWAY_VIEW_H +#define _SWAY_VIEW_H +#include +#include +#include +#include + +struct sway_container; +struct sway_view; + +struct sway_xdg_surface_v6 { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +struct sway_xwayland_surface { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener request_configure; + struct wl_listener unmap_notify; + struct wl_listener map_notify; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +struct sway_wl_shell_surface { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +enum sway_view_type { + SWAY_WL_SHELL_VIEW, + SWAY_XDG_SHELL_V6_VIEW, + SWAY_XWAYLAND_VIEW, + // Keep last + SWAY_VIEW_TYPES, +}; + +enum sway_view_prop { + VIEW_PROP_TITLE, + VIEW_PROP_APP_ID, + VIEW_PROP_CLASS, + VIEW_PROP_INSTANCE, +}; + +struct sway_view { + enum sway_view_type type; + struct sway_container *swayc; + struct wlr_surface *surface; + int width, height; + + union { + struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; + struct wlr_xwayland_surface *wlr_xwayland_surface; + struct wlr_wl_shell_surface *wlr_wl_shell_surface; + }; + + union { + struct sway_xdg_surface_v6 *sway_xdg_surface_v6; + struct sway_xwayland_surface *sway_xwayland_surface; + struct sway_wl_shell_surface *sway_wl_shell_surface; + }; + + struct { + const char *(*get_prop)(struct sway_view *view, + enum sway_view_prop prop); + void (*set_size)(struct sway_view *view, + int width, int height); + void (*set_position)(struct sway_view *view, + double ox, double oy); + void (*set_activated)(struct sway_view *view, bool activated); + void (*close)(struct sway_view *view); + } iface; + + // only used for unmanaged views (shell specific) + struct wl_list unmanaged_view_link; // sway_root::unmanaged views +}; + +const char *view_get_title(struct sway_view *view); + +const char *view_get_app_id(struct sway_view *view); + +const char *view_get_class(struct sway_view *view); + +const char *view_get_instance(struct sway_view *view); + +void view_set_size(struct sway_view *view, int width, int height); + +void view_set_position(struct sway_view *view, double ox, double oy); + +void view_set_activated(struct sway_view *view, bool activated); + +void view_close(struct sway_view *view); + +void view_update_outputs(struct sway_view *view, const struct wlr_box *before); + +#endif diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h new file mode 100644 index 00000000..c8ce40d1 --- /dev/null +++ b/include/sway/tree/workspace.h @@ -0,0 +1,26 @@ +#ifndef _SWAY_WORKSPACE_H +#define _SWAY_WORKSPACE_H + +#include "sway/tree/container.h" + +extern char *prev_workspace_name; + +char *workspace_next_name(const char *output_name); + +swayc_t *workspace_create(const char *name); + +bool workspace_switch(swayc_t *workspace); + +struct sway_container *workspace_by_number(const char* name); + +swayc_t *workspace_by_name(const char*); + +struct sway_container *workspace_output_next(swayc_t *current); + +struct sway_container *workspace_next(swayc_t *current); + +struct sway_container *workspace_output_prev(swayc_t *current); + +struct sway_container *workspace_prev(swayc_t *current); + +#endif diff --git a/include/sway/view.h b/include/sway/view.h deleted file mode 100644 index b2886211..00000000 --- a/include/sway/view.h +++ /dev/null @@ -1,120 +0,0 @@ -#ifndef _SWAY_VIEW_H -#define _SWAY_VIEW_H -#include -#include -#include -#include - -struct sway_container; -struct sway_view; - -struct sway_xdg_surface_v6 { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -struct sway_xwayland_surface { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener request_configure; - struct wl_listener unmap_notify; - struct wl_listener map_notify; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -struct sway_wl_shell_surface { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -enum sway_view_type { - SWAY_WL_SHELL_VIEW, - SWAY_XDG_SHELL_V6_VIEW, - SWAY_XWAYLAND_VIEW, - // Keep last - SWAY_VIEW_TYPES, -}; - -enum sway_view_prop { - VIEW_PROP_TITLE, - VIEW_PROP_APP_ID, - VIEW_PROP_CLASS, - VIEW_PROP_INSTANCE, -}; - -/** - * sway_view is a state container for surfaces that are arranged in the sway - * tree (shell surfaces). - */ -struct sway_view { - enum sway_view_type type; - struct sway_container *swayc; - struct wlr_surface *surface; - int width, height; - - union { - struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; - struct wlr_xwayland_surface *wlr_xwayland_surface; - struct wlr_wl_shell_surface *wlr_wl_shell_surface; - }; - - union { - struct sway_xdg_surface_v6 *sway_xdg_surface_v6; - struct sway_xwayland_surface *sway_xwayland_surface; - struct sway_wl_shell_surface *sway_wl_shell_surface; - }; - - struct { - const char *(*get_prop)(struct sway_view *view, - enum sway_view_prop prop); - void (*set_size)(struct sway_view *view, - int width, int height); - void (*set_position)(struct sway_view *view, - double ox, double oy); - void (*set_activated)(struct sway_view *view, bool activated); - void (*close)(struct sway_view *view); - } iface; - - // only used for unmanaged views (shell specific) - struct wl_list unmanaged_view_link; // sway_root::unmanaged views -}; - -const char *view_get_title(struct sway_view *view); - -const char *view_get_app_id(struct sway_view *view); - -const char *view_get_class(struct sway_view *view); - -const char *view_get_instance(struct sway_view *view); - -void view_set_size(struct sway_view *view, int width, int height); - -void view_set_position(struct sway_view *view, double ox, double oy); - -void view_set_activated(struct sway_view *view, bool activated); - -void view_close(struct sway_view *view); - -void view_update_outputs(struct sway_view *view, const struct wlr_box *before); - -#endif diff --git a/include/sway/workspace.h b/include/sway/workspace.h deleted file mode 100644 index fee54255..00000000 --- a/include/sway/workspace.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef _SWAY_WORKSPACE_H -#define _SWAY_WORKSPACE_H - -#include "sway/container.h" - -extern char *prev_workspace_name; - -char *workspace_next_name(const char *output_name); -swayc_t *workspace_create(const char *name); -bool workspace_switch(swayc_t *workspace); - -struct sway_container *workspace_by_number(const char* name); -swayc_t *workspace_by_name(const char*); - -struct sway_container *workspace_output_next(swayc_t *current); -struct sway_container *workspace_next(swayc_t *current); -struct sway_container *workspace_output_prev(swayc_t *current); -struct sway_container *workspace_prev(swayc_t *current); - -#endif diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index 61870c51..954950e7 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -6,8 +6,8 @@ #include #include "sway/commands.h" #include "sway/config.h" -#include "sway/container.h" -#include "sway/workspace.h" +#include "sway/tree/container.h" +#include "sway/tree/workspace.h" #include "log.h" #include "stringop.h" diff --git a/sway/commands/focus.c b/sway/commands/focus.c index f1a8078f..18e9e0bf 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -3,10 +3,11 @@ #include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/commands.h" -static bool parse_movement_direction(const char *name, enum movement_direction *out) { +static bool parse_movement_direction(const char *name, + enum movement_direction *out) { if (strcasecmp(name, "left") == 0) { *out = MOVE_LEFT; } else if (strcasecmp(name, "right") == 0) { diff --git a/sway/commands/kill.c b/sway/commands/kill.c index f408ce2a..c0faed7a 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -2,7 +2,7 @@ #include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { diff --git a/sway/commands/layout.c b/sway/commands/layout.c index b0fc5d66..2b193136 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -1,8 +1,8 @@ #include #include #include "sway/commands.h" -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "log.h" struct cmd_results *cmd_layout(int argc, char **argv) { diff --git a/sway/commands/reload.c b/sway/commands/reload.c index d54d40db..8cef789b 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -1,6 +1,6 @@ #include "sway/commands.h" #include "sway/config.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" struct cmd_results *cmd_reload(int argc, char **argv) { struct cmd_results *error = NULL; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index fa891398..8751dffe 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -4,7 +4,7 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/input/seat.h" -#include "sway/workspace.h" +#include "sway/tree/workspace.h" #include "list.h" #include "log.h" #include "stringop.h" diff --git a/sway/config.c b/sway/config.c index 213e7680..0b29735a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -24,7 +24,7 @@ #include "sway/input/seat.h" #include "sway/commands.h" #include "sway/config.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "readline.h" #include "stringop.h" #include "list.h" diff --git a/sway/criteria.c b/sway/criteria.c index 2eee331c..b8b581ed 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -4,9 +4,9 @@ #include #include #include "sway/criteria.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/config.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "stringop.h" #include "list.h" #include "log.h" diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index bd62f84a..137b3260 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -7,7 +7,7 @@ #include #include #include "sway/layers.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/server.h" diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 9e7fbcc6..debda396 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -11,14 +11,14 @@ #include #include #include "log.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/layers.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" /** * Rotate a child's position relative to a parent. The parent size is (pw, ph), diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index 0356aa81..bb97fad4 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" #include "log.h" diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 7bc17149..25ffacbb 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" #include "log.h" diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index f9b5242b..7f66f746 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -5,10 +5,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/output.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 8a0d1df5..c0e14265 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -7,7 +7,7 @@ #include #include #include "sway/input/cursor.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "list.h" #include "log.h" diff --git a/sway/input/seat.c b/sway/input/seat.c index 648e7914..56b39766 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,13 +1,13 @@ #define _XOPEN_SOURCE 700 #include #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/seat.h" #include "sway/input/cursor.h" #include "sway/input/input-manager.h" #include "sway/input/keyboard.h" #include "sway/output.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "log.h" static void seat_device_destroy(struct sway_seat_device *seat_device) { diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 977f1ecb..ebd5e43a 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -3,7 +3,7 @@ #include #include "log.h" #include "sway/ipc-json.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/output.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" diff --git a/sway/main.c b/sway/main.c index f2f24be3..31bf617b 100644 --- a/sway/main.c +++ b/sway/main.c @@ -18,7 +18,7 @@ #include #include "sway/config.h" #include "sway/server.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/ipc-server.h" #include "ipc-client.h" #include "readline.h" diff --git a/sway/tree/container.c b/sway/tree/container.c index bbafe9ec..805d5644 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -7,14 +7,14 @@ #include #include #include "sway/config.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/view.h" -#include "sway/workspace.h" +#include "sway/tree/view.h" +#include "sway/tree/workspace.h" #include "sway/ipc-server.h" #include "log.h" @@ -82,8 +82,6 @@ static swayc_t *new_swayc(enum swayc_types type) { c->layout = L_NONE; c->workspace_layout = L_NONE; c->type = type; - c->nb_master = 1; - c->nb_slave_groups = 1; if (type != C_VIEW) { c->children = create_list(); } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index de9e7b58..5a15f3a2 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -6,10 +6,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/output.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/input/seat.h" #include "list.h" #include "log.h" diff --git a/sway/tree/view.c b/sway/tree/view.c index 9499adca..20e657a2 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -1,8 +1,8 @@ #include #include -#include "sway/container.h" -#include "sway/layout.h" -#include "sway/view.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" +#include "sway/tree/view.h" const char *view_get_title(struct sway_view *view) { if (view->iface.get_prop) { @@ -45,6 +45,7 @@ void view_set_size(struct sway_view *view, int width, int height) { } } +// TODO make view coordinates void view_set_position(struct sway_view *view, double ox, double oy) { if (view->iface.set_position) { struct wlr_box box = { diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 861fda4d..3da3fde6 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/workspace.h" +#include "sway/tree/workspace.h" #include "log.h" #include "util.h" -- cgit v1.2.3 From b90099b4b7df8068446c658ab99b58ff83648954 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 29 Mar 2018 16:17:55 -0400 Subject: rename container functions --- include/sway/config.h | 8 +- include/sway/input/seat.h | 2 +- include/sway/tree/container.h | 70 +++++++++------- include/sway/tree/layout.h | 2 +- sway/commands/focus.c | 4 +- sway/commands/kill.c | 2 +- sway/commands/layout.c | 4 +- sway/commands/output.c | 2 +- sway/commands/workspace.c | 12 +-- sway/config/output.c | 4 +- sway/criteria.c | 12 +-- sway/desktop/output.c | 14 ++-- sway/desktop/wl_shell.c | 6 +- sway/desktop/xdg_shell_v6.c | 6 +- sway/desktop/xwayland.c | 18 ++--- sway/input/cursor.c | 8 +- sway/input/seat.c | 34 ++++---- sway/ipc-json.c | 14 ++-- sway/ipc-server.c | 4 +- sway/tree/container.c | 182 +++++++++++++++++++++--------------------- sway/tree/layout.c | 86 ++++++++++---------- sway/tree/workspace.c | 70 ++++++++-------- swaybar/ipc.c | 2 +- 23 files changed, 287 insertions(+), 279 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index e9910be4..7fdd0be0 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -299,8 +299,8 @@ struct sway_config { char *floating_scroll_down_cmd; char *floating_scroll_left_cmd; char *floating_scroll_right_cmd; - enum swayc_layouts default_orientation; - enum swayc_layouts default_layout; + enum sway_container_layout default_orientation; + enum sway_container_layout default_layout; char *font; int font_height; @@ -324,8 +324,8 @@ struct sway_config { list_t *config_chain; const char *current_config; - enum swayc_border_types border; - enum swayc_border_types floating_border; + enum sway_container_border border; + enum sway_container_border floating_border; int border_thickness; int floating_border_thickness; enum edge_border_types hide_edge_borders; diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index e43e6fd4..496bfd5d 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -71,7 +71,7 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container); struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum swayc_types type); + enum sway_container_type type); void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 5def5e71..0dfed455 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -17,7 +17,7 @@ struct sway_seat; * This enum is in order. A container will never be inside of a container below * it on this list. */ -enum swayc_types { +enum sway_container_type { C_ROOT, C_OUTPUT, C_WORKSPACE, @@ -27,7 +27,7 @@ enum swayc_types { C_TYPES, }; -enum swayc_layouts { +enum sway_container_layout { L_NONE, L_HORIZ, L_VERT, @@ -39,7 +39,7 @@ enum swayc_layouts { L_LAYOUTS, }; -enum swayc_border_types { +enum sway_container_border { B_NONE, B_PIXEL, B_NORMAL, @@ -65,10 +65,10 @@ struct sway_container { char *name; - enum swayc_types type; - enum swayc_layouts layout; - enum swayc_layouts prev_layout; - enum swayc_layouts workspace_layout; + enum sway_container_type type; + enum sway_container_layout layout; + enum sway_container_layout prev_layout; + enum sway_container_layout workspace_layout; // TODO convert to layout coordinates double x, y; @@ -87,53 +87,61 @@ struct sway_container { } events; }; -void swayc_descendants_of_type(struct sway_container *root, - enum swayc_types type, - void (*func)(struct sway_container *item, void *data), void *data); - // TODO only one container create function and pass the type? -struct sway_container *new_output(struct sway_output *sway_output); +struct sway_container *sway_container_output_create( + struct sway_output *sway_output); + +struct sway_container *sway_container_workspace_create( + struct sway_container *output, const char *name); -struct sway_container *new_workspace(struct sway_container *output, - const char *name); +struct sway_container *sway_container_view_create( + struct sway_container *sibling, struct sway_view *sway_view); -struct sway_container *new_view(struct sway_container *sibling, - struct sway_view *sway_view); +struct sway_container *sway_container_output_destroy( + struct sway_container *output); -struct sway_container *destroy_output(struct sway_container *output); -struct sway_container *destroy_view(struct sway_container *view); +struct sway_container *sway_container_view_destroy(struct sway_container *view); +struct sway_container *sway_container_set_layout( + struct sway_container *container, enum sway_container_layout layout); + +void sway_container_descendents(struct sway_container *root, + enum sway_container_type type, + void (*func)(struct sway_container *item, void *data), void *data); + +// XXX: what is this? struct sway_container *next_view_sibling(struct sway_seat *seat); /** * Finds a container based on test criteria. Returns the first container that * passes the test. */ -struct sway_container *swayc_by_test(struct sway_container *container, +struct sway_container *sway_container_find(struct sway_container *container, bool (*test)(struct sway_container *view, void *data), void *data); /** - * Finds a parent container with the given swayc_type. + * Finds a parent container with the given struct sway_containerype. */ -struct sway_container *swayc_parent_by_type(struct sway_container *container, - enum swayc_types type); +struct sway_container *sway_container_parent(struct sway_container *container, + enum sway_container_type type); /** - * Maps a container's children over a function. + * Run a function for each child. */ -void container_map(struct sway_container *container, +void sway_container_for_each(struct sway_container *container, void (*f)(struct sway_container *view, void *data), void *data); -struct sway_container *swayc_at(struct sway_container *parent, double lx, - double ly, struct wlr_surface **surface, double *sx, double *sy); +/** + * Find a container at the given coordinates. + */ +struct sway_container *sway_container_at(struct sway_container *parent, + double lx, double ly, struct wlr_surface **surface, + double *sx, double *sy); /** * Apply the function for each child of the container breadth first. */ -void container_for_each_bfs(struct sway_container *con, void (*f)(struct - sway_container *con, void *data), void *data); - -struct sway_container *swayc_change_layout(struct sway_container *container, - enum swayc_layouts layout); +void sway_container_for_each_bfs(struct sway_container *container, + void (*f)(struct sway_container *container, void *data), void *data); #endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 8bb9e075..f73b3880 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -39,7 +39,7 @@ struct sway_container *add_sibling(struct sway_container *parent, struct sway_container *remove_child(struct sway_container *child); -enum swayc_layouts default_layout(struct sway_container *output); +enum sway_container_layout default_layout(struct sway_container *output); void sort_workspaces(struct sway_container *output); diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 18e9e0bf..64b05904 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -32,7 +32,7 @@ static bool parse_movement_direction(const char *name, } struct cmd_results *cmd_focus(int argc, char **argv) { - swayc_t *con = config->handler_context.current_container; + struct sway_container *con = config->handler_context.current_container; struct sway_seat *seat = config->handler_context.seat; if (con->type < C_WORKSPACE) { return cmd_results_new(CMD_FAILURE, "focus", @@ -51,7 +51,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { "Expected 'focus ' or 'focus output '"); } - swayc_t *next_focus = get_swayc_in_direction(con, seat, direction); + struct sway_container *next_focus = get_swayc_in_direction(con, seat, direction); if (next_focus) { sway_seat_set_focus(seat, next_focus); } diff --git a/sway/commands/kill.c b/sway/commands/kill.c index c0faed7a..f6774767 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -6,7 +6,7 @@ #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - enum swayc_types type = config->handler_context.current_container->type; + enum sway_container_type type = config->handler_context.current_container->type; if (type != C_VIEW && type != C_CONTAINER) { return cmd_results_new(CMD_INVALID, NULL, "Can only kill views and containers with this command"); diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 2b193136..e10334e2 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -10,7 +10,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { return error; } - swayc_t *parent = config->handler_context.current_container; + struct sway_container *parent = config->handler_context.current_container; // TODO: floating /* @@ -28,7 +28,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if (strcasecmp(argv[0], "default") == 0) { swayc_change_layout(parent, parent->prev_layout); if (parent->layout == L_NONE) { - swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); + struct sway_container *output = sway_container_parent(parent, C_OUTPUT); swayc_change_layout(parent, default_layout(output)); } } else { diff --git a/sway/commands/output.c b/sway/commands/output.c index 35bc8099..f7e3372c 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -296,7 +296,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { char identifier[128]; bool all = strcmp(output->name, "*") == 0; for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *cont = root_container.children->items[i]; + struct sway_container *cont = root_container.children->items[i]; if (cont->type != C_OUTPUT) { continue; } diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 8751dffe..8b7139a9 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -17,15 +17,15 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { int output_location = -1; - swayc_t *current_container = config->handler_context.current_container; - swayc_t *old_workspace = NULL, *old_output = NULL; + struct sway_container *current_container = config->handler_context.current_container; + struct sway_container *old_workspace = NULL, *old_output = NULL; if (current_container) { if (current_container->type == C_WORKSPACE) { old_workspace = current_container; } else { - old_workspace = swayc_parent_by_type(current_container, C_WORKSPACE); + old_workspace = sway_container_parent(current_container, C_WORKSPACE); } - old_output = swayc_parent_by_type(current_container, C_OUTPUT); + old_output = sway_container_parent(current_container, C_OUTPUT); } for (int i = 0; i < argc; ++i) { @@ -57,7 +57,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (config->reading || !config->active) { return cmd_results_new(CMD_DEFER, "workspace", NULL); } - swayc_t *ws = NULL; + struct sway_container *ws = NULL; if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); @@ -92,7 +92,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { workspace_switch(ws); current_container = sway_seat_get_focus(config->handler_context.seat); - swayc_t *new_output = swayc_parent_by_type(current_container, C_OUTPUT); + struct sway_container *new_output = sway_container_parent(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { // TODO: Warp mouse diff --git a/sway/config/output.c b/sway/config/output.c index 9e211861..5763bd21 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -120,14 +120,14 @@ void terminate_swaybg(pid_t pid) { } } -void apply_output_config(struct output_config *oc, swayc_t *output) { +void apply_output_config(struct output_config *oc, struct sway_container *output) { assert(output->type == C_OUTPUT); struct wlr_output *wlr_output = output->sway_output->wlr_output; if (oc && oc->enabled == 0) { wlr_output_layout_remove(root_container.sway_root->output_layout, wlr_output); - destroy_output(output); + sway_container_output_destroy(output); return; } diff --git a/sway/criteria.c b/sway/criteria.c index b8b581ed..70f8e305 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -272,7 +272,7 @@ static int regex_cmp(const char *item, const pcre *regex) { } // test a single view if it matches list of criteria tokens (all of them). -static bool criteria_test(swayc_t *cont, list_t *tokens) { +static bool criteria_test(struct sway_container *cont, list_t *tokens) { if (cont->type != C_VIEW) { return false; } @@ -398,7 +398,7 @@ void free_criteria(struct criteria *crit) { free(crit); } -bool criteria_any(swayc_t *cont, list_t *criteria) { +bool criteria_any(struct sway_container *cont, list_t *criteria) { for (int i = 0; i < criteria->length; i++) { struct criteria *bc = criteria->items[i]; if (criteria_test(cont, bc->tokens)) { @@ -408,7 +408,7 @@ bool criteria_any(swayc_t *cont, list_t *criteria) { return false; } -list_t *criteria_for(swayc_t *cont) { +list_t *criteria_for(struct sway_container *cont) { list_t *criteria = config->criteria, *matches = create_list(); for (int i = 0; i < criteria->length; i++) { struct criteria *bc = criteria->items[i]; @@ -424,7 +424,7 @@ struct list_tokens { list_t *tokens; }; -static void container_match_add(swayc_t *container, +static void container_match_add(struct sway_container *container, struct list_tokens *list_tokens) { if (criteria_test(container, list_tokens->tokens)) { list_add(list_tokens->list, container); @@ -435,8 +435,8 @@ list_t *container_for_crit_tokens(list_t *tokens) { struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; - container_map(&root_container, - (void (*)(swayc_t *, void *))container_match_add, + sway_container_for_each(&root_container, + (void (*)(struct sway_container *, void *))container_match_add, &list_tokens); // TODO look in the scratchpad diff --git a/sway/desktop/output.c b/sway/desktop/output.c index debda396..3e7d8509 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -145,7 +145,7 @@ struct render_data { struct timespec *now; }; -static void output_frame_view(swayc_t *view, void *data) { +static void output_frame_view(struct sway_container *view, void *data) { struct render_data *rdata = data; struct sway_output *output = rdata->output; struct timespec *now = rdata->now; @@ -218,16 +218,16 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); - swayc_t *workspace = (focus->type == C_WORKSPACE ? + struct sway_container *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); + struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : - swayc_parent_by_type(focus, C_WORKSPACE)); + sway_container_parent(focus, C_WORKSPACE)); struct render_data rdata = { .output = soutput, .now = &now, }; - swayc_descendants_of_type(workspace, C_VIEW, output_frame_view, &rdata); + sway_container_descendents(workspace, C_VIEW, output_frame_view, &rdata); // render unmanaged views on top struct sway_view *view; @@ -258,7 +258,7 @@ static void handle_output_destroy(struct wl_listener *listener, void *data) { struct wlr_output *wlr_output = data; wlr_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); - destroy_output(output->swayc); + sway_container_output_destroy(output->swayc); } static void handle_output_mode(struct wl_listener *listener, void *data) { @@ -286,7 +286,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { wlr_output_set_mode(wlr_output, mode); } - output->swayc = new_output(output); + output->swayc = sway_container_output_create(output); if (!output->swayc) { free(output); return; diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index bb97fad4..bf41d7bf 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -74,7 +74,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_surface, destroy); wl_list_remove(&sway_surface->commit.link); wl_list_remove(&sway_surface->destroy.link); - swayc_t *parent = destroy_view(sway_surface->view->swayc); + struct sway_container *parent = sway_container_view_destroy(sway_surface->view->swayc); free(sway_surface->view); free(sway_surface); arrange_windows(parent, -1, -1); @@ -132,8 +132,8 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { wl_signal_add(&shell_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); - swayc_t *cont = new_view(focus, sway_view); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *cont = sway_container_view_create(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 25ffacbb..6b50d470 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -83,7 +83,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_xdg_surface, destroy); wl_list_remove(&sway_xdg_surface->commit.link); wl_list_remove(&sway_xdg_surface->destroy.link); - swayc_t *parent = destroy_view(sway_xdg_surface->view->swayc); + struct sway_container *parent = sway_container_view_destroy(sway_xdg_surface->view->swayc); free(sway_xdg_surface->view); free(sway_xdg_surface); arrange_windows(parent, -1, -1); @@ -136,8 +136,8 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); - swayc_t *cont = new_view(focus, sway_view); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *cont = sway_container_view_create(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 7f66f746..96edab51 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -49,11 +49,11 @@ static void set_position(struct sway_view *view, double ox, double oy) { if (!assert_xwayland(view)) { return; } - swayc_t *output = swayc_parent_by_type(view->swayc, C_OUTPUT); + struct sway_container *output = sway_container_parent(view->swayc, C_OUTPUT); if (!sway_assert(output, "view must be within tree to set position")) { return; } - swayc_t *root = swayc_parent_by_type(output, C_ROOT); + struct sway_container *root = sway_container_parent(output, C_ROOT); if (!sway_assert(root, "output must be within tree to set position")) { return; } @@ -114,7 +114,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { } } - swayc_t *parent = destroy_view(sway_surface->view->swayc); + struct sway_container *parent = sway_container_view_destroy(sway_surface->view->swayc); if (parent) { arrange_windows(parent, -1, -1); } @@ -132,7 +132,7 @@ static void handle_unmap_notify(struct wl_listener *listener, void *data) { } // take it out of the tree - swayc_t *parent = destroy_view(sway_surface->view->swayc); + struct sway_container *parent = sway_container_view_destroy(sway_surface->view->swayc); if (parent) { arrange_windows(parent, -1, -1); } @@ -155,12 +155,12 @@ static void handle_map_notify(struct wl_listener *listener, void *data) { &sway_surface->view->unmanaged_view_link); } else { struct sway_view *view = sway_surface->view; - destroy_view(view->swayc); + sway_container_view_destroy(view->swayc); - swayc_t *parent = root_container.children->items[0]; + struct sway_container *parent = root_container.children->items[0]; parent = parent->children->items[0]; // workspace - swayc_t *cont = new_view(parent, view); + struct sway_container *cont = sway_container_view_create(parent, view); view->swayc = cont; arrange_windows(cont->parent, -1, -1); @@ -238,8 +238,8 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); - swayc_t *cont = new_view(focus, sway_view); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *cont = sway_container_view_create(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/input/cursor.c b/sway/input/cursor.c index c0e14265..a0e4b9be 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -49,8 +49,8 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, } } - swayc_t *swayc = - swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + struct sway_container *swayc = + sway_container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); if (swayc) { wlr_seat_pointer_notify_enter(seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat, time, sx, sy); @@ -87,8 +87,8 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { if (event->button == BTN_LEFT) { struct wlr_surface *surface = NULL; double sx, sy; - swayc_t *swayc = - swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + struct sway_container *swayc = + sway_container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); sway_seat_set_focus(cursor->seat, swayc); } diff --git a/sway/input/seat.c b/sway/input/seat.c index 56b39766..f03a03b4 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -37,7 +37,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, struct sway_seat_container *seat_con = wl_container_of(listener, seat_con, destroy); struct sway_seat *seat = seat_con->seat; - swayc_t *con = seat_con->container; + struct sway_container *con = seat_con->container; bool is_focus = (sway_seat_get_focus(seat) == con); @@ -46,7 +46,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, if (is_focus) { // pick next focus sway_seat_set_focus(seat, NULL); - swayc_t *next = sway_seat_get_focus_inactive(seat, con->parent); + struct sway_container *next = sway_seat_get_focus_inactive(seat, con->parent); if (next == NULL) { next = con->parent; } @@ -59,7 +59,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, } static struct sway_seat_container *seat_container_from_container( - struct sway_seat *seat, swayc_t *con) { + struct sway_seat *seat, struct sway_container *con) { if (con->type < C_WORKSPACE) { // these don't get seat containers ever return NULL; @@ -89,11 +89,11 @@ static struct sway_seat_container *seat_container_from_container( static void handle_new_container(struct wl_listener *listener, void *data) { struct sway_seat *seat = wl_container_of(listener, seat, new_container); - swayc_t *con = data; + struct sway_container *con = data; seat_container_from_container(seat, con); } -static void collect_focus_iter(swayc_t *con, void *data) { +static void collect_focus_iter(struct sway_container *con, void *data) { struct sway_seat *seat = data; if (con->type > C_WORKSPACE) { return; @@ -130,7 +130,7 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, // init the focus stack wl_list_init(&seat->focus_stack); - container_for_each_bfs(&root_container, collect_focus_iter, seat); + sway_container_for_each_bfs(&root_container, collect_focus_iter, seat); wl_signal_add(&root_container.sway_root->events.new_container, &seat->new_container); @@ -166,7 +166,7 @@ static void seat_configure_keyboard(struct sway_seat *seat, sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); - swayc_t *focus = sway_seat_get_focus(seat); + struct sway_container *focus = sway_seat_get_focus(seat); if (focus && focus->type == C_VIEW) { // force notify reenter to pick up the new configuration wlr_seat_keyboard_clear_focus(seat->wlr_seat); @@ -270,7 +270,7 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { } for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *output_container = root_container.children->items[i]; + struct sway_container *output_container = root_container.children->items[i]; struct wlr_output *output = output_container->sway_output->wlr_output; bool result = @@ -289,8 +289,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { seat->cursor->cursor->y); } -void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { - swayc_t *last_focus = sway_seat_get_focus(seat); +void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *container) { + struct sway_container *last_focus = sway_seat_get_focus(seat); if (container && last_focus == container) { return; @@ -330,9 +330,9 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { seat->has_focus = (container != NULL); } -swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container) { +struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { struct sway_seat_container *current = NULL; - swayc_t *parent = NULL; + struct sway_container *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { parent = current->container->parent; @@ -351,21 +351,21 @@ swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container return NULL; } -swayc_t *sway_seat_get_focus(struct sway_seat *seat) { +struct sway_container *sway_seat_get_focus(struct sway_seat *seat) { if (!seat->has_focus) { return NULL; } return sway_seat_get_focus_inactive(seat, &root_container); } -swayc_t *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum swayc_types type) { - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); +struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, + enum sway_container_type type) { + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); if (focus->type == type) { return focus; } - return swayc_parent_by_type(focus, type); + return sway_container_parent(focus, type); } void sway_seat_set_config(struct sway_seat *seat, diff --git a/sway/ipc-json.c b/sway/ipc-json.c index ebd5e43a..7caa2457 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -25,7 +25,7 @@ json_object *ipc_json_get_version() { return version; } -static json_object *ipc_json_create_rect(swayc_t *c) { +static json_object *ipc_json_create_rect(struct sway_container *c) { json_object *rect = json_object_new_object(); json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x)); @@ -36,7 +36,7 @@ static json_object *ipc_json_create_rect(swayc_t *c) { return rect; } -static void ipc_json_describe_root(swayc_t *root, json_object *object) { +static void ipc_json_describe_root(struct sway_container *root, json_object *object) { json_object_object_add(object, "type", json_object_new_string("root")); json_object_object_add(object, "layout", json_object_new_string("splith")); } @@ -63,7 +63,7 @@ static const char *ipc_json_get_output_transform(enum wl_output_transform transf return NULL; } -static void ipc_json_describe_output(swayc_t *container, json_object *object) { +static void ipc_json_describe_output(struct sway_container *container, json_object *object) { struct wlr_output *wlr_output = container->sway_output->wlr_output; json_object_object_add(object, "type", json_object_new_string("output")); json_object_object_add(object, "active", json_object_new_boolean(true)); @@ -94,7 +94,7 @@ static void ipc_json_describe_output(swayc_t *container, json_object *object) { json_object_object_add(object, "modes", modes_array); } -static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) { +static void ipc_json_describe_workspace(struct sway_container *workspace, json_object *object) { int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1; json_object_object_add(object, "num", json_object_new_int(num)); @@ -102,11 +102,11 @@ static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) json_object_object_add(object, "type", json_object_new_string("workspace")); } -static void ipc_json_describe_view(swayc_t *c, json_object *object) { +static void ipc_json_describe_view(struct sway_container *c, json_object *object) { json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); } -json_object *ipc_json_describe_container(swayc_t *c) { +json_object *ipc_json_describe_container(struct sway_container *c) { if (!(sway_assert(c, "Container must not be null."))) { return NULL; } @@ -147,7 +147,7 @@ json_object *ipc_json_describe_container(swayc_t *c) { return object; } -json_object *ipc_json_describe_container_recursive(swayc_t *c) { +json_object *ipc_json_describe_container_recursive(struct sway_container *c) { json_object *object = ipc_json_describe_container(c); int i; diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 156de380..50d0bcf3 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -279,7 +279,7 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event) } } -void ipc_event_window(swayc_t *window, const char *change) { +void ipc_event_window(struct sway_container *window, const char *change) { wlr_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)); @@ -400,7 +400,7 @@ void ipc_client_handle_command(struct ipc_client *client) { { json_object *outputs = json_object_new_array(); for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *container = root_container.children->items[i]; + struct sway_container *container = root_container.children->items[i]; if (container->type == C_OUTPUT) { json_object_array_add(outputs, ipc_json_describe_container(container)); diff --git a/sway/tree/container.c b/sway/tree/container.c index 805d5644..d31966b3 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -33,48 +33,15 @@ static list_t *get_bfs_queue() { return bfs_queue; } -static void notify_new_container(swayc_t *container) { +static void notify_new_container(struct sway_container *container) { wl_signal_emit(&root_container.sway_root->events.new_container, container); ipc_event_window(container, "new"); } -swayc_t *swayc_by_test(swayc_t *container, - bool (*test)(swayc_t *view, void *data), void *data) { - if (!container->children) { - return NULL; - } - // TODO: floating windows - for (int i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - if (test(child, data)) { - return child; - } else { - swayc_t *res = swayc_by_test(child, test, data); - if (res) { - return res; - } - } - } - return NULL; -} - -void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, - void (*func)(swayc_t *item, void *data), void *data) { - for (int i = 0; i < root->children->length; ++i) { - swayc_t *item = root->children->items[i]; - if (item->type == type) { - func(item, data); - } - if (item->children && item->children->length) { - swayc_descendants_of_type(item, type, func, data); - } - } -} - -static swayc_t *new_swayc(enum swayc_types type) { +static struct sway_container *new_swayc(enum sway_container_type type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; - swayc_t *c = calloc(1, sizeof(swayc_t)); + struct sway_container *c = calloc(1, sizeof(struct sway_container)); if (!c) { return NULL; } @@ -91,7 +58,7 @@ static swayc_t *new_swayc(enum swayc_types type) { return c; } -static void free_swayc(swayc_t *cont) { +static void free_swayc(struct sway_container *cont) { if (!sway_assert(cont, "free_swayc passed NULL")) { return; } @@ -119,7 +86,7 @@ static void free_swayc(swayc_t *cont) { free(cont); } -swayc_t *new_output(struct sway_output *sway_output) { +struct sway_container *sway_container_output_create(struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, &size.height); @@ -154,7 +121,7 @@ swayc_t *new_output(struct sway_output *sway_output) { return NULL; } - swayc_t *output = new_swayc(C_OUTPUT); + struct sway_container *output = new_swayc(C_OUTPUT); output->sway_output = sway_output; output->name = strdup(name); if (output->name == NULL) { @@ -169,7 +136,7 @@ swayc_t *new_output(struct sway_output *sway_output) { // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - swayc_t *ws = new_workspace(output, ws_name); + struct sway_container *ws = sway_container_workspace_create(output, ws_name); // Set each seat's focus if not already set struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { @@ -183,12 +150,12 @@ swayc_t *new_output(struct sway_output *sway_output) { return output; } -swayc_t *new_workspace(swayc_t *output, const char *name) { - if (!sway_assert(output, "new_workspace called with null output")) { +struct sway_container *sway_container_workspace_create(struct sway_container *output, const char *name) { + if (!sway_assert(output, "sway_container_workspace_create called with null output")) { return NULL; } wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); - swayc_t *workspace = new_swayc(C_WORKSPACE); + struct sway_container *workspace = new_swayc(C_WORKSPACE); workspace->x = output->x; workspace->y = output->y; @@ -205,12 +172,12 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { return workspace; } -swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { - if (!sway_assert(sibling, "new_view called with NULL sibling/parent")) { +struct sway_container *sway_container_view_create(struct sway_container *sibling, struct sway_view *sway_view) { + if (!sway_assert(sibling, "sway_container_view_create called with NULL sibling/parent")) { return NULL; } const char *title = view_get_title(sway_view); - swayc_t *swayc = new_swayc(C_VIEW); + struct sway_container *swayc = new_swayc(C_VIEW); wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d %s", swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); // Setup values @@ -230,8 +197,8 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { return swayc; } -swayc_t *destroy_output(swayc_t *output) { - if (!sway_assert(output, "null output passed to destroy_output")) { +struct sway_container *sway_container_output_destroy(struct sway_container *output) { + if (!sway_assert(output, "null output passed to sway_container_output_destroy")) { return NULL; } @@ -243,7 +210,7 @@ swayc_t *destroy_output(swayc_t *output) { int p = root_container.children->items[0] == output; // Move workspace from this output to another output while (output->children->length) { - swayc_t *child = output->children->items[0]; + struct sway_container *child = output->children->items[0]; remove_child(child); add_child(root_container.children->items[p], child); } @@ -262,12 +229,12 @@ swayc_t *destroy_output(swayc_t *output) { return &root_container; } -swayc_t *destroy_view(swayc_t *view) { +struct sway_container *sway_container_view_destroy(struct sway_container *view) { if (!view) { return NULL; } wlr_log(L_DEBUG, "Destroying view '%s'", view->name); - swayc_t *parent = view->parent; + struct sway_container *parent = view->parent; free_swayc(view); // TODO WLR: Destroy empty containers @@ -279,7 +246,52 @@ swayc_t *destroy_view(swayc_t *view) { return parent; } -swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { +struct sway_container *swayc_change_layout(struct sway_container *container, enum sway_container_layout layout) { + if (container->type == C_WORKSPACE) { + container->workspace_layout = layout; + if (layout == L_HORIZ || layout == L_VERT) { + container->layout = layout; + } + } else { + container->layout = layout; + } + return container; +} + +void sway_container_descendents(struct sway_container *root, enum sway_container_type type, + void (*func)(struct sway_container *item, void *data), void *data) { + for (int i = 0; i < root->children->length; ++i) { + struct sway_container *item = root->children->items[i]; + if (item->type == type) { + func(item, data); + } + if (item->children && item->children->length) { + sway_container_descendents(item, type, func, data); + } + } +} + +struct sway_container *sway_container_find(struct sway_container *container, + bool (*test)(struct sway_container *view, void *data), void *data) { + if (!container->children) { + return NULL; + } + // TODO: floating windows + for (int i = 0; i < container->children->length; ++i) { + struct sway_container *child = container->children->items[i]; + if (test(child, data)) { + return child; + } else { + struct sway_container *res = sway_container_find(child, test, data); + if (res) { + return res; + } + } + } + return NULL; +} + +struct sway_container *sway_container_parent(struct sway_container *container, enum sway_container_type type) { if (!sway_assert(container, "container is NULL")) { return NULL; } @@ -292,7 +304,29 @@ swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { return container; } -swayc_t *swayc_at(swayc_t *parent, double lx, double ly, +void sway_container_for_each(struct sway_container *container, void (*f)(struct sway_container *view, void *data), void *data) { + if (container) { + int i; + if (container->children) { + for (i = 0; i < container->children->length; ++i) { + struct sway_container *child = container->children->items[i]; + sway_container_for_each(child, f, data); + } + } + // TODO + /* + if (container->floating) { + for (i = 0; i < container->floating->length; ++i) { + struct sway_container *child = container->floating->items[i]; + container_map(child, f, data); + } + } + */ + f(container, data); + } +} + +struct sway_container *sway_container_at(struct sway_container *parent, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy) { list_t *queue = get_bfs_queue(); if (!queue) { @@ -301,13 +335,13 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, list_add(queue, parent); - swayc_t *swayc = NULL; + struct sway_container *swayc = NULL; while (queue->length) { swayc = queue->items[0]; list_del(queue, 0); if (swayc->type == C_VIEW) { struct sway_view *sview = swayc->sway_view; - swayc_t *soutput = swayc_parent_by_type(swayc, C_OUTPUT); + struct sway_container *soutput = sway_container_parent(swayc, C_OUTPUT); struct wlr_box *output_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, @@ -377,29 +411,7 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, return NULL; } -void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { - if (container) { - int i; - if (container->children) { - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - container_map(child, f, data); - } - } - // TODO - /* - if (container->floating) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; - container_map(child, f, data); - } - } - */ - f(container, data); - } -} - -void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), +void sway_container_for_each_bfs(struct sway_container *con, void (*f)(struct sway_container *con, void *data), void *data) { list_t *queue = get_bfs_queue(); if (!queue) { @@ -413,7 +425,7 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), list_add(queue, con); - swayc_t *current = NULL; + struct sway_container *current = NULL; while (queue->length) { current = queue->items[0]; list_del(queue, 0); @@ -422,15 +434,3 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), list_cat(queue, current->children); } } - -swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { - if (container->type == C_WORKSPACE) { - container->workspace_layout = layout; - if (layout == L_HORIZ || layout == L_VERT) { - container->layout = layout; - } - } else { - container->layout = layout; - } - return container; -} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 5a15f3a2..068fb39c 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -14,7 +14,7 @@ #include "list.h" #include "log.h" -swayc_t root_container; +struct sway_container root_container; static void output_layout_change_notify(struct wl_listener *listener, void *data) { struct wlr_box *layout_box = wlr_output_layout_get_box( @@ -23,7 +23,7 @@ static void output_layout_change_notify(struct wl_listener *listener, void *data root_container.height = layout_box->height; for (int i = 0 ; i < root_container.children->length; ++i) { - swayc_t *output_container = root_container.children->items[i]; + struct sway_container *output_container = root_container.children->items[i]; if (output_container->type != C_OUTPUT) { continue; } @@ -62,9 +62,9 @@ void init_layout(void) { &root_container.sway_root->output_layout_change); } -static int index_child(const swayc_t *child) { +static int index_child(const struct sway_container *child) { // TODO handle floating - swayc_t *parent = child->parent; + struct sway_container *parent = child->parent; int i, len; len = parent->children->length; for (i = 0; i < len; ++i) { @@ -79,16 +79,16 @@ static int index_child(const swayc_t *child) { return i; } -swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { +struct sway_container *add_sibling(struct sway_container *fixed, struct sway_container *active) { // TODO handle floating - swayc_t *parent = fixed->parent; + struct sway_container *parent = fixed->parent; int i = index_child(fixed); list_insert(parent->children, i + 1, active); active->parent = parent; return active->parent; } -void add_child(swayc_t *parent, swayc_t *child) { +void add_child(struct sway_container *parent, struct sway_container *child) { wlr_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); @@ -102,9 +102,9 @@ void add_child(swayc_t *parent, swayc_t *child) { */ } -swayc_t *remove_child(swayc_t *child) { +struct sway_container *remove_child(struct sway_container *child) { int i; - swayc_t *parent = child->parent; + struct sway_container *parent = child->parent; for (i = 0; i < parent->children->length; ++i) { if (parent->children->items[i] == child) { list_del(parent->children, i); @@ -115,7 +115,7 @@ swayc_t *remove_child(swayc_t *child) { return parent; } -enum swayc_layouts default_layout(swayc_t *output) { +enum sway_container_layout default_layout(struct sway_container *output) { /* TODO WLR if (config->default_layout != L_NONE) { //return config->default_layout; @@ -129,8 +129,8 @@ enum swayc_layouts default_layout(swayc_t *output) { } static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { - swayc_t *a = *(void **)_a; - swayc_t *b = *(void **)_b; + struct sway_container *a = *(void **)_a; + struct sway_container *b = *(void **)_b; int retval = 0; if (isdigit(a->name[0]) && isdigit(b->name[0])) { @@ -146,21 +146,21 @@ static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { return retval; } -void sort_workspaces(swayc_t *output) { +void sort_workspaces(struct sway_container *output) { list_stable_sort(output->children, sort_workspace_cmp_qsort); } -static void apply_horiz_layout(swayc_t *container, const double x, +static void apply_horiz_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end); -static void apply_vert_layout(swayc_t *container, const double x, +static void apply_vert_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end); -void arrange_windows(swayc_t *container, double width, double height) { +void arrange_windows(struct sway_container *container, double width, double height) { int i; if (width == -1 || height == -1) { width = container->width; @@ -181,7 +181,7 @@ void arrange_windows(swayc_t *container, double width, double height) { case C_ROOT: // TODO: wlr_output_layout probably for (i = 0; i < container->children->length; ++i) { - swayc_t *output = container->children->items[i]; + struct sway_container *output = container->children->items[i]; wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); arrange_windows(output, -1, -1); @@ -197,13 +197,13 @@ void arrange_windows(swayc_t *container, double width, double height) { } // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + struct sway_container *child = container->children->items[i]; arrange_windows(child, -1, -1); } return; case C_WORKSPACE: { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + struct sway_container *output = sway_container_parent(container, C_OUTPUT); struct wlr_box *area = &output->sway_output->usable_area; wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); @@ -252,14 +252,14 @@ void arrange_windows(swayc_t *container, double width, double height) { } } -static void apply_horiz_layout(swayc_t *container, +static void apply_horiz_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end) { double scale = 0; // Calculate total width for (int i = start; i < end; ++i) { - double *old_width = &((swayc_t *)container->children->items[i])->width; + double *old_width = &((struct sway_container *)container->children->items[i])->width; if (*old_width <= 0) { if (end - start > 1) { *old_width = width / (end - start - 1); @@ -276,7 +276,7 @@ static void apply_horiz_layout(swayc_t *container, if (scale > 0.1) { wlr_log(L_DEBUG, "Arranging %p horizontally", container); for (int i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; + struct sway_container *child = container->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); @@ -301,7 +301,7 @@ static void apply_horiz_layout(swayc_t *container, } } -void apply_vert_layout(swayc_t *container, +void apply_vert_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end) { @@ -309,7 +309,7 @@ void apply_vert_layout(swayc_t *container, double scale = 0; // Calculate total height for (i = start; i < end; ++i) { - double *old_height = &((swayc_t *)container->children->items[i])->height; + double *old_height = &((struct sway_container *)container->children->items[i])->height; if (*old_height <= 0) { if (end - start > 1) { *old_height = height / (end - start - 1); @@ -326,7 +326,7 @@ void apply_vert_layout(swayc_t *container, if (scale > 0.1) { wlr_log(L_DEBUG, "Arranging %p vertically", container); for (i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; + struct sway_container *child = container->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); @@ -354,15 +354,15 @@ void apply_vert_layout(swayc_t *container, /** * Get swayc in the direction of newly entered output. */ -static swayc_t *get_swayc_in_output_direction(swayc_t *output, +static struct sway_container *get_swayc_in_output_direction(struct sway_container *output, enum movement_direction dir, struct sway_seat *seat) { if (!output) { return NULL; } - swayc_t *ws = sway_seat_get_focus_inactive(seat, output); + struct sway_container *ws = sway_seat_get_focus_inactive(seat, output); if (ws->type != C_WORKSPACE) { - ws = swayc_parent_by_type(ws, C_WORKSPACE); + ws = sway_container_parent(ws, C_WORKSPACE); } if (ws == NULL) { @@ -380,9 +380,9 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, return ws->children->items[0]; case MOVE_UP: case MOVE_DOWN: { - swayc_t *focused = sway_seat_get_focus_inactive(seat, ws); + struct sway_container *focused = sway_seat_get_focus_inactive(seat, ws); if (focused && focused->parent) { - swayc_t *parent = focused->parent; + struct sway_container *parent = focused->parent; if (parent->layout == L_VERT) { if (dir == MOVE_UP) { // get child furthest down on new output @@ -404,13 +404,13 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, return ws; } -static void get_layout_center_position(swayc_t *container, int *x, int *y) { +static void get_layout_center_position(struct sway_container *container, int *x, int *y) { // FIXME view coords are inconsistently referred to in layout/output systems if (container->type == C_OUTPUT) { *x = container->x + container->width/2; *y = container->y + container->height/2; } else { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + struct sway_container *output = sway_container_parent(container, C_OUTPUT); if (container->type == C_WORKSPACE) { // Workspace coordinates are actually wrong/arbitrary, but should // be same as output. @@ -444,12 +444,12 @@ static bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out return true; } -static swayc_t *sway_output_from_wlr(struct wlr_output *output) { +static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { if (output == NULL) { return NULL; } for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *o = root_container.children->items[i]; + struct sway_container *o = root_container.children->items[i]; if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { return o; } @@ -457,13 +457,13 @@ static swayc_t *sway_output_from_wlr(struct wlr_output *output) { return NULL; } -static swayc_t *get_swayc_in_direction_under(swayc_t *container, - enum movement_direction dir, struct sway_seat *seat, swayc_t *limit) { +static struct sway_container *get_swayc_in_direction_under(struct sway_container *container, + enum movement_direction dir, struct sway_seat *seat, struct sway_container *limit) { if (dir == MOVE_CHILD) { return sway_seat_get_focus_inactive(seat, container); } - swayc_t *parent = container->parent; + struct sway_container *parent = container->parent; if (dir == MOVE_PARENT) { if (parent->type == C_OUTPUT) { return NULL; @@ -496,9 +496,9 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, /* if (container->type == C_VIEW && swayc_is_fullscreen(container)) { wlr_log(L_DEBUG, "Moving from fullscreen view, skipping to output"); - container = swayc_parent_by_type(container, C_OUTPUT); + container = sway_container_parent(container, C_OUTPUT); get_layout_center_position(container, &abs_pos); - swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); + struct sway_container *output = swayc_adjacent_output(container, dir, &abs_pos, true); return get_swayc_in_output_direction(output, dir); } if (container->type == C_WORKSPACE && container->fullscreen) { @@ -507,7 +507,7 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, } */ - swayc_t *wrap_candidate = NULL; + struct sway_container *wrap_candidate = NULL; while (true) { // Test if we can even make a difference here bool can_move = false; @@ -525,12 +525,12 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, struct wlr_output *wlr_adjacent = wlr_output_layout_adjacent_output(layout, wlr_dir, container->sway_output->wlr_output, lx, ly); - swayc_t *adjacent = sway_output_from_wlr(wlr_adjacent); + struct sway_container *adjacent = sway_output_from_wlr(wlr_adjacent); if (!adjacent || adjacent == container) { return wrap_candidate; } - swayc_t *next = get_swayc_in_output_direction(adjacent, dir, seat); + struct sway_container *next = get_swayc_in_output_direction(adjacent, dir, seat); if (next == NULL) { return NULL; } @@ -587,7 +587,7 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, } } -swayc_t *get_swayc_in_direction(swayc_t *container, struct sway_seat *seat, +struct sway_container *get_swayc_in_direction(struct sway_container *container, struct sway_seat *seat, enum movement_direction dir) { return get_swayc_in_direction_under(container, dir, seat, NULL); } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 3da3fde6..32e82845 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -17,7 +17,7 @@ struct workspace_by_number_data { const char *name; }; -void next_name_map(swayc_t *ws, void *data) { +void next_name_map(struct sway_container *ws, void *data) { int *count = data; ++count; } @@ -37,7 +37,7 @@ char *workspace_next_name(const char *output_name) { return name; } -static bool _workspace_by_number(swayc_t *view, void *data) { +static bool _workspace_by_number(struct sway_container *view, void *data) { if (view->type != C_WORKSPACE) { return false; } @@ -46,27 +46,27 @@ static bool _workspace_by_number(swayc_t *view, void *data) { return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; } -swayc_t *workspace_by_number(const char* name) { +struct sway_container *workspace_by_number(const char* name) { struct workspace_by_number_data wbnd = {0, "1234567890", name}; wbnd.len = strspn(name, wbnd.cset); if (wbnd.len <= 0) { return NULL; } - return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); + return sway_container_find(&root_container, _workspace_by_number, (void *) &wbnd); } -static bool _workspace_by_name(swayc_t *view, void *data) { +static bool _workspace_by_name(struct sway_container *view, void *data) { return (view->type == C_WORKSPACE) && (strcasecmp(view->name, (char *) data) == 0); } -swayc_t *workspace_by_name(const char *name) { +struct sway_container *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *current_workspace = NULL, *current_output = NULL; - swayc_t *focus = sway_seat_get_focus(seat); + struct sway_container *current_workspace = NULL, *current_output = NULL; + struct sway_container *focus = sway_seat_get_focus(seat); if (focus) { - current_workspace = swayc_parent_by_type(focus, C_WORKSPACE); - current_output = swayc_parent_by_type(focus, C_OUTPUT); + current_workspace = sway_container_parent(focus, C_WORKSPACE); + current_output = sway_container_parent(focus, C_OUTPUT); } if (strcmp(name, "prev") == 0) { return workspace_prev(current_workspace); @@ -79,12 +79,12 @@ swayc_t *workspace_by_name(const char *name) { } else if (strcmp(name, "current") == 0) { return current_workspace; } else { - return swayc_by_test(&root_container, _workspace_by_name, (void *) name); + return sway_container_find(&root_container, _workspace_by_name, (void *) name); } } -swayc_t *workspace_create(const char *name) { - swayc_t *parent; +struct sway_container *workspace_create(const char *name) { + struct sway_container *parent; // Search for workspace<->output pair int i, e = config->workspace_outputs->length; for (i = 0; i < e; ++i) { @@ -95,7 +95,7 @@ swayc_t *workspace_create(const char *name) { for (i = 0; i < e; ++i) { parent = root_container.children->items[i]; if (strcmp(parent->name, wso->output) == 0) { - return new_workspace(parent, name); + return sway_container_workspace_create(parent, name); } } break; @@ -103,10 +103,10 @@ swayc_t *workspace_create(const char *name) { } // Otherwise create a new one struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); parent = focus; - parent = swayc_parent_by_type(parent, C_OUTPUT); - return new_workspace(parent, name); + parent = sway_container_parent(parent, C_OUTPUT); + return sway_container_workspace_create(parent, name); } /** @@ -114,17 +114,17 @@ swayc_t *workspace_create(const char *name) { * the end and beginning. If next is false, the previous workspace is returned, * otherwise the next one is returned. */ -swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { +struct sway_container *workspace_output_prev_next_impl(struct sway_container *output, bool next) { if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) { return NULL; } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, output); - swayc_t *workspace = (focus->type == C_WORKSPACE ? + struct sway_container *focus = sway_seat_get_focus_inactive(seat, output); + struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : - swayc_parent_by_type(focus, C_WORKSPACE)); + sway_container_parent(focus, C_WORKSPACE)); int i; for (i = 0; i < output->children->length; i++) { @@ -144,13 +144,13 @@ swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { * next is false, the previous workspace is returned, otherwise the next one is * returned. */ -swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { +struct sway_container *workspace_prev_next_impl(struct sway_container *workspace, bool next) { if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) { return NULL; } - swayc_t *current_output = workspace->parent; + struct sway_container *current_output = workspace->parent; int offset = next ? 1 : -1; int start = next ? 0 : 1; int end; @@ -170,7 +170,7 @@ swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { int num_outputs = root_container.children->length; for (i = 0; i < num_outputs; i++) { if (root_container.children->items[i] == current_output) { - swayc_t *next_output = root_container.children->items[ + struct sway_container *next_output = root_container.children->items[ wrap(i + offset, num_outputs)]; return workspace_output_prev_next_impl(next_output, next); } @@ -180,40 +180,40 @@ swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { return NULL; } -swayc_t *workspace_output_next(swayc_t *current) { +struct sway_container *workspace_output_next(struct sway_container *current) { return workspace_output_prev_next_impl(current, true); } -swayc_t *workspace_next(swayc_t *current) { +struct sway_container *workspace_next(struct sway_container *current) { return workspace_prev_next_impl(current, true); } -swayc_t *workspace_output_prev(swayc_t *current) { +struct sway_container *workspace_output_prev(struct sway_container *current) { return workspace_output_prev_next_impl(current, false); } -swayc_t *workspace_prev(swayc_t *current) { +struct sway_container *workspace_prev(struct sway_container *current) { return workspace_prev_next_impl(current, false); } -bool workspace_switch(swayc_t *workspace) { +bool workspace_switch(struct sway_container *workspace) { if (!workspace) { return false; } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); if (!seat || !focus) { return false; } - swayc_t *active_ws = focus; + struct sway_container *active_ws = focus; if (active_ws->type != C_WORKSPACE) { - swayc_parent_by_type(focus, C_WORKSPACE); + sway_container_parent(focus, C_WORKSPACE); } if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) { - swayc_t *new_ws = workspace_by_name(prev_workspace_name); + struct sway_container *new_ws = workspace_by_name(prev_workspace_name); workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); } @@ -231,12 +231,12 @@ bool workspace_switch(swayc_t *workspace) { // TODO: Deal with sticky containers wlr_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); - swayc_t *next = sway_seat_get_focus_inactive(seat, workspace); + struct sway_container *next = sway_seat_get_focus_inactive(seat, workspace); if (next == NULL) { next = workspace; } sway_seat_set_focus(seat, next); - swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); + struct sway_container *output = sway_container_parent(workspace, C_OUTPUT); arrange_windows(output, -1, -1); return true; } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 93d1219c..f9df0d10 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -352,7 +352,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { } // add bar to the output - struct output *bar_output = new_output(name); + struct output *bar_output = sway_container_output_create(name); bar_output->idx = i; list_add(bar->outputs, bar_output); } -- cgit v1.2.3 From eca029f218fbb54ddf7316845be5d296e834358e Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 29 Mar 2018 17:06:29 -0400 Subject: more renaming things --- include/sway/tree/container.h | 21 +++++++--------- include/sway/tree/layout.h | 16 ++++++------ sway/commands/layout.c | 16 ++++++------ sway/commands/reload.c | 2 +- sway/commands/workspace.c | 6 ++--- sway/config/output.c | 2 +- sway/desktop/layer_shell.c | 2 +- sway/desktop/output.c | 12 ++++----- sway/desktop/wl_shell.c | 8 +++--- sway/desktop/xdg_shell_v6.c | 8 +++--- sway/desktop/xwayland.c | 22 ++++++++-------- sway/input/seat.c | 2 +- sway/tree/container.c | 58 +++++++++++++++++++++---------------------- sway/tree/layout.c | 34 ++++++++++++------------- sway/tree/workspace.c | 22 ++++++++-------- swaybar/ipc.c | 2 +- 16 files changed, 115 insertions(+), 118 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 0dfed455..572dd8e3 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -88,41 +88,38 @@ struct sway_container { }; // TODO only one container create function and pass the type? -struct sway_container *sway_container_output_create( +struct sway_container *container_output_create( struct sway_output *sway_output); -struct sway_container *sway_container_workspace_create( +struct sway_container *container_workspace_create( struct sway_container *output, const char *name); -struct sway_container *sway_container_view_create( +struct sway_container *container_view_create( struct sway_container *sibling, struct sway_view *sway_view); -struct sway_container *sway_container_output_destroy( +struct sway_container *container_output_destroy( struct sway_container *output); -struct sway_container *sway_container_view_destroy(struct sway_container *view); +struct sway_container *container_view_destroy(struct sway_container *view); -struct sway_container *sway_container_set_layout( +struct sway_container *container_set_layout( struct sway_container *container, enum sway_container_layout layout); -void sway_container_descendents(struct sway_container *root, +void container_descendents(struct sway_container *root, enum sway_container_type type, void (*func)(struct sway_container *item, void *data), void *data); -// XXX: what is this? -struct sway_container *next_view_sibling(struct sway_seat *seat); - /** * Finds a container based on test criteria. Returns the first container that * passes the test. */ -struct sway_container *sway_container_find(struct sway_container *container, +struct sway_container *container_find(struct sway_container *container, bool (*test)(struct sway_container *view, void *data), void *data); /** * Finds a parent container with the given struct sway_containerype. */ -struct sway_container *sway_container_parent(struct sway_container *container, +struct sway_container *container_parent(struct sway_container *container, enum sway_container_type type); /** diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index f73b3880..6980ec9e 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -30,23 +30,23 @@ struct sway_root { } events; }; -void init_layout(void); +void layout_init(void); -void add_child(struct sway_container *parent, struct sway_container *child); +void container_add_child(struct sway_container *parent, struct sway_container *child); -struct sway_container *add_sibling(struct sway_container *parent, +struct sway_container *container_add_sibling(struct sway_container *parent, struct sway_container *child); -struct sway_container *remove_child(struct sway_container *child); +struct sway_container *container_remove_child(struct sway_container *child); -enum sway_container_layout default_layout(struct sway_container *output); +enum sway_container_layout container_get_default_layout(struct sway_container *output); -void sort_workspaces(struct sway_container *output); +void container_sort_workspaces(struct sway_container *output); -void arrange_windows(struct sway_container *container, +void container_arrange_windows(struct sway_container *container, double width, double height); -struct sway_container *get_swayc_in_direction(struct sway_container +struct sway_container *container_get_in_direction(struct sway_container *container, struct sway_seat *seat, enum movement_direction dir); #endif diff --git a/sway/commands/layout.c b/sway/commands/layout.c index e10334e2..e9cfeb8f 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -26,10 +26,10 @@ struct cmd_results *cmd_layout(int argc, char **argv) { // TODO: stacks and tabs if (strcasecmp(argv[0], "default") == 0) { - swayc_change_layout(parent, parent->prev_layout); + container_set_layout(parent, parent->prev_layout); if (parent->layout == L_NONE) { - struct sway_container *output = sway_container_parent(parent, C_OUTPUT); - swayc_change_layout(parent, default_layout(output)); + struct sway_container *output = container_parent(parent, C_OUTPUT); + container_set_layout(parent, container_get_default_layout(output)); } } else { if (parent->layout != L_TABBED && parent->layout != L_STACKED) { @@ -37,20 +37,20 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } if (strcasecmp(argv[0], "splith") == 0) { - swayc_change_layout(parent, L_HORIZ); + container_set_layout(parent, L_HORIZ); } else if (strcasecmp(argv[0], "splitv") == 0) { - swayc_change_layout(parent, L_VERT); + container_set_layout(parent, L_VERT); } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE || parent->workspace_layout == L_HORIZ)) { - swayc_change_layout(parent, L_VERT); + container_set_layout(parent, L_VERT); } else { - swayc_change_layout(parent, L_HORIZ); + container_set_layout(parent, L_HORIZ); } } } - arrange_windows(parent, parent->width, parent->height); + container_arrange_windows(parent, parent->width, parent->height); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/reload.c b/sway/commands/reload.c index 8cef789b..83ecd75b 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -13,6 +13,6 @@ struct cmd_results *cmd_reload(int argc, char **argv) { /* load_swaybars(); -- for when it's implemented */ - arrange_windows(&root_container, -1, -1); + container_arrange_windows(&root_container, -1, -1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 8b7139a9..8f39e5fc 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -23,9 +23,9 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (current_container->type == C_WORKSPACE) { old_workspace = current_container; } else { - old_workspace = sway_container_parent(current_container, C_WORKSPACE); + old_workspace = container_parent(current_container, C_WORKSPACE); } - old_output = sway_container_parent(current_container, C_OUTPUT); + old_output = container_parent(current_container, C_OUTPUT); } for (int i = 0; i < argc; ++i) { @@ -92,7 +92,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { workspace_switch(ws); current_container = sway_seat_get_focus(config->handler_context.seat); - struct sway_container *new_output = sway_container_parent(current_container, C_OUTPUT); + struct sway_container *new_output = container_parent(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { // TODO: Warp mouse diff --git a/sway/config/output.c b/sway/config/output.c index 5763bd21..7fc79739 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -127,7 +127,7 @@ void apply_output_config(struct output_config *oc, struct sway_container *output if (oc && oc->enabled == 0) { wlr_output_layout_remove(root_container.sway_root->output_layout, wlr_output); - sway_container_output_destroy(output); + container_output_destroy(output); return; } diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index 137b3260..4bfd1c45 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -172,7 +172,7 @@ void arrange_layers(struct sway_output *output) { if (memcmp(&usable_area_before, &usable_area, sizeof(struct wlr_box)) != 0) { wlr_log(L_DEBUG, "arrange"); - arrange_windows(output->swayc, -1, -1); + container_arrange_windows(output->swayc, -1, -1); } // Arrange non-exlusive surfaces from top->bottom diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 3e7d8509..fa1b0680 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -221,13 +221,13 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { struct sway_container *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : - sway_container_parent(focus, C_WORKSPACE)); + container_parent(focus, C_WORKSPACE)); struct render_data rdata = { .output = soutput, .now = &now, }; - sway_container_descendents(workspace, C_VIEW, output_frame_view, &rdata); + container_descendents(workspace, C_VIEW, output_frame_view, &rdata); // render unmanaged views on top struct sway_view *view; @@ -258,13 +258,13 @@ static void handle_output_destroy(struct wl_listener *listener, void *data) { struct wlr_output *wlr_output = data; wlr_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); - sway_container_output_destroy(output->swayc); + container_output_destroy(output->swayc); } static void handle_output_mode(struct wl_listener *listener, void *data) { struct sway_output *output = wl_container_of(listener, output, mode); arrange_layers(output); - arrange_windows(output->swayc, -1, -1); + container_arrange_windows(output->swayc, -1, -1); } void handle_new_output(struct wl_listener *listener, void *data) { @@ -286,7 +286,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { wlr_output_set_mode(wlr_output, mode); } - output->swayc = sway_container_output_create(output); + output->swayc = container_output_create(output); if (!output->swayc) { free(output); return; @@ -307,5 +307,5 @@ void handle_new_output(struct wl_listener *listener, void *data) { output->mode.notify = handle_output_mode; arrange_layers(output); - arrange_windows(&root_container, -1, -1); + container_arrange_windows(&root_container, -1, -1); } diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index bf41d7bf..ac1c7f26 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -74,10 +74,10 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_surface, destroy); wl_list_remove(&sway_surface->commit.link); wl_list_remove(&sway_surface->destroy.link); - struct sway_container *parent = sway_container_view_destroy(sway_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); free(sway_surface->view); free(sway_surface); - arrange_windows(parent, -1, -1); + container_arrange_windows(parent, -1, -1); } void handle_wl_shell_surface(struct wl_listener *listener, void *data) { @@ -133,9 +133,9 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - struct sway_container *cont = sway_container_view_create(focus, sway_view); + struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; - arrange_windows(cont->parent, -1, -1); + container_arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 6b50d470..616cb88f 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -83,10 +83,10 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_xdg_surface, destroy); wl_list_remove(&sway_xdg_surface->commit.link); wl_list_remove(&sway_xdg_surface->destroy.link); - struct sway_container *parent = sway_container_view_destroy(sway_xdg_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_xdg_surface->view->swayc); free(sway_xdg_surface->view); free(sway_xdg_surface); - arrange_windows(parent, -1, -1); + container_arrange_windows(parent, -1, -1); } void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { @@ -137,10 +137,10 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - struct sway_container *cont = sway_container_view_create(focus, sway_view); + struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; - arrange_windows(cont->parent, -1, -1); + container_arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 96edab51..fa1054f2 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -49,11 +49,11 @@ static void set_position(struct sway_view *view, double ox, double oy) { if (!assert_xwayland(view)) { return; } - struct sway_container *output = sway_container_parent(view->swayc, C_OUTPUT); + struct sway_container *output = container_parent(view->swayc, C_OUTPUT); if (!sway_assert(output, "view must be within tree to set position")) { return; } - struct sway_container *root = sway_container_parent(output, C_ROOT); + struct sway_container *root = container_parent(output, C_ROOT); if (!sway_assert(root, "output must be within tree to set position")) { return; } @@ -114,9 +114,9 @@ static void handle_destroy(struct wl_listener *listener, void *data) { } } - struct sway_container *parent = sway_container_view_destroy(sway_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); if (parent) { - arrange_windows(parent, -1, -1); + container_arrange_windows(parent, -1, -1); } free(sway_surface->view); @@ -132,9 +132,9 @@ static void handle_unmap_notify(struct wl_listener *listener, void *data) { } // take it out of the tree - struct sway_container *parent = sway_container_view_destroy(sway_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); if (parent) { - arrange_windows(parent, -1, -1); + container_arrange_windows(parent, -1, -1); } sway_surface->view->swayc = NULL; @@ -155,15 +155,15 @@ static void handle_map_notify(struct wl_listener *listener, void *data) { &sway_surface->view->unmanaged_view_link); } else { struct sway_view *view = sway_surface->view; - sway_container_view_destroy(view->swayc); + container_view_destroy(view->swayc); struct sway_container *parent = root_container.children->items[0]; parent = parent->children->items[0]; // workspace - struct sway_container *cont = sway_container_view_create(parent, view); + struct sway_container *cont = container_view_create(parent, view); view->swayc = cont; - arrange_windows(cont->parent, -1, -1); + container_arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } } @@ -239,9 +239,9 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - struct sway_container *cont = sway_container_view_create(focus, sway_view); + struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; - arrange_windows(cont->parent, -1, -1); + container_arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } diff --git a/sway/input/seat.c b/sway/input/seat.c index f03a03b4..8e2189de 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -365,7 +365,7 @@ struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, return focus; } - return sway_container_parent(focus, type); + return container_parent(focus, type); } void sway_seat_set_config(struct sway_seat *seat, diff --git a/sway/tree/container.c b/sway/tree/container.c index d31966b3..b41626e1 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -67,7 +67,7 @@ static void free_swayc(struct sway_container *cont) { if (cont->children) { // remove children until there are no more, free_swayc calls - // remove_child, which removes child from this container + // container_remove_child, which removes child from this container while (cont->children->length) { free_swayc(cont->children->items[0]); } @@ -78,7 +78,7 @@ static void free_swayc(struct sway_container *cont) { list_free(cont->marks); } if (cont->parent) { - remove_child(cont); + container_remove_child(cont); } if (cont->name) { free(cont->name); @@ -86,7 +86,7 @@ static void free_swayc(struct sway_container *cont) { free(cont); } -struct sway_container *sway_container_output_create(struct sway_output *sway_output) { +struct sway_container *container_output_create(struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, &size.height); @@ -131,12 +131,12 @@ struct sway_container *sway_container_output_create(struct sway_output *sway_out apply_output_config(oc, output); - add_child(&root_container, output); + container_add_child(&root_container, output); // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - struct sway_container *ws = sway_container_workspace_create(output, ws_name); + struct sway_container *ws = container_workspace_create(output, ws_name); // Set each seat's focus if not already set struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { @@ -150,8 +150,8 @@ struct sway_container *sway_container_output_create(struct sway_output *sway_out return output; } -struct sway_container *sway_container_workspace_create(struct sway_container *output, const char *name) { - if (!sway_assert(output, "sway_container_workspace_create called with null output")) { +struct sway_container *container_workspace_create(struct sway_container *output, const char *name) { + if (!sway_assert(output, "container_workspace_create called with null output")) { return NULL; } wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); @@ -163,17 +163,17 @@ struct sway_container *sway_container_workspace_create(struct sway_container *ou workspace->height = output->height; workspace->name = !name ? NULL : strdup(name); workspace->prev_layout = L_NONE; - workspace->layout = default_layout(output); - workspace->workspace_layout = default_layout(output); + workspace->layout = container_get_default_layout(output); + workspace->workspace_layout = container_get_default_layout(output); - add_child(output, workspace); - sort_workspaces(output); + container_add_child(output, workspace); + container_sort_workspaces(output); notify_new_container(workspace); return workspace; } -struct sway_container *sway_container_view_create(struct sway_container *sibling, struct sway_view *sway_view) { - if (!sway_assert(sibling, "sway_container_view_create called with NULL sibling/parent")) { +struct sway_container *container_view_create(struct sway_container *sibling, struct sway_view *sway_view) { + if (!sway_assert(sibling, "container_view_create called with NULL sibling/parent")) { return NULL; } const char *title = view_get_title(sway_view); @@ -188,17 +188,17 @@ struct sway_container *sway_container_view_create(struct sway_container *sibling if (sibling->type == C_WORKSPACE) { // Case of focused workspace, just create as child of it - add_child(sibling, swayc); + container_add_child(sibling, swayc); } else { // Regular case, create as sibling of current container - add_sibling(sibling, swayc); + container_add_sibling(sibling, swayc); } notify_new_container(swayc); return swayc; } -struct sway_container *sway_container_output_destroy(struct sway_container *output) { - if (!sway_assert(output, "null output passed to sway_container_output_destroy")) { +struct sway_container *container_output_destroy(struct sway_container *output) { + if (!sway_assert(output, "null output passed to container_output_destroy")) { return NULL; } @@ -211,11 +211,11 @@ struct sway_container *sway_container_output_destroy(struct sway_container *outp // Move workspace from this output to another output while (output->children->length) { struct sway_container *child = output->children->items[0]; - remove_child(child); - add_child(root_container.children->items[p], child); + container_remove_child(child); + container_add_child(root_container.children->items[p], child); } - sort_workspaces(root_container.children->items[p]); - arrange_windows(root_container.children->items[p], -1, -1); + container_sort_workspaces(root_container.children->items[p]); + container_arrange_windows(root_container.children->items[p], -1, -1); } } @@ -229,7 +229,7 @@ struct sway_container *sway_container_output_destroy(struct sway_container *outp return &root_container; } -struct sway_container *sway_container_view_destroy(struct sway_container *view) { +struct sway_container *container_view_destroy(struct sway_container *view) { if (!view) { return NULL; } @@ -246,7 +246,7 @@ struct sway_container *sway_container_view_destroy(struct sway_container *view) return parent; } -struct sway_container *swayc_change_layout(struct sway_container *container, enum sway_container_layout layout) { +struct sway_container *container_set_layout(struct sway_container *container, enum sway_container_layout layout) { if (container->type == C_WORKSPACE) { container->workspace_layout = layout; if (layout == L_HORIZ || layout == L_VERT) { @@ -258,7 +258,7 @@ struct sway_container *swayc_change_layout(struct sway_container *container, enu return container; } -void sway_container_descendents(struct sway_container *root, enum sway_container_type type, +void container_descendents(struct sway_container *root, enum sway_container_type type, void (*func)(struct sway_container *item, void *data), void *data) { for (int i = 0; i < root->children->length; ++i) { struct sway_container *item = root->children->items[i]; @@ -266,12 +266,12 @@ void sway_container_descendents(struct sway_container *root, enum sway_container func(item, data); } if (item->children && item->children->length) { - sway_container_descendents(item, type, func, data); + container_descendents(item, type, func, data); } } } -struct sway_container *sway_container_find(struct sway_container *container, +struct sway_container *container_find(struct sway_container *container, bool (*test)(struct sway_container *view, void *data), void *data) { if (!container->children) { return NULL; @@ -282,7 +282,7 @@ struct sway_container *sway_container_find(struct sway_container *container, if (test(child, data)) { return child; } else { - struct sway_container *res = sway_container_find(child, test, data); + struct sway_container *res = container_find(child, test, data); if (res) { return res; } @@ -291,7 +291,7 @@ struct sway_container *sway_container_find(struct sway_container *container, return NULL; } -struct sway_container *sway_container_parent(struct sway_container *container, enum sway_container_type type) { +struct sway_container *container_parent(struct sway_container *container, enum sway_container_type type) { if (!sway_assert(container, "container is NULL")) { return NULL; } @@ -341,7 +341,7 @@ struct sway_container *sway_container_at(struct sway_container *parent, double l list_del(queue, 0); if (swayc->type == C_VIEW) { struct sway_view *sview = swayc->sway_view; - struct sway_container *soutput = sway_container_parent(swayc, C_OUTPUT); + struct sway_container *soutput = container_parent(swayc, C_OUTPUT); struct wlr_box *output_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 068fb39c..07534620 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -40,7 +40,7 @@ static void output_layout_change_notify(struct wl_listener *listener, void *data output_container->height = output_box->height; } - arrange_windows(&root_container, -1, -1); + container_arrange_windows(&root_container, -1, -1); } void init_layout(void) { @@ -79,7 +79,7 @@ static int index_child(const struct sway_container *child) { return i; } -struct sway_container *add_sibling(struct sway_container *fixed, struct sway_container *active) { +struct sway_container *container_add_sibling(struct sway_container *fixed, struct sway_container *active) { // TODO handle floating struct sway_container *parent = fixed->parent; int i = index_child(fixed); @@ -88,7 +88,7 @@ struct sway_container *add_sibling(struct sway_container *fixed, struct sway_con return active->parent; } -void add_child(struct sway_container *parent, struct sway_container *child) { +void container_add_child(struct sway_container *parent, struct sway_container *child) { wlr_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); @@ -102,7 +102,7 @@ void add_child(struct sway_container *parent, struct sway_container *child) { */ } -struct sway_container *remove_child(struct sway_container *child) { +struct sway_container *container_remove_child(struct sway_container *child) { int i; struct sway_container *parent = child->parent; for (i = 0; i < parent->children->length; ++i) { @@ -115,7 +115,7 @@ struct sway_container *remove_child(struct sway_container *child) { return parent; } -enum sway_container_layout default_layout(struct sway_container *output) { +enum sway_container_layout container_get_default_layout(struct sway_container *output) { /* TODO WLR if (config->default_layout != L_NONE) { //return config->default_layout; @@ -146,7 +146,7 @@ static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { return retval; } -void sort_workspaces(struct sway_container *output) { +void container_sort_workspaces(struct sway_container *output) { list_stable_sort(output->children, sort_workspace_cmp_qsort); } @@ -160,7 +160,7 @@ static void apply_vert_layout(struct sway_container *container, const double x, const double height, const int start, const int end); -void arrange_windows(struct sway_container *container, double width, double height) { +void container_arrange_windows(struct sway_container *container, double width, double height) { int i; if (width == -1 || height == -1) { width = container->width; @@ -184,7 +184,7 @@ void arrange_windows(struct sway_container *container, double width, double heig struct sway_container *output = container->children->items[i]; wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); - arrange_windows(output, -1, -1); + container_arrange_windows(output, -1, -1); } return; case C_OUTPUT: @@ -198,12 +198,12 @@ void arrange_windows(struct sway_container *container, double width, double heig // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { struct sway_container *child = container->children->items[i]; - arrange_windows(child, -1, -1); + container_arrange_windows(child, -1, -1); } return; case C_WORKSPACE: { - struct sway_container *output = sway_container_parent(container, C_OUTPUT); + struct sway_container *output = container_parent(container, C_OUTPUT); struct wlr_box *area = &output->sway_output->usable_area; wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); @@ -284,9 +284,9 @@ static void apply_horiz_layout(struct sway_container *container, if (i == end - 1) { double remaining_width = x + width - child_x; - arrange_windows(child, remaining_width, height); + container_arrange_windows(child, remaining_width, height); } else { - arrange_windows(child, child->width * scale, height); + container_arrange_windows(child, child->width * scale, height); } child_x += child->width; } @@ -334,9 +334,9 @@ void apply_vert_layout(struct sway_container *container, if (i == end - 1) { double remaining_height = y + height - child_y; - arrange_windows(child, width, remaining_height); + container_arrange_windows(child, width, remaining_height); } else { - arrange_windows(child, width, child->height * scale); + container_arrange_windows(child, width, child->height * scale); } child_y += child->height; } @@ -362,7 +362,7 @@ static struct sway_container *get_swayc_in_output_direction(struct sway_containe struct sway_container *ws = sway_seat_get_focus_inactive(seat, output); if (ws->type != C_WORKSPACE) { - ws = sway_container_parent(ws, C_WORKSPACE); + ws = container_parent(ws, C_WORKSPACE); } if (ws == NULL) { @@ -410,7 +410,7 @@ static void get_layout_center_position(struct sway_container *container, int *x, *x = container->x + container->width/2; *y = container->y + container->height/2; } else { - struct sway_container *output = sway_container_parent(container, C_OUTPUT); + struct sway_container *output = container_parent(container, C_OUTPUT); if (container->type == C_WORKSPACE) { // Workspace coordinates are actually wrong/arbitrary, but should // be same as output. @@ -496,7 +496,7 @@ static struct sway_container *get_swayc_in_direction_under(struct sway_container /* if (container->type == C_VIEW && swayc_is_fullscreen(container)) { wlr_log(L_DEBUG, "Moving from fullscreen view, skipping to output"); - container = sway_container_parent(container, C_OUTPUT); + container = container_parent(container, C_OUTPUT); get_layout_center_position(container, &abs_pos); struct sway_container *output = swayc_adjacent_output(container, dir, &abs_pos, true); return get_swayc_in_output_direction(output, dir); diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 32e82845..0fdd9975 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -52,7 +52,7 @@ struct sway_container *workspace_by_number(const char* name) { if (wbnd.len <= 0) { return NULL; } - return sway_container_find(&root_container, _workspace_by_number, (void *) &wbnd); + return container_find(&root_container, _workspace_by_number, (void *) &wbnd); } static bool _workspace_by_name(struct sway_container *view, void *data) { @@ -65,8 +65,8 @@ struct sway_container *workspace_by_name(const char *name) { struct sway_container *current_workspace = NULL, *current_output = NULL; struct sway_container *focus = sway_seat_get_focus(seat); if (focus) { - current_workspace = sway_container_parent(focus, C_WORKSPACE); - current_output = sway_container_parent(focus, C_OUTPUT); + current_workspace = container_parent(focus, C_WORKSPACE); + current_output = container_parent(focus, C_OUTPUT); } if (strcmp(name, "prev") == 0) { return workspace_prev(current_workspace); @@ -79,7 +79,7 @@ struct sway_container *workspace_by_name(const char *name) { } else if (strcmp(name, "current") == 0) { return current_workspace; } else { - return sway_container_find(&root_container, _workspace_by_name, (void *) name); + return container_find(&root_container, _workspace_by_name, (void *) name); } } @@ -95,7 +95,7 @@ struct sway_container *workspace_create(const char *name) { for (i = 0; i < e; ++i) { parent = root_container.children->items[i]; if (strcmp(parent->name, wso->output) == 0) { - return sway_container_workspace_create(parent, name); + return container_workspace_create(parent, name); } } break; @@ -105,8 +105,8 @@ struct sway_container *workspace_create(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); parent = focus; - parent = sway_container_parent(parent, C_OUTPUT); - return sway_container_workspace_create(parent, name); + parent = container_parent(parent, C_OUTPUT); + return container_workspace_create(parent, name); } /** @@ -124,7 +124,7 @@ struct sway_container *workspace_output_prev_next_impl(struct sway_container *ou struct sway_container *focus = sway_seat_get_focus_inactive(seat, output); struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : - sway_container_parent(focus, C_WORKSPACE)); + container_parent(focus, C_WORKSPACE)); int i; for (i = 0; i < output->children->length; i++) { @@ -207,7 +207,7 @@ bool workspace_switch(struct sway_container *workspace) { } struct sway_container *active_ws = focus; if (active_ws->type != C_WORKSPACE) { - sway_container_parent(focus, C_WORKSPACE); + container_parent(focus, C_WORKSPACE); } if (config->auto_back_and_forth @@ -236,7 +236,7 @@ bool workspace_switch(struct sway_container *workspace) { next = workspace; } sway_seat_set_focus(seat, next); - struct sway_container *output = sway_container_parent(workspace, C_OUTPUT); - arrange_windows(output, -1, -1); + struct sway_container *output = container_parent(workspace, C_OUTPUT); + container_arrange_windows(output, -1, -1); return true; } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index f9df0d10..2d2b3b69 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -352,7 +352,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { } // add bar to the output - struct output *bar_output = sway_container_output_create(name); + struct output *bar_output = container_output_create(name); bar_output->idx = i; list_add(bar->outputs, bar_output); } -- cgit v1.2.3 From 62d1b4cb96f87274d695a9c11a041c42cf59ddc1 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 29 Mar 2018 18:17:31 -0400 Subject: fix container_get_in_direction name --- include/sway/tree/container.h | 7 +++---- sway/commands/focus.c | 2 +- sway/tree/layout.c | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 572dd8e3..3548afc1 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -97,13 +97,12 @@ struct sway_container *container_workspace_create( struct sway_container *container_view_create( struct sway_container *sibling, struct sway_view *sway_view); -struct sway_container *container_output_destroy( - struct sway_container *output); +struct sway_container *container_output_destroy(struct sway_container *output); struct sway_container *container_view_destroy(struct sway_container *view); -struct sway_container *container_set_layout( - struct sway_container *container, enum sway_container_layout layout); +struct sway_container *container_set_layout(struct sway_container *container, + enum sway_container_layout layout); void container_descendents(struct sway_container *root, enum sway_container_type type, diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 64b05904..64f079f4 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -51,7 +51,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { "Expected 'focus ' or 'focus output '"); } - struct sway_container *next_focus = get_swayc_in_direction(con, seat, direction); + struct sway_container *next_focus = container_get_in_direction(con, seat, direction); if (next_focus) { sway_seat_set_focus(seat, next_focus); } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 5152e523..fb34573f 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -587,7 +587,7 @@ static struct sway_container *get_swayc_in_direction_under(struct sway_container } } -struct sway_container *get_swayc_in_direction(struct sway_container *container, struct sway_seat *seat, +struct sway_container *container_get_in_direction(struct sway_container *container, struct sway_seat *seat, enum movement_direction dir) { return get_swayc_in_direction_under(container, dir, seat, NULL); } -- cgit v1.2.3 From bf7a4cd0ebd465a0597e9eec0142fad222b396de Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 17:20:03 -0400 Subject: Add bar configuration commands --- include/sway/commands.h | 3 + include/sway/ipc-server.h | 1 + sway/commands.c | 135 ++++++++++++++++++---------- sway/commands/bar.c | 57 ++++++++++++ sway/commands/bar/activate_button.c | 9 ++ sway/commands/bar/binding_mode_indicator.c | 27 ++++++ sway/commands/bar/bindsym.c | 11 +++ sway/commands/bar/colors.c | 129 ++++++++++++++++++++++++++ sway/commands/bar/context_button.c | 9 ++ sway/commands/bar/font.c | 26 ++++++ sway/commands/bar/height.c | 21 +++++ sway/commands/bar/hidden_state.c | 78 ++++++++++++++++ sway/commands/bar/icon_theme.c | 9 ++ sway/commands/bar/id.c | 33 +++++++ sway/commands/bar/mode.c | 78 ++++++++++++++++ sway/commands/bar/modifier.c | 35 ++++++++ sway/commands/bar/output.c | 50 +++++++++++ sway/commands/bar/pango_markup.c | 27 ++++++ sway/commands/bar/position.c | 29 ++++++ sway/commands/bar/secondary_button.c | 9 ++ sway/commands/bar/separator_symbol.c | 21 +++++ sway/commands/bar/status_command.c | 21 +++++ sway/commands/bar/strip_workspace_numbers.c | 27 ++++++ sway/commands/bar/swaybar_command.c | 21 +++++ sway/commands/bar/tray_output.c | 9 ++ sway/commands/bar/tray_padding.c | 10 +++ sway/commands/bar/workspace_buttons.c | 27 ++++++ sway/commands/bar/wrap_scroll.c | 27 ++++++ sway/config.c | 127 ++++++++++++++++++++++++++ sway/ipc-server.c | 10 ++- sway/meson.build | 25 ++++++ 31 files changed, 1051 insertions(+), 50 deletions(-) create mode 100644 sway/commands/bar.c create mode 100644 sway/commands/bar/activate_button.c create mode 100644 sway/commands/bar/binding_mode_indicator.c create mode 100644 sway/commands/bar/bindsym.c create mode 100644 sway/commands/bar/colors.c create mode 100644 sway/commands/bar/context_button.c create mode 100644 sway/commands/bar/font.c create mode 100644 sway/commands/bar/height.c create mode 100644 sway/commands/bar/hidden_state.c create mode 100644 sway/commands/bar/icon_theme.c create mode 100644 sway/commands/bar/id.c create mode 100644 sway/commands/bar/mode.c create mode 100644 sway/commands/bar/modifier.c create mode 100644 sway/commands/bar/output.c create mode 100644 sway/commands/bar/pango_markup.c create mode 100644 sway/commands/bar/position.c create mode 100644 sway/commands/bar/secondary_button.c create mode 100644 sway/commands/bar/separator_symbol.c create mode 100644 sway/commands/bar/status_command.c create mode 100644 sway/commands/bar/strip_workspace_numbers.c create mode 100644 sway/commands/bar/swaybar_command.c create mode 100644 sway/commands/bar/tray_output.c create mode 100644 sway/commands/bar/tray_padding.c create mode 100644 sway/commands/bar/workspace_buttons.c create mode 100644 sway/commands/bar/wrap_scroll.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 9ff18823..dda286a2 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -74,6 +74,9 @@ void free_cmd_results(struct cmd_results *results); */ const char *cmd_results_to_json(struct cmd_results *results); +struct cmd_results *add_color(const char *name, + char *buffer, const char *color); + typedef struct cmd_results *sway_cmd(int argc, char **argv); sway_cmd cmd_assign; diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index 6b7404e5..1f6fffff 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h @@ -12,5 +12,6 @@ struct sockaddr_un *ipc_user_sockaddr(void); void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change); void ipc_event_window(swayc_t *window, const char *change); +void ipc_event_barconfig_update(struct bar_config *bar); #endif diff --git a/sway/commands.c b/sway/commands.c index b52eb200..8d8b643b 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -91,45 +91,9 @@ void apply_seat_config(struct seat_config *seat) { sway_input_manager_apply_seat_config(input_manager, seat); } -/** - * Check and add color to buffer. - * - * return error object, or NULL if color is valid. - */ -struct cmd_results *add_color(const char *name, char *buffer, const char *color) { - int len = strlen(color); - if (len != 7 && len != 9) { - return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); - } - - if (color[0] != '#') { - return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); - } - - int i; - for (i = 1; i < len; ++i) { - if (!isxdigit(color[i])) { - return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); - } - } - - // copy color to buffer - strncpy(buffer, color, len); - // add default alpha channel if color was defined without it - if (len == 7) { - buffer[7] = 'f'; - buffer[8] = 'f'; - } - buffer[9] = '\0'; - - return NULL; -} - -/** - * handlers that can run in either config or command context - * Keep alphabetized - */ +/* Keep alphabetized */ static struct cmd_handler handlers[] = { + { "bar", cmd_bar }, { "bindcode", cmd_bindcode }, { "bindsym", cmd_bindsym }, { "exec", cmd_exec }, @@ -141,18 +105,53 @@ static struct cmd_handler handlers[] = { { "workspace", cmd_workspace }, }; -/** - * Commands that can *only* run in the config loading context - * Keep alphabetized - */ +static struct cmd_handler bar_handlers[] = { + { "activate_button", bar_cmd_activate_button }, + { "binding_mode_indicator", bar_cmd_binding_mode_indicator }, + { "bindsym", bar_cmd_bindsym }, + { "colors", bar_cmd_colors }, + { "context_button", bar_cmd_context_button }, + { "font", bar_cmd_font }, + { "height", bar_cmd_height }, + { "hidden_state", bar_cmd_hidden_state }, + { "icon_theme", bar_cmd_icon_theme }, + { "id", bar_cmd_id }, + { "mode", bar_cmd_mode }, + { "modifier", bar_cmd_modifier }, + { "output", bar_cmd_output }, + { "pango_markup", bar_cmd_pango_markup }, + { "position", bar_cmd_position }, + { "secondary_button", bar_cmd_secondary_button }, + { "separator_symbol", bar_cmd_separator_symbol }, + { "status_command", bar_cmd_status_command }, + { "strip_workspace_numbers", bar_cmd_strip_workspace_numbers }, + { "swaybar_command", bar_cmd_swaybar_command }, + { "tray_output", bar_cmd_tray_output }, + { "tray_padding", bar_cmd_tray_padding }, + { "workspace_buttons", bar_cmd_workspace_buttons }, + { "wrap_scroll", bar_cmd_wrap_scroll }, +}; + +static struct cmd_handler bar_colors_handlers[] = { + { "active_workspace", bar_colors_cmd_active_workspace }, + { "background", bar_colors_cmd_background }, + { "binding_mode", bar_colors_cmd_binding_mode }, + { "focused_background", bar_colors_cmd_focused_background }, + { "focused_separator", bar_colors_cmd_focused_separator }, + { "focused_statusline", bar_colors_cmd_focused_statusline }, + { "focused_workspace", bar_colors_cmd_focused_workspace }, + { "inactive_workspace", bar_colors_cmd_inactive_workspace }, + { "separator", bar_colors_cmd_separator }, + { "statusline", bar_colors_cmd_statusline }, + { "urgent_workspace", bar_colors_cmd_urgent_workspace }, +}; + +/* Config-time only commands. Keep alphabetized */ static struct cmd_handler config_handlers[] = { { "set", cmd_set }, }; -/** - * Commands that can *not* run in the config loading context - * Keep alphabetized - */ +/* Runtime-only commands. Keep alphabetized */ static struct cmd_handler command_handlers[] = { { "exit", cmd_exit }, { "focus", cmd_focus }, @@ -200,13 +199,19 @@ static struct cmd_handler *find_handler(char *line, enum cmd_status block) { bool config_loading = config->reading || !config->active; - if (block == CMD_BLOCK_INPUT) { - // input commands can run in either context + if (block == CMD_BLOCK_BAR) { + return bsearch(&d, bar_handlers, + sizeof(bar_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_BAR_COLORS) { + return bsearch(&d, bar_colors_handlers, + sizeof(bar_colors_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_INPUT) { return bsearch(&d, input_handlers, sizeof(input_handlers) / sizeof(struct cmd_handler), sizeof(struct cmd_handler), handler_compare); } else if (block == CMD_BLOCK_SEAT) { - // seat commands can run in either context return bsearch(&d, seat_handlers, sizeof(seat_handlers) / sizeof(struct cmd_handler), sizeof(struct cmd_handler), handler_compare); @@ -558,3 +563,35 @@ const char *cmd_results_to_json(struct cmd_results *results) { free(root); return json; } + +/** + * Check and add color to buffer. + * + * return error object, or NULL if color is valid. + */ +struct cmd_results *add_color(const char *name, + char *buffer, const char *color) { + int len = strlen(color); + if (len != 7 && len != 9) { + return cmd_results_new(CMD_INVALID, name, + "Invalid color definition %s", color); + } + if (color[0] != '#') { + return cmd_results_new(CMD_INVALID, name, + "Invalid color definition %s", color); + } + for (int i = 1; i < len; ++i) { + if (!isxdigit(color[i])) { + return cmd_results_new(CMD_INVALID, name, + "Invalid color definition %s", color); + } + } + strncpy(buffer, color, len); + // add default alpha channel if color was defined without it + if (len == 7) { + buffer[7] = 'f'; + buffer[8] = 'f'; + } + buffer[9] = '\0'; + return NULL; +} diff --git a/sway/commands/bar.c b/sway/commands/bar.c new file mode 100644 index 00000000..548106b3 --- /dev/null +++ b/sway/commands/bar.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "util.h" + +struct cmd_results *cmd_bar(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (config->reading && strcmp("{", argv[0]) != 0) { + return cmd_results_new(CMD_INVALID, "bar", + "Expected '{' at start of bar config definition."); + } + + if (!config->reading) { + if (argc > 1) { + if (strcasecmp("mode", argv[0]) == 0) { + return bar_cmd_mode(argc-1, argv + 1); + } + + if (strcasecmp("hidden_state", argv[0]) == 0) { + return bar_cmd_hidden_state(argc-1, argv + 1); + } + } + return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file."); + } + + // Create new bar with default values + struct bar_config *bar = default_bar_config(); + if (!bar) { + return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state"); + } + + // set bar id + int i; + for (i = 0; i < config->bars->length; ++i) { + if (bar == config->bars->items[i]) { + const int len = 5 + numlen(i); // "bar-" + i + \0 + bar->id = malloc(len * sizeof(char)); + if (bar->id) { + snprintf(bar->id, len, "bar-%d", i); + } else { + return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID"); + } + break; + } + } + + // Set current bar + config->current_bar = bar; + wlr_log(L_DEBUG, "Configuring bar %s", bar->id); + return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL); +} diff --git a/sway/commands/bar/activate_button.c b/sway/commands/bar/activate_button.c new file mode 100644 index 00000000..0665d2a6 --- /dev/null +++ b/sway/commands/bar/activate_button.c @@ -0,0 +1,9 @@ +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_activate_button(int argc, char **argv) { + const char *cmd_name = "activate_button"; + // TODO TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); +} diff --git a/sway/commands/bar/binding_mode_indicator.c b/sway/commands/bar/binding_mode_indicator.c new file mode 100644 index 00000000..e11e1033 --- /dev/null +++ b/sway/commands/bar/binding_mode_indicator.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_binding_mode_indicator(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "binding_mode_indicator", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->binding_mode_indicator = true; + wlr_log(L_DEBUG, "Enabling binding mode indicator on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->binding_mode_indicator = false; + wlr_log(L_DEBUG, "Disabling binding mode indicator on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "binding_mode_indicator", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c new file mode 100644 index 00000000..ac09a03f --- /dev/null +++ b/sway/commands/bar/bindsym.c @@ -0,0 +1,11 @@ +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { + return cmd_results_new(CMD_FAILURE, "bindsym", "TODO"); // TODO +} diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c new file mode 100644 index 00000000..8b3b0aac --- /dev/null +++ b/sway/commands/bar/colors.c @@ -0,0 +1,129 @@ +#include +#include "sway/commands.h" + +static struct cmd_results *parse_single_color(char **color, const char *cmd_name, int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!*color) { + *color = malloc(10); + if (!*color) { + return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); + } + } + + error = add_color(cmd_name, *color, argv[0]); + if (error) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *parse_three_colors(char ***colors, const char *cmd_name, int argc, char **argv) { + struct cmd_results *error = NULL; + if (argc != 3) { + return cmd_results_new(CMD_INVALID, cmd_name, "Requires exactly three color values"); + } + + int i; + for (i = 0; i < 3; i++) { + if (!*colors[i]) { + *(colors[i]) = malloc(10); + if (!*(colors[i])) { + return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); + } + } + error = add_color(cmd_name, *(colors[i]), argv[i]); + if (error) { + return error; + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *bar_cmd_colors(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcmp("{", argv[0]) != 0) { + return cmd_results_new(CMD_INVALID, "colors", + "Expected '{' at the start of colors config definition."); + } + + return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL); +} + +struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.active_workspace_border), + &(config->current_bar->colors.active_workspace_bg), + &(config->current_bar->colors.active_workspace_text) + }; + return parse_three_colors(colors, "active_workspace", argc, argv); +} + +struct cmd_results *bar_colors_cmd_background(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.background), "background", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_background(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.focused_background), "focused_background", argc, argv); +} + +struct cmd_results *bar_colors_cmd_binding_mode(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.binding_mode_border), + &(config->current_bar->colors.binding_mode_bg), + &(config->current_bar->colors.binding_mode_text) + }; + return parse_three_colors(colors, "binding_mode", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.focused_workspace_border), + &(config->current_bar->colors.focused_workspace_bg), + &(config->current_bar->colors.focused_workspace_text) + }; + return parse_three_colors(colors, "focused_workspace", argc, argv); +} + +struct cmd_results *bar_colors_cmd_inactive_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.inactive_workspace_border), + &(config->current_bar->colors.inactive_workspace_bg), + &(config->current_bar->colors.inactive_workspace_text) + }; + return parse_three_colors(colors, "inactive_workspace", argc, argv); +} + +struct cmd_results *bar_colors_cmd_separator(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.separator), "separator", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_separator(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); +} + +struct cmd_results *bar_colors_cmd_statusline(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.statusline), "statusline", argc, argv); +} + +struct cmd_results *bar_colors_cmd_focused_statusline(int argc, char **argv) { + return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); +} + +struct cmd_results *bar_colors_cmd_urgent_workspace(int argc, char **argv) { + char **colors[3] = { + &(config->current_bar->colors.urgent_workspace_border), + &(config->current_bar->colors.urgent_workspace_bg), + &(config->current_bar->colors.urgent_workspace_text) + }; + return parse_three_colors(colors, "urgent_workspace", argc, argv); +} diff --git a/sway/commands/bar/context_button.c b/sway/commands/bar/context_button.c new file mode 100644 index 00000000..e6d17f10 --- /dev/null +++ b/sway/commands/bar/context_button.c @@ -0,0 +1,9 @@ +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_context_button(int argc, char **argv) { + const char *cmd_name = "context_button"; + // TODO TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); +} diff --git a/sway/commands/bar/font.c b/sway/commands/bar/font.c new file mode 100644 index 00000000..6d7c533a --- /dev/null +++ b/sway/commands/bar/font.c @@ -0,0 +1,26 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_font(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "font", "No bar defined."); + } + + char *font = join_args(argv, argc); + free(config->current_bar->font); + if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) { + config->current_bar->font = font; + } else { + config->current_bar->font = font; + } + + wlr_log(L_DEBUG, "Settings font '%s' for bar: %s", config->current_bar->font, config->current_bar->id); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/height.c b/sway/commands/bar/height.c new file mode 100644 index 00000000..ae0c7834 --- /dev/null +++ b/sway/commands/bar/height.c @@ -0,0 +1,21 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_height(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "height", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + int height = atoi(argv[0]); + if (height < 0) { + return cmd_results_new(CMD_INVALID, "height", + "Invalid height value: %s", argv[0]); + } + + config->current_bar->height = height; + wlr_log(L_DEBUG, "Setting bar height to %d on bar: %s", height, config->current_bar->id); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c new file mode 100644 index 00000000..245d0858 --- /dev/null +++ b/sway/commands/bar/hidden_state.c @@ -0,0 +1,78 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/ipc-server.h" +#include "log.h" + +static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, + const char *hidden_state) { + char *old_state = bar->hidden_state; + if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) { + if (strcasecmp("hide", bar->hidden_state) == 0) { + bar->hidden_state = strdup("show"); + } else if (strcasecmp("show", bar->hidden_state) == 0) { + bar->hidden_state = strdup("hide"); + } + } else if (strcasecmp("hide", hidden_state) == 0) { + bar->hidden_state = strdup("hide"); + } else if (strcasecmp("show", hidden_state) == 0) { + bar->hidden_state = strdup("show"); + } else { + return cmd_results_new(CMD_INVALID, "hidden_state", + "Invalid value %s", hidden_state); + } + + if (strcmp(old_state, bar->hidden_state) != 0) { + if (!config->reading) { + ipc_event_barconfig_update(bar); + } + wlr_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", + bar->hidden_state, bar->id); + } + + // free old mode + free(old_state); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) { + return error; + } + if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) { + return error; + } + + if (config->reading && argc > 1) { + return cmd_results_new(CMD_INVALID, "hidden_state", + "Unexpected value %s in config mode", argv[1]); + } + + const char *state = argv[0]; + + if (config->reading) { + return bar_set_hidden_state(config->current_bar, state); + } + + const char *id = NULL; + if (argc == 2) { + id = argv[1]; + } + + struct bar_config *bar; + for (int i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + if (id && strcmp(id, bar->id) == 0) { + return bar_set_hidden_state(bar, state); + } + + error = bar_set_hidden_state(bar, state); + if (error) { + return error; + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/icon_theme.c b/sway/commands/bar/icon_theme.c new file mode 100644 index 00000000..c6090b81 --- /dev/null +++ b/sway/commands/bar/icon_theme.c @@ -0,0 +1,9 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" + +struct cmd_results *bar_cmd_icon_theme(int argc, char **argv) { + const char *cmd_name = "tray_output"; + // TODO TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); +} diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c new file mode 100644 index 00000000..d9e7f8df --- /dev/null +++ b/sway/commands/bar/id.c @@ -0,0 +1,33 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_id(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "id", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + const char *name = argv[0]; + const char *oldname = config->current_bar->id; + + // check if id is used by a previously defined bar + int i; + for (i = 0; i < config->bars->length; ++i) { + struct bar_config *find = config->bars->items[i]; + if (strcmp(name, find->id) == 0 && config->current_bar != find) { + return cmd_results_new(CMD_FAILURE, "id", + "Id '%s' already defined for another bar. Id unchanged (%s).", + name, oldname); + } + } + + wlr_log(L_DEBUG, "Renaming bar: '%s' to '%s'", oldname, name); + + // free old bar id + free(config->current_bar->id); + + config->current_bar->id = strdup(name); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c new file mode 100644 index 00000000..7d346956 --- /dev/null +++ b/sway/commands/bar/mode.c @@ -0,0 +1,78 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/ipc-server.h" +#include "log.h" + +static struct cmd_results *bar_set_mode(struct bar_config *bar, const char *mode) { + char *old_mode = bar->mode; + if (strcasecmp("toggle", mode) == 0 && !config->reading) { + if (strcasecmp("dock", bar->mode) == 0) { + bar->mode = strdup("hide"); + } else if (strcasecmp("hide", bar->mode) == 0) { + bar->mode = strdup("dock"); + } + } else if (strcasecmp("dock", mode) == 0) { + bar->mode = strdup("dock"); + } else if (strcasecmp("hide", mode) == 0) { + bar->mode = strdup("hide"); + } else if (strcasecmp("invisible", mode) == 0) { + bar->mode = strdup("invisible"); + } else { + return cmd_results_new(CMD_INVALID, "mode", "Invalid value %s", mode); + } + + if (strcmp(old_mode, bar->mode) != 0) { + if (!config->reading) { + ipc_event_barconfig_update(bar); + } + wlr_log(L_DEBUG, "Setting mode: '%s' for bar: %s", bar->mode, bar->id); + } + + // free old mode + free(old_mode); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *bar_cmd_mode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { + return error; + } + if ((error = checkarg(argc, "mode", EXPECTED_LESS_THAN, 3))) { + return error; + } + + if (config->reading && argc > 1) { + return cmd_results_new(CMD_INVALID, "mode", "Unexpected value %s in config mode", argv[1]); + } + + const char *mode = argv[0]; + + if (config->reading) { + return bar_set_mode(config->current_bar, mode); + } + + const char *id = NULL; + if (argc == 2) { + id = argv[1]; + } + + int i; + struct bar_config *bar; + for (i = 0; i < config->bars->length; ++i) { + bar = config->bars->items[i]; + if (id && strcmp(id, bar->id) == 0) { + return bar_set_mode(bar, mode); + } + + error = bar_set_mode(bar, mode); + if (error) { + return error; + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c new file mode 100644 index 00000000..9a1f8b01 --- /dev/null +++ b/sway/commands/bar/modifier.c @@ -0,0 +1,35 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" +#include "util.h" + +struct cmd_results *bar_cmd_modifier(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "modifier", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "modifier", "No bar defined."); + } + + uint32_t mod = 0; + + list_t *split = split_string(argv[0], "+"); + for (int i = 0; i < split->length; ++i) { + uint32_t tmp_mod; + if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) { + mod |= tmp_mod; + continue; + } else { + free_flat_list(split); + return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]); + } + } + free_flat_list(split); + + config->current_bar->modifier = mod; + wlr_log(L_DEBUG, "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c new file mode 100644 index 00000000..034d9ca4 --- /dev/null +++ b/sway/commands/bar/output.c @@ -0,0 +1,50 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "list.h" +#include "log.h" + +struct cmd_results *bar_cmd_output(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "output", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "output", "No bar defined."); + } + + const char *output = argv[0]; + list_t *outputs = config->current_bar->outputs; + if (!outputs) { + outputs = create_list(); + config->current_bar->outputs = outputs; + } + + int i; + int add_output = 1; + if (strcmp("*", output) == 0) { + // remove all previous defined outputs and replace with '*' + for (i = 0; i < outputs->length; ++i) { + free(outputs->items[i]); + list_del(outputs, i); + } + } else { + // only add output if not already defined with either the same + // name or as '*' + for (i = 0; i < outputs->length; ++i) { + const char *find = outputs->items[i]; + if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { + add_output = 0; + break; + } + } + } + + if (add_output) { + list_add(outputs, strdup(output)); + wlr_log(L_DEBUG, "Adding bar: '%s' to output '%s'", config->current_bar->id, output); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/pango_markup.c b/sway/commands/bar/pango_markup.c new file mode 100644 index 00000000..34b85e98 --- /dev/null +++ b/sway/commands/bar/pango_markup.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_pango_markup(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "pango_markup", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "pango_markup", "No bar defined."); + } + + if (strcasecmp("enabled", argv[0]) == 0) { + config->current_bar->pango_markup = true; + wlr_log(L_DEBUG, "Enabling pango markup for bar: %s", config->current_bar->id); + } else if (strcasecmp("disabled", argv[0]) == 0) { + config->current_bar->pango_markup = false; + wlr_log(L_DEBUG, "Disabling pango markup for bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "pango_markup", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/position.c b/sway/commands/bar/position.c new file mode 100644 index 00000000..efa8c0bc --- /dev/null +++ b/sway/commands/bar/position.c @@ -0,0 +1,29 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_position(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "position", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "position", "No bar defined."); + } + + char *valid[] = { "top", "bottom", "left", "right" }; + for (size_t i = 0; i < sizeof(valid) / sizeof(valid[0]); ++i) { + if (strcasecmp(valid[i], argv[0]) == 0) { + wlr_log(L_DEBUG, "Setting bar position '%s' for bar: %s", + argv[0], config->current_bar->id); + config->current_bar->position = strdup(argv[0]); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + } + + error = cmd_results_new(CMD_INVALID, "position", "Invalid value %s", argv[0]); + return error; +} diff --git a/sway/commands/bar/secondary_button.c b/sway/commands/bar/secondary_button.c new file mode 100644 index 00000000..46d53e3f --- /dev/null +++ b/sway/commands/bar/secondary_button.c @@ -0,0 +1,9 @@ +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_secondary_button(int argc, char **argv) { + const char *cmd_name = "secondary_button"; + // TODO TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); +} diff --git a/sway/commands/bar/separator_symbol.c b/sway/commands/bar/separator_symbol.c new file mode 100644 index 00000000..7dc0956b --- /dev/null +++ b/sway/commands/bar/separator_symbol.c @@ -0,0 +1,21 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_separator_symbol(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "separator_symbol", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "separator_symbol", "No bar defined."); + } + + free(config->current_bar->separator_symbol); + config->current_bar->separator_symbol = strdup(argv[0]); + wlr_log(L_DEBUG, "Settings separator_symbol '%s' for bar: %s", config->current_bar->separator_symbol, config->current_bar->id); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/status_command.c b/sway/commands/bar/status_command.c new file mode 100644 index 00000000..05377fcc --- /dev/null +++ b/sway/commands/bar/status_command.c @@ -0,0 +1,21 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_status_command(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "status_command", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "status_command", "No bar defined."); + } + + free(config->current_bar->status_command); + config->current_bar->status_command = join_args(argv, argc); + wlr_log(L_DEBUG, "Feeding bar with status command: %s", config->current_bar->status_command); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/strip_workspace_numbers.c b/sway/commands/bar/strip_workspace_numbers.c new file mode 100644 index 00000000..0558db8f --- /dev/null +++ b/sway/commands/bar/strip_workspace_numbers.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "strip_workspace_numbers", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->strip_workspace_numbers = true; + wlr_log(L_DEBUG, "Stripping workspace numbers on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->strip_workspace_numbers = false; + wlr_log(L_DEBUG, "Enabling workspace numbers on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "strip_workspace_numbers", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/swaybar_command.c b/sway/commands/bar/swaybar_command.c new file mode 100644 index 00000000..63d4f29a --- /dev/null +++ b/sway/commands/bar/swaybar_command.c @@ -0,0 +1,21 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *bar_cmd_swaybar_command(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "swaybar_command", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "swaybar_command", "No bar defined."); + } + + free(config->current_bar->swaybar_command); + config->current_bar->swaybar_command = join_args(argv, argc); + wlr_log(L_DEBUG, "Using custom swaybar command: %s", config->current_bar->swaybar_command); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/tray_output.c b/sway/commands/bar/tray_output.c new file mode 100644 index 00000000..2d822146 --- /dev/null +++ b/sway/commands/bar/tray_output.c @@ -0,0 +1,9 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" + +struct cmd_results *bar_cmd_tray_output(int argc, char **argv) { + const char *cmd_name = "tray_output"; + // TODO TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); +} diff --git a/sway/commands/bar/tray_padding.c b/sway/commands/bar/tray_padding.c new file mode 100644 index 00000000..95b8ad3b --- /dev/null +++ b/sway/commands/bar/tray_padding.c @@ -0,0 +1,10 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) { + const char *cmd_name = "tray_padding"; + // TODO TRAY + return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); +} diff --git a/sway/commands/bar/workspace_buttons.c b/sway/commands/bar/workspace_buttons.c new file mode 100644 index 00000000..1a617eb8 --- /dev/null +++ b/sway/commands/bar/workspace_buttons.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "workspace_buttons", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->workspace_buttons = true; + wlr_log(L_DEBUG, "Enabling workspace buttons on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->workspace_buttons = false; + wlr_log(L_DEBUG, "Disabling workspace buttons on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/bar/wrap_scroll.c b/sway/commands/bar/wrap_scroll.c new file mode 100644 index 00000000..c89dff5f --- /dev/null +++ b/sway/commands/bar/wrap_scroll.c @@ -0,0 +1,27 @@ +#include +#include +#include "sway/commands.h" +#include "log.h" + +struct cmd_results *bar_cmd_wrap_scroll(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "wrap_scroll", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "wrap_scroll", "No bar defined."); + } + + if (strcasecmp("yes", argv[0]) == 0) { + config->current_bar->wrap_scroll = true; + wlr_log(L_DEBUG, "Enabling wrap scroll on bar: %s", config->current_bar->id); + } else if (strcasecmp("no", argv[0]) == 0) { + config->current_bar->wrap_scroll = false; + wlr_log(L_DEBUG, "Disabling wrap scroll on bar: %s", config->current_bar->id); + } else { + error = cmd_results_new(CMD_INVALID, "wrap_scroll", "Invalid value %s", argv[0]); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 0422fdd9..a0e408de 100644 --- a/sway/config.c +++ b/sway/config.c @@ -110,6 +110,48 @@ void free_config(struct sway_config *config) { free(config); } +static void free_bar(struct bar_config *bar) { + if (!bar) { + return; + } + free(bar->mode); + free(bar->position); + free(bar->hidden_state); + free(bar->status_command); + free(bar->font); + free(bar->separator_symbol); + // TODO: Free mouse bindings + list_free(bar->bindings); + if (bar->outputs) { + free_flat_list(bar->outputs); + } + if (bar->pid != 0) { + // TODO 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); +} + static void destroy_removed_seats(struct sway_config *old_config, struct sway_config *new_config) { struct seat_config *seat_config; @@ -239,6 +281,91 @@ cleanup: sway_abort("Unable to allocate config structures"); } +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->outputs = NULL; + bar->position = strdup("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; + 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; +} + static bool file_exists(const char *path) { return path && access(path, R_OK) != -1; } diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 408ed432..59fc05f9 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -313,10 +313,18 @@ void ipc_event_window(swayc_t *window, const char *change) { 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) { + wlr_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 +} + int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { struct ipc_client *client = data; diff --git a/sway/meson.build b/sway/meson.build index 8bddb11b..8fcb0dbf 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -6,6 +6,7 @@ sway_sources = files( 'input/seat.c', 'input/cursor.c', 'input/keyboard.c', + 'commands/bar.c', 'commands/bind.c', 'commands/exit.c', 'commands/exec.c', @@ -19,6 +20,30 @@ sway_sources = files( 'commands/seat/attach.c', 'commands/seat/fallback.c', 'commands/set.c', + 'commands/bar/activate_button.c', + 'commands/bar/binding_mode_indicator.c', + 'commands/bar/bindsym.c', + 'commands/bar/colors.c', + 'commands/bar/context_button.c', + 'commands/bar/font.c', + 'commands/bar/height.c', + 'commands/bar/hidden_state.c', + 'commands/bar/icon_theme.c', + 'commands/bar/id.c', + 'commands/bar/mode.c', + 'commands/bar/modifier.c', + 'commands/bar/output.c', + 'commands/bar/pango_markup.c', + 'commands/bar/position.c', + 'commands/bar/secondary_button.c', + 'commands/bar/separator_symbol.c', + 'commands/bar/status_command.c', + 'commands/bar/strip_workspace_numbers.c', + 'commands/bar/swaybar_command.c', + 'commands/bar/tray_output.c', + 'commands/bar/tray_padding.c', + 'commands/bar/workspace_buttons.c', + 'commands/bar/wrap_scroll.c', 'commands/input/accel_profile.c', 'commands/input/click_method.c', 'commands/input/drag_lock.c', -- cgit v1.2.3 From 5c9cdbcdd2a07e2ced7b60d629a3e20bd7c8bf68 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 17:49:44 -0400 Subject: Add swaybg_command --- include/sway/commands.h | 1 + include/sway/config.h | 1 + sway/commands.c | 1 + sway/commands/swaybg_command.c | 20 ++++++++++++++++++++ sway/config/output.c | 25 +++++++++++++------------ sway/meson.build | 1 + sway/sway.5.txt | 3 +++ 7 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 sway/commands/swaybg_command.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index dda286a2..1291d5fb 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -141,6 +141,7 @@ sway_cmd cmd_splith; sway_cmd cmd_splitt; sway_cmd cmd_splitv; sway_cmd cmd_sticky; +sway_cmd cmd_swaybg_command; sway_cmd cmd_unmark; sway_cmd cmd_workspace; sway_cmd cmd_ws_auto_back_and_forth; diff --git a/include/sway/config.h b/include/sway/config.h index dbcfc91e..4a7fee0f 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -282,6 +282,7 @@ struct sway_config { list_t *active_bar_modifiers; struct sway_mode *current_mode; struct bar_config *current_bar; + char *swaybg_command; uint32_t floating_mod; uint32_t dragging_key; uint32_t resizing_key; diff --git a/sway/commands.c b/sway/commands.c index 8d8b643b..38e2f764 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -149,6 +149,7 @@ static struct cmd_handler bar_colors_handlers[] = { /* Config-time only commands. Keep alphabetized */ static struct cmd_handler config_handlers[] = { { "set", cmd_set }, + { "swaybg_command", cmd_swaybg_command }, }; /* Runtime-only commands. Keep alphabetized */ diff --git a/sway/commands/swaybg_command.c b/sway/commands/swaybg_command.c new file mode 100644 index 00000000..770d4821 --- /dev/null +++ b/sway/commands/swaybg_command.c @@ -0,0 +1,20 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_swaybg_command(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "swaybg_command", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (config->swaybg_command) { + free(config->swaybg_command); + } + config->swaybg_command = join_args(argv, argc); + wlr_log(L_DEBUG, "Using custom swaybg command: %s", + config->swaybg_command); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config/output.c b/sway/config/output.c index 24b4a18e..c3ec61b7 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -180,19 +180,20 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { wlr_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, - }; + size_t len = snprintf(NULL, 0, "%s %d %s %s", + config->swaybg_command ? config->swaybg_command : "swaybg", + output_i, oc->background, oc->background_option); + char *command = malloc(len + 1); + if (!command) { + wlr_log(L_DEBUG, "Unable to allocate swaybg command"); + return; + } + snprintf(command, len + 1, "%s %d %s %s", + config->swaybg_command ? config->swaybg_command : "swaybg", + output_i, oc->background, oc->background_option); + wlr_log(L_DEBUG, "-> %s", command); + char *const cmd[] = { "sh", "-c", command, NULL }; output->sway_output->bg_pid = fork(); if (output->sway_output->bg_pid == 0) { execvp(cmd[0], cmd); diff --git a/sway/meson.build b/sway/meson.build index ac65d05e..54c03061 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -20,6 +20,7 @@ sway_sources = files( 'commands/seat/attach.c', 'commands/seat/fallback.c', 'commands/set.c', + 'commands/swaybg_command.c', 'commands/bar/activate_button.c', 'commands/bar/binding_mode_indicator.c', 'commands/bar/bindsym.c', diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 6c9bce7a..900e499a 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -43,6 +43,9 @@ The following commands may only be used in the configuration file. Sets variable $name to _value_. You can use the new variable in the arguments of future commands. +**swaybg_command** :: + Executes custom bg command, default is _swaybg_. + The following commands cannot be used directly in the configuration file. They are expected to be used with **bindsym** or at runtime through **swaymsg**(1). -- cgit v1.2.3 From 8b5b72c576b59c875fc10f8ae88d9643c9797c39 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 18:09:42 -0400 Subject: Restart swaybar on config reload --- sway/commands/reload.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/reload.c b/sway/commands/reload.c index d54d40db..a06a9aa0 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -11,8 +11,7 @@ struct cmd_results *cmd_reload(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); } - /* load_swaybars(); -- for when it's implemented */ - + load_swaybars(); arrange_windows(&root_container, -1, -1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3 From 849c3515abff7033dbd4723fd7328cb07af74222 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 21:54:08 -0400 Subject: Use statically allocated text buffer --- common/pango.c | 6 ++---- sway/commands/bar.c | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'sway/commands') diff --git a/common/pango.c b/common/pango.c index 212d96cf..2ae7883c 100644 --- a/common/pango.c +++ b/common/pango.c @@ -32,7 +32,7 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, int32_t scale, bool markup, const char *fmt, ...) { - char *buf = malloc(2048); + static char buf[2048]; va_list args; va_start(args, fmt); @@ -45,12 +45,11 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, pango_cairo_update_layout(cairo, layout); pango_layout_get_pixel_size(layout, width, height); g_object_unref(layout); - free(buf); } void pango_printf(cairo_t *cairo, const char *font, int32_t scale, bool markup, const char *fmt, ...) { - char *buf = malloc(2048); + static char buf[2048]; va_list args; va_start(args, fmt); @@ -63,5 +62,4 @@ void pango_printf(cairo_t *cairo, const char *font, pango_cairo_update_layout(cairo, layout); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); - free(buf); } diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 548106b3..ff111163 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c @@ -36,15 +36,15 @@ struct cmd_results *cmd_bar(int argc, char **argv) { } // set bar id - int i; - for (i = 0; i < config->bars->length; ++i) { + for (int i = 0; i < config->bars->length; ++i) { if (bar == config->bars->items[i]) { const int len = 5 + numlen(i); // "bar-" + i + \0 bar->id = malloc(len * sizeof(char)); if (bar->id) { snprintf(bar->id, len, "bar-%d", i); } else { - return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID"); + return cmd_results_new(CMD_FAILURE, + "bar", "Unable to allocate bar ID"); } break; } -- cgit v1.2.3 From 741424c4e7811c12d8cca28466f89bd61eaf3a75 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 22:05:16 -0400 Subject: Clean up imported bar commands --- sway/commands/bar/activate_button.c | 3 +- sway/commands/bar/binding_mode_indicator.c | 20 +++++------ sway/commands/bar/colors.c | 51 +++++++++++++---------------- sway/commands/bar/context_button.c | 3 +- sway/commands/bar/font.c | 13 +++----- sway/commands/bar/height.c | 5 ++- sway/commands/bar/hidden_state.c | 5 --- sway/commands/bar/icon_theme.c | 3 +- sway/commands/bar/id.c | 5 +-- sway/commands/bar/mode.c | 10 ++---- sway/commands/bar/modifier.c | 8 ++--- sway/commands/bar/output.c | 15 ++++----- sway/commands/bar/pango_markup.c | 11 ++++--- sway/commands/bar/position.c | 7 ++-- sway/commands/bar/secondary_button.c | 3 +- sway/commands/bar/separator_symbol.c | 9 +++-- sway/commands/bar/status_command.c | 9 +++-- sway/commands/bar/strip_workspace_numbers.c | 18 +++++----- sway/commands/bar/swaybar_command.c | 9 +++-- sway/commands/bar/tray_output.c | 3 +- sway/commands/bar/tray_padding.c | 3 +- sway/commands/bar/workspace_buttons.c | 15 +++++---- sway/commands/bar/wrap_scroll.c | 12 +++---- 23 files changed, 104 insertions(+), 136 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/bar/activate_button.c b/sway/commands/bar/activate_button.c index 0665d2a6..7310e7ec 100644 --- a/sway/commands/bar/activate_button.c +++ b/sway/commands/bar/activate_button.c @@ -3,7 +3,6 @@ #include "log.h" struct cmd_results *bar_cmd_activate_button(int argc, char **argv) { - const char *cmd_name = "activate_button"; // TODO TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); + return cmd_results_new(CMD_INVALID, "activate_button", "TODO TRAY"); } diff --git a/sway/commands/bar/binding_mode_indicator.c b/sway/commands/bar/binding_mode_indicator.c index e11e1033..3ba5f33f 100644 --- a/sway/commands/bar/binding_mode_indicator.c +++ b/sway/commands/bar/binding_mode_indicator.c @@ -5,23 +5,23 @@ struct cmd_results *bar_cmd_binding_mode_indicator(int argc, char **argv) { struct cmd_results *error = NULL; - if ((error = checkarg(argc, "binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) { + if ((error = checkarg(argc, + "binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "binding_mode_indicator", "No bar defined."); + return cmd_results_new(CMD_FAILURE, + "binding_mode_indicator", "No bar defined."); } - if (strcasecmp("yes", argv[0]) == 0) { config->current_bar->binding_mode_indicator = true; - wlr_log(L_DEBUG, "Enabling binding mode indicator on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Enabling binding mode indicator on bar: %s", + config->current_bar->id); } else if (strcasecmp("no", argv[0]) == 0) { config->current_bar->binding_mode_indicator = false; - wlr_log(L_DEBUG, "Disabling binding mode indicator on bar: %s", config->current_bar->id); - } else { - error = cmd_results_new(CMD_INVALID, "binding_mode_indicator", "Invalid value %s", argv[0]); - return error; + wlr_log(L_DEBUG, "Disabling binding mode indicator on bar: %s", + config->current_bar->id); } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); + return cmd_results_new(CMD_INVALID, "binding_mode_indicator", + "Invalid value %s", argv[0]); } diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c index 8b3b0aac..17ba9b7c 100644 --- a/sway/commands/bar/colors.c +++ b/sway/commands/bar/colors.c @@ -1,47 +1,38 @@ #include #include "sway/commands.h" -static struct cmd_results *parse_single_color(char **color, const char *cmd_name, int argc, char **argv) { +static struct cmd_results *parse_single_color(char **color, + const char *cmd_name, int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { return error; } - - if (!*color) { - *color = malloc(10); - if (!*color) { - return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); - } + if (!*color && !(*color = malloc(10))) { + return NULL; } - error = add_color(cmd_name, *color, argv[0]); if (error) { return error; } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -static struct cmd_results *parse_three_colors(char ***colors, const char *cmd_name, int argc, char **argv) { +static struct cmd_results *parse_three_colors(char ***colors, + const char *cmd_name, int argc, char **argv) { struct cmd_results *error = NULL; if (argc != 3) { - return cmd_results_new(CMD_INVALID, cmd_name, "Requires exactly three color values"); + return cmd_results_new(CMD_INVALID, + cmd_name, "Requires exactly three color values"); } - - int i; - for (i = 0; i < 3; i++) { - if (!*colors[i]) { - *(colors[i]) = malloc(10); - if (!*(colors[i])) { - return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color"); - } + for (size_t i = 0; i < 3; i++) { + if (!*colors[i] && !(*(colors[i]) = malloc(10))) { + return NULL; } error = add_color(cmd_name, *(colors[i]), argv[i]); if (error) { return error; } } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); } @@ -50,12 +41,10 @@ struct cmd_results *bar_cmd_colors(int argc, char **argv) { if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) { return error; } - if (strcmp("{", argv[0]) != 0) { return cmd_results_new(CMD_INVALID, "colors", "Expected '{' at the start of colors config definition."); } - return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL); } @@ -69,11 +58,13 @@ struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) { } struct cmd_results *bar_colors_cmd_background(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.background), "background", argc, argv); + return parse_single_color(&(config->current_bar->colors.background), + "background", argc, argv); } struct cmd_results *bar_colors_cmd_focused_background(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.focused_background), "focused_background", argc, argv); + return parse_single_color(&(config->current_bar->colors.focused_background), + "focused_background", argc, argv); } struct cmd_results *bar_colors_cmd_binding_mode(int argc, char **argv) { @@ -104,19 +95,23 @@ struct cmd_results *bar_colors_cmd_inactive_workspace(int argc, char **argv) { } struct cmd_results *bar_colors_cmd_separator(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.separator), "separator", argc, argv); + return parse_single_color(&(config->current_bar->colors.separator), + "separator", argc, argv); } struct cmd_results *bar_colors_cmd_focused_separator(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); + return parse_single_color(&(config->current_bar->colors.focused_separator), + "focused_separator", argc, argv); } struct cmd_results *bar_colors_cmd_statusline(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.statusline), "statusline", argc, argv); + return parse_single_color(&(config->current_bar->colors.statusline), + "statusline", argc, argv); } struct cmd_results *bar_colors_cmd_focused_statusline(int argc, char **argv) { - return parse_single_color(&(config->current_bar->colors.focused_separator), "focused_separator", argc, argv); + return parse_single_color(&(config->current_bar->colors.focused_separator), + "focused_separator", argc, argv); } struct cmd_results *bar_colors_cmd_urgent_workspace(int argc, char **argv) { diff --git a/sway/commands/bar/context_button.c b/sway/commands/bar/context_button.c index e6d17f10..3b76885a 100644 --- a/sway/commands/bar/context_button.c +++ b/sway/commands/bar/context_button.c @@ -3,7 +3,6 @@ #include "log.h" struct cmd_results *bar_cmd_context_button(int argc, char **argv) { - const char *cmd_name = "context_button"; // TODO TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); + return cmd_results_new(CMD_INVALID, "context_button", "TODO TRAY"); } diff --git a/sway/commands/bar/font.c b/sway/commands/bar/font.c index 6d7c533a..80b7a593 100644 --- a/sway/commands/bar/font.c +++ b/sway/commands/bar/font.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200809L #include #include "sway/commands.h" #include "log.h" @@ -8,19 +9,13 @@ struct cmd_results *bar_cmd_font(int argc, char **argv) { if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) { return error; } - if (!config->current_bar) { return cmd_results_new(CMD_FAILURE, "font", "No bar defined."); } - char *font = join_args(argv, argc); free(config->current_bar->font); - if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) { - config->current_bar->font = font; - } else { - config->current_bar->font = font; - } - - wlr_log(L_DEBUG, "Settings font '%s' for bar: %s", config->current_bar->font, config->current_bar->id); + config->current_bar->font = strdup(font); + wlr_log(L_DEBUG, "Settings font '%s' for bar: %s", + config->current_bar->font, config->current_bar->id); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/height.c b/sway/commands/bar/height.c index ae0c7834..3160caed 100644 --- a/sway/commands/bar/height.c +++ b/sway/commands/bar/height.c @@ -8,14 +8,13 @@ struct cmd_results *bar_cmd_height(int argc, char **argv) { if ((error = checkarg(argc, "height", EXPECTED_EQUAL_TO, 1))) { return error; } - int height = atoi(argv[0]); if (height < 0) { return cmd_results_new(CMD_INVALID, "height", "Invalid height value: %s", argv[0]); } - config->current_bar->height = height; - wlr_log(L_DEBUG, "Setting bar height to %d on bar: %s", height, config->current_bar->id); + wlr_log(L_DEBUG, "Setting bar height to %d on bar: %s", + height, config->current_bar->id); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c index 245d0858..6641f184 100644 --- a/sway/commands/bar/hidden_state.c +++ b/sway/commands/bar/hidden_state.c @@ -23,7 +23,6 @@ static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, return cmd_results_new(CMD_INVALID, "hidden_state", "Invalid value %s", hidden_state); } - if (strcmp(old_state, bar->hidden_state) != 0) { if (!config->reading) { ipc_event_barconfig_update(bar); @@ -31,7 +30,6 @@ static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, wlr_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", bar->hidden_state, bar->id); } - // free old mode free(old_state); return cmd_results_new(CMD_SUCCESS, NULL, NULL); @@ -45,14 +43,12 @@ struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) { return error; } - if (config->reading && argc > 1) { return cmd_results_new(CMD_INVALID, "hidden_state", "Unexpected value %s in config mode", argv[1]); } const char *state = argv[0]; - if (config->reading) { return bar_set_hidden_state(config->current_bar, state); } @@ -61,7 +57,6 @@ struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { if (argc == 2) { id = argv[1]; } - struct bar_config *bar; for (int i = 0; i < config->bars->length; ++i) { bar = config->bars->items[i]; diff --git a/sway/commands/bar/icon_theme.c b/sway/commands/bar/icon_theme.c index c6090b81..44cd3076 100644 --- a/sway/commands/bar/icon_theme.c +++ b/sway/commands/bar/icon_theme.c @@ -3,7 +3,6 @@ #include "sway/commands.h" struct cmd_results *bar_cmd_icon_theme(int argc, char **argv) { - const char *cmd_name = "tray_output"; // TODO TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); + return cmd_results_new(CMD_INVALID, "icon_theme", "TODO TRAY"); } diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c index d9e7f8df..c1e56f03 100644 --- a/sway/commands/bar/id.c +++ b/sway/commands/bar/id.c @@ -11,10 +11,8 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) { const char *name = argv[0]; const char *oldname = config->current_bar->id; - // check if id is used by a previously defined bar - int i; - for (i = 0; i < config->bars->length; ++i) { + for (int i = 0; i < config->bars->length; ++i) { struct bar_config *find = config->bars->items[i]; if (strcmp(name, find->id) == 0 && config->current_bar != find) { return cmd_results_new(CMD_FAILURE, "id", @@ -27,7 +25,6 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) { // free old bar id free(config->current_bar->id); - config->current_bar->id = strdup(name); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c index 7d346956..34bb0a4f 100644 --- a/sway/commands/bar/mode.c +++ b/sway/commands/bar/mode.c @@ -44,13 +44,12 @@ struct cmd_results *bar_cmd_mode(int argc, char **argv) { if ((error = checkarg(argc, "mode", EXPECTED_LESS_THAN, 3))) { return error; } - if (config->reading && argc > 1) { - return cmd_results_new(CMD_INVALID, "mode", "Unexpected value %s in config mode", argv[1]); + return cmd_results_new(CMD_INVALID, + "mode", "Unexpected value %s in config mode", argv[1]); } const char *mode = argv[0]; - if (config->reading) { return bar_set_mode(config->current_bar, mode); } @@ -60,19 +59,16 @@ struct cmd_results *bar_cmd_mode(int argc, char **argv) { id = argv[1]; } - int i; struct bar_config *bar; - for (i = 0; i < config->bars->length; ++i) { + for (int i = 0; i < config->bars->length; ++i) { bar = config->bars->items[i]; if (id && strcmp(id, bar->id) == 0) { return bar_set_mode(bar, mode); } - error = bar_set_mode(bar, mode); if (error) { return error; } } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c index 9a1f8b01..7ba4b125 100644 --- a/sway/commands/bar/modifier.c +++ b/sway/commands/bar/modifier.c @@ -15,7 +15,6 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) { } uint32_t mod = 0; - list_t *split = split_string(argv[0], "+"); for (int i = 0; i < split->length; ++i) { uint32_t tmp_mod; @@ -24,12 +23,13 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) { continue; } else { free_flat_list(split); - return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]); + return cmd_results_new(CMD_INVALID, "modifier", + "Unknown modifier '%s'", split->items[i]); } } free_flat_list(split); - config->current_bar->modifier = mod; - wlr_log(L_DEBUG, "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); + wlr_log(L_DEBUG, + "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c index 034d9ca4..f7ca0aa4 100644 --- a/sway/commands/bar/output.c +++ b/sway/commands/bar/output.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 500 +#include #include #include "sway/commands.h" #include "list.h" @@ -9,7 +10,6 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { if ((error = checkarg(argc, "output", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { return cmd_results_new(CMD_FAILURE, "output", "No bar defined."); } @@ -21,21 +21,20 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { config->current_bar->outputs = outputs; } - int i; - int add_output = 1; + bool add_output = true; if (strcmp("*", output) == 0) { // remove all previous defined outputs and replace with '*' - for (i = 0; i < outputs->length; ++i) { + for (int i = 0; i < outputs->length; ++i) { free(outputs->items[i]); list_del(outputs, i); } } else { // only add output if not already defined with either the same // name or as '*' - for (i = 0; i < outputs->length; ++i) { + for (int i = 0; i < outputs->length; ++i) { const char *find = outputs->items[i]; if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { - add_output = 0; + add_output = false; break; } } @@ -43,8 +42,8 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { if (add_output) { list_add(outputs, strdup(output)); - wlr_log(L_DEBUG, "Adding bar: '%s' to output '%s'", config->current_bar->id, output); + wlr_log(L_DEBUG, "Adding bar: '%s' to output '%s'", + config->current_bar->id, output); } - return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/pango_markup.c b/sway/commands/bar/pango_markup.c index 34b85e98..480af724 100644 --- a/sway/commands/bar/pango_markup.c +++ b/sway/commands/bar/pango_markup.c @@ -8,19 +8,20 @@ struct cmd_results *bar_cmd_pango_markup(int argc, char **argv) { if ((error = checkarg(argc, "pango_markup", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { return cmd_results_new(CMD_FAILURE, "pango_markup", "No bar defined."); } - if (strcasecmp("enabled", argv[0]) == 0) { config->current_bar->pango_markup = true; - wlr_log(L_DEBUG, "Enabling pango markup for bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Enabling pango markup for bar: %s", + config->current_bar->id); } else if (strcasecmp("disabled", argv[0]) == 0) { config->current_bar->pango_markup = false; - wlr_log(L_DEBUG, "Disabling pango markup for bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Disabling pango markup for bar: %s", + config->current_bar->id); } else { - error = cmd_results_new(CMD_INVALID, "pango_markup", "Invalid value %s", argv[0]); + error = cmd_results_new(CMD_INVALID, "pango_markup", + "Invalid value %s", argv[0]); return error; } return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/bar/position.c b/sway/commands/bar/position.c index efa8c0bc..9c580483 100644 --- a/sway/commands/bar/position.c +++ b/sway/commands/bar/position.c @@ -9,11 +9,9 @@ struct cmd_results *bar_cmd_position(int argc, char **argv) { if ((error = checkarg(argc, "position", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { return cmd_results_new(CMD_FAILURE, "position", "No bar defined."); } - char *valid[] = { "top", "bottom", "left", "right" }; for (size_t i = 0; i < sizeof(valid) / sizeof(valid[0]); ++i) { if (strcasecmp(valid[i], argv[0]) == 0) { @@ -23,7 +21,6 @@ struct cmd_results *bar_cmd_position(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } } - - error = cmd_results_new(CMD_INVALID, "position", "Invalid value %s", argv[0]); - return error; + return cmd_results_new(CMD_INVALID, + "position", "Invalid value %s", argv[0]); } diff --git a/sway/commands/bar/secondary_button.c b/sway/commands/bar/secondary_button.c index 46d53e3f..449124cb 100644 --- a/sway/commands/bar/secondary_button.c +++ b/sway/commands/bar/secondary_button.c @@ -3,7 +3,6 @@ #include "log.h" struct cmd_results *bar_cmd_secondary_button(int argc, char **argv) { - const char *cmd_name = "secondary_button"; // TODO TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); + return cmd_results_new(CMD_INVALID, "secondary_button", "TODO TRAY"); } diff --git a/sway/commands/bar/separator_symbol.c b/sway/commands/bar/separator_symbol.c index 7dc0956b..1e08df6d 100644 --- a/sway/commands/bar/separator_symbol.c +++ b/sway/commands/bar/separator_symbol.c @@ -8,14 +8,13 @@ struct cmd_results *bar_cmd_separator_symbol(int argc, char **argv) { if ((error = checkarg(argc, "separator_symbol", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "separator_symbol", "No bar defined."); + return cmd_results_new(CMD_FAILURE, + "separator_symbol", "No bar defined."); } - free(config->current_bar->separator_symbol); config->current_bar->separator_symbol = strdup(argv[0]); - wlr_log(L_DEBUG, "Settings separator_symbol '%s' for bar: %s", config->current_bar->separator_symbol, config->current_bar->id); - + wlr_log(L_DEBUG, "Settings separator_symbol '%s' for bar: %s", + config->current_bar->separator_symbol, config->current_bar->id); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/status_command.c b/sway/commands/bar/status_command.c index 05377fcc..5e199cde 100644 --- a/sway/commands/bar/status_command.c +++ b/sway/commands/bar/status_command.c @@ -8,14 +8,13 @@ struct cmd_results *bar_cmd_status_command(int argc, char **argv) { if ((error = checkarg(argc, "status_command", EXPECTED_AT_LEAST, 1))) { return error; } - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "status_command", "No bar defined."); + return cmd_results_new(CMD_FAILURE, + "status_command", "No bar defined."); } - free(config->current_bar->status_command); config->current_bar->status_command = join_args(argv, argc); - wlr_log(L_DEBUG, "Feeding bar with status command: %s", config->current_bar->status_command); - + wlr_log(L_DEBUG, "Feeding bar with status command: %s", + config->current_bar->status_command); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/strip_workspace_numbers.c b/sway/commands/bar/strip_workspace_numbers.c index 0558db8f..4f24a356 100644 --- a/sway/commands/bar/strip_workspace_numbers.c +++ b/sway/commands/bar/strip_workspace_numbers.c @@ -5,23 +5,25 @@ struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv) { struct cmd_results *error = NULL; - if ((error = checkarg(argc, "strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) { + if ((error = checkarg(argc, + "strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "strip_workspace_numbers", "No bar defined."); + return cmd_results_new(CMD_FAILURE, + "strip_workspace_numbers", "No bar defined."); } - if (strcasecmp("yes", argv[0]) == 0) { config->current_bar->strip_workspace_numbers = true; - wlr_log(L_DEBUG, "Stripping workspace numbers on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Stripping workspace numbers on bar: %s", + config->current_bar->id); } else if (strcasecmp("no", argv[0]) == 0) { config->current_bar->strip_workspace_numbers = false; - wlr_log(L_DEBUG, "Enabling workspace numbers on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Enabling workspace numbers on bar: %s", + config->current_bar->id); } else { - error = cmd_results_new(CMD_INVALID, "strip_workspace_numbers", "Invalid value %s", argv[0]); - return error; + return cmd_results_new(CMD_INVALID, + "strip_workspace_numbers", "Invalid value %s", argv[0]); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/swaybar_command.c b/sway/commands/bar/swaybar_command.c index 63d4f29a..520cdd11 100644 --- a/sway/commands/bar/swaybar_command.c +++ b/sway/commands/bar/swaybar_command.c @@ -8,14 +8,13 @@ struct cmd_results *bar_cmd_swaybar_command(int argc, char **argv) { if ((error = checkarg(argc, "swaybar_command", EXPECTED_AT_LEAST, 1))) { return error; } - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "swaybar_command", "No bar defined."); + return cmd_results_new(CMD_FAILURE, + "swaybar_command", "No bar defined."); } - free(config->current_bar->swaybar_command); config->current_bar->swaybar_command = join_args(argv, argc); - wlr_log(L_DEBUG, "Using custom swaybar command: %s", config->current_bar->swaybar_command); - + wlr_log(L_DEBUG, "Using custom swaybar command: %s", + config->current_bar->swaybar_command); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/tray_output.c b/sway/commands/bar/tray_output.c index 2d822146..6ab16731 100644 --- a/sway/commands/bar/tray_output.c +++ b/sway/commands/bar/tray_output.c @@ -3,7 +3,6 @@ #include "sway/commands.h" struct cmd_results *bar_cmd_tray_output(int argc, char **argv) { - const char *cmd_name = "tray_output"; // TODO TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); + return cmd_results_new(CMD_INVALID, "tray_output", "TODO TRAY"); } diff --git a/sway/commands/bar/tray_padding.c b/sway/commands/bar/tray_padding.c index 95b8ad3b..91c56f19 100644 --- a/sway/commands/bar/tray_padding.c +++ b/sway/commands/bar/tray_padding.c @@ -4,7 +4,6 @@ #include "log.h" struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) { - const char *cmd_name = "tray_padding"; // TODO TRAY - return cmd_results_new(CMD_INVALID, cmd_name, "TODO TRAY"); + return cmd_results_new(CMD_INVALID, "tray_padding", "TODO TRAY"); } diff --git a/sway/commands/bar/workspace_buttons.c b/sway/commands/bar/workspace_buttons.c index 1a617eb8..6edc3a0d 100644 --- a/sway/commands/bar/workspace_buttons.c +++ b/sway/commands/bar/workspace_buttons.c @@ -8,20 +8,21 @@ struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { - return cmd_results_new(CMD_FAILURE, "workspace_buttons", "No bar defined."); + return cmd_results_new(CMD_FAILURE, + "workspace_buttons", "No bar defined."); } - if (strcasecmp("yes", argv[0]) == 0) { config->current_bar->workspace_buttons = true; - wlr_log(L_DEBUG, "Enabling workspace buttons on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Enabling workspace buttons on bar: %s", + config->current_bar->id); } else if (strcasecmp("no", argv[0]) == 0) { config->current_bar->workspace_buttons = false; - wlr_log(L_DEBUG, "Disabling workspace buttons on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Disabling workspace buttons on bar: %s", + config->current_bar->id); } else { - error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]); - return error; + return cmd_results_new(CMD_INVALID, "workspace_buttons", + "Invalid value %s", argv[0]); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar/wrap_scroll.c b/sway/commands/bar/wrap_scroll.c index c89dff5f..7386f82c 100644 --- a/sway/commands/bar/wrap_scroll.c +++ b/sway/commands/bar/wrap_scroll.c @@ -8,20 +8,20 @@ struct cmd_results *bar_cmd_wrap_scroll(int argc, char **argv) { if ((error = checkarg(argc, "wrap_scroll", EXPECTED_EQUAL_TO, 1))) { return error; } - if (!config->current_bar) { return cmd_results_new(CMD_FAILURE, "wrap_scroll", "No bar defined."); } - if (strcasecmp("yes", argv[0]) == 0) { config->current_bar->wrap_scroll = true; - wlr_log(L_DEBUG, "Enabling wrap scroll on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Enabling wrap scroll on bar: %s", + config->current_bar->id); } else if (strcasecmp("no", argv[0]) == 0) { config->current_bar->wrap_scroll = false; - wlr_log(L_DEBUG, "Disabling wrap scroll on bar: %s", config->current_bar->id); + wlr_log(L_DEBUG, "Disabling wrap scroll on bar: %s", + config->current_bar->id); } else { - error = cmd_results_new(CMD_INVALID, "wrap_scroll", "Invalid value %s", argv[0]); - return error; + return cmd_results_new(CMD_INVALID, + "wrap_scroll", "Invalid value %s", argv[0]); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3 From 8efee109ad2ab4861f25e54e9f6d1ceb06203791 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 22:10:33 -0400 Subject: Implement modes --- include/sway/ipc-server.h | 1 + sway/commands.c | 1 + sway/commands/mode.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ sway/ipc-server.c | 14 +++++++++-- sway/meson.build | 1 + 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 sway/commands/mode.c (limited to 'sway/commands') diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index 1f6fffff..b4db75c3 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h @@ -13,5 +13,6 @@ struct sockaddr_un *ipc_user_sockaddr(void); void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change); void ipc_event_window(swayc_t *window, const char *change); void ipc_event_barconfig_update(struct bar_config *bar); +void ipc_event_mode(const char *mode); #endif diff --git a/sway/commands.c b/sway/commands.c index 38e2f764..bcc777ed 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -100,6 +100,7 @@ static struct cmd_handler handlers[] = { { "exec_always", cmd_exec_always }, { "include", cmd_include }, { "input", cmd_input }, + { "mode", cmd_mode }, { "output", cmd_output }, { "seat", cmd_seat }, { "workspace", cmd_workspace }, diff --git a/sway/commands/mode.c b/sway/commands/mode.c new file mode 100644 index 00000000..c30a8bac --- /dev/null +++ b/sway/commands/mode.c @@ -0,0 +1,59 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/ipc-server.h" +#include "list.h" +#include "log.h" + +struct cmd_results *cmd_mode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { + return error; + } + + const char *mode_name = argv[0]; + bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0); + if (new_mode && !config->reading) { + return cmd_results_new(CMD_FAILURE, + "mode", "Can only be used in config file."); + } + struct sway_mode *mode = NULL; + // Find mode + for (int i = 0; i < config->modes->length; ++i) { + struct sway_mode *test = config->modes->items[i]; + if (strcasecmp(test->name, mode_name) == 0) { + mode = test; + break; + } + } + // Create mode if it doesn't exist + if (!mode && new_mode) { + mode = calloc(1, sizeof(struct sway_mode)); + if (!mode) { + return cmd_results_new(CMD_FAILURE, + "mode", "Unable to allocate mode"); + } + mode->name = strdup(mode_name); + mode->keysym_bindings = create_list(); + mode->keycode_bindings = create_list(); + list_add(config->modes, mode); + } + if (!mode) { + error = cmd_results_new(CMD_INVALID, + "mode", "Unknown mode `%s'", mode_name); + return error; + } + if ((config->reading && new_mode) || (!config->reading && !new_mode)) { + wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name); + } + // Set current mode + config->current_mode = mode; + if (!new_mode) { + // trigger IPC mode event + ipc_event_mode(config->current_mode->name); + } + return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 8250b3a0..c3b589a6 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -290,7 +290,7 @@ void ipc_event_window(swayc_t *window, const char *change) { const char *json_string = json_object_to_json_string(obj); ipc_send_event(json_string, IPC_EVENT_WINDOW); - json_object_put(obj); // free + json_object_put(obj); } void ipc_event_barconfig_update(struct bar_config *bar) { @@ -299,7 +299,17 @@ void ipc_event_barconfig_update(struct 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 + json_object_put(json); +} + +void ipc_event_mode(const char *mode) { + wlr_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); } int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { diff --git a/sway/meson.build b/sway/meson.build index 54c03061..1e7ee7ae 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -16,6 +16,7 @@ sway_sources = files( 'commands/include.c', 'commands/input.c', 'commands/layout.c', + 'commands/mode.c', 'commands/seat.c', 'commands/seat/attach.c', 'commands/seat/fallback.c', -- cgit v1.2.3 From 2778edef976a669dd0019ebb5327bcfeb4de13c5 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 29 Mar 2018 23:15:39 -0400 Subject: arrange windows --- include/sway/tree/layout.h | 2 +- sway/commands/layout.c | 2 +- sway/commands/reload.c | 2 +- sway/desktop/layer_shell.c | 2 +- sway/desktop/output.c | 4 ++-- sway/desktop/wl_shell.c | 4 ++-- sway/desktop/xdg_shell_v6.c | 4 ++-- sway/desktop/xwayland.c | 8 ++++---- sway/tree/container.c | 2 +- sway/tree/layout.c | 16 ++++++++-------- sway/tree/workspace.c | 2 +- 11 files changed, 24 insertions(+), 24 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 6980ec9e..ad52bdb0 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -43,7 +43,7 @@ enum sway_container_layout container_get_default_layout(struct sway_container *o void container_sort_workspaces(struct sway_container *output); -void container_arrange_windows(struct sway_container *container, +void arrange_windows(struct sway_container *container, double width, double height); struct sway_container *container_get_in_direction(struct sway_container diff --git a/sway/commands/layout.c b/sway/commands/layout.c index e9cfeb8f..ebab2a48 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -50,7 +50,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } } - container_arrange_windows(parent, parent->width, parent->height); + arrange_windows(parent, parent->width, parent->height); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/reload.c b/sway/commands/reload.c index 83ecd75b..8cef789b 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -13,6 +13,6 @@ struct cmd_results *cmd_reload(int argc, char **argv) { /* load_swaybars(); -- for when it's implemented */ - container_arrange_windows(&root_container, -1, -1); + arrange_windows(&root_container, -1, -1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index 4bfd1c45..137b3260 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -172,7 +172,7 @@ void arrange_layers(struct sway_output *output) { if (memcmp(&usable_area_before, &usable_area, sizeof(struct wlr_box)) != 0) { wlr_log(L_DEBUG, "arrange"); - container_arrange_windows(output->swayc, -1, -1); + arrange_windows(output->swayc, -1, -1); } // Arrange non-exlusive surfaces from top->bottom diff --git a/sway/desktop/output.c b/sway/desktop/output.c index fa1b0680..b463dfdc 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -264,7 +264,7 @@ static void handle_output_destroy(struct wl_listener *listener, void *data) { static void handle_output_mode(struct wl_listener *listener, void *data) { struct sway_output *output = wl_container_of(listener, output, mode); arrange_layers(output); - container_arrange_windows(output->swayc, -1, -1); + arrange_windows(output->swayc, -1, -1); } void handle_new_output(struct wl_listener *listener, void *data) { @@ -307,5 +307,5 @@ void handle_new_output(struct wl_listener *listener, void *data) { output->mode.notify = handle_output_mode; arrange_layers(output); - container_arrange_windows(&root_container, -1, -1); + arrange_windows(&root_container, -1, -1); } diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index ac1c7f26..4d4d1ed7 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -77,7 +77,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); free(sway_surface->view); free(sway_surface); - container_arrange_windows(parent, -1, -1); + arrange_windows(parent, -1, -1); } void handle_wl_shell_surface(struct wl_listener *listener, void *data) { @@ -136,6 +136,6 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; - container_arrange_windows(cont->parent, -1, -1); + arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 616cb88f..09894f0e 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -86,7 +86,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { struct sway_container *parent = container_view_destroy(sway_xdg_surface->view->swayc); free(sway_xdg_surface->view); free(sway_xdg_surface); - container_arrange_windows(parent, -1, -1); + arrange_windows(parent, -1, -1); } void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { @@ -140,7 +140,7 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; - container_arrange_windows(cont->parent, -1, -1); + arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index fa1054f2..fd0bcaca 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -116,7 +116,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); if (parent) { - container_arrange_windows(parent, -1, -1); + arrange_windows(parent, -1, -1); } free(sway_surface->view); @@ -134,7 +134,7 @@ static void handle_unmap_notify(struct wl_listener *listener, void *data) { // take it out of the tree struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); if (parent) { - container_arrange_windows(parent, -1, -1); + arrange_windows(parent, -1, -1); } sway_surface->view->swayc = NULL; @@ -163,7 +163,7 @@ static void handle_map_notify(struct wl_listener *listener, void *data) { struct sway_container *cont = container_view_create(parent, view); view->swayc = cont; - container_arrange_windows(cont->parent, -1, -1); + arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } } @@ -242,6 +242,6 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; - container_arrange_windows(cont->parent, -1, -1); + arrange_windows(cont->parent, -1, -1); sway_input_manager_set_focus(input_manager, cont); } diff --git a/sway/tree/container.c b/sway/tree/container.c index fe97d645..40047dcf 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -221,7 +221,7 @@ struct sway_container *container_output_destroy(struct sway_container *output) { container_add_child(root_container.children->items[p], child); } container_sort_workspaces(root_container.children->items[p]); - container_arrange_windows(root_container.children->items[p], + arrange_windows(root_container.children->items[p], -1, -1); } } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 614d0505..dc0ee5b4 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -42,7 +42,7 @@ static void output_layout_change_notify(struct wl_listener *listener, output_container->height = output_box->height; } - container_arrange_windows(&root_container, -1, -1); + arrange_windows(&root_container, -1, -1); } void layout_init(void) { @@ -167,7 +167,7 @@ static void apply_vert_layout(struct sway_container *container, const double x, const double height, const int start, const int end); -void container_arrange_windows(struct sway_container *container, +void arrange_windows(struct sway_container *container, double width, double height) { int i; if (width == -1 || height == -1) { @@ -192,7 +192,7 @@ void container_arrange_windows(struct sway_container *container, struct sway_container *output = container->children->items[i]; wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); - container_arrange_windows(output, -1, -1); + arrange_windows(output, -1, -1); } return; case C_OUTPUT: @@ -206,7 +206,7 @@ void container_arrange_windows(struct sway_container *container, // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { struct sway_container *child = container->children->items[i]; - container_arrange_windows(child, -1, -1); + arrange_windows(child, -1, -1); } return; case C_WORKSPACE: @@ -294,9 +294,9 @@ static void apply_horiz_layout(struct sway_container *container, if (i == end - 1) { double remaining_width = x + width - child_x; - container_arrange_windows(child, remaining_width, height); + arrange_windows(child, remaining_width, height); } else { - container_arrange_windows(child, child->width * scale, height); + arrange_windows(child, child->width * scale, height); } child_x += child->width; } @@ -345,9 +345,9 @@ void apply_vert_layout(struct sway_container *container, if (i == end - 1) { double remaining_height = y + height - child_y; - container_arrange_windows(child, width, remaining_height); + arrange_windows(child, width, remaining_height); } else { - container_arrange_windows(child, width, child->height * scale); + arrange_windows(child, width, child->height * scale); } child_y += child->height; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 369cf14c..ba04c55c 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -247,6 +247,6 @@ bool workspace_switch(struct sway_container *workspace) { } sway_seat_set_focus(seat, next); struct sway_container *output = container_parent(workspace, C_OUTPUT); - container_arrange_windows(output, -1, -1); + arrange_windows(output, -1, -1); return true; } -- cgit v1.2.3 From d0c7f66e950689b70196a890b62b82ff3c66e103 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 29 Mar 2018 23:29:29 -0400 Subject: Revert "Refactor tree" --- include/sway/config.h | 17 ++- include/sway/container.h | 169 ++++++++++++++++++++++++++++ include/sway/criteria.h | 6 +- include/sway/input/input-manager.h | 4 +- include/sway/input/seat.h | 13 +-- include/sway/ipc-json.h | 6 +- include/sway/ipc-server.h | 6 +- include/sway/layout.h | 43 ++++++++ include/sway/tree/container.h | 137 ----------------------- include/sway/tree/layout.h | 52 --------- include/sway/tree/view.h | 116 ------------------- include/sway/tree/workspace.h | 26 ----- include/sway/view.h | 120 ++++++++++++++++++++ include/sway/workspace.h | 20 ++++ sway/commands/exec_always.c | 4 +- sway/commands/focus.c | 9 +- sway/commands/kill.c | 4 +- sway/commands/layout.c | 20 ++-- sway/commands/output.c | 2 +- sway/commands/reload.c | 2 +- sway/commands/workspace.c | 14 +-- sway/config.c | 2 +- sway/config/output.c | 4 +- sway/criteria.c | 16 +-- sway/desktop/layer_shell.c | 2 +- sway/desktop/output.c | 20 ++-- sway/desktop/wl_shell.c | 12 +- sway/desktop/xdg_shell_v6.c | 12 +- sway/desktop/xwayland.c | 24 ++-- sway/input/cursor.c | 10 +- sway/input/input-manager.c | 4 +- sway/input/seat.c | 38 +++---- sway/ipc-json.c | 16 +-- sway/ipc-server.c | 4 +- sway/main.c | 4 +- sway/tree/container.c | 221 ++++++++++++++++++++----------------- sway/tree/layout.c | 133 +++++++++------------- sway/tree/view.c | 7 +- sway/tree/workspace.c | 92 +++++++-------- swaybar/ipc.c | 2 +- 40 files changed, 704 insertions(+), 709 deletions(-) create mode 100644 include/sway/container.h create mode 100644 include/sway/layout.h delete mode 100644 include/sway/tree/container.h delete mode 100644 include/sway/tree/layout.h delete mode 100644 include/sway/tree/view.h delete mode 100644 include/sway/tree/workspace.h create mode 100644 include/sway/view.h create mode 100644 include/sway/workspace.h (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 7fdd0be0..48a8b0ab 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -10,8 +10,8 @@ #include #include #include "list.h" -#include "tree/layout.h" -#include "tree/container.h" +#include "layout.h" +#include "container.h" /** * Describes a variable created via the `set` command. @@ -299,8 +299,8 @@ struct sway_config { char *floating_scroll_down_cmd; char *floating_scroll_left_cmd; char *floating_scroll_right_cmd; - enum sway_container_layout default_orientation; - enum sway_container_layout default_layout; + enum swayc_layouts default_orientation; + enum swayc_layouts default_layout; char *font; int font_height; @@ -324,8 +324,8 @@ struct sway_config { list_t *config_chain; const char *current_config; - enum sway_container_border border; - enum sway_container_border floating_border; + enum swayc_border_types border; + enum swayc_border_types floating_border; int border_thickness; int floating_border_thickness; enum edge_border_types hide_edge_borders; @@ -356,7 +356,7 @@ struct sway_config { struct input_config *input_config; struct seat_config *seat_config; struct sway_seat *seat; - struct sway_container *current_container; + swayc_t *current_container; } handler_context; }; @@ -416,8 +416,7 @@ void output_get_identifier(char *identifier, size_t len, struct sway_output *output); struct output_config *new_output_config(const char *name); void merge_output_config(struct output_config *dst, struct output_config *src); -void apply_output_config(struct output_config *oc, - struct sway_container *output); +void apply_output_config(struct output_config *oc, swayc_t *output); void free_output_config(struct output_config *oc); /** diff --git a/include/sway/container.h b/include/sway/container.h new file mode 100644 index 00000000..f200a1a2 --- /dev/null +++ b/include/sway/container.h @@ -0,0 +1,169 @@ +#ifndef _SWAY_CONTAINER_H +#define _SWAY_CONTAINER_H +#include +#include +#include +#include +#include "list.h" + +typedef struct sway_container swayc_t; + +extern swayc_t root_container; + +struct sway_view; +struct sway_seat; + +/** + * Different kinds of containers. + * + * This enum is in order. A container will never be inside of a container below + * it on this list. + */ +enum swayc_types { + C_ROOT, /**< The root container. Only one of these ever exists. */ + C_OUTPUT, /**< An output (aka monitor, head, etc). */ + C_WORKSPACE, /**< A workspace. */ + C_CONTAINER, /**< A manually created container. */ + C_VIEW, /**< A view (aka window). */ + + C_TYPES, +}; + +/** + * Different ways to arrange a container. + */ +enum swayc_layouts { + L_NONE, /**< Used for containers that have no layout (views, root) */ + L_HORIZ, + L_VERT, + L_STACKED, + L_TABBED, + L_FLOATING, /**< A psuedo-container, removed from the tree, to hold floating windows */ + + /* Awesome/Monad style auto layouts */ + L_AUTO_LEFT, + L_AUTO_RIGHT, + L_AUTO_TOP, + L_AUTO_BOTTOM, + + L_AUTO_FIRST = L_AUTO_LEFT, + L_AUTO_LAST = L_AUTO_BOTTOM, + + // Keep last + L_LAYOUTS, +}; + +enum swayc_border_types { + B_NONE, /**< No border */ + B_PIXEL, /**< 1px border */ + B_NORMAL, /**< Normal border with title bar */ +}; + +struct sway_root; +struct sway_output; +struct sway_view; + +/** + * Stores information about a container. + * + * The tree is made of these. Views are containers that cannot have children. + */ +struct sway_container { + union { + // TODO: Encapsulate state for other node types as well like C_CONTAINER + struct sway_root *sway_root; // C_ROOT + struct sway_output *sway_output; // C_OUTPUT + struct sway_view *sway_view; // C_VIEW + }; + + /** + * A unique ID to identify this container. Primarily used in the + * get_tree JSON output. + */ + size_t id; + + char *name; + + enum swayc_types type; + enum swayc_layouts layout; + enum swayc_layouts prev_layout; + enum swayc_layouts workspace_layout; + + /** + * The coordinates that this view appear at, relative to the output they + * are located on (output containers have absolute coordinates). + */ + double x, y; + + /** + * Width and height of this container, without borders or gaps. + */ + double width, height; + + list_t *children; + + /** + * The parent of this container. NULL for the root container. + */ + struct sway_container *parent; + + /** + * Number of master views in auto layouts. + */ + size_t nb_master; + + /** + * Number of slave groups (e.g. columns) in auto layouts. + */ + size_t nb_slave_groups; + + /** + * Marks applied to the container, list_t of char*. + */ + list_t *marks; + + struct { + struct wl_signal destroy; + } events; +}; + +void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, + void (*func)(swayc_t *item, void *data), void *data); + +swayc_t *new_output(struct sway_output *sway_output); +swayc_t *new_workspace(swayc_t *output, const char *name); +swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); + +swayc_t *destroy_output(swayc_t *output); +swayc_t *destroy_view(swayc_t *view); + +swayc_t *next_view_sibling(struct sway_seat *seat); + +/** + * Finds a container based on test criteria. Returns the first container that + * passes the test. + */ +swayc_t *swayc_by_test(swayc_t *container, + bool (*test)(swayc_t *view, void *data), void *data); +/** + * Finds a parent container with the given swayc_type. + */ +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); +/** + * Maps a container's children over a function. + */ +void container_map(swayc_t *container, + void (*f)(swayc_t *view, void *data), void *data); + +swayc_t *swayc_at(swayc_t *parent, double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy); + +/** + * Apply the function for each child of the container breadth first. + */ +void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), + void *data); + +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); + +#endif diff --git a/include/sway/criteria.h b/include/sway/criteria.h index ec256ddb..9b4b4bef 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -1,7 +1,7 @@ #ifndef _SWAY_CRITERIA_H #define _SWAY_CRITERIA_H -#include "tree/container.h" +#include "container.h" #include "list.h" /** @@ -31,12 +31,12 @@ char *extract_crit_tokens(list_t *tokens, const char *criteria); // Returns list of criteria that match given container. These criteria have // been set with `for_window` commands and have an associated cmdlist. -list_t *criteria_for(struct sway_container *cont); +list_t *criteria_for(swayc_t *cont); // Returns a list of all containers that match the given list of tokens. list_t *container_for_crit_tokens(list_t *tokens); // Returns true if any criteria in the given list matches this container -bool criteria_any(struct sway_container *cont, list_t *criteria); +bool criteria_any(swayc_t *cont, list_t *criteria); #endif diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index c6c73dba..eab7dc90 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -31,10 +31,10 @@ struct sway_input_manager *sway_input_manager_create( struct sway_server *server); bool sway_input_manager_has_focus(struct sway_input_manager *input, - struct sway_container *container); + swayc_t *container); void sway_input_manager_set_focus(struct sway_input_manager *input, - struct sway_container *container); + swayc_t *container); void sway_input_manager_configure_xcursor(struct sway_input_manager *input); diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index 496bfd5d..1d55bec7 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -14,7 +14,7 @@ struct sway_seat_device { struct sway_seat_container { struct sway_seat *seat; - struct sway_container *container; + swayc_t *container; struct wl_list link; // sway_seat::focus_stack @@ -54,9 +54,9 @@ void sway_seat_remove_device(struct sway_seat *seat, void sway_seat_configure_xcursor(struct sway_seat *seat); -void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *container); +void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); -struct sway_container *sway_seat_get_focus(struct sway_seat *seat); +swayc_t *sway_seat_get_focus(struct sway_seat *seat); /** * Return the last container to be focused for the seat (or the most recently @@ -67,11 +67,10 @@ struct sway_container *sway_seat_get_focus(struct sway_seat *seat); * is destroyed, or focus moves to a container with children and we need to * descend into the next leaf in focus order. */ -struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, - struct sway_container *container); +swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container); -struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum sway_container_type type); +swayc_t *sway_seat_get_focus_by_type(struct sway_seat *seat, + enum swayc_types type); void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); diff --git a/include/sway/ipc-json.h b/include/sway/ipc-json.h index 3d2fdc4f..eef5a018 100644 --- a/include/sway/ipc-json.h +++ b/include/sway/ipc-json.h @@ -1,13 +1,13 @@ #ifndef _SWAY_IPC_JSON_H #define _SWAY_IPC_JSON_H #include -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/input/input-manager.h" json_object *ipc_json_get_version(); -json_object *ipc_json_describe_container(struct sway_container *c); -json_object *ipc_json_describe_container_recursive(struct sway_container *c); +json_object *ipc_json_describe_container(swayc_t *c); +json_object *ipc_json_describe_container_recursive(swayc_t *c); json_object *ipc_json_describe_input(struct sway_input_device *device); #endif diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index d73006dc..bcf1c433 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h @@ -1,17 +1,15 @@ #ifndef _SWAY_IPC_SERVER_H #define _SWAY_IPC_SERVER_H #include -#include "sway/tree/container.h" +#include "sway/container.h" #include "ipc.h" struct sway_server; void ipc_init(struct sway_server *server); - void ipc_terminate(void); - struct sockaddr_un *ipc_user_sockaddr(void); -void ipc_event_window(struct sway_container *window, const char *change); +void ipc_event_window(swayc_t *window, const char *change); #endif diff --git a/include/sway/layout.h b/include/sway/layout.h new file mode 100644 index 00000000..e82c4442 --- /dev/null +++ b/include/sway/layout.h @@ -0,0 +1,43 @@ +#ifndef _SWAY_LAYOUT_H +#define _SWAY_LAYOUT_H + +#include +#include "sway/container.h" + +enum movement_direction { + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN, + MOVE_PARENT, + MOVE_CHILD, + MOVE_NEXT, + MOVE_PREV, + MOVE_FIRST +}; + +struct sway_container; + +struct sway_root { + struct wlr_output_layout *output_layout; + + struct wl_listener output_layout_change; + + struct wl_list unmanaged_views; // sway_view::unmanaged_view_link + + struct { + struct wl_signal new_container; + } events; +}; + +void init_layout(void); +void add_child(struct sway_container *parent, struct sway_container *child); +swayc_t *add_sibling(swayc_t *parent, swayc_t *child); +struct sway_container *remove_child(struct sway_container *child); +enum swayc_layouts default_layout(struct sway_container *output); +void sort_workspaces(struct sway_container *output); +void arrange_windows(struct sway_container *container, double width, double height); +swayc_t *get_swayc_in_direction(swayc_t *container, + struct sway_seat *seat, enum movement_direction dir); + +#endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h deleted file mode 100644 index 16df3ee7..00000000 --- a/include/sway/tree/container.h +++ /dev/null @@ -1,137 +0,0 @@ -#ifndef _SWAY_CONTAINER_H -#define _SWAY_CONTAINER_H -#include -#include -#include -#include -#include "list.h" - -extern struct sway_container root_container; - -struct sway_view; -struct sway_seat; - -/** - * Different kinds of containers. - * - * This enum is in order. A container will never be inside of a container below - * it on this list. - */ -enum sway_container_type { - C_ROOT, - C_OUTPUT, - C_WORKSPACE, - C_CONTAINER, - C_VIEW, - - C_TYPES, -}; - -enum sway_container_layout { - L_NONE, - L_HORIZ, - L_VERT, - L_STACKED, - L_TABBED, - L_FLOATING, - - // Keep last - L_LAYOUTS, -}; - -enum sway_container_border { - B_NONE, - B_PIXEL, - B_NORMAL, -}; - -struct sway_root; -struct sway_output; -struct sway_view; - -struct sway_container { - union { - // TODO: Encapsulate state for other node types as well like C_CONTAINER - struct sway_root *sway_root; - struct sway_output *sway_output; - struct sway_view *sway_view; - }; - - /** - * A unique ID to identify this container. Primarily used in the - * get_tree JSON output. - */ - size_t id; - - char *name; - - enum sway_container_type type; - enum sway_container_layout layout; - enum sway_container_layout prev_layout; - enum sway_container_layout workspace_layout; - - // TODO convert to layout coordinates - double x, y; - - // does not include borders or gaps. - double width, height; - - list_t *children; - - struct sway_container *parent; - - list_t *marks; // list of char* - - struct { - struct wl_signal destroy; - } events; -}; - -// TODO only one container create function and pass the type? -struct sway_container *container_output_create( - struct sway_output *sway_output); - -struct sway_container *container_workspace_create( - struct sway_container *output, const char *name); - -struct sway_container *container_view_create( - struct sway_container *sibling, struct sway_view *sway_view); - -struct sway_container *container_output_destroy(struct sway_container *output); - -struct sway_container *container_view_destroy(struct sway_container *view); - -struct sway_container *container_set_layout(struct sway_container *container, - enum sway_container_layout layout); - -void container_descendents(struct sway_container *root, - enum sway_container_type type, - void (*func)(struct sway_container *item, void *data), void *data); - -/** - * Finds a container based on test criteria. Returns the first container that - * passes the test. - */ -struct sway_container *container_find(struct sway_container *container, - bool (*test)(struct sway_container *view, void *data), void *data); - -/** - * Finds a parent container with the given struct sway_containerype. - */ -struct sway_container *container_parent(struct sway_container *container, - enum sway_container_type type); - -/** - * Find a container at the given coordinates. - */ -struct sway_container *container_at(struct sway_container *parent, - double lx, double ly, struct wlr_surface **surface, - double *sx, double *sy); - -/** - * Apply the function for each child of the container breadth first. - */ -void container_for_each_descendent(struct sway_container *container, - void (*f)(struct sway_container *container, void *data), void *data); - -#endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h deleted file mode 100644 index ad52bdb0..00000000 --- a/include/sway/tree/layout.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef _SWAY_LAYOUT_H -#define _SWAY_LAYOUT_H - -#include -#include "sway/tree/container.h" - -enum movement_direction { - MOVE_LEFT, - MOVE_RIGHT, - MOVE_UP, - MOVE_DOWN, - MOVE_PARENT, - MOVE_CHILD, - MOVE_NEXT, - MOVE_PREV, - MOVE_FIRST -}; - -struct sway_container; - -struct sway_root { - struct wlr_output_layout *output_layout; - - struct wl_listener output_layout_change; - - struct wl_list unmanaged_views; // sway_view::unmanaged_view_link - - struct { - struct wl_signal new_container; - } events; -}; - -void layout_init(void); - -void container_add_child(struct sway_container *parent, struct sway_container *child); - -struct sway_container *container_add_sibling(struct sway_container *parent, - struct sway_container *child); - -struct sway_container *container_remove_child(struct sway_container *child); - -enum sway_container_layout container_get_default_layout(struct sway_container *output); - -void container_sort_workspaces(struct sway_container *output); - -void arrange_windows(struct sway_container *container, - double width, double height); - -struct sway_container *container_get_in_direction(struct sway_container - *container, struct sway_seat *seat, enum movement_direction dir); - -#endif diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h deleted file mode 100644 index e5f53f4e..00000000 --- a/include/sway/tree/view.h +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef _SWAY_VIEW_H -#define _SWAY_VIEW_H -#include -#include -#include -#include - -struct sway_container; -struct sway_view; - -struct sway_xdg_surface_v6 { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -struct sway_xwayland_surface { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener request_configure; - struct wl_listener unmap_notify; - struct wl_listener map_notify; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -struct sway_wl_shell_surface { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -enum sway_view_type { - SWAY_WL_SHELL_VIEW, - SWAY_XDG_SHELL_V6_VIEW, - SWAY_XWAYLAND_VIEW, - // Keep last - SWAY_VIEW_TYPES, -}; - -enum sway_view_prop { - VIEW_PROP_TITLE, - VIEW_PROP_APP_ID, - VIEW_PROP_CLASS, - VIEW_PROP_INSTANCE, -}; - -struct sway_view { - enum sway_view_type type; - struct sway_container *swayc; - struct wlr_surface *surface; - int width, height; - - union { - struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; - struct wlr_xwayland_surface *wlr_xwayland_surface; - struct wlr_wl_shell_surface *wlr_wl_shell_surface; - }; - - union { - struct sway_xdg_surface_v6 *sway_xdg_surface_v6; - struct sway_xwayland_surface *sway_xwayland_surface; - struct sway_wl_shell_surface *sway_wl_shell_surface; - }; - - struct { - const char *(*get_prop)(struct sway_view *view, - enum sway_view_prop prop); - void (*set_size)(struct sway_view *view, - int width, int height); - void (*set_position)(struct sway_view *view, - double ox, double oy); - void (*set_activated)(struct sway_view *view, bool activated); - void (*close)(struct sway_view *view); - } iface; - - // only used for unmanaged views (shell specific) - struct wl_list unmanaged_view_link; // sway_root::unmanaged views -}; - -const char *view_get_title(struct sway_view *view); - -const char *view_get_app_id(struct sway_view *view); - -const char *view_get_class(struct sway_view *view); - -const char *view_get_instance(struct sway_view *view); - -void view_set_size(struct sway_view *view, int width, int height); - -void view_set_position(struct sway_view *view, double ox, double oy); - -void view_set_activated(struct sway_view *view, bool activated); - -void view_close(struct sway_view *view); - -void view_update_outputs(struct sway_view *view, const struct wlr_box *before); - -#endif diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h deleted file mode 100644 index d73b29c1..00000000 --- a/include/sway/tree/workspace.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _SWAY_WORKSPACE_H -#define _SWAY_WORKSPACE_H - -#include "sway/tree/container.h" - -extern char *prev_workspace_name; - -char *workspace_next_name(const char *output_name); - -struct sway_container *workspace_create(const char *name); - -bool workspace_switch(struct sway_container *workspace); - -struct sway_container *workspace_by_number(const char* name); - -struct sway_container *workspace_by_name(const char*); - -struct sway_container *workspace_output_next(struct sway_container *current); - -struct sway_container *workspace_next(struct sway_container *current); - -struct sway_container *workspace_output_prev(struct sway_container *current); - -struct sway_container *workspace_prev(struct sway_container *current); - -#endif diff --git a/include/sway/view.h b/include/sway/view.h new file mode 100644 index 00000000..b2886211 --- /dev/null +++ b/include/sway/view.h @@ -0,0 +1,120 @@ +#ifndef _SWAY_VIEW_H +#define _SWAY_VIEW_H +#include +#include +#include +#include + +struct sway_container; +struct sway_view; + +struct sway_xdg_surface_v6 { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +struct sway_xwayland_surface { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener request_configure; + struct wl_listener unmap_notify; + struct wl_listener map_notify; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +struct sway_wl_shell_surface { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +enum sway_view_type { + SWAY_WL_SHELL_VIEW, + SWAY_XDG_SHELL_V6_VIEW, + SWAY_XWAYLAND_VIEW, + // Keep last + SWAY_VIEW_TYPES, +}; + +enum sway_view_prop { + VIEW_PROP_TITLE, + VIEW_PROP_APP_ID, + VIEW_PROP_CLASS, + VIEW_PROP_INSTANCE, +}; + +/** + * sway_view is a state container for surfaces that are arranged in the sway + * tree (shell surfaces). + */ +struct sway_view { + enum sway_view_type type; + struct sway_container *swayc; + struct wlr_surface *surface; + int width, height; + + union { + struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; + struct wlr_xwayland_surface *wlr_xwayland_surface; + struct wlr_wl_shell_surface *wlr_wl_shell_surface; + }; + + union { + struct sway_xdg_surface_v6 *sway_xdg_surface_v6; + struct sway_xwayland_surface *sway_xwayland_surface; + struct sway_wl_shell_surface *sway_wl_shell_surface; + }; + + struct { + const char *(*get_prop)(struct sway_view *view, + enum sway_view_prop prop); + void (*set_size)(struct sway_view *view, + int width, int height); + void (*set_position)(struct sway_view *view, + double ox, double oy); + void (*set_activated)(struct sway_view *view, bool activated); + void (*close)(struct sway_view *view); + } iface; + + // only used for unmanaged views (shell specific) + struct wl_list unmanaged_view_link; // sway_root::unmanaged views +}; + +const char *view_get_title(struct sway_view *view); + +const char *view_get_app_id(struct sway_view *view); + +const char *view_get_class(struct sway_view *view); + +const char *view_get_instance(struct sway_view *view); + +void view_set_size(struct sway_view *view, int width, int height); + +void view_set_position(struct sway_view *view, double ox, double oy); + +void view_set_activated(struct sway_view *view, bool activated); + +void view_close(struct sway_view *view); + +void view_update_outputs(struct sway_view *view, const struct wlr_box *before); + +#endif diff --git a/include/sway/workspace.h b/include/sway/workspace.h new file mode 100644 index 00000000..fee54255 --- /dev/null +++ b/include/sway/workspace.h @@ -0,0 +1,20 @@ +#ifndef _SWAY_WORKSPACE_H +#define _SWAY_WORKSPACE_H + +#include "sway/container.h" + +extern char *prev_workspace_name; + +char *workspace_next_name(const char *output_name); +swayc_t *workspace_create(const char *name); +bool workspace_switch(swayc_t *workspace); + +struct sway_container *workspace_by_number(const char* name); +swayc_t *workspace_by_name(const char*); + +struct sway_container *workspace_output_next(swayc_t *current); +struct sway_container *workspace_next(swayc_t *current); +struct sway_container *workspace_output_prev(swayc_t *current); +struct sway_container *workspace_prev(swayc_t *current); + +#endif diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index 954950e7..61870c51 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -6,8 +6,8 @@ #include #include "sway/commands.h" #include "sway/config.h" -#include "sway/tree/container.h" -#include "sway/tree/workspace.h" +#include "sway/container.h" +#include "sway/workspace.h" #include "log.h" #include "stringop.h" diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 64f079f4..f1a8078f 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -3,11 +3,10 @@ #include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "sway/commands.h" -static bool parse_movement_direction(const char *name, - enum movement_direction *out) { +static bool parse_movement_direction(const char *name, enum movement_direction *out) { if (strcasecmp(name, "left") == 0) { *out = MOVE_LEFT; } else if (strcasecmp(name, "right") == 0) { @@ -32,7 +31,7 @@ static bool parse_movement_direction(const char *name, } struct cmd_results *cmd_focus(int argc, char **argv) { - struct sway_container *con = config->handler_context.current_container; + swayc_t *con = config->handler_context.current_container; struct sway_seat *seat = config->handler_context.seat; if (con->type < C_WORKSPACE) { return cmd_results_new(CMD_FAILURE, "focus", @@ -51,7 +50,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { "Expected 'focus ' or 'focus output '"); } - struct sway_container *next_focus = container_get_in_direction(con, seat, direction); + swayc_t *next_focus = get_swayc_in_direction(con, seat, direction); if (next_focus) { sway_seat_set_focus(seat, next_focus); } diff --git a/sway/commands/kill.c b/sway/commands/kill.c index f6774767..f408ce2a 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -2,11 +2,11 @@ #include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - enum sway_container_type type = config->handler_context.current_container->type; + enum swayc_types type = config->handler_context.current_container->type; if (type != C_VIEW && type != C_CONTAINER) { return cmd_results_new(CMD_INVALID, NULL, "Can only kill views and containers with this command"); diff --git a/sway/commands/layout.c b/sway/commands/layout.c index ebab2a48..b0fc5d66 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -1,8 +1,8 @@ #include #include #include "sway/commands.h" -#include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/container.h" +#include "sway/layout.h" #include "log.h" struct cmd_results *cmd_layout(int argc, char **argv) { @@ -10,7 +10,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { return error; } - struct sway_container *parent = config->handler_context.current_container; + swayc_t *parent = config->handler_context.current_container; // TODO: floating /* @@ -26,10 +26,10 @@ struct cmd_results *cmd_layout(int argc, char **argv) { // TODO: stacks and tabs if (strcasecmp(argv[0], "default") == 0) { - container_set_layout(parent, parent->prev_layout); + swayc_change_layout(parent, parent->prev_layout); if (parent->layout == L_NONE) { - struct sway_container *output = container_parent(parent, C_OUTPUT); - container_set_layout(parent, container_get_default_layout(output)); + swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); + swayc_change_layout(parent, default_layout(output)); } } else { if (parent->layout != L_TABBED && parent->layout != L_STACKED) { @@ -37,15 +37,15 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } if (strcasecmp(argv[0], "splith") == 0) { - container_set_layout(parent, L_HORIZ); + swayc_change_layout(parent, L_HORIZ); } else if (strcasecmp(argv[0], "splitv") == 0) { - container_set_layout(parent, L_VERT); + swayc_change_layout(parent, L_VERT); } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE || parent->workspace_layout == L_HORIZ)) { - container_set_layout(parent, L_VERT); + swayc_change_layout(parent, L_VERT); } else { - container_set_layout(parent, L_HORIZ); + swayc_change_layout(parent, L_HORIZ); } } } diff --git a/sway/commands/output.c b/sway/commands/output.c index f7e3372c..35bc8099 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -296,7 +296,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { char identifier[128]; bool all = strcmp(output->name, "*") == 0; for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *cont = root_container.children->items[i]; + swayc_t *cont = root_container.children->items[i]; if (cont->type != C_OUTPUT) { continue; } diff --git a/sway/commands/reload.c b/sway/commands/reload.c index 8cef789b..d54d40db 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -1,6 +1,6 @@ #include "sway/commands.h" #include "sway/config.h" -#include "sway/tree/layout.h" +#include "sway/layout.h" struct cmd_results *cmd_reload(int argc, char **argv) { struct cmd_results *error = NULL; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 8f39e5fc..fa891398 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -4,7 +4,7 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/input/seat.h" -#include "sway/tree/workspace.h" +#include "sway/workspace.h" #include "list.h" #include "log.h" #include "stringop.h" @@ -17,15 +17,15 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { int output_location = -1; - struct sway_container *current_container = config->handler_context.current_container; - struct sway_container *old_workspace = NULL, *old_output = NULL; + swayc_t *current_container = config->handler_context.current_container; + swayc_t *old_workspace = NULL, *old_output = NULL; if (current_container) { if (current_container->type == C_WORKSPACE) { old_workspace = current_container; } else { - old_workspace = container_parent(current_container, C_WORKSPACE); + old_workspace = swayc_parent_by_type(current_container, C_WORKSPACE); } - old_output = container_parent(current_container, C_OUTPUT); + old_output = swayc_parent_by_type(current_container, C_OUTPUT); } for (int i = 0; i < argc; ++i) { @@ -57,7 +57,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (config->reading || !config->active) { return cmd_results_new(CMD_DEFER, "workspace", NULL); } - struct sway_container *ws = NULL; + swayc_t *ws = NULL; if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); @@ -92,7 +92,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { workspace_switch(ws); current_container = sway_seat_get_focus(config->handler_context.seat); - struct sway_container *new_output = container_parent(current_container, C_OUTPUT); + swayc_t *new_output = swayc_parent_by_type(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { // TODO: Warp mouse diff --git a/sway/config.c b/sway/config.c index 0b29735a..213e7680 100644 --- a/sway/config.c +++ b/sway/config.c @@ -24,7 +24,7 @@ #include "sway/input/seat.h" #include "sway/commands.h" #include "sway/config.h" -#include "sway/tree/layout.h" +#include "sway/layout.h" #include "readline.h" #include "stringop.h" #include "list.h" diff --git a/sway/config/output.c b/sway/config/output.c index 7fc79739..9e211861 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -120,14 +120,14 @@ void terminate_swaybg(pid_t pid) { } } -void apply_output_config(struct output_config *oc, struct sway_container *output) { +void apply_output_config(struct output_config *oc, swayc_t *output) { assert(output->type == C_OUTPUT); struct wlr_output *wlr_output = output->sway_output->wlr_output; if (oc && oc->enabled == 0) { wlr_output_layout_remove(root_container.sway_root->output_layout, wlr_output); - container_output_destroy(output); + destroy_output(output); return; } diff --git a/sway/criteria.c b/sway/criteria.c index 247f6b75..2eee331c 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -4,9 +4,9 @@ #include #include #include "sway/criteria.h" -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/config.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "stringop.h" #include "list.h" #include "log.h" @@ -272,7 +272,7 @@ static int regex_cmp(const char *item, const pcre *regex) { } // test a single view if it matches list of criteria tokens (all of them). -static bool criteria_test(struct sway_container *cont, list_t *tokens) { +static bool criteria_test(swayc_t *cont, list_t *tokens) { if (cont->type != C_VIEW) { return false; } @@ -398,7 +398,7 @@ void free_criteria(struct criteria *crit) { free(crit); } -bool criteria_any(struct sway_container *cont, list_t *criteria) { +bool criteria_any(swayc_t *cont, list_t *criteria) { for (int i = 0; i < criteria->length; i++) { struct criteria *bc = criteria->items[i]; if (criteria_test(cont, bc->tokens)) { @@ -408,7 +408,7 @@ bool criteria_any(struct sway_container *cont, list_t *criteria) { return false; } -list_t *criteria_for(struct sway_container *cont) { +list_t *criteria_for(swayc_t *cont) { list_t *criteria = config->criteria, *matches = create_list(); for (int i = 0; i < criteria->length; i++) { struct criteria *bc = criteria->items[i]; @@ -424,7 +424,7 @@ struct list_tokens { list_t *tokens; }; -static void container_match_add(struct sway_container *container, +static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) { if (criteria_test(container, list_tokens->tokens)) { list_add(list_tokens->list, container); @@ -435,8 +435,8 @@ list_t *container_for_crit_tokens(list_t *tokens) { struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; - container_for_each_descendent(&root_container, - (void (*)(struct sway_container *, void *))container_match_add, + container_map(&root_container, + (void (*)(swayc_t *, void *))container_match_add, &list_tokens); // TODO look in the scratchpad diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index 137b3260..bd62f84a 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -7,7 +7,7 @@ #include #include #include "sway/layers.h" -#include "sway/tree/layout.h" +#include "sway/layout.h" #include "sway/output.h" #include "sway/server.h" diff --git a/sway/desktop/output.c b/sway/desktop/output.c index ba778f4c..b8253ace 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -11,14 +11,14 @@ #include #include #include "log.h" -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/layers.h" -#include "sway/tree/layout.h" +#include "sway/layout.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/tree/view.h" +#include "sway/view.h" /** * Rotate a child's position relative to a parent. The parent size is (pw, ph), @@ -145,7 +145,7 @@ struct render_data { struct timespec *now; }; -static void output_frame_view(struct sway_container *view, void *data) { +static void output_frame_view(swayc_t *view, void *data) { struct render_data *rdata = data; struct sway_output *output = rdata->output; struct timespec *now = rdata->now; @@ -219,16 +219,16 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); - struct sway_container *workspace = (focus->type == C_WORKSPACE ? + swayc_t *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); + swayc_t *workspace = (focus->type == C_WORKSPACE ? focus : - container_parent(focus, C_WORKSPACE)); + swayc_parent_by_type(focus, C_WORKSPACE)); struct render_data rdata = { .output = soutput, .now = &now, }; - container_descendents(workspace, C_VIEW, output_frame_view, &rdata); + swayc_descendants_of_type(workspace, C_VIEW, output_frame_view, &rdata); // render unmanaged views on top struct sway_view *view; @@ -259,7 +259,7 @@ static void handle_output_destroy(struct wl_listener *listener, void *data) { struct wlr_output *wlr_output = data; wlr_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); - container_output_destroy(output->swayc); + destroy_output(output->swayc); } static void handle_output_mode(struct wl_listener *listener, void *data) { @@ -287,7 +287,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { wlr_output_set_mode(wlr_output, mode); } - output->swayc = container_output_create(output); + output->swayc = new_output(output); if (!output->swayc) { free(output); return; diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index 4d4d1ed7..0356aa81 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/container.h" +#include "sway/layout.h" #include "sway/server.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" #include "log.h" @@ -74,7 +74,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_surface, destroy); wl_list_remove(&sway_surface->commit.link); wl_list_remove(&sway_surface->destroy.link); - struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); + swayc_t *parent = destroy_view(sway_surface->view->swayc); free(sway_surface->view); free(sway_surface); arrange_windows(parent, -1, -1); @@ -132,8 +132,8 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { wl_signal_add(&shell_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - struct sway_container *cont = container_view_create(focus, sway_view); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + swayc_t *cont = new_view(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 25c0cbca..18e7d399 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/container.h" +#include "sway/layout.h" #include "sway/server.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" #include "log.h" @@ -83,7 +83,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_xdg_surface, destroy); wl_list_remove(&sway_xdg_surface->commit.link); wl_list_remove(&sway_xdg_surface->destroy.link); - struct sway_container *parent = container_view_destroy(sway_xdg_surface->view->swayc); + swayc_t *parent = destroy_view(sway_xdg_surface->view->swayc); free(sway_xdg_surface->view); free(sway_xdg_surface); arrange_windows(parent, -1, -1); @@ -137,8 +137,8 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - struct sway_container *cont = container_view_create(focus, sway_view); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + swayc_t *cont = new_view(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index fd0bcaca..f9b5242b 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -5,10 +5,10 @@ #include #include #include -#include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/container.h" +#include "sway/layout.h" #include "sway/server.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "sway/output.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" @@ -49,11 +49,11 @@ static void set_position(struct sway_view *view, double ox, double oy) { if (!assert_xwayland(view)) { return; } - struct sway_container *output = container_parent(view->swayc, C_OUTPUT); + swayc_t *output = swayc_parent_by_type(view->swayc, C_OUTPUT); if (!sway_assert(output, "view must be within tree to set position")) { return; } - struct sway_container *root = container_parent(output, C_ROOT); + swayc_t *root = swayc_parent_by_type(output, C_ROOT); if (!sway_assert(root, "output must be within tree to set position")) { return; } @@ -114,7 +114,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { } } - struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); + swayc_t *parent = destroy_view(sway_surface->view->swayc); if (parent) { arrange_windows(parent, -1, -1); } @@ -132,7 +132,7 @@ static void handle_unmap_notify(struct wl_listener *listener, void *data) { } // take it out of the tree - struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); + swayc_t *parent = destroy_view(sway_surface->view->swayc); if (parent) { arrange_windows(parent, -1, -1); } @@ -155,12 +155,12 @@ static void handle_map_notify(struct wl_listener *listener, void *data) { &sway_surface->view->unmanaged_view_link); } else { struct sway_view *view = sway_surface->view; - container_view_destroy(view->swayc); + destroy_view(view->swayc); - struct sway_container *parent = root_container.children->items[0]; + swayc_t *parent = root_container.children->items[0]; parent = parent->children->items[0]; // workspace - struct sway_container *cont = container_view_create(parent, view); + swayc_t *cont = new_view(parent, view); view->swayc = cont; arrange_windows(cont->parent, -1, -1); @@ -238,8 +238,8 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { } struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - struct sway_container *cont = container_view_create(focus, sway_view); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + swayc_t *cont = new_view(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/input/cursor.c b/sway/input/cursor.c index d57ac3e3..8a0d1df5 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -7,7 +7,7 @@ #include #include #include "sway/input/cursor.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "list.h" #include "log.h" @@ -49,8 +49,8 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, } } - struct sway_container *swayc = - container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + swayc_t *swayc = + swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); if (swayc) { wlr_seat_pointer_notify_enter(seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat, time, sx, sy); @@ -87,8 +87,8 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { if (event->button == BTN_LEFT) { struct wlr_surface *surface = NULL; double sx, sy; - struct sway_container *swayc = - container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + swayc_t *swayc = + swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); sway_seat_set_focus(cursor->seat, swayc); } diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index d421a03f..27c2c72e 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -278,7 +278,7 @@ struct sway_input_manager *sway_input_manager_create( } bool sway_input_manager_has_focus(struct sway_input_manager *input, - struct sway_container *container) { + swayc_t *container) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { if (sway_seat_get_focus(seat) == container) { @@ -290,7 +290,7 @@ bool sway_input_manager_has_focus(struct sway_input_manager *input, } void sway_input_manager_set_focus(struct sway_input_manager *input, - struct sway_container *container) { + swayc_t *container) { struct sway_seat *seat ; wl_list_for_each(seat, &input->seats, link) { sway_seat_set_focus(seat, container); diff --git a/sway/input/seat.c b/sway/input/seat.c index 76d29b52..648e7914 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,13 +1,13 @@ #define _XOPEN_SOURCE 700 #include #include -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/input/seat.h" #include "sway/input/cursor.h" #include "sway/input/input-manager.h" #include "sway/input/keyboard.h" #include "sway/output.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "log.h" static void seat_device_destroy(struct sway_seat_device *seat_device) { @@ -37,7 +37,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, struct sway_seat_container *seat_con = wl_container_of(listener, seat_con, destroy); struct sway_seat *seat = seat_con->seat; - struct sway_container *con = seat_con->container; + swayc_t *con = seat_con->container; bool is_focus = (sway_seat_get_focus(seat) == con); @@ -46,7 +46,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, if (is_focus) { // pick next focus sway_seat_set_focus(seat, NULL); - struct sway_container *next = sway_seat_get_focus_inactive(seat, con->parent); + swayc_t *next = sway_seat_get_focus_inactive(seat, con->parent); if (next == NULL) { next = con->parent; } @@ -59,7 +59,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, } static struct sway_seat_container *seat_container_from_container( - struct sway_seat *seat, struct sway_container *con) { + struct sway_seat *seat, swayc_t *con) { if (con->type < C_WORKSPACE) { // these don't get seat containers ever return NULL; @@ -89,11 +89,11 @@ static struct sway_seat_container *seat_container_from_container( static void handle_new_container(struct wl_listener *listener, void *data) { struct sway_seat *seat = wl_container_of(listener, seat, new_container); - struct sway_container *con = data; + swayc_t *con = data; seat_container_from_container(seat, con); } -static void collect_focus_iter(struct sway_container *con, void *data) { +static void collect_focus_iter(swayc_t *con, void *data) { struct sway_seat *seat = data; if (con->type > C_WORKSPACE) { return; @@ -130,7 +130,7 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, // init the focus stack wl_list_init(&seat->focus_stack); - container_for_each_descendent(&root_container, collect_focus_iter, seat); + container_for_each_bfs(&root_container, collect_focus_iter, seat); wl_signal_add(&root_container.sway_root->events.new_container, &seat->new_container); @@ -166,7 +166,7 @@ static void seat_configure_keyboard(struct sway_seat *seat, sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); - struct sway_container *focus = sway_seat_get_focus(seat); + swayc_t *focus = sway_seat_get_focus(seat); if (focus && focus->type == C_VIEW) { // force notify reenter to pick up the new configuration wlr_seat_keyboard_clear_focus(seat->wlr_seat); @@ -270,7 +270,7 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { } for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *output_container = root_container.children->items[i]; + swayc_t *output_container = root_container.children->items[i]; struct wlr_output *output = output_container->sway_output->wlr_output; bool result = @@ -289,8 +289,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { seat->cursor->cursor->y); } -void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *container) { - struct sway_container *last_focus = sway_seat_get_focus(seat); +void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { + swayc_t *last_focus = sway_seat_get_focus(seat); if (container && last_focus == container) { return; @@ -330,9 +330,9 @@ void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *containe seat->has_focus = (container != NULL); } -struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { +swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container) { struct sway_seat_container *current = NULL; - struct sway_container *parent = NULL; + swayc_t *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { parent = current->container->parent; @@ -351,21 +351,21 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, stru return NULL; } -struct sway_container *sway_seat_get_focus(struct sway_seat *seat) { +swayc_t *sway_seat_get_focus(struct sway_seat *seat) { if (!seat->has_focus) { return NULL; } return sway_seat_get_focus_inactive(seat, &root_container); } -struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum sway_container_type type) { - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); +swayc_t *sway_seat_get_focus_by_type(struct sway_seat *seat, + enum swayc_types type) { + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); if (focus->type == type) { return focus; } - return container_parent(focus, type); + return swayc_parent_by_type(focus, type); } void sway_seat_set_config(struct sway_seat *seat, diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 7caa2457..977f1ecb 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -3,7 +3,7 @@ #include #include "log.h" #include "sway/ipc-json.h" -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/output.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" @@ -25,7 +25,7 @@ json_object *ipc_json_get_version() { return version; } -static json_object *ipc_json_create_rect(struct sway_container *c) { +static json_object *ipc_json_create_rect(swayc_t *c) { json_object *rect = json_object_new_object(); json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x)); @@ -36,7 +36,7 @@ static json_object *ipc_json_create_rect(struct sway_container *c) { return rect; } -static void ipc_json_describe_root(struct sway_container *root, json_object *object) { +static void ipc_json_describe_root(swayc_t *root, json_object *object) { json_object_object_add(object, "type", json_object_new_string("root")); json_object_object_add(object, "layout", json_object_new_string("splith")); } @@ -63,7 +63,7 @@ static const char *ipc_json_get_output_transform(enum wl_output_transform transf return NULL; } -static void ipc_json_describe_output(struct sway_container *container, json_object *object) { +static void ipc_json_describe_output(swayc_t *container, json_object *object) { struct wlr_output *wlr_output = container->sway_output->wlr_output; json_object_object_add(object, "type", json_object_new_string("output")); json_object_object_add(object, "active", json_object_new_boolean(true)); @@ -94,7 +94,7 @@ static void ipc_json_describe_output(struct sway_container *container, json_obje json_object_object_add(object, "modes", modes_array); } -static void ipc_json_describe_workspace(struct sway_container *workspace, json_object *object) { +static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) { int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1; json_object_object_add(object, "num", json_object_new_int(num)); @@ -102,11 +102,11 @@ static void ipc_json_describe_workspace(struct sway_container *workspace, json_o json_object_object_add(object, "type", json_object_new_string("workspace")); } -static void ipc_json_describe_view(struct sway_container *c, json_object *object) { +static void ipc_json_describe_view(swayc_t *c, json_object *object) { json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); } -json_object *ipc_json_describe_container(struct sway_container *c) { +json_object *ipc_json_describe_container(swayc_t *c) { if (!(sway_assert(c, "Container must not be null."))) { return NULL; } @@ -147,7 +147,7 @@ json_object *ipc_json_describe_container(struct sway_container *c) { return object; } -json_object *ipc_json_describe_container_recursive(struct sway_container *c) { +json_object *ipc_json_describe_container_recursive(swayc_t *c) { json_object *object = ipc_json_describe_container(c); int i; diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 50d0bcf3..156de380 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -279,7 +279,7 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event) } } -void ipc_event_window(struct sway_container *window, const char *change) { +void ipc_event_window(swayc_t *window, const char *change) { wlr_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)); @@ -400,7 +400,7 @@ void ipc_client_handle_command(struct ipc_client *client) { { json_object *outputs = json_object_new_array(); for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *container = root_container.children->items[i]; + swayc_t *container = root_container.children->items[i]; if (container->type == C_OUTPUT) { json_object_array_add(outputs, ipc_json_describe_container(container)); diff --git a/sway/main.c b/sway/main.c index ded922ee..f2f24be3 100644 --- a/sway/main.c +++ b/sway/main.c @@ -18,7 +18,7 @@ #include #include "sway/config.h" #include "sway/server.h" -#include "sway/tree/layout.h" +#include "sway/layout.h" #include "sway/ipc-server.h" #include "ipc-client.h" #include "readline.h" @@ -382,7 +382,7 @@ int main(int argc, char **argv) { wlr_log(L_INFO, "Starting sway version " SWAY_VERSION); - layout_init(); + init_layout(); if (!server_init(&server)) { return 1; diff --git a/sway/tree/container.c b/sway/tree/container.c index 40047dcf..bbafe9ec 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -7,14 +7,14 @@ #include #include #include "sway/config.h" -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/tree/layout.h" +#include "sway/layout.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/tree/view.h" -#include "sway/tree/workspace.h" +#include "sway/view.h" +#include "sway/workspace.h" #include "sway/ipc-server.h" #include "log.h" @@ -33,15 +33,48 @@ static list_t *get_bfs_queue() { return bfs_queue; } -static void notify_new_container(struct sway_container *container) { +static void notify_new_container(swayc_t *container) { wl_signal_emit(&root_container.sway_root->events.new_container, container); ipc_event_window(container, "new"); } -static struct sway_container *container_create(enum sway_container_type type) { +swayc_t *swayc_by_test(swayc_t *container, + bool (*test)(swayc_t *view, void *data), void *data) { + if (!container->children) { + return NULL; + } + // TODO: floating windows + for (int i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + if (test(child, data)) { + return child; + } else { + swayc_t *res = swayc_by_test(child, test, data); + if (res) { + return res; + } + } + } + return NULL; +} + +void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, + void (*func)(swayc_t *item, void *data), void *data) { + for (int i = 0; i < root->children->length; ++i) { + swayc_t *item = root->children->items[i]; + if (item->type == type) { + func(item, data); + } + if (item->children && item->children->length) { + swayc_descendants_of_type(item, type, func, data); + } + } +} + +static swayc_t *new_swayc(enum swayc_types type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; - struct sway_container *c = calloc(1, sizeof(struct sway_container)); + swayc_t *c = calloc(1, sizeof(swayc_t)); if (!c) { return NULL; } @@ -49,6 +82,8 @@ static struct sway_container *container_create(enum sway_container_type type) { c->layout = L_NONE; c->workspace_layout = L_NONE; c->type = type; + c->nb_master = 1; + c->nb_slave_groups = 1; if (type != C_VIEW) { c->children = create_list(); } @@ -58,18 +93,18 @@ static struct sway_container *container_create(enum sway_container_type type) { return c; } -static void container_destroy(struct sway_container *cont) { - if (cont == NULL) { +static void free_swayc(swayc_t *cont) { + if (!sway_assert(cont, "free_swayc passed NULL")) { return; } wl_signal_emit(&cont->events.destroy, cont); if (cont->children) { - // remove children until there are no more, container_destroy calls - // container_remove_child, which removes child from this container + // remove children until there are no more, free_swayc calls + // remove_child, which removes child from this container while (cont->children->length) { - container_destroy(cont->children->items[0]); + free_swayc(cont->children->items[0]); } list_free(cont->children); } @@ -78,7 +113,7 @@ static void container_destroy(struct sway_container *cont) { list_free(cont->marks); } if (cont->parent) { - container_remove_child(cont); + remove_child(cont); } if (cont->name) { free(cont->name); @@ -86,8 +121,7 @@ static void container_destroy(struct sway_container *cont) { free(cont); } -struct sway_container *container_output_create( - struct sway_output *sway_output) { +swayc_t *new_output(struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, &size.height); @@ -122,22 +156,22 @@ struct sway_container *container_output_create( return NULL; } - struct sway_container *output = container_create(C_OUTPUT); + swayc_t *output = new_swayc(C_OUTPUT); output->sway_output = sway_output; output->name = strdup(name); if (output->name == NULL) { - container_destroy(output); + free_swayc(output); return NULL; } apply_output_config(oc, output); - container_add_child(&root_container, output); + add_child(&root_container, output); // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - struct sway_container *ws = container_workspace_create(output, ws_name); + swayc_t *ws = new_workspace(output, ws_name); // Set each seat's focus if not already set struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { @@ -151,14 +185,12 @@ struct sway_container *container_output_create( return output; } -struct sway_container *container_workspace_create( - struct sway_container *output, const char *name) { - if (!sway_assert(output, - "container_workspace_create called with null output")) { +swayc_t *new_workspace(swayc_t *output, const char *name) { + if (!sway_assert(output, "new_workspace called with null output")) { return NULL; } wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); - struct sway_container *workspace = container_create(C_WORKSPACE); + swayc_t *workspace = new_swayc(C_WORKSPACE); workspace->x = output->x; workspace->y = output->y; @@ -166,23 +198,21 @@ struct sway_container *container_workspace_create( workspace->height = output->height; workspace->name = !name ? NULL : strdup(name); workspace->prev_layout = L_NONE; - workspace->layout = container_get_default_layout(output); - workspace->workspace_layout = container_get_default_layout(output); + workspace->layout = default_layout(output); + workspace->workspace_layout = default_layout(output); - container_add_child(output, workspace); - container_sort_workspaces(output); + add_child(output, workspace); + sort_workspaces(output); notify_new_container(workspace); return workspace; } -struct sway_container *container_view_create(struct sway_container *sibling, - struct sway_view *sway_view) { - if (!sway_assert(sibling, - "container_view_create called with NULL sibling/parent")) { +swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { + if (!sway_assert(sibling, "new_view called with NULL sibling/parent")) { return NULL; } const char *title = view_get_title(sway_view); - struct sway_container *swayc = container_create(C_VIEW); + swayc_t *swayc = new_swayc(C_VIEW); wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d %s", swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); // Setup values @@ -193,18 +223,17 @@ struct sway_container *container_view_create(struct sway_container *sibling, if (sibling->type == C_WORKSPACE) { // Case of focused workspace, just create as child of it - container_add_child(sibling, swayc); + add_child(sibling, swayc); } else { // Regular case, create as sibling of current container - container_add_sibling(sibling, swayc); + add_sibling(sibling, swayc); } notify_new_container(swayc); return swayc; } -struct sway_container *container_output_destroy(struct sway_container *output) { - if (!sway_assert(output, - "null output passed to container_output_destroy")) { +swayc_t *destroy_output(swayc_t *output) { + if (!sway_assert(output, "null output passed to destroy_output")) { return NULL; } @@ -216,13 +245,12 @@ struct sway_container *container_output_destroy(struct sway_container *output) { int p = root_container.children->items[0] == output; // Move workspace from this output to another output while (output->children->length) { - struct sway_container *child = output->children->items[0]; - container_remove_child(child); - container_add_child(root_container.children->items[p], child); + swayc_t *child = output->children->items[0]; + remove_child(child); + add_child(root_container.children->items[p], child); } - container_sort_workspaces(root_container.children->items[p]); - arrange_windows(root_container.children->items[p], - -1, -1); + sort_workspaces(root_container.children->items[p]); + arrange_windows(root_container.children->items[p], -1, -1); } } @@ -231,18 +259,18 @@ struct sway_container *container_output_destroy(struct sway_container *output) { wl_list_remove(&output->sway_output->mode.link); wlr_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); - container_destroy(output); + free_swayc(output); return &root_container; } -struct sway_container *container_view_destroy(struct sway_container *view) { +swayc_t *destroy_view(swayc_t *view) { if (!view) { return NULL; } wlr_log(L_DEBUG, "Destroying view '%s'", view->name); - struct sway_container *parent = view->parent; - container_destroy(view); + swayc_t *parent = view->parent; + free_swayc(view); // TODO WLR: Destroy empty containers /* @@ -253,55 +281,7 @@ struct sway_container *container_view_destroy(struct sway_container *view) { return parent; } -struct sway_container *container_set_layout(struct sway_container *container, - enum sway_container_layout layout) { - if (container->type == C_WORKSPACE) { - container->workspace_layout = layout; - if (layout == L_HORIZ || layout == L_VERT) { - container->layout = layout; - } - } else { - container->layout = layout; - } - return container; -} - -void container_descendents(struct sway_container *root, - enum sway_container_type type, - void (*func)(struct sway_container *item, void *data), void *data) { - for (int i = 0; i < root->children->length; ++i) { - struct sway_container *item = root->children->items[i]; - if (item->type == type) { - func(item, data); - } - if (item->children && item->children->length) { - container_descendents(item, type, func, data); - } - } -} - -struct sway_container *container_find(struct sway_container *container, - bool (*test)(struct sway_container *view, void *data), void *data) { - if (!container->children) { - return NULL; - } - // TODO: floating windows - for (int i = 0; i < container->children->length; ++i) { - struct sway_container *child = container->children->items[i]; - if (test(child, data)) { - return child; - } else { - struct sway_container *res = container_find(child, test, data); - if (res) { - return res; - } - } - } - return NULL; -} - -struct sway_container *container_parent(struct sway_container *container, - enum sway_container_type type) { +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { if (!sway_assert(container, "container is NULL")) { return NULL; } @@ -314,8 +294,7 @@ struct sway_container *container_parent(struct sway_container *container, return container; } -struct sway_container *container_at(struct sway_container *parent, - double lx, double ly, +swayc_t *swayc_at(swayc_t *parent, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy) { list_t *queue = get_bfs_queue(); if (!queue) { @@ -324,13 +303,13 @@ struct sway_container *container_at(struct sway_container *parent, list_add(queue, parent); - struct sway_container *swayc = NULL; + swayc_t *swayc = NULL; while (queue->length) { swayc = queue->items[0]; list_del(queue, 0); if (swayc->type == C_VIEW) { struct sway_view *sview = swayc->sway_view; - struct sway_container *soutput = container_parent(swayc, C_OUTPUT); + swayc_t *soutput = swayc_parent_by_type(swayc, C_OUTPUT); struct wlr_box *output_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, @@ -400,8 +379,30 @@ struct sway_container *container_at(struct sway_container *parent, return NULL; } -void container_for_each_descendent(struct sway_container *con, - void (*f)(struct sway_container *con, void *data), void *data) { +void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { + if (container) { + int i; + if (container->children) { + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + container_map(child, f, data); + } + } + // TODO + /* + if (container->floating) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *child = container->floating->items[i]; + container_map(child, f, data); + } + } + */ + f(container, data); + } +} + +void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), + void *data) { list_t *queue = get_bfs_queue(); if (!queue) { return; @@ -414,7 +415,7 @@ void container_for_each_descendent(struct sway_container *con, list_add(queue, con); - struct sway_container *current = NULL; + swayc_t *current = NULL; while (queue->length) { current = queue->items[0]; list_del(queue, 0); @@ -423,3 +424,15 @@ void container_for_each_descendent(struct sway_container *con, list_cat(queue, current->children); } } + +swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { + if (container->type == C_WORKSPACE) { + container->workspace_layout = layout; + if (layout == L_HORIZ || layout == L_VERT) { + container->layout = layout; + } + } else { + container->layout = layout; + } + return container; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index dc0ee5b4..de9e7b58 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -6,26 +6,24 @@ #include #include #include -#include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/container.h" +#include "sway/layout.h" #include "sway/output.h" -#include "sway/tree/view.h" +#include "sway/view.h" #include "sway/input/seat.h" #include "list.h" #include "log.h" -struct sway_container root_container; +swayc_t root_container; -static void output_layout_change_notify(struct wl_listener *listener, - void *data) { +static void output_layout_change_notify(struct wl_listener *listener, void *data) { struct wlr_box *layout_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, NULL); root_container.width = layout_box->width; root_container.height = layout_box->height; for (int i = 0 ; i < root_container.children->length; ++i) { - struct sway_container *output_container = - root_container.children->items[i]; + swayc_t *output_container = root_container.children->items[i]; if (output_container->type != C_OUTPUT) { continue; } @@ -45,7 +43,7 @@ static void output_layout_change_notify(struct wl_listener *listener, arrange_windows(&root_container, -1, -1); } -void layout_init(void) { +void init_layout(void) { root_container.id = 0; // normally assigned in new_swayc() root_container.type = C_ROOT; root_container.layout = L_NONE; @@ -64,9 +62,9 @@ void layout_init(void) { &root_container.sway_root->output_layout_change); } -static int index_child(const struct sway_container *child) { +static int index_child(const swayc_t *child) { // TODO handle floating - struct sway_container *parent = child->parent; + swayc_t *parent = child->parent; int i, len; len = parent->children->length; for (i = 0; i < len; ++i) { @@ -81,18 +79,16 @@ static int index_child(const struct sway_container *child) { return i; } -struct sway_container *container_add_sibling(struct sway_container *fixed, - struct sway_container *active) { +swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { // TODO handle floating - struct sway_container *parent = fixed->parent; + swayc_t *parent = fixed->parent; int i = index_child(fixed); list_insert(parent->children, i + 1, active); active->parent = parent; return active->parent; } -void container_add_child(struct sway_container *parent, - struct sway_container *child) { +void add_child(swayc_t *parent, swayc_t *child) { wlr_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); @@ -100,17 +96,15 @@ void container_add_child(struct sway_container *parent, child->parent = parent; // set focus for this container /* TODO WLR - if (parent->type == C_WORKSPACE && child->type == C_VIEW && - (parent->workspace_layout == L_TABBED || parent->workspace_layout == - L_STACKED)) { + if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { child = new_container(child, parent->workspace_layout); } */ } -struct sway_container *container_remove_child(struct sway_container *child) { +swayc_t *remove_child(swayc_t *child) { int i; - struct sway_container *parent = child->parent; + swayc_t *parent = child->parent; for (i = 0; i < parent->children->length; ++i) { if (parent->children->items[i] == child) { list_del(parent->children, i); @@ -121,8 +115,7 @@ struct sway_container *container_remove_child(struct sway_container *child) { return parent; } -enum sway_container_layout container_get_default_layout( - struct sway_container *output) { +enum swayc_layouts default_layout(swayc_t *output) { /* TODO WLR if (config->default_layout != L_NONE) { //return config->default_layout; @@ -136,8 +129,8 @@ enum sway_container_layout container_get_default_layout( } static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { - struct sway_container *a = *(void **)_a; - struct sway_container *b = *(void **)_b; + swayc_t *a = *(void **)_a; + swayc_t *b = *(void **)_b; int retval = 0; if (isdigit(a->name[0]) && isdigit(b->name[0])) { @@ -153,22 +146,21 @@ static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { return retval; } -void container_sort_workspaces(struct sway_container *output) { +void sort_workspaces(swayc_t *output) { list_stable_sort(output->children, sort_workspace_cmp_qsort); } -static void apply_horiz_layout(struct sway_container *container, const double x, +static void apply_horiz_layout(swayc_t *container, const double x, const double y, const double width, const double height, const int start, const int end); -static void apply_vert_layout(struct sway_container *container, const double x, +static void apply_vert_layout(swayc_t *container, const double x, const double y, const double width, const double height, const int start, const int end); -void arrange_windows(struct sway_container *container, - double width, double height) { +void arrange_windows(swayc_t *container, double width, double height) { int i; if (width == -1 || height == -1) { width = container->width; @@ -189,7 +181,7 @@ void arrange_windows(struct sway_container *container, case C_ROOT: // TODO: wlr_output_layout probably for (i = 0; i < container->children->length; ++i) { - struct sway_container *output = container->children->items[i]; + swayc_t *output = container->children->items[i]; wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); arrange_windows(output, -1, -1); @@ -205,14 +197,13 @@ void arrange_windows(struct sway_container *container, } // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { - struct sway_container *child = container->children->items[i]; + swayc_t *child = container->children->items[i]; arrange_windows(child, -1, -1); } return; case C_WORKSPACE: { - struct sway_container *output = - container_parent(container, C_OUTPUT); + swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); struct wlr_box *area = &output->sway_output->usable_area; wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); @@ -261,15 +252,14 @@ void arrange_windows(struct sway_container *container, } } -static void apply_horiz_layout(struct sway_container *container, +static void apply_horiz_layout(swayc_t *container, const double x, const double y, const double width, const double height, const int start, const int end) { double scale = 0; // Calculate total width for (int i = start; i < end; ++i) { - double *old_width = - &((struct sway_container *)container->children->items[i])->width; + double *old_width = &((swayc_t *)container->children->items[i])->width; if (*old_width <= 0) { if (end - start > 1) { *old_width = width / (end - start - 1); @@ -286,7 +276,7 @@ static void apply_horiz_layout(struct sway_container *container, if (scale > 0.1) { wlr_log(L_DEBUG, "Arranging %p horizontally", container); for (int i = start; i < end; ++i) { - struct sway_container *child = container->children->items[i]; + swayc_t *child = container->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); @@ -311,7 +301,7 @@ static void apply_horiz_layout(struct sway_container *container, } } -void apply_vert_layout(struct sway_container *container, +void apply_vert_layout(swayc_t *container, const double x, const double y, const double width, const double height, const int start, const int end) { @@ -319,8 +309,7 @@ void apply_vert_layout(struct sway_container *container, double scale = 0; // Calculate total height for (i = start; i < end; ++i) { - double *old_height = - &((struct sway_container *)container->children->items[i])->height; + double *old_height = &((swayc_t *)container->children->items[i])->height; if (*old_height <= 0) { if (end - start > 1) { *old_height = height / (end - start - 1); @@ -337,7 +326,7 @@ void apply_vert_layout(struct sway_container *container, if (scale > 0.1) { wlr_log(L_DEBUG, "Arranging %p vertically", container); for (i = start; i < end; ++i) { - struct sway_container *child = container->children->items[i]; + swayc_t *child = container->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); @@ -365,16 +354,15 @@ void apply_vert_layout(struct sway_container *container, /** * Get swayc in the direction of newly entered output. */ -static struct sway_container *get_swayc_in_output_direction( - struct sway_container *output, enum movement_direction dir, - struct sway_seat *seat) { +static swayc_t *get_swayc_in_output_direction(swayc_t *output, + enum movement_direction dir, struct sway_seat *seat) { if (!output) { return NULL; } - struct sway_container *ws = sway_seat_get_focus_inactive(seat, output); + swayc_t *ws = sway_seat_get_focus_inactive(seat, output); if (ws->type != C_WORKSPACE) { - ws = container_parent(ws, C_WORKSPACE); + ws = swayc_parent_by_type(ws, C_WORKSPACE); } if (ws == NULL) { @@ -392,15 +380,13 @@ static struct sway_container *get_swayc_in_output_direction( return ws->children->items[0]; case MOVE_UP: case MOVE_DOWN: { - struct sway_container *focused = - sway_seat_get_focus_inactive(seat, ws); + swayc_t *focused = sway_seat_get_focus_inactive(seat, ws); if (focused && focused->parent) { - struct sway_container *parent = focused->parent; + swayc_t *parent = focused->parent; if (parent->layout == L_VERT) { if (dir == MOVE_UP) { // get child furthest down on new output - int idx = parent->children->length - 1; - return parent->children->items[idx]; + return parent->children->items[parent->children->length-1]; } else if (dir == MOVE_DOWN) { // get child furthest up on new output return parent->children->items[0]; @@ -418,14 +404,13 @@ static struct sway_container *get_swayc_in_output_direction( return ws; } -static void get_layout_center_position(struct sway_container *container, - int *x, int *y) { +static void get_layout_center_position(swayc_t *container, int *x, int *y) { // FIXME view coords are inconsistently referred to in layout/output systems if (container->type == C_OUTPUT) { *x = container->x + container->width/2; *y = container->y + container->height/2; } else { - struct sway_container *output = container_parent(container, C_OUTPUT); + swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); if (container->type == C_WORKSPACE) { // Workspace coordinates are actually wrong/arbitrary, but should // be same as output. @@ -438,8 +423,7 @@ static void get_layout_center_position(struct sway_container *container, } } -static bool sway_dir_to_wlr(enum movement_direction dir, - enum wlr_direction *out) { +static bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out) { switch (dir) { case MOVE_UP: *out = WLR_DIRECTION_UP; @@ -460,12 +444,12 @@ static bool sway_dir_to_wlr(enum movement_direction dir, return true; } -static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { +static swayc_t *sway_output_from_wlr(struct wlr_output *output) { if (output == NULL) { return NULL; } for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *o = root_container.children->items[i]; + swayc_t *o = root_container.children->items[i]; if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { return o; } @@ -473,14 +457,13 @@ static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { return NULL; } -static struct sway_container *get_swayc_in_direction_under( - struct sway_container *container, enum movement_direction dir, - struct sway_seat *seat, struct sway_container *limit) { +static swayc_t *get_swayc_in_direction_under(swayc_t *container, + enum movement_direction dir, struct sway_seat *seat, swayc_t *limit) { if (dir == MOVE_CHILD) { return sway_seat_get_focus_inactive(seat, container); } - struct sway_container *parent = container->parent; + swayc_t *parent = container->parent; if (dir == MOVE_PARENT) { if (parent->type == C_OUTPUT) { return NULL; @@ -513,10 +496,9 @@ static struct sway_container *get_swayc_in_direction_under( /* if (container->type == C_VIEW && swayc_is_fullscreen(container)) { wlr_log(L_DEBUG, "Moving from fullscreen view, skipping to output"); - container = container_parent(container, C_OUTPUT); + container = swayc_parent_by_type(container, C_OUTPUT); get_layout_center_position(container, &abs_pos); - struct sway_container *output = - swayc_adjacent_output(container, dir, &abs_pos, true); + swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); return get_swayc_in_output_direction(output, dir); } if (container->type == C_WORKSPACE && container->fullscreen) { @@ -525,7 +507,7 @@ static struct sway_container *get_swayc_in_direction_under( } */ - struct sway_container *wrap_candidate = NULL; + swayc_t *wrap_candidate = NULL; while (true) { // Test if we can even make a difference here bool can_move = false; @@ -539,19 +521,16 @@ static struct sway_container *get_swayc_in_direction_under( } int lx, ly; get_layout_center_position(container, &lx, &ly); - struct wlr_output_layout *layout = - root_container.sway_root->output_layout; + struct wlr_output_layout *layout = root_container.sway_root->output_layout; struct wlr_output *wlr_adjacent = wlr_output_layout_adjacent_output(layout, wlr_dir, container->sway_output->wlr_output, lx, ly); - struct sway_container *adjacent = - sway_output_from_wlr(wlr_adjacent); + swayc_t *adjacent = sway_output_from_wlr(wlr_adjacent); if (!adjacent || adjacent == container) { return wrap_candidate; } - struct sway_container *next = - get_swayc_in_output_direction(adjacent, dir, seat); + swayc_t *next = get_swayc_in_output_direction(adjacent, dir, seat); if (next == NULL) { return NULL; } @@ -591,9 +570,8 @@ static struct sway_container *get_swayc_in_direction_under( } } } else { - wlr_log(L_DEBUG, - "cont %d-%p dir %i sibling %d: %p", idx, - container, dir, desired, parent->children->items[desired]); + wlr_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, + idx, container, dir, desired, parent->children->items[desired]); return parent->children->items[desired]; } } @@ -609,8 +587,7 @@ static struct sway_container *get_swayc_in_direction_under( } } -struct sway_container *container_get_in_direction( - struct sway_container *container, struct sway_seat *seat, +swayc_t *get_swayc_in_direction(swayc_t *container, struct sway_seat *seat, enum movement_direction dir) { return get_swayc_in_direction_under(container, dir, seat, NULL); } diff --git a/sway/tree/view.c b/sway/tree/view.c index d5325c31..9499adca 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -1,8 +1,8 @@ #include #include -#include "sway/tree/container.h" -#include "sway/tree/layout.h" -#include "sway/tree/view.h" +#include "sway/container.h" +#include "sway/layout.h" +#include "sway/view.h" const char *view_get_title(struct sway_view *view) { if (view->iface.get_prop) { @@ -45,7 +45,6 @@ void view_set_size(struct sway_view *view, int width, int height) { } } -// TODO make view coordinates in layout coordinates void view_set_position(struct sway_view *view, double ox, double oy) { if (view->iface.set_position) { struct wlr_box box = { diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index ba04c55c..861fda4d 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/tree/container.h" +#include "sway/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/tree/workspace.h" +#include "sway/workspace.h" #include "log.h" #include "util.h" @@ -17,7 +17,7 @@ struct workspace_by_number_data { const char *name; }; -void next_name_map(struct sway_container *ws, void *data) { +void next_name_map(swayc_t *ws, void *data) { int *count = data; ++count; } @@ -37,7 +37,7 @@ char *workspace_next_name(const char *output_name) { return name; } -static bool _workspace_by_number(struct sway_container *view, void *data) { +static bool _workspace_by_number(swayc_t *view, void *data) { if (view->type != C_WORKSPACE) { return false; } @@ -46,28 +46,27 @@ static bool _workspace_by_number(struct sway_container *view, void *data) { return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; } -struct sway_container *workspace_by_number(const char* name) { +swayc_t *workspace_by_number(const char* name) { struct workspace_by_number_data wbnd = {0, "1234567890", name}; wbnd.len = strspn(name, wbnd.cset); if (wbnd.len <= 0) { return NULL; } - return container_find(&root_container, - _workspace_by_number, (void *) &wbnd); + return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); } -static bool _workspace_by_name(struct sway_container *view, void *data) { +static bool _workspace_by_name(swayc_t *view, void *data) { return (view->type == C_WORKSPACE) && (strcasecmp(view->name, (char *) data) == 0); } -struct sway_container *workspace_by_name(const char *name) { +swayc_t *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *current_workspace = NULL, *current_output = NULL; - struct sway_container *focus = sway_seat_get_focus(seat); + swayc_t *current_workspace = NULL, *current_output = NULL; + swayc_t *focus = sway_seat_get_focus(seat); if (focus) { - current_workspace = container_parent(focus, C_WORKSPACE); - current_output = container_parent(focus, C_OUTPUT); + current_workspace = swayc_parent_by_type(focus, C_WORKSPACE); + current_output = swayc_parent_by_type(focus, C_OUTPUT); } if (strcmp(name, "prev") == 0) { return workspace_prev(current_workspace); @@ -80,13 +79,12 @@ struct sway_container *workspace_by_name(const char *name) { } else if (strcmp(name, "current") == 0) { return current_workspace; } else { - return container_find(&root_container, _workspace_by_name, - (void *)name); + return swayc_by_test(&root_container, _workspace_by_name, (void *) name); } } -struct sway_container *workspace_create(const char *name) { - struct sway_container *parent; +swayc_t *workspace_create(const char *name) { + swayc_t *parent; // Search for workspace<->output pair int i, e = config->workspace_outputs->length; for (i = 0; i < e; ++i) { @@ -97,7 +95,7 @@ struct sway_container *workspace_create(const char *name) { for (i = 0; i < e; ++i) { parent = root_container.children->items[i]; if (strcmp(parent->name, wso->output) == 0) { - return container_workspace_create(parent, name); + return new_workspace(parent, name); } } break; @@ -105,11 +103,10 @@ struct sway_container *workspace_create(const char *name) { } // Otherwise create a new one struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = - sway_seat_get_focus_inactive(seat, &root_container); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); parent = focus; - parent = container_parent(parent, C_OUTPUT); - return container_workspace_create(parent, name); + parent = swayc_parent_by_type(parent, C_OUTPUT); + return new_workspace(parent, name); } /** @@ -117,18 +114,17 @@ struct sway_container *workspace_create(const char *name) { * the end and beginning. If next is false, the previous workspace is returned, * otherwise the next one is returned. */ -struct sway_container *workspace_output_prev_next_impl( - struct sway_container *output, bool next) { +swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) { return NULL; } struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, output); - struct sway_container *workspace = (focus->type == C_WORKSPACE ? + swayc_t *focus = sway_seat_get_focus_inactive(seat, output); + swayc_t *workspace = (focus->type == C_WORKSPACE ? focus : - container_parent(focus, C_WORKSPACE)); + swayc_parent_by_type(focus, C_WORKSPACE)); int i; for (i = 0; i < output->children->length; i++) { @@ -138,8 +134,7 @@ struct sway_container *workspace_output_prev_next_impl( } } - // Doesn't happen, at worst the for loop returns the previously active - // workspace + // Doesn't happen, at worst the for loop returns the previously active workspace return NULL; } @@ -149,14 +144,13 @@ struct sway_container *workspace_output_prev_next_impl( * next is false, the previous workspace is returned, otherwise the next one is * returned. */ -struct sway_container *workspace_prev_next_impl( - struct sway_container *workspace, bool next) { +swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) { return NULL; } - struct sway_container *current_output = workspace->parent; + swayc_t *current_output = workspace->parent; int offset = next ? 1 : -1; int start = next ? 0 : 1; int end; @@ -172,57 +166,54 @@ struct sway_container *workspace_prev_next_impl( } } - // Given workspace is the first/last on the output, jump to the - // previous/next output + // Given workspace is the first/last on the output, jump to the previous/next output int num_outputs = root_container.children->length; for (i = 0; i < num_outputs; i++) { if (root_container.children->items[i] == current_output) { - struct sway_container *next_output = root_container.children->items[ + swayc_t *next_output = root_container.children->items[ wrap(i + offset, num_outputs)]; return workspace_output_prev_next_impl(next_output, next); } } - // Doesn't happen, at worst the for loop returns the previously active - // workspace on the active output + // Doesn't happen, at worst the for loop returns the previously active workspace on the active output return NULL; } -struct sway_container *workspace_output_next(struct sway_container *current) { +swayc_t *workspace_output_next(swayc_t *current) { return workspace_output_prev_next_impl(current, true); } -struct sway_container *workspace_next(struct sway_container *current) { +swayc_t *workspace_next(swayc_t *current) { return workspace_prev_next_impl(current, true); } -struct sway_container *workspace_output_prev(struct sway_container *current) { +swayc_t *workspace_output_prev(swayc_t *current) { return workspace_output_prev_next_impl(current, false); } -struct sway_container *workspace_prev(struct sway_container *current) { +swayc_t *workspace_prev(swayc_t *current) { return workspace_prev_next_impl(current, false); } -bool workspace_switch(struct sway_container *workspace) { +bool workspace_switch(swayc_t *workspace) { if (!workspace) { return false; } struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = - sway_seat_get_focus_inactive(seat, &root_container); + swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); if (!seat || !focus) { return false; } - struct sway_container *active_ws = focus; + swayc_t *active_ws = focus; if (active_ws->type != C_WORKSPACE) { - container_parent(focus, C_WORKSPACE); + swayc_parent_by_type(focus, C_WORKSPACE); } if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) { - struct sway_container *new_ws = workspace_by_name(prev_workspace_name); + swayc_t *new_ws = workspace_by_name(prev_workspace_name); workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); } @@ -239,14 +230,13 @@ bool workspace_switch(struct sway_container *workspace) { // TODO: Deal with sticky containers - wlr_log(L_DEBUG, "Switching to workspace %p:%s", - workspace, workspace->name); - struct sway_container *next = sway_seat_get_focus_inactive(seat, workspace); + wlr_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); + swayc_t *next = sway_seat_get_focus_inactive(seat, workspace); if (next == NULL) { next = workspace; } sway_seat_set_focus(seat, next); - struct sway_container *output = container_parent(workspace, C_OUTPUT); + swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); arrange_windows(output, -1, -1); return true; } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 2d2b3b69..93d1219c 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -352,7 +352,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { } // add bar to the output - struct output *bar_output = container_output_create(name); + struct output *bar_output = new_output(name); bar_output->idx = i; list_add(bar->outputs, bar_output); } -- cgit v1.2.3 From dc8c9fbeb664518c76066cc28ee29452c6c30128 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 29 Mar 2018 23:41:33 -0400 Subject: Revert "Merge pull request #1653 from swaywm/revert-1647-refactor-tree" This reverts commit 472e81f35d689d67cda241acafda91c688d61046, reversing changes made to 6b7841b11ff4cd35f54d69dc92029855893e5ce0. --- include/sway/config.h | 17 +-- include/sway/container.h | 169 ---------------------------- include/sway/criteria.h | 6 +- include/sway/input/input-manager.h | 4 +- include/sway/input/seat.h | 13 ++- include/sway/ipc-json.h | 6 +- include/sway/ipc-server.h | 6 +- include/sway/layout.h | 43 -------- include/sway/tree/container.h | 137 +++++++++++++++++++++++ include/sway/tree/layout.h | 52 +++++++++ include/sway/tree/view.h | 116 +++++++++++++++++++ include/sway/tree/workspace.h | 26 +++++ include/sway/view.h | 120 -------------------- include/sway/workspace.h | 20 ---- sway/commands/exec_always.c | 4 +- sway/commands/focus.c | 9 +- sway/commands/kill.c | 4 +- sway/commands/layout.c | 20 ++-- sway/commands/output.c | 2 +- sway/commands/reload.c | 2 +- sway/commands/workspace.c | 14 +-- sway/config.c | 2 +- sway/config/output.c | 4 +- sway/criteria.c | 16 +-- sway/desktop/layer_shell.c | 2 +- sway/desktop/output.c | 20 ++-- sway/desktop/wl_shell.c | 12 +- sway/desktop/xdg_shell_v6.c | 12 +- sway/desktop/xwayland.c | 24 ++-- sway/input/cursor.c | 10 +- sway/input/input-manager.c | 4 +- sway/input/seat.c | 38 +++---- sway/ipc-json.c | 16 +-- sway/ipc-server.c | 4 +- sway/main.c | 4 +- sway/tree/container.c | 221 +++++++++++++++++-------------------- sway/tree/layout.c | 133 +++++++++++++--------- sway/tree/view.c | 7 +- sway/tree/workspace.c | 92 ++++++++------- swaybar/ipc.c | 2 +- 40 files changed, 709 insertions(+), 704 deletions(-) delete mode 100644 include/sway/container.h delete mode 100644 include/sway/layout.h create mode 100644 include/sway/tree/container.h create mode 100644 include/sway/tree/layout.h create mode 100644 include/sway/tree/view.h create mode 100644 include/sway/tree/workspace.h delete mode 100644 include/sway/view.h delete mode 100644 include/sway/workspace.h (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index 48a8b0ab..7fdd0be0 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -10,8 +10,8 @@ #include #include #include "list.h" -#include "layout.h" -#include "container.h" +#include "tree/layout.h" +#include "tree/container.h" /** * Describes a variable created via the `set` command. @@ -299,8 +299,8 @@ struct sway_config { char *floating_scroll_down_cmd; char *floating_scroll_left_cmd; char *floating_scroll_right_cmd; - enum swayc_layouts default_orientation; - enum swayc_layouts default_layout; + enum sway_container_layout default_orientation; + enum sway_container_layout default_layout; char *font; int font_height; @@ -324,8 +324,8 @@ struct sway_config { list_t *config_chain; const char *current_config; - enum swayc_border_types border; - enum swayc_border_types floating_border; + enum sway_container_border border; + enum sway_container_border floating_border; int border_thickness; int floating_border_thickness; enum edge_border_types hide_edge_borders; @@ -356,7 +356,7 @@ struct sway_config { struct input_config *input_config; struct seat_config *seat_config; struct sway_seat *seat; - swayc_t *current_container; + struct sway_container *current_container; } handler_context; }; @@ -416,7 +416,8 @@ void output_get_identifier(char *identifier, size_t len, struct sway_output *output); struct output_config *new_output_config(const char *name); void merge_output_config(struct output_config *dst, struct output_config *src); -void apply_output_config(struct output_config *oc, swayc_t *output); +void apply_output_config(struct output_config *oc, + struct sway_container *output); void free_output_config(struct output_config *oc); /** diff --git a/include/sway/container.h b/include/sway/container.h deleted file mode 100644 index f200a1a2..00000000 --- a/include/sway/container.h +++ /dev/null @@ -1,169 +0,0 @@ -#ifndef _SWAY_CONTAINER_H -#define _SWAY_CONTAINER_H -#include -#include -#include -#include -#include "list.h" - -typedef struct sway_container swayc_t; - -extern swayc_t root_container; - -struct sway_view; -struct sway_seat; - -/** - * Different kinds of containers. - * - * This enum is in order. A container will never be inside of a container below - * it on this list. - */ -enum swayc_types { - C_ROOT, /**< The root container. Only one of these ever exists. */ - C_OUTPUT, /**< An output (aka monitor, head, etc). */ - C_WORKSPACE, /**< A workspace. */ - C_CONTAINER, /**< A manually created container. */ - C_VIEW, /**< A view (aka window). */ - - C_TYPES, -}; - -/** - * Different ways to arrange a container. - */ -enum swayc_layouts { - L_NONE, /**< Used for containers that have no layout (views, root) */ - L_HORIZ, - L_VERT, - L_STACKED, - L_TABBED, - L_FLOATING, /**< A psuedo-container, removed from the tree, to hold floating windows */ - - /* Awesome/Monad style auto layouts */ - L_AUTO_LEFT, - L_AUTO_RIGHT, - L_AUTO_TOP, - L_AUTO_BOTTOM, - - L_AUTO_FIRST = L_AUTO_LEFT, - L_AUTO_LAST = L_AUTO_BOTTOM, - - // Keep last - L_LAYOUTS, -}; - -enum swayc_border_types { - B_NONE, /**< No border */ - B_PIXEL, /**< 1px border */ - B_NORMAL, /**< Normal border with title bar */ -}; - -struct sway_root; -struct sway_output; -struct sway_view; - -/** - * Stores information about a container. - * - * The tree is made of these. Views are containers that cannot have children. - */ -struct sway_container { - union { - // TODO: Encapsulate state for other node types as well like C_CONTAINER - struct sway_root *sway_root; // C_ROOT - struct sway_output *sway_output; // C_OUTPUT - struct sway_view *sway_view; // C_VIEW - }; - - /** - * A unique ID to identify this container. Primarily used in the - * get_tree JSON output. - */ - size_t id; - - char *name; - - enum swayc_types type; - enum swayc_layouts layout; - enum swayc_layouts prev_layout; - enum swayc_layouts workspace_layout; - - /** - * The coordinates that this view appear at, relative to the output they - * are located on (output containers have absolute coordinates). - */ - double x, y; - - /** - * Width and height of this container, without borders or gaps. - */ - double width, height; - - list_t *children; - - /** - * The parent of this container. NULL for the root container. - */ - struct sway_container *parent; - - /** - * Number of master views in auto layouts. - */ - size_t nb_master; - - /** - * Number of slave groups (e.g. columns) in auto layouts. - */ - size_t nb_slave_groups; - - /** - * Marks applied to the container, list_t of char*. - */ - list_t *marks; - - struct { - struct wl_signal destroy; - } events; -}; - -void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, - void (*func)(swayc_t *item, void *data), void *data); - -swayc_t *new_output(struct sway_output *sway_output); -swayc_t *new_workspace(swayc_t *output, const char *name); -swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); - -swayc_t *destroy_output(swayc_t *output); -swayc_t *destroy_view(swayc_t *view); - -swayc_t *next_view_sibling(struct sway_seat *seat); - -/** - * Finds a container based on test criteria. Returns the first container that - * passes the test. - */ -swayc_t *swayc_by_test(swayc_t *container, - bool (*test)(swayc_t *view, void *data), void *data); -/** - * Finds a parent container with the given swayc_type. - */ -swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); -/** - * Maps a container's children over a function. - */ -void container_map(swayc_t *container, - void (*f)(swayc_t *view, void *data), void *data); - -swayc_t *swayc_at(swayc_t *parent, double lx, double ly, - struct wlr_surface **surface, double *sx, double *sy); - -/** - * Apply the function for each child of the container breadth first. - */ -void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), - void *data); - -swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout); - -#endif diff --git a/include/sway/criteria.h b/include/sway/criteria.h index 9b4b4bef..ec256ddb 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -1,7 +1,7 @@ #ifndef _SWAY_CRITERIA_H #define _SWAY_CRITERIA_H -#include "container.h" +#include "tree/container.h" #include "list.h" /** @@ -31,12 +31,12 @@ char *extract_crit_tokens(list_t *tokens, const char *criteria); // Returns list of criteria that match given container. These criteria have // been set with `for_window` commands and have an associated cmdlist. -list_t *criteria_for(swayc_t *cont); +list_t *criteria_for(struct sway_container *cont); // Returns a list of all containers that match the given list of tokens. list_t *container_for_crit_tokens(list_t *tokens); // Returns true if any criteria in the given list matches this container -bool criteria_any(swayc_t *cont, list_t *criteria); +bool criteria_any(struct sway_container *cont, list_t *criteria); #endif diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index eab7dc90..c6c73dba 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -31,10 +31,10 @@ struct sway_input_manager *sway_input_manager_create( struct sway_server *server); bool sway_input_manager_has_focus(struct sway_input_manager *input, - swayc_t *container); + struct sway_container *container); void sway_input_manager_set_focus(struct sway_input_manager *input, - swayc_t *container); + struct sway_container *container); void sway_input_manager_configure_xcursor(struct sway_input_manager *input); diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index 1d55bec7..496bfd5d 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -14,7 +14,7 @@ struct sway_seat_device { struct sway_seat_container { struct sway_seat *seat; - swayc_t *container; + struct sway_container *container; struct wl_list link; // sway_seat::focus_stack @@ -54,9 +54,9 @@ void sway_seat_remove_device(struct sway_seat *seat, void sway_seat_configure_xcursor(struct sway_seat *seat); -void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); +void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *container); -swayc_t *sway_seat_get_focus(struct sway_seat *seat); +struct sway_container *sway_seat_get_focus(struct sway_seat *seat); /** * Return the last container to be focused for the seat (or the most recently @@ -67,10 +67,11 @@ swayc_t *sway_seat_get_focus(struct sway_seat *seat); * is destroyed, or focus moves to a container with children and we need to * descend into the next leaf in focus order. */ -swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container); +struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, + struct sway_container *container); -swayc_t *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum swayc_types type); +struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, + enum sway_container_type type); void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); diff --git a/include/sway/ipc-json.h b/include/sway/ipc-json.h index eef5a018..3d2fdc4f 100644 --- a/include/sway/ipc-json.h +++ b/include/sway/ipc-json.h @@ -1,13 +1,13 @@ #ifndef _SWAY_IPC_JSON_H #define _SWAY_IPC_JSON_H #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" json_object *ipc_json_get_version(); -json_object *ipc_json_describe_container(swayc_t *c); -json_object *ipc_json_describe_container_recursive(swayc_t *c); +json_object *ipc_json_describe_container(struct sway_container *c); +json_object *ipc_json_describe_container_recursive(struct sway_container *c); json_object *ipc_json_describe_input(struct sway_input_device *device); #endif diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index bcf1c433..d73006dc 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h @@ -1,15 +1,17 @@ #ifndef _SWAY_IPC_SERVER_H #define _SWAY_IPC_SERVER_H #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "ipc.h" struct sway_server; void ipc_init(struct sway_server *server); + void ipc_terminate(void); + struct sockaddr_un *ipc_user_sockaddr(void); -void ipc_event_window(swayc_t *window, const char *change); +void ipc_event_window(struct sway_container *window, const char *change); #endif diff --git a/include/sway/layout.h b/include/sway/layout.h deleted file mode 100644 index e82c4442..00000000 --- a/include/sway/layout.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef _SWAY_LAYOUT_H -#define _SWAY_LAYOUT_H - -#include -#include "sway/container.h" - -enum movement_direction { - MOVE_LEFT, - MOVE_RIGHT, - MOVE_UP, - MOVE_DOWN, - MOVE_PARENT, - MOVE_CHILD, - MOVE_NEXT, - MOVE_PREV, - MOVE_FIRST -}; - -struct sway_container; - -struct sway_root { - struct wlr_output_layout *output_layout; - - struct wl_listener output_layout_change; - - struct wl_list unmanaged_views; // sway_view::unmanaged_view_link - - struct { - struct wl_signal new_container; - } events; -}; - -void init_layout(void); -void add_child(struct sway_container *parent, struct sway_container *child); -swayc_t *add_sibling(swayc_t *parent, swayc_t *child); -struct sway_container *remove_child(struct sway_container *child); -enum swayc_layouts default_layout(struct sway_container *output); -void sort_workspaces(struct sway_container *output); -void arrange_windows(struct sway_container *container, double width, double height); -swayc_t *get_swayc_in_direction(swayc_t *container, - struct sway_seat *seat, enum movement_direction dir); - -#endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h new file mode 100644 index 00000000..16df3ee7 --- /dev/null +++ b/include/sway/tree/container.h @@ -0,0 +1,137 @@ +#ifndef _SWAY_CONTAINER_H +#define _SWAY_CONTAINER_H +#include +#include +#include +#include +#include "list.h" + +extern struct sway_container root_container; + +struct sway_view; +struct sway_seat; + +/** + * Different kinds of containers. + * + * This enum is in order. A container will never be inside of a container below + * it on this list. + */ +enum sway_container_type { + C_ROOT, + C_OUTPUT, + C_WORKSPACE, + C_CONTAINER, + C_VIEW, + + C_TYPES, +}; + +enum sway_container_layout { + L_NONE, + L_HORIZ, + L_VERT, + L_STACKED, + L_TABBED, + L_FLOATING, + + // Keep last + L_LAYOUTS, +}; + +enum sway_container_border { + B_NONE, + B_PIXEL, + B_NORMAL, +}; + +struct sway_root; +struct sway_output; +struct sway_view; + +struct sway_container { + union { + // TODO: Encapsulate state for other node types as well like C_CONTAINER + struct sway_root *sway_root; + struct sway_output *sway_output; + struct sway_view *sway_view; + }; + + /** + * A unique ID to identify this container. Primarily used in the + * get_tree JSON output. + */ + size_t id; + + char *name; + + enum sway_container_type type; + enum sway_container_layout layout; + enum sway_container_layout prev_layout; + enum sway_container_layout workspace_layout; + + // TODO convert to layout coordinates + double x, y; + + // does not include borders or gaps. + double width, height; + + list_t *children; + + struct sway_container *parent; + + list_t *marks; // list of char* + + struct { + struct wl_signal destroy; + } events; +}; + +// TODO only one container create function and pass the type? +struct sway_container *container_output_create( + struct sway_output *sway_output); + +struct sway_container *container_workspace_create( + struct sway_container *output, const char *name); + +struct sway_container *container_view_create( + struct sway_container *sibling, struct sway_view *sway_view); + +struct sway_container *container_output_destroy(struct sway_container *output); + +struct sway_container *container_view_destroy(struct sway_container *view); + +struct sway_container *container_set_layout(struct sway_container *container, + enum sway_container_layout layout); + +void container_descendents(struct sway_container *root, + enum sway_container_type type, + void (*func)(struct sway_container *item, void *data), void *data); + +/** + * Finds a container based on test criteria. Returns the first container that + * passes the test. + */ +struct sway_container *container_find(struct sway_container *container, + bool (*test)(struct sway_container *view, void *data), void *data); + +/** + * Finds a parent container with the given struct sway_containerype. + */ +struct sway_container *container_parent(struct sway_container *container, + enum sway_container_type type); + +/** + * Find a container at the given coordinates. + */ +struct sway_container *container_at(struct sway_container *parent, + double lx, double ly, struct wlr_surface **surface, + double *sx, double *sy); + +/** + * Apply the function for each child of the container breadth first. + */ +void container_for_each_descendent(struct sway_container *container, + void (*f)(struct sway_container *container, void *data), void *data); + +#endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h new file mode 100644 index 00000000..ad52bdb0 --- /dev/null +++ b/include/sway/tree/layout.h @@ -0,0 +1,52 @@ +#ifndef _SWAY_LAYOUT_H +#define _SWAY_LAYOUT_H + +#include +#include "sway/tree/container.h" + +enum movement_direction { + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN, + MOVE_PARENT, + MOVE_CHILD, + MOVE_NEXT, + MOVE_PREV, + MOVE_FIRST +}; + +struct sway_container; + +struct sway_root { + struct wlr_output_layout *output_layout; + + struct wl_listener output_layout_change; + + struct wl_list unmanaged_views; // sway_view::unmanaged_view_link + + struct { + struct wl_signal new_container; + } events; +}; + +void layout_init(void); + +void container_add_child(struct sway_container *parent, struct sway_container *child); + +struct sway_container *container_add_sibling(struct sway_container *parent, + struct sway_container *child); + +struct sway_container *container_remove_child(struct sway_container *child); + +enum sway_container_layout container_get_default_layout(struct sway_container *output); + +void container_sort_workspaces(struct sway_container *output); + +void arrange_windows(struct sway_container *container, + double width, double height); + +struct sway_container *container_get_in_direction(struct sway_container + *container, struct sway_seat *seat, enum movement_direction dir); + +#endif diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h new file mode 100644 index 00000000..e5f53f4e --- /dev/null +++ b/include/sway/tree/view.h @@ -0,0 +1,116 @@ +#ifndef _SWAY_VIEW_H +#define _SWAY_VIEW_H +#include +#include +#include +#include + +struct sway_container; +struct sway_view; + +struct sway_xdg_surface_v6 { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +struct sway_xwayland_surface { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener request_configure; + struct wl_listener unmap_notify; + struct wl_listener map_notify; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +struct sway_wl_shell_surface { + struct sway_view *view; + + struct wl_listener commit; + struct wl_listener request_move; + struct wl_listener request_resize; + struct wl_listener request_maximize; + struct wl_listener destroy; + + int pending_width, pending_height; +}; + +enum sway_view_type { + SWAY_WL_SHELL_VIEW, + SWAY_XDG_SHELL_V6_VIEW, + SWAY_XWAYLAND_VIEW, + // Keep last + SWAY_VIEW_TYPES, +}; + +enum sway_view_prop { + VIEW_PROP_TITLE, + VIEW_PROP_APP_ID, + VIEW_PROP_CLASS, + VIEW_PROP_INSTANCE, +}; + +struct sway_view { + enum sway_view_type type; + struct sway_container *swayc; + struct wlr_surface *surface; + int width, height; + + union { + struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; + struct wlr_xwayland_surface *wlr_xwayland_surface; + struct wlr_wl_shell_surface *wlr_wl_shell_surface; + }; + + union { + struct sway_xdg_surface_v6 *sway_xdg_surface_v6; + struct sway_xwayland_surface *sway_xwayland_surface; + struct sway_wl_shell_surface *sway_wl_shell_surface; + }; + + struct { + const char *(*get_prop)(struct sway_view *view, + enum sway_view_prop prop); + void (*set_size)(struct sway_view *view, + int width, int height); + void (*set_position)(struct sway_view *view, + double ox, double oy); + void (*set_activated)(struct sway_view *view, bool activated); + void (*close)(struct sway_view *view); + } iface; + + // only used for unmanaged views (shell specific) + struct wl_list unmanaged_view_link; // sway_root::unmanaged views +}; + +const char *view_get_title(struct sway_view *view); + +const char *view_get_app_id(struct sway_view *view); + +const char *view_get_class(struct sway_view *view); + +const char *view_get_instance(struct sway_view *view); + +void view_set_size(struct sway_view *view, int width, int height); + +void view_set_position(struct sway_view *view, double ox, double oy); + +void view_set_activated(struct sway_view *view, bool activated); + +void view_close(struct sway_view *view); + +void view_update_outputs(struct sway_view *view, const struct wlr_box *before); + +#endif diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h new file mode 100644 index 00000000..d73b29c1 --- /dev/null +++ b/include/sway/tree/workspace.h @@ -0,0 +1,26 @@ +#ifndef _SWAY_WORKSPACE_H +#define _SWAY_WORKSPACE_H + +#include "sway/tree/container.h" + +extern char *prev_workspace_name; + +char *workspace_next_name(const char *output_name); + +struct sway_container *workspace_create(const char *name); + +bool workspace_switch(struct sway_container *workspace); + +struct sway_container *workspace_by_number(const char* name); + +struct sway_container *workspace_by_name(const char*); + +struct sway_container *workspace_output_next(struct sway_container *current); + +struct sway_container *workspace_next(struct sway_container *current); + +struct sway_container *workspace_output_prev(struct sway_container *current); + +struct sway_container *workspace_prev(struct sway_container *current); + +#endif diff --git a/include/sway/view.h b/include/sway/view.h deleted file mode 100644 index b2886211..00000000 --- a/include/sway/view.h +++ /dev/null @@ -1,120 +0,0 @@ -#ifndef _SWAY_VIEW_H -#define _SWAY_VIEW_H -#include -#include -#include -#include - -struct sway_container; -struct sway_view; - -struct sway_xdg_surface_v6 { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -struct sway_xwayland_surface { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener request_configure; - struct wl_listener unmap_notify; - struct wl_listener map_notify; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -struct sway_wl_shell_surface { - struct sway_view *view; - - struct wl_listener commit; - struct wl_listener request_move; - struct wl_listener request_resize; - struct wl_listener request_maximize; - struct wl_listener destroy; - - int pending_width, pending_height; -}; - -enum sway_view_type { - SWAY_WL_SHELL_VIEW, - SWAY_XDG_SHELL_V6_VIEW, - SWAY_XWAYLAND_VIEW, - // Keep last - SWAY_VIEW_TYPES, -}; - -enum sway_view_prop { - VIEW_PROP_TITLE, - VIEW_PROP_APP_ID, - VIEW_PROP_CLASS, - VIEW_PROP_INSTANCE, -}; - -/** - * sway_view is a state container for surfaces that are arranged in the sway - * tree (shell surfaces). - */ -struct sway_view { - enum sway_view_type type; - struct sway_container *swayc; - struct wlr_surface *surface; - int width, height; - - union { - struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; - struct wlr_xwayland_surface *wlr_xwayland_surface; - struct wlr_wl_shell_surface *wlr_wl_shell_surface; - }; - - union { - struct sway_xdg_surface_v6 *sway_xdg_surface_v6; - struct sway_xwayland_surface *sway_xwayland_surface; - struct sway_wl_shell_surface *sway_wl_shell_surface; - }; - - struct { - const char *(*get_prop)(struct sway_view *view, - enum sway_view_prop prop); - void (*set_size)(struct sway_view *view, - int width, int height); - void (*set_position)(struct sway_view *view, - double ox, double oy); - void (*set_activated)(struct sway_view *view, bool activated); - void (*close)(struct sway_view *view); - } iface; - - // only used for unmanaged views (shell specific) - struct wl_list unmanaged_view_link; // sway_root::unmanaged views -}; - -const char *view_get_title(struct sway_view *view); - -const char *view_get_app_id(struct sway_view *view); - -const char *view_get_class(struct sway_view *view); - -const char *view_get_instance(struct sway_view *view); - -void view_set_size(struct sway_view *view, int width, int height); - -void view_set_position(struct sway_view *view, double ox, double oy); - -void view_set_activated(struct sway_view *view, bool activated); - -void view_close(struct sway_view *view); - -void view_update_outputs(struct sway_view *view, const struct wlr_box *before); - -#endif diff --git a/include/sway/workspace.h b/include/sway/workspace.h deleted file mode 100644 index fee54255..00000000 --- a/include/sway/workspace.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef _SWAY_WORKSPACE_H -#define _SWAY_WORKSPACE_H - -#include "sway/container.h" - -extern char *prev_workspace_name; - -char *workspace_next_name(const char *output_name); -swayc_t *workspace_create(const char *name); -bool workspace_switch(swayc_t *workspace); - -struct sway_container *workspace_by_number(const char* name); -swayc_t *workspace_by_name(const char*); - -struct sway_container *workspace_output_next(swayc_t *current); -struct sway_container *workspace_next(swayc_t *current); -struct sway_container *workspace_output_prev(swayc_t *current); -struct sway_container *workspace_prev(swayc_t *current); - -#endif diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index 61870c51..954950e7 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -6,8 +6,8 @@ #include #include "sway/commands.h" #include "sway/config.h" -#include "sway/container.h" -#include "sway/workspace.h" +#include "sway/tree/container.h" +#include "sway/tree/workspace.h" #include "log.h" #include "stringop.h" diff --git a/sway/commands/focus.c b/sway/commands/focus.c index f1a8078f..64f079f4 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -3,10 +3,11 @@ #include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/commands.h" -static bool parse_movement_direction(const char *name, enum movement_direction *out) { +static bool parse_movement_direction(const char *name, + enum movement_direction *out) { if (strcasecmp(name, "left") == 0) { *out = MOVE_LEFT; } else if (strcasecmp(name, "right") == 0) { @@ -31,7 +32,7 @@ static bool parse_movement_direction(const char *name, enum movement_direction * } struct cmd_results *cmd_focus(int argc, char **argv) { - swayc_t *con = config->handler_context.current_container; + struct sway_container *con = config->handler_context.current_container; struct sway_seat *seat = config->handler_context.seat; if (con->type < C_WORKSPACE) { return cmd_results_new(CMD_FAILURE, "focus", @@ -50,7 +51,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { "Expected 'focus ' or 'focus output '"); } - swayc_t *next_focus = get_swayc_in_direction(con, seat, direction); + struct sway_container *next_focus = container_get_in_direction(con, seat, direction); if (next_focus) { sway_seat_set_focus(seat, next_focus); } diff --git a/sway/commands/kill.c b/sway/commands/kill.c index f408ce2a..f6774767 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -2,11 +2,11 @@ #include "log.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - enum swayc_types type = config->handler_context.current_container->type; + enum sway_container_type type = config->handler_context.current_container->type; if (type != C_VIEW && type != C_CONTAINER) { return cmd_results_new(CMD_INVALID, NULL, "Can only kill views and containers with this command"); diff --git a/sway/commands/layout.c b/sway/commands/layout.c index b0fc5d66..ebab2a48 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -1,8 +1,8 @@ #include #include #include "sway/commands.h" -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "log.h" struct cmd_results *cmd_layout(int argc, char **argv) { @@ -10,7 +10,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { return error; } - swayc_t *parent = config->handler_context.current_container; + struct sway_container *parent = config->handler_context.current_container; // TODO: floating /* @@ -26,10 +26,10 @@ struct cmd_results *cmd_layout(int argc, char **argv) { // TODO: stacks and tabs if (strcasecmp(argv[0], "default") == 0) { - swayc_change_layout(parent, parent->prev_layout); + container_set_layout(parent, parent->prev_layout); if (parent->layout == L_NONE) { - swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); - swayc_change_layout(parent, default_layout(output)); + struct sway_container *output = container_parent(parent, C_OUTPUT); + container_set_layout(parent, container_get_default_layout(output)); } } else { if (parent->layout != L_TABBED && parent->layout != L_STACKED) { @@ -37,15 +37,15 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } if (strcasecmp(argv[0], "splith") == 0) { - swayc_change_layout(parent, L_HORIZ); + container_set_layout(parent, L_HORIZ); } else if (strcasecmp(argv[0], "splitv") == 0) { - swayc_change_layout(parent, L_VERT); + container_set_layout(parent, L_VERT); } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE || parent->workspace_layout == L_HORIZ)) { - swayc_change_layout(parent, L_VERT); + container_set_layout(parent, L_VERT); } else { - swayc_change_layout(parent, L_HORIZ); + container_set_layout(parent, L_HORIZ); } } } diff --git a/sway/commands/output.c b/sway/commands/output.c index 35bc8099..f7e3372c 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -296,7 +296,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { char identifier[128]; bool all = strcmp(output->name, "*") == 0; for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *cont = root_container.children->items[i]; + struct sway_container *cont = root_container.children->items[i]; if (cont->type != C_OUTPUT) { continue; } diff --git a/sway/commands/reload.c b/sway/commands/reload.c index d54d40db..8cef789b 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -1,6 +1,6 @@ #include "sway/commands.h" #include "sway/config.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" struct cmd_results *cmd_reload(int argc, char **argv) { struct cmd_results *error = NULL; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index fa891398..8f39e5fc 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -4,7 +4,7 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/input/seat.h" -#include "sway/workspace.h" +#include "sway/tree/workspace.h" #include "list.h" #include "log.h" #include "stringop.h" @@ -17,15 +17,15 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { int output_location = -1; - swayc_t *current_container = config->handler_context.current_container; - swayc_t *old_workspace = NULL, *old_output = NULL; + struct sway_container *current_container = config->handler_context.current_container; + struct sway_container *old_workspace = NULL, *old_output = NULL; if (current_container) { if (current_container->type == C_WORKSPACE) { old_workspace = current_container; } else { - old_workspace = swayc_parent_by_type(current_container, C_WORKSPACE); + old_workspace = container_parent(current_container, C_WORKSPACE); } - old_output = swayc_parent_by_type(current_container, C_OUTPUT); + old_output = container_parent(current_container, C_OUTPUT); } for (int i = 0; i < argc; ++i) { @@ -57,7 +57,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (config->reading || !config->active) { return cmd_results_new(CMD_DEFER, "workspace", NULL); } - swayc_t *ws = NULL; + struct sway_container *ws = NULL; if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); @@ -92,7 +92,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { workspace_switch(ws); current_container = sway_seat_get_focus(config->handler_context.seat); - swayc_t *new_output = swayc_parent_by_type(current_container, C_OUTPUT); + struct sway_container *new_output = container_parent(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { // TODO: Warp mouse diff --git a/sway/config.c b/sway/config.c index 213e7680..0b29735a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -24,7 +24,7 @@ #include "sway/input/seat.h" #include "sway/commands.h" #include "sway/config.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "readline.h" #include "stringop.h" #include "list.h" diff --git a/sway/config/output.c b/sway/config/output.c index 9e211861..7fc79739 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -120,14 +120,14 @@ void terminate_swaybg(pid_t pid) { } } -void apply_output_config(struct output_config *oc, swayc_t *output) { +void apply_output_config(struct output_config *oc, struct sway_container *output) { assert(output->type == C_OUTPUT); struct wlr_output *wlr_output = output->sway_output->wlr_output; if (oc && oc->enabled == 0) { wlr_output_layout_remove(root_container.sway_root->output_layout, wlr_output); - destroy_output(output); + container_output_destroy(output); return; } diff --git a/sway/criteria.c b/sway/criteria.c index 2eee331c..247f6b75 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -4,9 +4,9 @@ #include #include #include "sway/criteria.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/config.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "stringop.h" #include "list.h" #include "log.h" @@ -272,7 +272,7 @@ static int regex_cmp(const char *item, const pcre *regex) { } // test a single view if it matches list of criteria tokens (all of them). -static bool criteria_test(swayc_t *cont, list_t *tokens) { +static bool criteria_test(struct sway_container *cont, list_t *tokens) { if (cont->type != C_VIEW) { return false; } @@ -398,7 +398,7 @@ void free_criteria(struct criteria *crit) { free(crit); } -bool criteria_any(swayc_t *cont, list_t *criteria) { +bool criteria_any(struct sway_container *cont, list_t *criteria) { for (int i = 0; i < criteria->length; i++) { struct criteria *bc = criteria->items[i]; if (criteria_test(cont, bc->tokens)) { @@ -408,7 +408,7 @@ bool criteria_any(swayc_t *cont, list_t *criteria) { return false; } -list_t *criteria_for(swayc_t *cont) { +list_t *criteria_for(struct sway_container *cont) { list_t *criteria = config->criteria, *matches = create_list(); for (int i = 0; i < criteria->length; i++) { struct criteria *bc = criteria->items[i]; @@ -424,7 +424,7 @@ struct list_tokens { list_t *tokens; }; -static void container_match_add(swayc_t *container, +static void container_match_add(struct sway_container *container, struct list_tokens *list_tokens) { if (criteria_test(container, list_tokens->tokens)) { list_add(list_tokens->list, container); @@ -435,8 +435,8 @@ list_t *container_for_crit_tokens(list_t *tokens) { struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; - container_map(&root_container, - (void (*)(swayc_t *, void *))container_match_add, + container_for_each_descendent(&root_container, + (void (*)(struct sway_container *, void *))container_match_add, &list_tokens); // TODO look in the scratchpad diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index bd62f84a..137b3260 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -7,7 +7,7 @@ #include #include #include "sway/layers.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/server.h" diff --git a/sway/desktop/output.c b/sway/desktop/output.c index b8253ace..ba778f4c 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -11,14 +11,14 @@ #include #include #include "log.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/layers.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" /** * Rotate a child's position relative to a parent. The parent size is (pw, ph), @@ -145,7 +145,7 @@ struct render_data { struct timespec *now; }; -static void output_frame_view(swayc_t *view, void *data) { +static void output_frame_view(struct sway_container *view, void *data) { struct render_data *rdata = data; struct sway_output *output = rdata->output; struct timespec *now = rdata->now; @@ -219,16 +219,16 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { &soutput->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); - swayc_t *workspace = (focus->type == C_WORKSPACE ? + struct sway_container *focus = sway_seat_get_focus_inactive(seat, soutput->swayc); + struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : - swayc_parent_by_type(focus, C_WORKSPACE)); + container_parent(focus, C_WORKSPACE)); struct render_data rdata = { .output = soutput, .now = &now, }; - swayc_descendants_of_type(workspace, C_VIEW, output_frame_view, &rdata); + container_descendents(workspace, C_VIEW, output_frame_view, &rdata); // render unmanaged views on top struct sway_view *view; @@ -259,7 +259,7 @@ static void handle_output_destroy(struct wl_listener *listener, void *data) { struct wlr_output *wlr_output = data; wlr_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); - destroy_output(output->swayc); + container_output_destroy(output->swayc); } static void handle_output_mode(struct wl_listener *listener, void *data) { @@ -287,7 +287,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { wlr_output_set_mode(wlr_output, mode); } - output->swayc = new_output(output); + output->swayc = container_output_create(output); if (!output->swayc) { free(output); return; diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index 0356aa81..4d4d1ed7 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" #include "log.h" @@ -74,7 +74,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_surface, destroy); wl_list_remove(&sway_surface->commit.link); wl_list_remove(&sway_surface->destroy.link); - swayc_t *parent = destroy_view(sway_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); free(sway_surface->view); free(sway_surface); arrange_windows(parent, -1, -1); @@ -132,8 +132,8 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { wl_signal_add(&shell_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); - swayc_t *cont = new_view(focus, sway_view); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 18e7d399..25c0cbca 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" #include "log.h" @@ -83,7 +83,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_container_of(listener, sway_xdg_surface, destroy); wl_list_remove(&sway_xdg_surface->commit.link); wl_list_remove(&sway_xdg_surface->destroy.link); - swayc_t *parent = destroy_view(sway_xdg_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_xdg_surface->view->swayc); free(sway_xdg_surface->view); free(sway_xdg_surface); arrange_windows(parent, -1, -1); @@ -137,8 +137,8 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); - swayc_t *cont = new_view(focus, sway_view); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index f9b5242b..fd0bcaca 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -5,10 +5,10 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/server.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/output.h" #include "sway/input/seat.h" #include "sway/input/input-manager.h" @@ -49,11 +49,11 @@ static void set_position(struct sway_view *view, double ox, double oy) { if (!assert_xwayland(view)) { return; } - swayc_t *output = swayc_parent_by_type(view->swayc, C_OUTPUT); + struct sway_container *output = container_parent(view->swayc, C_OUTPUT); if (!sway_assert(output, "view must be within tree to set position")) { return; } - swayc_t *root = swayc_parent_by_type(output, C_ROOT); + struct sway_container *root = container_parent(output, C_ROOT); if (!sway_assert(root, "output must be within tree to set position")) { return; } @@ -114,7 +114,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { } } - swayc_t *parent = destroy_view(sway_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); if (parent) { arrange_windows(parent, -1, -1); } @@ -132,7 +132,7 @@ static void handle_unmap_notify(struct wl_listener *listener, void *data) { } // take it out of the tree - swayc_t *parent = destroy_view(sway_surface->view->swayc); + struct sway_container *parent = container_view_destroy(sway_surface->view->swayc); if (parent) { arrange_windows(parent, -1, -1); } @@ -155,12 +155,12 @@ static void handle_map_notify(struct wl_listener *listener, void *data) { &sway_surface->view->unmanaged_view_link); } else { struct sway_view *view = sway_surface->view; - destroy_view(view->swayc); + container_view_destroy(view->swayc); - swayc_t *parent = root_container.children->items[0]; + struct sway_container *parent = root_container.children->items[0]; parent = parent->children->items[0]; // workspace - swayc_t *cont = new_view(parent, view); + struct sway_container *cont = container_view_create(parent, view); view->swayc = cont; arrange_windows(cont->parent, -1, -1); @@ -238,8 +238,8 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); - swayc_t *cont = new_view(focus, sway_view); + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 8a0d1df5..d57ac3e3 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -7,7 +7,7 @@ #include #include #include "sway/input/cursor.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "list.h" #include "log.h" @@ -49,8 +49,8 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, } } - swayc_t *swayc = - swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + struct sway_container *swayc = + container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); if (swayc) { wlr_seat_pointer_notify_enter(seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat, time, sx, sy); @@ -87,8 +87,8 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { if (event->button == BTN_LEFT) { struct wlr_surface *surface = NULL; double sx, sy; - swayc_t *swayc = - swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + struct sway_container *swayc = + container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); sway_seat_set_focus(cursor->seat, swayc); } diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 27c2c72e..d421a03f 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -278,7 +278,7 @@ struct sway_input_manager *sway_input_manager_create( } bool sway_input_manager_has_focus(struct sway_input_manager *input, - swayc_t *container) { + struct sway_container *container) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { if (sway_seat_get_focus(seat) == container) { @@ -290,7 +290,7 @@ bool sway_input_manager_has_focus(struct sway_input_manager *input, } void sway_input_manager_set_focus(struct sway_input_manager *input, - swayc_t *container) { + struct sway_container *container) { struct sway_seat *seat ; wl_list_for_each(seat, &input->seats, link) { sway_seat_set_focus(seat, container); diff --git a/sway/input/seat.c b/sway/input/seat.c index 648e7914..76d29b52 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,13 +1,13 @@ #define _XOPEN_SOURCE 700 #include #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/seat.h" #include "sway/input/cursor.h" #include "sway/input/input-manager.h" #include "sway/input/keyboard.h" #include "sway/output.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "log.h" static void seat_device_destroy(struct sway_seat_device *seat_device) { @@ -37,7 +37,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, struct sway_seat_container *seat_con = wl_container_of(listener, seat_con, destroy); struct sway_seat *seat = seat_con->seat; - swayc_t *con = seat_con->container; + struct sway_container *con = seat_con->container; bool is_focus = (sway_seat_get_focus(seat) == con); @@ -46,7 +46,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, if (is_focus) { // pick next focus sway_seat_set_focus(seat, NULL); - swayc_t *next = sway_seat_get_focus_inactive(seat, con->parent); + struct sway_container *next = sway_seat_get_focus_inactive(seat, con->parent); if (next == NULL) { next = con->parent; } @@ -59,7 +59,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, } static struct sway_seat_container *seat_container_from_container( - struct sway_seat *seat, swayc_t *con) { + struct sway_seat *seat, struct sway_container *con) { if (con->type < C_WORKSPACE) { // these don't get seat containers ever return NULL; @@ -89,11 +89,11 @@ static struct sway_seat_container *seat_container_from_container( static void handle_new_container(struct wl_listener *listener, void *data) { struct sway_seat *seat = wl_container_of(listener, seat, new_container); - swayc_t *con = data; + struct sway_container *con = data; seat_container_from_container(seat, con); } -static void collect_focus_iter(swayc_t *con, void *data) { +static void collect_focus_iter(struct sway_container *con, void *data) { struct sway_seat *seat = data; if (con->type > C_WORKSPACE) { return; @@ -130,7 +130,7 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, // init the focus stack wl_list_init(&seat->focus_stack); - container_for_each_bfs(&root_container, collect_focus_iter, seat); + container_for_each_descendent(&root_container, collect_focus_iter, seat); wl_signal_add(&root_container.sway_root->events.new_container, &seat->new_container); @@ -166,7 +166,7 @@ static void seat_configure_keyboard(struct sway_seat *seat, sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); - swayc_t *focus = sway_seat_get_focus(seat); + struct sway_container *focus = sway_seat_get_focus(seat); if (focus && focus->type == C_VIEW) { // force notify reenter to pick up the new configuration wlr_seat_keyboard_clear_focus(seat->wlr_seat); @@ -270,7 +270,7 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { } for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *output_container = root_container.children->items[i]; + struct sway_container *output_container = root_container.children->items[i]; struct wlr_output *output = output_container->sway_output->wlr_output; bool result = @@ -289,8 +289,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { seat->cursor->cursor->y); } -void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { - swayc_t *last_focus = sway_seat_get_focus(seat); +void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *container) { + struct sway_container *last_focus = sway_seat_get_focus(seat); if (container && last_focus == container) { return; @@ -330,9 +330,9 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { seat->has_focus = (container != NULL); } -swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container) { +struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { struct sway_seat_container *current = NULL; - swayc_t *parent = NULL; + struct sway_container *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { parent = current->container->parent; @@ -351,21 +351,21 @@ swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container return NULL; } -swayc_t *sway_seat_get_focus(struct sway_seat *seat) { +struct sway_container *sway_seat_get_focus(struct sway_seat *seat) { if (!seat->has_focus) { return NULL; } return sway_seat_get_focus_inactive(seat, &root_container); } -swayc_t *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum swayc_types type) { - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); +struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, + enum sway_container_type type) { + struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); if (focus->type == type) { return focus; } - return swayc_parent_by_type(focus, type); + return container_parent(focus, type); } void sway_seat_set_config(struct sway_seat *seat, diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 977f1ecb..7caa2457 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -3,7 +3,7 @@ #include #include "log.h" #include "sway/ipc-json.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/output.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" @@ -25,7 +25,7 @@ json_object *ipc_json_get_version() { return version; } -static json_object *ipc_json_create_rect(swayc_t *c) { +static json_object *ipc_json_create_rect(struct sway_container *c) { json_object *rect = json_object_new_object(); json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x)); @@ -36,7 +36,7 @@ static json_object *ipc_json_create_rect(swayc_t *c) { return rect; } -static void ipc_json_describe_root(swayc_t *root, json_object *object) { +static void ipc_json_describe_root(struct sway_container *root, json_object *object) { json_object_object_add(object, "type", json_object_new_string("root")); json_object_object_add(object, "layout", json_object_new_string("splith")); } @@ -63,7 +63,7 @@ static const char *ipc_json_get_output_transform(enum wl_output_transform transf return NULL; } -static void ipc_json_describe_output(swayc_t *container, json_object *object) { +static void ipc_json_describe_output(struct sway_container *container, json_object *object) { struct wlr_output *wlr_output = container->sway_output->wlr_output; json_object_object_add(object, "type", json_object_new_string("output")); json_object_object_add(object, "active", json_object_new_boolean(true)); @@ -94,7 +94,7 @@ static void ipc_json_describe_output(swayc_t *container, json_object *object) { json_object_object_add(object, "modes", modes_array); } -static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) { +static void ipc_json_describe_workspace(struct sway_container *workspace, json_object *object) { int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1; json_object_object_add(object, "num", json_object_new_int(num)); @@ -102,11 +102,11 @@ static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) json_object_object_add(object, "type", json_object_new_string("workspace")); } -static void ipc_json_describe_view(swayc_t *c, json_object *object) { +static void ipc_json_describe_view(struct sway_container *c, json_object *object) { json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL); } -json_object *ipc_json_describe_container(swayc_t *c) { +json_object *ipc_json_describe_container(struct sway_container *c) { if (!(sway_assert(c, "Container must not be null."))) { return NULL; } @@ -147,7 +147,7 @@ json_object *ipc_json_describe_container(swayc_t *c) { return object; } -json_object *ipc_json_describe_container_recursive(swayc_t *c) { +json_object *ipc_json_describe_container_recursive(struct sway_container *c) { json_object *object = ipc_json_describe_container(c); int i; diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 156de380..50d0bcf3 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -279,7 +279,7 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event) } } -void ipc_event_window(swayc_t *window, const char *change) { +void ipc_event_window(struct sway_container *window, const char *change) { wlr_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)); @@ -400,7 +400,7 @@ void ipc_client_handle_command(struct ipc_client *client) { { json_object *outputs = json_object_new_array(); for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *container = root_container.children->items[i]; + struct sway_container *container = root_container.children->items[i]; if (container->type == C_OUTPUT) { json_object_array_add(outputs, ipc_json_describe_container(container)); diff --git a/sway/main.c b/sway/main.c index f2f24be3..ded922ee 100644 --- a/sway/main.c +++ b/sway/main.c @@ -18,7 +18,7 @@ #include #include "sway/config.h" #include "sway/server.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/ipc-server.h" #include "ipc-client.h" #include "readline.h" @@ -382,7 +382,7 @@ int main(int argc, char **argv) { wlr_log(L_INFO, "Starting sway version " SWAY_VERSION); - init_layout(); + layout_init(); if (!server_init(&server)) { return 1; diff --git a/sway/tree/container.c b/sway/tree/container.c index bbafe9ec..40047dcf 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -7,14 +7,14 @@ #include #include #include "sway/config.h" -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/layout.h" +#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/view.h" -#include "sway/workspace.h" +#include "sway/tree/view.h" +#include "sway/tree/workspace.h" #include "sway/ipc-server.h" #include "log.h" @@ -33,48 +33,15 @@ static list_t *get_bfs_queue() { return bfs_queue; } -static void notify_new_container(swayc_t *container) { +static void notify_new_container(struct sway_container *container) { wl_signal_emit(&root_container.sway_root->events.new_container, container); ipc_event_window(container, "new"); } -swayc_t *swayc_by_test(swayc_t *container, - bool (*test)(swayc_t *view, void *data), void *data) { - if (!container->children) { - return NULL; - } - // TODO: floating windows - for (int i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - if (test(child, data)) { - return child; - } else { - swayc_t *res = swayc_by_test(child, test, data); - if (res) { - return res; - } - } - } - return NULL; -} - -void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, - void (*func)(swayc_t *item, void *data), void *data) { - for (int i = 0; i < root->children->length; ++i) { - swayc_t *item = root->children->items[i]; - if (item->type == type) { - func(item, data); - } - if (item->children && item->children->length) { - swayc_descendants_of_type(item, type, func, data); - } - } -} - -static swayc_t *new_swayc(enum swayc_types type) { +static struct sway_container *container_create(enum sway_container_type type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; - swayc_t *c = calloc(1, sizeof(swayc_t)); + struct sway_container *c = calloc(1, sizeof(struct sway_container)); if (!c) { return NULL; } @@ -82,8 +49,6 @@ static swayc_t *new_swayc(enum swayc_types type) { c->layout = L_NONE; c->workspace_layout = L_NONE; c->type = type; - c->nb_master = 1; - c->nb_slave_groups = 1; if (type != C_VIEW) { c->children = create_list(); } @@ -93,18 +58,18 @@ static swayc_t *new_swayc(enum swayc_types type) { return c; } -static void free_swayc(swayc_t *cont) { - if (!sway_assert(cont, "free_swayc passed NULL")) { +static void container_destroy(struct sway_container *cont) { + if (cont == NULL) { return; } wl_signal_emit(&cont->events.destroy, cont); if (cont->children) { - // remove children until there are no more, free_swayc calls - // remove_child, which removes child from this container + // remove children until there are no more, container_destroy calls + // container_remove_child, which removes child from this container while (cont->children->length) { - free_swayc(cont->children->items[0]); + container_destroy(cont->children->items[0]); } list_free(cont->children); } @@ -113,7 +78,7 @@ static void free_swayc(swayc_t *cont) { list_free(cont->marks); } if (cont->parent) { - remove_child(cont); + container_remove_child(cont); } if (cont->name) { free(cont->name); @@ -121,7 +86,8 @@ static void free_swayc(swayc_t *cont) { free(cont); } -swayc_t *new_output(struct sway_output *sway_output) { +struct sway_container *container_output_create( + struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, &size.height); @@ -156,22 +122,22 @@ swayc_t *new_output(struct sway_output *sway_output) { return NULL; } - swayc_t *output = new_swayc(C_OUTPUT); + struct sway_container *output = container_create(C_OUTPUT); output->sway_output = sway_output; output->name = strdup(name); if (output->name == NULL) { - free_swayc(output); + container_destroy(output); return NULL; } apply_output_config(oc, output); - add_child(&root_container, output); + container_add_child(&root_container, output); // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - swayc_t *ws = new_workspace(output, ws_name); + struct sway_container *ws = container_workspace_create(output, ws_name); // Set each seat's focus if not already set struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { @@ -185,12 +151,14 @@ swayc_t *new_output(struct sway_output *sway_output) { return output; } -swayc_t *new_workspace(swayc_t *output, const char *name) { - if (!sway_assert(output, "new_workspace called with null output")) { +struct sway_container *container_workspace_create( + struct sway_container *output, const char *name) { + if (!sway_assert(output, + "container_workspace_create called with null output")) { return NULL; } wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); - swayc_t *workspace = new_swayc(C_WORKSPACE); + struct sway_container *workspace = container_create(C_WORKSPACE); workspace->x = output->x; workspace->y = output->y; @@ -198,21 +166,23 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { workspace->height = output->height; workspace->name = !name ? NULL : strdup(name); workspace->prev_layout = L_NONE; - workspace->layout = default_layout(output); - workspace->workspace_layout = default_layout(output); + workspace->layout = container_get_default_layout(output); + workspace->workspace_layout = container_get_default_layout(output); - add_child(output, workspace); - sort_workspaces(output); + container_add_child(output, workspace); + container_sort_workspaces(output); notify_new_container(workspace); return workspace; } -swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { - if (!sway_assert(sibling, "new_view called with NULL sibling/parent")) { +struct sway_container *container_view_create(struct sway_container *sibling, + struct sway_view *sway_view) { + if (!sway_assert(sibling, + "container_view_create called with NULL sibling/parent")) { return NULL; } const char *title = view_get_title(sway_view); - swayc_t *swayc = new_swayc(C_VIEW); + struct sway_container *swayc = container_create(C_VIEW); wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d %s", swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); // Setup values @@ -223,17 +193,18 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { if (sibling->type == C_WORKSPACE) { // Case of focused workspace, just create as child of it - add_child(sibling, swayc); + container_add_child(sibling, swayc); } else { // Regular case, create as sibling of current container - add_sibling(sibling, swayc); + container_add_sibling(sibling, swayc); } notify_new_container(swayc); return swayc; } -swayc_t *destroy_output(swayc_t *output) { - if (!sway_assert(output, "null output passed to destroy_output")) { +struct sway_container *container_output_destroy(struct sway_container *output) { + if (!sway_assert(output, + "null output passed to container_output_destroy")) { return NULL; } @@ -245,12 +216,13 @@ swayc_t *destroy_output(swayc_t *output) { int p = root_container.children->items[0] == output; // Move workspace from this output to another output while (output->children->length) { - swayc_t *child = output->children->items[0]; - remove_child(child); - add_child(root_container.children->items[p], child); + struct sway_container *child = output->children->items[0]; + container_remove_child(child); + container_add_child(root_container.children->items[p], child); } - sort_workspaces(root_container.children->items[p]); - arrange_windows(root_container.children->items[p], -1, -1); + container_sort_workspaces(root_container.children->items[p]); + arrange_windows(root_container.children->items[p], + -1, -1); } } @@ -259,18 +231,18 @@ swayc_t *destroy_output(swayc_t *output) { wl_list_remove(&output->sway_output->mode.link); wlr_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); - free_swayc(output); + container_destroy(output); return &root_container; } -swayc_t *destroy_view(swayc_t *view) { +struct sway_container *container_view_destroy(struct sway_container *view) { if (!view) { return NULL; } wlr_log(L_DEBUG, "Destroying view '%s'", view->name); - swayc_t *parent = view->parent; - free_swayc(view); + struct sway_container *parent = view->parent; + container_destroy(view); // TODO WLR: Destroy empty containers /* @@ -281,7 +253,55 @@ swayc_t *destroy_view(swayc_t *view) { return parent; } -swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { +struct sway_container *container_set_layout(struct sway_container *container, + enum sway_container_layout layout) { + if (container->type == C_WORKSPACE) { + container->workspace_layout = layout; + if (layout == L_HORIZ || layout == L_VERT) { + container->layout = layout; + } + } else { + container->layout = layout; + } + return container; +} + +void container_descendents(struct sway_container *root, + enum sway_container_type type, + void (*func)(struct sway_container *item, void *data), void *data) { + for (int i = 0; i < root->children->length; ++i) { + struct sway_container *item = root->children->items[i]; + if (item->type == type) { + func(item, data); + } + if (item->children && item->children->length) { + container_descendents(item, type, func, data); + } + } +} + +struct sway_container *container_find(struct sway_container *container, + bool (*test)(struct sway_container *view, void *data), void *data) { + if (!container->children) { + return NULL; + } + // TODO: floating windows + for (int i = 0; i < container->children->length; ++i) { + struct sway_container *child = container->children->items[i]; + if (test(child, data)) { + return child; + } else { + struct sway_container *res = container_find(child, test, data); + if (res) { + return res; + } + } + } + return NULL; +} + +struct sway_container *container_parent(struct sway_container *container, + enum sway_container_type type) { if (!sway_assert(container, "container is NULL")) { return NULL; } @@ -294,7 +314,8 @@ swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { return container; } -swayc_t *swayc_at(swayc_t *parent, double lx, double ly, +struct sway_container *container_at(struct sway_container *parent, + double lx, double ly, struct wlr_surface **surface, double *sx, double *sy) { list_t *queue = get_bfs_queue(); if (!queue) { @@ -303,13 +324,13 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, list_add(queue, parent); - swayc_t *swayc = NULL; + struct sway_container *swayc = NULL; while (queue->length) { swayc = queue->items[0]; list_del(queue, 0); if (swayc->type == C_VIEW) { struct sway_view *sview = swayc->sway_view; - swayc_t *soutput = swayc_parent_by_type(swayc, C_OUTPUT); + struct sway_container *soutput = container_parent(swayc, C_OUTPUT); struct wlr_box *output_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, @@ -379,30 +400,8 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, return NULL; } -void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { - if (container) { - int i; - if (container->children) { - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - container_map(child, f, data); - } - } - // TODO - /* - if (container->floating) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; - container_map(child, f, data); - } - } - */ - f(container, data); - } -} - -void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), - void *data) { +void container_for_each_descendent(struct sway_container *con, + void (*f)(struct sway_container *con, void *data), void *data) { list_t *queue = get_bfs_queue(); if (!queue) { return; @@ -415,7 +414,7 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), list_add(queue, con); - swayc_t *current = NULL; + struct sway_container *current = NULL; while (queue->length) { current = queue->items[0]; list_del(queue, 0); @@ -424,15 +423,3 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data), list_cat(queue, current->children); } } - -swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { - if (container->type == C_WORKSPACE) { - container->workspace_layout = layout; - if (layout == L_HORIZ || layout == L_VERT) { - container->layout = layout; - } - } else { - container->layout = layout; - } - return container; -} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index de9e7b58..dc0ee5b4 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -6,24 +6,26 @@ #include #include #include -#include "sway/container.h" -#include "sway/layout.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" #include "sway/output.h" -#include "sway/view.h" +#include "sway/tree/view.h" #include "sway/input/seat.h" #include "list.h" #include "log.h" -swayc_t root_container; +struct sway_container root_container; -static void output_layout_change_notify(struct wl_listener *listener, void *data) { +static void output_layout_change_notify(struct wl_listener *listener, + void *data) { struct wlr_box *layout_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, NULL); root_container.width = layout_box->width; root_container.height = layout_box->height; for (int i = 0 ; i < root_container.children->length; ++i) { - swayc_t *output_container = root_container.children->items[i]; + struct sway_container *output_container = + root_container.children->items[i]; if (output_container->type != C_OUTPUT) { continue; } @@ -43,7 +45,7 @@ static void output_layout_change_notify(struct wl_listener *listener, void *data arrange_windows(&root_container, -1, -1); } -void init_layout(void) { +void layout_init(void) { root_container.id = 0; // normally assigned in new_swayc() root_container.type = C_ROOT; root_container.layout = L_NONE; @@ -62,9 +64,9 @@ void init_layout(void) { &root_container.sway_root->output_layout_change); } -static int index_child(const swayc_t *child) { +static int index_child(const struct sway_container *child) { // TODO handle floating - swayc_t *parent = child->parent; + struct sway_container *parent = child->parent; int i, len; len = parent->children->length; for (i = 0; i < len; ++i) { @@ -79,16 +81,18 @@ static int index_child(const swayc_t *child) { return i; } -swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { +struct sway_container *container_add_sibling(struct sway_container *fixed, + struct sway_container *active) { // TODO handle floating - swayc_t *parent = fixed->parent; + struct sway_container *parent = fixed->parent; int i = index_child(fixed); list_insert(parent->children, i + 1, active); active->parent = parent; return active->parent; } -void add_child(swayc_t *parent, swayc_t *child) { +void container_add_child(struct sway_container *parent, + struct sway_container *child) { wlr_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); @@ -96,15 +100,17 @@ void add_child(swayc_t *parent, swayc_t *child) { child->parent = parent; // set focus for this container /* TODO WLR - if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { + if (parent->type == C_WORKSPACE && child->type == C_VIEW && + (parent->workspace_layout == L_TABBED || parent->workspace_layout == + L_STACKED)) { child = new_container(child, parent->workspace_layout); } */ } -swayc_t *remove_child(swayc_t *child) { +struct sway_container *container_remove_child(struct sway_container *child) { int i; - swayc_t *parent = child->parent; + struct sway_container *parent = child->parent; for (i = 0; i < parent->children->length; ++i) { if (parent->children->items[i] == child) { list_del(parent->children, i); @@ -115,7 +121,8 @@ swayc_t *remove_child(swayc_t *child) { return parent; } -enum swayc_layouts default_layout(swayc_t *output) { +enum sway_container_layout container_get_default_layout( + struct sway_container *output) { /* TODO WLR if (config->default_layout != L_NONE) { //return config->default_layout; @@ -129,8 +136,8 @@ enum swayc_layouts default_layout(swayc_t *output) { } static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { - swayc_t *a = *(void **)_a; - swayc_t *b = *(void **)_b; + struct sway_container *a = *(void **)_a; + struct sway_container *b = *(void **)_b; int retval = 0; if (isdigit(a->name[0]) && isdigit(b->name[0])) { @@ -146,21 +153,22 @@ static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { return retval; } -void sort_workspaces(swayc_t *output) { +void container_sort_workspaces(struct sway_container *output) { list_stable_sort(output->children, sort_workspace_cmp_qsort); } -static void apply_horiz_layout(swayc_t *container, const double x, +static void apply_horiz_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end); -static void apply_vert_layout(swayc_t *container, const double x, +static void apply_vert_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end); -void arrange_windows(swayc_t *container, double width, double height) { +void arrange_windows(struct sway_container *container, + double width, double height) { int i; if (width == -1 || height == -1) { width = container->width; @@ -181,7 +189,7 @@ void arrange_windows(swayc_t *container, double width, double height) { case C_ROOT: // TODO: wlr_output_layout probably for (i = 0; i < container->children->length; ++i) { - swayc_t *output = container->children->items[i]; + struct sway_container *output = container->children->items[i]; wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); arrange_windows(output, -1, -1); @@ -197,13 +205,14 @@ void arrange_windows(swayc_t *container, double width, double height) { } // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + struct sway_container *child = container->children->items[i]; arrange_windows(child, -1, -1); } return; case C_WORKSPACE: { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + struct sway_container *output = + container_parent(container, C_OUTPUT); struct wlr_box *area = &output->sway_output->usable_area; wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); @@ -252,14 +261,15 @@ void arrange_windows(swayc_t *container, double width, double height) { } } -static void apply_horiz_layout(swayc_t *container, +static void apply_horiz_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end) { double scale = 0; // Calculate total width for (int i = start; i < end; ++i) { - double *old_width = &((swayc_t *)container->children->items[i])->width; + double *old_width = + &((struct sway_container *)container->children->items[i])->width; if (*old_width <= 0) { if (end - start > 1) { *old_width = width / (end - start - 1); @@ -276,7 +286,7 @@ static void apply_horiz_layout(swayc_t *container, if (scale > 0.1) { wlr_log(L_DEBUG, "Arranging %p horizontally", container); for (int i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; + struct sway_container *child = container->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); @@ -301,7 +311,7 @@ static void apply_horiz_layout(swayc_t *container, } } -void apply_vert_layout(swayc_t *container, +void apply_vert_layout(struct sway_container *container, const double x, const double y, const double width, const double height, const int start, const int end) { @@ -309,7 +319,8 @@ void apply_vert_layout(swayc_t *container, double scale = 0; // Calculate total height for (i = start; i < end; ++i) { - double *old_height = &((swayc_t *)container->children->items[i])->height; + double *old_height = + &((struct sway_container *)container->children->items[i])->height; if (*old_height <= 0) { if (end - start > 1) { *old_height = height / (end - start - 1); @@ -326,7 +337,7 @@ void apply_vert_layout(swayc_t *container, if (scale > 0.1) { wlr_log(L_DEBUG, "Arranging %p vertically", container); for (i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; + struct sway_container *child = container->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); @@ -354,15 +365,16 @@ void apply_vert_layout(swayc_t *container, /** * Get swayc in the direction of newly entered output. */ -static swayc_t *get_swayc_in_output_direction(swayc_t *output, - enum movement_direction dir, struct sway_seat *seat) { +static struct sway_container *get_swayc_in_output_direction( + struct sway_container *output, enum movement_direction dir, + struct sway_seat *seat) { if (!output) { return NULL; } - swayc_t *ws = sway_seat_get_focus_inactive(seat, output); + struct sway_container *ws = sway_seat_get_focus_inactive(seat, output); if (ws->type != C_WORKSPACE) { - ws = swayc_parent_by_type(ws, C_WORKSPACE); + ws = container_parent(ws, C_WORKSPACE); } if (ws == NULL) { @@ -380,13 +392,15 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, return ws->children->items[0]; case MOVE_UP: case MOVE_DOWN: { - swayc_t *focused = sway_seat_get_focus_inactive(seat, ws); + struct sway_container *focused = + sway_seat_get_focus_inactive(seat, ws); if (focused && focused->parent) { - swayc_t *parent = focused->parent; + struct sway_container *parent = focused->parent; if (parent->layout == L_VERT) { if (dir == MOVE_UP) { // get child furthest down on new output - return parent->children->items[parent->children->length-1]; + int idx = parent->children->length - 1; + return parent->children->items[idx]; } else if (dir == MOVE_DOWN) { // get child furthest up on new output return parent->children->items[0]; @@ -404,13 +418,14 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, return ws; } -static void get_layout_center_position(swayc_t *container, int *x, int *y) { +static void get_layout_center_position(struct sway_container *container, + int *x, int *y) { // FIXME view coords are inconsistently referred to in layout/output systems if (container->type == C_OUTPUT) { *x = container->x + container->width/2; *y = container->y + container->height/2; } else { - swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); + struct sway_container *output = container_parent(container, C_OUTPUT); if (container->type == C_WORKSPACE) { // Workspace coordinates are actually wrong/arbitrary, but should // be same as output. @@ -423,7 +438,8 @@ static void get_layout_center_position(swayc_t *container, int *x, int *y) { } } -static bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out) { +static bool sway_dir_to_wlr(enum movement_direction dir, + enum wlr_direction *out) { switch (dir) { case MOVE_UP: *out = WLR_DIRECTION_UP; @@ -444,12 +460,12 @@ static bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out return true; } -static swayc_t *sway_output_from_wlr(struct wlr_output *output) { +static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { if (output == NULL) { return NULL; } for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *o = root_container.children->items[i]; + struct sway_container *o = root_container.children->items[i]; if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { return o; } @@ -457,13 +473,14 @@ static swayc_t *sway_output_from_wlr(struct wlr_output *output) { return NULL; } -static swayc_t *get_swayc_in_direction_under(swayc_t *container, - enum movement_direction dir, struct sway_seat *seat, swayc_t *limit) { +static struct sway_container *get_swayc_in_direction_under( + struct sway_container *container, enum movement_direction dir, + struct sway_seat *seat, struct sway_container *limit) { if (dir == MOVE_CHILD) { return sway_seat_get_focus_inactive(seat, container); } - swayc_t *parent = container->parent; + struct sway_container *parent = container->parent; if (dir == MOVE_PARENT) { if (parent->type == C_OUTPUT) { return NULL; @@ -496,9 +513,10 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, /* if (container->type == C_VIEW && swayc_is_fullscreen(container)) { wlr_log(L_DEBUG, "Moving from fullscreen view, skipping to output"); - container = swayc_parent_by_type(container, C_OUTPUT); + container = container_parent(container, C_OUTPUT); get_layout_center_position(container, &abs_pos); - swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); + struct sway_container *output = + swayc_adjacent_output(container, dir, &abs_pos, true); return get_swayc_in_output_direction(output, dir); } if (container->type == C_WORKSPACE && container->fullscreen) { @@ -507,7 +525,7 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, } */ - swayc_t *wrap_candidate = NULL; + struct sway_container *wrap_candidate = NULL; while (true) { // Test if we can even make a difference here bool can_move = false; @@ -521,16 +539,19 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, } int lx, ly; get_layout_center_position(container, &lx, &ly); - struct wlr_output_layout *layout = root_container.sway_root->output_layout; + struct wlr_output_layout *layout = + root_container.sway_root->output_layout; struct wlr_output *wlr_adjacent = wlr_output_layout_adjacent_output(layout, wlr_dir, container->sway_output->wlr_output, lx, ly); - swayc_t *adjacent = sway_output_from_wlr(wlr_adjacent); + struct sway_container *adjacent = + sway_output_from_wlr(wlr_adjacent); if (!adjacent || adjacent == container) { return wrap_candidate; } - swayc_t *next = get_swayc_in_output_direction(adjacent, dir, seat); + struct sway_container *next = + get_swayc_in_output_direction(adjacent, dir, seat); if (next == NULL) { return NULL; } @@ -570,8 +591,9 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, } } } else { - wlr_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, - idx, container, dir, desired, parent->children->items[desired]); + wlr_log(L_DEBUG, + "cont %d-%p dir %i sibling %d: %p", idx, + container, dir, desired, parent->children->items[desired]); return parent->children->items[desired]; } } @@ -587,7 +609,8 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container, } } -swayc_t *get_swayc_in_direction(swayc_t *container, struct sway_seat *seat, +struct sway_container *container_get_in_direction( + struct sway_container *container, struct sway_seat *seat, enum movement_direction dir) { return get_swayc_in_direction_under(container, dir, seat, NULL); } diff --git a/sway/tree/view.c b/sway/tree/view.c index 9499adca..d5325c31 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -1,8 +1,8 @@ #include #include -#include "sway/container.h" -#include "sway/layout.h" -#include "sway/view.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" +#include "sway/tree/view.h" const char *view_get_title(struct sway_view *view) { if (view->iface.get_prop) { @@ -45,6 +45,7 @@ void view_set_size(struct sway_view *view, int width, int height) { } } +// TODO make view coordinates in layout coordinates void view_set_position(struct sway_view *view, double ox, double oy) { if (view->iface.set_position) { struct wlr_box box = { diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 861fda4d..ba04c55c 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -3,10 +3,10 @@ #include #include #include -#include "sway/container.h" +#include "sway/tree/container.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" -#include "sway/workspace.h" +#include "sway/tree/workspace.h" #include "log.h" #include "util.h" @@ -17,7 +17,7 @@ struct workspace_by_number_data { const char *name; }; -void next_name_map(swayc_t *ws, void *data) { +void next_name_map(struct sway_container *ws, void *data) { int *count = data; ++count; } @@ -37,7 +37,7 @@ char *workspace_next_name(const char *output_name) { return name; } -static bool _workspace_by_number(swayc_t *view, void *data) { +static bool _workspace_by_number(struct sway_container *view, void *data) { if (view->type != C_WORKSPACE) { return false; } @@ -46,27 +46,28 @@ static bool _workspace_by_number(swayc_t *view, void *data) { return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; } -swayc_t *workspace_by_number(const char* name) { +struct sway_container *workspace_by_number(const char* name) { struct workspace_by_number_data wbnd = {0, "1234567890", name}; wbnd.len = strspn(name, wbnd.cset); if (wbnd.len <= 0) { return NULL; } - return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); + return container_find(&root_container, + _workspace_by_number, (void *) &wbnd); } -static bool _workspace_by_name(swayc_t *view, void *data) { +static bool _workspace_by_name(struct sway_container *view, void *data) { return (view->type == C_WORKSPACE) && (strcasecmp(view->name, (char *) data) == 0); } -swayc_t *workspace_by_name(const char *name) { +struct sway_container *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *current_workspace = NULL, *current_output = NULL; - swayc_t *focus = sway_seat_get_focus(seat); + struct sway_container *current_workspace = NULL, *current_output = NULL; + struct sway_container *focus = sway_seat_get_focus(seat); if (focus) { - current_workspace = swayc_parent_by_type(focus, C_WORKSPACE); - current_output = swayc_parent_by_type(focus, C_OUTPUT); + current_workspace = container_parent(focus, C_WORKSPACE); + current_output = container_parent(focus, C_OUTPUT); } if (strcmp(name, "prev") == 0) { return workspace_prev(current_workspace); @@ -79,12 +80,13 @@ swayc_t *workspace_by_name(const char *name) { } else if (strcmp(name, "current") == 0) { return current_workspace; } else { - return swayc_by_test(&root_container, _workspace_by_name, (void *) name); + return container_find(&root_container, _workspace_by_name, + (void *)name); } } -swayc_t *workspace_create(const char *name) { - swayc_t *parent; +struct sway_container *workspace_create(const char *name) { + struct sway_container *parent; // Search for workspace<->output pair int i, e = config->workspace_outputs->length; for (i = 0; i < e; ++i) { @@ -95,7 +97,7 @@ swayc_t *workspace_create(const char *name) { for (i = 0; i < e; ++i) { parent = root_container.children->items[i]; if (strcmp(parent->name, wso->output) == 0) { - return new_workspace(parent, name); + return container_workspace_create(parent, name); } } break; @@ -103,10 +105,11 @@ swayc_t *workspace_create(const char *name) { } // Otherwise create a new one struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = + sway_seat_get_focus_inactive(seat, &root_container); parent = focus; - parent = swayc_parent_by_type(parent, C_OUTPUT); - return new_workspace(parent, name); + parent = container_parent(parent, C_OUTPUT); + return container_workspace_create(parent, name); } /** @@ -114,17 +117,18 @@ swayc_t *workspace_create(const char *name) { * the end and beginning. If next is false, the previous workspace is returned, * otherwise the next one is returned. */ -swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { +struct sway_container *workspace_output_prev_next_impl( + struct sway_container *output, bool next) { if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) { return NULL; } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, output); - swayc_t *workspace = (focus->type == C_WORKSPACE ? + struct sway_container *focus = sway_seat_get_focus_inactive(seat, output); + struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : - swayc_parent_by_type(focus, C_WORKSPACE)); + container_parent(focus, C_WORKSPACE)); int i; for (i = 0; i < output->children->length; i++) { @@ -134,7 +138,8 @@ swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { } } - // Doesn't happen, at worst the for loop returns the previously active workspace + // Doesn't happen, at worst the for loop returns the previously active + // workspace return NULL; } @@ -144,13 +149,14 @@ swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { * next is false, the previous workspace is returned, otherwise the next one is * returned. */ -swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { +struct sway_container *workspace_prev_next_impl( + struct sway_container *workspace, bool next) { if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) { return NULL; } - swayc_t *current_output = workspace->parent; + struct sway_container *current_output = workspace->parent; int offset = next ? 1 : -1; int start = next ? 0 : 1; int end; @@ -166,54 +172,57 @@ swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { } } - // Given workspace is the first/last on the output, jump to the previous/next output + // Given workspace is the first/last on the output, jump to the + // previous/next output int num_outputs = root_container.children->length; for (i = 0; i < num_outputs; i++) { if (root_container.children->items[i] == current_output) { - swayc_t *next_output = root_container.children->items[ + struct sway_container *next_output = root_container.children->items[ wrap(i + offset, num_outputs)]; return workspace_output_prev_next_impl(next_output, next); } } - // Doesn't happen, at worst the for loop returns the previously active workspace on the active output + // Doesn't happen, at worst the for loop returns the previously active + // workspace on the active output return NULL; } -swayc_t *workspace_output_next(swayc_t *current) { +struct sway_container *workspace_output_next(struct sway_container *current) { return workspace_output_prev_next_impl(current, true); } -swayc_t *workspace_next(swayc_t *current) { +struct sway_container *workspace_next(struct sway_container *current) { return workspace_prev_next_impl(current, true); } -swayc_t *workspace_output_prev(swayc_t *current) { +struct sway_container *workspace_output_prev(struct sway_container *current) { return workspace_output_prev_next_impl(current, false); } -swayc_t *workspace_prev(swayc_t *current) { +struct sway_container *workspace_prev(struct sway_container *current) { return workspace_prev_next_impl(current, false); } -bool workspace_switch(swayc_t *workspace) { +bool workspace_switch(struct sway_container *workspace) { if (!workspace) { return false; } struct sway_seat *seat = input_manager_current_seat(input_manager); - swayc_t *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = + sway_seat_get_focus_inactive(seat, &root_container); if (!seat || !focus) { return false; } - swayc_t *active_ws = focus; + struct sway_container *active_ws = focus; if (active_ws->type != C_WORKSPACE) { - swayc_parent_by_type(focus, C_WORKSPACE); + container_parent(focus, C_WORKSPACE); } if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) { - swayc_t *new_ws = workspace_by_name(prev_workspace_name); + struct sway_container *new_ws = workspace_by_name(prev_workspace_name); workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); } @@ -230,13 +239,14 @@ bool workspace_switch(swayc_t *workspace) { // TODO: Deal with sticky containers - wlr_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); - swayc_t *next = sway_seat_get_focus_inactive(seat, workspace); + wlr_log(L_DEBUG, "Switching to workspace %p:%s", + workspace, workspace->name); + struct sway_container *next = sway_seat_get_focus_inactive(seat, workspace); if (next == NULL) { next = workspace; } sway_seat_set_focus(seat, next); - swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); + struct sway_container *output = container_parent(workspace, C_OUTPUT); arrange_windows(output, -1, -1); return true; } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 93d1219c..2d2b3b69 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -352,7 +352,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { } // add bar to the output - struct output *bar_output = new_output(name); + struct output *bar_output = container_output_create(name); bar_output->idx = i; list_add(bar->outputs, bar_output); } -- cgit v1.2.3 From 69eb021767d8cf57b08699c7e330fe8c52ca2764 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 30 Mar 2018 10:43:55 -0400 Subject: Add default_orientation command --- include/sway/commands.h | 2 +- sway/commands.c | 1 + sway/commands/default_orientation.c | 21 +++++++++++++++++++++ sway/meson.build | 1 + sway/tree/layout.c | 5 ++--- 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 sway/commands/default_orientation.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 1291d5fb..66f097ea 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -95,6 +95,7 @@ sway_cmd cmd_commands; sway_cmd cmd_debuglog; sway_cmd cmd_default_border; sway_cmd cmd_default_floating_border; +sway_cmd cmd_default_orientation; sway_cmd cmd_exec; sway_cmd cmd_exec_always; sway_cmd cmd_exit; @@ -125,7 +126,6 @@ sway_cmd cmd_move; sway_cmd cmd_new_float; sway_cmd cmd_new_window; sway_cmd cmd_no_focus; -sway_cmd cmd_orientation; sway_cmd cmd_output; sway_cmd cmd_permit; sway_cmd cmd_reject; diff --git a/sway/commands.c b/sway/commands.c index bcc777ed..eee7f254 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -149,6 +149,7 @@ static struct cmd_handler bar_colors_handlers[] = { /* Config-time only commands. Keep alphabetized */ static struct cmd_handler config_handlers[] = { + { "default_orientation", cmd_default_orientation }, { "set", cmd_set }, { "swaybg_command", cmd_swaybg_command }, }; diff --git a/sway/commands/default_orientation.c b/sway/commands/default_orientation.c new file mode 100644 index 00000000..a5347ce2 --- /dev/null +++ b/sway/commands/default_orientation.c @@ -0,0 +1,21 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_default_orientation(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "default_orientation", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if (strcasecmp(argv[0], "horizontal") == 0) { + config->default_orientation = L_HORIZ; + } else if (strcasecmp(argv[0], "vertical") == 0) { + config->default_orientation = L_VERT; + } else if (strcasecmp(argv[0], "auto") == 0) { + // Do nothing + } else { + return cmd_results_new(CMD_INVALID, "default_orientation", + "Expected 'orientation '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index 1e7ee7ae..9c5e4a00 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -8,6 +8,7 @@ sway_sources = files( 'input/keyboard.c', 'commands/bar.c', 'commands/bind.c', + 'commands/default_orientation.c', 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', diff --git a/sway/tree/layout.c b/sway/tree/layout.c index dc0ee5b4..c7cf16e6 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -123,12 +123,11 @@ struct sway_container *container_remove_child(struct sway_container *child) { enum sway_container_layout container_get_default_layout( struct sway_container *output) { - /* TODO WLR if (config->default_layout != L_NONE) { - //return config->default_layout; + return config->default_layout; } else if (config->default_orientation != L_NONE) { return config->default_orientation; - } else */if (output->width >= output->height) { + } else if (output->width >= output->height) { return L_HORIZ; } else { return L_VERT; -- cgit v1.2.3 From ae6d459000865f0a3a54a1d0429ee28b282a7954 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 31 Mar 2018 10:49:52 -0400 Subject: Implement mouse warping --- sway/commands.c | 1 + sway/commands/mouse_warping.c | 19 +++++++++++++++++++ sway/input/cursor.c | 4 ++-- sway/input/seat.c | 18 ++++++++++++++++-- sway/meson.build | 1 + 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 sway/commands/mouse_warping.c (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index eee7f254..d983dcbb 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -101,6 +101,7 @@ static struct cmd_handler handlers[] = { { "include", cmd_include }, { "input", cmd_input }, { "mode", cmd_mode }, + { "mouse_warping", cmd_mouse_warping }, { "output", cmd_output }, { "seat", cmd_seat }, { "workspace", cmd_workspace }, diff --git a/sway/commands/mouse_warping.c b/sway/commands/mouse_warping.c new file mode 100644 index 00000000..eef32ce7 --- /dev/null +++ b/sway/commands/mouse_warping.c @@ -0,0 +1,19 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_mouse_warping(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "mouse_warping", EXPECTED_EQUAL_TO, 1))) { + return error; + } else if (strcasecmp(argv[0], "output") == 0) { + config->mouse_warping = true; + } else if (strcasecmp(argv[0], "none") == 0) { + config->mouse_warping = false; + } else { + return cmd_results_new(CMD_FAILURE, "mouse_warping", + "Expected 'mouse_warping output|none'"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 74af6426..9cdd66d8 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -156,8 +156,8 @@ static void handle_cursor_motion(struct wl_listener *listener, void *data) { cursor_send_pointer_motion(cursor, event->time_msec); } -static void handle_cursor_motion_absolute(struct wl_listener *listener, - void *data) { +static void handle_cursor_motion_absolute( + struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, motion_absolute); struct wlr_event_pointer_motion_absolute *event = data; diff --git a/sway/input/seat.c b/sway/input/seat.c index 8d592872..eab5cf40 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -333,15 +333,29 @@ void sway_seat_set_focus(struct sway_seat *seat, if (last_focus) { struct sway_container *last_ws = last_focus; if (last_ws && last_ws->type != C_WORKSPACE) { - last_ws = container_parent(last_focus, C_WORKSPACE); + last_ws = container_parent(last_ws, C_WORKSPACE); } if (last_ws) { - wlr_log(L_DEBUG, "sending workspace event"); ipc_event_workspace(last_ws, container, "focus"); if (last_ws->children->length == 0) { container_workspace_destroy(last_ws); } } + struct sway_container *last_output = last_focus; + if (last_output && last_output->type != C_OUTPUT) { + last_output = container_parent(last_output, C_OUTPUT); + } + struct sway_container *new_output = container; + if (new_output && new_output->type != C_OUTPUT) { + new_output = container_parent(new_output, C_OUTPUT); + } + if (new_output != last_output && config->mouse_warping) { + struct wlr_output *output = new_output->sway_output->wlr_output; + // TODO: Change container coords to layout coords + double x = container->x + output->lx + container->width / 2.0; + double y = container->y + output->ly + container->height / 2.0; + wlr_cursor_warp(seat->cursor->cursor, NULL, x, y); + } } if (last_focus && last_focus->type == C_VIEW && diff --git a/sway/meson.build b/sway/meson.build index e8a192f0..5bc57964 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -63,6 +63,7 @@ sway_sources = files( 'commands/input/xkb_options.c', 'commands/input/xkb_rules.c', 'commands/input/xkb_variant.c', + 'commands/mouse_warping.c', 'commands/output.c', 'commands/reload.c', 'commands/workspace.c', -- cgit v1.2.3 From 9b38ef950fcf293ba11f54c14d8b3a87f050b154 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 31 Mar 2018 10:11:15 -0400 Subject: Implement focus_follows_mouse Also contains two other small changes: - Clicking any button will focus the container clicked (not just left) - Remove seamless_mouse (doesn't make sense on wlroots) --- include/sway/config.h | 1 - sway/commands.c | 1 + sway/commands/focus_follows_mouse.c | 12 +++++++++ sway/config.c | 1 - sway/input/cursor.c | 53 +++++++++++++++++++------------------ sway/meson.build | 1 + 6 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 sway/commands/focus_follows_mouse.c (limited to 'sway/commands') diff --git a/include/sway/config.h b/include/sway/config.h index ac1105b4..03b51948 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -304,7 +304,6 @@ struct sway_config { bool reloading; bool reading; bool auto_back_and_forth; - bool seamless_mouse; bool show_marks; bool edge_gaps; diff --git a/sway/commands.c b/sway/commands.c index d983dcbb..90544220 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -98,6 +98,7 @@ static struct cmd_handler handlers[] = { { "bindsym", cmd_bindsym }, { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, + { "focus_follows_mouse", cmd_focus_follows_mouse }, { "include", cmd_include }, { "input", cmd_input }, { "mode", cmd_mode }, diff --git a/sway/commands/focus_follows_mouse.c b/sway/commands/focus_follows_mouse.c new file mode 100644 index 00000000..661e7852 --- /dev/null +++ b/sway/commands/focus_follows_mouse.c @@ -0,0 +1,12 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_focus_follows_mouse(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1))) { + return error; + } + config->focus_follows_mouse = !strcasecmp(argv[0], "yes"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index e9e7057d..0eecf7f6 100644 --- a/sway/config.c +++ b/sway/config.c @@ -178,7 +178,6 @@ static void config_defaults(struct sway_config *config) { config->active = false; config->failed = false; config->auto_back_and_forth = false; - config->seamless_mouse = true; config->reading = false; config->show_marks = true; diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 9cdd66d8..35dd5dc8 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -125,7 +125,10 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, struct wlr_seat *seat = cursor->seat->wlr_seat; struct wlr_surface *surface = NULL; double sx, sy; - container_at_cursor(cursor, &surface, &sx, &sy); + struct sway_container *c = container_at_cursor(cursor, &surface, &sx, &sy); + if (c && config->focus_follows_mouse) { + sway_seat_set_focus(cursor->seat, c); + } // reset cursor if switching between clients struct wl_client *client = NULL; @@ -170,33 +173,31 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, button); struct wlr_event_pointer_button *event = data; - if (event->button == BTN_LEFT) { - struct wlr_surface *surface = NULL; - double sx, sy; - struct sway_container *cont = - container_at_cursor(cursor, &surface, &sx, &sy); - // Avoid moving keyboard focus from a surface that accepts it to one - // that does not unless the change would move us to a new workspace. - // - // This prevents, for example, losing focus when clicking on swaybar. - // - // TODO: Replace this condition with something like - // !surface_accepts_keyboard_input - if (surface && cont && cont->type != C_VIEW) { - struct sway_container *new_ws = cont; - if (new_ws && new_ws->type != C_WORKSPACE) { - new_ws = container_parent(new_ws, C_WORKSPACE); - } - struct sway_container *old_ws = sway_seat_get_focus(cursor->seat); - if (old_ws && old_ws->type != C_WORKSPACE) { - old_ws = container_parent(old_ws, C_WORKSPACE); - } - if (new_ws != old_ws) { - sway_seat_set_focus(cursor->seat, cont); - } - } else { + struct wlr_surface *surface = NULL; + double sx, sy; + struct sway_container *cont = + container_at_cursor(cursor, &surface, &sx, &sy); + // Avoid moving keyboard focus from a surface that accepts it to one + // that does not unless the change would move us to a new workspace. + // + // This prevents, for example, losing focus when clicking on swaybar. + // + // TODO: Replace this condition with something like + // !surface_accepts_keyboard_input + if (surface && cont && cont->type != C_VIEW) { + struct sway_container *new_ws = cont; + if (new_ws && new_ws->type != C_WORKSPACE) { + new_ws = container_parent(new_ws, C_WORKSPACE); + } + struct sway_container *old_ws = sway_seat_get_focus(cursor->seat); + if (old_ws && old_ws->type != C_WORKSPACE) { + old_ws = container_parent(old_ws, C_WORKSPACE); + } + if (new_ws != old_ws) { sway_seat_set_focus(cursor->seat, cont); } + } else { + sway_seat_set_focus(cursor->seat, cont); } wlr_seat_pointer_notify_button(cursor->seat->wlr_seat, event->time_msec, diff --git a/sway/meson.build b/sway/meson.build index 5bc57964..0cc620ea 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -13,6 +13,7 @@ sway_sources = files( 'commands/exec.c', 'commands/exec_always.c', 'commands/focus.c', + 'commands/focus_follows_mouse.c', 'commands/kill.c', 'commands/include.c', 'commands/input.c', -- cgit v1.2.3 From 7706d83160267be61accb1b6f7bdc2f43299cae7 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 31 Mar 2018 00:44:17 -0400 Subject: basic split containers --- include/sway/input/seat.h | 2 +- include/sway/tree/container.h | 2 + include/sway/tree/layout.h | 3 ++ sway/commands.c | 4 ++ sway/commands/kill.c | 29 ++++++++---- sway/commands/split.c | 107 ++++++++++++++++++++++++++++++++++++++++++ sway/input/seat.c | 32 ++++++------- sway/meson.build | 1 + sway/tree/container.c | 7 +-- sway/tree/layout.c | 99 ++++++++++++++++++++++++++++++++------ 10 files changed, 240 insertions(+), 46 deletions(-) create mode 100644 sway/commands/split.c (limited to 'sway/commands') diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index 31210a5a..38795e14 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -74,7 +74,7 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container); struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum sway_container_type type); + struct sway_container *container, enum sway_container_type type); void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 6aa66da0..46f1c5ab 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -87,6 +87,8 @@ struct sway_container { } events; }; +struct sway_container *container_create(enum sway_container_type type); + // TODO only one container create function and pass the type? struct sway_container *container_output_create( struct sway_output *sway_output); diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 0a904c4b..79c14eda 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -54,4 +54,7 @@ void arrange_windows(struct sway_container *container, struct sway_container *container_get_in_direction(struct sway_container *container, struct sway_seat *seat, enum movement_direction dir); +struct sway_container *container_split(struct sway_container *child, + enum sway_container_layout layout); + #endif diff --git a/sway/commands.c b/sway/commands.c index 90544220..c85ddf7a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -163,6 +163,10 @@ static struct cmd_handler command_handlers[] = { { "kill", cmd_kill }, { "layout", cmd_layout }, { "reload", cmd_reload }, + { "split", cmd_split }, + { "splith", cmd_splith }, + { "splitt", cmd_splitt }, + { "splitv", cmd_splitv }, }; static int handler_compare(const void *_a, const void *_b) { diff --git a/sway/commands/kill.c b/sway/commands/kill.c index f6774767..80120832 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -3,21 +3,30 @@ #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/tree/view.h" +#include "sway/tree/container.h" #include "sway/commands.h" struct cmd_results *cmd_kill(int argc, char **argv) { - enum sway_container_type type = config->handler_context.current_container->type; - if (type != C_VIEW && type != C_CONTAINER) { + struct sway_container *con = + config->handler_context.current_container; + + switch (con->type) { + case C_ROOT: + case C_OUTPUT: + case C_WORKSPACE: return cmd_results_new(CMD_INVALID, NULL, "Can only kill views and containers with this command"); - } - - // TODO close arbitrary containers without a view - struct sway_view *view = - config->handler_context.current_container->sway_view; - - if (view) { - view_close(view); + break; + case C_CONTAINER: + con = container_destroy(con); + con = container_reap_empty(con); + arrange_windows(con, -1, -1); + break; + case C_VIEW: + view_close(con->sway_view); + break; + default: + break; } return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/split.c b/sway/commands/split.c new file mode 100644 index 00000000..6df20e88 --- /dev/null +++ b/sway/commands/split.c @@ -0,0 +1,107 @@ +#include +#include +#include "sway/commands.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" +#include "sway/tree/view.h" +#include "sway/input/input-manager.h" +#include "sway/input/seat.h" +#include "log.h" + +static struct cmd_results *_do_split(int argc, char **argv, int layout) { + char *name = layout == L_VERT ? "splitv" : + layout == L_HORIZ ? "splith" : "split"; + struct cmd_results *error = NULL; + if (config->reading) { + return cmd_results_new(CMD_FAILURE, name, + "Can't be used in config file."); + } + if (!config->active) { + return cmd_results_new(CMD_FAILURE, name, + "Can only be used when sway is running."); + } + if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) { + return error; + } + + struct sway_container *focused = config->handler_context.current_container; + + // TODO floating: dont split + + /* Case that focus is on an workspace with 0/1 children.change its layout */ + if (focused->type == C_WORKSPACE && focused->children->length <= 1) { + wlr_log(L_DEBUG, "changing workspace layout"); + container_set_layout(focused, layout); + } else if (focused->type != C_WORKSPACE && + focused->parent->children->length == 1) { + /* Case of no siblings. change parent layout */ + wlr_log(L_DEBUG, "changing container layout"); + container_set_layout(focused->parent, layout); + } else { + // regular case where new split container is build around focused + // container or in case of workspace, container inherits its children + wlr_log(L_DEBUG, + "Adding new container around current focused container"); + wlr_log(L_INFO, "FOCUSED SIZE: %.f %.f", + focused->width, focused->height); + + struct sway_container *parent = container_split(focused, layout); + arrange_windows(parent, -1, -1); + } + + // TODO borders: update borders + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_split(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "split", + "Can't be used in config file."); + } + if (!config->active) { + return cmd_results_new(CMD_FAILURE, "split", + "Can only be used when sway is running."); + } + if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) { + return error; + } + if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) { + _do_split(argc - 1, argv + 1, L_VERT); + } else if (strcasecmp(argv[0], "h") == 0 || + strcasecmp(argv[0], "horizontal") == 0) { + _do_split(argc - 1, argv + 1, L_HORIZ); + } else if (strcasecmp(argv[0], "t") == 0 || + strcasecmp(argv[0], "toggle") == 0) { + struct sway_container *focused = + config->handler_context.current_container; + if (focused->parent->layout == L_VERT) { + _do_split(argc - 1, argv + 1, L_HORIZ); + } else { + _do_split(argc - 1, argv + 1, L_VERT); + } + } else { + error = cmd_results_new(CMD_FAILURE, "split", + "Invalid split command (expected either horizontal or vertical)."); + return error; + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_splitv(int argc, char **argv) { + return _do_split(argc, argv, L_VERT); +} + +struct cmd_results *cmd_splith(int argc, char **argv) { + return _do_split(argc, argv, L_HORIZ); +} + +struct cmd_results *cmd_splitt(int argc, char **argv) { + struct sway_container *focused = config->handler_context.current_container; + if (focused->parent->layout == L_VERT) { + return _do_split(argc, argv, L_HORIZ); + } else { + return _do_split(argc, argv, L_VERT); + } +} diff --git a/sway/input/seat.c b/sway/input/seat.c index 9aa34aca..e0fd314a 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include #include #include #include @@ -378,6 +379,18 @@ void sway_seat_set_focus(struct sway_seat *seat, } struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { + return sway_seat_get_focus_by_type(seat, container, C_TYPES); +} + +struct sway_container *sway_seat_get_focus(struct sway_seat *seat) { + if (!seat->has_focus) { + return NULL; + } + return sway_seat_get_focus_inactive(seat, &root_container); +} + +struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, + struct sway_container *container, enum sway_container_type type) { struct sway_seat_container *current = NULL; struct sway_container *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { @@ -388,7 +401,7 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, stru } while (parent) { - if (parent == container) { + if (parent == container && (type == C_TYPES || current->container->type == type)) { return current->container; } parent = parent->parent; @@ -398,23 +411,6 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, stru return NULL; } -struct sway_container *sway_seat_get_focus(struct sway_seat *seat) { - if (!seat->has_focus) { - return NULL; - } - return sway_seat_get_focus_inactive(seat, &root_container); -} - -struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, - enum sway_container_type type) { - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); - if (focus->type == type) { - return focus; - } - - return container_parent(focus, type); -} - void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config) { // clear configs diff --git a/sway/meson.build b/sway/meson.build index 0cc620ea..b4775d58 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -19,6 +19,7 @@ sway_sources = files( 'commands/input.c', 'commands/layout.c', 'commands/mode.c', + 'commands/split.c', 'commands/seat.c', 'commands/seat/attach.c', 'commands/seat/fallback.c', diff --git a/sway/tree/container.c b/sway/tree/container.c index 746dbf1f..7b88cccb 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -38,7 +38,7 @@ static void notify_new_container(struct sway_container *container) { ipc_event_window(container, "new"); } -static struct sway_container *container_create(enum sway_container_type type) { +struct sway_container *container_create(enum sway_container_type type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; struct sway_container *c = calloc(1, sizeof(struct sway_container)); @@ -66,13 +66,14 @@ struct sway_container *container_destroy(struct sway_container *cont) { wl_signal_emit(&cont->events.destroy, cont); struct sway_container *parent = cont->parent; - if (cont->children) { + if (cont->children != NULL) { // remove children until there are no more, container_destroy calls // container_remove_child, which removes child from this container - while (cont->children->length) { + while (cont->children->length != 0) { container_destroy(cont->children->items[0]); } list_free(cont->children); + cont->children = NULL; } if (cont->marks) { list_foreach(cont->marks, free); diff --git a/sway/tree/layout.c b/sway/tree/layout.c index ce0682dc..62df19e9 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200809L +#include #include #include #include @@ -100,6 +101,8 @@ void container_add_child(struct sway_container *parent, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); child->parent = parent; + // TODO: set focus for this container? + sway_input_manager_set_focus(input_manager, child); } struct sway_container *container_reap_empty(struct sway_container *container) { @@ -135,7 +138,7 @@ struct sway_container *container_remove_child(struct sway_container *child) { } } child->parent = NULL; - return container_reap_empty(parent); + return parent; } void container_move_to(struct sway_container* container, @@ -322,7 +325,12 @@ static void apply_horiz_layout(struct sway_container *container, wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); - view_set_position(child->sway_view, child_x, y); + if (child->type == C_VIEW) { + view_set_position(child->sway_view, child_x, y); + } else { + child->x = child_x; + child->y = y; + } if (i == end - 1) { double remaining_width = x + width - child_x; @@ -505,9 +513,9 @@ static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { return NULL; } -static struct sway_container *get_swayc_in_direction_under( - struct sway_container *container, enum movement_direction dir, - struct sway_seat *seat, struct sway_container *limit) { +struct sway_container *container_get_in_direction( + struct sway_container *container, struct sway_seat *seat, + enum movement_direction dir) { if (dir == MOVE_CHILD) { return sway_seat_get_focus_inactive(seat, container); } @@ -559,7 +567,6 @@ static struct sway_container *get_swayc_in_direction_under( struct sway_container *wrap_candidate = NULL; while (true) { - // Test if we can even make a difference here bool can_move = false; int desired; int idx = index_child(container); @@ -589,7 +596,7 @@ static struct sway_container *get_swayc_in_direction_under( } if (next->children && next->children->length) { // TODO consider floating children as well - return sway_seat_get_focus_inactive(seat, next); + return sway_seat_get_focus_by_type(seat, next, C_VIEW); } else { return next; } @@ -619,21 +626,22 @@ static struct sway_container *get_swayc_in_direction_under( wrap_candidate = parent->children->items[0]; } if (config->force_focus_wrapping) { - return wrap_candidate; + return sway_seat_get_focus_by_type(seat, wrap_candidate, C_VIEW); } } } else { wlr_log(L_DEBUG, "cont %d-%p dir %i sibling %d: %p", idx, container, dir, desired, parent->children->items[desired]); - return parent->children->items[desired]; + return sway_seat_get_focus_by_type(seat, + parent->children->items[desired], C_VIEW); } } if (!can_move) { container = parent; parent = parent->parent; - if (!parent || container == limit) { + if (!parent) { // wrapping is the last chance return wrap_candidate; } @@ -641,8 +649,71 @@ static struct sway_container *get_swayc_in_direction_under( } } -struct sway_container *container_get_in_direction( - struct sway_container *container, struct sway_seat *seat, - enum movement_direction dir) { - return get_swayc_in_direction_under(container, dir, seat, NULL); +struct sway_container *container_replace_child(struct sway_container *child, + struct sway_container *new_child) { + struct sway_container *parent = child->parent; + if (parent == NULL) { + return NULL; + } + int i = index_child(child); + + // TODO floating + parent->children->items[i] = new_child; + new_child->parent = parent; + child->parent = NULL; + + // Set geometry for new child + new_child->x = child->x; + new_child->y = child->y; + new_child->width = child->width; + new_child->height = child->height; + + // reset geometry for child + child->width = 0; + child->height = 0; + + return parent; +} + +struct sway_container *container_split(struct sway_container *child, + enum sway_container_layout layout) { + // TODO floating: cannot split a floating container + if (!sway_assert(child, "child cannot be null")) { + return NULL; + } + struct sway_container *cont = container_create(C_CONTAINER); + + wlr_log(L_DEBUG, "creating container %p around %p", cont, child); + + cont->prev_layout = L_NONE; + cont->layout = layout; + cont->width = child->width; + cont->height = child->height; + cont->x = child->x; + cont->y = child->y; + + /* Container inherits all of workspaces children, layout and whatnot */ + if (child->type == C_WORKSPACE) { + struct sway_container *workspace = child; + // reorder focus + int i; + for (i = 0; i < workspace->children->length; ++i) { + ((struct sway_container *)workspace->children->items[i])->parent = + cont; + } + + // Swap children + list_t *tmp_list = workspace->children; + workspace->children = cont->children; + cont->children = tmp_list; + // add container to workspace chidren + container_add_child(workspace, cont); + // give them proper layouts + cont->layout = workspace->workspace_layout; + cont->prev_layout = workspace->prev_layout; + } else { // Or is built around container + container_replace_child(child, cont); + container_add_child(cont, child); + } + return cont; } -- cgit v1.2.3 From e7ecb001d70c71f799547d15cd45c235412af402 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 31 Mar 2018 13:20:05 -0400 Subject: reap container parent on destroy --- sway/commands/kill.c | 1 - sway/tree/container.c | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 80120832..e6036cb3 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -19,7 +19,6 @@ struct cmd_results *cmd_kill(int argc, char **argv) { break; case C_CONTAINER: con = container_destroy(con); - con = container_reap_empty(con); arrange_windows(con, -1, -1); break; case C_VIEW: diff --git a/sway/tree/container.c b/sway/tree/container.c index 7b88cccb..f972ac24 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -58,7 +58,7 @@ struct sway_container *container_create(enum sway_container_type type) { return c; } -struct sway_container *container_destroy(struct sway_container *cont) { +static struct sway_container *_container_destroy(struct sway_container *cont) { if (cont == NULL) { return NULL; } @@ -89,6 +89,11 @@ struct sway_container *container_destroy(struct sway_container *cont) { return parent; } +struct sway_container *container_destroy(struct sway_container *cont) { + cont = _container_destroy(cont); + return container_reap_empty(cont->parent); +} + struct sway_container *container_output_create( struct sway_output *sway_output) { struct wlr_box size; -- cgit v1.2.3 From b5d49cc4e86b5da5a3479c2d2a763be50184f75d Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 31 Mar 2018 21:05:58 -0400 Subject: remove default from kill switch --- sway/commands/kill.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/kill.c b/sway/commands/kill.c index e6036cb3..46d6e98e 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -14,6 +14,7 @@ struct cmd_results *cmd_kill(int argc, char **argv) { case C_ROOT: case C_OUTPUT: case C_WORKSPACE: + case C_TYPES: return cmd_results_new(CMD_INVALID, NULL, "Can only kill views and containers with this command"); break; @@ -24,8 +25,6 @@ struct cmd_results *cmd_kill(int argc, char **argv) { case C_VIEW: view_close(con->sway_view); break; - default: - break; } return cmd_results_new(CMD_SUCCESS, NULL, NULL); -- cgit v1.2.3 From e677c5b204971af00d71f9a50a89206d01b46a36 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 08:45:37 -0400 Subject: rename seat functions --- include/sway/input/seat.h | 24 +++++++++---------- sway/commands.c | 2 +- sway/commands/focus.c | 4 ++-- sway/commands/workspace.c | 2 +- sway/config.c | 2 +- sway/desktop/output.c | 2 +- sway/desktop/wl_shell.c | 2 +- sway/desktop/xdg_shell_v6.c | 2 +- sway/desktop/xwayland.c | 2 +- sway/input/cursor.c | 10 ++++---- sway/input/input-manager.c | 28 +++++++++++----------- sway/input/seat.c | 58 ++++++++++++++++++++++----------------------- sway/ipc-json.c | 4 ++-- sway/ipc-server.c | 4 ++-- sway/tree/container.c | 2 +- sway/tree/layout.c | 8 +++---- sway/tree/workspace.c | 14 +++++------ 17 files changed, 85 insertions(+), 85 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index 31210a5a..38a6379c 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -38,28 +38,28 @@ struct sway_seat { struct wl_list link; // input_manager::seats }; -struct sway_seat *sway_seat_create(struct sway_input_manager *input, +struct sway_seat *seat_create(struct sway_input_manager *input, const char *seat_name); -void sway_seat_destroy(struct sway_seat *seat); +void seat_destroy(struct sway_seat *seat); -void sway_seat_add_device(struct sway_seat *seat, +void seat_add_device(struct sway_seat *seat, struct sway_input_device *device); -void sway_seat_configure_device(struct sway_seat *seat, +void seat_configure_device(struct sway_seat *seat, struct sway_input_device *device); -void sway_seat_remove_device(struct sway_seat *seat, +void seat_remove_device(struct sway_seat *seat, struct sway_input_device *device); -void sway_seat_configure_xcursor(struct sway_seat *seat); +void seat_configure_xcursor(struct sway_seat *seat); -void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *container); +void seat_set_focus(struct sway_seat *seat, struct sway_container *container); -void sway_seat_set_focus_warp(struct sway_seat *seat, +void seat_set_focus_warp(struct sway_seat *seat, struct sway_container *container, bool warp); -struct sway_container *sway_seat_get_focus(struct sway_seat *seat); +struct sway_container *seat_get_focus(struct sway_seat *seat); /** * Return the last container to be focused for the seat (or the most recently @@ -70,12 +70,12 @@ struct sway_container *sway_seat_get_focus(struct sway_seat *seat); * is destroyed, or focus moves to a container with children and we need to * descend into the next leaf in focus order. */ -struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, +struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container); -struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, +struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, enum sway_container_type type); -void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); +void seat_set_config(struct sway_seat *seat, struct seat_config *seat_config); #endif diff --git a/sway/commands.c b/sway/commands.c index 90544220..91b44fbf 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -340,7 +340,7 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) { // without criteria, the command acts upon the focused // container config->handler_context.current_container = - sway_seat_get_focus_inactive(seat, &root_container); + seat_get_focus_inactive(seat, &root_container); if (!sway_assert(config->handler_context.current_container, "could not get focus-inactive for root container")) { return NULL; diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 64f079f4..4fb732dd 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -40,7 +40,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { } if (argc == 0) { - sway_seat_set_focus(seat, con); + seat_set_focus(seat, con); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } @@ -53,7 +53,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { struct sway_container *next_focus = container_get_in_direction(con, seat, direction); if (next_focus) { - sway_seat_set_focus(seat, next_focus); + seat_set_focus(seat, next_focus); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 8f39e5fc..aa4096f7 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -91,7 +91,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { } workspace_switch(ws); current_container = - sway_seat_get_focus(config->handler_context.seat); + seat_get_focus(config->handler_context.seat); struct sway_container *new_output = container_parent(current_container, C_OUTPUT); if (config->mouse_warping && old_output != new_output) { diff --git a/sway/config.c b/sway/config.c index 0eecf7f6..90b833ab 100644 --- a/sway/config.c +++ b/sway/config.c @@ -125,7 +125,7 @@ static void destroy_removed_seats(struct sway_config *old_config, seat_name_cmp, seat_config->name) < 0) { seat = input_manager_get_seat(input_manager, seat_config->name); - sway_seat_destroy(seat); + seat_destroy(seat); } } } diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 0d706c52..86b023cb 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -228,7 +228,7 @@ static void render_output(struct sway_output *output, struct timespec *when, struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = - sway_seat_get_focus_inactive(seat, output->swayc); + seat_get_focus_inactive(seat, output->swayc); struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : container_parent(focus, C_WORKSPACE)); diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index 4fcc6317..3e275f2b 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -133,7 +133,7 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { wl_signal_add(&shell_surface->events.destroy, &sway_surface->destroy); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = seat_get_focus_inactive(seat, &root_container); struct sway_container *cont = container_view_create(focus, sway_view); sway_view->swayc = cont; diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 713437f2..286d52cc 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -98,7 +98,7 @@ static void handle_map(struct wl_listener *listener, void *data) { container_view_destroy(view->swayc); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = seat_get_focus_inactive(seat, &root_container); struct sway_container *cont = container_view_create(focus, view); view->swayc = cont; arrange_windows(cont->parent, -1, -1); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 273ca2bf..8fb6cb52 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -143,7 +143,7 @@ static void handle_map(struct wl_listener *listener, void *data) { wlr_xwayland_surface_set_maximized(xsurface, true); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, + struct sway_container *focus = seat_get_focus_inactive(seat, &root_container); struct sway_container *cont = container_view_create(focus, view); view->swayc = cont; diff --git a/sway/input/cursor.c b/sway/input/cursor.c index d608a9cf..7d05e942 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -84,7 +84,7 @@ static struct sway_container *container_at_cursor(struct sway_cursor *cursor, // find the focused workspace on the output for this seat struct sway_container *ws = - sway_seat_get_focus_inactive(cursor->seat, output->swayc); + seat_get_focus_inactive(cursor->seat, output->swayc); if (ws && ws->type != C_WORKSPACE) { ws = container_parent(ws, C_WORKSPACE); } @@ -129,7 +129,7 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, double sx, sy; struct sway_container *c = container_at_cursor(cursor, &surface, &sx, &sy); if (c && config->focus_follows_mouse) { - sway_seat_set_focus_warp(cursor->seat, c, false); + seat_set_focus_warp(cursor->seat, c, false); } // reset cursor if switching between clients @@ -191,15 +191,15 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { if (new_ws && new_ws->type != C_WORKSPACE) { new_ws = container_parent(new_ws, C_WORKSPACE); } - struct sway_container *old_ws = sway_seat_get_focus(cursor->seat); + struct sway_container *old_ws = seat_get_focus(cursor->seat); if (old_ws && old_ws->type != C_WORKSPACE) { old_ws = container_parent(old_ws, C_WORKSPACE); } if (new_ws != old_ws) { - sway_seat_set_focus(cursor->seat, cont); + seat_set_focus(cursor->seat, cont); } } else { - sway_seat_set_focus(cursor->seat, cont); + seat_set_focus(cursor->seat, cont); } wlr_seat_pointer_notify_button(cursor->seat->wlr_seat, event->time_msec, diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index d421a03f..66ade685 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -40,7 +40,7 @@ struct sway_seat *input_manager_get_seat( } } - return sway_seat_create(input, seat_name); + return seat_create(input, seat_name); } static char *get_device_identifier(struct wlr_input_device *device) { @@ -175,7 +175,7 @@ static void handle_device_destroy(struct wl_listener *listener, void *data) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { - sway_seat_remove_device(seat, input_device); + seat_remove_device(seat, input_device); } wl_list_remove(&input_device->link); @@ -221,7 +221,7 @@ static void handle_new_input(struct wl_listener *listener, void *data) { if (!input_has_seat_configuration(input)) { wlr_log(L_DEBUG, "no seat configuration, using default seat"); seat = input_manager_get_seat(input, default_seat); - sway_seat_add_device(seat, input_device); + seat_add_device(seat, input_device); return; } @@ -232,7 +232,7 @@ static void handle_new_input(struct wl_listener *listener, void *data) { seat_config_get_attachment(seat->config, "*")); if (has_attachment) { - sway_seat_add_device(seat, input_device); + seat_add_device(seat, input_device); added = true; } } @@ -240,7 +240,7 @@ static void handle_new_input(struct wl_listener *listener, void *data) { if (!added) { wl_list_for_each(seat, &input->seats, link) { if (seat->config && seat->config->fallback == 1) { - sway_seat_add_device(seat, input_device); + seat_add_device(seat, input_device); added = true; } } @@ -281,7 +281,7 @@ bool sway_input_manager_has_focus(struct sway_input_manager *input, struct sway_container *container) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - if (sway_seat_get_focus(seat) == container) { + if (seat_get_focus(seat) == container) { return true; } } @@ -293,7 +293,7 @@ void sway_input_manager_set_focus(struct sway_input_manager *input, struct sway_container *container) { struct sway_seat *seat ; wl_list_for_each(seat, &input->seats, link) { - sway_seat_set_focus(seat, container); + seat_set_focus(seat, container); } } @@ -311,7 +311,7 @@ void sway_input_manager_apply_input_config(struct sway_input_manager *input, struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - sway_seat_configure_device(seat, input_device); + seat_configure_device(seat, input_device); } } } @@ -326,7 +326,7 @@ void sway_input_manager_apply_seat_config(struct sway_input_manager *input, return; } - sway_seat_set_config(seat, seat_config); + seat_set_config(seat, seat_config); // for every device, try to add it to a seat and if no seat has it // attached, add it to the fallback seats. @@ -355,17 +355,17 @@ void sway_input_manager_apply_seat_config(struct sway_input_manager *input, } } if (attached) { - sway_seat_add_device(seat, input_device); + seat_add_device(seat, input_device); } else { - sway_seat_remove_device(seat, input_device); + seat_remove_device(seat, input_device); } } } else { wl_list_for_each(seat, &input->seats, link) { if (seat->config && seat->config->fallback == 1) { - sway_seat_add_device(seat, input_device); + seat_add_device(seat, input_device); } else { - sway_seat_remove_device(seat, input_device); + seat_remove_device(seat, input_device); } } } @@ -376,7 +376,7 @@ void sway_input_manager_apply_seat_config(struct sway_input_manager *input, void sway_input_manager_configure_xcursor(struct sway_input_manager *input) { struct sway_seat *seat = NULL; wl_list_for_each(seat, &input->seats, link) { - sway_seat_configure_xcursor(seat); + seat_configure_xcursor(seat); } } diff --git a/sway/input/seat.c b/sway/input/seat.c index 9aa34aca..70395144 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -25,7 +25,7 @@ static void seat_device_destroy(struct sway_seat_device *seat_device) { free(seat_device); } -void sway_seat_destroy(struct sway_seat *seat) { +void seat_destroy(struct sway_seat *seat) { struct sway_seat_device *seat_device, *next; wl_list_for_each_safe(seat_device, next, &seat->devices, link) { seat_device_destroy(seat_device); @@ -42,18 +42,18 @@ static void handle_seat_container_destroy(struct wl_listener *listener, struct sway_seat *seat = seat_con->seat; struct sway_container *con = seat_con->container; - bool is_focus = (sway_seat_get_focus(seat) == con); + bool is_focus = (seat_get_focus(seat) == con); wl_list_remove(&seat_con->link); if (is_focus) { // pick next focus - sway_seat_set_focus(seat, NULL); - struct sway_container *next = sway_seat_get_focus_inactive(seat, con->parent); + seat_set_focus(seat, NULL); + struct sway_container *next = seat_get_focus_inactive(seat, con->parent); if (next == NULL) { next = con->parent; } - sway_seat_set_focus(seat, next); + seat_set_focus(seat, next); } wl_list_remove(&seat_con->destroy.link); @@ -110,7 +110,7 @@ static void collect_focus_iter(struct sway_container *con, void *data) { wl_list_insert(&seat->focus_stack, &seat_con->link); } -struct sway_seat *sway_seat_create(struct sway_input_manager *input, +struct sway_seat *seat_create(struct sway_input_manager *input, const char *seat_name) { struct sway_seat *seat = calloc(1, sizeof(struct sway_seat)); if (!seat) { @@ -147,7 +147,7 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH); - sway_seat_configure_xcursor(seat); + seat_configure_xcursor(seat); wl_list_insert(&input->seats, &seat->link); @@ -169,7 +169,7 @@ static void seat_configure_keyboard(struct sway_seat *seat, sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); - struct sway_container *focus = sway_seat_get_focus(seat); + struct sway_container *focus = seat_get_focus(seat); if (focus && focus->type == C_VIEW) { // force notify reenter to pick up the new configuration wlr_seat_keyboard_clear_focus(seat->wlr_seat); @@ -179,7 +179,7 @@ static void seat_configure_keyboard(struct sway_seat *seat, } } -static struct sway_seat_device *sway_seat_get_device(struct sway_seat *seat, +static struct sway_seat_device *seat_get_device(struct sway_seat *seat, struct sway_input_device *input_device) { struct sway_seat_device *seat_device = NULL; wl_list_for_each(seat_device, &seat->devices, link) { @@ -191,10 +191,10 @@ static struct sway_seat_device *sway_seat_get_device(struct sway_seat *seat, return NULL; } -void sway_seat_configure_device(struct sway_seat *seat, +void seat_configure_device(struct sway_seat *seat, struct sway_input_device *input_device) { struct sway_seat_device *seat_device = - sway_seat_get_device(seat, input_device); + seat_get_device(seat, input_device); if (!seat_device) { return; } @@ -219,10 +219,10 @@ void sway_seat_configure_device(struct sway_seat *seat, } } -void sway_seat_add_device(struct sway_seat *seat, +void seat_add_device(struct sway_seat *seat, struct sway_input_device *input_device) { - if (sway_seat_get_device(seat, input_device)) { - sway_seat_configure_device(seat, input_device); + if (seat_get_device(seat, input_device)) { + seat_configure_device(seat, input_device); return; } @@ -240,13 +240,13 @@ void sway_seat_add_device(struct sway_seat *seat, seat_device->input_device = input_device; wl_list_insert(&seat->devices, &seat_device->link); - sway_seat_configure_device(seat, input_device); + seat_configure_device(seat, input_device); } -void sway_seat_remove_device(struct sway_seat *seat, +void seat_remove_device(struct sway_seat *seat, struct sway_input_device *input_device) { struct sway_seat_device *seat_device = - sway_seat_get_device(seat, input_device); + seat_get_device(seat, input_device); if (!seat_device) { return; @@ -258,7 +258,7 @@ void sway_seat_remove_device(struct sway_seat *seat, seat_device_destroy(seat_device); } -void sway_seat_configure_xcursor(struct sway_seat *seat) { +void seat_configure_xcursor(struct sway_seat *seat) { // TODO configure theme and size const char *cursor_theme = NULL; @@ -292,9 +292,9 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { seat->cursor->cursor->y); } -void sway_seat_set_focus_warp(struct sway_seat *seat, +void seat_set_focus_warp(struct sway_seat *seat, struct sway_container *container, bool warp) { - struct sway_container *last_focus = sway_seat_get_focus(seat); + struct sway_container *last_focus = seat_get_focus(seat); if (container && last_focus == container) { return; @@ -372,12 +372,12 @@ void sway_seat_set_focus_warp(struct sway_seat *seat, seat->has_focus = (container != NULL); } -void sway_seat_set_focus(struct sway_seat *seat, +void seat_set_focus(struct sway_seat *seat, struct sway_container *container) { - sway_seat_set_focus_warp(seat, container, true); + seat_set_focus_warp(seat, container, true); } -struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { +struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { struct sway_seat_container *current = NULL; struct sway_container *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { @@ -398,16 +398,16 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, stru return NULL; } -struct sway_container *sway_seat_get_focus(struct sway_seat *seat) { +struct sway_container *seat_get_focus(struct sway_seat *seat) { if (!seat->has_focus) { return NULL; } - return sway_seat_get_focus_inactive(seat, &root_container); + return seat_get_focus_inactive(seat, &root_container); } -struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, +struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, enum sway_container_type type) { - struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = seat_get_focus_inactive(seat, &root_container); if (focus->type == type) { return focus; } @@ -415,7 +415,7 @@ struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat, return container_parent(focus, type); } -void sway_seat_set_config(struct sway_seat *seat, +void seat_set_config(struct sway_seat *seat, struct seat_config *seat_config) { // clear configs free_seat_config(seat->config); @@ -434,6 +434,6 @@ void sway_seat_set_config(struct sway_seat *seat, seat->config = copy_seat_config(seat_config); wl_list_for_each(seat_device, &seat->devices, link) { - sway_seat_configure_device(seat, seat_device->input_device); + seat_configure_device(seat, seat_device->input_device); } } diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 7c5f7304..1b906e6f 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -92,7 +92,7 @@ static void ipc_json_describe_output(struct sway_container *container, json_obje const char *ws = NULL; if (seat) { struct sway_container *focus = - sway_seat_get_focus_inactive(seat, container); + seat_get_focus_inactive(seat, container); if (focus && focus->type != C_WORKSPACE) { focus = container_parent(focus, C_WORKSPACE); } @@ -140,7 +140,7 @@ json_object *ipc_json_describe_container(struct sway_container *c) { } struct sway_seat *seat = sway_input_manager_get_default_seat(input_manager); - bool focused = sway_seat_get_focus(seat) == c; + bool focused = seat_get_focus(seat) == c; json_object *object = json_object_new_object(); diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 869f1ed0..8c06f71b 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -389,7 +389,7 @@ static void ipc_get_workspaces_callback(struct sway_container *workspace, // it's set differently for the get_workspaces reply struct sway_seat *seat = sway_input_manager_get_default_seat(input_manager); - struct sway_container *focused_ws = sway_seat_get_focus(seat); + struct sway_container *focused_ws = seat_get_focus(seat); if (focused_ws != NULL && focused_ws->type != C_WORKSPACE) { focused_ws = container_parent(focused_ws, C_WORKSPACE); } @@ -399,7 +399,7 @@ static void ipc_get_workspaces_callback(struct sway_container *workspace, json_object_new_boolean(focused)); json_object_array_add((json_object *)data, workspace_json); - focused_ws = sway_seat_get_focus_inactive(seat, workspace->parent); + focused_ws = seat_get_focus_inactive(seat, workspace->parent); if (focused_ws->type != C_WORKSPACE) { focused_ws = container_parent(focused_ws, C_WORKSPACE); } diff --git a/sway/tree/container.c b/sway/tree/container.c index 746dbf1f..2d752862 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -144,7 +144,7 @@ struct sway_container *container_output_create( struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { if (!seat->has_focus) { - sway_seat_set_focus(seat, ws); + seat_set_focus(seat, ws); } } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index ce0682dc..0a766b22 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -404,7 +404,7 @@ static struct sway_container *get_swayc_in_output_direction( return NULL; } - struct sway_container *ws = sway_seat_get_focus_inactive(seat, output); + struct sway_container *ws = seat_get_focus_inactive(seat, output); if (ws->type != C_WORKSPACE) { ws = container_parent(ws, C_WORKSPACE); } @@ -425,7 +425,7 @@ static struct sway_container *get_swayc_in_output_direction( case MOVE_UP: case MOVE_DOWN: { struct sway_container *focused = - sway_seat_get_focus_inactive(seat, ws); + seat_get_focus_inactive(seat, ws); if (focused && focused->parent) { struct sway_container *parent = focused->parent; if (parent->layout == L_VERT) { @@ -509,7 +509,7 @@ static struct sway_container *get_swayc_in_direction_under( struct sway_container *container, enum movement_direction dir, struct sway_seat *seat, struct sway_container *limit) { if (dir == MOVE_CHILD) { - return sway_seat_get_focus_inactive(seat, container); + return seat_get_focus_inactive(seat, container); } struct sway_container *parent = container->parent; @@ -589,7 +589,7 @@ static struct sway_container *get_swayc_in_direction_under( } if (next->children && next->children->length) { // TODO consider floating children as well - return sway_seat_get_focus_inactive(seat, next); + return seat_get_focus_inactive(seat, next); } else { return next; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index c629f1f1..c47bab33 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -158,7 +158,7 @@ static bool _workspace_by_name(struct sway_container *view, void *data) { struct sway_container *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *current_workspace = NULL, *current_output = NULL; - struct sway_container *focus = sway_seat_get_focus(seat); + struct sway_container *focus = seat_get_focus(seat); if (focus) { current_workspace = container_parent(focus, C_WORKSPACE); current_output = container_parent(focus, C_OUTPUT); @@ -200,7 +200,7 @@ struct sway_container *workspace_create(const char *name) { // Otherwise create a new one struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = - sway_seat_get_focus_inactive(seat, &root_container); + seat_get_focus_inactive(seat, &root_container); parent = focus; parent = container_parent(parent, C_OUTPUT); struct sway_container *new_ws = container_workspace_create(parent, name); @@ -260,7 +260,7 @@ struct sway_container *workspace_output_prev_next_impl( } struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, output); + struct sway_container *focus = seat_get_focus_inactive(seat, output); struct sway_container *workspace = (focus->type == C_WORKSPACE ? focus : container_parent(focus, C_WORKSPACE)); @@ -345,7 +345,7 @@ bool workspace_switch(struct sway_container *workspace) { } struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = - sway_seat_get_focus_inactive(seat, &root_container); + seat_get_focus_inactive(seat, &root_container); if (!seat || !focus) { return false; } @@ -376,11 +376,11 @@ bool workspace_switch(struct sway_container *workspace) { wlr_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); - struct sway_container *next = sway_seat_get_focus_inactive(seat, workspace); + struct sway_container *next = seat_get_focus_inactive(seat, workspace); if (next == NULL) { next = workspace; } - sway_seat_set_focus(seat, next); + seat_set_focus(seat, next); struct sway_container *output = container_parent(workspace, C_OUTPUT); arrange_windows(output, -1, -1); return true; @@ -389,7 +389,7 @@ bool workspace_switch(struct sway_container *workspace) { bool workspace_is_visible(struct sway_container *ws) { struct sway_container *output = container_parent(ws, C_OUTPUT); struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = sway_seat_get_focus_inactive(seat, output); + struct sway_container *focus = seat_get_focus_inactive(seat, output); if (focus->type != C_WORKSPACE) { focus = container_parent(focus, C_WORKSPACE); } -- cgit v1.2.3 From b2d871cfe215a82266d01847f4787bbcf8c721c9 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 31 Mar 2018 21:21:26 -0400 Subject: Partially implement move command Works: - move [container|window] to workspace - Note, this should be able to move C_CONTAINER but this is untested - move [workspace] to output [left|right|up|down|] Not implemented yet: - move [left|right|up|down] - move scratchpad - move position --- include/sway/output.h | 2 + include/sway/tree/container.h | 5 ++ include/sway/tree/layout.h | 12 +-- sway/commands.c | 1 + sway/commands/focus.c | 7 +- sway/commands/move.c | 181 ++++++++++++++++++++++++++++++++++++++++++ sway/desktop/output.c | 9 ++- sway/input/keyboard.c | 4 +- sway/meson.build | 1 + sway/tree/container.c | 18 +++++ sway/tree/layout.c | 58 ++++++++------ sway/tree/output.c | 11 +++ 12 files changed, 268 insertions(+), 41 deletions(-) create mode 100644 sway/commands/move.c (limited to 'sway/commands') diff --git a/include/sway/output.h b/include/sway/output.h index b4980cd8..b343ecff 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -36,4 +36,6 @@ void output_damage_whole(struct sway_output *output); void output_damage_whole_view(struct sway_output *output, struct sway_view *view); +struct sway_container *output_by_name(const char *name); + #endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 6aa66da0..aff2e58e 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -84,9 +84,14 @@ struct sway_container { struct { struct wl_signal destroy; + // Raised after the tree updates, but before arrange_windows + // Passed the previous parent + struct wl_signal reparent; } events; }; +const char *container_type_to_str(enum sway_container_type type); + // TODO only one container create function and pass the type? struct sway_container *container_output_create( struct sway_output *sway_output); diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 0a904c4b..e1034657 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -11,9 +11,6 @@ enum movement_direction { MOVE_DOWN, MOVE_PARENT, MOVE_CHILD, - MOVE_NEXT, - MOVE_PREV, - MOVE_FIRST }; struct sway_container; @@ -32,7 +29,8 @@ struct sway_root { void layout_init(void); -void container_add_child(struct sway_container *parent, struct sway_container *child); +void container_add_child(struct sway_container *parent, + struct sway_container *child); struct sway_container *container_add_sibling(struct sway_container *parent, struct sway_container *child); @@ -44,7 +42,11 @@ struct sway_container *container_reap_empty(struct sway_container *container); void container_move_to(struct sway_container* container, struct sway_container* destination); -enum sway_container_layout container_get_default_layout(struct sway_container *output); +void container_move(struct sway_container *container, + enum movement_direction dir, int move_amt); + +enum sway_container_layout container_get_default_layout( + struct sway_container *output); void container_sort_workspaces(struct sway_container *output); diff --git a/sway/commands.c b/sway/commands.c index 90544220..b6af3d1a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -162,6 +162,7 @@ static struct cmd_handler command_handlers[] = { { "focus", cmd_focus }, { "kill", cmd_kill }, { "layout", cmd_layout }, + { "move", cmd_move }, { "reload", cmd_reload }, }; diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 64f079f4..0a521b9e 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -20,10 +20,6 @@ static bool parse_movement_direction(const char *name, *out = MOVE_PARENT; } else if (strcasecmp(name, "child") == 0) { *out = MOVE_CHILD; - } else if (strcasecmp(name, "next") == 0) { - *out = MOVE_NEXT; - } else if (strcasecmp(name, "prev") == 0) { - *out = MOVE_PREV; } else { return false; } @@ -51,7 +47,8 @@ struct cmd_results *cmd_focus(int argc, char **argv) { "Expected 'focus ' or 'focus output '"); } - struct sway_container *next_focus = container_get_in_direction(con, seat, direction); + struct sway_container *next_focus = container_get_in_direction( + con, seat, direction); if (next_focus) { sway_seat_set_focus(seat, next_focus); } diff --git a/sway/commands/move.c b/sway/commands/move.c new file mode 100644 index 00000000..222ef314 --- /dev/null +++ b/sway/commands/move.c @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/input/seat.h" +#include "sway/output.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" +#include "sway/tree/workspace.h" +#include "stringop.h" +#include "list.h" + +static const char* expected_syntax = + "Expected 'move <[px] px>' or " + "'move to workspace ' or " + "'move to output ' or " + "'move position mouse'"; + +static struct sway_container *output_in_direction(const char *direction, + struct wlr_output *reference, int ref_ox, int ref_oy) { + int ref_lx = ref_ox + reference->lx, + ref_ly = ref_oy + reference->ly; + struct { + char *name; + enum wlr_direction direction; + } names[] = { + { "up", WLR_DIRECTION_UP }, + { "down", WLR_DIRECTION_DOWN }, + { "left", WLR_DIRECTION_LEFT }, + { "right", WLR_DIRECTION_RIGHT }, + }; + for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); ++i) { + if (strcasecmp(names[i].name, direction) == 0) { + struct wlr_output *adjacent = wlr_output_layout_adjacent_output( + root_container.sway_root->output_layout, + names[i].direction, reference, ref_lx, ref_ly); + if (adjacent) { + struct sway_output *sway_output = adjacent->data; + return sway_output->swayc; + } + break; + } + } + return output_by_name(direction); +} + +static struct cmd_results *cmd_move_container(struct sway_container *current, + int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "move container/window", + EXPECTED_AT_LEAST, 4))) { + return error; + } else if (strcasecmp(argv[1], "to") == 0 + && strcasecmp(argv[2], "workspace") == 0) { + // move container to workspace x + if (current->type == C_WORKSPACE) { + // TODO: Wrap children in a container and move that + return cmd_results_new(CMD_FAILURE, "move", "Unimplemented"); + } else if (current->type != C_CONTAINER && current->type != C_VIEW) { + return cmd_results_new(CMD_FAILURE, "move", + "Can only move containers and views."); + } + struct sway_container *ws; + const char *num_name = NULL; + char *ws_name = NULL; + if (argc == 5 && strcasecmp(argv[3], "number") == 0) { + // move "container to workspace number x" + num_name = argv[4]; + ws = workspace_by_number(num_name); + } else { + ws_name = join_args(argv + 3, argc - 3); + ws = workspace_by_name(ws_name); + } + if (!ws) { + ws = workspace_create(ws_name ? ws_name : num_name); + } + free(ws_name); + struct sway_container *old_parent = current->parent; + struct sway_container *focus = sway_seat_get_focus_inactive( + config->handler_context.seat, ws); + container_move_to(current, focus); + sway_seat_set_focus(config->handler_context.seat, old_parent); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } else if (strcasecmp(argv[1], "to") == 0 + && strcasecmp(argv[2], "output") == 0) { + if (current->type == C_WORKSPACE) { + // TODO: Wrap children in a container and move that + return cmd_results_new(CMD_FAILURE, "move", "Unimplemented"); + } else if (current->type != C_CONTAINER + && current->type != C_VIEW) { + return cmd_results_new(CMD_FAILURE, "move", + "Can only move containers and views."); + } + struct sway_container *source = container_parent(current, C_OUTPUT); + struct sway_container *destination = output_in_direction(argv[3], + source->sway_output->wlr_output, current->x, current->y); + if (!destination) { + return cmd_results_new(CMD_FAILURE, "move workspace", + "Can't find output with name/direction '%s'", argv[3]); + } + struct sway_container *focus = sway_seat_get_focus_inactive( + config->handler_context.seat, destination); + if (!focus) { + // We've never been to this output before + focus = destination->children->items[0]; + } + container_move_to(current, focus); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + return cmd_results_new(CMD_INVALID, "move", expected_syntax); +} + +static struct cmd_results *cmd_move_workspace(struct sway_container *current, + int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 4))) { + return error; + } else if (strcasecmp(argv[1], "to") != 0 + || strcasecmp(argv[2], "output") != 0) { + return cmd_results_new(CMD_INVALID, "move", expected_syntax); + } + struct sway_container *source = container_parent(current, C_OUTPUT); + int center_x = current->width / 2 + current->x, + center_y = current->height / 2 + current->y; + struct sway_container *destination = output_in_direction(argv[3], + source->sway_output->wlr_output, center_x, center_y); + if (!destination) { + return cmd_results_new(CMD_FAILURE, "move workspace", + "Can't find output with name/direction '%s'", argv[3]); + } + if (current->type != C_WORKSPACE) { + current = container_parent(current, C_WORKSPACE); + } + container_move_to(current, destination); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +struct cmd_results *cmd_move(int argc, char **argv) { + struct cmd_results *error = NULL; + int move_amt = 10; + if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) { + return error; + } + struct sway_container *current = config->handler_context.current_container; + + if (argc == 2 || (argc == 3 && strcasecmp(argv[2], "px") == 0)) { + char *inv; + move_amt = (int)strtol(argv[1], &inv, 10); + if (*inv != '\0' && strcasecmp(inv, "px") != 0) { + move_amt = 10; + } + } + + if (strcasecmp(argv[0], "left") == 0) { + container_move(current, MOVE_LEFT, move_amt); + } else if (strcasecmp(argv[0], "right") == 0) { + container_move(current, MOVE_RIGHT, move_amt); + } else if (strcasecmp(argv[0], "up") == 0) { + container_move(current, MOVE_UP, move_amt); + } else if (strcasecmp(argv[0], "down") == 0) { + container_move(current, MOVE_DOWN, move_amt); + } else if (strcasecmp(argv[0], "container") == 0 + || strcasecmp(argv[0], "window") == 0) { + return cmd_move_container(current, argc, argv); + } else if (strcasecmp(argv[0], "workspace") == 0) { + return cmd_move_workspace(current, argc, argv); + } else if (strcasecmp(argv[0], "scratchpad") == 0 + || (strcasecmp(argv[0], "to") == 0 + && strcasecmp(argv[1], "scratchpad") == 0)) { + // TODO: scratchpad + return cmd_results_new(CMD_FAILURE, "move", "Unimplemented"); + } else if (strcasecmp(argv[0], "position") == 0) { + // TODO: floating + return cmd_results_new(CMD_FAILURE, "move", "Unimplemented"); + } else { + return cmd_results_new(CMD_INVALID, "move", expected_syntax); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 0d706c52..c4265818 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -229,9 +229,12 @@ static void render_output(struct sway_output *output, struct timespec *when, struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = sway_seat_get_focus_inactive(seat, output->swayc); - struct sway_container *workspace = (focus->type == C_WORKSPACE ? - focus : - container_parent(focus, C_WORKSPACE)); + if (!focus) { + // We've never been to this output before + focus = output->swayc->children->items[0]; + } + struct sway_container *workspace = focus->type == C_WORKSPACE ? + focus : container_parent(focus, C_WORKSPACE); struct render_data rdata = { .output = output, diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 99685052..8d22b684 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -97,8 +97,8 @@ static void keyboard_execute_command(struct sway_keyboard *keyboard, config->handler_context.seat = keyboard->seat_device->sway_seat; struct cmd_results *results = execute_command(binding->command, NULL); if (results->status != CMD_SUCCESS) { - wlr_log(L_DEBUG, "could not run command for binding: %s", - binding->command); + wlr_log(L_DEBUG, "could not run command for binding: %s (%s)", + binding->command, results->error); } free_cmd_results(results); } diff --git a/sway/meson.build b/sway/meson.build index 0cc620ea..38945b4c 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -19,6 +19,7 @@ sway_sources = files( 'commands/input.c', 'commands/layout.c', 'commands/mode.c', + 'commands/move.c', 'commands/seat.c', 'commands/seat/attach.c', 'commands/seat/fallback.c', diff --git a/sway/tree/container.c b/sway/tree/container.c index 746dbf1f..21fe3fb0 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -33,6 +33,23 @@ static list_t *get_bfs_queue() { return bfs_queue; } +const char *container_type_to_str(enum sway_container_type type) { + switch (type) { + case C_ROOT: + return "C_ROOT"; + case C_OUTPUT: + return "C_OUTPUT"; + case C_WORKSPACE: + return "C_WORKSPACE"; + case C_CONTAINER: + return "C_CONTAINER"; + case C_VIEW: + return "C_VIEW"; + default: + return "C_UNKNOWN"; + } +} + static void notify_new_container(struct sway_container *container) { wl_signal_emit(&root_container.sway_root->events.new_container, container); ipc_event_window(container, "new"); @@ -54,6 +71,7 @@ static struct sway_container *container_create(enum sway_container_type type) { } wl_signal_init(&c->events.destroy); + wl_signal_init(&c->events.reparent); return c; } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index ce0682dc..a8941a40 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -106,8 +106,10 @@ struct sway_container *container_reap_empty(struct sway_container *container) { if (!sway_assert(container, "reaping null container")) { return NULL; } - wlr_log(L_DEBUG, "reaping %p %s", container, container->name); - while (container != &root_container && container->children->length == 0) { + wlr_log(L_DEBUG, "Reaping %p %s '%s'", container, + container_type_to_str(container->type), container->name); + while (container->type != C_ROOT && container->type != C_OUTPUT + && container->children->length == 0) { if (container->type == C_WORKSPACE) { if (!workspace_is_visible(container)) { struct sway_container *parent = container->parent; @@ -138,22 +140,46 @@ struct sway_container *container_remove_child(struct sway_container *child) { return container_reap_empty(parent); } -void container_move_to(struct sway_container* container, - struct sway_container* destination) { +void container_move_to(struct sway_container *container, + struct sway_container *destination) { if (container == destination || container_has_anscestor(container, destination)) { return; } struct sway_container *old_parent = container_remove_child(container); container->width = container->height = 0; - struct sway_container *new_parent = - container_add_sibling(destination, container); + struct sway_container *new_parent; + if (destination->type == C_VIEW) { + new_parent = container_add_sibling(destination, container); + } else { + new_parent = destination; + container_add_child(destination, container); + } + wl_signal_emit(&container->events.reparent, old_parent); + if (container->type == C_WORKSPACE) { + struct sway_seat *seat = sway_input_manager_get_default_seat( + input_manager); + if (old_parent->children->length == 0) { + char *ws_name = workspace_next_name(old_parent->name); + struct sway_container *ws = + container_workspace_create(old_parent, ws_name); + free(ws_name); + sway_seat_set_focus(seat, ws); + } + container_sort_workspaces(new_parent); + sway_seat_set_focus(seat, new_parent); + } if (old_parent) { arrange_windows(old_parent, -1, -1); } arrange_windows(new_parent, -1, -1); } +void container_move(struct sway_container *container, + enum movement_direction dir, int move_amt) { + // TODO +} + enum sway_container_layout container_get_default_layout( struct sway_container *output) { if (config->default_layout != L_NONE) { @@ -521,26 +547,6 @@ static struct sway_container *get_swayc_in_direction_under( } } - if (dir == MOVE_PREV || dir == MOVE_NEXT) { - int focused_idx = index_child(container); - if (focused_idx == -1) { - return NULL; - } else { - int desired = (focused_idx + (dir == MOVE_NEXT ? 1 : -1)) % - parent->children->length; - if (desired < 0) { - desired += parent->children->length; - } - return parent->children->items[desired]; - } - } - - // If moving to an adjacent output we need a starting position (since this - // output might border to multiple outputs). - //struct wlc_point abs_pos; - //get_layout_center_position(container, &abs_pos); - - // TODO WLR fullscreen /* if (container->type == C_VIEW && swayc_is_fullscreen(container)) { diff --git a/sway/tree/output.c b/sway/tree/output.c index 7248fd00..2331dc2b 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -1,3 +1,4 @@ +#include #include "sway/tree/container.h" #include "sway/tree/layout.h" #include "sway/output.h" @@ -37,3 +38,13 @@ struct sway_container *container_output_destroy(struct sway_container *output) { container_destroy(output); return &root_container; } + +struct sway_container *output_by_name(const char *name) { + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + if (strcasecmp(output->name, name) == 0){ + return output; + } + } + return NULL; +} -- cgit v1.2.3 From a82c107c7613548a3ae0e75bb951ba57b7d2f22b Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 11:44:42 -0400 Subject: 80col --- sway/commands.c | 14 +++++++------- sway/commands/seat.c | 11 ++++++++--- sway/input/input-manager.c | 45 ++++++++++++++++++++++++++++++--------------- sway/input/seat.c | 18 ++++++++++++------ 4 files changed, 57 insertions(+), 31 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index dc6c867f..9e83a401 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -75,20 +75,20 @@ void apply_input_config(struct input_config *input) { input_manager_apply_input_config(input_manager, input); } -void apply_seat_config(struct seat_config *seat) { +void apply_seat_config(struct seat_config *seat_config) { int i; - i = list_seq_find(config->seat_configs, seat_name_cmp, seat->name); + i = list_seq_find(config->seat_configs, seat_name_cmp, seat_config->name); if (i >= 0) { // merge existing config struct seat_config *sc = config->seat_configs->items[i]; - merge_seat_config(sc, seat); - free_seat_config(seat); - seat = sc; + merge_seat_config(sc, seat_config); + free_seat_config(seat_config); + seat_config = sc; } else { - list_add(config->seat_configs, seat); + list_add(config->seat_configs, seat_config); } - input_manager_apply_seat_config(input_manager, seat); + input_manager_apply_seat_config(input_manager, seat_config); } /* Keep alphabetized */ diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 45079616..819b769c 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -14,7 +14,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { free_seat_config(config->handler_context.seat_config); config->handler_context.seat_config = new_seat_config(argv[0]); if (!config->handler_context.seat_config) { - return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + return cmd_results_new(CMD_FAILURE, NULL, + "Couldn't allocate config"); } wlr_log(L_DEBUG, "entering seat block: %s", argv[0]); return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); @@ -28,7 +29,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { if (!has_context) { config->handler_context.seat_config = new_seat_config(argv[0]); if (!config->handler_context.seat_config) { - return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); + return cmd_results_new(CMD_FAILURE, NULL, + "Couldn't allocate config"); } } @@ -41,7 +43,10 @@ struct cmd_results *cmd_seat(int argc, char **argv) { } else if (strcasecmp("fallback", argv[1]) == 0) { res = seat_cmd_fallback(argc_new, argv_new); } else { - res = cmd_results_new(CMD_INVALID, "seat ", "Unknown command %s", argv[1]); + res = + cmd_results_new(CMD_INVALID, + "seat ", "Unknown command %s", + argv[1]); } if (!has_context) { diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index d9d20200..34d5b4cf 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -92,7 +92,8 @@ static bool input_has_seat_configuration(struct sway_input_manager *input) { return false; } -static void input_manager_libinput_config_pointer(struct sway_input_device *input_device) { +static void input_manager_libinput_config_pointer( + struct sway_input_device *input_device) { struct wlr_input_device *wlr_device = input_device->wlr_device; struct input_config *ic = input_device->config; struct libinput_device *libinput_device; @@ -102,22 +103,27 @@ static void input_manager_libinput_config_pointer(struct sway_input_device *inpu } libinput_device = wlr_libinput_get_device_handle(wlr_device); - wlr_log(L_DEBUG, "input_manager_libinput_config_pointer(%s)", ic->identifier); + wlr_log(L_DEBUG, "input_manager_libinput_config_pointer(%s)", + ic->identifier); if (ic->accel_profile != INT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) accel_set_profile(%d)", ic->identifier, ic->accel_profile); - libinput_device_config_accel_set_profile(libinput_device, ic->accel_profile); + libinput_device_config_accel_set_profile(libinput_device, + ic->accel_profile); } if (ic->click_method != INT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) click_set_method(%d)", ic->identifier, ic->click_method); - libinput_device_config_click_set_method(libinput_device, ic->click_method); + libinput_device_config_click_set_method(libinput_device, + ic->click_method); } if (ic->drag_lock != INT_MIN) { - wlr_log(L_DEBUG, "libinput_config_pointer(%s) tap_set_drag_lock_enabled(%d)", + wlr_log(L_DEBUG, + "libinput_config_pointer(%s) tap_set_drag_lock_enabled(%d)", ic->identifier, ic->click_method); - libinput_device_config_tap_set_drag_lock_enabled(libinput_device, ic->drag_lock); + libinput_device_config_tap_set_drag_lock_enabled(libinput_device, + ic->drag_lock); } if (ic->dwt != INT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) dwt_set_enabled(%d)", @@ -125,34 +131,43 @@ static void input_manager_libinput_config_pointer(struct sway_input_device *inpu libinput_device_config_dwt_set_enabled(libinput_device, ic->dwt); } if (ic->left_handed != INT_MIN) { - wlr_log(L_DEBUG, "libinput_config_pointer(%s) left_handed_set_enabled(%d)", + wlr_log(L_DEBUG, + "libinput_config_pointer(%s) left_handed_set_enabled(%d)", ic->identifier, ic->left_handed); - libinput_device_config_left_handed_set(libinput_device, ic->left_handed); + libinput_device_config_left_handed_set(libinput_device, + ic->left_handed); } if (ic->middle_emulation != INT_MIN) { - wlr_log(L_DEBUG, "libinput_config_pointer(%s) middle_emulation_set_enabled(%d)", + wlr_log(L_DEBUG, + "libinput_config_pointer(%s) middle_emulation_set_enabled(%d)", ic->identifier, ic->middle_emulation); - libinput_device_config_middle_emulation_set_enabled(libinput_device, ic->middle_emulation); + libinput_device_config_middle_emulation_set_enabled(libinput_device, + ic->middle_emulation); } if (ic->natural_scroll != INT_MIN) { - wlr_log(L_DEBUG, "libinput_config_pointer(%s) natural_scroll_set_enabled(%d)", + wlr_log(L_DEBUG, + "libinput_config_pointer(%s) natural_scroll_set_enabled(%d)", ic->identifier, ic->natural_scroll); - libinput_device_config_scroll_set_natural_scroll_enabled(libinput_device, ic->natural_scroll); + libinput_device_config_scroll_set_natural_scroll_enabled( + libinput_device, ic->natural_scroll); } if (ic->pointer_accel != FLT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) accel_set_speed(%f)", ic->identifier, ic->pointer_accel); - libinput_device_config_accel_set_speed(libinput_device, ic->pointer_accel); + libinput_device_config_accel_set_speed(libinput_device, + ic->pointer_accel); } if (ic->scroll_method != INT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) scroll_set_method(%d)", ic->identifier, ic->scroll_method); - libinput_device_config_scroll_set_method(libinput_device, ic->scroll_method); + libinput_device_config_scroll_set_method(libinput_device, + ic->scroll_method); } if (ic->send_events != INT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) send_events_set_mode(%d)", ic->identifier, ic->send_events); - libinput_device_config_send_events_set_mode(libinput_device, ic->send_events); + libinput_device_config_send_events_set_mode(libinput_device, + ic->send_events); } if (ic->tap != INT_MIN) { wlr_log(L_DEBUG, "libinput_config_pointer(%s) tap_set_enabled(%d)", diff --git a/sway/input/seat.c b/sway/input/seat.c index 91a9e32b..38b617b7 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -49,7 +49,8 @@ static void handle_seat_container_destroy(struct wl_listener *listener, if (is_focus) { // pick next focus seat_set_focus(seat, NULL); - struct sway_container *next = seat_get_focus_inactive(seat, con->parent); + struct sway_container *next = + seat_get_focus_inactive(seat, con->parent); if (next == NULL) { next = con->parent; } @@ -133,7 +134,8 @@ struct sway_seat *seat_create(struct sway_input_manager *input, // init the focus stack wl_list_init(&seat->focus_stack); - container_for_each_descendant_dfs(&root_container, collect_focus_iter, seat); + container_for_each_descendant_dfs(&root_container, + collect_focus_iter, seat); wl_signal_add(&root_container.sway_root->events.new_container, &seat->new_container); @@ -165,7 +167,8 @@ static void seat_configure_keyboard(struct sway_seat *seat, if (!seat_device->keyboard) { sway_keyboard_create(seat, seat_device); } - struct wlr_keyboard *wlr_keyboard = seat_device->input_device->wlr_device->keyboard; + struct wlr_keyboard *wlr_keyboard = + seat_device->input_device->wlr_device->keyboard; sway_keyboard_configure(seat_device->keyboard); wlr_seat_set_keyboard(seat->wlr_seat, seat_device->input_device->wlr_device); @@ -275,7 +278,8 @@ void seat_configure_xcursor(struct sway_seat *seat) { } for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *output_container = root_container.children->items[i]; + struct sway_container *output_container = + root_container.children->items[i]; struct wlr_output *output = output_container->sway_output->wlr_output; bool result = @@ -379,7 +383,8 @@ void seat_set_focus(struct sway_seat *seat, seat_set_focus_warp(seat, container, true); } -struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { +struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, + struct sway_container *container) { struct sway_seat_container *current = NULL; struct sway_container *parent = NULL; wl_list_for_each(current, &seat->focus_stack, link) { @@ -409,7 +414,8 @@ struct sway_container *seat_get_focus(struct sway_seat *seat) { struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, enum sway_container_type type) { - struct sway_container *focus = seat_get_focus_inactive(seat, &root_container); + struct sway_container *focus = + seat_get_focus_inactive(seat, &root_container); if (focus->type == type) { return focus; } -- cgit v1.2.3 From e3689dd5a93b4f4ae62637a2b81797335aafdcaf Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 2 Apr 2018 11:57:06 -0400 Subject: Fixes regarding @emersion's feedback --- sway/commands/move.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'sway/commands') diff --git a/sway/commands/move.c b/sway/commands/move.c index 222ef314..ab959b77 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -106,7 +106,9 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, // We've never been to this output before focus = destination->children->items[0]; } + struct sway_container *old_parent = current->parent; container_move_to(current, focus); + sway_seat_set_focus(config->handler_context.seat, old_parent); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } return cmd_results_new(CMD_INVALID, "move", expected_syntax); @@ -149,7 +151,8 @@ struct cmd_results *cmd_move(int argc, char **argv) { char *inv; move_amt = (int)strtol(argv[1], &inv, 10); if (*inv != '\0' && strcasecmp(inv, "px") != 0) { - move_amt = 10; + return cmd_results_new(CMD_FAILURE, "move", + "Invalid distance specified"); } } -- cgit v1.2.3 From 357a4401fa117416f3182a5f91d27b52b114f3ee Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 14:15:40 -0400 Subject: address feedback --- sway/commands/split.c | 1 + sway/input/seat.c | 1 - sway/tree/container.c | 2 +- sway/tree/layout.c | 5 ++--- 4 files changed, 4 insertions(+), 5 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/split.c b/sway/commands/split.c index 6df20e88..fe4013e9 100644 --- a/sway/commands/split.c +++ b/sway/commands/split.c @@ -76,6 +76,7 @@ struct cmd_results *cmd_split(int argc, char **argv) { strcasecmp(argv[0], "toggle") == 0) { struct sway_container *focused = config->handler_context.current_container; + if (focused->parent->layout == L_VERT) { _do_split(argc - 1, argv + 1, L_HORIZ); } else { diff --git a/sway/input/seat.c b/sway/input/seat.c index a1b1caa8..1fd204d4 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -92,7 +92,6 @@ static void handle_seat_container_destroy(struct wl_listener *listener, struct sway_container *parent = con->parent; struct sway_container *focus = sway_seat_get_focus(seat); - // TODO handle workspace switch in the seat? bool set_focus = focus != NULL && (focus == con || container_has_child(con, focus)) && diff --git a/sway/tree/container.c b/sway/tree/container.c index 41ba973f..9205d929 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -424,7 +424,7 @@ bool container_has_anscestor(struct sway_container *descendant, return false; } -bool find_child_func(struct sway_container *con, void *data) { +static bool find_child_func(struct sway_container *con, void *data) { struct sway_container *child = data; return con == child; } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 83e4fe37..e13c2ac4 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -101,8 +101,6 @@ void container_add_child(struct sway_container *parent, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); child->parent = parent; - // TODO: set focus for this container? - sway_input_manager_set_focus(input_manager, child); } struct sway_container *container_reap_empty(struct sway_container *container) { @@ -632,7 +630,8 @@ struct sway_container *container_get_in_direction( wrap_candidate = parent->children->items[0]; } if (config->force_focus_wrapping) { - return sway_seat_get_focus_by_type(seat, wrap_candidate, C_VIEW); + return sway_seat_get_focus_by_type(seat, + wrap_candidate, C_VIEW); } } } else { -- cgit v1.2.3 From d070244362f7d34bc15418154e52f8c968fbad41 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 15:40:40 -0400 Subject: fix workspace splits --- sway/commands/split.c | 25 ++----------------------- sway/tree/layout.c | 38 +++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 40 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/split.c b/sway/commands/split.c index fe4013e9..ca116aef 100644 --- a/sway/commands/split.c +++ b/sway/commands/split.c @@ -25,29 +25,8 @@ static struct cmd_results *_do_split(int argc, char **argv, int layout) { } struct sway_container *focused = config->handler_context.current_container; - - // TODO floating: dont split - - /* Case that focus is on an workspace with 0/1 children.change its layout */ - if (focused->type == C_WORKSPACE && focused->children->length <= 1) { - wlr_log(L_DEBUG, "changing workspace layout"); - container_set_layout(focused, layout); - } else if (focused->type != C_WORKSPACE && - focused->parent->children->length == 1) { - /* Case of no siblings. change parent layout */ - wlr_log(L_DEBUG, "changing container layout"); - container_set_layout(focused->parent, layout); - } else { - // regular case where new split container is build around focused - // container or in case of workspace, container inherits its children - wlr_log(L_DEBUG, - "Adding new container around current focused container"); - wlr_log(L_INFO, "FOCUSED SIZE: %.f %.f", - focused->width, focused->height); - - struct sway_container *parent = container_split(focused, layout); - arrange_windows(parent, -1, -1); - } + struct sway_container *parent = container_split(focused, layout); + arrange_windows(parent, -1, -1); // TODO borders: update borders diff --git a/sway/tree/layout.c b/sway/tree/layout.c index e13c2ac4..bea15f8a 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -405,7 +405,12 @@ void apply_vert_layout(struct sway_container *container, wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); - view_set_position(child->sway_view, x, child_y); + if (child->type == C_VIEW) { + view_set_position(child->sway_view, x, child_y); + } else { + child->x = x; + child->y = child_y; + } if (i == end - 1) { double remaining_height = y + height - child_y; @@ -691,34 +696,33 @@ struct sway_container *container_split(struct sway_container *child, wlr_log(L_DEBUG, "creating container %p around %p", cont, child); cont->prev_layout = L_NONE; - cont->layout = layout; cont->width = child->width; cont->height = child->height; cont->x = child->x; cont->y = child->y; - /* Container inherits all of workspaces children, layout and whatnot */ if (child->type == C_WORKSPACE) { + struct sway_seat *seat = sway_input_manager_get_default_seat(input_manager); struct sway_container *workspace = child; - // reorder focus - int i; - for (i = 0; i < workspace->children->length; ++i) { - ((struct sway_container *)workspace->children->items[i])->parent = - cont; + bool set_focus = (sway_seat_get_focus(seat) == workspace); + + while (workspace->children->length) { + struct sway_container *ws_child = workspace->children->items[0]; + container_remove_child(ws_child); + container_add_child(cont, ws_child); } - // Swap children - list_t *tmp_list = workspace->children; - workspace->children = cont->children; - cont->children = tmp_list; - // add container to workspace chidren container_add_child(workspace, cont); - // give them proper layouts - cont->layout = workspace->workspace_layout; - cont->prev_layout = workspace->prev_layout; - } else { // Or is built around container + container_set_layout(workspace, layout); + + if (set_focus) { + sway_seat_set_focus(seat, cont); + } + } else { + cont->layout = layout; container_replace_child(child, cont); container_add_child(cont, child); } + return cont; } -- cgit v1.2.3 From 32ef182f474dbb40c4bedb69256ca6ec8bd31039 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 20:12:18 -0400 Subject: cleanup split command handlers --- sway/commands/split.c | 65 +++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 38 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/split.c b/sway/commands/split.c index ca116aef..ab8565a9 100644 --- a/sway/commands/split.c +++ b/sway/commands/split.c @@ -8,58 +8,33 @@ #include "sway/input/seat.h" #include "log.h" -static struct cmd_results *_do_split(int argc, char **argv, int layout) { - char *name = layout == L_VERT ? "splitv" : - layout == L_HORIZ ? "splith" : "split"; - struct cmd_results *error = NULL; - if (config->reading) { - return cmd_results_new(CMD_FAILURE, name, - "Can't be used in config file."); - } - if (!config->active) { - return cmd_results_new(CMD_FAILURE, name, - "Can only be used when sway is running."); - } - if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) { - return error; - } - - struct sway_container *focused = config->handler_context.current_container; - struct sway_container *parent = container_split(focused, layout); +static struct cmd_results *do_split(int layout) { + struct sway_container *con = config->handler_context.current_container; + struct sway_container *parent = container_split(con, layout); arrange_windows(parent, -1, -1); - // TODO borders: update borders - return cmd_results_new(CMD_SUCCESS, NULL, NULL); } struct cmd_results *cmd_split(int argc, char **argv) { struct cmd_results *error = NULL; - if (config->reading) { - return cmd_results_new(CMD_FAILURE, "split", - "Can't be used in config file."); - } - if (!config->active) { - return cmd_results_new(CMD_FAILURE, "split", - "Can only be used when sway is running."); - } if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) { return error; } if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) { - _do_split(argc - 1, argv + 1, L_VERT); + do_split(L_VERT); } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) { - _do_split(argc - 1, argv + 1, L_HORIZ); + do_split(L_HORIZ); } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) { struct sway_container *focused = config->handler_context.current_container; if (focused->parent->layout == L_VERT) { - _do_split(argc - 1, argv + 1, L_HORIZ); + do_split(L_HORIZ); } else { - _do_split(argc - 1, argv + 1, L_VERT); + do_split(L_VERT); } } else { error = cmd_results_new(CMD_FAILURE, "split", @@ -70,18 +45,32 @@ struct cmd_results *cmd_split(int argc, char **argv) { } struct cmd_results *cmd_splitv(int argc, char **argv) { - return _do_split(argc, argv, L_VERT); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "splitv", EXPECTED_EQUAL_TO, 0))) { + return error; + } + return do_split(L_VERT); } struct cmd_results *cmd_splith(int argc, char **argv) { - return _do_split(argc, argv, L_HORIZ); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "splitv", EXPECTED_EQUAL_TO, 0))) { + return error; + } + return do_split(L_HORIZ); } struct cmd_results *cmd_splitt(int argc, char **argv) { - struct sway_container *focused = config->handler_context.current_container; - if (focused->parent->layout == L_VERT) { - return _do_split(argc, argv, L_HORIZ); + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "splitv", EXPECTED_EQUAL_TO, 0))) { + return error; + } + + struct sway_container *con = config->handler_context.current_container; + + if (con->parent->layout == L_VERT) { + return do_split(L_HORIZ); } else { - return _do_split(argc, argv, L_VERT); + return do_split(L_VERT); } } -- cgit v1.2.3 From 2c165e1288cbb60f5e677595e35f58a9c56c7010 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 21:01:33 -0400 Subject: fix more close segfaults --- include/sway/tree/container.h | 2 ++ sway/commands/kill.c | 5 +---- sway/tree/container.c | 37 ++++++++++++++++++++++++++++++++++++- sway/tree/layout.c | 2 +- sway/tree/view.c | 2 +- 5 files changed, 41 insertions(+), 7 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 464f80c4..5d15f12b 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -134,6 +134,8 @@ struct sway_container *container_workspace_destroy(struct sway_container *contai struct sway_container *container_output_destroy(struct sway_container *container); struct sway_container *container_view_destroy(struct sway_container *container); +struct sway_container *container_close(struct sway_container *container); + // TODO move to layout.c struct sway_container *container_set_layout(struct sway_container *container, enum sway_container_layout layout); diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 46d6e98e..811c3e6b 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -19,11 +19,8 @@ struct cmd_results *cmd_kill(int argc, char **argv) { "Can only kill views and containers with this command"); break; case C_CONTAINER: - con = container_destroy(con); - arrange_windows(con, -1, -1); - break; case C_VIEW: - view_close(con->sway_view); + container_close(con); break; } diff --git a/sway/tree/container.c b/sway/tree/container.c index 4db93ce8..8688edd6 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -112,10 +112,45 @@ static struct sway_container *_container_destroy(struct sway_container *cont) { struct sway_container *container_destroy(struct sway_container *cont) { struct sway_container *parent = _container_destroy(cont); parent = container_reap_empty(parent); - arrange_windows(&root_container, -1, -1); return parent; } +static void container_close_func(struct sway_container *container, void *data) { + if (container->type == C_VIEW) { + view_close(container->sway_view); + } +} + +struct sway_container *container_close(struct sway_container *con) { + if (!sway_assert(con != NULL, "container_close called with a NULL container")) { + return NULL; + } + + switch (con->type) { + case C_TYPES: + wlr_log(L_ERROR, "tried to close an invalid container"); + break; + case C_ROOT: + wlr_log(L_ERROR, "tried to close the root container"); + break; + case C_OUTPUT: + container_output_destroy(con); + break; + case C_WORKSPACE: + container_workspace_destroy(con); + break; + case C_CONTAINER: + container_for_each_descendant_dfs(con, container_close_func, NULL); + break; + case C_VIEW: + view_close(con->sway_view); + break; + + } + + return con->parent; +} + struct sway_container *container_output_create( struct sway_output *sway_output) { struct wlr_box size; diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 95a84d12..b0ce4aaf 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -110,7 +110,7 @@ struct sway_container *container_reap_empty(struct sway_container *container) { wlr_log(L_DEBUG, "Reaping %p %s '%s'", container, container_type_to_str(container->type), container->name); while (container->type != C_ROOT && container->type != C_OUTPUT - && container->children->length == 0) { + && container->children && container->children->length == 0) { if (container->type == C_WORKSPACE) { if (!workspace_is_visible(container)) { struct sway_container *parent = container->parent; diff --git a/sway/tree/view.c b/sway/tree/view.c index 09c804e4..4e695b5f 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -84,7 +84,7 @@ struct sway_container *container_view_destroy(struct sway_container *view) { } wlr_log(L_DEBUG, "Destroying view '%s'", view->name); struct sway_container *parent = container_destroy(view); - arrange_windows(parent, -1, -1); + arrange_windows(&root_container, -1, -1); return parent; } -- cgit v1.2.3 From 5f4761c4f40f5d6ec550ccabaebe0f990b6e8bbc Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 13:08:45 -0400 Subject: unify workspace create functions --- include/sway/tree/workspace.h | 2 -- sway/commands/move.c | 2 +- sway/commands/workspace.c | 6 +++--- sway/tree/container.c | 47 +++++++++++++++++++++++++++++++++++-------- sway/tree/workspace.c | 33 +++--------------------------- 5 files changed, 46 insertions(+), 44 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h index 4e4c3450..8d49fefb 100644 --- a/include/sway/tree/workspace.h +++ b/include/sway/tree/workspace.h @@ -7,8 +7,6 @@ extern char *prev_workspace_name; char *workspace_next_name(const char *output_name); -struct sway_container *workspace_create(const char *name); - bool workspace_switch(struct sway_container *workspace); struct sway_container *workspace_by_number(const char* name); diff --git a/sway/commands/move.c b/sway/commands/move.c index 644c622b..7ac5f009 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -74,7 +74,7 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, ws = workspace_by_name(ws_name); } if (!ws) { - ws = workspace_create(ws_name ? ws_name : num_name); + ws = container_workspace_create(NULL, ws_name ? ws_name : num_name); } free(ws_name); struct sway_container *old_parent = current->parent; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index aa4096f7..a3702803 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -61,7 +61,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); - ws = workspace_create(name); + ws = container_workspace_create(NULL, name); free(name); } } else if (strcasecmp(argv[0], "next") == 0) { @@ -80,12 +80,12 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { ws = old_workspace; } else if (prev_workspace_name && !(ws = workspace_by_name(prev_workspace_name))) { - ws = workspace_create(prev_workspace_name); + ws = container_workspace_create(NULL, prev_workspace_name); } } else { char *name = join_args(argv, argc); if (!(ws = workspace_by_name(name))) { - ws = workspace_create(name); + ws = container_workspace_create(NULL, name); } free(name); } diff --git a/sway/tree/container.c b/sway/tree/container.c index 1c41bf5d..7ccd43ea 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -110,7 +110,8 @@ struct sway_container *container_finish(struct sway_container *cont) { return parent; } -static struct sway_container *container_output_destroy(struct sway_container *output) { +static struct sway_container *container_output_destroy( + struct sway_container *output) { if (!sway_assert(output, "cannot destroy null output")) { return NULL; } @@ -245,7 +246,8 @@ struct sway_container *container_destroy(struct sway_container *con) { break; case C_CONTAINER: if (con->children != NULL && con->children->length) { - assert(false && "dont destroy container containers with children"); + assert(false && + "dont destroy container containers with children"); } container_finish(con); // TODO return parent to arrange maybe? @@ -271,7 +273,8 @@ static void container_close_func(struct sway_container *container, void *data) { } struct sway_container *container_close(struct sway_container *con) { - if (!sway_assert(con != NULL, "container_close called with a NULL container")) { + if (!sway_assert(con != NULL, + "container_close called with a NULL container")) { return NULL; } @@ -359,12 +362,39 @@ struct sway_container *container_output_create( return output; } -struct sway_container *container_workspace_create( - struct sway_container *output, const char *name) { - if (!sway_assert(output, - "container_workspace_create called with null output")) { - return NULL; +static struct sway_container *workspace_get_initial_output(const char *name) { + struct sway_container *parent; + // Search for workspace<->output pair + int i, e = config->workspace_outputs->length; + for (i = 0; i < e; ++i) { + struct workspace_output *wso = config->workspace_outputs->items[i]; + if (strcasecmp(wso->workspace, name) == 0) { + // Find output to use if it exists + e = root_container.children->length; + for (i = 0; i < e; ++i) { + parent = root_container.children->items[i]; + if (strcmp(parent->name, wso->output) == 0) { + return parent; + } + } + break; + } } + // Otherwise put it on the focused output + struct sway_seat *seat = input_manager_current_seat(input_manager); + struct sway_container *focus = + seat_get_focus_inactive(seat, &root_container); + parent = focus; + parent = container_parent(parent, C_OUTPUT); + return parent; +} + +struct sway_container *container_workspace_create(struct sway_container *output, + const char *name) { + if (output == NULL) { + output = workspace_get_initial_output(name); + } + wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); struct sway_container *workspace = container_create(C_WORKSPACE); @@ -380,6 +410,7 @@ struct sway_container *container_workspace_create( container_add_child(output, workspace); container_sort_workspaces(output); notify_new_container(workspace); + return workspace; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 7d180009..d5a16410 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -179,35 +179,6 @@ struct sway_container *workspace_by_name(const char *name) { } } -struct sway_container *workspace_create(const char *name) { - struct sway_container *parent; - // Search for workspace<->output pair - int i, e = config->workspace_outputs->length; - for (i = 0; i < e; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; - if (strcasecmp(wso->workspace, name) == 0) { - // Find output to use if it exists - e = root_container.children->length; - for (i = 0; i < e; ++i) { - parent = root_container.children->items[i]; - if (strcmp(parent->name, wso->output) == 0) { - return container_workspace_create(parent, name); - } - } - break; - } - } - // Otherwise create a new one - struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = - seat_get_focus_inactive(seat, &root_container); - parent = focus; - parent = container_parent(parent, C_OUTPUT); - struct sway_container *new_ws = container_workspace_create(parent, name); - ipc_event_workspace(NULL, new_ws, "init"); - return new_ws; -} - /** * Get the previous or next workspace on the specified output. Wraps around at * the end and beginning. If next is false, the previous workspace is returned, @@ -319,7 +290,9 @@ bool workspace_switch(struct sway_container *workspace) { && active_ws == workspace && prev_workspace_name) { struct sway_container *new_ws = workspace_by_name(prev_workspace_name); - workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); + workspace = new_ws ? + new_ws : + container_workspace_create(NULL, prev_workspace_name); } if (!prev_workspace_name || (strcmp(prev_workspace_name, active_ws->name) -- cgit v1.2.3 From 9b567fc37e83ed9f0cbb028fd801a2f2609c79dc Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 13:23:34 -0400 Subject: clean up container_get_default_layout --- include/sway/tree/layout.h | 3 +-- sway/commands/layout.c | 3 +-- sway/tree/container.c | 2 +- sway/tree/layout.c | 14 ++++++++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 725422c6..12689375 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -55,9 +55,8 @@ void container_move_to(struct sway_container* container, void container_move(struct sway_container *container, enum movement_direction dir, int move_amt); -// TODO move to output.c enum sway_container_layout container_get_default_layout( - struct sway_container *output); + struct sway_container *con); // TODO move to output.c void container_sort_workspaces(struct sway_container *output); diff --git a/sway/commands/layout.c b/sway/commands/layout.c index ebab2a48..4c49a627 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -28,8 +28,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if (strcasecmp(argv[0], "default") == 0) { container_set_layout(parent, parent->prev_layout); if (parent->layout == L_NONE) { - struct sway_container *output = container_parent(parent, C_OUTPUT); - container_set_layout(parent, container_get_default_layout(output)); + container_set_layout(parent, container_get_default_layout(parent)); } } else { if (parent->layout != L_TABBED && parent->layout != L_STACKED) { diff --git a/sway/tree/container.c b/sway/tree/container.c index a79bc22c..64b51db6 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -405,7 +405,7 @@ struct sway_container *container_workspace_create(struct sway_container *output, workspace->name = !name ? NULL : strdup(name); workspace->prev_layout = L_NONE; workspace->layout = container_get_default_layout(output); - workspace->workspace_layout = container_get_default_layout(output); + workspace->workspace_layout = workspace->layout; container_add_child(output, workspace); container_sort_workspaces(output); diff --git a/sway/tree/layout.c b/sway/tree/layout.c index dfcdbb9c..c3cdaae0 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -168,12 +168,22 @@ void container_move(struct sway_container *container, } enum sway_container_layout container_get_default_layout( - struct sway_container *output) { + struct sway_container *con) { + if (con->type != C_OUTPUT) { + con = container_parent(con, C_OUTPUT); + } + + if (!sway_assert(con != NULL, + "container_get_default_layout must be called on an attached " + " container below the root container")) { + return 0; + } + if (config->default_layout != L_NONE) { return config->default_layout; } else if (config->default_orientation != L_NONE) { return config->default_orientation; - } else if (output->width >= output->height) { + } else if (con->width >= con->height) { return L_HORIZ; } else { return L_VERT; -- cgit v1.2.3 From 481a8275c178f81bb2d9927c5047e959fdbc383a Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 19:23:59 -0400 Subject: address feedback --- sway/commands/kill.c | 14 +----- sway/desktop/output.c | 2 +- sway/tree/container.c | 127 ++++++++++++++++++++++++++++---------------------- sway/tree/layout.c | 2 +- sway/tree/view.c | 4 +- 5 files changed, 75 insertions(+), 74 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 811c3e6b..f3fa52f1 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -10,19 +10,7 @@ struct cmd_results *cmd_kill(int argc, char **argv) { struct sway_container *con = config->handler_context.current_container; - switch (con->type) { - case C_ROOT: - case C_OUTPUT: - case C_WORKSPACE: - case C_TYPES: - return cmd_results_new(CMD_INVALID, NULL, - "Can only kill views and containers with this command"); - break; - case C_CONTAINER: - case C_VIEW: - container_close(con); - break; - } + container_close(con); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/desktop/output.c b/sway/desktop/output.c index e60fac5f..de5076d8 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -25,7 +25,7 @@ struct sway_container *output_by_name(const char *name) { for (int i = 0; i < root_container.children->length; ++i) { struct sway_container *output = root_container.children->items[i]; - if (strcasecmp(output->name, name) == 0){ + if (strcasecmp(output->name, name) == 0) { return output; } } diff --git a/sway/tree/container.c b/sway/tree/container.c index f616af09..77c61b3f 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -186,45 +186,44 @@ static struct sway_container *container_workspace_destroy( return parent; } +static void container_root_finish(struct sway_container *con) { + wlr_log(L_ERROR, "TODO: destroy the root container"); +} -static void reap_empty_func(struct sway_container *con, void *data) { +static bool container_reap_empty(struct sway_container *con) { switch (con->type) { - case C_TYPES: - case C_ROOT: - case C_OUTPUT: - // dont reap these - break; - case C_WORKSPACE: - if (!workspace_is_visible(con) && con->children->length == 0) { - container_workspace_destroy(con); - } - break; - case C_CONTAINER: - if (con->children->length == 0) { + case C_ROOT: + case C_OUTPUT: + // dont reap these + break; + case C_WORKSPACE: + if (!workspace_is_visible(con) && con->children->length == 0) { + container_workspace_destroy(con); + return true; + } + break; + case C_CONTAINER: + if (con->children->length == 0) { + container_finish(con); + return true; + } else if (con->children->length == 1) { + struct sway_container *child = con->children->items[0]; + if (child->type == C_CONTAINER) { + container_remove_child(child); + container_replace_child(con, child); container_finish(con); - } else if (con->children->length == 1) { - struct sway_container *only_child = con->children->items[0]; - if (only_child->type == C_CONTAINER) { - container_remove_child(only_child); - container_replace_child(con, only_child); - container_finish(con); - } + return true; } - case C_VIEW: - break; + } + case C_VIEW: + break; + case C_TYPES: + sway_assert(false, "container_reap_empty called on an invalid " + "container"); + break; } -} - -struct sway_container *container_reap_empty(struct sway_container *container) { - struct sway_container *parent = container->parent; - - container_for_each_descendant_dfs(container, reap_empty_func, NULL); - return parent; -} - -static void container_root_finish(struct sway_container *con) { - wlr_log(L_ERROR, "TODO: destroy the root container"); + return false; } struct sway_container *container_destroy(struct sway_container *con) { @@ -232,38 +231,52 @@ struct sway_container *container_destroy(struct sway_container *con) { return NULL; } - struct sway_container *anscestor = NULL; + struct sway_container *parent = con->parent; switch (con->type) { case C_ROOT: container_root_finish(con); break; case C_OUTPUT: - anscestor = container_output_destroy(con); + // dont try to reap the root after this + container_output_destroy(con); break; case C_WORKSPACE: - anscestor = container_workspace_destroy(con); + // dont try to reap the output after this + container_workspace_destroy(con); break; case C_CONTAINER: - if (con->children != NULL && con->children->length) { - assert(false && - "dont destroy container containers with children"); + if (con->children->length) { + for (int i = 0; i < con->children->length; ++i) { + struct sway_container *child = con->children->items[0]; + container_remove_child(child); + container_add_child(parent, child); + } + container_finish(con); } container_finish(con); - // TODO return parent to arrange maybe? break; case C_VIEW: container_finish(con); - // TODO return parent to arrange maybe? break; case C_TYPES: - wlr_log(L_ERROR, "tried to destroy an invalid container"); + wlr_log(L_ERROR, "container_destroy called on an invalid " + "container"); break; } - container_reap_empty(&root_container); + struct sway_container *tmp = parent; + while (parent) { + tmp = parent->parent; + + if (!container_reap_empty(parent)) { + break; + } + + parent = tmp; + } - return anscestor; + return tmp; } static void container_close_func(struct sway_container *container, void *data) { @@ -274,23 +287,23 @@ static void container_close_func(struct sway_container *container, void *data) { struct sway_container *container_close(struct sway_container *con) { if (!sway_assert(con != NULL, - "container_close called with a NULL container")) { + "container_close called with a NULL container")) { return NULL; } struct sway_container *parent = con->parent; switch (con->type) { - case C_TYPES: - case C_ROOT: - case C_OUTPUT: - case C_WORKSPACE: - case C_CONTAINER: - container_for_each_descendant_dfs(con, container_close_func, NULL); - break; - case C_VIEW: - view_close(con->sway_view); - break; + case C_TYPES: + case C_ROOT: + case C_OUTPUT: + case C_WORKSPACE: + case C_CONTAINER: + container_for_each_descendant_dfs(con, container_close_func, NULL); + break; + case C_VIEW: + view_close(con->sway_view); + break; } @@ -365,8 +378,8 @@ struct sway_container *container_output_create( static struct sway_container *get_workspace_initial_output(const char *name) { struct sway_container *parent; // Search for workspace<->output pair - int i, e = config->workspace_outputs->length; - for (i = 0; i < e; ++i) { + int e = config->workspace_outputs->length; + for (int i = 0; i < config->workspace_outputs->length; ++i) { struct workspace_output *wso = config->workspace_outputs->items[i]; if (strcasecmp(wso->workspace, name) == 0) { // Find output to use if it exists diff --git a/sway/tree/layout.c b/sway/tree/layout.c index c3cdaae0..3cbbf3b4 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -174,7 +174,7 @@ enum sway_container_layout container_get_default_layout( } if (!sway_assert(con != NULL, - "container_get_default_layout must be called on an attached " + "container_get_default_layout must be called on an attached" " container below the root container")) { return 0; } diff --git a/sway/tree/view.c b/sway/tree/view.c index c06924f5..aa010a40 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -149,12 +149,12 @@ void view_unmap(struct sway_view *view) { view_damage_whole(view); - container_destroy(view->swayc); + struct sway_container *parent = container_destroy(view->swayc); view->swayc = NULL; view->surface = NULL; - arrange_windows(&root_container, -1, -1); + arrange_windows(parent, -1, -1); } void view_update_position(struct sway_view *view, double ox, double oy) { -- cgit v1.2.3 From a001890fb8a9fc8c7f0b8eac03ca5912be2de479 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 19:52:17 -0400 Subject: move workspace create to workspace.c --- include/sway/tree/container.h | 8 +++--- sway/commands/move.c | 2 +- sway/commands/workspace.c | 6 ++--- sway/desktop/output.c | 2 +- sway/tree/container.c | 62 ++++--------------------------------------- sway/tree/layout.c | 2 +- sway/tree/workspace.c | 54 ++++++++++++++++++++++++++++++++++++- 7 files changed, 69 insertions(+), 67 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index f36b4bd1..277165ea 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -95,7 +95,7 @@ struct sway_container *container_create(enum sway_container_type type); const char *container_type_to_str(enum sway_container_type type); -struct sway_container *container_output_create( +struct sway_container *output_create( struct sway_output *sway_output); /** @@ -108,13 +108,13 @@ struct sway_container *container_container_create(); * Create a new output. Outputs are children of the root container and have no * order in the tree structure. */ -struct sway_container *container_output_create(struct sway_output *sway_output); +struct sway_container *output_create(struct sway_output *sway_output); /** * Create a new workspace container. Workspaces are children of an output * container and are ordered alphabetically by name. */ -struct sway_container *container_workspace_create(struct sway_container *output, const char *name); +struct sway_container *workspace_create(struct sway_container *output, const char *name); /* * Create a new view container. A view can be a child of a workspace container @@ -178,4 +178,6 @@ bool container_has_anscestor(struct sway_container *container, bool container_has_child(struct sway_container *con, struct sway_container *child); +void container_create_notify(struct sway_container *container); + #endif diff --git a/sway/commands/move.c b/sway/commands/move.c index 7ac5f009..c954ab94 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -74,7 +74,7 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, ws = workspace_by_name(ws_name); } if (!ws) { - ws = container_workspace_create(NULL, ws_name ? ws_name : num_name); + ws = workspace_create(NULL, ws_name ? ws_name : num_name); } free(ws_name); struct sway_container *old_parent = current->parent; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index a3702803..958b3222 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -61,7 +61,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); - ws = container_workspace_create(NULL, name); + ws = workspace_create(NULL, name); free(name); } } else if (strcasecmp(argv[0], "next") == 0) { @@ -80,12 +80,12 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { ws = old_workspace; } else if (prev_workspace_name && !(ws = workspace_by_name(prev_workspace_name))) { - ws = container_workspace_create(NULL, prev_workspace_name); + ws = workspace_create(NULL, prev_workspace_name); } } else { char *name = join_args(argv, argc); if (!(ws = workspace_by_name(name))) { - ws = container_workspace_create(NULL, name); + ws = workspace_create(NULL, name); } free(name); } diff --git a/sway/desktop/output.c b/sway/desktop/output.c index de5076d8..96f23291 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -396,7 +396,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { output->damage = wlr_output_damage_create(wlr_output); - output->swayc = container_output_create(output); + output->swayc = output_create(output); if (!output->swayc) { free(output); return; diff --git a/sway/tree/container.c b/sway/tree/container.c index fdbb2922..8ed30b44 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -51,7 +51,7 @@ const char *container_type_to_str(enum sway_container_type type) { } } -static void notify_new_container(struct sway_container *container) { +void container_create_notify(struct sway_container *container) { wl_signal_emit(&root_container.sway_root->events.new_container, container); ipc_event_window(container, "new"); } @@ -312,7 +312,7 @@ struct sway_container *container_close(struct sway_container *con) { return parent; } -struct sway_container *container_output_create( +struct sway_container *output_create( struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, @@ -363,7 +363,7 @@ struct sway_container *container_output_create( // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - struct sway_container *ws = container_workspace_create(output, ws_name); + struct sway_container *ws = workspace_create(output, ws_name); // Set each seat's focus if not already set struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { @@ -373,62 +373,10 @@ struct sway_container *container_output_create( } free(ws_name); - notify_new_container(output); + container_create_notify(output); return output; } -static struct sway_container *get_workspace_initial_output(const char *name) { - struct sway_container *parent; - // Search for workspace<->output pair - int e = config->workspace_outputs->length; - for (int i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; - if (strcasecmp(wso->workspace, name) == 0) { - // Find output to use if it exists - e = root_container.children->length; - for (i = 0; i < e; ++i) { - parent = root_container.children->items[i]; - if (strcmp(parent->name, wso->output) == 0) { - return parent; - } - } - break; - } - } - // Otherwise put it on the focused output - struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = - seat_get_focus_inactive(seat, &root_container); - parent = focus; - parent = container_parent(parent, C_OUTPUT); - return parent; -} - -struct sway_container *container_workspace_create(struct sway_container *output, - const char *name) { - if (output == NULL) { - output = get_workspace_initial_output(name); - } - - wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); - struct sway_container *workspace = container_create(C_WORKSPACE); - - workspace->x = output->x; - workspace->y = output->y; - workspace->width = output->width; - workspace->height = output->height; - workspace->name = !name ? NULL : strdup(name); - workspace->prev_layout = L_NONE; - workspace->layout = container_get_default_layout(output); - workspace->workspace_layout = workspace->layout; - - container_add_child(output, workspace); - container_sort_workspaces(output); - notify_new_container(workspace); - - return workspace; -} - struct sway_container *container_view_create(struct sway_container *sibling, struct sway_view *sway_view) { if (!sway_assert(sibling, @@ -452,7 +400,7 @@ struct sway_container *container_view_create(struct sway_container *sibling, // Regular case, create as sibling of current container container_add_sibling(sibling, swayc); } - notify_new_container(swayc); + container_create_notify(swayc); return swayc; } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 3cbbf3b4..a46359bd 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -149,7 +149,7 @@ void container_move_to(struct sway_container *container, if (old_parent->children->length == 0) { char *ws_name = workspace_next_name(old_parent->name); struct sway_container *ws = - container_workspace_create(old_parent, ws_name); + workspace_create(old_parent, ws_name); free(ws_name); seat_set_focus(seat, ws); } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index d5a16410..6ba3d973 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -14,6 +14,58 @@ #include "log.h" #include "util.h" +static struct sway_container *get_workspace_initial_output(const char *name) { + struct sway_container *parent; + // Search for workspace<->output pair + int e = config->workspace_outputs->length; + for (int i = 0; i < config->workspace_outputs->length; ++i) { + struct workspace_output *wso = config->workspace_outputs->items[i]; + if (strcasecmp(wso->workspace, name) == 0) { + // Find output to use if it exists + e = root_container.children->length; + for (i = 0; i < e; ++i) { + parent = root_container.children->items[i]; + if (strcmp(parent->name, wso->output) == 0) { + return parent; + } + } + break; + } + } + // Otherwise put it on the focused output + struct sway_seat *seat = input_manager_current_seat(input_manager); + struct sway_container *focus = + seat_get_focus_inactive(seat, &root_container); + parent = focus; + parent = container_parent(parent, C_OUTPUT); + return parent; +} + +struct sway_container *workspace_create(struct sway_container *output, + const char *name) { + if (output == NULL) { + output = get_workspace_initial_output(name); + } + + wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); + struct sway_container *workspace = container_create(C_WORKSPACE); + + workspace->x = output->x; + workspace->y = output->y; + workspace->width = output->width; + workspace->height = output->height; + workspace->name = !name ? NULL : strdup(name); + workspace->prev_layout = L_NONE; + workspace->layout = container_get_default_layout(output); + workspace->workspace_layout = workspace->layout; + + container_add_child(output, workspace); + container_sort_workspaces(output); + container_create_notify(workspace); + + return workspace; +} + char *prev_workspace_name = NULL; struct workspace_by_number_data { int len; @@ -292,7 +344,7 @@ bool workspace_switch(struct sway_container *workspace) { struct sway_container *new_ws = workspace_by_name(prev_workspace_name); workspace = new_ws ? new_ws : - container_workspace_create(NULL, prev_workspace_name); + workspace_create(NULL, prev_workspace_name); } if (!prev_workspace_name || (strcmp(prev_workspace_name, active_ws->name) -- cgit v1.2.3 From fc9398a42e1dfc15bbb8490c049981034abb4926 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 00:47:45 -0400 Subject: Implement opacity command --- include/sway/commands.h | 1 + include/sway/tree/container.h | 2 ++ sway/commands.c | 1 + sway/commands/opacity.c | 39 +++++++++++++++++++++++++++++++++++++++ sway/desktop/output.c | 34 ++++++++++++++++++---------------- sway/meson.build | 1 + sway/sway.5.txt | 4 ++++ sway/tree/container.c | 2 ++ 8 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 sway/commands/opacity.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index 66f097ea..edb5a213 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -123,6 +123,7 @@ sway_cmd cmd_mark; sway_cmd cmd_mode; sway_cmd cmd_mouse_warping; sway_cmd cmd_move; +sway_cmd cmd_opacity; sway_cmd cmd_new_float; sway_cmd cmd_new_window; sway_cmd cmd_no_focus; diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 277165ea..3a3a9429 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -83,6 +83,8 @@ struct sway_container { list_t *marks; // list of char* + float alpha; + struct { struct wl_signal destroy; // Raised after the tree updates, but before arrange_windows diff --git a/sway/commands.c b/sway/commands.c index 8156a08e..2786a879 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -163,6 +163,7 @@ static struct cmd_handler command_handlers[] = { { "kill", cmd_kill }, { "layout", cmd_layout }, { "move", cmd_move }, + { "opacity", cmd_opacity }, { "reload", cmd_reload }, { "split", cmd_split }, { "splith", cmd_splith }, diff --git a/sway/commands/opacity.c b/sway/commands/opacity.c new file mode 100644 index 00000000..b8cd1f09 --- /dev/null +++ b/sway/commands/opacity.c @@ -0,0 +1,39 @@ +#include +#include +#include "sway/commands.h" +#include "sway/tree/view.h" +#include "log.h" + +static bool parse_opacity(const char *opacity, float *val) { + char *err; + *val = strtof(opacity, &err); + if (*val < 0 || *val > 1 || *err) { + return false; + } + return true; +} + +struct cmd_results *cmd_opacity(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "layout", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + struct sway_container *con = + config->handler_context.current_container; + + float opacity = 0.0f; + + if (!parse_opacity(argv[0], &opacity)) { + return cmd_results_new(CMD_INVALID, "opacity ", + "Invalid value (expected 0..1): %s", argv[0]); + } + + con->alpha = opacity; + + if (con->type == C_VIEW) { + view_damage_whole(con->sway_view); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 8a4fb4a2..6cf5da48 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -75,7 +75,7 @@ static bool surface_intersect_output(struct wlr_surface *surface, static void render_surface(struct wlr_surface *surface, struct wlr_output *wlr_output, struct timespec *when, - double ox, double oy, float rotation) { + double ox, double oy, float rotation, float alpha) { struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); @@ -95,8 +95,8 @@ static void render_surface(struct wlr_surface *surface, wlr_matrix_project_box(matrix, &box, transform, rotation, wlr_output->transform_matrix); - // TODO: configurable alpha - wlr_render_texture_with_matrix(renderer, surface->texture, matrix, 1.0f); + wlr_render_texture_with_matrix(renderer, surface->texture, + matrix, alpha); wlr_surface_send_frame_done(surface, when); } @@ -110,13 +110,13 @@ static void render_surface(struct wlr_surface *surface, surface->current->width, surface->current->height, rotation); render_surface(subsurface->surface, wlr_output, when, - ox + sx, oy + sy, rotation); + ox + sx, oy + sy, rotation, alpha); } } static void render_xdg_v6_popups(struct wlr_xdg_surface_v6 *surface, struct wlr_output *wlr_output, struct timespec *when, double base_x, - double base_y, float rotation) { + double base_y, float rotation, float alpha) { double width = surface->surface->current->width; double height = surface->surface->current->height; @@ -136,19 +136,19 @@ static void render_xdg_v6_popups(struct wlr_xdg_surface_v6 *surface, width, height, rotation); render_surface(popup->surface, wlr_output, when, - base_x + popup_sx, base_y + popup_sy, rotation); + base_x + popup_sx, base_y + popup_sy, rotation, alpha); render_xdg_v6_popups(popup, wlr_output, when, - base_x + popup_sx, base_y + popup_sy, rotation); + base_x + popup_sx, base_y + popup_sy, rotation, alpha); } } static void render_wl_shell_surface(struct wlr_wl_shell_surface *surface, struct wlr_output *wlr_output, struct timespec *when, - double lx, double ly, float rotation, + double lx, double ly, float rotation, float alpha, bool is_child) { if (is_child || surface->state != WLR_WL_SHELL_SURFACE_STATE_POPUP) { render_surface(surface->surface, wlr_output, when, - lx, ly, rotation); + lx, ly, rotation, alpha); double width = surface->surface->current->width; double height = surface->surface->current->height; @@ -164,7 +164,7 @@ static void render_wl_shell_surface(struct wlr_wl_shell_surface *surface, width, height, rotation); render_wl_shell_surface(popup, wlr_output, when, - lx + popup_x, ly + popup_y, rotation, true); + lx + popup_x, ly + popup_y, rotation, alpha, true); } } } @@ -181,6 +181,7 @@ static void render_view(struct sway_container *view, void *data) { struct wlr_output *wlr_output = output->wlr_output; struct sway_view *sway_view = view->sway_view; struct wlr_surface *surface = sway_view->surface; + float alpha = sway_view->swayc->alpha; if (!surface) { return; @@ -191,17 +192,18 @@ static void render_view(struct sway_container *view, void *data) { int window_offset_x = view->sway_view->wlr_xdg_surface_v6->geometry.x; int window_offset_y = view->sway_view->wlr_xdg_surface_v6->geometry.y; render_surface(surface, wlr_output, when, - view->x - window_offset_x, view->y - window_offset_y, 0); + view->x - window_offset_x, view->y - window_offset_y, 0, alpha); render_xdg_v6_popups(sway_view->wlr_xdg_surface_v6, wlr_output, - when, view->x - window_offset_x, view->y - window_offset_y, 0); + when, view->x - window_offset_x, view->y - window_offset_y, 0, alpha); break; } case SWAY_WL_SHELL_VIEW: render_wl_shell_surface(sway_view->wlr_wl_shell_surface, wlr_output, - when, view->x, view->y, 0, false); + when, view->x, view->y, 0, alpha, false); break; case SWAY_XWAYLAND_VIEW: - render_surface(surface, wlr_output, when, view->x, view->y, 0); + render_surface(surface, wlr_output, when, view->x, view->y, + 0, alpha); break; default: break; @@ -214,7 +216,7 @@ static void render_layer(struct sway_output *output, struct timespec *when, wl_list_for_each(sway_layer, layer, link) { struct wlr_layer_surface *layer = sway_layer->layer_surface; render_surface(layer->surface, output->wlr_output, when, - sway_layer->geo.x, sway_layer->geo.y, 0); + sway_layer->geo.x, sway_layer->geo.y, 0, 1.0f); wlr_surface_send_frame_done(layer->surface, when); } } @@ -288,7 +290,7 @@ static void render_output(struct sway_output *output, struct timespec *when, } render_surface(xsurface->surface, wlr_output, &output->last_frame, - view_box.x - output_box->x, view_box.y - output_box->y, 0); + view_box.x - output_box->x, view_box.y - output_box->y, 0, 1.0f); } // TODO: Consider revising this when fullscreen windows are supported diff --git a/sway/meson.build b/sway/meson.build index 91aab0a0..f210c195 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -15,6 +15,7 @@ sway_sources = files( 'commands/focus.c', 'commands/focus_follows_mouse.c', 'commands/kill.c', + 'commands/opacity.c', 'commands/include.c', 'commands/input.c', 'commands/layout.c', diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 900e499a..59c3295a 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -413,6 +413,10 @@ The default colors are: However, any mark that starts with an underscore will not be drawn even if the option is on. The default option is _on_. +**opacity** :: + Set the opacity of the window between 0 (completely transparent) and 1 + (completely opaque). + **unmark** :: **Unmark** will remove _identifier_ from the list of current marks on a window. If no _identifier_ is specified, then **unmark** will remove all marks. diff --git a/sway/tree/container.c b/sway/tree/container.c index 753f333c..3be08645 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -72,6 +72,8 @@ struct sway_container *container_create(enum sway_container_type type) { c->layout = L_NONE; c->workspace_layout = L_NONE; c->type = type; + c->alpha = 1.0f; + if (type != C_VIEW) { c->children = create_list(); } -- cgit v1.2.3 From f77986338fc4186d003908012685c12d718ed647 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 4 Apr 2018 21:32:31 -0400 Subject: Implement resize command --- config.in | 16 +-- include/sway/tree/layout.h | 10 ++ sway/commands.c | 1 + sway/commands/resize.c | 284 +++++++++++++++++++++++++++++++++++++++++++++ sway/meson.build | 46 +++++--- sway/tree/layout.c | 21 ++++ 6 files changed, 351 insertions(+), 27 deletions(-) create mode 100644 sway/commands/resize.c (limited to 'sway/commands') diff --git a/config.in b/config.in index 086b66dc..54817f2a 100644 --- a/config.in +++ b/config.in @@ -164,16 +164,16 @@ mode "resize" { # right will grow the containers width # up will shrink the containers height # down will grow the containers height - bindsym $left resize shrink width 10 px or 10 ppt - bindsym $down resize grow height 10 px or 10 ppt - bindsym $up resize shrink height 10 px or 10 ppt - bindsym $right resize grow width 10 px or 10 ppt + bindsym $left resize shrink width 10px + bindsym $down resize grow height 10px + bindsym $up resize shrink height 10px + bindsym $right resize grow width 10px # ditto, with arrow keys - bindsym Left resize shrink width 10 px or 10 ppt - bindsym Down resize grow height 10 px or 10 ppt - bindsym Up resize shrink height 10 px or 10 ppt - bindsym Right resize grow width 10 px or 10 ppt + bindsym Left resize shrink width 10px + bindsym Down resize grow height 10px + bindsym Up resize shrink height 10px + bindsym Right resize grow width 10px # return to default mode bindsym Return mode "default" diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 784dcc9b..fc5ce21f 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -13,6 +13,13 @@ enum movement_direction { MOVE_CHILD, }; +enum resize_edge { + RESIZE_EDGE_LEFT, + RESIZE_EDGE_RIGHT, + RESIZE_EDGE_TOP, + RESIZE_EDGE_BOTTOM, +}; + struct sway_container; struct sway_root { @@ -63,4 +70,7 @@ struct sway_container *container_get_in_direction(struct sway_container struct sway_container *container_split(struct sway_container *child, enum sway_container_layout layout); +void container_recursive_resize(struct sway_container *container, + double amount, enum resize_edge edge); + #endif diff --git a/sway/commands.c b/sway/commands.c index 2786a879..22decef3 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -165,6 +165,7 @@ static struct cmd_handler command_handlers[] = { { "move", cmd_move }, { "opacity", cmd_opacity }, { "reload", cmd_reload }, + { "resize", cmd_resize }, { "split", cmd_split }, { "splith", cmd_splith }, { "splitt", cmd_splitt }, diff --git a/sway/commands/resize.c b/sway/commands/resize.c new file mode 100644 index 00000000..93c1fe7d --- /dev/null +++ b/sway/commands/resize.c @@ -0,0 +1,284 @@ +#include +#include +#include +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/tree/layout.h" +#include "log.h" + +static const int MIN_SANE_W = 100, MIN_SANE_H = 60; + +enum resize_unit { + RESIZE_UNIT_PX, + RESIZE_UNIT_PPT, + RESIZE_UNIT_DEFAULT, + RESIZE_UNIT_INVALID, +}; + +enum resize_axis { + RESIZE_AXIS_HORIZONTAL, + RESIZE_AXIS_VERTICAL, + RESIZE_AXIS_INVALID, +}; + +static enum resize_unit parse_resize_unit(const char *unit) { + if (strcasecmp(unit, "px") == 0) { + return RESIZE_UNIT_PX; + } + if (strcasecmp(unit, "ppt") == 0) { + return RESIZE_UNIT_PPT; + } + if (strcasecmp(unit, "default") == 0) { + return RESIZE_UNIT_DEFAULT; + } + return RESIZE_UNIT_INVALID; +} + +static enum resize_axis parse_resize_axis(const char *axis) { + if (strcasecmp(axis, "width") == 0 || strcasecmp(axis, "horizontal") == 0) { + return RESIZE_AXIS_HORIZONTAL; + } + if (strcasecmp(axis, "height") == 0 || strcasecmp(axis, "vertical") == 0) { + return RESIZE_AXIS_VERTICAL; + } + return RESIZE_AXIS_INVALID; +} + +static int parallel_coord(struct sway_container *c, enum resize_axis a) { + return a == RESIZE_AXIS_HORIZONTAL ? c->x : c->y; +} + +static int parallel_size(struct sway_container *c, enum resize_axis a) { + return a == RESIZE_AXIS_HORIZONTAL ? c->width : c->height; +} + +static void resize_tiled(int amount, enum resize_axis axis) { + struct sway_container *parent = config->handler_context.current_container; + struct sway_container *focused = parent; + if (!parent) { + return; + } + + enum sway_container_layout parallel_layout = + axis == RESIZE_AXIS_HORIZONTAL ? L_HORIZ : L_VERT; + int minor_weight = 0; + int major_weight = 0; + while (parent->parent) { + struct sway_container *next = parent->parent; + if (next->layout == parallel_layout) { + for (int i = 0; i < next->children->length; i++) { + struct sway_container *sibling = next->children->items[i]; + + int sibling_pos = parallel_coord(sibling, axis); + int focused_pos = parallel_coord(focused, axis); + int parent_pos = parallel_coord(parent, axis); + + if (sibling_pos != focused_pos) { + if (sibling_pos < parent_pos) { + minor_weight++; + } else if (sibling_pos > parent_pos) { + major_weight++; + } + } + } + if (major_weight || minor_weight) { + break; + } + } + parent = next; + } + + if (parent->type == C_ROOT) { + return; + } + + wlr_log(L_DEBUG, + "Found the proper parent: %p. It has %d l conts, and %d r conts", + parent->parent, minor_weight, major_weight); + + int min_sane = axis == RESIZE_AXIS_HORIZONTAL ? MIN_SANE_W : MIN_SANE_H; + + //TODO: Ensure rounding is done in such a way that there are NO pixel leaks + // ^ ????? + + for (int i = 0; i < parent->parent->children->length; i++) { + struct sway_container *sibling = parent->parent->children->items[i]; + + int sibling_pos = parallel_coord(sibling, axis); + int focused_pos = parallel_coord(focused, axis); + int parent_pos = parallel_coord(parent, axis); + + int sibling_size = parallel_size(sibling, axis); + int parent_size = parallel_size(parent, axis); + + if (sibling_pos != focused_pos) { + if (sibling_pos < parent_pos) { + double pixels = -amount / minor_weight; + if (major_weight && (sibling_size + pixels / 2) < min_sane) { + return; // Too small + } else if ((sibling_size + pixels) < min_sane) { + return; // Too small + } + } else if (sibling_pos > parent_pos) { + double pixels = -amount / major_weight; + if (minor_weight && (sibling_size + pixels / 2) < min_sane) { + return; // Too small + } else if ((sibling_size + pixels) < min_sane) { + return; // Too small + } + } + } else { + double pixels = amount; + if (parent_size + pixels < min_sane) { + return; // Too small + } + } + } + + enum resize_edge minor_edge = axis == RESIZE_AXIS_HORIZONTAL ? + RESIZE_EDGE_LEFT : RESIZE_EDGE_TOP; + enum resize_edge major_edge = axis == RESIZE_AXIS_HORIZONTAL ? + RESIZE_EDGE_RIGHT : RESIZE_EDGE_BOTTOM; + + for (int i = 0; i < parent->parent->children->length; i++) { + struct sway_container *sibling = parent->parent->children->items[i]; + + int sibling_pos = parallel_coord(sibling, axis); + int focused_pos = parallel_coord(focused, axis); + int parent_pos = parallel_coord(parent, axis); + + if (sibling_pos != focused_pos) { + if (sibling_pos < parent_pos) { + double pixels = -1 * amount; + pixels /= minor_weight; + if (major_weight) { + container_recursive_resize(sibling, pixels / 2, major_edge); + } else { + container_recursive_resize(sibling, pixels, major_edge); + } + } else if (sibling_pos > parent_pos) { + double pixels = -1 * amount; + pixels /= major_weight; + if (minor_weight) { + container_recursive_resize(sibling, pixels / 2, minor_edge); + } else { + container_recursive_resize(sibling, pixels, minor_edge); + } + } + } else { + if (major_weight != 0 && minor_weight != 0) { + double pixels = amount; + pixels /= 2; + container_recursive_resize(parent, pixels, minor_edge); + container_recursive_resize(parent, pixels, major_edge); + } else if (major_weight) { + container_recursive_resize(parent, amount, major_edge); + } else if (minor_weight) { + container_recursive_resize(parent, amount, minor_edge); + } + } + } + + arrange_windows(parent->parent, -1, -1); +} + +static void resize(int amount, enum resize_axis axis, enum resize_unit unit) { + struct sway_container *current = config->handler_context.current_container; + if (unit == RESIZE_UNIT_DEFAULT) { + // Default for tiling; TODO floating should be px + unit = RESIZE_UNIT_PPT; + } + + if (unit == RESIZE_UNIT_PPT) { + float pct = amount / 100.0f; + switch (axis) { + case RESIZE_AXIS_HORIZONTAL: + amount = (float)current->width * pct; + break; + case RESIZE_AXIS_VERTICAL: + amount = (float)current->height * pct; + break; + default: + sway_assert(0, "invalid resize axis"); + return; + } + } + + return resize_tiled(amount, axis); +} + +struct cmd_results *cmd_resize(int argc, char **argv) { + struct sway_container *current = config->handler_context.current_container; + if (!current) { + return cmd_results_new(CMD_INVALID, "resize", "Cannot resize nothing"); + } + if (current->type != C_VIEW && current->type != C_CONTAINER) { + return cmd_results_new(CMD_INVALID, "resize", + "Can only resize views/containers"); + } + if (strcasecmp(argv[0], "set") == 0) { + // TODO + //return cmd_resize_set(argc - 1, &argv[1]); + return cmd_results_new(CMD_INVALID, "resize", "resize set unimplemented"); + } + struct cmd_results *error; + if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { + return error; + } + + // TODO: resize grow|shrink left|right|up|down + + const char *usage = "Expected 'resize " + " [] [px|ppt]'"; + + int multiplier = 0; + if (strcasecmp(*argv, "grow") == 0) { + multiplier = 1; + } else if (strcasecmp(*argv, "shrink") == 0) { + multiplier = -1; + } else { + return cmd_results_new(CMD_INVALID, "resize", usage); + } + --argc; ++argv; + + enum resize_axis axis = parse_resize_axis(*argv); + if (axis == RESIZE_AXIS_INVALID) { + return cmd_results_new(CMD_INVALID, "resize", usage); + } + --argc; ++argv; + + int amount = 10; // Default amount + enum resize_unit unit = RESIZE_UNIT_DEFAULT; + + if (argc) { + char *err; + amount = (int)strtol(*argv, &err, 10); + if (*err) { + // e.g. `resize grow width 10px` + unit = parse_resize_unit(err); + if (unit == RESIZE_UNIT_INVALID) { + return cmd_results_new(CMD_INVALID, "resize", usage); + } + } + --argc; ++argv; + } + + if (argc) { + unit = parse_resize_unit(*argv); + if (unit == RESIZE_UNIT_INVALID) { + return cmd_results_new(CMD_INVALID, "resize", usage); + } + --argc; ++argv; + } + + if (argc) { + // Provied too many args, the bastard + return cmd_results_new(CMD_INVALID, "resize", usage); + } + + resize(amount * multiplier, axis, unit); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index f210c195..ec7b4c42 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -2,10 +2,28 @@ sway_sources = files( 'main.c', 'server.c', 'commands.c', + 'config.c', + 'criteria.c', + 'ipc-json.c', + 'ipc-server.c', + 'security.c', + + 'desktop/output.c', + 'desktop/layer_shell.c', + 'desktop/wl_shell.c', + 'desktop/xdg_shell_v6.c', + 'desktop/xwayland.c', + 'input/input-manager.c', 'input/seat.c', 'input/cursor.c', 'input/keyboard.c', + + 'config/bar.c', + 'config/output.c', + 'config/seat.c', + 'config/input.c', + 'commands/bar.c', 'commands/bind.c', 'commands/default_orientation.c', @@ -20,13 +38,19 @@ sway_sources = files( 'commands/input.c', 'commands/layout.c', 'commands/mode.c', - 'commands/split.c', + 'commands/mouse_warping.c', 'commands/move.c', + 'commands/output.c', + 'commands/reload.c', + 'commands/resize.c', 'commands/seat.c', 'commands/seat/attach.c', 'commands/seat/fallback.c', 'commands/set.c', + 'commands/split.c', 'commands/swaybg_command.c', + 'commands/workspace.c', + 'commands/bar/activate_button.c', 'commands/bar/binding_mode_indicator.c', 'commands/bar/bindsym.c', @@ -51,6 +75,7 @@ sway_sources = files( 'commands/bar/tray_padding.c', 'commands/bar/workspace_buttons.c', 'commands/bar/wrap_scroll.c', + 'commands/input/accel_profile.c', 'commands/input/click_method.c', 'commands/input/drag_lock.c', @@ -67,24 +92,7 @@ sway_sources = files( 'commands/input/xkb_options.c', 'commands/input/xkb_rules.c', 'commands/input/xkb_variant.c', - 'commands/mouse_warping.c', - 'commands/output.c', - 'commands/reload.c', - 'commands/workspace.c', - 'config.c', - 'config/bar.c', - 'config/output.c', - 'config/seat.c', - 'config/input.c', - 'criteria.c', - 'ipc-json.c', - 'ipc-server.c', - 'desktop/output.c', - 'desktop/layer_shell.c', - 'desktop/wl_shell.c', - 'desktop/xdg_shell_v6.c', - 'desktop/xwayland.c', - 'security.c', + 'tree/container.c', 'tree/layout.c', 'tree/view.c', diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 5abdbc32..7e2c61ed 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -729,3 +729,24 @@ struct sway_container *container_split(struct sway_container *child, return cont; } + +void container_recursive_resize(struct sway_container *container, + double amount, enum resize_edge edge) { + bool layout_match = true; + wlr_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); + if (edge == RESIZE_EDGE_LEFT || edge == RESIZE_EDGE_RIGHT) { + container->width += amount; + layout_match = container->layout == L_HORIZ; + } else if (edge == RESIZE_EDGE_TOP || edge == RESIZE_EDGE_BOTTOM) { + container->height += amount; + layout_match = container->layout == L_VERT; + } + if (container->children) { + for (int i = 0; i < container->children->length; i++) { + struct sway_container *child = container->children->items[i]; + double amt = layout_match ? + amount / container->children->length : amount; + container_recursive_resize(child, amt, edge); + } + } +} -- cgit v1.2.3 From 57954a2b24f1e211c3b8811fb898ef4e076cb098 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 2 Apr 2018 13:49:37 -0400 Subject: Implement move [left|right|up|down] The exact semantics of this command are complicated. I'll describe each test scenario as s-expressions. Everything assumes L_HORIZ if not specified, but if you rotate everything 90 degrees the same test cases hold. ``` (container (view a) (view b focus) (view c)) -> move left (container (view b focus) (view a) (view c)) (container (view a) (view b focus) (view c)) -> move right (container (view a) (view c) (view b focus)) (container L_VERT (view a)) (container L_HORIZ (view b) (view c focus)) -> move up (container L_VERT (view a) (view c focus)) (container L_HORIZ (view b)) (workspace (view a) (view b focus) (view c)) -> move up (workspace [split direction flipped] (view b focus) (container (view a) (view c))) (workspace (view a) (view b focus) (view c)) -> move down (workspace [split direction flipped] (container (view a) (view c)) (view b focus))) Note: outputs use wlr_output_layout instead of assuming that i+/-1 is the next output in the move direction. (root (output X11-1 (workspace 1)) (output X11-2 (workspace 1 (view a focus) (view b))))) -> move left (root (output X11-1 (workspace 1 (view a focus))) (output X11-2 (workspace 1 (view b))))) (root (output X11-1 (workspace 1 (container (view a) (view b))) (output X11-2 (workspace 1 (view c focus))))) -> move left (root (output X11-1 (workspace 1 (container (view a) (view b)) (view c focus))) (output X11-2 (workspace 1))) ``` --- include/sway/tree/container.h | 7 + sway/commands/move.c | 4 + sway/tree/container.c | 46 ++++--- sway/tree/layout.c | 294 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 306 insertions(+), 45 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 4c60530f..2a8b8aba 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -182,4 +182,11 @@ void container_create_notify(struct sway_container *container); void container_damage_whole(struct sway_container *container); +bool container_reap_empty(struct sway_container *con); + +struct sway_container *container_reap_empty_recursive( + struct sway_container *con); + +struct sway_container *container_flatten(struct sway_container *container); + #endif diff --git a/sway/commands/move.c b/sway/commands/move.c index c954ab94..15a5ebc4 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -82,6 +82,8 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, config->handler_context.seat, ws); container_move_to(current, focus); seat_set_focus(config->handler_context.seat, old_parent); + container_reap_empty(old_parent); + container_reap_empty(focus->parent); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "output") == 0) { @@ -109,6 +111,8 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, struct sway_container *old_parent = current->parent; container_move_to(current, focus); seat_set_focus(config->handler_context.seat, old_parent); + container_reap_empty(old_parent); + container_reap_empty(focus->parent); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } return cmd_results_new(CMD_INVALID, "move", expected_syntax); diff --git a/sway/tree/container.c b/sway/tree/container.c index ab8363bc..ea1c93bb 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -208,7 +208,7 @@ static void container_root_finish(struct sway_container *con) { wlr_log(L_ERROR, "TODO: destroy the root container"); } -static bool container_reap_empty(struct sway_container *con) { +bool container_reap_empty(struct sway_container *con) { switch (con->type) { case C_ROOT: case C_OUTPUT: @@ -225,14 +225,6 @@ static bool container_reap_empty(struct sway_container *con) { if (con->children->length == 0) { _container_destroy(con); return true; - } else if (con->children->length == 1) { - struct sway_container *child = con->children->items[0]; - if (child->type == C_CONTAINER) { - container_remove_child(child); - container_replace_child(con, child); - _container_destroy(con); - return true; - } } case C_VIEW: break; @@ -245,6 +237,29 @@ static bool container_reap_empty(struct sway_container *con) { return false; } +struct sway_container *container_reap_empty_recursive( + struct sway_container *con) { + while (con) { + struct sway_container *next = con->parent; + if (!container_reap_empty(con)) { + break; + } + con = next; + } + return con; +} + +struct sway_container *container_flatten(struct sway_container *container) { + while (container->type == C_CONTAINER && container->children->length == 1) { + struct sway_container *child = container->children->items[0]; + struct sway_container *parent = container->parent; + container_replace_child(container, child); + container_destroy(container); + container = parent; + } + return container; +} + struct sway_container *container_destroy(struct sway_container *con) { if (con == NULL) { return NULL; @@ -283,18 +298,7 @@ struct sway_container *container_destroy(struct sway_container *con) { break; } - struct sway_container *tmp = parent; - while (parent) { - tmp = parent->parent; - - if (!container_reap_empty(parent)) { - break; - } - - parent = tmp; - } - - return tmp; + return container_reap_empty_recursive(parent); } static void container_close_func(struct sway_container *container, void *data) { diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 343f349a..a060cb85 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -100,13 +100,31 @@ static int index_child(const struct sway_container *child) { return i; } +void container_insert_child(struct sway_container *parent, + struct sway_container *child, int i) { + struct sway_container *old_parent = NULL; + list_insert(parent->children, i, child); + child->parent = parent; + if (old_parent && old_parent != parent) { + wl_signal_emit(&child->events.reparent, old_parent); + } +} + struct sway_container *container_add_sibling(struct sway_container *fixed, struct sway_container *active) { // TODO handle floating + struct sway_container *old_parent = NULL; + if (active->parent) { + old_parent = active->parent; + container_remove_child(active); + } struct sway_container *parent = fixed->parent; int i = index_child(fixed); list_insert(parent->children, i + 1, active); active->parent = parent; + if (old_parent && old_parent != parent) { + wl_signal_emit(&active->events.reparent, old_parent); + } return active->parent; } @@ -166,9 +184,253 @@ void container_move_to(struct sway_container *container, arrange_windows(new_parent, -1, -1); } +static bool sway_dir_to_wlr(enum movement_direction dir, + enum wlr_direction *out) { + switch (dir) { + case MOVE_UP: + *out = WLR_DIRECTION_UP; + break; + case MOVE_DOWN: + *out = WLR_DIRECTION_DOWN; + break; + case MOVE_LEFT: + *out = WLR_DIRECTION_LEFT; + break; + case MOVE_RIGHT: + *out = WLR_DIRECTION_RIGHT; + break; + default: + return false; + } + + return true; +} + +static bool is_parallel(enum sway_container_layout layout, + enum movement_direction dir) { + switch (layout) { + case L_TABBED: + case L_STACKED: + case L_HORIZ: + return dir == MOVE_LEFT || dir == MOVE_RIGHT; + case L_VERT: + return dir == MOVE_UP || dir == MOVE_DOWN; + default: + return false; + } +} + +static enum movement_direction invert_movement(enum movement_direction dir) { + switch (dir) { + case MOVE_LEFT: + return MOVE_RIGHT; + case MOVE_RIGHT: + return MOVE_LEFT; + case MOVE_UP: + return MOVE_DOWN; + case MOVE_DOWN: + return MOVE_LEFT; + default: + sway_assert(0, "This function expects left|right|up|down"); + return MOVE_LEFT; + } +} + +/* Gets the index of the most extreme member based on the movement offset */ +static int container_limit(struct sway_container *container, int offs) { + if (container->children->length == 0) { + return 0; + } + return offs < 0 ? 0 : container->children->length - 1; +} + +static int move_offs(enum movement_direction move_dir) { + return move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1; +} + +static void workspace_rejigger(struct sway_container *ws, + struct sway_container *child, enum movement_direction move_dir) { + struct sway_container *original_parent = child->parent; + struct sway_container *new_parent = + container_split(ws, ws->layout); + + container_remove_child(child); + for (int i = 0; i < ws->children->length; ++i) { + struct sway_container *_child = ws->children->items[i]; + container_move_to(new_parent, _child); + } + + int index = move_offs(move_dir); + container_insert_child(ws, child, index < 0 ? 0 : 1); + container_set_layout(ws, + move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT); + + container_flatten(ws); + container_reap_empty_recursive(original_parent); + arrange_windows(ws, -1, -1); +} + void container_move(struct sway_container *container, - enum movement_direction dir, int move_amt) { - // TODO + enum movement_direction move_dir, int move_amt) { + if (!sway_assert( + container->type != C_CONTAINER || container->type != C_VIEW, + "Can only move containers and views")) { + return; + } + int offs = move_offs(move_dir); + + struct sway_container *sibling = NULL; + struct sway_container *current = container; + struct sway_container *parent = current->parent; + + if (parent != container_flatten(parent)) { + // Special case: we were the last one in this container, so flatten it + // and leave + return; + } + + while (!sibling) { + if (current->type == C_ROOT) { + return; + } + + parent = current->parent; + wlr_log(L_DEBUG, "Visiting %p %s '%s'", current, + container_type_to_str(current->type), current->name); + + int index = index_child(current); + int limit = container_limit(parent, offs); + + switch (current->type) { + case C_OUTPUT: { + enum wlr_direction wlr_dir; + sway_dir_to_wlr(move_dir, &wlr_dir); + double ref_x = current->x + current->width / 2; + double ref_y = current->y + current->height / 2; + ref_x += current->sway_output->wlr_output->lx; + ref_y += current->sway_output->wlr_output->ly; + struct wlr_output *next = wlr_output_layout_adjacent_output( + root_container.sway_root->output_layout, wlr_dir, + current->sway_output->wlr_output, + current->x, current->y); + if (!next) { + wlr_log(L_DEBUG, "Hit edge of output, nowhere else to go"); + return; + } + struct sway_output *next_output = next->data; + current = next_output->swayc; + wlr_log(L_DEBUG, "Selected next output (%s)", current->name); + // Select workspace and get outta here + current = seat_get_focus_inactive( + config->handler_context.seat, current); + if (current->type != C_WORKSPACE) { + current = container_parent(current, C_WORKSPACE); + } + sibling = current; + break; + } + case C_WORKSPACE: + if (!is_parallel(current->layout, move_dir)) { + // Special case + wlr_log(L_DEBUG, "Rejiggering the workspace"); + workspace_rejigger(current, container, move_dir); + return; + } else { + wlr_log(L_DEBUG, "Selecting output"); + current = current->parent; + } + break; + case C_CONTAINER: + case C_VIEW: + if (is_parallel(parent->layout, move_dir)) { + if (index == limit) { + if (current->parent == container->parent) { + wlr_log(L_DEBUG, "Hit limit, selecting parent"); + current = current->parent; + } else { + wlr_log(L_DEBUG, "Hit limit, " + "promoting descendant to sibling"); + // Special case + struct sway_container *old_parent = container->parent; + container_remove_child(container); + container_insert_child(current->parent, container, + index + (offs < 0 ? 0 : 1)); + container->width = container->height = 0; + arrange_windows(current->parent, -1, -1); + arrange_windows(old_parent, -1, -1); + return; + } + } else { + wlr_log(L_DEBUG, "Selecting sibling"); + sibling = parent->children->items[index + offs]; + } + } else { + wlr_log(L_DEBUG, "Moving up to find a parallel container"); + current = current->parent; + } + break; + default: + sway_assert(0, "Not expecting to see container of type %s here", + container_type_to_str(current->type)); + return; + } + } + + // Part two: move stuff around + int index = index_child(container); + struct sway_container *old_parent = container->parent; + + switch (sibling->type) { + case C_VIEW: + if (sibling->parent == container->parent) { + wlr_log(L_DEBUG, "Swapping siblings"); + sibling->parent->children->items[index + offs] = container; + sibling->parent->children->items[index] = sibling; + arrange_windows(sibling->parent, -1, -1); + } else { + wlr_log(L_DEBUG, "Promoting to sibling of cousin"); + container_remove_child(container); + container_insert_child(sibling->parent, container, + index_child(sibling) + (offs > 0 ? 0 : 1)); + container->width = container->height = 0; + arrange_windows(sibling->parent, -1, -1); + arrange_windows(old_parent, -1, -1); + } + break; + case C_WORKSPACE: // Note: only in the case of moving between outputs + case C_CONTAINER: + if (is_parallel(sibling->layout, move_dir)) { + int limit = container_limit(sibling, move_dir); + wlr_log(L_DEBUG, "Reparenting container (paralell)"); + limit = limit != 0 ? limit + 1 : limit; // Convert to index + wlr_log(L_DEBUG, "Reparenting container (paralell) %d", limit); + container_remove_child(container); + container_insert_child(sibling, container, limit); + container->width = container->height = 0; + arrange_windows(sibling, -1, -1); + arrange_windows(old_parent, -1, -1); + } else { + wlr_log(L_DEBUG, "Reparenting container (perpendicular)"); + container_remove_child(container); + struct sway_container *focus_inactive = seat_get_focus_inactive( + config->handler_context.seat, sibling); + if (focus_inactive) { + container_add_sibling(focus_inactive, container); + } else if (sibling->children->length) { + container_add_sibling(sibling->children->items[0], container); + } else { + container_add_child(sibling, container); + } + container->width = container->height = 0; + arrange_windows(sibling, -1, -1); + arrange_windows(old_parent, -1, -1); + } + break; + default: + sway_assert(0, "Not expecting to see container of type %s here", + container_type_to_str(sibling->type)); + return; + } } enum sway_container_layout container_get_default_layout( @@ -320,6 +582,7 @@ void arrange_windows(struct sway_container *container, container->children->length); break; } + container_damage_whole(container); } static void apply_horiz_layout(struct sway_container *container, @@ -512,28 +775,6 @@ static void get_layout_center_position(struct sway_container *container, } } -static bool sway_dir_to_wlr(enum movement_direction dir, - enum wlr_direction *out) { - switch (dir) { - case MOVE_UP: - *out = WLR_DIRECTION_UP; - break; - case MOVE_DOWN: - *out = WLR_DIRECTION_DOWN; - break; - case MOVE_LEFT: - *out = WLR_DIRECTION_LEFT; - break; - case MOVE_RIGHT: - *out = WLR_DIRECTION_RIGHT; - break; - default: - return false; - } - - return true; -} - static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { if (output == NULL) { return NULL; @@ -673,6 +914,9 @@ struct sway_container *container_replace_child(struct sway_container *child, int i = index_child(child); // TODO floating + if (new_child->parent) { + container_remove_child(new_child); + } parent->children->items[i] = new_child; new_child->parent = parent; child->parent = NULL; @@ -718,7 +962,9 @@ struct sway_container *container_split(struct sway_container *child, } container_add_child(workspace, cont); + enum sway_container_layout old_layout = workspace->layout; container_set_layout(workspace, layout); + cont->layout = old_layout; if (set_focus) { seat_set_focus(seat, cont); -- cgit v1.2.3 From 516f5454adb3fc7dd2e02258251b7cb6d6949aa3 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 6 Apr 2018 11:27:40 -0400 Subject: Simplify damage tracking functions, use them in layer shell --- include/sway/desktop.h | 7 ++----- include/sway/output.h | 8 ++++---- include/sway/tree/view.h | 4 +--- sway/commands/opacity.c | 5 +---- sway/desktop/desktop.c | 14 ++++---------- sway/desktop/layer_shell.c | 44 ++++++++++++++++++++++++++------------------ sway/desktop/output.c | 8 ++++---- sway/desktop/wl_shell.c | 2 +- sway/desktop/xdg_shell_v6.c | 2 +- sway/desktop/xwayland.c | 18 +++++++++--------- sway/tree/view.c | 27 +++++++++++---------------- 11 files changed, 64 insertions(+), 75 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/desktop.h b/include/sway/desktop.h index 96bdc94c..f1ad759a 100644 --- a/include/sway/desktop.h +++ b/include/sway/desktop.h @@ -1,7 +1,4 @@ #include -void desktop_damage_whole_surface(struct wlr_surface *surface, double lx, - double ly); - -void desktop_damage_from_surface(struct wlr_surface *surface, double lx, - double ly); +void desktop_damage_surface(struct wlr_surface *surface, double lx, double ly, + bool whole); diff --git a/include/sway/output.h b/include/sway/output.h index 4bffa2b7..56571548 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -34,11 +34,11 @@ struct sway_output { void output_damage_whole(struct sway_output *output); -void output_damage_whole_surface(struct sway_output *output, - double ox, double oy, struct wlr_surface *surface); +void output_damage_surface(struct sway_output *output, double ox, double oy, + struct wlr_surface *surface, bool whole); -void output_damage_whole_view(struct sway_output *output, - struct sway_view *view); +void output_damage_view(struct sway_output *output, struct sway_view *view, + bool whole); void output_damage_whole_container(struct sway_output *output, struct sway_container *con); diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 611c4f0b..b51c54b5 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -157,9 +157,7 @@ void view_set_activated(struct sway_view *view, bool activated); void view_close(struct sway_view *view); -void view_damage_whole(struct sway_view *view); - -void view_damage_from(struct sway_view *view); +void view_damage(struct sway_view *view, bool whole); void view_for_each_surface(struct sway_view *view, wlr_surface_iterator_func_t iterator, void *user_data); diff --git a/sway/commands/opacity.c b/sway/commands/opacity.c index b8cd1f09..68fd9f42 100644 --- a/sway/commands/opacity.c +++ b/sway/commands/opacity.c @@ -30,10 +30,7 @@ struct cmd_results *cmd_opacity(int argc, char **argv) { } con->alpha = opacity; - - if (con->type == C_VIEW) { - view_damage_whole(con->sway_view); - } + container_damage_whole(con); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/desktop/desktop.c b/sway/desktop/desktop.c index 3a13191f..66f33151 100644 --- a/sway/desktop/desktop.c +++ b/sway/desktop/desktop.c @@ -2,19 +2,13 @@ #include "sway/desktop.h" #include "sway/output.h" -void desktop_damage_whole_surface(struct wlr_surface *surface, double lx, - double ly) { +void desktop_damage_surface(struct wlr_surface *surface, double lx, double ly, + bool whole) { for (int i = 0; i < root_container.children->length; ++i) { struct sway_container *cont = root_container.children->items[i]; if (cont->type == C_OUTPUT) { - output_damage_whole_surface(cont->sway_output, - lx - cont->x, ly - cont->y, surface); + output_damage_surface(cont->sway_output, lx - cont->x, ly - cont->y, + surface, whole); } } } - -void desktop_damage_from_surface(struct wlr_surface *surface, double lx, - double ly) { - // TODO - desktop_damage_whole_surface(surface, lx, ly); -} diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index 663ec7ba..f841e5f1 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -229,33 +229,39 @@ static void handle_surface_commit(struct wl_listener *listener, void *data) { wl_container_of(listener, layer, surface_commit); struct wlr_layer_surface *layer_surface = layer->layer_surface; struct wlr_output *wlr_output = layer_surface->output; - if (wlr_output != NULL) { - struct sway_output *output = wlr_output->data; - struct wlr_box old_geo = layer->geo; - arrange_layers(output); - if (memcmp(&old_geo, &layer->geo, sizeof(struct wlr_box)) != 0) { - // TODO DAMAGE apply whole surface from previous and new geos - } else { - // TODO DAMAGE from surface damage - } - wlr_output_damage_add_box(output->damage, &old_geo); - wlr_output_damage_add_box(output->damage, &layer->geo); + if (wlr_output == NULL) { + return; + } + + struct sway_output *output = wlr_output->data; + struct wlr_box old_geo = layer->geo; + arrange_layers(output); + if (memcmp(&old_geo, &layer->geo, sizeof(struct wlr_box)) != 0) { + output_damage_surface(output, old_geo.x, old_geo.y, + layer_surface->surface, true); + output_damage_surface(output, layer->geo.x, layer->geo.y, + layer_surface->surface, true); + } else { + output_damage_surface(output, layer->geo.x, layer->geo.y, + layer_surface->surface, false); } } static void unmap(struct sway_layer_surface *sway_layer) { struct wlr_output *wlr_output = sway_layer->layer_surface->output; - if (wlr_output != NULL) { - struct sway_output *output = wlr_output->data; - wlr_output_damage_add_box(output->damage, &sway_layer->geo); + if (wlr_output == NULL) { + return; } + struct sway_output *output = wlr_output->data; + output_damage_surface(output, sway_layer->geo.x, sway_layer->geo.y, + sway_layer->layer_surface->surface, true); } static void handle_destroy(struct wl_listener *listener, void *data) { - struct sway_layer_surface *sway_layer = wl_container_of(listener, - sway_layer, destroy); + struct sway_layer_surface *sway_layer = + wl_container_of(listener, sway_layer, destroy); wlr_log(L_DEBUG, "Layer surface destroyed (%s)", - sway_layer->layer_surface->namespace); + sway_layer->layer_surface->namespace); if (sway_layer->layer_surface->mapped) { unmap(sway_layer); } @@ -277,7 +283,9 @@ static void handle_map(struct wl_listener *listener, void *data) { struct sway_layer_surface *sway_layer = wl_container_of(listener, sway_layer, map); struct sway_output *output = sway_layer->layer_surface->output->data; - wlr_output_damage_add_box(output->damage, &sway_layer->geo); + output_damage_surface(output, sway_layer->geo.x, sway_layer->geo.y, + sway_layer->layer_surface->surface, true); + // TODO: send enter to subsurfaces and popups wlr_surface_send_enter(sway_layer->layer_surface->surface, sway_layer->layer_surface->output); } diff --git a/sway/desktop/output.c b/sway/desktop/output.c index aa18f1b8..3bbd0bb2 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -332,14 +332,14 @@ void output_damage_whole(struct sway_output *output) { wlr_output_damage_add_whole(output->damage); } -void output_damage_whole_surface(struct sway_output *output, - double ox, double oy, struct wlr_surface *surface) { +void output_damage_surface(struct sway_output *output, double ox, double oy, + struct wlr_surface *surface, bool whole) { // TODO output_damage_whole(output); } -void output_damage_whole_view(struct sway_output *output, - struct sway_view *view) { +void output_damage_view(struct sway_output *output, struct sway_view *view, + bool whole) { // TODO output_damage_whole(output); } diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index fff31da8..b63c220c 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -79,7 +79,7 @@ static void handle_commit(struct wl_listener *listener, void *data) { // TODO: Let floating views do whatever view_update_size(view, wl_shell_view->pending_width, wl_shell_view->pending_height); - view_damage_from(view); + view_damage(view, false); } static void handle_destroy(struct wl_listener *listener, void *data) { diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 8361aab3..b82eec8f 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -169,7 +169,7 @@ static void handle_commit(struct wl_listener *listener, void *data) { // TODO: Let floating views do whatever view_update_size(view, xdg_shell_v6_view->pending_width, xdg_shell_v6_view->pending_height); - view_damage_from(view); + view_damage(view, false); } static void handle_new_popup(struct wl_listener *listener, void *data) { diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 10bfcc89..6de1365d 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -32,15 +32,15 @@ static void unmanaged_handle_commit(struct wl_listener *listener, void *data) { if (xsurface->x != surface->lx || xsurface->y != surface->ly) { // Surface has moved - desktop_damage_whole_surface(xsurface->surface, - surface->lx, surface->ly); + desktop_damage_surface(xsurface->surface, surface->lx, surface->ly, + true); surface->lx = xsurface->x; surface->ly = xsurface->y; - desktop_damage_whole_surface(xsurface->surface, - surface->lx, surface->ly); + desktop_damage_surface(xsurface->surface, surface->lx, surface->ly, + true); } else { - desktop_damage_from_surface(xsurface->surface, - xsurface->x, xsurface->y); + desktop_damage_surface(xsurface->surface, xsurface->x, xsurface->y, + false); } } @@ -57,7 +57,7 @@ static void unmanaged_handle_map(struct wl_listener *listener, void *data) { surface->lx = xsurface->x; surface->ly = xsurface->y; - desktop_damage_whole_surface(xsurface->surface, surface->lx, surface->ly); + desktop_damage_surface(xsurface->surface, surface->lx, surface->ly, true); // TODO: we don't send surface enter/leave events to xwayland unmanaged // surfaces, but xwayland doesn't support HiDPI anyway @@ -67,7 +67,7 @@ static void unmanaged_handle_unmap(struct wl_listener *listener, void *data) { struct sway_xwayland_unmanaged *surface = wl_container_of(listener, surface, unmap); struct wlr_xwayland_surface *xsurface = surface->wlr_xwayland_surface; - desktop_damage_whole_surface(xsurface->surface, xsurface->x, xsurface->y); + desktop_damage_surface(xsurface->surface, xsurface->x, xsurface->y, true); wl_list_remove(&surface->link); wl_list_remove(&surface->commit.link); } @@ -209,7 +209,7 @@ static void handle_commit(struct wl_listener *listener, void *data) { // TODO: Let floating views do whatever view_update_size(view, xwayland_view->pending_width, xwayland_view->pending_height); - view_damage_from(view); + view_damage(view, false); } static void handle_unmap(struct wl_listener *listener, void *data) { diff --git a/sway/tree/view.c b/sway/tree/view.c index 9855c5e1..99b44720 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -79,20 +79,15 @@ void view_close(struct sway_view *view) { } } -void view_damage_whole(struct sway_view *view) { +void view_damage(struct sway_view *view, bool whole) { for (int i = 0; i < root_container.children->length; ++i) { struct sway_container *cont = root_container.children->items[i]; if (cont->type == C_OUTPUT) { - output_damage_whole_view(cont->sway_output, view); + output_damage_view(cont->sway_output, view, whole); } } } -void view_damage_from(struct sway_view *view) { - // TODO - view_damage_whole(view); -} - static void view_get_layout_box(struct sway_view *view, struct wlr_box *box) { struct sway_container *output = container_parent(view->swayc, C_OUTPUT); @@ -191,7 +186,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { arrange_windows(cont->parent, -1, -1); input_manager_set_focus(input_manager, cont); - view_damage_whole(view); + view_damage(view, true); view_handle_container_reparent(&view->container_reparent, NULL); } @@ -202,7 +197,7 @@ void view_unmap(struct sway_view *view) { wl_signal_emit(&view->events.unmap, view); - view_damage_whole(view); + view_damage(view, true); wl_list_remove(&view->surface_new_subsurface.link); wl_list_remove(&view->container_reparent.link); @@ -220,10 +215,10 @@ void view_update_position(struct sway_view *view, double ox, double oy) { return; } - view_damage_whole(view); + view_damage(view, true); view->swayc->x = ox; view->swayc->y = oy; - view_damage_whole(view); + view_damage(view, true); } void view_update_size(struct sway_view *view, int width, int height) { @@ -231,10 +226,10 @@ void view_update_size(struct sway_view *view, int width, int height) { return; } - view_damage_whole(view); + view_damage(view, true); view->width = width; view->height = height; - view_damage_whole(view); + view_damage(view, true); } @@ -253,7 +248,7 @@ static void view_child_handle_surface_commit(struct wl_listener *listener, struct sway_view_child *child = wl_container_of(listener, child, surface_commit); // TODO: only accumulate damage from the child - view_damage_from(child->view); + view_damage(child->view, false); } static void view_child_handle_surface_new_subsurface( @@ -315,12 +310,12 @@ void view_child_init(struct sway_view_child *child, view_init_subsurfaces(child->view, surface); // TODO: only damage the whole child - view_damage_whole(child->view); + view_damage(child->view, true); } void view_child_destroy(struct sway_view_child *child) { // TODO: only damage the whole child - view_damage_whole(child->view); + view_damage(child->view, true); wl_list_remove(&child->surface_commit.link); wl_list_remove(&child->surface_destroy.link); -- cgit v1.2.3 From 9db859585e79d468ff79f41db6bc0950fb285a5a Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 7 Apr 2018 18:01:18 -0400 Subject: container_create_notify on split --- sway/commands/split.c | 1 + sway/tree/layout.c | 1 + 2 files changed, 2 insertions(+) (limited to 'sway/commands') diff --git a/sway/commands/split.c b/sway/commands/split.c index ab8565a9..130ed31f 100644 --- a/sway/commands/split.c +++ b/sway/commands/split.c @@ -11,6 +11,7 @@ static struct cmd_results *do_split(int layout) { struct sway_container *con = config->handler_context.current_container; struct sway_container *parent = container_split(con, layout); + container_create_notify(parent); arrange_windows(parent, -1, -1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/tree/layout.c b/sway/tree/layout.c index ce4457b1..ae76ca26 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -251,6 +251,7 @@ static void workspace_rejigger(struct sway_container *ws, container_flatten(ws); container_reap_empty_recursive(original_parent); wl_signal_emit(&child->events.reparent, original_parent); + container_create_notify(new_parent); arrange_windows(ws, -1, -1); } -- cgit v1.2.3 From 042b80b9faef72c36451eecb5c803223fda3264a Mon Sep 17 00:00:00 2001 From: db Date: Sun, 8 Apr 2018 16:44:59 +0200 Subject: Add workspace_auto_back_and_forth command This is the only missing piece - other code regarding this functionality has already been ported from pre-wlroots source. --- sway/commands.c | 1 + sway/commands/ws_auto_back_and_forth.c | 12 ++++++++++++ sway/meson.build | 1 + 3 files changed, 14 insertions(+) create mode 100644 sway/commands/ws_auto_back_and_forth.c (limited to 'sway/commands') diff --git a/sway/commands.c b/sway/commands.c index 22decef3..20b8a2aa 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -106,6 +106,7 @@ static struct cmd_handler handlers[] = { { "output", cmd_output }, { "seat", cmd_seat }, { "workspace", cmd_workspace }, + { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, }; static struct cmd_handler bar_handlers[] = { diff --git a/sway/commands/ws_auto_back_and_forth.c b/sway/commands/ws_auto_back_and_forth.c new file mode 100644 index 00000000..2485db35 --- /dev/null +++ b/sway/commands/ws_auto_back_and_forth.c @@ -0,0 +1,12 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_ws_auto_back_and_forth(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1))) { + return error; + } + config->auto_back_and_forth = !strcasecmp(argv[0], "yes"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index 1fe0f29a..2521069f 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -52,6 +52,7 @@ sway_sources = files( 'commands/split.c', 'commands/swaybg_command.c', 'commands/workspace.c', + 'commands/ws_auto_back_and_forth.c', 'commands/bar/activate_button.c', 'commands/bar/binding_mode_indicator.c', -- cgit v1.2.3 From 0e3ddf255ef56b7fe2b868232b80d04ea961120b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 8 Apr 2018 14:15:13 -0400 Subject: Add input "identifier" map_to_output "identifier" --- include/sway/commands.h | 1 + include/sway/config.h | 4 +++- sway/commands.c | 1 + sway/commands/input/map_to_output.c | 27 +++++++++++++++++++++++++++ sway/config/input.c | 4 ++++ sway/input/seat.c | 30 ++++++++++++++++++++++++++++++ sway/meson.build | 1 + sway/sway-input.5.txt | 20 ++++++++++++++++++++ 8 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 sway/commands/input/map_to_output.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index edb5a213..bc5d5412 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -192,6 +192,7 @@ sway_cmd input_cmd_drag_lock; sway_cmd input_cmd_dwt; sway_cmd input_cmd_events; sway_cmd input_cmd_left_handed; +sway_cmd input_cmd_map_to_output; sway_cmd input_cmd_middle_emulation; sway_cmd input_cmd_natural_scroll; sway_cmd input_cmd_pointer_accel; diff --git a/include/sway/config.h b/include/sway/config.h index 91f772b5..ed49fbbd 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -52,7 +52,7 @@ struct sway_mode { }; /** - * libinput options for input devices + * options for input devices */ struct input_config { char *identifier; @@ -75,6 +75,8 @@ struct input_config { char *xkb_rules; char *xkb_variant; + char *mapped_output; + bool capturable; struct wlr_box region; }; diff --git a/sway/commands.c b/sway/commands.c index 20b8a2aa..55929659 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -187,6 +187,7 @@ static struct cmd_handler input_handlers[] = { { "dwt", input_cmd_dwt }, { "events", input_cmd_events }, { "left_handed", input_cmd_left_handed }, + { "map_to_output", input_cmd_map_to_output }, { "middle_emulation", input_cmd_middle_emulation }, { "natural_scroll", input_cmd_natural_scroll }, { "pointer_accel", input_cmd_pointer_accel }, diff --git a/sway/commands/input/map_to_output.c b/sway/commands/input/map_to_output.c new file mode 100644 index 00000000..60e4608e --- /dev/null +++ b/sway/commands/input/map_to_output.c @@ -0,0 +1,27 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include "sway/config.h" +#include "sway/commands.h" +#include "sway/input/input-manager.h" +#include "log.h" + +struct cmd_results *input_cmd_map_to_output(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "map_to_output", EXPECTED_EQUAL_TO, 1))) { + return error; + } + struct input_config *current_input_config = + config->handler_context.input_config; + if (!current_input_config) { + return cmd_results_new(CMD_FAILURE, "map_to_output", + "No input device defined."); + } + struct input_config *new_config = + new_input_config(current_input_config->identifier); + + new_config->mapped_output = strdup(argv[0]); + apply_input_config(new_config); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config/input.c b/sway/config/input.c index c4f6211d..5e657c43 100644 --- a/sway/config/input.c +++ b/sway/config/input.c @@ -88,6 +88,10 @@ void merge_input_config(struct input_config *dst, struct input_config *src) { free(dst->xkb_variant); dst->xkb_variant = strdup(src->xkb_variant); } + if (src->mapped_output) { + free(dst->mapped_output); + dst->mapped_output = strdup(src->mapped_output); + } } struct input_config *copy_input_config(struct input_config *ic) { diff --git a/sway/input/seat.c b/sway/input/seat.c index d9b42828..7b01fe88 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -1,6 +1,7 @@ #define _XOPEN_SOURCE 700 #define _POSIX_C_SOURCE 199309L #include +#include #include #include #include @@ -219,10 +220,38 @@ struct sway_seat *seat_create(struct sway_input_manager *input, return seat; } +static void seat_apply_input_config(struct sway_seat *seat, + struct sway_seat_device *sway_device) { + struct input_config *ic = input_device_get_config( + sway_device->input_device); + if (!ic) { + return; + } + wlr_log(L_DEBUG, "Applying input config to %s", + sway_device->input_device->identifier); + if (ic->mapped_output) { + struct sway_container *output = NULL; + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *_output = root_container.children->items[i]; + if (strcasecmp(_output->name, ic->mapped_output) == 0) { + output = _output; + break; + } + } + if (output) { + wlr_cursor_map_input_to_output(seat->cursor->cursor, + sway_device->input_device->wlr_device, + output->sway_output->wlr_output); + wlr_log(L_DEBUG, "Mapped to output %s", output->name); + } + } +} + static void seat_configure_pointer(struct sway_seat *seat, struct sway_seat_device *sway_device) { wlr_cursor_attach_input_device(seat->cursor->cursor, sway_device->input_device->wlr_device); + seat_apply_input_config(seat, sway_device); } static void seat_configure_keyboard(struct sway_seat *seat, @@ -249,6 +278,7 @@ static void seat_configure_tablet_tool(struct sway_seat *seat, struct sway_seat_device *sway_device) { wlr_cursor_attach_input_device(seat->cursor->cursor, sway_device->input_device->wlr_device); + seat_apply_input_config(seat, sway_device); } static struct sway_seat_device *seat_get_device(struct sway_seat *seat, diff --git a/sway/meson.build b/sway/meson.build index 2521069f..d0730296 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -85,6 +85,7 @@ sway_sources = files( 'commands/input/dwt.c', 'commands/input/events.c', 'commands/input/left_handed.c', + 'commands/input/map_to_output.c', 'commands/input/middle_emulation.c', 'commands/input/natural_scroll.c', 'commands/input/pointer_accel.c', diff --git a/sway/sway-input.5.txt b/sway/sway-input.5.txt index 0603616b..05725360 100644 --- a/sway/sway-input.5.txt +++ b/sway/sway-input.5.txt @@ -40,6 +40,26 @@ For more information on these xkb configuration options, see **input** xkb_variant :: Sets the variant of the keyboard like _dvorak_ or _colemak_. +Mapping Configuration +--------------------- + +**input** map_to_output :: + Maps inputs from this device to the specified output. Only meaningful if the + device is a pointer, touch, or drawing tablet device. + +**input** map_to_region :: + Maps inputs from this device to the specified region of the global output + layout. Only meaningful if the device is a pointer, touch, or drawing tablet + device. + +**input** map_region :: + Ignores inputs from this device that do not occur within the specified region. + Can be in millimeters (e.g. 10mmx20mm\@10mm,20mm) or in terms of 0..1 (e.g. + 0.5x0.5\@0,0). Not all devices support millimeters. Only meaningful if the + device is not a keyboard an provides events in absolute terms (such as a + drawing tablet or touch screen - most pointers provide events relative to the + previous frame). + Libinput Configuration ~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.2.3 From 63b2543749e57e4340f539a2742fd13e89d96d94 Mon Sep 17 00:00:00 2001 From: db Date: Sun, 8 Apr 2018 22:49:06 +0200 Subject: Add workspace_auto_back_and_forth for move cmd --- sway/commands/move.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/move.c b/sway/commands/move.c index 15a5ebc4..a5273ba4 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE 500 #include #include #include @@ -63,18 +64,29 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, "Can only move containers and views."); } struct sway_container *ws; - const char *num_name = NULL; char *ws_name = NULL; if (argc == 5 && strcasecmp(argv[3], "number") == 0) { // move "container to workspace number x" - num_name = argv[4]; - ws = workspace_by_number(num_name); + ws_name = strdup(argv[4]); + ws = workspace_by_number(ws_name); } else { ws_name = join_args(argv + 3, argc - 3); ws = workspace_by_name(ws_name); } + + if (config->auto_back_and_forth && prev_workspace_name) { + // auto back and forth move + struct sway_container *curr_ws = container_parent(current, C_WORKSPACE); + if (curr_ws->name && strcmp(curr_ws->name, ws_name) == 0) { + // if target workspace is the current one + free(ws_name); + ws_name = strdup(prev_workspace_name); + ws = workspace_by_name(ws_name); + } + } + if (!ws) { - ws = workspace_create(NULL, ws_name ? ws_name : num_name); + ws = workspace_create(NULL, ws_name); } free(ws_name); struct sway_container *old_parent = current->parent; -- cgit v1.2.3 From 1edb2bd89254069066bab51d316962e85d4445b4 Mon Sep 17 00:00:00 2001 From: Danny Bautista Date: Tue, 10 Apr 2018 11:32:37 -0400 Subject: Implement cursor event simulation with sway commands. --- include/sway/commands.h | 3 +- include/sway/input/cursor.h | 1 + sway/commands.c | 1 + sway/commands/seat.c | 2 ++ sway/commands/seat/cursor.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ sway/input/cursor.c | 2 +- sway/meson.build | 1 + 7 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 sway/commands/seat/cursor.c (limited to 'sway/commands') diff --git a/include/sway/commands.h b/include/sway/commands.h index bc5d5412..dbebaa49 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -204,8 +204,9 @@ sway_cmd input_cmd_xkb_options; sway_cmd input_cmd_xkb_rules; sway_cmd input_cmd_xkb_variant; -sway_cmd seat_cmd_fallback; sway_cmd seat_cmd_attach; +sway_cmd seat_cmd_fallback; +sway_cmd seat_cmd_cursor; sway_cmd cmd_ipc_cmd; sway_cmd cmd_ipc_events; diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h index daf7d4ee..30169aa1 100644 --- a/include/sway/input/cursor.h +++ b/include/sway/input/cursor.h @@ -30,5 +30,6 @@ struct sway_cursor { void sway_cursor_destroy(struct sway_cursor *cursor); struct sway_cursor *sway_cursor_create(struct sway_seat *seat); void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time); +void dispatch_cursor_button(struct sway_cursor *cursor, uint32_t time_msec, uint32_t button, enum wlr_button_state state); #endif diff --git a/sway/commands.c b/sway/commands.c index 55929659..54d84450 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -203,6 +203,7 @@ static struct cmd_handler input_handlers[] = { // must be in order for the bsearch static struct cmd_handler seat_handlers[] = { { "attach", seat_cmd_attach }, + { "cursor", seat_cmd_cursor }, { "fallback", seat_cmd_fallback }, }; diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 819b769c..5916015f 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -40,6 +40,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { struct cmd_results *res; if (strcasecmp("attach", argv[1]) == 0) { res = seat_cmd_attach(argc_new, argv_new); + } else if (strcasecmp("cursor", argv[1]) == 0) { + res = seat_cmd_cursor(argc_new, argv_new); } else if (strcasecmp("fallback", argv[1]) == 0) { res = seat_cmd_fallback(argc_new, argv_new); } else { diff --git a/sway/commands/seat/cursor.c b/sway/commands/seat/cursor.c new file mode 100644 index 00000000..2f2aa5ca --- /dev/null +++ b/sway/commands/seat/cursor.c @@ -0,0 +1,83 @@ +#define _XOPEN_SOURCE 700 +#ifdef __linux__ +#include +#elif __FreeBSD__ +#include +#endif + +#include +#include +#include "sway/commands.h" +#include "sway/input/cursor.h" + +static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *action, char *button_str); + +static const char *expected_syntax = "Expected 'cursor ' or " + "'cursor ' or " + "'curor '"; + +struct cmd_results *seat_cmd_cursor(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "cursor", EXPECTED_AT_LEAST, 2))) { + return error; + } + struct sway_seat *seat = config->handler_context.seat; + if (!seat) { + return cmd_results_new(CMD_FAILURE, "cursor", "No seat defined"); + } + + struct sway_cursor *cursor = seat->cursor; + + if (strcasecmp(argv[0], "move") == 0) { + if (argc < 3) { + return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); + } + int delta_x = strtol(argv[1], NULL, 10); + int delta_y = strtol(argv[2], NULL, 10); + wlr_cursor_move(cursor->cursor, NULL, delta_x, delta_y); + cursor_send_pointer_motion(cursor, 1); + } else if (strcasecmp(argv[0], "set") == 0) { + if (argc < 3) { + return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); + } + // map absolute coords (0..1,0..1) to root container coords + float x = strtof(argv[1], NULL) / root_container.width; + float y = strtof(argv[2], NULL) / root_container.height; + wlr_cursor_warp_absolute(cursor->cursor, NULL, x, y); + cursor_send_pointer_motion(cursor, 0); + } else { + if (argc < 2) { + return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); + } + if ((error = press_or_release(cursor, argv[0], argv[1]))) { + return error; + } + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *action, char *button_str) { + enum wlr_button_state state; + uint32_t button; + if (strcasecmp(action, "press") == 0) { + state = WLR_BUTTON_PRESSED; + } else if (strcasecmp(action, "release") == 0) { + state = WLR_BUTTON_RELEASED; + } else { + return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); + } + + if (strcasecmp(button_str, "left") == 0) { + button = BTN_LEFT; + } else if (strcasecmp(button_str, "right") == 0) { + button = BTN_RIGHT; + } else { + button = strtol(button_str, NULL, 10); + if (button == 0) { + return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); + } + } + dispatch_cursor_button(cursor, 1, button, state); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 0df01504..15a61cbf 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -166,7 +166,7 @@ static void handle_cursor_motion_absolute( cursor_send_pointer_motion(cursor, event->time_msec); } -static void dispatch_cursor_button(struct sway_cursor *cursor, +void dispatch_cursor_button(struct sway_cursor *cursor, uint32_t time_msec, uint32_t button, enum wlr_button_state state) { struct wlr_surface *surface = NULL; double sx, sy; diff --git a/sway/meson.build b/sway/meson.build index d0730296..9e55e335 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -47,6 +47,7 @@ sway_sources = files( 'commands/resize.c', 'commands/seat.c', 'commands/seat/attach.c', + 'commands/seat/cursor.c', 'commands/seat/fallback.c', 'commands/set.c', 'commands/split.c', -- cgit v1.2.3 From c355d680e9a009e09e027a79b27ad21763fa67ea Mon Sep 17 00:00:00 2001 From: Danny Bautista Date: Tue, 10 Apr 2018 15:40:27 -0400 Subject: Clean up cursor simulation code. --- include/sway/input/cursor.h | 3 ++- sway/commands/seat/cursor.c | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'sway/commands') diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h index 30169aa1..fcd94437 100644 --- a/include/sway/input/cursor.h +++ b/include/sway/input/cursor.h @@ -30,6 +30,7 @@ struct sway_cursor { void sway_cursor_destroy(struct sway_cursor *cursor); struct sway_cursor *sway_cursor_create(struct sway_seat *seat); void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time); -void dispatch_cursor_button(struct sway_cursor *cursor, uint32_t time_msec, uint32_t button, enum wlr_button_state state); +void dispatch_cursor_button(struct sway_cursor *cursor, uint32_t time_msec, + uint32_t button, enum wlr_button_state state); #endif diff --git a/sway/commands/seat/cursor.c b/sway/commands/seat/cursor.c index 2f2aa5ca..5dad97f1 100644 --- a/sway/commands/seat/cursor.c +++ b/sway/commands/seat/cursor.c @@ -10,7 +10,8 @@ #include "sway/commands.h" #include "sway/input/cursor.h" -static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *action, char *button_str); +static struct cmd_results *press_or_release(struct sway_cursor *cursor, + char *action, char *button_str, uint32_t time); static const char *expected_syntax = "Expected 'cursor ' or " "'cursor ' or " @@ -28,6 +29,10 @@ struct cmd_results *seat_cmd_cursor(int argc, char **argv) { struct sway_cursor *cursor = seat->cursor; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + uint32_t time = now.tv_nsec / 1000; + if (strcasecmp(argv[0], "move") == 0) { if (argc < 3) { return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); @@ -35,7 +40,7 @@ struct cmd_results *seat_cmd_cursor(int argc, char **argv) { int delta_x = strtol(argv[1], NULL, 10); int delta_y = strtol(argv[2], NULL, 10); wlr_cursor_move(cursor->cursor, NULL, delta_x, delta_y); - cursor_send_pointer_motion(cursor, 1); + cursor_send_pointer_motion(cursor, time); } else if (strcasecmp(argv[0], "set") == 0) { if (argc < 3) { return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); @@ -44,12 +49,12 @@ struct cmd_results *seat_cmd_cursor(int argc, char **argv) { float x = strtof(argv[1], NULL) / root_container.width; float y = strtof(argv[2], NULL) / root_container.height; wlr_cursor_warp_absolute(cursor->cursor, NULL, x, y); - cursor_send_pointer_motion(cursor, 0); + cursor_send_pointer_motion(cursor, time); } else { if (argc < 2) { return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); } - if ((error = press_or_release(cursor, argv[0], argv[1]))) { + if ((error = press_or_release(cursor, argv[0], argv[1], time))) { return error; } } @@ -57,7 +62,8 @@ struct cmd_results *seat_cmd_cursor(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *action, char *button_str) { +static struct cmd_results *press_or_release(struct sway_cursor *cursor, + char *action, char *button_str, uint32_t time) { enum wlr_button_state state; uint32_t button; if (strcasecmp(action, "press") == 0) { @@ -78,6 +84,6 @@ static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *ac return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); } } - dispatch_cursor_button(cursor, 1, button, state); + dispatch_cursor_button(cursor, time, button, state); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3