tony / oxwm

updated for scrolling with gestures, and size of windows in scrolling layout. Browse files
tonybanters committed 2026-09-20 parent 8a50a1f commit b419fcd74069c1862f3e26c264069159605a9663
Showing 16 changed files with 730 additions and 131 deletions. UnifiedSplit
+4 −2 README.md
@@ -55,7 +55,7 @@ This will automatically put a desktop session file into your xsessions directory
55 Manually, install the dependencies and see Building from Source: 55 Manually, install the dependencies and see Building from Source:
56 56
57 ```sh 57 ```sh
58 -sudo pacman -S zig libx11 libxft freetype2 fontconfig libxinerama 58 +sudo pacman -S zig libx11 libxft freetype2 fontconfig libxinerama libinput systemd-libs
59 ``` 59 ```
60 60
61 ### Building from Source 61 ### Building from Source
@@ -76,6 +76,8 @@ cd oxwm
76 zig build -Doptimize=ReleaseSmall --prefix /usr 76 zig build -Doptimize=ReleaseSmall --prefix /usr
77 ``` 77 ```
78 78
79 +Touchpad swipe gestures for the scrolling layout use libinput and libudev, which only exist on Linux. They are built in by default and skipped automatically on BSD targets. Pass `-Dno_gestures` to leave them out anywhere, or use `zig build openbsd` (also `freebsd`, `netbsd`, `bsd`) which does the same and installs the result.
80 +
79 ### Setting up OXWM 81 ### Setting up OXWM
80 82
81 #### Without a display manager (startx) 83 #### Without a display manager (startx)
@@ -221,7 +223,7 @@ Default keybindings (fully customizable in `~/.config/oxwm/config.lua`):
221 - Normie (floating-by-default) 223 - Normie (floating-by-default)
222 - Monocle (fullscreen stacking) 224 - Monocle (fullscreen stacking)
223 - Grid (equal-sized grid) 225 - Grid (equal-sized grid)
224 - - Scrolling (horizontal scroll) 226 + - Scrolling (niri-style horizontal strip with per-window widths and three-finger touchpad swipes)
225 - Dwindle (fibonacci spiral) 227 - Dwindle (fibonacci spiral)
226 - **Configurable Attach Method** for new windows: aside, top, bottom, above, or below 228 - **Configurable Attach Method** for new windows: aside, top, bottom, above, or below
227 - **Lua Configuration System** 229 - **Lua Configuration System**
+65 −24 build.zig
@@ -7,36 +7,28 @@ pub fn build(b: *std.Build) void {
7 const lua_dep = b.dependency("lua", .{}); 7 const lua_dep = b.dependency("lua", .{});
8 const lua_headers = lua_dep.path("src/"); 8 const lua_headers = lua_dep.path("src/");
9 9
10 - const exe = b.addExecutable(.{ 10 + const no_gestures = b.option(bool, "no_gestures", "Build without libinput touchpad gestures (implied on BSD targets)") orelse false;
11 - .name = "oxwm", 11 + const gestures = !no_gestures and !isBsd(target.result.os.tag);
12 - .root_module = b.createModule(.{
13 - .root_source_file = b.path("src/main.zig"),
14 - .target = target,
15 - .optimize = optimize,
16 - .link_libc = true,
17 - }),
18 - });
19 -
20 - const exe_options = b.addOptions();
21 - exe_options.addOption([]const u8, "version", zon.version);
22 - exe.root_module.addOptions("build_options", exe_options);
23 -
24 - exe.root_module.addAnonymousImport("templates/config.lua", .{
25 - .root_source_file = b.path("templates/config.lua"),
26 - });
27 -
28 - exe.use_lld = false;
29 12
30 const lua = buildLua(b, lua_dep, target, optimize); 13 const lua = buildLua(b, lua_dep, target, optimize);
31 14
32 - exe.root_module.linkLibrary(lua); 15 + const exe_options = b.addOptions();
33 - exe.root_module.linkSystemLibrary("X11", .{}); 16 + exe_options.addOption([]const u8, "version", zon.version);
34 - exe.root_module.linkSystemLibrary("Xinerama", .{}); 17 + exe_options.addOption(bool, "gestures", gestures);
35 - exe.root_module.linkSystemLibrary("Xft", .{});
36 - exe.root_module.linkSystemLibrary("fontconfig", .{});
37 18
19 + const exe = addOxwm(b, target, optimize, lua, exe_options, gestures);
38 b.installArtifact(exe); 20 b.installArtifact(exe);
39 21
22 + const bsd_options = b.addOptions();
23 + bsd_options.addOption([]const u8, "version", zon.version);
24 + bsd_options.addOption(bool, "gestures", false);
25 + const bsd_exe = addOxwm(b, target, optimize, lua, bsd_options, false);
26 + const bsd_install = b.addInstallArtifact(bsd_exe, .{});
27 + for ([_][]const u8{ "openbsd", "freebsd", "netbsd", "bsd" }) |name| {
28 + const bsd_step = b.step(name, "Build oxwm without Linux-only features (libinput gestures)");
29 + bsd_step.dependOn(&bsd_install.step);
30 + }
31 +
40 const run_step = b.step("run", "Run oxwm"); 32 const run_step = b.step("run", "Run oxwm");
41 const run_cmd = b.addRunArtifact(exe); 33 const run_cmd = b.addRunArtifact(exe);
42 run_step.dependOn(&run_cmd.step); 34 run_step.dependOn(&run_cmd.step);
@@ -71,6 +63,11 @@ pub fn build(b: *std.Build) void {
71 src_main_unit_tests.root_module.linkSystemLibrary("Xinerama", .{}); 63 src_main_unit_tests.root_module.linkSystemLibrary("Xinerama", .{});
72 src_main_unit_tests.root_module.linkSystemLibrary("Xft", .{}); 64 src_main_unit_tests.root_module.linkSystemLibrary("Xft", .{});
73 src_main_unit_tests.root_module.linkSystemLibrary("fontconfig", .{}); 65 src_main_unit_tests.root_module.linkSystemLibrary("fontconfig", .{});
66 + src_main_unit_tests.root_module.addOptions("build_options", exe_options);
67 + if (gestures) {
68 + src_main_unit_tests.root_module.linkSystemLibrary("libinput", .{});
69 + src_main_unit_tests.root_module.linkSystemLibrary("libudev", .{});
70 + }
74 test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step); 71 test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step);
75 72
76 const lua_config_tests = b.addTest(.{ 73 const lua_config_tests = b.addTest(.{
@@ -138,6 +135,50 @@ pub fn build(b: *std.Build) void {
138 }).step); 135 }).step);
139 } 136 }
140 137
138 +fn isBsd(tag: std.Target.Os.Tag) bool {
139 + return switch (tag) {
140 + .freebsd, .openbsd, .netbsd, .dragonfly => true,
141 + else => false,
142 + };
143 +}
144 +
145 +fn addOxwm(
146 + b: *std.Build,
147 + target: std.Build.ResolvedTarget,
148 + optimize: std.builtin.OptimizeMode,
149 + lua: *std.Build.Step.Compile,
150 + options: *std.Build.Step.Options,
151 + gestures: bool,
152 +) *std.Build.Step.Compile {
153 + const exe = b.addExecutable(.{
154 + .name = "oxwm",
155 + .root_module = b.createModule(.{
156 + .root_source_file = b.path("src/main.zig"),
157 + .target = target,
158 + .optimize = optimize,
159 + .link_libc = true,
160 + }),
161 + });
162 +
163 + exe.root_module.addOptions("build_options", options);
164 + exe.root_module.addAnonymousImport("templates/config.lua", .{
165 + .root_source_file = b.path("templates/config.lua"),
166 + });
167 +
168 + exe.use_lld = false;
169 +
170 + exe.root_module.linkLibrary(lua);
171 + exe.root_module.linkSystemLibrary("X11", .{});
172 + exe.root_module.linkSystemLibrary("Xinerama", .{});
173 + exe.root_module.linkSystemLibrary("Xft", .{});
174 + exe.root_module.linkSystemLibrary("fontconfig", .{});
175 + if (gestures) {
176 + exe.root_module.linkSystemLibrary("libinput", .{});
177 + exe.root_module.linkSystemLibrary("libudev", .{});
178 + }
179 + return exe;
180 +}
181 +
141 fn addXephyrRun(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run { 182 fn addXephyrRun(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run {
142 const kill_cmd = if (multimon) 183 const kill_cmd = if (multimon)
143 "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1" 184 "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1"
+4 −0 default.nix
@@ -10,6 +10,8 @@
10 lua5_4, 10 lua5_4,
11 freetype, 11 freetype,
12 fontconfig, 12 fontconfig,
13 + libinput,
14 + udev,
13 gitRev ? "unknown", 15 gitRev ? "unknown",
14 }: 16 }:
15 stdenv.mkDerivation (finalAttrs: { 17 stdenv.mkDerivation (finalAttrs: {
@@ -29,6 +31,8 @@ stdenv.mkDerivation (finalAttrs: {
29 lua5_4 31 lua5_4
30 freetype 32 freetype
31 fontconfig 33 fontconfig
34 + libinput
35 + udev
32 ]; 36 ];
33 37
34 postInstall = '' 38 postInstall = ''
+1 −1 resources/PKGBUILD
@@ -7,7 +7,7 @@ arch=('x86_64')
7 url="https://github.com/tonybanters/oxwm" 7 url="https://github.com/tonybanters/oxwm"
8 pkgdesc="DWM but better with sane defaults, and not suckless." 8 pkgdesc="DWM but better with sane defaults, and not suckless."
9 license=('GPL-3.0-or-later') 9 license=('GPL-3.0-or-later')
10 -depends=('libx11' 'libxft' 'libxcb' 'fontconfig' 'freetype2' 'libxrender') 10 +depends=('libx11' 'libxft' 'libxcb' 'fontconfig' 'freetype2' 'libxrender' 'libinput' 'systemd-libs')
11 makedepends=('cargo' 'git') 11 makedepends=('cargo' 'git')
12 provides=('oxwm') 12 provides=('oxwm')
13 conflicts=('oxwm') 13 conflicts=('oxwm')
+45 −0 src/animations.zig
@@ -22,6 +22,13 @@ pub const AnimationConfig = struct {
22 easing: Easing = .ease_out, 22 easing: Easing = .ease_out,
23 }; 23 };
24 24
25 +pub const SpringConfig = struct {
26 + stiffness: f64 = 800.0,
27 + damping_ratio: f64 = 1.0,
28 + epsilon: f64 = 0.5,
29 + max_duration_ms: i64 = 2000,
30 +};
31 +
25 pub const ScrollAnimation = struct { 32 pub const ScrollAnimation = struct {
26 start_value: i32 = 0, 33 start_value: i32 = 0,
27 end_value: i32 = 0, 34 end_value: i32 = 0,
@@ -29,6 +36,9 @@ pub const ScrollAnimation = struct {
29 duration_ms: u64 = 150, 36 duration_ms: u64 = 150,
30 easing: Easing = .ease_out, 37 easing: Easing = .ease_out,
31 active: bool = false, 38 active: bool = false,
39 + spring: bool = false,
40 + velocity: f64 = 0,
41 + spring_config: SpringConfig = .{},
32 42
33 pub fn start(self: *ScrollAnimation, io: std.Io, from: i32, to: i32, config: AnimationConfig) void { 43 pub fn start(self: *ScrollAnimation, io: std.Io, from: i32, to: i32, config: AnimationConfig) void {
34 if (from == to) { 44 if (from == to) {
@@ -40,15 +50,50 @@ pub const ScrollAnimation = struct {
40 self.start_time = std.Io.Timestamp.now(io, .awake).toMilliseconds(); 50 self.start_time = std.Io.Timestamp.now(io, .awake).toMilliseconds();
41 self.duration_ms = config.duration_ms; 51 self.duration_ms = config.duration_ms;
42 self.easing = config.easing; 52 self.easing = config.easing;
53 + self.spring = false;
54 + self.active = true;
55 + }
56 +
57 + /// Starts a critically damped spring from `from` to `to` that carries
58 + /// the initial `velocity` (in pixels per second) into the motion.
59 + pub fn startSpring(self: *ScrollAnimation, io: std.Io, from: i32, to: i32, velocity: f64, config: SpringConfig) void {
60 + if (from == to) {
61 + self.active = false;
62 + return;
63 + }
64 + self.start_value = from;
65 + self.end_value = to;
66 + self.start_time = std.Io.Timestamp.now(io, .awake).toMilliseconds();
67 + self.velocity = velocity;
68 + self.spring_config = config;
69 + self.spring = true;
43 self.active = true; 70 self.active = true;
44 } 71 }
45 72
73 + fn springPosition(self: *const ScrollAnimation, elapsed_ms: i64) f64 {
74 + const t = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0;
75 + const beta = self.spring_config.damping_ratio * @sqrt(self.spring_config.stiffness);
76 + const to: f64 = @floatFromInt(self.end_value);
77 + const x0 = @as(f64, @floatFromInt(self.start_value)) - to;
78 + return to + @exp(-beta * t) * (x0 + (beta * x0 + self.velocity) * t);
79 + }
80 +
46 pub fn update(self: *ScrollAnimation, io: std.Io) ?i32 { 81 pub fn update(self: *ScrollAnimation, io: std.Io) ?i32 {
47 if (!self.active) return null; 82 if (!self.active) return null;
48 83
49 const now = std.Io.Timestamp.now(io, .awake).toMilliseconds(); 84 const now = std.Io.Timestamp.now(io, .awake).toMilliseconds();
50 const elapsed = now - self.start_time; 85 const elapsed = now - self.start_time;
51 86
87 + if (self.spring) {
88 + const pos = self.springPosition(elapsed);
89 + const settled = @abs(pos - @as(f64, @floatFromInt(self.end_value))) < self.spring_config.epsilon;
90 + if (settled or elapsed > self.spring_config.max_duration_ms) {
91 + self.active = false;
92 + return self.end_value;
93 + }
94 + return @intFromFloat(pos);
95 + }
96 +
52 if (elapsed >= @as(i64, @intCast(self.duration_ms))) { 97 if (elapsed >= @as(i64, @intCast(self.duration_ms))) {
53 self.active = false; 98 self.active = false;
54 return self.end_value; 99 return self.end_value;
+1 −0 src/client.zig
@@ -33,6 +33,7 @@ pub const Client = struct {
33 never_focus: bool = false, 33 never_focus: bool = false,
34 old_state: bool = false, 34 old_state: bool = false,
35 is_fullscreen: bool = false, 35 is_fullscreen: bool = false,
36 + scroll_width: f32 = 0,
36 next: ?*Client = null, 37 next: ?*Client = null,
37 stack_next: ?*Client = null, 38 stack_next: ?*Client = null,
38 monitor: ?*Monitor = null, 39 monitor: ?*Monitor = null,
+11 −0 src/config/config.zig
@@ -35,6 +35,8 @@ pub const Action = enum {
35 send_to_monitor, 35 send_to_monitor,
36 scroll_left, 36 scroll_left,
37 scroll_right, 37 scroll_right,
38 + cycle_width,
39 + set_width,
38 }; 40 };
39 41
40 pub const KeyPress = struct { 42 pub const KeyPress = struct {
@@ -58,6 +60,7 @@ pub const Rule = struct {
58 is_floating: bool, 60 is_floating: bool,
59 monitor: i32, 61 monitor: i32,
60 focus: bool, 62 focus: bool,
63 + width: f32 = 0,
61 }; 64 };
62 65
63 pub const BlockType = enum { 66 pub const BlockType = enum {
@@ -222,6 +225,14 @@ pub const Config = struct {
222 warp_cursor_to_monitor: bool = false, 225 warp_cursor_to_monitor: bool = false,
223 warp_cursor_on_send: bool = false, 226 warp_cursor_on_send: bool = false,
224 227
228 + scroll_default_width: f32 = 0.5,
229 + scroll_width_presets: [8]f32 = .{ 1.0 / 3.0, 0.5, 2.0 / 3.0, 0, 0, 0, 0, 0 },
230 + scroll_width_preset_count: u32 = 3,
231 + gesture_enabled: bool = true,
232 + gesture_fingers: u32 = 3,
233 + gesture_speed: f32 = 2.0,
234 + gesture_natural: bool = false,
235 +
225 layout_tile_symbol: []const u8 = "[]=", 236 layout_tile_symbol: []const u8 = "[]=",
226 layout_monocle_symbol: []const u8 = "[M]", 237 layout_monocle_symbol: []const u8 = "[M]",
227 layout_floating_symbol: []const u8 = "><>", 238 layout_floating_symbol: []const u8 = "><>",
+119 −0 src/config/lua.zig
@@ -89,6 +89,7 @@ fn registerApi() void {
89 registerBorderModule(state); 89 registerBorderModule(state);
90 registerClientModule(state); 90 registerClientModule(state);
91 registerLayoutModule(state); 91 registerLayoutModule(state);
92 + registerScrollModule(state);
92 registerTagModule(state); 93 registerTagModule(state);
93 registerMonitorModule(state); 94 registerMonitorModule(state);
94 registerRuleModule(state); 95 registerRuleModule(state);
@@ -193,9 +194,39 @@ fn registerLayoutModule(state: *c.lua_State) void {
193 c.lua_pushcfunction(state, luaLayoutScrollRight); 194 c.lua_pushcfunction(state, luaLayoutScrollRight);
194 c.lua_setfield(state, -2, "scroll_right"); 195 c.lua_setfield(state, -2, "scroll_right");
195 196
197 + c.lua_pushcfunction(state, luaLayoutCycleWidth);
198 + c.lua_setfield(state, -2, "cycle_width");
199 +
200 + c.lua_pushcfunction(state, luaLayoutSetWidth);
201 + c.lua_setfield(state, -2, "set_width");
202 +
196 c.lua_setfield(state, -2, "layout"); 203 c.lua_setfield(state, -2, "layout");
197 } 204 }
198 205
206 +fn registerScrollModule(state: *c.lua_State) void {
207 + c.lua_createtable(state, 0, 6);
208 +
209 + c.lua_pushcfunction(state, luaScrollSetDefaultWidth);
210 + c.lua_setfield(state, -2, "set_default_width");
211 +
212 + c.lua_pushcfunction(state, luaScrollSetWidthPresets);
213 + c.lua_setfield(state, -2, "set_width_presets");
214 +
215 + c.lua_pushcfunction(state, luaScrollSetGestureEnabled);
216 + c.lua_setfield(state, -2, "set_gesture_enabled");
217 +
218 + c.lua_pushcfunction(state, luaScrollSetGestureFingers);
219 + c.lua_setfield(state, -2, "set_gesture_fingers");
220 +
221 + c.lua_pushcfunction(state, luaScrollSetGestureSpeed);
222 + c.lua_setfield(state, -2, "set_gesture_speed");
223 +
224 + c.lua_pushcfunction(state, luaScrollSetGestureNatural);
225 + c.lua_setfield(state, -2, "set_gesture_natural");
226 +
227 + c.lua_setfield(state, -2, "scroll");
228 +}
229 +
199 fn registerTagModule(state: *c.lua_State) void { 230 fn registerTagModule(state: *c.lua_State) void {
200 c.lua_createtable(state, 0, 10); 231 c.lua_createtable(state, 0, 10);
201 232
@@ -639,6 +670,85 @@ fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int {
639 return 1; 670 return 1;
640 } 671 }
641 672
673 +fn luaLayoutCycleWidth(state: ?*c.lua_State) callconv(.c) c_int {
674 + const s = state orelse return 0;
675 + var isnum: c_int = 0;
676 + const raw = c.lua_tointegerx(s, 1, &isnum);
677 + const dir: i32 = if (isnum != 0) @intCast(raw) else 1;
678 + createActionTableWithInt(s, "CycleWidth", dir);
679 + return 1;
680 +}
681 +
682 +fn luaLayoutSetWidth(state: ?*c.lua_State) callconv(.c) c_int {
683 + const s = state orelse return 0;
684 + var isnum: c_int = 0;
685 + const value = c.lua_tonumberx(s, 1, &isnum);
686 + const permille: i32 = if (isnum != 0 and value > 0) @intFromFloat(@round(value * 1000.0)) else 0;
687 + createActionTableWithInt(s, "SetWidth", permille);
688 + return 1;
689 +}
690 +
691 +fn luaScrollSetDefaultWidth(state: ?*c.lua_State) callconv(.c) c_int {
692 + const cfg = config orelse return 0;
693 + const s = state orelse return 0;
694 + var isnum: c_int = 0;
695 + const value = c.lua_tonumberx(s, 1, &isnum);
696 + if (isnum != 0 and value > 0) cfg.scroll_default_width = @floatCast(value);
697 + return 0;
698 +}
699 +
700 +fn luaScrollSetWidthPresets(state: ?*c.lua_State) callconv(.c) c_int {
701 + const cfg = config orelse return 0;
702 + const s = state orelse return 0;
703 + if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
704 +
705 + const len = c.lua_rawlen(s, 1);
706 + var count: u32 = 0;
707 + var i: c_longlong = 1;
708 + while (i <= len and count < cfg.scroll_width_presets.len) : (i += 1) {
709 + _ = c.lua_rawgeti(s, 1, i);
710 + var isnum: c_int = 0;
711 + const value = c.lua_tonumberx(s, -1, &isnum);
712 + c.lua_settop(s, -2);
713 + if (isnum == 0 or value <= 0) continue;
714 + cfg.scroll_width_presets[count] = @floatCast(value);
715 + count += 1;
716 + }
717 + if (count > 0) cfg.scroll_width_preset_count = count;
718 + return 0;
719 +}
720 +
721 +fn luaScrollSetGestureEnabled(state: ?*c.lua_State) callconv(.c) c_int {
722 + const cfg = config orelse return 0;
723 + const s = state orelse return 0;
724 + cfg.gesture_enabled = c.lua_toboolean(s, 1) != 0;
725 + return 0;
726 +}
727 +
728 +fn luaScrollSetGestureFingers(state: ?*c.lua_State) callconv(.c) c_int {
729 + const cfg = config orelse return 0;
730 + const s = state orelse return 0;
731 + const value = c.lua_tointegerx(s, 1, null);
732 + if (value >= 3 and value <= 5) cfg.gesture_fingers = @intCast(value);
733 + return 0;
734 +}
735 +
736 +fn luaScrollSetGestureSpeed(state: ?*c.lua_State) callconv(.c) c_int {
737 + const cfg = config orelse return 0;
738 + const s = state orelse return 0;
739 + var isnum: c_int = 0;
740 + const value = c.lua_tonumberx(s, 1, &isnum);
741 + if (isnum != 0 and value > 0) cfg.gesture_speed = @floatCast(value);
742 + return 0;
743 +}
744 +
745 +fn luaScrollSetGestureNatural(state: ?*c.lua_State) callconv(.c) c_int {
746 + const cfg = config orelse return 0;
747 + const s = state orelse return 0;
748 + cfg.gesture_natural = c.lua_toboolean(s, 1) != 0;
749 + return 0;
750 +}
751 +
642 fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int { 752 fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int {
643 const s = state orelse return 0; 753 const s = state orelse return 0;
644 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); 754 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
@@ -787,6 +897,13 @@ fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int {
787 } 897 }
788 c.lua_settop(s, -2); 898 c.lua_settop(s, -2);
789 899
900 + _ = c.lua_getfield(s, 1, "width");
901 + if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
902 + const width = c.lua_tonumberx(s, -1, null);
903 + if (width > 0) rule.width = @floatCast(width);
904 + }
905 + c.lua_settop(s, -2);
906 +
790 cfg.addRule(rule) catch return 0; 907 cfg.addRule(rule) catch return 0;
791 return 0; 908 return 0;
792 } 909 }
@@ -1421,6 +1538,8 @@ fn parseAction(name: []const u8) ?Action {
1421 .{ "TagMonitor", Action.send_to_monitor }, 1538 .{ "TagMonitor", Action.send_to_monitor },
1422 .{ "ScrollLeft", Action.scroll_left }, 1539 .{ "ScrollLeft", Action.scroll_left },
1423 .{ "ScrollRight", Action.scroll_right }, 1540 .{ "ScrollRight", Action.scroll_right },
1541 + .{ "CycleWidth", Action.cycle_width },
1542 + .{ "SetWidth", Action.set_width },
1424 }; 1543 };
1425 1544
1426 inline for (action_map) |entry| { 1545 inline for (action_map) |entry| {
+135 −80 src/layouts/scrolling.zig
@@ -1,7 +1,6 @@
1 const std = @import("std"); 1 const std = @import("std");
2 const client_mod = @import("../client.zig"); 2 const client_mod = @import("../client.zig");
3 const monitor_mod = @import("../monitor.zig"); 3 const monitor_mod = @import("../monitor.zig");
4 -const xlib = @import("../x11/xlib.zig");
5 const tiling = @import("tiling.zig"); 4 const tiling = @import("tiling.zig");
6 5
7 const Client = client_mod.Client; 6 const Client = client_mod.Client;
@@ -12,109 +11,165 @@ pub const layout = monitor_mod.Layout{
12 .arrange_fn = scroll, 11 .arrange_fn = scroll,
13 }; 12 };
14 13
15 -pub fn scroll(monitor: *Monitor) void { 14 +pub const Snap = struct {
16 - var client_count: u32 = 0; 15 + client: *Client,
16 + offset: i32,
17 + width: i32,
18 +};
19 +
20 +pub const Geometry = struct {
21 + outer_h: i32,
22 + outer_v: i32,
23 + inner: i32,
24 + available_w: i32,
25 + available_h: i32,
26 + count: u32,
27 +};
28 +
29 +pub fn tiledCount(monitor: *Monitor) u32 {
30 + var count: u32 = 0;
17 var current = client_mod.nextTiled(monitor.clients); 31 var current = client_mod.nextTiled(monitor.clients);
18 - while (current) |_| : (current = client_mod.nextTiled(current.?.next)) { 32 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
19 - client_count += 1; 33 + count += 1;
20 } 34 }
35 + return count;
36 +}
21 37
22 - if (client_count == 0) return; 38 +pub fn geometry(monitor: *Monitor) Geometry {
23 - 39 + const count = tiledCount(monitor);
24 - const gap_outer_h = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_h; 40 + const smart = monitor.smartgaps_enabled and count == 1;
25 - const gap_outer_v = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_v; 41 + const outer_h = if (smart) 0 else monitor.gap_outer_h;
26 - const gap_inner_v = monitor.gap_inner_v; 42 + const outer_v = if (smart) 0 else monitor.gap_outer_v;
43 + return .{
44 + .outer_h = outer_h,
45 + .outer_v = outer_v,
46 + .inner = monitor.gap_inner_v,
47 + .available_w = monitor.win_w - 2 * outer_v,
48 + .available_h = monitor.win_h - 2 * outer_h,
49 + .count = count,
50 + };
51 +}
27 52
28 - const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); 53 +pub fn windowWidth(monitor: *Monitor, client: *Client, geo: Geometry) i32 {
29 - const available_width = monitor.win_w - 2 * gap_outer_v; 54 + const value: f32 = if (client.scroll_width > 0) client.scroll_width else monitor.scroll_default_width;
30 - const available_height = monitor.win_h - 2 * gap_outer_h; 55 + const available: f32 = @floatFromInt(geo.available_w);
56 + const inner: f32 = @floatFromInt(geo.inner);
57 + const width: i32 = if (value > 1.0)
58 + @intFromFloat(value)
59 + else
60 + @intFromFloat((available + inner) * value - inner);
61 + return @max(1, @min(width, geo.available_w));
62 +}
31 63
32 - const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0)); 64 +pub fn contentWidth(monitor: *Monitor) i32 {
33 - const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count))); 65 + const geo = geometry(monitor);
66 + var offset: i32 = 0;
67 + var current = client_mod.nextTiled(monitor.clients);
68 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
69 + offset += windowWidth(monitor, client, geo) + geo.inner;
70 + }
71 + return @max(0, offset - geo.inner);
72 +}
34 73
35 - var x_pos: i32 = monitor.win_x + gap_outer_v - monitor.scroll_offset; 74 +pub fn maxScroll(monitor: *Monitor) i32 {
36 - const y_pos: i32 = monitor.win_y + gap_outer_h; 75 + const geo = geometry(monitor);
37 - const height = available_height; 76 + return @max(0, contentWidth(monitor) - geo.available_w);
77 +}
38 78
39 - var index: u32 = 0; 79 +pub fn snapFor(monitor: *Monitor, target: *Client) ?Snap {
40 - current = client_mod.nextTiled(monitor.clients); 80 + const geo = geometry(monitor);
81 + const max = maxScroll(monitor);
82 + var offset: i32 = 0;
83 + var current = client_mod.nextTiled(monitor.clients);
41 while (current) |client| : (current = client_mod.nextTiled(client.next)) { 84 while (current) |client| : (current = client_mod.nextTiled(client.next)) {
42 - const window_right = x_pos + window_width; 85 + const width = windowWidth(monitor, client, geo);
43 - const screen_left = monitor.win_x; 86 + if (client == target) return .{ .client = client, .offset = @min(offset, max), .width = width };
44 - const screen_right = monitor.win_x + monitor.win_w; 87 + offset += width + geo.inner;
45 - const is_visible = window_right > screen_left and x_pos < screen_right;
46 -
47 - if (is_visible) {
48 - tiling.resize(
49 - client,
50 - x_pos,
51 - y_pos,
52 - window_width - 2 * client.border_width,
53 - height - 2 * client.border_width,
54 - false,
55 - );
56 - } else {
57 - tiling.resizeClient(
58 - client,
59 - -2 * window_width,
60 - y_pos,
61 - window_width - 2 * client.border_width,
62 - height - 2 * client.border_width,
63 - );
64 - }
65 - x_pos += window_width + gap_inner_v;
66 - index += 1;
67 } 88 }
89 + return null;
68 } 90 }
69 91
70 -pub fn getScrollStep(monitor: *Monitor) i32 { 92 +pub fn offsetToReveal(monitor: *Monitor, offset: i32, target: *Client) i32 {
71 - var client_count: u32 = 0; 93 + const geo = geometry(monitor);
94 + const max = maxScroll(monitor);
95 + var left: i32 = 0;
72 var current = client_mod.nextTiled(monitor.clients); 96 var current = client_mod.nextTiled(monitor.clients);
73 - while (current) |_| : (current = client_mod.nextTiled(current.?.next)) { 97 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
74 - client_count += 1; 98 + const width = windowWidth(monitor, client, geo);
99 + if (client == target) {
100 + var result = offset;
101 + if (left < offset) {
102 + result = left;
103 + } else if (left + width > offset + geo.available_w) {
104 + result = left + width - geo.available_w;
105 + }
106 + return @max(0, @min(result, max));
107 + }
108 + left += width + geo.inner;
75 } 109 }
76 - 110 + return @max(0, @min(offset, max));
77 - const gap_outer_v = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_v;
78 - const gap_inner_v = monitor.gap_inner_v;
79 - const visible_count: u32 = @intCast(@max(1, monitor.nmaster));
80 - const available_width = monitor.win_w - 2 * gap_outer_v;
81 - const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0));
82 - const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count)));
83 - return window_width + gap_inner_v;
84 } 111 }
85 112
86 -pub fn getMaxScroll(monitor: *Monitor) i32 { 113 +pub fn nearestSnap(monitor: *Monitor, position: f64) ?Snap {
87 - var client_count: u32 = 0; 114 + const geo = geometry(monitor);
115 + const max = maxScroll(monitor);
116 + var best: ?Snap = null;
117 + var best_dist: f64 = std.math.inf(f64);
118 + var offset: i32 = 0;
88 var current = client_mod.nextTiled(monitor.clients); 119 var current = client_mod.nextTiled(monitor.clients);
89 - while (current) |_| : (current = client_mod.nextTiled(current.?.next)) { 120 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
90 - client_count += 1; 121 + const width = windowWidth(monitor, client, geo);
122 + const snap_offset = @min(offset, max);
123 + const dist = @abs(@as(f64, @floatFromInt(snap_offset)) - position);
124 + if (dist < best_dist) {
125 + best_dist = dist;
126 + best = .{ .client = client, .offset = snap_offset, .width = width };
127 + }
128 + offset += width + geo.inner;
91 } 129 }
92 - 130 + return best;
93 - const visible_count: u32 = @intCast(@max(1, monitor.nmaster));
94 - if (client_count <= visible_count) return 0;
95 -
96 - const scroll_step = getScrollStep(monitor);
97 - const scrollable = client_count - visible_count;
98 - return scroll_step * @as(i32, @intCast(scrollable));
99 } 131 }
100 132
101 -pub fn getWindowIndex(monitor: *Monitor, target: *Client) ?u32 { 133 +pub fn adjacentSnap(monitor: *Monitor, position: i32, direction: i32) ?Snap {
102 - var index: u32 = 0; 134 + const geo = geometry(monitor);
135 + const max = maxScroll(monitor);
136 + var result: ?Snap = null;
137 + var offset: i32 = 0;
103 var current = client_mod.nextTiled(monitor.clients); 138 var current = client_mod.nextTiled(monitor.clients);
104 while (current) |client| : (current = client_mod.nextTiled(client.next)) { 139 while (current) |client| : (current = client_mod.nextTiled(client.next)) {
105 - if (client == target) return index; 140 + const width = windowWidth(monitor, client, geo);
106 - index += 1; 141 + const snap_offset = @min(offset, max);
142 + const snap = Snap{ .client = client, .offset = snap_offset, .width = width };
143 + if (direction > 0) {
144 + if (snap_offset > position) return snap;
145 + } else if (snap_offset < position) {
146 + result = snap;
147 + }
148 + offset += width + geo.inner;
107 } 149 }
108 - return null; 150 + return result;
109 } 151 }
110 152
111 -pub fn getTargetScrollForWindow(monitor: *Monitor, target: *Client) i32 { 153 +pub fn scroll(monitor: *Monitor) void {
112 - const index = getWindowIndex(monitor, target) orelse return 0; 154 + const geo = geometry(monitor);
113 - if (index == 0) return 0; 155 + if (geo.count == 0) return;
156 +
157 + var x_pos: i32 = monitor.win_x + geo.outer_v - monitor.scroll_offset;
158 + const y_pos: i32 = monitor.win_y + geo.outer_h;
159 + const screen_left = monitor.win_x;
160 + const screen_right = monitor.win_x + monitor.win_w;
114 161
115 - const scroll_step = getScrollStep(monitor); 162 + var current = client_mod.nextTiled(monitor.clients);
116 - const max_scroll = getMaxScroll(monitor); 163 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
164 + const width = windowWidth(monitor, client, geo);
165 + const border = 2 * client.border_width;
166 + const is_visible = x_pos + width > screen_left and x_pos < screen_right;
117 167
118 - const target_scroll = scroll_step * @as(i32, @intCast(index)); 168 + if (is_visible) {
119 - return @min(target_scroll, max_scroll); 169 + tiling.resize(client, x_pos, y_pos, width - border, geo.available_h - border, false);
170 + } else {
171 + tiling.resizeClient(client, -2 * (width + border), y_pos, width - border, geo.available_h - border);
172 + }
173 + x_pos += width + geo.inner;
174 + }
120 } 175 }
+1 −1 src/main.zig
@@ -81,7 +81,7 @@ pub fn main(init: std.process.Init) !void {
81 81
82 try runAutostartCommands(&wm); 82 try runAutostartCommands(&wm);
83 std.debug.print("entering event loop\n", .{}); 83 std.debug.print("entering event loop\n", .{});
84 - wm.run(handlers.handleEvent, window_manager.core.tickAnimations); 84 + wm.run(handlers.handleEvent, window_manager.core.tickAnimations, window_manager.core.handleGesture);
85 85
86 lua.deinit(); 86 lua.deinit();
87 std.debug.print("oxwm exiting\n", .{}); 87 std.debug.print("oxwm exiting\n", .{});
+1 −0 src/monitor.zig
@@ -39,6 +39,7 @@ pub const Monitor = struct {
39 gap_outer_v: i32 = 0, 39 gap_outer_v: i32 = 0,
40 smartgaps_enabled: bool = false, 40 smartgaps_enabled: bool = false,
41 scroll_offset: i32 = 0, 41 scroll_offset: i32 = 0,
42 + scroll_default_width: f32 = 0.5,
42 sel_tags: u32 = 0, 43 sel_tags: u32 = 0,
43 sel_lt: u32 = 0, 44 sel_lt: u32 = 0,
44 tagset: [2]u32 = .{ 1, 1 }, 45 tagset: [2]u32 = .{ 1, 1 },
+85 −0 src/wm/actions.zig
@@ -377,8 +377,91 @@ pub fn incnmaster(delta: i32, wm: *WindowManager) void {
377 std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster}); 377 std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
378 } 378 }
379 379
380 +fn focusedScrollClient(wm: *WindowManager) ?*Client {
381 + const monitor = wm.selected_monitor orelse return null;
382 + if (!core.isScrollingLayout(monitor)) return null;
383 + const client = monitor.sel orelse return null;
384 + if (client.is_floating) return null;
385 + return client;
386 +}
387 +
388 +fn effectiveScrollWidth(client: *Client) f32 {
389 + if (client.scroll_width > 0) return client.scroll_width;
390 + if (client.monitor) |monitor| return monitor.scroll_default_width;
391 + return 0.5;
392 +}
393 +
394 +pub fn cycleWidth(direction: i32, wm: *WindowManager) void {
395 + const client = focusedScrollClient(wm) orelse return;
396 + const monitor = client.monitor orelse return;
397 + const count: usize = @intCast(wm.config.scroll_width_preset_count);
398 + if (count == 0) return;
399 + const presets = wm.config.scroll_width_presets[0..count];
400 + const current = effectiveScrollWidth(client);
401 +
402 + var matched: ?usize = null;
403 + for (presets, 0..) |preset, index| {
404 + if (@abs(preset - current) < 0.001) matched = index;
405 + }
406 +
407 + var next: usize = 0;
408 + if (matched) |index| {
409 + const step: i32 = if (direction >= 0) 1 else -1;
410 + next = @intCast(@mod(@as(i32, @intCast(index)) + step, @as(i32, @intCast(count))));
411 + } else if (direction >= 0) {
412 + next = 0;
413 + for (presets, 0..) |preset, index| {
414 + if (preset > current) {
415 + next = index;
416 + break;
417 + }
418 + }
419 + } else {
420 + next = count - 1;
421 + var index: usize = count;
422 + while (index > 0) {
423 + index -= 1;
424 + if (presets[index] < current) {
425 + next = index;
426 + break;
427 + }
428 + }
429 + }
430 +
431 + client.scroll_width = presets[next];
432 + core.arrange(monitor, wm);
433 + std.debug.print("cycle_width: width={d:.3}\n", .{client.scroll_width});
434 +}
435 +
436 +pub fn setWidth(permille: i32, wm: *WindowManager) void {
437 + if (permille <= 0) return;
438 + const client = focusedScrollClient(wm) orelse return;
439 + const monitor = client.monitor orelse return;
440 + client.scroll_width = @as(f32, @floatFromInt(permille)) / 1000.0;
441 + core.arrange(monitor, wm);
442 + std.debug.print("set_width: width={d:.3}\n", .{client.scroll_width});
443 +}
444 +
445 +fn adjustScrollWidth(delta: f32, wm: *WindowManager) void {
446 + const client = focusedScrollClient(wm) orelse return;
447 + const monitor = client.monitor orelse return;
448 + const current = effectiveScrollWidth(client);
449 + if (current > 1.0) {
450 + const available: f32 = @floatFromInt(monitor.win_w - 2 * monitor.gap_outer_v);
451 + client.scroll_width = @max(100.0, @min(current + delta * available, available));
452 + } else {
453 + client.scroll_width = @max(0.1, @min(current + delta, 1.0));
454 + }
455 + core.arrange(monitor, wm);
456 + std.debug.print("adjust_width: width={d:.3}\n", .{client.scroll_width});
457 +}
458 +
380 pub fn setmfact(delta: f32, wm: *WindowManager) void { 459 pub fn setmfact(delta: f32, wm: *WindowManager) void {
381 const monitor = wm.selected_monitor orelse return; 460 const monitor = wm.selected_monitor orelse return;
461 + if (core.isScrollingLayout(monitor)) {
462 + adjustScrollWidth(delta, wm);
463 + return;
464 + }
382 const new_mfact = monitor.mfact + delta; 465 const new_mfact = monitor.mfact + delta;
383 if (new_mfact < 0.05 or new_mfact > 0.95) { 466 if (new_mfact < 0.05 or new_mfact > 0.95) {
384 return; 467 return;
@@ -856,5 +939,7 @@ pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const
856 .send_to_monitor => sendmon(int_arg, wm), 939 .send_to_monitor => sendmon(int_arg, wm),
857 .scroll_left => core.scrollLayout(-1, wm), 940 .scroll_left => core.scrollLayout(-1, wm),
858 .scroll_right => core.scrollLayout(1, wm), 941 .scroll_right => core.scrollLayout(1, wm),
942 + .cycle_width => cycleWidth(if (int_arg == 0) 1 else int_arg, wm),
943 + .set_width => setWidth(int_arg, wm),
859 } 944 }
860 } 945 }
+113 −22 src/wm/core.zig
@@ -4,6 +4,7 @@ const client_mod = @import("../client.zig");
4 const monitor_mod = @import("../monitor.zig"); 4 const monitor_mod = @import("../monitor.zig");
5 const tiling = @import("../layouts/tiling.zig"); 5 const tiling = @import("../layouts/tiling.zig");
6 const scrolling = @import("../layouts/scrolling.zig"); 6 const scrolling = @import("../layouts/scrolling.zig");
7 +const gesture_mod = @import("../input/gesture.zig");
7 const window_manager = @import("wm.zig"); 8 const window_manager = @import("wm.zig");
8 9
9 const config_mod = @import("../config/config.zig"); 10 const config_mod = @import("../config/config.zig");
@@ -104,7 +105,7 @@ pub fn manage(win: xlib.Window, window_attrs: *xlib.XWindowAttributes, wm: *Wind
104 monitor.sel = client; 105 monitor.sel = client;
105 106
106 if (isScrollingLayout(monitor)) { 107 if (isScrollingLayout(monitor)) {
107 - monitor.scroll_offset = 0; 108 + scrollToWindow(client, true, wm);
108 } 109 }
109 110
110 arrange(monitor, wm); 111 arrange(monitor, wm);
@@ -140,12 +141,9 @@ pub fn unmanage(client: *Client, wm: *WindowManager) void {
140 monitor.sel = if (next_focus) |nf| nf else monitor.stack; 141 monitor.sel = if (next_focus) |nf| nf else monitor.stack;
141 } 142 }
142 if (isScrollingLayout(monitor)) { 143 if (isScrollingLayout(monitor)) {
143 - const target = if (monitor.sel) |sel| scrolling.getTargetScrollForWindow(monitor, sel) else 0; 144 + if (wm.animation_monitor == monitor) wm.scroll_animation.stop();
144 - if (target == 0) { 145 + monitor.scroll_offset = @max(0, @min(monitor.scroll_offset, scrolling.maxScroll(monitor)));
145 - monitor.scroll_offset = scrolling.getScrollStep(monitor); 146 + if (monitor.sel) |sel| scrollToWindow(sel, true, wm);
146 - } else {
147 - monitor.scroll_offset = 0;
148 - }
149 } 147 }
150 arrange(monitor, wm); 148 arrange(monitor, wm);
151 } 149 }
@@ -259,6 +257,15 @@ pub fn restack(monitor: *Monitor, wm: *WindowManager) void {
259 257
260 pub fn arrange(monitor: *Monitor, wm: *WindowManager) void { 258 pub fn arrange(monitor: *Monitor, wm: *WindowManager) void {
261 showhide(monitor, wm); 259 showhide(monitor, wm);
260 + if (isScrollingLayout(monitor) and !scrollInMotion(monitor, wm)) {
261 + var offset: i32 = @max(0, @min(monitor.scroll_offset, scrolling.maxScroll(monitor)));
262 + if (monitor.sel) |sel| {
263 + if (!sel.is_floating and client_mod.isVisible(sel)) {
264 + offset = scrolling.offsetToReveal(monitor, offset, sel);
265 + }
266 + }
267 + monitor.scroll_offset = offset;
268 + }
262 if (monitor.lt[monitor.sel_lt]) |layout| { 269 if (monitor.lt[monitor.sel_lt]) |layout| {
263 if (layout.arrange_fn) |arrange_fn| { 270 if (layout.arrange_fn) |arrange_fn| {
264 arrange_fn(monitor); 271 arrange_fn(monitor);
@@ -401,7 +408,7 @@ pub fn focusTopClient(monitor: *Monitor, wm: *WindowManager) void {
401 pub fn tickAnimations(wm: *WindowManager) void { 408 pub fn tickAnimations(wm: *WindowManager) void {
402 if (!wm.scroll_animation.isActive()) return; 409 if (!wm.scroll_animation.isActive()) return;
403 410
404 - const monitor = wm.selected_monitor orelse return; 411 + const monitor = wm.animation_monitor orelse return;
405 if (wm.scroll_animation.update(wm.io)) |new_offset| { 412 if (wm.scroll_animation.update(wm.io)) |new_offset| {
406 monitor.scroll_offset = new_offset; 413 monitor.scroll_offset = new_offset;
407 arrange(monitor, wm); 414 arrange(monitor, wm);
@@ -415,38 +422,121 @@ pub fn isScrollingLayout(monitor: *Monitor) bool {
415 return false; 422 return false;
416 } 423 }
417 424
418 -pub fn scrollLayout(direction: i32, wm: *WindowManager) void { 425 +fn animationActiveOn(monitor: *Monitor, wm: *WindowManager) bool {
419 - const monitor = wm.selected_monitor orelse return; 426 + return wm.scroll_animation.isActive() and wm.animation_monitor == monitor;
420 - if (!isScrollingLayout(monitor)) return; 427 +}
421 -
422 - const scroll_step = scrolling.getScrollStep(monitor);
423 - const max_scroll = scrolling.getMaxScroll(monitor);
424 428
425 - const current = if (wm.scroll_animation.isActive()) 429 +fn scrollInMotion(monitor: *Monitor, wm: *WindowManager) bool {
426 - wm.scroll_animation.target() 430 + return animationActiveOn(monitor, wm) or (wm.gesture.active and wm.gesture.monitor == monitor);
427 - else 431 +}
428 - monitor.scroll_offset;
429 432
430 - var target = current + direction * scroll_step; 433 +fn scrollTarget(monitor: *Monitor, wm: *WindowManager) i32 {
431 - target = @max(0, @min(target, max_scroll)); 434 + return if (animationActiveOn(monitor, wm)) wm.scroll_animation.target() else monitor.scroll_offset;
435 +}
432 436
437 +fn startScrollAnimation(monitor: *Monitor, target: i32, wm: *WindowManager) void {
438 + wm.animation_monitor = monitor;
433 wm.scroll_animation.start(wm.io, monitor.scroll_offset, target, wm.animation_config); 439 wm.scroll_animation.start(wm.io, monitor.scroll_offset, target, wm.animation_config);
434 } 440 }
435 441
442 +pub fn scrollLayout(direction: i32, wm: *WindowManager) void {
443 + const monitor = wm.selected_monitor orelse return;
444 + if (!isScrollingLayout(monitor) or wm.gesture.active) return;
445 +
446 + const current = scrollTarget(monitor, wm);
447 + const snap = scrolling.adjacentSnap(monitor, current, direction) orelse return;
448 +
449 + focus(snap.client, wm);
450 + startScrollAnimation(monitor, snap.offset, wm);
451 +}
452 +
436 pub fn scrollToWindow(client: *Client, animate: bool, wm: *WindowManager) void { 453 pub fn scrollToWindow(client: *Client, animate: bool, wm: *WindowManager) void {
437 const monitor = client.monitor orelse return; 454 const monitor = client.monitor orelse return;
438 if (!isScrollingLayout(monitor)) return; 455 if (!isScrollingLayout(monitor)) return;
456 + if (wm.gesture.active and wm.gesture.monitor == monitor) return;
439 457
440 - const target = scrolling.getTargetScrollForWindow(monitor, client); 458 + const base = scrollTarget(monitor, wm);
459 + const target = scrolling.offsetToReveal(monitor, base, client);
441 460
442 if (animate) { 461 if (animate) {
443 - wm.scroll_animation.start(wm.io, monitor.scroll_offset, target, wm.animation_config); 462 + if (target == base and animationActiveOn(monitor, wm)) return;
463 + startScrollAnimation(monitor, target, wm);
444 } else { 464 } else {
445 monitor.scroll_offset = target; 465 monitor.scroll_offset = target;
446 arrange(monitor, wm); 466 arrange(monitor, wm);
447 } 467 }
448 } 468 }
449 469
470 +fn nowMs(wm: *WindowManager) i64 {
471 + return std.Io.Timestamp.now(wm.io, .awake).toMilliseconds();
472 +}
473 +
474 +pub fn handleGesture(wm: *WindowManager, event: gesture_mod.Event) void {
475 + switch (event.kind) {
476 + .begin => gestureBegin(event, wm),
477 + .update => gestureUpdate(event, wm),
478 + .end => gestureEnd(event, wm),
479 + }
480 +}
481 +
482 +fn gestureBegin(event: gesture_mod.Event, wm: *WindowManager) void {
483 + if (event.fingers != wm.config.gesture_fingers) return;
484 + const monitor = wm.selected_monitor orelse return;
485 + if (!isScrollingLayout(monitor)) return;
486 +
487 + if (animationActiveOn(monitor, wm)) wm.scroll_animation.stop();
488 +
489 + wm.gesture = .{
490 + .active = true,
491 + .monitor = monitor,
492 + .position = @floatFromInt(monitor.scroll_offset),
493 + };
494 +}
495 +
496 +fn gestureUpdate(event: gesture_mod.Event, wm: *WindowManager) void {
497 + if (!wm.gesture.active) return;
498 + const monitor = wm.gesture.monitor orelse return;
499 + if (!isScrollingLayout(monitor)) {
500 + wm.gesture.active = false;
501 + return;
502 + }
503 +
504 + const sign: f64 = if (wm.config.gesture_natural) -1.0 else 1.0;
505 + const delta = event.dx * @as(f64, wm.config.gesture_speed) * sign;
506 +
507 + const geo = scrolling.geometry(monitor);
508 + const min_position: f64 = @floatFromInt(-geo.available_w);
509 + const max_position: f64 = @floatFromInt(scrolling.contentWidth(monitor));
510 +
511 + wm.gesture.position = @max(min_position, @min(wm.gesture.position + delta, max_position));
512 + wm.gesture.push(delta, nowMs(wm));
513 +
514 + monitor.scroll_offset = @intFromFloat(@round(wm.gesture.position));
515 + arrange(monitor, wm);
516 +}
517 +
518 +fn gestureEnd(event: gesture_mod.Event, wm: *WindowManager) void {
519 + _ = event;
520 + if (!wm.gesture.active) return;
521 + wm.gesture.active = false;
522 + const monitor = wm.gesture.monitor orelse return;
523 + if (!isScrollingLayout(monitor)) return;
524 +
525 + const velocity = wm.gesture.velocity(nowMs(wm));
526 + const deceleration = 1000.0 * @log(window_manager.gesture_deceleration);
527 + const projected = wm.gesture.position - velocity / deceleration;
528 +
529 + const snap = scrolling.nearestSnap(monitor, projected) orelse {
530 + arrange(monitor, wm);
531 + return;
532 + };
533 +
534 + focus(snap.client, wm);
535 + wm.animation_monitor = monitor;
536 + wm.scroll_animation.startSpring(wm.io, monitor.scroll_offset, snap.offset, velocity, .{});
537 + if (!wm.scroll_animation.isActive()) arrange(monitor, wm);
538 +}
539 +
450 fn positionFloating(client: *Client, monitor: *Monitor, pos: config_mod.FloatingPosition) void { 540 fn positionFloating(client: *Client, monitor: *Monitor, pos: config_mod.FloatingPosition) void {
451 const bw = 2 * client.border_width; 541 const bw = 2 * client.border_width;
452 const x: i32 = switch (pos) { 542 const x: i32 = switch (pos) {
@@ -482,6 +572,7 @@ pub fn applyRules(client: *Client, wm: *WindowManager) void {
482 if (class_matches and instance_matches and title_matches) { 572 if (class_matches and instance_matches and title_matches) {
483 client.is_floating = rule.is_floating; 573 client.is_floating = rule.is_floating;
484 client.tags |= rule.tags; 574 client.tags |= rule.tags;
575 + if (rule.width > 0) client.scroll_width = rule.width;
485 if (rule.monitor >= 0) { 576 if (rule.monitor >= 0) {
486 var target = wm.monitors; 577 var target = wm.monitors;
487 var index: i32 = 0; 578 var index: i32 = 0;
+86 −0 src/wm/wm.zig
@@ -18,6 +18,7 @@ const floating = @import("../layouts/floating.zig");
18 const scrolling = @import("../layouts/scrolling.zig"); 18 const scrolling = @import("../layouts/scrolling.zig");
19 const grid = @import("../layouts/grid.zig"); 19 const grid = @import("../layouts/grid.zig");
20 const dwindle = @import("../layouts/dwindle.zig"); 20 const dwindle = @import("../layouts/dwindle.zig");
21 +const gesture_mod = @import("../input/gesture.zig");
21 22
22 pub const core = @import("core.zig"); 23 pub const core = @import("core.zig");
23 pub const actions = @import("actions.zig"); 24 pub const actions = @import("actions.zig");
@@ -36,6 +37,50 @@ pub const WithdrawnState: c_long = 0;
36 pub const IconicState: c_long = 3; 37 pub const IconicState: c_long = 3;
37 pub const IsViewable: c_int = 2; 38 pub const IsViewable: c_int = 2;
38 39
40 +pub const gesture_deceleration: f64 = 0.997;
41 +pub const gesture_history_size = 32;
42 +pub const gesture_history_ms: i64 = 150;
43 +
44 +pub const GestureSample = struct {
45 + dx: f64,
46 + time_ms: i64,
47 +};
48 +
49 +pub const GestureState = struct {
50 + active: bool = false,
51 + monitor: ?*Monitor = null,
52 + position: f64 = 0,
53 + history: [gesture_history_size]GestureSample = undefined,
54 + history_len: usize = 0,
55 +
56 + pub fn push(self: *GestureState, dx: f64, time_ms: i64) void {
57 + if (self.history_len == gesture_history_size) {
58 + std.mem.copyForwards(GestureSample, self.history[0 .. gesture_history_size - 1], self.history[1..]);
59 + self.history_len -= 1;
60 + }
61 + self.history[self.history_len] = .{ .dx = dx, .time_ms = time_ms };
62 + self.history_len += 1;
63 + }
64 +
65 + pub fn velocity(self: *const GestureState, now_ms: i64) f64 {
66 + if (self.history_len < 2) return 0.0;
67 + const cutoff = now_ms - gesture_history_ms;
68 + var total: f64 = 0.0;
69 + var first: ?i64 = null;
70 + var last: i64 = 0;
71 + for (self.history[0..self.history_len]) |sample| {
72 + if (sample.time_ms < cutoff) continue;
73 + total += sample.dx;
74 + if (first == null) first = sample.time_ms;
75 + last = sample.time_ms;
76 + }
77 + const start = first orelse return 0.0;
78 + const span = last - start;
79 + if (span <= 0) return 0.0;
80 + return total / @as(f64, @floatFromInt(span)) * 1000.0;
81 + }
82 +};
83 +
39 pub const Cursors = struct { 84 pub const Cursors = struct {
40 normal: xlib.Cursor, 85 normal: xlib.Cursor,
41 resize: xlib.Cursor, 86 resize: xlib.Cursor,
@@ -86,6 +131,9 @@ pub const WindowManager = struct {
86 131
87 scroll_animation: animations.ScrollAnimation, 132 scroll_animation: animations.ScrollAnimation,
88 animation_config: animations.AnimationConfig, 133 animation_config: animations.AnimationConfig,
134 + animation_monitor: ?*Monitor,
135 + gesture: GestureState,
136 + gestures: ?gesture_mod.Gestures,
89 137
90 running: bool, 138 running: bool,
91 next_spawn_floating: bool = false, 139 next_spawn_floating: bool = false,
@@ -130,6 +178,9 @@ pub const WindowManager = struct {
130 .overlay = null, 178 .overlay = null,
131 .scroll_animation = .{}, 179 .scroll_animation = .{},
132 .animation_config = .{ .duration_ms = 150, .easing = .ease_out }, 180 .animation_config = .{ .duration_ms = 150, .easing = .ease_out },
181 + .animation_monitor = null,
182 + .gesture = .{},
183 + .gestures = null,
133 .running = true, 184 .running = true,
134 .last_motion_monitor = null, 185 .last_motion_monitor = null,
135 }; 186 };
@@ -137,12 +188,33 @@ pub const WindowManager = struct {
137 wm.setupMonitors(); 188 wm.setupMonitors();
138 wm.setupBars(); 189 wm.setupBars();
139 wm.setupOverlay(); 190 wm.setupOverlay();
191 + wm.syncGestures();
140 192
141 return wm; 193 return wm;
142 } 194 }
143 195
196 + pub fn syncGestures(self: *WindowManager) void {
197 + if (!gesture_mod.enabled) return;
198 + if (self.config.gesture_enabled and self.gestures == null) {
199 + self.gestures = gesture_mod.Gestures.init() catch |err| blk: {
200 + std.debug.print("touchpad gestures unavailable: {s}\n", .{@errorName(err)});
201 + break :blk null;
202 + };
203 + if (self.gestures != null) std.debug.print("touchpad gestures enabled\n", .{});
204 + } else if (!self.config.gesture_enabled and self.gestures != null) {
205 + self.gestures.?.deinit();
206 + self.gestures = null;
207 + self.gesture = .{};
208 + }
209 + }
210 +
144 /// Release all allocated memory owned by the WM. 211 /// Release all allocated memory owned by the WM.
145 pub fn deinit(self: *WindowManager) void { 212 pub fn deinit(self: *WindowManager) void {
213 + if (self.gestures) |*gestures| {
214 + gestures.deinit();
215 + self.gestures = null;
216 + }
217 +
146 bar_mod.destroyBars(self.bars, self.display.handle); 218 bar_mod.destroyBars(self.bars, self.display.handle);
147 self.bars = null; 219 self.bars = null;
148 220
@@ -416,6 +488,7 @@ pub const WindowManager = struct {
416 cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0; 488 cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0;
417 489
418 mon.smartgaps_enabled = cfg.smartgaps_enabled; 490 mon.smartgaps_enabled = cfg.smartgaps_enabled;
491 + mon.scroll_default_width = cfg.scroll_default_width;
419 492
420 if (cfg.gaps_enabled and any_gap_nonzero) { 493 if (cfg.gaps_enabled and any_gap_nonzero) {
421 mon.gap_inner_h = cfg.gap_inner_h; 494 mon.gap_inner_h = cfg.gap_inner_h;
@@ -672,9 +745,11 @@ pub const WindowManager = struct {
672 self: *WindowManager, 745 self: *WindowManager,
673 eventFn: fn (*xlib.XEvent, *WindowManager) void, 746 eventFn: fn (*xlib.XEvent, *WindowManager) void,
674 tickFn: fn (*WindowManager) void, 747 tickFn: fn (*WindowManager) void,
748 + gestureFn: fn (*WindowManager, gesture_mod.Event) void,
675 ) void { 749 ) void {
676 var fds = [_]std.posix.pollfd{ 750 var fds = [_]std.posix.pollfd{
677 .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 }, 751 .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
752 + .{ .fd = -1, .events = std.posix.POLL.IN, .revents = 0 },
678 }; 753 };
679 754
680 _ = xlib.XSync(self.display.handle, xlib.False); 755 _ = xlib.XSync(self.display.handle, xlib.False);
@@ -685,6 +760,10 @@ pub const WindowManager = struct {
685 eventFn(&event, self); 760 eventFn(&event, self);
686 } 761 }
687 762
763 + if (self.gestures) |*gestures| {
764 + gestures.dispatch(self, gestureFn);
765 + }
766 +
688 tickFn(self); 767 tickFn(self);
689 768
690 var current_bar = self.bars; 769 var current_bar = self.bars;
@@ -694,6 +773,7 @@ pub const WindowManager = struct {
694 current_bar = bar.next; 773 current_bar = bar.next;
695 } 774 }
696 775
776 + fds[1].fd = if (self.gestures) |gestures| gestures.fd else -1;
697 const poll_timeout: i32 = if (self.scroll_animation.isActive()) 16 else 1000; 777 const poll_timeout: i32 = if (self.scroll_animation.isActive()) 16 else 1000;
698 _ = std.posix.poll(&fds, poll_timeout) catch 0; 778 _ = std.posix.poll(&fds, poll_timeout) catch 0;
699 } 779 }
@@ -718,6 +798,12 @@ pub const WindowManager = struct {
718 798
719 loadFn(self); 799 loadFn(self);
720 800
801 + var mon = self.monitors;
802 + while (mon) |m| : (mon = m.next) {
803 + m.scroll_default_width = self.config.scroll_default_width;
804 + }
805 + self.syncGestures();
806 +
721 bar_mod.destroyBars(self.bars, self.display.handle); 807 bar_mod.destroyBars(self.bars, self.display.handle);
722 self.bars = null; 808 self.bars = null;
723 self.setupBars(); 809 self.setupBars();
+20 −0 templates/config.lua
@@ -129,6 +129,17 @@ oxwm.set_layout_symbol("tabbed", "[=]")
129 -- Unset value uses oxwm.set_layout value 129 -- Unset value uses oxwm.set_layout value
130 -- oxwm.set_tag_layout(1, "grid") 130 -- oxwm.set_tag_layout(1, "grid")
131 131
132 +-- Scrolling layout: windows sit side by side in an infinite strip (like niri)
133 +-- Widths at or below 1 are a proportion of the screen, larger values are pixels
134 +-- oxwm.scroll.set_default_width(0.5)
135 +-- oxwm.scroll.set_width_presets({ 1 / 3, 1 / 2, 2 / 3 })
136 +-- Three-finger touchpad swipes scroll the strip and snap to the nearest window
137 +-- (needs read access to /dev/input, usually the "input" group)
138 +-- oxwm.scroll.set_gesture_enabled(true)
139 +-- oxwm.scroll.set_gesture_fingers(3)
140 +-- oxwm.scroll.set_gesture_speed(2.0)
141 +-- oxwm.scroll.set_gesture_natural(false)
142 +
132 -- Set the default attach method, similar to the patches from dwm 143 -- Set the default attach method, similar to the patches from dwm
133 -- Available methods: "aside" (default), "top", "bottom", "above", "below" 144 -- Available methods: "aside" (default), "top", "bottom", "above", "below"
134 -- For example "above" makes new clients appear above the currently selected one 145 -- For example "above" makes new clients appear above the currently selected one
@@ -239,6 +250,15 @@ oxwm.key.bind({ modkey }, "C", oxwm.layout.set("tiling"))
239 -- Cycle through layouts 250 -- Cycle through layouts
240 oxwm.key.bind({ modkey }, "N", oxwm.layout.cycle()) 251 oxwm.key.bind({ modkey }, "N", oxwm.layout.cycle())
241 252
253 +-- Scrolling layout controls
254 +-- oxwm.key.bind({ modkey }, "G", oxwm.layout.set("scrolling"))
255 +-- oxwm.key.bind({ modkey }, "Left", oxwm.layout.scroll_left())
256 +-- oxwm.key.bind({ modkey }, "Right", oxwm.layout.scroll_right())
257 +-- oxwm.key.bind({ modkey }, "R", oxwm.layout.cycle_width(1))
258 +-- oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.layout.cycle_width(-1))
259 +-- oxwm.key.bind({ modkey }, "M", oxwm.layout.set_width(1))
260 +-- In the scrolling layout set_master_factor resizes the focused window instead
261 +
242 -- Master area controls (tiling layout) 262 -- Master area controls (tiling layout)
243 263
244 -- Decrease/Increase master area width 264 -- Decrease/Increase master area width
+39 −1 templates/oxwm.lua
@@ -76,7 +76,7 @@ function oxwm.toggle_bar() end
76 oxwm.rule = {} 76 oxwm.rule = {}
77 77
78 ---Add a window rule 78 ---Add a window rule
79 ----@param rule {class: string?, instance: string?, title: string?, role: string?, floating: boolean?, tag: integer?, fullscreen: boolean?, focus: boolean?} Rule configuration 79 +---@param rule {class: string?, instance: string?, title: string?, role: string?, floating: boolean?, tag: integer?, fullscreen: boolean?, focus: boolean?, width: number?} Rule configuration (`width` is the scrolling layout column width: a proportion at or below 1, otherwise pixels)
80 function oxwm.rule.add(rule) end 80 function oxwm.rule.add(rule) end
81 81
82 ---Quit the window manager 82 ---Quit the window manager
@@ -237,6 +237,44 @@ function oxwm.layout.scroll_left() end
237 ---@return table Action table for keybinding 237 ---@return table Action table for keybinding
238 function oxwm.layout.scroll_right() end 238 function oxwm.layout.scroll_right() end
239 239
240 +---Cycle the focused window through the scrolling width presets
241 +---@param direction integer? 1 for the next preset, -1 for the previous (default 1)
242 +---@return table Action table for keybinding
243 +function oxwm.layout.cycle_width(direction) end
244 +
245 +---Set the focused window's width in the scrolling layout
246 +---@param width number Proportion of the screen (0-1) or pixels (> 1)
247 +---@return table Action table for keybinding
248 +function oxwm.layout.set_width(width) end
249 +
250 +---Scrolling layout module
251 +---@class oxwm.scroll
252 +oxwm.scroll = {}
253 +
254 +---Set the default column width for new windows in the scrolling layout
255 +---@param width number Proportion of the screen (0-1) or pixels (> 1), default 0.5
256 +function oxwm.scroll.set_default_width(width) end
257 +
258 +---Set the width presets cycled by oxwm.layout.cycle_width
259 +---@param presets number[] Up to 8 widths, default { 1/3, 1/2, 2/3 }
260 +function oxwm.scroll.set_width_presets(presets) end
261 +
262 +---Enable or disable touchpad swipe gestures (requires read access to /dev/input)
263 +---@param enabled boolean Default true
264 +function oxwm.scroll.set_gesture_enabled(enabled) end
265 +
266 +---Set how many fingers a swipe needs to scroll the layout
267 +---@param fingers integer 3, 4 or 5 (default 3)
268 +function oxwm.scroll.set_gesture_fingers(fingers) end
269 +
270 +---Set how far the view moves per unit of finger travel
271 +---@param speed number Multiplier, default 2.0
272 +function oxwm.scroll.set_gesture_speed(speed) end
273 +
274 +---Invert the swipe direction so the windows follow the fingers
275 +---@param natural boolean Default false
276 +function oxwm.scroll.set_gesture_natural(natural) end
277 +
240 ---Tag/workspace management module 278 ---Tag/workspace management module
241 ---@class oxwm.tag 279 ---@class oxwm.tag
242 oxwm.tag = {} 280 oxwm.tag = {}