Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dpi scaling of maximum width #1877

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <cmath>
#include <cstdarg>
#include <ctime>
#include <filesystem>
#include <iostream>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -275,7 +276,7 @@ int text_width = 1,
struct information info;

/* path to config file */
std::string current_config;
std::filesystem::path current_config;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice


/* set to 1 if you want all text to be in uppercase */
static conky::simple_config_setting<bool> stuff_in_uppercase("uppercase", false,
Expand Down Expand Up @@ -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; }
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Comment on lines +1965 to +1997
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding $HOME/.config/conky and config parent directory to lua path before loading the script.


try {
#ifdef BUILD_BUILTIN_CONFIG
if (current_config == builtin_config_magic) {
Expand Down
3 changes: 2 additions & 1 deletion src/conky.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <config.h> /* defines */
#include <sys/utsname.h> /* struct uname_s */
#include <csignal>
#include <filesystem>
#include <memory>

#include "colours.h"
Expand Down Expand Up @@ -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"

Expand Down
30 changes: 17 additions & 13 deletions src/display-output.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define DISPLAY_OUTPUT_HH

#include <string.h>
#include <cmath>
#include <limits>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -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() {}
Expand Down Expand Up @@ -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() {
Expand Down
17 changes: 12 additions & 5 deletions src/display-wayland.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +304 to +312
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the workarea change.

}

static void output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
int32_t width, int32_t height, int32_t refresh) {}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/display-wayland.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 7 additions & 11 deletions src/display-x11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down Expand Up @@ -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;
Copy link
Collaborator Author

@Caellian Caellian Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe (value > 0 ? 48 : -48) was rounding the result for int values before, so I replaced it with ceil and floor in new code. PIXELS_PER_INCH is 96.

} 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() {
Expand Down
4 changes: 1 addition & 3 deletions src/display-x11.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down