more clean up of fusuma
This commit is contained in:
1 parent
24574f6a01
commit
572447c6a4
4 files changed
-198
No files matched your search
@@ -1,17 +0,0 @@
|
||||
# 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
|
||||
@@ -1,137 +0,0 @@
|
||||
# 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<String>]
|
||||
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
|
||||
@@ -1,29 +0,0 @@
|
||||
# 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/<name>.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
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
# 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/<base-namespace>/*.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"
|
||||
Reference in new issue
Block a user