From 1dc557456eb7418b6d97af36274a2575df64915b Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 09:05:34 +0000 Subject: [PATCH 01/14] Add missing includes for libc++ In file included from ../src/modules/custom.cpp:1: In file included from ../include/modules/custom.hpp:7: ../include/util/command.hpp:15:25: error: implicit instantiation of undefined template 'std::__1::array' std::array buffer = {0}; ^ ../src/modules/pulseaudio.cpp:175:41: error: implicit instantiation of undefined template 'std::__1::array, 9>' static const std::array ports = { ^ /usr/include/c++/v1/__tuple:223:64: note: template is declared here template struct _LIBCPP_TEMPLATE_VIS array; ^ In file included from ../src/factory.cpp:1: In file included from ../include/factory.hpp:8: ../include/modules/sway/workspaces.hpp:39:8: error: no template named 'unordered_map' in namespace 'std' std::unordered_map buttons_; ~~~~~^ ../src/factory.cpp:20:14: error: cannot initialize return object of type 'waybar::AModule *' with an rvalue of type 'waybar::modules::sway::Workspaces *' return new waybar::modules::sway::Workspaces(id, bar_, config_[name]); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- include/modules/pulseaudio.hpp | 1 + include/modules/sway/workspaces.hpp | 1 + include/util/command.hpp | 1 + 3 files changed, 3 insertions(+) diff --git a/include/modules/pulseaudio.hpp b/include/modules/pulseaudio.hpp index 541747c2..5f17620a 100644 --- a/include/modules/pulseaudio.hpp +++ b/include/modules/pulseaudio.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "ALabel.hpp" namespace waybar::modules { diff --git a/include/modules/sway/workspaces.hpp b/include/modules/sway/workspaces.hpp index fef24bf9..5454b9e3 100644 --- a/include/modules/sway/workspaces.hpp +++ b/include/modules/sway/workspaces.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "AModule.hpp" #include "bar.hpp" #include "client.hpp" diff --git a/include/util/command.hpp b/include/util/command.hpp index f3dd4e1a..62feec78 100644 --- a/include/util/command.hpp +++ b/include/util/command.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace waybar::util::command { From fe2dd1e8435f200c9a8432c935c84592c5f50902 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 10:00:52 +0000 Subject: [PATCH 02/14] build: handle systems where libc++ is default ld: error: unable to find library -lstdc++fs ld: error: unable to find library -lc++abi --- meson.build | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index d785dfd2..aefd3563 100644 --- a/meson.build +++ b/meson.build @@ -17,11 +17,13 @@ cpp_link_args = [] if get_option('libcxx') cpp_args += ['-stdlib=libc++'] cpp_link_args += ['-stdlib=libc++', '-lc++abi'] +endif - if compiler.has_link_argument('-lc++fs') - cpp_link_args += ['-lc++fs'] - endif -else +if compiler.has_link_argument('-lc++fs') + cpp_link_args += ['-lc++fs'] +elif compiler.has_link_argument('-lc++experimental') + cpp_link_args += ['-lc++experimental'] +elif compiler.has_link_argument('-lstdc++fs') cpp_link_args += ['-lstdc++fs'] endif From 74db69dcb7cc9ef46c6036d0474c42e90a47fb82 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 17:45:28 +0000 Subject: [PATCH 03/14] build: drop libinput as it was never used --- README.md | 1 - meson.build | 2 -- 2 files changed, 3 deletions(-) diff --git a/README.md b/README.md index c7ef77b7..f7bc4c89 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ $ waybar ``` gtkmm3 jsoncpp -libinput libsigc++ fmt wayland diff --git a/meson.build b/meson.build index aefd3563..63139035 100644 --- a/meson.build +++ b/meson.build @@ -67,7 +67,6 @@ add_global_arguments(cpp_args, language : 'cpp') add_global_link_arguments(cpp_link_args, language : 'cpp') thread_dep = dependency('threads') -libinput = dependency('libinput') fmt = dependency('fmt', version : ['>=5.3.0'], fallback : ['fmt', 'fmt_dep']) spdlog = dependency('spdlog', version : ['>=1.3.1'], fallback : ['spdlog', 'spdlog_dep']) wayland_client = dependency('wayland-client') @@ -180,7 +179,6 @@ executable( spdlog, sigcpp, jsoncpp, - libinput, wayland_cursor, gtkmm, dbusmenu_gtk, From c94ef092ff2aa59b08cb673c007ee732de3dbeb7 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Sun, 11 Aug 2019 10:09:55 +0000 Subject: [PATCH 04/14] build: drop -Dout in favor of --prefix $ meson --prefix=/tmp/foo _build $ ninja install -C _build [49/50] Installing files. Installing waybar to /tmp/foo/bin Traceback (most recent call last): File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 127, in run return options.run_func(options) File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 514, in run installer.do_install(datafilename) File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 346, in do_install self.install_data(d) File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 375, in install_data d.dirmaker.makedirs(outdir, exist_ok=True) File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 55, in makedirs os.makedirs(path, exist_ok=exist_ok) File "/usr/lib/python3.6/os.py", line 210, in makedirs makedirs(head, mode, exist_ok) File "/usr/lib/python3.6/os.py", line 220, in makedirs mkdir(name, mode) PermissionError: [Errno 13] Permission denied: '/etc/xdg' FAILED: meson-install --- meson.build | 5 ++++- meson_options.txt | 1 - src/client.cpp | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index 63139035..0b2aa154 100644 --- a/meson.build +++ b/meson.build @@ -89,9 +89,12 @@ systemd = dependency('systemd', required: get_option('systemd')) tz_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'tz_dep' ]) prefix = get_option('prefix') +sysconfdir = get_option('sysconfdir') conf_data = configuration_data() conf_data.set('prefix', prefix) +add_project_arguments('-DSYSCONFDIR="/@0@"'.format(join_paths(prefix, sysconfdir)), language : 'cpp') + if systemd.found() user_units_dir = systemd.get_pkgconfig_variable('systemduserunitdir') @@ -198,7 +201,7 @@ executable( install_data( './resources/config', './resources/style.css', - install_dir: join_paths(get_option('out'), 'etc/xdg/waybar') + install_dir: sysconfdir + '/xdg/waybar' ) scdoc = dependency('scdoc', version: '>=1.9.2', native: true, required: get_option('man-pages')) diff --git a/meson_options.txt b/meson_options.txt index 8d442c77..a44ff648 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -6,5 +6,4 @@ option('systemd', type: 'feature', value: 'auto', description: 'Install systemd option('dbusmenu-gtk', type: 'feature', value: 'auto', description: 'Enable support for tray') option('man-pages', type: 'feature', value: 'auto', description: 'Generate and install man pages') option('mpd', type: 'feature', value: 'auto', description: 'Enable support for the Music Player Daemon') -option('out', type: 'string', value : '/', description: 'output prefix directory') option('gtk-layer-shell', type: 'feature', value: 'auto', description: 'Use gtk-layer-shell library for popups support') diff --git a/src/client.cpp b/src/client.cpp index 5e76b3ce..e0515d4f 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -161,7 +161,7 @@ std::tuple waybar::Client::getConfigs( "$XDG_CONFIG_HOME/waybar/config", "$HOME/.config/waybar/config", "$HOME/waybar/config", - "/etc/xdg/waybar/config", + SYSCONFDIR "/xdg/waybar/config", "./resources/config", }) : config; @@ -169,7 +169,7 @@ std::tuple waybar::Client::getConfigs( "$XDG_CONFIG_HOME/waybar/style.css", "$HOME/.config/waybar/style.css", "$HOME/waybar/style.css", - "/etc/xdg/waybar/style.css", + SYSCONFDIR "/xdg/waybar/style.css", "./resources/style.css", }) : style; From d5df185ac6435a46e4640e1d63a235f2cd00fd2b Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 09:07:58 +0000 Subject: [PATCH 05/14] cpu: make getCpuLoad more portable ../include/modules/cpu.hpp:4:10: fatal error: 'sys/sysinfo.h' file not found #include ^~~~~~~~~~~~~~~ --- include/modules/cpu.hpp | 2 +- src/modules/cpu.cpp | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 32883952..1b490faf 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index a0f646f5..79b84d52 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -23,11 +23,9 @@ auto waybar::modules::Cpu::update() -> void { } uint16_t waybar::modules::Cpu::getCpuLoad() { - struct sysinfo info = {0}; - if (sysinfo(&info) == 0) { - float f_load = 1.F / (1U << SI_LOAD_SHIFT); - uint16_t load = info.loads[0] * f_load * 100 / get_nprocs(); - return load; + double load[1]; + if (getloadavg(load, 1) != -1) { + return load[0] * 100 / sysconf(_SC_NPROCESSORS_ONLN); } throw std::runtime_error("Can't get Cpu load"); } From cfd7577e1b570b36b91dd0b0e0da4a86ae3a4292 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 09:52:16 +0000 Subject: [PATCH 06/14] backlight: require libepoll on non-Linux ../src/modules/backlight.cpp:9:10: fatal error: 'sys/epoll.h' file not found #include ^~~~~~~~~~~~~ --- meson.build | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 0b2aa154..a8b91dc3 100644 --- a/meson.build +++ b/meson.build @@ -66,6 +66,8 @@ endif add_global_arguments(cpp_args, language : 'cpp') add_global_link_arguments(cpp_link_args, language : 'cpp') +is_linux = host_machine.system() == 'linux' + thread_dep = dependency('threads') fmt = dependency('fmt', version : ['>=5.3.0'], fallback : ['fmt', 'fmt_dep']) spdlog = dependency('spdlog', version : ['>=1.3.1'], fallback : ['spdlog', 'spdlog_dep']) @@ -77,6 +79,7 @@ dbusmenu_gtk = dependency('dbusmenu-gtk3-0.4', required: get_option('dbusmenu-gt giounix = dependency('gio-unix-2.0', required: get_option('dbusmenu-gtk')) jsoncpp = dependency('jsoncpp') sigcpp = dependency('sigc++-2.0') +libepoll = dependency('epoll-shim', required: false) libnl = dependency('libnl-3.0', required: get_option('libnl')) libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl')) libpulse = dependency('libpulse', required: get_option('pulseaudio')) @@ -155,7 +158,7 @@ if dbusmenu_gtk.found() ) endif -if libudev.found() +if libudev.found() and (is_linux or libepoll.found()) add_project_arguments('-DHAVE_LIBUDEV', language: 'cpp') src_files += 'src/modules/backlight.cpp' endif @@ -190,6 +193,7 @@ executable( libnlgen, libpulse, libudev, + libepoll, libmpdclient, gtk_layer_shell, tz_dep From 496e7825441cba05939090de4a9bc94d6292b02c Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 09:52:16 +0000 Subject: [PATCH 07/14] battery: limit to Linux due to /sys/class/power_supply ../include/modules/battery.hpp:9:10: fatal error: 'sys/inotify.h' file not found #include ^~~~~~~~~~~~~~~ --- include/factory.hpp | 2 +- meson.build | 7 ++++++- src/factory.cpp | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/factory.hpp b/include/factory.hpp index b14b9988..a337335b 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -7,7 +7,7 @@ #include "modules/sway/window.hpp" #include "modules/sway/workspaces.hpp" #endif -#ifndef NO_FILESYSTEM +#if defined(__linux__) && !defined(NO_FILESYSTEM) #include "modules/battery.hpp" #endif #include "modules/cpu.hpp" diff --git a/meson.build b/meson.build index a8b91dc3..d1528c44 100644 --- a/meson.build +++ b/meson.build @@ -114,7 +114,6 @@ src_files = files( 'src/AModule.cpp', 'src/ALabel.cpp', 'src/modules/memory.cpp', - 'src/modules/battery.cpp', 'src/modules/bluetooth.cpp', 'src/modules/clock.cpp', 'src/modules/custom.cpp', @@ -128,6 +127,12 @@ src_files = files( 'src/util/rfkill.cpp' ) +if is_linux + src_files += files( + 'src/modules/battery.cpp', + ) +endif + if true # find_program('sway', required : false).found() add_project_arguments('-DHAVE_SWAY', language: 'cpp') src_files += [ diff --git a/src/factory.cpp b/src/factory.cpp index 16a6903a..73c9f9f7 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -7,7 +7,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { auto hash_pos = name.find('#'); auto ref = name.substr(0, hash_pos); auto id = hash_pos != std::string::npos ? name.substr(hash_pos + 1) : ""; -#ifndef NO_FILESYSTEM +#if defined(__linux__) && !defined(NO_FILESYSTEM) if (ref == "battery") { return new waybar::modules::Battery(id, config_[name]); } From c844d7ac2e70945234d169356866b2c1d965e274 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Sun, 11 Aug 2019 15:23:37 +0000 Subject: [PATCH 08/14] tray: drop std::filesystem dependency --- include/factory.hpp | 2 +- include/modules/sni/item.hpp | 5 ----- src/factory.cpp | 2 +- src/modules/sni/item.cpp | 8 +++----- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/include/factory.hpp b/include/factory.hpp index a337335b..a5515b8f 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -14,7 +14,7 @@ #include "modules/idle_inhibitor.hpp" #include "modules/memory.hpp" #include "modules/disk.hpp" -#if defined(HAVE_DBUSMENU) && !defined(NO_FILESYSTEM) +#ifdef HAVE_DBUSMENU #include "modules/sni/tray.hpp" #endif #ifdef HAVE_LIBNL diff --git a/include/modules/sni/item.hpp b/include/modules/sni/item.hpp index fc04673e..3cbd0b74 100644 --- a/include/modules/sni/item.hpp +++ b/include/modules/sni/item.hpp @@ -10,11 +10,6 @@ #include #include #include -#ifdef FILESYSTEM_EXPERIMENTAL -#include -#else -#include -#endif namespace waybar::modules::SNI { diff --git a/src/factory.cpp b/src/factory.cpp index 73c9f9f7..263071b5 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -38,7 +38,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { if (ref == "disk") { return new waybar::modules::Disk(id, config_[name]); } -#if defined(HAVE_DBUSMENU) && !defined(NO_FILESYSTEM) +#ifdef HAVE_DBUSMENU if (ref == "tray") { return new waybar::modules::SNI::Tray(id, bar_, config_[name]); } diff --git a/src/modules/sni/item.cpp b/src/modules/sni/item.cpp index b298e442..abf95560 100644 --- a/src/modules/sni/item.cpp +++ b/src/modules/sni/item.cpp @@ -1,6 +1,7 @@ #include "modules/sni/item.hpp" #include #include +#include template <> struct fmt::formatter : formatter { @@ -256,11 +257,8 @@ void Item::updateImage() { if (!icon_name.empty()) { try { // Try to find icons specified by path and filename -#ifdef FILESYSTEM_EXPERIMENTAL - if (std::experimental::filesystem::exists(icon_name)) { -#else - if (std::filesystem::exists(icon_name)) { -#endif + std::ifstream temp(icon_name); + if (temp.is_open()) { auto pixbuf = Gdk::Pixbuf::create_from_file(icon_name); if (pixbuf->gobj() != nullptr) { // An icon specified by path and filename may be the wrong size for From c4f7cdeec45f3493834a27267818e7f7b8a81c5c Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 10:40:33 +0000 Subject: [PATCH 09/14] memory: port parseMeminfo to BSDs --- include/factory.hpp | 2 + include/modules/memory.hpp | 1 - meson.build | 14 ++- src/factory.cpp | 2 + src/modules/memory/bsd.cpp | 89 +++++++++++++++++++ src/modules/{memory.cpp => memory/common.cpp} | 18 ---- src/modules/memory/linux.cpp | 20 +++++ 7 files changed, 126 insertions(+), 20 deletions(-) create mode 100644 src/modules/memory/bsd.cpp rename src/modules/{memory.cpp => memory/common.cpp} (78%) create mode 100644 src/modules/memory/linux.cpp diff --git a/include/factory.hpp b/include/factory.hpp index a5515b8f..dfd0cc7f 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -12,7 +12,9 @@ #endif #include "modules/cpu.hpp" #include "modules/idle_inhibitor.hpp" +#if defined(HAVE_MEMORY_LINUX) || defined(HAVE_MEMORY_BSD) #include "modules/memory.hpp" +#endif #include "modules/disk.hpp" #ifdef HAVE_DBUSMENU #include "modules/sni/tray.hpp" diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index 59f0e787..abfe2872 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -15,7 +15,6 @@ class Memory : public ALabel { auto update() -> void; private: - static inline const std::string data_dir_ = "/proc/meminfo"; void parseMeminfo(); std::unordered_map meminfo_; diff --git a/meson.build b/meson.build index d1528c44..5dd3b1b9 100644 --- a/meson.build +++ b/meson.build @@ -67,6 +67,10 @@ add_global_arguments(cpp_args, language : 'cpp') add_global_link_arguments(cpp_link_args, language : 'cpp') is_linux = host_machine.system() == 'linux' +is_dragonfly = host_machine.system() == 'dragonfly' +is_freebsd = host_machine.system() == 'freebsd' +is_netbsd = host_machine.system() == 'netbsd' +is_openbsd = host_machine.system() == 'openbsd' thread_dep = dependency('threads') fmt = dependency('fmt', version : ['>=5.3.0'], fallback : ['fmt', 'fmt_dep']) @@ -113,7 +117,6 @@ src_files = files( 'src/factory.cpp', 'src/AModule.cpp', 'src/ALabel.cpp', - 'src/modules/memory.cpp', 'src/modules/bluetooth.cpp', 'src/modules/clock.cpp', 'src/modules/custom.cpp', @@ -128,8 +131,17 @@ src_files = files( ) if is_linux + add_project_arguments('-DHAVE_MEMORY_LINUX', language: 'cpp') src_files += files( 'src/modules/battery.cpp', + 'src/modules/memory/common.cpp', + 'src/modules/memory/linux.cpp', + ) +elif is_dragonfly or is_freebsd or is_netbsd or is_openbsd + add_project_arguments('-DHAVE_MEMORY_BSD', language: 'cpp') + src_files += files( + 'src/modules/memory/bsd.cpp', + 'src/modules/memory/common.cpp', ) endif diff --git a/src/factory.cpp b/src/factory.cpp index 263071b5..4b3f3377 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -26,9 +26,11 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { if (ref == "idle_inhibitor") { return new waybar::modules::IdleInhibitor(id, bar_, config_[name]); } +#if defined(HAVE_MEMORY_LINUX) || defined(HAVE_MEMORY_BSD) if (ref == "memory") { return new waybar::modules::Memory(id, config_[name]); } +#endif if (ref == "cpu") { return new waybar::modules::Cpu(id, config_[name]); } diff --git a/src/modules/memory/bsd.cpp b/src/modules/memory/bsd.cpp new file mode 100644 index 00000000..b3520d0c --- /dev/null +++ b/src/modules/memory/bsd.cpp @@ -0,0 +1,89 @@ +#include "modules/memory.hpp" + +#include +#include +#include // getpagesize + +#if defined(__DragonFly__) +# include // struct vmstats +#elif defined(__NetBSD__) +# include // struct uvmexp_sysctl +#elif defined(__OpenBSD__) +# include // struct uvmexp +#endif + +static uint64_t get_total_memory() { +#if defined(HW_MEMSIZE) || defined(HW_PHYSMEM64) + uint64_t physmem; +#else + u_long physmem; +#endif + int mib[] = { + CTL_HW, +#if defined(HW_MEMSIZE) + HW_MEMSIZE, +#elif defined(HW_PHYSMEM64) + HW_PHYSMEM64, +#else + HW_PHYSMEM, +#endif + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + size_t sz = sizeof(physmem); + if (sysctl(mib, miblen, &physmem, &sz, NULL, 0)) { + throw std::runtime_error("sysctl hw.physmem failed"); + } + return physmem; +} + +static uint64_t get_free_memory() { +#if defined(__DragonFly__) + struct vmstats vms; + size_t sz = sizeof(vms); + if (sysctlbyname("vm.vmstats", &vms, &sz, NULL, 0)) { + throw std::runtime_error("sysctl vm.vmstats failed"); + } + return static_cast + (vms.v_free_count + vms.v_inactive_count + vms.v_cache_count) + * getpagesize(); +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + u_int v_free_count = 0, v_inactive_count = 0, v_cache_count = 0; + size_t sz = sizeof(u_int); + sysctlbyname("vm.stats.vm.v_free_count", + &v_free_count, &sz, NULL, 0); + sysctlbyname("vm.stats.vm.v_inactive_count", + &v_inactive_count, &sz, NULL, 0); + sysctlbyname("vm.stats.vm.v_cache_count", + &v_cache_count, &sz, NULL, 0); + return static_cast + (v_free_count + v_inactive_count + v_cache_count) + * getpagesize(); +#elif defined(__NetBSD__) || defined(__OpenBSD__) +#ifdef VM_UVMEXP2 +# undef VM_UVMEXP +# define VM_UVMEXP VM_UVMEXP2 +# define uvmexp uvmexp_sysctl +#else +# define filepages vnodepages +# define execpages vtextpages +#endif + int mib[] = { + CTL_VM, + VM_UVMEXP, + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + struct uvmexp uvmexp; + size_t sz = sizeof(uvmexp); + if (sysctl(mib, miblen, &uvmexp, &sz, NULL, 0)) { + throw std::runtime_error("sysctl vm.uvmexp failed"); + } + return static_cast + (uvmexp.free + uvmexp.inactive + uvmexp.filepages + uvmexp.execpages) + * uvmexp.pagesize; +#endif +} + +void waybar::modules::Memory::parseMeminfo() { + meminfo_["MemTotal"] = get_total_memory() / 1024; + meminfo_["MemAvailable"] = get_free_memory() / 1024; +} diff --git a/src/modules/memory.cpp b/src/modules/memory/common.cpp similarity index 78% rename from src/modules/memory.cpp rename to src/modules/memory/common.cpp index 8e54d273..4875ec8f 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory/common.cpp @@ -45,21 +45,3 @@ auto waybar::modules::Memory::update() -> void { // Call parent update ALabel::update(); } - -void waybar::modules::Memory::parseMeminfo() { - std::ifstream info(data_dir_); - if (!info.is_open()) { - throw std::runtime_error("Can't open " + data_dir_); - } - std::string line; - while (getline(info, line)) { - auto posDelim = line.find(':'); - if (posDelim == std::string::npos) { - continue; - } - - std::string name = line.substr(0, posDelim); - int64_t value = std::stol(line.substr(posDelim + 1)); - meminfo_[name] = value; - } -} diff --git a/src/modules/memory/linux.cpp b/src/modules/memory/linux.cpp new file mode 100644 index 00000000..75f05fe3 --- /dev/null +++ b/src/modules/memory/linux.cpp @@ -0,0 +1,20 @@ +#include "modules/memory.hpp" + +void waybar::modules::Memory::parseMeminfo() { + const std::string data_dir_ = "/proc/meminfo"; + std::ifstream info(data_dir_); + if (!info.is_open()) { + throw std::runtime_error("Can't open " + data_dir_); + } + std::string line; + while (getline(info, line)) { + auto posDelim = line.find(':'); + if (posDelim == std::string::npos) { + continue; + } + + std::string name = line.substr(0, posDelim); + int64_t value = std::stol(line.substr(posDelim + 1)); + meminfo_[name] = value; + } +} From 4e567d0483481191ac019b3531b10f6afcc9688a Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Sun, 11 Aug 2019 13:10:37 +0000 Subject: [PATCH 10/14] cpu: port parseCpuinfo to BSDs --- include/factory.hpp | 2 + include/modules/cpu.hpp | 1 - meson.build | 7 +- src/factory.cpp | 2 + src/modules/cpu/bsd.cpp | 97 +++++++++++++++++++++++++ src/modules/{cpu.cpp => cpu/common.cpp} | 28 ------- src/modules/cpu/linux.cpp | 29 ++++++++ 7 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 src/modules/cpu/bsd.cpp rename src/modules/{cpu.cpp => cpu/common.cpp} (68%) create mode 100644 src/modules/cpu/linux.cpp diff --git a/include/factory.hpp b/include/factory.hpp index dfd0cc7f..e584b251 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -10,7 +10,9 @@ #if defined(__linux__) && !defined(NO_FILESYSTEM) #include "modules/battery.hpp" #endif +#if defined(HAVE_CPU_LINUX) || defined(HAVE_CPU_BSD) #include "modules/cpu.hpp" +#endif #include "modules/idle_inhibitor.hpp" #if defined(HAVE_MEMORY_LINUX) || defined(HAVE_MEMORY_BSD) #include "modules/memory.hpp" diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 1b490faf..7a703364 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -20,7 +20,6 @@ class Cpu : public ALabel { auto update() -> void; private: - static inline const std::string data_dir_ = "/proc/stat"; uint16_t getCpuLoad(); std::tuple getCpuUsage(); std::vector> parseCpuinfo(); diff --git a/meson.build b/meson.build index 5dd3b1b9..5a8570c2 100644 --- a/meson.build +++ b/meson.build @@ -120,7 +120,6 @@ src_files = files( 'src/modules/bluetooth.cpp', 'src/modules/clock.cpp', 'src/modules/custom.cpp', - 'src/modules/cpu.cpp', 'src/modules/disk.cpp', 'src/modules/idle_inhibitor.cpp', 'src/modules/temperature.cpp', @@ -131,15 +130,21 @@ src_files = files( ) if is_linux + add_project_arguments('-DHAVE_CPU_LINUX', language: 'cpp') add_project_arguments('-DHAVE_MEMORY_LINUX', language: 'cpp') src_files += files( 'src/modules/battery.cpp', + 'src/modules/cpu/common.cpp', + 'src/modules/cpu/linux.cpp', 'src/modules/memory/common.cpp', 'src/modules/memory/linux.cpp', ) elif is_dragonfly or is_freebsd or is_netbsd or is_openbsd + add_project_arguments('-DHAVE_CPU_BSD', language: 'cpp') add_project_arguments('-DHAVE_MEMORY_BSD', language: 'cpp') src_files += files( + 'src/modules/cpu/bsd.cpp', + 'src/modules/cpu/common.cpp', 'src/modules/memory/bsd.cpp', 'src/modules/memory/common.cpp', ) diff --git a/src/factory.cpp b/src/factory.cpp index 4b3f3377..88a1322b 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -31,9 +31,11 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { return new waybar::modules::Memory(id, config_[name]); } #endif +#if defined(HAVE_CPU_LINUX) || defined(HAVE_CPU_BSD) if (ref == "cpu") { return new waybar::modules::Cpu(id, config_[name]); } +#endif if (ref == "clock") { return new waybar::modules::Clock(id, config_[name]); } diff --git a/src/modules/cpu/bsd.cpp b/src/modules/cpu/bsd.cpp new file mode 100644 index 00000000..73ab1e8a --- /dev/null +++ b/src/modules/cpu/bsd.cpp @@ -0,0 +1,97 @@ +#include "modules/cpu.hpp" + +#include +#include +#include // malloc +#include // sysconf + +#if defined(__NetBSD__) || defined(__OpenBSD__) +# include +#else +# include +#endif + +#if defined(__NetBSD__) +typedef uint64_t cp_time_t; +#else +typedef long cp_time_t; +#endif +#if defined(__NetBSD__) || defined(__OpenBSD__) +typedef uint64_t pcp_time_t; +#else +typedef long pcp_time_t; +#endif + +std::vector> waybar::modules::Cpu::parseCpuinfo() { + cp_time_t sum_cp_time[CPUSTATES]; + size_t sum_sz = sizeof(sum_cp_time); + int ncpu = sysconf(_SC_NPROCESSORS_CONF); + size_t sz = CPUSTATES * (ncpu + 1) * sizeof(pcp_time_t); + pcp_time_t *cp_time = static_cast(malloc(sz)), *pcp_time = cp_time; +#if defined(__NetBSD__) + int mib[] = { + CTL_KERN, + KERN_CP_TIME, + }; + if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), sum_cp_time, &sum_sz, NULL, 0)) { + throw std::runtime_error("sysctl kern.cp_time failed"); + } + for (int state = 0; state < CPUSTATES; state++) { + cp_time[state] = sum_cp_time[state]; + } + pcp_time += CPUSTATES; + if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), pcp_time, &sz, NULL, 0)) { + throw std::runtime_error("sysctl kern.cp_time failed"); + } +#elif defined(__OpenBSD__) + { + int mib[] = { + CTL_KERN, + KERN_CPTIME, + }; + if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), sum_cp_time, &sum_sz, NULL, 0)) { + throw std::runtime_error("sysctl kern.cp_time failed"); + } + } + for (int state = 0; state < CPUSTATES; state++) { + cp_time[state] = sum_cp_time[state]; + } + pcp_time = cp_time; + sz /= ncpu + 1; + { + int mib[] = { + CTL_KERN, + KERN_CPTIME2, + 0, + }; + for (int cpu = 0; cpu < ncpu; cpu++) { + mib[2] = cpu; + pcp_time += CPUSTATES; + if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), pcp_time, &sz, NULL, 0)) { + throw std::runtime_error("sysctl kern.cp_time2 failed"); + } + } + } +#else + if (sysctlbyname("kern.cp_time", sum_cp_time, &sum_sz, NULL, 0)) { + throw std::runtime_error("sysctl kern.cp_time failed"); + } + for (int state = 0; state < CPUSTATES; state++) { + cp_time[state] = sum_cp_time[state]; + } + pcp_time += CPUSTATES; + if (sysctlbyname("kern.cp_times", pcp_time, &sz, NULL, 0)) { + throw std::runtime_error("sysctl kern.cp_times failed"); + } +#endif + std::vector> cpuinfo; + for (int cpu = 0; cpu < ncpu + 1; cpu++) { + pcp_time_t total = 0, *single_cp_time = &cp_time[cpu * CPUSTATES]; + for (int state = 0; state < CPUSTATES; state++) { + total += single_cp_time[state]; + } + cpuinfo.emplace_back(single_cp_time[CP_IDLE], total); + } + free(cp_time); + return cpuinfo; +} diff --git a/src/modules/cpu.cpp b/src/modules/cpu/common.cpp similarity index 68% rename from src/modules/cpu.cpp rename to src/modules/cpu/common.cpp index 79b84d52..f2204cde 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu/common.cpp @@ -1,5 +1,4 @@ #include "modules/cpu.hpp" -#include waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config) : ALabel(config, "cpu", id, "{usage}%", 10) { @@ -54,30 +53,3 @@ std::tuple waybar::modules::Cpu::getCpuUsage() { prev_times_ = curr_times; return {usage, tooltip}; } - -std::vector> waybar::modules::Cpu::parseCpuinfo() { - std::ifstream info(data_dir_); - if (!info.is_open()) { - throw std::runtime_error("Can't open " + data_dir_); - } - std::vector> cpuinfo; - std::string line; - while (getline(info, line)) { - if (line.substr(0, 3).compare("cpu") != 0) { - break; - } - std::stringstream sline(line.substr(5)); - std::vector times; - for (size_t time = 0; sline >> time; times.push_back(time)) - ; - - size_t idle_time = 0; - size_t total_time = 0; - if (times.size() >= 4) { - idle_time = times[3]; - total_time = std::accumulate(times.begin(), times.end(), 0); - } - cpuinfo.emplace_back(idle_time, total_time); - } - return cpuinfo; -} diff --git a/src/modules/cpu/linux.cpp b/src/modules/cpu/linux.cpp new file mode 100644 index 00000000..9f1734fb --- /dev/null +++ b/src/modules/cpu/linux.cpp @@ -0,0 +1,29 @@ +#include "modules/cpu.hpp" + +std::vector> waybar::modules::Cpu::parseCpuinfo() { + const std::string data_dir_ = "/proc/stat"; + std::ifstream info(data_dir_); + if (!info.is_open()) { + throw std::runtime_error("Can't open " + data_dir_); + } + std::vector> cpuinfo; + std::string line; + while (getline(info, line)) { + if (line.substr(0, 3).compare("cpu") != 0) { + break; + } + std::stringstream sline(line.substr(5)); + std::vector times; + for (size_t time = 0; sline >> time; times.push_back(time)) + ; + + size_t idle_time = 0; + size_t total_time = 0; + if (times.size() >= 4) { + idle_time = times[3]; + total_time = std::accumulate(times.begin(), times.end(), 0); + } + cpuinfo.emplace_back(idle_time, total_time); + } + return cpuinfo; +} From 71b7b4e0f407f3819407c9e258774d4639029a21 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Mon, 30 Sep 2019 21:40:36 +0000 Subject: [PATCH 11/14] disk: properly calculate free/total size On most Unix systems `f_blocks`, `f_bfree`, and `f_bavail` are reported in units of `f_frsize`. --- src/modules/disk.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/disk.cpp b/src/modules/disk.cpp index 87240dee..59ffea67 100644 --- a/src/modules/disk.cpp +++ b/src/modules/disk.cpp @@ -44,9 +44,9 @@ auto waybar::modules::Disk::update() -> void { return; } - auto free = pow_format(stats.f_bavail * stats.f_bsize, "B", true); - auto used = pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true); - auto total = pow_format(stats.f_blocks * stats.f_bsize, "B", true); + auto free = pow_format(stats.f_bavail * stats.f_frsize, "B", true); + auto used = pow_format((stats.f_blocks - stats.f_bavail) * stats.f_frsize, "B", true); + auto total = pow_format(stats.f_blocks * stats.f_frsize, "B", true); label_.set_markup(fmt::format(format_ , stats.f_bavail * 100 / stats.f_blocks From 1885ecc9583612bb578defd05e3d7e8b25af1f64 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 9 Aug 2019 09:52:16 +0000 Subject: [PATCH 12/14] bluetooth: limit to Linux due to missing /dev/rfkill ../src/modules/bluetooth.cpp:3:10: fatal error: 'linux/rfkill.h' file not found #include ^~~~~~~~~~~~~~~~ --- include/factory.hpp | 2 ++ meson.build | 4 ++-- src/factory.cpp | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/factory.hpp b/include/factory.hpp index e584b251..c698aa32 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -36,7 +36,9 @@ #include "bar.hpp" #include "modules/custom.hpp" #include "modules/temperature.hpp" +#if defined(__linux__) #include "modules/bluetooth.hpp" +#endif namespace waybar { diff --git a/meson.build b/meson.build index 5a8570c2..86e7ba4d 100644 --- a/meson.build +++ b/meson.build @@ -117,7 +117,6 @@ src_files = files( 'src/factory.cpp', 'src/AModule.cpp', 'src/ALabel.cpp', - 'src/modules/bluetooth.cpp', 'src/modules/clock.cpp', 'src/modules/custom.cpp', 'src/modules/disk.cpp', @@ -126,7 +125,6 @@ src_files = files( 'src/main.cpp', 'src/bar.cpp', 'src/client.cpp', - 'src/util/rfkill.cpp' ) if is_linux @@ -134,10 +132,12 @@ if is_linux add_project_arguments('-DHAVE_MEMORY_LINUX', language: 'cpp') src_files += files( 'src/modules/battery.cpp', + 'src/modules/bluetooth.cpp', 'src/modules/cpu/common.cpp', 'src/modules/cpu/linux.cpp', 'src/modules/memory/common.cpp', 'src/modules/memory/linux.cpp', + 'src/util/rfkill.cpp' ) elif is_dragonfly or is_freebsd or is_netbsd or is_openbsd add_project_arguments('-DHAVE_CPU_BSD', language: 'cpp') diff --git a/src/factory.cpp b/src/factory.cpp index 88a1322b..6005cad5 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -70,9 +70,11 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { if (ref == "temperature") { return new waybar::modules::Temperature(id, config_[name]); } +#if defined(__linux__) if (ref == "bluetooth") { return new waybar::modules::Bluetooth(id, config_[name]); } +#endif if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) { return new waybar::modules::Custom(ref.substr(7), id, config_[name]); } From 4f646543fccebad747f75905e0f2730434aaa986 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Sun, 17 May 2020 09:06:44 +0000 Subject: [PATCH 13/14] CI: add FreeBSD job --- .travis.yml | 67 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index fd94669f..1a85950b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,24 +1,51 @@ -sudo: false - -services: - - docker - git: submodules: false -env: - - distro: debian - - distro: archlinux - - distro: fedora - - distro: alpine - - distro: opensuse +_linux_shared: &linux_shared + os: linux + sudo: false + services: + - docker + before_install: + - docker pull alexays/waybar:${distro} + - find . -type f \( -name '*.cpp' -o -name '*.h' \) -print0 | xargs -r0 clang-format -i + script: + - echo FROM alexays/waybar:${distro} > Dockerfile + - echo ADD . /root >> Dockerfile + - docker build -t waybar . + - docker run waybar /bin/sh -c "cd /root && meson build -Dman-pages=enabled && ninja -C build" -before_install: - - docker pull alexays/waybar:${distro} - - find . -type f \( -name '*.cpp' -o -name '*.h' \) -print0 | xargs -r0 clang-format -i - -script: - - echo FROM alexays/waybar:${distro} > Dockerfile - - echo ADD . /root >> Dockerfile - - docker build -t waybar . - - docker run waybar /bin/sh -c "cd /root && meson build -Dman-pages=enabled && ninja -C build" +jobs: + include: + - <<: *linux_shared + env: distro=debian + - <<: *linux_shared + env: distro=archlinux + - <<: *linux_shared + env: distro=fedora + - <<: *linux_shared + env: distro=alpine + - <<: *linux_shared + env: distro=opensuse + - os: freebsd + language: cpp + compiler: clang + addons: + pkg: + branch: latest + packages: + - date + - gtk-layer-shell + - gtkmm30 + - jsoncpp + - libdbusmenu + - libfmt + - libmpdclient + - libudev-devd + - meson + - pulseaudio + - scdoc + - spdlog + script: + - meson build -Dman-pages=enabled + - ninja -C build From 8d8c0489243fe2731161820e3013da7bf1ef434d Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Mon, 18 May 2020 23:10:48 +0000 Subject: [PATCH 14/14] CI: Switch FreeBSD to manual install due to pkg addon not working --- .travis.yml | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1a85950b..3085d7b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,22 +30,11 @@ jobs: - os: freebsd language: cpp compiler: clang - addons: - pkg: - branch: latest - packages: - - date - - gtk-layer-shell - - gtkmm30 - - jsoncpp - - libdbusmenu - - libfmt - - libmpdclient - - libudev-devd - - meson - - pulseaudio - - scdoc - - spdlog + sudo: required + install: + - sudo sed -i '' 's/quarterly/latest/' /etc/pkg/FreeBSD.conf + - sudo pkg install -y date gtk-layer-shell gtkmm30 jsoncpp libdbusmenu + libfmt libmpdclient libudev-devd meson pulseaudio scdoc spdlog script: - meson build -Dman-pages=enabled - ninja -C build