Sha256: cc131a30af504f88e3399a2d3e67e9114e9d348bd2ef2c185a4f5280945879f8

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'alsa/aconnect/version'
require 'alsa/aconnect/config'
require 'alsa/aconnect/error'
require 'alsa/aconnect/parser'
require 'alsa/aconnect/port'
require 'alsa/aconnect/client'

require 'open3'

module ALSA
  module Aconnect
    module_function

    def input_clients
      out = run('-i', '-l')
      Parser.parse_clients(out).map { |text| Client.new(text, :input) }
    end

    def input_ports
      input_clients.map(&:ports).flatten
    end

    def output_clients
      out = run('-o', '-l')
      Parser.parse_clients(out).map { |text| Client.new(text, :output) }
    end

    def output_ports
      output_clients.map(&:ports).flatten
    end

    def connect(input, output)
      input = "#{input.client.id}:#{input.id}" if input.is_a?(Port)
      output = "#{output.client.id}:#{output.id}" if output.is_a?(Port)

      run(input, output)
    end

    def disconnect(input, output)
      input = "#{input.client.id}:#{input.id}" if input.is_a?(Port)
      output = "#{output.client.id}:#{output.id}" if output.is_a?(Port)

      run('-d', input, output)
    end

    def run(*arguments)
      cmd = [config.exec_path, *arguments].compact.join(' ')
      out, err, status = Open3.capture3(cmd)
      code = status.exitstatus
      raise Error.new code, err unless code.zero?

      out
    end

    def config
      @config ||= Config.new
    end

    def configure
      yield config if block_given?
    end

    configure
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alsa_aconnect-0.1.5 lib/alsa/aconnect.rb
alsa_aconnect-0.1.4 lib/alsa/aconnect.rb