From e1f301354b275e0687e78c4fef9b1e3f3635939f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20=C5=A0vagelj?= Date: Tue, 30 Apr 2024 20:21:39 +0000 Subject: [PATCH] Remove dpi scaling of maximum width (#1877) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix dpi scaling maximum_width setting * Set workspace dimensions from WL output_geometry * Append script parent dir to lua package.path * Fix issues with dpi_scale function selection * Re-implement rounding through `type_traits` generically Signed-off-by: Tin Å vagelj --- src/conky.cc | 45 +++++++++++++++++++++++++++++++++++++----- src/conky.h | 3 ++- src/display-output.hh | 30 ++++++++++++++++------------ src/display-wayland.cc | 17 +++++++++++----- src/display-wayland.hh | 4 +--- src/display-x11.cc | 18 +++++++---------- src/display-x11.hh | 4 +--- 7 files changed, 80 insertions(+), 41 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 0681782b..327f3f59 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -275,7 +276,7 @@ int text_width = 1, struct information info; /* path to config file */ -std::string current_config; +std::filesystem::path current_config; /* set to 1 if you want all text to be in uppercase */ static conky::simple_config_setting stuff_in_uppercase("uppercase", false, @@ -864,7 +865,7 @@ void update_text_area() { if (text_height < dpi_scale(minimum_height.get(*state))) { text_height = dpi_scale(minimum_height.get(*state)); } - int mw = dpi_scale(maximum_width.get(*state)); + int mw = maximum_width.get(*state); if (text_width > mw && mw > 0) { text_width = mw; } } @@ -987,7 +988,7 @@ static int text_size_updater(char *s, int special_index) { w += get_string_width(s); if (w > text_width) { text_width = w; } - int mw = dpi_scale(maximum_width.get(*state)); + int mw = maximum_width.get(*state); if (text_width > mw && mw > 0) { text_width = mw; } text_height += last_font_height; @@ -1051,7 +1052,7 @@ static void draw_string(const char *s) { } #ifdef BUILD_GUI if (display_output() && display_output()->graphical()) { - int mw = display_output()->dpi_scale(maximum_width.get(*state)); + int mw = maximum_width.get(*state); if (text_width == mw) { /* this means the text is probably pushing the limit, * so we'll chop it */ @@ -1121,7 +1122,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) { #ifdef BUILD_GUI if (display_output() && display_output()->graphical()) { - mw = display_output()->dpi_scale(maximum_width.get(*state)); + mw = maximum_width.get(*state); font_h = font_height(); cur_y += font_ascent(); } @@ -1961,6 +1962,40 @@ void load_config_file() { lua::stack_sentry s(l); l.checkstack(2); + // Extend lua package.path so scripts can use relative paths + { + struct stat file_stat {}; + + std::string path_ext; + + // add XDG directory to lua path + auto xdg_path = + std::filesystem::path(to_real_path(XDG_CONFIG_FILE)).parent_path(); + if (stat(xdg_path.c_str(), &file_stat) == 0) { + path_ext.push_back(';'); + path_ext.append(xdg_path); + path_ext.append("/?.lua"); + } + + auto parent_path = current_config.parent_path(); + if (xdg_path != parent_path && stat(path_ext.c_str(), &file_stat) == 0) { + path_ext.push_back(';'); + path_ext.append(parent_path); + path_ext.append("/?.lua"); + } + + l.getglobal("package"); + l.getfield(-1, "path"); + + auto path = l.tostring(-1); + path.append(path_ext); + l.pop(); + l.pushstring(path.c_str()); + + l.setfield(-2, "path"); + l.pop(); + } + try { #ifdef BUILD_BUILTIN_CONFIG if (current_config == builtin_config_magic) { diff --git a/src/conky.h b/src/conky.h index c7a35724..efb8633f 100644 --- a/src/conky.h +++ b/src/conky.h @@ -38,6 +38,7 @@ #include /* defines */ #include /* struct uname_s */ #include +#include #include #include "colours.h" @@ -346,7 +347,7 @@ extern conky::simple_config_setting utf8_mode; extern conky::range_config_setting max_user_text; /* path to config file */ -extern std::string current_config; +extern std::filesystem::path current_config; #define DEFAULT_TEXT_BUFFER_SIZE_S "##DEFAULT_TEXT_BUFFER_SIZE" diff --git a/src/display-output.hh b/src/display-output.hh index e74733ef..fa5efa24 100644 --- a/src/display-output.hh +++ b/src/display-output.hh @@ -24,6 +24,7 @@ #define DISPLAY_OUTPUT_HH #include +#include #include #include #include @@ -107,11 +108,7 @@ class display_output_base { virtual void draw_arc(int /*x*/, int /*y*/, int /*w*/, int /*h*/, int /*a1*/, int /*a2*/) {} virtual void move_win(int /*x*/, int /*y*/) {} - template ::value, T>::type> - T dpi_scale(T value) { - return value; - } + virtual float get_dpi_scale() { return 1.0; }; virtual void begin_draw_stuff() {} virtual void end_draw_stuff() {} @@ -186,19 +183,26 @@ static inline conky::display_output_base *display_output() { return nullptr; } -template ::value, T>::type> -inline T dpi_scale(T value) { +template +inline std::enable_if_t::value, T> dpi_scale(T value) { #ifdef BUILD_GUI auto output = display_output(); if (output) { - return output->dpi_scale(value); - } else { - return value; + if constexpr (std::is_integral_v) { + if (value > 0) { + return static_cast( + std::ceil(static_cast(value) * output->get_dpi_scale())); + } else { + return static_cast( + std::floor(static_cast(value) * output->get_dpi_scale())); + } + } else { + return value * output->get_dpi_scale(); + } } -#else /* BUILD_GUI */ - return value; #endif /* BUILD_GUI */ + + return value; } static inline void unset_display_output() { diff --git a/src/display-wayland.cc b/src/display-wayland.cc index 32ebd061..1c9f6ae1 100644 --- a/src/display-wayland.cc +++ b/src/display-wayland.cc @@ -300,7 +300,17 @@ static void output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char *make, const char *model, - int32_t transform) {} + int32_t transform) { + // TODO: Add support for proper output management through: + // - xdg-output-unstable-v1 + // Maybe also support (if XDG protocol not reported): + // - kde-output-management(-v2) + // - wlr-output-management-unstable-v1 + workarea[0] = x; // TODO: use xdg_output.logical_position + workarea[1] = y; + workarea[2] = physical_width; + workarea[3] = physical_height; +} static void output_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {} @@ -913,10 +923,7 @@ void display_output_wayland::move_win(int x, int y) { // window.y = y; // TODO } -template -T display_output_wayland::dpi_scale(T value) { - return value; -} +float display_output_wayland::get_dpi_scale() { return 1.0; } void display_output_wayland::end_draw_stuff() { window_commit_buffer(global_window); diff --git a/src/display-wayland.hh b/src/display-wayland.hh index 6bc60da5..0cc2098b 100644 --- a/src/display-wayland.hh +++ b/src/display-wayland.hh @@ -71,9 +71,7 @@ class display_output_wayland : public display_output_base { virtual void fill_rect(int, int, int, int); virtual void draw_arc(int, int, int, int, int, int); virtual void move_win(int, int); - template ::value, T>::type> - T dpi_scale(T value); + virtual float get_dpi_scale(); virtual void end_draw_stuff(); virtual void clear_text(int); diff --git a/src/display-x11.cc b/src/display-x11.cc index 5e7b0412..47105bf8 100644 --- a/src/display-x11.cc +++ b/src/display-x11.cc @@ -657,7 +657,7 @@ bool handle_event( text_width = window.width - 2 * border_total; text_height = window.height - 2 * border_total; - int mw = surface->dpi_scale(maximum_width.get(*state)); + int mw = maximum_width.get(*state); if (text_width > mw && mw > 0) { text_width = mw; } } @@ -945,18 +945,14 @@ void display_output_x11::move_win(int x, int y) { #endif /* OWN_WINDOW */ } -const size_t PIXELS_PER_INCH = 96; -template -T display_output_x11::dpi_scale(T value) { -#if defined(BUILD_XFT) +const float PIXELS_PER_INCH = 96.0; +float display_output_x11::get_dpi_scale() { +#ifdef BUILD_XFT if (use_xft.get(*state) && xft_dpi > 0) { - return (value * xft_dpi + (value > 0 ? 48 : -48)) / PIXELS_PER_INCH; - } else { - return value; + return static_cast(xft_dpi) / PIXELS_PER_INCH; } -#else /* defined(BUILD_XFT) */ - return value; -#endif /* defined(BUILD_XFT) */ +#endif /* BUILD_XFT */ + return 1.0; } void display_output_x11::end_draw_stuff() { diff --git a/src/display-x11.hh b/src/display-x11.hh index 131a886e..c4837397 100644 --- a/src/display-x11.hh +++ b/src/display-x11.hh @@ -69,9 +69,7 @@ class display_output_x11 : public display_output_base { virtual void fill_rect(int, int, int, int); virtual void draw_arc(int, int, int, int, int, int); virtual void move_win(int, int); - template ::value, T>::type> - T dpi_scale(T value); + virtual float get_dpi_scale(); virtual void end_draw_stuff(); virtual void clear_text(int);