Skip to content

Commit

Permalink
Add some api for the display applet.
Browse files Browse the repository at this point in the history
display: Add helper functions for translating between logical and
xinerama monitor numbers.

Monitor 0 is traditionally the primary monitor. Logical monitors
don't work this way - if you change the primary, its number stays
the same. Fortunately the underlying x number is still available.

This may eventually be complicated by monitors that allow multiple
logical inputs (tiling), but I have none to test with.
  • Loading branch information
mtwebster committed Jun 9, 2022
1 parent 2cabdbd commit 6a75174
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
147 changes: 147 additions & 0 deletions src/backends/meta-monitor-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -3564,3 +3564,150 @@ meta_monitor_manager_get_vendor_name (MetaMonitorManager *manager,

return gnome_pnp_ids_get_pnp_id (manager->pnp_ids, vendor);
}

#define META_MONITOR_TRANSFORM_INVALID -1

MetaMonitorTransform
xrandr_to_monitor_transform (MetaXrandrRotation rotation)
{
switch (rotation)
{
case META_XRANDR_ROTATION_NORMAL:
return META_MONITOR_TRANSFORM_NORMAL;
case META_XRANDR_ROTATION_LEFT:
return META_MONITOR_TRANSFORM_90;
case META_XRANDR_ROTATION_FLIPPED:
return META_MONITOR_TRANSFORM_180;
case META_XRANDR_ROTATION_RIGHT:
return META_MONITOR_TRANSFORM_270;

default:
g_return_val_if_reached (META_MONITOR_TRANSFORM_INVALID);
}
}

gboolean
meta_monitor_manager_can_apply_rotation (MetaMonitorManager *manager,
MetaXrandrRotation rotation)
{
gboolean ret = FALSE;

if (!meta_monitor_manager_get_is_builtin_display_on (manager))
return FALSE;

MetaOrientation orientation = xrandr_to_monitor_transform (rotation);

if (orientation == META_MONITOR_TRANSFORM_INVALID)
{
meta_warning ("Invalid orientation requested.");
return ret;
}

MetaMonitorsConfig *config = meta_monitor_config_manager_create_for_orientation (manager->config_manager, orientation);

ret = config != NULL;

g_clear_object (&config);

if (ret)
return TRUE;

MetaXrandrRotation current_rotation;
meta_monitor_manager_get_current_rotation (manager, &current_rotation);

if (current_rotation == rotation)
return TRUE;

return FALSE;
}

gboolean
meta_monitor_manager_apply_temporary_rotation (MetaMonitorManager *manager,
MetaXrandrRotation rotation)
{
MetaOrientation orientation = xrandr_to_monitor_transform (rotation);

if (orientation == META_MONITOR_TRANSFORM_INVALID)
{
meta_warning ("Invalid orientation requested.");
return FALSE;
}

GError *error = NULL;
MetaMonitorsConfig *config = meta_monitor_config_manager_create_for_orientation (manager->config_manager, orientation);

if (!config)
{
meta_warning ("Could not create config for rotation.");
return FALSE;
}

if (!meta_monitor_manager_apply_monitors_config (manager,
config,
META_MONITORS_CONFIG_METHOD_TEMPORARY,
&error))
{
g_warning ("Failed to use rotate monitor configuration: %s",
error->message);
g_error_free (error);
g_object_unref (config);
return FALSE;
}

g_object_unref (config);

return TRUE;
}

MetaXrandrRotation
monitor_transform_to_xrandr_rotation (MetaMonitorTransform transform)
{
switch (transform)
{
case META_MONITOR_TRANSFORM_NORMAL:
return META_XRANDR_ROTATION_NORMAL;
case META_MONITOR_TRANSFORM_90:
return META_XRANDR_ROTATION_LEFT;
case META_MONITOR_TRANSFORM_180:
return META_XRANDR_ROTATION_FLIPPED;
case META_MONITOR_TRANSFORM_270:
return META_XRANDR_ROTATION_RIGHT;
default:
g_return_val_if_reached (META_XRANDR_ROTATION_NORMAL);
}
}

