diff options
Diffstat (limited to 'pkgs/util')
| -rw-r--r-- | pkgs/util/default.nix | 44 | ||||
| -rw-r--r-- | pkgs/util/util.lua | 35 | ||||
| -rw-r--r-- | pkgs/util/util/brightness.lua | 40 | ||||
| -rw-r--r-- | pkgs/util/util/music.lua | 124 | ||||
| -rw-r--r-- | pkgs/util/util/volume.lua | 47 |
5 files changed, 290 insertions, 0 deletions
diff --git a/pkgs/util/default.nix b/pkgs/util/default.nix new file mode 100644 index 0000000..9dbe067 --- /dev/null +++ b/pkgs/util/default.nix @@ -0,0 +1,44 @@ +{pkgs ? import <nixpkgs> {system = "x86_64-linux";}, ...}: let + lsdbus-src = pkgs.fetchFromGitHub { + owner = "kmarkus"; + repo = "lsdbus"; + rev = "6f80930248110927d1f898966544eec202ea3550"; + hash = "sha256-N/VCjrI7RolHV0ya1U+YTLGAWv8+WNz2nobaGxdR1wQ="; + }; + + lsdbus = pkgs.stdenv.mkDerivation { + pname = "lsdbus"; + version = "scm-3"; + src = lsdbus-src; + nativeBuildInputs = with pkgs; [cmake gnumake pkg-config systemdLibs minixml lua5_4]; + cmakeFlags = ["-DCONFIG_LUA_VER=5.4"]; + }; + + lua = + pkgs.runCommand "lua-env" { + buildInputs = with pkgs; [lua5_4]; + nativeBuildInputs = [pkgs.makeWrapper]; + } '' + mkdir -p $out/bin + install -D -m 755 ${pkgs.lua5_4}/bin/lua $out/bin/lua + wrapProgram $out/bin/lua \ + --prefix LUA_PATH \; '?.lua;?/init.lua' \ + --prefix LUA_PATH \; '${lsdbus}/share/lua/5.4/?.lua' \ + --prefix LUA_PATH \; '${lsdbus}/share/lua/5.4/?/init.lua' \ + --prefix LUA_CPATH \; '${lsdbus}/lib/lua/5.4/?.so' + ''; +in + pkgs.stdenv.mkDerivation { + pname = "util.lua"; + version = "1.0.0"; + src = ./.; + buildInputs = [pkgs.brightnessctl]; + nativeBuildInputs = [pkgs.makeWrapper]; + + buildPhase = '' + mkdir -p $out/{bin,lib} + substitute $src/util.lua $out/bin/util.lua --replace-fail @lua@ ${lua} --replace-fail @out@ $out + cp -r $src/util $out/lib/util + chmod +x $out/bin/util.lua + ''; + } diff --git a/pkgs/util/util.lua b/pkgs/util/util.lua new file mode 100644 index 0000000..ade7019 --- /dev/null +++ b/pkgs/util/util.lua @@ -0,0 +1,35 @@ +#!@lua@/bin/lua +local lsdbus = require("lsdbus") + +local music = dofile"@out@/lib/util/music.lua" +local brightness = dofile"@out@/lib/util/brightness.lua" +local volume = dofile"@out@/lib/util/volume.lua" + +local b = lsdbus.open() +local s = lsdbus.open("system") + +if arg[1] == "music" then + if arg[2] == "toggle" then + music.toggle_play(b) + elseif arg[2] == "next" then + music.next_song(b) + elseif arg[2] == "prev" then + music.prev_song(b) + end +elseif arg[1] == "brightness" then + if arg[2] == "up" then + brightness.brightness_up(s) + elseif arg[2] == "down" then + brightness.brightness_down(s) + else + error(("arg[2] '%s' incorrect format. Should be `up` or `down`"):format(tostring(arg[2]))) + end +elseif arg[1] == "volume" then + if arg[2] == "up" then + volume.volume_up() + elseif arg[2] == "down" then + volume.volume_down() + else + error(("arg[2] '%s' incorrect format. Should be `up` or `down`"):format(tostring(arg[2]))) + end +end diff --git a/pkgs/util/util/brightness.lua b/pkgs/util/util/brightness.lua new file mode 100644 index 0000000..13a7974 --- /dev/null +++ b/pkgs/util/util/brightness.lua @@ -0,0 +1,40 @@ +local lsdbus = require("lsdbus") + +local function get_brightness() + local proc = io.open("/sys/class/backlight/intel_backlight/brightness") + local cur_brightness = tonumber(proc:read("*l")) + return cur_brightness +end + +local function get_max_brightness() + local proc = io.open("/sys/class/backlight/intel_backlight/max_brightness") + local max_brightness = tonumber(proc:read("*l")) + return max_brightness +end + +local function notify_brightness() + local brightness_percent = math.floor((get_brightness() / get_max_brightness()) * 100) + os.execute(string.format( + "dunstify -t 2000 -h string:x-canonical-private-synchronous:brightness 'Brightness' -h int:value:%d", + brightness_percent + )) +end + +local M = {} + +function set_brightness(s, brightness) + local brightness_proxy = lsdbus.proxy.new(s, 'org.freedesktop.login1', '/org/freedesktop/login1/session/1', "org.freedesktop.login1.Session") + brightness_proxy("SetBrightness", "backlight", "intel_backlight", brightness) +end + +function M.brightness_down(s, brightness) + set_brightness(s, get_brightness() - 5*(get_max_brightness() / 100)) + notify_brightness() +end + +function M.brightness_up(s, brightness) + set_brightness(s, get_brightness() + 5*(get_max_brightness() / 100)) + notify_brightness() +end + +return M diff --git a/pkgs/util/util/music.lua b/pkgs/util/util/music.lua new file mode 100644 index 0000000..c9dd726 --- /dev/null +++ b/pkgs/util/util/music.lua @@ -0,0 +1,124 @@ +local lsdbus = require("lsdbus") + +function list_players(b) + local dbus = lsdbus.proxy.new(b, + 'org.freedesktop.DBus', + '/org/freedesktop/DBus', + 'org.freedesktop.DBus' + ) + local all_services = dbus("ListNames") + + local players = {} + for _, name in ipairs(all_services) do + if name:match("^org.mpris.MediaPlayer2") then + table.insert(players, name) + end + end + return players +end + +local function get_player_proxy(b, player) + local ok, res = pcall(function() return lsdbus.proxy.new(b, + player, + '/org/mpris/MediaPlayer2', + 'org.mpris.MediaPlayer2.Player' + ) end) + if ok then + return res + end +end + +local function list_playing_players(b) + local playing = {} + for _, player in ipairs(list_players(b)) do + local proxy = get_player_proxy(b, player) + if proxy and proxy.PlaybackStatus == "Playing" then + -- print(player) + table.insert(playing, player) + end + end + return playing +end + +local function solo_player(b, player) + for _, i in list_playing_players(b) do + if i ~= player then + get_player_proxy(b, player)"Pause" + end + end +end + +local STATE_FILE = os.getenv("HOME") .. "/.local/state/music_player_paused" +local function write_state(state_file, player) + local f, _ = io.open(state_file, "w") + f:write(player.."\n") + f:close() +end + +local function read_state(state_file) + local f, _ = io.open(state_file, "r") + local read = f:read("*l") + f:close() + return read +end + +local M = {} + +local function notify_music(b, player) + local player_proxy = lsdbus.proxy.new(b, player, '/org/mpris/MediaPlayer2', 'org.mpris.MediaPlayer2.Player') + local meta = player_proxy.Metadata + + os.execute(string.format( + "dunstify -t 2000 -h string:x-canonical-private-synchronous:music -i '%s' '%s' '%s' -h int:value:%d", + meta["mpris:artUrl"] or "", + player_proxy.PlaybackStatus .. " " .. meta["xesam:title"] or "", + "by " .. meta["xesam:artist"][1], + math.floor(player_proxy.Position / meta["mpris:length"]) + )) +end + +function M.toggle_play(b) + local current_playing = list_playing_players(b) + local state = read_state(STATE_FILE) + + if #current_playing == 0 and state ~= "" then + local p = get_player_proxy(b, state) + if p then + p("Play") + notify_music(b, state) + end + return + end + + if #current_playing == 1 then + local c = current_playing[1] + write_state(STATE_FILE, c) + get_player_proxy(b, c)("Pause") + notify_music(b, c) + return + end + + if #current_playing > 1 then + for _, player in ipairs(current_playing) do + get_player_proxy(b, player)"Pause" + end + end +end + +function M.next_song(b) + local currently_playing = list_playing_players(b) + if #currently_playing ~= 0 then + local player_proxy = lsdbus.proxy.new(b, currently_playing[1], '/org/mpris/MediaPlayer2', 'org.mpris.MediaPlayer2.Player') + player_proxy("Next") + end +end + +function M.prev_song(b) + local currently_playing = list_playing_players(b) + if #currently_playing ~= 0 then + local player_proxy = lsdbus.proxy.new(b, currently_playing[1], '/org/mpris/MediaPlayer2', 'org.mpris.MediaPlayer2.Player') + player_proxy("Previous") + end +end + +return M diff --git a/pkgs/util/util/volume.lua b/pkgs/util/util/volume.lua new file mode 100644 index 0000000..750f67c --- /dev/null +++ b/pkgs/util/util/volume.lua @@ -0,0 +1,47 @@ +local function get_volume() + local handle = io.popen("wpctl get-volume @DEFAULT_AUDIO_SINK@") + local out = handle:read("*a") + handle:close() + + local num = out:match("^Volume:%s*([%d%.]+)") + if not num then + error("Unexpected wpctl output: " .. out) + end + + return tonumber(num) * 100 +end + +local function notify_volume() + os.execute(string.format( + "dunstify -t 2000 -h string:x-canonical-private-synchronous:audio 'Volume' -h int:value:%d", + math.floor(get_volume()) + )) +end + +local M = {} + +function M.set_volume(vol) + os.execute(string.format("wpctl set-volume @DEFAULT_AUDIO_SINK@ %.2f", vol / 100)) + notify_volume() +end + +function M.volume_up(step) + step = step or 5 + local current = get_volume() + local new_volume = math.min(150, current + step) + M.set_volume(new_volume) +end + +function M.volume_down(step) + step = step or 5 + local current = get_volume() + local new_volume = math.max(0, current - step) + M.set_volume(new_volume) +end + +function M.toggle_mute() + os.execute("wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle") + notify_volume() +end + +return M |
