From a431c4a25707f2c1d7f126f06e03b7c47862f347 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 12 Sep 2017 14:19:05 +0200 Subject: [PATCH] Added music stuff --- awesome/music.lua | 107 ++++++++++++++++++++++++++++++++++++++++++++++ awesome/rc.lua | 22 ++++++---- 2 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 awesome/music.lua diff --git a/awesome/music.lua b/awesome/music.lua new file mode 100644 index 0000000..7b25eb8 --- /dev/null +++ b/awesome/music.lua @@ -0,0 +1,107 @@ +local naughty = require("naughty") +local wibox = require("wibox") +local awful = require("awful") + +local ret = {} +ret.widget = wibox.widget.textbox() +ret.widget:set_align("right") +ret.widget.font = "Ubuntu Mono" + +local total_text = "" +local position = 1 +local width = 30 +local dots = 3 + +local last_notification = nil + +local function string_starts_with(String,Start) + return string.sub(String,1,string.len(Start))==Start +end + +local function string_split(str, delimiter) + if str == nil then + return {} + end + local ret = {''} + count = 1 + for i = 1, string.len(str) do + local c = str:sub(i,i) + if c == delimiter then + count = count + 1 + ret[count] = '' + else + ret[count] = ret[count] .. c + end + end + return ret +end + +local function pad(str, len, char) + if char == nil then char = ' ' end + return str .. string.rep(char, len - #str) +end + +local function set_text(text) + + total_text = text[1] + + if last_notification ~= nil then + naughty.destroy(last_notification) + end + + if text[2] == nil then + return + end + + elts = string_split(text[1], '-') + artist = string.sub(elts[1], 1, -1) + album = string.sub(elts[2], 2, -1) + song = string.sub(elts[3], 2) + notification_text = '\n' .. album .. '\n' .. song .. '\n' + + if string.len(total_text) >= width then + position = math.floor((width - string.len(total_text)) / 2) + end + + update_widget() +end + +ret.execute_command = function(command) + local fd = io.popen('music-client ' .. command) + local text = string_split(fd:read('*all'), '\n') + local rc = {fd:close()} + if rc[3] == 0 then + if string_starts_with(command, 'file') or string_starts_with(command, 'next') or string_starts_with(command, 'previous') then + set_text(text) + elseif string_starts_with(command, 'noop') then + total_text = text[1] + position = math.floor((width - string.len(total_text)) / 2) + end + else + set_text({'music-server not running'}) + end +end + +function update_widget() + if string.len(total_text) < width then + ret.widget:set_text(pad(total_text, width, ' ')) + else + local pos = math.min(position, string.len(total_text) - width) + pos = math.max(1, pos) + ret.widget:set_text(string.sub(total_text, pos, pos + width)) + position = position + 1 + if position > string.len(total_text) then + position = math.floor((width - string.len(total_text)) / 2) + end + end +end + +ret.execute_command('noop') + +ret.widget.timer = timer({timeout=0.2}) +ret.widget.timer:connect_signal("timeout", update_widget) +ret.widget.timer:start() + +ret.widget = ret.widget + +return ret diff --git a/awesome/rc.lua b/awesome/rc.lua index 3f3e73b..f30ab2a 100644 --- a/awesome/rc.lua +++ b/awesome/rc.lua @@ -18,6 +18,13 @@ home = os.getenv('HOME') os.execute('xset m 16/1 0') os.setlocale("fr_FR.UTF-8") +-- Set maximum size for notifactions +naughty.config.notify_callback = function(args) + if args.icon_size == nil or args.icon_size > 128 then + args.icon_size = 128 + end + return args +end -- awful.util.spawn_with_shell("xcompmgr -cF &") naughty.config.presets.normal.bg = "#000000" @@ -180,6 +187,7 @@ local battery = require('battery') local volume = require('volume') local calendar = require('calendar') local launchbar = require('launchbar') +local music = require('music') local delimiter = wibox.widget.textbox(" | ") local function set_wallpaper(s) @@ -238,6 +246,8 @@ awful.screen.connect_for_each_screen(function(s) { -- Right widgets layout = wibox.layout.fixed.horizontal, delimiter, + -- music.widget, + -- delimiter, volume_widget, delimiter, battery_widget, @@ -388,23 +398,19 @@ globalkeys = awful.util.table.join( end, {description = "Shutdown", group="custom"}), awful.key({ }, "XF86AudioPlay", function() - awful.util.spawn("music-client play", false) - update_volume() + music.execute_command('play') end, {description = "play or pause the current music", group="custom"}), awful.key({ }, "XF86AudioStop", function() - awful.util.spawn("music-client stop", false) - update_volume() + music.execute_command('stop') end, {description = "stop the current music", group="custom"}), awful.key({ }, "XF86AudioNext", function() - awful.util.spawn("music-client next", false) - update_volume() + music.execute_command('next') end, {description = "skip to the next music", group="custom"}), awful.key({ }, "XF86AudioPrev", function() - awful.util.spawn("music-client previous", false) - update_volume() + music.execute_command('previous') end, {description = "skip to the previous music", group="custom"}), awful.key({ }, "Print", function ()