SNI: Add config

This commit is contained in:
scorpion-26 2023-05-03 20:57:24 +02:00
parent 67f048cd01
commit 31e070a3da
9 changed files with 108 additions and 49 deletions

View file

@ -71,6 +71,9 @@ NetworkAdapter: eno1
# Disables the network widget when set to false
NetworkWidget: true
# Enables tray icons
EnableSNI: true
# These set the range for the network widget. The widget changes colors at six intervals:
# - Below Min...Bytes ("under")
# - Between ]0%;25%]. 0% = Min...Bytes; 100% = Max...Bytes ("low")

View file

@ -2,7 +2,7 @@ project('gBar',
['c', 'cpp'],
version: '0.0.1',
license: 'MIT',
meson_version: '>=0.49.0',
meson_version: '>=0.53.0',
default_options: ['cpp_std=c++17',
'warning_level=3',
'default_library=static',
@ -23,32 +23,11 @@ ext_workspace_header = custom_target('generate-ext-workspace-header',
output: ['ext-workspace-unstable-v1.h'],
command: [wayland_scanner, 'client-header', '@INPUT@', '@OUTPUT@'])
# DBus protocols
dbus_gen = find_program('gdbus-codegen')
sni_item_src = custom_target('generate-sni-item-src',
input: ['protocols/sni-item.xml'],
output: ['sni-item.c'],
command: [dbus_gen, '--c-namespace', 'sni', '--body', '--output', '@OUTPUT@', '@INPUT@'])
sni_item_header = custom_target('generate-sni-item-header',
input: ['protocols/sni-item.xml'],
output: ['sni-item.h'],
command: [dbus_gen, '--c-namespace', 'sni', '--header', '--output', '@OUTPUT@', '@INPUT@'])
sni_watcher_src = custom_target('generate-sni-watcher-src',
input: ['protocols/sni-watcher.xml'],
output: ['sni-watcher.c'],
command: [dbus_gen, '--c-namespace', 'sni', '--body', '--output', '@OUTPUT@', '@INPUT@'])
sni_watcher_header = custom_target('generate-sni-watcher-header',
input: ['protocols/sni-watcher.xml'],
output: ['sni-watcher.h'],
command: [dbus_gen, '--c-namespace', 'sni', '--header', '--output', '@OUTPUT@', '@INPUT@'])
libdbusmenu = dependency('dbusmenu-gtk3-0.4')
gtk = dependency('gtk+-3.0')
gtk_layer_shell = dependency('gtk-layer-shell-0')
pulse = dependency('libpulse')
headers = [
'src/Common.h',
'src/Log.h',
@ -60,6 +39,25 @@ headers = [
'src/CSS.h'
]
sources = [
ext_workspace_src,
ext_workspace_header,
'src/Window.cpp',
'src/Widget.cpp',
'src/System.cpp',
'src/Bar.cpp',
'src/Workspaces.cpp',
'src/AudioFlyin.cpp',
'src/BluetoothDevices.cpp',
'src/Plugin.cpp',
'src/Config.cpp',
'src/CSS.cpp',
'src/Log.cpp',
'src/SNI.cpp',
]
dependencies = [gtk, gtk_layer_shell, pulse, wayland_client ]
if get_option('WithHyprland')
add_global_arguments('-DWITH_HYPRLAND', language: 'cpp')
headers += 'src/Workspaces.h'
@ -82,35 +80,54 @@ endif
if get_option('WithSys')
add_global_arguments('-DWITH_SYS', language: 'cpp')
endif
if get_option('WithSNI')
add_global_arguments('-DWITH_SNI', language: 'cpp')
# DBus protocols
dbus_gen = find_program('gdbus-codegen')
sni_item_src = custom_target('generate-sni-item-src',
input: ['protocols/sni-item.xml'],
output: ['sni-item.c'],
command: [dbus_gen, '--c-namespace', 'sni', '--body', '--output', '@OUTPUT@', '@INPUT@'])
sni_item_header = custom_target('generate-sni-item-header',
input: ['protocols/sni-item.xml'],
output: ['sni-item.h'],
command: [dbus_gen, '--c-namespace', 'sni', '--header', '--output', '@OUTPUT@', '@INPUT@'])
sni_watcher_src = custom_target('generate-sni-watcher-src',
input: ['protocols/sni-watcher.xml'],
output: ['sni-watcher.c'],
command: [dbus_gen, '--c-namespace', 'sni', '--body', '--output', '@OUTPUT@', '@INPUT@'])
sni_watcher_header = custom_target('generate-sni-watcher-header',
input: ['protocols/sni-watcher.xml'],
output: ['sni-watcher.h'],
command: [dbus_gen, '--c-namespace', 'sni', '--header', '--output', '@OUTPUT@', '@INPUT@'])
libdbusmenu = dependency('dbusmenu-gtk3-0.4')
sources += sni_item_src
sources += sni_item_header
sources += sni_watcher_src
sources += sni_watcher_header
dependencies += libdbusmenu
endif
add_global_arguments('-DUSE_LOGFILE', language: 'cpp')
pulse = dependency('libpulse')
# stb
fs = import('fs')
stb = include_directories('thirdparty')
if fs.exists('thirdparty/stb/stb_image.h')
add_global_arguments('-DHAS_STB', language: 'cpp')
endif
libgBar = library('gBar',
[ ext_workspace_src,
ext_workspace_header,
sni_item_src,
sni_item_header,
sni_watcher_src,
sni_watcher_header,
'src/Window.cpp',
'src/Widget.cpp',
'src/System.cpp',
'src/Bar.cpp',
'src/Workspaces.cpp',
'src/AudioFlyin.cpp',
'src/BluetoothDevices.cpp',
'src/Plugin.cpp',
'src/Config.cpp',
'src/CSS.cpp',
'src/Log.cpp',
'src/SNI.cpp',
],
dependencies: [gtk, gtk_layer_shell, pulse, wayland_client, libdbusmenu],
sources,
dependencies: dependencies,
include_directories: stb,
install: true)

View file

@ -4,6 +4,9 @@ option('WithHyprland', type: 'boolean', value : true)
# Workspaces general, enables Wayland protocol
option('WithWorkspaces', type: 'boolean', value : true)
# Tray icons, requires stb git submodule
option('WithSNI', type: 'boolean', value : true)
option('WithNvidia', type: 'boolean', value : true)
option('WithAMD', type: 'boolean', value : true)
option('WithBlueZ', type: 'boolean', value : true)

View file

@ -647,7 +647,9 @@ namespace Bar
right->SetSpacing({8, false});
right->SetHorizontalTransform({-1, true, Alignment::Right});
{
#ifdef WITH_SNI
SNI::WidgetSNI(*right);
#endif
WidgetPackages(*right);

View file

@ -147,6 +147,7 @@ void Config::Load()
AddConfigVar("WorkspaceScrollOnMonitor", config.workspaceScrollOnMonitor, lineView, foundProperty);
AddConfigVar("WorkspaceScrollInvert", config.workspaceScrollInvert, lineView, foundProperty);
AddConfigVar("UseHyprlandIPC", config.useHyprlandIPC, lineView, foundProperty);
AddConfigVar("EnableSNI", config.enableSNI, lineView, foundProperty);
AddConfigVar("MinUploadBytes", config.minUploadBytes, lineView, foundProperty);
AddConfigVar("MaxUploadBytes", config.maxUploadBytes, lineView, foundProperty);

View file

@ -27,6 +27,7 @@ public:
bool workspaceScrollOnMonitor = true; // Scroll through workspaces on monitor instead of all
bool workspaceScrollInvert = false; // Up = +1, instead of Up = -1
bool useHyprlandIPC = false; // Use Hyprland IPC instead of ext_workspaces protocol (Less buggy, but also less performant)
bool enableSNI = true; // Enable tray icon
// Controls for color progression of the network widget
uint32_t minUploadBytes = 0; // Bottom limit of the network widgets upload. Everything below it is considered "under"
@ -73,6 +74,12 @@ public:
bool hasBlueZ = false;
#endif
#if defined WITH_SNI && defined HAS_STB
bool hasSNI = true;
#else
bool hasSNI = false;
#endif
bool hasNet = true;
bool hasPackagesScript = true;

View file

@ -1,6 +1,9 @@
#include "SNI.h"
#include "Log.h"
#include "Widget.h"
#include "Config.h"
#ifdef WITH_SNI
#include <sni-watcher.h>
#include <sni-item.h>
@ -245,6 +248,11 @@ namespace SNI
static TimerResult UpdateWidgets(Box&)
{
if (RuntimeConfig::Get().hasSNI == false || Config::Get().enableSNI == false)
{
// Don't bother
return TimerResult::Delete;
}
for (auto& client : clientsToQuery)
{
LOG("SNI: Creating Item " << client.name << " " << client.object);
@ -324,6 +332,10 @@ namespace SNI
void WidgetSNI(Widget& parent)
{
if (RuntimeConfig::Get().hasSNI == false || Config::Get().enableSNI == false)
{
return;
}
// Add parent box
auto box = Widget::Create<Box>();
auto container = Widget::Create<Box>();
@ -386,13 +398,19 @@ namespace SNI
void Init()
{
if (RuntimeConfig::Get().hasSNI == false || Config::Get().enableSNI == false)
{
return;
}
auto busAcquired = [](GDBusConnection* connection, const char*, void*)
{
GError* err = nullptr;
g_dbus_interface_skeleton_export((GDBusInterfaceSkeleton*)watcherSkeleton, connection, "/StatusNotifierWatcher", &err);
if (err)
{
LOG("Failed to connect to dbus! Error: " << err->message);
LOG("SNI: Failed to connect to dbus! Disabling SNI. Error: " << err->message);
RuntimeConfig::Get().hasSNI = false;
g_error_free(err);
return;
}
@ -409,9 +427,10 @@ namespace SNI
sni_watcher_set_is_status_notifier_host_registered(watcherSkeleton, true);
};
auto emptyCallback = [](GDBusConnection*, const char*, void*) {};
auto lostName = [](GDBusConnection*, const char*, void*)
auto lostName = [](GDBusConnection*, const char* msg, void*)
{
LOG("Lost name!");
LOG("SNI: Lost Name! Disabling SNI!");
RuntimeConfig::Get().hasSNI = false;
};
auto flags = G_BUS_NAME_OWNER_FLAGS_REPLACE | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
g_bus_own_name(G_BUS_TYPE_SESSION, "org.kde.StatusNotifierWatcher", (GBusNameOwnerFlags)flags, +busAcquired, +emptyCallback, +lostName,
@ -429,3 +448,4 @@ namespace SNI
void Shutdown() {}
}
#endif

View file

@ -1,4 +1,5 @@
#pragma once
#ifdef WITH_SNI
class Widget;
namespace SNI
{
@ -6,3 +7,4 @@ namespace SNI
void WidgetSNI(Widget& parent);
void Shutdown();
}
#endif

View file

@ -647,7 +647,9 @@ namespace System
PulseAudio::Init();
#ifdef WITH_SNI
SNI::Init();
#endif
CheckNetwork();
}
@ -665,7 +667,9 @@ namespace System
#ifdef WITH_BLUEZ
StopBTScan();
#endif
#ifdef WITH_SNI
SNI::Shutdown();
#endif
Logging::Shutdown();
}