Skip to content

Commit

Permalink
Implement volume command
Browse files Browse the repository at this point in the history
It's unbelievably stupid that I need a 3rd party library for such a
simple task... Oh, Apple...
  • Loading branch information
nikitabobko committed Oct 18, 2024
1 parent 125e9b9 commit 24e1f08
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"version" : "0.1.3"
}
},
{
"identity" : "issoundadditions",
"kind" : "remoteSourceControl",
"location" : "https://github.com/InerziaSoft/ISSoundAdditions",
"state" : {
"revision" : "4b555f0354e6c280917bae8a598a258efe87ab98",
"version" : "2.0.1"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let package = Package(
.library(name: "AppBundle", targets: ["AppBundle"]),
],
dependencies: [
.package(url: "https://github.com/InerziaSoft/ISSoundAdditions", from: "2.0.1"),
.package(url: "https://github.com/Kitura/BlueSocket", exact: "2.0.4"),
.package(url: "https://github.com/soffes/HotKey", exact: "0.1.3"),
.package(url: "https://github.com/LebJe/TOMLKit", exact: "0.5.5"),
Expand Down Expand Up @@ -53,6 +54,7 @@ let package = Package(
.product(name: "Antlr4Static", package: "antlr4"),
.product(name: "Socket", package: "BlueSocket"),
.product(name: "HotKey", package: "HotKey"),
.product(name: "ISSoundAdditions", package: "ISSoundAdditions"),
.product(name: "TOMLKit", package: "TOMLKit"),
.product(name: "Collections", package: "swift-collections"),
]
Expand Down
2 changes: 2 additions & 0 deletions Sources/AppBundle/command/cmdManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ extension CmdArgs {
command = ServerVersionInternalCommandCommand()
case .triggerBinding:
command = TriggerBindingCommand(args: self as! TriggerBindingCmdArgs)
case .volume:
command = VolumeCommand(args: self as! VolumeCmdArgs)
case .workspace:
command = WorkspaceCommand(args: self as! WorkspaceCmdArgs)
case .workspaceBackAndForth:
Expand Down
26 changes: 26 additions & 0 deletions Sources/AppBundle/command/impl/VolumeCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AppKit
import Common
import ISSoundAdditions

struct VolumeCommand: Command {
let args: VolumeCmdArgs

func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
check(Thread.current.isMainThread)
switch args.action.val {
case .up:
Sound.output.increaseVolume(by: 0.0625, autoMuteUnmute: true)
case .down:
Sound.output.decreaseVolume(by: 0.0625, autoMuteUnmute: true)
case .muteToggle:
Sound.output.isMuted.toggle()
case .muteOn:
Sound.output.isMuted = true
case .muteOff:
Sound.output.isMuted = false
case .set(let int):
Sound.output.setVolume(Float(int) / 100, autoMuteUnmute: true)
}
return true
}
}
1 change: 1 addition & 0 deletions Sources/Cli/subcommandDescriptionsGenerated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ let subcommandDescriptions = [
[" split", "Split focused window"],
[" summon-workspace", "Move the requested workspace to the focused monitor."],
[" trigger-binding", "Trigger AeroSpace binding as if it was pressed by user"],
[" volume", "Manipulate volume"],
[" workspace-back-and-forth", "Switch between the focused workspace and previously focused workspace back and forth"],
[" workspace", "Focus the specified workspace"],
]
3 changes: 3 additions & 0 deletions Sources/Common/cmdArgs/cmdArgsManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum CmdKind: String, CaseIterable, Equatable {
case split
case summonWorkspace = "summon-workspace"
case triggerBinding = "trigger-binding"
case volume
case workspace
case workspaceBackAndForth = "workspace-back-and-forth"

Expand Down Expand Up @@ -115,6 +116,8 @@ func initSubcommands() -> [String: any SubCommandParserProtocol] {
}
case .triggerBinding:
result[kind.rawValue] = SubCommandParser(parseTriggerBindingCmdArgs)
case .volume:
result[kind.rawValue] = SubCommandParser(VolumeCmdArgs.init)
case .workspace:
result[kind.rawValue] = SubCommandParser(parseWorkspaceCmdArgs)
case .workspaceBackAndForth:
Expand Down
40 changes: 40 additions & 0 deletions Sources/Common/cmdArgs/impl/VolumeCmdArgs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public struct VolumeCmdArgs: CmdArgs {
public let rawArgs: EquatableNoop<[String]>
public init(rawArgs: [String]) { self.rawArgs = .init(rawArgs) }
public static let parser: CmdParser<Self> = cmdParser(
kind: .volume,
allowInConfig: true,
help: volume_help_generated,
options: [:],
arguments: [newArgParser(\.action, parseVolumeAction, mandatoryArgPlaceholder: VolumeAction.argsUnion)]
)

public var windowId: UInt32?
public var workspaceName: WorkspaceName?

public var action: Lateinit<VolumeAction> = .uninitialized
}

public enum VolumeAction: Equatable {
case up, down, muteToggle, muteOn, muteOff
case set(Int)

static let argsUnion: String = "(up|down|mute-toggle|mute-on|mute-off|set)"
}

func parseVolumeAction(arg: String, nextArgs: inout [String]) -> Parsed<VolumeAction> {
switch arg {
case "up": return .success(.up)
case "down": return .success(.down)
case "mute-toggle": return .success(.muteToggle)
case "mute-off": return .success(.muteOff)
case "mute-on": return .success(.muteOn)
case "set":
guard let arg = nextArgs.nextNonFlagOrNil() else { return .failure("set argument must be followed by <number>") }
guard let int = Int(arg) else { return .failure("Can't parse number '\(arg)'") }
if !(0 ... 100).contains(int) { return .failure("\(int) must be in range from 0 to 100") }
return .success(.set(int))
default:
return .failure("Unknown argument '\(arg)'. Possible values: \(VolumeAction.argsUnion)")
}
}
5 changes: 5 additions & 0 deletions Sources/Common/cmdHelpGenerated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ let summon_workspace_help_generated = """
let trigger_binding_help_generated = """
USAGE: trigger-binding [-h|--help] <binding> --mode <mode-id>
"""
let volume_help_generated = """
USAGE: volume [-h|--help] (up|down)
OR: volume [-h|--help] (mute-toggle|mute-off|mute-on)
OR: volume [-h|--help] set <number>
"""
let workspace_back_and_forth_help_generated = """
USAGE: workspace-back-and-forth [-h|--help]
"""
Expand Down
39 changes: 39 additions & 0 deletions docs/aerospace-volume.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
= aerospace-volume(1)
include::util/man-attributes.adoc[]
// tag::purpose[]
:manpurpose: Manipulate volume
// end::purpose[]
:manname: aerospace-volume

// =========================================================== Synopsis
== Synopsis
[verse]
// tag::synopsis[]
aerospace volume [-h|--help] (up|down)
aerospace volume [-h|--help] (mute-toggle|mute-off|mute-on)
aerospace volume [-h|--help] set <number>

// end::synopsis[]

// =========================================================== Description
== Description

// tag::body[]
{manpurpose}

// =========================================================== Options
include::./util/conditional-options-header.adoc[]

-h, --help:: Print help

// =========================================================== Arguments
include::./util/conditional-arguments-header.adoc[]

(up|down):: Increase or decrease the volume
(mute-toggle|mute-on|mute-off):: Toggle/On/Off mute
set <number>:: Set volume to the exact value on scale from 0 to 100

// end::body[]

// =========================================================== Footer
include::util/man-footer.adoc[]
7 changes: 7 additions & 0 deletions docs/commands.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ include::aerospace-trigger-binding.adoc[tags=synopsis]
include::aerospace-trigger-binding.adoc[tags=purpose]
include::aerospace-trigger-binding.adoc[tags=body]

== volume
----
include::./aerospace-volume.adoc[tags=synopsis]
----
include::./aerospace-volume.adoc[tags=purpose]
include::./aerospace-volume.adoc[tags=body]

== workspace
----
include::aerospace-workspace.adoc[tags=synopsis]
Expand Down
4 changes: 4 additions & 0 deletions docs/config-examples/default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,7 @@ alt-shift-h = ['join-with left', 'mode main']
alt-shift-j = ['join-with down', 'mode main']
alt-shift-k = ['join-with up', 'mode main']
alt-shift-l = ['join-with right', 'mode main']

down = 'volume down'
up = 'volume up'
shift-down = ['volume set 0', 'mode main']
4 changes: 4 additions & 0 deletions grammar/commands-bnf-grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ aerospace -h;
| trigger-binding <binding> --mode <mode_id>
| trigger-binding --mode <mode_id> <binding>

| volume (up|down)
| volume (mute-toggle|mute-on|mute-off)
| volume set <number>

| workspace [--auto-back-and-forth|--fail-if-noop]... <workspace> [--auto-back-and-forth|--fail-if-noop]...
| workspace [--wrap-around] (next|prev) [--wrap-around]

Expand Down
5 changes: 5 additions & 0 deletions legal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ ANTLR is used to parse AeroSpace built-in shell like language.
[swift-collections Apache 2.0 license](./third-party-license/LICENSE-swift-collections.txt).
swift-collections is used for more advanced Swift collections.

**ISSoundAdditions**
[ISSoundAdditions GitHub link](https://github.com/InerziaSoft/ISSoundAdditions).
[ISSoundAdditions MIT license](./third-party-license/LICENSE-ISSoundAdditions.txt).
ISSoundAdditions is used as a convenient API to change system volume.

**Mac OS X Snow Leopard • Wallpaper**.
Created by Fons Mans.
[Figma design file link](https://www.figma.com/community/file/1228988440310597758).
Expand Down
21 changes: 21 additions & 0 deletions legal/third-party-license/LICENSE-ISSoundAdditions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 InerziaSoft - Massimo and Alessio Moiso.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 24e1f08

Please sign in to comment.