From 8e05aab4d9564c0f5e9deccbd99baae5ac751466 Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Fri, 31 Jan 2020 08:54:41 -0800 Subject: [PATCH 01/19] Current month calendar in clock tooltip. --- man/waybar-clock.5.scd | 4 ++ meson.build | 4 +- resources/config | 2 +- src/modules/clock.cpp | 96 ++++++++++++++++++++++++++++++++++++----- subprojects/utfcpp.wrap | 9 ++++ 5 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 subprojects/utfcpp.wrap diff --git a/man/waybar-clock.5.scd b/man/waybar-clock.5.scd index e3213f39..6684d897 100644 --- a/man/waybar-clock.5.scd +++ b/man/waybar-clock.5.scd @@ -65,6 +65,10 @@ The *clock* module displays the current date and time. View all valid format options in *strftime(3)*. +# FORMAT REPLACEMENTS + +*{calendar}*: Current month calendar + # EXAMPLES ``` diff --git a/meson.build b/meson.build index 69439aca..812cc428 100644 --- a/meson.build +++ b/meson.build @@ -67,6 +67,7 @@ gtk_layer_shell = dependency('gtk-layer-shell-0', fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) systemd = dependency('systemd', required: get_option('systemd')) tz_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'tz_dep' ]) +utfcpp = dependency('utfcpp', fallback: [ 'utfcpp', 'utfcpp_dep' ]) prefix = get_option('prefix') conf_data = configuration_data() @@ -168,7 +169,8 @@ executable( libudev, libmpdclient, gtk_layer_shell, - tz_dep + tz_dep, + utfcpp ], include_directories: [include_directories('include')], install: true, diff --git a/resources/config b/resources/config index 8dfa012b..116d377e 100644 --- a/resources/config +++ b/resources/config @@ -65,7 +65,7 @@ }, "clock": { // "timezone": "America/New_York", - "tooltip-format": "{:%Y-%m-%d | %H:%M}", + "tooltip-format": "{:%Y %B}\n{calendar}", "format-alt": "{:%Y-%m-%d}" }, "cpu": { diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 38af1152..f9181378 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,4 +1,86 @@ #include "modules/clock.hpp" +#include +#include + +using zoned_time = date::zoned_time; + +struct waybar_time { + std::locale locale; + zoned_time ztime; +}; + +namespace { + +size_t utf8_strlen(const std::string& s) { + return utf8::distance(s.begin(), s.end()); +} + +std::string utf8_substr(const std::string s, size_t len) { + utf8::iterator it(s.begin(), s.begin(), s.end()); + for (size_t i = 0; i < len; ++i) { + ++it; + } + int byte_count = it.base() - s.begin(); + return s.substr(0, byte_count); +} + +void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) { + auto wd = first_dow; + do { + if (wd != first_dow) os << ' '; + auto wd_string = date::format(locale, "%a", wd); + auto wd_string_len = utf8_strlen(wd_string); + if (wd_string_len > 2) { + wd_string = utf8_substr(wd_string, 2); + wd_string_len = 2; + } + const std::string pad(2 - wd_string_len, ' '); + os << pad << wd_string; + } while (++wd != first_dow); + os << "\n"; +} + +std::string calendar_text(const waybar_time& wtime, const date::weekday& first_dow) { + const auto daypoint = date::floor(wtime.ztime.get_local_time()); + const auto ymd = date::year_month_day(daypoint); + const date::year_month ym(ymd.year(), ymd.month()); + const auto curr_day = ymd.day(); + + std::stringstream os; + weekdays_header(wtime.locale, first_dow, os); + + // First week prefixed with spaces if needed. + auto wd = date::weekday(ym/1); + auto empty_days = (wd - first_dow).count(); + if (empty_days > 0) { + os << std::string(empty_days * 3 - 1, ' '); + } + auto d = date::day(1); + do { + if (wd != first_dow) os << ' '; + if (d == curr_day) { + os << "" << date::format("%e", d) << ""; + } else { + os << date::format("%e", d); + } + ++d; + } while (++wd != first_dow); + + // Following weeks. + auto last_day = (ym/date::literals::last).day(); + for ( ; d <= last_day; ++d, ++wd) { + os << ((wd == first_dow) ? '\n' : ' '); + if (d == curr_day) { + os << "" << date::format("%e", d) << ""; + } else { + os << date::format("%e", d); + } + } + + return os.str(); +} + +} waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60) @@ -24,13 +106,6 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) }; } -using zoned_time = date::zoned_time; - -struct waybar_time { - std::locale locale; - zoned_time ztime; -}; - auto waybar::modules::Clock::update() -> void { if (!fixed_time_zone_) { // Time zone can change. Be sure to pick that. @@ -44,11 +119,12 @@ auto waybar::modules::Clock::update() -> void { if (tooltipEnabled()) { if (config_["tooltip-format"].isString()) { + const auto calendar = calendar_text(wtime, date::Sunday); auto tooltip_format = config_["tooltip-format"].asString(); - auto tooltip_text = fmt::format(tooltip_format, wtime); - label_.set_tooltip_text(tooltip_text); + auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar)); + label_.set_tooltip_markup(tooltip_text); } else { - label_.set_tooltip_text(text); + label_.set_tooltip_markup(text); } } } diff --git a/subprojects/utfcpp.wrap b/subprojects/utfcpp.wrap new file mode 100644 index 00000000..51cf131b --- /dev/null +++ b/subprojects/utfcpp.wrap @@ -0,0 +1,9 @@ +[wrap-file] +directory = utfcpp-2.3.5 +source_url = https://github.com/nemtrif/utfcpp/archive/v2.3.5.zip +source_filename = utfcpp-2.3.5.zip +source_hash = 1d5cb7d908202d734ec35b84087400013f352ffb4612e978ffb948986b76df14 + +patch_url = https://github.com/mesonbuild/utfcpp/releases/download/2.3.5-1/utfcpp.zip +patch_filename = utfcpp-2.3.5-1-wrap.zip +patch_hash = 5f504de947b34bb5995fcdb66a1ea1392288076a9400ead829da79e73441a13f From 527fa982d232eed3a22c1d6c5d707a0c57a7c708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=BChrk?= Date: Fri, 31 Jan 2020 22:47:26 +0100 Subject: [PATCH 02/19] pulseaudio: adapt icon names to form factors --- src/modules/pulseaudio.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index eb05b8c7..7f02be87 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -176,11 +176,11 @@ void waybar::modules::Pulseaudio::serverInfoCb(pa_context *context, const pa_ser } static const std::array ports = { - "headphones", + "headphone", "speaker", "hdmi", "headset", - "handsfree", + "hands-free", "portable", "car", "hifi", From ea9591baea2e661829a7509ad51c681dc4925dff Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Sun, 2 Feb 2020 14:35:07 -0800 Subject: [PATCH 03/19] Switch from utfcpp to Glib::ustring for UTF-8 string mangling. --- meson.build | 4 +--- src/modules/clock.cpp | 28 +++++++--------------------- subprojects/utfcpp.wrap | 9 --------- 3 files changed, 8 insertions(+), 33 deletions(-) delete mode 100644 subprojects/utfcpp.wrap diff --git a/meson.build b/meson.build index 812cc428..69439aca 100644 --- a/meson.build +++ b/meson.build @@ -67,7 +67,6 @@ gtk_layer_shell = dependency('gtk-layer-shell-0', fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) systemd = dependency('systemd', required: get_option('systemd')) tz_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'tz_dep' ]) -utfcpp = dependency('utfcpp', fallback: [ 'utfcpp', 'utfcpp_dep' ]) prefix = get_option('prefix') conf_data = configuration_data() @@ -169,8 +168,7 @@ executable( libudev, libmpdclient, gtk_layer_shell, - tz_dep, - utfcpp + tz_dep ], include_directories: [include_directories('include')], install: true, diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index f9181378..5efca403 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,6 +1,5 @@ #include "modules/clock.hpp" #include -#include using zoned_time = date::zoned_time; @@ -11,31 +10,18 @@ struct waybar_time { namespace { -size_t utf8_strlen(const std::string& s) { - return utf8::distance(s.begin(), s.end()); -} - -std::string utf8_substr(const std::string s, size_t len) { - utf8::iterator it(s.begin(), s.begin(), s.end()); - for (size_t i = 0; i < len; ++i) { - ++it; - } - int byte_count = it.base() - s.begin(); - return s.substr(0, byte_count); -} - void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) { auto wd = first_dow; do { if (wd != first_dow) os << ' '; - auto wd_string = date::format(locale, "%a", wd); - auto wd_string_len = utf8_strlen(wd_string); - if (wd_string_len > 2) { - wd_string = utf8_substr(wd_string, 2); - wd_string_len = 2; + Glib::ustring wd_ustring(date::format(locale, "%a", wd)); + auto wd_len = wd_ustring.length(); + if (wd_len > 2) { + wd_ustring = wd_ustring.substr(0, 2); + wd_len = 2; } - const std::string pad(2 - wd_string_len, ' '); - os << pad << wd_string; + const std::string pad(2 - wd_len, ' '); + os << pad << wd_ustring; } while (++wd != first_dow); os << "\n"; } diff --git a/subprojects/utfcpp.wrap b/subprojects/utfcpp.wrap deleted file mode 100644 index 51cf131b..00000000 --- a/subprojects/utfcpp.wrap +++ /dev/null @@ -1,9 +0,0 @@ -[wrap-file] -directory = utfcpp-2.3.5 -source_url = https://github.com/nemtrif/utfcpp/archive/v2.3.5.zip -source_filename = utfcpp-2.3.5.zip -source_hash = 1d5cb7d908202d734ec35b84087400013f352ffb4612e978ffb948986b76df14 - -patch_url = https://github.com/mesonbuild/utfcpp/releases/download/2.3.5-1/utfcpp.zip -patch_filename = utfcpp-2.3.5-1-wrap.zip -patch_hash = 5f504de947b34bb5995fcdb66a1ea1392288076a9400ead829da79e73441a13f From f6b200568742cbd94dc8cac17e7ac6728cc82008 Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Sun, 2 Feb 2020 14:44:26 -0800 Subject: [PATCH 04/19] Cache calendar tooltip text to reduce computations. --- src/modules/clock.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 5efca403..e7193539 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,4 +1,5 @@ #include "modules/clock.hpp" +#include #include using zoned_time = date::zoned_time; @@ -26,9 +27,28 @@ void weekdays_header(const std::locale& locale, const date::weekday& first_dow, os << "\n"; } +struct CachedCalendar { + date::year_month_day ymd; + std::string text; + + void set(const date::year_month_day& ymd_, std::string text_) { + ymd = ymd_; + text = text_; + } +}; + +std::mutex cached_calendar_mutex; // protects cached_calendar. +CachedCalendar cached_calendar; + std::string calendar_text(const waybar_time& wtime, const date::weekday& first_dow) { const auto daypoint = date::floor(wtime.ztime.get_local_time()); const auto ymd = date::year_month_day(daypoint); + + const std::lock_guard lock(cached_calendar_mutex); + if (cached_calendar.ymd == ymd) { + return cached_calendar.text; + } + const date::year_month ym(ymd.year(), ymd.month()); const auto curr_day = ymd.day(); @@ -63,7 +83,9 @@ std::string calendar_text(const waybar_time& wtime, const date::weekday& first_d } } - return os.str(); + auto result = os.str(); + cached_calendar.set(ymd, result); + return result; } } From 218bb3bc2bb607b5c2978696d24ddf3f32203454 Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Sun, 2 Feb 2020 14:55:37 -0800 Subject: [PATCH 05/19] Simpify calendar generation, single loop handles both first week and subsequent weeks. --- src/modules/clock.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index e7193539..134288ce 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -61,21 +61,13 @@ std::string calendar_text(const waybar_time& wtime, const date::weekday& first_d if (empty_days > 0) { os << std::string(empty_days * 3 - 1, ' '); } - auto d = date::day(1); - do { - if (wd != first_dow) os << ' '; - if (d == curr_day) { - os << "" << date::format("%e", d) << ""; - } else { - os << date::format("%e", d); - } - ++d; - } while (++wd != first_dow); - - // Following weeks. auto last_day = (ym/date::literals::last).day(); - for ( ; d <= last_day; ++d, ++wd) { - os << ((wd == first_dow) ? '\n' : ' '); + for (auto d = date::day(1); d <= last_day; ++d, ++wd) { + if (wd != first_dow) { + os << ' '; + } else if (unsigned(d) != 1) { + os << '\n'; + } if (d == curr_day) { os << "" << date::format("%e", d) << ""; } else { From 34a710cce3d51ff055bd3f272564e356e07ad172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20Huseb=C3=B8?= Date: Mon, 3 Feb 2020 10:40:26 +0100 Subject: [PATCH 06/19] Fix typos --- man/waybar-temperature.5.scd | 8 ++++---- resources/custom_modules/mediaplayer.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/man/waybar-temperature.5.scd b/man/waybar-temperature.5.scd index 437cd29d..f8676867 100644 --- a/man/waybar-temperature.5.scd +++ b/man/waybar-temperature.5.scd @@ -22,7 +22,7 @@ Addressed by *temperature* *critical-threshold*: ++ typeof: integer ++ - The threshold before it is considered critical (Celcius). + The threshold before it is considered critical (Celsius). *interval*: ++ typeof: integer ++ @@ -36,11 +36,11 @@ Addressed by *temperature* *format*: ++ typeof: string ++ default: {temperatureC}°C ++ - The format (Celcius/Farenheit) in which the temperature should be displayed. + The format (Celsius/Fahrenheit) in which the temperature should be displayed. *format-icons*: ++ typeof: array ++ - Based on the current temperature (Celcius) and *critical-threshold* if available, the corresponding icon gets selected. The order is *low* to *high*. + Based on the current temperature (Celsius) and *critical-threshold* if available, the corresponding icon gets selected. The order is *low* to *high*. *rotate*: ++ typeof: integer ++ @@ -81,7 +81,7 @@ Addressed by *temperature* # FORMAT REPLACEMENTS -*{temperatureC}*: Temperature in Celcius. +*{temperatureC}*: Temperature in Celsius. *{temperatureF}*: Temperature in Fahrenheit. diff --git a/resources/custom_modules/mediaplayer.py b/resources/custom_modules/mediaplayer.py index 7404f48f..cf3df4b6 100755 --- a/resources/custom_modules/mediaplayer.py +++ b/resources/custom_modules/mediaplayer.py @@ -79,7 +79,7 @@ def signal_handler(sig, frame): def parse_arguments(): parser = argparse.ArgumentParser() - # Increase verbosity with every occurance of -v + # Increase verbosity with every occurence of -v parser.add_argument('-v', '--verbose', action='count', default=0) # Define for which player we're listening From 4c40f9c6351fc7ef18efff290879b81b680caaf6 Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Mon, 3 Feb 2020 16:19:32 -0800 Subject: [PATCH 07/19] Stop using a mutex for guarding CachedCalendar. --- src/modules/clock.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 134288ce..ef79274e 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,5 +1,4 @@ #include "modules/clock.hpp" -#include #include using zoned_time = date::zoned_time; @@ -37,14 +36,11 @@ struct CachedCalendar { } }; -std::mutex cached_calendar_mutex; // protects cached_calendar. CachedCalendar cached_calendar; std::string calendar_text(const waybar_time& wtime, const date::weekday& first_dow) { const auto daypoint = date::floor(wtime.ztime.get_local_time()); const auto ymd = date::year_month_day(daypoint); - - const std::lock_guard lock(cached_calendar_mutex); if (cached_calendar.ymd == ymd) { return cached_calendar.text; } From cd2db19267214ca2422b5ecd07d7eb99d049132e Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Mon, 3 Feb 2020 16:58:18 -0800 Subject: [PATCH 08/19] Detect presence, call nl_langinfo() to get first day of week. --- meson.build | 12 ++++++++++++ src/modules/clock.cpp | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 69439aca..98dd25e8 100644 --- a/meson.build +++ b/meson.build @@ -42,6 +42,18 @@ if not compiler.has_header('filesystem') endif endif +code = '''#include +int main(int argc, char** argv) { + char* str; + str = nl_langinfo(_NL_TIME_WEEK_1STDAY); + str = nl_langinfo(_NL_TIME_FIRST_WEEKDAY); + return 0; +} +''' +if compiler.links(code, name : 'nl_langinfo with _NL_TIME_WEEK_1STDAY, _NL_TIME_FIRST_WEEKDAY') + add_project_arguments('-DHAVE_LANGINFO_1STDAY', language: 'cpp') +endif + add_global_arguments(cpp_args, language : 'cpp') add_global_link_arguments(cpp_link_args, language : 'cpp') diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index ef79274e..8419d18b 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,5 +1,8 @@ #include "modules/clock.hpp" #include +#ifdef HAVE_LANGINFO_1STDAY +#include +#endif using zoned_time = date::zoned_time; @@ -10,6 +13,17 @@ struct waybar_time { namespace { +#ifdef HAVE_LANGINFO_1STDAY +// Computations done similarly to Linux cal utility. +date::weekday first_day_of_week() { + const int i = (std::intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY); + auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100); + auto wd = date::weekday(ymd); + uint8_t j = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY); + return wd + date::days(j - 1); +} +#endif + void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) { auto wd = first_dow; do { @@ -38,7 +52,7 @@ struct CachedCalendar { CachedCalendar cached_calendar; -std::string calendar_text(const waybar_time& wtime, const date::weekday& first_dow) { +std::string calendar_text(const waybar_time& wtime) { const auto daypoint = date::floor(wtime.ztime.get_local_time()); const auto ymd = date::year_month_day(daypoint); if (cached_calendar.ymd == ymd) { @@ -49,6 +63,11 @@ std::string calendar_text(const waybar_time& wtime, const date::weekday& first_d const auto curr_day = ymd.day(); std::stringstream os; +#ifdef HAVE_LANGINFO_1STDAY + const auto first_dow = first_day_of_week(); +#else + const auto first_dow = date::Sunday; +#endif weekdays_header(wtime.locale, first_dow, os); // First week prefixed with spaces if needed. @@ -115,7 +134,7 @@ auto waybar::modules::Clock::update() -> void { if (tooltipEnabled()) { if (config_["tooltip-format"].isString()) { - const auto calendar = calendar_text(wtime, date::Sunday); + const auto calendar = calendar_text(wtime); auto tooltip_format = config_["tooltip-format"].asString(); auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar)); label_.set_tooltip_markup(tooltip_text); From f107aaddc394b7f757c1f271e604adb35aa47639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20Huseb=C3=B8?= Date: Tue, 4 Feb 2020 12:16:50 +0100 Subject: [PATCH 09/19] Finish #571 --- man/waybar-pulseaudio.5.scd | 5 +++-- resources/config | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/man/waybar-pulseaudio.5.scd b/man/waybar-pulseaudio.5.scd index 7640c706..487888a4 100644 --- a/man/waybar-pulseaudio.5.scd +++ b/man/waybar-pulseaudio.5.scd @@ -100,16 +100,17 @@ The following strings for *format-icons* are supported. If they are found in the current PulseAudio port name, the corresponding icons will be selected. - *default* (Shown, when no other port is found) -- *headphones* +- *headphone* - *speaker* - *hdmi* - *headset* -- *handsfree* +- *hands-free* - *portable* - *car* - *hifi* - *phone* + # EXAMPLES ``` diff --git a/resources/config b/resources/config index 8dfa012b..fcc365e2 100644 --- a/resources/config +++ b/resources/config @@ -122,8 +122,8 @@ "format-source": "{volume}% ", "format-source-muted": "", "format-icons": { - "headphones": "", - "handsfree": "", + "headphone": "", + "hands-free": "", "headset": "", "phone": "", "portable": "", From ae6ca36fa74802f487c3b45c8ff32c571ec36833 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Tue, 27 Aug 2019 19:43:03 -0700 Subject: [PATCH 10/19] fix(sway): resolve destruction dependency between Ipc and SleeperThread Ipc destructor closes socket and thus wakes up SleeperThread which was waiting for socket data in Ipc::handleEvent. Ipc::handleEvent then proceeds with sending signal to already destroyed object, causing heap-use-after-free Address Sanitizer error. --- include/modules/sway/ipc/client.hpp | 9 ++++++--- include/modules/sway/mode.hpp | 6 +----- include/modules/sway/window.hpp | 6 +----- include/modules/sway/workspaces.hpp | 6 +----- src/modules/sway/ipc/client.cpp | 10 +++++++--- src/modules/sway/mode.cpp | 18 +++++++----------- src/modules/sway/window.cpp | 18 +++++++----------- src/modules/sway/workspaces.cpp | 18 +++++++----------- 8 files changed, 37 insertions(+), 54 deletions(-) diff --git a/include/modules/sway/ipc/client.hpp b/include/modules/sway/ipc/client.hpp index 629556f7..7df53629 100644 --- a/include/modules/sway/ipc/client.hpp +++ b/include/modules/sway/ipc/client.hpp @@ -8,6 +8,7 @@ #include #include #include "ipc.hpp" +#include "util/sleeper_thread.hpp" namespace waybar::modules::sway { @@ -28,6 +29,7 @@ class Ipc { void sendCmd(uint32_t type, const std::string &payload = ""); void subscribe(const std::string &payload); void handleEvent(); + void setWorker(std::function &&func); protected: static inline const std::string ipc_magic_ = "i3-ipc"; @@ -38,9 +40,10 @@ class Ipc { struct ipc_response send(int fd, uint32_t type, const std::string &payload = ""); struct ipc_response recv(int fd); - int fd_; - int fd_event_; - std::mutex mutex_; + int fd_; + int fd_event_; + std::mutex mutex_; + util::SleeperThread thread_; }; } // namespace waybar::modules::sway diff --git a/include/modules/sway/mode.hpp b/include/modules/sway/mode.hpp index f0cf74c1..a1a88b02 100644 --- a/include/modules/sway/mode.hpp +++ b/include/modules/sway/mode.hpp @@ -6,7 +6,6 @@ #include "client.hpp" #include "modules/sway/ipc/client.hpp" #include "util/json.hpp" -#include "util/sleeper_thread.hpp" namespace waybar::modules::sway { @@ -18,14 +17,11 @@ class Mode : public ALabel, public sigc::trackable { private: void onEvent(const struct Ipc::ipc_response&); - void worker(); std::string mode_; util::JsonParser parser_; std::mutex mutex_; - - util::SleeperThread thread_; - Ipc ipc_; + Ipc ipc_; }; } // namespace waybar::modules::sway diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index 5bb129d7..40aaa1a0 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -7,7 +7,6 @@ #include "client.hpp" #include "modules/sway/ipc/client.hpp" #include "util/json.hpp" -#include "util/sleeper_thread.hpp" namespace waybar::modules::sway { @@ -20,7 +19,6 @@ class Window : public ALabel, public sigc::trackable { private: void onEvent(const struct Ipc::ipc_response&); void onCmd(const struct Ipc::ipc_response&); - void worker(); std::tuple getFocusedNode(const Json::Value& nodes, std::string& output); void getTree(); @@ -33,9 +31,7 @@ class Window : public ALabel, public sigc::trackable { std::size_t app_nb_; util::JsonParser parser_; std::mutex mutex_; - - util::SleeperThread thread_; - Ipc ipc_; + Ipc ipc_; }; } // namespace waybar::modules::sway diff --git a/include/modules/sway/workspaces.hpp b/include/modules/sway/workspaces.hpp index 498acc95..cd806125 100644 --- a/include/modules/sway/workspaces.hpp +++ b/include/modules/sway/workspaces.hpp @@ -8,7 +8,6 @@ #include "client.hpp" #include "modules/sway/ipc/client.hpp" #include "util/json.hpp" -#include "util/sleeper_thread.hpp" namespace waybar::modules::sway { @@ -21,7 +20,6 @@ class Workspaces : public AModule, public sigc::trackable { private: void onCmd(const struct Ipc::ipc_response&); void onEvent(const struct Ipc::ipc_response&); - void worker(); bool filterButtons(); Gtk::Button& addButton(const Json::Value&); void onButtonReady(const Json::Value&, Gtk::Button&); @@ -38,9 +36,7 @@ class Workspaces : public AModule, public sigc::trackable { util::JsonParser parser_; std::unordered_map buttons_; std::mutex mutex_; - - util::SleeperThread thread_; - Ipc ipc_; + Ipc ipc_; }; } // namespace waybar::modules::sway diff --git a/src/modules/sway/ipc/client.cpp b/src/modules/sway/ipc/client.cpp index eae6c76e..58aed60c 100644 --- a/src/modules/sway/ipc/client.cpp +++ b/src/modules/sway/ipc/client.cpp @@ -10,19 +10,23 @@ Ipc::Ipc() { } Ipc::~Ipc() { - // To fail the IPC header - write(fd_, "close-sway-ipc", 14); - write(fd_event_, "close-sway-ipc", 14); + thread_.stop(); + if (fd_ > 0) { + // To fail the IPC header + write(fd_, "close-sway-ipc", 14); close(fd_); fd_ = -1; } if (fd_event_ > 0) { + write(fd_event_, "close-sway-ipc", 14); close(fd_event_); fd_event_ = -1; } } +void Ipc::setWorker(std::function&& func) { thread_ = func; } + const std::string Ipc::getSocketPath() const { const char* env = getenv("SWAYSOCK"); if (env != nullptr) { diff --git a/src/modules/sway/mode.cpp b/src/modules/sway/mode.cpp index cd02c0ca..632709d0 100644 --- a/src/modules/sway/mode.cpp +++ b/src/modules/sway/mode.cpp @@ -8,7 +8,13 @@ Mode::Mode(const std::string& id, const Json::Value& config) ipc_.subscribe(R"(["mode"])"); ipc_.signal_event.connect(sigc::mem_fun(*this, &Mode::onEvent)); // Launch worker - worker(); + ipc_.setWorker([this] { + try { + ipc_.handleEvent(); + } catch (const std::exception& e) { + spdlog::error("Mode: {}", e.what()); + } + }); dp.emit(); } @@ -31,16 +37,6 @@ void Mode::onEvent(const struct Ipc::ipc_response& res) { } } -void Mode::worker() { - thread_ = [this] { - try { - ipc_.handleEvent(); - } catch (const std::exception& e) { - spdlog::error("Mode: {}", e.what()); - } - }; -} - auto Mode::update() -> void { if (mode_.empty()) { event_box_.hide(); diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 2e4ec468..c139180a 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -11,7 +11,13 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) // Get Initial focused window getTree(); // Launch worker - worker(); + ipc_.setWorker([this] { + try { + ipc_.handleEvent(); + } catch (const std::exception& e) { + spdlog::error("Window: {}", e.what()); + } + }); } void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); } @@ -28,16 +34,6 @@ void Window::onCmd(const struct Ipc::ipc_response& res) { } } -void Window::worker() { - thread_ = [this] { - try { - ipc_.handleEvent(); - } catch (const std::exception& e) { - spdlog::error("Window: {}", e.what()); - } - }; -} - auto Window::update() -> void { if (!old_app_id_.empty()) { bar_.window.get_style_context()->remove_class(old_app_id_); diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index b65e47db..fe87f5ea 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -22,7 +22,13 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value window.signal_scroll_event().connect(sigc::mem_fun(*this, &Workspaces::handleScroll)); } // Launch worker - worker(); + ipc_.setWorker([this] { + try { + ipc_.handleEvent(); + } catch (const std::exception &e) { + spdlog::error("Workspaces: {}", e.what()); + } + }); } void Workspaces::onEvent(const struct Ipc::ipc_response &res) { @@ -102,16 +108,6 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) { } } -void Workspaces::worker() { - thread_ = [this] { - try { - ipc_.handleEvent(); - } catch (const std::exception &e) { - spdlog::error("Workspaces: {}", e.what()); - } - }; -} - bool Workspaces::filterButtons() { bool needReorder = false; for (auto it = buttons_.begin(); it != buttons_.end();) { From d1f427618fbcdab4a11d84e6fde987f1b62c9bec Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Wed, 5 Feb 2020 11:02:42 -0800 Subject: [PATCH 11/19] Cache calendar per clock instance, weekdays properly handle locales. --- include/modules/clock.hpp | 11 +++ meson.build | 10 ++- src/modules/clock.cpp | 175 ++++++++++++++++++-------------------- 3 files changed, 101 insertions(+), 95 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index e54f4d2c..bb63241d 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -12,6 +12,11 @@ namespace waybar::modules { +struct waybar_time { + std::locale locale; + date::zoned_time ztime; +}; + class Clock : public ALabel { public: Clock(const std::string&, const Json::Value&); @@ -23,6 +28,12 @@ class Clock : public ALabel { std::locale locale_; const date::time_zone* time_zone_; bool fixed_time_zone_; + date::year_month_day cached_calendar_ymd_; + std::string cached_calendar_text_; + + auto calendar_text(const waybar_time& wtime) -> std::string; + auto weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void; + auto first_day_of_week() -> date::weekday; }; } // namespace waybar::modules diff --git a/meson.build b/meson.build index 98dd25e8..8cfe5d89 100644 --- a/meson.build +++ b/meson.build @@ -42,11 +42,15 @@ if not compiler.has_header('filesystem') endif endif -code = '''#include +code = ''' +#include +#include int main(int argc, char** argv) { + locale_t locale = newlocale(LC_ALL, "en_US.UTF-8", nullptr); char* str; - str = nl_langinfo(_NL_TIME_WEEK_1STDAY); - str = nl_langinfo(_NL_TIME_FIRST_WEEKDAY); + str = nl_langinfo_l(_NL_TIME_WEEK_1STDAY, locale); + str = nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, locale); + freelocale(locale); return 0; } ''' diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 8419d18b..5655168f 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,101 +1,12 @@ #include "modules/clock.hpp" #include +#include #ifdef HAVE_LANGINFO_1STDAY #include +#include #endif -using zoned_time = date::zoned_time; - -struct waybar_time { - std::locale locale; - zoned_time ztime; -}; - -namespace { - -#ifdef HAVE_LANGINFO_1STDAY -// Computations done similarly to Linux cal utility. -date::weekday first_day_of_week() { - const int i = (std::intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY); - auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100); - auto wd = date::weekday(ymd); - uint8_t j = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY); - return wd + date::days(j - 1); -} -#endif - -void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) { - auto wd = first_dow; - do { - if (wd != first_dow) os << ' '; - Glib::ustring wd_ustring(date::format(locale, "%a", wd)); - auto wd_len = wd_ustring.length(); - if (wd_len > 2) { - wd_ustring = wd_ustring.substr(0, 2); - wd_len = 2; - } - const std::string pad(2 - wd_len, ' '); - os << pad << wd_ustring; - } while (++wd != first_dow); - os << "\n"; -} - -struct CachedCalendar { - date::year_month_day ymd; - std::string text; - - void set(const date::year_month_day& ymd_, std::string text_) { - ymd = ymd_; - text = text_; - } -}; - -CachedCalendar cached_calendar; - -std::string calendar_text(const waybar_time& wtime) { - const auto daypoint = date::floor(wtime.ztime.get_local_time()); - const auto ymd = date::year_month_day(daypoint); - if (cached_calendar.ymd == ymd) { - return cached_calendar.text; - } - - const date::year_month ym(ymd.year(), ymd.month()); - const auto curr_day = ymd.day(); - - std::stringstream os; -#ifdef HAVE_LANGINFO_1STDAY - const auto first_dow = first_day_of_week(); -#else - const auto first_dow = date::Sunday; -#endif - weekdays_header(wtime.locale, first_dow, os); - - // First week prefixed with spaces if needed. - auto wd = date::weekday(ym/1); - auto empty_days = (wd - first_dow).count(); - if (empty_days > 0) { - os << std::string(empty_days * 3 - 1, ' '); - } - auto last_day = (ym/date::literals::last).day(); - for (auto d = date::day(1); d <= last_day; ++d, ++wd) { - if (wd != first_dow) { - os << ' '; - } else if (unsigned(d) != 1) { - os << '\n'; - } - if (d == curr_day) { - os << "" << date::format("%e", d) << ""; - } else { - os << date::format("%e", d); - } - } - - auto result = os.str(); - cached_calendar.set(ymd, result); - return result; -} - -} +using waybar::modules::waybar_time; waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60) @@ -144,6 +55,86 @@ auto waybar::modules::Clock::update() -> void { } } +auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::string { + const auto daypoint = date::floor(wtime.ztime.get_local_time()); + const auto ymd = date::year_month_day(daypoint); + if (cached_calendar_ymd_ == ymd) { + return cached_calendar_text_; + } + + const date::year_month ym(ymd.year(), ymd.month()); + const auto curr_day = ymd.day(); + + std::stringstream os; + const auto first_dow = first_day_of_week(); + weekdays_header(first_dow, os); + + // First week prefixed with spaces if needed. + auto wd = date::weekday(ym/1); + auto empty_days = (wd - first_dow).count(); + if (empty_days > 0) { + os << std::string(empty_days * 3 - 1, ' '); + } + auto last_day = (ym/date::literals::last).day(); + for (auto d = date::day(1); d <= last_day; ++d, ++wd) { + if (wd != first_dow) { + os << ' '; + } else if (unsigned(d) != 1) { + os << '\n'; + } + if (d == curr_day) { + os << "" << date::format("%e", d) << ""; + } else { + os << date::format("%e", d); + } + } + + auto result = os.str(); + cached_calendar_ymd_ = ymd; + cached_calendar_text_ = result; + return result; +} + +auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void { + auto wd = first_dow; + do { + if (wd != first_dow) os << ' '; + Glib::ustring wd_ustring(date::format(locale_, "%a", wd)); + auto wd_len = wd_ustring.length(); + if (wd_len > 2) { + wd_ustring = wd_ustring.substr(0, 2); + wd_len = 2; + } + const std::string pad(2 - wd_len, ' '); + os << pad << wd_ustring; + } while (++wd != first_dow); + os << "\n"; +} + +#ifdef HAVE_LANGINFO_1STDAY +template +using deleter_from_fn = std::integral_constant; + +template +using deleting_unique_ptr = std::unique_ptr>; +#endif + +// Computations done similarly to Linux cal utility. +auto waybar::modules::Clock::first_day_of_week() -> date::weekday { +#ifdef HAVE_LANGINFO_1STDAY + deleting_unique_ptr::type, freelocale> + posix_locale{newlocale(LC_ALL, locale_.name().c_str(), nullptr)}; + if (posix_locale) { + const int i = (std::intptr_t) nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get()); + auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100); + auto wd = date::weekday(ymd); + uint8_t j = *nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get()); + return wd + date::days(j - 1); + } +#endif + return date::Sunday; +} + template <> struct fmt::formatter : fmt::formatter { template From e70f8d87302d5ec0e0dfd40076a09183c41822fa Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Wed, 5 Feb 2020 00:51:56 -0800 Subject: [PATCH 12/19] fix(clock): lower precision of zoned_time to avoid fractional seconds in output --- include/modules/clock.hpp | 2 +- src/modules/clock.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index bb63241d..e3873a6d 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -14,7 +14,7 @@ namespace waybar::modules { struct waybar_time { std::locale locale; - date::zoned_time ztime; + date::zoned_seconds ztime; }; class Clock : public ALabel { diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 5655168f..7fa0ad63 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -37,8 +37,9 @@ auto waybar::modules::Clock::update() -> void { // Time zone can change. Be sure to pick that. time_zone_ = date::current_zone(); } - auto now = std::chrono::system_clock::now(); - waybar_time wtime = {locale_, date::make_zoned(time_zone_, now)}; + auto now = std::chrono::system_clock::now(); + waybar_time wtime = {locale_, + date::make_zoned(time_zone_, date::floor(now))}; auto text = fmt::format(format_, wtime); label_.set_markup(text); From aae105c998ccec3b67d770b206f5d751719725ef Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 10 Feb 2020 23:47:23 +0000 Subject: [PATCH 13/19] chore: 0.9.1 --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 8cfe5d89..d65ee6bd 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,6 @@ project( 'waybar', 'cpp', 'c', - version: '0.9.0', + version: '0.9.1', license: 'MIT', default_options : [ 'cpp_std=c++17', From e0c42ae4151d77bb54c42fc871f0ca525d7916ce Mon Sep 17 00:00:00 2001 From: Danilo Spinella Date: Sun, 9 Feb 2020 16:58:42 +0100 Subject: [PATCH 14/19] fix(sway): add missing unordered_map include --- include/modules/sway/workspaces.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/modules/sway/workspaces.hpp b/include/modules/sway/workspaces.hpp index cd806125..fef24bf9 100644 --- a/include/modules/sway/workspaces.hpp +++ b/include/modules/sway/workspaces.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include From 16c68ee13279117e493d614088159525942bd0ac Mon Sep 17 00:00:00 2001 From: Danilo Spinella Date: Tue, 11 Feb 2020 14:38:05 +0100 Subject: [PATCH 15/19] fix(meson): Support libc++ >=9.0.0 From LLVM libc++ documentation: "Prior to LLVM 9.0, libc++ provides the implementation of the filesystem library in a separate static library." Now the filesystem library (not the experimental one) is shipped inside the libc++.so library. Check if '-lc++fs' link flag is needed and supported before adding it. --- meson.build | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index d65ee6bd..b8185c27 100644 --- a/meson.build +++ b/meson.build @@ -9,6 +9,8 @@ project( ], ) +compiler = meson.get_compiler('cpp') + cpp_args = [] cpp_link_args = [] @@ -16,12 +18,13 @@ if get_option('libcxx') cpp_args += ['-stdlib=libc++'] cpp_link_args += ['-stdlib=libc++', '-lc++abi'] - cpp_link_args += ['-lc++fs'] + if compiler.has_link_argument('-lc++fs') + cpp_link_args += ['-lc++fs'] + endif else cpp_link_args += ['-lstdc++fs'] endif -compiler = meson.get_compiler('cpp') git = find_program('git', required: false) if not git.found() From 4f8a396692c7ade124a736f125b2bfdeaac3c827 Mon Sep 17 00:00:00 2001 From: Jordan Leppert Date: Sat, 15 Feb 2020 16:51:18 +0000 Subject: [PATCH 16/19] Fix for 'Network label text not updated properly when formats contain Unicode characters' (#588) --- src/modules/network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 43f29f67..cd675c8a 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -279,7 +279,7 @@ auto waybar::modules::Network::update() -> void { fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / interval_.count(), "b/s")), fmt::arg("bandwidthDownOctets", pow_format(bandwidth_down / interval_.count(), "o/s")), fmt::arg("bandwidthUpOctets", pow_format(bandwidth_up / interval_.count(), "o/s"))); - if (text != label_.get_label()) { + if (text.compare(label_.get_label()) != 0) { label_.set_markup(text); if (text.empty()) { event_box_.hide(); From 543589a97b7b949a4c75fe116f9dedc8011ae23d Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 16 Feb 2020 21:48:22 +0000 Subject: [PATCH 17/19] Update pulseaudio.cpp --- src/modules/pulseaudio.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 7f02be87..1c709d0e 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -200,21 +200,23 @@ const std::string waybar::modules::Pulseaudio::getPortIcon() const { auto waybar::modules::Pulseaudio::update() -> void { auto format = format_; - std::string format_name = "format"; - if (monitor_.find("a2dp_sink") != std::string::npos) { - format_name = format_name + "-bluetooth"; - label_.get_style_context()->add_class("bluetooth"); - } else { - label_.get_style_context()->remove_class("bluetooth"); + if (!alt_) { + std::string format_name = "format"; + if (monitor_.find("a2dp_sink") != std::string::npos) { + format_name = format_name + "-bluetooth"; + label_.get_style_context()->add_class("bluetooth"); + } else { + label_.get_style_context()->remove_class("bluetooth"); + } + if (muted_ ) { + format_name = format_name + "-muted"; + label_.get_style_context()->add_class("muted"); + } else { + label_.get_style_context()->remove_class("muted"); + } + format = + config_[format_name].isString() ? config_[format_name].asString() : format; } - if (muted_ ) { - format_name = format_name + "-muted"; - label_.get_style_context()->add_class("muted"); - } else { - label_.get_style_context()->remove_class("muted"); - } - format = - config_[format_name].isString() ? config_[format_name].asString() : format; // TODO: find a better way to split source/sink std::string format_source = "{volume}%"; if (source_muted_ && config_["format-source-muted"].isString()) { From 047c2929c115783f0fbd6c0169e64af8eb127c02 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 19 Feb 2020 12:06:35 +0100 Subject: [PATCH 18/19] Use the same StatusNotifierWatcher for all trays --- include/modules/sni/tray.hpp | 2 +- include/modules/sni/watcher.hpp | 17 +++++++++++++++-- src/modules/sni/tray.cpp | 2 +- src/modules/sni/watcher.cpp | 6 +++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/modules/sni/tray.hpp b/include/modules/sni/tray.hpp index e0aced1f..aaa1e4f9 100644 --- a/include/modules/sni/tray.hpp +++ b/include/modules/sni/tray.hpp @@ -21,7 +21,7 @@ class Tray : public AModule { static inline std::size_t nb_hosts_ = 0; Gtk::Box box_; - SNI::Watcher watcher_; + SNI::Watcher::singleton watcher_; SNI::Host host_; }; diff --git a/include/modules/sni/watcher.hpp b/include/modules/sni/watcher.hpp index 599380af..5a55d2d3 100644 --- a/include/modules/sni/watcher.hpp +++ b/include/modules/sni/watcher.hpp @@ -7,10 +7,24 @@ namespace waybar::modules::SNI { class Watcher { + private: + Watcher(); + public: - Watcher(std::size_t id); ~Watcher(); + using singleton = std::shared_ptr; + static singleton getInstance() { + static std::weak_ptr weak; + + std::shared_ptr strong = weak.lock(); + if (!strong) { + strong = std::shared_ptr(new Watcher()); + weak = strong; + } + return strong; + } + private: typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType; @@ -34,7 +48,6 @@ class Watcher { void updateRegisteredItems(SnWatcher *obj); uint32_t bus_name_id_; - uint32_t watcher_id_; GSList * hosts_ = nullptr; GSList * items_ = nullptr; SnWatcher *watcher_ = nullptr; diff --git a/src/modules/sni/tray.cpp b/src/modules/sni/tray.cpp index a698a979..e16837f5 100644 --- a/src/modules/sni/tray.cpp +++ b/src/modules/sni/tray.cpp @@ -6,7 +6,7 @@ namespace waybar::modules::SNI { Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config) : AModule(config, "tray", id), box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), - watcher_(nb_hosts_), + watcher_(SNI::Watcher::getInstance()), host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1), std::bind(&Tray::onRemove, this, std::placeholders::_1)) { spdlog::warn( diff --git a/src/modules/sni/watcher.cpp b/src/modules/sni/watcher.cpp index 1db37088..73b3eaca 100644 --- a/src/modules/sni/watcher.cpp +++ b/src/modules/sni/watcher.cpp @@ -3,14 +3,13 @@ using namespace waybar::modules::SNI; -Watcher::Watcher(std::size_t id) +Watcher::Watcher() : bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, "org.kde.StatusNotifierWatcher", sigc::mem_fun(*this, &Watcher::busAcquired), Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(), Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)), - watcher_id_(id), watcher_(sn_watcher_skeleton_new()) {} Watcher::~Watcher() { @@ -23,6 +22,7 @@ Watcher::~Watcher() { g_slist_free_full(items_, gfWatchFree); items_ = nullptr; } + Gio::DBus::unown_name(bus_name_id_); auto iface = G_DBUS_INTERFACE_SKELETON(watcher_); g_dbus_interface_skeleton_unexport(iface); } @@ -34,7 +34,7 @@ void Watcher::busAcquired(const Glib::RefPtr& conn, Glib: if (error != nullptr) { // Don't print an error when a watcher is already present if (error->code != 2) { - spdlog::error("Watcher {}: {}", watcher_id_, error->message); + spdlog::error("Watcher: {}", error->message); } g_error_free(error); return; From 9a5f5114c438e358a300f99ce0089489d61c11aa Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 19 Feb 2020 12:28:36 +0100 Subject: [PATCH 19/19] pulse: track default source/sink changes --- src/modules/pulseaudio.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 1c709d0e..46dc4266 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -52,7 +52,8 @@ void waybar::modules::Pulseaudio::contextStateCb(pa_context *c, void *data) { pa_context_set_subscribe_callback(c, subscribeCb, data); pa_context_subscribe( c, - static_cast(static_cast(PA_SUBSCRIPTION_MASK_SINK) | + static_cast(static_cast(PA_SUBSCRIPTION_MASK_SERVER) | + static_cast(PA_SUBSCRIPTION_MASK_SINK) | static_cast(PA_SUBSCRIPTION_MASK_SOURCE)), nullptr, nullptr); @@ -109,7 +110,9 @@ void waybar::modules::Pulseaudio::subscribeCb(pa_context * conte if (operation != PA_SUBSCRIPTION_EVENT_CHANGE) { return; } - if (facility == PA_SUBSCRIPTION_EVENT_SINK) { + if (facility == PA_SUBSCRIPTION_EVENT_SERVER) { + pa_context_get_server_info(context, serverInfoCb, data); + } else if (facility == PA_SUBSCRIPTION_EVENT_SINK) { pa_context_get_sink_info_by_index(context, idx, sinkInfoCb, data); } else if (facility == PA_SUBSCRIPTION_EVENT_SOURCE) { pa_context_get_source_info_by_index(context, idx, sourceInfoCb, data); @@ -214,7 +217,7 @@ auto waybar::modules::Pulseaudio::update() -> void { } else { label_.get_style_context()->remove_class("muted"); } - format = + format = config_[format_name].isString() ? config_[format_name].asString() : format; } // TODO: find a better way to split source/sink