Skip to content

Commit

Permalink
getvolume in C, barbq
Browse files Browse the repository at this point in the history
  • Loading branch information
bkase committed Dec 24, 2019
1 parent d74b28f commit 88a305f
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 158 deletions.
3 changes: 1 addition & 2 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ tilingShell =

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

externalIpShell :: Text
externalIpShell = "curl -s icanhazip.com | tr -d '\n' || true"
Expand Down
6 changes: 3 additions & 3 deletions barbq2.cabal → barbq.cabal
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cabal-version: >=1.10
-- Initial package description 'barbq2.cabal' generated by 'cabal init'.
-- Initial package description 'barbq.cabal' generated by 'cabal init'.
-- For further documentation, see http://haskell.org/cabal/users-guide/

name: barbq2
name: barbq
version: 0.1.0.0
-- synopsis:
-- description:
Expand All @@ -16,7 +16,7 @@ version: 0.1.0.0
build-type: Simple
extra-source-files: CHANGELOG.md

executable barbq2
executable barbq
main-is: Main.hs
ghc-options: -O2 -flate-specialise -fspecialise-aggressively -threaded -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints -Wno-name-shadowing
other-modules: Barbq.Types Barbq.UI.Framework Barbq.UI.Runtime Barbq.UI.Components
Expand Down
19 changes: 8 additions & 11 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
{ compiler ? "ghc865", pkgs ? import <nixpkgs> {} }:

let

getvolume = pkgs.callPackage ./getvolume/default.nix { };
packageSet = pkgs.haskell.packages.${compiler};
haskellPackages =
packageSet.override {
overrides = (self: super:
{
ghc = super.ghc // { withPackages = super.ghc.withHoogle; };
ghcWithPackages = self.ghc.withPackages;

polysemy = self.polysemy_1_2_1_0;

async-timer = pkgs.haskell.lib.dontCheck super.async-timer;

polysemy-plugin = pkgs.haskell.lib.dontCheck (super.polysemy-plugin.override { polysemy = self.polysemy_1_2_1_0; });
}
);
};
drv = haskellPackages.callCabal2nix "barbq2" ./. {};
raw = haskellPackages.callCabal2nix "barbq" ./. {};
drv = pkgs.symlinkJoin {
name = "barbq";
paths = [ raw getvolume ];
};

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

22 changes: 0 additions & 22 deletions getvolume/Package.swift

This file was deleted.

3 changes: 0 additions & 3 deletions getvolume/README.md

This file was deleted.

52 changes: 0 additions & 52 deletions getvolume/Sources/getvolume/main.swift

This file was deleted.

7 changes: 0 additions & 7 deletions getvolume/Tests/LinuxMain.swift

This file was deleted.

9 changes: 0 additions & 9 deletions getvolume/Tests/getvolumeTests/XCTestManifests.swift

This file was deleted.

47 changes: 0 additions & 47 deletions getvolume/Tests/getvolumeTests/getvolumeTests.swift

This file was deleted.

14 changes: 14 additions & 0 deletions getvolume/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation rec {
name = "barbq-getvolume";
version = "0.2";
src = ./.;
buildPhase = ''
clang main.c -framework CoreAudio -framework AudioToolbox
'';
installPhase = ''
mkdir -p $out/bin
mv a.out $out/bin/barbq-getvolume
'';
buildInputs = [ pkgs.darwin.apple_sdk.frameworks.CoreAudio pkgs.darwin.apple_sdk.frameworks.AudioToolbox ];
}
72 changes: 72 additions & 0 deletions getvolume/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <AudioToolbox/AudioToolbox.h>
#include <stdio.h>
#include <math.h>

AudioDeviceID getCurrentlySelectedDeviceID() {
UInt32 propertySize;
AudioDeviceID deviceID = kAudioDeviceUnknown;

AudioObjectPropertyAddress pa;
pa.mScope = kAudioObjectPropertyScopeGlobal;
pa.mElement = kAudioObjectPropertyElementMaster;
pa.mSelector = kAudioHardwarePropertyDefaultOutputDevice;

// get the default output device
propertySize = sizeof(deviceID);

OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &pa, 0, NULL, &propertySize, &deviceID);
if (err != noErr) {
fprintf(stderr, "Failed to get default deviceid\n");
}
return deviceID;
}

UInt32 isMuted(AudioDeviceID deviceID) {
UInt32 propertySize;

AudioObjectPropertyAddress pa;
pa.mSelector = kAudioDevicePropertyMute;
pa.mScope = kAudioObjectPropertyScopeOutput;
pa.mElement = kAudioObjectPropertyElementMaster;

UInt32 isMuted = 0;
propertySize = sizeof(isMuted);

OSStatus err = AudioObjectGetPropertyData(deviceID, &pa, 0, NULL, &propertySize, &isMuted);
if (err != noErr) {
fprintf(stderr, "Failed to get mute data\n");
}

return isMuted;
}

void getDeviceVolume(AudioDeviceID deviceID, float *volume) {
UInt32 propertySize = sizeof(float);

AudioObjectPropertyAddress pa;
pa.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
pa.mScope = kAudioDevicePropertyScopeOutput;
pa.mElement = kAudioObjectPropertyElementMaster;

OSStatus err = AudioObjectGetPropertyData(
deviceID,
&pa,
0,
NULL,
&propertySize,
volume);
if (err != noErr) {
fprintf(stderr, "Failed to get volume data\n");
return;
}
}

int main() {
AudioDeviceID deviceID = getCurrentlySelectedDeviceID();
UInt32 mute = isMuted(deviceID);
float volume = 100.0;
getDeviceVolume(deviceID, &volume);
printf("%d,%d\n", (int)round(volume*100.0), mute);
return 0;
}

4 changes: 2 additions & 2 deletions hie.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cradle:
cabal:
component: "exe:barbq2"
component: "exe:barbq"

dependencies:
- barbq2.cabal
- barbq.cabal
- shell.nix
- default.nix

0 comments on commit 88a305f

Please sign in to comment.