finished bluetooth module, cleaned and documented

This commit is contained in:
2022-11-28 06:47:05 +01:00
parent 3e628db637
commit d63ffa1171
2 changed files with 139 additions and 174 deletions

View File

@@ -3,34 +3,39 @@
--------------------------------------
-- Awesome Libs
local awful = require("awful")
local dpi = require("beautiful").xresources.apply_dpi
local gtable = require("gears").table
local gcolor = require("gears").color
local gshape = require("gears").shape
local gfilesystem = require("gears").filesystem
local wibox = require("wibox")
local abutton = require("awful.button")
local aspawn = require("awful.spawn")
local base = require("wibox.widget.base")
local dbus_proxy = require("dbus_proxy")
local lgi = require("lgi")
local dpi = require("beautiful").xresources.apply_dpi
local gcolor = require("gears").color
local gfilesystem = require("gears").filesystem
local gshape = require("gears").shape
local gtable = require("gears").table
local gtimer = require("gears.timer")
local lgi = require("lgi")
local naughty = require("naughty")
local wibox = require("wibox")
local bt_device = require("src.modules.bluetooth.device")
-- Third party libs
local rubato = require("src.lib.rubato")
local icondir = gfilesystem.get_configuration_dir() .. "src/assets/icons/bluetooth/"
-- Own libs
local bt_device = require("src.modules.bluetooth.device")
local dnd_widget = require("awful.widget.toggle_widget")
local icondir = gfilesystem.get_configuration_dir() .. "src/assets/icons/bluetooth/"
local capi = {
awesome = awesome,
mouse = mouse,
mousegrabber = mousegrabber,
}
local bluetooth = { mt = {} }
--#region wibox.widget.base boilerplate
function bluetooth:layout(_, width, height)
if self._private.widget then
return { base.place_widget_at(self._private.widget, 0, 0, width, height) }
@@ -51,36 +56,41 @@ function bluetooth:get_widget()
return self._private.widget
end
--#endregion
---Get the list of paired devices
---@return table devices table of paired devices
function bluetooth:get_paired_devices()
return self:get_children_by_id("connected_device_list")[1].children
end
---Get the list of discovered devices
---@return table devices table of discovered devices
function bluetooth:get_discovered_devices()
return self:get_children_by_id("discovered_device_list")[1].children
end
--- Remove a device by first disconnecting it async then removing it
function bluetooth:remove_device_information(device)
-- Either disconnect async and have to remove the device "twice"
-- or do it sync but awesome freezes for a second or two
print("bruh?")
device:DisconnectAsync(function(_, _, out, err)
print(out, err)
self._private.Adapter1:RemoveDevice(device.object_path)
end)
end
--- Add a new device into the devices list
function bluetooth:add_device(device, object_path)
-- Get a reference to both lists
local plist = self:get_children_by_id("connected_device_list")[1]
local dlist = self:get_children_by_id("discovered_device_list")[1]
-- For the first list check if the device already exists and if its connection state changed
-- if it changed then remove it from the current list and put it into the other one
for _, value in pairs(dlist.children) do
-- I'm not sure why Connected is in both cases true when its a new connection but eh just take it, it works
if value.device.Address:match(device.Address) and (device.Connected ~= value.device.Connected) then
print("Bad ", value.device.Alias)
return
elseif value.device.Address:match(device.Address) and (device.Connected == value.device.Connected) then
print("Good ", value.device.Alias)
dlist:remove_widgets(value)
plist:add(plist:add(bt_device {
device = device,
@@ -92,10 +102,13 @@ function bluetooth:add_device(device, object_path)
return;
end
end
-- Just check if the device already exists in the list
for _, value in pairs(plist.children) do
if value.device.Address:match(device.Address) then return end
end
-- If its paired add it to the paired list
-- else add it to the discovered list
if device.Paired then
plist:add(bt_device {
device = device,
@@ -115,6 +128,8 @@ function bluetooth:add_device(device, object_path)
end
end
---Remove a device from any list
---@param object_path string the object path of the device
function bluetooth:remove_device(object_path)
local plist = self:get_children_by_id("connected_device_list")[1]
local dlist = self:get_children_by_id("discovered_device_list")[1]
@@ -130,27 +145,17 @@ function bluetooth:remove_device(object_path)
end
end
function bluetooth:update_device(new_device, object_path)
for _, device in ipairs(self.devices.paired:get_children()) do
if device.path == object_path then
device.device:update(new_device)
end
end
for _, device in ipairs(self.devices.discovered:get_children()) do
if device.path == object_path then
device.device:update(new_device)
end
end
end
---Start scanning for devices
function bluetooth:scan()
self._private.Adapter1:StartDiscovery()
end
---Stop scanning for devices
function bluetooth:stop_scan()
self._private.Adapter1:StopDiscovery()
end
---Toggle bluetooth on or off
function bluetooth:toggle()
local powered = self._private.Adapter1.Powered
@@ -161,13 +166,18 @@ function bluetooth:toggle()
}
end
--- Open blueman-manager
function bluetooth:open_settings()
awful.spawn("blueman-manager")
aspawn("blueman-manager")
end
---Get a new device proxy and connect a PropertyChanged signal to it and
---add the device to the list
---@param object_path string the object path of the device
function bluetooth:get_device_info(object_path)
if (not object_path) or (not object_path:match("/org/bluez/hci0/dev")) then return end
-- New Device1 proxy
local Device1 = dbus_proxy.Proxy:new {
bus = dbus_proxy.Bus.SYSTEM,
name = "org.bluez",
@@ -175,6 +185,7 @@ function bluetooth:get_device_info(object_path)
path = object_path
}
-- New Properties proxy for the object_path
local Device1Properties = dbus_proxy.Proxy:new {
bus = dbus_proxy.Bus.SYSTEM,
name = "org.bluez",
@@ -182,8 +193,10 @@ function bluetooth:get_device_info(object_path)
path = object_path
}
-- Just return if the Device1 has no name, this usually means random devices with just a mac address
if (not Device1.Name) or (Device1.Name == "") then return end
-- For some reason it notifies twice or thrice
local just_notified = false
local notify_timer = gtimer {
@@ -195,6 +208,7 @@ function bluetooth:get_device_info(object_path)
end
}
-- Connect the PropertyChanged signal to update the device when a property changes and send a notification
Device1Properties:connect_signal(function(_, _, changed_props)
if changed_props["Connected"] ~= nil then
if not just_notified then
@@ -218,6 +232,8 @@ function bluetooth:get_device_info(object_path)
self:add_device(Device1, object_path)
end
---Send a notification
---@param powered boolean the powered state of the adapter
local function send_state_notification(powered)
naughty.notification {
app_icon = gcolor.recolor_image(icondir .. "bluetooth-on.svg", Theme_config.bluetooth_controller.icon_color),
@@ -241,25 +257,20 @@ function bluetooth.new(args)
{
{
{
{
resize = false,
image = gcolor.recolor_image(icondir .. "menu-down.svg",
Theme_config.bluetooth_controller.connected_icon_color),
widget = wibox.widget.imagebox,
valign = "center",
halign = "center",
id = "icon"
},
id = "center",
halign = "center",
resize = false,
image = gcolor.recolor_image(icondir .. "menu-down.svg",
Theme_config.bluetooth_controller.connected_icon_color),
widget = wibox.widget.imagebox,
valign = "center",
widget = wibox.container.place,
halign = "center",
id = "icon"
},
{
{
text = "Paired Devices",
valign = "center",
halign = "center",
widget = wibox.widget.textbox,
id = "device_name"
},
margins = dpi(5),
widget = wibox.container.margin
@@ -267,19 +278,15 @@ function bluetooth.new(args)
id = "connected",
layout = wibox.layout.fixed.horizontal
},
id = "connected_bg",
bg = Theme_config.bluetooth_controller.connected_bg,
fg = Theme_config.bluetooth_controller.connected_fg,
shape = function(cr, width, height)
gshape.rounded_rect(cr, width, height, dpi(4))
end,
shape = Theme_config.bluetooth_controller.connected_shape,
widget = wibox.container.background
},
id = "connected_margin",
widget = wibox.container.margin
},
{
id = "connected_list",
{
{
step = dpi(50),
@@ -294,35 +301,29 @@ function bluetooth.new(args)
},
border_color = Theme_config.bluetooth_controller.con_device_border_color,
border_width = Theme_config.bluetooth_controller.con_device_border_width,
shape = function(cr, width, height)
gshape.partially_rounded_rect(cr, width, height, false, false, true, true, dpi(4))
end,
shape = Theme_config.bluetooth_controller.con_device_shape,
widget = wibox.container.background,
forced_height = 0
forced_height = 0,
id = "connected_list",
},
{
{
{
{
{
resize = false,
image = gcolor.recolor_image(icondir .. "menu-down.svg",
Theme_config.bluetooth_controller.discovered_icon_color),
widget = wibox.widget.imagebox,
valign = "center",
halign = "center",
id = "icon",
},
id = "center",
halign = "center",
resize = false,
image = gcolor.recolor_image(icondir .. "menu-down.svg",
Theme_config.bluetooth_controller.discovered_icon_color),
widget = wibox.widget.imagebox,
valign = "center",
widget = wibox.container.place,
halign = "center",
id = "icon",
},
{
{
text = "Nearby Devices",
valign = "center",
halign = "center",
widget = wibox.widget.textbox,
id = "device_name"
},
margins = dpi(5),
widget = wibox.container.margin
@@ -333,9 +334,7 @@ function bluetooth.new(args)
id = "discovered_bg",
bg = Theme_config.bluetooth_controller.discovered_bg,
fg = Theme_config.bluetooth_controller.discovered_fg,
shape = function(cr, width, height)
gshape.rounded_rect(cr, width, height, dpi(4))
end,
shape = Theme_config.bluetooth_controller.discovered_shape,
widget = wibox.container.background
},
id = "discovered_margin",
@@ -343,7 +342,6 @@ function bluetooth.new(args)
widget = wibox.container.margin
},
{
id = "discovered_list",
{
{
id = "discovered_device_list",
@@ -352,17 +350,15 @@ function bluetooth.new(args)
layout = require("src.lib.overflow_widget.overflow").vertical,
scrollbar_width = 0,
},
id = "margin",
margins = dpi(10),
widget = wibox.container.margin
},
border_color = Theme_config.bluetooth_controller.con_device_border_color,
border_width = Theme_config.bluetooth_controller.con_device_border_width,
shape = function(cr, width, height)
gshape.partially_rounded_rect(cr, width, height, false, false, true, true, dpi(4))
end,
shape = Theme_config.bluetooth_controller.con_device_shape,
widget = wibox.container.background,
forced_height = 0
forced_height = 0,
id = "discovered_list",
},
{
{ -- action buttons
@@ -390,43 +386,36 @@ function bluetooth.new(args)
widget = wibox.container.margin,
margins = dpi(5),
},
shape = function(cr, width, height)
gshape.rounded_rect(cr, width, height, dpi(4))
end,
shape = Theme_config.bluetooth_controller.refresh_shape,
bg = Theme_config.bluetooth_controller.refresh_bg,
id = "scan",
widget = wibox.container.background
},
layout = wibox.layout.align.horizontal
},
id = "marg_dnd",
widget = wibox.container.margin,
top = dpi(10),
},
id = "layout1",
layout = wibox.layout.fixed.vertical
},
id = "margin",
margins = dpi(15),
widget = wibox.container.margin
},
shape = function(cr, width, height)
gshape.rounded_rect(cr, width, height, dpi(8))
end,
shape = Theme_config.bluetooth_controller.shape,
border_color = Theme_config.bluetooth_controller.container_border_color,
border_width = Theme_config.bluetooth_controller.container_border_width,
bg = Theme_config.bluetooth_controller.container_bg,
id = "background",
widget = wibox.container.background
},
width = dpi(400),
forced_width = dpi(400),
strategy = "exact",
widget = wibox.container.constraint
})
-- Get a reference to the dnd button
local dnd = ret:get_children_by_id("dnd")[1]:get_widget()
-- Toggle bluetooth on or off
dnd:connect_signal("dnd::toggle", function(enable)
ret:toggle()
end)
@@ -574,8 +563,9 @@ function bluetooth.new(args)
)
--#endregion
-- Add buttons to the scan button
ret:get_children_by_id("scan")[1]:buttons({
awful.button({}, 1, function()
abutton({}, 1, function()
ret:scan()
end)
})