Skip to content

Commit 88a305f

Browse files
committed
getvolume in C, barbq
1 parent d74b28f commit 88a305f

12 files changed

+100
-158
lines changed

Main.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ tilingShell =
8888

8989
-- scripts taken from ubersicht status widget via chunkwm sample ubersicht
9090
volumeShell :: Text
91-
volumeShell =
92-
[r| /Users/bkase/barbq2/getvolume/.build/release/getvolume |]
91+
volumeShell = "barbq-getvolume"
9392

9493
externalIpShell :: Text
9594
externalIpShell = "curl -s icanhazip.com | tr -d '\n' || true"

barbq2.cabal barbq.cabal

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cabal-version: >=1.10
2-
-- Initial package description 'barbq2.cabal' generated by 'cabal init'.
2+
-- Initial package description 'barbq.cabal' generated by 'cabal init'.
33
-- For further documentation, see http://haskell.org/cabal/users-guide/
44

5-
name: barbq2
5+
name: barbq
66
version: 0.1.0.0
77
-- synopsis:
88
-- description:
@@ -16,7 +16,7 @@ version: 0.1.0.0
1616
build-type: Simple
1717
extra-source-files: CHANGELOG.md
1818

19-
executable barbq2
19+
executable barbq
2020
main-is: Main.hs
2121
ghc-options: -O2 -flate-specialise -fspecialise-aggressively -threaded -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints -Wno-name-shadowing
2222
other-modules: Barbq.Types Barbq.UI.Framework Barbq.UI.Runtime Barbq.UI.Components

default.nix

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
{ compiler ? "ghc865", pkgs ? import <nixpkgs> {} }:
22

33
let
4-
4+
getvolume = pkgs.callPackage ./getvolume/default.nix { };
55
packageSet = pkgs.haskell.packages.${compiler};
66
haskellPackages =
77
packageSet.override {
88
overrides = (self: super:
99
{
1010
ghc = super.ghc // { withPackages = super.ghc.withHoogle; };
1111
ghcWithPackages = self.ghc.withPackages;
12-
13-
polysemy = self.polysemy_1_2_1_0;
14-
1512
async-timer = pkgs.haskell.lib.dontCheck super.async-timer;
16-
17-
polysemy-plugin = pkgs.haskell.lib.dontCheck (super.polysemy-plugin.override { polysemy = self.polysemy_1_2_1_0; });
1813
}
1914
);
2015
};
21-
drv = haskellPackages.callCabal2nix "barbq2" ./. {};
16+
raw = haskellPackages.callCabal2nix "barbq" ./. {};
17+
drv = pkgs.symlinkJoin {
18+
name = "barbq";
19+
paths = [ raw getvolume ];
20+
};
2221

2322
in
2423
{
2524
barbq = drv;
2625
barbq-shell = haskellPackages.shellFor {
27-
packages = p: [drv];
26+
packages = p: [raw];
2827
shellHook = ''
2928
export HIE_HOOGLE_DATABASE="$(cat $(which hoogle) | sed -n -e 's|.*--database \(.*\.hoo\).*|\1|p')"
30-
31-
pushd getvolume && swift build -c release && popd
3229
'';
33-
buildInputs = with pkgs; [ cabal-install hlint ghcid ] ++ (if pkgs.stdenv.isDarwin then [] else [ swift ]) ;
30+
buildInputs = with pkgs; [ cabal-install hlint ghcid getvolume ] ;
3431
};
3532
}
3633

getvolume/Package.swift

-22
This file was deleted.

getvolume/README.md

-3
This file was deleted.

getvolume/Sources/getvolume/main.swift

-52
This file was deleted.

getvolume/Tests/LinuxMain.swift

-7
This file was deleted.

getvolume/Tests/getvolumeTests/XCTestManifests.swift

-9
This file was deleted.

getvolume/Tests/getvolumeTests/getvolumeTests.swift

-47
This file was deleted.

getvolume/default.nix

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{pkgs ? import <nixpkgs> {} }:
2+
pkgs.stdenv.mkDerivation rec {
3+
name = "barbq-getvolume";
4+
version = "0.2";
5+
src = ./.;
6+
buildPhase = ''
7+
clang main.c -framework CoreAudio -framework AudioToolbox
8+
'';
9+
installPhase = ''
10+
mkdir -p $out/bin
11+
mv a.out $out/bin/barbq-getvolume
12+
'';
13+
buildInputs = [ pkgs.darwin.apple_sdk.frameworks.CoreAudio pkgs.darwin.apple_sdk.frameworks.AudioToolbox ];
14+
}

getvolume/main.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <AudioToolbox/AudioToolbox.h>
2+
#include <stdio.h>
3+
#include <math.h>
4+
5+
AudioDeviceID getCurrentlySelectedDeviceID() {
6+
UInt32 propertySize;
7+
AudioDeviceID deviceID = kAudioDeviceUnknown;
8+
9+
AudioObjectPropertyAddress pa;
10+
pa.mScope = kAudioObjectPropertyScopeGlobal;
11+
pa.mElement = kAudioObjectPropertyElementMaster;
12+
pa.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
13+
14+
// get the default output device
15+
propertySize = sizeof(deviceID);
16+
17+
OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &pa, 0, NULL, &propertySize, &deviceID);
18+
if (err != noErr) {
19+
fprintf(stderr, "Failed to get default deviceid\n");
20+
}
21+
return deviceID;
22+
}
23+
24+
UInt32 isMuted(AudioDeviceID deviceID) {
25+
UInt32 propertySize;
26+
27+
AudioObjectPropertyAddress pa;
28+
pa.mSelector = kAudioDevicePropertyMute;
29+
pa.mScope = kAudioObjectPropertyScopeOutput;
30+
pa.mElement = kAudioObjectPropertyElementMaster;
31+
32+
UInt32 isMuted = 0;
33+
propertySize = sizeof(isMuted);
34+
35+
OSStatus err = AudioObjectGetPropertyData(deviceID, &pa, 0, NULL, &propertySize, &isMuted);
36+
if (err != noErr) {
37+
fprintf(stderr, "Failed to get mute data\n");
38+
}
39+
40+
return isMuted;
41+
}
42+
43+
void getDeviceVolume(AudioDeviceID deviceID, float *volume) {
44+
UInt32 propertySize = sizeof(float);
45+
46+
AudioObjectPropertyAddress pa;
47+
pa.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
48+
pa.mScope = kAudioDevicePropertyScopeOutput;
49+
pa.mElement = kAudioObjectPropertyElementMaster;
50+
51+
OSStatus err = AudioObjectGetPropertyData(
52+
deviceID,
53+
&pa,
54+
0,
55+
NULL,
56+
&propertySize,
57+
volume);
58+
if (err != noErr) {
59+
fprintf(stderr, "Failed to get volume data\n");
60+
return;
61+
}
62+
}
63+
64+
int main() {
65+
AudioDeviceID deviceID = getCurrentlySelectedDeviceID();
66+
UInt32 mute = isMuted(deviceID);
67+
float volume = 100.0;
68+
getDeviceVolume(deviceID, &volume);
69+
printf("%d,%d\n", (int)round(volume*100.0), mute);
70+
return 0;
71+
}
72+

hie.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cradle:
22
cabal:
3-
component: "exe:barbq2"
3+
component: "exe:barbq"
44

55
dependencies:
6-
- barbq2.cabal
6+
- barbq.cabal
77
- shell.nix
88
- default.nix

0 commit comments

Comments
 (0)