summaryrefslogtreecommitdiff
path: root/src/linux/up-dock.c
blob: 59475f7d5b4d1cd80155f777bd3ae4f9dff76403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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);
}