From 405c2c9286a719481de59bda030dd1ee99a5f994 Mon Sep 17 00:00:00 2001 From: o9000 Date: Wed, 17 Jan 2018 03:51:32 +0000 Subject: [PATCH] Compute monitor DPI (issue #656) --- src/util/server.c | 36 ++++++++++++++++++++++++++++++++---- src/util/server.h | 1 + tint2.files | 2 ++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/util/server.c b/src/util/server.c index 9221965..5d7d296 100644 --- a/src/util/server.c +++ b/src/util/server.c @@ -19,11 +19,11 @@ **************************************************************************/ #include -#include #include +#include -#include #include +#include #include #include #include @@ -311,6 +311,21 @@ void sort_monitors() qsort(server.monitors, server.num_monitors, sizeof(Monitor), compare_monitor_pos); } +int compute_dpi(XRRCrtcInfo *crtc, XRROutputInfo *output) +{ + int width = output->mm_width; + int height = output->mm_height; + int x_res = crtc->width; + int y_res = crtc->height; + + if (width > 0 && height > 0) { + int dpi_x = x_res / width * 25.4; + int dpi_y = y_res / height * 25.4; + return MAX(dpi_x, dpi_y); + } + return 0; +} + void get_monitors() { if (XineramaIsActive(server.display)) { @@ -339,12 +354,22 @@ void get_monitors() server.monitors[i_monitor].width = crtc_info->width; server.monitors[i_monitor].height = crtc_info->height; server.monitors[i_monitor].names = calloc((crtc_info->noutput + 1), sizeof(gchar *)); + server.monitors[i_monitor].dpi = 96; for (int j = 0; j < crtc_info->noutput; ++j) { XRROutputInfo *output_info = XRRGetOutputInfo(server.display, res, crtc_info->outputs[j]); - fprintf(stderr, "tint2: xRandr: Linking output %s with crtc %d\n", output_info->name, i); server.monitors[i_monitor].names[j] = g_strdup(output_info->name); XRRFreeOutputInfo(output_info); server.monitors[i_monitor].primary = crtc_info->outputs[j] == primary_output; + int dpi = compute_dpi(crtc_info, output_info); + if (dpi) + server.monitors[i_monitor].dpi = dpi; + fprintf(stderr, + "tint2: xRandr: Linking output %s with crtc %d, resolution %dx%d, DPI %d\n", + output_info->name, + i, + server.monitors[i_monitor].width, + server.monitors[i_monitor].height, + server.monitors[i_monitor].dpi); } server.monitors[i_monitor].names[crtc_info->noutput] = NULL; XRRFreeCrtcInfo(crtc_info); @@ -357,6 +382,7 @@ void get_monitors() server.monitors[i].width = info[i].width; server.monitors[i].height = info[i].height; server.monitors[i].names = NULL; + server.monitors[i].dpi = 96; } } @@ -393,6 +419,7 @@ void get_monitors() server.monitors[0].width = DisplayWidth(server.display, server.screen); server.monitors[0].height = DisplayHeight(server.display, server.screen); server.monitors[0].names = 0; + server.monitors[0].dpi = 96; } } @@ -523,7 +550,8 @@ int get_current_desktop() // fprintf(stderr, "tint2: \n"); // fprintf(stderr, "tint2: Work area size: %d x %d\n", work_area_width, work_area_height); // fprintf(stderr, "tint2: Viewport pos: %d x %d\n", viewport_x, viewport_y); - // fprintf(stderr, "tint2: Viewport i: %d\n", (viewport_y / work_area_height) * ncols + viewport_x / work_area_width); + // fprintf(stderr, "tint2: Viewport i: %d\n", (viewport_y / work_area_height) * ncols + viewport_x / + //work_area_width); int result = (viewport_y / work_area_height) * ncols + viewport_x / work_area_width; return MAX(0, MIN(server.num_desktops - 1, result)); diff --git a/src/util/server.h b/src/util/server.h index d2a3f0e..079c23e 100644 --- a/src/util/server.h +++ b/src/util/server.h @@ -107,6 +107,7 @@ typedef struct Monitor { int y; int width; int height; + int dpi; gboolean primary; gchar **names; } Monitor; diff --git a/tint2.files b/tint2.files index 008f763..92f8c77 100644 --- a/tint2.files +++ b/tint2.files @@ -61,6 +61,8 @@ src/util/blur.c src/util/blur.h src/util/common.c src/util/common.h +src/util/server.c +src/util/server.h src/util/strlcat.c src/util/strlcat.h src/util/timer.c