monitor: Add display name

The display name is being used by the monitor manager to expose to name
to the DBUS API.

It is being rebuilt each time, so instead build the displa yname once
for the monitor and keep it around, with an API to retrieve it, so that
we can reuse it in preparation of xdg-output v2 support.

https://gitlab.gnome.org/GNOME/mutter/issues/645
This commit is contained in:
Olivier Fourdan 2019-06-26 11:24:19 +02:00
parent c3c6668343
commit 6541d49fe7
3 changed files with 120 additions and 5 deletions

View File

@ -2734,7 +2734,7 @@ rebuild_monitors (MetaMonitorManager *manager)
{ {
MetaMonitorNormal *monitor_normal; MetaMonitorNormal *monitor_normal;
monitor_normal = meta_monitor_normal_new (gpu, output); monitor_normal = meta_monitor_normal_new (gpu, manager, output);
manager->monitors = g_list_append (manager->monitors, manager->monitors = g_list_append (manager->monitors,
monitor_normal); monitor_normal);
} }

View File

@ -75,6 +75,8 @@ typedef struct _MetaMonitorPrivate
* the primary one). * the primary one).
*/ */
uint64_t winsys_id; uint64_t winsys_id;
char *display_name;
} MetaMonitorPrivate; } MetaMonitorPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (MetaMonitor, meta_monitor, G_TYPE_OBJECT) G_DEFINE_TYPE_WITH_PRIVATE (MetaMonitor, meta_monitor, G_TYPE_OBJECT)
@ -181,6 +183,99 @@ meta_monitor_generate_spec (MetaMonitor *monitor)
priv->spec = monitor_spec; priv->spec = monitor_spec;
} }
static const double known_diagonals[] = {
12.1,
13.3,
15.6
};
static char *
diagonal_to_str (double d)
{
unsigned int i;
for (i = 0; i < G_N_ELEMENTS (known_diagonals); i++)
{
double delta;
delta = fabs(known_diagonals[i] - d);
if (delta < 0.1)
return g_strdup_printf ("%0.1lf\"", known_diagonals[i]);
}
return g_strdup_printf ("%d\"", (int) (d + 0.5));
}
static char *
meta_monitor_make_display_name (MetaMonitor *monitor,
MetaMonitorManager *monitor_manager)
{
g_autofree char *inches = NULL;
g_autofree char *vendor_name = NULL;
const char *vendor = NULL;
const char *product_name = NULL;
int width_mm;
int height_mm;
meta_monitor_get_physical_dimensions (monitor, &width_mm, &height_mm);
if (meta_monitor_is_laptop_panel (monitor))
return g_strdup (_("Built-in display"));
if (width_mm > 0 && height_mm > 0)
{
if (!meta_monitor_has_aspect_as_size (monitor))
{
double d = sqrt (width_mm * width_mm +
height_mm * height_mm);
inches = diagonal_to_str (d / 25.4);
}
else
{
product_name = meta_monitor_get_product (monitor);
}
}
vendor = meta_monitor_get_vendor (monitor);
if (g_strcmp0 (vendor, "unknown") != 0)
{
vendor_name = meta_monitor_manager_get_vendor_name (monitor_manager,
vendor);
if (!vendor_name)
vendor_name = g_strdup (vendor);
}
else
{
if (inches != NULL)
vendor_name = g_strdup (_("Unknown"));
else
vendor_name = g_strdup (_("Unknown Display"));
}
if (inches != NULL)
{
/**/
return g_strdup_printf (C_("This is a monitor vendor name, followed by a "
"size in inches, like 'Dell 15\"'",
"%s %s"),
vendor_name, inches);
}
else if (product_name != NULL)
{
return g_strdup_printf (C_("This is a monitor vendor name followed by "
"product/model name where size in inches "
"could not be calculated, e.g. Dell U2414H",
"%s %s"),
vendor_name, product_name);
}
else
{
return g_strdup (vendor_name);
}
}
MetaGpu * MetaGpu *
meta_monitor_get_gpu (MetaMonitor *monitor) meta_monitor_get_gpu (MetaMonitor *monitor)
{ {
@ -410,6 +505,7 @@ meta_monitor_finalize (GObject *object)
g_hash_table_destroy (priv->mode_ids); g_hash_table_destroy (priv->mode_ids);
g_list_free_full (priv->modes, (GDestroyNotify) meta_monitor_mode_free); g_list_free_full (priv->modes, (GDestroyNotify) meta_monitor_mode_free);
meta_monitor_spec_free (priv->spec); meta_monitor_spec_free (priv->spec);
g_free (priv->display_name);
G_OBJECT_CLASS (meta_monitor_parent_class)->finalize (object); G_OBJECT_CLASS (meta_monitor_parent_class)->finalize (object);
} }
@ -552,6 +648,7 @@ meta_monitor_normal_generate_modes (MetaMonitorNormal *monitor_normal)
MetaMonitorNormal * MetaMonitorNormal *
meta_monitor_normal_new (MetaGpu *gpu, meta_monitor_normal_new (MetaGpu *gpu,
MetaMonitorManager *monitor_manager,
MetaOutput *output) MetaOutput *output)
{ {
MetaMonitorNormal *monitor_normal; MetaMonitorNormal *monitor_normal;
@ -570,6 +667,9 @@ meta_monitor_normal_new (MetaGpu *gpu,
meta_monitor_normal_generate_modes (monitor_normal); meta_monitor_normal_generate_modes (monitor_normal);
monitor_priv->display_name = meta_monitor_make_display_name (monitor,
monitor_manager);
return monitor_normal; return monitor_normal;
} }
@ -1208,6 +1308,9 @@ meta_monitor_tiled_new (MetaGpu *gpu,
meta_monitor_tiled_generate_modes (monitor_tiled); meta_monitor_tiled_generate_modes (monitor_tiled);
monitor_priv->display_name = meta_monitor_make_display_name (monitor,
monitor_manager);
return monitor_tiled; return monitor_tiled;
} }
@ -1765,3 +1868,12 @@ meta_monitor_mode_foreach_output (MetaMonitor *monitor,
return TRUE; return TRUE;
} }
const char *
meta_monitor_get_display_name (MetaMonitor *monitor)
{
MetaMonitorPrivate *monitor_priv =
meta_monitor_get_instance_private (monitor);
return monitor_priv->display_name;
}

View File

@ -102,6 +102,7 @@ MetaMonitorTiled * meta_monitor_tiled_new (MetaGpu *gpu,
META_EXPORT_TEST META_EXPORT_TEST
MetaMonitorNormal * meta_monitor_normal_new (MetaGpu *gpu, MetaMonitorNormal * meta_monitor_normal_new (MetaGpu *gpu,
MetaMonitorManager *monitor_manager,
MetaOutput *output); MetaOutput *output);
META_EXPORT_TEST META_EXPORT_TEST
@ -280,4 +281,6 @@ int meta_monitor_spec_compare (MetaMonitorSpec *monitor_spec_a,
META_EXPORT_TEST META_EXPORT_TEST
void meta_monitor_spec_free (MetaMonitorSpec *monitor_id); void meta_monitor_spec_free (MetaMonitorSpec *monitor_id);
const char * meta_monitor_get_display_name (MetaMonitor *monitor);
#endif /* META_MONITOR_H */ #endif /* META_MONITOR_H */