diff --git a/awesome/rc.lua b/awesome/rc.lua index 07f5359..98be6fe 100644 --- a/awesome/rc.lua +++ b/awesome/rc.lua @@ -16,4 +16,5 @@ require("src.core.rules") require("src.bindings.global_buttons") require("src.bindings.bind_to_tags") require("src.modules.init") ---require("src.tools.auto_starter")(user_vars.autostart) +require("src.tools.auto_starter")(user_vars.autostart) +require("src.dbus.bluetooth_dbus")() diff --git a/awesome/src/assets/icons/notifications/spotify.svg b/awesome/src/assets/icons/notifications/spotify.svg new file mode 100644 index 0000000..e702de1 --- /dev/null +++ b/awesome/src/assets/icons/notifications/spotify.svg @@ -0,0 +1 @@ + diff --git a/awesome/src/dbus/bluetooth_dbus.lua b/awesome/src/dbus/bluetooth_dbus.lua index 0c2d013..8511ebd 100644 --- a/awesome/src/dbus/bluetooth_dbus.lua +++ b/awesome/src/dbus/bluetooth_dbus.lua @@ -1,52 +1,124 @@ +local gears = require("gears") local dbus_proxy = require("dbus_proxy") +local lgi = require("lgi") local naughty = require("naughty") return function() - local function get_device_info(self, object_path) + local function get_device_info(object_path) if object_path ~= nil and object_path:match("/org/bluez/hci0/dev") then - local device_proxy = dbus_proxy.Proxy:new { + local device = dbus_proxy.Proxy:new { bus = dbus_proxy.Bus.SYSTEM, name = "org.bluez", interface = "org.bluez.Device1", path = object_path } - local bat_proxy = dbus_proxy.Proxy:new { + local battery = dbus_proxy.Proxy:new { bus = dbus_proxy.Bus.SYSTEM, name = "org.bluez", interface = "org.bluez.Battery1", path = object_path } - local device_properties_proxy = dbus_proxy.Proxy:new { + local device_properties = dbus_proxy.Proxy:new { bus = dbus_proxy.Bus.SYSTEM, name = "org.bluez", interface = "org.freedesktop.DBus.Properties", path = object_path } - if device_proxy.Name ~= "" and device_proxy.Name ~= nil then - --[[ device_properties_proxy:connect_signal("PropertiesChanged", function() - awesome.emit_signal(object_path .. "_updated", device_proxy) - naughty.notify({ title = "hi" }) - end) ]] - naughty.notify({ title = device_proxy.Name }) + if device.Name ~= "" and device.Name ~= nil then + device_properties:connect_signal(function() + naughty.notification { + title = "Bluetooth Device Connected", + message = device.Name, + icon = require("awful").util.getdir("config") .. "src/assets/icons/bluetooth/bluetooth.svg" + } + awesome.emit_signal("device_added", object_path, device, battery) + end, "PropertiesChanged") end end end - local ret = dbus_proxy.Proxy:new { + local ObjectManager = dbus_proxy.Proxy:new { bus = dbus_proxy.Bus.SYSTEM, name = "org.bluez", - interface = "org.freedesktop.Dbus.ObjectManager", + interface = "org.freedesktop.DBus.ObjectManager", path = "/" } - local objects = ret:GetManagedObjects() + local Adapter = dbus_proxy.Proxy:new { + bus = dbus_proxy.Bus.SYSTEM, + name = "org.bluez", + interface = "org.bluez.Adapter1", + path = "/org/bluez/hci0" + } - for object_path, _ in pairs(objects) do - get_device_info(ret, object_path) - end + local AdapterProperties = dbus_proxy.Proxy:new { + bus = dbus_proxy.Bus.SYSTEM, + name = "org.bluez", + interface = "org.freedesktop.DBus.Properties", + path = "/org/bluez/hci0" + } + + ObjectManager:connect_signal( + function(interface) + get_device_info(interface) + end, + "InterfacesAdded" + ) + + ObjectManager:connect_signal( + function(interface) + awesome.emit_signal("device_removed", interface) + end, + "InterfacesRemoved" + ) + + Adapter:connect_signal( + function(data) + if data.Powered ~= nil then + awesome.emit_signal("state", data.Powered) + end + end, + "PropertiesChanged" + ) + + AdapterProperties:connect_signal( + function(data) + if data.Powered ~= nil then + awesome.emit_signal("state", data.Powered) + if data.Powered then + Adapter:StartDiscovery() + end + end + end, + "PropertiesChanged" + ) + + awesome.connect_signal("toggle_bluetooth", + function() + local is_powered = Adapter.Powered + Adapter:Set( + "org.bluez.Adapter1", + "Powered", + lgi.GLib.Variant("b", not is_powered) + ) + Adapter.Powered = { signature = "b", value = not is_powered } + awesome.emit_signal("state", Adapter.Powered) + end) + + gears.timer.delayed_call( + function() + local objects = ObjectManager:GetManagedObjects() + + for object_path, _ in pairs(objects) do + get_device_info(object_path) + end + + awesome.emit_signal("state", Adapter.Powered) + end + ) end diff --git a/awesome/src/widgets/bluetooth.lua b/awesome/src/widgets/bluetooth.lua index 0d22343..93405e1 100644 --- a/awesome/src/widgets/bluetooth.lua +++ b/awesome/src/widgets/bluetooth.lua @@ -40,90 +40,21 @@ return function() end, widget = wibox.container.background } - - local bluetooth_tooltip = awful.tooltip { - objects = { bluetooth_widget }, - text = "", - mode = "inside", - preferred_alignments = "middle", - margins = dpi(10) - } - - local bluetooth_state = "off" - local connected_device = "nothing" - - awful.widget.watch( - "rfkill list bluetooth", - 5, - function(_, stdout) - local icon = icondir .. "bluetooth" - if stdout:match('Soft blocked: yes') or stdout:gsub("\n", "") == '' then - icon = icon .. "-off" - bluetooth_state = "off" - bluetooth_tooltip:set_text("Bluetooth is turned " .. bluetooth_state .. "\n") - else - icon = icon .. "-on" - bluetooth_state = "on" - awful.spawn.easy_async_with_shell( - './.config/awesome/src/scripts/bt.sh', - function(stdout2) - if stdout2 == nil or stdout2:gsub("\n", "") == "" then - bluetooth_tooltip:set_text("Bluetooth is turned " .. bluetooth_state .. "\n" .. "You are currently not connected") - else - connected_device = stdout2:gsub("%(", ""):gsub("%)", "") - bluetooth_tooltip:set_text("Bluetooth is turned " .. bluetooth_state .. "\n" .. "You are currently connected to:\n" .. connected_device) - end - end - ) - end - bluetooth_widget.icon_margin.icon_layout.icon:set_image(gears.color.recolor_image(icon .. ".svg", color["Grey900"])) - end, - bluetooth_widget - ) - - -- Signals + -- Hover signal to change color when mouse is over Hover_signal(bluetooth_widget, color["Blue200"], color["Grey900"]) + awesome.connect_signal("state", function(state) + if state then + bluetooth_widget:get_children_by_id("icon")[1]:set_image(gears.color.recolor_image(icondir .. "bluetooth-on.svg", color["Grey900"])) + else + bluetooth_widget:get_children_by_id("icon")[1]:set_image(gears.color.recolor_image(icondir .. "bluetooth-off.svg", color["Grey900"])) + end + end) + bluetooth_widget:connect_signal( "button::press", function() - awful.spawn.easy_async_with_shell( - "rfkill list bluetooth", - function(stdout) - if stdout:gsub("\n", "") ~= '' then - if bluetooth_state == "off" then - awful.spawn.easy_async_with_shell( - [[ - rfkill unblock bluetooth - sleep 1 - bluetoothctl power on - ]] , - function() - naughty.notification { - title = "System Notification", - app_name = "Bluetooth", - message = "Bluetooth activated" - } - end - ) - else - awful.spawn.easy_async_with_shell( - [[ - bluetoothctl power off - rfkill block bluetooth - ]] , - function() - naughty.notification { - title = "System Notification", - app_name = "Bluetooth", - message = "Bluetooth deactivated" - } - end - ) - end - end - end - ) + awesome.emit_signal("toggle_bluetooth") end )