diff --git a/configs/niri/shared/general.kdl b/configs/niri/shared/general.kdl index bd5817d..06f3278 100644 --- a/configs/niri/shared/general.kdl +++ b/configs/niri/shared/general.kdl @@ -65,3 +65,7 @@ gestures { bottom-right } } + +debug { + honor-xdg-activation-with-invalid-serial +} diff --git a/configs/niri/shared/startup.kdl b/configs/niri/shared/startup.kdl index be52b87..755ea89 100644 --- a/configs/niri/shared/startup.kdl +++ b/configs/niri/shared/startup.kdl @@ -1,2 +1,2 @@ -spawn-at-startup "niri-touchscreen-gestures" +// spawn-at-startup "niri-touchscreen-gestures" diff --git a/modules/nixos/common/fusuma.nix b/modules/nixos/common/fusuma.nix new file mode 100644 index 0000000..5e00251 --- /dev/null +++ b/modules/nixos/common/fusuma.nix @@ -0,0 +1,59 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.skyg.nixos.common.fusuma; + configFile = + if cfg.configFile != null then + cfg.configFile + else + pkgs.writeText "fusuma-config.yml" (lib.generators.toYAML { } cfg.settings); +in +{ + options.skyg.nixos.common.fusuma = { + enable = lib.mkEnableOption "Fusuma multi-touch gesture daemon (niri + per-app support)"; + + package = lib.mkPackageOption pkgs "fusuma" { }; + + settings = lib.mkOption { + type = with lib.types; attrsOf anything; + default = { }; + example = { + swipe."3".left.command = "niri msg action FocusColumnRight"; + app.zed = { + match.class = "dev.zed.Zed"; + swipe."2".left.keypress = { ALT = "press"; LEFT = "press"; }; + }; + }; + description = '' + Fusuma configuration as a Nix attribute set. + Converted to YAML and passed to the daemon. + ''; + }; + + configFile = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + description = "Path to an existing Fusuma config.yml (overrides generated settings)"; + }; + }; + + config = lib.mkIf cfg.enable { + environment.systemPackages = [ cfg.package ]; + + # Ensure the user can read raw input devices + users.users.${config.skyg.user.name}.extraGroups = lib.mkIf config.skyg.user.enable [ "input" ]; + + systemd.user.services.fusuma = { + description = "Fusuma multi-touch gesture daemon"; + partOf = [ "graphical-session.target" ]; + after = [ "graphical-session.target" ]; + wantedBy = [ "graphical-session.target" ]; + + serviceConfig = { + ExecStart = "${lib.getExe cfg.package} --daemon --config ${configFile}"; + Restart = "on-failure"; + RestartSec = 2; + }; + }; + }; +} diff --git a/pkgs/fusuma-plugin-niri-appmatcher/fusuma-plugin-niri-appmatcher.gemspec b/pkgs/fusuma-plugin-niri-appmatcher/fusuma-plugin-niri-appmatcher.gemspec new file mode 100644 index 0000000..124ea09 --- /dev/null +++ b/pkgs/fusuma-plugin-niri-appmatcher/fusuma-plugin-niri-appmatcher.gemspec @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +Gem::Specification.new do |spec| + spec.name = "fusuma-plugin-niri-appmatcher" + spec.version = "0.1.0" + spec.authors = ["Ari Sosnovsky"] + spec.email = ["ari@sosnovsky.ca"] + spec.summary = "Niri appmatcher plugin for Fusuma" + spec.description = "Provides per-application gesture rules for the niri Wayland compositor by querying the focused window via niri IPC." + spec.homepage = "https://github.com/skykanin/nixos-setup" + spec.license = "MIT" + + spec.files = Dir["lib/**/*.rb"] + spec.require_paths = ["lib"] + + spec.add_dependency "fusuma", "~> 3.0" +end diff --git a/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/appmatcher/niri.rb b/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/appmatcher/niri.rb new file mode 100644 index 0000000..dc3a269 --- /dev/null +++ b/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/appmatcher/niri.rb @@ -0,0 +1,137 @@ +# frozen_string_literal: true + +require "json" +require "open3" +require "socket" +require "fusuma/plugin/appmatcher/user_switcher" +require "fusuma/multi_logger" +require "fusuma/custom_process" + +module Fusuma + module Plugin + module Appmatcher + # Search Active Window's Name for niri (scrollable-tiling Wayland compositor) + class Niri + include UserSwitcher + + attr_reader :reader, :writer + + # @return [Boolean] + def self.available? + return true if ENV["NIRI_SOCKET"] + + ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).any? do |dir| + path = File.join(dir, "niri") + File.executable?(path) && !File.directory?(path) + end + end + + def initialize + @reader, @writer = IO.pipe + end + + # fork process and watch signal + # @return [Integer] Process id + def watch_start + as_user(proctitle: self.class.name.underscore) do |_user| + @reader.close + register_on_application_changed(Matcher.new) + end + end + + private + + def register_on_application_changed(matcher) + @writer.puts(matcher.active_application || "NOT FOUND") + + matcher.on_active_application_changed do |name| + notify(name) + end + end + + def notify(name) + @writer.puts(name) + rescue Errno::EPIPE + exit 0 + rescue => e + MultiLogger.error e.message + exit 1 + end + + # Look up application name using the niri IPC socket + class Matcher + # @return [Array] + def running_applications + request("Windows")["Ok"]["Windows"] + .map { |w| w["app_id"] } + .compact + .uniq + rescue => e + MultiLogger.error "Failed to get running applications: #{e.message}" + [] + end + + # @return [String, nil] + def active_application + request("FocusedWindow")["Ok"]["FocusedWindow"]&.dig("app_id") + rescue => e + MultiLogger.error "Failed to get active application: #{e.message}" + nil + end + + # Subscribe to niri's event stream and yield the app_id on focus change + def on_active_application_changed + unix_socket_request("EventStream") do |socket| + # The first line of an EventStream reply is the Ok confirmation; + # subsequent lines are JSON events. + socket.each_line do |line| + event = JSON.parse(line) + changed = event["WindowFocusChanged"] || + event.dig("WindowsChanged", "windows") && + event["WindowsChanged"]["windows"].find { |w| w["is_focused"] } + + app_id = + case changed + when Hash then changed["app_id"] + when NilClass then nil + else changed && active_application + end + + # WindowFocusChanged carries { id: ... } or null; on id-only + # events re-query the focused window for its app_id. + app_id ||= active_application if event["WindowFocusChanged"] + + yield(app_id || "NOT FOUND") if app_id || event["WindowFocusChanged"] + rescue JSON::ParserError + next + end + end + rescue => e + MultiLogger.error "Niri subscription error: #{e.message}" + sleep 1 + retry + end + + private + + # @return [String] + def socket_path + ENV["NIRI_SOCKET"] || raise("NIRI_SOCKET is not set") + end + + # Send a single request and return the parsed reply (one line of JSON). + def request(action) + unix_socket_request(action) { |s| JSON.parse(s.gets) } + end + + def unix_socket_request(action) + UNIXSocket.open(socket_path) do |socket| + socket.puts(JSON.generate(action)) + yield socket + end + end + end + end + end + end +end diff --git a/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/appmatcher/niri_backend_patch.rb b/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/appmatcher/niri_backend_patch.rb new file mode 100644 index 0000000..313f805 --- /dev/null +++ b/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/appmatcher/niri_backend_patch.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# Registers the niri backend for fusuma-plugin-appmatcher. +# +# Upstream `Fusuma::Plugin::Appmatcher.backend_klass` selects a backend via a +# hardcoded case on XDG_SESSION_TYPE / XDG_CURRENT_DESKTOP and knows nothing +# about niri. We prepend a module that returns our Niri backend whenever it is +# available (i.e. NIRI_SOCKET is set), and otherwise defer to upstream. +# +# This file lives in a nested path on purpose: Fusuma's plugin manager +# auto-requires `fusuma/plugin/**/*.rb` from fusuma-plugin-* gems but +# explicitly excludes top-level files (fusuma/plugin/.rb). +require_relative "niri" + +module Fusuma + module Plugin + module Appmatcher + module NiriBackendSelection + def backend_klass + return Niri if Niri.available? + + super + end + end + + singleton_class.prepend(NiriBackendSelection) + end + end +end diff --git a/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/detectors/niri_appmatcher_loader.rb b/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/detectors/niri_appmatcher_loader.rb new file mode 100644 index 0000000..e6a8ffd --- /dev/null +++ b/pkgs/fusuma-plugin-niri-appmatcher/lib/fusuma/plugin/detectors/niri_appmatcher_loader.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# Bootstrap for the niri appmatcher backend. +# +# fusuma-plugin-appmatcher hardcodes its backend list in +# `fusuma/plugin/appmatcher.rb` and offers no registry for third-party +# backends, so our Niri backend must be required explicitly. Fusuma only +# auto-requires `fusuma/plugin//*.rb` (detectors, inputs, +# buffers, ...) from `fusuma-plugin-*` gems on the load path; it never scans +# the `appmatcher/` backend directory. This file therefore lives in the +# `detectors` namespace purely so Fusuma auto-loads it at boot — it registers +# no detector, it just pulls in the backend and the `backend_klass` patch. +require "fusuma/plugin/appmatcher" +require "fusuma/plugin/appmatcher/niri" +require "fusuma/plugin/appmatcher/niri_backend_patch" diff --git a/pkgs/fusuma/Gemfile b/pkgs/fusuma/Gemfile new file mode 100644 index 0000000..6e6b3cc --- /dev/null +++ b/pkgs/fusuma/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' +gem 'fusuma' +gem 'fusuma-plugin-appmatcher' +gem 'fusuma-plugin-keypress' +gem 'fusuma-plugin-sendkey' diff --git a/pkgs/fusuma/Gemfile.lock b/pkgs/fusuma/Gemfile.lock new file mode 100644 index 0000000..a0d27ae --- /dev/null +++ b/pkgs/fusuma/Gemfile.lock @@ -0,0 +1,42 @@ +GEM + remote: https://rubygems.org/ + specs: + fusuma (3.12.0) + fusuma-plugin-appmatcher (0.12.0) + fusuma (>= 3.0) + rexml + ruby-dbus + fusuma-plugin-keypress (0.11.0) + fusuma (>= 3.1) + fusuma-plugin-sendkey (0.14.0) + fusuma (>= 3.1) + revdev + logger (1.7.0) + revdev (0.2.1) + rexml (3.4.4) + ruby-dbus (0.25.0) + logger + rexml + +PLATFORMS + ruby + x86_64-linux + +DEPENDENCIES + fusuma + fusuma-plugin-appmatcher + fusuma-plugin-keypress + fusuma-plugin-sendkey + +CHECKSUMS + fusuma (3.12.0) sha256=2115b5522797b0c61215bbd37fb0fc8cf52b4c7d4c1a036aef20cb6ae049fca6 + fusuma-plugin-appmatcher (0.12.0) sha256=30e397eb4a62debb14ce70bf71d3a1d54a4ad552bdd77665b5979a3f0f2be246 + fusuma-plugin-keypress (0.11.0) sha256=341acb8ca7b167ec398006e3b42e18d979ed4ef18b1543f7422bf9928c6c9a99 + fusuma-plugin-sendkey (0.14.0) sha256=12729afc260359b17f64b48af89d541122a8d45f7e63a709de6e2b3fc57fc0bf + logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203 + revdev (0.2.1) sha256=e9eb8b015d0189637050c37342ad6ccf4d8b49839df58edd08332a8ab779dfac + rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142 + ruby-dbus (0.25.0) sha256=fe431ca96a61f7c87a5177a43c9fe0a50eafe174c1d108421e38bb5165ea4814 + +BUNDLED WITH + 2.7.2 diff --git a/pkgs/fusuma/default.nix b/pkgs/fusuma/default.nix new file mode 100644 index 0000000..6a7aa78 --- /dev/null +++ b/pkgs/fusuma/default.nix @@ -0,0 +1,24 @@ +{ lib +, ruby +, bundlerEnv +, runCommand +, makeWrapper +, ... +}: + +let + env = bundlerEnv { + name = "fusuma-3.12.0"; + inherit ruby; + gemdir = ./.; + }; + + pluginLib = "${../fusuma-plugin-niri-appmatcher}/lib"; +in +runCommand "fusuma-3.12.0" { + nativeBuildInputs = [ makeWrapper ]; +} '' + mkdir -p $out/bin + makeWrapper ${env}/bin/fusuma $out/bin/fusuma \ + --set RUBYLIB "${pluginLib}:$RUBYLIB" +'' diff --git a/pkgs/fusuma/gemset.nix b/pkgs/fusuma/gemset.nix new file mode 100644 index 0000000..f2177aa --- /dev/null +++ b/pkgs/fusuma/gemset.nix @@ -0,0 +1,86 @@ +{ + fusuma = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "19pw97h6mjr0xxm066jcgm62pxcczjq7zlxv2l9cdc4p4x9ba591"; + type = "gem"; + }; + version = "3.12.0"; + }; + fusuma-plugin-appmatcher = { + dependencies = ["fusuma" "rexml" "ruby-dbus"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0ip25c7kz6lpnmjpdmxxaballjnml79p3gvhrqabppk29bmrgqrh"; + type = "gem"; + }; + version = "0.12.0"; + }; + fusuma-plugin-keypress = { + dependencies = ["fusuma"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "sha256-NBrLjKexZ+w5gAbjtC4Y2XntTvGLFUP3Qiv5koxsmpk="; + type = "gem"; + }; + version = "0.11.0"; + }; + fusuma-plugin-sendkey = { + dependencies = ["fusuma" "revdev"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1gy0gz2kyavfvq4sfqvybzaah8hiajfzi2mlcizv2n834vy9lwhj"; + type = "gem"; + }; + version = "0.14.0"; + }; + logger = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00q2zznygpbls8asz5knjvvj2brr3ghmqxgr83xnrdj4rk3xwvhr"; + type = "gem"; + }; + version = "1.7.0"; + }; + revdev = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1b6zg6vqlaik13fqxxcxhd4qnkfgdjnl4wy3a1q67281bl0qpsz9"; + type = "gem"; + }; + version = "0.2.1"; + }; + rexml = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0hninnbvqd2pn40h863lbrn9p11gvdxp928izkag5ysx8b1s5q0r"; + type = "gem"; + }; + version = "3.4.4"; + }; + ruby-dbus = { + dependencies = ["logger" "rexml"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0528x9jm3frq3r10ilf1fkhsy3m5w2gkr93pa5xcixv1daliqhzy"; + type = "gem"; + }; + version = "0.25.0"; + }; +}