/**
* meta_monitor_manager_get_current_rotation:
* @manager: A #MetaMonitorManager object
* @rotation: (out): The current #MetaXrandrRotation applied to the laptop panel.
*
* Returns %TRUE if there is a laptop panel and its current rotation
* can be determined, otherwise %FALSE
*/
gboolean
meta_monitor_manager_get_current_rotation (MetaMonitorManager *manager,
MetaXrandrRotation *rotation)
{
MetaMonitor *monitor;
MetaLogicalMonitor *logical_monitor;

*rotation = META_XRANDR_ROTATION_NORMAL;
monitor = meta_monitor_manager_get_laptop_panel (manager);

if (monitor == NULL || !meta_monitor_is_active (monitor))
{
return FALSE;
}

logical_monitor = meta_monitor_get_logical_monitor (monitor);

if (logical_monitor != NULL)
{
MetaMonitorTransform transform = meta_logical_monitor_get_transform (logical_monitor);
*rotation = monitor_transform_to_xrandr_rotation (transform);
return TRUE;
}

return FALSE;
}
36 changes: 36 additions & 0 deletions src/core/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -3925,4 +3925,40 @@ gboolean
meta_display_get_desklets_above (MetaDisplay *display)
{
return display->desklets_above;
}

gint
meta_display_xinerama_index_to_logical_index (MetaDisplay *display,
gint x_index)
{
MetaLogicalMonitor *monitor;

monitor = meta_x11_display_xinerama_index_to_logical_monitor (display->x11_display, x_index);

if (monitor == NULL)
{
return 0;
}

return monitor->number;
}

gint
meta_display_logical_index_to_xinerama_index (MetaDisplay *display,
gint log_index)
{
MetaBackend *backend = meta_get_backend ();
MetaMonitorManager *manager;
MetaLogicalMonitor *monitor;

manager = meta_backend_get_monitor_manager (backend);
monitor = meta_monitor_manager_get_logical_monitor_from_number (manager, log_index);

if (monitor == NULL)
{
return 0;
}

return meta_x11_display_logical_monitor_to_xinerama_index (display->x11_display, monitor);

}
8 changes: 8 additions & 0 deletions src/meta/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ META_EXPORT
void meta_display_focus_default_window (MetaDisplay *display,
guint32 timestamp);

META_EXPORT
gint meta_display_xinerama_index_to_logical_index (MetaDisplay *display,
gint x_index);

META_EXPORT
gint meta_display_logical_index_to_xinerama_index (MetaDisplay *display,
gint log_index);

/**
* MetaDisplayCorner:
* @META_DISPLAY_TOPLEFT: top-left corner
Expand Down
16 changes: 16 additions & 0 deletions src/meta/meta-monitor-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ typedef enum
META_MONITOR_SWITCH_CONFIG_UNKNOWN,
} MetaMonitorSwitchConfigType;

typedef enum
{
META_XRANDR_ROTATION_NORMAL,
META_XRANDR_ROTATION_LEFT,
META_XRANDR_ROTATION_FLIPPED,
META_XRANDR_ROTATION_RIGHT
} MetaXrandrRotation;

typedef struct _MetaMonitorManagerClass MetaMonitorManagerClass;
typedef struct _MetaMonitorManager MetaMonitorManager;

Expand Down Expand Up @@ -62,4 +70,12 @@ MetaMonitorSwitchConfigType meta_monitor_manager_get_switch_config (MetaMonitorM
META_EXPORT
gint meta_monitor_manager_get_display_configuration_timeout (void);

META_EXPORT
gboolean meta_monitor_manager_can_apply_rotation (MetaMonitorManager *manager, MetaXrandrRotation rotation);

META_EXPORT
gboolean meta_monitor_manager_apply_temporary_rotation (MetaMonitorManager *manager, MetaXrandrRotation rotation);

META_EXPORT
gboolean meta_monitor_manager_get_current_rotation (MetaMonitorManager *manager, MetaXrandrRotation *rotation);
#endif /* META_MONITOR_MANAGER_H */

0 comments on commit 6a75174

Please sign in to comment.