display-config: Use G_APPROX_VALUE to compare scale doubles

It's not safe to compare double values directly, use G_APPROX_VALUE with
DBL_EPSILON instead.
This commit is contained in:
Marco Trevisan (Treviño) 2021-05-29 18:38:18 +02:00 committed by Georges Basile Stavracas Neto
parent ca884dc666
commit 89cb1d99cb
2 changed files with 14 additions and 6 deletions

View file

@ -17,6 +17,7 @@
* *
*/ */
#include <float.h>
#include <math.h> #include <math.h>
#include <gio/gio.h> #include <gio/gio.h>
@ -110,8 +111,12 @@ cc_display_mode_dbus_is_supported_scale (CcDisplayMode *pself,
guint i; guint i;
for (i = 0; i < self->supported_scales->len; i++) for (i = 0; i < self->supported_scales->len; i++)
if (g_array_index (self->supported_scales, double, i) == scale) {
double v = g_array_index (self->supported_scales, double, i);
if (G_APPROX_VALUE (v, scale, DBL_EPSILON))
return TRUE; return TRUE;
}
return FALSE; return FALSE;
} }
@ -246,7 +251,7 @@ cc_display_logical_monitor_equal (const CcDisplayLogicalMonitor *m1,
return m1->x == m2->x && return m1->x == m2->x &&
m1->y == m2->y && m1->y == m2->y &&
m1->scale == m2->scale && G_APPROX_VALUE (m1->scale, m2->scale, DBL_EPSILON) &&
m1->rotation == m2->rotation && m1->rotation == m2->rotation &&
m1->primary == m2->primary; m1->primary == m2->primary;
} }
@ -700,7 +705,7 @@ cc_display_monitor_dbus_set_scale (CcDisplayMonitor *pself,
if (!self->logical_monitor) if (!self->logical_monitor)
return; return;
if (self->logical_monitor->scale != scale) if (!G_APPROX_VALUE (self->logical_monitor->scale, scale, DBL_EPSILON))
{ {
self->logical_monitor->scale = scale; self->logical_monitor->scale = scale;

View file

@ -21,6 +21,7 @@
#include <handy.h> #include <handy.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <float.h>
#include <math.h> #include <math.h>
#include "list-box-helper.h" #include "list-box-helper.h"
#include "cc-display-settings.h" #include "cc-display-settings.h"
@ -399,7 +400,8 @@ cc_display_settings_rebuild_ui (CcDisplaySettings *self)
if (!cc_display_config_is_scaled_mode_valid (self->config, if (!cc_display_config_is_scaled_mode_valid (self->config,
current_mode, current_mode,
*scale) && *scale) &&
cc_display_monitor_get_scale (self->selected_output) != *scale) !G_APPROX_VALUE (cc_display_monitor_get_scale (self->selected_output),
*scale, DBL_EPSILON))
continue; continue;
scale_str = make_scale_string (*scale); scale_str = make_scale_string (*scale);
@ -419,7 +421,8 @@ cc_display_settings_rebuild_ui (CcDisplaySettings *self)
G_CALLBACK (on_scale_btn_active_changed_cb), G_CALLBACK (on_scale_btn_active_changed_cb),
self, 0); self, 0);
if (cc_display_monitor_get_scale (self->selected_output) == *scale) if (G_APPROX_VALUE (cc_display_monitor_get_scale (self->selected_output),
*scale, DBL_EPSILON))
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scale_btn), TRUE); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scale_btn), TRUE);
buttons += 1; buttons += 1;