mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-02-04 16:30:27 +01:00
linux: use the number of active DRM devices to determine the system docked status
Not ideal, but better than relying on ACPI.
This commit is contained in:
parent
c507b516d4
commit
17b7aef701
5 changed files with 260 additions and 2 deletions
|
|
@ -35,6 +35,8 @@ libupshared_la_SOURCES = \
|
|||
up-device-wup.h \
|
||||
up-input.c \
|
||||
up-input.h \
|
||||
up-dock.c \
|
||||
up-dock.h \
|
||||
up-backend.c \
|
||||
up-native.c \
|
||||
sysfs-utils.c \
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
|
||||
* Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* Licensed under the GNU General Public License Version 2
|
||||
*
|
||||
|
|
@ -39,6 +39,7 @@
|
|||
#include "up-device-wup.h"
|
||||
#include "up-device-hid.h"
|
||||
#include "up-input.h"
|
||||
#include "up-dock.h"
|
||||
#ifdef HAVE_IDEVICE
|
||||
#include "up-device-idevice.h"
|
||||
#endif /* HAVE_IDEVICE */
|
||||
|
|
@ -55,6 +56,7 @@ struct UpBackendPrivate
|
|||
UpDeviceList *device_list;
|
||||
GUdevClient *gudev_client;
|
||||
UpDeviceList *managed_devices;
|
||||
UpDock *dock;
|
||||
};
|
||||
|
||||
enum {
|
||||
|
|
@ -298,6 +300,7 @@ up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
|
|||
GList *devices;
|
||||
GList *l;
|
||||
guint i;
|
||||
gboolean ret;
|
||||
const gchar *subsystems[] = {"power_supply", "usb", "tty", "input", NULL};
|
||||
|
||||
backend->priv->daemon = g_object_ref (daemon);
|
||||
|
|
@ -318,6 +321,12 @@ up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
|
|||
g_list_free (devices);
|
||||
}
|
||||
|
||||
/* add dock update object */
|
||||
backend->priv->dock = up_dock_new ();
|
||||
ret = up_dock_coldplug (backend->priv->dock, daemon);
|
||||
if (!ret)
|
||||
g_warning ("failed to coldplug dock devices");
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
|||
186
src/linux/up-dock.c
Normal file
186
src/linux/up-dock.c
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <gudev/gudev.h>
|
||||
|
||||
#include "up-daemon.h"
|
||||
#include "up-dock.h"
|
||||
|
||||
struct UpDockPrivate
|
||||
{
|
||||
UpDaemon *daemon;
|
||||
GUdevClient *gudev_client;
|
||||
guint poll_id;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (UpDock, up_dock, G_TYPE_OBJECT)
|
||||
#define UP_DOCK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_DOCK, UpDockPrivate))
|
||||
|
||||
/* the kernel is pretty crap at sending a uevent when the status changes */
|
||||
#define UP_DOCK_POLL_TIMEOUT 30
|
||||
|
||||
/**
|
||||
* up_dock_device_check:
|
||||
**/
|
||||
static gboolean
|
||||
up_dock_device_check (GUdevDevice *d)
|
||||
{
|
||||
const gchar *status;
|
||||
gboolean ret = FALSE;
|
||||
|
||||
/* Get the boolean state from the kernel -- note that ideally
|
||||
* the property value would be "1" or "true" but now it's
|
||||
* set in stone as ABI. Urgh. */
|
||||
status = g_udev_device_get_sysfs_attr (d, "status");
|
||||
if (status == NULL)
|
||||
goto out;
|
||||
ret = (g_strcmp0 (status, "connected") == 0);
|
||||
g_debug ("graphics device %s is %s",
|
||||
g_udev_device_get_sysfs_path (d),
|
||||
ret ? "on" : "off");
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_refresh:
|
||||
**/
|
||||
static gboolean
|
||||
up_dock_refresh (UpDock *dock)
|
||||
{
|
||||
GList *devices;
|
||||
GList *l;
|
||||
GUdevDevice *native;
|
||||
guint count = 0;
|
||||
|
||||
/* the metric we're using here is that a machine is docked when
|
||||
* there is more than one active output */
|
||||
devices = g_udev_client_query_by_subsystem (dock->priv->gudev_client,
|
||||
"drm");
|
||||
for (l = devices; l != NULL; l = l->next) {
|
||||
native = l->data;
|
||||
count += up_dock_device_check (native);
|
||||
}
|
||||
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
|
||||
g_list_free (devices);
|
||||
|
||||
/* update the daemon state, which causes DBus traffic */
|
||||
up_daemon_set_is_docked (dock->priv->daemon, (count > 1));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_poll_cb:
|
||||
**/
|
||||
static gboolean
|
||||
up_dock_poll_cb (UpDock *dock)
|
||||
{
|
||||
g_debug ("Doing the dock refresh");
|
||||
up_dock_refresh (dock);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_coldplug:
|
||||
**/
|
||||
gboolean
|
||||
up_dock_coldplug (UpDock *dock, UpDaemon *daemon)
|
||||
{
|
||||
/* save daemon */
|
||||
dock->priv->daemon = g_object_ref (daemon);
|
||||
dock->priv->poll_id = g_timeout_add_seconds (UP_DOCK_POLL_TIMEOUT,
|
||||
(GSourceFunc) up_dock_poll_cb,
|
||||
dock);
|
||||
return up_dock_refresh (dock);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* up_dock_uevent_signal_handler_cb:
|
||||
**/
|
||||
static void
|
||||
up_dock_uevent_signal_handler_cb (GUdevClient *client, const gchar *action,
|
||||
GUdevDevice *device, gpointer user_data)
|
||||
{
|
||||
UpDock *dock = UP_DOCK (user_data);
|
||||
up_dock_refresh (dock);
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_init:
|
||||
**/
|
||||
static void
|
||||
up_dock_init (UpDock *dock)
|
||||
{
|
||||
const gchar *subsystems[] = { "drm", NULL};
|
||||
dock->priv = UP_DOCK_GET_PRIVATE (dock);
|
||||
dock->priv->gudev_client = g_udev_client_new (subsystems);
|
||||
g_signal_connect (dock->priv->gudev_client, "uevent",
|
||||
G_CALLBACK (up_dock_uevent_signal_handler_cb), dock);
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_finalize:
|
||||
**/
|
||||
static void
|
||||
up_dock_finalize (GObject *object)
|
||||
{
|
||||
UpDock *dock;
|
||||
|
||||
g_return_if_fail (object != NULL);
|
||||
g_return_if_fail (UP_IS_DOCK (object));
|
||||
|
||||
dock = UP_DOCK (object);
|
||||
g_return_if_fail (dock->priv != NULL);
|
||||
|
||||
g_object_unref (dock->priv->gudev_client);
|
||||
if (dock->priv->daemon != NULL)
|
||||
g_object_unref (dock->priv->daemon);
|
||||
if (dock->priv->poll_id != 0)
|
||||
g_source_remove (dock->priv->poll_id);
|
||||
G_OBJECT_CLASS (up_dock_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_class_init:
|
||||
**/
|
||||
static void
|
||||
up_dock_class_init (UpDockClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
object_class->finalize = up_dock_finalize;
|
||||
g_type_class_add_private (klass, sizeof (UpDockPrivate));
|
||||
}
|
||||
|
||||
/**
|
||||
* up_dock_new:
|
||||
**/
|
||||
UpDock *
|
||||
up_dock_new (void)
|
||||
{
|
||||
return g_object_new (UP_TYPE_DOCK, NULL);
|
||||
}
|
||||
|
||||
58
src/linux/up-dock.h
Normal file
58
src/linux/up-dock.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __UP_DOCK_H__
|
||||
#define __UP_DOCK_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
#include "up-daemon.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define UP_TYPE_DOCK (up_dock_get_type ())
|
||||
#define UP_DOCK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), UP_TYPE_DOCK, UpDock))
|
||||
#define UP_DOCK_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), UP_TYPE_DOCK, UpDockClass))
|
||||
#define UP_IS_DOCK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), UP_TYPE_DOCK))
|
||||
#define UP_IS_DOCK_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), UP_TYPE_DOCK))
|
||||
#define UP_DOCK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), UP_TYPE_DOCK, UpDockClass))
|
||||
|
||||
typedef struct UpDockPrivate UpDockPrivate;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GObject parent;
|
||||
UpDockPrivate *priv;
|
||||
} UpDock;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
} UpDockClass;
|
||||
|
||||
GType up_dock_get_type (void);
|
||||
UpDock *up_dock_new (void);
|
||||
gboolean up_dock_coldplug (UpDock *dock,
|
||||
UpDaemon *daemon);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __UP_DOCK_H__ */
|
||||
|
||||
|
|
@ -124,6 +124,7 @@ up_client_print (UpClient *client)
|
|||
gboolean on_low_battery;
|
||||
gboolean lid_is_closed;
|
||||
gboolean lid_is_present;
|
||||
gboolean is_docked;
|
||||
|
||||
g_object_get (client,
|
||||
"daemon-version", &daemon_version,
|
||||
|
|
@ -133,6 +134,7 @@ up_client_print (UpClient *client)
|
|||
"on-low_battery", &on_low_battery,
|
||||
"lid-is-closed", &lid_is_closed,
|
||||
"lid-is-present", &lid_is_present,
|
||||
"is-docked", &is_docked,
|
||||
NULL);
|
||||
|
||||
g_print (" daemon-version: %s\n", daemon_version);
|
||||
|
|
@ -141,7 +143,8 @@ up_client_print (UpClient *client)
|
|||
g_print (" on-battery: %s\n", on_battery ? "yes" : "no");
|
||||
g_print (" on-low-battery: %s\n", on_low_battery ? "yes" : "no");
|
||||
g_print (" lid-is-closed: %s\n", lid_is_closed ? "yes" : "no");
|
||||
g_print (" lid-is-present: %s\n", lid_is_present ? "yes" : "no");
|
||||
g_print (" lid-is-present: %s\n", lid_is_present ? "yes" : "no");
|
||||
g_print (" is-docked: %s\n", is_docked ? "yes" : "no");
|
||||
|
||||
g_free (daemon_version);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue