1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-06-10 11:12:21 +00:00

Remove dpi scaling of maximum width (#1877)

* 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 <tin.svagelj@live.com>
This commit is contained in:
Tin Švagelj 2024-04-30 20:21:39 +00:00 committed by GitHub
parent b486263b27
commit e1f301354b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 80 additions and 41 deletions

View File

@ -38,6 +38,7 @@
#include <cmath>
#include <cstdarg>
#include <ctime>
#include <filesystem>
#include <iostream>
#include <memory>
#include <sstream>
@ -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<bool> 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) {

View File

@ -38,6 +38,7 @@
#include <config.h> /* defines */
#include <sys/utsname.h> /* struct uname_s */
#include <csignal>
#include <filesystem>
#include <memory>
#include "colours.h"
@ -346,7 +347,7 @@ extern conky::simple_config_setting<bool> utf8_mode;
extern conky::range_config_setting<unsigned int> 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"

View File

@ -24,6 +24,7 @@
#define DISPLAY_OUTPUT_HH
#include <string.h>
#include <cmath>
#include <limits>
#include <string>
#include <type_traits>
@ -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 <typename T, typename = typename std::enable_if<
std::is_arithmetic<T>::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 <typename T, typename = typename std::enable_if<
std::is_arithmetic<T>::value, T>::type>
inline T dpi_scale(T value) {
template <typename T>
inline std::enable_if_t<std::is_arithmetic<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<T>) {
if (value > 0) {
return static_cast<T>(
std::ceil(static_cast<float>(value) * output->get_dpi_scale()));
} else {
return static_cast<T>(
std::floor(static_cast<float>(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() {

View File

@ -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 <typename T, typename>
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);

View File

@ -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 <typename T, typename = typename std::enable_if<
std::is_arithmetic<T>::value, T>::type>
T dpi_scale(T value);
virtual float get_dpi_scale();
virtual void end_draw_stuff();
virtual void clear_text(int);

View File

@ -657,7 +657,7 @@ bool handle_event<x_event_handler::CONFIGURE>(
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 <typename T, typename>
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<float>(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() {

View File

@ -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 <typename T, typename = typename std::enable_if<
std::is_arithmetic<T>::value, T>::type>
T dpi_scale(T value);
virtual float get_dpi_scale();
virtual void end_draw_stuff();
virtual void clear_text(int);